2 * Arm "Angel" semihosting syscalls
4 * Copyright (c) 2005 CodeSourcery, LLC. Written by Paul Brook.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <sys/types.h>
31 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
34 #define SYS_CLOSE 0x02
35 #define SYS_WRITEC 0x03
36 #define SYS_WRITE0 0x04
37 #define SYS_WRITE 0x05
39 #define SYS_READC 0x07
40 #define SYS_ISTTY 0x09
43 #define SYS_TMPNAM 0x0d
44 #define SYS_REMOVE 0x0e
45 #define SYS_RENAME 0x0f
46 #define SYS_CLOCK 0x10
48 #define SYS_SYSTEM 0x12
49 #define SYS_ERRNO 0x13
50 #define SYS_GET_CMDLINE 0x15
51 #define SYS_HEAPINFO 0x16
58 int open_modeflags[12] = {
63 O_WRONLY | O_CREAT | O_TRUNC,
64 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
65 O_RDWR | O_CREAT | O_TRUNC,
66 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
67 O_WRONLY | O_CREAT | O_APPEND,
68 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
69 O_RDWR | O_CREAT | O_APPEND,
70 O_RDWR | O_CREAT | O_APPEND | O_BINARY
73 static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
75 if (code == (uint32_t)-1)
76 ts->swi_errno = errno;
80 #define ARG(n) tget32(args + n * 4)
81 uint32_t do_arm_semihosting(CPUState *env)
87 TaskState *ts = env->opaque;
93 s = (char *)g2h(ARG(0));
96 if (strcmp(s, ":tt") == 0) {
100 return STDOUT_FILENO;
102 return set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644));
104 return set_swi_errno(ts, close(ARG(0)));
107 char c = tget8(args);
108 /* Write to debug console. stderr is near enough. */
109 return write(STDERR_FILENO, &c, 1);
112 s = lock_user_string(args);
113 ret = write(STDERR_FILENO, s, strlen(s));
114 unlock_user(s, args, 0);
117 ret = set_swi_errno(ts, write(ARG(0), g2h(ARG(1)), ARG(2)));
118 if (ret == (uint32_t)-1)
122 ret = set_swi_errno(ts, read(ARG(0), g2h(ARG(1)), ARG(2)));
123 if (ret == (uint32_t)-1)
127 /* XXX: Read from debug cosole. Not implemented. */
130 return isatty(ARG(0));
132 ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
133 if (ret == (uint32_t)-1)
139 ret = set_swi_errno(ts, fstat(ARG(0), &buf));
140 if (ret == (uint32_t)-1)
145 /* XXX: Not implemented. */
148 return set_swi_errno(ts, remove((char *)g2h(ARG(0))));
150 return set_swi_errno(ts, rename((char *)g2h(ARG(0)),
151 (char *)g2h(ARG(2))));
153 return clock() / (CLOCKS_PER_SEC / 100);
155 return set_swi_errno(ts, time(NULL));
157 return set_swi_errno(ts, system((char *)g2h(ARG(0))));
159 return ts->swi_errno;
160 case SYS_GET_CMDLINE:
161 /* XXX: Not implemented. */
162 s = (char *)g2h(ARG(0));
170 /* Some C llibraries assume the heap immediately follows .bss, so
171 allocate it using sbrk. */
172 if (!ts->heap_limit) {
175 ts->heap_base = do_brk(0);
176 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
177 /* Try a big heap, and reduce the size if that fails. */
182 limit = (ts->heap_base >> 1) + (limit >> 1);
184 ts->heap_limit = limit;
187 page_unprotect_range (ARG(0), 32);
188 ptr = (uint32_t *)g2h(ARG(0));
189 ptr[0] = tswap32(ts->heap_base);
190 ptr[1] = tswap32(ts->heap_limit);
191 ptr[2] = tswap32(ts->stack_base);
192 ptr[3] = tswap32(0); /* Stack limit. */
198 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
199 cpu_dump_state(env, stderr, fprintf, 0);