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 #ifdef CONFIG_USER_ONLY
33 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
35 #include "qemu-common.h"
36 #include "exec/gdbstub.h"
37 #include "hw/arm/arm.h"
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
61 /* ADP_Stopped_ApplicationExit is used for exit(0),
62 * anything else is implemented as exit(1) */
63 #define ADP_Stopped_ApplicationExit (0x20026)
69 #define GDB_O_RDONLY 0x000
70 #define GDB_O_WRONLY 0x001
71 #define GDB_O_RDWR 0x002
72 #define GDB_O_APPEND 0x008
73 #define GDB_O_CREAT 0x200
74 #define GDB_O_TRUNC 0x400
75 #define GDB_O_BINARY 0
77 static int gdb_open_modeflags
[12] = {
79 GDB_O_RDONLY
| GDB_O_BINARY
,
81 GDB_O_RDWR
| GDB_O_BINARY
,
82 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
,
83 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
84 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
,
85 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
86 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
,
87 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
,
88 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
,
89 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
92 static int open_modeflags
[12] = {
97 O_WRONLY
| O_CREAT
| O_TRUNC
,
98 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
99 O_RDWR
| O_CREAT
| O_TRUNC
,
100 O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
,
101 O_WRONLY
| O_CREAT
| O_APPEND
,
102 O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
,
103 O_RDWR
| O_CREAT
| O_APPEND
,
104 O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
107 #ifdef CONFIG_USER_ONLY
108 static inline uint32_t set_swi_errno(TaskState
*ts
, uint32_t code
)
110 if (code
== (uint32_t)-1)
111 ts
->swi_errno
= errno
;
115 static inline uint32_t set_swi_errno(CPUARMState
*env
, uint32_t code
)
120 #include "exec/softmmu-semi.h"
123 static target_ulong arm_semi_syscall_len
;
125 #if !defined(CONFIG_USER_ONLY)
126 static target_ulong syscall_err
;
129 static void arm_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
131 ARMCPU
*cpu
= ARM_CPU(cs
);
132 CPUARMState
*env
= &cpu
->env
;
133 #ifdef CONFIG_USER_ONLY
134 TaskState
*ts
= cs
->opaque
;
137 if (ret
== (target_ulong
)-1) {
138 #ifdef CONFIG_USER_ONLY
145 /* Fixup syscalls that use nonstardard return conventions. */
146 switch (env
->regs
[0]) {
147 case TARGET_SYS_WRITE
:
148 case TARGET_SYS_READ
:
149 env
->regs
[0] = arm_semi_syscall_len
- ret
;
151 case TARGET_SYS_SEEK
:
161 static void arm_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
163 ARMCPU
*cpu
= ARM_CPU(cs
);
164 CPUARMState
*env
= &cpu
->env
;
165 /* The size is always stored in big-endian order, extract
166 the value. We assume the size always fit in 32 bits. */
168 cpu_memory_rw_debug(cs
, env
->regs
[13]-64+32, (uint8_t *)&size
, 4, 0);
169 env
->regs
[0] = be32_to_cpu(size
);
170 #ifdef CONFIG_USER_ONLY
171 ((TaskState
*)cs
->opaque
)->swi_errno
= err
;
177 /* Read the input value from the argument block; fail the semihosting
178 * call if the memory read fails.
180 #define GET_ARG(n) do { \
181 if (get_user_ual(arg ## n, args + (n) * 4)) { \
182 return (uint32_t)-1; \
186 #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
187 uint32_t do_arm_semihosting(CPUARMState
*env
)
189 ARMCPU
*cpu
= arm_env_get_cpu(env
);
190 CPUState
*cs
= CPU(cpu
);
192 target_ulong arg0
, arg1
, arg2
, arg3
;
197 #ifdef CONFIG_USER_ONLY
198 TaskState
*ts
= cs
->opaque
;
200 CPUARMState
*ts
= env
;
206 case TARGET_SYS_OPEN
:
210 s
= lock_user_string(arg0
);
212 /* FIXME - should this error code be -TARGET_EFAULT ? */
216 unlock_user(s
, arg0
, 0);
219 if (strcmp(s
, ":tt") == 0) {
220 int result_fileno
= arg1
< 4 ? STDIN_FILENO
: STDOUT_FILENO
;
221 unlock_user(s
, arg0
, 0);
222 return result_fileno
;
224 if (use_gdb_syscalls()) {
225 gdb_do_syscall(arm_semi_cb
, "open,%s,%x,1a4", arg0
,
226 (int)arg2
+1, gdb_open_modeflags
[arg1
]);
229 ret
= set_swi_errno(ts
, open(s
, open_modeflags
[arg1
], 0644));
231 unlock_user(s
, arg0
, 0);
233 case TARGET_SYS_CLOSE
:
235 if (use_gdb_syscalls()) {
236 gdb_do_syscall(arm_semi_cb
, "close,%x", arg0
);
239 return set_swi_errno(ts
, close(arg0
));
241 case TARGET_SYS_WRITEC
:
245 if (get_user_u8(c
, args
))
246 /* FIXME - should this error code be -TARGET_EFAULT ? */
248 /* Write to debug console. stderr is near enough. */
249 if (use_gdb_syscalls()) {
250 gdb_do_syscall(arm_semi_cb
, "write,2,%x,1", args
);
253 return write(STDERR_FILENO
, &c
, 1);
256 case TARGET_SYS_WRITE0
:
257 if (!(s
= lock_user_string(args
)))
258 /* FIXME - should this error code be -TARGET_EFAULT ? */
261 if (use_gdb_syscalls()) {
262 gdb_do_syscall(arm_semi_cb
, "write,2,%x,%x\n", args
, len
);
265 ret
= write(STDERR_FILENO
, s
, len
);
267 unlock_user(s
, args
, 0);
269 case TARGET_SYS_WRITE
:
274 if (use_gdb_syscalls()) {
275 arm_semi_syscall_len
= len
;
276 gdb_do_syscall(arm_semi_cb
, "write,%x,%x,%x", arg0
, arg1
, len
);
279 s
= lock_user(VERIFY_READ
, arg1
, len
, 1);
281 /* FIXME - should this error code be -TARGET_EFAULT ? */
284 ret
= set_swi_errno(ts
, write(arg0
, s
, len
));
285 unlock_user(s
, arg1
, 0);
286 if (ret
== (uint32_t)-1)
290 case TARGET_SYS_READ
:
295 if (use_gdb_syscalls()) {
296 arm_semi_syscall_len
= len
;
297 gdb_do_syscall(arm_semi_cb
, "read,%x,%x,%x", arg0
, arg1
, len
);
300 s
= lock_user(VERIFY_WRITE
, arg1
, len
, 0);
302 /* FIXME - should this error code be -TARGET_EFAULT ? */
306 ret
= set_swi_errno(ts
, read(arg0
, s
, len
));
307 } while (ret
== -1 && errno
== EINTR
);
308 unlock_user(s
, arg1
, len
);
309 if (ret
== (uint32_t)-1)
313 case TARGET_SYS_READC
:
314 /* XXX: Read from debug console. Not implemented. */
316 case TARGET_SYS_ISTTY
:
318 if (use_gdb_syscalls()) {
319 gdb_do_syscall(arm_semi_cb
, "isatty,%x", arg0
);
324 case TARGET_SYS_SEEK
:
327 if (use_gdb_syscalls()) {
328 gdb_do_syscall(arm_semi_cb
, "lseek,%x,%x,0", arg0
, arg1
);
331 ret
= set_swi_errno(ts
, lseek(arg0
, arg1
, SEEK_SET
));
332 if (ret
== (uint32_t)-1)
336 case TARGET_SYS_FLEN
:
338 if (use_gdb_syscalls()) {
339 gdb_do_syscall(arm_semi_flen_cb
, "fstat,%x,%x",
340 arg0
, env
->regs
[13]-64);
344 ret
= set_swi_errno(ts
, fstat(arg0
, &buf
));
345 if (ret
== (uint32_t)-1)
349 case TARGET_SYS_TMPNAM
:
350 /* XXX: Not implemented. */
352 case TARGET_SYS_REMOVE
:
355 if (use_gdb_syscalls()) {
356 gdb_do_syscall(arm_semi_cb
, "unlink,%s", arg0
, (int)arg1
+1);
359 s
= lock_user_string(arg0
);
361 /* FIXME - should this error code be -TARGET_EFAULT ? */
364 ret
= set_swi_errno(ts
, remove(s
));
365 unlock_user(s
, arg0
, 0);
368 case TARGET_SYS_RENAME
:
373 if (use_gdb_syscalls()) {
374 gdb_do_syscall(arm_semi_cb
, "rename,%s,%s",
375 arg0
, (int)arg1
+1, arg2
, (int)arg3
+1);
379 s
= lock_user_string(arg0
);
380 s2
= lock_user_string(arg2
);
382 /* FIXME - should this error code be -TARGET_EFAULT ? */
385 ret
= set_swi_errno(ts
, rename(s
, s2
));
387 unlock_user(s2
, arg2
, 0);
389 unlock_user(s
, arg0
, 0);
392 case TARGET_SYS_CLOCK
:
393 return clock() / (CLOCKS_PER_SEC
/ 100);
394 case TARGET_SYS_TIME
:
395 return set_swi_errno(ts
, time(NULL
));
396 case TARGET_SYS_SYSTEM
:
399 if (use_gdb_syscalls()) {
400 gdb_do_syscall(arm_semi_cb
, "system,%s", arg0
, (int)arg1
+1);
403 s
= lock_user_string(arg0
);
405 /* FIXME - should this error code be -TARGET_EFAULT ? */
408 ret
= set_swi_errno(ts
, system(s
));
409 unlock_user(s
, arg0
, 0);
412 case TARGET_SYS_ERRNO
:
413 #ifdef CONFIG_USER_ONLY
414 return ts
->swi_errno
;
418 case TARGET_SYS_GET_CMDLINE
:
420 /* Build a command-line from the original argv.
423 * * arg0, pointer to a buffer of at least the size
425 * * arg1, size of the buffer pointed to by arg0 in
429 * * arg0, pointer to null-terminated string of the
431 * * arg1, length of the string pointed to by arg0.
441 /* Compute the size of the output string. */
442 #if !defined(CONFIG_USER_ONLY)
443 output_size
= strlen(ts
->boot_info
->kernel_filename
)
444 + 1 /* Separating space. */
445 + strlen(ts
->boot_info
->kernel_cmdline
)
446 + 1; /* Terminating null byte. */
450 output_size
= ts
->info
->arg_end
- ts
->info
->arg_start
;
452 /* We special-case the "empty command line" case (argc==0).
453 Just provide the terminating 0. */
458 if (output_size
> input_size
) {
459 /* Not enough space to store command-line arguments. */
463 /* Adjust the command-line length. */
464 if (SET_ARG(1, output_size
- 1)) {
465 /* Couldn't write back to argument block */
469 /* Lock the buffer on the ARM side. */
470 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
471 if (!output_buffer
) {
475 /* Copy the command-line arguments. */
476 #if !defined(CONFIG_USER_ONLY)
477 pstrcpy(output_buffer
, output_size
, ts
->boot_info
->kernel_filename
);
478 pstrcat(output_buffer
, output_size
, " ");
479 pstrcat(output_buffer
, output_size
, ts
->boot_info
->kernel_cmdline
);
481 if (output_size
== 1) {
482 /* Empty command-line. */
483 output_buffer
[0] = '\0';
487 if (copy_from_user(output_buffer
, ts
->info
->arg_start
,
493 /* Separate arguments by white spaces. */
494 for (i
= 0; i
< output_size
- 1; i
++) {
495 if (output_buffer
[i
] == 0) {
496 output_buffer
[i
] = ' ';
501 /* Unlock the buffer on the ARM side. */
502 unlock_user(output_buffer
, arg0
, output_size
);
506 case TARGET_SYS_HEAPINFO
:
512 #ifdef CONFIG_USER_ONLY
513 /* Some C libraries assume the heap immediately follows .bss, so
514 allocate it using sbrk. */
515 if (!ts
->heap_limit
) {
518 ts
->heap_base
= do_brk(0);
519 limit
= ts
->heap_base
+ ARM_ANGEL_HEAP_SIZE
;
520 /* Try a big heap, and reduce the size if that fails. */
526 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
528 ts
->heap_limit
= limit
;
531 ptr
= lock_user(VERIFY_WRITE
, arg0
, 16, 0);
533 /* FIXME - should this error code be -TARGET_EFAULT ? */
536 ptr
[0] = tswap32(ts
->heap_base
);
537 ptr
[1] = tswap32(ts
->heap_limit
);
538 ptr
[2] = tswap32(ts
->stack_base
);
539 ptr
[3] = tswap32(0); /* Stack limit. */
540 unlock_user(ptr
, arg0
, 16);
543 ptr
= lock_user(VERIFY_WRITE
, arg0
, 16, 0);
545 /* FIXME - should this error code be -TARGET_EFAULT ? */
548 /* TODO: Make this use the limit of the loaded application. */
549 ptr
[0] = tswap32(limit
/ 2);
550 ptr
[1] = tswap32(limit
);
551 ptr
[2] = tswap32(limit
); /* Stack base */
552 ptr
[3] = tswap32(0); /* Stack limit. */
553 unlock_user(ptr
, arg0
, 16);
557 case TARGET_SYS_EXIT
:
558 /* ARM specifies only Stopped_ApplicationExit as normal
559 * exit, everything else is considered an error */
560 ret
= (args
== ADP_Stopped_ApplicationExit
) ? 0 : 1;
564 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
565 cpu_dump_state(cs
, stderr
, fprintf
, 0);