2 * Arm "Angel" semihosting syscalls
4 * Copyright (c) 2005, 2007 CodeSourcery.
5 * Written by Paul Brook.
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.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
24 #include "exec/semihost.h"
25 #ifdef CONFIG_USER_ONLY
28 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
30 #include "qemu-common.h"
31 #include "exec/gdbstub.h"
32 #include "hw/arm/arm.h"
33 #include "qemu/cutils.h"
36 #define TARGET_SYS_OPEN 0x01
37 #define TARGET_SYS_CLOSE 0x02
38 #define TARGET_SYS_WRITEC 0x03
39 #define TARGET_SYS_WRITE0 0x04
40 #define TARGET_SYS_WRITE 0x05
41 #define TARGET_SYS_READ 0x06
42 #define TARGET_SYS_READC 0x07
43 #define TARGET_SYS_ISTTY 0x09
44 #define TARGET_SYS_SEEK 0x0a
45 #define TARGET_SYS_FLEN 0x0c
46 #define TARGET_SYS_TMPNAM 0x0d
47 #define TARGET_SYS_REMOVE 0x0e
48 #define TARGET_SYS_RENAME 0x0f
49 #define TARGET_SYS_CLOCK 0x10
50 #define TARGET_SYS_TIME 0x11
51 #define TARGET_SYS_SYSTEM 0x12
52 #define TARGET_SYS_ERRNO 0x13
53 #define TARGET_SYS_GET_CMDLINE 0x15
54 #define TARGET_SYS_HEAPINFO 0x16
55 #define TARGET_SYS_EXIT 0x18
56 #define TARGET_SYS_SYNCCACHE 0x19
58 /* ADP_Stopped_ApplicationExit is used for exit(0),
59 * anything else is implemented as exit(1) */
60 #define ADP_Stopped_ApplicationExit (0x20026)
66 #define GDB_O_RDONLY 0x000
67 #define GDB_O_WRONLY 0x001
68 #define GDB_O_RDWR 0x002
69 #define GDB_O_APPEND 0x008
70 #define GDB_O_CREAT 0x200
71 #define GDB_O_TRUNC 0x400
72 #define GDB_O_BINARY 0
74 static int gdb_open_modeflags[12] = {
76 GDB_O_RDONLY | GDB_O_BINARY,
78 GDB_O_RDWR | GDB_O_BINARY,
79 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
80 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
81 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
82 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
83 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
84 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
85 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
86 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
89 static int open_modeflags[12] = {
94 O_WRONLY | O_CREAT | O_TRUNC,
95 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
96 O_RDWR | O_CREAT | O_TRUNC,
97 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
98 O_WRONLY | O_CREAT | O_APPEND,
99 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
100 O_RDWR | O_CREAT | O_APPEND,
101 O_RDWR | O_CREAT | O_APPEND | O_BINARY
104 #ifdef CONFIG_USER_ONLY
105 static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
107 if (code == (uint32_t)-1)
108 ts->swi_errno = errno;
112 static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code)
117 #include "exec/softmmu-semi.h"
120 static target_ulong arm_semi_syscall_len;
122 #if !defined(CONFIG_USER_ONLY)
123 static target_ulong syscall_err;
126 static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
128 ARMCPU *cpu = ARM_CPU(cs);
129 CPUARMState *env = &cpu->env;
130 #ifdef CONFIG_USER_ONLY
131 TaskState *ts = cs->opaque;
133 target_ulong reg0 = is_a64(env) ? env->xregs[0] : env->regs[0];
135 if (ret == (target_ulong)-1) {
136 #ifdef CONFIG_USER_ONLY
143 /* Fixup syscalls that use nonstardard return conventions. */
145 case TARGET_SYS_WRITE:
146 case TARGET_SYS_READ:
147 reg0 = arm_semi_syscall_len - ret;
149 case TARGET_SYS_SEEK:
158 env->xregs[0] = reg0;
164 static target_ulong arm_flen_buf(ARMCPU *cpu)
166 /* Return an address in target memory of 64 bytes where the remote
167 * gdb should write its stat struct. (The format of this structure
168 * is defined by GDB's remote protocol and is not target-specific.)
169 * We put this on the guest's stack just below SP.
171 CPUARMState *env = &cpu->env;
183 static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
185 ARMCPU *cpu = ARM_CPU(cs);
186 CPUARMState *env = &cpu->env;
187 /* The size is always stored in big-endian order, extract
188 the value. We assume the size always fit in 32 bits. */
190 cpu_memory_rw_debug(cs, arm_flen_buf(cpu) + 32, (uint8_t *)&size, 4, 0);
191 size = be32_to_cpu(size);
193 env->xregs[0] = size;
197 #ifdef CONFIG_USER_ONLY
198 ((TaskState *)cs->opaque)->swi_errno = err;
204 static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb,
205 const char *fmt, ...)
208 CPUARMState *env = &cpu->env;
211 gdb_do_syscallv(cb, fmt, va);
214 /* FIXME: we are implicitly relying on the syscall completing
215 * before this point, which is not guaranteed. We should
216 * put in an explicit synchronization between this and
217 * the callback function.
220 return is_a64(env) ? env->xregs[0] : env->regs[0];
223 /* Read the input value from the argument block; fail the semihosting
224 * call if the memory read fails.
226 #define GET_ARG(n) do { \
228 if (get_user_u64(arg ## n, args + (n) * 8)) { \
232 if (get_user_u32(arg ## n, args + (n) * 4)) { \
238 #define SET_ARG(n, val) \
240 put_user_u64(val, args + (n) * 8) : \
241 put_user_u32(val, args + (n) * 4))
243 target_ulong do_arm_semihosting(CPUARMState *env)
245 ARMCPU *cpu = arm_env_get_cpu(env);
246 CPUState *cs = CPU(cpu);
248 target_ulong arg0, arg1, arg2, arg3;
253 #ifdef CONFIG_USER_ONLY
254 TaskState *ts = cs->opaque;
256 CPUARMState *ts = env;
260 /* Note that the syscall number is in W0, not X0 */
261 nr = env->xregs[0] & 0xffffffffU;
262 args = env->xregs[1];
269 case TARGET_SYS_OPEN:
273 s = lock_user_string(arg0);
275 /* FIXME - should this error code be -TARGET_EFAULT ? */
279 unlock_user(s, arg0, 0);
282 if (strcmp(s, ":tt") == 0) {
283 int result_fileno = arg1 < 4 ? STDIN_FILENO : STDOUT_FILENO;
284 unlock_user(s, arg0, 0);
285 return result_fileno;
287 if (use_gdb_syscalls()) {
288 ret = arm_gdb_syscall(cpu, arm_semi_cb, "open,%s,%x,1a4", arg0,
289 (int)arg2+1, gdb_open_modeflags[arg1]);
291 ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644));
293 unlock_user(s, arg0, 0);
295 case TARGET_SYS_CLOSE:
297 if (use_gdb_syscalls()) {
298 return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", arg0);
300 return set_swi_errno(ts, close(arg0));
302 case TARGET_SYS_WRITEC:
306 if (get_user_u8(c, args))
307 /* FIXME - should this error code be -TARGET_EFAULT ? */
309 /* Write to debug console. stderr is near enough. */
310 if (use_gdb_syscalls()) {
311 return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,1", args);
313 return write(STDERR_FILENO, &c, 1);
316 case TARGET_SYS_WRITE0:
317 if (!(s = lock_user_string(args)))
318 /* FIXME - should this error code be -TARGET_EFAULT ? */
321 if (use_gdb_syscalls()) {
322 return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,%x",
325 ret = write(STDERR_FILENO, s, len);
327 unlock_user(s, args, 0);
329 case TARGET_SYS_WRITE:
334 if (use_gdb_syscalls()) {
335 arm_semi_syscall_len = len;
336 return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x",
339 s = lock_user(VERIFY_READ, arg1, len, 1);
341 /* FIXME - should this error code be -TARGET_EFAULT ? */
344 ret = set_swi_errno(ts, write(arg0, s, len));
345 unlock_user(s, arg1, 0);
346 if (ret == (uint32_t)-1)
350 case TARGET_SYS_READ:
355 if (use_gdb_syscalls()) {
356 arm_semi_syscall_len = len;
357 return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x",
360 s = lock_user(VERIFY_WRITE, arg1, len, 0);
362 /* FIXME - should this error code be -TARGET_EFAULT ? */
366 ret = set_swi_errno(ts, read(arg0, s, len));
367 } while (ret == -1 && errno == EINTR);
368 unlock_user(s, arg1, len);
369 if (ret == (uint32_t)-1)
373 case TARGET_SYS_READC:
374 /* XXX: Read from debug console. Not implemented. */
376 case TARGET_SYS_ISTTY:
378 if (use_gdb_syscalls()) {
379 return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", arg0);
383 case TARGET_SYS_SEEK:
386 if (use_gdb_syscalls()) {
387 return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0",
390 ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET));
391 if (ret == (uint32_t)-1)
395 case TARGET_SYS_FLEN:
397 if (use_gdb_syscalls()) {
398 return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x",
399 arg0, arm_flen_buf(cpu));
402 ret = set_swi_errno(ts, fstat(arg0, &buf));
403 if (ret == (uint32_t)-1)
407 case TARGET_SYS_TMPNAM:
408 /* XXX: Not implemented. */
410 case TARGET_SYS_REMOVE:
413 if (use_gdb_syscalls()) {
414 ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s",
417 s = lock_user_string(arg0);
419 /* FIXME - should this error code be -TARGET_EFAULT ? */
422 ret = set_swi_errno(ts, remove(s));
423 unlock_user(s, arg0, 0);
426 case TARGET_SYS_RENAME:
431 if (use_gdb_syscalls()) {
432 return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s",
433 arg0, (int)arg1+1, arg2, (int)arg3+1);
436 s = lock_user_string(arg0);
437 s2 = lock_user_string(arg2);
439 /* FIXME - should this error code be -TARGET_EFAULT ? */
442 ret = set_swi_errno(ts, rename(s, s2));
444 unlock_user(s2, arg2, 0);
446 unlock_user(s, arg0, 0);
449 case TARGET_SYS_CLOCK:
450 return clock() / (CLOCKS_PER_SEC / 100);
451 case TARGET_SYS_TIME:
452 return set_swi_errno(ts, time(NULL));
453 case TARGET_SYS_SYSTEM:
456 if (use_gdb_syscalls()) {
457 return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s",
460 s = lock_user_string(arg0);
462 /* FIXME - should this error code be -TARGET_EFAULT ? */
465 ret = set_swi_errno(ts, system(s));
466 unlock_user(s, arg0, 0);
469 case TARGET_SYS_ERRNO:
470 #ifdef CONFIG_USER_ONLY
471 return ts->swi_errno;
475 case TARGET_SYS_GET_CMDLINE:
477 /* Build a command-line from the original argv.
480 * * arg0, pointer to a buffer of at least the size
482 * * arg1, size of the buffer pointed to by arg0 in
486 * * arg0, pointer to null-terminated string of the
488 * * arg1, length of the string pointed to by arg0.
495 #if !defined(CONFIG_USER_ONLY)
501 /* Compute the size of the output string. */
502 #if !defined(CONFIG_USER_ONLY)
503 cmdline = semihosting_get_cmdline();
504 if (cmdline == NULL) {
505 cmdline = ""; /* Default to an empty line. */
507 output_size = strlen(cmdline) + 1; /* Count terminating 0. */
511 output_size = ts->info->arg_end - ts->info->arg_start;
513 /* We special-case the "empty command line" case (argc==0).
514 Just provide the terminating 0. */
519 if (output_size > input_size) {
520 /* Not enough space to store command-line arguments. */
524 /* Adjust the command-line length. */
525 if (SET_ARG(1, output_size - 1)) {
526 /* Couldn't write back to argument block */
530 /* Lock the buffer on the ARM side. */
531 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
532 if (!output_buffer) {
536 /* Copy the command-line arguments. */
537 #if !defined(CONFIG_USER_ONLY)
538 pstrcpy(output_buffer, output_size, cmdline);
540 if (output_size == 1) {
541 /* Empty command-line. */
542 output_buffer[0] = '\0';
546 if (copy_from_user(output_buffer, ts->info->arg_start,
552 /* Separate arguments by white spaces. */
553 for (i = 0; i < output_size - 1; i++) {
554 if (output_buffer[i] == 0) {
555 output_buffer[i] = ' ';
560 /* Unlock the buffer on the ARM side. */
561 unlock_user(output_buffer, arg0, output_size);
565 case TARGET_SYS_HEAPINFO:
567 target_ulong retvals[4];
573 #ifdef CONFIG_USER_ONLY
574 /* Some C libraries assume the heap immediately follows .bss, so
575 allocate it using sbrk. */
576 if (!ts->heap_limit) {
579 ts->heap_base = do_brk(0);
580 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
581 /* Try a big heap, and reduce the size if that fails. */
587 limit = (ts->heap_base >> 1) + (limit >> 1);
589 ts->heap_limit = limit;
592 retvals[0] = ts->heap_base;
593 retvals[1] = ts->heap_limit;
594 retvals[2] = ts->stack_base;
595 retvals[3] = 0; /* Stack limit. */
598 /* TODO: Make this use the limit of the loaded application. */
599 retvals[0] = limit / 2;
601 retvals[2] = limit; /* Stack base */
602 retvals[3] = 0; /* Stack limit. */
605 for (i = 0; i < ARRAY_SIZE(retvals); i++) {
609 fail = put_user_u64(retvals[i], arg0 + i * 8);
611 fail = put_user_u32(retvals[i], arg0 + i * 4);
615 /* Couldn't write back to argument block */
621 case TARGET_SYS_EXIT:
623 /* The A64 version of this call takes a parameter block,
624 * so the application-exit type can return a subcode which
625 * is the exit status code from the application.
630 if (arg0 == ADP_Stopped_ApplicationExit) {
636 /* ARM specifies only Stopped_ApplicationExit as normal
637 * exit, everything else is considered an error */
638 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
642 case TARGET_SYS_SYNCCACHE:
643 /* Clean the D-cache and invalidate the I-cache for the specified
644 * virtual address range. This is a nop for us since we don't
645 * implement caches. This is only present on A64.
650 /* fall through -- invalid for A32/T32 */
652 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
653 cpu_dump_state(cs, stderr, fprintf, 0);