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 <sys/types.h>
30 #include "exec/semihost.h"
31 #ifdef CONFIG_USER_ONLY
34 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
36 #include "qemu-common.h"
37 #include "exec/gdbstub.h"
38 #include "hw/arm/arm.h"
41 #define TARGET_SYS_OPEN 0x01
42 #define TARGET_SYS_CLOSE 0x02
43 #define TARGET_SYS_WRITEC 0x03
44 #define TARGET_SYS_WRITE0 0x04
45 #define TARGET_SYS_WRITE 0x05
46 #define TARGET_SYS_READ 0x06
47 #define TARGET_SYS_READC 0x07
48 #define TARGET_SYS_ISTTY 0x09
49 #define TARGET_SYS_SEEK 0x0a
50 #define TARGET_SYS_FLEN 0x0c
51 #define TARGET_SYS_TMPNAM 0x0d
52 #define TARGET_SYS_REMOVE 0x0e
53 #define TARGET_SYS_RENAME 0x0f
54 #define TARGET_SYS_CLOCK 0x10
55 #define TARGET_SYS_TIME 0x11
56 #define TARGET_SYS_SYSTEM 0x12
57 #define TARGET_SYS_ERRNO 0x13
58 #define TARGET_SYS_GET_CMDLINE 0x15
59 #define TARGET_SYS_HEAPINFO 0x16
60 #define TARGET_SYS_EXIT 0x18
61 #define TARGET_SYS_SYNCCACHE 0x19
63 /* ADP_Stopped_ApplicationExit is used for exit(0),
64 * anything else is implemented as exit(1) */
65 #define ADP_Stopped_ApplicationExit (0x20026)
71 #define GDB_O_RDONLY 0x000
72 #define GDB_O_WRONLY 0x001
73 #define GDB_O_RDWR 0x002
74 #define GDB_O_APPEND 0x008
75 #define GDB_O_CREAT 0x200
76 #define GDB_O_TRUNC 0x400
77 #define GDB_O_BINARY 0
79 static int gdb_open_modeflags
[12] = {
81 GDB_O_RDONLY
| GDB_O_BINARY
,
83 GDB_O_RDWR
| GDB_O_BINARY
,
84 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
,
85 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
86 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
,
87 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
88 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
,
89 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
,
90 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
,
91 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
94 static int open_modeflags
[12] = {
99 O_WRONLY
| O_CREAT
| O_TRUNC
,
100 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
101 O_RDWR
| O_CREAT
| O_TRUNC
,
102 O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
,
103 O_WRONLY
| O_CREAT
| O_APPEND
,
104 O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
,
105 O_RDWR
| O_CREAT
| O_APPEND
,
106 O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
109 #ifdef CONFIG_USER_ONLY
110 static inline uint32_t set_swi_errno(TaskState
*ts
, uint32_t code
)
112 if (code
== (uint32_t)-1)
113 ts
->swi_errno
= errno
;
117 static inline uint32_t set_swi_errno(CPUARMState
*env
, uint32_t code
)
122 #include "exec/softmmu-semi.h"
125 static target_ulong arm_semi_syscall_len
;
127 #if !defined(CONFIG_USER_ONLY)
128 static target_ulong syscall_err
;
131 static void arm_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
133 ARMCPU
*cpu
= ARM_CPU(cs
);
134 CPUARMState
*env
= &cpu
->env
;
135 #ifdef CONFIG_USER_ONLY
136 TaskState
*ts
= cs
->opaque
;
138 target_ulong reg0
= is_a64(env
) ? env
->xregs
[0] : env
->regs
[0];
140 if (ret
== (target_ulong
)-1) {
141 #ifdef CONFIG_USER_ONLY
148 /* Fixup syscalls that use nonstardard return conventions. */
150 case TARGET_SYS_WRITE
:
151 case TARGET_SYS_READ
:
152 reg0
= arm_semi_syscall_len
- ret
;
154 case TARGET_SYS_SEEK
:
163 env
->xregs
[0] = reg0
;
169 static target_ulong
arm_flen_buf(ARMCPU
*cpu
)
171 /* Return an address in target memory of 64 bytes where the remote
172 * gdb should write its stat struct. (The format of this structure
173 * is defined by GDB's remote protocol and is not target-specific.)
174 * We put this on the guest's stack just below SP.
176 CPUARMState
*env
= &cpu
->env
;
188 static void arm_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
190 ARMCPU
*cpu
= ARM_CPU(cs
);
191 CPUARMState
*env
= &cpu
->env
;
192 /* The size is always stored in big-endian order, extract
193 the value. We assume the size always fit in 32 bits. */
195 cpu_memory_rw_debug(cs
, arm_flen_buf(cpu
) + 32, (uint8_t *)&size
, 4, 0);
196 size
= be32_to_cpu(size
);
198 env
->xregs
[0] = size
;
202 #ifdef CONFIG_USER_ONLY
203 ((TaskState
*)cs
->opaque
)->swi_errno
= err
;
209 static target_ulong
arm_gdb_syscall(ARMCPU
*cpu
, gdb_syscall_complete_cb cb
,
210 const char *fmt
, ...)
213 CPUARMState
*env
= &cpu
->env
;
216 gdb_do_syscallv(cb
, fmt
, va
);
219 /* FIXME: we are implicitly relying on the syscall completing
220 * before this point, which is not guaranteed. We should
221 * put in an explicit synchronization between this and
222 * the callback function.
225 return is_a64(env
) ? env
->xregs
[0] : env
->regs
[0];
228 /* Read the input value from the argument block; fail the semihosting
229 * call if the memory read fails.
231 #define GET_ARG(n) do { \
233 if (get_user_u64(arg ## n, args + (n) * 8)) { \
237 if (get_user_u32(arg ## n, args + (n) * 4)) { \
243 #define SET_ARG(n, val) \
245 put_user_u64(val, args + (n) * 8) : \
246 put_user_u32(val, args + (n) * 4))
248 target_ulong
do_arm_semihosting(CPUARMState
*env
)
250 ARMCPU
*cpu
= arm_env_get_cpu(env
);
251 CPUState
*cs
= CPU(cpu
);
253 target_ulong arg0
, arg1
, arg2
, arg3
;
258 #ifdef CONFIG_USER_ONLY
259 TaskState
*ts
= cs
->opaque
;
261 CPUARMState
*ts
= env
;
265 /* Note that the syscall number is in W0, not X0 */
266 nr
= env
->xregs
[0] & 0xffffffffU
;
267 args
= env
->xregs
[1];
274 case TARGET_SYS_OPEN
:
278 s
= lock_user_string(arg0
);
280 /* FIXME - should this error code be -TARGET_EFAULT ? */
284 unlock_user(s
, arg0
, 0);
287 if (strcmp(s
, ":tt") == 0) {
288 int result_fileno
= arg1
< 4 ? STDIN_FILENO
: STDOUT_FILENO
;
289 unlock_user(s
, arg0
, 0);
290 return result_fileno
;
292 if (use_gdb_syscalls()) {
293 ret
= arm_gdb_syscall(cpu
, arm_semi_cb
, "open,%s,%x,1a4", arg0
,
294 (int)arg2
+1, gdb_open_modeflags
[arg1
]);
296 ret
= set_swi_errno(ts
, open(s
, open_modeflags
[arg1
], 0644));
298 unlock_user(s
, arg0
, 0);
300 case TARGET_SYS_CLOSE
:
302 if (use_gdb_syscalls()) {
303 return arm_gdb_syscall(cpu
, arm_semi_cb
, "close,%x", arg0
);
305 return set_swi_errno(ts
, close(arg0
));
307 case TARGET_SYS_WRITEC
:
311 if (get_user_u8(c
, args
))
312 /* FIXME - should this error code be -TARGET_EFAULT ? */
314 /* Write to debug console. stderr is near enough. */
315 if (use_gdb_syscalls()) {
316 return arm_gdb_syscall(cpu
, arm_semi_cb
, "write,2,%x,1", args
);
318 return write(STDERR_FILENO
, &c
, 1);
321 case TARGET_SYS_WRITE0
:
322 if (!(s
= lock_user_string(args
)))
323 /* FIXME - should this error code be -TARGET_EFAULT ? */
326 if (use_gdb_syscalls()) {
327 return arm_gdb_syscall(cpu
, arm_semi_cb
, "write,2,%x,%x",
330 ret
= write(STDERR_FILENO
, s
, len
);
332 unlock_user(s
, args
, 0);
334 case TARGET_SYS_WRITE
:
339 if (use_gdb_syscalls()) {
340 arm_semi_syscall_len
= len
;
341 return arm_gdb_syscall(cpu
, arm_semi_cb
, "write,%x,%x,%x",
344 s
= lock_user(VERIFY_READ
, arg1
, len
, 1);
346 /* FIXME - should this error code be -TARGET_EFAULT ? */
349 ret
= set_swi_errno(ts
, write(arg0
, s
, len
));
350 unlock_user(s
, arg1
, 0);
351 if (ret
== (uint32_t)-1)
355 case TARGET_SYS_READ
:
360 if (use_gdb_syscalls()) {
361 arm_semi_syscall_len
= len
;
362 return arm_gdb_syscall(cpu
, arm_semi_cb
, "read,%x,%x,%x",
365 s
= lock_user(VERIFY_WRITE
, arg1
, len
, 0);
367 /* FIXME - should this error code be -TARGET_EFAULT ? */
371 ret
= set_swi_errno(ts
, read(arg0
, s
, len
));
372 } while (ret
== -1 && errno
== EINTR
);
373 unlock_user(s
, arg1
, len
);
374 if (ret
== (uint32_t)-1)
378 case TARGET_SYS_READC
:
379 /* XXX: Read from debug console. Not implemented. */
381 case TARGET_SYS_ISTTY
:
383 if (use_gdb_syscalls()) {
384 return arm_gdb_syscall(cpu
, arm_semi_cb
, "isatty,%x", arg0
);
388 case TARGET_SYS_SEEK
:
391 if (use_gdb_syscalls()) {
392 return arm_gdb_syscall(cpu
, arm_semi_cb
, "lseek,%x,%x,0",
395 ret
= set_swi_errno(ts
, lseek(arg0
, arg1
, SEEK_SET
));
396 if (ret
== (uint32_t)-1)
400 case TARGET_SYS_FLEN
:
402 if (use_gdb_syscalls()) {
403 return arm_gdb_syscall(cpu
, arm_semi_flen_cb
, "fstat,%x,%x",
404 arg0
, arm_flen_buf(cpu
));
407 ret
= set_swi_errno(ts
, fstat(arg0
, &buf
));
408 if (ret
== (uint32_t)-1)
412 case TARGET_SYS_TMPNAM
:
413 /* XXX: Not implemented. */
415 case TARGET_SYS_REMOVE
:
418 if (use_gdb_syscalls()) {
419 ret
= arm_gdb_syscall(cpu
, arm_semi_cb
, "unlink,%s",
422 s
= lock_user_string(arg0
);
424 /* FIXME - should this error code be -TARGET_EFAULT ? */
427 ret
= set_swi_errno(ts
, remove(s
));
428 unlock_user(s
, arg0
, 0);
431 case TARGET_SYS_RENAME
:
436 if (use_gdb_syscalls()) {
437 return arm_gdb_syscall(cpu
, arm_semi_cb
, "rename,%s,%s",
438 arg0
, (int)arg1
+1, arg2
, (int)arg3
+1);
441 s
= lock_user_string(arg0
);
442 s2
= lock_user_string(arg2
);
444 /* FIXME - should this error code be -TARGET_EFAULT ? */
447 ret
= set_swi_errno(ts
, rename(s
, s2
));
449 unlock_user(s2
, arg2
, 0);
451 unlock_user(s
, arg0
, 0);
454 case TARGET_SYS_CLOCK
:
455 return clock() / (CLOCKS_PER_SEC
/ 100);
456 case TARGET_SYS_TIME
:
457 return set_swi_errno(ts
, time(NULL
));
458 case TARGET_SYS_SYSTEM
:
461 if (use_gdb_syscalls()) {
462 return arm_gdb_syscall(cpu
, arm_semi_cb
, "system,%s",
465 s
= lock_user_string(arg0
);
467 /* FIXME - should this error code be -TARGET_EFAULT ? */
470 ret
= set_swi_errno(ts
, system(s
));
471 unlock_user(s
, arg0
, 0);
474 case TARGET_SYS_ERRNO
:
475 #ifdef CONFIG_USER_ONLY
476 return ts
->swi_errno
;
480 case TARGET_SYS_GET_CMDLINE
:
482 /* Build a command-line from the original argv.
485 * * arg0, pointer to a buffer of at least the size
487 * * arg1, size of the buffer pointed to by arg0 in
491 * * arg0, pointer to null-terminated string of the
493 * * arg1, length of the string pointed to by arg0.
500 #if !defined(CONFIG_USER_ONLY)
506 /* Compute the size of the output string. */
507 #if !defined(CONFIG_USER_ONLY)
508 cmdline
= semihosting_get_cmdline();
509 if (cmdline
== NULL
) {
510 cmdline
= ""; /* Default to an empty line. */
512 output_size
= strlen(cmdline
) + 1; /* Count terminating 0. */
516 output_size
= ts
->info
->arg_end
- ts
->info
->arg_start
;
518 /* We special-case the "empty command line" case (argc==0).
519 Just provide the terminating 0. */
524 if (output_size
> input_size
) {
525 /* Not enough space to store command-line arguments. */
529 /* Adjust the command-line length. */
530 if (SET_ARG(1, output_size
- 1)) {
531 /* Couldn't write back to argument block */
535 /* Lock the buffer on the ARM side. */
536 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
537 if (!output_buffer
) {
541 /* Copy the command-line arguments. */
542 #if !defined(CONFIG_USER_ONLY)
543 pstrcpy(output_buffer
, output_size
, cmdline
);
545 if (output_size
== 1) {
546 /* Empty command-line. */
547 output_buffer
[0] = '\0';
551 if (copy_from_user(output_buffer
, ts
->info
->arg_start
,
557 /* Separate arguments by white spaces. */
558 for (i
= 0; i
< output_size
- 1; i
++) {
559 if (output_buffer
[i
] == 0) {
560 output_buffer
[i
] = ' ';
565 /* Unlock the buffer on the ARM side. */
566 unlock_user(output_buffer
, arg0
, output_size
);
570 case TARGET_SYS_HEAPINFO
:
576 #ifdef CONFIG_USER_ONLY
577 /* Some C libraries assume the heap immediately follows .bss, so
578 allocate it using sbrk. */
579 if (!ts
->heap_limit
) {
582 ts
->heap_base
= do_brk(0);
583 limit
= ts
->heap_base
+ ARM_ANGEL_HEAP_SIZE
;
584 /* Try a big heap, and reduce the size if that fails. */
590 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
592 ts
->heap_limit
= limit
;
595 ptr
= lock_user(VERIFY_WRITE
, arg0
, 16, 0);
597 /* FIXME - should this error code be -TARGET_EFAULT ? */
600 ptr
[0] = tswap32(ts
->heap_base
);
601 ptr
[1] = tswap32(ts
->heap_limit
);
602 ptr
[2] = tswap32(ts
->stack_base
);
603 ptr
[3] = tswap32(0); /* Stack limit. */
604 unlock_user(ptr
, arg0
, 16);
607 ptr
= lock_user(VERIFY_WRITE
, arg0
, 16, 0);
609 /* FIXME - should this error code be -TARGET_EFAULT ? */
612 /* TODO: Make this use the limit of the loaded application. */
613 ptr
[0] = tswap32(limit
/ 2);
614 ptr
[1] = tswap32(limit
);
615 ptr
[2] = tswap32(limit
); /* Stack base */
616 ptr
[3] = tswap32(0); /* Stack limit. */
617 unlock_user(ptr
, arg0
, 16);
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);