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
62 /* ADP_Stopped_ApplicationExit is used for exit(0),
63 * anything else is implemented as exit(1) */
64 #define ADP_Stopped_ApplicationExit (0x20026)
70 #define GDB_O_RDONLY 0x000
71 #define GDB_O_WRONLY 0x001
72 #define GDB_O_RDWR 0x002
73 #define GDB_O_APPEND 0x008
74 #define GDB_O_CREAT 0x200
75 #define GDB_O_TRUNC 0x400
76 #define GDB_O_BINARY 0
78 static int gdb_open_modeflags
[12] = {
80 GDB_O_RDONLY
| GDB_O_BINARY
,
82 GDB_O_RDWR
| GDB_O_BINARY
,
83 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
,
84 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
85 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
,
86 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
87 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
,
88 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
,
89 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
,
90 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
93 static int open_modeflags
[12] = {
98 O_WRONLY
| O_CREAT
| O_TRUNC
,
99 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
100 O_RDWR
| O_CREAT
| O_TRUNC
,
101 O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
,
102 O_WRONLY
| O_CREAT
| O_APPEND
,
103 O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
,
104 O_RDWR
| O_CREAT
| O_APPEND
,
105 O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
108 #ifdef CONFIG_USER_ONLY
109 static inline uint32_t set_swi_errno(TaskState
*ts
, uint32_t code
)
111 if (code
== (uint32_t)-1)
112 ts
->swi_errno
= errno
;
116 static inline uint32_t set_swi_errno(CPUARMState
*env
, uint32_t code
)
121 #include "exec/softmmu-semi.h"
124 static target_ulong arm_semi_syscall_len
;
126 #if !defined(CONFIG_USER_ONLY)
127 static target_ulong syscall_err
;
130 static void arm_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
132 ARMCPU
*cpu
= ARM_CPU(cs
);
133 CPUARMState
*env
= &cpu
->env
;
134 #ifdef CONFIG_USER_ONLY
135 TaskState
*ts
= cs
->opaque
;
138 if (ret
== (target_ulong
)-1) {
139 #ifdef CONFIG_USER_ONLY
146 /* Fixup syscalls that use nonstardard return conventions. */
147 switch (env
->regs
[0]) {
148 case TARGET_SYS_WRITE
:
149 case TARGET_SYS_READ
:
150 env
->regs
[0] = arm_semi_syscall_len
- ret
;
152 case TARGET_SYS_SEEK
:
162 static void arm_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
164 ARMCPU
*cpu
= ARM_CPU(cs
);
165 CPUARMState
*env
= &cpu
->env
;
166 /* The size is always stored in big-endian order, extract
167 the value. We assume the size always fit in 32 bits. */
169 cpu_memory_rw_debug(cs
, env
->regs
[13]-64+32, (uint8_t *)&size
, 4, 0);
170 env
->regs
[0] = be32_to_cpu(size
);
171 #ifdef CONFIG_USER_ONLY
172 ((TaskState
*)cs
->opaque
)->swi_errno
= err
;
178 /* Read the input value from the argument block; fail the semihosting
179 * call if the memory read fails.
181 #define GET_ARG(n) do { \
182 if (get_user_ual(arg ## n, args + (n) * 4)) { \
183 return (uint32_t)-1; \
187 #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
188 uint32_t do_arm_semihosting(CPUARMState
*env
)
190 ARMCPU
*cpu
= arm_env_get_cpu(env
);
191 CPUState
*cs
= CPU(cpu
);
193 target_ulong arg0
, arg1
, arg2
, arg3
;
198 #ifdef CONFIG_USER_ONLY
199 TaskState
*ts
= cs
->opaque
;
201 CPUARMState
*ts
= env
;
207 case TARGET_SYS_OPEN
:
211 s
= lock_user_string(arg0
);
213 /* FIXME - should this error code be -TARGET_EFAULT ? */
217 unlock_user(s
, arg0
, 0);
220 if (strcmp(s
, ":tt") == 0) {
221 int result_fileno
= arg1
< 4 ? STDIN_FILENO
: STDOUT_FILENO
;
222 unlock_user(s
, arg0
, 0);
223 return result_fileno
;
225 if (use_gdb_syscalls()) {
226 gdb_do_syscall(arm_semi_cb
, "open,%s,%x,1a4", arg0
,
227 (int)arg2
+1, gdb_open_modeflags
[arg1
]);
230 ret
= set_swi_errno(ts
, open(s
, open_modeflags
[arg1
], 0644));
232 unlock_user(s
, arg0
, 0);
234 case TARGET_SYS_CLOSE
:
236 if (use_gdb_syscalls()) {
237 gdb_do_syscall(arm_semi_cb
, "close,%x", arg0
);
240 return set_swi_errno(ts
, close(arg0
));
242 case TARGET_SYS_WRITEC
:
246 if (get_user_u8(c
, args
))
247 /* FIXME - should this error code be -TARGET_EFAULT ? */
249 /* Write to debug console. stderr is near enough. */
250 if (use_gdb_syscalls()) {
251 gdb_do_syscall(arm_semi_cb
, "write,2,%x,1", args
);
254 return write(STDERR_FILENO
, &c
, 1);
257 case TARGET_SYS_WRITE0
:
258 if (!(s
= lock_user_string(args
)))
259 /* FIXME - should this error code be -TARGET_EFAULT ? */
262 if (use_gdb_syscalls()) {
263 gdb_do_syscall(arm_semi_cb
, "write,2,%x,%x\n", args
, len
);
266 ret
= write(STDERR_FILENO
, s
, len
);
268 unlock_user(s
, args
, 0);
270 case TARGET_SYS_WRITE
:
275 if (use_gdb_syscalls()) {
276 arm_semi_syscall_len
= len
;
277 gdb_do_syscall(arm_semi_cb
, "write,%x,%x,%x", arg0
, arg1
, len
);
280 s
= lock_user(VERIFY_READ
, arg1
, len
, 1);
282 /* FIXME - should this error code be -TARGET_EFAULT ? */
285 ret
= set_swi_errno(ts
, write(arg0
, s
, len
));
286 unlock_user(s
, arg1
, 0);
287 if (ret
== (uint32_t)-1)
291 case TARGET_SYS_READ
:
296 if (use_gdb_syscalls()) {
297 arm_semi_syscall_len
= len
;
298 gdb_do_syscall(arm_semi_cb
, "read,%x,%x,%x", arg0
, arg1
, len
);
301 s
= lock_user(VERIFY_WRITE
, arg1
, len
, 0);
303 /* FIXME - should this error code be -TARGET_EFAULT ? */
307 ret
= set_swi_errno(ts
, read(arg0
, s
, len
));
308 } while (ret
== -1 && errno
== EINTR
);
309 unlock_user(s
, arg1
, len
);
310 if (ret
== (uint32_t)-1)
314 case TARGET_SYS_READC
:
315 /* XXX: Read from debug console. Not implemented. */
317 case TARGET_SYS_ISTTY
:
319 if (use_gdb_syscalls()) {
320 gdb_do_syscall(arm_semi_cb
, "isatty,%x", arg0
);
325 case TARGET_SYS_SEEK
:
328 if (use_gdb_syscalls()) {
329 gdb_do_syscall(arm_semi_cb
, "lseek,%x,%x,0", arg0
, arg1
);
332 ret
= set_swi_errno(ts
, lseek(arg0
, arg1
, SEEK_SET
));
333 if (ret
== (uint32_t)-1)
337 case TARGET_SYS_FLEN
:
339 if (use_gdb_syscalls()) {
340 gdb_do_syscall(arm_semi_flen_cb
, "fstat,%x,%x",
341 arg0
, env
->regs
[13]-64);
345 ret
= set_swi_errno(ts
, fstat(arg0
, &buf
));
346 if (ret
== (uint32_t)-1)
350 case TARGET_SYS_TMPNAM
:
351 /* XXX: Not implemented. */
353 case TARGET_SYS_REMOVE
:
356 if (use_gdb_syscalls()) {
357 gdb_do_syscall(arm_semi_cb
, "unlink,%s", arg0
, (int)arg1
+1);
360 s
= lock_user_string(arg0
);
362 /* FIXME - should this error code be -TARGET_EFAULT ? */
365 ret
= set_swi_errno(ts
, remove(s
));
366 unlock_user(s
, arg0
, 0);
369 case TARGET_SYS_RENAME
:
374 if (use_gdb_syscalls()) {
375 gdb_do_syscall(arm_semi_cb
, "rename,%s,%s",
376 arg0
, (int)arg1
+1, arg2
, (int)arg3
+1);
380 s
= lock_user_string(arg0
);
381 s2
= lock_user_string(arg2
);
383 /* FIXME - should this error code be -TARGET_EFAULT ? */
386 ret
= set_swi_errno(ts
, rename(s
, s2
));
388 unlock_user(s2
, arg2
, 0);
390 unlock_user(s
, arg0
, 0);
393 case TARGET_SYS_CLOCK
:
394 return clock() / (CLOCKS_PER_SEC
/ 100);
395 case TARGET_SYS_TIME
:
396 return set_swi_errno(ts
, time(NULL
));
397 case TARGET_SYS_SYSTEM
:
400 if (use_gdb_syscalls()) {
401 gdb_do_syscall(arm_semi_cb
, "system,%s", arg0
, (int)arg1
+1);
404 s
= lock_user_string(arg0
);
406 /* FIXME - should this error code be -TARGET_EFAULT ? */
409 ret
= set_swi_errno(ts
, system(s
));
410 unlock_user(s
, arg0
, 0);
413 case TARGET_SYS_ERRNO
:
414 #ifdef CONFIG_USER_ONLY
415 return ts
->swi_errno
;
419 case TARGET_SYS_GET_CMDLINE
:
421 /* Build a command-line from the original argv.
424 * * arg0, pointer to a buffer of at least the size
426 * * arg1, size of the buffer pointed to by arg0 in
430 * * arg0, pointer to null-terminated string of the
432 * * arg1, length of the string pointed to by arg0.
442 /* Compute the size of the output string. */
443 #if !defined(CONFIG_USER_ONLY)
444 output_size
= strlen(semihosting_get_cmdline()) + 1;
448 output_size
= ts
->info
->arg_end
- ts
->info
->arg_start
;
450 /* We special-case the "empty command line" case (argc==0).
451 Just provide the terminating 0. */
456 if (output_size
> input_size
) {
457 /* Not enough space to store command-line arguments. */
461 /* Adjust the command-line length. */
462 if (SET_ARG(1, output_size
- 1)) {
463 /* Couldn't write back to argument block */
467 /* Lock the buffer on the ARM side. */
468 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
469 if (!output_buffer
) {
473 /* Copy the command-line arguments. */
474 #if !defined(CONFIG_USER_ONLY)
475 pstrcpy(output_buffer
, output_size
, semihosting_get_cmdline());
477 if (output_size
== 1) {
478 /* Empty command-line. */
479 output_buffer
[0] = '\0';
483 if (copy_from_user(output_buffer
, ts
->info
->arg_start
,
489 /* Separate arguments by white spaces. */
490 for (i
= 0; i
< output_size
- 1; i
++) {
491 if (output_buffer
[i
] == 0) {
492 output_buffer
[i
] = ' ';
497 /* Unlock the buffer on the ARM side. */
498 unlock_user(output_buffer
, arg0
, output_size
);
502 case TARGET_SYS_HEAPINFO
:
508 #ifdef CONFIG_USER_ONLY
509 /* Some C libraries assume the heap immediately follows .bss, so
510 allocate it using sbrk. */
511 if (!ts
->heap_limit
) {
514 ts
->heap_base
= do_brk(0);
515 limit
= ts
->heap_base
+ ARM_ANGEL_HEAP_SIZE
;
516 /* Try a big heap, and reduce the size if that fails. */
522 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
524 ts
->heap_limit
= limit
;
527 ptr
= lock_user(VERIFY_WRITE
, arg0
, 16, 0);
529 /* FIXME - should this error code be -TARGET_EFAULT ? */
532 ptr
[0] = tswap32(ts
->heap_base
);
533 ptr
[1] = tswap32(ts
->heap_limit
);
534 ptr
[2] = tswap32(ts
->stack_base
);
535 ptr
[3] = tswap32(0); /* Stack limit. */
536 unlock_user(ptr
, arg0
, 16);
539 ptr
= lock_user(VERIFY_WRITE
, arg0
, 16, 0);
541 /* FIXME - should this error code be -TARGET_EFAULT ? */
544 /* TODO: Make this use the limit of the loaded application. */
545 ptr
[0] = tswap32(limit
/ 2);
546 ptr
[1] = tswap32(limit
);
547 ptr
[2] = tswap32(limit
); /* Stack base */
548 ptr
[3] = tswap32(0); /* Stack limit. */
549 unlock_user(ptr
, arg0
, 16);
553 case TARGET_SYS_EXIT
:
554 /* ARM specifies only Stopped_ApplicationExit as normal
555 * exit, everything else is considered an error */
556 ret
= (args
== ADP_Stopped_ApplicationExit
) ? 0 : 1;
560 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
561 cpu_dump_state(cs
, stderr
, fprintf
, 0);