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
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
73 static int gdb_open_modeflags
[12] = {
75 GDB_O_RDONLY
| GDB_O_BINARY
,
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
88 static int open_modeflags
[12] = {
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
103 #ifdef CONFIG_USER_ONLY
104 static inline uint32_t set_swi_errno(TaskState
*ts
, uint32_t code
)
106 if (code
== (uint32_t)-1)
107 ts
->swi_errno
= errno
;
111 static inline uint32_t set_swi_errno(CPUARMState
*env
, uint32_t code
)
116 #include "exec/softmmu-semi.h"
119 static target_ulong arm_semi_syscall_len
;
121 #if !defined(CONFIG_USER_ONLY)
122 static target_ulong syscall_err
;
125 static void arm_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
127 ARMCPU
*cpu
= ARM_CPU(cs
);
128 CPUARMState
*env
= &cpu
->env
;
129 #ifdef CONFIG_USER_ONLY
130 TaskState
*ts
= env
->opaque
;
133 if (ret
== (target_ulong
)-1) {
134 #ifdef CONFIG_USER_ONLY
141 /* Fixup syscalls that use nonstardard return conventions. */
142 switch (env
->regs
[0]) {
143 case TARGET_SYS_WRITE
:
144 case TARGET_SYS_READ
:
145 env
->regs
[0] = arm_semi_syscall_len
- ret
;
147 case TARGET_SYS_SEEK
:
157 static void arm_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
159 ARMCPU
*cpu
= ARM_CPU(cs
);
160 CPUARMState
*env
= &cpu
->env
;
161 /* The size is always stored in big-endian order, extract
162 the value. We assume the size always fit in 32 bits. */
164 cpu_memory_rw_debug(cs
, env
->regs
[13]-64+32, (uint8_t *)&size
, 4, 0);
165 env
->regs
[0] = be32_to_cpu(size
);
166 #ifdef CONFIG_USER_ONLY
167 ((TaskState
*)env
->opaque
)->swi_errno
= err
;
173 /* Read the input value from the argument block; fail the semihosting
174 * call if the memory read fails.
176 #define GET_ARG(n) do { \
177 if (get_user_ual(arg ## n, args + (n) * 4)) { \
178 return (uint32_t)-1; \
182 #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
183 uint32_t do_arm_semihosting(CPUARMState
*env
)
185 ARMCPU
*cpu
= arm_env_get_cpu(env
);
187 target_ulong arg0
, arg1
, arg2
, arg3
;
192 #ifdef CONFIG_USER_ONLY
193 TaskState
*ts
= env
->opaque
;
195 CPUARMState
*ts
= env
;
201 case TARGET_SYS_OPEN
:
205 s
= lock_user_string(arg0
);
207 /* FIXME - should this error code be -TARGET_EFAULT ? */
211 unlock_user(s
, arg0
, 0);
214 if (strcmp(s
, ":tt") == 0) {
215 int result_fileno
= arg1
< 4 ? STDIN_FILENO
: STDOUT_FILENO
;
216 unlock_user(s
, arg0
, 0);
217 return result_fileno
;
219 if (use_gdb_syscalls()) {
220 gdb_do_syscall(arm_semi_cb
, "open,%s,%x,1a4", arg0
,
221 (int)arg2
+1, gdb_open_modeflags
[arg1
]);
224 ret
= set_swi_errno(ts
, open(s
, open_modeflags
[arg1
], 0644));
226 unlock_user(s
, arg0
, 0);
228 case TARGET_SYS_CLOSE
:
230 if (use_gdb_syscalls()) {
231 gdb_do_syscall(arm_semi_cb
, "close,%x", arg0
);
234 return set_swi_errno(ts
, close(arg0
));
236 case TARGET_SYS_WRITEC
:
240 if (get_user_u8(c
, args
))
241 /* FIXME - should this error code be -TARGET_EFAULT ? */
243 /* Write to debug console. stderr is near enough. */
244 if (use_gdb_syscalls()) {
245 gdb_do_syscall(arm_semi_cb
, "write,2,%x,1", args
);
248 return write(STDERR_FILENO
, &c
, 1);
251 case TARGET_SYS_WRITE0
:
252 if (!(s
= lock_user_string(args
)))
253 /* FIXME - should this error code be -TARGET_EFAULT ? */
256 if (use_gdb_syscalls()) {
257 gdb_do_syscall(arm_semi_cb
, "write,2,%x,%x\n", args
, len
);
260 ret
= write(STDERR_FILENO
, s
, len
);
262 unlock_user(s
, args
, 0);
264 case TARGET_SYS_WRITE
:
269 if (use_gdb_syscalls()) {
270 arm_semi_syscall_len
= len
;
271 gdb_do_syscall(arm_semi_cb
, "write,%x,%x,%x", arg0
, arg1
, len
);
274 s
= lock_user(VERIFY_READ
, arg1
, len
, 1);
276 /* FIXME - should this error code be -TARGET_EFAULT ? */
279 ret
= set_swi_errno(ts
, write(arg0
, s
, len
));
280 unlock_user(s
, arg1
, 0);
281 if (ret
== (uint32_t)-1)
285 case TARGET_SYS_READ
:
290 if (use_gdb_syscalls()) {
291 arm_semi_syscall_len
= len
;
292 gdb_do_syscall(arm_semi_cb
, "read,%x,%x,%x", arg0
, arg1
, len
);
295 s
= lock_user(VERIFY_WRITE
, arg1
, len
, 0);
297 /* FIXME - should this error code be -TARGET_EFAULT ? */
301 ret
= set_swi_errno(ts
, read(arg0
, s
, len
));
302 } while (ret
== -1 && errno
== EINTR
);
303 unlock_user(s
, arg1
, len
);
304 if (ret
== (uint32_t)-1)
308 case TARGET_SYS_READC
:
309 /* XXX: Read from debug console. Not implemented. */
311 case TARGET_SYS_ISTTY
:
313 if (use_gdb_syscalls()) {
314 gdb_do_syscall(arm_semi_cb
, "isatty,%x", arg0
);
319 case TARGET_SYS_SEEK
:
322 if (use_gdb_syscalls()) {
323 gdb_do_syscall(arm_semi_cb
, "lseek,%x,%x,0", arg0
, arg1
);
326 ret
= set_swi_errno(ts
, lseek(arg0
, arg1
, SEEK_SET
));
327 if (ret
== (uint32_t)-1)
331 case TARGET_SYS_FLEN
:
333 if (use_gdb_syscalls()) {
334 gdb_do_syscall(arm_semi_flen_cb
, "fstat,%x,%x",
335 arg0
, env
->regs
[13]-64);
339 ret
= set_swi_errno(ts
, fstat(arg0
, &buf
));
340 if (ret
== (uint32_t)-1)
344 case TARGET_SYS_TMPNAM
:
345 /* XXX: Not implemented. */
347 case TARGET_SYS_REMOVE
:
350 if (use_gdb_syscalls()) {
351 gdb_do_syscall(arm_semi_cb
, "unlink,%s", arg0
, (int)arg1
+1);
354 s
= lock_user_string(arg0
);
356 /* FIXME - should this error code be -TARGET_EFAULT ? */
359 ret
= set_swi_errno(ts
, remove(s
));
360 unlock_user(s
, arg0
, 0);
363 case TARGET_SYS_RENAME
:
368 if (use_gdb_syscalls()) {
369 gdb_do_syscall(arm_semi_cb
, "rename,%s,%s",
370 arg0
, (int)arg1
+1, arg2
, (int)arg3
+1);
374 s
= lock_user_string(arg0
);
375 s2
= lock_user_string(arg2
);
377 /* FIXME - should this error code be -TARGET_EFAULT ? */
380 ret
= set_swi_errno(ts
, rename(s
, s2
));
382 unlock_user(s2
, arg2
, 0);
384 unlock_user(s
, arg0
, 0);
387 case TARGET_SYS_CLOCK
:
388 return clock() / (CLOCKS_PER_SEC
/ 100);
389 case TARGET_SYS_TIME
:
390 return set_swi_errno(ts
, time(NULL
));
391 case TARGET_SYS_SYSTEM
:
394 if (use_gdb_syscalls()) {
395 gdb_do_syscall(arm_semi_cb
, "system,%s", arg0
, (int)arg1
+1);
398 s
= lock_user_string(arg0
);
400 /* FIXME - should this error code be -TARGET_EFAULT ? */
403 ret
= set_swi_errno(ts
, system(s
));
404 unlock_user(s
, arg0
, 0);
407 case TARGET_SYS_ERRNO
:
408 #ifdef CONFIG_USER_ONLY
409 return ts
->swi_errno
;
413 case TARGET_SYS_GET_CMDLINE
:
415 /* Build a command-line from the original argv.
418 * * arg0, pointer to a buffer of at least the size
420 * * arg1, size of the buffer pointed to by arg0 in
424 * * arg0, pointer to null-terminated string of the
426 * * arg1, length of the string pointed to by arg0.
436 /* Compute the size of the output string. */
437 #if !defined(CONFIG_USER_ONLY)
438 output_size
= strlen(ts
->boot_info
->kernel_filename
)
439 + 1 /* Separating space. */
440 + strlen(ts
->boot_info
->kernel_cmdline
)
441 + 1; /* Terminating null byte. */
445 output_size
= ts
->info
->arg_end
- ts
->info
->arg_start
;
447 /* We special-case the "empty command line" case (argc==0).
448 Just provide the terminating 0. */
453 if (output_size
> input_size
) {
454 /* Not enough space to store command-line arguments. */
458 /* Adjust the command-line length. */
459 if (SET_ARG(1, output_size
- 1)) {
460 /* Couldn't write back to argument block */
464 /* Lock the buffer on the ARM side. */
465 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
466 if (!output_buffer
) {
470 /* Copy the command-line arguments. */
471 #if !defined(CONFIG_USER_ONLY)
472 pstrcpy(output_buffer
, output_size
, ts
->boot_info
->kernel_filename
);
473 pstrcat(output_buffer
, output_size
, " ");
474 pstrcat(output_buffer
, output_size
, ts
->boot_info
->kernel_cmdline
);
476 if (output_size
== 1) {
477 /* Empty command-line. */
478 output_buffer
[0] = '\0';
482 if (copy_from_user(output_buffer
, ts
->info
->arg_start
,
488 /* Separate arguments by white spaces. */
489 for (i
= 0; i
< output_size
- 1; i
++) {
490 if (output_buffer
[i
] == 0) {
491 output_buffer
[i
] = ' ';
496 /* Unlock the buffer on the ARM side. */
497 unlock_user(output_buffer
, arg0
, output_size
);
501 case TARGET_SYS_HEAPINFO
:
507 #ifdef CONFIG_USER_ONLY
508 /* Some C libraries assume the heap immediately follows .bss, so
509 allocate it using sbrk. */
510 if (!ts
->heap_limit
) {
513 ts
->heap_base
= do_brk(0);
514 limit
= ts
->heap_base
+ ARM_ANGEL_HEAP_SIZE
;
515 /* Try a big heap, and reduce the size if that fails. */
521 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
523 ts
->heap_limit
= limit
;
526 ptr
= lock_user(VERIFY_WRITE
, arg0
, 16, 0);
528 /* FIXME - should this error code be -TARGET_EFAULT ? */
531 ptr
[0] = tswap32(ts
->heap_base
);
532 ptr
[1] = tswap32(ts
->heap_limit
);
533 ptr
[2] = tswap32(ts
->stack_base
);
534 ptr
[3] = tswap32(0); /* Stack limit. */
535 unlock_user(ptr
, arg0
, 16);
538 ptr
= lock_user(VERIFY_WRITE
, arg0
, 16, 0);
540 /* FIXME - should this error code be -TARGET_EFAULT ? */
543 /* TODO: Make this use the limit of the loaded application. */
544 ptr
[0] = tswap32(limit
/ 2);
545 ptr
[1] = tswap32(limit
);
546 ptr
[2] = tswap32(limit
); /* Stack base */
547 ptr
[3] = tswap32(0); /* Stack limit. */
548 unlock_user(ptr
, arg0
, 16);
552 case TARGET_SYS_EXIT
:
556 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
557 cpu_dump_state(CPU(cpu
), stderr
, fprintf
, 0);