blob: e9e6f8993f11bc630e3b9e8825caea068c7065f0 [file] [log] [blame]
bellarda4f81972005-04-23 18:25:41 +00001/*
2 * Arm "Angel" semihosting syscalls
ths5fafdf22007-09-16 21:08:06 +00003 *
pbrook8e716212007-01-20 17:12:09 +00004 * Copyright (c) 2005, 2007 CodeSourcery.
5 * Written by Paul Brook.
bellarda4f81972005-04-23 18:25:41 +00006 *
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
Blue Swirl8167ee82009-07-16 20:47:01 +000018 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellarda4f81972005-04-23 18:25:41 +000019 */
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <unistd.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <time.h>
28
pbrook8e716212007-01-20 17:12:09 +000029#include "cpu.h"
30#ifdef CONFIG_USER_ONLY
bellarda4f81972005-04-23 18:25:41 +000031#include "qemu.h"
32
33#define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
pbrook8e716212007-01-20 17:12:09 +000034#else
pbrook87ecb682007-11-17 17:14:51 +000035#include "qemu-common.h"
pbrook87ecb682007-11-17 17:14:51 +000036#include "gdbstub.h"
pbrook8e716212007-01-20 17:12:09 +000037#endif
bellarda4f81972005-04-23 18:25:41 +000038
39#define SYS_OPEN 0x01
40#define SYS_CLOSE 0x02
41#define SYS_WRITEC 0x03
42#define SYS_WRITE0 0x04
43#define SYS_WRITE 0x05
44#define SYS_READ 0x06
45#define SYS_READC 0x07
46#define SYS_ISTTY 0x09
47#define SYS_SEEK 0x0a
48#define SYS_FLEN 0x0c
49#define SYS_TMPNAM 0x0d
50#define SYS_REMOVE 0x0e
51#define SYS_RENAME 0x0f
52#define SYS_CLOCK 0x10
53#define SYS_TIME 0x11
54#define SYS_SYSTEM 0x12
55#define SYS_ERRNO 0x13
56#define SYS_GET_CMDLINE 0x15
57#define SYS_HEAPINFO 0x16
58#define SYS_EXIT 0x18
59
60#ifndef O_BINARY
61#define O_BINARY 0
62#endif
63
pbrooka2d1eba2007-01-28 03:10:55 +000064#define GDB_O_RDONLY 0x000
65#define GDB_O_WRONLY 0x001
66#define GDB_O_RDWR 0x002
67#define GDB_O_APPEND 0x008
68#define GDB_O_CREAT 0x200
69#define GDB_O_TRUNC 0x400
70#define GDB_O_BINARY 0
71
72static int gdb_open_modeflags[12] = {
73 GDB_O_RDONLY,
74 GDB_O_RDONLY | GDB_O_BINARY,
75 GDB_O_RDWR,
76 GDB_O_RDWR | GDB_O_BINARY,
77 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
78 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
79 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
80 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
81 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
82 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
83 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
84 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
85};
86
87static int open_modeflags[12] = {
bellarda4f81972005-04-23 18:25:41 +000088 O_RDONLY,
89 O_RDONLY | O_BINARY,
90 O_RDWR,
91 O_RDWR | O_BINARY,
92 O_WRONLY | O_CREAT | O_TRUNC,
93 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
94 O_RDWR | O_CREAT | O_TRUNC,
95 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
96 O_WRONLY | O_CREAT | O_APPEND,
97 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
98 O_RDWR | O_CREAT | O_APPEND,
99 O_RDWR | O_CREAT | O_APPEND | O_BINARY
100};
101
pbrook8e716212007-01-20 17:12:09 +0000102#ifdef CONFIG_USER_ONLY
bellarda4f81972005-04-23 18:25:41 +0000103static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
104{
pbrook8e716212007-01-20 17:12:09 +0000105 if (code == (uint32_t)-1)
106 ts->swi_errno = errno;
107 return code;
bellarda4f81972005-04-23 18:25:41 +0000108}
pbrook8e716212007-01-20 17:12:09 +0000109#else
110static inline uint32_t set_swi_errno(CPUState *env, uint32_t code)
111{
112 return code;
113}
114
pbrooka87295e2007-05-26 15:09:38 +0000115#include "softmmu-semi.h"
pbrook8e716212007-01-20 17:12:09 +0000116#endif
bellarda4f81972005-04-23 18:25:41 +0000117
pbrooka2d1eba2007-01-28 03:10:55 +0000118static target_ulong arm_semi_syscall_len;
119
pbrook33d9cc82007-06-09 14:44:00 +0000120#if !defined(CONFIG_USER_ONLY)
121static target_ulong syscall_err;
122#endif
123
pbrooka2d1eba2007-01-28 03:10:55 +0000124static void arm_semi_cb(CPUState *env, target_ulong ret, target_ulong err)
125{
126#ifdef CONFIG_USER_ONLY
127 TaskState *ts = env->opaque;
128#endif
pbrook33d9cc82007-06-09 14:44:00 +0000129
pbrooka2d1eba2007-01-28 03:10:55 +0000130 if (ret == (target_ulong)-1) {
131#ifdef CONFIG_USER_ONLY
132 ts->swi_errno = err;
pbrook33d9cc82007-06-09 14:44:00 +0000133#else
134 syscall_err = err;
pbrooka2d1eba2007-01-28 03:10:55 +0000135#endif
136 env->regs[0] = ret;
137 } else {
138 /* Fixup syscalls that use nonstardard return conventions. */
139 switch (env->regs[0]) {
140 case SYS_WRITE:
141 case SYS_READ:
142 env->regs[0] = arm_semi_syscall_len - ret;
143 break;
144 case SYS_SEEK:
145 env->regs[0] = 0;
146 break;
147 default:
148 env->regs[0] = ret;
149 break;
150 }
151 }
152}
153
pbrook33d9cc82007-06-09 14:44:00 +0000154static void arm_semi_flen_cb(CPUState *env, target_ulong ret, target_ulong err)
155{
156 /* The size is always stored in big-endian order, extract
157 the value. We assume the size always fit in 32 bits. */
158 uint32_t size;
159 cpu_memory_rw_debug(env, env->regs[13]-64+32, (uint8_t *)&size, 4, 0);
160 env->regs[0] = be32_to_cpu(size);
161#ifdef CONFIG_USER_ONLY
162 ((TaskState *)env->opaque)->swi_errno = err;
163#else
164 syscall_err = err;
165#endif
166}
167
bellard2f619692007-11-16 10:46:05 +0000168#define ARG(n) \
169({ \
170 target_ulong __arg; \
171 /* FIXME - handle get_user() failure */ \
172 get_user_ual(__arg, args + (n) * 4); \
173 __arg; \
174})
175#define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
bellarda4f81972005-04-23 18:25:41 +0000176uint32_t do_arm_semihosting(CPUState *env)
177{
pbrook53a59602006-03-25 19:31:22 +0000178 target_ulong args;
bellarda4f81972005-04-23 18:25:41 +0000179 char * s;
180 int nr;
181 uint32_t ret;
pbrook8e716212007-01-20 17:12:09 +0000182 uint32_t len;
183#ifdef CONFIG_USER_ONLY
bellarda4f81972005-04-23 18:25:41 +0000184 TaskState *ts = env->opaque;
pbrook8e716212007-01-20 17:12:09 +0000185#else
186 CPUState *ts = env;
187#endif
bellarda4f81972005-04-23 18:25:41 +0000188
189 nr = env->regs[0];
pbrook53a59602006-03-25 19:31:22 +0000190 args = env->regs[1];
bellarda4f81972005-04-23 18:25:41 +0000191 switch (nr) {
192 case SYS_OPEN:
bellard579a97f2007-11-11 14:26:47 +0000193 if (!(s = lock_user_string(ARG(0))))
194 /* FIXME - should this error code be -TARGET_EFAULT ? */
195 return (uint32_t)-1;
bellarda4f81972005-04-23 18:25:41 +0000196 if (ARG(1) >= 12)
bellard579a97f2007-11-11 14:26:47 +0000197 return (uint32_t)-1;
bellarda4f81972005-04-23 18:25:41 +0000198 if (strcmp(s, ":tt") == 0) {
199 if (ARG(1) < 4)
200 return STDIN_FILENO;
201 else
202 return STDOUT_FILENO;
203 }
pbrooka2d1eba2007-01-28 03:10:55 +0000204 if (use_gdb_syscalls()) {
ths5fafdf22007-09-16 21:08:06 +0000205 gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0),
pbrook33d9cc82007-06-09 14:44:00 +0000206 (int)ARG(2)+1, gdb_open_modeflags[ARG(1)]);
pbrooka2d1eba2007-01-28 03:10:55 +0000207 return env->regs[0];
208 } else {
209 ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644));
210 }
pbrook8e716212007-01-20 17:12:09 +0000211 unlock_user(s, ARG(0), 0);
212 return ret;
bellarda4f81972005-04-23 18:25:41 +0000213 case SYS_CLOSE:
pbrooka2d1eba2007-01-28 03:10:55 +0000214 if (use_gdb_syscalls()) {
215 gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0));
216 return env->regs[0];
217 } else {
218 return set_swi_errno(ts, close(ARG(0)));
219 }
bellarda4f81972005-04-23 18:25:41 +0000220 case SYS_WRITEC:
pbrook53a59602006-03-25 19:31:22 +0000221 {
bellard2f619692007-11-16 10:46:05 +0000222 char c;
223
224 if (get_user_u8(c, args))
225 /* FIXME - should this error code be -TARGET_EFAULT ? */
226 return (uint32_t)-1;
pbrook53a59602006-03-25 19:31:22 +0000227 /* Write to debug console. stderr is near enough. */
pbrooka2d1eba2007-01-28 03:10:55 +0000228 if (use_gdb_syscalls()) {
229 gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args);
230 return env->regs[0];
231 } else {
232 return write(STDERR_FILENO, &c, 1);
233 }
pbrook53a59602006-03-25 19:31:22 +0000234 }
bellarda4f81972005-04-23 18:25:41 +0000235 case SYS_WRITE0:
bellard579a97f2007-11-11 14:26:47 +0000236 if (!(s = lock_user_string(args)))
237 /* FIXME - should this error code be -TARGET_EFAULT ? */
238 return (uint32_t)-1;
pbrooka2d1eba2007-01-28 03:10:55 +0000239 len = strlen(s);
240 if (use_gdb_syscalls()) {
241 gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len);
242 ret = env->regs[0];
243 } else {
244 ret = write(STDERR_FILENO, s, len);
245 }
pbrook53a59602006-03-25 19:31:22 +0000246 unlock_user(s, args, 0);
247 return ret;
bellarda4f81972005-04-23 18:25:41 +0000248 case SYS_WRITE:
pbrook8e716212007-01-20 17:12:09 +0000249 len = ARG(2);
pbrooka2d1eba2007-01-28 03:10:55 +0000250 if (use_gdb_syscalls()) {
251 arm_semi_syscall_len = len;
252 gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len);
253 return env->regs[0];
254 } else {
bellard579a97f2007-11-11 14:26:47 +0000255 if (!(s = lock_user(VERIFY_READ, ARG(1), len, 1)))
256 /* FIXME - should this error code be -TARGET_EFAULT ? */
257 return (uint32_t)-1;
pbrooka2d1eba2007-01-28 03:10:55 +0000258 ret = set_swi_errno(ts, write(ARG(0), s, len));
259 unlock_user(s, ARG(1), 0);
260 if (ret == (uint32_t)-1)
261 return -1;
262 return len - ret;
263 }
bellarda4f81972005-04-23 18:25:41 +0000264 case SYS_READ:
pbrook8e716212007-01-20 17:12:09 +0000265 len = ARG(2);
pbrooka2d1eba2007-01-28 03:10:55 +0000266 if (use_gdb_syscalls()) {
267 arm_semi_syscall_len = len;
268 gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len);
269 return env->regs[0];
270 } else {
bellard579a97f2007-11-11 14:26:47 +0000271 if (!(s = lock_user(VERIFY_WRITE, ARG(1), len, 0)))
272 /* FIXME - should this error code be -TARGET_EFAULT ? */
273 return (uint32_t)-1;
pbrooka2d1eba2007-01-28 03:10:55 +0000274 do
275 ret = set_swi_errno(ts, read(ARG(0), s, len));
276 while (ret == -1 && errno == EINTR);
277 unlock_user(s, ARG(1), len);
278 if (ret == (uint32_t)-1)
279 return -1;
280 return len - ret;
281 }
bellarda4f81972005-04-23 18:25:41 +0000282 case SYS_READC:
283 /* XXX: Read from debug cosole. Not implemented. */
284 return 0;
285 case SYS_ISTTY:
pbrooka2d1eba2007-01-28 03:10:55 +0000286 if (use_gdb_syscalls()) {
287 gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0));
288 return env->regs[0];
289 } else {
290 return isatty(ARG(0));
291 }
bellarda4f81972005-04-23 18:25:41 +0000292 case SYS_SEEK:
pbrooka2d1eba2007-01-28 03:10:55 +0000293 if (use_gdb_syscalls()) {
pbrook33d9cc82007-06-09 14:44:00 +0000294 gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", ARG(0), ARG(1));
pbrooka2d1eba2007-01-28 03:10:55 +0000295 return env->regs[0];
296 } else {
297 ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
298 if (ret == (uint32_t)-1)
299 return -1;
300 return 0;
301 }
bellarda4f81972005-04-23 18:25:41 +0000302 case SYS_FLEN:
pbrooka2d1eba2007-01-28 03:10:55 +0000303 if (use_gdb_syscalls()) {
ths5fafdf22007-09-16 21:08:06 +0000304 gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x",
pbrook33d9cc82007-06-09 14:44:00 +0000305 ARG(0), env->regs[13]-64);
306 return env->regs[0];
pbrooka2d1eba2007-01-28 03:10:55 +0000307 } else {
bellarda4f81972005-04-23 18:25:41 +0000308 struct stat buf;
309 ret = set_swi_errno(ts, fstat(ARG(0), &buf));
310 if (ret == (uint32_t)-1)
311 return -1;
312 return buf.st_size;
313 }
314 case SYS_TMPNAM:
315 /* XXX: Not implemented. */
316 return -1;
317 case SYS_REMOVE:
pbrooka2d1eba2007-01-28 03:10:55 +0000318 if (use_gdb_syscalls()) {
pbrook33d9cc82007-06-09 14:44:00 +0000319 gdb_do_syscall(arm_semi_cb, "unlink,%s", ARG(0), (int)ARG(1)+1);
pbrooka2d1eba2007-01-28 03:10:55 +0000320 ret = env->regs[0];
321 } else {
bellard579a97f2007-11-11 14:26:47 +0000322 if (!(s = lock_user_string(ARG(0))))
323 /* FIXME - should this error code be -TARGET_EFAULT ? */
324 return (uint32_t)-1;
pbrooka2d1eba2007-01-28 03:10:55 +0000325 ret = set_swi_errno(ts, remove(s));
326 unlock_user(s, ARG(0), 0);
327 }
pbrook8e716212007-01-20 17:12:09 +0000328 return ret;
bellarda4f81972005-04-23 18:25:41 +0000329 case SYS_RENAME:
pbrooka2d1eba2007-01-28 03:10:55 +0000330 if (use_gdb_syscalls()) {
331 gdb_do_syscall(arm_semi_cb, "rename,%s,%s",
pbrook33d9cc82007-06-09 14:44:00 +0000332 ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1);
pbrooka2d1eba2007-01-28 03:10:55 +0000333 return env->regs[0];
334 } else {
pbrook8e716212007-01-20 17:12:09 +0000335 char *s2;
336 s = lock_user_string(ARG(0));
337 s2 = lock_user_string(ARG(2));
bellard579a97f2007-11-11 14:26:47 +0000338 if (!s || !s2)
339 /* FIXME - should this error code be -TARGET_EFAULT ? */
340 ret = (uint32_t)-1;
341 else
342 ret = set_swi_errno(ts, rename(s, s2));
343 if (s2)
344 unlock_user(s2, ARG(2), 0);
345 if (s)
346 unlock_user(s, ARG(0), 0);
pbrook8e716212007-01-20 17:12:09 +0000347 return ret;
348 }
bellarda4f81972005-04-23 18:25:41 +0000349 case SYS_CLOCK:
350 return clock() / (CLOCKS_PER_SEC / 100);
351 case SYS_TIME:
352 return set_swi_errno(ts, time(NULL));
353 case SYS_SYSTEM:
pbrooka2d1eba2007-01-28 03:10:55 +0000354 if (use_gdb_syscalls()) {
pbrook33d9cc82007-06-09 14:44:00 +0000355 gdb_do_syscall(arm_semi_cb, "system,%s", ARG(0), (int)ARG(1)+1);
pbrooka2d1eba2007-01-28 03:10:55 +0000356 return env->regs[0];
357 } else {
bellard579a97f2007-11-11 14:26:47 +0000358 if (!(s = lock_user_string(ARG(0))))
359 /* FIXME - should this error code be -TARGET_EFAULT ? */
360 return (uint32_t)-1;
pbrooka2d1eba2007-01-28 03:10:55 +0000361 ret = set_swi_errno(ts, system(s));
362 unlock_user(s, ARG(0), 0);
thsa982b532008-07-01 16:40:04 +0000363 return ret;
pbrooka2d1eba2007-01-28 03:10:55 +0000364 }
bellarda4f81972005-04-23 18:25:41 +0000365 case SYS_ERRNO:
pbrook8e716212007-01-20 17:12:09 +0000366#ifdef CONFIG_USER_ONLY
bellarda4f81972005-04-23 18:25:41 +0000367 return ts->swi_errno;
pbrook8e716212007-01-20 17:12:09 +0000368#else
pbrook33d9cc82007-06-09 14:44:00 +0000369 return syscall_err;
pbrook8e716212007-01-20 17:12:09 +0000370#endif
bellarda4f81972005-04-23 18:25:41 +0000371 case SYS_GET_CMDLINE:
pbrook8e716212007-01-20 17:12:09 +0000372#ifdef CONFIG_USER_ONLY
pbrook38d06622006-11-19 20:29:35 +0000373 /* Build a commandline from the original argv. */
374 {
Wolfgang Schildbach2e8785a2010-12-06 15:06:05 +0000375 char *arm_cmdline_buffer;
376 const char *host_cmdline_buffer;
bellard579a97f2007-11-11 14:26:47 +0000377
Wolfgang Schildbach2e8785a2010-12-06 15:06:05 +0000378 unsigned int i;
379 unsigned int arm_cmdline_len = ARG(1);
380 unsigned int host_cmdline_len =
381 ts->info->arg_end-ts->info->arg_start;
pbrook38d06622006-11-19 20:29:35 +0000382
Wolfgang Schildbach2e8785a2010-12-06 15:06:05 +0000383 if (!arm_cmdline_len || host_cmdline_len > arm_cmdline_len) {
384 return -1; /* not enough space to store command line */
pbrook38d06622006-11-19 20:29:35 +0000385 }
pbrook38d06622006-11-19 20:29:35 +0000386
Wolfgang Schildbach2e8785a2010-12-06 15:06:05 +0000387 if (!host_cmdline_len) {
388 /* We special-case the "empty command line" case (argc==0).
389 Just provide the terminating 0. */
390 arm_cmdline_buffer = lock_user(VERIFY_WRITE, ARG(0), 1, 0);
391 arm_cmdline_buffer[0] = 0;
392 unlock_user(arm_cmdline_buffer, ARG(0), 1);
pbrook38d06622006-11-19 20:29:35 +0000393
Wolfgang Schildbach2e8785a2010-12-06 15:06:05 +0000394 /* Adjust the commandline length argument. */
395 SET_ARG(1, 0);
396 return 0;
397 }
pbrook38d06622006-11-19 20:29:35 +0000398
Wolfgang Schildbach2e8785a2010-12-06 15:06:05 +0000399 /* lock the buffers on the ARM side */
400 arm_cmdline_buffer =
401 lock_user(VERIFY_WRITE, ARG(0), host_cmdline_len, 0);
402 host_cmdline_buffer =
403 lock_user(VERIFY_READ, ts->info->arg_start,
404 host_cmdline_len, 1);
405
406 if (arm_cmdline_buffer && host_cmdline_buffer)
407 {
408 /* the last argument is zero-terminated;
409 no need for additional termination */
410 memcpy(arm_cmdline_buffer, host_cmdline_buffer,
411 host_cmdline_len);
412
413 /* separate arguments by white spaces */
414 for (i = 0; i < host_cmdline_len-1; i++) {
415 if (arm_cmdline_buffer[i] == 0) {
416 arm_cmdline_buffer[i] = ' ';
417 }
418 }
419
420 /* Adjust the commandline length argument. */
421 SET_ARG(1, host_cmdline_len-1);
422 }
423
424 /* Unlock the buffers on the ARM side. */
425 unlock_user(arm_cmdline_buffer, ARG(0), host_cmdline_len);
426 unlock_user((void*)host_cmdline_buffer, ts->info->arg_start, 0);
427
428 /* Return success if we could return a commandline. */
429 return (arm_cmdline_buffer && host_cmdline_buffer) ? 0 : -1;
pbrook38d06622006-11-19 20:29:35 +0000430 }
pbrook8e716212007-01-20 17:12:09 +0000431#else
Wolfgang Schildbach2e8785a2010-12-06 15:06:05 +0000432 return -1;
pbrook8e716212007-01-20 17:12:09 +0000433#endif
bellarda4f81972005-04-23 18:25:41 +0000434 case SYS_HEAPINFO:
435 {
436 uint32_t *ptr;
437 uint32_t limit;
438
pbrook8e716212007-01-20 17:12:09 +0000439#ifdef CONFIG_USER_ONLY
440 /* Some C libraries assume the heap immediately follows .bss, so
bellarda4f81972005-04-23 18:25:41 +0000441 allocate it using sbrk. */
442 if (!ts->heap_limit) {
443 long ret;
444
pbrook53a59602006-03-25 19:31:22 +0000445 ts->heap_base = do_brk(0);
bellarda4f81972005-04-23 18:25:41 +0000446 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
447 /* Try a big heap, and reduce the size if that fails. */
448 for (;;) {
pbrook53a59602006-03-25 19:31:22 +0000449 ret = do_brk(limit);
bellarda4f81972005-04-23 18:25:41 +0000450 if (ret != -1)
451 break;
452 limit = (ts->heap_base >> 1) + (limit >> 1);
453 }
454 ts->heap_limit = limit;
455 }
ths3b46e622007-09-17 08:09:54 +0000456
bellard579a97f2007-11-11 14:26:47 +0000457 if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
458 /* FIXME - should this error code be -TARGET_EFAULT ? */
459 return (uint32_t)-1;
bellarda4f81972005-04-23 18:25:41 +0000460 ptr[0] = tswap32(ts->heap_base);
461 ptr[1] = tswap32(ts->heap_limit);
462 ptr[2] = tswap32(ts->stack_base);
463 ptr[3] = tswap32(0); /* Stack limit. */
pbrook8e716212007-01-20 17:12:09 +0000464 unlock_user(ptr, ARG(0), 16);
465#else
466 limit = ram_size;
bellard579a97f2007-11-11 14:26:47 +0000467 if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
468 /* FIXME - should this error code be -TARGET_EFAULT ? */
469 return (uint32_t)-1;
pbrook8e716212007-01-20 17:12:09 +0000470 /* TODO: Make this use the limit of the loaded application. */
471 ptr[0] = tswap32(limit / 2);
472 ptr[1] = tswap32(limit);
473 ptr[2] = tswap32(limit); /* Stack base */
474 ptr[3] = tswap32(0); /* Stack limit. */
475 unlock_user(ptr, ARG(0), 16);
476#endif
bellarda4f81972005-04-23 18:25:41 +0000477 return 0;
478 }
479 case SYS_EXIT:
Paul Brook0e1c9c52010-06-16 13:03:51 +0100480 gdb_exit(env, 0);
bellarda4f81972005-04-23 18:25:41 +0000481 exit(0);
482 default:
483 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
484 cpu_dump_state(env, stderr, fprintf, 0);
485 abort();
486 }
487}