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, see <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
30 #ifdef CONFIG_USER_ONLY
33 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
35 #include "qemu-common.h"
40 #define SYS_CLOSE 0x02
41 #define SYS_WRITEC 0x03
42 #define SYS_WRITE0 0x04
43 #define SYS_WRITE 0x05
45 #define SYS_READC 0x07
46 #define SYS_ISTTY 0x09
49 #define SYS_TMPNAM 0x0d
50 #define SYS_REMOVE 0x0e
51 #define SYS_RENAME 0x0f
52 #define SYS_CLOCK 0x10
54 #define SYS_SYSTEM 0x12
55 #define SYS_ERRNO 0x13
56 #define SYS_GET_CMDLINE 0x15
57 #define SYS_HEAPINFO 0x16
64 #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
72 static int gdb_open_modeflags
[12] = {
74 GDB_O_RDONLY
| GDB_O_BINARY
,
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
87 static int open_modeflags
[12] = {
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
102 #ifdef CONFIG_USER_ONLY
103 static inline uint32_t set_swi_errno(TaskState
*ts
, uint32_t code
)
105 if (code
== (uint32_t)-1)
106 ts
->swi_errno
= errno
;
110 static inline uint32_t set_swi_errno(CPUState
*env
, uint32_t code
)
115 #include "softmmu-semi.h"
118 static target_ulong arm_semi_syscall_len
;
120 #if !defined(CONFIG_USER_ONLY)
121 static target_ulong syscall_err
;
124 static void arm_semi_cb(CPUState
*env
, target_ulong ret
, target_ulong err
)
126 #ifdef CONFIG_USER_ONLY
127 TaskState
*ts
= env
->opaque
;
130 if (ret
== (target_ulong
)-1) {
131 #ifdef CONFIG_USER_ONLY
138 /* Fixup syscalls that use nonstardard return conventions. */
139 switch (env
->regs
[0]) {
142 env
->regs
[0] = arm_semi_syscall_len
- ret
;
154 static void arm_semi_flen_cb(CPUState
*env
, target_ulong ret
, target_ulong err
)
156 /* The size is always stored in big-endian order, extract
157 the value. We assume the size always fit in 32 bits. */
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
;
170 target_ulong __arg; \
171 /* FIXME - handle get_user() failure */ \
172 get_user_ual(__arg, args + (n) * 4); \
175 #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
176 uint32_t do_arm_semihosting(CPUState
*env
)
183 #ifdef CONFIG_USER_ONLY
184 TaskState
*ts
= env
->opaque
;
193 if (!(s
= lock_user_string(ARG(0))))
194 /* FIXME - should this error code be -TARGET_EFAULT ? */
198 if (strcmp(s
, ":tt") == 0) {
202 return STDOUT_FILENO
;
204 if (use_gdb_syscalls()) {
205 gdb_do_syscall(arm_semi_cb
, "open,%s,%x,1a4", ARG(0),
206 (int)ARG(2)+1, gdb_open_modeflags
[ARG(1)]);
209 ret
= set_swi_errno(ts
, open(s
, open_modeflags
[ARG(1)], 0644));
211 unlock_user(s
, ARG(0), 0);
214 if (use_gdb_syscalls()) {
215 gdb_do_syscall(arm_semi_cb
, "close,%x", ARG(0));
218 return set_swi_errno(ts
, close(ARG(0)));
224 if (get_user_u8(c
, args
))
225 /* FIXME - should this error code be -TARGET_EFAULT ? */
227 /* Write to debug console. stderr is near enough. */
228 if (use_gdb_syscalls()) {
229 gdb_do_syscall(arm_semi_cb
, "write,2,%x,1", args
);
232 return write(STDERR_FILENO
, &c
, 1);
236 if (!(s
= lock_user_string(args
)))
237 /* FIXME - should this error code be -TARGET_EFAULT ? */
240 if (use_gdb_syscalls()) {
241 gdb_do_syscall(arm_semi_cb
, "write,2,%x,%x\n", args
, len
);
244 ret
= write(STDERR_FILENO
, s
, len
);
246 unlock_user(s
, args
, 0);
250 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
);
255 if (!(s
= lock_user(VERIFY_READ
, ARG(1), len
, 1)))
256 /* FIXME - should this error code be -TARGET_EFAULT ? */
258 ret
= set_swi_errno(ts
, write(ARG(0), s
, len
));
259 unlock_user(s
, ARG(1), 0);
260 if (ret
== (uint32_t)-1)
266 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
);
271 if (!(s
= lock_user(VERIFY_WRITE
, ARG(1), len
, 0)))
272 /* FIXME - should this error code be -TARGET_EFAULT ? */
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)
283 /* XXX: Read from debug cosole. Not implemented. */
286 if (use_gdb_syscalls()) {
287 gdb_do_syscall(arm_semi_cb
, "isatty,%x", ARG(0));
290 return isatty(ARG(0));
293 if (use_gdb_syscalls()) {
294 gdb_do_syscall(arm_semi_cb
, "lseek,%x,%x,0", ARG(0), ARG(1));
297 ret
= set_swi_errno(ts
, lseek(ARG(0), ARG(1), SEEK_SET
));
298 if (ret
== (uint32_t)-1)
303 if (use_gdb_syscalls()) {
304 gdb_do_syscall(arm_semi_flen_cb
, "fstat,%x,%x",
305 ARG(0), env
->regs
[13]-64);
309 ret
= set_swi_errno(ts
, fstat(ARG(0), &buf
));
310 if (ret
== (uint32_t)-1)
315 /* XXX: Not implemented. */
318 if (use_gdb_syscalls()) {
319 gdb_do_syscall(arm_semi_cb
, "unlink,%s", ARG(0), (int)ARG(1)+1);
322 if (!(s
= lock_user_string(ARG(0))))
323 /* FIXME - should this error code be -TARGET_EFAULT ? */
325 ret
= set_swi_errno(ts
, remove(s
));
326 unlock_user(s
, ARG(0), 0);
330 if (use_gdb_syscalls()) {
331 gdb_do_syscall(arm_semi_cb
, "rename,%s,%s",
332 ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1);
336 s
= lock_user_string(ARG(0));
337 s2
= lock_user_string(ARG(2));
339 /* FIXME - should this error code be -TARGET_EFAULT ? */
342 ret
= set_swi_errno(ts
, rename(s
, s2
));
344 unlock_user(s2
, ARG(2), 0);
346 unlock_user(s
, ARG(0), 0);
350 return clock() / (CLOCKS_PER_SEC
/ 100);
352 return set_swi_errno(ts
, time(NULL
));
354 if (use_gdb_syscalls()) {
355 gdb_do_syscall(arm_semi_cb
, "system,%s", ARG(0), (int)ARG(1)+1);
358 if (!(s
= lock_user_string(ARG(0))))
359 /* FIXME - should this error code be -TARGET_EFAULT ? */
361 ret
= set_swi_errno(ts
, system(s
));
362 unlock_user(s
, ARG(0), 0);
366 #ifdef CONFIG_USER_ONLY
367 return ts
->swi_errno
;
371 case SYS_GET_CMDLINE
:
372 #ifdef CONFIG_USER_ONLY
373 /* Build a commandline from the original argv. */
375 char *arm_cmdline_buffer
;
376 const char *host_cmdline_buffer
;
379 unsigned int arm_cmdline_len
= ARG(1);
380 unsigned int host_cmdline_len
=
381 ts
->info
->arg_end
-ts
->info
->arg_start
;
383 if (!arm_cmdline_len
|| host_cmdline_len
> arm_cmdline_len
) {
384 return -1; /* not enough space to store command line */
387 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);
394 /* Adjust the commandline length argument. */
399 /* lock the buffers on the ARM side */
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);
406 if (arm_cmdline_buffer
&& host_cmdline_buffer
)
408 /* the last argument is zero-terminated;
409 no need for additional termination */
410 memcpy(arm_cmdline_buffer
, host_cmdline_buffer
,
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
] = ' ';
420 /* Adjust the commandline length argument. */
421 SET_ARG(1, host_cmdline_len
-1);
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);
428 /* Return success if we could return a commandline. */
429 return (arm_cmdline_buffer
&& host_cmdline_buffer
) ? 0 : -1;
439 #ifdef CONFIG_USER_ONLY
440 /* Some C libraries assume the heap immediately follows .bss, so
441 allocate it using sbrk. */
442 if (!ts
->heap_limit
) {
445 ts
->heap_base
= do_brk(0);
446 limit
= ts
->heap_base
+ ARM_ANGEL_HEAP_SIZE
;
447 /* Try a big heap, and reduce the size if that fails. */
452 limit
= (ts
->heap_base
>> 1) + (limit
>> 1);
454 ts
->heap_limit
= limit
;
457 if (!(ptr
= lock_user(VERIFY_WRITE
, ARG(0), 16, 0)))
458 /* FIXME - should this error code be -TARGET_EFAULT ? */
460 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. */
464 unlock_user(ptr
, ARG(0), 16);
467 if (!(ptr
= lock_user(VERIFY_WRITE
, ARG(0), 16, 0)))
468 /* FIXME - should this error code be -TARGET_EFAULT ? */
470 /* 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);
483 fprintf(stderr
, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr
);
484 cpu_dump_state(env
, stderr
, fprintf
, 0);