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"
35 #define TARGET_SYS_OPEN 0x01
36 #define TARGET_SYS_CLOSE 0x02
37 #define TARGET_SYS_WRITEC 0x03
38 #define TARGET_SYS_WRITE0 0x04
39 #define TARGET_SYS_WRITE 0x05
40 #define TARGET_SYS_READ 0x06
41 #define TARGET_SYS_READC 0x07
42 #define TARGET_SYS_ISTTY 0x09
43 #define TARGET_SYS_SEEK 0x0a
44 #define TARGET_SYS_FLEN 0x0c
45 #define TARGET_SYS_TMPNAM 0x0d
46 #define TARGET_SYS_REMOVE 0x0e
47 #define TARGET_SYS_RENAME 0x0f
48 #define TARGET_SYS_CLOCK 0x10
49 #define TARGET_SYS_TIME 0x11
50 #define TARGET_SYS_SYSTEM 0x12
51 #define TARGET_SYS_ERRNO 0x13
52 #define TARGET_SYS_GET_CMDLINE 0x15
53 #define TARGET_SYS_HEAPINFO 0x16
54 #define TARGET_SYS_EXIT 0x18
55 #define TARGET_SYS_SYNCCACHE 0x19
57 /* ADP_Stopped_ApplicationExit is used for exit(0),
58 * anything else is implemented as exit(1) */
59 #define ADP_Stopped_ApplicationExit (0x20026)
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 = cs->opaque;
132 target_ulong reg0 = is_a64(env) ? env->xregs[0] : env->regs[0];
134 if (ret == (target_ulong)-1) {
135 #ifdef CONFIG_USER_ONLY
142 /* Fixup syscalls that use nonstardard return conventions. */
144 case TARGET_SYS_WRITE:
145 case TARGET_SYS_READ:
146 reg0 = arm_semi_syscall_len - ret;
148 case TARGET_SYS_SEEK:
157 env->xregs[0] = reg0;
163 static target_ulong arm_flen_buf(ARMCPU *cpu)
165 /* Return an address in target memory of 64 bytes where the remote
166 * gdb should write its stat struct. (The format of this structure
167 * is defined by GDB's remote protocol and is not target-specific.)
168 * We put this on the guest's stack just below SP.
170 CPUARMState *env = &cpu->env;
182 static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
184 ARMCPU *cpu = ARM_CPU(cs);
185 CPUARMState *env = &cpu->env;
186 /* The size is always stored in big-endian order, extract
187 the value. We assume the size always fit in 32 bits. */
189 cpu_memory_rw_debug(cs, arm_flen_buf(cpu) + 32, (uint8_t *)&size, 4, 0);
190 size = be32_to_cpu(size);
192 env->xregs[0] = size;
196 #ifdef CONFIG_USER_ONLY
197 ((TaskState *)cs->opaque)->swi_errno = err;
203 static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb,
204 const char *fmt, ...)
207 CPUARMState *env = &cpu->env;
210 gdb_do_syscallv(cb, fmt, va);
213 /* FIXME: we are implicitly relying on the syscall completing
214 * before this point, which is not guaranteed. We should
215 * put in an explicit synchronization between this and
216 * the callback function.
219 return is_a64(env) ? env->xregs[0] : env->regs[0];
222 /* Read the input value from the argument block; fail the semihosting
223 * call if the memory read fails.
225 #define GET_ARG(n) do { \
227 if (get_user_u64(arg ## n, args + (n) * 8)) { \
231 if (get_user_u32(arg ## n, args + (n) * 4)) { \
237 #define SET_ARG(n, val) \
239 put_user_u64(val, args + (n) * 8) : \
240 put_user_u32(val, args + (n) * 4))
242 target_ulong do_arm_semihosting(CPUARMState *env)
244 ARMCPU *cpu = arm_env_get_cpu(env);
245 CPUState *cs = CPU(cpu);
247 target_ulong arg0, arg1, arg2, arg3;
252 #ifdef CONFIG_USER_ONLY
253 TaskState *ts = cs->opaque;
255 CPUARMState *ts = env;
259 /* Note that the syscall number is in W0, not X0 */
260 nr = env->xregs[0] & 0xffffffffU;
261 args = env->xregs[1];
268 case TARGET_SYS_OPEN:
272 s = lock_user_string(arg0);
274 /* FIXME - should this error code be -TARGET_EFAULT ? */
278 unlock_user(s, arg0, 0);
281 if (strcmp(s, ":tt") == 0) {
282 int result_fileno = arg1 < 4 ? STDIN_FILENO : STDOUT_FILENO;
283 unlock_user(s, arg0, 0);
284 return result_fileno;
286 if (use_gdb_syscalls()) {
287 ret = arm_gdb_syscall(cpu, arm_semi_cb, "open,%s,%x,1a4", arg0,
288 (int)arg2+1, gdb_open_modeflags[arg1]);
290 ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644));
292 unlock_user(s, arg0, 0);
294 case TARGET_SYS_CLOSE:
296 if (use_gdb_syscalls()) {
297 return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", arg0);
299 return set_swi_errno(ts, close(arg0));
301 case TARGET_SYS_WRITEC:
305 if (get_user_u8(c, args))
306 /* FIXME - should this error code be -TARGET_EFAULT ? */
308 /* Write to debug console. stderr is near enough. */
309 if (use_gdb_syscalls()) {
310 return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,1", args);
312 return write(STDERR_FILENO, &c, 1);
315 case TARGET_SYS_WRITE0:
316 if (!(s = lock_user_string(args)))
317 /* FIXME - should this error code be -TARGET_EFAULT ? */
320 if (use_gdb_syscalls()) {
321 return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,%x",
324 ret = write(STDERR_FILENO, s, len);
326 unlock_user(s, args, 0);
328 case TARGET_SYS_WRITE:
333 if (use_gdb_syscalls()) {
334 arm_semi_syscall_len = len;
335 return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x",
338 s = lock_user(VERIFY_READ, arg1, len, 1);
340 /* FIXME - should this error code be -TARGET_EFAULT ? */
343 ret = set_swi_errno(ts, write(arg0, s, len));
344 unlock_user(s, arg1, 0);
345 if (ret == (uint32_t)-1)
349 case TARGET_SYS_READ:
354 if (use_gdb_syscalls()) {
355 arm_semi_syscall_len = len;
356 return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x",
359 s = lock_user(VERIFY_WRITE, arg1, len, 0);
361 /* FIXME - should this error code be -TARGET_EFAULT ? */
365 ret = set_swi_errno(ts, read(arg0, s, len));
366 } while (ret == -1 && errno == EINTR);
367 unlock_user(s, arg1, len);
368 if (ret == (uint32_t)-1)
372 case TARGET_SYS_READC:
373 /* XXX: Read from debug console. Not implemented. */
375 case TARGET_SYS_ISTTY:
377 if (use_gdb_syscalls()) {
378 return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", arg0);
382 case TARGET_SYS_SEEK:
385 if (use_gdb_syscalls()) {
386 return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0",
389 ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET));
390 if (ret == (uint32_t)-1)
394 case TARGET_SYS_FLEN:
396 if (use_gdb_syscalls()) {
397 return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x",
398 arg0, arm_flen_buf(cpu));
401 ret = set_swi_errno(ts, fstat(arg0, &buf));
402 if (ret == (uint32_t)-1)
406 case TARGET_SYS_TMPNAM:
407 /* XXX: Not implemented. */
409 case TARGET_SYS_REMOVE:
412 if (use_gdb_syscalls()) {
413 ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s",
416 s = lock_user_string(arg0);
418 /* FIXME - should this error code be -TARGET_EFAULT ? */
421 ret = set_swi_errno(ts, remove(s));
422 unlock_user(s, arg0, 0);
425 case TARGET_SYS_RENAME:
430 if (use_gdb_syscalls()) {
431 return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s",
432 arg0, (int)arg1+1, arg2, (int)arg3+1);
435 s = lock_user_string(arg0);
436 s2 = lock_user_string(arg2);
438 /* FIXME - should this error code be -TARGET_EFAULT ? */
441 ret = set_swi_errno(ts, rename(s, s2));
443 unlock_user(s2, arg2, 0);
445 unlock_user(s, arg0, 0);
448 case TARGET_SYS_CLOCK:
449 return clock() / (CLOCKS_PER_SEC / 100);
450 case TARGET_SYS_TIME:
451 return set_swi_errno(ts, time(NULL));
452 case TARGET_SYS_SYSTEM:
455 if (use_gdb_syscalls()) {
456 return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s",
459 s = lock_user_string(arg0);
461 /* FIXME - should this error code be -TARGET_EFAULT ? */
464 ret = set_swi_errno(ts, system(s));
465 unlock_user(s, arg0, 0);
468 case TARGET_SYS_ERRNO:
469 #ifdef CONFIG_USER_ONLY
470 return ts->swi_errno;
474 case TARGET_SYS_GET_CMDLINE:
476 /* Build a command-line from the original argv.
479 * * arg0, pointer to a buffer of at least the size
481 * * arg1, size of the buffer pointed to by arg0 in
485 * * arg0, pointer to null-terminated string of the
487 * * arg1, length of the string pointed to by arg0.
494 #if !defined(CONFIG_USER_ONLY)
500 /* Compute the size of the output string. */
501 #if !defined(CONFIG_USER_ONLY)
502 cmdline = semihosting_get_cmdline();
503 if (cmdline == NULL) {
504 cmdline = ""; /* Default to an empty line. */
506 output_size = strlen(cmdline) + 1; /* Count terminating 0. */
510 output_size = ts->info->arg_end - ts->info->arg_start;
512 /* We special-case the "empty command line" case (argc==0).
513 Just provide the terminating 0. */
518 if (output_size > input_size) {
519 /* Not enough space to store command-line arguments. */
523 /* Adjust the command-line length. */
524 if (SET_ARG(1, output_size - 1)) {
525 /* Couldn't write back to argument block */
529 /* Lock the buffer on the ARM side. */
530 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
531 if (!output_buffer) {
535 /* Copy the command-line arguments. */
536 #if !defined(CONFIG_USER_ONLY)
537 pstrcpy(output_buffer, output_size, cmdline);
539 if (output_size == 1) {
540 /* Empty command-line. */
541 output_buffer[0] = '\0';
545 if (copy_from_user(output_buffer, ts->info->arg_start,
551 /* Separate arguments by white spaces. */
552 for (i = 0; i < output_size - 1; i++) {
553 if (output_buffer[i] == 0) {
554 output_buffer[i] = ' ';
559 /* Unlock the buffer on the ARM side. */
560 unlock_user(output_buffer, arg0, output_size);
564 case TARGET_SYS_HEAPINFO:
570 #ifdef CONFIG_USER_ONLY
571 /* Some C libraries assume the heap immediately follows .bss, so
572 allocate it using sbrk. */
573 if (!ts->heap_limit) {
576 ts->heap_base = do_brk(0);
577 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
578 /* Try a big heap, and reduce the size if that fails. */
584 limit = (ts->heap_base >> 1) + (limit >> 1);
586 ts->heap_limit = limit;
589 ptr = lock_user(VERIFY_WRITE, arg0, 16, 0);
591 /* FIXME - should this error code be -TARGET_EFAULT ? */
594 ptr[0] = tswap32(ts->heap_base);
595 ptr[1] = tswap32(ts->heap_limit);
596 ptr[2] = tswap32(ts->stack_base);
597 ptr[3] = tswap32(0); /* Stack limit. */
598 unlock_user(ptr, arg0, 16);
601 ptr = lock_user(VERIFY_WRITE, arg0, 16, 0);
603 /* FIXME - should this error code be -TARGET_EFAULT ? */
606 /* TODO: Make this use the limit of the loaded application. */
607 ptr[0] = tswap32(limit / 2);
608 ptr[1] = tswap32(limit);
609 ptr[2] = tswap32(limit); /* Stack base */
610 ptr[3] = tswap32(0); /* Stack limit. */
611 unlock_user(ptr, arg0, 16);
615 case TARGET_SYS_EXIT:
617 /* The A64 version of this call takes a parameter block,
618 * so the application-exit type can return a subcode which
619 * is the exit status code from the application.
624 if (arg0 == ADP_Stopped_ApplicationExit) {
630 /* ARM specifies only Stopped_ApplicationExit as normal
631 * exit, everything else is considered an error */
632 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
636 case TARGET_SYS_SYNCCACHE:
637 /* Clean the D-cache and invalidate the I-cache for the specified
638 * virtual address range. This is a nop for us since we don't
639 * implement caches. This is only present on A64.
644 /* fall through -- invalid for A32/T32 */
646 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
647 cpu_dump_state(cs, stderr, fprintf, 0);