Lines Matching +full:cs +full:- +full:0
3 * semihosting syscalls design. This includes Arm and RISC-V processors
10 * Adapted for systems other than ARM, including RISC-V, by Keith Packard
27 * https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst
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
40 #include "semihosting/common-semi.h"
54 #define TARGET_SYS_OPEN 0x01
55 #define TARGET_SYS_CLOSE 0x02
56 #define TARGET_SYS_WRITEC 0x03
57 #define TARGET_SYS_WRITE0 0x04
58 #define TARGET_SYS_WRITE 0x05
59 #define TARGET_SYS_READ 0x06
60 #define TARGET_SYS_READC 0x07
61 #define TARGET_SYS_ISERROR 0x08
62 #define TARGET_SYS_ISTTY 0x09
63 #define TARGET_SYS_SEEK 0x0a
64 #define TARGET_SYS_FLEN 0x0c
65 #define TARGET_SYS_TMPNAM 0x0d
66 #define TARGET_SYS_REMOVE 0x0e
67 #define TARGET_SYS_RENAME 0x0f
68 #define TARGET_SYS_CLOCK 0x10
69 #define TARGET_SYS_TIME 0x11
70 #define TARGET_SYS_SYSTEM 0x12
71 #define TARGET_SYS_ERRNO 0x13
72 #define TARGET_SYS_GET_CMDLINE 0x15
73 #define TARGET_SYS_HEAPINFO 0x16
74 #define TARGET_SYS_EXIT 0x18
75 #define TARGET_SYS_SYNCCACHE 0x19
76 #define TARGET_SYS_EXIT_EXTENDED 0x20
77 #define TARGET_SYS_ELAPSED 0x30
78 #define TARGET_SYS_TICKFREQ 0x31
80 /* ADP_Stopped_ApplicationExit is used for exit(0),
82 #define ADP_Stopped_ApplicationExit (0x20026)
85 #define O_BINARY 0
130 if (!mr->ram || mr->readonly) { in find_ram_cb()
134 if (size > info->ramsize) { in find_ram_cb()
135 info->rambase = int128_get64(start); in find_ram_cb()
136 info->ramsize = size; in find_ram_cb()
143 static LayoutInfo common_semi_find_bases(CPUState *cs) in common_semi_find_bases() argument
146 LayoutInfo info = { 0, 0, 0, 0 }; in common_semi_find_bases()
150 fv = address_space_to_flatview(cs->as); in common_semi_find_bases()
158 if (info.rambase || info.ramsize > 0) { in common_semi_find_bases()
170 #include "common-semi-target.h"
180 * error indication (0 on success, non-0 for error) which the caller
194 } while (0)
203 * The semihosting API has no concept of its errno being thread-safe,
205 * real-hardware set of debug functionality. For QEMU, we make the
206 * errno be per-thread in linux-user mode; in system-mode it is a simple
215 static inline uint32_t get_swi_errno(CPUState *cs) in get_swi_errno() argument
218 TaskState *ts = get_task_state(cs); in get_swi_errno()
220 return ts->swi_errno; in get_swi_errno()
226 static void common_semi_cb(CPUState *cs, uint64_t ret, int err) in common_semi_cb() argument
230 TaskState *ts = get_task_state(cs); in common_semi_cb()
231 ts->swi_errno = err; in common_semi_cb()
236 common_semi_set_ret(cs, ret); in common_semi_cb()
240 * Use 0xdeadbeef as the return value when there isn't a defined
243 static void common_semi_dead_cb(CPUState *cs, uint64_t ret, int err) in common_semi_dead_cb() argument
245 common_semi_set_ret(cs, 0xdeadbeef); in common_semi_dead_cb()
252 static void common_semi_rw_cb(CPUState *cs, uint64_t ret, int err) in common_semi_rw_cb() argument
255 CPUArchState *env G_GNUC_UNUSED = cpu_env(cs); in common_semi_rw_cb()
256 target_ulong args = common_semi_arg(cs, 1); in common_semi_rw_cb()
262 ret = 0; /* error: no bytes transmitted */ in common_semi_rw_cb()
264 common_semi_set_ret(cs, arg2 - ret); in common_semi_rw_cb()
271 static void common_semi_istty_cb(CPUState *cs, uint64_t ret, int err) in common_semi_istty_cb() argument
274 ret = (err == ENOTTY ? 0 : -1); in common_semi_istty_cb()
276 common_semi_cb(cs, ret, err); in common_semi_istty_cb()
280 * SYS_SEEK returns 0 on success, not the resulting offset.
282 static void common_semi_seek_cb(CPUState *cs, uint64_t ret, int err) in common_semi_seek_cb() argument
285 ret = 0; in common_semi_seek_cb()
287 common_semi_cb(cs, ret, err); in common_semi_seek_cb()
293 * is defined by GDB's remote protocol and is not target-specific.)
296 static target_ulong common_semi_flen_buf(CPUState *cs) in common_semi_flen_buf() argument
298 target_ulong sp = common_semi_stack_bottom(cs); in common_semi_flen_buf()
299 return sp - 64; in common_semi_flen_buf()
303 common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err) in common_semi_flen_fstat_cb() argument
306 /* The size is always stored in big-endian order, extract the value. */ in common_semi_flen_fstat_cb()
308 if (cpu_memory_rw_debug(cs, common_semi_flen_buf(cs) + in common_semi_flen_fstat_cb()
310 &size, 8, 0)) { in common_semi_flen_fstat_cb()
311 ret = -1, err = EFAULT; in common_semi_flen_fstat_cb()
315 ret = -1, err = EOVERFLOW; in common_semi_flen_fstat_cb()
319 common_semi_cb(cs, ret, err); in common_semi_flen_fstat_cb()
323 common_semi_readc_cb(CPUState *cs, uint64_t ret, int err) in common_semi_readc_cb() argument
326 CPUArchState *env G_GNUC_UNUSED = cpu_env(cs); in common_semi_readc_cb()
329 if (get_user_u8(ch, common_semi_stack_bottom(cs) - 1)) { in common_semi_readc_cb()
330 ret = -1, err = EFAULT; in common_semi_readc_cb()
335 common_semi_cb(cs, ret, err); in common_semi_readc_cb()
338 #define SHFB_MAGIC_0 0x53
339 #define SHFB_MAGIC_1 0x48
340 #define SHFB_MAGIC_2 0x46
341 #define SHFB_MAGIC_3 0x42
343 /* Feature bits reportable in feature byte 0 */
344 #define SH_EXT_EXIT_EXTENDED (1 << 0)
352 SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */
363 void do_common_semihosting(CPUState *cs) in do_common_semihosting() argument
365 CPUArchState *env = cpu_env(cs); in do_common_semihosting()
373 nr = common_semi_arg(cs, 0) & 0xffffffffU; in do_common_semihosting()
374 args = common_semi_arg(cs, 1); in do_common_semihosting()
379 int ret, err = 0; in do_common_semihosting()
382 GET_ARG(0); in do_common_semihosting()
390 unlock_user(s, arg0, 0); in do_common_semihosting()
391 common_semi_cb(cs, -1, EINVAL); in do_common_semihosting()
395 if (strcmp(s, ":tt") == 0) { in do_common_semihosting()
411 } else if (strcmp(s, ":semihosting-features") == 0) { in do_common_semihosting()
412 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */ in do_common_semihosting()
413 if (arg1 != 0 && arg1 != 1) { in do_common_semihosting()
414 ret = -1; in do_common_semihosting()
422 unlock_user(s, arg0, 0); in do_common_semihosting()
423 semihost_sys_open(cs, common_semi_cb, arg0, arg2 + 1, in do_common_semihosting()
427 unlock_user(s, arg0, 0); in do_common_semihosting()
428 common_semi_cb(cs, ret, err); in do_common_semihosting()
433 GET_ARG(0); in do_common_semihosting()
434 semihost_sys_close(cs, common_semi_cb, arg0); in do_common_semihosting()
440 * which means this is wrong for a big-endian guest. in do_common_semihosting()
442 semihost_sys_write_gf(cs, common_semi_dead_cb, in do_common_semihosting()
449 if (len < 0) { in do_common_semihosting()
450 common_semi_dead_cb(cs, -1, EFAULT); in do_common_semihosting()
452 semihost_sys_write_gf(cs, common_semi_dead_cb, in do_common_semihosting()
459 GET_ARG(0); in do_common_semihosting()
462 semihost_sys_write(cs, common_semi_rw_cb, arg0, arg1, arg2); in do_common_semihosting()
466 GET_ARG(0); in do_common_semihosting()
469 semihost_sys_read(cs, common_semi_rw_cb, arg0, arg1, arg2); in do_common_semihosting()
473 semihost_sys_read_gf(cs, common_semi_readc_cb, &console_in_gf, in do_common_semihosting()
474 common_semi_stack_bottom(cs) - 1, 1); in do_common_semihosting()
478 GET_ARG(0); in do_common_semihosting()
479 common_semi_set_ret(cs, (target_long)arg0 < 0); in do_common_semihosting()
483 GET_ARG(0); in do_common_semihosting()
484 semihost_sys_isatty(cs, common_semi_istty_cb, arg0); in do_common_semihosting()
488 GET_ARG(0); in do_common_semihosting()
490 semihost_sys_lseek(cs, common_semi_seek_cb, arg0, arg1, GDB_SEEK_SET); in do_common_semihosting()
494 GET_ARG(0); in do_common_semihosting()
495 semihost_sys_flen(cs, common_semi_flen_fstat_cb, common_semi_cb, in do_common_semihosting()
496 arg0, common_semi_flen_buf(cs)); in do_common_semihosting()
504 GET_ARG(0); in do_common_semihosting()
507 len = asprintf(&s, "%s/qemu-%x%02x", g_get_tmp_dir(), in do_common_semihosting()
508 getpid(), (int)arg1 & 0xff); in do_common_semihosting()
509 if (len < 0) { in do_common_semihosting()
510 common_semi_set_ret(cs, -1); in do_common_semihosting()
519 common_semi_set_ret(cs, -1); in do_common_semihosting()
522 p = lock_user(VERIFY_WRITE, arg0, len, 0); in do_common_semihosting()
530 common_semi_set_ret(cs, 0); in do_common_semihosting()
535 GET_ARG(0); in do_common_semihosting()
537 semihost_sys_remove(cs, common_semi_cb, arg0, arg1 + 1); in do_common_semihosting()
541 GET_ARG(0); in do_common_semihosting()
545 semihost_sys_rename(cs, common_semi_cb, arg0, arg1 + 1, arg2, arg3 + 1); in do_common_semihosting()
549 common_semi_set_ret(cs, clock() / (CLOCKS_PER_SEC / 100)); in do_common_semihosting()
554 common_semi_cb(cs, ul_ret, ul_ret == -1 ? errno : 0); in do_common_semihosting()
558 GET_ARG(0); in do_common_semihosting()
560 semihost_sys_system(cs, common_semi_cb, arg0, arg1 + 1); in do_common_semihosting()
564 common_semi_set_ret(cs, get_swi_errno(cs)); in do_common_semihosting()
569 /* Build a command-line from the original argv. in do_common_semihosting()
578 * * arg0, pointer to null-terminated string of the in do_common_semihosting()
586 int status = 0; in do_common_semihosting()
590 TaskState *ts = get_task_state(cs); in do_common_semihosting()
592 GET_ARG(0); in do_common_semihosting()
601 output_size = strlen(cmdline) + 1; /* Count terminating 0. */ in do_common_semihosting()
605 output_size = ts->info->env_strings - ts->info->arg_strings; in do_common_semihosting()
608 * We special-case the "empty command line" case (argc==0). in do_common_semihosting()
609 * Just provide the terminating 0. in do_common_semihosting()
616 /* Not enough space to store command-line arguments. */ in do_common_semihosting()
617 common_semi_cb(cs, -1, E2BIG); in do_common_semihosting()
621 /* Adjust the command-line length. */ in do_common_semihosting()
622 if (SET_ARG(1, output_size - 1)) { in do_common_semihosting()
628 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0); in do_common_semihosting()
633 /* Copy the command-line arguments. */ in do_common_semihosting()
638 /* Empty command-line. */ in do_common_semihosting()
639 output_buffer[0] = '\0'; in do_common_semihosting()
643 if (copy_from_user(output_buffer, ts->info->arg_strings, in do_common_semihosting()
645 unlock_user(output_buffer, arg0, 0); in do_common_semihosting()
650 for (i = 0; i < output_size - 1; i++) { in do_common_semihosting()
651 if (output_buffer[i] == 0) { in do_common_semihosting()
659 common_semi_cb(cs, status, 0); in do_common_semihosting()
668 TaskState *ts = get_task_state(cs); in do_common_semihosting()
671 LayoutInfo info = common_semi_find_bases(cs); in do_common_semihosting()
674 GET_ARG(0); in do_common_semihosting()
681 if (!ts->heap_limit) { in do_common_semihosting()
684 ts->heap_base = do_brk(0); in do_common_semihosting()
685 limit = ts->heap_base + COMMON_SEMI_HEAP_SIZE; in do_common_semihosting()
692 limit = (ts->heap_base >> 1) + (limit >> 1); in do_common_semihosting()
694 ts->heap_limit = limit; in do_common_semihosting()
697 retvals[0] = ts->heap_base; in do_common_semihosting()
698 retvals[1] = ts->heap_limit; in do_common_semihosting()
699 retvals[2] = ts->stack_base; in do_common_semihosting()
700 retvals[3] = 0; /* Stack limit. */ in do_common_semihosting()
702 retvals[0] = info.heapbase; /* Heap Base */ in do_common_semihosting()
708 for (i = 0; i < ARRAY_SIZE(retvals); i++) { in do_common_semihosting()
722 common_semi_set_ret(cs, 0); in do_common_semihosting()
731 if (common_semi_sys_exit_extended(cs, nr)) { in do_common_semihosting()
734 * so the application-exit type can return a subcode which in do_common_semihosting()
736 * SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function in do_common_semihosting()
739 GET_ARG(0); in do_common_semihosting()
754 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1; in do_common_semihosting()
761 elapsed = get_clock() - clock_start; in do_common_semihosting()
763 if (SET_ARG(0, elapsed)) { in do_common_semihosting()
767 if (SET_ARG(0, (uint32_t) elapsed) || in do_common_semihosting()
772 common_semi_set_ret(cs, 0); in do_common_semihosting()
777 common_semi_set_ret(cs, 1000000000); in do_common_semihosting()
782 * Clean the D-cache and invalidate the I-cache for the specified in do_common_semihosting()
787 common_semi_set_ret(cs, 0); in do_common_semihosting()
792 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr); in do_common_semihosting()
793 cpu_dump_state(cs, stderr, 0); in do_common_semihosting()
797 common_semi_cb(cs, -1, EFAULT); in do_common_semihosting()