]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/arm-semi.c
Include qemu-common.h exactly where needed
[mirror_qemu.git] / target / arm / arm-semi.c
CommitLineData
a4f81979
FB
1/*
2 * Arm "Angel" semihosting syscalls
5fafdf24 3 *
8e71621f 4 * Copyright (c) 2005, 2007 CodeSourcery.
4cb28db9 5 * Copyright (c) 2019 Linaro
8e71621f 6 * Written by Paul Brook.
a4f81979
FB
7 *
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.
12 *
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.
17 *
18 * You should have received a copy of the GNU General Public License
8167ee88 19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
4cb28db9
AB
20 *
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
a4f81979
FB
24 */
25
74c21bd0 26#include "qemu/osdep.h"
a4f81979 27
8e71621f 28#include "cpu.h"
f1672e6f 29#include "hw/semihosting/semihost.h"
0dc07721 30#include "hw/semihosting/console.h"
a131795f 31#include "qemu/log.h"
8e71621f 32#ifdef CONFIG_USER_ONLY
a4f81979
FB
33#include "qemu.h"
34
35#define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
8e71621f 36#else
022c62cb 37#include "exec/gdbstub.h"
f348b6d1 38#include "qemu/cutils.h"
8e71621f 39#endif
a4f81979 40
3881725c
SW
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
e9ebfbfc 61#define TARGET_SYS_SYNCCACHE 0x19
a4f81979 62
1ecc3a2d
LI
63/* ADP_Stopped_ApplicationExit is used for exit(0),
64 * anything else is implemented as exit(1) */
65#define ADP_Stopped_ApplicationExit (0x20026)
66
a4f81979
FB
67#ifndef O_BINARY
68#define O_BINARY 0
69#endif
70
a2d1ebaf
PB
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
78
79static 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
92};
93
94static int open_modeflags[12] = {
a4f81979
FB
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
107};
108
8e71621f 109#ifdef CONFIG_USER_ONLY
a4f81979
FB
110static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
111{
8e71621f
PB
112 if (code == (uint32_t)-1)
113 ts->swi_errno = errno;
114 return code;
115}
116#else
81926f47 117static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code)
8e71621f
PB
118{
119 return code;
120}
121
022c62cb 122#include "exec/softmmu-semi.h"
8e71621f 123#endif
a4f81979 124
a2d1ebaf
PB
125static target_ulong arm_semi_syscall_len;
126
33d9cc8a
PB
127#if !defined(CONFIG_USER_ONLY)
128static target_ulong syscall_err;
129#endif
130
9e0c5422 131static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
a2d1ebaf 132{
9e0c5422
AF
133 ARMCPU *cpu = ARM_CPU(cs);
134 CPUARMState *env = &cpu->env;
a2d1ebaf 135#ifdef CONFIG_USER_ONLY
0429a971 136 TaskState *ts = cs->opaque;
a2d1ebaf 137#endif
faacc041 138 target_ulong reg0 = is_a64(env) ? env->xregs[0] : env->regs[0];
33d9cc8a 139
a2d1ebaf
PB
140 if (ret == (target_ulong)-1) {
141#ifdef CONFIG_USER_ONLY
142 ts->swi_errno = err;
33d9cc8a 143#else
6e0fafe2 144 syscall_err = err;
a2d1ebaf 145#endif
bb19cbc9 146 reg0 = ret;
a2d1ebaf
PB
147 } else {
148 /* Fixup syscalls that use nonstardard return conventions. */
bb19cbc9 149 switch (reg0) {
3881725c
SW
150 case TARGET_SYS_WRITE:
151 case TARGET_SYS_READ:
bb19cbc9 152 reg0 = arm_semi_syscall_len - ret;
a2d1ebaf 153 break;
3881725c 154 case TARGET_SYS_SEEK:
bb19cbc9 155 reg0 = 0;
a2d1ebaf
PB
156 break;
157 default:
bb19cbc9 158 reg0 = ret;
a2d1ebaf
PB
159 break;
160 }
161 }
faacc041
PM
162 if (is_a64(env)) {
163 env->xregs[0] = reg0;
164 } else {
165 env->regs[0] = reg0;
166 }
167}
168
169static target_ulong arm_flen_buf(ARMCPU *cpu)
170{
171 /* Return an address in target memory of 64 bytes where the remote
172 * gdb should write its stat struct. (The format of this structure
173 * is defined by GDB's remote protocol and is not target-specific.)
174 * We put this on the guest's stack just below SP.
175 */
176 CPUARMState *env = &cpu->env;
177 target_ulong sp;
178
179 if (is_a64(env)) {
180 sp = env->xregs[31];
181 } else {
182 sp = env->regs[13];
183 }
184
185 return sp - 64;
a2d1ebaf
PB
186}
187
9e0c5422 188static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
33d9cc8a 189{
9e0c5422
AF
190 ARMCPU *cpu = ARM_CPU(cs);
191 CPUARMState *env = &cpu->env;
33d9cc8a
PB
192 /* The size is always stored in big-endian order, extract
193 the value. We assume the size always fit in 32 bits. */
194 uint32_t size;
faacc041
PM
195 cpu_memory_rw_debug(cs, arm_flen_buf(cpu) + 32, (uint8_t *)&size, 4, 0);
196 size = be32_to_cpu(size);
197 if (is_a64(env)) {
198 env->xregs[0] = size;
199 } else {
200 env->regs[0] = size;
201 }
33d9cc8a 202#ifdef CONFIG_USER_ONLY
0429a971 203 ((TaskState *)cs->opaque)->swi_errno = err;
33d9cc8a
PB
204#else
205 syscall_err = err;
206#endif
207}
208
bb19cbc9
PM
209static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb,
210 const char *fmt, ...)
211{
212 va_list va;
213 CPUARMState *env = &cpu->env;
214
215 va_start(va, fmt);
216 gdb_do_syscallv(cb, fmt, va);
217 va_end(va);
218
219 /* FIXME: we are implicitly relying on the syscall completing
220 * before this point, which is not guaranteed. We should
221 * put in an explicit synchronization between this and
222 * the callback function.
223 */
224
faacc041 225 return is_a64(env) ? env->xregs[0] : env->regs[0];
bb19cbc9
PM
226}
227
f296c0d1
PM
228/* Read the input value from the argument block; fail the semihosting
229 * call if the memory read fails.
230 */
231#define GET_ARG(n) do { \
faacc041
PM
232 if (is_a64(env)) { \
233 if (get_user_u64(arg ## n, args + (n) * 8)) { \
234 return -1; \
235 } \
236 } else { \
237 if (get_user_u32(arg ## n, args + (n) * 4)) { \
238 return -1; \
239 } \
f296c0d1
PM
240 } \
241} while (0)
242
faacc041
PM
243#define SET_ARG(n, val) \
244 (is_a64(env) ? \
245 put_user_u64(val, args + (n) * 8) : \
246 put_user_u32(val, args + (n) * 4))
247
4cb28db9
AB
248/*
249 * Do a semihosting call.
250 *
251 * The specification always says that the "return register" either
252 * returns a specific value or is corrupted, so we don't need to
253 * report to our caller whether we are returning a value or trying to
254 * leave the register unchanged. We use 0xdeadbeef as the return value
255 * when there isn't a defined return value for the call.
256 */
faacc041 257target_ulong do_arm_semihosting(CPUARMState *env)
a4f81979 258{
2fc0cc0e
RH
259 ARMCPU *cpu = env_archcpu(env);
260 CPUState *cs = env_cpu(env);
53a5960a 261 target_ulong args;
f296c0d1 262 target_ulong arg0, arg1, arg2, arg3;
a4f81979
FB
263 char * s;
264 int nr;
265 uint32_t ret;
8e71621f
PB
266 uint32_t len;
267#ifdef CONFIG_USER_ONLY
0429a971 268 TaskState *ts = cs->opaque;
8e71621f 269#else
81926f47 270 CPUARMState *ts = env;
8e71621f 271#endif
a4f81979 272
faacc041
PM
273 if (is_a64(env)) {
274 /* Note that the syscall number is in W0, not X0 */
275 nr = env->xregs[0] & 0xffffffffU;
276 args = env->xregs[1];
277 } else {
278 nr = env->regs[0];
279 args = env->regs[1];
280 }
281
a4f81979 282 switch (nr) {
3881725c 283 case TARGET_SYS_OPEN:
f296c0d1
PM
284 GET_ARG(0);
285 GET_ARG(1);
286 GET_ARG(2);
287 s = lock_user_string(arg0);
288 if (!s) {
579a97f7
FB
289 /* FIXME - should this error code be -TARGET_EFAULT ? */
290 return (uint32_t)-1;
f296c0d1
PM
291 }
292 if (arg1 >= 12) {
293 unlock_user(s, arg0, 0);
579a97f7 294 return (uint32_t)-1;
396bef4b 295 }
a4f81979 296 if (strcmp(s, ":tt") == 0) {
f296c0d1
PM
297 int result_fileno = arg1 < 4 ? STDIN_FILENO : STDOUT_FILENO;
298 unlock_user(s, arg0, 0);
396bef4b 299 return result_fileno;
a4f81979 300 }
a2d1ebaf 301 if (use_gdb_syscalls()) {
bb19cbc9
PM
302 ret = arm_gdb_syscall(cpu, arm_semi_cb, "open,%s,%x,1a4", arg0,
303 (int)arg2+1, gdb_open_modeflags[arg1]);
a2d1ebaf 304 } else {
f296c0d1 305 ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644));
a2d1ebaf 306 }
f296c0d1 307 unlock_user(s, arg0, 0);
8e71621f 308 return ret;
3881725c 309 case TARGET_SYS_CLOSE:
f296c0d1 310 GET_ARG(0);
a2d1ebaf 311 if (use_gdb_syscalls()) {
bb19cbc9 312 return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", arg0);
a2d1ebaf 313 } else {
f296c0d1 314 return set_swi_errno(ts, close(arg0));
a2d1ebaf 315 }
3881725c 316 case TARGET_SYS_WRITEC:
0dc07721
AB
317 qemu_semihosting_console_out(env, args, 1);
318 return 0xdeadbeef;
3881725c 319 case TARGET_SYS_WRITE0:
0dc07721 320 return qemu_semihosting_console_out(env, args, 0);
3881725c 321 case TARGET_SYS_WRITE:
f296c0d1
PM
322 GET_ARG(0);
323 GET_ARG(1);
324 GET_ARG(2);
325 len = arg2;
a2d1ebaf
PB
326 if (use_gdb_syscalls()) {
327 arm_semi_syscall_len = len;
bb19cbc9
PM
328 return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x",
329 arg0, arg1, len);
a2d1ebaf 330 } else {
f296c0d1
PM
331 s = lock_user(VERIFY_READ, arg1, len, 1);
332 if (!s) {
629a0b06
AB
333 /* Return bytes not written on error */
334 return len;
f296c0d1
PM
335 }
336 ret = set_swi_errno(ts, write(arg0, s, len));
337 unlock_user(s, arg1, 0);
629a0b06
AB
338 if (ret == (uint32_t)-1) {
339 ret = 0;
340 }
341 /* Return bytes not written */
a2d1ebaf
PB
342 return len - ret;
343 }
3881725c 344 case TARGET_SYS_READ:
f296c0d1
PM
345 GET_ARG(0);
346 GET_ARG(1);
347 GET_ARG(2);
348 len = arg2;
a2d1ebaf
PB
349 if (use_gdb_syscalls()) {
350 arm_semi_syscall_len = len;
bb19cbc9
PM
351 return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x",
352 arg0, arg1, len);
a2d1ebaf 353 } else {
f296c0d1
PM
354 s = lock_user(VERIFY_WRITE, arg1, len, 0);
355 if (!s) {
629a0b06
AB
356 /* return bytes not read */
357 return len;
f296c0d1
PM
358 }
359 do {
360 ret = set_swi_errno(ts, read(arg0, s, len));
361 } while (ret == -1 && errno == EINTR);
362 unlock_user(s, arg1, len);
629a0b06
AB
363 if (ret == (uint32_t)-1) {
364 ret = 0;
365 }
366 /* Return bytes not read */
a2d1ebaf
PB
367 return len - ret;
368 }
3881725c 369 case TARGET_SYS_READC:
a131795f 370 qemu_log_mask(LOG_UNIMP, "%s: SYS_READC not implemented", __func__);
a4f81979 371 return 0;
3881725c 372 case TARGET_SYS_ISTTY:
f296c0d1 373 GET_ARG(0);
a2d1ebaf 374 if (use_gdb_syscalls()) {
bb19cbc9 375 return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", arg0);
a2d1ebaf 376 } else {
f296c0d1 377 return isatty(arg0);
a2d1ebaf 378 }
3881725c 379 case TARGET_SYS_SEEK:
f296c0d1
PM
380 GET_ARG(0);
381 GET_ARG(1);
a2d1ebaf 382 if (use_gdb_syscalls()) {
bb19cbc9
PM
383 return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0",
384 arg0, arg1);
a2d1ebaf 385 } else {
f296c0d1 386 ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET));
a2d1ebaf
PB
387 if (ret == (uint32_t)-1)
388 return -1;
389 return 0;
390 }
3881725c 391 case TARGET_SYS_FLEN:
f296c0d1 392 GET_ARG(0);
a2d1ebaf 393 if (use_gdb_syscalls()) {
bb19cbc9 394 return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x",
faacc041 395 arg0, arm_flen_buf(cpu));
a2d1ebaf 396 } else {
a4f81979 397 struct stat buf;
f296c0d1 398 ret = set_swi_errno(ts, fstat(arg0, &buf));
a4f81979
FB
399 if (ret == (uint32_t)-1)
400 return -1;
401 return buf.st_size;
402 }
3881725c 403 case TARGET_SYS_TMPNAM:
a131795f 404 qemu_log_mask(LOG_UNIMP, "%s: SYS_TMPNAM not implemented", __func__);
a4f81979 405 return -1;
3881725c 406 case TARGET_SYS_REMOVE:
f296c0d1
PM
407 GET_ARG(0);
408 GET_ARG(1);
a2d1ebaf 409 if (use_gdb_syscalls()) {
bb19cbc9
PM
410 ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s",
411 arg0, (int)arg1+1);
a2d1ebaf 412 } else {
f296c0d1
PM
413 s = lock_user_string(arg0);
414 if (!s) {
579a97f7
FB
415 /* FIXME - should this error code be -TARGET_EFAULT ? */
416 return (uint32_t)-1;
f296c0d1 417 }
a2d1ebaf 418 ret = set_swi_errno(ts, remove(s));
f296c0d1 419 unlock_user(s, arg0, 0);
a2d1ebaf 420 }
8e71621f 421 return ret;
3881725c 422 case TARGET_SYS_RENAME:
f296c0d1
PM
423 GET_ARG(0);
424 GET_ARG(1);
425 GET_ARG(2);
426 GET_ARG(3);
a2d1ebaf 427 if (use_gdb_syscalls()) {
bb19cbc9
PM
428 return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s",
429 arg0, (int)arg1+1, arg2, (int)arg3+1);
a2d1ebaf 430 } else {
8e71621f 431 char *s2;
f296c0d1
PM
432 s = lock_user_string(arg0);
433 s2 = lock_user_string(arg2);
579a97f7
FB
434 if (!s || !s2)
435 /* FIXME - should this error code be -TARGET_EFAULT ? */
436 ret = (uint32_t)-1;
437 else
438 ret = set_swi_errno(ts, rename(s, s2));
439 if (s2)
f296c0d1 440 unlock_user(s2, arg2, 0);
579a97f7 441 if (s)
f296c0d1 442 unlock_user(s, arg0, 0);
8e71621f
PB
443 return ret;
444 }
3881725c 445 case TARGET_SYS_CLOCK:
a4f81979 446 return clock() / (CLOCKS_PER_SEC / 100);
3881725c 447 case TARGET_SYS_TIME:
a4f81979 448 return set_swi_errno(ts, time(NULL));
3881725c 449 case TARGET_SYS_SYSTEM:
f296c0d1
PM
450 GET_ARG(0);
451 GET_ARG(1);
a2d1ebaf 452 if (use_gdb_syscalls()) {
bb19cbc9
PM
453 return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s",
454 arg0, (int)arg1+1);
a2d1ebaf 455 } else {
f296c0d1
PM
456 s = lock_user_string(arg0);
457 if (!s) {
579a97f7
FB
458 /* FIXME - should this error code be -TARGET_EFAULT ? */
459 return (uint32_t)-1;
f296c0d1 460 }
a2d1ebaf 461 ret = set_swi_errno(ts, system(s));
f296c0d1 462 unlock_user(s, arg0, 0);
a982b531 463 return ret;
a2d1ebaf 464 }
3881725c 465 case TARGET_SYS_ERRNO:
8e71621f 466#ifdef CONFIG_USER_ONLY
a4f81979 467 return ts->swi_errno;
8e71621f 468#else
33d9cc8a 469 return syscall_err;
8e71621f 470#endif
3881725c 471 case TARGET_SYS_GET_CMDLINE:
38d0662a 472 {
1c1b40c1
CV
473 /* Build a command-line from the original argv.
474 *
475 * The inputs are:
f296c0d1
PM
476 * * arg0, pointer to a buffer of at least the size
477 * specified in arg1.
478 * * arg1, size of the buffer pointed to by arg0 in
1c1b40c1
CV
479 * bytes.
480 *
481 * The outputs are:
f296c0d1 482 * * arg0, pointer to null-terminated string of the
1c1b40c1 483 * command line.
f296c0d1 484 * * arg1, length of the string pointed to by arg0.
1c1b40c1 485 */
579a97f7 486
1c1b40c1 487 char *output_buffer;
f296c0d1 488 size_t input_size;
1c1b40c1
CV
489 size_t output_size;
490 int status = 0;
f3c2bda2
LI
491#if !defined(CONFIG_USER_ONLY)
492 const char *cmdline;
493#endif
f296c0d1
PM
494 GET_ARG(0);
495 GET_ARG(1);
496 input_size = arg1;
1c1b40c1
CV
497 /* Compute the size of the output string. */
498#if !defined(CONFIG_USER_ONLY)
f3c2bda2
LI
499 cmdline = semihosting_get_cmdline();
500 if (cmdline == NULL) {
501 cmdline = ""; /* Default to an empty line. */
502 }
503 output_size = strlen(cmdline) + 1; /* Count terminating 0. */
1c1b40c1
CV
504#else
505 unsigned int i;
38d0662a 506
1c1b40c1
CV
507 output_size = ts->info->arg_end - ts->info->arg_start;
508 if (!output_size) {
4cb28db9
AB
509 /*
510 * We special-case the "empty command line" case (argc==0).
511 * Just provide the terminating 0.
512 */
1c1b40c1
CV
513 output_size = 1;
514 }
515#endif
38d0662a 516
1c1b40c1 517 if (output_size > input_size) {
4cb28db9 518 /* Not enough space to store command-line arguments. */
1c1b40c1 519 return -1;
38d0662a 520 }
38d0662a 521
1c1b40c1 522 /* Adjust the command-line length. */
f296c0d1
PM
523 if (SET_ARG(1, output_size - 1)) {
524 /* Couldn't write back to argument block */
525 return -1;
526 }
38d0662a 527
1c1b40c1 528 /* Lock the buffer on the ARM side. */
f296c0d1 529 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
1c1b40c1
CV
530 if (!output_buffer) {
531 return -1;
532 }
38d0662a 533
1c1b40c1
CV
534 /* Copy the command-line arguments. */
535#if !defined(CONFIG_USER_ONLY)
f3c2bda2 536 pstrcpy(output_buffer, output_size, cmdline);
1c1b40c1
CV
537#else
538 if (output_size == 1) {
539 /* Empty command-line. */
540 output_buffer[0] = '\0';
541 goto out;
542 }
2e8785ac 543
1c1b40c1
CV
544 if (copy_from_user(output_buffer, ts->info->arg_start,
545 output_size)) {
546 status = -1;
547 goto out;
2e8785ac
WS
548 }
549
1c1b40c1
CV
550 /* Separate arguments by white spaces. */
551 for (i = 0; i < output_size - 1; i++) {
552 if (output_buffer[i] == 0) {
553 output_buffer[i] = ' ';
554 }
555 }
556 out:
557#endif
558 /* Unlock the buffer on the ARM side. */
f296c0d1 559 unlock_user(output_buffer, arg0, output_size);
2e8785ac 560
1c1b40c1 561 return status;
38d0662a 562 }
3881725c 563 case TARGET_SYS_HEAPINFO:
a4f81979 564 {
f5666418 565 target_ulong retvals[4];
90e26f5a 566 target_ulong limit;
f5666418
PM
567 int i;
568
f296c0d1 569 GET_ARG(0);
a4f81979 570
8e71621f 571#ifdef CONFIG_USER_ONLY
4cb28db9
AB
572 /*
573 * Some C libraries assume the heap immediately follows .bss, so
574 * allocate it using sbrk.
575 */
a4f81979 576 if (!ts->heap_limit) {
206ae74a 577 abi_ulong ret;
a4f81979 578
53a5960a 579 ts->heap_base = do_brk(0);
a4f81979
FB
580 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
581 /* Try a big heap, and reduce the size if that fails. */
582 for (;;) {
53a5960a 583 ret = do_brk(limit);
206ae74a 584 if (ret >= limit) {
a4f81979 585 break;
206ae74a 586 }
a4f81979
FB
587 limit = (ts->heap_base >> 1) + (limit >> 1);
588 }
589 ts->heap_limit = limit;
590 }
3b46e624 591
f5666418
PM
592 retvals[0] = ts->heap_base;
593 retvals[1] = ts->heap_limit;
594 retvals[2] = ts->stack_base;
595 retvals[3] = 0; /* Stack limit. */
8e71621f
PB
596#else
597 limit = ram_size;
8e71621f 598 /* TODO: Make this use the limit of the loaded application. */
f5666418
PM
599 retvals[0] = limit / 2;
600 retvals[1] = limit;
601 retvals[2] = limit; /* Stack base */
602 retvals[3] = 0; /* Stack limit. */
8e71621f 603#endif
f5666418
PM
604
605 for (i = 0; i < ARRAY_SIZE(retvals); i++) {
606 bool fail;
607
608 if (is_a64(env)) {
609 fail = put_user_u64(retvals[i], arg0 + i * 8);
610 } else {
611 fail = put_user_u32(retvals[i], arg0 + i * 4);
612 }
613
614 if (fail) {
615 /* Couldn't write back to argument block */
616 return -1;
617 }
618 }
a4f81979
FB
619 return 0;
620 }
3881725c 621 case TARGET_SYS_EXIT:
7446d35e 622 if (is_a64(env)) {
4cb28db9
AB
623 /*
624 * The A64 version of this call takes a parameter block,
7446d35e
PM
625 * so the application-exit type can return a subcode which
626 * is the exit status code from the application.
627 */
628 GET_ARG(0);
629 GET_ARG(1);
630
631 if (arg0 == ADP_Stopped_ApplicationExit) {
632 ret = arg1;
633 } else {
634 ret = 1;
635 }
636 } else {
4cb28db9
AB
637 /*
638 * ARM specifies only Stopped_ApplicationExit as normal
639 * exit, everything else is considered an error
640 */
7446d35e
PM
641 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
642 }
1ecc3a2d
LI
643 gdb_exit(env, ret);
644 exit(ret);
e9ebfbfc 645 case TARGET_SYS_SYNCCACHE:
4cb28db9
AB
646 /*
647 * Clean the D-cache and invalidate the I-cache for the specified
e9ebfbfc
PM
648 * virtual address range. This is a nop for us since we don't
649 * implement caches. This is only present on A64.
650 */
651 if (is_a64(env)) {
652 return 0;
653 }
654 /* fall through -- invalid for A32/T32 */
a4f81979
FB
655 default:
656 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
90c84c56 657 cpu_dump_state(cs, stderr, 0);
a4f81979
FB
658 abort();
659 }
660}