Revived GUEST_BASE support for usermode emulation targets.
authorRiku Voipio <riku.voipio@nokia.com>
Thu, 5 Mar 2009 09:54:03 +0000 (11:54 +0200)
committerRiku Voipio <riku.voipio@nokia.com>
Thu, 5 Mar 2009 09:54:03 +0000 (11:54 +0200)
From: Mika Westerberg <mika.westerberg@iki.fi>

- Now GUEST_BASE is dynamic and can be set from command line.
- Qemu checks /proc/sys/vm/mmap_min_addr and sets GUEST_BASE
  if needed.
- Code generation supports GUEST_BASE for i386 and x86_64 hosts.
- Strace is currently broken.

configure
cpu-all.h
debian/changelog
linux-user/elfload.c
linux-user/main.c
linux-user/qemu.h
linux-user/syscall.c
tcg/i386/tcg-target.c
tcg/x86_64/tcg-target.c

index ee8e446..4ad3af4 100755 (executable)
--- a/configure
+++ b/configure
@@ -175,6 +175,7 @@ softmmu="yes"
 linux_user="no"
 darwin_user="no"
 bsd_user="no"
+guest_base="no"
 build_docs="no"
 uname_release=""
 curses="yes"
@@ -425,6 +426,8 @@ for opt do
   ;;
   --enable-bsd-user) bsd_user="yes"
   ;;
+  --enable-guest-base) guest_base="yes"
+  ;;
   --enable-uname-release=*) uname_release="$optarg"
   ;;
   --sparc_cpu=*)
@@ -557,6 +560,8 @@ echo "  --enable-darwin-user     enable all darwin usermode emulation targets"
 echo "  --disable-darwin-user    disable all darwin usermode emulation targets"
 echo "  --enable-bsd-user        enable all BSD usermode emulation targets"
 echo "  --disable-bsd-user       disable all BSD usermode emulation targets"
+echo "  --enable-guest-base      enable GUEST_BASE support for usermode"
+echo "                           emulation targets"
 echo "  --fmod-lib               path to FMOD library"
 echo "  --fmod-inc               path to FMOD includes"
 echo "  --oss-lib                path to OSS library"
@@ -1064,7 +1069,7 @@ EOF
   fi
 fi
 
-#
+##########################################
 # Check for xxxat() functions when we are building linux-user
 # emulator.  This is done because older glibc versions don't
 # have syscall stubs for these implemented.  In that case we
@@ -1134,59 +1139,60 @@ else
   binsuffix="/bin"
 fi
 
-echo "Install prefix    $prefix"
-echo "BIOS directory    $prefix$datasuffix"
-echo "binary directory  $prefix$binsuffix"
+echo "Install prefix     $prefix"
+echo "BIOS directory     $prefix$datasuffix"
+echo "binary directory   $prefix$binsuffix"
 if test "$mingw32" = "no" ; then
-echo "Manual directory  $prefix$mansuffix"
-echo "ELF interp prefix $interp_prefix"
-fi
-echo "Source path       $source_path"
-echo "C compiler        $cc"
-echo "Host C compiler   $host_cc"
-echo "ARCH_CFLAGS       $ARCH_CFLAGS"
-echo "make              $make"
-echo "install           $install"
-echo "host CPU          $cpu"
-echo "host big endian   $bigendian"
-echo "target list       $target_list"
-echo "gprof enabled     $gprof"
-echo "sparse enabled    $sparse"
-echo "profiler          $profiler"
-echo "static build      $static"
-echo "-Werror enabled   $werror"
+echo "Manual directory   $prefix$mansuffix"
+echo "ELF interp prefix  $interp_prefix"
+fi
+echo "Source path        $source_path"
+echo "C compiler         $cc"
+echo "Host C compiler    $host_cc"
+echo "ARCH_CFLAGS        $ARCH_CFLAGS"
+echo "make               $make"
+echo "install            $install"
+echo "host CPU           $cpu"
+echo "host big endian    $bigendian"
+echo "target list        $target_list"
+echo "gprof enabled      $gprof"
+echo "sparse enabled     $sparse"
+echo "profiler           $profiler"
+echo "static build       $static"
+echo "-Werror enabled    $werror"
 if test "$darwin" = "yes" ; then
-    echo "Cocoa support     $cocoa"
+    echo "Cocoa support      $cocoa"
 fi
-echo "SDL support       $sdl"
+echo "SDL support        $sdl"
 if test "$sdl" != "no" ; then
-    echo "SDL static link   $sdl_static"
-fi
-echo "curses support    $curses"
-echo "atfile support    $atfile"
-echo "mingw32 support   $mingw32"
-echo "Audio drivers     $audio_drv_list"
-echo "Extra audio cards $audio_card_list"
-echo "Mixer emulation   $mixemu"
-echo "VNC TLS support   $vnc_tls"
+    echo "SDL static link    $sdl_static"
+fi
+echo "curses support     $curses"
+echo "atfile support     $atfile"
+echo "mingw32 support    $mingw32"
+echo "Audio drivers      $audio_drv_list"
+echo "Extra audio cards  $audio_card_list"
+echo "Mixer emulation    $mixemu"
+echo "VNC TLS support    $vnc_tls"
 if test "$vnc_tls" = "yes" ; then
-    echo "    TLS CFLAGS    $vnc_tls_cflags"
-    echo "    TLS LIBS      $vnc_tls_libs"
+    echo "    TLS CFLAGS     $vnc_tls_cflags"
+    echo "    TLS LIBS       $vnc_tls_libs"
 fi
 if test -n "$sparc_cpu"; then
-    echo "Target Sparc Arch $sparc_cpu"
+    echo "Target Sparc Arch  $sparc_cpu"
 fi
-echo "kqemu support     $kqemu"
-echo "brlapi support    $brlapi"
-echo "Documentation     $build_docs"
+echo "kqemu support      $kqemu"
+echo "brlapi support     $brlapi"
+echo "Documentation      $build_docs"
 [ ! -z "$uname_release" ] && \
-echo "uname -r          $uname_release"
-echo "NPTL support      $nptl"
-echo "vde support       $vde"
-echo "AIO support       $aio"
-echo "Install blobs     $blobs"
-echo "KVM support       $kvm"
-echo "fdt support       $fdt"
+echo "uname -r           $uname_release"
+echo "NPTL support       $nptl"
+echo "vde support        $vde"
+echo "AIO support        $aio"
+echo "Install blobs      $blobs"
+echo "KVM support        $kvm"
+echo "fdt support        $fdt"
+echo "GUEST_BASE support $guest_base"
 
 if test $sdl_too_old = "yes"; then
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -1822,6 +1828,9 @@ if test "$target_user_only" = "yes" -a "$elfload32" = "yes"; then
   echo "TARGET_HAS_ELFLOAD32=yes" >> $config_mak
   echo "#define TARGET_HAS_ELFLOAD32 1" >> $config_h
 fi
+if test "$target_user_only" = "yes" -a "$guest_base" = "yes"; then
+  echo "#define CONFIG_USE_GUEST_BASE 1" >> $config_h
+fi
 if test "$target_bsd_user" = "yes" ; then
   echo "CONFIG_BSD_USER=yes" >> $config_mak
   echo "#define CONFIG_BSD_USER 1" >> $config_h
index e0c3efd..b58c6b3 100644 (file)
--- a/cpu-all.h
+++ b/cpu-all.h
@@ -629,8 +629,12 @@ static inline void stfq_be_p(void *ptr, float64 v)
 /* On some host systems the guest address space is reserved on the host.
  * This allows the guest address space to be offset to a convenient location.
  */
-//#define GUEST_BASE 0x20000000
+#if defined(CONFIG_USE_GUEST_BASE)
+extern unsigned long guest_base;
+#define GUEST_BASE guest_base
+#else
 #define GUEST_BASE 0
+#endif
 
 /* All direct uses of g2h and h2g need to go away for usermode softmmu.  */
 #define g2h(x) ((void *)((unsigned long)(x) + GUEST_BASE))
index 1f19d42..84471e6 100644 (file)
@@ -1,3 +1,10 @@
+qemu (0.10.0-0maemo1) unstable; urgency=low
+
+  * New upstream version
+  * merge maemo patches 
+
+ -- Riku Voipio <riku.voipio@iki.fi>  Thu, 05 Mar 2009 11:50:07 +0200
+
 qemu (0.9.1+svn20090113-0maemo1) unstable; urgency=low
 
   * maemo patched version
index 6de30f4..ea012e0 100644 (file)
@@ -1337,6 +1337,30 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
     info->mmap = 0;
     elf_entry = (abi_ulong) elf_ex.e_entry;
 
+#if defined(CONFIG_USE_GUEST_BASE)
+    /*
+     * In case where user has not explicitly set the guest_base, we
+     * probe here that should we set it automatically.
+     */
+    if (guest_base == 0) {
+        /*
+         * Go through ELF program header table and find out whether
+        * any of the segments drop below our current mmap_min_addr and
+         * in that case set guest_base to corresponding address.
+         */
+        for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum;
+            i++, elf_ppnt++) {
+            if (elf_ppnt->p_type != PT_LOAD)
+                continue;
+            if (HOST_PAGE_ALIGN(elf_ppnt->p_vaddr) < mmap_min_addr) {
+                guest_base = HOST_PAGE_ALIGN(mmap_min_addr);
+                qemu_log("setting guest_base=0x%lx\n", guest_base);
+                break;
+            }
+        }
+    }
+#endif /* CONFIG_USE_GUEST_BASE */
+
     /* Do this so that we can load the interpreter, if need be.  We will
        change some of these later */
     info->rss = 0;
index 724387a..293749c 100644 (file)
 
 #define DEBUG_LOGFILE "/tmp/qemu.log"
 
-char *exec_path;
+const char *exec_path;
+
+#if defined(CONFIG_USE_GUEST_BASE)
+unsigned long mmap_min_addr = 0;
+unsigned long guest_base = 0;
+#endif
 
 static const char *interp_prefix = CONFIG_QEMU_PREFIX;
 const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
@@ -2194,6 +2199,9 @@ static void usage(void)
            "-E var=value      sets/modifies targets environment variable(s)\n"
            "-U var            unsets targets environment variable(s)\n"
            "-0 argv0          forces target process argv[0] to be argv0\n"
+#if defined(CONFIG_USE_GUEST_BASE)
+           "-B address        set guest_base address to address\n"
+#endif
            "\n"
            "Debug options:\n"
            "-d options   activate log (logfile=%s)\n"
@@ -2203,12 +2211,22 @@ static void usage(void)
            "Environment variables:\n"
            "QEMU_STRACE       Print system calls and arguments similar to the\n"
            "                  'strace' program.  Enable by setting to any value.\n"
+           "\n"
            "You can use -E and -U options to set/unset environment variables\n"
            "for target process.  It is possible to provide several variables\n"
            "by repeating the option.  For example:\n"
            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
            "Note that if you provide several changes to single variable\n"
            "last change will stay in effect.\n"
+#if defined(CONFIG_USE_GUEST_BASE)
+           "\n"
+           "You can use -B option to load target binary into different\n"
+           "address that is specified in elf headers.  This can be useful\n"
+           "when target binary would be loaded to low addresses and\n"
+           "/proc/sys/vm/mmap_min_addr is set to higher.  For example\n"
+           "     qemu-" TARGET_ARCH " -B 0x100000 ...\n"
+           "loads target binary starting from the first meg.\n"
+#endif
            ,
            TARGET_ARCH,
            interp_prefix,
@@ -2234,7 +2252,7 @@ void init_task_state(TaskState *ts)
  
 int main(int argc, char **argv, char **envp)
 {
-    char *filename = NULL;
+    const char *filename = NULL;
     const char *cpu_model;
     struct target_pt_regs regs1, *regs = &regs1;
     struct image_info info1, *info = &info1;
@@ -2350,6 +2368,10 @@ int main(int argc, char **argv, char **envp)
 #endif
                 exit(1);
             }
+#if defined(CONFIG_USE_GUEST_BASE)
+        } else if (!strcmp(r, "B")) {
+           guest_base = strtol(argv[optind++], NULL, 0);
+#endif
         } else if (!strcmp(r, "drop-ld-preload")) {
             drop_ld_preload = 1;
         } else if (!strcmp(r, "keep-ld-preload")) {
@@ -2432,6 +2454,39 @@ int main(int argc, char **argv, char **envp)
     target_environ = envlist_to_environ(envlist, NULL);
     envlist_free(envlist);
 
+#if defined(CONFIG_USE_GUEST_BASE)
+    /*
+     * Now that page sizes are configured in cpu_init() we can do
+     * proper page alignment for guest_base.
+     */
+    guest_base = HOST_PAGE_ALIGN(guest_base);
+
+    /*
+     * Read in mmap_min_addr kernel parameter and check
+     * whether it is set to some value > 0.  This value is used
+     * later on when doing mmap(2)s to calculate where guest_base
+     * is to set, if needed.
+     *
+     * When user has explicitly set the quest base, we skip this
+     * test.
+     */
+    if (guest_base == 0) {
+        FILE *fp;
+
+        if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
+            unsigned long tmp;
+            if (fscanf(fp, "%lu", &tmp) == 1) {
+                mmap_min_addr = tmp;
+                if (loglevel) {
+                    (void) fprintf(logfile, "kernel mmap_min_addr=%lu\n",
+                        mmap_min_addr);
+                }
+            }
+            fclose(fp);
+        }
+    }
+#endif /* CONFIG_USE_GUEST_BASE */
+
     /*
      * Prepare copy of argv vector for target.
      */
@@ -2472,6 +2527,18 @@ int main(int argc, char **argv, char **envp)
     free(target_environ);
 
     if (qemu_log_enabled()) {
+#if defined(CONFIG_USE_GUEST_BASE)
+        if (guest_base > 0) {
+            qemu_log("guest_base is set to 0x%lx\n", guest_base);
+            qemu_log(
+                "==========================================================\n"
+                "Note that all target addresses below are given in target\n"
+                "address space which is different from host by guest_base.\n"
+                "For example: target address 0x%x becomes 0x%x and so on.\n"
+                "==========================================================\n",
+                (uintptr_t)0x8000, (uintptr_t)g2h(0x8000));
+        }
+#endif
         log_page_dump();
 
         qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
index 4137567..b3c5fbc 100644 (file)
@@ -120,9 +120,12 @@ typedef struct TaskState {
     uint8_t stack[0];
 } __attribute__((aligned(16))) TaskState;
 
-extern char *exec_path;
+extern const char *exec_path;
 void init_task_state(TaskState *ts);
 extern const char *qemu_uname_release;
+#if defined(CONFIG_USE_GUEST_BASE)
+extern unsigned long mmap_min_addr;
+#endif
 
 /* ??? See if we can avoid exposing so much of the loader internals.  */
 /*
index b5d2406..48d263e 100644 (file)
@@ -2597,6 +2597,8 @@ static inline abi_long do_shmat(int shmid, abi_ulong shmaddr, int shmflg,
         }
     }
 
+    *raddr = h2g(*raddr);
+
     mmap_unlock();
     return 0;
 }
index fc9c8a1..4801a9c 100644 (file)
@@ -558,6 +558,12 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     /* add x(r1), r0 */
     tcg_out_modrm_offset(s, 0x03, r0, r1, offsetof(CPUTLBEntry, addend) - 
                          offsetof(CPUTLBEntry, addr_read));
+#elif defined(CONFIG_USE_GUEST_BASE)
+    /*
+     * Add guest_base to all loads.
+     */
+    tcg_out_mov(s, r0, addr_reg);    /* movl addr_reg, r0 */
+    tcg_out_addi(s, r0, GUEST_BASE); /* addl $GUEST_BASE, r0 */
 #else
     r0 = addr_reg;
 #endif
@@ -792,6 +798,12 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
     /* add x(r1), r0 */
     tcg_out_modrm_offset(s, 0x03, r0, r1, offsetof(CPUTLBEntry, addend) - 
                          offsetof(CPUTLBEntry, addr_write));
+#elif defined(CONFIG_USE_GUEST_BASE)
+    /*
+     * Add guest_base to all stores.
+     */
+    tcg_out_mov(s, r0, addr_reg);    /* movl addr_reg, r0 */
+    tcg_out_addi(s, r0, GUEST_BASE); /* addl $GUEST_BASE, r0 */
 #else
     r0 = addr_reg;
 #endif
index ce58fa1..239f97c 100644 (file)
@@ -603,6 +603,12 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     /* add x(r1), r0 */
     tcg_out_modrm_offset(s, 0x03 | P_REXW, r0, r1, offsetof(CPUTLBEntry, addend) - 
                          offsetof(CPUTLBEntry, addr_read));
+#elif defined(CONFIG_USE_GUEST_BASE)
+    /*
+     * Add guest_base to all loads.
+     */
+    tcg_out_modrm(s, 0x8b | rexw, r0, addr_reg); /* movq addr_reg, r0 */
+    tcg_out_addi(s, r0, GUEST_BASE);             /* addq $GUEST_BASE, r0 */
 #else
     r0 = addr_reg;
 #endif    
@@ -774,6 +780,12 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
     /* add x(r1), r0 */
     tcg_out_modrm_offset(s, 0x03 | P_REXW, r0, r1, offsetof(CPUTLBEntry, addend) - 
                          offsetof(CPUTLBEntry, addr_write));
+#elif defined(CONFIG_USE_GUEST_BASE)
+    /*
+     * Add guest_base to all stores.
+     */
+    tcg_out_modrm(s, 0x8b | rexw, r0, addr_reg); /* movq addr_reg, r0 */
+    tcg_out_addi(s, r0, GUEST_BASE);             /* addq $GUEST_BASE, r0 */
 #else
     r0 = addr_reg;
 #endif