2 * Semihosting support for systems modeled on the Arm "Angel"
3 * semihosting syscalls design. This includes Arm and RISC-V processors
5 * Copyright (c) 2005, 2007 CodeSourcery.
6 * Copyright (c) 2019 Linaro
7 * Written by Paul Brook.
9 * Copyright © 2020 by Keith Packard <keithp@keithp.com>
10 * Adapted for systems other than ARM, including RISC-V, by Keith Packard
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 * ARM Semihosting is documented in:
26 * Semihosting for AArch32 and AArch64 Release 2.0
27 * https://static.docs.arm.com/100863/0200/semihosting.pdf
29 * RISC-V Semihosting is documented in:
31 * https://github.com/riscv/riscv-semihosting-spec/blob/main/riscv-semihosting-spec.adoc
34 #include "qemu/osdep.h"
35 #include "semihosting/semihost.h"
36 #include "semihosting/console.h"
37 #include "semihosting/common-semi.h"
38 #include "semihosting/guestfd.h"
39 #include "qemu/timer.h"
40 #include "exec/gdbstub.h"
42 #ifdef CONFIG_USER_ONLY
45 #define COMMON_SEMI_HEAP_SIZE (128 * 1024 * 1024)
47 #include "qemu/cutils.h"
48 #include "hw/loader.h"
50 #include "hw/arm/boot.h"
52 #include "hw/boards.h"
55 #define TARGET_SYS_OPEN 0x01
56 #define TARGET_SYS_CLOSE 0x02
57 #define TARGET_SYS_WRITEC 0x03
58 #define TARGET_SYS_WRITE0 0x04
59 #define TARGET_SYS_WRITE 0x05
60 #define TARGET_SYS_READ 0x06
61 #define TARGET_SYS_READC 0x07
62 #define TARGET_SYS_ISERROR 0x08
63 #define TARGET_SYS_ISTTY 0x09
64 #define TARGET_SYS_SEEK 0x0a
65 #define TARGET_SYS_FLEN 0x0c
66 #define TARGET_SYS_TMPNAM 0x0d
67 #define TARGET_SYS_REMOVE 0x0e
68 #define TARGET_SYS_RENAME 0x0f
69 #define TARGET_SYS_CLOCK 0x10
70 #define TARGET_SYS_TIME 0x11
71 #define TARGET_SYS_SYSTEM 0x12
72 #define TARGET_SYS_ERRNO 0x13
73 #define TARGET_SYS_GET_CMDLINE 0x15
74 #define TARGET_SYS_HEAPINFO 0x16
75 #define TARGET_SYS_EXIT 0x18
76 #define TARGET_SYS_SYNCCACHE 0x19
77 #define TARGET_SYS_EXIT_EXTENDED 0x20
78 #define TARGET_SYS_ELAPSED 0x30
79 #define TARGET_SYS_TICKFREQ 0x31
81 /* ADP_Stopped_ApplicationExit is used for exit(0),
82 * anything else is implemented as exit(1) */
83 #define ADP_Stopped_ApplicationExit (0x20026)
89 static int gdb_open_modeflags
[12] = {
94 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
,
95 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
,
96 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
,
97 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
,
98 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
,
99 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
,
100 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
,
101 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
,
104 static int open_modeflags
[12] = {
109 O_WRONLY
| O_CREAT
| O_TRUNC
,
110 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
111 O_RDWR
| O_CREAT
| O_TRUNC
,
112 O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
,
113 O_WRONLY
| O_CREAT
| O_APPEND
,
114 O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
,
115 O_RDWR
| O_CREAT
| O_APPEND
,
116 O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
119 #ifndef CONFIG_USER_ONLY
122 * common_semi_find_bases: find information about ram and heap base
124 * This function attempts to provide meaningful numbers for RAM and
125 * HEAP base addresses. The rambase is simply the lowest addressable
126 * RAM position. For the heapbase we ask the loader to scan the
127 * address space and the largest available gap by querying the "ROM"
130 * Returns: a structure with the numbers we need.
133 typedef struct LayoutInfo
{
134 target_ulong rambase
;
140 static bool find_ram_cb(Int128 start
, Int128 len
, const MemoryRegion
*mr
,
141 hwaddr offset_in_region
, void *opaque
)
143 LayoutInfo
*info
= (LayoutInfo
*) opaque
;
144 uint64_t size
= int128_get64(len
);
146 if (!mr
->ram
|| mr
->readonly
) {
150 if (size
> info
->ramsize
) {
151 info
->rambase
= int128_get64(start
);
152 info
->ramsize
= size
;
155 /* search exhaustively for largest RAM */
159 static LayoutInfo
common_semi_find_bases(CPUState
*cs
)
162 LayoutInfo info
= { 0, 0, 0, 0 };
164 RCU_READ_LOCK_GUARD();
166 fv
= address_space_to_flatview(cs
->as
);
167 flatview_for_each_range(fv
, find_ram_cb
, &info
);
170 * If we have found the RAM lets iterate through the ROM blobs to
171 * work out the best place for the remainder of RAM and split it
172 * equally between stack and heap.
174 if (info
.rambase
|| info
.ramsize
> 0) {
175 RomGap gap
= rom_find_largest_gap_between(info
.rambase
, info
.ramsize
);
176 info
.heapbase
= gap
.base
;
177 info
.heaplimit
= gap
.base
+ gap
.size
;
186 static inline target_ulong
187 common_semi_arg(CPUState
*cs
, int argno
)
189 ARMCPU
*cpu
= ARM_CPU(cs
);
190 CPUARMState
*env
= &cpu
->env
;
192 return env
->xregs
[argno
];
194 return env
->regs
[argno
];
199 common_semi_set_ret(CPUState
*cs
, target_ulong ret
)
201 ARMCPU
*cpu
= ARM_CPU(cs
);
202 CPUARMState
*env
= &cpu
->env
;
211 common_semi_sys_exit_extended(CPUState
*cs
, int nr
)
213 return (nr
== TARGET_SYS_EXIT_EXTENDED
|| is_a64(cs
->env_ptr
));
216 static inline bool is_64bit_semihosting(CPUArchState
*env
)
221 static inline target_ulong
common_semi_stack_bottom(CPUState
*cs
)
223 ARMCPU
*cpu
= ARM_CPU(cs
);
224 CPUARMState
*env
= &cpu
->env
;
225 return is_a64(env
) ? env
->xregs
[31] : env
->regs
[13];
228 static inline bool common_semi_has_synccache(CPUArchState
*env
)
230 /* Ok for A64, invalid for A32/T32. */
233 #endif /* TARGET_ARM */
236 static inline target_ulong
237 common_semi_arg(CPUState
*cs
, int argno
)
239 RISCVCPU
*cpu
= RISCV_CPU(cs
);
240 CPURISCVState
*env
= &cpu
->env
;
241 return env
->gpr
[xA0
+ argno
];
245 common_semi_set_ret(CPUState
*cs
, target_ulong ret
)
247 RISCVCPU
*cpu
= RISCV_CPU(cs
);
248 CPURISCVState
*env
= &cpu
->env
;
253 common_semi_sys_exit_extended(CPUState
*cs
, int nr
)
255 return (nr
== TARGET_SYS_EXIT_EXTENDED
|| sizeof(target_ulong
) == 8);
258 static inline bool is_64bit_semihosting(CPUArchState
*env
)
260 return riscv_cpu_mxl(env
) != MXL_RV32
;
263 static inline target_ulong
common_semi_stack_bottom(CPUState
*cs
)
265 RISCVCPU
*cpu
= RISCV_CPU(cs
);
266 CPURISCVState
*env
= &cpu
->env
;
267 return env
->gpr
[xSP
];
270 static inline bool common_semi_has_synccache(CPUArchState
*env
)
277 * The semihosting API has no concept of its errno being thread-safe,
278 * as the API design predates SMP CPUs and was intended as a simple
279 * real-hardware set of debug functionality. For QEMU, we make the
280 * errno be per-thread in linux-user mode; in softmmu it is a simple
281 * global, and we assume that the guest takes care of avoiding any races.
283 #ifndef CONFIG_USER_ONLY
284 static target_ulong syscall_err
;
286 #include "semihosting/softmmu-uaccess.h"
289 static inline uint32_t get_swi_errno(CPUState
*cs
)
291 #ifdef CONFIG_USER_ONLY
292 TaskState
*ts
= cs
->opaque
;
294 return ts
->swi_errno
;
300 static target_ulong common_semi_syscall_len
;
302 static void common_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
305 #ifdef CONFIG_USER_ONLY
306 TaskState
*ts
= cs
->opaque
;
312 /* Fixup syscalls that use nonstardard return conventions. */
313 target_ulong reg0
= common_semi_arg(cs
, 0);
315 case TARGET_SYS_WRITE
:
316 case TARGET_SYS_READ
:
317 ret
= common_semi_syscall_len
- ret
;
319 case TARGET_SYS_SEEK
:
326 common_semi_set_ret(cs
, ret
);
330 * Return an address in target memory of 64 bytes where the remote
331 * gdb should write its stat struct. (The format of this structure
332 * is defined by GDB's remote protocol and is not target-specific.)
333 * We put this on the guest's stack just below SP.
335 static target_ulong
common_semi_flen_buf(CPUState
*cs
)
337 target_ulong sp
= common_semi_stack_bottom(cs
);
342 common_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
345 /* The size is always stored in big-endian order, extract the value. */
347 cpu_memory_rw_debug(cs
, common_semi_flen_buf(cs
) +
348 offsetof(struct gdb_stat
, gdb_st_size
),
350 ret
= be64_to_cpu(size
);
352 common_semi_cb(cs
, ret
, err
);
355 static int common_semi_open_guestfd
;
358 common_semi_open_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
361 dealloc_guestfd(common_semi_open_guestfd
);
363 associate_guestfd(common_semi_open_guestfd
, ret
);
364 ret
= common_semi_open_guestfd
;
366 common_semi_cb(cs
, ret
, err
);
370 * Types for functions implementing various semihosting calls
371 * for specific types of guest file descriptor. These must all
372 * do the work and return the required return value to the guest
373 * via common_semi_cb.
375 typedef void sys_closefn(CPUState
*cs
, GuestFD
*gf
);
376 typedef void sys_writefn(CPUState
*cs
, GuestFD
*gf
,
377 target_ulong buf
, uint32_t len
);
378 typedef void sys_readfn(CPUState
*cs
, GuestFD
*gf
,
379 target_ulong buf
, uint32_t len
);
380 typedef void sys_isattyfn(CPUState
*cs
, GuestFD
*gf
);
381 typedef void sys_seekfn(CPUState
*cs
, GuestFD
*gf
, target_ulong offset
);
382 typedef void sys_flenfn(CPUState
*cs
, GuestFD
*gf
);
384 static void host_closefn(CPUState
*cs
, GuestFD
*gf
)
388 * Only close the underlying host fd if it's one we opened on behalf
389 * of the guest in SYS_OPEN.
391 if (gf
->hostfd
== STDIN_FILENO
||
392 gf
->hostfd
== STDOUT_FILENO
||
393 gf
->hostfd
== STDERR_FILENO
) {
396 ret
= close(gf
->hostfd
);
398 common_semi_cb(cs
, ret
, ret
? errno
: 0);
401 static void host_writefn(CPUState
*cs
, GuestFD
*gf
,
402 target_ulong buf
, uint32_t len
)
404 CPUArchState
*env
= cs
->env_ptr
;
406 char *s
= lock_user(VERIFY_READ
, buf
, len
, 1);
407 (void) env
; /* Used in arm softmmu lock_user implicitly */
409 ret
= write(gf
->hostfd
, s
, len
);
410 unlock_user(s
, buf
, 0);
411 if (ret
== (uint32_t)-1) {
415 /* Return bytes not written, on error as well. */
416 common_semi_cb(cs
, len
- ret
, 0);
419 static void host_readfn(CPUState
*cs
, GuestFD
*gf
,
420 target_ulong buf
, uint32_t len
)
422 CPUArchState
*env
= cs
->env_ptr
;
424 char *s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
425 (void) env
; /* Used in arm softmmu lock_user implicitly */
428 ret
= read(gf
->hostfd
, s
, len
);
429 } while (ret
== -1 && errno
== EINTR
);
430 unlock_user(s
, buf
, len
);
431 if (ret
== (uint32_t)-1) {
435 /* Return bytes not read, on error as well. */
436 common_semi_cb(cs
, len
- ret
, 0);
439 static void host_isattyfn(CPUState
*cs
, GuestFD
*gf
)
441 common_semi_cb(cs
, isatty(gf
->hostfd
), 0);
444 static void host_seekfn(CPUState
*cs
, GuestFD
*gf
, target_ulong offset
)
446 off_t ret
= lseek(gf
->hostfd
, offset
, SEEK_SET
);
447 common_semi_cb(cs
, ret
, ret
== -1 ? errno
: 0);
450 static void host_flenfn(CPUState
*cs
, GuestFD
*gf
)
454 if (fstat(gf
->hostfd
, &buf
)) {
455 common_semi_cb(cs
, -1, errno
);
457 common_semi_cb(cs
, buf
.st_size
, 0);
461 static void gdb_closefn(CPUState
*cs
, GuestFD
*gf
)
463 gdb_do_syscall(common_semi_cb
, "close,%x", gf
->hostfd
);
466 static void gdb_writefn(CPUState
*cs
, GuestFD
*gf
,
467 target_ulong buf
, uint32_t len
)
469 common_semi_syscall_len
= len
;
470 gdb_do_syscall(common_semi_cb
, "write,%x,%x,%x", gf
->hostfd
, buf
, len
);
473 static void gdb_readfn(CPUState
*cs
, GuestFD
*gf
,
474 target_ulong buf
, uint32_t len
)
476 common_semi_syscall_len
= len
;
477 gdb_do_syscall(common_semi_cb
, "read,%x,%x,%x", gf
->hostfd
, buf
, len
);
480 static void gdb_isattyfn(CPUState
*cs
, GuestFD
*gf
)
482 gdb_do_syscall(common_semi_cb
, "isatty,%x", gf
->hostfd
);
485 static void gdb_seekfn(CPUState
*cs
, GuestFD
*gf
, target_ulong offset
)
487 gdb_do_syscall(common_semi_cb
, "lseek,%x,%x,0", gf
->hostfd
, offset
);
490 static void gdb_flenfn(CPUState
*cs
, GuestFD
*gf
)
492 gdb_do_syscall(common_semi_flen_cb
, "fstat,%x,%x",
493 gf
->hostfd
, common_semi_flen_buf(cs
));
496 #define SHFB_MAGIC_0 0x53
497 #define SHFB_MAGIC_1 0x48
498 #define SHFB_MAGIC_2 0x46
499 #define SHFB_MAGIC_3 0x42
501 /* Feature bits reportable in feature byte 0 */
502 #define SH_EXT_EXIT_EXTENDED (1 << 0)
503 #define SH_EXT_STDOUT_STDERR (1 << 1)
505 static const uint8_t featurefile_data
[] = {
510 SH_EXT_EXIT_EXTENDED
| SH_EXT_STDOUT_STDERR
, /* Feature byte 0 */
513 static void staticfile_closefn(CPUState
*cs
, GuestFD
*gf
)
516 common_semi_cb(cs
, 0, 0);
519 static void staticfile_writefn(CPUState
*cs
, GuestFD
*gf
,
520 target_ulong buf
, uint32_t len
)
522 /* This fd can never be open for writing */
523 common_semi_cb(cs
, -1, EBADF
);
526 static void staticfile_readfn(CPUState
*cs
, GuestFD
*gf
,
527 target_ulong buf
, uint32_t len
)
529 CPUArchState
*env
= cs
->env_ptr
;
533 (void) env
; /* Used in arm softmmu lock_user implicitly */
534 s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
536 for (i
= 0; i
< len
; i
++) {
537 if (gf
->staticfile
.off
>= gf
->staticfile
.len
) {
540 s
[i
] = gf
->staticfile
.data
[gf
->staticfile
.off
];
541 gf
->staticfile
.off
++;
543 unlock_user(s
, buf
, len
);
546 /* Return number of bytes not read */
547 common_semi_cb(cs
, len
- i
, 0);
550 static void staticfile_isattyfn(CPUState
*cs
, GuestFD
*gf
)
552 common_semi_cb(cs
, 0, 0);
555 static void staticfile_seekfn(CPUState
*cs
, GuestFD
*gf
, target_ulong offset
)
557 gf
->staticfile
.off
= offset
;
558 common_semi_cb(cs
, 0, 0);
561 static void staticfile_flenfn(CPUState
*cs
, GuestFD
*gf
)
563 common_semi_cb(cs
, gf
->staticfile
.len
, 0);
566 typedef struct GuestFDFunctions
{
567 sys_closefn
*closefn
;
568 sys_writefn
*writefn
;
570 sys_isattyfn
*isattyfn
;
575 static const GuestFDFunctions guestfd_fns
[] = {
577 .closefn
= host_closefn
,
578 .writefn
= host_writefn
,
579 .readfn
= host_readfn
,
580 .isattyfn
= host_isattyfn
,
581 .seekfn
= host_seekfn
,
582 .flenfn
= host_flenfn
,
585 .closefn
= gdb_closefn
,
586 .writefn
= gdb_writefn
,
587 .readfn
= gdb_readfn
,
588 .isattyfn
= gdb_isattyfn
,
589 .seekfn
= gdb_seekfn
,
590 .flenfn
= gdb_flenfn
,
593 .closefn
= staticfile_closefn
,
594 .writefn
= staticfile_writefn
,
595 .readfn
= staticfile_readfn
,
596 .isattyfn
= staticfile_isattyfn
,
597 .seekfn
= staticfile_seekfn
,
598 .flenfn
= staticfile_flenfn
,
603 * Read the input value from the argument block; fail the semihosting
604 * call if the memory read fails. Eventually we could use a generic
605 * CPUState helper function here.
608 #define GET_ARG(n) do { \
609 if (is_64bit_semihosting(env)) { \
610 if (get_user_u64(arg ## n, args + (n) * 8)) { \
614 if (get_user_u32(arg ## n, args + (n) * 4)) { \
620 #define SET_ARG(n, val) \
621 (is_64bit_semihosting(env) ? \
622 put_user_u64(val, args + (n) * 8) : \
623 put_user_u32(val, args + (n) * 4))
627 * Do a semihosting call.
629 * The specification always says that the "return register" either
630 * returns a specific value or is corrupted, so we don't need to
631 * report to our caller whether we are returning a value or trying to
632 * leave the register unchanged. We use 0xdeadbeef as the return value
633 * when there isn't a defined return value for the call.
635 void do_common_semihosting(CPUState
*cs
)
637 CPUArchState
*env
= cs
->env_ptr
;
639 target_ulong arg0
, arg1
, arg2
, arg3
;
648 (void) env
; /* Used implicitly by arm lock_user macro */
649 nr
= common_semi_arg(cs
, 0) & 0xffffffffU
;
650 args
= common_semi_arg(cs
, 1);
653 case TARGET_SYS_OPEN
:
661 s
= lock_user_string(arg0
);
666 unlock_user(s
, arg0
, 0);
667 common_semi_cb(cs
, -1, EINVAL
);
671 if (strcmp(s
, ":tt") == 0) {
673 * We implement SH_EXT_STDOUT_STDERR, so:
674 * open for read == stdin
675 * open for write == stdout
676 * open for append == stderr
679 hostfd
= STDIN_FILENO
;
680 } else if (arg1
< 8) {
681 hostfd
= STDOUT_FILENO
;
683 hostfd
= STDERR_FILENO
;
685 ret
= alloc_guestfd();
686 associate_guestfd(ret
, hostfd
);
687 } else if (strcmp(s
, ":semihosting-features") == 0) {
688 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
689 if (arg1
!= 0 && arg1
!= 1) {
693 ret
= alloc_guestfd();
694 staticfile_guestfd(ret
, featurefile_data
,
695 sizeof(featurefile_data
));
697 } else if (use_gdb_syscalls()) {
698 unlock_user(s
, arg0
, 0);
699 common_semi_open_guestfd
= alloc_guestfd();
700 gdb_do_syscall(common_semi_open_cb
,
701 "open,%s,%x,1a4", arg0
, (int)arg2
+ 1,
702 gdb_open_modeflags
[arg1
]);
705 hostfd
= open(s
, open_modeflags
[arg1
], 0644);
710 ret
= alloc_guestfd();
711 associate_guestfd(ret
, hostfd
);
714 unlock_user(s
, arg0
, 0);
715 common_semi_cb(cs
, ret
, err
);
719 case TARGET_SYS_CLOSE
:
722 gf
= get_guestfd(arg0
);
726 guestfd_fns
[gf
->type
].closefn(cs
, gf
);
727 dealloc_guestfd(arg0
);
730 case TARGET_SYS_WRITEC
:
731 qemu_semihosting_console_outc(cs
->env_ptr
, args
);
732 common_semi_set_ret(cs
, 0xdeadbeef);
735 case TARGET_SYS_WRITE0
:
736 ret
= qemu_semihosting_console_outs(cs
->env_ptr
, args
);
737 common_semi_set_ret(cs
, ret
);
740 case TARGET_SYS_WRITE
:
746 gf
= get_guestfd(arg0
);
750 guestfd_fns
[gf
->type
].writefn(cs
, gf
, arg1
, len
);
753 case TARGET_SYS_READ
:
759 gf
= get_guestfd(arg0
);
763 guestfd_fns
[gf
->type
].readfn(cs
, gf
, arg1
, len
);
766 case TARGET_SYS_READC
:
767 ret
= qemu_semihosting_console_inc(cs
->env_ptr
);
768 common_semi_set_ret(cs
, ret
);
771 case TARGET_SYS_ISERROR
:
773 common_semi_set_ret(cs
, (target_long
)arg0
< 0);
776 case TARGET_SYS_ISTTY
:
779 gf
= get_guestfd(arg0
);
783 guestfd_fns
[gf
->type
].isattyfn(cs
, gf
);
786 case TARGET_SYS_SEEK
:
790 gf
= get_guestfd(arg0
);
794 guestfd_fns
[gf
->type
].seekfn(cs
, gf
, arg1
);
797 case TARGET_SYS_FLEN
:
800 gf
= get_guestfd(arg0
);
804 guestfd_fns
[gf
->type
].flenfn(cs
, gf
);
807 case TARGET_SYS_TMPNAM
:
815 len
= asprintf(&s
, "/tmp/qemu-%x%02x", getpid(), (int)arg1
& 0xff);
816 /* Make sure there's enough space in the buffer */
817 if (len
< 0 || len
>= arg2
) {
818 common_semi_set_ret(cs
, -1);
821 p
= lock_user(VERIFY_WRITE
, arg0
, len
, 0);
825 memcpy(p
, s
, len
+ 1);
826 unlock_user(p
, arg0
, len
);
828 common_semi_set_ret(cs
, 0);
832 case TARGET_SYS_REMOVE
:
835 if (use_gdb_syscalls()) {
836 gdb_do_syscall(common_semi_cb
, "unlink,%s",
837 arg0
, (int)arg1
+ 1);
840 s
= lock_user_string(arg0
);
845 unlock_user(s
, arg0
, 0);
846 common_semi_cb(cs
, ret
, ret
? errno
: 0);
849 case TARGET_SYS_RENAME
:
854 if (use_gdb_syscalls()) {
855 gdb_do_syscall(common_semi_cb
, "rename,%s,%s",
856 arg0
, (int)arg1
+ 1, arg2
, (int)arg3
+ 1);
860 s
= lock_user_string(arg0
);
864 s2
= lock_user_string(arg2
);
866 unlock_user(s
, arg0
, 0);
870 unlock_user(s2
, arg2
, 0);
871 unlock_user(s
, arg0
, 0);
872 common_semi_cb(cs
, ret
, ret
? errno
: 0);
876 case TARGET_SYS_CLOCK
:
877 common_semi_set_ret(cs
, clock() / (CLOCKS_PER_SEC
/ 100));
880 case TARGET_SYS_TIME
:
882 common_semi_cb(cs
, ul_ret
, ul_ret
== -1 ? errno
: 0);
885 case TARGET_SYS_SYSTEM
:
888 if (use_gdb_syscalls()) {
889 gdb_do_syscall(common_semi_cb
, "system,%s", arg0
, (int)arg1
+ 1);
892 s
= lock_user_string(arg0
);
897 unlock_user(s
, arg0
, 0);
898 common_semi_cb(cs
, ret
, ret
== -1 ? errno
: 0);
901 case TARGET_SYS_ERRNO
:
902 common_semi_set_ret(cs
, get_swi_errno(cs
));
905 case TARGET_SYS_GET_CMDLINE
:
907 /* Build a command-line from the original argv.
910 * * arg0, pointer to a buffer of at least the size
912 * * arg1, size of the buffer pointed to by arg0 in
916 * * arg0, pointer to null-terminated string of the
918 * * arg1, length of the string pointed to by arg0.
925 #if !defined(CONFIG_USER_ONLY)
928 TaskState
*ts
= cs
->opaque
;
933 /* Compute the size of the output string. */
934 #if !defined(CONFIG_USER_ONLY)
935 cmdline
= semihosting_get_cmdline();
936 if (cmdline
== NULL
) {
937 cmdline
= ""; /* Default to an empty line. */
939 output_size
= strlen(cmdline
) + 1; /* Count terminating 0. */
943 output_size
= ts
->info
->env_strings
- ts
->info
->arg_strings
;
946 * We special-case the "empty command line" case (argc==0).
947 * Just provide the terminating 0.
953 if (output_size
> input_size
) {
954 /* Not enough space to store command-line arguments. */
955 common_semi_cb(cs
, -1, E2BIG
);
959 /* Adjust the command-line length. */
960 if (SET_ARG(1, output_size
- 1)) {
961 /* Couldn't write back to argument block */
965 /* Lock the buffer on the ARM side. */
966 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
967 if (!output_buffer
) {
971 /* Copy the command-line arguments. */
972 #if !defined(CONFIG_USER_ONLY)
973 pstrcpy(output_buffer
, output_size
, cmdline
);
975 if (output_size
== 1) {
976 /* Empty command-line. */
977 output_buffer
[0] = '\0';
981 if (copy_from_user(output_buffer
, ts
->info
->arg_strings
,
983 unlock_user(output_buffer
, arg0
, 0);
987 /* Separate arguments by white spaces. */
988 for (i
= 0; i
< output_size
- 1; i
++) {
989 if (output_buffer
[i
] == 0) {
990 output_buffer
[i
] = ' ';
995 /* Unlock the buffer on the ARM side. */
996 unlock_user(output_buffer
, arg0
, output_size
);
997 common_semi_cb(cs
, status
, 0);
1001 case TARGET_SYS_HEAPINFO
:
1003 target_ulong retvals
[4];
1005 #ifdef CONFIG_USER_ONLY
1006 TaskState
*ts
= cs
->opaque
;
1009 LayoutInfo info
= common_semi_find_bases(cs
);
1014 #ifdef CONFIG_USER_ONLY
1016 * Some C libraries assume the heap immediately follows .bss, so
1017 * allocate it using sbrk.
1019 if (!ts
->heap_limit
) {
1022 ts
->heap_base
= do_brk(0);
1023 limit
= ts
->heap_base
+ COMMON_SEMI_HEAP_SIZE
;
1024 /* Try a big heap, and reduce the size if that fails. */
1026 ret
= do_brk(limit
);
1030 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
1032 ts
->heap_limit
= limit
;
1035 retvals
[0] = ts
->heap_base
;
1036 retvals
[1] = ts
->heap_limit
;
1037 retvals
[2] = ts
->stack_base
;
1038 retvals
[3] = 0; /* Stack limit. */
1040 retvals
[0] = info
.heapbase
; /* Heap Base */
1041 retvals
[1] = info
.heaplimit
; /* Heap Limit */
1042 retvals
[2] = info
.heaplimit
; /* Stack base */
1043 retvals
[3] = info
.heapbase
; /* Stack limit. */
1046 for (i
= 0; i
< ARRAY_SIZE(retvals
); i
++) {
1049 if (is_64bit_semihosting(env
)) {
1050 fail
= put_user_u64(retvals
[i
], arg0
+ i
* 8);
1052 fail
= put_user_u32(retvals
[i
], arg0
+ i
* 4);
1056 /* Couldn't write back to argument block */
1060 common_semi_set_ret(cs
, 0);
1064 case TARGET_SYS_EXIT
:
1065 case TARGET_SYS_EXIT_EXTENDED
:
1066 if (common_semi_sys_exit_extended(cs
, nr
)) {
1068 * The A64 version of SYS_EXIT takes a parameter block,
1069 * so the application-exit type can return a subcode which
1070 * is the exit status code from the application.
1071 * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
1072 * which allows A32/T32 guests to also provide a status code.
1077 if (arg0
== ADP_Stopped_ApplicationExit
) {
1084 * The A32/T32 version of SYS_EXIT specifies only
1085 * Stopped_ApplicationExit as normal exit, but does not
1086 * allow the guest to specify the exit status code.
1087 * Everything else is considered an error.
1089 ret
= (args
== ADP_Stopped_ApplicationExit
) ? 0 : 1;
1094 case TARGET_SYS_ELAPSED
:
1095 elapsed
= get_clock() - clock_start
;
1096 if (sizeof(target_ulong
) == 8) {
1097 SET_ARG(0, elapsed
);
1099 SET_ARG(0, (uint32_t) elapsed
);
1100 SET_ARG(1, (uint32_t) (elapsed
>> 32));
1102 common_semi_set_ret(cs
, 0);
1105 case TARGET_SYS_TICKFREQ
:
1106 /* qemu always uses nsec */
1107 common_semi_set_ret(cs
, 1000000000);
1110 case TARGET_SYS_SYNCCACHE
:
1112 * Clean the D-cache and invalidate the I-cache for the specified
1113 * virtual address range. This is a nop for us since we don't
1114 * implement caches. This is only present on A64.
1116 if (common_semi_has_synccache(env
)) {
1117 common_semi_set_ret(cs
, 0);
1122 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
1123 cpu_dump_state(cs
, stderr
, 0);
1127 common_semi_cb(cs
, -1, EBADF
);
1130 common_semi_cb(cs
, -1, EFAULT
);