]> git.proxmox.com Git - qemu.git/commitdiff
ARM "Angel" semihosting syscalls (Paul Brook)
authorbellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>
Sat, 23 Apr 2005 18:25:41 +0000 (18:25 +0000)
committerbellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>
Sat, 23 Apr 2005 18:25:41 +0000 (18:25 +0000)
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1383 c046a42c-6fe2-441c-8c8c-71466251a162

Makefile.target
linux-user/arm-semi.c [new file with mode: 0644]
linux-user/arm/syscall.h
linux-user/main.c
linux-user/qemu.h
linux-user/syscall.c

index 789848e53100696c1afdd57f79bc00646c0c9635..5375cb32b7027df0b0e5e4efe5ddd8d046487358 100644 (file)
@@ -233,7 +233,7 @@ endif
 ifeq ($(TARGET_ARCH), arm)
 OBJS+=nwfpe/fpa11.o nwfpe/fpa11_cpdo.o \
 nwfpe/fpa11_cpdt.o nwfpe/fpa11_cprt.o nwfpe/fpopcode.o nwfpe/single_cpdo.o \
- nwfpe/double_cpdo.o nwfpe/extended_cpdo.o
+ nwfpe/double_cpdo.o nwfpe/extended_cpdo.o arm-semi.o
 endif
 SRCS:= $(OBJS:.o=.c)
 OBJS+= libqemu.a
diff --git a/linux-user/arm-semi.c b/linux-user/arm-semi.c
new file mode 100644 (file)
index 0000000..f37b65c
--- /dev/null
@@ -0,0 +1,193 @@
+/*
+ *  Arm "Angel" semihosting syscalls
+ * 
+ *  Copyright (c) 2005 CodeSourcery, LLC. Written by Paul Brook.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+
+#include "qemu.h"
+
+#define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
+
+#define SYS_OPEN        0x01
+#define SYS_CLOSE       0x02
+#define SYS_WRITEC      0x03
+#define SYS_WRITE0      0x04
+#define SYS_WRITE       0x05
+#define SYS_READ        0x06
+#define SYS_READC       0x07
+#define SYS_ISTTY       0x09
+#define SYS_SEEK        0x0a
+#define SYS_FLEN        0x0c
+#define SYS_TMPNAM      0x0d
+#define SYS_REMOVE      0x0e
+#define SYS_RENAME      0x0f
+#define SYS_CLOCK       0x10
+#define SYS_TIME        0x11
+#define SYS_SYSTEM      0x12
+#define SYS_ERRNO       0x13
+#define SYS_GET_CMDLINE 0x15
+#define SYS_HEAPINFO    0x16
+#define SYS_EXIT        0x18
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+int open_modeflags[12] = {
+    O_RDONLY,
+    O_RDONLY | O_BINARY,
+    O_RDWR,
+    O_RDWR | O_BINARY,
+    O_WRONLY | O_CREAT | O_TRUNC,
+    O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
+    O_RDWR | O_CREAT | O_TRUNC,
+    O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
+    O_WRONLY | O_CREAT | O_APPEND,
+    O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
+    O_RDWR | O_CREAT | O_APPEND,
+    O_RDWR | O_CREAT | O_APPEND | O_BINARY
+};
+
+static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
+{
+  if (code == (uint32_t)-1)
+      ts->swi_errno = errno;
+  return code;
+}
+
+#define ARG(x) tswap32(args[x])
+uint32_t do_arm_semihosting(CPUState *env)
+{
+    uint32_t *args;
+    char * s;
+    int nr;
+    uint32_t ret;
+    TaskState *ts = env->opaque;
+
+    nr = env->regs[0];
+    args = (uint32_t *)env->regs[1];
+    switch (nr) {
+    case SYS_OPEN:
+        s = (char *)ARG(0);
+        if (ARG(1) >= 12)
+          return (uint32_t)-1;
+        if (strcmp(s, ":tt") == 0) {
+            if (ARG(1) < 4)
+                return STDIN_FILENO;
+            else
+                return STDOUT_FILENO;
+        }
+        return set_swi_errno(ts, open(s, open_modeflags[ARG(1)]));
+    case SYS_CLOSE:
+        return set_swi_errno(ts, close(ARG(0)));
+    case SYS_WRITEC:
+        /* Write to debug console.  stderr is near enough.  */
+        return write(STDERR_FILENO, args, 1);
+    case SYS_WRITE0:
+        s = (char *)args;
+        return write(STDERR_FILENO, s, strlen(s));
+    case SYS_WRITE:
+        ret = set_swi_errno(ts, write(ARG(0), (void *)ARG(1), ARG(2)));
+        if (ret == (uint32_t)-1)
+            return -1;
+        return ARG(2) - ret;
+    case SYS_READ:
+        ret = set_swi_errno(ts, read(ARG(0), (void *)ARG(1), ARG(2)));
+        if (ret == (uint32_t)-1)
+            return -1;
+        return ARG(2) - ret;
+    case SYS_READC:
+       /* XXX: Read from debug cosole. Not implemented.  */
+        return 0;
+    case SYS_ISTTY:
+        return isatty(ARG(0));
+    case SYS_SEEK:
+        return set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
+    case SYS_FLEN:
+        {
+            struct stat buf;
+            ret = set_swi_errno(ts, fstat(ARG(0), &buf));
+            if (ret == (uint32_t)-1)
+                return -1;
+            return buf.st_size;
+        }
+    case SYS_TMPNAM:
+        /* XXX: Not implemented.  */
+        return -1;
+    case SYS_REMOVE:
+        return set_swi_errno(ts, remove((char *)ARG(0)));
+    case SYS_RENAME:
+        return set_swi_errno(ts, rename((char *)ARG(0), (char *)ARG(2)));
+    case SYS_CLOCK:
+        return clock() / (CLOCKS_PER_SEC / 100);
+    case SYS_TIME:
+        return set_swi_errno(ts, time(NULL));
+    case SYS_SYSTEM:
+        return set_swi_errno(ts, system((char *)ARG(0)));
+    case SYS_ERRNO:
+        return ts->swi_errno;
+    case SYS_GET_CMDLINE:
+        /* XXX: Not implemented.  */
+        s = (char *)ARG(0);
+        *s = 0;
+        return -1;
+    case SYS_HEAPINFO:
+        {
+            uint32_t *ptr;
+            uint32_t limit;
+
+            /* Some C llibraries assume the heap immediately follows .bss, so
+               allocate it using sbrk.  */
+            if (!ts->heap_limit) {
+                long ret;
+
+                ts->heap_base = do_brk(NULL);
+                limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
+                /* Try a big heap, and reduce the size if that fails.  */
+                for (;;) {
+                    ret = do_brk((char *)limit);
+                    if (ret != -1)
+                        break;
+                    limit = (ts->heap_base >> 1) + (limit >> 1);
+                }
+                ts->heap_limit = limit;
+            }
+              
+            ptr = (uint32_t *)tswap32(ARG(0));
+            ptr[0] = tswap32(ts->heap_base);
+            ptr[1] = tswap32(ts->heap_limit);
+            ptr[2] = tswap32(ts->stack_base);
+            ptr[3] = tswap32(0); /* Stack limit.  */
+            return 0;
+        }
+    case SYS_EXIT:
+        exit(0);
+    default:
+        fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
+        cpu_dump_state(env, stderr, fprintf, 0);
+        abort();
+    }
+}
+
index f74d7652896b7fb9caf46a83754f37e8dd2237ea..daf4b34af8bfc4dc9424898cf9dd868d0699fb32 100644 (file)
@@ -29,8 +29,13 @@ struct target_pt_regs {
 
 #define ARM_NR_cacheflush (ARM_SYSCALL_BASE + 0xf0000 + 2)
 
+#define ARM_NR_semihosting       0x123456
+#define ARM_NR_thumb_semihosting  0xAB
+
 #if defined(TARGET_WORDS_BIGENDIAN)
 #define UNAME_MACHINE "armv5teb"
 #else
 #define UNAME_MACHINE "armv5tel"
 #endif
+
+uint32_t do_arm_semihosting(CPUState *);
index 4d1f8ce88c2856cfed89690415e886e2fcc3e9a3..bcdf8441b85fdbc628a02f7187b998cfd1181c26 100644 (file)
@@ -363,6 +363,9 @@ void cpu_loop(CPUARMState *env)
                 n = insn & 0xffffff;
                 if (n == ARM_NR_cacheflush) {
                     arm_cache_flush(env->regs[0], env->regs[1]);
+                } else if (n == ARM_NR_semihosting
+                           || n == ARM_NR_thumb_semihosting) {
+                    env->regs[0] = do_arm_semihosting (env);
                 } else if (n >= ARM_SYSCALL_BASE) {
                     /* linux syscall */
                     n -= ARM_SYSCALL_BASE;
@@ -1207,6 +1210,10 @@ int main(int argc, char **argv)
             env->regs[i] = regs->uregs[i];
         }
         env->cpsr = regs->uregs[16];
+        ts->stack_base = info->start_stack;
+        ts->heap_base = info->brk;
+        /* This will be filled in on the first SYS_HEAPINFO call.  */
+        ts->heap_limit = 0;
     }
 #elif defined(TARGET_SPARC)
     {
index 2a815eb81f820c34fe6ff5faeeae6b19000081d0..f385a1c92b9a26bf13ef578120bd169f45aef6f4 100644 (file)
@@ -62,6 +62,11 @@ typedef struct TaskState {
 #ifdef TARGET_ARM
     /* FPA state */
     FPA11 fpa;
+    /* Extra fields for semihosted binaries.  */
+    uint32_t stack_base;
+    uint32_t heap_base;
+    uint32_t heap_limit;
+    int swi_errno;
 #endif
 #ifdef TARGET_I386
     struct target_vm86plus_struct *target_v86;
@@ -80,6 +85,7 @@ int elf_exec(const char * filename, char ** argv, char ** envp,
              struct target_pt_regs * regs, struct image_info *infop);
 
 void target_set_brk(char *new_brk);
+long do_brk(char *new_brk);
 void syscall_init(void);
 long do_syscall(void *cpu_env, int num, long arg1, long arg2, long arg3, 
                 long arg4, long arg5, long arg6);
index d69b21e6f68418bf42fb4d47d0cc8bd05c3c3612..b00ce90cbf36e3261ae677d6f1b07f81aca453e2 100644 (file)
@@ -264,7 +264,7 @@ void target_set_brk(char *new_brk)
     target_original_brk = new_brk;
 }
 
-static long do_brk(char *new_brk)
+long do_brk(char *new_brk)
 {
     char *brk_page;
     long mapped_addr;