acpi: add aml_increment() term
[qemu.git] / target-arm / arm-semi.c
bloba8b83e6912f8a8685caeae71cfa6f08f41fef4a7
1 /*
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>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <time.h>
29 #include "cpu.h"
30 #ifdef CONFIG_USER_ONLY
31 #include "qemu.h"
33 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
34 #else
35 #include "qemu-common.h"
36 #include "exec/gdbstub.h"
37 #include "hw/arm/arm.h"
38 #endif
40 #define TARGET_SYS_OPEN 0x01
41 #define TARGET_SYS_CLOSE 0x02
42 #define TARGET_SYS_WRITEC 0x03
43 #define TARGET_SYS_WRITE0 0x04
44 #define TARGET_SYS_WRITE 0x05
45 #define TARGET_SYS_READ 0x06
46 #define TARGET_SYS_READC 0x07
47 #define TARGET_SYS_ISTTY 0x09
48 #define TARGET_SYS_SEEK 0x0a
49 #define TARGET_SYS_FLEN 0x0c
50 #define TARGET_SYS_TMPNAM 0x0d
51 #define TARGET_SYS_REMOVE 0x0e
52 #define TARGET_SYS_RENAME 0x0f
53 #define TARGET_SYS_CLOCK 0x10
54 #define TARGET_SYS_TIME 0x11
55 #define TARGET_SYS_SYSTEM 0x12
56 #define TARGET_SYS_ERRNO 0x13
57 #define TARGET_SYS_GET_CMDLINE 0x15
58 #define TARGET_SYS_HEAPINFO 0x16
59 #define TARGET_SYS_EXIT 0x18
61 /* ADP_Stopped_ApplicationExit is used for exit(0),
62 * anything else is implemented as exit(1) */
63 #define ADP_Stopped_ApplicationExit (0x20026)
65 #ifndef O_BINARY
66 #define O_BINARY 0
67 #endif
69 #define GDB_O_RDONLY 0x000
70 #define GDB_O_WRONLY 0x001
71 #define GDB_O_RDWR 0x002
72 #define GDB_O_APPEND 0x008
73 #define GDB_O_CREAT 0x200
74 #define GDB_O_TRUNC 0x400
75 #define GDB_O_BINARY 0
77 static int gdb_open_modeflags[12] = {
78 GDB_O_RDONLY,
79 GDB_O_RDONLY | GDB_O_BINARY,
80 GDB_O_RDWR,
81 GDB_O_RDWR | GDB_O_BINARY,
82 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
83 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
84 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
85 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
86 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
87 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
88 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
89 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
92 static int open_modeflags[12] = {
93 O_RDONLY,
94 O_RDONLY | O_BINARY,
95 O_RDWR,
96 O_RDWR | O_BINARY,
97 O_WRONLY | O_CREAT | O_TRUNC,
98 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
99 O_RDWR | O_CREAT | O_TRUNC,
100 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
101 O_WRONLY | O_CREAT | O_APPEND,
102 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
103 O_RDWR | O_CREAT | O_APPEND,
104 O_RDWR | O_CREAT | O_APPEND | O_BINARY
107 #ifdef CONFIG_USER_ONLY
108 static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
110 if (code == (uint32_t)-1)
111 ts->swi_errno = errno;
112 return code;
114 #else
115 static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code)
117 return code;
120 #include "exec/softmmu-semi.h"
121 #endif
123 static target_ulong arm_semi_syscall_len;
125 #if !defined(CONFIG_USER_ONLY)
126 static target_ulong syscall_err;
127 #endif
129 static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
131 ARMCPU *cpu = ARM_CPU(cs);
132 CPUARMState *env = &cpu->env;
133 #ifdef CONFIG_USER_ONLY
134 TaskState *ts = cs->opaque;
135 #endif
137 if (ret == (target_ulong)-1) {
138 #ifdef CONFIG_USER_ONLY
139 ts->swi_errno = err;
140 #else
141 syscall_err = err;
142 #endif
143 env->regs[0] = ret;
144 } else {
145 /* Fixup syscalls that use nonstardard return conventions. */
146 switch (env->regs[0]) {
147 case TARGET_SYS_WRITE:
148 case TARGET_SYS_READ:
149 env->regs[0] = arm_semi_syscall_len - ret;
150 break;
151 case TARGET_SYS_SEEK:
152 env->regs[0] = 0;
153 break;
154 default:
155 env->regs[0] = ret;
156 break;
161 static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
163 ARMCPU *cpu = ARM_CPU(cs);
164 CPUARMState *env = &cpu->env;
165 /* The size is always stored in big-endian order, extract
166 the value. We assume the size always fit in 32 bits. */
167 uint32_t size;
168 cpu_memory_rw_debug(cs, env->regs[13]-64+32, (uint8_t *)&size, 4, 0);
169 env->regs[0] = be32_to_cpu(size);
170 #ifdef CONFIG_USER_ONLY
171 ((TaskState *)cs->opaque)->swi_errno = err;
172 #else
173 syscall_err = err;
174 #endif
177 /* Read the input value from the argument block; fail the semihosting
178 * call if the memory read fails.
180 #define GET_ARG(n) do { \
181 if (get_user_ual(arg ## n, args + (n) * 4)) { \
182 return (uint32_t)-1; \
184 } while (0)
186 #define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
187 uint32_t do_arm_semihosting(CPUARMState *env)
189 ARMCPU *cpu = arm_env_get_cpu(env);
190 CPUState *cs = CPU(cpu);
191 target_ulong args;
192 target_ulong arg0, arg1, arg2, arg3;
193 char * s;
194 int nr;
195 uint32_t ret;
196 uint32_t len;
197 #ifdef CONFIG_USER_ONLY
198 TaskState *ts = cs->opaque;
199 #else
200 CPUARMState *ts = env;
201 #endif
203 nr = env->regs[0];
204 args = env->regs[1];
205 switch (nr) {
206 case TARGET_SYS_OPEN:
207 GET_ARG(0);
208 GET_ARG(1);
209 GET_ARG(2);
210 s = lock_user_string(arg0);
211 if (!s) {
212 /* FIXME - should this error code be -TARGET_EFAULT ? */
213 return (uint32_t)-1;
215 if (arg1 >= 12) {
216 unlock_user(s, arg0, 0);
217 return (uint32_t)-1;
219 if (strcmp(s, ":tt") == 0) {
220 int result_fileno = arg1 < 4 ? STDIN_FILENO : STDOUT_FILENO;
221 unlock_user(s, arg0, 0);
222 return result_fileno;
224 if (use_gdb_syscalls()) {
225 gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", arg0,
226 (int)arg2+1, gdb_open_modeflags[arg1]);
227 ret = env->regs[0];
228 } else {
229 ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644));
231 unlock_user(s, arg0, 0);
232 return ret;
233 case TARGET_SYS_CLOSE:
234 GET_ARG(0);
235 if (use_gdb_syscalls()) {
236 gdb_do_syscall(arm_semi_cb, "close,%x", arg0);
237 return env->regs[0];
238 } else {
239 return set_swi_errno(ts, close(arg0));
241 case TARGET_SYS_WRITEC:
243 char c;
245 if (get_user_u8(c, args))
246 /* FIXME - should this error code be -TARGET_EFAULT ? */
247 return (uint32_t)-1;
248 /* Write to debug console. stderr is near enough. */
249 if (use_gdb_syscalls()) {
250 gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args);
251 return env->regs[0];
252 } else {
253 return write(STDERR_FILENO, &c, 1);
256 case TARGET_SYS_WRITE0:
257 if (!(s = lock_user_string(args)))
258 /* FIXME - should this error code be -TARGET_EFAULT ? */
259 return (uint32_t)-1;
260 len = strlen(s);
261 if (use_gdb_syscalls()) {
262 gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len);
263 ret = env->regs[0];
264 } else {
265 ret = write(STDERR_FILENO, s, len);
267 unlock_user(s, args, 0);
268 return ret;
269 case TARGET_SYS_WRITE:
270 GET_ARG(0);
271 GET_ARG(1);
272 GET_ARG(2);
273 len = arg2;
274 if (use_gdb_syscalls()) {
275 arm_semi_syscall_len = len;
276 gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", arg0, arg1, len);
277 return env->regs[0];
278 } else {
279 s = lock_user(VERIFY_READ, arg1, len, 1);
280 if (!s) {
281 /* FIXME - should this error code be -TARGET_EFAULT ? */
282 return (uint32_t)-1;
284 ret = set_swi_errno(ts, write(arg0, s, len));
285 unlock_user(s, arg1, 0);
286 if (ret == (uint32_t)-1)
287 return -1;
288 return len - ret;
290 case TARGET_SYS_READ:
291 GET_ARG(0);
292 GET_ARG(1);
293 GET_ARG(2);
294 len = arg2;
295 if (use_gdb_syscalls()) {
296 arm_semi_syscall_len = len;
297 gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", arg0, arg1, len);
298 return env->regs[0];
299 } else {
300 s = lock_user(VERIFY_WRITE, arg1, len, 0);
301 if (!s) {
302 /* FIXME - should this error code be -TARGET_EFAULT ? */
303 return (uint32_t)-1;
305 do {
306 ret = set_swi_errno(ts, read(arg0, s, len));
307 } while (ret == -1 && errno == EINTR);
308 unlock_user(s, arg1, len);
309 if (ret == (uint32_t)-1)
310 return -1;
311 return len - ret;
313 case TARGET_SYS_READC:
314 /* XXX: Read from debug console. Not implemented. */
315 return 0;
316 case TARGET_SYS_ISTTY:
317 GET_ARG(0);
318 if (use_gdb_syscalls()) {
319 gdb_do_syscall(arm_semi_cb, "isatty,%x", arg0);
320 return env->regs[0];
321 } else {
322 return isatty(arg0);
324 case TARGET_SYS_SEEK:
325 GET_ARG(0);
326 GET_ARG(1);
327 if (use_gdb_syscalls()) {
328 gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", arg0, arg1);
329 return env->regs[0];
330 } else {
331 ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET));
332 if (ret == (uint32_t)-1)
333 return -1;
334 return 0;
336 case TARGET_SYS_FLEN:
337 GET_ARG(0);
338 if (use_gdb_syscalls()) {
339 gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x",
340 arg0, env->regs[13]-64);
341 return env->regs[0];
342 } else {
343 struct stat buf;
344 ret = set_swi_errno(ts, fstat(arg0, &buf));
345 if (ret == (uint32_t)-1)
346 return -1;
347 return buf.st_size;
349 case TARGET_SYS_TMPNAM:
350 /* XXX: Not implemented. */
351 return -1;
352 case TARGET_SYS_REMOVE:
353 GET_ARG(0);
354 GET_ARG(1);
355 if (use_gdb_syscalls()) {
356 gdb_do_syscall(arm_semi_cb, "unlink,%s", arg0, (int)arg1+1);
357 ret = env->regs[0];
358 } else {
359 s = lock_user_string(arg0);
360 if (!s) {
361 /* FIXME - should this error code be -TARGET_EFAULT ? */
362 return (uint32_t)-1;
364 ret = set_swi_errno(ts, remove(s));
365 unlock_user(s, arg0, 0);
367 return ret;
368 case TARGET_SYS_RENAME:
369 GET_ARG(0);
370 GET_ARG(1);
371 GET_ARG(2);
372 GET_ARG(3);
373 if (use_gdb_syscalls()) {
374 gdb_do_syscall(arm_semi_cb, "rename,%s,%s",
375 arg0, (int)arg1+1, arg2, (int)arg3+1);
376 return env->regs[0];
377 } else {
378 char *s2;
379 s = lock_user_string(arg0);
380 s2 = lock_user_string(arg2);
381 if (!s || !s2)
382 /* FIXME - should this error code be -TARGET_EFAULT ? */
383 ret = (uint32_t)-1;
384 else
385 ret = set_swi_errno(ts, rename(s, s2));
386 if (s2)
387 unlock_user(s2, arg2, 0);
388 if (s)
389 unlock_user(s, arg0, 0);
390 return ret;
392 case TARGET_SYS_CLOCK:
393 return clock() / (CLOCKS_PER_SEC / 100);
394 case TARGET_SYS_TIME:
395 return set_swi_errno(ts, time(NULL));
396 case TARGET_SYS_SYSTEM:
397 GET_ARG(0);
398 GET_ARG(1);
399 if (use_gdb_syscalls()) {
400 gdb_do_syscall(arm_semi_cb, "system,%s", arg0, (int)arg1+1);
401 return env->regs[0];
402 } else {
403 s = lock_user_string(arg0);
404 if (!s) {
405 /* FIXME - should this error code be -TARGET_EFAULT ? */
406 return (uint32_t)-1;
408 ret = set_swi_errno(ts, system(s));
409 unlock_user(s, arg0, 0);
410 return ret;
412 case TARGET_SYS_ERRNO:
413 #ifdef CONFIG_USER_ONLY
414 return ts->swi_errno;
415 #else
416 return syscall_err;
417 #endif
418 case TARGET_SYS_GET_CMDLINE:
420 /* Build a command-line from the original argv.
422 * The inputs are:
423 * * arg0, pointer to a buffer of at least the size
424 * specified in arg1.
425 * * arg1, size of the buffer pointed to by arg0 in
426 * bytes.
428 * The outputs are:
429 * * arg0, pointer to null-terminated string of the
430 * command line.
431 * * arg1, length of the string pointed to by arg0.
434 char *output_buffer;
435 size_t input_size;
436 size_t output_size;
437 int status = 0;
438 GET_ARG(0);
439 GET_ARG(1);
440 input_size = arg1;
441 /* Compute the size of the output string. */
442 #if !defined(CONFIG_USER_ONLY)
443 output_size = strlen(ts->boot_info->kernel_filename)
444 + 1 /* Separating space. */
445 + strlen(ts->boot_info->kernel_cmdline)
446 + 1; /* Terminating null byte. */
447 #else
448 unsigned int i;
450 output_size = ts->info->arg_end - ts->info->arg_start;
451 if (!output_size) {
452 /* We special-case the "empty command line" case (argc==0).
453 Just provide the terminating 0. */
454 output_size = 1;
456 #endif
458 if (output_size > input_size) {
459 /* Not enough space to store command-line arguments. */
460 return -1;
463 /* Adjust the command-line length. */
464 if (SET_ARG(1, output_size - 1)) {
465 /* Couldn't write back to argument block */
466 return -1;
469 /* Lock the buffer on the ARM side. */
470 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
471 if (!output_buffer) {
472 return -1;
475 /* Copy the command-line arguments. */
476 #if !defined(CONFIG_USER_ONLY)
477 pstrcpy(output_buffer, output_size, ts->boot_info->kernel_filename);
478 pstrcat(output_buffer, output_size, " ");
479 pstrcat(output_buffer, output_size, ts->boot_info->kernel_cmdline);
480 #else
481 if (output_size == 1) {
482 /* Empty command-line. */
483 output_buffer[0] = '\0';
484 goto out;
487 if (copy_from_user(output_buffer, ts->info->arg_start,
488 output_size)) {
489 status = -1;
490 goto out;
493 /* Separate arguments by white spaces. */
494 for (i = 0; i < output_size - 1; i++) {
495 if (output_buffer[i] == 0) {
496 output_buffer[i] = ' ';
499 out:
500 #endif
501 /* Unlock the buffer on the ARM side. */
502 unlock_user(output_buffer, arg0, output_size);
504 return status;
506 case TARGET_SYS_HEAPINFO:
508 uint32_t *ptr;
509 uint32_t limit;
510 GET_ARG(0);
512 #ifdef CONFIG_USER_ONLY
513 /* Some C libraries assume the heap immediately follows .bss, so
514 allocate it using sbrk. */
515 if (!ts->heap_limit) {
516 abi_ulong ret;
518 ts->heap_base = do_brk(0);
519 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
520 /* Try a big heap, and reduce the size if that fails. */
521 for (;;) {
522 ret = do_brk(limit);
523 if (ret >= limit) {
524 break;
526 limit = (ts->heap_base >> 1) + (limit >> 1);
528 ts->heap_limit = limit;
531 ptr = lock_user(VERIFY_WRITE, arg0, 16, 0);
532 if (!ptr) {
533 /* FIXME - should this error code be -TARGET_EFAULT ? */
534 return (uint32_t)-1;
536 ptr[0] = tswap32(ts->heap_base);
537 ptr[1] = tswap32(ts->heap_limit);
538 ptr[2] = tswap32(ts->stack_base);
539 ptr[3] = tswap32(0); /* Stack limit. */
540 unlock_user(ptr, arg0, 16);
541 #else
542 limit = ram_size;
543 ptr = lock_user(VERIFY_WRITE, arg0, 16, 0);
544 if (!ptr) {
545 /* FIXME - should this error code be -TARGET_EFAULT ? */
546 return (uint32_t)-1;
548 /* TODO: Make this use the limit of the loaded application. */
549 ptr[0] = tswap32(limit / 2);
550 ptr[1] = tswap32(limit);
551 ptr[2] = tswap32(limit); /* Stack base */
552 ptr[3] = tswap32(0); /* Stack limit. */
553 unlock_user(ptr, arg0, 16);
554 #endif
555 return 0;
557 case TARGET_SYS_EXIT:
558 /* ARM specifies only Stopped_ApplicationExit as normal
559 * exit, everything else is considered an error */
560 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
561 gdb_exit(env, ret);
562 exit(ret);
563 default:
564 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
565 cpu_dump_state(cs, stderr, fprintf, 0);
566 abort();