target/arm/arm-semi: Factor out implementation of SYS_CLOSE
[qemu/ar7.git] / target / arm / arm-semi.c
blobe5f1e2aaaf2223c9575cbfbce7b6f960d73fda45
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
63 /* ADP_Stopped_ApplicationExit is used for exit(0),
64 * anything else is implemented as exit(1) */
65 #define ADP_Stopped_ApplicationExit (0x20026)
67 #ifndef O_BINARY
68 #define O_BINARY 0
69 #endif
71 #define GDB_O_RDONLY 0x000
72 #define GDB_O_WRONLY 0x001
73 #define GDB_O_RDWR 0x002
74 #define GDB_O_APPEND 0x008
75 #define GDB_O_CREAT 0x200
76 #define GDB_O_TRUNC 0x400
77 #define GDB_O_BINARY 0
79 static int gdb_open_modeflags[12] = {
80 GDB_O_RDONLY,
81 GDB_O_RDONLY | GDB_O_BINARY,
82 GDB_O_RDWR,
83 GDB_O_RDWR | GDB_O_BINARY,
84 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
85 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
86 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
87 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
88 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
89 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
90 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
91 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
94 static int open_modeflags[12] = {
95 O_RDONLY,
96 O_RDONLY | O_BINARY,
97 O_RDWR,
98 O_RDWR | O_BINARY,
99 O_WRONLY | O_CREAT | O_TRUNC,
100 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
101 O_RDWR | O_CREAT | O_TRUNC,
102 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
103 O_WRONLY | O_CREAT | O_APPEND,
104 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
105 O_RDWR | O_CREAT | O_APPEND,
106 O_RDWR | O_CREAT | O_APPEND | O_BINARY
109 typedef enum GuestFDType {
110 GuestFDUnused = 0,
111 GuestFDHost = 1,
112 GuestFDGDB = 2,
113 } GuestFDType;
116 * Guest file descriptors are integer indexes into an array of
117 * these structures (we will dynamically resize as necessary).
119 typedef struct GuestFD {
120 GuestFDType type;
121 int hostfd;
122 } GuestFD;
124 static GArray *guestfd_array;
127 * Allocate a new guest file descriptor and return it; if we
128 * couldn't allocate a new fd then return -1.
129 * This is a fairly simplistic implementation because we don't
130 * expect that most semihosting guest programs will make very
131 * heavy use of opening and closing fds.
133 static int alloc_guestfd(void)
135 guint i;
137 if (!guestfd_array) {
138 /* New entries zero-initialized, i.e. type GuestFDUnused */
139 guestfd_array = g_array_new(FALSE, TRUE, sizeof(GuestFD));
142 for (i = 0; i < guestfd_array->len; i++) {
143 GuestFD *gf = &g_array_index(guestfd_array, GuestFD, i);
145 if (gf->type == GuestFDUnused) {
146 return i;
150 /* All elements already in use: expand the array */
151 g_array_set_size(guestfd_array, i + 1);
152 return i;
156 * Look up the guestfd in the data structure; return NULL
157 * for out of bounds, but don't check whether the slot is unused.
158 * This is used internally by the other guestfd functions.
160 static GuestFD *do_get_guestfd(int guestfd)
162 if (!guestfd_array) {
163 return NULL;
166 if (guestfd < 0 || guestfd >= guestfd_array->len) {
167 return NULL;
170 return &g_array_index(guestfd_array, GuestFD, guestfd);
174 * Associate the specified guest fd (which must have been
175 * allocated via alloc_fd() and not previously used) with
176 * the specified host/gdb fd.
178 static void associate_guestfd(int guestfd, int hostfd)
180 GuestFD *gf = do_get_guestfd(guestfd);
182 assert(gf);
183 gf->type = use_gdb_syscalls() ? GuestFDGDB : GuestFDHost;
184 gf->hostfd = hostfd;
188 * Deallocate the specified guest file descriptor. This doesn't
189 * close the host fd, it merely undoes the work of alloc_fd().
191 static void dealloc_guestfd(int guestfd)
193 GuestFD *gf = do_get_guestfd(guestfd);
195 assert(gf);
196 gf->type = GuestFDUnused;
200 * Given a guest file descriptor, get the associated struct.
201 * If the fd is not valid, return NULL. This is the function
202 * used by the various semihosting calls to validate a handle
203 * from the guest.
204 * Note: calling alloc_guestfd() or dealloc_guestfd() will
205 * invalidate any GuestFD* obtained by calling this function.
207 static GuestFD *get_guestfd(int guestfd)
209 GuestFD *gf = do_get_guestfd(guestfd);
211 if (!gf || gf->type == GuestFDUnused) {
212 return NULL;
214 return gf;
218 * The semihosting API has no concept of its errno being thread-safe,
219 * as the API design predates SMP CPUs and was intended as a simple
220 * real-hardware set of debug functionality. For QEMU, we make the
221 * errno be per-thread in linux-user mode; in softmmu it is a simple
222 * global, and we assume that the guest takes care of avoiding any races.
224 #ifndef CONFIG_USER_ONLY
225 static target_ulong syscall_err;
227 #include "exec/softmmu-semi.h"
228 #endif
230 static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code)
232 if (code == (uint32_t)-1) {
233 #ifdef CONFIG_USER_ONLY
234 CPUState *cs = env_cpu(env);
235 TaskState *ts = cs->opaque;
237 ts->swi_errno = errno;
238 #else
239 syscall_err = errno;
240 #endif
242 return code;
245 static inline uint32_t get_swi_errno(CPUARMState *env)
247 #ifdef CONFIG_USER_ONLY
248 CPUState *cs = env_cpu(env);
249 TaskState *ts = cs->opaque;
251 return ts->swi_errno;
252 #else
253 return syscall_err;
254 #endif
257 static target_ulong arm_semi_syscall_len;
259 static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
261 ARMCPU *cpu = ARM_CPU(cs);
262 CPUARMState *env = &cpu->env;
263 target_ulong reg0 = is_a64(env) ? env->xregs[0] : env->regs[0];
265 if (ret == (target_ulong)-1) {
266 errno = err;
267 set_swi_errno(env, -1);
268 reg0 = ret;
269 } else {
270 /* Fixup syscalls that use nonstardard return conventions. */
271 switch (reg0) {
272 case TARGET_SYS_WRITE:
273 case TARGET_SYS_READ:
274 reg0 = arm_semi_syscall_len - ret;
275 break;
276 case TARGET_SYS_SEEK:
277 reg0 = 0;
278 break;
279 default:
280 reg0 = ret;
281 break;
284 if (is_a64(env)) {
285 env->xregs[0] = reg0;
286 } else {
287 env->regs[0] = reg0;
291 static target_ulong arm_flen_buf(ARMCPU *cpu)
293 /* Return an address in target memory of 64 bytes where the remote
294 * gdb should write its stat struct. (The format of this structure
295 * is defined by GDB's remote protocol and is not target-specific.)
296 * We put this on the guest's stack just below SP.
298 CPUARMState *env = &cpu->env;
299 target_ulong sp;
301 if (is_a64(env)) {
302 sp = env->xregs[31];
303 } else {
304 sp = env->regs[13];
307 return sp - 64;
310 static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
312 ARMCPU *cpu = ARM_CPU(cs);
313 CPUARMState *env = &cpu->env;
314 /* The size is always stored in big-endian order, extract
315 the value. We assume the size always fit in 32 bits. */
316 uint32_t size;
317 cpu_memory_rw_debug(cs, arm_flen_buf(cpu) + 32, (uint8_t *)&size, 4, 0);
318 size = be32_to_cpu(size);
319 if (is_a64(env)) {
320 env->xregs[0] = size;
321 } else {
322 env->regs[0] = size;
324 errno = err;
325 set_swi_errno(env, -1);
328 static int arm_semi_open_guestfd;
330 static void arm_semi_open_cb(CPUState *cs, target_ulong ret, target_ulong err)
332 ARMCPU *cpu = ARM_CPU(cs);
333 CPUARMState *env = &cpu->env;
334 if (ret == (target_ulong)-1) {
335 errno = err;
336 set_swi_errno(env, -1);
337 dealloc_guestfd(arm_semi_open_guestfd);
338 } else {
339 associate_guestfd(arm_semi_open_guestfd, ret);
340 ret = arm_semi_open_guestfd;
343 if (is_a64(env)) {
344 env->xregs[0] = ret;
345 } else {
346 env->regs[0] = ret;
350 static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb,
351 const char *fmt, ...)
353 va_list va;
354 CPUARMState *env = &cpu->env;
356 va_start(va, fmt);
357 gdb_do_syscallv(cb, fmt, va);
358 va_end(va);
361 * FIXME: in softmmu mode, the gdbstub will schedule our callback
362 * to occur, but will not actually call it to complete the syscall
363 * until after this function has returned and we are back in the
364 * CPU main loop. Therefore callers to this function must not
365 * do anything with its return value, because it is not necessarily
366 * the result of the syscall, but could just be the old value of X0.
367 * The only thing safe to do with this is that the callers of
368 * do_arm_semihosting() will write it straight back into X0.
369 * (In linux-user mode, the callback will have happened before
370 * gdb_do_syscallv() returns.)
372 * We should tidy this up so neither this function nor
373 * do_arm_semihosting() return a value, so the mistake of
374 * doing something with the return value is not possible to make.
377 return is_a64(env) ? env->xregs[0] : env->regs[0];
381 * Types for functions implementing various semihosting calls
382 * for specific types of guest file descriptor. These must all
383 * do the work and return the required return value for the guest,
384 * setting the guest errno if appropriate.
386 typedef uint32_t sys_closefn(ARMCPU *cpu, GuestFD *gf);
388 static uint32_t host_closefn(ARMCPU *cpu, GuestFD *gf)
390 CPUARMState *env = &cpu->env;
392 return set_swi_errno(env, close(gf->hostfd));
395 static uint32_t gdb_closefn(ARMCPU *cpu, GuestFD *gf)
397 return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", gf->hostfd);
400 typedef struct GuestFDFunctions {
401 sys_closefn *closefn;
402 } GuestFDFunctions;
404 static const GuestFDFunctions guestfd_fns[] = {
405 [GuestFDHost] = {
406 .closefn = host_closefn,
408 [GuestFDGDB] = {
409 .closefn = gdb_closefn,
413 /* Read the input value from the argument block; fail the semihosting
414 * call if the memory read fails.
416 #define GET_ARG(n) do { \
417 if (is_a64(env)) { \
418 if (get_user_u64(arg ## n, args + (n) * 8)) { \
419 errno = EFAULT; \
420 return set_swi_errno(env, -1); \
422 } else { \
423 if (get_user_u32(arg ## n, args + (n) * 4)) { \
424 errno = EFAULT; \
425 return set_swi_errno(env, -1); \
428 } while (0)
430 #define SET_ARG(n, val) \
431 (is_a64(env) ? \
432 put_user_u64(val, args + (n) * 8) : \
433 put_user_u32(val, args + (n) * 4))
436 * Do a semihosting call.
438 * The specification always says that the "return register" either
439 * returns a specific value or is corrupted, so we don't need to
440 * report to our caller whether we are returning a value or trying to
441 * leave the register unchanged. We use 0xdeadbeef as the return value
442 * when there isn't a defined return value for the call.
444 target_ulong do_arm_semihosting(CPUARMState *env)
446 ARMCPU *cpu = env_archcpu(env);
447 CPUState *cs = env_cpu(env);
448 target_ulong args;
449 target_ulong arg0, arg1, arg2, arg3;
450 char * s;
451 int nr;
452 uint32_t ret;
453 uint32_t len;
454 GuestFD *gf;
456 if (is_a64(env)) {
457 /* Note that the syscall number is in W0, not X0 */
458 nr = env->xregs[0] & 0xffffffffU;
459 args = env->xregs[1];
460 } else {
461 nr = env->regs[0];
462 args = env->regs[1];
465 switch (nr) {
466 case TARGET_SYS_OPEN:
468 int guestfd;
470 GET_ARG(0);
471 GET_ARG(1);
472 GET_ARG(2);
473 s = lock_user_string(arg0);
474 if (!s) {
475 errno = EFAULT;
476 return set_swi_errno(env, -1);
478 if (arg1 >= 12) {
479 unlock_user(s, arg0, 0);
480 errno = EINVAL;
481 return set_swi_errno(env, -1);
484 guestfd = alloc_guestfd();
485 if (guestfd < 0) {
486 unlock_user(s, arg0, 0);
487 errno = EMFILE;
488 return set_swi_errno(env, -1);
491 if (strcmp(s, ":tt") == 0) {
492 int result_fileno = arg1 < 4 ? STDIN_FILENO : STDOUT_FILENO;
493 associate_guestfd(guestfd, result_fileno);
494 unlock_user(s, arg0, 0);
495 return guestfd;
497 if (use_gdb_syscalls()) {
498 arm_semi_open_guestfd = guestfd;
499 ret = arm_gdb_syscall(cpu, arm_semi_open_cb, "open,%s,%x,1a4", arg0,
500 (int)arg2+1, gdb_open_modeflags[arg1]);
501 } else {
502 ret = set_swi_errno(env, open(s, open_modeflags[arg1], 0644));
503 if (ret == (uint32_t)-1) {
504 dealloc_guestfd(guestfd);
505 } else {
506 associate_guestfd(guestfd, ret);
507 ret = guestfd;
510 unlock_user(s, arg0, 0);
511 return ret;
513 case TARGET_SYS_CLOSE:
514 GET_ARG(0);
516 gf = get_guestfd(arg0);
517 if (!gf) {
518 errno = EBADF;
519 return set_swi_errno(env, -1);
522 ret = guestfd_fns[gf->type].closefn(cpu, gf);
523 dealloc_guestfd(arg0);
524 return ret;
525 case TARGET_SYS_WRITEC:
526 qemu_semihosting_console_outc(env, args);
527 return 0xdeadbeef;
528 case TARGET_SYS_WRITE0:
529 return qemu_semihosting_console_outs(env, args);
530 case TARGET_SYS_WRITE:
531 GET_ARG(0);
532 GET_ARG(1);
533 GET_ARG(2);
534 len = arg2;
536 gf = get_guestfd(arg0);
537 if (!gf) {
538 errno = EBADF;
539 return set_swi_errno(env, -1);
542 if (use_gdb_syscalls()) {
543 arm_semi_syscall_len = len;
544 return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x",
545 gf->hostfd, arg1, len);
546 } else {
547 s = lock_user(VERIFY_READ, arg1, len, 1);
548 if (!s) {
549 /* Return bytes not written on error */
550 return len;
552 ret = set_swi_errno(env, write(gf->hostfd, s, len));
553 unlock_user(s, arg1, 0);
554 if (ret == (uint32_t)-1) {
555 ret = 0;
557 /* Return bytes not written */
558 return len - ret;
560 case TARGET_SYS_READ:
561 GET_ARG(0);
562 GET_ARG(1);
563 GET_ARG(2);
564 len = arg2;
566 gf = get_guestfd(arg0);
567 if (!gf) {
568 errno = EBADF;
569 return set_swi_errno(env, -1);
572 if (use_gdb_syscalls()) {
573 arm_semi_syscall_len = len;
574 return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x",
575 gf->hostfd, arg1, len);
576 } else {
577 s = lock_user(VERIFY_WRITE, arg1, len, 0);
578 if (!s) {
579 /* return bytes not read */
580 return len;
582 do {
583 ret = set_swi_errno(env, read(gf->hostfd, s, len));
584 } while (ret == -1 && errno == EINTR);
585 unlock_user(s, arg1, len);
586 if (ret == (uint32_t)-1) {
587 ret = 0;
589 /* Return bytes not read */
590 return len - ret;
592 case TARGET_SYS_READC:
593 qemu_log_mask(LOG_UNIMP, "%s: SYS_READC not implemented", __func__);
594 return 0;
595 case TARGET_SYS_ISTTY:
596 GET_ARG(0);
598 gf = get_guestfd(arg0);
599 if (!gf) {
600 errno = EBADF;
601 return set_swi_errno(env, -1);
604 if (use_gdb_syscalls()) {
605 return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", gf->hostfd);
606 } else {
607 return isatty(gf->hostfd);
609 case TARGET_SYS_SEEK:
610 GET_ARG(0);
611 GET_ARG(1);
613 gf = get_guestfd(arg0);
614 if (!gf) {
615 errno = EBADF;
616 return set_swi_errno(env, -1);
619 if (use_gdb_syscalls()) {
620 return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0",
621 gf->hostfd, arg1);
622 } else {
623 ret = set_swi_errno(env, lseek(gf->hostfd, arg1, SEEK_SET));
624 if (ret == (uint32_t)-1)
625 return -1;
626 return 0;
628 case TARGET_SYS_FLEN:
629 GET_ARG(0);
631 gf = get_guestfd(arg0);
632 if (!gf) {
633 errno = EBADF;
634 return set_swi_errno(env, -1);
637 if (use_gdb_syscalls()) {
638 return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x",
639 gf->hostfd, arm_flen_buf(cpu));
640 } else {
641 struct stat buf;
642 ret = set_swi_errno(env, fstat(gf->hostfd, &buf));
643 if (ret == (uint32_t)-1)
644 return -1;
645 return buf.st_size;
647 case TARGET_SYS_TMPNAM:
648 qemu_log_mask(LOG_UNIMP, "%s: SYS_TMPNAM not implemented", __func__);
649 return -1;
650 case TARGET_SYS_REMOVE:
651 GET_ARG(0);
652 GET_ARG(1);
653 if (use_gdb_syscalls()) {
654 ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s",
655 arg0, (int)arg1+1);
656 } else {
657 s = lock_user_string(arg0);
658 if (!s) {
659 errno = EFAULT;
660 return set_swi_errno(env, -1);
662 ret = set_swi_errno(env, remove(s));
663 unlock_user(s, arg0, 0);
665 return ret;
666 case TARGET_SYS_RENAME:
667 GET_ARG(0);
668 GET_ARG(1);
669 GET_ARG(2);
670 GET_ARG(3);
671 if (use_gdb_syscalls()) {
672 return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s",
673 arg0, (int)arg1+1, arg2, (int)arg3+1);
674 } else {
675 char *s2;
676 s = lock_user_string(arg0);
677 s2 = lock_user_string(arg2);
678 if (!s || !s2) {
679 errno = EFAULT;
680 ret = set_swi_errno(env, -1);
681 } else {
682 ret = set_swi_errno(env, rename(s, s2));
684 if (s2)
685 unlock_user(s2, arg2, 0);
686 if (s)
687 unlock_user(s, arg0, 0);
688 return ret;
690 case TARGET_SYS_CLOCK:
691 return clock() / (CLOCKS_PER_SEC / 100);
692 case TARGET_SYS_TIME:
693 return set_swi_errno(env, time(NULL));
694 case TARGET_SYS_SYSTEM:
695 GET_ARG(0);
696 GET_ARG(1);
697 if (use_gdb_syscalls()) {
698 return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s",
699 arg0, (int)arg1+1);
700 } else {
701 s = lock_user_string(arg0);
702 if (!s) {
703 errno = EFAULT;
704 return set_swi_errno(env, -1);
706 ret = set_swi_errno(env, system(s));
707 unlock_user(s, arg0, 0);
708 return ret;
710 case TARGET_SYS_ERRNO:
711 return get_swi_errno(env);
712 case TARGET_SYS_GET_CMDLINE:
714 /* Build a command-line from the original argv.
716 * The inputs are:
717 * * arg0, pointer to a buffer of at least the size
718 * specified in arg1.
719 * * arg1, size of the buffer pointed to by arg0 in
720 * bytes.
722 * The outputs are:
723 * * arg0, pointer to null-terminated string of the
724 * command line.
725 * * arg1, length of the string pointed to by arg0.
728 char *output_buffer;
729 size_t input_size;
730 size_t output_size;
731 int status = 0;
732 #if !defined(CONFIG_USER_ONLY)
733 const char *cmdline;
734 #else
735 TaskState *ts = cs->opaque;
736 #endif
737 GET_ARG(0);
738 GET_ARG(1);
739 input_size = arg1;
740 /* Compute the size of the output string. */
741 #if !defined(CONFIG_USER_ONLY)
742 cmdline = semihosting_get_cmdline();
743 if (cmdline == NULL) {
744 cmdline = ""; /* Default to an empty line. */
746 output_size = strlen(cmdline) + 1; /* Count terminating 0. */
747 #else
748 unsigned int i;
750 output_size = ts->info->arg_end - ts->info->arg_start;
751 if (!output_size) {
753 * We special-case the "empty command line" case (argc==0).
754 * Just provide the terminating 0.
756 output_size = 1;
758 #endif
760 if (output_size > input_size) {
761 /* Not enough space to store command-line arguments. */
762 errno = E2BIG;
763 return set_swi_errno(env, -1);
766 /* Adjust the command-line length. */
767 if (SET_ARG(1, output_size - 1)) {
768 /* Couldn't write back to argument block */
769 errno = EFAULT;
770 return set_swi_errno(env, -1);
773 /* Lock the buffer on the ARM side. */
774 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
775 if (!output_buffer) {
776 errno = EFAULT;
777 return set_swi_errno(env, -1);
780 /* Copy the command-line arguments. */
781 #if !defined(CONFIG_USER_ONLY)
782 pstrcpy(output_buffer, output_size, cmdline);
783 #else
784 if (output_size == 1) {
785 /* Empty command-line. */
786 output_buffer[0] = '\0';
787 goto out;
790 if (copy_from_user(output_buffer, ts->info->arg_start,
791 output_size)) {
792 errno = EFAULT;
793 status = set_swi_errno(env, -1);
794 goto out;
797 /* Separate arguments by white spaces. */
798 for (i = 0; i < output_size - 1; i++) {
799 if (output_buffer[i] == 0) {
800 output_buffer[i] = ' ';
803 out:
804 #endif
805 /* Unlock the buffer on the ARM side. */
806 unlock_user(output_buffer, arg0, output_size);
808 return status;
810 case TARGET_SYS_HEAPINFO:
812 target_ulong retvals[4];
813 target_ulong limit;
814 int i;
815 #ifdef CONFIG_USER_ONLY
816 TaskState *ts = cs->opaque;
817 #endif
819 GET_ARG(0);
821 #ifdef CONFIG_USER_ONLY
823 * Some C libraries assume the heap immediately follows .bss, so
824 * allocate it using sbrk.
826 if (!ts->heap_limit) {
827 abi_ulong ret;
829 ts->heap_base = do_brk(0);
830 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
831 /* Try a big heap, and reduce the size if that fails. */
832 for (;;) {
833 ret = do_brk(limit);
834 if (ret >= limit) {
835 break;
837 limit = (ts->heap_base >> 1) + (limit >> 1);
839 ts->heap_limit = limit;
842 retvals[0] = ts->heap_base;
843 retvals[1] = ts->heap_limit;
844 retvals[2] = ts->stack_base;
845 retvals[3] = 0; /* Stack limit. */
846 #else
847 limit = ram_size;
848 /* TODO: Make this use the limit of the loaded application. */
849 retvals[0] = limit / 2;
850 retvals[1] = limit;
851 retvals[2] = limit; /* Stack base */
852 retvals[3] = 0; /* Stack limit. */
853 #endif
855 for (i = 0; i < ARRAY_SIZE(retvals); i++) {
856 bool fail;
858 if (is_a64(env)) {
859 fail = put_user_u64(retvals[i], arg0 + i * 8);
860 } else {
861 fail = put_user_u32(retvals[i], arg0 + i * 4);
864 if (fail) {
865 /* Couldn't write back to argument block */
866 errno = EFAULT;
867 return set_swi_errno(env, -1);
870 return 0;
872 case TARGET_SYS_EXIT:
873 if (is_a64(env)) {
875 * The A64 version of this call takes a parameter block,
876 * so the application-exit type can return a subcode which
877 * is the exit status code from the application.
879 GET_ARG(0);
880 GET_ARG(1);
882 if (arg0 == ADP_Stopped_ApplicationExit) {
883 ret = arg1;
884 } else {
885 ret = 1;
887 } else {
889 * ARM specifies only Stopped_ApplicationExit as normal
890 * exit, everything else is considered an error
892 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
894 gdb_exit(env, ret);
895 exit(ret);
896 case TARGET_SYS_SYNCCACHE:
898 * Clean the D-cache and invalidate the I-cache for the specified
899 * virtual address range. This is a nop for us since we don't
900 * implement caches. This is only present on A64.
902 if (is_a64(env)) {
903 return 0;
905 /* fall through -- invalid for A32/T32 */
906 default:
907 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
908 cpu_dump_state(cs, stderr, 0);
909 abort();