]> git.proxmox.com Git - mirror_qemu.git/blame - target-arm/arm-semi.c
linux-user: Remove #if 0'd cpu_get_real_ticks() definition
[mirror_qemu.git] / target-arm / arm-semi.c
CommitLineData
a4f81979
FB
1/*
2 * Arm "Angel" semihosting syscalls
5fafdf24 3 *
8e71621f
PB
4 * Copyright (c) 2005, 2007 CodeSourcery.
5 * Written by Paul Brook.
a4f81979
FB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
8167ee88 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
a4f81979
FB
19 */
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <time.h>
28
8e71621f
PB
29#include "cpu.h"
30#ifdef CONFIG_USER_ONLY
a4f81979
FB
31#include "qemu.h"
32
33#define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
8e71621f 34#else
87ecb68b 35#include "qemu-common.h"
87ecb68b 36#include "gdbstub.h"
1c1b40c1 37#include "hw/arm-misc.h"
8e71621f 38#endif
a4f81979 39
3881725c
SW
40#define TARGET_SYS_OPEN 0x01
41#define TARGET_SYS_CLOSE 0x02
42#define TARGET_SYS_WRITEC 0x03
43#define TARGET_SYS_WRITE0 0x04
44#define TARGET_SYS_WRITE 0x05
45#define TARGET_SYS_READ 0x06
46#define TARGET_SYS_READC 0x07
47#define TARGET_SYS_ISTTY 0x09
48#define TARGET_SYS_SEEK 0x0a
49#define TARGET_SYS_FLEN 0x0c
50#define TARGET_SYS_TMPNAM 0x0d
51#define TARGET_SYS_REMOVE 0x0e
52#define TARGET_SYS_RENAME 0x0f
53#define TARGET_SYS_CLOCK 0x10
54#define TARGET_SYS_TIME 0x11
55#define TARGET_SYS_SYSTEM 0x12
56#define TARGET_SYS_ERRNO 0x13
57#define TARGET_SYS_GET_CMDLINE 0x15
58#define TARGET_SYS_HEAPINFO 0x16
59#define TARGET_SYS_EXIT 0x18
a4f81979
FB
60
61#ifndef O_BINARY
62#define O_BINARY 0
63#endif
64
a2d1ebaf
PB
65#define GDB_O_RDONLY 0x000
66#define GDB_O_WRONLY 0x001
67#define GDB_O_RDWR 0x002
68#define GDB_O_APPEND 0x008
69#define GDB_O_CREAT 0x200
70#define GDB_O_TRUNC 0x400
71#define GDB_O_BINARY 0
72
73static int gdb_open_modeflags[12] = {
74 GDB_O_RDONLY,
75 GDB_O_RDONLY | GDB_O_BINARY,
76 GDB_O_RDWR,
77 GDB_O_RDWR | GDB_O_BINARY,
78 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
79 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
80 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
81 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
82 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
83 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
84 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
85 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
86};
87
88static int open_modeflags[12] = {
a4f81979
FB
89 O_RDONLY,
90 O_RDONLY | O_BINARY,
91 O_RDWR,
92 O_RDWR | O_BINARY,
93 O_WRONLY | O_CREAT | O_TRUNC,
94 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
95 O_RDWR | O_CREAT | O_TRUNC,
96 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
97 O_WRONLY | O_CREAT | O_APPEND,
98 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
99 O_RDWR | O_CREAT | O_APPEND,
100 O_RDWR | O_CREAT | O_APPEND | O_BINARY
101};
102
8e71621f 103#ifdef CONFIG_USER_ONLY
a4f81979
FB
104static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
105{
8e71621f
PB
106 if (code == (uint32_t)-1)
107 ts->swi_errno = errno;
108 return code;
109}
110#else
81926f47 111static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code)
8e71621f
PB
112{
113 return code;
114}
115
a87295e8 116#include "softmmu-semi.h"
8e71621f 117#endif
a4f81979 118
a2d1ebaf
PB
119static target_ulong arm_semi_syscall_len;
120
33d9cc8a
PB
121#if !defined(CONFIG_USER_ONLY)
122static target_ulong syscall_err;
123#endif
124
81926f47 125static void arm_semi_cb(CPUARMState *env, target_ulong ret, target_ulong err)
a2d1ebaf
PB
126{
127#ifdef CONFIG_USER_ONLY
128 TaskState *ts = env->opaque;
129#endif
33d9cc8a 130
a2d1ebaf
PB
131 if (ret == (target_ulong)-1) {
132#ifdef CONFIG_USER_ONLY
133 ts->swi_errno = err;
33d9cc8a
PB
134#else
135 syscall_err = err;
a2d1ebaf
PB
136#endif
137 env->regs[0] = ret;
138 } else {
139 /* Fixup syscalls that use nonstardard return conventions. */
140 switch (env->regs[0]) {
3881725c
SW
141 case TARGET_SYS_WRITE:
142 case TARGET_SYS_READ:
a2d1ebaf
PB
143 env->regs[0] = arm_semi_syscall_len - ret;
144 break;
3881725c 145 case TARGET_SYS_SEEK:
a2d1ebaf
PB
146 env->regs[0] = 0;
147 break;
148 default:
149 env->regs[0] = ret;
150 break;
151 }
152 }
153}
154
81926f47 155static void arm_semi_flen_cb(CPUARMState *env, target_ulong ret, target_ulong err)
33d9cc8a
PB
156{
157 /* The size is always stored in big-endian order, extract
158 the value. We assume the size always fit in 32 bits. */
159 uint32_t size;
160 cpu_memory_rw_debug(env, env->regs[13]-64+32, (uint8_t *)&size, 4, 0);
161 env->regs[0] = be32_to_cpu(size);
162#ifdef CONFIG_USER_ONLY
163 ((TaskState *)env->opaque)->swi_errno = err;
164#else
165 syscall_err = err;
166#endif
167}
168
2f619698
FB
169#define ARG(n) \
170({ \
171 target_ulong __arg; \
172 /* FIXME - handle get_user() failure */ \
173 get_user_ual(__arg, args + (n) * 4); \
174 __arg; \
175})
176#define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
81926f47 177uint32_t do_arm_semihosting(CPUARMState *env)
a4f81979 178{
53a5960a 179 target_ulong args;
a4f81979
FB
180 char * s;
181 int nr;
182 uint32_t ret;
8e71621f
PB
183 uint32_t len;
184#ifdef CONFIG_USER_ONLY
a4f81979 185 TaskState *ts = env->opaque;
8e71621f 186#else
81926f47 187 CPUARMState *ts = env;
8e71621f 188#endif
a4f81979
FB
189
190 nr = env->regs[0];
53a5960a 191 args = env->regs[1];
a4f81979 192 switch (nr) {
3881725c 193 case TARGET_SYS_OPEN:
579a97f7
FB
194 if (!(s = lock_user_string(ARG(0))))
195 /* FIXME - should this error code be -TARGET_EFAULT ? */
196 return (uint32_t)-1;
396bef4b
JM
197 if (ARG(1) >= 12) {
198 unlock_user(s, ARG(0), 0);
579a97f7 199 return (uint32_t)-1;
396bef4b 200 }
a4f81979 201 if (strcmp(s, ":tt") == 0) {
396bef4b
JM
202 int result_fileno = ARG(1) < 4 ? STDIN_FILENO : STDOUT_FILENO;
203 unlock_user(s, ARG(0), 0);
204 return result_fileno;
a4f81979 205 }
a2d1ebaf 206 if (use_gdb_syscalls()) {
5fafdf24 207 gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0),
33d9cc8a 208 (int)ARG(2)+1, gdb_open_modeflags[ARG(1)]);
396bef4b 209 ret = env->regs[0];
a2d1ebaf
PB
210 } else {
211 ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644));
212 }
8e71621f
PB
213 unlock_user(s, ARG(0), 0);
214 return ret;
3881725c 215 case TARGET_SYS_CLOSE:
a2d1ebaf
PB
216 if (use_gdb_syscalls()) {
217 gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0));
218 return env->regs[0];
219 } else {
220 return set_swi_errno(ts, close(ARG(0)));
221 }
3881725c 222 case TARGET_SYS_WRITEC:
53a5960a 223 {
2f619698
FB
224 char c;
225
226 if (get_user_u8(c, args))
227 /* FIXME - should this error code be -TARGET_EFAULT ? */
228 return (uint32_t)-1;
53a5960a 229 /* Write to debug console. stderr is near enough. */
a2d1ebaf
PB
230 if (use_gdb_syscalls()) {
231 gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args);
232 return env->regs[0];
233 } else {
234 return write(STDERR_FILENO, &c, 1);
235 }
53a5960a 236 }
3881725c 237 case TARGET_SYS_WRITE0:
579a97f7
FB
238 if (!(s = lock_user_string(args)))
239 /* FIXME - should this error code be -TARGET_EFAULT ? */
240 return (uint32_t)-1;
a2d1ebaf
PB
241 len = strlen(s);
242 if (use_gdb_syscalls()) {
243 gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len);
244 ret = env->regs[0];
245 } else {
246 ret = write(STDERR_FILENO, s, len);
247 }
53a5960a
PB
248 unlock_user(s, args, 0);
249 return ret;
3881725c 250 case TARGET_SYS_WRITE:
8e71621f 251 len = ARG(2);
a2d1ebaf
PB
252 if (use_gdb_syscalls()) {
253 arm_semi_syscall_len = len;
254 gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len);
255 return env->regs[0];
256 } else {
579a97f7
FB
257 if (!(s = lock_user(VERIFY_READ, ARG(1), len, 1)))
258 /* FIXME - should this error code be -TARGET_EFAULT ? */
259 return (uint32_t)-1;
a2d1ebaf
PB
260 ret = set_swi_errno(ts, write(ARG(0), s, len));
261 unlock_user(s, ARG(1), 0);
262 if (ret == (uint32_t)-1)
263 return -1;
264 return len - ret;
265 }
3881725c 266 case TARGET_SYS_READ:
8e71621f 267 len = ARG(2);
a2d1ebaf
PB
268 if (use_gdb_syscalls()) {
269 arm_semi_syscall_len = len;
270 gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len);
271 return env->regs[0];
272 } else {
579a97f7
FB
273 if (!(s = lock_user(VERIFY_WRITE, ARG(1), len, 0)))
274 /* FIXME - should this error code be -TARGET_EFAULT ? */
275 return (uint32_t)-1;
a2d1ebaf
PB
276 do
277 ret = set_swi_errno(ts, read(ARG(0), s, len));
278 while (ret == -1 && errno == EINTR);
279 unlock_user(s, ARG(1), len);
280 if (ret == (uint32_t)-1)
281 return -1;
282 return len - ret;
283 }
3881725c 284 case TARGET_SYS_READC:
b90372ad 285 /* XXX: Read from debug console. Not implemented. */
a4f81979 286 return 0;
3881725c 287 case TARGET_SYS_ISTTY:
a2d1ebaf
PB
288 if (use_gdb_syscalls()) {
289 gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0));
290 return env->regs[0];
291 } else {
292 return isatty(ARG(0));
293 }
3881725c 294 case TARGET_SYS_SEEK:
a2d1ebaf 295 if (use_gdb_syscalls()) {
33d9cc8a 296 gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", ARG(0), ARG(1));
a2d1ebaf
PB
297 return env->regs[0];
298 } else {
299 ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
300 if (ret == (uint32_t)-1)
301 return -1;
302 return 0;
303 }
3881725c 304 case TARGET_SYS_FLEN:
a2d1ebaf 305 if (use_gdb_syscalls()) {
5fafdf24 306 gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x",
33d9cc8a
PB
307 ARG(0), env->regs[13]-64);
308 return env->regs[0];
a2d1ebaf 309 } else {
a4f81979
FB
310 struct stat buf;
311 ret = set_swi_errno(ts, fstat(ARG(0), &buf));
312 if (ret == (uint32_t)-1)
313 return -1;
314 return buf.st_size;
315 }
3881725c 316 case TARGET_SYS_TMPNAM:
a4f81979
FB
317 /* XXX: Not implemented. */
318 return -1;
3881725c 319 case TARGET_SYS_REMOVE:
a2d1ebaf 320 if (use_gdb_syscalls()) {
33d9cc8a 321 gdb_do_syscall(arm_semi_cb, "unlink,%s", ARG(0), (int)ARG(1)+1);
a2d1ebaf
PB
322 ret = env->regs[0];
323 } else {
579a97f7
FB
324 if (!(s = lock_user_string(ARG(0))))
325 /* FIXME - should this error code be -TARGET_EFAULT ? */
326 return (uint32_t)-1;
a2d1ebaf
PB
327 ret = set_swi_errno(ts, remove(s));
328 unlock_user(s, ARG(0), 0);
329 }
8e71621f 330 return ret;
3881725c 331 case TARGET_SYS_RENAME:
a2d1ebaf
PB
332 if (use_gdb_syscalls()) {
333 gdb_do_syscall(arm_semi_cb, "rename,%s,%s",
33d9cc8a 334 ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1);
a2d1ebaf
PB
335 return env->regs[0];
336 } else {
8e71621f
PB
337 char *s2;
338 s = lock_user_string(ARG(0));
339 s2 = lock_user_string(ARG(2));
579a97f7
FB
340 if (!s || !s2)
341 /* FIXME - should this error code be -TARGET_EFAULT ? */
342 ret = (uint32_t)-1;
343 else
344 ret = set_swi_errno(ts, rename(s, s2));
345 if (s2)
346 unlock_user(s2, ARG(2), 0);
347 if (s)
348 unlock_user(s, ARG(0), 0);
8e71621f
PB
349 return ret;
350 }
3881725c 351 case TARGET_SYS_CLOCK:
a4f81979 352 return clock() / (CLOCKS_PER_SEC / 100);
3881725c 353 case TARGET_SYS_TIME:
a4f81979 354 return set_swi_errno(ts, time(NULL));
3881725c 355 case TARGET_SYS_SYSTEM:
a2d1ebaf 356 if (use_gdb_syscalls()) {
33d9cc8a 357 gdb_do_syscall(arm_semi_cb, "system,%s", ARG(0), (int)ARG(1)+1);
a2d1ebaf
PB
358 return env->regs[0];
359 } else {
579a97f7
FB
360 if (!(s = lock_user_string(ARG(0))))
361 /* FIXME - should this error code be -TARGET_EFAULT ? */
362 return (uint32_t)-1;
a2d1ebaf
PB
363 ret = set_swi_errno(ts, system(s));
364 unlock_user(s, ARG(0), 0);
a982b531 365 return ret;
a2d1ebaf 366 }
3881725c 367 case TARGET_SYS_ERRNO:
8e71621f 368#ifdef CONFIG_USER_ONLY
a4f81979 369 return ts->swi_errno;
8e71621f 370#else
33d9cc8a 371 return syscall_err;
8e71621f 372#endif
3881725c 373 case TARGET_SYS_GET_CMDLINE:
38d0662a 374 {
1c1b40c1
CV
375 /* Build a command-line from the original argv.
376 *
377 * The inputs are:
378 * * ARG(0), pointer to a buffer of at least the size
379 * specified in ARG(1).
380 * * ARG(1), size of the buffer pointed to by ARG(0) in
381 * bytes.
382 *
383 * The outputs are:
384 * * ARG(0), pointer to null-terminated string of the
385 * command line.
386 * * ARG(1), length of the string pointed to by ARG(0).
387 */
579a97f7 388
1c1b40c1
CV
389 char *output_buffer;
390 size_t input_size = ARG(1);
391 size_t output_size;
392 int status = 0;
2e8785ac 393
1c1b40c1
CV
394 /* Compute the size of the output string. */
395#if !defined(CONFIG_USER_ONLY)
396 output_size = strlen(ts->boot_info->kernel_filename)
397 + 1 /* Separating space. */
398 + strlen(ts->boot_info->kernel_cmdline)
399 + 1; /* Terminating null byte. */
400#else
401 unsigned int i;
38d0662a 402
1c1b40c1
CV
403 output_size = ts->info->arg_end - ts->info->arg_start;
404 if (!output_size) {
2e8785ac
WS
405 /* We special-case the "empty command line" case (argc==0).
406 Just provide the terminating 0. */
1c1b40c1
CV
407 output_size = 1;
408 }
409#endif
38d0662a 410
1c1b40c1
CV
411 if (output_size > input_size) {
412 /* Not enough space to store command-line arguments. */
413 return -1;
38d0662a 414 }
38d0662a 415
1c1b40c1
CV
416 /* Adjust the command-line length. */
417 SET_ARG(1, output_size - 1);
38d0662a 418
1c1b40c1
CV
419 /* Lock the buffer on the ARM side. */
420 output_buffer = lock_user(VERIFY_WRITE, ARG(0), output_size, 0);
421 if (!output_buffer) {
422 return -1;
423 }
38d0662a 424
1c1b40c1
CV
425 /* Copy the command-line arguments. */
426#if !defined(CONFIG_USER_ONLY)
427 pstrcpy(output_buffer, output_size, ts->boot_info->kernel_filename);
428 pstrcat(output_buffer, output_size, " ");
429 pstrcat(output_buffer, output_size, ts->boot_info->kernel_cmdline);
430#else
431 if (output_size == 1) {
432 /* Empty command-line. */
433 output_buffer[0] = '\0';
434 goto out;
435 }
2e8785ac 436
1c1b40c1
CV
437 if (copy_from_user(output_buffer, ts->info->arg_start,
438 output_size)) {
439 status = -1;
440 goto out;
2e8785ac
WS
441 }
442
1c1b40c1
CV
443 /* Separate arguments by white spaces. */
444 for (i = 0; i < output_size - 1; i++) {
445 if (output_buffer[i] == 0) {
446 output_buffer[i] = ' ';
447 }
448 }
449 out:
450#endif
451 /* Unlock the buffer on the ARM side. */
452 unlock_user(output_buffer, ARG(0), output_size);
2e8785ac 453
1c1b40c1 454 return status;
38d0662a 455 }
3881725c 456 case TARGET_SYS_HEAPINFO:
a4f81979
FB
457 {
458 uint32_t *ptr;
459 uint32_t limit;
460
8e71621f
PB
461#ifdef CONFIG_USER_ONLY
462 /* Some C libraries assume the heap immediately follows .bss, so
a4f81979
FB
463 allocate it using sbrk. */
464 if (!ts->heap_limit) {
206ae74a 465 abi_ulong ret;
a4f81979 466
53a5960a 467 ts->heap_base = do_brk(0);
a4f81979
FB
468 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
469 /* Try a big heap, and reduce the size if that fails. */
470 for (;;) {
53a5960a 471 ret = do_brk(limit);
206ae74a 472 if (ret >= limit) {
a4f81979 473 break;
206ae74a 474 }
a4f81979
FB
475 limit = (ts->heap_base >> 1) + (limit >> 1);
476 }
477 ts->heap_limit = limit;
478 }
3b46e624 479
579a97f7
FB
480 if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
481 /* FIXME - should this error code be -TARGET_EFAULT ? */
482 return (uint32_t)-1;
a4f81979
FB
483 ptr[0] = tswap32(ts->heap_base);
484 ptr[1] = tswap32(ts->heap_limit);
485 ptr[2] = tswap32(ts->stack_base);
486 ptr[3] = tswap32(0); /* Stack limit. */
8e71621f
PB
487 unlock_user(ptr, ARG(0), 16);
488#else
489 limit = ram_size;
579a97f7
FB
490 if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
491 /* FIXME - should this error code be -TARGET_EFAULT ? */
492 return (uint32_t)-1;
8e71621f
PB
493 /* TODO: Make this use the limit of the loaded application. */
494 ptr[0] = tswap32(limit / 2);
495 ptr[1] = tswap32(limit);
496 ptr[2] = tswap32(limit); /* Stack base */
497 ptr[3] = tswap32(0); /* Stack limit. */
498 unlock_user(ptr, ARG(0), 16);
499#endif
a4f81979
FB
500 return 0;
501 }
3881725c 502 case TARGET_SYS_EXIT:
0e1c9c54 503 gdb_exit(env, 0);
a4f81979
FB
504 exit(0);
505 default:
506 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
507 cpu_dump_state(env, stderr, fprintf, 0);
508 abort();
509 }
510}