hw: Add support for LSI SAS1068 (mptsas) device
[qemu/ar7.git] / target-arm / arm-semi.c
blob76c33b97e7f8bd482e49b5f89c4fb6148d283c7c
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 "qemu/osdep.h"
23 #include "cpu.h"
24 #include "exec/semihost.h"
25 #ifdef CONFIG_USER_ONLY
26 #include "qemu.h"
28 #define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
29 #else
30 #include "qemu-common.h"
31 #include "exec/gdbstub.h"
32 #include "hw/arm/arm.h"
33 #endif
35 #define TARGET_SYS_OPEN 0x01
36 #define TARGET_SYS_CLOSE 0x02
37 #define TARGET_SYS_WRITEC 0x03
38 #define TARGET_SYS_WRITE0 0x04
39 #define TARGET_SYS_WRITE 0x05
40 #define TARGET_SYS_READ 0x06
41 #define TARGET_SYS_READC 0x07
42 #define TARGET_SYS_ISTTY 0x09
43 #define TARGET_SYS_SEEK 0x0a
44 #define TARGET_SYS_FLEN 0x0c
45 #define TARGET_SYS_TMPNAM 0x0d
46 #define TARGET_SYS_REMOVE 0x0e
47 #define TARGET_SYS_RENAME 0x0f
48 #define TARGET_SYS_CLOCK 0x10
49 #define TARGET_SYS_TIME 0x11
50 #define TARGET_SYS_SYSTEM 0x12
51 #define TARGET_SYS_ERRNO 0x13
52 #define TARGET_SYS_GET_CMDLINE 0x15
53 #define TARGET_SYS_HEAPINFO 0x16
54 #define TARGET_SYS_EXIT 0x18
55 #define TARGET_SYS_SYNCCACHE 0x19
57 /* ADP_Stopped_ApplicationExit is used for exit(0),
58 * anything else is implemented as exit(1) */
59 #define ADP_Stopped_ApplicationExit (0x20026)
61 #ifndef O_BINARY
62 #define O_BINARY 0
63 #endif
65 #define GDB_O_RDONLY 0x000
66 #define GDB_O_WRONLY 0x001
67 #define GDB_O_RDWR 0x002
68 #define GDB_O_APPEND 0x008
69 #define GDB_O_CREAT 0x200
70 #define GDB_O_TRUNC 0x400
71 #define GDB_O_BINARY 0
73 static int gdb_open_modeflags[12] = {
74 GDB_O_RDONLY,
75 GDB_O_RDONLY | GDB_O_BINARY,
76 GDB_O_RDWR,
77 GDB_O_RDWR | GDB_O_BINARY,
78 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
79 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
80 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
81 GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
82 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
83 GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
84 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
85 GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
88 static int open_modeflags[12] = {
89 O_RDONLY,
90 O_RDONLY | O_BINARY,
91 O_RDWR,
92 O_RDWR | O_BINARY,
93 O_WRONLY | O_CREAT | O_TRUNC,
94 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
95 O_RDWR | O_CREAT | O_TRUNC,
96 O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
97 O_WRONLY | O_CREAT | O_APPEND,
98 O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
99 O_RDWR | O_CREAT | O_APPEND,
100 O_RDWR | O_CREAT | O_APPEND | O_BINARY
103 #ifdef CONFIG_USER_ONLY
104 static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
106 if (code == (uint32_t)-1)
107 ts->swi_errno = errno;
108 return code;
110 #else
111 static inline uint32_t set_swi_errno(CPUARMState *env, uint32_t code)
113 return code;
116 #include "exec/softmmu-semi.h"
117 #endif
119 static target_ulong arm_semi_syscall_len;
121 #if !defined(CONFIG_USER_ONLY)
122 static target_ulong syscall_err;
123 #endif
125 static void arm_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
127 ARMCPU *cpu = ARM_CPU(cs);
128 CPUARMState *env = &cpu->env;
129 #ifdef CONFIG_USER_ONLY
130 TaskState *ts = cs->opaque;
131 #endif
132 target_ulong reg0 = is_a64(env) ? env->xregs[0] : env->regs[0];
134 if (ret == (target_ulong)-1) {
135 #ifdef CONFIG_USER_ONLY
136 ts->swi_errno = err;
137 #else
138 syscall_err = err;
139 #endif
140 reg0 = ret;
141 } else {
142 /* Fixup syscalls that use nonstardard return conventions. */
143 switch (reg0) {
144 case TARGET_SYS_WRITE:
145 case TARGET_SYS_READ:
146 reg0 = arm_semi_syscall_len - ret;
147 break;
148 case TARGET_SYS_SEEK:
149 reg0 = 0;
150 break;
151 default:
152 reg0 = ret;
153 break;
156 if (is_a64(env)) {
157 env->xregs[0] = reg0;
158 } else {
159 env->regs[0] = reg0;
163 static target_ulong arm_flen_buf(ARMCPU *cpu)
165 /* Return an address in target memory of 64 bytes where the remote
166 * gdb should write its stat struct. (The format of this structure
167 * is defined by GDB's remote protocol and is not target-specific.)
168 * We put this on the guest's stack just below SP.
170 CPUARMState *env = &cpu->env;
171 target_ulong sp;
173 if (is_a64(env)) {
174 sp = env->xregs[31];
175 } else {
176 sp = env->regs[13];
179 return sp - 64;
182 static void arm_semi_flen_cb(CPUState *cs, target_ulong ret, target_ulong err)
184 ARMCPU *cpu = ARM_CPU(cs);
185 CPUARMState *env = &cpu->env;
186 /* The size is always stored in big-endian order, extract
187 the value. We assume the size always fit in 32 bits. */
188 uint32_t size;
189 cpu_memory_rw_debug(cs, arm_flen_buf(cpu) + 32, (uint8_t *)&size, 4, 0);
190 size = be32_to_cpu(size);
191 if (is_a64(env)) {
192 env->xregs[0] = size;
193 } else {
194 env->regs[0] = size;
196 #ifdef CONFIG_USER_ONLY
197 ((TaskState *)cs->opaque)->swi_errno = err;
198 #else
199 syscall_err = err;
200 #endif
203 static target_ulong arm_gdb_syscall(ARMCPU *cpu, gdb_syscall_complete_cb cb,
204 const char *fmt, ...)
206 va_list va;
207 CPUARMState *env = &cpu->env;
209 va_start(va, fmt);
210 gdb_do_syscallv(cb, fmt, va);
211 va_end(va);
213 /* FIXME: we are implicitly relying on the syscall completing
214 * before this point, which is not guaranteed. We should
215 * put in an explicit synchronization between this and
216 * the callback function.
219 return is_a64(env) ? env->xregs[0] : env->regs[0];
222 /* Read the input value from the argument block; fail the semihosting
223 * call if the memory read fails.
225 #define GET_ARG(n) do { \
226 if (is_a64(env)) { \
227 if (get_user_u64(arg ## n, args + (n) * 8)) { \
228 return -1; \
230 } else { \
231 if (get_user_u32(arg ## n, args + (n) * 4)) { \
232 return -1; \
235 } while (0)
237 #define SET_ARG(n, val) \
238 (is_a64(env) ? \
239 put_user_u64(val, args + (n) * 8) : \
240 put_user_u32(val, args + (n) * 4))
242 target_ulong do_arm_semihosting(CPUARMState *env)
244 ARMCPU *cpu = arm_env_get_cpu(env);
245 CPUState *cs = CPU(cpu);
246 target_ulong args;
247 target_ulong arg0, arg1, arg2, arg3;
248 char * s;
249 int nr;
250 uint32_t ret;
251 uint32_t len;
252 #ifdef CONFIG_USER_ONLY
253 TaskState *ts = cs->opaque;
254 #else
255 CPUARMState *ts = env;
256 #endif
258 if (is_a64(env)) {
259 /* Note that the syscall number is in W0, not X0 */
260 nr = env->xregs[0] & 0xffffffffU;
261 args = env->xregs[1];
262 } else {
263 nr = env->regs[0];
264 args = env->regs[1];
267 switch (nr) {
268 case TARGET_SYS_OPEN:
269 GET_ARG(0);
270 GET_ARG(1);
271 GET_ARG(2);
272 s = lock_user_string(arg0);
273 if (!s) {
274 /* FIXME - should this error code be -TARGET_EFAULT ? */
275 return (uint32_t)-1;
277 if (arg1 >= 12) {
278 unlock_user(s, arg0, 0);
279 return (uint32_t)-1;
281 if (strcmp(s, ":tt") == 0) {
282 int result_fileno = arg1 < 4 ? STDIN_FILENO : STDOUT_FILENO;
283 unlock_user(s, arg0, 0);
284 return result_fileno;
286 if (use_gdb_syscalls()) {
287 ret = arm_gdb_syscall(cpu, arm_semi_cb, "open,%s,%x,1a4", arg0,
288 (int)arg2+1, gdb_open_modeflags[arg1]);
289 } else {
290 ret = set_swi_errno(ts, open(s, open_modeflags[arg1], 0644));
292 unlock_user(s, arg0, 0);
293 return ret;
294 case TARGET_SYS_CLOSE:
295 GET_ARG(0);
296 if (use_gdb_syscalls()) {
297 return arm_gdb_syscall(cpu, arm_semi_cb, "close,%x", arg0);
298 } else {
299 return set_swi_errno(ts, close(arg0));
301 case TARGET_SYS_WRITEC:
303 char c;
305 if (get_user_u8(c, args))
306 /* FIXME - should this error code be -TARGET_EFAULT ? */
307 return (uint32_t)-1;
308 /* Write to debug console. stderr is near enough. */
309 if (use_gdb_syscalls()) {
310 return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,1", args);
311 } else {
312 return write(STDERR_FILENO, &c, 1);
315 case TARGET_SYS_WRITE0:
316 if (!(s = lock_user_string(args)))
317 /* FIXME - should this error code be -TARGET_EFAULT ? */
318 return (uint32_t)-1;
319 len = strlen(s);
320 if (use_gdb_syscalls()) {
321 return arm_gdb_syscall(cpu, arm_semi_cb, "write,2,%x,%x",
322 args, len);
323 } else {
324 ret = write(STDERR_FILENO, s, len);
326 unlock_user(s, args, 0);
327 return ret;
328 case TARGET_SYS_WRITE:
329 GET_ARG(0);
330 GET_ARG(1);
331 GET_ARG(2);
332 len = arg2;
333 if (use_gdb_syscalls()) {
334 arm_semi_syscall_len = len;
335 return arm_gdb_syscall(cpu, arm_semi_cb, "write,%x,%x,%x",
336 arg0, arg1, len);
337 } else {
338 s = lock_user(VERIFY_READ, arg1, len, 1);
339 if (!s) {
340 /* FIXME - should this error code be -TARGET_EFAULT ? */
341 return (uint32_t)-1;
343 ret = set_swi_errno(ts, write(arg0, s, len));
344 unlock_user(s, arg1, 0);
345 if (ret == (uint32_t)-1)
346 return -1;
347 return len - ret;
349 case TARGET_SYS_READ:
350 GET_ARG(0);
351 GET_ARG(1);
352 GET_ARG(2);
353 len = arg2;
354 if (use_gdb_syscalls()) {
355 arm_semi_syscall_len = len;
356 return arm_gdb_syscall(cpu, arm_semi_cb, "read,%x,%x,%x",
357 arg0, arg1, len);
358 } else {
359 s = lock_user(VERIFY_WRITE, arg1, len, 0);
360 if (!s) {
361 /* FIXME - should this error code be -TARGET_EFAULT ? */
362 return (uint32_t)-1;
364 do {
365 ret = set_swi_errno(ts, read(arg0, s, len));
366 } while (ret == -1 && errno == EINTR);
367 unlock_user(s, arg1, len);
368 if (ret == (uint32_t)-1)
369 return -1;
370 return len - ret;
372 case TARGET_SYS_READC:
373 /* XXX: Read from debug console. Not implemented. */
374 return 0;
375 case TARGET_SYS_ISTTY:
376 GET_ARG(0);
377 if (use_gdb_syscalls()) {
378 return arm_gdb_syscall(cpu, arm_semi_cb, "isatty,%x", arg0);
379 } else {
380 return isatty(arg0);
382 case TARGET_SYS_SEEK:
383 GET_ARG(0);
384 GET_ARG(1);
385 if (use_gdb_syscalls()) {
386 return arm_gdb_syscall(cpu, arm_semi_cb, "lseek,%x,%x,0",
387 arg0, arg1);
388 } else {
389 ret = set_swi_errno(ts, lseek(arg0, arg1, SEEK_SET));
390 if (ret == (uint32_t)-1)
391 return -1;
392 return 0;
394 case TARGET_SYS_FLEN:
395 GET_ARG(0);
396 if (use_gdb_syscalls()) {
397 return arm_gdb_syscall(cpu, arm_semi_flen_cb, "fstat,%x,%x",
398 arg0, arm_flen_buf(cpu));
399 } else {
400 struct stat buf;
401 ret = set_swi_errno(ts, fstat(arg0, &buf));
402 if (ret == (uint32_t)-1)
403 return -1;
404 return buf.st_size;
406 case TARGET_SYS_TMPNAM:
407 /* XXX: Not implemented. */
408 return -1;
409 case TARGET_SYS_REMOVE:
410 GET_ARG(0);
411 GET_ARG(1);
412 if (use_gdb_syscalls()) {
413 ret = arm_gdb_syscall(cpu, arm_semi_cb, "unlink,%s",
414 arg0, (int)arg1+1);
415 } else {
416 s = lock_user_string(arg0);
417 if (!s) {
418 /* FIXME - should this error code be -TARGET_EFAULT ? */
419 return (uint32_t)-1;
421 ret = set_swi_errno(ts, remove(s));
422 unlock_user(s, arg0, 0);
424 return ret;
425 case TARGET_SYS_RENAME:
426 GET_ARG(0);
427 GET_ARG(1);
428 GET_ARG(2);
429 GET_ARG(3);
430 if (use_gdb_syscalls()) {
431 return arm_gdb_syscall(cpu, arm_semi_cb, "rename,%s,%s",
432 arg0, (int)arg1+1, arg2, (int)arg3+1);
433 } else {
434 char *s2;
435 s = lock_user_string(arg0);
436 s2 = lock_user_string(arg2);
437 if (!s || !s2)
438 /* FIXME - should this error code be -TARGET_EFAULT ? */
439 ret = (uint32_t)-1;
440 else
441 ret = set_swi_errno(ts, rename(s, s2));
442 if (s2)
443 unlock_user(s2, arg2, 0);
444 if (s)
445 unlock_user(s, arg0, 0);
446 return ret;
448 case TARGET_SYS_CLOCK:
449 return clock() / (CLOCKS_PER_SEC / 100);
450 case TARGET_SYS_TIME:
451 return set_swi_errno(ts, time(NULL));
452 case TARGET_SYS_SYSTEM:
453 GET_ARG(0);
454 GET_ARG(1);
455 if (use_gdb_syscalls()) {
456 return arm_gdb_syscall(cpu, arm_semi_cb, "system,%s",
457 arg0, (int)arg1+1);
458 } else {
459 s = lock_user_string(arg0);
460 if (!s) {
461 /* FIXME - should this error code be -TARGET_EFAULT ? */
462 return (uint32_t)-1;
464 ret = set_swi_errno(ts, system(s));
465 unlock_user(s, arg0, 0);
466 return ret;
468 case TARGET_SYS_ERRNO:
469 #ifdef CONFIG_USER_ONLY
470 return ts->swi_errno;
471 #else
472 return syscall_err;
473 #endif
474 case TARGET_SYS_GET_CMDLINE:
476 /* Build a command-line from the original argv.
478 * The inputs are:
479 * * arg0, pointer to a buffer of at least the size
480 * specified in arg1.
481 * * arg1, size of the buffer pointed to by arg0 in
482 * bytes.
484 * The outputs are:
485 * * arg0, pointer to null-terminated string of the
486 * command line.
487 * * arg1, length of the string pointed to by arg0.
490 char *output_buffer;
491 size_t input_size;
492 size_t output_size;
493 int status = 0;
494 #if !defined(CONFIG_USER_ONLY)
495 const char *cmdline;
496 #endif
497 GET_ARG(0);
498 GET_ARG(1);
499 input_size = arg1;
500 /* Compute the size of the output string. */
501 #if !defined(CONFIG_USER_ONLY)
502 cmdline = semihosting_get_cmdline();
503 if (cmdline == NULL) {
504 cmdline = ""; /* Default to an empty line. */
506 output_size = strlen(cmdline) + 1; /* Count terminating 0. */
507 #else
508 unsigned int i;
510 output_size = ts->info->arg_end - ts->info->arg_start;
511 if (!output_size) {
512 /* We special-case the "empty command line" case (argc==0).
513 Just provide the terminating 0. */
514 output_size = 1;
516 #endif
518 if (output_size > input_size) {
519 /* Not enough space to store command-line arguments. */
520 return -1;
523 /* Adjust the command-line length. */
524 if (SET_ARG(1, output_size - 1)) {
525 /* Couldn't write back to argument block */
526 return -1;
529 /* Lock the buffer on the ARM side. */
530 output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
531 if (!output_buffer) {
532 return -1;
535 /* Copy the command-line arguments. */
536 #if !defined(CONFIG_USER_ONLY)
537 pstrcpy(output_buffer, output_size, cmdline);
538 #else
539 if (output_size == 1) {
540 /* Empty command-line. */
541 output_buffer[0] = '\0';
542 goto out;
545 if (copy_from_user(output_buffer, ts->info->arg_start,
546 output_size)) {
547 status = -1;
548 goto out;
551 /* Separate arguments by white spaces. */
552 for (i = 0; i < output_size - 1; i++) {
553 if (output_buffer[i] == 0) {
554 output_buffer[i] = ' ';
557 out:
558 #endif
559 /* Unlock the buffer on the ARM side. */
560 unlock_user(output_buffer, arg0, output_size);
562 return status;
564 case TARGET_SYS_HEAPINFO:
566 uint32_t *ptr;
567 uint32_t limit;
568 GET_ARG(0);
570 #ifdef CONFIG_USER_ONLY
571 /* Some C libraries assume the heap immediately follows .bss, so
572 allocate it using sbrk. */
573 if (!ts->heap_limit) {
574 abi_ulong ret;
576 ts->heap_base = do_brk(0);
577 limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
578 /* Try a big heap, and reduce the size if that fails. */
579 for (;;) {
580 ret = do_brk(limit);
581 if (ret >= limit) {
582 break;
584 limit = (ts->heap_base >> 1) + (limit >> 1);
586 ts->heap_limit = limit;
589 ptr = lock_user(VERIFY_WRITE, arg0, 16, 0);
590 if (!ptr) {
591 /* FIXME - should this error code be -TARGET_EFAULT ? */
592 return (uint32_t)-1;
594 ptr[0] = tswap32(ts->heap_base);
595 ptr[1] = tswap32(ts->heap_limit);
596 ptr[2] = tswap32(ts->stack_base);
597 ptr[3] = tswap32(0); /* Stack limit. */
598 unlock_user(ptr, arg0, 16);
599 #else
600 limit = ram_size;
601 ptr = lock_user(VERIFY_WRITE, arg0, 16, 0);
602 if (!ptr) {
603 /* FIXME - should this error code be -TARGET_EFAULT ? */
604 return (uint32_t)-1;
606 /* TODO: Make this use the limit of the loaded application. */
607 ptr[0] = tswap32(limit / 2);
608 ptr[1] = tswap32(limit);
609 ptr[2] = tswap32(limit); /* Stack base */
610 ptr[3] = tswap32(0); /* Stack limit. */
611 unlock_user(ptr, arg0, 16);
612 #endif
613 return 0;
615 case TARGET_SYS_EXIT:
616 if (is_a64(env)) {
617 /* The A64 version of this call takes a parameter block,
618 * so the application-exit type can return a subcode which
619 * is the exit status code from the application.
621 GET_ARG(0);
622 GET_ARG(1);
624 if (arg0 == ADP_Stopped_ApplicationExit) {
625 ret = arg1;
626 } else {
627 ret = 1;
629 } else {
630 /* ARM specifies only Stopped_ApplicationExit as normal
631 * exit, everything else is considered an error */
632 ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
634 gdb_exit(env, ret);
635 exit(ret);
636 case TARGET_SYS_SYNCCACHE:
637 /* Clean the D-cache and invalidate the I-cache for the specified
638 * virtual address range. This is a nop for us since we don't
639 * implement caches. This is only present on A64.
641 if (is_a64(env)) {
642 return 0;
644 /* fall through -- invalid for A32/T32 */
645 default:
646 fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
647 cpu_dump_state(cs, stderr, fprintf, 0);
648 abort();