semihosting: Adjust error checking in common_semi_cb
[qemu/ar7.git] / semihosting / arm-compat-semi.c
blob88d6bdeaf27c3d9d111ff95a80ed45795b9956a7
1 /*
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:
30 * RISC-V Semihosting
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
43 #include "qemu.h"
45 #define COMMON_SEMI_HEAP_SIZE (128 * 1024 * 1024)
46 #else
47 #include "qemu/cutils.h"
48 #include "hw/loader.h"
49 #ifdef TARGET_ARM
50 #include "hw/arm/boot.h"
51 #endif
52 #include "hw/boards.h"
53 #endif
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)
85 #ifndef O_BINARY
86 #define O_BINARY 0
87 #endif
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] = {
98 GDB_O_RDONLY,
99 GDB_O_RDONLY | GDB_O_BINARY,
100 GDB_O_RDWR,
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] = {
113 O_RDONLY,
114 O_RDONLY | O_BINARY,
115 O_RDWR,
116 O_RDWR | O_BINARY,
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"
136 * regions.
138 * Returns: a structure with the numbers we need.
141 typedef struct LayoutInfo {
142 target_ulong rambase;
143 size_t ramsize;
144 hwaddr heapbase;
145 hwaddr heaplimit;
146 } LayoutInfo;
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) {
155 return false;
158 if (size > info->ramsize) {
159 info->rambase = int128_get64(start);
160 info->ramsize = size;
163 /* search exhaustively for largest RAM */
164 return false;
167 static LayoutInfo common_semi_find_bases(CPUState *cs)
169 FlatView *fv;
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;
188 return info;
191 #endif
193 #ifdef TARGET_ARM
194 static inline target_ulong
195 common_semi_arg(CPUState *cs, int argno)
197 ARMCPU *cpu = ARM_CPU(cs);
198 CPUARMState *env = &cpu->env;
199 if (is_a64(env)) {
200 return env->xregs[argno];
201 } else {
202 return env->regs[argno];
206 static inline void
207 common_semi_set_ret(CPUState *cs, target_ulong ret)
209 ARMCPU *cpu = ARM_CPU(cs);
210 CPUARMState *env = &cpu->env;
211 if (is_a64(env)) {
212 env->xregs[0] = ret;
213 } else {
214 env->regs[0] = ret;
218 static inline bool
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 */
226 #ifdef TARGET_RISCV
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];
235 static inline void
236 common_semi_set_ret(CPUState *cs, target_ulong ret)
238 RISCVCPU *cpu = RISCV_CPU(cs);
239 CPURISCVState *env = &cpu->env;
240 env->gpr[xA0] = ret;
243 static inline bool
244 common_semi_sys_exit_extended(CPUState *cs, int nr)
246 return (nr == TARGET_SYS_EXIT_EXTENDED || sizeof(target_ulong) == 8);
249 #endif
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"
262 #endif
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;
271 #else
272 syscall_err = errno;
273 #endif
275 return code;
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;
284 #else
285 return syscall_err;
286 #endif
289 static target_ulong common_semi_syscall_len;
291 static void common_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
293 if (err) {
294 #ifdef CONFIG_USER_ONLY
295 TaskState *ts = cs->opaque;
296 ts->swi_errno = err;
297 #else
298 syscall_err = err;
299 #endif
300 } else {
301 /* Fixup syscalls that use nonstardard return conventions. */
302 target_ulong reg0 = common_semi_arg(cs, 0);
303 switch (reg0) {
304 case TARGET_SYS_WRITE:
305 case TARGET_SYS_READ:
306 ret = common_semi_syscall_len - ret;
307 break;
308 case TARGET_SYS_SEEK:
309 ret = 0;
310 break;
311 default:
312 break;
315 common_semi_set_ret(cs, ret);
318 static target_ulong common_semi_flen_buf(CPUState *cs)
320 target_ulong sp;
321 #ifdef TARGET_ARM
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;
330 if (is_a64(env)) {
331 sp = env->xregs[31];
332 } else {
333 sp = env->regs[13];
335 #endif
336 #ifdef TARGET_RISCV
337 RISCVCPU *cpu = RISCV_CPU(cs);
338 CPURISCVState *env = &cpu->env;
340 sp = env->gpr[xSP];
341 #endif
343 return sp - 64;
346 static void
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. */
351 uint32_t size;
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);
356 errno = err;
357 set_swi_errno(cs, -1);
360 static int common_semi_open_guestfd;
362 static void
363 common_semi_open_cb(CPUState *cs, target_ulong ret, target_ulong err)
365 if (ret == (target_ulong)-1) {
366 errno = err;
367 set_swi_errno(cs, -1);
368 dealloc_guestfd(common_semi_open_guestfd);
369 } else {
370 associate_guestfd(common_semi_open_guestfd, ret);
371 ret = common_semi_open_guestfd;
373 common_semi_set_ret(cs, ret);
376 static target_ulong
377 common_semi_gdb_syscall(CPUState *cs, gdb_syscall_complete_cb cb,
378 const char *fmt, ...)
380 va_list va;
382 va_start(va, fmt);
383 gdb_do_syscallv(cb, fmt, va);
384 va_end(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) {
431 return 0;
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;
440 uint32_t ret;
441 char *s = lock_user(VERIFY_READ, buf, len, 1);
442 (void) env; /* Used in arm softmmu lock_user implicitly */
443 if (!s) {
444 /* Return bytes not written on error */
445 return len;
447 ret = set_swi_errno(cs, write(gf->hostfd, s, len));
448 unlock_user(s, buf, 0);
449 if (ret == (uint32_t)-1) {
450 ret = 0;
452 /* Return bytes not written */
453 return len - ret;
456 static uint32_t host_readfn(CPUState *cs, GuestFD *gf,
457 target_ulong buf, uint32_t len)
459 CPUArchState *env = cs->env_ptr;
460 uint32_t ret;
461 char *s = lock_user(VERIFY_WRITE, buf, len, 0);
462 (void) env; /* Used in arm softmmu lock_user implicitly */
463 if (!s) {
464 /* return bytes not read */
465 return len;
467 do {
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) {
472 ret = 0;
474 /* Return bytes not read */
475 return len - ret;
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) {
487 return -1;
489 return 0;
492 static uint32_t host_flenfn(CPUState *cs, GuestFD *gf)
494 struct stat buf;
495 uint32_t ret = set_swi_errno(cs, fstat(gf->hostfd, &buf));
496 if (ret == (uint32_t)-1) {
497 return -1;
499 return buf.st_size;
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",
531 gf->hostfd, offset);
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[] = {
550 SHFB_MAGIC_0,
551 SHFB_MAGIC_1,
552 SHFB_MAGIC_2,
553 SHFB_MAGIC_3,
554 SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */
557 static uint32_t staticfile_closefn(CPUState *cs, GuestFD *gf)
559 /* Nothing to do */
560 return 0;
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 */
568 errno = EBADF;
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;
576 uint32_t i;
577 char *s;
579 (void) env; /* Used in arm softmmu lock_user implicitly */
580 s = lock_user(VERIFY_WRITE, buf, len, 0);
581 if (!s) {
582 return len;
585 for (i = 0; i < len; i++) {
586 if (gf->staticfile.off >= gf->staticfile.len) {
587 break;
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 */
596 return len - i;
599 static uint32_t staticfile_isattyfn(CPUState *cs, GuestFD *gf)
601 return 0;
604 static uint32_t staticfile_seekfn(CPUState *cs, GuestFD *gf,
605 target_ulong offset)
607 gf->staticfile.off = offset;
608 return 0;
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;
619 sys_readfn *readfn;
620 sys_isattyfn *isattyfn;
621 sys_seekfn *seekfn;
622 sys_flenfn *flenfn;
623 } GuestFDFunctions;
625 static const GuestFDFunctions guestfd_fns[] = {
626 [GuestFDHost] = {
627 .closefn = host_closefn,
628 .writefn = host_writefn,
629 .readfn = host_readfn,
630 .isattyfn = host_isattyfn,
631 .seekfn = host_seekfn,
632 .flenfn = host_flenfn,
634 [GuestFDGDB] = {
635 .closefn = gdb_closefn,
636 .writefn = gdb_writefn,
637 .readfn = gdb_readfn,
638 .isattyfn = gdb_isattyfn,
639 .seekfn = gdb_seekfn,
640 .flenfn = gdb_flenfn,
642 [GuestFDStatic] = {
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)
660 return is_a64(env);
661 #elif defined(TARGET_RISCV)
662 return riscv_cpu_mxl(env) != MXL_RV32;
663 #else
664 #error un-handled architecture
665 #endif
669 #define GET_ARG(n) do { \
670 if (is_64bit_semihosting(env)) { \
671 if (get_user_u64(arg ## n, args + (n) * 8)) { \
672 errno = EFAULT; \
673 return set_swi_errno(cs, -1); \
675 } else { \
676 if (get_user_u32(arg ## n, args + (n) * 4)) { \
677 errno = EFAULT; \
678 return set_swi_errno(cs, -1); \
681 } while (0)
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;
701 target_ulong args;
702 target_ulong arg0, arg1, arg2, arg3;
703 target_ulong ul_ret;
704 char * s;
705 int nr;
706 uint32_t ret;
707 uint32_t len;
708 GuestFD *gf;
709 int64_t elapsed;
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);
715 switch (nr) {
716 case TARGET_SYS_OPEN:
718 int guestfd;
720 GET_ARG(0);
721 GET_ARG(1);
722 GET_ARG(2);
723 s = lock_user_string(arg0);
724 if (!s) {
725 errno = EFAULT;
726 return set_swi_errno(cs, -1);
728 if (arg1 >= 12) {
729 unlock_user(s, arg0, 0);
730 errno = EINVAL;
731 return set_swi_errno(cs, -1);
734 guestfd = alloc_guestfd();
735 if (guestfd < 0) {
736 unlock_user(s, arg0, 0);
737 errno = EMFILE;
738 return set_swi_errno(cs, -1);
741 if (strcmp(s, ":tt") == 0) {
742 int result_fileno;
745 * We implement SH_EXT_STDOUT_STDERR, so:
746 * open for read == stdin
747 * open for write == stdout
748 * open for append == stderr
750 if (arg1 < 4) {
751 result_fileno = STDIN_FILENO;
752 } else if (arg1 < 8) {
753 result_fileno = STDOUT_FILENO;
754 } else {
755 result_fileno = STDERR_FILENO;
757 associate_guestfd(guestfd, result_fileno);
758 unlock_user(s, arg0, 0);
759 return guestfd;
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);
766 errno = EACCES;
767 return set_swi_errno(cs, -1);
769 staticfile_guestfd(guestfd, featurefile_data,
770 sizeof(featurefile_data));
771 return guestfd;
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]);
779 } else {
780 ret = set_swi_errno(cs, open(s, open_modeflags[arg1], 0644));
781 if (ret == (uint32_t)-1) {
782 dealloc_guestfd(guestfd);
783 } else {
784 associate_guestfd(guestfd, ret);
785 ret = guestfd;
788 unlock_user(s, arg0, 0);
789 return ret;
791 case TARGET_SYS_CLOSE:
792 GET_ARG(0);
794 gf = get_guestfd(arg0);
795 if (!gf) {
796 errno = EBADF;
797 return set_swi_errno(cs, -1);
800 ret = guestfd_fns[gf->type].closefn(cs, gf);
801 dealloc_guestfd(arg0);
802 return ret;
803 case TARGET_SYS_WRITEC:
804 qemu_semihosting_console_outc(cs->env_ptr, args);
805 return 0xdeadbeef;
806 case TARGET_SYS_WRITE0:
807 return qemu_semihosting_console_outs(cs->env_ptr, args);
808 case TARGET_SYS_WRITE:
809 GET_ARG(0);
810 GET_ARG(1);
811 GET_ARG(2);
812 len = arg2;
814 gf = get_guestfd(arg0);
815 if (!gf) {
816 errno = EBADF;
817 return set_swi_errno(cs, -1);
820 return guestfd_fns[gf->type].writefn(cs, gf, arg1, len);
821 case TARGET_SYS_READ:
822 GET_ARG(0);
823 GET_ARG(1);
824 GET_ARG(2);
825 len = arg2;
827 gf = get_guestfd(arg0);
828 if (!gf) {
829 errno = EBADF;
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:
837 GET_ARG(0);
838 return (target_long) arg0 < 0 ? 1 : 0;
839 case TARGET_SYS_ISTTY:
840 GET_ARG(0);
842 gf = get_guestfd(arg0);
843 if (!gf) {
844 errno = EBADF;
845 return set_swi_errno(cs, -1);
848 return guestfd_fns[gf->type].isattyfn(cs, gf);
849 case TARGET_SYS_SEEK:
850 GET_ARG(0);
851 GET_ARG(1);
853 gf = get_guestfd(arg0);
854 if (!gf) {
855 errno = EBADF;
856 return set_swi_errno(cs, -1);
859 return guestfd_fns[gf->type].seekfn(cs, gf, arg1);
860 case TARGET_SYS_FLEN:
861 GET_ARG(0);
863 gf = get_guestfd(arg0);
864 if (!gf) {
865 errno = EBADF;
866 return set_swi_errno(cs, -1);
869 return guestfd_fns[gf->type].flenfn(cs, gf);
870 case TARGET_SYS_TMPNAM:
871 GET_ARG(0);
872 GET_ARG(1);
873 GET_ARG(2);
874 if (asprintf(&s, "/tmp/qemu-%x%02x", getpid(),
875 (int) (arg1 & 0xff)) < 0) {
876 return -1;
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);
883 strcpy(output, s);
884 unlock_user(output, arg0, arg2);
885 ul_ret = 0;
887 free(s);
888 return ul_ret;
889 case TARGET_SYS_REMOVE:
890 GET_ARG(0);
891 GET_ARG(1);
892 if (use_gdb_syscalls()) {
893 ret = common_semi_gdb_syscall(cs, common_semi_cb, "unlink,%s",
894 arg0, (int)arg1 + 1);
895 } else {
896 s = lock_user_string(arg0);
897 if (!s) {
898 errno = EFAULT;
899 return set_swi_errno(cs, -1);
901 ret = set_swi_errno(cs, remove(s));
902 unlock_user(s, arg0, 0);
904 return ret;
905 case TARGET_SYS_RENAME:
906 GET_ARG(0);
907 GET_ARG(1);
908 GET_ARG(2);
909 GET_ARG(3);
910 if (use_gdb_syscalls()) {
911 return common_semi_gdb_syscall(cs, common_semi_cb, "rename,%s,%s",
912 arg0, (int)arg1 + 1, arg2,
913 (int)arg3 + 1);
914 } else {
915 char *s2;
916 s = lock_user_string(arg0);
917 s2 = lock_user_string(arg2);
918 if (!s || !s2) {
919 errno = EFAULT;
920 ret = set_swi_errno(cs, -1);
921 } else {
922 ret = set_swi_errno(cs, rename(s, s2));
924 if (s2)
925 unlock_user(s2, arg2, 0);
926 if (s)
927 unlock_user(s, arg0, 0);
928 return ret;
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:
935 GET_ARG(0);
936 GET_ARG(1);
937 if (use_gdb_syscalls()) {
938 return common_semi_gdb_syscall(cs, common_semi_cb, "system,%s",
939 arg0, (int)arg1 + 1);
940 } else {
941 s = lock_user_string(arg0);
942 if (!s) {
943 errno = EFAULT;
944 return set_swi_errno(cs, -1);
946 ret = set_swi_errno(cs, system(s));
947 unlock_user(s, arg0, 0);
948 return ret;
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.
956 * The inputs are:
957 * * arg0, pointer to a buffer of at least the size
958 * specified in arg1.
959 * * arg1, size of the buffer pointed to by arg0 in
960 * bytes.
962 * The outputs are:
963 * * arg0, pointer to null-terminated string of the
964 * command line.
965 * * arg1, length of the string pointed to by arg0.
968 char *output_buffer;
969 size_t input_size;
970 size_t output_size;
971 int status = 0;
972 #if !defined(CONFIG_USER_ONLY)
973 const char *cmdline;
974 #else
975 TaskState *ts = cs->opaque;
976 #endif
977 GET_ARG(0);
978 GET_ARG(1);
979 input_size = arg1;
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. */
987 #else
988 unsigned int i;
990 output_size = ts->info->env_strings - ts->info->arg_strings;
991 if (!output_size) {
993 * We special-case the "empty command line" case (argc==0).
994 * Just provide the terminating 0.
996 output_size = 1;
998 #endif
1000 if (output_size > input_size) {
1001 /* Not enough space to store command-line arguments. */
1002 errno = E2BIG;
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 */
1009 errno = EFAULT;
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) {
1016 errno = EFAULT;
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);
1023 #else
1024 if (output_size == 1) {
1025 /* Empty command-line. */
1026 output_buffer[0] = '\0';
1027 goto out;
1030 if (copy_from_user(output_buffer, ts->info->arg_strings,
1031 output_size)) {
1032 errno = EFAULT;
1033 status = set_swi_errno(cs, -1);
1034 goto out;
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] = ' ';
1043 out:
1044 #endif
1045 /* Unlock the buffer on the ARM side. */
1046 unlock_user(output_buffer, arg0, output_size);
1048 return status;
1050 case TARGET_SYS_HEAPINFO:
1052 target_ulong retvals[4];
1053 int i;
1054 #ifdef CONFIG_USER_ONLY
1055 TaskState *ts = cs->opaque;
1056 target_ulong limit;
1057 #else
1058 LayoutInfo info = common_semi_find_bases(cs);
1059 #endif
1061 GET_ARG(0);
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) {
1069 abi_ulong ret;
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. */
1074 for (;;) {
1075 ret = do_brk(limit);
1076 if (ret >= limit) {
1077 break;
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. */
1088 #else
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. */
1093 #endif
1095 for (i = 0; i < ARRAY_SIZE(retvals); i++) {
1096 bool fail;
1098 if (is_64bit_semihosting(env)) {
1099 fail = put_user_u64(retvals[i], arg0 + i * 8);
1100 } else {
1101 fail = put_user_u32(retvals[i], arg0 + i * 4);
1104 if (fail) {
1105 /* Couldn't write back to argument block */
1106 errno = EFAULT;
1107 return set_swi_errno(cs, -1);
1110 return 0;
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.
1122 GET_ARG(0);
1123 GET_ARG(1);
1125 if (arg0 == ADP_Stopped_ApplicationExit) {
1126 ret = arg1;
1127 } else {
1128 ret = 1;
1130 } else {
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;
1139 gdb_exit(ret);
1140 exit(ret);
1141 case TARGET_SYS_ELAPSED:
1142 elapsed = get_clock() - clock_start;
1143 if (sizeof(target_ulong) == 8) {
1144 SET_ARG(0, elapsed);
1145 } else {
1146 SET_ARG(0, (uint32_t) elapsed);
1147 SET_ARG(1, (uint32_t) (elapsed >> 32));
1149 return 0;
1150 case TARGET_SYS_TICKFREQ:
1151 /* qemu always uses nsec */
1152 return 1000000000;
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.
1159 #ifdef TARGET_ARM
1160 if (is_a64(cs->env_ptr)) {
1161 return 0;
1163 #endif
1164 #ifdef TARGET_RISCV
1165 return 0;
1166 #endif
1167 /* fall through -- invalid for A32/T32 */
1168 default:
1169 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
1170 cpu_dump_state(cs, stderr, 0);
1171 abort();