2 * Arm "Angel" semihosting syscalls
4 * Copyright (c) 2005, 2007 CodeSourcery.
5 * Written by Paul Brook.
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.
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.
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.
22 #include <sys/types.h>
31 #ifdef CONFIG_USER_ONLY
34 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
36 #include "qemu-common.h"
42 #define SYS_CLOSE 0x02
43 #define SYS_WRITEC 0x03
44 #define SYS_WRITE0 0x04
45 #define SYS_WRITE 0x05
47 #define SYS_READC 0x07
48 #define SYS_ISTTY 0x09
51 #define SYS_TMPNAM 0x0d
52 #define SYS_REMOVE 0x0e
53 #define SYS_RENAME 0x0f
54 #define SYS_CLOCK 0x10
56 #define SYS_SYSTEM 0x12
57 #define SYS_ERRNO 0x13
58 #define SYS_GET_CMDLINE 0x15
59 #define SYS_HEAPINFO 0x16
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
74 static int gdb_open_modeflags
[12] = {
76 GDB_O_RDONLY
| GDB_O_BINARY
,
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
89 static int open_modeflags
[12] = {
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
104 #ifdef CONFIG_USER_ONLY
105 static inline uint32_t set_swi_errno(TaskState
*ts
, uint32_t code
)
107 if (code
== (uint32_t)-1)
108 ts
->swi_errno
= errno
;
112 static inline uint32_t set_swi_errno(CPUState
*env
, uint32_t code
)
117 #include "softmmu-semi.h"
120 static target_ulong arm_semi_syscall_len
;
122 #if !defined(CONFIG_USER_ONLY)
123 static target_ulong syscall_err
;
126 static void arm_semi_cb(CPUState
*env
, target_ulong ret
, target_ulong err
)
128 #ifdef CONFIG_USER_ONLY
129 TaskState
*ts
= env
->opaque
;
132 if (ret
== (target_ulong
)-1) {
133 #ifdef CONFIG_USER_ONLY
140 /* Fixup syscalls that use nonstardard return conventions. */
141 switch (env
->regs
[0]) {
144 env
->regs
[0] = arm_semi_syscall_len
- ret
;
156 static void arm_semi_flen_cb(CPUState
*env
, target_ulong ret
, target_ulong err
)
158 /* The size is always stored in big-endian order, extract
159 the value. We assume the size always fit in 32 bits. */
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
;
172 target_ulong __arg; \
173 /* FIXME - handle get_user() failure */ \
174 get_user_ual(__arg, args + (n) * 4); \
177 #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
178 uint32_t do_arm_semihosting(CPUState
*env
)
185 #ifdef CONFIG_USER_ONLY
186 TaskState
*ts
= env
->opaque
;
195 if (!(s
= lock_user_string(ARG(0))))
196 /* FIXME - should this error code be -TARGET_EFAULT ? */
200 if (strcmp(s
, ":tt") == 0) {
204 return STDOUT_FILENO
;
206 if (use_gdb_syscalls()) {
207 gdb_do_syscall(arm_semi_cb
, "open,%s,%x,1a4", ARG(0),
208 (int)ARG(2)+1, gdb_open_modeflags
[ARG(1)]);
211 ret
= set_swi_errno(ts
, open(s
, open_modeflags
[ARG(1)], 0644));
213 unlock_user(s
, ARG(0), 0);
216 if (use_gdb_syscalls()) {
217 gdb_do_syscall(arm_semi_cb
, "close,%x", ARG(0));
220 return set_swi_errno(ts
, close(ARG(0)));
226 if (get_user_u8(c
, args
))
227 /* FIXME - should this error code be -TARGET_EFAULT ? */
229 /* Write to debug console. stderr is near enough. */
230 if (use_gdb_syscalls()) {
231 gdb_do_syscall(arm_semi_cb
, "write,2,%x,1", args
);
234 return write(STDERR_FILENO
, &c
, 1);
238 if (!(s
= lock_user_string(args
)))
239 /* FIXME - should this error code be -TARGET_EFAULT ? */
242 if (use_gdb_syscalls()) {
243 gdb_do_syscall(arm_semi_cb
, "write,2,%x,%x\n", args
, len
);
246 ret
= write(STDERR_FILENO
, s
, len
);
248 unlock_user(s
, args
, 0);
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
);
257 if (!(s
= lock_user(VERIFY_READ
, ARG(1), len
, 1)))
258 /* FIXME - should this error code be -TARGET_EFAULT ? */
260 ret
= set_swi_errno(ts
, write(ARG(0), s
, len
));
261 unlock_user(s
, ARG(1), 0);
262 if (ret
== (uint32_t)-1)
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
);
273 if (!(s
= lock_user(VERIFY_WRITE
, ARG(1), len
, 0)))
274 /* FIXME - should this error code be -TARGET_EFAULT ? */
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)
285 /* XXX: Read from debug cosole. Not implemented. */
288 if (use_gdb_syscalls()) {
289 gdb_do_syscall(arm_semi_cb
, "isatty,%x", ARG(0));
292 return isatty(ARG(0));
295 if (use_gdb_syscalls()) {
296 gdb_do_syscall(arm_semi_cb
, "lseek,%x,%x,0", ARG(0), ARG(1));
299 ret
= set_swi_errno(ts
, lseek(ARG(0), ARG(1), SEEK_SET
));
300 if (ret
== (uint32_t)-1)
305 if (use_gdb_syscalls()) {
306 gdb_do_syscall(arm_semi_flen_cb
, "fstat,%x,%x",
307 ARG(0), env
->regs
[13]-64);
311 ret
= set_swi_errno(ts
, fstat(ARG(0), &buf
));
312 if (ret
== (uint32_t)-1)
317 /* XXX: Not implemented. */
320 if (use_gdb_syscalls()) {
321 gdb_do_syscall(arm_semi_cb
, "unlink,%s", ARG(0), (int)ARG(1)+1);
324 if (!(s
= lock_user_string(ARG(0))))
325 /* FIXME - should this error code be -TARGET_EFAULT ? */
327 ret
= set_swi_errno(ts
, remove(s
));
328 unlock_user(s
, ARG(0), 0);
332 if (use_gdb_syscalls()) {
333 gdb_do_syscall(arm_semi_cb
, "rename,%s,%s",
334 ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1);
338 s
= lock_user_string(ARG(0));
339 s2
= lock_user_string(ARG(2));
341 /* FIXME - should this error code be -TARGET_EFAULT ? */
344 ret
= set_swi_errno(ts
, rename(s
, s2
));
346 unlock_user(s2
, ARG(2), 0);
348 unlock_user(s
, ARG(0), 0);
352 return clock() / (CLOCKS_PER_SEC
/ 100);
354 return set_swi_errno(ts
, time(NULL
));
356 if (use_gdb_syscalls()) {
357 gdb_do_syscall(arm_semi_cb
, "system,%s", ARG(0), (int)ARG(1)+1);
360 if (!(s
= lock_user_string(ARG(0))))
361 /* FIXME - should this error code be -TARGET_EFAULT ? */
363 ret
= set_swi_errno(ts
, system(s
));
364 unlock_user(s
, ARG(0), 0);
367 #ifdef CONFIG_USER_ONLY
368 return ts
->swi_errno
;
372 case SYS_GET_CMDLINE
:
373 #ifdef CONFIG_USER_ONLY
374 /* Build a commandline from the original argv. */
376 char **arg
= ts
->info
->host_argv
;
378 /* lock the buffer on the ARM side */
379 char *cmdline_buffer
= (char*)lock_user(VERIFY_WRITE
, ARG(0), len
, 0);
382 /* FIXME - should this error code be -TARGET_EFAULT ? */
386 while (*arg
&& len
> 2) {
387 int n
= strlen(*arg
);
389 if (s
!= cmdline_buffer
) {
400 /* Null terminate the string. */
402 len
= s
- cmdline_buffer
;
404 /* Unlock the buffer on the ARM side. */
405 unlock_user(cmdline_buffer
, ARG(0), len
);
407 /* Adjust the commandline length argument. */
410 /* Return success if commandline fit into buffer. */
411 return *arg
? -1 : 0;
421 #ifdef CONFIG_USER_ONLY
422 /* Some C libraries assume the heap immediately follows .bss, so
423 allocate it using sbrk. */
424 if (!ts
->heap_limit
) {
427 ts
->heap_base
= do_brk(0);
428 limit
= ts
->heap_base
+ ARM_ANGEL_HEAP_SIZE
;
429 /* Try a big heap, and reduce the size if that fails. */
434 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
436 ts
->heap_limit
= limit
;
439 if (!(ptr
= lock_user(VERIFY_WRITE
, ARG(0), 16, 0)))
440 /* FIXME - should this error code be -TARGET_EFAULT ? */
442 ptr
[0] = tswap32(ts
->heap_base
);
443 ptr
[1] = tswap32(ts
->heap_limit
);
444 ptr
[2] = tswap32(ts
->stack_base
);
445 ptr
[3] = tswap32(0); /* Stack limit. */
446 unlock_user(ptr
, ARG(0), 16);
449 if (!(ptr
= lock_user(VERIFY_WRITE
, ARG(0), 16, 0)))
450 /* FIXME - should this error code be -TARGET_EFAULT ? */
452 /* TODO: Make this use the limit of the loaded application. */
453 ptr
[0] = tswap32(limit
/ 2);
454 ptr
[1] = tswap32(limit
);
455 ptr
[2] = tswap32(limit
); /* Stack base */
456 ptr
[3] = tswap32(0); /* Stack limit. */
457 unlock_user(ptr
, ARG(0), 16);
464 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
465 cpu_dump_state(env
, stderr
, fprintf
, 0);