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)
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 static void arm_semi_cb(CPUState *env, target_ulong ret, target_ulong err)
122 #ifdef CONFIG_USER_ONLY
123 TaskState *ts = env->opaque;
125 if (ret == (target_ulong)-1) {
126 #ifdef CONFIG_USER_ONLY
131 /* Fixup syscalls that use nonstardard return conventions. */
132 switch (env->regs[0]) {
135 env->regs[0] = arm_semi_syscall_len - ret;
147 #define ARG(n) tget32(args + (n) * 4)
148 #define SET_ARG(n, val) tput32(args + (n) * 4,val)
149 uint32_t do_arm_semihosting(CPUState *env)
156 #ifdef CONFIG_USER_ONLY
157 TaskState *ts = env->opaque;
166 s = lock_user_string(ARG(0));
169 if (strcmp(s, ":tt") == 0) {
173 return STDOUT_FILENO;
175 if (use_gdb_syscalls()) {
176 gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0), (int)ARG(2),
177 gdb_open_modeflags[ARG(1)]);
180 ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644));
182 unlock_user(s, ARG(0), 0);
185 if (use_gdb_syscalls()) {
186 gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0));
189 return set_swi_errno(ts, close(ARG(0)));
193 char c = tget8(args);
194 /* Write to debug console. stderr is near enough. */
195 if (use_gdb_syscalls()) {
196 gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args);
199 return write(STDERR_FILENO, &c, 1);
203 s = lock_user_string(args);
205 if (use_gdb_syscalls()) {
206 gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len);
209 ret = write(STDERR_FILENO, s, len);
211 unlock_user(s, args, 0);
215 if (use_gdb_syscalls()) {
216 arm_semi_syscall_len = len;
217 gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len);
220 s = lock_user(ARG(1), len, 1);
221 ret = set_swi_errno(ts, write(ARG(0), s, len));
222 unlock_user(s, ARG(1), 0);
223 if (ret == (uint32_t)-1)
229 if (use_gdb_syscalls()) {
230 arm_semi_syscall_len = len;
231 gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len);
234 s = lock_user(ARG(1), len, 0);
236 ret = set_swi_errno(ts, read(ARG(0), s, len));
237 while (ret == -1 && errno == EINTR);
238 unlock_user(s, ARG(1), len);
239 if (ret == (uint32_t)-1)
244 /* XXX: Read from debug cosole. Not implemented. */
247 if (use_gdb_syscalls()) {
248 gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0));
251 return isatty(ARG(0));
254 if (use_gdb_syscalls()) {
255 gdb_do_syscall(arm_semi_cb, "fseek,%x,%x,0", ARG(0), ARG(1));
258 ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
259 if (ret == (uint32_t)-1)
264 if (use_gdb_syscalls()) {
265 /* TODO: Use stat syscall. */
269 ret = set_swi_errno(ts, fstat(ARG(0), &buf));
270 if (ret == (uint32_t)-1)
275 /* XXX: Not implemented. */
278 if (use_gdb_syscalls()) {
279 gdb_do_syscall(arm_semi_cb, "unlink,%s", ARG(0), (int)ARG(1));
282 s = lock_user_string(ARG(0));
283 ret = set_swi_errno(ts, remove(s));
284 unlock_user(s, ARG(0), 0);
288 if (use_gdb_syscalls()) {
289 gdb_do_syscall(arm_semi_cb, "rename,%s,%s",
290 ARG(0), (int)ARG(1), ARG(2), (int)ARG(3));
294 s = lock_user_string(ARG(0));
295 s2 = lock_user_string(ARG(2));
296 ret = set_swi_errno(ts, rename(s, s2));
297 unlock_user(s2, ARG(2), 0);
298 unlock_user(s, ARG(0), 0);
302 return clock() / (CLOCKS_PER_SEC / 100);
304 return set_swi_errno(ts, time(NULL));
306 if (use_gdb_syscalls()) {
307 gdb_do_syscall(arm_semi_cb, "system,%s", ARG(0), (int)ARG(1));
310 s = lock_user_string(ARG(0));
311 ret = set_swi_errno(ts, system(s));
312 unlock_user(s, ARG(0), 0);
315 #ifdef CONFIG_USER_ONLY
316 return ts->swi_errno;
320 case SYS_GET_CMDLINE:
321 #ifdef CONFIG_USER_ONLY
322 /* Build a commandline from the original argv. */
324 char **arg = ts->info->host_argv;
326 /* lock the buffer on the ARM side */
327 char *cmdline_buffer = (char*)lock_user(ARG(0), len, 0);
330 while (*arg && len > 2) {
331 int n = strlen(*arg);
333 if (s != cmdline_buffer) {
344 /* Null terminate the string. */
346 len = s - cmdline_buffer;
348 /* Unlock the buffer on the ARM side. */
349 unlock_user(cmdline_buffer, ARG(0), len);
351 /* Adjust the commandline length argument. */
354 /* Return success if commandline fit into buffer. */
355 return *arg ? -1 : 0;
365 #ifdef CONFIG_USER_ONLY
366 /* Some C libraries assume the heap immediately follows .bss, so
367 allocate it using sbrk. */
368 if (!ts->heap_limit) {
371 ts->heap_base = do_brk(0);
372 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
373 /* Try a big heap, and reduce the size if that fails. */
378 limit = (ts->heap_base >> 1) + (limit >> 1);
380 ts->heap_limit = limit;
383 ptr = lock_user(ARG(0), 16, 0);
384 ptr[0] = tswap32(ts->heap_base);
385 ptr[1] = tswap32(ts->heap_limit);
386 ptr[2] = tswap32(ts->stack_base);
387 ptr[3] = tswap32(0); /* Stack limit. */
388 unlock_user(ptr, ARG(0), 16);
391 ptr = lock_user(ARG(0), 16, 0);
392 /* TODO: Make this use the limit of the loaded application. */
393 ptr[0] = tswap32(limit / 2);
394 ptr[1] = tswap32(limit);
395 ptr[2] = tswap32(limit); /* Stack base */
396 ptr[3] = tswap32(0); /* Stack limit. */
397 unlock_user(ptr, ARG(0), 16);
404 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
405 cpu_dump_state(env, stderr, fprintf, 0);