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