semihosting: Split out common_semi_has_synccache
[qemu/rayw.git] / semihosting / arm-compat-semi.c
blob50f40a2a1a7a7418fed09b3ce1ebcd19fcd87073
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 static int gdb_open_modeflags[12] = {
90 GDB_O_RDONLY,
91 GDB_O_RDONLY,
92 GDB_O_RDWR,
93 GDB_O_RDWR,
94 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
95 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
96 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
97 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
98 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
99 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
100 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
101 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
104 static int open_modeflags[12] = {
105 O_RDONLY,
106 O_RDONLY | O_BINARY,
107 O_RDWR,
108 O_RDWR | O_BINARY,
109 O_WRONLY | O_CREAT | O_TRUNC,
110 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
111 O_RDWR | O_CREAT | O_TRUNC,
112 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
113 O_WRONLY | O_CREAT | O_APPEND,
114 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
115 O_RDWR | O_CREAT | O_APPEND,
116 O_RDWR | O_CREAT | O_APPEND | O_BINARY
119 #ifndef CONFIG_USER_ONLY
122 * common_semi_find_bases: find information about ram and heap base
124 * This function attempts to provide meaningful numbers for RAM and
125 * HEAP base addresses. The rambase is simply the lowest addressable
126 * RAM position. For the heapbase we ask the loader to scan the
127 * address space and the largest available gap by querying the "ROM"
128 * regions.
130 * Returns: a structure with the numbers we need.
133 typedef struct LayoutInfo {
134 target_ulong rambase;
135 size_t ramsize;
136 hwaddr heapbase;
137 hwaddr heaplimit;
138 } LayoutInfo;
140 static bool find_ram_cb(Int128 start, Int128 len, const MemoryRegion *mr,
141 hwaddr offset_in_region, void *opaque)
143 LayoutInfo *info = (LayoutInfo *) opaque;
144 uint64_t size = int128_get64(len);
146 if (!mr->ram || mr->readonly) {
147 return false;
150 if (size > info->ramsize) {
151 info->rambase = int128_get64(start);
152 info->ramsize = size;
155 /* search exhaustively for largest RAM */
156 return false;
159 static LayoutInfo common_semi_find_bases(CPUState *cs)
161 FlatView *fv;
162 LayoutInfo info = { 0, 0, 0, 0 };
164 RCU_READ_LOCK_GUARD();
166 fv = address_space_to_flatview(cs->as);
167 flatview_for_each_range(fv, find_ram_cb, &info);
170 * If we have found the RAM lets iterate through the ROM blobs to
171 * work out the best place for the remainder of RAM and split it
172 * equally between stack and heap.
174 if (info.rambase || info.ramsize > 0) {
175 RomGap gap = rom_find_largest_gap_between(info.rambase, info.ramsize);
176 info.heapbase = gap.base;
177 info.heaplimit = gap.base + gap.size;
180 return info;
183 #endif
185 #ifdef TARGET_ARM
186 static inline target_ulong
187 common_semi_arg(CPUState *cs, int argno)
189 ARMCPU *cpu = ARM_CPU(cs);
190 CPUARMState *env = &cpu->env;
191 if (is_a64(env)) {
192 return env->xregs[argno];
193 } else {
194 return env->regs[argno];
198 static inline void
199 common_semi_set_ret(CPUState *cs, target_ulong ret)
201 ARMCPU *cpu = ARM_CPU(cs);
202 CPUARMState *env = &cpu->env;
203 if (is_a64(env)) {
204 env->xregs[0] = ret;
205 } else {
206 env->regs[0] = ret;
210 static inline bool
211 common_semi_sys_exit_extended(CPUState *cs, int nr)
213 return (nr == TARGET_SYS_EXIT_EXTENDED || is_a64(cs->env_ptr));
216 static inline bool is_64bit_semihosting(CPUArchState *env)
218 return is_a64(env);
221 static inline target_ulong common_semi_stack_bottom(CPUState *cs)
223 ARMCPU *cpu = ARM_CPU(cs);
224 CPUARMState *env = &cpu->env;
225 return is_a64(env) ? env->xregs[31] : env->regs[13];
228 static inline bool common_semi_has_synccache(CPUArchState *env)
230 /* Ok for A64, invalid for A32/T32. */
231 return is_a64(env);
233 #endif /* TARGET_ARM */
235 #ifdef TARGET_RISCV
236 static inline target_ulong
237 common_semi_arg(CPUState *cs, int argno)
239 RISCVCPU *cpu = RISCV_CPU(cs);
240 CPURISCVState *env = &cpu->env;
241 return env->gpr[xA0 + argno];
244 static inline void
245 common_semi_set_ret(CPUState *cs, target_ulong ret)
247 RISCVCPU *cpu = RISCV_CPU(cs);
248 CPURISCVState *env = &cpu->env;
249 env->gpr[xA0] = ret;
252 static inline bool
253 common_semi_sys_exit_extended(CPUState *cs, int nr)
255 return (nr == TARGET_SYS_EXIT_EXTENDED || sizeof(target_ulong) == 8);
258 static inline bool is_64bit_semihosting(CPUArchState *env)
260 return riscv_cpu_mxl(env) != MXL_RV32;
263 static inline target_ulong common_semi_stack_bottom(CPUState *cs)
265 RISCVCPU *cpu = RISCV_CPU(cs);
266 CPURISCVState *env = &cpu->env;
267 return env->gpr[xSP];
270 static inline bool common_semi_has_synccache(CPUArchState *env)
272 return true;
274 #endif
277 * The semihosting API has no concept of its errno being thread-safe,
278 * as the API design predates SMP CPUs and was intended as a simple
279 * real-hardware set of debug functionality. For QEMU, we make the
280 * errno be per-thread in linux-user mode; in softmmu it is a simple
281 * global, and we assume that the guest takes care of avoiding any races.
283 #ifndef CONFIG_USER_ONLY
284 static target_ulong syscall_err;
286 #include "semihosting/softmmu-uaccess.h"
287 #endif
289 static inline uint32_t get_swi_errno(CPUState *cs)
291 #ifdef CONFIG_USER_ONLY
292 TaskState *ts = cs->opaque;
294 return ts->swi_errno;
295 #else
296 return syscall_err;
297 #endif
300 static target_ulong common_semi_syscall_len;
302 static void common_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
304 if (err) {
305 #ifdef CONFIG_USER_ONLY
306 TaskState *ts = cs->opaque;
307 ts->swi_errno = err;
308 #else
309 syscall_err = err;
310 #endif
311 } else {
312 /* Fixup syscalls that use nonstardard return conventions. */
313 target_ulong reg0 = common_semi_arg(cs, 0);
314 switch (reg0) {
315 case TARGET_SYS_WRITE:
316 case TARGET_SYS_READ:
317 ret = common_semi_syscall_len - ret;
318 break;
319 case TARGET_SYS_SEEK:
320 ret = 0;
321 break;
322 default:
323 break;
326 common_semi_set_ret(cs, ret);
330 * Return an address in target memory of 64 bytes where the remote
331 * gdb should write its stat struct. (The format of this structure
332 * is defined by GDB's remote protocol and is not target-specific.)
333 * We put this on the guest's stack just below SP.
335 static target_ulong common_semi_flen_buf(CPUState *cs)
337 target_ulong sp = common_semi_stack_bottom(cs);
338 return sp - 64;
341 static void
342 common_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
344 if (!err) {
345 /* The size is always stored in big-endian order, extract the value. */
346 uint64_t size;
347 cpu_memory_rw_debug(cs, common_semi_flen_buf(cs) +
348 offsetof(struct gdb_stat, gdb_st_size),
349 &size, 8, 0);
350 ret = be64_to_cpu(size);
352 common_semi_cb(cs, ret, err);
355 static int common_semi_open_guestfd;
357 static void
358 common_semi_open_cb(CPUState *cs, target_ulong ret, target_ulong err)
360 if (err) {
361 dealloc_guestfd(common_semi_open_guestfd);
362 } else {
363 associate_guestfd(common_semi_open_guestfd, ret);
364 ret = common_semi_open_guestfd;
366 common_semi_cb(cs, ret, err);
370 * Types for functions implementing various semihosting calls
371 * for specific types of guest file descriptor. These must all
372 * do the work and return the required return value to the guest
373 * via common_semi_cb.
375 typedef void sys_closefn(CPUState *cs, GuestFD *gf);
376 typedef void sys_writefn(CPUState *cs, GuestFD *gf,
377 target_ulong buf, uint32_t len);
378 typedef void sys_readfn(CPUState *cs, GuestFD *gf,
379 target_ulong buf, uint32_t len);
380 typedef void sys_isattyfn(CPUState *cs, GuestFD *gf);
381 typedef void sys_seekfn(CPUState *cs, GuestFD *gf, target_ulong offset);
382 typedef void sys_flenfn(CPUState *cs, GuestFD *gf);
384 static void host_closefn(CPUState *cs, GuestFD *gf)
386 int ret;
388 * Only close the underlying host fd if it's one we opened on behalf
389 * of the guest in SYS_OPEN.
391 if (gf->hostfd == STDIN_FILENO ||
392 gf->hostfd == STDOUT_FILENO ||
393 gf->hostfd == STDERR_FILENO) {
394 ret = 0;
395 } else {
396 ret = close(gf->hostfd);
398 common_semi_cb(cs, ret, ret ? errno : 0);
401 static void host_writefn(CPUState *cs, GuestFD *gf,
402 target_ulong buf, uint32_t len)
404 CPUArchState *env = cs->env_ptr;
405 uint32_t ret = 0;
406 char *s = lock_user(VERIFY_READ, buf, len, 1);
407 (void) env; /* Used in arm softmmu lock_user implicitly */
408 if (s) {
409 ret = write(gf->hostfd, s, len);
410 unlock_user(s, buf, 0);
411 if (ret == (uint32_t)-1) {
412 ret = 0;
415 /* Return bytes not written, on error as well. */
416 common_semi_cb(cs, len - ret, 0);
419 static void host_readfn(CPUState *cs, GuestFD *gf,
420 target_ulong buf, uint32_t len)
422 CPUArchState *env = cs->env_ptr;
423 uint32_t ret = 0;
424 char *s = lock_user(VERIFY_WRITE, buf, len, 0);
425 (void) env; /* Used in arm softmmu lock_user implicitly */
426 if (s) {
427 do {
428 ret = read(gf->hostfd, s, len);
429 } while (ret == -1 && errno == EINTR);
430 unlock_user(s, buf, len);
431 if (ret == (uint32_t)-1) {
432 ret = 0;
435 /* Return bytes not read, on error as well. */
436 common_semi_cb(cs, len - ret, 0);
439 static void host_isattyfn(CPUState *cs, GuestFD *gf)
441 common_semi_cb(cs, isatty(gf->hostfd), 0);
444 static void host_seekfn(CPUState *cs, GuestFD *gf, target_ulong offset)
446 off_t ret = lseek(gf->hostfd, offset, SEEK_SET);
447 common_semi_cb(cs, ret, ret == -1 ? errno : 0);
450 static void host_flenfn(CPUState *cs, GuestFD *gf)
452 struct stat buf;
454 if (fstat(gf->hostfd, &buf)) {
455 common_semi_cb(cs, -1, errno);
456 } else {
457 common_semi_cb(cs, buf.st_size, 0);
461 static void gdb_closefn(CPUState *cs, GuestFD *gf)
463 gdb_do_syscall(common_semi_cb, "close,%x", gf->hostfd);
466 static void gdb_writefn(CPUState *cs, GuestFD *gf,
467 target_ulong buf, uint32_t len)
469 common_semi_syscall_len = len;
470 gdb_do_syscall(common_semi_cb, "write,%x,%x,%x", gf->hostfd, buf, len);
473 static void gdb_readfn(CPUState *cs, GuestFD *gf,
474 target_ulong buf, uint32_t len)
476 common_semi_syscall_len = len;
477 gdb_do_syscall(common_semi_cb, "read,%x,%x,%x", gf->hostfd, buf, len);
480 static void gdb_isattyfn(CPUState *cs, GuestFD *gf)
482 gdb_do_syscall(common_semi_cb, "isatty,%x", gf->hostfd);
485 static void gdb_seekfn(CPUState *cs, GuestFD *gf, target_ulong offset)
487 gdb_do_syscall(common_semi_cb, "lseek,%x,%x,0", gf->hostfd, offset);
490 static void gdb_flenfn(CPUState *cs, GuestFD *gf)
492 gdb_do_syscall(common_semi_flen_cb, "fstat,%x,%x",
493 gf->hostfd, common_semi_flen_buf(cs));
496 #define SHFB_MAGIC_0 0x53
497 #define SHFB_MAGIC_1 0x48
498 #define SHFB_MAGIC_2 0x46
499 #define SHFB_MAGIC_3 0x42
501 /* Feature bits reportable in feature byte 0 */
502 #define SH_EXT_EXIT_EXTENDED (1 << 0)
503 #define SH_EXT_STDOUT_STDERR (1 << 1)
505 static const uint8_t featurefile_data[] = {
506 SHFB_MAGIC_0,
507 SHFB_MAGIC_1,
508 SHFB_MAGIC_2,
509 SHFB_MAGIC_3,
510 SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */
513 static void staticfile_closefn(CPUState *cs, GuestFD *gf)
515 /* Nothing to do */
516 common_semi_cb(cs, 0, 0);
519 static void staticfile_writefn(CPUState *cs, GuestFD *gf,
520 target_ulong buf, uint32_t len)
522 /* This fd can never be open for writing */
523 common_semi_cb(cs, -1, EBADF);
526 static void staticfile_readfn(CPUState *cs, GuestFD *gf,
527 target_ulong buf, uint32_t len)
529 CPUArchState *env = cs->env_ptr;
530 uint32_t i = 0;
531 char *s;
533 (void) env; /* Used in arm softmmu lock_user implicitly */
534 s = lock_user(VERIFY_WRITE, buf, len, 0);
535 if (s) {
536 for (i = 0; i < len; i++) {
537 if (gf->staticfile.off >= gf->staticfile.len) {
538 break;
540 s[i] = gf->staticfile.data[gf->staticfile.off];
541 gf->staticfile.off++;
543 unlock_user(s, buf, len);
546 /* Return number of bytes not read */
547 common_semi_cb(cs, len - i, 0);
550 static void staticfile_isattyfn(CPUState *cs, GuestFD *gf)
552 common_semi_cb(cs, 0, 0);
555 static void staticfile_seekfn(CPUState *cs, GuestFD *gf, target_ulong offset)
557 gf->staticfile.off = offset;
558 common_semi_cb(cs, 0, 0);
561 static void staticfile_flenfn(CPUState *cs, GuestFD *gf)
563 common_semi_cb(cs, gf->staticfile.len, 0);
566 typedef struct GuestFDFunctions {
567 sys_closefn *closefn;
568 sys_writefn *writefn;
569 sys_readfn *readfn;
570 sys_isattyfn *isattyfn;
571 sys_seekfn *seekfn;
572 sys_flenfn *flenfn;
573 } GuestFDFunctions;
575 static const GuestFDFunctions guestfd_fns[] = {
576 [GuestFDHost] = {
577 .closefn = host_closefn,
578 .writefn = host_writefn,
579 .readfn = host_readfn,
580 .isattyfn = host_isattyfn,
581 .seekfn = host_seekfn,
582 .flenfn = host_flenfn,
584 [GuestFDGDB] = {
585 .closefn = gdb_closefn,
586 .writefn = gdb_writefn,
587 .readfn = gdb_readfn,
588 .isattyfn = gdb_isattyfn,
589 .seekfn = gdb_seekfn,
590 .flenfn = gdb_flenfn,
592 [GuestFDStatic] = {
593 .closefn = staticfile_closefn,
594 .writefn = staticfile_writefn,
595 .readfn = staticfile_readfn,
596 .isattyfn = staticfile_isattyfn,
597 .seekfn = staticfile_seekfn,
598 .flenfn = staticfile_flenfn,
603 * Read the input value from the argument block; fail the semihosting
604 * call if the memory read fails. Eventually we could use a generic
605 * CPUState helper function here.
608 #define GET_ARG(n) do { \
609 if (is_64bit_semihosting(env)) { \
610 if (get_user_u64(arg ## n, args + (n) * 8)) { \
611 goto do_fault; \
613 } else { \
614 if (get_user_u32(arg ## n, args + (n) * 4)) { \
615 goto do_fault; \
618 } while (0)
620 #define SET_ARG(n, val) \
621 (is_64bit_semihosting(env) ? \
622 put_user_u64(val, args + (n) * 8) : \
623 put_user_u32(val, args + (n) * 4))
627 * Do a semihosting call.
629 * The specification always says that the "return register" either
630 * returns a specific value or is corrupted, so we don't need to
631 * report to our caller whether we are returning a value or trying to
632 * leave the register unchanged. We use 0xdeadbeef as the return value
633 * when there isn't a defined return value for the call.
635 void do_common_semihosting(CPUState *cs)
637 CPUArchState *env = cs->env_ptr;
638 target_ulong args;
639 target_ulong arg0, arg1, arg2, arg3;
640 target_ulong ul_ret;
641 char * s;
642 int nr;
643 uint32_t ret;
644 uint32_t len;
645 GuestFD *gf;
646 int64_t elapsed;
648 (void) env; /* Used implicitly by arm lock_user macro */
649 nr = common_semi_arg(cs, 0) & 0xffffffffU;
650 args = common_semi_arg(cs, 1);
652 switch (nr) {
653 case TARGET_SYS_OPEN:
655 int ret, err = 0;
656 int hostfd;
658 GET_ARG(0);
659 GET_ARG(1);
660 GET_ARG(2);
661 s = lock_user_string(arg0);
662 if (!s) {
663 goto do_fault;
665 if (arg1 >= 12) {
666 unlock_user(s, arg0, 0);
667 common_semi_cb(cs, -1, EINVAL);
668 break;
671 if (strcmp(s, ":tt") == 0) {
673 * We implement SH_EXT_STDOUT_STDERR, so:
674 * open for read == stdin
675 * open for write == stdout
676 * open for append == stderr
678 if (arg1 < 4) {
679 hostfd = STDIN_FILENO;
680 } else if (arg1 < 8) {
681 hostfd = STDOUT_FILENO;
682 } else {
683 hostfd = STDERR_FILENO;
685 ret = alloc_guestfd();
686 associate_guestfd(ret, hostfd);
687 } else if (strcmp(s, ":semihosting-features") == 0) {
688 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
689 if (arg1 != 0 && arg1 != 1) {
690 ret = -1;
691 err = EACCES;
692 } else {
693 ret = alloc_guestfd();
694 staticfile_guestfd(ret, featurefile_data,
695 sizeof(featurefile_data));
697 } else if (use_gdb_syscalls()) {
698 unlock_user(s, arg0, 0);
699 common_semi_open_guestfd = alloc_guestfd();
700 gdb_do_syscall(common_semi_open_cb,
701 "open,%s,%x,1a4", arg0, (int)arg2 + 1,
702 gdb_open_modeflags[arg1]);
703 break;
704 } else {
705 hostfd = open(s, open_modeflags[arg1], 0644);
706 if (hostfd < 0) {
707 ret = -1;
708 err = errno;
709 } else {
710 ret = alloc_guestfd();
711 associate_guestfd(ret, hostfd);
714 unlock_user(s, arg0, 0);
715 common_semi_cb(cs, ret, err);
716 break;
719 case TARGET_SYS_CLOSE:
720 GET_ARG(0);
722 gf = get_guestfd(arg0);
723 if (!gf) {
724 goto do_badf;
726 guestfd_fns[gf->type].closefn(cs, gf);
727 dealloc_guestfd(arg0);
728 break;
730 case TARGET_SYS_WRITEC:
731 qemu_semihosting_console_outc(cs->env_ptr, args);
732 common_semi_set_ret(cs, 0xdeadbeef);
733 break;
735 case TARGET_SYS_WRITE0:
736 ret = qemu_semihosting_console_outs(cs->env_ptr, args);
737 common_semi_set_ret(cs, ret);
738 break;
740 case TARGET_SYS_WRITE:
741 GET_ARG(0);
742 GET_ARG(1);
743 GET_ARG(2);
744 len = arg2;
746 gf = get_guestfd(arg0);
747 if (!gf) {
748 goto do_badf;
750 guestfd_fns[gf->type].writefn(cs, gf, arg1, len);
751 break;
753 case TARGET_SYS_READ:
754 GET_ARG(0);
755 GET_ARG(1);
756 GET_ARG(2);
757 len = arg2;
759 gf = get_guestfd(arg0);
760 if (!gf) {
761 goto do_badf;
763 guestfd_fns[gf->type].readfn(cs, gf, arg1, len);
764 break;
766 case TARGET_SYS_READC:
767 ret = qemu_semihosting_console_inc(cs->env_ptr);
768 common_semi_set_ret(cs, ret);
769 break;
771 case TARGET_SYS_ISERROR:
772 GET_ARG(0);
773 common_semi_set_ret(cs, (target_long)arg0 < 0);
774 break;
776 case TARGET_SYS_ISTTY:
777 GET_ARG(0);
779 gf = get_guestfd(arg0);
780 if (!gf) {
781 goto do_badf;
783 guestfd_fns[gf->type].isattyfn(cs, gf);
784 break;
786 case TARGET_SYS_SEEK:
787 GET_ARG(0);
788 GET_ARG(1);
790 gf = get_guestfd(arg0);
791 if (!gf) {
792 goto do_badf;
794 guestfd_fns[gf->type].seekfn(cs, gf, arg1);
795 break;
797 case TARGET_SYS_FLEN:
798 GET_ARG(0);
800 gf = get_guestfd(arg0);
801 if (!gf) {
802 goto do_badf;
804 guestfd_fns[gf->type].flenfn(cs, gf);
805 break;
807 case TARGET_SYS_TMPNAM:
809 int len;
810 char *p;
812 GET_ARG(0);
813 GET_ARG(1);
814 GET_ARG(2);
815 len = asprintf(&s, "/tmp/qemu-%x%02x", getpid(), (int)arg1 & 0xff);
816 /* Make sure there's enough space in the buffer */
817 if (len < 0 || len >= arg2) {
818 common_semi_set_ret(cs, -1);
819 break;
821 p = lock_user(VERIFY_WRITE, arg0, len, 0);
822 if (!p) {
823 goto do_fault;
825 memcpy(p, s, len + 1);
826 unlock_user(p, arg0, len);
827 free(s);
828 common_semi_set_ret(cs, 0);
829 break;
832 case TARGET_SYS_REMOVE:
833 GET_ARG(0);
834 GET_ARG(1);
835 if (use_gdb_syscalls()) {
836 gdb_do_syscall(common_semi_cb, "unlink,%s",
837 arg0, (int)arg1 + 1);
838 break;
840 s = lock_user_string(arg0);
841 if (!s) {
842 goto do_fault;
844 ret = remove(s);
845 unlock_user(s, arg0, 0);
846 common_semi_cb(cs, ret, ret ? errno : 0);
847 break;
849 case TARGET_SYS_RENAME:
850 GET_ARG(0);
851 GET_ARG(1);
852 GET_ARG(2);
853 GET_ARG(3);
854 if (use_gdb_syscalls()) {
855 gdb_do_syscall(common_semi_cb, "rename,%s,%s",
856 arg0, (int)arg1 + 1, arg2, (int)arg3 + 1);
857 } else {
858 char *s2;
860 s = lock_user_string(arg0);
861 if (!s) {
862 goto do_fault;
864 s2 = lock_user_string(arg2);
865 if (!s2) {
866 unlock_user(s, arg0, 0);
867 goto do_fault;
869 ret = rename(s, s2);
870 unlock_user(s2, arg2, 0);
871 unlock_user(s, arg0, 0);
872 common_semi_cb(cs, ret, ret ? errno : 0);
874 break;
876 case TARGET_SYS_CLOCK:
877 common_semi_set_ret(cs, clock() / (CLOCKS_PER_SEC / 100));
878 break;
880 case TARGET_SYS_TIME:
881 ul_ret = time(NULL);
882 common_semi_cb(cs, ul_ret, ul_ret == -1 ? errno : 0);
883 break;
885 case TARGET_SYS_SYSTEM:
886 GET_ARG(0);
887 GET_ARG(1);
888 if (use_gdb_syscalls()) {
889 gdb_do_syscall(common_semi_cb, "system,%s", arg0, (int)arg1 + 1);
890 break;
892 s = lock_user_string(arg0);
893 if (!s) {
894 goto do_fault;
896 ret = system(s);
897 unlock_user(s, arg0, 0);
898 common_semi_cb(cs, ret, ret == -1 ? errno : 0);
899 break;
901 case TARGET_SYS_ERRNO:
902 common_semi_set_ret(cs, get_swi_errno(cs));
903 break;
905 case TARGET_SYS_GET_CMDLINE:
907 /* Build a command-line from the original argv.
909 * The inputs are:
910 * * arg0, pointer to a buffer of at least the size
911 * specified in arg1.
912 * * arg1, size of the buffer pointed to by arg0 in
913 * bytes.
915 * The outputs are:
916 * * arg0, pointer to null-terminated string of the
917 * command line.
918 * * arg1, length of the string pointed to by arg0.
921 char *output_buffer;
922 size_t input_size;
923 size_t output_size;
924 int status = 0;
925 #if !defined(CONFIG_USER_ONLY)
926 const char *cmdline;
927 #else
928 TaskState *ts = cs->opaque;
929 #endif
930 GET_ARG(0);
931 GET_ARG(1);
932 input_size = arg1;
933 /* Compute the size of the output string. */
934 #if !defined(CONFIG_USER_ONLY)
935 cmdline = semihosting_get_cmdline();
936 if (cmdline == NULL) {
937 cmdline = ""; /* Default to an empty line. */
939 output_size = strlen(cmdline) + 1; /* Count terminating 0. */
940 #else
941 unsigned int i;
943 output_size = ts->info->env_strings - ts->info->arg_strings;
944 if (!output_size) {
946 * We special-case the "empty command line" case (argc==0).
947 * Just provide the terminating 0.
949 output_size = 1;
951 #endif
953 if (output_size > input_size) {
954 /* Not enough space to store command-line arguments. */
955 common_semi_cb(cs, -1, E2BIG);
956 break;
959 /* Adjust the command-line length. */
960 if (SET_ARG(1, output_size - 1)) {
961 /* Couldn't write back to argument block */
962 goto do_fault;
965 /* Lock the buffer on the ARM side. */
966 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
967 if (!output_buffer) {
968 goto do_fault;
971 /* Copy the command-line arguments. */
972 #if !defined(CONFIG_USER_ONLY)
973 pstrcpy(output_buffer, output_size, cmdline);
974 #else
975 if (output_size == 1) {
976 /* Empty command-line. */
977 output_buffer[0] = '\0';
978 goto out;
981 if (copy_from_user(output_buffer, ts->info->arg_strings,
982 output_size)) {
983 unlock_user(output_buffer, arg0, 0);
984 goto do_fault;
987 /* Separate arguments by white spaces. */
988 for (i = 0; i < output_size - 1; i++) {
989 if (output_buffer[i] == 0) {
990 output_buffer[i] = ' ';
993 out:
994 #endif
995 /* Unlock the buffer on the ARM side. */
996 unlock_user(output_buffer, arg0, output_size);
997 common_semi_cb(cs, status, 0);
999 break;
1001 case TARGET_SYS_HEAPINFO:
1003 target_ulong retvals[4];
1004 int i;
1005 #ifdef CONFIG_USER_ONLY
1006 TaskState *ts = cs->opaque;
1007 target_ulong limit;
1008 #else
1009 LayoutInfo info = common_semi_find_bases(cs);
1010 #endif
1012 GET_ARG(0);
1014 #ifdef CONFIG_USER_ONLY
1016 * Some C libraries assume the heap immediately follows .bss, so
1017 * allocate it using sbrk.
1019 if (!ts->heap_limit) {
1020 abi_ulong ret;
1022 ts->heap_base = do_brk(0);
1023 limit = ts->heap_base + COMMON_SEMI_HEAP_SIZE;
1024 /* Try a big heap, and reduce the size if that fails. */
1025 for (;;) {
1026 ret = do_brk(limit);
1027 if (ret >= limit) {
1028 break;
1030 limit = (ts->heap_base >> 1) + (limit >> 1);
1032 ts->heap_limit = limit;
1035 retvals[0] = ts->heap_base;
1036 retvals[1] = ts->heap_limit;
1037 retvals[2] = ts->stack_base;
1038 retvals[3] = 0; /* Stack limit. */
1039 #else
1040 retvals[0] = info.heapbase; /* Heap Base */
1041 retvals[1] = info.heaplimit; /* Heap Limit */
1042 retvals[2] = info.heaplimit; /* Stack base */
1043 retvals[3] = info.heapbase; /* Stack limit. */
1044 #endif
1046 for (i = 0; i < ARRAY_SIZE(retvals); i++) {
1047 bool fail;
1049 if (is_64bit_semihosting(env)) {
1050 fail = put_user_u64(retvals[i], arg0 + i * 8);
1051 } else {
1052 fail = put_user_u32(retvals[i], arg0 + i * 4);
1055 if (fail) {
1056 /* Couldn't write back to argument block */
1057 goto do_fault;
1060 common_semi_set_ret(cs, 0);
1062 break;
1064 case TARGET_SYS_EXIT:
1065 case TARGET_SYS_EXIT_EXTENDED:
1066 if (common_semi_sys_exit_extended(cs, nr)) {
1068 * The A64 version of SYS_EXIT takes a parameter block,
1069 * so the application-exit type can return a subcode which
1070 * is the exit status code from the application.
1071 * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
1072 * which allows A32/T32 guests to also provide a status code.
1074 GET_ARG(0);
1075 GET_ARG(1);
1077 if (arg0 == ADP_Stopped_ApplicationExit) {
1078 ret = arg1;
1079 } else {
1080 ret = 1;
1082 } else {
1084 * The A32/T32 version of SYS_EXIT specifies only
1085 * Stopped_ApplicationExit as normal exit, but does not
1086 * allow the guest to specify the exit status code.
1087 * Everything else is considered an error.
1089 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
1091 gdb_exit(ret);
1092 exit(ret);
1094 case TARGET_SYS_ELAPSED:
1095 elapsed = get_clock() - clock_start;
1096 if (sizeof(target_ulong) == 8) {
1097 SET_ARG(0, elapsed);
1098 } else {
1099 SET_ARG(0, (uint32_t) elapsed);
1100 SET_ARG(1, (uint32_t) (elapsed >> 32));
1102 common_semi_set_ret(cs, 0);
1103 break;
1105 case TARGET_SYS_TICKFREQ:
1106 /* qemu always uses nsec */
1107 common_semi_set_ret(cs, 1000000000);
1108 break;
1110 case TARGET_SYS_SYNCCACHE:
1112 * Clean the D-cache and invalidate the I-cache for the specified
1113 * virtual address range. This is a nop for us since we don't
1114 * implement caches. This is only present on A64.
1116 if (common_semi_has_synccache(env)) {
1117 common_semi_set_ret(cs, 0);
1118 break;
1120 /* fall through */
1121 default:
1122 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
1123 cpu_dump_state(cs, stderr, 0);
1124 abort();
1126 do_badf:
1127 common_semi_cb(cs, -1, EBADF);
1128 break;
1129 do_fault:
1130 common_semi_cb(cs, -1, EFAULT);
1131 break;