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 for (i
= 0; i
< guestfd_array
->len
; i
++) {
148 GuestFD
*gf
= &g_array_index(guestfd_array
, GuestFD
, i
);
150 if (gf
->type
== GuestFDUnused
) {
155 /* All elements already in use: expand the array */
156 g_array_set_size(guestfd_array
, i
+ 1);
161 * Look up the guestfd in the data structure; return NULL
162 * for out of bounds, but don't check whether the slot is unused.
163 * This is used internally by the other guestfd functions.
165 static GuestFD
*do_get_guestfd(int guestfd
)
167 if (!guestfd_array
) {
171 if (guestfd
< 0 || guestfd
>= guestfd_array
->len
) {
175 return &g_array_index(guestfd_array
, GuestFD
, guestfd
);
179 * Associate the specified guest fd (which must have been
180 * allocated via alloc_fd() and not previously used) with
181 * the specified host/gdb fd.
183 static void associate_guestfd(int guestfd
, int hostfd
)
185 GuestFD
*gf
= do_get_guestfd(guestfd
);
188 gf
->type
= use_gdb_syscalls() ? GuestFDGDB
: GuestFDHost
;
193 * Deallocate the specified guest file descriptor. This doesn't
194 * close the host fd, it merely undoes the work of alloc_fd().
196 static void dealloc_guestfd(int guestfd
)
198 GuestFD
*gf
= do_get_guestfd(guestfd
);
201 gf
->type
= GuestFDUnused
;
205 * Given a guest file descriptor, get the associated struct.
206 * If the fd is not valid, return NULL. This is the function
207 * used by the various semihosting calls to validate a handle
209 * Note: calling alloc_guestfd() or dealloc_guestfd() will
210 * invalidate any GuestFD* obtained by calling this function.
212 static GuestFD
*get_guestfd(int guestfd
)
214 GuestFD
*gf
= do_get_guestfd(guestfd
);
216 if (!gf
|| gf
->type
== GuestFDUnused
) {
223 * The semihosting API has no concept of its errno being thread-safe,
224 * as the API design predates SMP CPUs and was intended as a simple
225 * real-hardware set of debug functionality. For QEMU, we make the
226 * errno be per-thread in linux-user mode; in softmmu it is a simple
227 * global, and we assume that the guest takes care of avoiding any races.
229 #ifndef CONFIG_USER_ONLY
230 static target_ulong syscall_err
;
232 #include "exec/softmmu-semi.h"
235 static inline uint32_t set_swi_errno(CPUARMState
*env
, uint32_t code
)
237 if (code
== (uint32_t)-1) {
238 #ifdef CONFIG_USER_ONLY
239 CPUState
*cs
= env_cpu(env
);
240 TaskState
*ts
= cs
->opaque
;
242 ts
->swi_errno
= errno
;
250 static inline uint32_t get_swi_errno(CPUARMState
*env
)
252 #ifdef CONFIG_USER_ONLY
253 CPUState
*cs
= env_cpu(env
);
254 TaskState
*ts
= cs
->opaque
;
256 return ts
->swi_errno
;
262 static target_ulong arm_semi_syscall_len
;
264 static void arm_semi_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
266 ARMCPU
*cpu
= ARM_CPU(cs
);
267 CPUARMState
*env
= &cpu
->env
;
268 target_ulong reg0
= is_a64(env
) ? env
->xregs
[0] : env
->regs
[0];
270 if (ret
== (target_ulong
)-1) {
272 set_swi_errno(env
, -1);
275 /* Fixup syscalls that use nonstardard return conventions. */
277 case TARGET_SYS_WRITE
:
278 case TARGET_SYS_READ
:
279 reg0
= arm_semi_syscall_len
- ret
;
281 case TARGET_SYS_SEEK
:
290 env
->xregs
[0] = reg0
;
296 static target_ulong
arm_flen_buf(ARMCPU
*cpu
)
298 /* Return an address in target memory of 64 bytes where the remote
299 * gdb should write its stat struct. (The format of this structure
300 * is defined by GDB's remote protocol and is not target-specific.)
301 * We put this on the guest's stack just below SP.
303 CPUARMState
*env
= &cpu
->env
;
315 static void arm_semi_flen_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
317 ARMCPU
*cpu
= ARM_CPU(cs
);
318 CPUARMState
*env
= &cpu
->env
;
319 /* The size is always stored in big-endian order, extract
320 the value. We assume the size always fit in 32 bits. */
322 cpu_memory_rw_debug(cs
, arm_flen_buf(cpu
) + 32, (uint8_t *)&size
, 4, 0);
323 size
= be32_to_cpu(size
);
325 env
->xregs
[0] = size
;
330 set_swi_errno(env
, -1);
333 static int arm_semi_open_guestfd
;
335 static void arm_semi_open_cb(CPUState
*cs
, target_ulong ret
, target_ulong err
)
337 ARMCPU
*cpu
= ARM_CPU(cs
);
338 CPUARMState
*env
= &cpu
->env
;
339 if (ret
== (target_ulong
)-1) {
341 set_swi_errno(env
, -1);
342 dealloc_guestfd(arm_semi_open_guestfd
);
344 associate_guestfd(arm_semi_open_guestfd
, ret
);
345 ret
= arm_semi_open_guestfd
;
355 static target_ulong
arm_gdb_syscall(ARMCPU
*cpu
, gdb_syscall_complete_cb cb
,
356 const char *fmt
, ...)
359 CPUARMState
*env
= &cpu
->env
;
362 gdb_do_syscallv(cb
, fmt
, va
);
366 * FIXME: in softmmu mode, the gdbstub will schedule our callback
367 * to occur, but will not actually call it to complete the syscall
368 * until after this function has returned and we are back in the
369 * CPU main loop. Therefore callers to this function must not
370 * do anything with its return value, because it is not necessarily
371 * the result of the syscall, but could just be the old value of X0.
372 * The only thing safe to do with this is that the callers of
373 * do_arm_semihosting() will write it straight back into X0.
374 * (In linux-user mode, the callback will have happened before
375 * gdb_do_syscallv() returns.)
377 * We should tidy this up so neither this function nor
378 * do_arm_semihosting() return a value, so the mistake of
379 * doing something with the return value is not possible to make.
382 return is_a64(env
) ? env
->xregs
[0] : env
->regs
[0];
386 * Types for functions implementing various semihosting calls
387 * for specific types of guest file descriptor. These must all
388 * do the work and return the required return value for the guest,
389 * setting the guest errno if appropriate.
391 typedef uint32_t sys_closefn(ARMCPU
*cpu
, GuestFD
*gf
);
392 typedef uint32_t sys_writefn(ARMCPU
*cpu
, GuestFD
*gf
,
393 target_ulong buf
, uint32_t len
);
394 typedef uint32_t sys_readfn(ARMCPU
*cpu
, GuestFD
*gf
,
395 target_ulong buf
, uint32_t len
);
396 typedef uint32_t sys_isattyfn(ARMCPU
*cpu
, GuestFD
*gf
);
397 typedef uint32_t sys_seekfn(ARMCPU
*cpu
, GuestFD
*gf
,
398 target_ulong offset
);
399 typedef uint32_t sys_flenfn(ARMCPU
*cpu
, GuestFD
*gf
);
401 static uint32_t host_closefn(ARMCPU
*cpu
, GuestFD
*gf
)
403 CPUARMState
*env
= &cpu
->env
;
405 return set_swi_errno(env
, close(gf
->hostfd
));
408 static uint32_t host_writefn(ARMCPU
*cpu
, GuestFD
*gf
,
409 target_ulong buf
, uint32_t len
)
412 CPUARMState
*env
= &cpu
->env
;
413 char *s
= lock_user(VERIFY_READ
, buf
, len
, 1);
415 /* Return bytes not written on error */
418 ret
= set_swi_errno(env
, write(gf
->hostfd
, s
, len
));
419 unlock_user(s
, buf
, 0);
420 if (ret
== (uint32_t)-1) {
423 /* Return bytes not written */
427 static uint32_t host_readfn(ARMCPU
*cpu
, GuestFD
*gf
,
428 target_ulong buf
, uint32_t len
)
431 CPUARMState
*env
= &cpu
->env
;
432 char *s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
434 /* return bytes not read */
438 ret
= set_swi_errno(env
, read(gf
->hostfd
, s
, len
));
439 } while (ret
== -1 && errno
== EINTR
);
440 unlock_user(s
, buf
, len
);
441 if (ret
== (uint32_t)-1) {
444 /* Return bytes not read */
448 static uint32_t host_isattyfn(ARMCPU
*cpu
, GuestFD
*gf
)
450 return isatty(gf
->hostfd
);
453 static uint32_t host_seekfn(ARMCPU
*cpu
, GuestFD
*gf
, target_ulong offset
)
455 CPUARMState
*env
= &cpu
->env
;
456 uint32_t ret
= set_swi_errno(env
, lseek(gf
->hostfd
, offset
, SEEK_SET
));
457 if (ret
== (uint32_t)-1) {
463 static uint32_t host_flenfn(ARMCPU
*cpu
, GuestFD
*gf
)
465 CPUARMState
*env
= &cpu
->env
;
467 uint32_t ret
= set_swi_errno(env
, fstat(gf
->hostfd
, &buf
));
468 if (ret
== (uint32_t)-1) {
474 static uint32_t gdb_closefn(ARMCPU
*cpu
, GuestFD
*gf
)
476 return arm_gdb_syscall(cpu
, arm_semi_cb
, "close,%x", gf
->hostfd
);
479 static uint32_t gdb_writefn(ARMCPU
*cpu
, GuestFD
*gf
,
480 target_ulong buf
, uint32_t len
)
482 arm_semi_syscall_len
= len
;
483 return arm_gdb_syscall(cpu
, arm_semi_cb
, "write,%x,%x,%x",
484 gf
->hostfd
, buf
, len
);
487 static uint32_t gdb_readfn(ARMCPU
*cpu
, GuestFD
*gf
,
488 target_ulong buf
, uint32_t len
)
490 arm_semi_syscall_len
= len
;
491 return arm_gdb_syscall(cpu
, arm_semi_cb
, "read,%x,%x,%x",
492 gf
->hostfd
, buf
, len
);
495 static uint32_t gdb_isattyfn(ARMCPU
*cpu
, GuestFD
*gf
)
497 return arm_gdb_syscall(cpu
, arm_semi_cb
, "isatty,%x", gf
->hostfd
);
500 static uint32_t gdb_seekfn(ARMCPU
*cpu
, GuestFD
*gf
, target_ulong offset
)
502 return arm_gdb_syscall(cpu
, arm_semi_cb
, "lseek,%x,%x,0",
506 static uint32_t gdb_flenfn(ARMCPU
*cpu
, GuestFD
*gf
)
508 return arm_gdb_syscall(cpu
, arm_semi_flen_cb
, "fstat,%x,%x",
509 gf
->hostfd
, arm_flen_buf(cpu
));
512 #define SHFB_MAGIC_0 0x53
513 #define SHFB_MAGIC_1 0x48
514 #define SHFB_MAGIC_2 0x46
515 #define SHFB_MAGIC_3 0x42
517 /* Feature bits reportable in feature byte 0 */
518 #define SH_EXT_EXIT_EXTENDED (1 << 0)
519 #define SH_EXT_STDOUT_STDERR (1 << 1)
521 static const uint8_t featurefile_data
[] = {
526 SH_EXT_EXIT_EXTENDED
| SH_EXT_STDOUT_STDERR
, /* Feature byte 0 */
529 static void init_featurefile_guestfd(int guestfd
)
531 GuestFD
*gf
= do_get_guestfd(guestfd
);
534 gf
->type
= GuestFDFeatureFile
;
535 gf
->featurefile_offset
= 0;
538 static uint32_t featurefile_closefn(ARMCPU
*cpu
, GuestFD
*gf
)
544 static uint32_t featurefile_writefn(ARMCPU
*cpu
, GuestFD
*gf
,
545 target_ulong buf
, uint32_t len
)
547 /* This fd can never be open for writing */
548 CPUARMState
*env
= &cpu
->env
;
551 return set_swi_errno(env
, -1);
554 static uint32_t featurefile_readfn(ARMCPU
*cpu
, GuestFD
*gf
,
555 target_ulong buf
, uint32_t len
)
558 #ifndef CONFIG_USER_ONLY
559 CPUARMState
*env
= &cpu
->env
;
563 s
= lock_user(VERIFY_WRITE
, buf
, len
, 0);
568 for (i
= 0; i
< len
; i
++) {
569 if (gf
->featurefile_offset
>= sizeof(featurefile_data
)) {
572 s
[i
] = featurefile_data
[gf
->featurefile_offset
];
573 gf
->featurefile_offset
++;
576 unlock_user(s
, buf
, len
);
578 /* Return number of bytes not read */
582 static uint32_t featurefile_isattyfn(ARMCPU
*cpu
, GuestFD
*gf
)
587 static uint32_t featurefile_seekfn(ARMCPU
*cpu
, GuestFD
*gf
,
590 gf
->featurefile_offset
= offset
;
594 static uint32_t featurefile_flenfn(ARMCPU
*cpu
, GuestFD
*gf
)
596 return sizeof(featurefile_data
);
599 typedef struct GuestFDFunctions
{
600 sys_closefn
*closefn
;
601 sys_writefn
*writefn
;
603 sys_isattyfn
*isattyfn
;
608 static const GuestFDFunctions guestfd_fns
[] = {
610 .closefn
= host_closefn
,
611 .writefn
= host_writefn
,
612 .readfn
= host_readfn
,
613 .isattyfn
= host_isattyfn
,
614 .seekfn
= host_seekfn
,
615 .flenfn
= host_flenfn
,
618 .closefn
= gdb_closefn
,
619 .writefn
= gdb_writefn
,
620 .readfn
= gdb_readfn
,
621 .isattyfn
= gdb_isattyfn
,
622 .seekfn
= gdb_seekfn
,
623 .flenfn
= gdb_flenfn
,
625 [GuestFDFeatureFile
] = {
626 .closefn
= featurefile_closefn
,
627 .writefn
= featurefile_writefn
,
628 .readfn
= featurefile_readfn
,
629 .isattyfn
= featurefile_isattyfn
,
630 .seekfn
= featurefile_seekfn
,
631 .flenfn
= featurefile_flenfn
,
635 /* Read the input value from the argument block; fail the semihosting
636 * call if the memory read fails.
638 #define GET_ARG(n) do { \
640 if (get_user_u64(arg ## n, args + (n) * 8)) { \
642 return set_swi_errno(env, -1); \
645 if (get_user_u32(arg ## n, args + (n) * 4)) { \
647 return set_swi_errno(env, -1); \
652 #define SET_ARG(n, val) \
654 put_user_u64(val, args + (n) * 8) : \
655 put_user_u32(val, args + (n) * 4))
658 * Do a semihosting call.
660 * The specification always says that the "return register" either
661 * returns a specific value or is corrupted, so we don't need to
662 * report to our caller whether we are returning a value or trying to
663 * leave the register unchanged. We use 0xdeadbeef as the return value
664 * when there isn't a defined return value for the call.
666 target_ulong
do_arm_semihosting(CPUARMState
*env
)
668 ARMCPU
*cpu
= env_archcpu(env
);
669 CPUState
*cs
= env_cpu(env
);
671 target_ulong arg0
, arg1
, arg2
, arg3
;
679 /* Note that the syscall number is in W0, not X0 */
680 nr
= env
->xregs
[0] & 0xffffffffU
;
681 args
= env
->xregs
[1];
688 case TARGET_SYS_OPEN
:
695 s
= lock_user_string(arg0
);
698 return set_swi_errno(env
, -1);
701 unlock_user(s
, arg0
, 0);
703 return set_swi_errno(env
, -1);
706 guestfd
= alloc_guestfd();
708 unlock_user(s
, arg0
, 0);
710 return set_swi_errno(env
, -1);
713 if (strcmp(s
, ":tt") == 0) {
717 * We implement SH_EXT_STDOUT_STDERR, so:
718 * open for read == stdin
719 * open for write == stdout
720 * open for append == stderr
723 result_fileno
= STDIN_FILENO
;
724 } else if (arg1
< 8) {
725 result_fileno
= STDOUT_FILENO
;
727 result_fileno
= STDERR_FILENO
;
729 associate_guestfd(guestfd
, result_fileno
);
730 unlock_user(s
, arg0
, 0);
733 if (strcmp(s
, ":semihosting-features") == 0) {
734 unlock_user(s
, arg0
, 0);
735 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
736 if (arg1
!= 0 && arg1
!= 1) {
737 dealloc_guestfd(guestfd
);
739 return set_swi_errno(env
, -1);
741 init_featurefile_guestfd(guestfd
);
745 if (use_gdb_syscalls()) {
746 arm_semi_open_guestfd
= guestfd
;
747 ret
= arm_gdb_syscall(cpu
, arm_semi_open_cb
, "open,%s,%x,1a4", arg0
,
748 (int)arg2
+1, gdb_open_modeflags
[arg1
]);
750 ret
= set_swi_errno(env
, open(s
, open_modeflags
[arg1
], 0644));
751 if (ret
== (uint32_t)-1) {
752 dealloc_guestfd(guestfd
);
754 associate_guestfd(guestfd
, ret
);
758 unlock_user(s
, arg0
, 0);
761 case TARGET_SYS_CLOSE
:
764 gf
= get_guestfd(arg0
);
767 return set_swi_errno(env
, -1);
770 ret
= guestfd_fns
[gf
->type
].closefn(cpu
, gf
);
771 dealloc_guestfd(arg0
);
773 case TARGET_SYS_WRITEC
:
774 qemu_semihosting_console_outc(env
, args
);
776 case TARGET_SYS_WRITE0
:
777 return qemu_semihosting_console_outs(env
, args
);
778 case TARGET_SYS_WRITE
:
784 gf
= get_guestfd(arg0
);
787 return set_swi_errno(env
, -1);
790 return guestfd_fns
[gf
->type
].writefn(cpu
, gf
, arg1
, len
);
791 case TARGET_SYS_READ
:
797 gf
= get_guestfd(arg0
);
800 return set_swi_errno(env
, -1);
803 return guestfd_fns
[gf
->type
].readfn(cpu
, gf
, arg1
, len
);
804 case TARGET_SYS_READC
:
805 qemu_log_mask(LOG_UNIMP
, "%s: SYS_READC not implemented", __func__
);
807 case TARGET_SYS_ISTTY
:
810 gf
= get_guestfd(arg0
);
813 return set_swi_errno(env
, -1);
816 return guestfd_fns
[gf
->type
].isattyfn(cpu
, gf
);
817 case TARGET_SYS_SEEK
:
821 gf
= get_guestfd(arg0
);
824 return set_swi_errno(env
, -1);
827 return guestfd_fns
[gf
->type
].seekfn(cpu
, gf
, arg1
);
828 case TARGET_SYS_FLEN
:
831 gf
= get_guestfd(arg0
);
834 return set_swi_errno(env
, -1);
837 return guestfd_fns
[gf
->type
].flenfn(cpu
, gf
);
838 case TARGET_SYS_TMPNAM
:
839 qemu_log_mask(LOG_UNIMP
, "%s: SYS_TMPNAM not implemented", __func__
);
841 case TARGET_SYS_REMOVE
:
844 if (use_gdb_syscalls()) {
845 ret
= arm_gdb_syscall(cpu
, arm_semi_cb
, "unlink,%s",
848 s
= lock_user_string(arg0
);
851 return set_swi_errno(env
, -1);
853 ret
= set_swi_errno(env
, remove(s
));
854 unlock_user(s
, arg0
, 0);
857 case TARGET_SYS_RENAME
:
862 if (use_gdb_syscalls()) {
863 return arm_gdb_syscall(cpu
, arm_semi_cb
, "rename,%s,%s",
864 arg0
, (int)arg1
+1, arg2
, (int)arg3
+1);
867 s
= lock_user_string(arg0
);
868 s2
= lock_user_string(arg2
);
871 ret
= set_swi_errno(env
, -1);
873 ret
= set_swi_errno(env
, rename(s
, s2
));
876 unlock_user(s2
, arg2
, 0);
878 unlock_user(s
, arg0
, 0);
881 case TARGET_SYS_CLOCK
:
882 return clock() / (CLOCKS_PER_SEC
/ 100);
883 case TARGET_SYS_TIME
:
884 return set_swi_errno(env
, time(NULL
));
885 case TARGET_SYS_SYSTEM
:
888 if (use_gdb_syscalls()) {
889 return arm_gdb_syscall(cpu
, arm_semi_cb
, "system,%s",
892 s
= lock_user_string(arg0
);
895 return set_swi_errno(env
, -1);
897 ret
= set_swi_errno(env
, system(s
));
898 unlock_user(s
, arg0
, 0);
901 case TARGET_SYS_ERRNO
:
902 return get_swi_errno(env
);
903 case TARGET_SYS_GET_CMDLINE
:
905 /* Build a command-line from the original argv.
908 * * arg0, pointer to a buffer of at least the size
910 * * arg1, size of the buffer pointed to by arg0 in
914 * * arg0, pointer to null-terminated string of the
916 * * arg1, length of the string pointed to by arg0.
923 #if !defined(CONFIG_USER_ONLY)
926 TaskState
*ts
= cs
->opaque
;
931 /* Compute the size of the output string. */
932 #if !defined(CONFIG_USER_ONLY)
933 cmdline
= semihosting_get_cmdline();
934 if (cmdline
== NULL
) {
935 cmdline
= ""; /* Default to an empty line. */
937 output_size
= strlen(cmdline
) + 1; /* Count terminating 0. */
941 output_size
= ts
->info
->arg_end
- ts
->info
->arg_start
;
944 * We special-case the "empty command line" case (argc==0).
945 * Just provide the terminating 0.
951 if (output_size
> input_size
) {
952 /* Not enough space to store command-line arguments. */
954 return set_swi_errno(env
, -1);
957 /* Adjust the command-line length. */
958 if (SET_ARG(1, output_size
- 1)) {
959 /* Couldn't write back to argument block */
961 return set_swi_errno(env
, -1);
964 /* Lock the buffer on the ARM side. */
965 output_buffer
= lock_user(VERIFY_WRITE
, arg0
, output_size
, 0);
966 if (!output_buffer
) {
968 return set_swi_errno(env
, -1);
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_start
,
984 status
= set_swi_errno(env
, -1);
988 /* Separate arguments by white spaces. */
989 for (i
= 0; i
< output_size
- 1; i
++) {
990 if (output_buffer
[i
] == 0) {
991 output_buffer
[i
] = ' ';
996 /* Unlock the buffer on the ARM side. */
997 unlock_user(output_buffer
, arg0
, output_size
);
1001 case TARGET_SYS_HEAPINFO
:
1003 target_ulong retvals
[4];
1006 #ifdef CONFIG_USER_ONLY
1007 TaskState
*ts
= cs
->opaque
;
1012 #ifdef CONFIG_USER_ONLY
1014 * Some C libraries assume the heap immediately follows .bss, so
1015 * allocate it using sbrk.
1017 if (!ts
->heap_limit
) {
1020 ts
->heap_base
= do_brk(0);
1021 limit
= ts
->heap_base
+ ARM_ANGEL_HEAP_SIZE
;
1022 /* Try a big heap, and reduce the size if that fails. */
1024 ret
= do_brk(limit
);
1028 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
1030 ts
->heap_limit
= limit
;
1033 retvals
[0] = ts
->heap_base
;
1034 retvals
[1] = ts
->heap_limit
;
1035 retvals
[2] = ts
->stack_base
;
1036 retvals
[3] = 0; /* Stack limit. */
1039 /* TODO: Make this use the limit of the loaded application. */
1040 retvals
[0] = limit
/ 2;
1042 retvals
[2] = limit
; /* Stack base */
1043 retvals
[3] = 0; /* Stack limit. */
1046 for (i
= 0; i
< ARRAY_SIZE(retvals
); i
++) {
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 */
1058 return set_swi_errno(env
, -1);
1063 case TARGET_SYS_EXIT
:
1064 case TARGET_SYS_EXIT_EXTENDED
:
1065 if (nr
== TARGET_SYS_EXIT_EXTENDED
|| is_a64(env
)) {
1067 * The A64 version of SYS_EXIT takes a parameter block,
1068 * so the application-exit type can return a subcode which
1069 * is the exit status code from the application.
1070 * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
1071 * which allows A32/T32 guests to also provide a status code.
1076 if (arg0
== ADP_Stopped_ApplicationExit
) {
1083 * The A32/T32 version of SYS_EXIT specifies only
1084 * Stopped_ApplicationExit as normal exit, but does not
1085 * allow the guest to specify the exit status code.
1086 * Everything else is considered an error.
1088 ret
= (args
== ADP_Stopped_ApplicationExit
) ? 0 : 1;
1092 case TARGET_SYS_SYNCCACHE
:
1094 * Clean the D-cache and invalidate the I-cache for the specified
1095 * virtual address range. This is a nop for us since we don't
1096 * implement caches. This is only present on A64.
1101 /* fall through -- invalid for A32/T32 */
1103 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
1104 cpu_dump_state(cs
, stderr
, 0);