target/arm/arm-semi: fix SYS_OPEN to return nonzero filehandle
[qemu/ar7.git] / target / arm / arm-semi.c
blob788fe61b51add73e00684bece1eab810c3217934
1 /*
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"
28 #include "cpu.h"
29 #include "hw/semihosting/semihost.h"
30 #include "hw/semihosting/console.h"
31 #include "qemu/log.h"
32 #ifdef CONFIG_USER_ONLY
33 #include "qemu.h"
35 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
36 #else
37 #include "exec/gdbstub.h"
38 #include "qemu/cutils.h"
39 #endif
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)
68 #ifndef O_BINARY
69 #define O_BINARY 0
70 #endif
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] = {
81 GDB_O_RDONLY,
82 GDB_O_RDONLY | GDB_O_BINARY,
83 GDB_O_RDWR,
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] = {
96 O_RDONLY,
97 O_RDONLY | O_BINARY,
98 O_RDWR,
99 O_RDWR | O_BINARY,
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 {
111 GuestFDUnused = 0,
112 GuestFDHost = 1,
113 GuestFDGDB = 2,
114 GuestFDFeatureFile = 3,
115 } GuestFDType;
118 * Guest file descriptors are integer indexes into an array of
119 * these structures (we will dynamically resize as necessary).
121 typedef struct GuestFD {
122 GuestFDType type;
123 union {
124 int hostfd;
125 target_ulong featurefile_offset;
127 } GuestFD;
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)
140 guint i;
142 if (!guestfd_array) {
143 /* New entries zero-initialized, i.e. type GuestFDUnused */
144 guestfd_array = g_array_new(FALSE, TRUE, sizeof(GuestFD));
147 /* SYS_OPEN should return nonzero handle on success. Start guestfd from 1 */
148 for (i = 1; i < guestfd_array->len; i++) {
149 GuestFD *gf = &g_array_index(guestfd_array, GuestFD, i);
151 if (gf->type == GuestFDUnused) {
152 return i;
156 /* All elements already in use: expand the array */
157 g_array_set_size(guestfd_array, i + 1);
158 return i;
162 * Look up the guestfd in the data structure; return NULL
163 * for out of bounds, but don't check whether the slot is unused.
164 * This is used internally by the other guestfd functions.
166 static GuestFD *do_get_guestfd(int guestfd)
168 if (!guestfd_array) {
169 return NULL;
172 if (guestfd <= 0 || guestfd >= guestfd_array->len) {
173 return NULL;
176 return &g_array_index(guestfd_array, GuestFD, guestfd);
180 * Associate the specified guest fd (which must have been
181 * allocated via alloc_fd() and not previously used) with
182 * the specified host/gdb fd.
184 static void associate_guestfd(int guestfd, int hostfd)
186 GuestFD *gf = do_get_guestfd(guestfd);
188 assert(gf);
189 gf->type = use_gdb_syscalls() ? GuestFDGDB : GuestFDHost;
190 gf->hostfd = hostfd;
194 * Deallocate the specified guest file descriptor. This doesn't
195 * close the host fd, it merely undoes the work of alloc_fd().
197 static void dealloc_guestfd(int guestfd)
199 GuestFD *gf = do_get_guestfd(guestfd);
201 assert(gf);
202 gf->type = GuestFDUnused;
206 * Given a guest file descriptor, get the associated struct.
207 * If the fd is not valid, return NULL. This is the function
208 * used by the various semihosting calls to validate a handle
209 * from the guest.
210 * Note: calling alloc_guestfd() or dealloc_guestfd() will
211 * invalidate any GuestFD* obtained by calling this function.
213 static GuestFD *get_guestfd(int guestfd)
215 GuestFD *gf = do_get_guestfd(guestfd);
217 if (!gf || gf->type == GuestFDUnused) {
218 return NULL;
220 return gf;
224 * The semihosting API has no concept of its errno being thread-safe,
225 * as the API design predates SMP CPUs and was intended as a simple
226 * real-hardware set of debug functionality. For QEMU, we make the
227 * errno be per-thread in linux-user mode; in softmmu it is a simple
228 * global, and we assume that the guest takes care of avoiding any races.
230 #ifndef CONFIG_USER_ONLY
231 static target_ulong syscall_err;
233 #include "exec/softmmu-semi.h"
234 #endif
236 static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code)
238 if (code == (uint32_t)-1) {
239 #ifdef CONFIG_USER_ONLY
240 CPUState *cs = env_cpu(env);
241 TaskState *ts = cs->opaque;
243 ts->swi_errno = errno;
244 #else
245 syscall_err = errno;
246 #endif
248 return code;
251 static inline uint32_t get_swi_errno(CPUARMState *env)
253 #ifdef CONFIG_USER_ONLY
254 CPUState *cs = env_cpu(env);
255 TaskState *ts = cs->opaque;
257 return ts->swi_errno;
258 #else
259 return syscall_err;
260 #endif
263 static target_ulong arm_semi_syscall_len;
265 static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
267 ARMCPU *cpu = ARM_CPU(cs);
268 CPUARMState *env = &cpu->env;
269 target_ulong reg0 = is_a64(env) ? env->xregs[0] : env->regs[0];
271 if (ret == (target_ulong)-1) {
272 errno = err;
273 set_swi_errno(env, -1);
274 reg0 = ret;
275 } else {
276 /* Fixup syscalls that use nonstardard return conventions. */
277 switch (reg0) {
278 case TARGET_SYS_WRITE:
279 case TARGET_SYS_READ:
280 reg0 = arm_semi_syscall_len - ret;
281 break;
282 case TARGET_SYS_SEEK:
283 reg0 = 0;
284 break;
285 default:
286 reg0 = ret;
287 break;
290 if (is_a64(env)) {
291 env->xregs[0] = reg0;
292 } else {
293 env->regs[0] = reg0;
297 static target_ulong arm_flen_buf(ARMCPU *cpu)
299 /* Return an address in target memory of 64 bytes where the remote
300 * gdb should write its stat struct. (The format of this structure
301 * is defined by GDB's remote protocol and is not target-specific.)
302 * We put this on the guest's stack just below SP.
304 CPUARMState *env = &cpu->env;
305 target_ulong sp;
307 if (is_a64(env)) {
308 sp = env->xregs[31];
309 } else {
310 sp = env->regs[13];
313 return sp - 64;
316 static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
318 ARMCPU *cpu = ARM_CPU(cs);
319 CPUARMState *env = &cpu->env;
320 /* The size is always stored in big-endian order, extract
321 the value. We assume the size always fit in 32 bits. */
322 uint32_t size;
323 cpu_memory_rw_debug(cs, arm_flen_buf(cpu) + 32, (uint8_t *)&size, 4, 0);
324 size = be32_to_cpu(size);
325 if (is_a64(env)) {
326 env->xregs[0] = size;
327 } else {
328 env->regs[0] = size;
330 errno = err;
331 set_swi_errno(env, -1);
334 static int arm_semi_open_guestfd;
336 static void arm_semi_open_cb(CPUState *cs, target_ulong ret, target_ulong err)
338 ARMCPU *cpu = ARM_CPU(cs);
339 CPUARMState *env = &cpu->env;
340 if (ret == (target_ulong)-1) {
341 errno = err;
342 set_swi_errno(env, -1);
343 dealloc_guestfd(arm_semi_open_guestfd);
344 } else {
345 associate_guestfd(arm_semi_open_guestfd, ret);
346 ret = arm_semi_open_guestfd;
349 if (is_a64(env)) {
350 env->xregs[0] = ret;
351 } else {
352 env->regs[0] = ret;
356 static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb,
357 const char *fmt, ...)
359 va_list va;
360 CPUARMState *env = &cpu->env;
362 va_start(va, fmt);
363 gdb_do_syscallv(cb, fmt, va);
364 va_end(va);
367 * FIXME: in softmmu mode, the gdbstub will schedule our callback
368 * to occur, but will not actually call it to complete the syscall
369 * until after this function has returned and we are back in the
370 * CPU main loop. Therefore callers to this function must not
371 * do anything with its return value, because it is not necessarily
372 * the result of the syscall, but could just be the old value of X0.
373 * The only thing safe to do with this is that the callers of
374 * do_arm_semihosting() will write it straight back into X0.
375 * (In linux-user mode, the callback will have happened before
376 * gdb_do_syscallv() returns.)
378 * We should tidy this up so neither this function nor
379 * do_arm_semihosting() return a value, so the mistake of
380 * doing something with the return value is not possible to make.
383 return is_a64(env) ? env->xregs[0] : env->regs[0];
387 * Types for functions implementing various semihosting calls
388 * for specific types of guest file descriptor. These must all
389 * do the work and return the required return value for the guest,
390 * setting the guest errno if appropriate.
392 typedef uint32_t sys_closefn(ARMCPU *cpu, GuestFD *gf);
393 typedef uint32_t sys_writefn(ARMCPU *cpu, GuestFD *gf,
394 target_ulong buf, uint32_t len);
395 typedef uint32_t sys_readfn(ARMCPU *cpu, GuestFD *gf,
396 target_ulong buf, uint32_t len);
397 typedef uint32_t sys_isattyfn(ARMCPU *cpu, GuestFD *gf);
398 typedef uint32_t sys_seekfn(ARMCPU *cpu, GuestFD *gf,
399 target_ulong offset);
400 typedef uint32_t sys_flenfn(ARMCPU *cpu, GuestFD *gf);
402 static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
404 CPUARMState *env = &cpu->env;
406 return set_swi_errno(env, close(gf->hostfd));
409 static uint32_t host_writefn(ARMCPU *cpu, GuestFD *gf,
410 target_ulong buf, uint32_t len)
412 uint32_t ret;
413 CPUARMState *env = &cpu->env;
414 char *s = lock_user(VERIFY_READ, buf, len, 1);
415 if (!s) {
416 /* Return bytes not written on error */
417 return len;
419 ret = set_swi_errno(env, write(gf->hostfd, s, len));
420 unlock_user(s, buf, 0);
421 if (ret == (uint32_t)-1) {
422 ret = 0;
424 /* Return bytes not written */
425 return len - ret;
428 static uint32_t host_readfn(ARMCPU *cpu, GuestFD *gf,
429 target_ulong buf, uint32_t len)
431 uint32_t ret;
432 CPUARMState *env = &cpu->env;
433 char *s = lock_user(VERIFY_WRITE, buf, len, 0);
434 if (!s) {
435 /* return bytes not read */
436 return len;
438 do {
439 ret = set_swi_errno(env, read(gf->hostfd, s, len));
440 } while (ret == -1 && errno == EINTR);
441 unlock_user(s, buf, len);
442 if (ret == (uint32_t)-1) {
443 ret = 0;
445 /* Return bytes not read */
446 return len - ret;
449 static uint32_t host_isattyfn(ARMCPU *cpu, GuestFD *gf)
451 return isatty(gf->hostfd);
454 static uint32_t host_seekfn(ARMCPU *cpu, GuestFD *gf, target_ulong offset)
456 CPUARMState *env = &cpu->env;
457 uint32_t ret = set_swi_errno(env, lseek(gf->hostfd, offset, SEEK_SET));
458 if (ret == (uint32_t)-1) {
459 return -1;
461 return 0;
464 static uint32_t host_flenfn(ARMCPU *cpu, GuestFD *gf)
466 CPUARMState *env = &cpu->env;
467 struct stat buf;
468 uint32_t ret = set_swi_errno(env, fstat(gf->hostfd, &buf));
469 if (ret == (uint32_t)-1) {
470 return -1;
472 return buf.st_size;
475 static uint32_t gdb_closefn(ARMCPU *cpu, GuestFD *gf)
477 return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", gf->hostfd);
480 static uint32_t gdb_writefn(ARMCPU *cpu, GuestFD *gf,
481 target_ulong buf, uint32_t len)
483 arm_semi_syscall_len = len;
484 return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x",
485 gf->hostfd, buf, len);
488 static uint32_t gdb_readfn(ARMCPU *cpu, GuestFD *gf,
489 target_ulong buf, uint32_t len)
491 arm_semi_syscall_len = len;
492 return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x",
493 gf->hostfd, buf, len);
496 static uint32_t gdb_isattyfn(ARMCPU *cpu, GuestFD *gf)
498 return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", gf->hostfd);
501 static uint32_t gdb_seekfn(ARMCPU *cpu, GuestFD *gf, target_ulong offset)
503 return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0",
504 gf->hostfd, offset);
507 static uint32_t gdb_flenfn(ARMCPU *cpu, GuestFD *gf)
509 return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x",
510 gf->hostfd, arm_flen_buf(cpu));
513 #define SHFB_MAGIC_0 0x53
514 #define SHFB_MAGIC_1 0x48
515 #define SHFB_MAGIC_2 0x46
516 #define SHFB_MAGIC_3 0x42
518 /* Feature bits reportable in feature byte 0 */
519 #define SH_EXT_EXIT_EXTENDED (1 << 0)
520 #define SH_EXT_STDOUT_STDERR (1 << 1)
522 static const uint8_t featurefile_data[] = {
523 SHFB_MAGIC_0,
524 SHFB_MAGIC_1,
525 SHFB_MAGIC_2,
526 SHFB_MAGIC_3,
527 SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */
530 static void init_featurefile_guestfd(int guestfd)
532 GuestFD *gf = do_get_guestfd(guestfd);
534 assert(gf);
535 gf->type = GuestFDFeatureFile;
536 gf->featurefile_offset = 0;
539 static uint32_t featurefile_closefn(ARMCPU *cpu, GuestFD *gf)
541 /* Nothing to do */
542 return 0;
545 static uint32_t featurefile_writefn(ARMCPU *cpu, GuestFD *gf,
546 target_ulong buf, uint32_t len)
548 /* This fd can never be open for writing */
549 CPUARMState *env = &cpu->env;
551 errno = EBADF;
552 return set_swi_errno(env, -1);
555 static uint32_t featurefile_readfn(ARMCPU *cpu, GuestFD *gf,
556 target_ulong buf, uint32_t len)
558 uint32_t i;
559 #ifndef CONFIG_USER_ONLY
560 CPUARMState *env = &cpu->env;
561 #endif
562 char *s;
564 s = lock_user(VERIFY_WRITE, buf, len, 0);
565 if (!s) {
566 return len;
569 for (i = 0; i < len; i++) {
570 if (gf->featurefile_offset >= sizeof(featurefile_data)) {
571 break;
573 s[i] = featurefile_data[gf->featurefile_offset];
574 gf->featurefile_offset++;
577 unlock_user(s, buf, len);
579 /* Return number of bytes not read */
580 return len - i;
583 static uint32_t featurefile_isattyfn(ARMCPU *cpu, GuestFD *gf)
585 return 0;
588 static uint32_t featurefile_seekfn(ARMCPU *cpu, GuestFD *gf,
589 target_ulong offset)
591 gf->featurefile_offset = offset;
592 return 0;
595 static uint32_t featurefile_flenfn(ARMCPU *cpu, GuestFD *gf)
597 return sizeof(featurefile_data);
600 typedef struct GuestFDFunctions {
601 sys_closefn *closefn;
602 sys_writefn *writefn;
603 sys_readfn *readfn;
604 sys_isattyfn *isattyfn;
605 sys_seekfn *seekfn;
606 sys_flenfn *flenfn;
607 } GuestFDFunctions;
609 static const GuestFDFunctions guestfd_fns[] = {
610 [GuestFDHost] = {
611 .closefn = host_closefn,
612 .writefn = host_writefn,
613 .readfn = host_readfn,
614 .isattyfn = host_isattyfn,
615 .seekfn = host_seekfn,
616 .flenfn = host_flenfn,
618 [GuestFDGDB] = {
619 .closefn = gdb_closefn,
620 .writefn = gdb_writefn,
621 .readfn = gdb_readfn,
622 .isattyfn = gdb_isattyfn,
623 .seekfn = gdb_seekfn,
624 .flenfn = gdb_flenfn,
626 [GuestFDFeatureFile] = {
627 .closefn = featurefile_closefn,
628 .writefn = featurefile_writefn,
629 .readfn = featurefile_readfn,
630 .isattyfn = featurefile_isattyfn,
631 .seekfn = featurefile_seekfn,
632 .flenfn = featurefile_flenfn,
636 /* Read the input value from the argument block; fail the semihosting
637 * call if the memory read fails.
639 #define GET_ARG(n) do { \
640 if (is_a64(env)) { \
641 if (get_user_u64(arg ## n, args + (n) * 8)) { \
642 errno = EFAULT; \
643 return set_swi_errno(env, -1); \
645 } else { \
646 if (get_user_u32(arg ## n, args + (n) * 4)) { \
647 errno = EFAULT; \
648 return set_swi_errno(env, -1); \
651 } while (0)
653 #define SET_ARG(n, val) \
654 (is_a64(env) ? \
655 put_user_u64(val, args + (n) * 8) : \
656 put_user_u32(val, args + (n) * 4))
659 * Do a semihosting call.
661 * The specification always says that the "return register" either
662 * returns a specific value or is corrupted, so we don't need to
663 * report to our caller whether we are returning a value or trying to
664 * leave the register unchanged. We use 0xdeadbeef as the return value
665 * when there isn't a defined return value for the call.
667 target_ulong do_arm_semihosting(CPUARMState *env)
669 ARMCPU *cpu = env_archcpu(env);
670 CPUState *cs = env_cpu(env);
671 target_ulong args;
672 target_ulong arg0, arg1, arg2, arg3;
673 char * s;
674 int nr;
675 uint32_t ret;
676 uint32_t len;
677 GuestFD *gf;
679 if (is_a64(env)) {
680 /* Note that the syscall number is in W0, not X0 */
681 nr = env->xregs[0] & 0xffffffffU;
682 args = env->xregs[1];
683 } else {
684 nr = env->regs[0];
685 args = env->regs[1];
688 switch (nr) {
689 case TARGET_SYS_OPEN:
691 int guestfd;
693 GET_ARG(0);
694 GET_ARG(1);
695 GET_ARG(2);
696 s = lock_user_string(arg0);
697 if (!s) {
698 errno = EFAULT;
699 return set_swi_errno(env, -1);
701 if (arg1 >= 12) {
702 unlock_user(s, arg0, 0);
703 errno = EINVAL;
704 return set_swi_errno(env, -1);
707 guestfd = alloc_guestfd();
708 if (guestfd < 0) {
709 unlock_user(s, arg0, 0);
710 errno = EMFILE;
711 return set_swi_errno(env, -1);
714 if (strcmp(s, ":tt") == 0) {
715 int result_fileno;
718 * We implement SH_EXT_STDOUT_STDERR, so:
719 * open for read == stdin
720 * open for write == stdout
721 * open for append == stderr
723 if (arg1 < 4) {
724 result_fileno = STDIN_FILENO;
725 } else if (arg1 < 8) {
726 result_fileno = STDOUT_FILENO;
727 } else {
728 result_fileno = STDERR_FILENO;
730 associate_guestfd(guestfd, result_fileno);
731 unlock_user(s, arg0, 0);
732 return guestfd;
734 if (strcmp(s, ":semihosting-features") == 0) {
735 unlock_user(s, arg0, 0);
736 /* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
737 if (arg1 != 0 && arg1 != 1) {
738 dealloc_guestfd(guestfd);
739 errno = EACCES;
740 return set_swi_errno(env, -1);
742 init_featurefile_guestfd(guestfd);
743 return guestfd;
746 if (use_gdb_syscalls()) {
747 arm_semi_open_guestfd = guestfd;
748 ret = arm_gdb_syscall(cpu, arm_semi_open_cb, "open,%s,%x,1a4", arg0,
749 (int)arg2+1, gdb_open_modeflags[arg1]);
750 } else {
751 ret = set_swi_errno(env, open(s, open_modeflags[arg1], 0644));
752 if (ret == (uint32_t)-1) {
753 dealloc_guestfd(guestfd);
754 } else {
755 associate_guestfd(guestfd, ret);
756 ret = guestfd;
759 unlock_user(s, arg0, 0);
760 return ret;
762 case TARGET_SYS_CLOSE:
763 GET_ARG(0);
765 gf = get_guestfd(arg0);
766 if (!gf) {
767 errno = EBADF;
768 return set_swi_errno(env, -1);
771 ret = guestfd_fns[gf->type].closefn(cpu, gf);
772 dealloc_guestfd(arg0);
773 return ret;
774 case TARGET_SYS_WRITEC:
775 qemu_semihosting_console_outc(env, args);
776 return 0xdeadbeef;
777 case TARGET_SYS_WRITE0:
778 return qemu_semihosting_console_outs(env, args);
779 case TARGET_SYS_WRITE:
780 GET_ARG(0);
781 GET_ARG(1);
782 GET_ARG(2);
783 len = arg2;
785 gf = get_guestfd(arg0);
786 if (!gf) {
787 errno = EBADF;
788 return set_swi_errno(env, -1);
791 return guestfd_fns[gf->type].writefn(cpu, gf, arg1, len);
792 case TARGET_SYS_READ:
793 GET_ARG(0);
794 GET_ARG(1);
795 GET_ARG(2);
796 len = arg2;
798 gf = get_guestfd(arg0);
799 if (!gf) {
800 errno = EBADF;
801 return set_swi_errno(env, -1);
804 return guestfd_fns[gf->type].readfn(cpu, gf, arg1, len);
805 case TARGET_SYS_READC:
806 return qemu_semihosting_console_inc(env);
807 case TARGET_SYS_ISTTY:
808 GET_ARG(0);
810 gf = get_guestfd(arg0);
811 if (!gf) {
812 errno = EBADF;
813 return set_swi_errno(env, -1);
816 return guestfd_fns[gf->type].isattyfn(cpu, gf);
817 case TARGET_SYS_SEEK:
818 GET_ARG(0);
819 GET_ARG(1);
821 gf = get_guestfd(arg0);
822 if (!gf) {
823 errno = EBADF;
824 return set_swi_errno(env, -1);
827 return guestfd_fns[gf->type].seekfn(cpu, gf, arg1);
828 case TARGET_SYS_FLEN:
829 GET_ARG(0);
831 gf = get_guestfd(arg0);
832 if (!gf) {
833 errno = EBADF;
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__);
840 return -1;
841 case TARGET_SYS_REMOVE:
842 GET_ARG(0);
843 GET_ARG(1);
844 if (use_gdb_syscalls()) {
845 ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s",
846 arg0, (int)arg1+1);
847 } else {
848 s = lock_user_string(arg0);
849 if (!s) {
850 errno = EFAULT;
851 return set_swi_errno(env, -1);
853 ret = set_swi_errno(env, remove(s));
854 unlock_user(s, arg0, 0);
856 return ret;
857 case TARGET_SYS_RENAME:
858 GET_ARG(0);
859 GET_ARG(1);
860 GET_ARG(2);
861 GET_ARG(3);
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);
865 } else {
866 char *s2;
867 s = lock_user_string(arg0);
868 s2 = lock_user_string(arg2);
869 if (!s || !s2) {
870 errno = EFAULT;
871 ret = set_swi_errno(env, -1);
872 } else {
873 ret = set_swi_errno(env, rename(s, s2));
875 if (s2)
876 unlock_user(s2, arg2, 0);
877 if (s)
878 unlock_user(s, arg0, 0);
879 return ret;
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:
886 GET_ARG(0);
887 GET_ARG(1);
888 if (use_gdb_syscalls()) {
889 return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s",
890 arg0, (int)arg1+1);
891 } else {
892 s = lock_user_string(arg0);
893 if (!s) {
894 errno = EFAULT;
895 return set_swi_errno(env, -1);
897 ret = set_swi_errno(env, system(s));
898 unlock_user(s, arg0, 0);
899 return ret;
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.
907 * The inputs are:
908 * * arg0, pointer to a buffer of at least the size
909 * specified in arg1.
910 * * arg1, size of the buffer pointed to by arg0 in
911 * bytes.
913 * The outputs are:
914 * * arg0, pointer to null-terminated string of the
915 * command line.
916 * * arg1, length of the string pointed to by arg0.
919 char *output_buffer;
920 size_t input_size;
921 size_t output_size;
922 int status = 0;
923 #if !defined(CONFIG_USER_ONLY)
924 const char *cmdline;
925 #else
926 TaskState *ts = cs->opaque;
927 #endif
928 GET_ARG(0);
929 GET_ARG(1);
930 input_size = arg1;
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. */
938 #else
939 unsigned int i;
941 output_size = ts->info->arg_end - ts->info->arg_start;
942 if (!output_size) {
944 * We special-case the "empty command line" case (argc==0).
945 * Just provide the terminating 0.
947 output_size = 1;
949 #endif
951 if (output_size > input_size) {
952 /* Not enough space to store command-line arguments. */
953 errno = E2BIG;
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 */
960 errno = EFAULT;
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) {
967 errno = EFAULT;
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);
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_start,
982 output_size)) {
983 errno = EFAULT;
984 status = set_swi_errno(env, -1);
985 goto out;
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] = ' ';
994 out:
995 #endif
996 /* Unlock the buffer on the ARM side. */
997 unlock_user(output_buffer, arg0, output_size);
999 return status;
1001 case TARGET_SYS_HEAPINFO:
1003 target_ulong retvals[4];
1004 target_ulong limit;
1005 int i;
1006 #ifdef CONFIG_USER_ONLY
1007 TaskState *ts = cs->opaque;
1008 #endif
1010 GET_ARG(0);
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) {
1018 abi_ulong ret;
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. */
1023 for (;;) {
1024 ret = do_brk(limit);
1025 if (ret >= limit) {
1026 break;
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. */
1037 #else
1038 limit = ram_size;
1039 /* TODO: Make this use the limit of the loaded application. */
1040 retvals[0] = limit / 2;
1041 retvals[1] = limit;
1042 retvals[2] = limit; /* Stack base */
1043 retvals[3] = 0; /* Stack limit. */
1044 #endif
1046 for (i = 0; i < ARRAY_SIZE(retvals); i++) {
1047 bool fail;
1049 if (is_a64(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 errno = EFAULT;
1058 return set_swi_errno(env, -1);
1061 return 0;
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.
1073 GET_ARG(0);
1074 GET_ARG(1);
1076 if (arg0 == ADP_Stopped_ApplicationExit) {
1077 ret = arg1;
1078 } else {
1079 ret = 1;
1081 } else {
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;
1090 gdb_exit(env, ret);
1091 exit(ret);
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.
1098 if (is_a64(env)) {
1099 return 0;
1101 /* fall through -- invalid for A32/T32 */
1102 default:
1103 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
1104 cpu_dump_state(cs, stderr, 0);
1105 abort();