2 * Arm "Angel" semihosting syscalls
4 * Copyright (c) 2005, 2007 CodeSourcery.
5 * Copyright (c) 2019 Linaro
6 * Written by Paul Brook.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 * ARM Semihosting is documented in:
22 * Semihosting for AArch32 and AArch64 Release 2.0
23 * https://static.docs.arm.com/100863/0200/semihosting.pdf
26 #include "qemu/osdep.h"
29 #include "hw/semihosting/semihost.h"
30 #include "hw/semihosting/console.h"
32 #ifdef CONFIG_USER_ONLY
35 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
37 #include "exec/gdbstub.h"
38 #include "qemu/cutils.h"
41 #define TARGET_SYS_OPEN 0x01
42 #define TARGET_SYS_CLOSE 0x02
43 #define TARGET_SYS_WRITEC 0x03
44 #define TARGET_SYS_WRITE0 0x04
45 #define TARGET_SYS_WRITE 0x05
46 #define TARGET_SYS_READ 0x06
47 #define TARGET_SYS_READC 0x07
48 #define TARGET_SYS_ISTTY 0x09
49 #define TARGET_SYS_SEEK 0x0a
50 #define TARGET_SYS_FLEN 0x0c
51 #define TARGET_SYS_TMPNAM 0x0d
52 #define TARGET_SYS_REMOVE 0x0e
53 #define TARGET_SYS_RENAME 0x0f
54 #define TARGET_SYS_CLOCK 0x10
55 #define TARGET_SYS_TIME 0x11
56 #define TARGET_SYS_SYSTEM 0x12
57 #define TARGET_SYS_ERRNO 0x13
58 #define TARGET_SYS_GET_CMDLINE 0x15
59 #define TARGET_SYS_HEAPINFO 0x16
60 #define TARGET_SYS_EXIT 0x18
61 #define TARGET_SYS_SYNCCACHE 0x19
62 #define TARGET_SYS_EXIT_EXTENDED 0x20
64 /* ADP_Stopped_ApplicationExit is used for exit(0),
65 * anything else is implemented as exit(1) */
66 #define ADP_Stopped_ApplicationExit (0x20026)
72 #define GDB_O_RDONLY 0x000
73 #define GDB_O_WRONLY 0x001
74 #define GDB_O_RDWR 0x002
75 #define GDB_O_APPEND 0x008
76 #define GDB_O_CREAT 0x200
77 #define GDB_O_TRUNC 0x400
78 #define GDB_O_BINARY 0
80 static int gdb_open_modeflags
[12] = {
82 GDB_O_RDONLY
| GDB_O_BINARY
,
84 GDB_O_RDWR
| GDB_O_BINARY
,
85 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
,
86 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
87 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
,
88 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_TRUNC
| GDB_O_BINARY
,
89 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
,
90 GDB_O_WRONLY
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
,
91 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
,
92 GDB_O_RDWR
| GDB_O_CREAT
| GDB_O_APPEND
| GDB_O_BINARY
95 static int open_modeflags
[12] = {
100 O_WRONLY
| O_CREAT
| O_TRUNC
,
101 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
,
102 O_RDWR
| O_CREAT
| O_TRUNC
,
103 O_RDWR
| O_CREAT
| O_TRUNC
| O_BINARY
,
104 O_WRONLY
| O_CREAT
| O_APPEND
,
105 O_WRONLY
| O_CREAT
| O_APPEND
| O_BINARY
,
106 O_RDWR
| O_CREAT
| O_APPEND
,
107 O_RDWR
| O_CREAT
| O_APPEND
| O_BINARY
110 typedef enum GuestFDType
{
114 GuestFDFeatureFile
= 3,
118 * Guest file descriptors are integer indexes into an array of
119 * these structures (we will dynamically resize as necessary).
121 typedef struct GuestFD
{
125 target_ulong featurefile_offset
;
129 static GArray
*guestfd_array
;
132 * Allocate a new guest file descriptor and return it; if we
133 * couldn't allocate a new fd then return -1.
134 * This is a fairly simplistic implementation because we don't
135 * expect that most semihosting guest programs will make very
136 * heavy use of opening and closing fds.
138 static int alloc_guestfd(void)
142 if (!guestfd_array
) {
143 /* New entries zero-initialized, i.e. type GuestFDUnused */
144 guestfd_array
= g_array_new(FALSE
, TRUE
, sizeof(GuestFD
));
147 /* SYS_OPEN should return nonzero handle on success. Start guestfd from 1 */
148 for (i
= 1; i
< guestfd_array
->len
; i
++) {
149 GuestFD
*gf
= &g_array_index(guestfd_array
, GuestFD
, i
);
151 if (gf
->type
== GuestFDUnused
) {
156 /* All elements already in use: expand the array */
157 g_array_set_size(guestfd_array
, i
+ 1);
162 * Look up the guestfd in the data structure; return NULL
163 * for out of bounds, but don't check whether the slot is unused.
164 * This is used internally by the other guestfd functions.
166 static GuestFD
*do_get_guestfd(int guestfd
)
168 if (!guestfd_array
) {
172 if (guestfd
<= 0 || guestfd
>= guestfd_array
->len
) {
176 return &g_array_index(guestfd_array
, GuestFD
, guestfd
);
180 * Associate the specified guest fd (which must have been
181 * allocated via alloc_fd() and not previously used) with
182 * the specified host/gdb fd.
184 static void associate_guestfd(int guestfd
, int hostfd
)
186 GuestFD
*gf
= do_get_guestfd(guestfd
);
189 gf
->type
= use_gdb_syscalls() ? GuestFDGDB
: GuestFDHost
;
194 * Deallocate the specified guest file descriptor. This doesn't
195 * close the host fd, it merely undoes the work of alloc_fd().
197 static void dealloc_guestfd(int guestfd
)
199 GuestFD
*gf
= do_get_guestfd(guestfd
);
202 gf
->type
= GuestFDUnused
;
206 * Given a guest file descriptor, get the associated struct.
207 * If the fd is not valid, return NULL. This is the function
208 * used by the various semihosting calls to validate a handle
210 * Note: calling alloc_guestfd() or dealloc_guestfd() will
211 * invalidate any GuestFD* obtained by calling this function.
213 static GuestFD
*get_guestfd(int guestfd
)
215 GuestFD
*gf
= do_get_guestfd(guestfd
);
217 if (!gf
|| gf
->type
== GuestFDUnused
) {
224 * The semihosting API has no concept of its errno being thread-safe,
225 * as the API design predates SMP CPUs and was intended as a simple
226 * real-hardware set of debug functionality. For QEMU, we make the
227 * errno be per-thread in linux-user mode; in softmmu it is a simple
228 * global, and we assume that the guest takes care of avoiding any races.
230 #ifndef CONFIG_USER_ONLY
231 static target_ulong syscall_err
;
233 #include "exec/softmmu-semi.h"
236 static inline uint32_t set_swi_errno(CPUARMState
*env
, uint32_t code
)
238 if (code
== (uint32_t)-1) {
239 #ifdef CONFIG_USER_ONLY
240 CPUState
*cs
= env_cpu(env
);
241 TaskState
*ts
= cs
->opaque
;
243 ts
->swi_errno
= errno
;
251 static inline uint32_t get_swi_errno(CPUARMState
*env
)
253 #ifdef CONFIG_USER_ONLY
254 CPUState
*cs
= env_cpu(env
);
255 TaskState
*ts
= cs
->opaque
;
257 return ts
->swi_errno
;
263 static target_ulong arm_semi_syscall_len
;
265 static void arm_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
267 ARMCPU
*cpu
= ARM_CPU(cs
);
268 CPUARMState
*env
= &cpu
->env
;
269 target_ulong reg0
= is_a64(env
) ? env
->xregs
[0] : env
->regs
[0];
271 if (ret
== (target_ulong
)-1) {
273 set_swi_errno(env
, -1);
276 /* Fixup syscalls that use nonstardard return conventions. */
278 case TARGET_SYS_WRITE
:
279 case TARGET_SYS_READ
:
280 reg0
= arm_semi_syscall_len
- ret
;
282 case TARGET_SYS_SEEK
:
291 env
->xregs
[0] = reg0
;
297 static target_ulong
arm_flen_buf(ARMCPU
*cpu
)
299 /* Return an address in target memory of 64 bytes where the remote
300 * gdb should write its stat struct. (The format of this structure
301 * is defined by GDB's remote protocol and is not target-specific.)
302 * We put this on the guest's stack just below SP.
304 CPUARMState
*env
= &cpu
->env
;
316 static void arm_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
318 ARMCPU
*cpu
= ARM_CPU(cs
);
319 CPUARMState
*env
= &cpu
->env
;
320 /* The size is always stored in big-endian order, extract
321 the value. We assume the size always fit in 32 bits. */
323 cpu_memory_rw_debug(cs
, arm_flen_buf(cpu
) + 32, (uint8_t *)&size
, 4, 0);
324 size
= be32_to_cpu(size
);
326 env
->xregs
[0] = size
;
331 set_swi_errno(env
, -1);
334 static int arm_semi_open_guestfd
;
336 static void arm_semi_open_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
338 ARMCPU
*cpu
= ARM_CPU(cs
);
339 CPUARMState
*env
= &cpu
->env
;
340 if (ret
== (target_ulong
)-1) {
342 set_swi_errno(env
, -1);
343 dealloc_guestfd(arm_semi_open_guestfd
);
345 associate_guestfd(arm_semi_open_guestfd
, ret
);
346 ret
= arm_semi_open_guestfd
;
356 static target_ulong
arm_gdb_syscall(ARMCPU
*cpu
, gdb_syscall_complete_cb cb
,
357 const char *fmt
, ...)
360 CPUARMState
*env
= &cpu
->env
;
363 gdb_do_syscallv(cb
, fmt
, va
);
367 * FIXME: in softmmu mode, the gdbstub will schedule our callback
368 * to occur, but will not actually call it to complete the syscall
369 * until after this function has returned and we are back in the
370 * CPU main loop. Therefore callers to this function must not
371 * do anything with its return value, because it is not necessarily
372 * the result of the syscall, but could just be the old value of X0.
373 * The only thing safe to do with this is that the callers of
374 * do_arm_semihosting() will write it straight back into X0.
375 * (In linux-user mode, the callback will have happened before
376 * gdb_do_syscallv() returns.)
378 * We should tidy this up so neither this function nor
379 * do_arm_semihosting() return a value, so the mistake of
380 * doing something with the return value is not possible to make.
383 return is_a64(env
) ? env
->xregs
[0] : env
->regs
[0];
387 * Types for functions implementing various semihosting calls
388 * for specific types of guest file descriptor. These must all
389 * do the work and return the required return value for the guest,
390 * setting the guest errno if appropriate.
392 typedef uint32_t sys_closefn(ARMCPU
*cpu
, GuestFD
*gf
);
393 typedef uint32_t sys_writefn(ARMCPU
*cpu
, GuestFD
*gf
,
394 target_ulong buf
, uint32_t len
);
395 typedef uint32_t sys_readfn(ARMCPU
*cpu
, GuestFD
*gf
,
396 target_ulong buf
, uint32_t len
);
397 typedef uint32_t sys_isattyfn(ARMCPU
*cpu
, GuestFD
*gf
);
398 typedef uint32_t sys_seekfn(ARMCPU
*cpu
, GuestFD
*gf
,
399 target_ulong offset
);
400 typedef uint32_t sys_flenfn(ARMCPU
*cpu
, GuestFD
*gf
);
402 static uint32_t host_closefn(ARMCPU
*cpu
, GuestFD
*gf
)
404 CPUARMState
*env
= &cpu
->env
;
407 * Only close the underlying host fd if it's one we opened on behalf
408 * of the guest in SYS_OPEN.
410 if (gf
->hostfd
== STDIN_FILENO
||
411 gf
->hostfd
== STDOUT_FILENO
||
412 gf
->hostfd
== STDERR_FILENO
) {
415 return set_swi_errno(env
, close(gf
->hostfd
));
418 static uint32_t host_writefn(ARMCPU
*cpu
, GuestFD
*gf
,
419 target_ulong buf
, uint32_t len
)
422 CPUARMState
*env
= &cpu
->env
;
423 char *s
= lock_user(VERIFY_READ
, buf
, len
, 1);
425 /* Return bytes not written on error */
428 ret
= set_swi_errno(env
, write(gf
->hostfd
, s
, len
));
429 unlock_user(s
, buf
, 0);
430 if (ret
== (uint32_t)-1) {
433 /* Return bytes not written */
437 static uint32_t host_readfn(ARMCPU
*cpu
, GuestFD
*gf
,
438 target_ulong buf
, uint32_t len
)
441 CPUARMState
*env
= &cpu
->env
;
442 char *s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
444 /* return bytes not read */
448 ret
= set_swi_errno(env
, read(gf
->hostfd
, s
, len
));
449 } while (ret
== -1 && errno
== EINTR
);
450 unlock_user(s
, buf
, len
);
451 if (ret
== (uint32_t)-1) {
454 /* Return bytes not read */
458 static uint32_t host_isattyfn(ARMCPU
*cpu
, GuestFD
*gf
)
460 return isatty(gf
->hostfd
);
463 static uint32_t host_seekfn(ARMCPU
*cpu
, GuestFD
*gf
, target_ulong offset
)
465 CPUARMState
*env
= &cpu
->env
;
466 uint32_t ret
= set_swi_errno(env
, lseek(gf
->hostfd
, offset
, SEEK_SET
));
467 if (ret
== (uint32_t)-1) {
473 static uint32_t host_flenfn(ARMCPU
*cpu
, GuestFD
*gf
)
475 CPUARMState
*env
= &cpu
->env
;
477 uint32_t ret
= set_swi_errno(env
, fstat(gf
->hostfd
, &buf
));
478 if (ret
== (uint32_t)-1) {
484 static uint32_t gdb_closefn(ARMCPU
*cpu
, GuestFD
*gf
)
486 return arm_gdb_syscall(cpu
, arm_semi_cb
, "close,%x", gf
->hostfd
);
489 static uint32_t gdb_writefn(ARMCPU
*cpu
, GuestFD
*gf
,
490 target_ulong buf
, uint32_t len
)
492 arm_semi_syscall_len
= len
;
493 return arm_gdb_syscall(cpu
, arm_semi_cb
, "write,%x,%x,%x",
494 gf
->hostfd
, buf
, len
);
497 static uint32_t gdb_readfn(ARMCPU
*cpu
, GuestFD
*gf
,
498 target_ulong buf
, uint32_t len
)
500 arm_semi_syscall_len
= len
;
501 return arm_gdb_syscall(cpu
, arm_semi_cb
, "read,%x,%x,%x",
502 gf
->hostfd
, buf
, len
);
505 static uint32_t gdb_isattyfn(ARMCPU
*cpu
, GuestFD
*gf
)
507 return arm_gdb_syscall(cpu
, arm_semi_cb
, "isatty,%x", gf
->hostfd
);
510 static uint32_t gdb_seekfn(ARMCPU
*cpu
, GuestFD
*gf
, target_ulong offset
)
512 return arm_gdb_syscall(cpu
, arm_semi_cb
, "lseek,%x,%x,0",
516 static uint32_t gdb_flenfn(ARMCPU
*cpu
, GuestFD
*gf
)
518 return arm_gdb_syscall(cpu
, arm_semi_flen_cb
, "fstat,%x,%x",
519 gf
->hostfd
, arm_flen_buf(cpu
));
522 #define SHFB_MAGIC_0 0x53
523 #define SHFB_MAGIC_1 0x48
524 #define SHFB_MAGIC_2 0x46
525 #define SHFB_MAGIC_3 0x42
527 /* Feature bits reportable in feature byte 0 */
528 #define SH_EXT_EXIT_EXTENDED (1 << 0)
529 #define SH_EXT_STDOUT_STDERR (1 << 1)
531 static const uint8_t featurefile_data
[] = {
536 SH_EXT_EXIT_EXTENDED
| SH_EXT_STDOUT_STDERR
, /* Feature byte 0 */
539 static void init_featurefile_guestfd(int guestfd
)
541 GuestFD
*gf
= do_get_guestfd(guestfd
);
544 gf
->type
= GuestFDFeatureFile
;
545 gf
->featurefile_offset
= 0;
548 static uint32_t featurefile_closefn(ARMCPU
*cpu
, GuestFD
*gf
)
554 static uint32_t featurefile_writefn(ARMCPU
*cpu
, GuestFD
*gf
,
555 target_ulong buf
, uint32_t len
)
557 /* This fd can never be open for writing */
558 CPUARMState
*env
= &cpu
->env
;
561 return set_swi_errno(env
, -1);
564 static uint32_t featurefile_readfn(ARMCPU
*cpu
, GuestFD
*gf
,
565 target_ulong buf
, uint32_t len
)
568 #ifndef CONFIG_USER_ONLY
569 CPUARMState
*env
= &cpu
->env
;
573 s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
578 for (i
= 0; i
< len
; i
++) {
579 if (gf
->featurefile_offset
>= sizeof(featurefile_data
)) {
582 s
[i
] = featurefile_data
[gf
->featurefile_offset
];
583 gf
->featurefile_offset
++;
586 unlock_user(s
, buf
, len
);
588 /* Return number of bytes not read */
592 static uint32_t featurefile_isattyfn(ARMCPU
*cpu
, GuestFD
*gf
)
597 static uint32_t featurefile_seekfn(ARMCPU
*cpu
, GuestFD
*gf
,
600 gf
->featurefile_offset
= offset
;
604 static uint32_t featurefile_flenfn(ARMCPU
*cpu
, GuestFD
*gf
)
606 return sizeof(featurefile_data
);
609 typedef struct GuestFDFunctions
{
610 sys_closefn
*closefn
;
611 sys_writefn
*writefn
;
613 sys_isattyfn
*isattyfn
;
618 static const GuestFDFunctions guestfd_fns
[] = {
620 .closefn
= host_closefn
,
621 .writefn
= host_writefn
,
622 .readfn
= host_readfn
,
623 .isattyfn
= host_isattyfn
,
624 .seekfn
= host_seekfn
,
625 .flenfn
= host_flenfn
,
628 .closefn
= gdb_closefn
,
629 .writefn
= gdb_writefn
,
630 .readfn
= gdb_readfn
,
631 .isattyfn
= gdb_isattyfn
,
632 .seekfn
= gdb_seekfn
,
633 .flenfn
= gdb_flenfn
,
635 [GuestFDFeatureFile
] = {
636 .closefn
= featurefile_closefn
,
637 .writefn
= featurefile_writefn
,
638 .readfn
= featurefile_readfn
,
639 .isattyfn
= featurefile_isattyfn
,
640 .seekfn
= featurefile_seekfn
,
641 .flenfn
= featurefile_flenfn
,
645 /* Read the input value from the argument block; fail the semihosting
646 * call if the memory read fails.
648 #define GET_ARG(n) do { \
650 if (get_user_u64(arg ## n, args + (n) * 8)) { \
652 return set_swi_errno(env, -1); \
655 if (get_user_u32(arg ## n, args + (n) * 4)) { \
657 return set_swi_errno(env, -1); \
662 #define SET_ARG(n, val) \
664 put_user_u64(val, args + (n) * 8) : \
665 put_user_u32(val, args + (n) * 4))
668 * Do a semihosting call.
670 * The specification always says that the "return register" either
671 * returns a specific value or is corrupted, so we don't need to
672 * report to our caller whether we are returning a value or trying to
673 * leave the register unchanged. We use 0xdeadbeef as the return value
674 * when there isn't a defined return value for the call.
676 target_ulong
do_arm_semihosting(CPUARMState
*env
)
678 ARMCPU
*cpu
= env_archcpu(env
);
679 CPUState
*cs
= env_cpu(env
);
681 target_ulong arg0
, arg1
, arg2
, arg3
;
689 /* Note that the syscall number is in W0, not X0 */
690 nr
= env
->xregs
[0] & 0xffffffffU
;
691 args
= env
->xregs
[1];
698 case TARGET_SYS_OPEN
:
705 s
= lock_user_string(arg0
);
708 return set_swi_errno(env
, -1);
711 unlock_user(s
, arg0
, 0);
713 return set_swi_errno(env
, -1);
716 guestfd
= alloc_guestfd();
718 unlock_user(s
, arg0
, 0);
720 return set_swi_errno(env
, -1);
723 if (strcmp(s
, ":tt") == 0) {
727 * We implement SH_EXT_STDOUT_STDERR, so:
728 * open for read == stdin
729 * open for write == stdout
730 * open for append == stderr
733 result_fileno
= STDIN_FILENO
;
734 } else if (arg1
< 8) {
735 result_fileno
= STDOUT_FILENO
;
737 result_fileno
= STDERR_FILENO
;
739 associate_guestfd(guestfd
, result_fileno
);
740 unlock_user(s
, arg0
, 0);
743 if (strcmp(s
, ":semihosting-features") == 0) {
744 unlock_user(s
, arg0
, 0);
745 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
746 if (arg1
!= 0 && arg1
!= 1) {
747 dealloc_guestfd(guestfd
);
749 return set_swi_errno(env
, -1);
751 init_featurefile_guestfd(guestfd
);
755 if (use_gdb_syscalls()) {
756 arm_semi_open_guestfd
= guestfd
;
757 ret
= arm_gdb_syscall(cpu
, arm_semi_open_cb
, "open,%s,%x,1a4", arg0
,
758 (int)arg2
+1, gdb_open_modeflags
[arg1
]);
760 ret
= set_swi_errno(env
, open(s
, open_modeflags
[arg1
], 0644));
761 if (ret
== (uint32_t)-1) {
762 dealloc_guestfd(guestfd
);
764 associate_guestfd(guestfd
, ret
);
768 unlock_user(s
, arg0
, 0);
771 case TARGET_SYS_CLOSE
:
774 gf
= get_guestfd(arg0
);
777 return set_swi_errno(env
, -1);
780 ret
= guestfd_fns
[gf
->type
].closefn(cpu
, gf
);
781 dealloc_guestfd(arg0
);
783 case TARGET_SYS_WRITEC
:
784 qemu_semihosting_console_outc(env
, args
);
786 case TARGET_SYS_WRITE0
:
787 return qemu_semihosting_console_outs(env
, args
);
788 case TARGET_SYS_WRITE
:
794 gf
= get_guestfd(arg0
);
797 return set_swi_errno(env
, -1);
800 return guestfd_fns
[gf
->type
].writefn(cpu
, gf
, arg1
, len
);
801 case TARGET_SYS_READ
:
807 gf
= get_guestfd(arg0
);
810 return set_swi_errno(env
, -1);
813 return guestfd_fns
[gf
->type
].readfn(cpu
, gf
, arg1
, len
);
814 case TARGET_SYS_READC
:
815 return qemu_semihosting_console_inc(env
);
816 case TARGET_SYS_ISTTY
:
819 gf
= get_guestfd(arg0
);
822 return set_swi_errno(env
, -1);
825 return guestfd_fns
[gf
->type
].isattyfn(cpu
, gf
);
826 case TARGET_SYS_SEEK
:
830 gf
= get_guestfd(arg0
);
833 return set_swi_errno(env
, -1);
836 return guestfd_fns
[gf
->type
].seekfn(cpu
, gf
, arg1
);
837 case TARGET_SYS_FLEN
:
840 gf
= get_guestfd(arg0
);
843 return set_swi_errno(env
, -1);
846 return guestfd_fns
[gf
->type
].flenfn(cpu
, gf
);
847 case TARGET_SYS_TMPNAM
:
848 qemu_log_mask(LOG_UNIMP
, "%s: SYS_TMPNAM not implemented", __func__
);
850 case TARGET_SYS_REMOVE
:
853 if (use_gdb_syscalls()) {
854 ret
= arm_gdb_syscall(cpu
, arm_semi_cb
, "unlink,%s",
857 s
= lock_user_string(arg0
);
860 return set_swi_errno(env
, -1);
862 ret
= set_swi_errno(env
, remove(s
));
863 unlock_user(s
, arg0
, 0);
866 case TARGET_SYS_RENAME
:
871 if (use_gdb_syscalls()) {
872 return arm_gdb_syscall(cpu
, arm_semi_cb
, "rename,%s,%s",
873 arg0
, (int)arg1
+1, arg2
, (int)arg3
+1);
876 s
= lock_user_string(arg0
);
877 s2
= lock_user_string(arg2
);
880 ret
= set_swi_errno(env
, -1);
882 ret
= set_swi_errno(env
, rename(s
, s2
));
885 unlock_user(s2
, arg2
, 0);
887 unlock_user(s
, arg0
, 0);
890 case TARGET_SYS_CLOCK
:
891 return clock() / (CLOCKS_PER_SEC
/ 100);
892 case TARGET_SYS_TIME
:
893 return set_swi_errno(env
, time(NULL
));
894 case TARGET_SYS_SYSTEM
:
897 if (use_gdb_syscalls()) {
898 return arm_gdb_syscall(cpu
, arm_semi_cb
, "system,%s",
901 s
= lock_user_string(arg0
);
904 return set_swi_errno(env
, -1);
906 ret
= set_swi_errno(env
, system(s
));
907 unlock_user(s
, arg0
, 0);
910 case TARGET_SYS_ERRNO
:
911 return get_swi_errno(env
);
912 case TARGET_SYS_GET_CMDLINE
:
914 /* Build a command-line from the original argv.
917 * * arg0, pointer to a buffer of at least the size
919 * * arg1, size of the buffer pointed to by arg0 in
923 * * arg0, pointer to null-terminated string of the
925 * * arg1, length of the string pointed to by arg0.
932 #if !defined(CONFIG_USER_ONLY)
935 TaskState
*ts
= cs
->opaque
;
940 /* Compute the size of the output string. */
941 #if !defined(CONFIG_USER_ONLY)
942 cmdline
= semihosting_get_cmdline();
943 if (cmdline
== NULL
) {
944 cmdline
= ""; /* Default to an empty line. */
946 output_size
= strlen(cmdline
) + 1; /* Count terminating 0. */
950 output_size
= ts
->info
->arg_end
- ts
->info
->arg_start
;
953 * We special-case the "empty command line" case (argc==0).
954 * Just provide the terminating 0.
960 if (output_size
> input_size
) {
961 /* Not enough space to store command-line arguments. */
963 return set_swi_errno(env
, -1);
966 /* Adjust the command-line length. */
967 if (SET_ARG(1, output_size
- 1)) {
968 /* Couldn't write back to argument block */
970 return set_swi_errno(env
, -1);
973 /* Lock the buffer on the ARM side. */
974 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
975 if (!output_buffer
) {
977 return set_swi_errno(env
, -1);
980 /* Copy the command-line arguments. */
981 #if !defined(CONFIG_USER_ONLY)
982 pstrcpy(output_buffer
, output_size
, cmdline
);
984 if (output_size
== 1) {
985 /* Empty command-line. */
986 output_buffer
[0] = '\0';
990 if (copy_from_user(output_buffer
, ts
->info
->arg_start
,
993 status
= set_swi_errno(env
, -1);
997 /* Separate arguments by white spaces. */
998 for (i
= 0; i
< output_size
- 1; i
++) {
999 if (output_buffer
[i
] == 0) {
1000 output_buffer
[i
] = ' ';
1005 /* Unlock the buffer on the ARM side. */
1006 unlock_user(output_buffer
, arg0
, output_size
);
1010 case TARGET_SYS_HEAPINFO
:
1012 target_ulong retvals
[4];
1015 #ifdef CONFIG_USER_ONLY
1016 TaskState
*ts
= cs
->opaque
;
1021 #ifdef CONFIG_USER_ONLY
1023 * Some C libraries assume the heap immediately follows .bss, so
1024 * allocate it using sbrk.
1026 if (!ts
->heap_limit
) {
1029 ts
->heap_base
= do_brk(0);
1030 limit
= ts
->heap_base
+ ARM_ANGEL_HEAP_SIZE
;
1031 /* Try a big heap, and reduce the size if that fails. */
1033 ret
= do_brk(limit
);
1037 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
1039 ts
->heap_limit
= limit
;
1042 retvals
[0] = ts
->heap_base
;
1043 retvals
[1] = ts
->heap_limit
;
1044 retvals
[2] = ts
->stack_base
;
1045 retvals
[3] = 0; /* Stack limit. */
1048 /* TODO: Make this use the limit of the loaded application. */
1049 retvals
[0] = limit
/ 2;
1051 retvals
[2] = limit
; /* Stack base */
1052 retvals
[3] = 0; /* Stack limit. */
1055 for (i
= 0; i
< ARRAY_SIZE(retvals
); i
++) {
1059 fail
= put_user_u64(retvals
[i
], arg0
+ i
* 8);
1061 fail
= put_user_u32(retvals
[i
], arg0
+ i
* 4);
1065 /* Couldn't write back to argument block */
1067 return set_swi_errno(env
, -1);
1072 case TARGET_SYS_EXIT
:
1073 case TARGET_SYS_EXIT_EXTENDED
:
1074 if (nr
== TARGET_SYS_EXIT_EXTENDED
|| is_a64(env
)) {
1076 * The A64 version of SYS_EXIT takes a parameter block,
1077 * so the application-exit type can return a subcode which
1078 * is the exit status code from the application.
1079 * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
1080 * which allows A32/T32 guests to also provide a status code.
1085 if (arg0
== ADP_Stopped_ApplicationExit
) {
1092 * The A32/T32 version of SYS_EXIT specifies only
1093 * Stopped_ApplicationExit as normal exit, but does not
1094 * allow the guest to specify the exit status code.
1095 * Everything else is considered an error.
1097 ret
= (args
== ADP_Stopped_ApplicationExit
) ? 0 : 1;
1101 case TARGET_SYS_SYNCCACHE
:
1103 * Clean the D-cache and invalidate the I-cache for the specified
1104 * virtual address range. This is a nop for us since we don't
1105 * implement caches. This is only present on A64.
1110 /* fall through -- invalid for A32/T32 */
1112 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
1113 cpu_dump_state(cs
, stderr
, 0);