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