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 #define GDB_O_RDONLY 0x000
90 #define GDB_O_WRONLY 0x001
91 #define GDB_O_RDWR 0x002
92 #define GDB_O_APPEND 0x008
93 #define GDB_O_CREAT 0x200
94 #define GDB_O_TRUNC 0x400
95 #define GDB_O_BINARY 0
97 static int gdb_open_modeflags
[12] = {
99 GDB_O_RDONLY
| GDB_O_BINARY
,
101 GDB_O_RDWR
| GDB_O_BINARY
,
102 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
,
103 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
104 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
,
105 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
106 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
,
107 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
,
108 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
,
109 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
112 static int open_modeflags
[12] = {
117 O_WRONLY
| O_CREAT
| O_TRUNC
,
118 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
119 O_RDWR
| O_CREAT
| O_TRUNC
,
120 O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
,
121 O_WRONLY
| O_CREAT
| O_APPEND
,
122 O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
,
123 O_RDWR
| O_CREAT
| O_APPEND
,
124 O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
127 #ifndef CONFIG_USER_ONLY
130 * common_semi_find_bases: find information about ram and heap base
132 * This function attempts to provide meaningful numbers for RAM and
133 * HEAP base addresses. The rambase is simply the lowest addressable
134 * RAM position. For the heapbase we ask the loader to scan the
135 * address space and the largest available gap by querying the "ROM"
138 * Returns: a structure with the numbers we need.
141 typedef struct LayoutInfo
{
142 target_ulong rambase
;
148 static bool find_ram_cb(Int128 start
, Int128 len
, const MemoryRegion
*mr
,
149 hwaddr offset_in_region
, void *opaque
)
151 LayoutInfo
*info
= (LayoutInfo
*) opaque
;
152 uint64_t size
= int128_get64(len
);
154 if (!mr
->ram
|| mr
->readonly
) {
158 if (size
> info
->ramsize
) {
159 info
->rambase
= int128_get64(start
);
160 info
->ramsize
= size
;
163 /* search exhaustively for largest RAM */
167 static LayoutInfo
common_semi_find_bases(CPUState
*cs
)
170 LayoutInfo info
= { 0, 0, 0, 0 };
172 RCU_READ_LOCK_GUARD();
174 fv
= address_space_to_flatview(cs
->as
);
175 flatview_for_each_range(fv
, find_ram_cb
, &info
);
178 * If we have found the RAM lets iterate through the ROM blobs to
179 * work out the best place for the remainder of RAM and split it
180 * equally between stack and heap.
182 if (info
.rambase
|| info
.ramsize
> 0) {
183 RomGap gap
= rom_find_largest_gap_between(info
.rambase
, info
.ramsize
);
184 info
.heapbase
= gap
.base
;
185 info
.heaplimit
= gap
.base
+ gap
.size
;
194 static inline target_ulong
195 common_semi_arg(CPUState
*cs
, int argno
)
197 ARMCPU
*cpu
= ARM_CPU(cs
);
198 CPUARMState
*env
= &cpu
->env
;
200 return env
->xregs
[argno
];
202 return env
->regs
[argno
];
207 common_semi_set_ret(CPUState
*cs
, target_ulong ret
)
209 ARMCPU
*cpu
= ARM_CPU(cs
);
210 CPUARMState
*env
= &cpu
->env
;
219 common_semi_sys_exit_extended(CPUState
*cs
, int nr
)
221 return (nr
== TARGET_SYS_EXIT_EXTENDED
|| is_a64(cs
->env_ptr
));
224 #endif /* TARGET_ARM */
227 static inline target_ulong
228 common_semi_arg(CPUState
*cs
, int argno
)
230 RISCVCPU
*cpu
= RISCV_CPU(cs
);
231 CPURISCVState
*env
= &cpu
->env
;
232 return env
->gpr
[xA0
+ argno
];
236 common_semi_set_ret(CPUState
*cs
, target_ulong ret
)
238 RISCVCPU
*cpu
= RISCV_CPU(cs
);
239 CPURISCVState
*env
= &cpu
->env
;
244 common_semi_sys_exit_extended(CPUState
*cs
, int nr
)
246 return (nr
== TARGET_SYS_EXIT_EXTENDED
|| sizeof(target_ulong
) == 8);
252 * The semihosting API has no concept of its errno being thread-safe,
253 * as the API design predates SMP CPUs and was intended as a simple
254 * real-hardware set of debug functionality. For QEMU, we make the
255 * errno be per-thread in linux-user mode; in softmmu it is a simple
256 * global, and we assume that the guest takes care of avoiding any races.
258 #ifndef CONFIG_USER_ONLY
259 static target_ulong syscall_err
;
261 #include "semihosting/softmmu-uaccess.h"
264 static inline uint32_t set_swi_errno(CPUState
*cs
, uint32_t code
)
266 if (code
== (uint32_t)-1) {
267 #ifdef CONFIG_USER_ONLY
268 TaskState
*ts
= cs
->opaque
;
270 ts
->swi_errno
= errno
;
278 static inline uint32_t get_swi_errno(CPUState
*cs
)
280 #ifdef CONFIG_USER_ONLY
281 TaskState
*ts
= cs
->opaque
;
283 return ts
->swi_errno
;
289 static target_ulong common_semi_syscall_len
;
291 static void common_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
294 #ifdef CONFIG_USER_ONLY
295 TaskState
*ts
= cs
->opaque
;
301 /* Fixup syscalls that use nonstardard return conventions. */
302 target_ulong reg0
= common_semi_arg(cs
, 0);
304 case TARGET_SYS_WRITE
:
305 case TARGET_SYS_READ
:
306 ret
= common_semi_syscall_len
- ret
;
308 case TARGET_SYS_SEEK
:
315 common_semi_set_ret(cs
, ret
);
318 static target_ulong
common_semi_flen_buf(CPUState
*cs
)
322 /* Return an address in target memory of 64 bytes where the remote
323 * gdb should write its stat struct. (The format of this structure
324 * is defined by GDB's remote protocol and is not target-specific.)
325 * We put this on the guest's stack just below SP.
327 ARMCPU
*cpu
= ARM_CPU(cs
);
328 CPUARMState
*env
= &cpu
->env
;
337 RISCVCPU
*cpu
= RISCV_CPU(cs
);
338 CPURISCVState
*env
= &cpu
->env
;
347 common_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
349 /* The size is always stored in big-endian order, extract
350 the value. We assume the size always fit in 32 bits. */
352 cpu_memory_rw_debug(cs
, common_semi_flen_buf(cs
) + 32,
353 (uint8_t *)&size
, 4, 0);
354 size
= be32_to_cpu(size
);
355 common_semi_set_ret(cs
, size
);
357 set_swi_errno(cs
, -1);
360 static int common_semi_open_guestfd
;
363 common_semi_open_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
365 if (ret
== (target_ulong
)-1) {
367 set_swi_errno(cs
, -1);
368 dealloc_guestfd(common_semi_open_guestfd
);
370 associate_guestfd(common_semi_open_guestfd
, ret
);
371 ret
= common_semi_open_guestfd
;
373 common_semi_set_ret(cs
, ret
);
377 common_semi_gdb_syscall(CPUState
*cs
, gdb_syscall_complete_cb cb
,
378 const char *fmt
, ...)
383 gdb_do_syscallv(cb
, fmt
, va
);
387 * FIXME: in softmmu mode, the gdbstub will schedule our callback
388 * to occur, but will not actually call it to complete the syscall
389 * until after this function has returned and we are back in the
390 * CPU main loop. Therefore callers to this function must not
391 * do anything with its return value, because it is not necessarily
392 * the result of the syscall, but could just be the old value of X0.
393 * The only thing safe to do with this is that the callers of
394 * do_common_semihosting() will write it straight back into X0.
395 * (In linux-user mode, the callback will have happened before
396 * gdb_do_syscallv() returns.)
398 * We should tidy this up so neither this function nor
399 * do_common_semihosting() return a value, so the mistake of
400 * doing something with the return value is not possible to make.
403 return common_semi_arg(cs
, 0);
407 * Types for functions implementing various semihosting calls
408 * for specific types of guest file descriptor. These must all
409 * do the work and return the required return value for the guest,
410 * setting the guest errno if appropriate.
412 typedef uint32_t sys_closefn(CPUState
*cs
, GuestFD
*gf
);
413 typedef uint32_t sys_writefn(CPUState
*cs
, GuestFD
*gf
,
414 target_ulong buf
, uint32_t len
);
415 typedef uint32_t sys_readfn(CPUState
*cs
, GuestFD
*gf
,
416 target_ulong buf
, uint32_t len
);
417 typedef uint32_t sys_isattyfn(CPUState
*cs
, GuestFD
*gf
);
418 typedef uint32_t sys_seekfn(CPUState
*cs
, GuestFD
*gf
,
419 target_ulong offset
);
420 typedef uint32_t sys_flenfn(CPUState
*cs
, GuestFD
*gf
);
422 static uint32_t host_closefn(CPUState
*cs
, GuestFD
*gf
)
425 * Only close the underlying host fd if it's one we opened on behalf
426 * of the guest in SYS_OPEN.
428 if (gf
->hostfd
== STDIN_FILENO
||
429 gf
->hostfd
== STDOUT_FILENO
||
430 gf
->hostfd
== STDERR_FILENO
) {
433 return set_swi_errno(cs
, close(gf
->hostfd
));
436 static uint32_t host_writefn(CPUState
*cs
, GuestFD
*gf
,
437 target_ulong buf
, uint32_t len
)
439 CPUArchState
*env
= cs
->env_ptr
;
441 char *s
= lock_user(VERIFY_READ
, buf
, len
, 1);
442 (void) env
; /* Used in arm softmmu lock_user implicitly */
444 /* Return bytes not written on error */
447 ret
= set_swi_errno(cs
, write(gf
->hostfd
, s
, len
));
448 unlock_user(s
, buf
, 0);
449 if (ret
== (uint32_t)-1) {
452 /* Return bytes not written */
456 static uint32_t host_readfn(CPUState
*cs
, GuestFD
*gf
,
457 target_ulong buf
, uint32_t len
)
459 CPUArchState
*env
= cs
->env_ptr
;
461 char *s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
462 (void) env
; /* Used in arm softmmu lock_user implicitly */
464 /* return bytes not read */
468 ret
= set_swi_errno(cs
, read(gf
->hostfd
, s
, len
));
469 } while (ret
== -1 && errno
== EINTR
);
470 unlock_user(s
, buf
, len
);
471 if (ret
== (uint32_t)-1) {
474 /* Return bytes not read */
478 static uint32_t host_isattyfn(CPUState
*cs
, GuestFD
*gf
)
480 return isatty(gf
->hostfd
);
483 static uint32_t host_seekfn(CPUState
*cs
, GuestFD
*gf
, target_ulong offset
)
485 uint32_t ret
= set_swi_errno(cs
, lseek(gf
->hostfd
, offset
, SEEK_SET
));
486 if (ret
== (uint32_t)-1) {
492 static uint32_t host_flenfn(CPUState
*cs
, GuestFD
*gf
)
495 uint32_t ret
= set_swi_errno(cs
, fstat(gf
->hostfd
, &buf
));
496 if (ret
== (uint32_t)-1) {
502 static uint32_t gdb_closefn(CPUState
*cs
, GuestFD
*gf
)
504 return common_semi_gdb_syscall(cs
, common_semi_cb
, "close,%x", gf
->hostfd
);
507 static uint32_t gdb_writefn(CPUState
*cs
, GuestFD
*gf
,
508 target_ulong buf
, uint32_t len
)
510 common_semi_syscall_len
= len
;
511 return common_semi_gdb_syscall(cs
, common_semi_cb
, "write,%x,%x,%x",
512 gf
->hostfd
, buf
, len
);
515 static uint32_t gdb_readfn(CPUState
*cs
, GuestFD
*gf
,
516 target_ulong buf
, uint32_t len
)
518 common_semi_syscall_len
= len
;
519 return common_semi_gdb_syscall(cs
, common_semi_cb
, "read,%x,%x,%x",
520 gf
->hostfd
, buf
, len
);
523 static uint32_t gdb_isattyfn(CPUState
*cs
, GuestFD
*gf
)
525 return common_semi_gdb_syscall(cs
, common_semi_cb
, "isatty,%x", gf
->hostfd
);
528 static uint32_t gdb_seekfn(CPUState
*cs
, GuestFD
*gf
, target_ulong offset
)
530 return common_semi_gdb_syscall(cs
, common_semi_cb
, "lseek,%x,%x,0",
534 static uint32_t gdb_flenfn(CPUState
*cs
, GuestFD
*gf
)
536 return common_semi_gdb_syscall(cs
, common_semi_flen_cb
, "fstat,%x,%x",
537 gf
->hostfd
, common_semi_flen_buf(cs
));
540 #define SHFB_MAGIC_0 0x53
541 #define SHFB_MAGIC_1 0x48
542 #define SHFB_MAGIC_2 0x46
543 #define SHFB_MAGIC_3 0x42
545 /* Feature bits reportable in feature byte 0 */
546 #define SH_EXT_EXIT_EXTENDED (1 << 0)
547 #define SH_EXT_STDOUT_STDERR (1 << 1)
549 static const uint8_t featurefile_data
[] = {
554 SH_EXT_EXIT_EXTENDED
| SH_EXT_STDOUT_STDERR
, /* Feature byte 0 */
557 static uint32_t staticfile_closefn(CPUState
*cs
, GuestFD
*gf
)
563 static uint32_t staticfile_writefn(CPUState
*cs
, GuestFD
*gf
,
564 target_ulong buf
, uint32_t len
)
566 /* This fd can never be open for writing */
569 return set_swi_errno(cs
, -1);
572 static uint32_t staticfile_readfn(CPUState
*cs
, GuestFD
*gf
,
573 target_ulong buf
, uint32_t len
)
575 CPUArchState
*env
= cs
->env_ptr
;
579 (void) env
; /* Used in arm softmmu lock_user implicitly */
580 s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
585 for (i
= 0; i
< len
; i
++) {
586 if (gf
->staticfile
.off
>= gf
->staticfile
.len
) {
589 s
[i
] = gf
->staticfile
.data
[gf
->staticfile
.off
];
590 gf
->staticfile
.off
++;
593 unlock_user(s
, buf
, len
);
595 /* Return number of bytes not read */
599 static uint32_t staticfile_isattyfn(CPUState
*cs
, GuestFD
*gf
)
604 static uint32_t staticfile_seekfn(CPUState
*cs
, GuestFD
*gf
,
607 gf
->staticfile
.off
= offset
;
611 static uint32_t staticfile_flenfn(CPUState
*cs
, GuestFD
*gf
)
613 return gf
->staticfile
.len
;
616 typedef struct GuestFDFunctions
{
617 sys_closefn
*closefn
;
618 sys_writefn
*writefn
;
620 sys_isattyfn
*isattyfn
;
625 static const GuestFDFunctions guestfd_fns
[] = {
627 .closefn
= host_closefn
,
628 .writefn
= host_writefn
,
629 .readfn
= host_readfn
,
630 .isattyfn
= host_isattyfn
,
631 .seekfn
= host_seekfn
,
632 .flenfn
= host_flenfn
,
635 .closefn
= gdb_closefn
,
636 .writefn
= gdb_writefn
,
637 .readfn
= gdb_readfn
,
638 .isattyfn
= gdb_isattyfn
,
639 .seekfn
= gdb_seekfn
,
640 .flenfn
= gdb_flenfn
,
643 .closefn
= staticfile_closefn
,
644 .writefn
= staticfile_writefn
,
645 .readfn
= staticfile_readfn
,
646 .isattyfn
= staticfile_isattyfn
,
647 .seekfn
= staticfile_seekfn
,
648 .flenfn
= staticfile_flenfn
,
653 * Read the input value from the argument block; fail the semihosting
654 * call if the memory read fails. Eventually we could use a generic
655 * CPUState helper function here.
657 static inline bool is_64bit_semihosting(CPUArchState
*env
)
659 #if defined(TARGET_ARM)
661 #elif defined(TARGET_RISCV)
662 return riscv_cpu_mxl(env
) != MXL_RV32
;
664 #error un-handled architecture
669 #define GET_ARG(n) do { \
670 if (is_64bit_semihosting(env)) { \
671 if (get_user_u64(arg ## n, args + (n) * 8)) { \
673 return set_swi_errno(cs, -1); \
676 if (get_user_u32(arg ## n, args + (n) * 4)) { \
678 return set_swi_errno(cs, -1); \
683 #define SET_ARG(n, val) \
684 (is_64bit_semihosting(env) ? \
685 put_user_u64(val, args + (n) * 8) : \
686 put_user_u32(val, args + (n) * 4))
690 * Do a semihosting call.
692 * The specification always says that the "return register" either
693 * returns a specific value or is corrupted, so we don't need to
694 * report to our caller whether we are returning a value or trying to
695 * leave the register unchanged. We use 0xdeadbeef as the return value
696 * when there isn't a defined return value for the call.
698 target_ulong
do_common_semihosting(CPUState
*cs
)
700 CPUArchState
*env
= cs
->env_ptr
;
702 target_ulong arg0
, arg1
, arg2
, arg3
;
711 (void) env
; /* Used implicitly by arm lock_user macro */
712 nr
= common_semi_arg(cs
, 0) & 0xffffffffU
;
713 args
= common_semi_arg(cs
, 1);
716 case TARGET_SYS_OPEN
:
723 s
= lock_user_string(arg0
);
726 return set_swi_errno(cs
, -1);
729 unlock_user(s
, arg0
, 0);
731 return set_swi_errno(cs
, -1);
734 guestfd
= alloc_guestfd();
736 unlock_user(s
, arg0
, 0);
738 return set_swi_errno(cs
, -1);
741 if (strcmp(s
, ":tt") == 0) {
745 * We implement SH_EXT_STDOUT_STDERR, so:
746 * open for read == stdin
747 * open for write == stdout
748 * open for append == stderr
751 result_fileno
= STDIN_FILENO
;
752 } else if (arg1
< 8) {
753 result_fileno
= STDOUT_FILENO
;
755 result_fileno
= STDERR_FILENO
;
757 associate_guestfd(guestfd
, result_fileno
);
758 unlock_user(s
, arg0
, 0);
761 if (strcmp(s
, ":semihosting-features") == 0) {
762 unlock_user(s
, arg0
, 0);
763 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
764 if (arg1
!= 0 && arg1
!= 1) {
765 dealloc_guestfd(guestfd
);
767 return set_swi_errno(cs
, -1);
769 staticfile_guestfd(guestfd
, featurefile_data
,
770 sizeof(featurefile_data
));
774 if (use_gdb_syscalls()) {
775 common_semi_open_guestfd
= guestfd
;
776 ret
= common_semi_gdb_syscall(cs
, common_semi_open_cb
,
777 "open,%s,%x,1a4", arg0
, (int)arg2
+ 1,
778 gdb_open_modeflags
[arg1
]);
780 ret
= set_swi_errno(cs
, open(s
, open_modeflags
[arg1
], 0644));
781 if (ret
== (uint32_t)-1) {
782 dealloc_guestfd(guestfd
);
784 associate_guestfd(guestfd
, ret
);
788 unlock_user(s
, arg0
, 0);
791 case TARGET_SYS_CLOSE
:
794 gf
= get_guestfd(arg0
);
797 return set_swi_errno(cs
, -1);
800 ret
= guestfd_fns
[gf
->type
].closefn(cs
, gf
);
801 dealloc_guestfd(arg0
);
803 case TARGET_SYS_WRITEC
:
804 qemu_semihosting_console_outc(cs
->env_ptr
, args
);
806 case TARGET_SYS_WRITE0
:
807 return qemu_semihosting_console_outs(cs
->env_ptr
, args
);
808 case TARGET_SYS_WRITE
:
814 gf
= get_guestfd(arg0
);
817 return set_swi_errno(cs
, -1);
820 return guestfd_fns
[gf
->type
].writefn(cs
, gf
, arg1
, len
);
821 case TARGET_SYS_READ
:
827 gf
= get_guestfd(arg0
);
830 return set_swi_errno(cs
, -1);
833 return guestfd_fns
[gf
->type
].readfn(cs
, gf
, arg1
, len
);
834 case TARGET_SYS_READC
:
835 return qemu_semihosting_console_inc(cs
->env_ptr
);
836 case TARGET_SYS_ISERROR
:
838 return (target_long
) arg0
< 0 ? 1 : 0;
839 case TARGET_SYS_ISTTY
:
842 gf
= get_guestfd(arg0
);
845 return set_swi_errno(cs
, -1);
848 return guestfd_fns
[gf
->type
].isattyfn(cs
, gf
);
849 case TARGET_SYS_SEEK
:
853 gf
= get_guestfd(arg0
);
856 return set_swi_errno(cs
, -1);
859 return guestfd_fns
[gf
->type
].seekfn(cs
, gf
, arg1
);
860 case TARGET_SYS_FLEN
:
863 gf
= get_guestfd(arg0
);
866 return set_swi_errno(cs
, -1);
869 return guestfd_fns
[gf
->type
].flenfn(cs
, gf
);
870 case TARGET_SYS_TMPNAM
:
874 if (asprintf(&s
, "/tmp/qemu-%x%02x", getpid(),
875 (int) (arg1
& 0xff)) < 0) {
878 ul_ret
= (target_ulong
) -1;
880 /* Make sure there's enough space in the buffer */
881 if (strlen(s
) < arg2
) {
882 char *output
= lock_user(VERIFY_WRITE
, arg0
, arg2
, 0);
884 unlock_user(output
, arg0
, arg2
);
889 case TARGET_SYS_REMOVE
:
892 if (use_gdb_syscalls()) {
893 ret
= common_semi_gdb_syscall(cs
, common_semi_cb
, "unlink,%s",
894 arg0
, (int)arg1
+ 1);
896 s
= lock_user_string(arg0
);
899 return set_swi_errno(cs
, -1);
901 ret
= set_swi_errno(cs
, remove(s
));
902 unlock_user(s
, arg0
, 0);
905 case TARGET_SYS_RENAME
:
910 if (use_gdb_syscalls()) {
911 return common_semi_gdb_syscall(cs
, common_semi_cb
, "rename,%s,%s",
912 arg0
, (int)arg1
+ 1, arg2
,
916 s
= lock_user_string(arg0
);
917 s2
= lock_user_string(arg2
);
920 ret
= set_swi_errno(cs
, -1);
922 ret
= set_swi_errno(cs
, rename(s
, s2
));
925 unlock_user(s2
, arg2
, 0);
927 unlock_user(s
, arg0
, 0);
930 case TARGET_SYS_CLOCK
:
931 return clock() / (CLOCKS_PER_SEC
/ 100);
932 case TARGET_SYS_TIME
:
933 return set_swi_errno(cs
, time(NULL
));
934 case TARGET_SYS_SYSTEM
:
937 if (use_gdb_syscalls()) {
938 return common_semi_gdb_syscall(cs
, common_semi_cb
, "system,%s",
939 arg0
, (int)arg1
+ 1);
941 s
= lock_user_string(arg0
);
944 return set_swi_errno(cs
, -1);
946 ret
= set_swi_errno(cs
, system(s
));
947 unlock_user(s
, arg0
, 0);
950 case TARGET_SYS_ERRNO
:
951 return get_swi_errno(cs
);
952 case TARGET_SYS_GET_CMDLINE
:
954 /* Build a command-line from the original argv.
957 * * arg0, pointer to a buffer of at least the size
959 * * arg1, size of the buffer pointed to by arg0 in
963 * * arg0, pointer to null-terminated string of the
965 * * arg1, length of the string pointed to by arg0.
972 #if !defined(CONFIG_USER_ONLY)
975 TaskState
*ts
= cs
->opaque
;
980 /* Compute the size of the output string. */
981 #if !defined(CONFIG_USER_ONLY)
982 cmdline
= semihosting_get_cmdline();
983 if (cmdline
== NULL
) {
984 cmdline
= ""; /* Default to an empty line. */
986 output_size
= strlen(cmdline
) + 1; /* Count terminating 0. */
990 output_size
= ts
->info
->env_strings
- ts
->info
->arg_strings
;
993 * We special-case the "empty command line" case (argc==0).
994 * Just provide the terminating 0.
1000 if (output_size
> input_size
) {
1001 /* Not enough space to store command-line arguments. */
1003 return set_swi_errno(cs
, -1);
1006 /* Adjust the command-line length. */
1007 if (SET_ARG(1, output_size
- 1)) {
1008 /* Couldn't write back to argument block */
1010 return set_swi_errno(cs
, -1);
1013 /* Lock the buffer on the ARM side. */
1014 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
1015 if (!output_buffer
) {
1017 return set_swi_errno(cs
, -1);
1020 /* Copy the command-line arguments. */
1021 #if !defined(CONFIG_USER_ONLY)
1022 pstrcpy(output_buffer
, output_size
, cmdline
);
1024 if (output_size
== 1) {
1025 /* Empty command-line. */
1026 output_buffer
[0] = '\0';
1030 if (copy_from_user(output_buffer
, ts
->info
->arg_strings
,
1033 status
= set_swi_errno(cs
, -1);
1037 /* Separate arguments by white spaces. */
1038 for (i
= 0; i
< output_size
- 1; i
++) {
1039 if (output_buffer
[i
] == 0) {
1040 output_buffer
[i
] = ' ';
1045 /* Unlock the buffer on the ARM side. */
1046 unlock_user(output_buffer
, arg0
, output_size
);
1050 case TARGET_SYS_HEAPINFO
:
1052 target_ulong retvals
[4];
1054 #ifdef CONFIG_USER_ONLY
1055 TaskState
*ts
= cs
->opaque
;
1058 LayoutInfo info
= common_semi_find_bases(cs
);
1063 #ifdef CONFIG_USER_ONLY
1065 * Some C libraries assume the heap immediately follows .bss, so
1066 * allocate it using sbrk.
1068 if (!ts
->heap_limit
) {
1071 ts
->heap_base
= do_brk(0);
1072 limit
= ts
->heap_base
+ COMMON_SEMI_HEAP_SIZE
;
1073 /* Try a big heap, and reduce the size if that fails. */
1075 ret
= do_brk(limit
);
1079 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
1081 ts
->heap_limit
= limit
;
1084 retvals
[0] = ts
->heap_base
;
1085 retvals
[1] = ts
->heap_limit
;
1086 retvals
[2] = ts
->stack_base
;
1087 retvals
[3] = 0; /* Stack limit. */
1089 retvals
[0] = info
.heapbase
; /* Heap Base */
1090 retvals
[1] = info
.heaplimit
; /* Heap Limit */
1091 retvals
[2] = info
.heaplimit
; /* Stack base */
1092 retvals
[3] = info
.heapbase
; /* Stack limit. */
1095 for (i
= 0; i
< ARRAY_SIZE(retvals
); i
++) {
1098 if (is_64bit_semihosting(env
)) {
1099 fail
= put_user_u64(retvals
[i
], arg0
+ i
* 8);
1101 fail
= put_user_u32(retvals
[i
], arg0
+ i
* 4);
1105 /* Couldn't write back to argument block */
1107 return set_swi_errno(cs
, -1);
1112 case TARGET_SYS_EXIT
:
1113 case TARGET_SYS_EXIT_EXTENDED
:
1114 if (common_semi_sys_exit_extended(cs
, nr
)) {
1116 * The A64 version of SYS_EXIT takes a parameter block,
1117 * so the application-exit type can return a subcode which
1118 * is the exit status code from the application.
1119 * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
1120 * which allows A32/T32 guests to also provide a status code.
1125 if (arg0
== ADP_Stopped_ApplicationExit
) {
1132 * The A32/T32 version of SYS_EXIT specifies only
1133 * Stopped_ApplicationExit as normal exit, but does not
1134 * allow the guest to specify the exit status code.
1135 * Everything else is considered an error.
1137 ret
= (args
== ADP_Stopped_ApplicationExit
) ? 0 : 1;
1141 case TARGET_SYS_ELAPSED
:
1142 elapsed
= get_clock() - clock_start
;
1143 if (sizeof(target_ulong
) == 8) {
1144 SET_ARG(0, elapsed
);
1146 SET_ARG(0, (uint32_t) elapsed
);
1147 SET_ARG(1, (uint32_t) (elapsed
>> 32));
1150 case TARGET_SYS_TICKFREQ
:
1151 /* qemu always uses nsec */
1153 case TARGET_SYS_SYNCCACHE
:
1155 * Clean the D-cache and invalidate the I-cache for the specified
1156 * virtual address range. This is a nop for us since we don't
1157 * implement caches. This is only present on A64.
1160 if (is_a64(cs
->env_ptr
)) {
1167 /* fall through -- invalid for A32/T32 */
1169 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
1170 cpu_dump_state(cs
, stderr
, 0);