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"
37 #include "semihosting/semihost.h"
38 #include "semihosting/console.h"
39 #include "semihosting/common-semi.h"
41 #include "qemu/timer.h"
42 #ifdef CONFIG_USER_ONLY
45 #define COMMON_SEMI_HEAP_SIZE (128 * 1024 * 1024)
47 #include "exec/gdbstub.h"
48 #include "qemu/cutils.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 typedef enum GuestFDType
{
131 GuestFDFeatureFile
= 3,
135 * Guest file descriptors are integer indexes into an array of
136 * these structures (we will dynamically resize as necessary).
138 typedef struct GuestFD
{
142 target_ulong featurefile_offset
;
146 static GArray
*guestfd_array
;
148 #ifndef CONFIG_USER_ONLY
149 #include "exec/address-spaces.h"
151 * Find the base of a RAM region containing the specified address
154 common_semi_find_region_base(hwaddr addr
)
156 MemoryRegion
*subregion
;
159 * Find the chunk of R/W memory containing the address. This is
160 * used for the SYS_HEAPINFO semihosting call, which should
161 * probably be using information from the loaded application.
163 QTAILQ_FOREACH(subregion
, &get_system_memory()->subregions
,
165 if (subregion
->ram
&& !subregion
->readonly
) {
166 Int128 top128
= int128_add(int128_make64(subregion
->addr
),
168 Int128 addr128
= int128_make64(addr
);
169 if (subregion
->addr
<= addr
&& int128_lt(addr128
, top128
)) {
170 return subregion
->addr
;
179 static inline target_ulong
180 common_semi_arg(CPUState
*cs
, int argno
)
182 ARMCPU
*cpu
= ARM_CPU(cs
);
183 CPUARMState
*env
= &cpu
->env
;
185 return env
->xregs
[argno
];
187 return env
->regs
[argno
];
192 common_semi_set_ret(CPUState
*cs
, target_ulong ret
)
194 ARMCPU
*cpu
= ARM_CPU(cs
);
195 CPUARMState
*env
= &cpu
->env
;
204 common_semi_sys_exit_extended(CPUState
*cs
, int nr
)
206 return (nr
== TARGET_SYS_EXIT_EXTENDED
|| is_a64(cs
->env_ptr
));
209 #ifndef CONFIG_USER_ONLY
210 #include "hw/arm/boot.h"
211 static inline target_ulong
212 common_semi_rambase(CPUState
*cs
)
214 CPUArchState
*env
= cs
->env_ptr
;
215 const struct arm_boot_info
*info
= env
->boot_info
;
219 return info
->loader_start
;
227 return common_semi_find_region_base(sp
);
231 #endif /* TARGET_ARM */
234 static inline target_ulong
235 common_semi_arg(CPUState
*cs
, int argno
)
237 RISCVCPU
*cpu
= RISCV_CPU(cs
);
238 CPURISCVState
*env
= &cpu
->env
;
239 return env
->gpr
[xA0
+ argno
];
243 common_semi_set_ret(CPUState
*cs
, target_ulong ret
)
245 RISCVCPU
*cpu
= RISCV_CPU(cs
);
246 CPURISCVState
*env
= &cpu
->env
;
251 common_semi_sys_exit_extended(CPUState
*cs
, int nr
)
253 return (nr
== TARGET_SYS_EXIT_EXTENDED
|| sizeof(target_ulong
) == 8);
256 #ifndef CONFIG_USER_ONLY
258 static inline target_ulong
259 common_semi_rambase(CPUState
*cs
)
261 RISCVCPU
*cpu
= RISCV_CPU(cs
);
262 CPURISCVState
*env
= &cpu
->env
;
263 return common_semi_find_region_base(env
->gpr
[xSP
]);
270 * Allocate a new guest file descriptor and return it; if we
271 * couldn't allocate a new fd then return -1.
272 * This is a fairly simplistic implementation because we don't
273 * expect that most semihosting guest programs will make very
274 * heavy use of opening and closing fds.
276 static int alloc_guestfd(void)
280 if (!guestfd_array
) {
281 /* New entries zero-initialized, i.e. type GuestFDUnused */
282 guestfd_array
= g_array_new(FALSE
, TRUE
, sizeof(GuestFD
));
285 /* SYS_OPEN should return nonzero handle on success. Start guestfd from 1 */
286 for (i
= 1; i
< guestfd_array
->len
; i
++) {
287 GuestFD
*gf
= &g_array_index(guestfd_array
, GuestFD
, i
);
289 if (gf
->type
== GuestFDUnused
) {
294 /* All elements already in use: expand the array */
295 g_array_set_size(guestfd_array
, i
+ 1);
300 * Look up the guestfd in the data structure; return NULL
301 * for out of bounds, but don't check whether the slot is unused.
302 * This is used internally by the other guestfd functions.
304 static GuestFD
*do_get_guestfd(int guestfd
)
306 if (!guestfd_array
) {
310 if (guestfd
<= 0 || guestfd
>= guestfd_array
->len
) {
314 return &g_array_index(guestfd_array
, GuestFD
, guestfd
);
318 * Associate the specified guest fd (which must have been
319 * allocated via alloc_fd() and not previously used) with
320 * the specified host/gdb fd.
322 static void associate_guestfd(int guestfd
, int hostfd
)
324 GuestFD
*gf
= do_get_guestfd(guestfd
);
327 gf
->type
= use_gdb_syscalls() ? GuestFDGDB
: GuestFDHost
;
332 * Deallocate the specified guest file descriptor. This doesn't
333 * close the host fd, it merely undoes the work of alloc_fd().
335 static void dealloc_guestfd(int guestfd
)
337 GuestFD
*gf
= do_get_guestfd(guestfd
);
340 gf
->type
= GuestFDUnused
;
344 * Given a guest file descriptor, get the associated struct.
345 * If the fd is not valid, return NULL. This is the function
346 * used by the various semihosting calls to validate a handle
348 * Note: calling alloc_guestfd() or dealloc_guestfd() will
349 * invalidate any GuestFD* obtained by calling this function.
351 static GuestFD
*get_guestfd(int guestfd
)
353 GuestFD
*gf
= do_get_guestfd(guestfd
);
355 if (!gf
|| gf
->type
== GuestFDUnused
) {
362 * The semihosting API has no concept of its errno being thread-safe,
363 * as the API design predates SMP CPUs and was intended as a simple
364 * real-hardware set of debug functionality. For QEMU, we make the
365 * errno be per-thread in linux-user mode; in softmmu it is a simple
366 * global, and we assume that the guest takes care of avoiding any races.
368 #ifndef CONFIG_USER_ONLY
369 static target_ulong syscall_err
;
371 #include "exec/softmmu-semi.h"
374 static inline uint32_t set_swi_errno(CPUState
*cs
, uint32_t code
)
376 if (code
== (uint32_t)-1) {
377 #ifdef CONFIG_USER_ONLY
378 TaskState
*ts
= cs
->opaque
;
380 ts
->swi_errno
= errno
;
388 static inline uint32_t get_swi_errno(CPUState
*cs
)
390 #ifdef CONFIG_USER_ONLY
391 TaskState
*ts
= cs
->opaque
;
393 return ts
->swi_errno
;
399 static target_ulong common_semi_syscall_len
;
401 static void common_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
403 target_ulong reg0
= common_semi_arg(cs
, 0);
405 if (ret
== (target_ulong
)-1) {
407 set_swi_errno(cs
, -1);
410 /* Fixup syscalls that use nonstardard return conventions. */
412 case TARGET_SYS_WRITE
:
413 case TARGET_SYS_READ
:
414 reg0
= common_semi_syscall_len
- ret
;
416 case TARGET_SYS_SEEK
:
424 common_semi_set_ret(cs
, reg0
);
427 static target_ulong
common_semi_flen_buf(CPUState
*cs
)
431 /* Return an address in target memory of 64 bytes where the remote
432 * gdb should write its stat struct. (The format of this structure
433 * is defined by GDB's remote protocol and is not target-specific.)
434 * We put this on the guest's stack just below SP.
436 ARMCPU
*cpu
= ARM_CPU(cs
);
437 CPUARMState
*env
= &cpu
->env
;
446 RISCVCPU
*cpu
= RISCV_CPU(cs
);
447 CPURISCVState
*env
= &cpu
->env
;
456 common_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
458 /* The size is always stored in big-endian order, extract
459 the value. We assume the size always fit in 32 bits. */
461 cpu_memory_rw_debug(cs
, common_semi_flen_buf(cs
) + 32,
462 (uint8_t *)&size
, 4, 0);
463 size
= be32_to_cpu(size
);
464 common_semi_set_ret(cs
, size
);
466 set_swi_errno(cs
, -1);
469 static int common_semi_open_guestfd
;
472 common_semi_open_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
474 if (ret
== (target_ulong
)-1) {
476 set_swi_errno(cs
, -1);
477 dealloc_guestfd(common_semi_open_guestfd
);
479 associate_guestfd(common_semi_open_guestfd
, ret
);
480 ret
= common_semi_open_guestfd
;
482 common_semi_set_ret(cs
, ret
);
486 common_semi_gdb_syscall(CPUState
*cs
, gdb_syscall_complete_cb cb
,
487 const char *fmt
, ...)
492 gdb_do_syscallv(cb
, fmt
, va
);
496 * FIXME: in softmmu mode, the gdbstub will schedule our callback
497 * to occur, but will not actually call it to complete the syscall
498 * until after this function has returned and we are back in the
499 * CPU main loop. Therefore callers to this function must not
500 * do anything with its return value, because it is not necessarily
501 * the result of the syscall, but could just be the old value of X0.
502 * The only thing safe to do with this is that the callers of
503 * do_common_semihosting() will write it straight back into X0.
504 * (In linux-user mode, the callback will have happened before
505 * gdb_do_syscallv() returns.)
507 * We should tidy this up so neither this function nor
508 * do_common_semihosting() return a value, so the mistake of
509 * doing something with the return value is not possible to make.
512 return common_semi_arg(cs
, 0);
516 * Types for functions implementing various semihosting calls
517 * for specific types of guest file descriptor. These must all
518 * do the work and return the required return value for the guest,
519 * setting the guest errno if appropriate.
521 typedef uint32_t sys_closefn(CPUState
*cs
, GuestFD
*gf
);
522 typedef uint32_t sys_writefn(CPUState
*cs
, GuestFD
*gf
,
523 target_ulong buf
, uint32_t len
);
524 typedef uint32_t sys_readfn(CPUState
*cs
, GuestFD
*gf
,
525 target_ulong buf
, uint32_t len
);
526 typedef uint32_t sys_isattyfn(CPUState
*cs
, GuestFD
*gf
);
527 typedef uint32_t sys_seekfn(CPUState
*cs
, GuestFD
*gf
,
528 target_ulong offset
);
529 typedef uint32_t sys_flenfn(CPUState
*cs
, GuestFD
*gf
);
531 static uint32_t host_closefn(CPUState
*cs
, GuestFD
*gf
)
534 * Only close the underlying host fd if it's one we opened on behalf
535 * of the guest in SYS_OPEN.
537 if (gf
->hostfd
== STDIN_FILENO
||
538 gf
->hostfd
== STDOUT_FILENO
||
539 gf
->hostfd
== STDERR_FILENO
) {
542 return set_swi_errno(cs
, close(gf
->hostfd
));
545 static uint32_t host_writefn(CPUState
*cs
, GuestFD
*gf
,
546 target_ulong buf
, uint32_t len
)
548 CPUArchState
*env
= cs
->env_ptr
;
550 char *s
= lock_user(VERIFY_READ
, buf
, len
, 1);
551 (void) env
; /* Used in arm softmmu lock_user implicitly */
553 /* Return bytes not written on error */
556 ret
= set_swi_errno(cs
, write(gf
->hostfd
, s
, len
));
557 unlock_user(s
, buf
, 0);
558 if (ret
== (uint32_t)-1) {
561 /* Return bytes not written */
565 static uint32_t host_readfn(CPUState
*cs
, GuestFD
*gf
,
566 target_ulong buf
, uint32_t len
)
568 CPUArchState
*env
= cs
->env_ptr
;
570 char *s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
571 (void) env
; /* Used in arm softmmu lock_user implicitly */
573 /* return bytes not read */
577 ret
= set_swi_errno(cs
, read(gf
->hostfd
, s
, len
));
578 } while (ret
== -1 && errno
== EINTR
);
579 unlock_user(s
, buf
, len
);
580 if (ret
== (uint32_t)-1) {
583 /* Return bytes not read */
587 static uint32_t host_isattyfn(CPUState
*cs
, GuestFD
*gf
)
589 return isatty(gf
->hostfd
);
592 static uint32_t host_seekfn(CPUState
*cs
, GuestFD
*gf
, target_ulong offset
)
594 uint32_t ret
= set_swi_errno(cs
, lseek(gf
->hostfd
, offset
, SEEK_SET
));
595 if (ret
== (uint32_t)-1) {
601 static uint32_t host_flenfn(CPUState
*cs
, GuestFD
*gf
)
604 uint32_t ret
= set_swi_errno(cs
, fstat(gf
->hostfd
, &buf
));
605 if (ret
== (uint32_t)-1) {
611 static uint32_t gdb_closefn(CPUState
*cs
, GuestFD
*gf
)
613 return common_semi_gdb_syscall(cs
, common_semi_cb
, "close,%x", gf
->hostfd
);
616 static uint32_t gdb_writefn(CPUState
*cs
, GuestFD
*gf
,
617 target_ulong buf
, uint32_t len
)
619 common_semi_syscall_len
= len
;
620 return common_semi_gdb_syscall(cs
, common_semi_cb
, "write,%x,%x,%x",
621 gf
->hostfd
, buf
, len
);
624 static uint32_t gdb_readfn(CPUState
*cs
, GuestFD
*gf
,
625 target_ulong buf
, uint32_t len
)
627 common_semi_syscall_len
= len
;
628 return common_semi_gdb_syscall(cs
, common_semi_cb
, "read,%x,%x,%x",
629 gf
->hostfd
, buf
, len
);
632 static uint32_t gdb_isattyfn(CPUState
*cs
, GuestFD
*gf
)
634 return common_semi_gdb_syscall(cs
, common_semi_cb
, "isatty,%x", gf
->hostfd
);
637 static uint32_t gdb_seekfn(CPUState
*cs
, GuestFD
*gf
, target_ulong offset
)
639 return common_semi_gdb_syscall(cs
, common_semi_cb
, "lseek,%x,%x,0",
643 static uint32_t gdb_flenfn(CPUState
*cs
, GuestFD
*gf
)
645 return common_semi_gdb_syscall(cs
, common_semi_flen_cb
, "fstat,%x,%x",
646 gf
->hostfd
, common_semi_flen_buf(cs
));
649 #define SHFB_MAGIC_0 0x53
650 #define SHFB_MAGIC_1 0x48
651 #define SHFB_MAGIC_2 0x46
652 #define SHFB_MAGIC_3 0x42
654 /* Feature bits reportable in feature byte 0 */
655 #define SH_EXT_EXIT_EXTENDED (1 << 0)
656 #define SH_EXT_STDOUT_STDERR (1 << 1)
658 static const uint8_t featurefile_data
[] = {
663 SH_EXT_EXIT_EXTENDED
| SH_EXT_STDOUT_STDERR
, /* Feature byte 0 */
666 static void init_featurefile_guestfd(int guestfd
)
668 GuestFD
*gf
= do_get_guestfd(guestfd
);
671 gf
->type
= GuestFDFeatureFile
;
672 gf
->featurefile_offset
= 0;
675 static uint32_t featurefile_closefn(CPUState
*cs
, GuestFD
*gf
)
681 static uint32_t featurefile_writefn(CPUState
*cs
, GuestFD
*gf
,
682 target_ulong buf
, uint32_t len
)
684 /* This fd can never be open for writing */
687 return set_swi_errno(cs
, -1);
690 static uint32_t featurefile_readfn(CPUState
*cs
, GuestFD
*gf
,
691 target_ulong buf
, uint32_t len
)
693 CPUArchState
*env
= cs
->env_ptr
;
697 (void) env
; /* Used in arm softmmu lock_user implicitly */
698 s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
703 for (i
= 0; i
< len
; i
++) {
704 if (gf
->featurefile_offset
>= sizeof(featurefile_data
)) {
707 s
[i
] = featurefile_data
[gf
->featurefile_offset
];
708 gf
->featurefile_offset
++;
711 unlock_user(s
, buf
, len
);
713 /* Return number of bytes not read */
717 static uint32_t featurefile_isattyfn(CPUState
*cs
, GuestFD
*gf
)
722 static uint32_t featurefile_seekfn(CPUState
*cs
, GuestFD
*gf
,
725 gf
->featurefile_offset
= offset
;
729 static uint32_t featurefile_flenfn(CPUState
*cs
, GuestFD
*gf
)
731 return sizeof(featurefile_data
);
734 typedef struct GuestFDFunctions
{
735 sys_closefn
*closefn
;
736 sys_writefn
*writefn
;
738 sys_isattyfn
*isattyfn
;
743 static const GuestFDFunctions guestfd_fns
[] = {
745 .closefn
= host_closefn
,
746 .writefn
= host_writefn
,
747 .readfn
= host_readfn
,
748 .isattyfn
= host_isattyfn
,
749 .seekfn
= host_seekfn
,
750 .flenfn
= host_flenfn
,
753 .closefn
= gdb_closefn
,
754 .writefn
= gdb_writefn
,
755 .readfn
= gdb_readfn
,
756 .isattyfn
= gdb_isattyfn
,
757 .seekfn
= gdb_seekfn
,
758 .flenfn
= gdb_flenfn
,
760 [GuestFDFeatureFile
] = {
761 .closefn
= featurefile_closefn
,
762 .writefn
= featurefile_writefn
,
763 .readfn
= featurefile_readfn
,
764 .isattyfn
= featurefile_isattyfn
,
765 .seekfn
= featurefile_seekfn
,
766 .flenfn
= featurefile_flenfn
,
770 /* Read the input value from the argument block; fail the semihosting
771 * call if the memory read fails.
774 #define GET_ARG(n) do { \
776 if (get_user_u64(arg ## n, args + (n) * 8)) { \
778 return set_swi_errno(cs, -1); \
781 if (get_user_u32(arg ## n, args + (n) * 4)) { \
783 return set_swi_errno(cs, -1); \
788 #define SET_ARG(n, val) \
790 put_user_u64(val, args + (n) * 8) : \
791 put_user_u32(val, args + (n) * 4))
797 * get_user_ual is defined as get_user_u32 in softmmu-semi.h,
798 * we need a macro that fetches a target_ulong
800 #define get_user_utl(arg, p) \
801 ((sizeof(target_ulong) == 8) ? \
802 get_user_u64(arg, p) : \
803 get_user_u32(arg, p))
806 * put_user_ual is defined as put_user_u32 in softmmu-semi.h,
807 * we need a macro that stores a target_ulong
809 #define put_user_utl(arg, p) \
810 ((sizeof(target_ulong) == 8) ? \
811 put_user_u64(arg, p) : \
812 put_user_u32(arg, p))
814 #define GET_ARG(n) do { \
815 if (get_user_utl(arg ## n, args + (n) * sizeof(target_ulong))) { \
817 return set_swi_errno(cs, -1); \
821 #define SET_ARG(n, val) \
822 put_user_utl(val, args + (n) * sizeof(target_ulong))
826 * Do a semihosting call.
828 * The specification always says that the "return register" either
829 * returns a specific value or is corrupted, so we don't need to
830 * report to our caller whether we are returning a value or trying to
831 * leave the register unchanged. We use 0xdeadbeef as the return value
832 * when there isn't a defined return value for the call.
834 target_ulong
do_common_semihosting(CPUState
*cs
)
836 CPUArchState
*env
= cs
->env_ptr
;
838 target_ulong arg0
, arg1
, arg2
, arg3
;
847 (void) env
; /* Used implicitly by arm lock_user macro */
848 nr
= common_semi_arg(cs
, 0) & 0xffffffffU
;
849 args
= common_semi_arg(cs
, 1);
852 case TARGET_SYS_OPEN
:
859 s
= lock_user_string(arg0
);
862 return set_swi_errno(cs
, -1);
865 unlock_user(s
, arg0
, 0);
867 return set_swi_errno(cs
, -1);
870 guestfd
= alloc_guestfd();
872 unlock_user(s
, arg0
, 0);
874 return set_swi_errno(cs
, -1);
877 if (strcmp(s
, ":tt") == 0) {
881 * We implement SH_EXT_STDOUT_STDERR, so:
882 * open for read == stdin
883 * open for write == stdout
884 * open for append == stderr
887 result_fileno
= STDIN_FILENO
;
888 } else if (arg1
< 8) {
889 result_fileno
= STDOUT_FILENO
;
891 result_fileno
= STDERR_FILENO
;
893 associate_guestfd(guestfd
, result_fileno
);
894 unlock_user(s
, arg0
, 0);
897 if (strcmp(s
, ":semihosting-features") == 0) {
898 unlock_user(s
, arg0
, 0);
899 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
900 if (arg1
!= 0 && arg1
!= 1) {
901 dealloc_guestfd(guestfd
);
903 return set_swi_errno(cs
, -1);
905 init_featurefile_guestfd(guestfd
);
909 if (use_gdb_syscalls()) {
910 common_semi_open_guestfd
= guestfd
;
911 ret
= common_semi_gdb_syscall(cs
, common_semi_open_cb
,
912 "open,%s,%x,1a4", arg0
, (int)arg2
+ 1,
913 gdb_open_modeflags
[arg1
]);
915 ret
= set_swi_errno(cs
, open(s
, open_modeflags
[arg1
], 0644));
916 if (ret
== (uint32_t)-1) {
917 dealloc_guestfd(guestfd
);
919 associate_guestfd(guestfd
, ret
);
923 unlock_user(s
, arg0
, 0);
926 case TARGET_SYS_CLOSE
:
929 gf
= get_guestfd(arg0
);
932 return set_swi_errno(cs
, -1);
935 ret
= guestfd_fns
[gf
->type
].closefn(cs
, gf
);
936 dealloc_guestfd(arg0
);
938 case TARGET_SYS_WRITEC
:
939 qemu_semihosting_console_outc(cs
->env_ptr
, args
);
941 case TARGET_SYS_WRITE0
:
942 return qemu_semihosting_console_outs(cs
->env_ptr
, args
);
943 case TARGET_SYS_WRITE
:
949 gf
= get_guestfd(arg0
);
952 return set_swi_errno(cs
, -1);
955 return guestfd_fns
[gf
->type
].writefn(cs
, gf
, arg1
, len
);
956 case TARGET_SYS_READ
:
962 gf
= get_guestfd(arg0
);
965 return set_swi_errno(cs
, -1);
968 return guestfd_fns
[gf
->type
].readfn(cs
, gf
, arg1
, len
);
969 case TARGET_SYS_READC
:
970 return qemu_semihosting_console_inc(cs
->env_ptr
);
971 case TARGET_SYS_ISERROR
:
973 return (target_long
) arg0
< 0 ? 1 : 0;
974 case TARGET_SYS_ISTTY
:
977 gf
= get_guestfd(arg0
);
980 return set_swi_errno(cs
, -1);
983 return guestfd_fns
[gf
->type
].isattyfn(cs
, gf
);
984 case TARGET_SYS_SEEK
:
988 gf
= get_guestfd(arg0
);
991 return set_swi_errno(cs
, -1);
994 return guestfd_fns
[gf
->type
].seekfn(cs
, gf
, arg1
);
995 case TARGET_SYS_FLEN
:
998 gf
= get_guestfd(arg0
);
1001 return set_swi_errno(cs
, -1);
1004 return guestfd_fns
[gf
->type
].flenfn(cs
, gf
);
1005 case TARGET_SYS_TMPNAM
:
1009 if (asprintf(&s
, "/tmp/qemu-%x%02x", getpid(),
1010 (int) (arg1
& 0xff)) < 0) {
1013 ul_ret
= (target_ulong
) -1;
1015 /* Make sure there's enough space in the buffer */
1016 if (strlen(s
) < arg2
) {
1017 char *output
= lock_user(VERIFY_WRITE
, arg0
, arg2
, 0);
1019 unlock_user(output
, arg0
, arg2
);
1024 case TARGET_SYS_REMOVE
:
1027 if (use_gdb_syscalls()) {
1028 ret
= common_semi_gdb_syscall(cs
, common_semi_cb
, "unlink,%s",
1029 arg0
, (int)arg1
+ 1);
1031 s
= lock_user_string(arg0
);
1034 return set_swi_errno(cs
, -1);
1036 ret
= set_swi_errno(cs
, remove(s
));
1037 unlock_user(s
, arg0
, 0);
1040 case TARGET_SYS_RENAME
:
1045 if (use_gdb_syscalls()) {
1046 return common_semi_gdb_syscall(cs
, common_semi_cb
, "rename,%s,%s",
1047 arg0
, (int)arg1
+ 1, arg2
,
1051 s
= lock_user_string(arg0
);
1052 s2
= lock_user_string(arg2
);
1055 ret
= set_swi_errno(cs
, -1);
1057 ret
= set_swi_errno(cs
, rename(s
, s2
));
1060 unlock_user(s2
, arg2
, 0);
1062 unlock_user(s
, arg0
, 0);
1065 case TARGET_SYS_CLOCK
:
1066 return clock() / (CLOCKS_PER_SEC
/ 100);
1067 case TARGET_SYS_TIME
:
1068 return set_swi_errno(cs
, time(NULL
));
1069 case TARGET_SYS_SYSTEM
:
1072 if (use_gdb_syscalls()) {
1073 return common_semi_gdb_syscall(cs
, common_semi_cb
, "system,%s",
1074 arg0
, (int)arg1
+ 1);
1076 s
= lock_user_string(arg0
);
1079 return set_swi_errno(cs
, -1);
1081 ret
= set_swi_errno(cs
, system(s
));
1082 unlock_user(s
, arg0
, 0);
1085 case TARGET_SYS_ERRNO
:
1086 return get_swi_errno(cs
);
1087 case TARGET_SYS_GET_CMDLINE
:
1089 /* Build a command-line from the original argv.
1092 * * arg0, pointer to a buffer of at least the size
1093 * specified in arg1.
1094 * * arg1, size of the buffer pointed to by arg0 in
1098 * * arg0, pointer to null-terminated string of the
1100 * * arg1, length of the string pointed to by arg0.
1103 char *output_buffer
;
1107 #if !defined(CONFIG_USER_ONLY)
1108 const char *cmdline
;
1110 TaskState
*ts
= cs
->opaque
;
1115 /* Compute the size of the output string. */
1116 #if !defined(CONFIG_USER_ONLY)
1117 cmdline
= semihosting_get_cmdline();
1118 if (cmdline
== NULL
) {
1119 cmdline
= ""; /* Default to an empty line. */
1121 output_size
= strlen(cmdline
) + 1; /* Count terminating 0. */
1125 output_size
= ts
->info
->arg_end
- ts
->info
->arg_start
;
1128 * We special-case the "empty command line" case (argc==0).
1129 * Just provide the terminating 0.
1135 if (output_size
> input_size
) {
1136 /* Not enough space to store command-line arguments. */
1138 return set_swi_errno(cs
, -1);
1141 /* Adjust the command-line length. */
1142 if (SET_ARG(1, output_size
- 1)) {
1143 /* Couldn't write back to argument block */
1145 return set_swi_errno(cs
, -1);
1148 /* Lock the buffer on the ARM side. */
1149 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
1150 if (!output_buffer
) {
1152 return set_swi_errno(cs
, -1);
1155 /* Copy the command-line arguments. */
1156 #if !defined(CONFIG_USER_ONLY)
1157 pstrcpy(output_buffer
, output_size
, cmdline
);
1159 if (output_size
== 1) {
1160 /* Empty command-line. */
1161 output_buffer
[0] = '\0';
1165 if (copy_from_user(output_buffer
, ts
->info
->arg_start
,
1168 status
= set_swi_errno(cs
, -1);
1172 /* Separate arguments by white spaces. */
1173 for (i
= 0; i
< output_size
- 1; i
++) {
1174 if (output_buffer
[i
] == 0) {
1175 output_buffer
[i
] = ' ';
1180 /* Unlock the buffer on the ARM side. */
1181 unlock_user(output_buffer
, arg0
, output_size
);
1185 case TARGET_SYS_HEAPINFO
:
1187 target_ulong retvals
[4];
1190 #ifdef CONFIG_USER_ONLY
1191 TaskState
*ts
= cs
->opaque
;
1193 target_ulong rambase
= common_semi_rambase(cs
);
1198 #ifdef CONFIG_USER_ONLY
1200 * Some C libraries assume the heap immediately follows .bss, so
1201 * allocate it using sbrk.
1203 if (!ts
->heap_limit
) {
1206 ts
->heap_base
= do_brk(0);
1207 limit
= ts
->heap_base
+ COMMON_SEMI_HEAP_SIZE
;
1208 /* Try a big heap, and reduce the size if that fails. */
1210 ret
= do_brk(limit
);
1214 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
1216 ts
->heap_limit
= limit
;
1219 retvals
[0] = ts
->heap_base
;
1220 retvals
[1] = ts
->heap_limit
;
1221 retvals
[2] = ts
->stack_base
;
1222 retvals
[3] = 0; /* Stack limit. */
1224 limit
= current_machine
->ram_size
;
1225 /* TODO: Make this use the limit of the loaded application. */
1226 retvals
[0] = rambase
+ limit
/ 2;
1227 retvals
[1] = rambase
+ limit
;
1228 retvals
[2] = rambase
+ limit
; /* Stack base */
1229 retvals
[3] = rambase
; /* Stack limit. */
1232 for (i
= 0; i
< ARRAY_SIZE(retvals
); i
++) {
1235 fail
= SET_ARG(i
, retvals
[i
]);
1238 /* Couldn't write back to argument block */
1240 return set_swi_errno(cs
, -1);
1245 case TARGET_SYS_EXIT
:
1246 case TARGET_SYS_EXIT_EXTENDED
:
1247 if (common_semi_sys_exit_extended(cs
, nr
)) {
1249 * The A64 version of SYS_EXIT takes a parameter block,
1250 * so the application-exit type can return a subcode which
1251 * is the exit status code from the application.
1252 * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
1253 * which allows A32/T32 guests to also provide a status code.
1258 if (arg0
== ADP_Stopped_ApplicationExit
) {
1265 * The A32/T32 version of SYS_EXIT specifies only
1266 * Stopped_ApplicationExit as normal exit, but does not
1267 * allow the guest to specify the exit status code.
1268 * Everything else is considered an error.
1270 ret
= (args
== ADP_Stopped_ApplicationExit
) ? 0 : 1;
1274 case TARGET_SYS_ELAPSED
:
1275 elapsed
= get_clock() - clock_start
;
1276 if (sizeof(target_ulong
) == 8) {
1277 SET_ARG(0, elapsed
);
1279 SET_ARG(0, (uint32_t) elapsed
);
1280 SET_ARG(1, (uint32_t) (elapsed
>> 32));
1283 case TARGET_SYS_TICKFREQ
:
1284 /* qemu always uses nsec */
1286 case TARGET_SYS_SYNCCACHE
:
1288 * Clean the D-cache and invalidate the I-cache for the specified
1289 * virtual address range. This is a nop for us since we don't
1290 * implement caches. This is only present on A64.
1293 if (is_a64(cs
->env_ptr
)) {
1300 /* fall through -- invalid for A32/T32 */
1302 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
1303 cpu_dump_state(cs
, stderr
, 0);