]> git.proxmox.com Git - mirror_qemu.git/blame - arm-semi.c
gdbstub: Return appropriate watch message to gdb (Jan Kiszka)
[mirror_qemu.git] / 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <unistd.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <time.h>
29
8e71621f
PB
30#include "cpu.h"
31#ifdef CONFIG_USER_ONLY
a4f81979
FB
32#include "qemu.h"
33
34#define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
8e71621f 35#else
87ecb68b
PB
36#include "qemu-common.h"
37#include "sysemu.h"
38#include "gdbstub.h"
8e71621f 39#endif
a4f81979
FB
40
41#define SYS_OPEN 0x01
42#define SYS_CLOSE 0x02
43#define SYS_WRITEC 0x03
44#define SYS_WRITE0 0x04
45#define SYS_WRITE 0x05
46#define SYS_READ 0x06
47#define SYS_READC 0x07
48#define SYS_ISTTY 0x09
49#define SYS_SEEK 0x0a
50#define SYS_FLEN 0x0c
51#define SYS_TMPNAM 0x0d
52#define SYS_REMOVE 0x0e
53#define SYS_RENAME 0x0f
54#define SYS_CLOCK 0x10
55#define SYS_TIME 0x11
56#define SYS_SYSTEM 0x12
57#define SYS_ERRNO 0x13
58#define SYS_GET_CMDLINE 0x15
59#define SYS_HEAPINFO 0x16
60#define SYS_EXIT 0x18
61
62#ifndef O_BINARY
63#define O_BINARY 0
64#endif
65
a2d1ebaf
PB
66#define GDB_O_RDONLY 0x000
67#define GDB_O_WRONLY 0x001
68#define GDB_O_RDWR 0x002
69#define GDB_O_APPEND 0x008
70#define GDB_O_CREAT 0x200
71#define GDB_O_TRUNC 0x400
72#define GDB_O_BINARY 0
73
74static int gdb_open_modeflags[12] = {
75 GDB_O_RDONLY,
76 GDB_O_RDONLY | GDB_O_BINARY,
77 GDB_O_RDWR,
78 GDB_O_RDWR | GDB_O_BINARY,
79 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
80 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
81 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
82 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
83 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
84 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
85 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
86 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
87};
88
89static int open_modeflags[12] = {
a4f81979
FB
90 O_RDONLY,
91 O_RDONLY | O_BINARY,
92 O_RDWR,
93 O_RDWR | O_BINARY,
94 O_WRONLY | O_CREAT | O_TRUNC,
95 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
96 O_RDWR | O_CREAT | O_TRUNC,
97 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
98 O_WRONLY | O_CREAT | O_APPEND,
99 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
100 O_RDWR | O_CREAT | O_APPEND,
101 O_RDWR | O_CREAT | O_APPEND | O_BINARY
102};
103
8e71621f 104#ifdef CONFIG_USER_ONLY
a4f81979
FB
105static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
106{
8e71621f
PB
107 if (code == (uint32_t)-1)
108 ts->swi_errno = errno;
109 return code;
110}
111#else
112static inline uint32_t set_swi_errno(CPUState *env, uint32_t code)
113{
114 return code;
115}
116
a87295e8 117#include "softmmu-semi.h"
8e71621f 118#endif
a4f81979 119
a2d1ebaf
PB
120static target_ulong arm_semi_syscall_len;
121
33d9cc8a
PB
122#if !defined(CONFIG_USER_ONLY)
123static target_ulong syscall_err;
124#endif
125
a2d1ebaf
PB
126static void arm_semi_cb(CPUState *env, target_ulong ret, target_ulong err)
127{
128#ifdef CONFIG_USER_ONLY
129 TaskState *ts = env->opaque;
130#endif
33d9cc8a 131
a2d1ebaf
PB
132 if (ret == (target_ulong)-1) {
133#ifdef CONFIG_USER_ONLY
134 ts->swi_errno = err;
33d9cc8a
PB
135#else
136 syscall_err = err;
a2d1ebaf
PB
137#endif
138 env->regs[0] = ret;
139 } else {
140 /* Fixup syscalls that use nonstardard return conventions. */
141 switch (env->regs[0]) {
142 case SYS_WRITE:
143 case SYS_READ:
144 env->regs[0] = arm_semi_syscall_len - ret;
145 break;
146 case SYS_SEEK:
147 env->regs[0] = 0;
148 break;
149 default:
150 env->regs[0] = ret;
151 break;
152 }
153 }
154}
155
33d9cc8a
PB
156static void arm_semi_flen_cb(CPUState *env, target_ulong ret, target_ulong err)
157{
158 /* The size is always stored in big-endian order, extract
159 the value. We assume the size always fit in 32 bits. */
160 uint32_t size;
161 cpu_memory_rw_debug(env, env->regs[13]-64+32, (uint8_t *)&size, 4, 0);
162 env->regs[0] = be32_to_cpu(size);
163#ifdef CONFIG_USER_ONLY
164 ((TaskState *)env->opaque)->swi_errno = err;
165#else
166 syscall_err = err;
167#endif
168}
169
2f619698
FB
170#define ARG(n) \
171({ \
172 target_ulong __arg; \
173 /* FIXME - handle get_user() failure */ \
174 get_user_ual(__arg, args + (n) * 4); \
175 __arg; \
176})
177#define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
a4f81979
FB
178uint32_t do_arm_semihosting(CPUState *env)
179{
53a5960a 180 target_ulong args;
a4f81979
FB
181 char * s;
182 int nr;
183 uint32_t ret;
8e71621f
PB
184 uint32_t len;
185#ifdef CONFIG_USER_ONLY
a4f81979 186 TaskState *ts = env->opaque;
8e71621f
PB
187#else
188 CPUState *ts = env;
189#endif
a4f81979
FB
190
191 nr = env->regs[0];
53a5960a 192 args = env->regs[1];
a4f81979
FB
193 switch (nr) {
194 case SYS_OPEN:
579a97f7
FB
195 if (!(s = lock_user_string(ARG(0))))
196 /* FIXME - should this error code be -TARGET_EFAULT ? */
197 return (uint32_t)-1;
a4f81979 198 if (ARG(1) >= 12)
579a97f7 199 return (uint32_t)-1;
a4f81979
FB
200 if (strcmp(s, ":tt") == 0) {
201 if (ARG(1) < 4)
202 return STDIN_FILENO;
203 else
204 return STDOUT_FILENO;
205 }
a2d1ebaf 206 if (use_gdb_syscalls()) {
5fafdf24 207 gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0),
33d9cc8a 208 (int)ARG(2)+1, gdb_open_modeflags[ARG(1)]);
a2d1ebaf
PB
209 return env->regs[0];
210 } else {
211 ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644));
212 }
8e71621f
PB
213 unlock_user(s, ARG(0), 0);
214 return ret;
a4f81979 215 case SYS_CLOSE:
a2d1ebaf
PB
216 if (use_gdb_syscalls()) {
217 gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0));
218 return env->regs[0];
219 } else {
220 return set_swi_errno(ts, close(ARG(0)));
221 }
a4f81979 222 case SYS_WRITEC:
53a5960a 223 {
2f619698
FB
224 char c;
225
226 if (get_user_u8(c, args))
227 /* FIXME - should this error code be -TARGET_EFAULT ? */
228 return (uint32_t)-1;
53a5960a 229 /* Write to debug console. stderr is near enough. */
a2d1ebaf
PB
230 if (use_gdb_syscalls()) {
231 gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args);
232 return env->regs[0];
233 } else {
234 return write(STDERR_FILENO, &c, 1);
235 }
53a5960a 236 }
a4f81979 237 case SYS_WRITE0:
579a97f7
FB
238 if (!(s = lock_user_string(args)))
239 /* FIXME - should this error code be -TARGET_EFAULT ? */
240 return (uint32_t)-1;
a2d1ebaf
PB
241 len = strlen(s);
242 if (use_gdb_syscalls()) {
243 gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len);
244 ret = env->regs[0];
245 } else {
246 ret = write(STDERR_FILENO, s, len);
247 }
53a5960a
PB
248 unlock_user(s, args, 0);
249 return ret;
a4f81979 250 case SYS_WRITE:
8e71621f 251 len = ARG(2);
a2d1ebaf
PB
252 if (use_gdb_syscalls()) {
253 arm_semi_syscall_len = len;
254 gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len);
255 return env->regs[0];
256 } else {
579a97f7
FB
257 if (!(s = lock_user(VERIFY_READ, ARG(1), len, 1)))
258 /* FIXME - should this error code be -TARGET_EFAULT ? */
259 return (uint32_t)-1;
a2d1ebaf
PB
260 ret = set_swi_errno(ts, write(ARG(0), s, len));
261 unlock_user(s, ARG(1), 0);
262 if (ret == (uint32_t)-1)
263 return -1;
264 return len - ret;
265 }
a4f81979 266 case SYS_READ:
8e71621f 267 len = ARG(2);
a2d1ebaf
PB
268 if (use_gdb_syscalls()) {
269 arm_semi_syscall_len = len;
270 gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len);
271 return env->regs[0];
272 } else {
579a97f7
FB
273 if (!(s = lock_user(VERIFY_WRITE, ARG(1), len, 0)))
274 /* FIXME - should this error code be -TARGET_EFAULT ? */
275 return (uint32_t)-1;
a2d1ebaf
PB
276 do
277 ret = set_swi_errno(ts, read(ARG(0), s, len));
278 while (ret == -1 && errno == EINTR);
279 unlock_user(s, ARG(1), len);
280 if (ret == (uint32_t)-1)
281 return -1;
282 return len - ret;
283 }
a4f81979
FB
284 case SYS_READC:
285 /* XXX: Read from debug cosole. Not implemented. */
286 return 0;
287 case SYS_ISTTY:
a2d1ebaf
PB
288 if (use_gdb_syscalls()) {
289 gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0));
290 return env->regs[0];
291 } else {
292 return isatty(ARG(0));
293 }
a4f81979 294 case SYS_SEEK:
a2d1ebaf 295 if (use_gdb_syscalls()) {
33d9cc8a 296 gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", ARG(0), ARG(1));
a2d1ebaf
PB
297 return env->regs[0];
298 } else {
299 ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
300 if (ret == (uint32_t)-1)
301 return -1;
302 return 0;
303 }
a4f81979 304 case SYS_FLEN:
a2d1ebaf 305 if (use_gdb_syscalls()) {
5fafdf24 306 gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x",
33d9cc8a
PB
307 ARG(0), env->regs[13]-64);
308 return env->regs[0];
a2d1ebaf 309 } else {
a4f81979
FB
310 struct stat buf;
311 ret = set_swi_errno(ts, fstat(ARG(0), &buf));
312 if (ret == (uint32_t)-1)
313 return -1;
314 return buf.st_size;
315 }
316 case SYS_TMPNAM:
317 /* XXX: Not implemented. */
318 return -1;
319 case SYS_REMOVE:
a2d1ebaf 320 if (use_gdb_syscalls()) {
33d9cc8a 321 gdb_do_syscall(arm_semi_cb, "unlink,%s", ARG(0), (int)ARG(1)+1);
a2d1ebaf
PB
322 ret = env->regs[0];
323 } else {
579a97f7
FB
324 if (!(s = lock_user_string(ARG(0))))
325 /* FIXME - should this error code be -TARGET_EFAULT ? */
326 return (uint32_t)-1;
a2d1ebaf
PB
327 ret = set_swi_errno(ts, remove(s));
328 unlock_user(s, ARG(0), 0);
329 }
8e71621f 330 return ret;
a4f81979 331 case SYS_RENAME:
a2d1ebaf
PB
332 if (use_gdb_syscalls()) {
333 gdb_do_syscall(arm_semi_cb, "rename,%s,%s",
33d9cc8a 334 ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1);
a2d1ebaf
PB
335 return env->regs[0];
336 } else {
8e71621f
PB
337 char *s2;
338 s = lock_user_string(ARG(0));
339 s2 = lock_user_string(ARG(2));
579a97f7
FB
340 if (!s || !s2)
341 /* FIXME - should this error code be -TARGET_EFAULT ? */
342 ret = (uint32_t)-1;
343 else
344 ret = set_swi_errno(ts, rename(s, s2));
345 if (s2)
346 unlock_user(s2, ARG(2), 0);
347 if (s)
348 unlock_user(s, ARG(0), 0);
8e71621f
PB
349 return ret;
350 }
a4f81979
FB
351 case SYS_CLOCK:
352 return clock() / (CLOCKS_PER_SEC / 100);
353 case SYS_TIME:
354 return set_swi_errno(ts, time(NULL));
355 case SYS_SYSTEM:
a2d1ebaf 356 if (use_gdb_syscalls()) {
33d9cc8a 357 gdb_do_syscall(arm_semi_cb, "system,%s", ARG(0), (int)ARG(1)+1);
a2d1ebaf
PB
358 return env->regs[0];
359 } else {
579a97f7
FB
360 if (!(s = lock_user_string(ARG(0))))
361 /* FIXME - should this error code be -TARGET_EFAULT ? */
362 return (uint32_t)-1;
a2d1ebaf
PB
363 ret = set_swi_errno(ts, system(s));
364 unlock_user(s, ARG(0), 0);
a982b531 365 return ret;
a2d1ebaf 366 }
a4f81979 367 case SYS_ERRNO:
8e71621f 368#ifdef CONFIG_USER_ONLY
a4f81979 369 return ts->swi_errno;
8e71621f 370#else
33d9cc8a 371 return syscall_err;
8e71621f 372#endif
a4f81979 373 case SYS_GET_CMDLINE:
8e71621f 374#ifdef CONFIG_USER_ONLY
38d0662a
PB
375 /* Build a commandline from the original argv. */
376 {
377 char **arg = ts->info->host_argv;
378 int len = ARG(1);
379 /* lock the buffer on the ARM side */
579a97f7
FB
380 char *cmdline_buffer = (char*)lock_user(VERIFY_WRITE, ARG(0), len, 0);
381
382 if (!cmdline_buffer)
383 /* FIXME - should this error code be -TARGET_EFAULT ? */
384 return (uint32_t)-1;
38d0662a
PB
385
386 s = cmdline_buffer;
387 while (*arg && len > 2) {
388 int n = strlen(*arg);
389
390 if (s != cmdline_buffer) {
391 *(s++) = ' ';
392 len--;
393 }
394 if (n >= len)
395 n = len - 1;
396 memcpy(s, *arg, n);
397 s += n;
398 len -= n;
399 arg++;
400 }
401 /* Null terminate the string. */
402 *s = 0;
403 len = s - cmdline_buffer;
404
405 /* Unlock the buffer on the ARM side. */
406 unlock_user(cmdline_buffer, ARG(0), len);
407
408 /* Adjust the commandline length argument. */
409 SET_ARG(1, len);
410
411 /* Return success if commandline fit into buffer. */
412 return *arg ? -1 : 0;
413 }
8e71621f
PB
414#else
415 return -1;
416#endif
a4f81979
FB
417 case SYS_HEAPINFO:
418 {
419 uint32_t *ptr;
420 uint32_t limit;
421
8e71621f
PB
422#ifdef CONFIG_USER_ONLY
423 /* Some C libraries assume the heap immediately follows .bss, so
a4f81979
FB
424 allocate it using sbrk. */
425 if (!ts->heap_limit) {
426 long ret;
427
53a5960a 428 ts->heap_base = do_brk(0);
a4f81979
FB
429 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
430 /* Try a big heap, and reduce the size if that fails. */
431 for (;;) {
53a5960a 432 ret = do_brk(limit);
a4f81979
FB
433 if (ret != -1)
434 break;
435 limit = (ts->heap_base >> 1) + (limit >> 1);
436 }
437 ts->heap_limit = limit;
438 }
3b46e624 439
579a97f7
FB
440 if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
441 /* FIXME - should this error code be -TARGET_EFAULT ? */
442 return (uint32_t)-1;
a4f81979
FB
443 ptr[0] = tswap32(ts->heap_base);
444 ptr[1] = tswap32(ts->heap_limit);
445 ptr[2] = tswap32(ts->stack_base);
446 ptr[3] = tswap32(0); /* Stack limit. */
8e71621f
PB
447 unlock_user(ptr, ARG(0), 16);
448#else
449 limit = ram_size;
579a97f7
FB
450 if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
451 /* FIXME - should this error code be -TARGET_EFAULT ? */
452 return (uint32_t)-1;
8e71621f
PB
453 /* TODO: Make this use the limit of the loaded application. */
454 ptr[0] = tswap32(limit / 2);
455 ptr[1] = tswap32(limit);
456 ptr[2] = tswap32(limit); /* Stack base */
457 ptr[3] = tswap32(0); /* Stack limit. */
458 unlock_user(ptr, ARG(0), 16);
459#endif
a4f81979
FB
460 return 0;
461 }
462 case SYS_EXIT:
463 exit(0);
464 default:
465 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
466 cpu_dump_state(env, stderr, fprintf, 0);
467 abort();
468 }
469}