qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat
@ 2009-04-29 18:03 riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 02/10] Implement shm* syscalls and fix 64/32bit errors riku.voipio
                   ` (9 more replies)
  0 siblings, 10 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Riku Voipio <riku.voipio@iki.fi>

since mmap_find_vma rewrite is being passively refused, drop it
for now, and just export the existing function.

Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 linux-user/mmap.c |    2 +-
 linux-user/qemu.h |    1 +
 2 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/linux-user/mmap.c b/linux-user/mmap.c
index 6f300a0..aa5813f 100644
--- a/linux-user/mmap.c
+++ b/linux-user/mmap.c
@@ -281,7 +281,7 @@ unsigned long last_brk;
 */
 /* page_init() marks pages used by the host as reserved to be sure not
    to use them. */
-static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
+abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
 {
     abi_ulong addr, addr1, addr_start;
     int prot;
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index 447caf9..15db106 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -229,6 +229,7 @@ int target_msync(abi_ulong start, abi_ulong len, int flags);
 extern unsigned long last_brk;
 void mmap_lock(void);
 void mmap_unlock(void);
+abi_ulong mmap_find_vma(abi_ulong, abi_ulong);
 void cpu_list_lock(void);
 void cpu_list_unlock(void);
 #if defined(USE_NPTL)
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 02/10] Implement shm* syscalls and fix 64/32bit errors
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 03/10] linux-user: implemented ELF coredump support for ARM target [v2] riku.voipio
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Riku Voipio <riku.voipio@iki.fi>

Verified with ltp shm* tests. Following test still
fails

shmt09      3  FAIL

No regressions were observed on either 64bit or 32bit
IA hosts.

Patch based on original patches by:
  Kirill A. Shutemov <kirill@shutemov.name>
  - Implement shm* syscalls
  - Fix and cleanup IPCOP_shm* ipc calls handling

Various whitespace uglifications applied to minimize
patch size.

Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 linux-user/syscall.c |  299 +++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 250 insertions(+), 49 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 0bc9902..ec5beda 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -1924,14 +1924,12 @@ static abi_long do_socketcall(int num, abi_ulong vptr)
 }
 #endif
 
-#ifdef TARGET_NR_ipc
 #define N_SHM_REGIONS	32
 
 static struct shm_region {
     abi_ulong	start;
     abi_ulong	size;
 } shm_regions[N_SHM_REGIONS];
-#endif
 
 struct target_ipc_perm
 {
@@ -2424,6 +2422,224 @@ end:
     return ret;
 }
 
+struct target_shmid_ds
+{
+    struct target_ipc_perm shm_perm;
+    abi_ulong shm_segsz;
+    abi_ulong shm_atime;
+#if TARGET_ABI_BITS == 32
+    abi_ulong __unused1;
+#endif
+    abi_ulong shm_dtime;
+#if TARGET_ABI_BITS == 32
+    abi_ulong __unused2;
+#endif
+    abi_ulong shm_ctime;
+#if TARGET_ABI_BITS == 32
+    abi_ulong __unused3;
+#endif
+    int shm_cpid;
+    int shm_lpid;
+    abi_ulong shm_nattch;
+    unsigned long int __unused4;
+    unsigned long int __unused5;
+};
+
+static inline abi_long target_to_host_shmid_ds(struct shmid_ds *host_sd,
+                                               abi_ulong target_addr)
+{
+    struct target_shmid_ds *target_sd;
+
+    if (!lock_user_struct(VERIFY_READ, target_sd, target_addr, 1))
+        return -TARGET_EFAULT;
+    if (target_to_host_ipc_perm(&(host_sd->shm_perm), target_addr))
+        return -TARGET_EFAULT;
+    __get_user(host_sd->shm_segsz, &target_sd->shm_segsz);
+    __get_user(host_sd->shm_atime, &target_sd->shm_atime);
+    __get_user(host_sd->shm_dtime, &target_sd->shm_dtime);
+    __get_user(host_sd->shm_ctime, &target_sd->shm_ctime);
+    __get_user(host_sd->shm_cpid, &target_sd->shm_cpid);
+    __get_user(host_sd->shm_lpid, &target_sd->shm_lpid);
+    __get_user(host_sd->shm_nattch, &target_sd->shm_nattch);
+    unlock_user_struct(target_sd, target_addr, 0);
+    return 0;
+}
+
+static inline abi_long host_to_target_shmid_ds(abi_ulong target_addr,
+                                               struct shmid_ds *host_sd)
+{
+    struct target_shmid_ds *target_sd;
+
+    if (!lock_user_struct(VERIFY_WRITE, target_sd, target_addr, 0))
+        return -TARGET_EFAULT;
+    if (host_to_target_ipc_perm(target_addr, &(host_sd->shm_perm)))
+        return -TARGET_EFAULT;
+    __put_user(host_sd->shm_segsz, &target_sd->shm_segsz);
+    __put_user(host_sd->shm_atime, &target_sd->shm_atime);
+    __put_user(host_sd->shm_dtime, &target_sd->shm_dtime);
+    __put_user(host_sd->shm_ctime, &target_sd->shm_ctime);
+    __put_user(host_sd->shm_cpid, &target_sd->shm_cpid);
+    __put_user(host_sd->shm_lpid, &target_sd->shm_lpid);
+    __put_user(host_sd->shm_nattch, &target_sd->shm_nattch);
+    unlock_user_struct(target_sd, target_addr, 1);
+    return 0;
+}
+
+struct  target_shminfo {
+    abi_ulong shmmax;
+    abi_ulong shmmin;
+    abi_ulong shmmni;
+    abi_ulong shmseg;
+    abi_ulong shmall;
+};
+
+static inline abi_long host_to_target_shminfo(abi_ulong target_addr,
+                                              struct shminfo *host_shminfo)
+{
+    struct target_shminfo *target_shminfo;
+    if (!lock_user_struct(VERIFY_WRITE, target_shminfo, target_addr, 0))
+        return -TARGET_EFAULT;
+    __put_user(host_shminfo->shmmax, &target_shminfo->shmmax);
+    __put_user(host_shminfo->shmmin, &target_shminfo->shmmin);
+    __put_user(host_shminfo->shmmni, &target_shminfo->shmmni);
+    __put_user(host_shminfo->shmseg, &target_shminfo->shmseg);
+    __put_user(host_shminfo->shmall, &target_shminfo->shmall);
+    unlock_user_struct(target_shminfo, target_addr, 1);
+    return 0;
+}
+
+struct target_shm_info {
+    int used_ids;
+    abi_ulong shm_tot;
+    abi_ulong shm_rss;
+    abi_ulong shm_swp;
+    abi_ulong swap_attempts;
+    abi_ulong swap_successes;
+};
+
+static inline abi_long host_to_target_shm_info(abi_ulong target_addr,
+                                               struct shm_info *host_shm_info)
+{
+    struct target_shm_info *target_shm_info;
+    if (!lock_user_struct(VERIFY_WRITE, target_shm_info, target_addr, 0))
+        return -TARGET_EFAULT;
+    __put_user(host_shm_info->used_ids, &target_shm_info->used_ids);
+    __put_user(host_shm_info->shm_tot, &target_shm_info->shm_tot);
+    __put_user(host_shm_info->shm_rss, &target_shm_info->shm_rss);
+    __put_user(host_shm_info->shm_swp, &target_shm_info->shm_swp);
+    __put_user(host_shm_info->swap_attempts, &target_shm_info->swap_attempts);
+    __put_user(host_shm_info->swap_successes, &target_shm_info->swap_successes);
+    unlock_user_struct(target_shm_info, target_addr, 1);
+    return 0;
+}
+
+static inline abi_long do_shmctl(int shmid, int cmd, abi_long buf)
+{
+    struct shmid_ds dsarg;
+    struct shminfo shminfo;
+    struct shm_info shm_info;
+    abi_long ret = -TARGET_EINVAL;
+
+    cmd &= 0xff;
+
+    switch(cmd) {
+    case IPC_STAT:
+    case IPC_SET:
+    case SHM_STAT:
+        if (target_to_host_shmid_ds(&dsarg, buf))
+            return -TARGET_EFAULT;
+        ret = get_errno(shmctl(shmid, cmd, &dsarg));
+        if (host_to_target_shmid_ds(buf, &dsarg))
+            return -TARGET_EFAULT;
+        break;
+    case IPC_INFO:
+        ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shminfo));
+        if (host_to_target_shminfo(buf, &shminfo))
+            return -TARGET_EFAULT;
+        break;
+    case SHM_INFO:
+        ret = get_errno(shmctl(shmid, cmd, (struct shmid_ds *)&shm_info));
+        if (host_to_target_shm_info(buf, &shm_info))
+            return -TARGET_EFAULT;
+        break;
+    case IPC_RMID:
+    case SHM_LOCK:
+    case SHM_UNLOCK:
+        ret = get_errno(shmctl(shmid, cmd, NULL));
+        break;
+    }
+
+    return ret;
+}
+
+static inline abi_ulong do_shmat(int shmid, abi_ulong shmaddr, int shmflg)
+{
+    abi_long raddr;
+    void *host_raddr;
+    struct shmid_ds shm_info;
+    int i,ret;
+
+    /* find out the length of the shared memory segment */
+    ret = get_errno(shmctl(shmid, IPC_STAT, &shm_info));
+    if (is_error(ret)) {
+        /* can't get length, bail out */
+        return ret;
+    }
+
+    mmap_lock();
+
+    if (shmaddr)
+        host_raddr = shmat(shmid, (void *)g2h(shmaddr), shmflg);
+    else {
+        abi_ulong mmap_start;
+
+        mmap_start = mmap_find_vma(0, shm_info.shm_segsz);
+
+        if (mmap_start == -1) {
+            errno = ENOMEM;
+            host_raddr = (void *)-1;
+        } else
+            host_raddr = shmat(shmid, g2h(mmap_start), shmflg | SHM_REMAP);
+    }
+
+    if (host_raddr == (void *)-1) {
+        mmap_unlock();
+        return get_errno((long)host_raddr);
+    }
+    raddr=h2g((unsigned long)host_raddr);
+
+    page_set_flags(raddr, raddr + shm_info.shm_segsz,
+                   PAGE_VALID | PAGE_READ |
+                   ((shmflg & SHM_RDONLY)? 0 : PAGE_WRITE));
+
+    for (i = 0; i < N_SHM_REGIONS; i++) {
+        if (shm_regions[i].start == 0) {
+            shm_regions[i].start = raddr;
+            shm_regions[i].size = shm_info.shm_segsz;
+            break;
+        }
+    }
+
+    mmap_unlock();
+    return raddr;
+
+}
+
+static inline abi_long do_shmdt(abi_ulong shmaddr)
+{
+    int i;
+
+    for (i = 0; i < N_SHM_REGIONS; ++i) {
+        if (shm_regions[i].start == shmaddr) {
+            shm_regions[i].start = 0;
+            page_set_flags(shmaddr, shm_regions[i].size, 0);
+            break;
+        }
+    }
+
+    return get_errno(shmdt(g2h(shmaddr)));
+}
+
 #ifdef TARGET_NR_ipc
 /* ??? This only works with linear mappings.  */
 /* do_ipc() must return target values and target errnos. */
@@ -2433,8 +2649,6 @@ static abi_long do_ipc(unsigned int call, int first,
 {
     int version;
     abi_long ret = 0;
-    struct shmid_ds shm_info;
-    int i;
 
     version = call >> 16;
     call &= 0xffff;
@@ -2489,48 +2703,24 @@ static abi_long do_ipc(unsigned int call, int first,
         break;
 
     case IPCOP_shmat:
+        switch (version) {
+        default:
         {
             abi_ulong raddr;
-            void *host_addr;
-            /* SHM_* flags are the same on all linux platforms */
-            host_addr = shmat(first, (void *)g2h(ptr), second);
-            if (host_addr == (void *)-1) {
-                ret = get_errno((long)host_addr);
-                break;
-            }
-            raddr = h2g((unsigned long)host_addr);
-            /* find out the length of the shared memory segment */
-            
-            ret = get_errno(shmctl(first, IPC_STAT, &shm_info));
-            if (is_error(ret)) {
-                /* can't get length, bail out */
-                shmdt(host_addr);
-                break;
-            }
-            page_set_flags(raddr, raddr + shm_info.shm_segsz,
-                           PAGE_VALID | PAGE_READ |
-                           ((second & SHM_RDONLY)? 0: PAGE_WRITE));
-            for (i = 0; i < N_SHM_REGIONS; ++i) {
-                if (shm_regions[i].start == 0) {
-                    shm_regions[i].start = raddr;
-                    shm_regions[i].size = shm_info.shm_segsz;
-                    break;
-                }
-            }
+            raddr = do_shmat(first, ptr, second);
+            if (is_error(raddr))
+                return get_errno(raddr);
             if (put_user_ual(raddr, third))
                 return -TARGET_EFAULT;
-            ret = 0;
+            break;
+        }
+        case 1:
+            ret = -TARGET_EINVAL;
+            break;
         }
 	break;
     case IPCOP_shmdt:
-	for (i = 0; i < N_SHM_REGIONS; ++i) {
-	    if (shm_regions[i].start == ptr) {
-		shm_regions[i].start = 0;
-		page_set_flags(ptr, shm_regions[i].size, 0);
-		break;
-	    }
-	}
-	ret = get_errno(shmdt((void *)g2h(ptr)));
+        ret = do_shmdt(ptr);
 	break;
 
     case IPCOP_shmget:
@@ -2540,18 +2730,9 @@ static abi_long do_ipc(unsigned int call, int first,
 
 	/* IPC_* and SHM_* command values are the same on all linux platforms */
     case IPCOP_shmctl:
-        switch(second) {
-        case IPC_RMID:
-        case SHM_LOCK:
-        case SHM_UNLOCK:
-            ret = get_errno(shmctl(first, second, NULL));
-            break;
-        default:
-            goto unimplemented;
-        }
+        ret = do_shmctl(first, second, third);
         break;
     default:
-    unimplemented:
 	gemu_log("Unsupported ipc call: %d (version %d)\n", call, version);
 	ret = -TARGET_ENOSYS;
 	break;
@@ -5310,6 +5491,26 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         ret = do_msgsnd(arg1, arg2, arg3, arg4);
         break;
 #endif
+#ifdef TARGET_NR_shmget
+    case TARGET_NR_shmget:
+        ret = get_errno(shmget(arg1, arg2, arg3));
+        break;
+#endif
+#ifdef TARGET_NR_shmctl
+    case TARGET_NR_shmctl:
+        ret = do_shmctl(arg1, arg2, arg3);
+        break;
+#endif
+#ifdef TARGET_NR_shmat
+    case TARGET_NR_shmat:
+        ret = do_shmat(arg1, arg2, arg3);
+        break;
+#endif
+#ifdef TARGET_NR_shmdt
+    case TARGET_NR_shmdt:
+        ret = do_shmdt(arg1);
+        break;
+#endif
     case TARGET_NR_fsync:
         ret = get_errno(fsync(arg1));
         break;
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 03/10] linux-user: implemented ELF coredump support for ARM target [v2]
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 02/10] Implement shm* syscalls and fix 64/32bit errors riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 04/10] linux-user: added x86 and x86_64 support for ELF coredump riku.voipio
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Mika Westerberg <mika.westerberg@iki.fi>

When target process is killed with signal (such signal that
should dump core) a coredump file is created.  This file is
similar than coredump generated by Linux (there are few exceptions
though).

From: Mika Westerberg <mika.westerberg@iki.fi>

[v2] Riku Voipio: added support for obeying ulimit -c

Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 cpu-all.h              |    2 +
 elf.h                  |   19 +-
 exec.c                 |   69 +++--
 linux-user/elfload.c   |  927 +++++++++++++++++++++++++++++++++++++++++++++++-
 linux-user/linuxload.c |   50 ++--
 linux-user/main.c      |   42 ++-
 linux-user/qemu.h      |   13 +-
 linux-user/signal.c    |   40 ++-
 linux-user/syscall.c   |    6 +
 9 files changed, 1101 insertions(+), 67 deletions(-)

diff --git a/cpu-all.h b/cpu-all.h
index e8cccc6..43a06ba 100644
--- a/cpu-all.h
+++ b/cpu-all.h
@@ -739,6 +739,8 @@ extern unsigned long qemu_host_page_mask;
 #define PAGE_RESERVED  0x0020
 
 void page_dump(FILE *f);
+int walk_memory_regions(void *,
+    int (*fn)(void *, unsigned long, unsigned long, unsigned long));
 int page_get_flags(target_ulong address);
 void page_set_flags(target_ulong start, target_ulong end, int flags);
 int page_check_range(target_ulong start, target_ulong len, int flags);
diff --git a/elf.h b/elf.h
index 861f1d3..139f878 100644
--- a/elf.h
+++ b/elf.h
@@ -1079,7 +1079,23 @@ typedef struct elf64_shdr {
 #define	EI_CLASS	4
 #define	EI_DATA		5
 #define	EI_VERSION	6
-#define	EI_PAD		7
+#define    EI_OSABI	7
+#define	EI_PAD		8
+
+#define ELFOSABI_NONE           0       /* UNIX System V ABI */
+#define ELFOSABI_SYSV           0       /* Alias.  */
+#define ELFOSABI_HPUX           1       /* HP-UX */
+#define ELFOSABI_NETBSD         2       /* NetBSD.  */
+#define ELFOSABI_LINUX          3       /* Linux.  */
+#define ELFOSABI_SOLARIS        6       /* Sun Solaris.  */
+#define ELFOSABI_AIX            7       /* IBM AIX.  */
+#define ELFOSABI_IRIX           8       /* SGI Irix.  */
+#define ELFOSABI_FREEBSD        9       /* FreeBSD.  */
+#define ELFOSABI_TRU64          10      /* Compaq TRU64 UNIX.  */
+#define ELFOSABI_MODESTO        11      /* Novell Modesto.  */
+#define ELFOSABI_OPENBSD        12      /* OpenBSD.  */
+#define ELFOSABI_ARM            97      /* ARM */
+#define ELFOSABI_STANDALONE     255     /* Standalone (embedded) application */
 
 #define	ELFMAG0		0x7f		/* EI_MAG */
 #define	ELFMAG1		'E'
@@ -1106,6 +1122,7 @@ typedef struct elf64_shdr {
 #define NT_PRFPREG	2
 #define NT_PRPSINFO	3
 #define NT_TASKSTRUCT	4
+#define NT_AUXV		6
 #define NT_PRXFPREG     0x46e62b7f      /* copied from gdb5.1/include/elf/common.h */
 
 
diff --git a/exec.c b/exec.c
index 23013fb..63922d1 100644
--- a/exec.c
+++ b/exec.c
@@ -2121,36 +2121,36 @@ int tlb_set_page_exec(CPUState *env, target_ulong vaddr,
     return 0;
 }
 
-/* dump memory mappings */
-void page_dump(FILE *f)
+/*
+ * Walks guest process memory "regions" one by one
+ * and calls callback function 'fn' for each region.
+ */
+int walk_memory_regions(void *priv,
+    int (*fn)(void *, unsigned long, unsigned long, unsigned long))
 {
     unsigned long start, end;
+    PageDesc *p = NULL;
     int i, j, prot, prot1;
-    PageDesc *p;
+    int rc = 0;
 
-    fprintf(f, "%-8s %-8s %-8s %s\n",
-            "start", "end", "size", "prot");
-    start = -1;
-    end = -1;
+    start = end = -1;
     prot = 0;
-    for(i = 0; i <= L1_SIZE; i++) {
-        if (i < L1_SIZE)
-            p = l1_map[i];
-        else
-            p = NULL;
-        for(j = 0;j < L2_SIZE; j++) {
-            if (!p)
-                prot1 = 0;
-            else
-                prot1 = p[j].flags;
+
+    for (i = 0; i <= L1_SIZE; i++) {
+        p = (i < L1_SIZE) ? l1_map[i] : NULL;
+        for (j = 0; j < L2_SIZE; j++) {
+            prot1 = (p == NULL) ? 0 : p[j].flags;
+            /*
+             * "region" is one continuous chunk of memory
+             * that has same protection flags set.
+             */
             if (prot1 != prot) {
                 end = (i << (32 - L1_BITS)) | (j << TARGET_PAGE_BITS);
                 if (start != -1) {
-                    fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
-                            start, end, end - start,
-                            prot & PAGE_READ ? 'r' : '-',
-                            prot & PAGE_WRITE ? 'w' : '-',
-                            prot & PAGE_EXEC ? 'x' : '-');
+                    rc = (*fn)(priv, start, end, prot);
+                    /* callback can stop iteration by returning != 0 */
+                    if (rc != 0)
+                        return (rc);
                 }
                 if (prot1 != 0)
                     start = end;
@@ -2158,10 +2158,33 @@ void page_dump(FILE *f)
                     start = -1;
                 prot = prot1;
             }
-            if (!p)
+            if (p == NULL)
                 break;
         }
     }
+    return (rc);
+}
+
+static int dump_region(void *priv, unsigned long start,
+    unsigned long end, unsigned long prot)
+{
+    FILE *f = (FILE *)priv;
+
+    (void) fprintf(f, "%08lx-%08lx %08lx %c%c%c\n",
+        start, end, end - start,
+        ((prot & PAGE_READ) ? 'r' : '-'),
+        ((prot & PAGE_WRITE) ? 'w' : '-'),
+        ((prot & PAGE_EXEC) ? 'x' : '-'));
+
+    return (0);
+}
+
+/* dump memory mappings */
+void page_dump(FILE *f)
+{
+    (void) fprintf(f, "%-8s %-8s %-8s %s\n",
+            "start", "end", "size", "prot");
+    walk_memory_regions(f, dump_region);
 }
 
 int page_get_flags(target_ulong address)
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 6de30f4..c210e73 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -1,4 +1,6 @@
 /* This is the Linux kernel elf-loading code, ported into user space */
+#include <sys/time.h>
+#include <sys/param.h>
 
 #include <stdio.h>
 #include <sys/types.h>
@@ -6,8 +8,10 @@
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <sys/resource.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 
 #include "qemu.h"
 #include "disas.h"
@@ -21,6 +25,8 @@
 #undef ELF_ARCH
 #endif
 
+#define ELF_OSABI   ELFOSABI_SYSV
+
 /* from personality.h */
 
 /*
@@ -160,7 +166,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
 }
 #endif
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE	4096
 
 #endif
@@ -198,6 +203,37 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
     regs->ARM_r10 = infop->start_data;
 }
 
+typedef uint32_t elf_greg_t;
+typedef uint16_t target_uid_t;
+typedef uint16_t target_gid_t;
+typedef int32_t  target_pid_t;
+
+#define ELF_NREG    18
+typedef elf_greg_t  elf_gregset_t[ELF_NREG];
+
+static void elf_core_copy_regs(elf_gregset_t *regs, const CPUState *env)
+{
+    (*regs)[0] = env->regs[0];
+    (*regs)[1] = env->regs[1];
+    (*regs)[2] = env->regs[2];
+    (*regs)[3] = env->regs[3];
+    (*regs)[4] = env->regs[4];
+    (*regs)[5] = env->regs[5];
+    (*regs)[6] = env->regs[6];
+    (*regs)[7] = env->regs[7];
+    (*regs)[8] = env->regs[8];
+    (*regs)[9] = env->regs[9];
+    (*regs)[10] = env->regs[10];
+    (*regs)[11] = env->regs[11];
+    (*regs)[12] = env->regs[12];
+    (*regs)[13] = env->regs[13];
+    (*regs)[14] = env->regs[14];
+    (*regs)[15] = env->regs[15];
+
+    (*regs)[16] = cpsr_read((CPUState *)env);
+    (*regs)[17] = env->regs[0]; /* XXX */
+}
+
 #define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE	4096
 
@@ -360,7 +396,6 @@ static inline void init_thread(struct target_pt_regs *_regs, struct image_info *
     _regs->gpr[5] = pos;
 }
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE	4096
 
 #endif
@@ -390,7 +425,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
     regs->regs[29] = infop->start_stack;
 }
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE        4096
 
 #endif /* TARGET_MIPS */
@@ -412,7 +446,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
   regs->regs[15] = infop->start_stack;
 }
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE        4096
 
 #endif
@@ -432,7 +465,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
   regs->erp = infop->entry;
 }
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE        8192
 
 #endif
@@ -457,7 +489,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
     regs->pc = infop->entry;
 }
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE	8192
 
 #endif
@@ -482,7 +513,6 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
            regs->unique, infop->start_data);
 }
 
-#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE        8192
 
 #endif /* TARGET_ALPHA */
@@ -600,6 +630,20 @@ static void bswap_sym(struct elf_sym *sym)
 }
 #endif
 
+#ifdef USE_ELF_CORE_DUMP
+static int elf_core_dump(int, const CPUState *);
+
+#ifdef BSWAP_NEEDED
+static void bswap_note(struct elf_note *en)
+{
+    bswaptls(&en->n_namesz);
+    bswaptls(&en->n_descsz);
+    bswaptls(&en->n_type);
+}
+#endif /* BSWAP_NEEDED */
+
+#endif /* USE_ELF_CORE_DUMP */
+
 /*
  * 'copy_elf_strings()' copies argument/envelope strings from user
  * memory to free pages in kernel mem. These are in a format ready
@@ -824,6 +868,8 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
 #endif
 #undef NEW_AUX_ENT
 
+        info->saved_auxv = sp;
+
         sp = loader_build_argptr(envc, argc, sp, p, !ibcs);
         return sp;
 }
@@ -1506,9 +1552,876 @@ int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
 
     info->entry = elf_entry;
 
+#ifdef USE_ELF_CORE_DUMP
+    bprm->core_dump = &elf_core_dump;
+#endif
+
     return 0;
 }
 
+#ifdef USE_ELF_CORE_DUMP
+
+/*
+ * Definitions to generate Intel SVR4-like core files.
+ * These mostly have the same names as the SVR4 types with "elf_"
+ * tacked on the front to prevent clashes with linux definitions,
+ * and the typedef forms have been avoided.  This is mostly like
+ * the SVR4 structure, but more Linuxy, with things that Linux does
+ * not support and which gdb doesn't really use excluded.
+ *
+ * Fields we don't dump (their contents is zero) in linux-user qemu
+ * are marked with XXX.
+ *
+ * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
+ *
+ * Porting ELF coredump for target is (quite) simple process.  First you
+ * define ELF_USE_CORE_DUMP in target ELF code (where init_thread() for
+ * the target resides):
+ *
+ * #define USE_ELF_CORE_DUMP
+ *
+ * Next you define type of register set used for dumping.  ELF specification
+ * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
+ *
+ * typedef <target_regtype> elf_greg_t;
+ * #define ELF_NREG <number of registers>
+ * typedef elf_greg_t elf_gregset_t[ELF_NREG];
+ *
+ * Then define following types to match target types.  Actual types can
+ * be found from linux kernel (arch/<ARCH>/include/asm/posix_types.h):
+ *
+ * typedef <target_uid_type> target_uid_t;
+ * typedef <target_gid_type> target_gid_t;
+ * typedef <target_pid_type> target_pid_t;
+ *
+ * Last step is to implement target specific function that copies registers
+ * from given cpu into just specified register set.  Prototype is:
+ *
+ * static void elf_core_copy_regs(elf_gregset_t *regs, const CPUState *env);
+ *
+ * Parameters:
+ *     regs - copy register values into here (allocated and zeroed by caller)
+ *     env - copy registers from here
+ *
+ * Example for ARM target is provided in this file.
+ */
+
+/* An ELF note in memory */
+struct memelfnote {
+    const char *name;
+    size_t     namesz;
+    size_t     namesz_rounded;
+    int        type;
+    size_t     datasz;
+    void       *data;
+    size_t     notesz;
+};
+
+struct elf_siginfo {
+    int  si_signo; /* signal number */
+    int  si_code;  /* extra code */
+    int  si_errno; /* errno */
+};
+
+struct elf_prstatus {
+    struct elf_siginfo pr_info;      /* Info associated with signal */
+    short              pr_cursig;    /* Current signal */
+    target_ulong       pr_sigpend;   /* XXX */
+    target_ulong       pr_sighold;   /* XXX */
+    target_pid_t       pr_pid;
+    target_pid_t       pr_ppid;
+    target_pid_t       pr_pgrp;
+    target_pid_t       pr_sid;
+    struct target_timeval pr_utime;  /* XXX User time */
+    struct target_timeval pr_stime;  /* XXX System time */
+    struct target_timeval pr_cutime; /* XXX Cumulative user time */
+    struct target_timeval pr_cstime; /* XXX Cumulative system time */
+    elf_gregset_t      pr_reg;       /* GP registers */
+    int                pr_fpvalid;   /* XXX */
+};
+
+#define ELF_PRARGSZ     (80) /* Number of chars for args */
+
+struct elf_prpsinfo {
+    char         pr_state;       /* numeric process state */
+    char         pr_sname;       /* char for pr_state */
+    char         pr_zomb;        /* zombie */
+    char         pr_nice;        /* nice val */
+    target_ulong pr_flag;        /* flags */
+    target_uid_t pr_uid;
+    target_gid_t pr_gid;
+    target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
+    /* Lots missing */
+    char    pr_fname[16];           /* filename of executable */
+    char    pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
+};
+
+/* Here is the structure in which status of each thread is captured. */
+struct elf_thread_status {
+    TAILQ_ENTRY(elf_thread_status)  ets_link;
+    struct elf_prstatus prstatus;   /* NT_PRSTATUS */
+#if 0
+    elf_fpregset_t fpu;             /* NT_PRFPREG */
+    struct task_struct *thread;
+    elf_fpxregset_t xfpu;           /* ELF_CORE_XFPREG_TYPE */
+#endif
+    struct memelfnote notes[1];
+    int num_notes;
+};
+
+struct elf_note_info {
+    struct memelfnote   *notes;
+    struct elf_prstatus *prstatus;  /* NT_PRSTATUS */
+    struct elf_prpsinfo *psinfo;    /* NT_PRPSINFO */
+
+    TAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
+#if 0
+    /*
+     * Current version of ELF coredump doesn't support
+     * dumping fp regs etc.
+     */
+    elf_fpregset_t *fpu;
+    elf_fpxregset_t *xfpu;
+    int thread_status_size;
+#endif
+    int notes_size;
+    int numnote;
+};
+
+struct vm_area_struct {
+    abi_ulong   vma_start;  /* start vaddr of memory region */
+    abi_ulong   vma_end;    /* end vaddr of memory region */
+    abi_ulong   vma_flags;  /* protection etc. flags for the region */
+    TAILQ_ENTRY(vm_area_struct) vma_link;
+};
+
+struct mm_struct {
+    TAILQ_HEAD(, vm_area_struct) mm_mmap;
+    int mm_count;           /* number of mappings */
+};
+
+static struct mm_struct *vma_init(void);
+static void vma_delete(struct mm_struct *);
+static int vma_add_mapping(struct mm_struct *, abi_ulong,
+    abi_ulong, abi_ulong);
+static int vma_get_mapping_count(const struct mm_struct *);
+static struct vm_area_struct *vma_first(const struct mm_struct *);
+static struct vm_area_struct *vma_next(struct vm_area_struct *);
+static abi_ulong vma_dump_size(const struct vm_area_struct *);
+static int vma_walker(void *priv, unsigned long start, unsigned long end,
+    unsigned long flags);
+
+static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
+static void fill_note(struct memelfnote *, const char *, int,
+    unsigned int, void *);
+static void fill_prstatus(struct elf_prstatus *, const TaskState *, int);
+static int fill_psinfo(struct elf_prpsinfo *, const TaskState *);
+static void fill_auxv_note(struct memelfnote *, const TaskState *);
+static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
+static size_t note_size(const struct memelfnote *);
+static void free_note_info(struct elf_note_info *);
+static int fill_note_info(struct elf_note_info *, long, const CPUState *);
+static void fill_thread_info(struct elf_note_info *, const CPUState *);
+static int core_dump_filename(const TaskState *, char *, size_t);
+
+static int dump_write(int, const void *, size_t);
+static int write_note(struct memelfnote *, int);
+static int write_note_info(struct elf_note_info *, int);
+
+#ifdef BSWAP_NEEDED
+static void bswap_prstatus(struct elf_prstatus *);
+static void bswap_psinfo(struct elf_prpsinfo *);
+
+static void bswap_prstatus(struct elf_prstatus *prstatus)
+{
+    prstatus->pr_info.si_signo = tswapl(prstatus->pr_info.si_signo);
+    prstatus->pr_info.si_code = tswapl(prstatus->pr_info.si_code);
+    prstatus->pr_info.si_errno = tswapl(prstatus->pr_info.si_errno);
+    prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
+    prstatus->pr_sigpend = tswapl(prstatus->pr_sigpend);
+    prstatus->pr_sighold = tswapl(prstatus->pr_sighold);
+    prstatus->pr_pid = tswap32(prstatus->pr_pid);
+    prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
+    prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
+    prstatus->pr_sid = tswap32(prstatus->pr_sid);
+    /* cpu times are not filled, so we skip them */
+    /* regs should be in correct format already */
+    prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
+}
+
+static void bswap_psinfo(struct elf_prpsinfo *psinfo)
+{
+    psinfo->pr_flag = tswapl(psinfo->pr_flag);
+    psinfo->pr_uid = tswap16(psinfo->pr_uid);
+    psinfo->pr_gid = tswap16(psinfo->pr_gid);
+    psinfo->pr_pid = tswap32(psinfo->pr_pid);
+    psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
+    psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
+    psinfo->pr_sid = tswap32(psinfo->pr_sid);
+}
+#endif /* BSWAP_NEEDED */
+
+/*
+ * Minimal support for linux memory regions.  These are needed
+ * when we are finding out what memory exactly belongs to
+ * emulated process.  No locks needed here, as long as
+ * thread that received the signal is stopped.
+ */
+
+static struct mm_struct *vma_init(void)
+{
+    struct mm_struct *mm;
+
+    if ((mm = qemu_malloc(sizeof (*mm))) == NULL)
+        return (NULL);
+
+    mm->mm_count = 0;
+    TAILQ_INIT(&mm->mm_mmap);
+
+    return (mm);
+}
+
+static void vma_delete(struct mm_struct *mm)
+{
+    struct vm_area_struct *vma;
+
+    while ((vma = vma_first(mm)) != NULL) {
+        TAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
+        qemu_free(vma);
+    }
+    qemu_free(mm);
+}
+
+static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
+    abi_ulong end, abi_ulong flags)
+{
+    struct vm_area_struct *vma;
+
+    if ((vma = qemu_mallocz(sizeof (*vma))) == NULL)
+        return (-1);
+
+    vma->vma_start = start;
+    vma->vma_end = end;
+    vma->vma_flags = flags;
+
+    TAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
+    mm->mm_count++;
+
+    return (0);
+}
+
+static struct vm_area_struct *vma_first(const struct mm_struct *mm)
+{
+    return (TAILQ_FIRST(&mm->mm_mmap));
+}
+
+static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
+{
+    return (TAILQ_NEXT(vma, vma_link));
+}
+
+static int vma_get_mapping_count(const struct mm_struct *mm)
+{
+    return (mm->mm_count);
+}
+
+/*
+ * Calculate file (dump) size of given memory region.
+ */
+static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
+{
+    /* if we cannot even read the first page, skip it */
+    if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
+        return (0);
+
+    /*
+     * Usually we don't dump executable pages as they contain
+     * non-writable code that debugger can read directly from
+     * target library etc.  However, thread stacks are marked
+     * also executable so we read in first page of given region
+     * and check whether it contains elf header.  If there is
+     * no elf header, we dump it.
+     */
+    if (vma->vma_flags & PROT_EXEC) {
+        char page[TARGET_PAGE_SIZE];
+
+        copy_from_user(page, vma->vma_start, sizeof (page));
+        if ((page[EI_MAG0] == ELFMAG0) &&
+            (page[EI_MAG1] == ELFMAG1) &&
+            (page[EI_MAG2] == ELFMAG2) &&
+            (page[EI_MAG3] == ELFMAG3)) {
+            /*
+             * Mappings are possibly from ELF binary.  Don't dump
+             * them.
+             */
+            return (0);
+        }
+    }
+
+    return (vma->vma_end - vma->vma_start);
+}
+
+static int vma_walker(void *priv, unsigned long start, unsigned long end,
+    unsigned long flags)
+{
+    struct mm_struct *mm = (struct mm_struct *)priv;
+
+    /*
+     * Don't dump anything that qemu has reserved for internal use.
+     */
+    if (flags & PAGE_RESERVED)
+        return (0);
+
+    vma_add_mapping(mm, start, end, flags);
+    return (0);
+}
+
+static void fill_note(struct memelfnote *note, const char *name, int type,
+    unsigned int sz, void *data)
+{
+    unsigned int namesz;
+
+    namesz = strlen(name) + 1;
+    note->name = name;
+    note->namesz = namesz;
+    note->namesz_rounded = roundup(namesz, sizeof (int32_t));
+    note->type = type;
+    note->datasz = roundup(sz, sizeof (int32_t));;
+    note->data = data;
+
+    /*
+     * We calculate rounded up note size here as specified by
+     * ELF document.
+     */
+    note->notesz = sizeof (struct elf_note) +
+        note->namesz_rounded + note->datasz;
+}
+
+static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
+    uint32_t flags)
+{
+    (void) memset(elf, 0, sizeof(*elf));
+
+    (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
+    elf->e_ident[EI_CLASS] = ELF_CLASS;
+    elf->e_ident[EI_DATA] = ELF_DATA;
+    elf->e_ident[EI_VERSION] = EV_CURRENT;
+    elf->e_ident[EI_OSABI] = ELF_OSABI;
+
+    elf->e_type = ET_CORE;
+    elf->e_machine = machine;
+    elf->e_version = EV_CURRENT;
+    elf->e_phoff = sizeof(struct elfhdr);
+    elf->e_flags = flags;
+    elf->e_ehsize = sizeof(struct elfhdr);
+    elf->e_phentsize = sizeof(struct elf_phdr);
+    elf->e_phnum = segs;
+
+#ifdef BSWAP_NEEDED
+    bswap_ehdr(elf);
+#endif
+}
+
+static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
+{
+    phdr->p_type = PT_NOTE;
+    phdr->p_offset = offset;
+    phdr->p_vaddr = 0;
+    phdr->p_paddr = 0;
+    phdr->p_filesz = sz;
+    phdr->p_memsz = 0;
+    phdr->p_flags = 0;
+    phdr->p_align = 0;
+
+#ifdef BSWAP_NEEDED
+    bswap_phdr(phdr);
+#endif
+}
+
+static size_t note_size(const struct memelfnote *note)
+{
+    return (note->notesz);
+}
+
+static void fill_prstatus(struct elf_prstatus *prstatus,
+    const TaskState *ts, int signr)
+{
+    (void) memset(prstatus, 0, sizeof (*prstatus));
+    prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
+    prstatus->pr_pid = ts->ts_tid;
+    prstatus->pr_ppid = getppid();
+    prstatus->pr_pgrp = getpgrp();
+    prstatus->pr_sid = getsid(0);
+
+#ifdef BSWAP_NEEDED
+    bswap_prstatus(prstatus);
+#endif
+}
+
+static int fill_psinfo(struct elf_prpsinfo *psinfo, const TaskState *ts)
+{
+    char *filename, *base_filename;
+    unsigned int i, len;
+
+    (void) memset(psinfo, 0, sizeof (*psinfo));
+
+    len = ts->info->arg_end - ts->info->arg_start;
+    if (len >= ELF_PRARGSZ)
+        len = ELF_PRARGSZ - 1;
+    if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
+        return -EFAULT;
+    for (i = 0; i < len; i++)
+        if (psinfo->pr_psargs[i] == 0)
+            psinfo->pr_psargs[i] = ' ';
+    psinfo->pr_psargs[len] = 0;
+
+    psinfo->pr_pid = getpid();
+    psinfo->pr_ppid = getppid();
+    psinfo->pr_pgrp = getpgrp();
+    psinfo->pr_sid = getsid(0);
+    psinfo->pr_uid = getuid();
+    psinfo->pr_gid = getgid();
+
+    filename = strdup(ts->bprm->filename);
+    base_filename = strdup(basename(filename));
+    (void) strncpy(psinfo->pr_fname, base_filename,
+        sizeof(psinfo->pr_fname));
+    free(base_filename);
+    free(filename);
+
+#ifdef BSWAP_NEEDED
+    bswap_psinfo(psinfo);
+#endif
+    return (0);
+}
+
+static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
+{
+    elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
+    elf_addr_t orig_auxv = auxv;
+    abi_ulong val;
+    void *ptr;
+    int i, len;
+
+    /*
+     * Auxiliary vector is stored in target process stack.  It contains
+     * {type, value} pairs that we need to dump into note.  This is not
+     * strictly necessary but we do it here for sake of completeness.
+     */
+
+    /* find out lenght of the vector, AT_NULL is terminator */
+    i = len = 0;
+    do {
+        get_user_ual(val, auxv);
+        i += 2;
+        auxv += 2 * sizeof (elf_addr_t);
+    } while (val != AT_NULL);
+    len = i * sizeof (elf_addr_t);
+
+    /* read in whole auxv vector and copy it to memelfnote */
+    ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
+    if (ptr != NULL) {
+        fill_note(note, "CORE", NT_AUXV, len, ptr);
+        unlock_user(ptr, auxv, len);
+    }
+}
+
+/*
+ * Constructs name of coredump file.  We have following convention
+ * for the name:
+ *     qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
+ *
+ * Returns 0 in case of success, -1 otherwise (errno is set).
+ */
+static int core_dump_filename(const TaskState *ts, char *buf,
+    size_t bufsize)
+{
+    char timestamp[64];
+    char *filename = NULL;
+    char *base_filename = NULL;
+    struct timeval tv;
+    struct tm tm;
+
+    assert(bufsize >= PATH_MAX);
+
+    if (gettimeofday(&tv, NULL) < 0) {
+        (void) fprintf(stderr, "unable to get current timestamp: %s",
+            strerror(errno));
+        return (-1);
+    }
+
+    filename = strdup(ts->bprm->filename);
+    base_filename = strdup(basename(filename));
+    (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
+        localtime_r(&tv.tv_sec, &tm));
+    (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
+        base_filename, timestamp, (int)getpid());
+    free(base_filename);
+    free(filename);
+
+    return (0);
+}
+
+static int dump_write(int fd, const void *ptr, size_t size)
+{
+    const char *bufp = (const char *)ptr;
+    ssize_t bytes_written, bytes_left;
+    struct rlimit dumpsize;
+    off_t pos;
+
+    bytes_written = 0;
+    getrlimit(RLIMIT_CORE, &dumpsize);
+    if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
+        if (errno == ESPIPE) { /* not a seekable stream */
+            bytes_left = size;
+        } else {
+            return pos;
+        }
+    } else {
+        if (dumpsize.rlim_cur <= pos) {
+            return -1;
+        } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
+            bytes_left = size;
+        } else {
+            size_t limit_left=dumpsize.rlim_cur - pos;
+            bytes_left = limit_left >= size ? size : limit_left ;
+        }
+    }
+
+    /*
+     * In normal conditions, single write(2) should do but
+     * in case of socket etc. this mechanism is more portable.
+     */
+    do {
+        bytes_written = write(fd, bufp, bytes_left);
+        if (bytes_written < 0) {
+            if (errno == EINTR)
+                continue;
+            return (-1);
+        } else if (bytes_written == 0) { /* eof */
+            return (-1);
+        }
+        bufp += bytes_written;
+        bytes_left -= bytes_written;
+    } while (bytes_left > 0);
+
+    return (0);
+}
+
+static int write_note(struct memelfnote *men, int fd)
+{
+    struct elf_note en;
+
+    en.n_namesz = men->namesz;
+    en.n_type = men->type;
+    en.n_descsz = men->datasz;
+
+#ifdef BSWAP_NEEDED
+    bswap_note(&en);
+#endif
+
+    if (dump_write(fd, &en, sizeof(en)) != 0)
+        return (-1);
+    if (dump_write(fd, men->name, men->namesz_rounded) != 0)
+        return (-1);
+    if (dump_write(fd, men->data, men->datasz) != 0)
+        return (-1);
+
+    return (0);
+}
+
+static void fill_thread_info(struct elf_note_info *info, const CPUState *env)
+{
+    TaskState *ts = (TaskState *)env->opaque;
+    struct elf_thread_status *ets;
+
+    ets = qemu_mallocz(sizeof (*ets));
+    ets->num_notes = 1; /* only prstatus is dumped */
+    fill_prstatus(&ets->prstatus, ts, 0);
+    elf_core_copy_regs(&ets->prstatus.pr_reg, env);
+    fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
+        &ets->prstatus);
+
+    TAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
+
+    info->notes_size += note_size(&ets->notes[0]);
+}
+
+static int fill_note_info(struct elf_note_info *info,
+    long signr, const CPUState *env)
+{
+#define NUMNOTES 3
+    CPUState *cpu = NULL;
+    TaskState *ts = (TaskState *)env->opaque;
+    int i;
+
+    (void) memset(info, 0, sizeof (*info));
+
+    TAILQ_INIT(&info->thread_list);
+
+    info->notes = qemu_mallocz(NUMNOTES * sizeof (struct memelfnote));
+    if (info->notes == NULL)
+        return (-ENOMEM);
+    info->prstatus = qemu_mallocz(sizeof (*info->prstatus));
+    if (info->prstatus == NULL)
+        return (-ENOMEM);
+    info->psinfo = qemu_mallocz(sizeof (*info->psinfo));
+    if (info->prstatus == NULL)
+        return (-ENOMEM);
+
+    /*
+     * First fill in status (and registers) of current thread
+     * including process info & aux vector.
+     */
+    fill_prstatus(info->prstatus, ts, signr);
+    elf_core_copy_regs(&info->prstatus->pr_reg, env);
+    fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
+        sizeof (*info->prstatus), info->prstatus);
+    fill_psinfo(info->psinfo, ts);
+    fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
+        sizeof (*info->psinfo), info->psinfo);
+    fill_auxv_note(&info->notes[2], ts);
+    info->numnote = 3;
+
+    info->notes_size = 0;
+    for (i = 0; i < info->numnote; i++)
+        info->notes_size += note_size(&info->notes[i]);
+
+    /* read and fill status of all threads */
+    cpu_list_lock();
+    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
+        if (cpu == thread_env)
+            continue;
+        fill_thread_info(info, cpu);
+    }
+    cpu_list_unlock();
+
+    return (0);
+}
+
+static void free_note_info(struct elf_note_info *info)
+{
+    struct elf_thread_status *ets;
+
+    while (!TAILQ_EMPTY(&info->thread_list)) {
+        ets = TAILQ_FIRST(&info->thread_list);
+        TAILQ_REMOVE(&info->thread_list, ets, ets_link);
+        qemu_free(ets);
+    }
+
+    qemu_free(info->prstatus);
+    qemu_free(info->psinfo);
+    qemu_free(info->notes);
+}
+
+static int write_note_info(struct elf_note_info *info, int fd)
+{
+    struct elf_thread_status *ets;
+    int i, error = 0;
+
+    /* write prstatus, psinfo and auxv for current thread */
+    for (i = 0; i < info->numnote; i++)
+        if ((error = write_note(&info->notes[i], fd)) != 0)
+            return (error);
+
+    /* write prstatus for each thread */
+    for (ets = info->thread_list.tqh_first; ets != NULL;
+        ets = ets->ets_link.tqe_next) {
+        if ((error = write_note(&ets->notes[0], fd)) != 0)
+            return (error);
+    }
+
+    return (0);
+}
+
+/*
+ * Write out ELF coredump.
+ *
+ * See documentation of ELF object file format in:
+ * http://www.caldera.com/developers/devspecs/gabi41.pdf
+ *
+ * Coredump format in linux is following:
+ *
+ * 0   +----------------------+         \
+ *     | ELF header           | ET_CORE  |
+ *     +----------------------+          |
+ *     | ELF program headers  |          |--- headers
+ *     | - NOTE section       |          |
+ *     | - PT_LOAD sections   |          |
+ *     +----------------------+         /
+ *     | NOTEs:               |
+ *     | - NT_PRSTATUS        |
+ *     | - NT_PRSINFO         |
+ *     | - NT_AUXV            |
+ *     +----------------------+ <-- aligned to target page
+ *     | Process memory dump  |
+ *     :                      :
+ *     .                      .
+ *     :                      :
+ *     |                      |
+ *     +----------------------+
+ *
+ * NT_PRSTATUS -> struct elf_prstatus (per thread)
+ * NT_PRSINFO  -> struct elf_prpsinfo
+ * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
+ *
+ * Format follows System V format as close as possible.  Current
+ * version limitations are as follows:
+ *     - no floating point registers are dumped
+ *
+ * Function returns 0 in case of success, negative errno otherwise.
+ *
+ * TODO: make this work also during runtime: it should be
+ * possible to force coredump from running process and then
+ * continue processing.  For example qemu could set up SIGUSR2
+ * handler (provided that target process haven't registered
+ * handler for that) that does the dump when signal is received.
+ */
+static int elf_core_dump(int signr, const CPUState *env)
+{
+    const TaskState *ts = (const TaskState *)env->opaque;
+    struct vm_area_struct *vma = NULL;
+    char corefile[PATH_MAX];
+    struct elf_note_info info;
+    struct elfhdr elf;
+    struct elf_phdr phdr;
+    struct rlimit dumpsize;
+    struct mm_struct *mm = NULL;
+    off_t offset = 0, data_offset = 0;
+    int segs = 0;
+    int fd = -1;
+
+    errno = 0;
+    getrlimit(RLIMIT_CORE, &dumpsize);
+    if (dumpsize.rlim_cur == 0)
+       return 0;
+
+    if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
+        return (-errno);
+
+    if ((fd = open(corefile, O_WRONLY | O_CREAT,
+        S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
+        return (-errno);
+
+    /*
+     * Walk through target process memory mappings and
+     * set up structure containing this information.  After
+     * this point vma_xxx functions can be used.
+     */
+    if ((mm = vma_init()) == NULL)
+        goto out;
+
+    walk_memory_regions(mm, vma_walker);
+    segs = vma_get_mapping_count(mm);
+
+    /*
+     * Construct valid coredump ELF header.  We also
+     * add one more segment for notes.
+     */
+    fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
+    if (dump_write(fd, &elf, sizeof (elf)) != 0)
+        goto out;
+
+    /* fill in in-memory version of notes */
+    if (fill_note_info(&info, signr, env) < 0)
+        goto out;
+
+    offset += sizeof (elf);                             /* elf header */
+    offset += (segs + 1) * sizeof (struct elf_phdr);    /* program headers */
+
+    /* write out notes program header */
+    fill_elf_note_phdr(&phdr, info.notes_size, offset);
+
+    offset += info.notes_size;
+    if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
+        goto out;
+
+    /*
+     * ELF specification wants data to start at page boundary so
+     * we align it here.
+     */
+    offset = roundup(offset, ELF_EXEC_PAGESIZE);
+
+    /*
+     * Write program headers for memory regions mapped in
+     * the target process.
+     */
+    for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
+        (void) memset(&phdr, 0, sizeof (phdr));
+
+        phdr.p_type = PT_LOAD;
+        phdr.p_offset = offset;
+        phdr.p_vaddr = vma->vma_start;
+        phdr.p_paddr = 0;
+        phdr.p_filesz = vma_dump_size(vma);
+        offset += phdr.p_filesz;
+        phdr.p_memsz = vma->vma_end - vma->vma_start;
+        phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
+        if (vma->vma_flags & PROT_WRITE)
+            phdr.p_flags |= PF_W;
+        if (vma->vma_flags & PROT_EXEC)
+            phdr.p_flags |= PF_X;
+        phdr.p_align = ELF_EXEC_PAGESIZE;
+
+        dump_write(fd, &phdr, sizeof (phdr));
+    }
+
+    /*
+     * Next we write notes just after program headers.  No
+     * alignment needed here.
+     */
+    if (write_note_info(&info, fd) < 0)
+        goto out;
+
+    /* align data to page boundary */
+    data_offset = lseek(fd, 0, SEEK_CUR);
+    data_offset = TARGET_PAGE_ALIGN(data_offset);
+    if (lseek(fd, data_offset, SEEK_SET) != data_offset)
+        goto out;
+
+    /*
+     * Finally we can dump process memory into corefile as well.
+     */
+    for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
+        abi_ulong addr;
+        abi_ulong end;
+
+        end = vma->vma_start + vma_dump_size(vma);
+
+        for (addr = vma->vma_start; addr < end;
+            addr += TARGET_PAGE_SIZE) {
+            char page[TARGET_PAGE_SIZE];
+            int error;
+
+            /*
+             *  Read in page from target process memory and
+             *  write it to coredump file.
+             */
+            error = copy_from_user(page, addr, sizeof (page));
+            if (error != 0) {
+                (void) fprintf(stderr, "unable to dump " TARGET_FMT_lx "\n",
+                    addr);
+                errno = -error;
+                goto out;
+            }
+            if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
+                goto out;
+        }
+    }
+
+out:
+    free_note_info(&info);
+    if (mm != NULL)
+        vma_delete(mm);
+    (void) close(fd);
+
+    if (errno != 0)
+        return (-errno);
+    return (0);
+}
+
+#endif /* USE_ELF_CORE_DUMP */
+
 static int load_aout_interp(void * exptr, int interp_fd)
 {
     printf("a.out interpreter not yet supported\n");
diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c
index ada7c69..14c433e 100644
--- a/linux-user/linuxload.c
+++ b/linux-user/linuxload.c
@@ -115,6 +115,7 @@ static int prepare_binprm(struct linux_binprm *bprm)
 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
                               abi_ulong stringp, int push_ptr)
 {
+    TaskState *ts = (TaskState *)thread_env->opaque;
     int n = sizeof(abi_ulong);
     abi_ulong envp;
     abi_ulong argv;
@@ -133,13 +134,14 @@ abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
     sp -= n;
     /* FIXME - handle put_user() failures */
     put_user_ual(argc, sp);
-
+    ts->info->arg_start = stringp;
     while (argc-- > 0) {
         /* FIXME - handle put_user() failures */
         put_user_ual(stringp, argv);
         argv += n;
         stringp += target_strlen(stringp) + 1;
     }
+    ts->info->arg_end = stringp;
     /* FIXME - handle put_user() failures */
     put_user_ual(0, argv);
     while (envc-- > 0) {
@@ -155,45 +157,45 @@ abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
 }
 
 int loader_exec(const char * filename, char ** argv, char ** envp,
-             struct target_pt_regs * regs, struct image_info *infop)
+             struct target_pt_regs * regs, struct image_info *infop,
+             struct linux_binprm *bprm)
 {
-    struct linux_binprm bprm;
     int retval;
     int i;
 
-    bprm.p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
+    bprm->p = TARGET_PAGE_SIZE*MAX_ARG_PAGES-sizeof(unsigned int);
     for (i=0 ; i<MAX_ARG_PAGES ; i++)       /* clear page-table */
-            bprm.page[i] = 0;
+            bprm->page[i] = 0;
     retval = open(filename, O_RDONLY);
     if (retval < 0)
         return retval;
-    bprm.fd = retval;
-    bprm.filename = (char *)filename;
-    bprm.argc = count(argv);
-    bprm.argv = argv;
-    bprm.envc = count(envp);
-    bprm.envp = envp;
+    bprm->fd = retval;
+    bprm->filename = (char *)filename;
+    bprm->argc = count(argv);
+    bprm->argv = argv;
+    bprm->envc = count(envp);
+    bprm->envp = envp;
 
-    retval = prepare_binprm(&bprm);
+    retval = prepare_binprm(bprm);
 
     infop->host_argv = argv;
 
     if(retval>=0) {
-        if (bprm.buf[0] == 0x7f
-                && bprm.buf[1] == 'E'
-                && bprm.buf[2] == 'L'
-                && bprm.buf[3] == 'F') {
+        if (bprm->buf[0] == 0x7f
+                && bprm->buf[1] == 'E'
+                && bprm->buf[2] == 'L'
+                && bprm->buf[3] == 'F') {
 #ifndef TARGET_HAS_ELFLOAD32
-            retval = load_elf_binary(&bprm,regs,infop);
+            retval = load_elf_binary(bprm,regs,infop);
 #else
-            retval = load_elf_binary_multi(&bprm, regs, infop);
+            retval = load_elf_binary_multi(bprm, regs, infop);
 #endif
 #if defined(TARGET_HAS_BFLT)
-        } else if (bprm.buf[0] == 'b'
-                && bprm.buf[1] == 'F'
-                && bprm.buf[2] == 'L'
-                && bprm.buf[3] == 'T') {
-            retval = load_flt_binary(&bprm,regs,infop);
+        } else if (bprm->buf[0] == 'b'
+                && bprm->buf[1] == 'F'
+                && bprm->buf[2] == 'L'
+                && bprm->buf[3] == 'T') {
+            retval = load_flt_binary(bprm,regs,infop);
 #endif
         } else {
             fprintf(stderr, "Unknown binary format\n");
@@ -209,7 +211,7 @@ int loader_exec(const char * filename, char ** argv, char ** envp,
 
     /* Something went wrong, return the inode and free the argument pages*/
     for (i=0 ; i<MAX_ARG_PAGES ; i++) {
-        free(bprm.page[i]);
+        free(bprm->page[i]);
     }
     return(retval);
 }
diff --git a/linux-user/main.c b/linux-user/main.c
index dc39b05..72734c1 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <sys/mman.h>
+#include <sys/syscall.h>
 
 #include "qemu.h"
 #include "qemu-common.h"
@@ -2254,6 +2255,27 @@ static void usage(void)
 
 THREAD CPUState *thread_env;
 
+void task_settid(TaskState *ts)
+{
+    if (ts->ts_tid == 0) {
+#ifdef USE_NPTL
+        ts->ts_tid = (pid_t)syscall(SYS_gettid);
+#else
+        /* when no threads are used, tid becomes pid */
+        ts->ts_tid = getpid();
+#endif
+    }
+}
+
+void stop_all_tasks(void)
+{
+    /*
+     * We trust that when using NPTL, start_exclusive()
+     * handles thread stopping correctly.
+     */
+    start_exclusive();
+}
+
 /* Assumes contents are already zeroed.  */
 void init_task_state(TaskState *ts)
 {
@@ -2273,6 +2295,7 @@ int main(int argc, char **argv, char **envp)
     const char *cpu_model;
     struct target_pt_regs regs1, *regs = &regs1;
     struct image_info info1, *info = &info1;
+    struct linux_binprm bprm;
     TaskState ts1, *ts = &ts1;
     CPUState *env;
     int optind;
@@ -2402,6 +2425,8 @@ int main(int argc, char **argv, char **envp)
     /* Zero out image_info */
     memset(info, 0, sizeof(struct image_info));
 
+    memset(&bprm, 0, sizeof (bprm));
+
     /* Scan interp_prefix dir for replacement files. */
     init_paths(interp_prefix);
 
@@ -2478,7 +2503,16 @@ int main(int argc, char **argv, char **envp)
     }
     target_argv[target_argc] = NULL;
 
-    if (loader_exec(filename, target_argv, target_environ, regs, info) != 0) {
+    memset(ts, 0, sizeof(TaskState));
+    init_task_state(ts);
+    /* build Task State */
+    ts->info = info;
+    ts->bprm = &bprm;
+    env->opaque = ts;
+    task_settid(ts);
+
+    if (loader_exec(filename, target_argv, target_environ, regs,
+        info, &bprm) != 0) {
         printf("Error loading %s\n", filename);
         _exit(1);
     }
@@ -2514,12 +2548,6 @@ int main(int argc, char **argv, char **envp)
     syscall_init();
     signal_init();
 
-    /* build Task State */
-    memset(ts, 0, sizeof(TaskState));
-    init_task_state(ts);
-    ts->info = info;
-    env->opaque = ts;
-
 #if defined(TARGET_I386)
     cpu_x86_set_cpl(env, 3);
 
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index 15db106..ea4a57d 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -18,6 +18,7 @@
 #include "syscall.h"
 #include "target_signal.h"
 #include "gdbstub.h"
+#include "sys-queue.h"
 
 #if defined(USE_NPTL)
 #define THREAD __thread
@@ -44,6 +45,9 @@ struct image_info {
         abi_ulong       entry;
         abi_ulong       code_offset;
         abi_ulong       data_offset;
+        abi_ulong       saved_auxv;
+        abi_ulong       arg_start;
+        abi_ulong       arg_end;
         char            **host_argv;
 	int		personality;
 };
@@ -87,7 +91,7 @@ struct emulated_sigtable {
 /* NOTE: we force a big alignment so that the stack stored after is
    aligned too */
 typedef struct TaskState {
-    struct TaskState *next;
+    pid_t ts_tid;     /* tid (or pid) of this task */
 #ifdef TARGET_ARM
     /* FPA state */
     FPA11 fpa;
@@ -114,6 +118,7 @@ typedef struct TaskState {
 #endif
     int used; /* non zero if used */
     struct image_info *info;
+    struct linux_binprm *bprm;
 
     struct emulated_sigtable sigtab[TARGET_NSIG];
     struct sigqueue sigqueue_table[MAX_SIGQUEUE_SIZE]; /* siginfo queue */
@@ -125,6 +130,8 @@ typedef struct TaskState {
 
 extern char *exec_path;
 void init_task_state(TaskState *ts);
+void task_settid(TaskState *);
+void stop_all_tasks(void);
 extern const char *qemu_uname_release;
 
 /* ??? See if we can avoid exposing so much of the loader internals.  */
@@ -149,13 +156,15 @@ struct linux_binprm {
         char **argv;
         char **envp;
         char * filename;        /* Name of binary */
+        int (*core_dump)(int, const CPUState *); /* coredump routine */
 };
 
 void do_init_thread(struct target_pt_regs *regs, struct image_info *infop);
 abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
                               abi_ulong stringp, int push_ptr);
 int loader_exec(const char * filename, char ** argv, char ** envp,
-             struct target_pt_regs * regs, struct image_info *infop);
+             struct target_pt_regs * regs, struct image_info *infop,
+             struct linux_binprm *);
 
 int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
                     struct image_info * info);
diff --git a/linux-user/signal.c b/linux-user/signal.c
index 742d52a..433472e 100644
--- a/linux-user/signal.c
+++ b/linux-user/signal.c
@@ -27,6 +27,7 @@
 #include <errno.h>
 #include <assert.h>
 #include <sys/ucontext.h>
+#include <sys/resource.h>
 
 #include "qemu.h"
 #include "qemu-common.h"
@@ -287,6 +288,23 @@ static int fatal_signal (int sig)
     }
 }
 
+/* returns 1 if given signal should dump core if not handled */
+static int core_dump_signal(int sig)
+{
+    switch (sig) {
+    case TARGET_SIGABRT:
+    case TARGET_SIGFPE:
+    case TARGET_SIGILL:
+    case TARGET_SIGQUIT:
+    case TARGET_SIGSEGV:
+    case TARGET_SIGTRAP:
+    case TARGET_SIGBUS:
+        return (1);
+    default:
+        return (0);
+    }
+}
+
 void signal_init(void)
 {
     struct sigaction act;
@@ -352,13 +370,29 @@ static inline void free_sigqueue(CPUState *env, struct sigqueue *q)
 /* abort execution with signal */
 static void QEMU_NORETURN force_sig(int sig)
 {
-    int host_sig;
+    TaskState *ts = (TaskState *)thread_env->opaque;
+    int host_sig, core_dumped = 0;
     struct sigaction act;
     host_sig = target_to_host_signal(sig);
-    fprintf(stderr, "qemu: uncaught target signal %d (%s) - exiting\n",
-            sig, strsignal(host_sig));
     gdb_signalled(thread_env, sig);
 
+    /* dump core if supported by target binary format */
+    if (core_dump_signal(sig) && (ts->bprm->core_dump != NULL)) {
+        stop_all_tasks();
+        core_dumped =
+            ((*ts->bprm->core_dump)(sig, thread_env) == 0);
+    }
+    if (core_dumped) {
+        /* we already dumped the core of target process, we don't want
+         * a coredump of qemu itself */
+        struct rlimit nodump;
+        getrlimit(RLIMIT_CORE, &nodump);
+        nodump.rlim_cur=0;
+        setrlimit(RLIMIT_CORE, &nodump);
+        (void) fprintf(stderr, "qemu: uncaught target signal %d (%s) - %s\n",
+            sig, strsignal(host_sig), "core dumped" );
+    }
+
     /* The proper exit code for dieing from an uncaught signal is
      * -<signal>.  The kernel doesn't allow exit() or _exit() to pass
      * a negative value.  To get the proper exit code we need to
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index ec5beda..f93292d 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -3379,10 +3379,13 @@ static void *clone_func(void *arg)
 {
     new_thread_info *info = arg;
     CPUState *env;
+    TaskState *ts;
 
     env = info->env;
     thread_env = env;
+    ts = (TaskState *)thread_env->opaque;
     info->tid = gettid();
+    task_settid(ts);
     if (info->child_tidptr)
         put_user_u32(info->tid, info->child_tidptr);
     if (info->parent_tidptr)
@@ -3434,6 +3437,7 @@ static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
         flags &= ~(CLONE_VFORK | CLONE_VM);
 
     if (flags & CLONE_VM) {
+        TaskState *parent_ts = (TaskState *)env->opaque;
 #if defined(USE_NPTL)
         new_thread_info info;
         pthread_attr_t attr;
@@ -3446,6 +3450,8 @@ static int do_fork(CPUState *env, unsigned int flags, abi_ulong newsp,
         /* Init regs that differ from the parent.  */
         cpu_clone_regs(new_env, newsp);
         new_env->opaque = ts;
+        ts->bprm = parent_ts->bprm;
+        ts->info = parent_ts->info;
 #if defined(USE_NPTL)
         nptl_flags = flags;
         flags &= ~CLONE_NPTL_FLAGS2;
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 04/10] linux-user: added x86 and x86_64 support for ELF coredump
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 02/10] Implement shm* syscalls and fix 64/32bit errors riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 03/10] linux-user: implemented ELF coredump support for ARM target [v2] riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 05/10] linux-user: strace now handles guest strings correctly riku.voipio
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Mika Westerberg <mika.westerberg@iki.fi>

Depends on:

[PATCH 03/10] linux-user: implemented ELF coredump support for ARM target [v2]

From: Mika Westerberg <mika.westerberg@iki.fi>
Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 linux-user/elfload.c |   83 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 83 insertions(+), 0 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index c210e73..dc797bd 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -134,6 +134,52 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
     regs->rip = infop->entry;
 }
 
+typedef target_ulong    elf_greg_t;
+typedef uint32_t        target_uid_t;
+typedef uint32_t        target_gid_t;
+typedef int32_t         target_pid_t;
+
+#define ELF_NREG    27
+typedef elf_greg_t  elf_gregset_t[ELF_NREG];
+
+/*
+ * Note that ELF_NREG should be 29 as there should be place for
+ * TRAPNO and ERR "registers" as well but linux doesn't dump
+ * those.
+ *
+ * See linux kernel: arch/x86/include/asm/elf.h
+ */ 
+static void elf_core_copy_regs(elf_gregset_t *regs, const CPUState *env)
+{
+    (*regs)[0] = env->regs[15];
+    (*regs)[1] = env->regs[14];
+    (*regs)[2] = env->regs[13];
+    (*regs)[3] = env->regs[12];
+    (*regs)[4] = env->regs[R_EBP];
+    (*regs)[5] = env->regs[R_EBX];
+    (*regs)[6] = env->regs[11];
+    (*regs)[7] = env->regs[10];
+    (*regs)[8] = env->regs[9];
+    (*regs)[9] = env->regs[8];
+    (*regs)[10] = env->regs[R_EAX];
+    (*regs)[11] = env->regs[R_ECX];
+    (*regs)[12] = env->regs[R_EDX];
+    (*regs)[13] = env->regs[R_ESI];
+    (*regs)[14] = env->regs[R_EDI];
+    (*regs)[15] = env->regs[R_EAX]; /* XXX */
+    (*regs)[16] = env->eip;
+    (*regs)[17] = env->segs[R_CS].selector & 0xffff;
+    (*regs)[18] = env->eflags;
+    (*regs)[19] = env->regs[R_ESP];
+    (*regs)[20] = env->segs[R_SS].selector & 0xffff;
+    (*regs)[21] = env->segs[R_FS].selector & 0xffff;
+    (*regs)[22] = env->segs[R_GS].selector & 0xffff;
+    (*regs)[23] = env->segs[R_DS].selector & 0xffff;
+    (*regs)[24] = env->segs[R_ES].selector & 0xffff;
+    (*regs)[25] = env->segs[R_FS].selector & 0xffff;
+    (*regs)[26] = env->segs[R_GS].selector & 0xffff;
+}
+
 #else
 
 #define ELF_START_MMAP 0x80000000
@@ -164,8 +210,45 @@ static inline void init_thread(struct target_pt_regs *regs, struct image_info *i
        A value of 0 tells we have no such handler.  */
     regs->edx = 0;
 }
+
+typedef target_ulong    elf_greg_t;
+typedef uint16_t        target_uid_t;
+typedef uint16_t        target_gid_t;
+typedef int32_t         target_pid_t;
+
+#define ELF_NREG    17
+typedef elf_greg_t  elf_gregset_t[ELF_NREG];
+
+/*
+ * Note that ELF_NREG should be 19 as there should be place for
+ * TRAPNO and ERR "registers" as well but linux doesn't dump
+ * those.
+ *
+ * See linux kernel: arch/x86/include/asm/elf.h
+ */ 
+static void elf_core_copy_regs(elf_gregset_t *regs, const CPUState *env)
+{
+    (*regs)[0] = env->regs[R_EBX];
+    (*regs)[1] = env->regs[R_ECX];
+    (*regs)[2] = env->regs[R_EDX];
+    (*regs)[3] = env->regs[R_ESI];
+    (*regs)[4] = env->regs[R_EDI];
+    (*regs)[5] = env->regs[R_EBP];
+    (*regs)[6] = env->regs[R_EAX];
+    (*regs)[7] = env->segs[R_DS].selector & 0xffff;
+    (*regs)[8] = env->segs[R_ES].selector & 0xffff;
+    (*regs)[9] = env->segs[R_FS].selector & 0xffff;
+    (*regs)[10] = env->segs[R_GS].selector & 0xffff;
+    (*regs)[11] = env->regs[R_EAX]; /* XXX */
+    (*regs)[12] = env->eip;
+    (*regs)[13] = env->segs[R_CS].selector & 0xffff;
+    (*regs)[14] = env->eflags;
+    (*regs)[15] = env->regs[R_ESP];
+    (*regs)[16] = env->segs[R_SS].selector & 0xffff;
+}
 #endif
 
+#define USE_ELF_CORE_DUMP
 #define ELF_EXEC_PAGESIZE	4096
 
 #endif
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 05/10] linux-user: strace now handles guest strings correctly
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
                   ` (2 preceding siblings ...)
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 04/10] linux-user: added x86 and x86_64 support for ELF coredump riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2] riku.voipio
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Mika Westerberg <mika.westerberg@iki.fi>

- to not to break strace with GUEST_BASE is set:
- Strace now can load and print guest strings correctly.
- Added printing support for commonly used flags in some syscalls
  (e.g open, creat, mmap etc.)

From: Mika Westerberg <mika.westerberg@iki.fi>
Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 linux-user/strace.c       | 1013 ++++++++++++++++++++++++++++++++++++++++++++-
 linux-user/strace.list    |  109 +++---
 linux-user/syscall.c      |    6 -
 linux-user/syscall_defs.h |    6 +
 4 files changed, 1058 insertions(+), 76 deletions(-)

diff --git a/linux-user/strace.c b/linux-user/strace.c
index b4caffe..6119586 100644
--- a/linux-user/strace.c
+++ b/linux-user/strace.c
@@ -6,6 +6,8 @@
 #include <sys/shm.h>
 #include <sys/select.h>
 #include <sys/types.h>
+#include <sys/mount.h>
+#include <sys/mman.h>
 #include <unistd.h>
 #include "qemu.h"
 
@@ -21,6 +23,47 @@ struct syscallname {
     void (*result)(const struct syscallname *, abi_long);
 };
 
+#ifdef __GNUC__
+/*
+ * It is possible that target doesn't have syscall that uses
+ * following flags but we don't want the compiler to warn
+ * us about them being unused.  Same applies to utility print
+ * functions.  It is ok to keep them while not used.
+ */
+#define UNUSED __attribute__ ((unused))
+#else
+#define UNUSED
+#endif
+
+/*
+ * Structure used to translate flag values into strings.  This is
+ * similar that is in the actual strace tool.
+ */
+struct flags {
+    abi_long    f_value;  /* flag */
+    const char  *f_string; /* stringified flag */
+};
+
+/* common flags for all architectures */
+#define FLAG_GENERIC(name) { name, #name }
+/* target specific flags (syscall_defs.h has TARGET_<flag>) */
+#define FLAG_TARGET(name)  { TARGET_ ## name, #name }
+/* end of flags array */
+#define FLAG_END           { 0, NULL }
+
+UNUSED static const char *get_comma(int);
+UNUSED static void print_pointer(abi_long, int);
+UNUSED static void print_flags(const struct flags *, abi_long, int);
+UNUSED static void print_at_dirfd(abi_long, int);
+UNUSED static void print_file_mode(abi_long, int);
+UNUSED static void print_open_flags(abi_long, int);
+UNUSED static void print_syscall_prologue(const struct syscallname *);
+UNUSED static void print_syscall_epilogue(const struct syscallname *);
+UNUSED static void print_string(abi_long, int);
+UNUSED static void print_raw_param(const char *, abi_long, int);
+UNUSED static void print_timeval(abi_ulong, int);
+UNUSED static void print_number(abi_long, int);
+
 /*
  * Utility functions
  */
@@ -100,22 +143,6 @@ print_fdset(int n, abi_ulong target_fds_addr)
     }
     gemu_log("]");
 }
-
-static void
-print_timeval(abi_ulong tv_addr)
-{
-    if( tv_addr ) {
-        struct target_timeval *tv;
-
-        tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1);
-        if (!tv)
-            return;
-        gemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}",
-        	 tv->tv_sec, tv->tv_usec);
-        unlock_user(tv, tv_addr, 0);
-    } else
-        gemu_log("NULL");
-}
 #endif
 
 /*
@@ -142,7 +169,7 @@ print_newselect(const struct syscallname *name,
     gemu_log(",");
     print_fdset(arg1, arg4);
     gemu_log(",");
-    print_timeval(arg5);
+    print_timeval(arg5, 1);
     gemu_log(")");
 
     /* save for use in the return output function below */
@@ -250,11 +277,961 @@ print_syscall_ret_newselect(const struct syscallname *name, abi_long ret)
     gemu_log(",");
     print_fdset(newselect_arg1,newselect_arg4);
     gemu_log(",");
-    print_timeval(newselect_arg5);
+    print_timeval(newselect_arg5, 1);
     gemu_log(")\n");
 }
 #endif
 
+UNUSED static struct flags access_flags[] = {
+    FLAG_GENERIC(F_OK),
+    FLAG_GENERIC(R_OK),
+    FLAG_GENERIC(W_OK),
+    FLAG_GENERIC(X_OK),
+    FLAG_END,
+};
+
+UNUSED static struct flags at_file_flags[] = {
+    FLAG_GENERIC(AT_EACCESS),
+    FLAG_GENERIC(AT_SYMLINK_NOFOLLOW),
+    FLAG_END,
+};
+
+UNUSED static struct flags unlinkat_flags[] = {
+    FLAG_GENERIC(AT_REMOVEDIR),
+    FLAG_END,
+};
+
+UNUSED static struct flags mode_flags[] = {
+    FLAG_GENERIC(S_IFSOCK),
+    FLAG_GENERIC(S_IFLNK),
+    FLAG_GENERIC(S_IFREG),
+    FLAG_GENERIC(S_IFBLK),
+    FLAG_GENERIC(S_IFDIR),
+    FLAG_GENERIC(S_IFCHR),
+    FLAG_GENERIC(S_IFIFO),
+    FLAG_END,
+};
+
+UNUSED static struct flags open_access_flags[] = {
+    FLAG_TARGET(O_RDONLY),
+    FLAG_TARGET(O_WRONLY),
+    FLAG_TARGET(O_RDWR),
+    FLAG_END,
+};
+
+UNUSED static struct flags open_flags[] = {
+    FLAG_TARGET(O_APPEND),
+    FLAG_TARGET(O_CREAT),
+    FLAG_TARGET(O_DIRECTORY),
+    FLAG_TARGET(O_EXCL),
+    FLAG_TARGET(O_LARGEFILE),
+    FLAG_TARGET(O_NOCTTY),
+    FLAG_TARGET(O_NOFOLLOW),
+    FLAG_TARGET(O_NONBLOCK),      /* also O_NDELAY */
+    FLAG_TARGET(O_SYNC),
+    FLAG_TARGET(O_TRUNC),
+#ifdef O_DIRECT
+    FLAG_TARGET(O_DIRECT),
+#endif
+    FLAG_END,
+};
+
+UNUSED static struct flags mount_flags[] = {
+#ifdef MS_BIND
+    FLAG_GENERIC(MS_BIND),
+#endif
+#ifdef MS_DIRSYNC
+    FLAG_GENERIC(MS_DIRSYNC),
+#endif
+    FLAG_GENERIC(MS_MANDLOCK),
+#ifdef MS_MOVE
+    FLAG_GENERIC(MS_MOVE),
+#endif
+    FLAG_GENERIC(MS_NOATIME),
+    FLAG_GENERIC(MS_NODEV),
+    FLAG_GENERIC(MS_NODIRATIME),
+    FLAG_GENERIC(MS_NOEXEC),
+    FLAG_GENERIC(MS_NOSUID),
+    FLAG_GENERIC(MS_RDONLY),
+#ifdef MS_RELATIME
+    FLAG_GENERIC(MS_RELATIME),
+#endif
+    FLAG_GENERIC(MS_REMOUNT),
+    FLAG_GENERIC(MS_SYNCHRONOUS),
+    FLAG_END,
+};
+
+UNUSED static struct flags umount2_flags[] = {
+#ifdef MNT_FORCE
+    FLAG_GENERIC(MNT_FORCE),
+#endif
+#ifdef MNT_DETACH
+    FLAG_GENERIC(MNT_DETACH),
+#endif
+#ifdef MNT_EXPIRE
+    FLAG_GENERIC(MNT_EXPIRE),
+#endif
+    FLAG_END,
+};
+
+UNUSED static struct flags mmap_prot_flags[] = {
+    FLAG_GENERIC(PROT_NONE),
+    FLAG_GENERIC(PROT_EXEC),
+    FLAG_GENERIC(PROT_READ),
+    FLAG_GENERIC(PROT_WRITE),
+    FLAG_END,
+};
+
+UNUSED static struct flags mmap_flags[] = {
+    FLAG_TARGET(MAP_SHARED),
+    FLAG_TARGET(MAP_PRIVATE),
+    FLAG_TARGET(MAP_ANONYMOUS),
+    FLAG_TARGET(MAP_DENYWRITE),
+    FLAG_TARGET(MAP_FIXED),
+    FLAG_TARGET(MAP_GROWSDOWN),
+#ifdef MAP_LOCKED
+    FLAG_TARGET(MAP_LOCKED),
+#endif
+#ifdef MAP_NONBLOCK
+    FLAG_TARGET(MAP_NONBLOCK),
+#endif
+    FLAG_TARGET(MAP_NORESERVE),
+#ifdef MAP_POPULATE
+    FLAG_TARGET(MAP_POPULATE),
+#endif
+    FLAG_END,
+};
+
+UNUSED static struct flags fcntl_flags[] = {
+    FLAG_TARGET(F_DUPFD),
+    FLAG_TARGET(F_GETFD),
+    FLAG_TARGET(F_SETFD),
+    FLAG_TARGET(F_GETFL),
+    FLAG_TARGET(F_SETFL),
+    FLAG_TARGET(F_GETLK),
+    FLAG_TARGET(F_SETLK),
+    FLAG_TARGET(F_SETLKW),
+    FLAG_END,
+};
+
+/*
+ * print_xxx utility functions.  These are used to print syscall
+ * parameters in certain format.  All of these have parameter
+ * named 'last'.  This parameter is used to add comma to output
+ * when last == 0.
+ */
+
+static const char *
+get_comma(int last)
+{
+    return ((last) ? "" : ",");
+}
+
+static void
+print_flags(const struct flags *f, abi_long tflags, int last)
+{
+    const char *sep = "";
+    int flags;
+    int n;
+
+    flags = (int)tswap32(tflags);
+
+    if ((flags == 0) && (f->f_value == 0)) {
+        gemu_log("%s%s", f->f_string, get_comma(last));
+        return;
+    }
+    for (n = 0; f->f_string != NULL; f++) {
+        if ((f->f_value != 0) && ((flags & f->f_value) == f->f_value)) {
+            gemu_log("%s%s", sep, f->f_string);
+            flags &= ~f->f_value;
+            sep = "|";
+            n++;
+        }
+    }
+
+    if (n > 0) {
+        /* print rest of the flags as numeric */
+        if (flags != 0) {
+            gemu_log("%s%#x%s", sep, flags, get_comma(last));
+        } else {
+            gemu_log("%s", get_comma(last));
+        }
+    } else {
+        /* no string version of flags found, print them in hex then */
+        gemu_log("%#x%s", flags, get_comma(last));
+    }
+}
+
+static void
+print_at_dirfd(abi_long tdirfd, int last)
+{
+    int dirfd = tswap32(tdirfd);
+
+    if (dirfd == AT_FDCWD)
+        gemu_log("AT_FDCWD%s", get_comma(last));
+    else
+        gemu_log("%d%s", dirfd, get_comma(last));
+}
+
+static void
+print_file_mode(abi_long tmode, int last)
+{
+    const char *sep = "";
+    const struct flags *m;
+    mode_t mode = (mode_t)tswap32(tmode);
+
+    for (m = &mode_flags[0]; m->f_string != NULL; m++) {
+        if ((m->f_value & mode) == m->f_value) {
+            gemu_log("%s%s", m->f_string, sep);
+            sep = "|";
+            mode &= ~m->f_value;
+            break;
+        }
+    }
+
+    mode &= ~S_IFMT;
+    /* print rest of the mode as octal */
+    if (mode != 0)
+        gemu_log("%s%#o", sep, mode);
+
+    gemu_log("%s", get_comma(last));
+}
+
+static void
+print_open_flags(abi_long tflags, int last)
+{
+    int flags = tswap32(tflags);
+
+    print_flags(open_access_flags, flags & TARGET_O_ACCMODE, 1);
+    flags &= ~TARGET_O_ACCMODE;
+    if (flags == 0) {
+        gemu_log("%s", get_comma(last));
+        return;
+    }
+    gemu_log("|");
+    print_flags(open_flags, flags, last);
+}
+
+static void
+print_syscall_prologue(const struct syscallname *sc)
+{
+    gemu_log("%s(", sc->name);
+}
+
+/*ARGSUSED*/
+static void
+print_syscall_epilogue(const struct syscallname *sc)
+{
+    (void)sc;
+    gemu_log(")");
+}
+
+static void
+print_string(abi_long addr, int last)
+{
+    char *s;
+
+    if ((s = lock_user_string(addr)) != NULL) {
+        gemu_log("\"%s\"%s", s, get_comma(last));
+        unlock_user(s, addr, 0);
+    } else {
+        /* can't get string out of it, so print it as pointer */
+        print_pointer(addr, last);
+    }
+}
+
+/*
+ * Prints out raw parameter using given format.  Caller needs
+ * to do byte swapping if needed.
+ */
+static void
+print_raw_param(const char *fmt, abi_long param, int last)
+{
+    char format[64];
+
+    (void) snprintf(format, sizeof (format), "%s%s", fmt, get_comma(last));
+    gemu_log(format, param);
+}
+
+static void
+print_pointer(abi_long p, int last)
+{
+    if (p == 0)
+        gemu_log("NULL%s", get_comma(last));
+    else
+        gemu_log("0x" TARGET_ABI_FMT_lx "%s", p, get_comma(last));
+}
+
+/*
+ * Reads 32-bit (int) number from guest address space from
+ * address 'addr' and prints it.
+ */
+static void
+print_number(abi_long addr, int last)
+{
+    if (addr == 0) {
+        gemu_log("NULL%s", get_comma(last));
+    } else {
+        int num;
+
+        get_user_s32(num, addr);
+        gemu_log("[%d]%s", num, get_comma(last));
+    }
+}
+
+static void
+print_timeval(abi_ulong tv_addr, int last)
+{
+    if( tv_addr ) {
+        struct target_timeval *tv;
+
+        tv = lock_user(VERIFY_READ, tv_addr, sizeof(*tv), 1);
+        if (!tv)
+            return;
+        gemu_log("{" TARGET_ABI_FMT_ld "," TARGET_ABI_FMT_ld "}%s",
+            tv->tv_sec, tv->tv_usec, get_comma(last));
+        unlock_user(tv, tv_addr, 0);
+    } else
+        gemu_log("NULL%s", get_comma(last));
+}
+
+#undef UNUSED
+
+#ifdef TARGET_NR_accept
+static void
+print_accept(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_raw_param("%d", tswap32(arg0), 0);
+    print_pointer(arg1, 0);
+    print_number(arg2, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_access
+static void
+print_access(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_flags(access_flags, arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_brk
+static void
+print_brk(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_pointer(arg0, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_chdir
+static void
+print_chdir(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_chmod
+static void
+print_chmod(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_file_mode(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_creat
+static void
+print_creat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_file_mode(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_execv
+static void
+print_execv(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_raw_param("0x" TARGET_ABI_FMT_lx, tswapl(arg1), 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_faccessat
+static void
+print_faccessat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_flags(access_flags, arg2, 0);
+    print_flags(at_file_flags, arg3, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_fchmodat
+static void
+print_fchmodat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_file_mode(arg2, 0);
+    print_flags(at_file_flags, arg3, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_fchownat
+static void
+print_fchownat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+#ifdef USE_UID16
+    print_raw_param("%d", tswap16(arg2), 0);
+    print_raw_param("%d", tswap16(arg3), 0);
+#else
+    print_raw_param("%d", tswap32(arg2), 0);
+    print_raw_param("%d", tswap32(arg3), 0);
+#endif
+    print_flags(at_file_flags, arg4, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#if defined(TARGET_NR_fcntl) || defined(TARGET_NR_fcntl64)
+static void
+print_fcntl(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_raw_param("%d", tswap32(arg0), 0);
+    print_flags(fcntl_flags, arg1, 0);
+    /*
+     * TODO: check flags and print following argument only
+     *       when needed.
+     */
+    print_pointer(arg2, 1);
+    print_syscall_epilogue(name);
+}
+#define print_fcntl64   print_fcntl
+#endif
+
+
+#ifdef TARGET_NR_futimesat
+static void
+print_futimesat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_timeval(arg2, 0);
+    print_timeval(arg2 + sizeof (struct target_timeval), 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_link
+static void
+print_link(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_string(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_linkat
+static void
+print_linkat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_at_dirfd(arg2, 0);
+    print_string(arg3, 0);
+    print_flags(at_file_flags, arg4, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#if defined(TARGET_NR_stat) || defined(TARGET_NR_stat64) || \
+    defined(TARGET_NR_lstat) || defined(TARGET_NR_lstat64)
+static void
+print_stat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#define print_lstat     print_stat
+#define print_stat64	print_stat
+#define print_lstat64   print_stat
+#endif
+
+#if defined(TARGET_NR_fstat) || defined(TARGET_NR_fstat64)
+static void
+print_fstat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_raw_param("%d", tswap32(arg0), 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#define print_fstat64     print_fstat
+#endif
+
+#ifdef TARGET_NR_mkdir
+static void
+print_mkdir(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_file_mode(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_mkdirat
+static void
+print_mkdirat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_file_mode(arg2, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_mknod
+static void
+print_mknod(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    int hasdev = (tswapl(arg1) & (S_IFCHR|S_IFBLK));
+
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_file_mode(arg1, (hasdev == 0));
+    if (hasdev) {
+        print_raw_param("makedev(%d", major(tswapl(arg2)), 0);
+        print_raw_param("%d)", minor(tswapl(arg2)), 1);
+    }
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_mknodat
+static void
+print_mknodat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    int hasdev = (tswapl(arg2) & (S_IFCHR|S_IFBLK));
+
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_file_mode(arg2, (hasdev == 0));
+    if (hasdev) {
+        print_raw_param("makedev(%d", major(tswapl(arg3)), 0);
+        print_raw_param("%d)", minor(tswapl(arg3)), 1);
+    }
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_mq_open
+static void
+print_mq_open(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    int is_creat = (tswapl(arg1) & TARGET_O_CREAT);
+
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_open_flags(arg1, (is_creat == 0));
+    if (is_creat) {
+        print_file_mode(arg2, 0);
+        print_pointer(arg3, 1);
+    }
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_open
+static void
+print_open(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    int is_creat = (tswap32(arg1) & TARGET_O_CREAT);
+
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_open_flags(arg1, (is_creat == 0));
+    if (is_creat)
+        print_file_mode(arg2, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_openat
+static void
+print_openat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    int is_creat = (tswap32(arg2) & TARGET_O_CREAT);
+
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_open_flags(arg2, (is_creat == 0));
+    if (is_creat)
+        print_file_mode(arg3, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_mq_unlink
+static void
+print_mq_unlink(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#if defined(TARGET_NR_fstatat64) || defined(TARGET_NR_newfstatat)
+static void
+print_fstatat64(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_pointer(arg2, 0);
+    print_flags(at_file_flags, arg3, 1);
+    print_syscall_epilogue(name);
+}
+#define print_newfstatat    print_fstatat64
+#endif
+
+#ifdef TARGET_NR_readlink
+static void
+print_readlink(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_pointer(arg1, 0);
+    print_raw_param("%u", tswapl(arg2), 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_readlinkat
+static void
+print_readlinkat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_pointer(arg2, 0);
+    print_raw_param("%u", tswapl(arg3), 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_rename
+static void
+print_rename(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_string(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_renameat
+static void
+print_renameat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_at_dirfd(arg2, 0);
+    print_string(arg3, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_statfs
+static void
+print_statfs(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#define print_statfs64  print_statfs
+#endif
+
+#ifdef TARGET_NR_symlink
+static void
+print_symlink(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_string(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_symlinkat
+static void
+print_symlinkat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_at_dirfd(arg1, 0);
+    print_string(arg2, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_mount
+static void
+print_mount(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_string(arg1, 0);
+    print_string(arg2, 0);
+    print_flags(mount_flags, arg3, 0);
+    print_pointer(arg4, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_umount
+static void
+print_umount(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_umount2
+static void
+print_umount2(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_flags(umount2_flags, arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_unlink
+static void
+print_unlink(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_unlinkat
+static void
+print_unlinkat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_flags(unlinkat_flags, arg2, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_utime
+static void
+print_utime(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_utimes
+static void
+print_utimes(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_string(arg0, 0);
+    print_pointer(arg1, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_utimensat
+static void
+print_utimensat(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_at_dirfd(arg0, 0);
+    print_string(arg1, 0);
+    print_pointer(arg2, 0);
+    print_flags(at_file_flags, arg3, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_mmap
+static void
+print_mmap(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_pointer(arg0, 0);
+    print_raw_param("%d", tswapl(arg1), 0);
+    print_flags(mmap_prot_flags, arg2, 0);
+    print_flags(mmap_flags, arg3, 0);
+    print_raw_param("%d", tswapl(arg4), 0);
+    print_raw_param("%#x", tswapl(arg5), 1);
+    print_syscall_epilogue(name);
+}
+#define print_mmap2     print_mmap
+#endif
+
+#ifdef TARGET_NR_mprotect
+static void
+print_mprotect(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_pointer(arg0, 0);
+    print_raw_param("%d", tswapl(arg1), 0);
+    print_flags(mmap_prot_flags, arg2, 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
+#ifdef TARGET_NR_munmap
+static void
+print_munmap(const struct syscallname *name,
+    abi_long arg0, abi_long arg1, abi_long arg2,
+    abi_long arg3, abi_long arg4, abi_long arg5)
+{
+    print_syscall_prologue(name);
+    print_pointer(arg0, 0);
+    print_raw_param("%d", tswapl(arg1), 1);
+    print_syscall_epilogue(name);
+}
+#endif
+
 /*
  * An array of all of the syscalls we know about
  */
diff --git a/linux-user/strace.list b/linux-user/strace.list
index 3f688db..a8432af 100644
--- a/linux-user/strace.list
+++ b/linux-user/strace.list
@@ -1,8 +1,13 @@
+/*
+ * Note that if you change format strings in these, check also
+ * that corresponding print functions are able to handle string
+ * locking correctly (see strace.c).
+ */
 #ifdef TARGET_NR_accept
-{ TARGET_NR_accept, "accept" , "%s(%d,%#x,%#x)", NULL, NULL },
+{ TARGET_NR_accept, "accept" , NULL, print_accept, NULL },
 #endif
 #ifdef TARGET_NR_access
-{ TARGET_NR_access, "access" , "%s(\"%s\",%#o)", NULL, NULL },
+{ TARGET_NR_access, "access" , NULL, print_access, NULL },
 #endif
 #ifdef TARGET_NR_acct
 { TARGET_NR_acct, "acct" , NULL, NULL, NULL },
@@ -38,7 +43,7 @@
 { TARGET_NR_break, "break" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_brk
-{ TARGET_NR_brk, "brk" , NULL, NULL, print_syscall_ret_addr },
+{ TARGET_NR_brk, "brk" , NULL, print_brk, print_syscall_ret_addr },
 #endif
 #ifdef TARGET_NR_cachectl
 { TARGET_NR_cachectl, "cachectl" , NULL, NULL, NULL },
@@ -53,10 +58,10 @@
 { TARGET_NR_capset, "capset" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_chdir
-{ TARGET_NR_chdir, "chdir" , "%s(\"%s\")", NULL, NULL },
+{ TARGET_NR_chdir, "chdir" , NULL, print_chdir, NULL },
 #endif
 #ifdef TARGET_NR_chmod
-{ TARGET_NR_chmod, "chmod" , "%s(\"%s\",%#o)", NULL, NULL },
+{ TARGET_NR_chmod, "chmod" , NULL, print_chmod, NULL },
 #endif
 #ifdef TARGET_NR_chown
 { TARGET_NR_chown, "chown" , NULL, NULL, NULL },
@@ -89,7 +94,7 @@
 { TARGET_NR_connect, "connect" , "%s(%d,%#x,%d)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_creat
-{ TARGET_NR_creat, "creat" , "%s(\"%s\",%#o)", NULL, NULL },
+{ TARGET_NR_creat, "creat" , NULL, print_creat, NULL },
 #endif
 #ifdef TARGET_NR_create_module
 { TARGET_NR_create_module, "create_module" , NULL, NULL, NULL },
@@ -122,7 +127,7 @@
 { TARGET_NR_epoll_wait_old, "epoll_wait_old" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_execv
-{ TARGET_NR_execv, "execv" , "%s(\"%s\",%ld,%ld,%ld,%ld,%ld)\n", NULL, NULL },
+{ TARGET_NR_execv, "execv" , NULL, print_execv, NULL },
 #endif
 #ifdef TARGET_NR_execve
 { TARGET_NR_execve, "execve" , NULL, print_execve, NULL },
@@ -140,7 +145,7 @@
 { TARGET_NR_exit_group, "exit_group" , "%s(%d)\n", NULL, NULL },
 #endif
 #ifdef TARGET_NR_faccessat
-{ TARGET_NR_faccessat, "faccessat" , "%s(%d,\"%s\",%#o,%#x)", NULL, NULL },
+{ TARGET_NR_faccessat, "faccessat" , NULL, print_faccessat, NULL },
 #endif
 #ifdef TARGET_NR_fadvise64
 { TARGET_NR_fadvise64, "fadvise64" , NULL, NULL, NULL },
@@ -155,22 +160,22 @@
 { TARGET_NR_fchmod, "fchmod" , "%s(%d,%#o)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_fchmodat
-{ TARGET_NR_fchmodat, "fchmodat" , "%s(%d,\"%s\",%#o,%#x)", NULL, NULL },
+{ TARGET_NR_fchmodat, "fchmodat" , NULL, print_fchmodat, NULL },
 #endif
 #ifdef TARGET_NR_fchown
-{ TARGET_NR_fchown, "fchown" , "%s(\"%s\",%d,%d)", NULL, NULL },
+{ TARGET_NR_fchown, "fchown" , "%s(%d,%d,%d)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_fchown32
 { TARGET_NR_fchown32, "fchown32" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_fchownat
-{ TARGET_NR_fchownat, "fchownat" , "%s(%d,\"%s\",%d,%d,%#x)", NULL, NULL },
+{ TARGET_NR_fchownat, "fchownat" , NULL, print_fchownat, NULL },
 #endif
 #ifdef TARGET_NR_fcntl
-{ TARGET_NR_fcntl, "fcntl" , NULL, NULL, NULL },
+{ TARGET_NR_fcntl, "fcntl" , NULL, print_fcntl, NULL },
 #endif
 #ifdef TARGET_NR_fcntl64
-{ TARGET_NR_fcntl64, "fcntl64" , NULL, NULL, NULL },
+{ TARGET_NR_fcntl64, "fcntl64" , NULL, print_fcntl64, NULL },
 #endif
 #ifdef TARGET_NR_fdatasync
 { TARGET_NR_fdatasync, "fdatasync" , NULL, NULL, NULL },
@@ -194,10 +199,10 @@
 { TARGET_NR_fsetxattr, "fsetxattr" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_fstat
-{ TARGET_NR_fstat, "fstat" , "%s(%d,%p)", NULL, NULL },
+{ TARGET_NR_fstat, "fstat" , NULL, print_fstat, NULL },
 #endif
 #ifdef TARGET_NR_fstat64
-{ TARGET_NR_fstat64, "fstat64" , "%s(%d,%p)", NULL, NULL },
+{ TARGET_NR_fstat64, "fstat64" , NULL, print_fstat64, NULL },
 #endif
 #ifdef TARGET_NR_fstatfs
 { TARGET_NR_fstatfs, "fstatfs" , "%s(%d,%p)", NULL, NULL },
@@ -221,7 +226,7 @@
 { TARGET_NR_futex, "futex" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_futimesat
-{ TARGET_NR_futimesat, "futimesat" , "%s(%d,\"%s\",%p)", NULL, NULL },
+{ TARGET_NR_futimesat, "futimesat" , NULL, print_futimesat, NULL },
 #endif
 #ifdef TARGET_NR_getcwd
 { TARGET_NR_getcwd, "getcwd" , "%s(%p,%d)", NULL, NULL },
@@ -425,10 +430,10 @@
 { TARGET_NR_lgetxattr, "lgetxattr" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_link
-{ TARGET_NR_link, "link" , "%s(\"%s\",\"%s\")", NULL, NULL },
+{ TARGET_NR_link, "link" , NULL, print_link, NULL },
 #endif
 #ifdef TARGET_NR_linkat
-{ TARGET_NR_linkat, "linkat" , "%s(%d,\"%s\",%d,\"%s\",%#x)", NULL, NULL },
+{ TARGET_NR_linkat, "linkat" , NULL, print_linkat, NULL },
 #endif
 #ifdef TARGET_NR_Linux
 { TARGET_NR_Linux, "Linux" , NULL, NULL, NULL },
@@ -461,10 +466,10 @@
 { TARGET_NR_lsetxattr, "lsetxattr" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_lstat
-{ TARGET_NR_lstat, "lstat" , "%s(\"%s\",%p)", NULL, NULL },
+{ TARGET_NR_lstat, "lstat" , NULL, print_lstat, NULL },
 #endif
 #ifdef TARGET_NR_lstat64
-{ TARGET_NR_lstat64, "lstat64" , "%s(\"%s\",%p)", NULL, NULL },
+{ TARGET_NR_lstat64, "lstat64" , NULL, print_lstat64, NULL },
 #endif
 #ifdef TARGET_NR_madvise
 { TARGET_NR_madvise, "madvise" , NULL, NULL, NULL },
@@ -485,16 +490,16 @@
 { TARGET_NR_mincore, "mincore" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_mkdir
-{ TARGET_NR_mkdir, "mkdir" , "%s(\"%s\",%#o)", NULL, NULL },
+{ TARGET_NR_mkdir, "mkdir" , NULL, print_mkdir, NULL },
 #endif
 #ifdef TARGET_NR_mkdirat
-{ TARGET_NR_mkdirat, "mkdirat" , "%s(%d,\"%s\",%#o)", NULL, NULL },
+{ TARGET_NR_mkdirat, "mkdirat" , NULL, print_mkdirat, NULL },
 #endif
 #ifdef TARGET_NR_mknod
-{ TARGET_NR_mknod, "mknod" , "%s(\"%s\",%#o,%#x)", NULL, NULL },
+{ TARGET_NR_mknod, "mknod" , NULL, print_mknod, NULL },
 #endif
 #ifdef TARGET_NR_mknodat
-{ TARGET_NR_mknodat, "mknodat" , "%s(%d,\"%s\",%#o,%#x)", NULL, NULL },
+{ TARGET_NR_mknodat, "mknodat" , NULL, print_mknodat, NULL },
 #endif
 #ifdef TARGET_NR_mlock
 { TARGET_NR_mlock, "mlock" , NULL, NULL, NULL },
@@ -503,22 +508,22 @@
 { TARGET_NR_mlockall, "mlockall" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_mmap
-{ TARGET_NR_mmap, "mmap" , NULL, NULL, print_syscall_ret_addr },
+{ TARGET_NR_mmap, "mmap" , NULL, print_mmap, print_syscall_ret_addr },
 #endif
 #ifdef TARGET_NR_mmap2
-{ TARGET_NR_mmap2, "mmap2" , NULL, NULL, print_syscall_ret_addr },
+{ TARGET_NR_mmap2, "mmap2" , NULL, print_mmap2, print_syscall_ret_addr },
 #endif
 #ifdef TARGET_NR_modify_ldt
 { TARGET_NR_modify_ldt, "modify_ldt" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_mount
-{ TARGET_NR_mount, "mount" , NULL, NULL, NULL },
+{ TARGET_NR_mount, "mount" , NULL, print_mount, NULL },
 #endif
 #ifdef TARGET_NR_move_pages
 { TARGET_NR_move_pages, "move_pages" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_mprotect
-{ TARGET_NR_mprotect, "mprotect" , NULL, NULL, NULL },
+{ TARGET_NR_mprotect, "mprotect" , NULL, print_mprotect, NULL },
 #endif
 #ifdef TARGET_NR_mpx
 { TARGET_NR_mpx, "mpx" , NULL, NULL, NULL },
@@ -530,7 +535,7 @@
 { TARGET_NR_mq_notify, "mq_notify" , "%s(%d,%p)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_mq_open
-{ TARGET_NR_mq_open, "mq_open" , "%s(\"/%s\",%#x,%#o,%p)", NULL, NULL },
+{ TARGET_NR_mq_open, "mq_open" , NULL, print_mq_open, NULL },
 #endif
 #ifdef TARGET_NR_mq_timedreceive
 { TARGET_NR_mq_timedreceive, "mq_timedreceive" , "%s(%d,%p,%d,%u,%p)", NULL, NULL },
@@ -539,7 +544,7 @@
 { TARGET_NR_mq_timedsend, "mq_timedsend" , "%s(%d,%p,%d,%u,%p)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_mq_unlink
-{ TARGET_NR_mq_unlink, "mq_unlink" , "%s(%s)", NULL, NULL },
+{ TARGET_NR_mq_unlink, "mq_unlink" , NULL, print_mq_unlink, NULL },
 #endif
 #ifdef TARGET_NR_mremap
 { TARGET_NR_mremap, "mremap" , NULL, NULL, NULL },
@@ -569,16 +574,16 @@
 { TARGET_NR_munlockall, "munlockall" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_munmap
-{ TARGET_NR_munmap, "munmap" , "%s(%p,%d)", NULL, NULL },
+{ TARGET_NR_munmap, "munmap" , NULL, print_munmap, NULL },
 #endif
 #ifdef TARGET_NR_nanosleep
 { TARGET_NR_nanosleep, "nanosleep" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_fstatat64
-{ TARGET_NR_fstatat64, "fstatat64" , "%s(%d,\"%s\",%p,%#x)", NULL, NULL },
+{ TARGET_NR_fstatat64, "fstatat64" , NULL, print_fstatat64, NULL },
 #endif
 #ifdef TARGET_NR_newfstatat
-{ TARGET_NR_newfstatat, "newfstatat" , "%s(%d,\"%s\",%p,%#x)", NULL, NULL },
+{ TARGET_NR_newfstatat, "newfstatat" , NULL, print_newfstatat, NULL },
 #endif
 #ifdef TARGET_NR__newselect
 { TARGET_NR__newselect, "_newselect" , NULL, print_newselect, print_syscall_ret_newselect },
@@ -611,10 +616,10 @@
 { TARGET_NR_olduname, "olduname" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_open
-{ TARGET_NR_open, "open" , "%s(\"%s\",%#x,%#o)", NULL, NULL },
+{ TARGET_NR_open, "open" , NULL, print_open, NULL },
 #endif
 #ifdef TARGET_NR_openat
-{ TARGET_NR_openat, "openat" , "%s(%d,\"%s\",%#x,%#o)", NULL, NULL },
+{ TARGET_NR_openat, "openat" , NULL, print_openat, NULL },
 #endif
 #ifdef TARGET_NR_osf_adjtime
 { TARGET_NR_osf_adjtime, "osf_adjtime" , NULL, NULL, NULL },
@@ -1007,10 +1012,10 @@
 { TARGET_NR_readdir, "readdir" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_readlink
-{ TARGET_NR_readlink, "readlink" , "%s(\"%s\",%p,%d)", NULL, NULL },
+{ TARGET_NR_readlink, "readlink" , NULL, print_readlink, NULL },
 #endif
 #ifdef TARGET_NR_readlinkat
-{ TARGET_NR_readlinkat, "readlinkat" , "%s(%d,\"%s\",%p,%d)", NULL, NULL },
+{ TARGET_NR_readlinkat, "readlinkat" , NULL, print_readlinkat, NULL },
 #endif
 #ifdef TARGET_NR_readv
 { TARGET_NR_readv, "readv" , NULL, NULL, NULL },
@@ -1034,10 +1039,10 @@
 { TARGET_NR_removexattr, "removexattr" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_rename
-{ TARGET_NR_rename, "rename" , "%s(\"%s\",\"%s\")", NULL, NULL },
+{ TARGET_NR_rename, "rename" , NULL, print_rename, NULL },
 #endif
 #ifdef TARGET_NR_renameat
-{ TARGET_NR_renameat, "renameat" , "%s(%d,\"%s\",%d,\"%s\")", NULL, NULL },
+{ TARGET_NR_renameat, "renameat" , NULL, print_renameat, NULL },
 #endif
 #ifdef TARGET_NR_request_key
 { TARGET_NR_request_key, "request_key" , NULL, NULL, NULL },
@@ -1301,16 +1306,16 @@
 { TARGET_NR_ssetmask, "ssetmask" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_stat
-{ TARGET_NR_stat, "stat" , "%s(\"%s\",%p)", NULL, NULL },
+{ TARGET_NR_stat, "stat" , NULL, print_stat, NULL },
 #endif
 #ifdef TARGET_NR_stat64
-{ TARGET_NR_stat64, "stat64" , "%s(\"%s\",%p)", NULL, NULL },
+{ TARGET_NR_stat64, "stat64" , NULL, print_stat64, NULL },
 #endif
 #ifdef TARGET_NR_statfs
-{ TARGET_NR_statfs, "statfs" , "%s(\"%s\",%p)", NULL, NULL },
+{ TARGET_NR_statfs, "statfs" , NULL, print_statfs, NULL },
 #endif
 #ifdef TARGET_NR_statfs64
-{ TARGET_NR_statfs64, "statfs64" , "%s(\"%s\",%p)", NULL, NULL },
+{ TARGET_NR_statfs64, "statfs64" , NULL, print_statfs64, NULL },
 #endif
 #ifdef TARGET_NR_stime
 { TARGET_NR_stime, "stime" , NULL, NULL, NULL },
@@ -1334,10 +1339,10 @@
 { TARGET_NR_swapon, "swapon" , NULL, NULL, NULL },
 #endif
 #ifdef TARGET_NR_symlink
-{ TARGET_NR_symlink, "symlink" , "%s(\"%s\",\"%s\")", NULL, NULL },
+{ TARGET_NR_symlink, "symlink" , NULL, print_symlink, NULL },
 #endif
 #ifdef TARGET_NR_symlinkat
-{ TARGET_NR_symlinkat, "symlinkat" , "%s(\"%s\",%d,\"%s\")", NULL, NULL },
+{ TARGET_NR_symlinkat, "symlinkat", NULL, print_symlinkat, NULL },
 #endif
 #ifdef TARGET_NR_sync
 { TARGET_NR_sync, "sync" , NULL, NULL, NULL },
@@ -1427,19 +1432,19 @@
 { TARGET_NR_umask, "umask" , "%s(%#o)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_umount
-{ TARGET_NR_umount, "umount" , "%s(\"%s\",\"%s\",\"%s\",%#x,%p)", NULL, NULL },
+{ TARGET_NR_umount, "umount" , NULL, print_umount, NULL },
 #endif
 #ifdef TARGET_NR_umount2
-{ TARGET_NR_umount2, "umount2" , NULL, NULL, NULL },
+{ TARGET_NR_umount2, "umount2" , NULL, print_umount2, NULL },
 #endif
 #ifdef TARGET_NR_uname
 { TARGET_NR_uname, "uname" , "%s(%p)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_unlink
-{ TARGET_NR_unlink, "unlink" , "%s(\"%s\")", NULL, NULL },
+{ TARGET_NR_unlink, "unlink" , NULL, print_unlink, NULL },
 #endif
 #ifdef TARGET_NR_unlinkat
-{ TARGET_NR_unlinkat, "unlinkat" , "%s(%d,\"%s\",%#x)", NULL, NULL },
+{ TARGET_NR_unlinkat, "unlinkat" , NULL, print_unlinkat, NULL },
 #endif
 #ifdef TARGET_NR_unshare
 { TARGET_NR_unshare, "unshare" , NULL, NULL, NULL },
@@ -1469,10 +1474,10 @@
 { TARGET_NR_ustat, "ustat" , "%s(%#x,%p)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_utime
-{ TARGET_NR_utime, "utime" , "%s(\"%s\",%p)", NULL, NULL },
+{ TARGET_NR_utime, "utime" , NULL, print_utime, NULL },
 #endif
 #ifdef TARGET_NR_utimes
-{ TARGET_NR_utimes, "utimes" , NULL, NULL, NULL },
+{ TARGET_NR_utimes, "utimes" , NULL, print_utimes, NULL },
 #endif
 #ifdef TARGET_NR_utrap_install
 { TARGET_NR_utrap_install, "utrap_install" , NULL, NULL, NULL },
@@ -1511,5 +1516,5 @@
 { TARGET_NR_writev, "writev" , "%s(%d,%p,%#x)", NULL, NULL },
 #endif
 #ifdef TARGET_NR_utimensat
-{ TARGET_NR_utimensat, "utimensat", "%s(%d,\"%s\",%p,%#x)", NULL, NULL },
+{ TARGET_NR_utimensat, "utimensat", NULL, print_utimensat, NULL },
 #endif
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index f93292d..f1d844f 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -93,12 +93,6 @@
 
 //#define DEBUG
 
-#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
-    || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
-/* 16 bit uid wrappers emulation */
-#define USE_UID16
-#endif
-
 //#include <linux/msdos_fs.h>
 #define	VFAT_IOCTL_READDIR_BOTH		_IOR('r', 1, struct linux_dirent [2])
 #define	VFAT_IOCTL_READDIR_SHORT	_IOR('r', 2, struct linux_dirent [2])
diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h
index 7f0b0df..7c9d161 100644
--- a/linux-user/syscall_defs.h
+++ b/linux-user/syscall_defs.h
@@ -48,6 +48,12 @@
 #define TARGET_IOC_NRBITS	8
 #define TARGET_IOC_TYPEBITS	8
 
+#if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SPARC) \
+    || defined(TARGET_M68K) || defined(TARGET_SH4) || defined(TARGET_CRIS)
+    /* 16 bit uid wrappers emulation */
+#define USE_UID16
+#endif
+
 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_SH4) \
     || defined(TARGET_M68K) || defined(TARGET_CRIS)
 
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2]
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
                   ` (3 preceding siblings ...)
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 05/10] linux-user: strace now handles guest strings correctly riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-29 19:50   ` malc
  2009-04-30  7:07   ` [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2] Martin Mohring
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 07/10] linux-user: fix utimensat when used as futimens riku.voipio
                   ` (4 subsequent siblings)
  9 siblings, 2 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Mika Westerberg <mika.westerberg@iki.fi>

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.

[v2]: implemented GUEST_BASE with single LEA

Changed TCG (on x86 and x86_64) to generate single LEA instead
of MOV+ADD when calculating GUEST_BASE host addresses.

From: Mika Westerberg <mika.westerberg@iki.fi>
Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 configure               |    9 +++++++
 cpu-all.h               |    6 ++++-
 linux-user/elfload.c    |   24 ++++++++++++++++++++
 linux-user/main.c       |   54 +++++++++++++++++++++++++++++++++++++++++++++++
 linux-user/qemu.h       |    3 ++
 tcg/i386/tcg-target.c   |   14 ++++++++++++
 tcg/x86_64/tcg-target.c |   14 ++++++++++++
 7 files changed, 123 insertions(+), 1 deletions(-)

diff --git a/configure b/configure
index 82fb60a..bc89227 100755
--- a/configure
+++ b/configure
@@ -178,6 +178,7 @@ softmmu="yes"
 linux_user="no"
 darwin_user="no"
 bsd_user="no"
+guest_base="no"
 build_docs="no"
 uname_release=""
 curses="yes"
@@ -457,6 +458,8 @@ for opt do
   ;;
   --enable-bsd-user) bsd_user="yes"
   ;;
+  --enable-guest-base) guest_base="yes"
+  ;;
   --enable-uname-release=*) uname_release="$optarg"
   ;;
   --sparc_cpu=*)
@@ -611,6 +614,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"
@@ -1335,6 +1340,7 @@ echo "Documentation     $build_docs"
 [ ! -z "$uname_release" ] && \
 echo "uname -r          $uname_release"
 echo "NPTL support      $nptl"
+echo "GUEST_BASE        $guest_base"
 echo "vde support       $vde"
 echo "AIO support       $aio"
 echo "IO thread         $io_thread"
@@ -2022,6 +2028,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
diff --git a/cpu-all.h b/cpu-all.h
index 43a06ba..5413a92 100644
--- 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))
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index dc797bd..b5565dd 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -1466,6 +1466,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;
diff --git a/linux-user/main.c b/linux-user/main.c
index 72734c1..64b1ff0 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -41,6 +41,10 @@
 char *exec_path;
 
 int singlestep;
+#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;
@@ -2229,6 +2233,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"
@@ -2403,6 +2410,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")) {
             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
         } else if (!strcmp(r, "singlestep")) {
@@ -2480,6 +2491,36 @@ 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;
+                qemu_log("kernel mmap_min_addr=%lu\n", mmap_min_addr);
+            }
+            fclose(fp);
+        }
+    }
+#endif /* CONFIG_USE_GUEST_BASE */
+
     /*
      * Prepare copy of argv vector for target.
      */
@@ -2529,6 +2570,19 @@ 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" TARGET_ABI_FMT_lx " becomes\n"
+		"%p and so on.\n"
+                "==========================================================\n",
+                (abi_ulong)0x8000, g2h(0x8000));
+        }
+#endif
         log_page_dump();
 
         qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index ea4a57d..762f31f 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -133,6 +133,9 @@ void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
 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.  */
 /*
diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index e0fd434..757e128 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -560,6 +560,13 @@ 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.
+     *
+     * leaq GUEST_BASE(addr_reg), r0
+     */
+    tcg_out_modrm_offset(s, 0x8d, r0, addr_reg, GUEST_BASE);
 #else
     r0 = addr_reg;
 #endif
@@ -794,6 +801,13 @@ 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.
+     *
+     * leaq GUEST_BASE(addr_reg), r0
+     */
+    tcg_out_modrm_offset(s, 0x8d, r0, addr_reg, GUEST_BASE);
 #else
     r0 = addr_reg;
 #endif
diff --git a/tcg/x86_64/tcg-target.c b/tcg/x86_64/tcg-target.c
index 5378e85..ece2876 100644
--- a/tcg/x86_64/tcg-target.c
+++ b/tcg/x86_64/tcg-target.c
@@ -604,6 +604,13 @@ 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.
+     *
+     * leaq GUEST_BASE(addr_reg), r0
+     */
+    tcg_out_modrm_offset(s, 0x8d | rexw, r0, addr_reg, GUEST_BASE);
 #else
     r0 = addr_reg;
 #endif    
@@ -775,6 +782,13 @@ 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.
+     *
+     * leaq GUEST_BASE(addr_reg), r0
+     */
+    tcg_out_modrm_offset(s, 0x8d | rexw, r0, addr_reg, GUEST_BASE);
 #else
     r0 = addr_reg;
 #endif
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 07/10] linux-user: fix utimensat when used as futimens
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
                   ` (4 preceding siblings ...)
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2] riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 08/10] Fix struct termios host - target translation riku.voipio
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Riku Voipio <riku.voipio@iki.fi>

The glibc function for utimensat glibc returns -EINVAL when the path is null
which is a different behaviour with the syscall.

path can be null because internally the glibc is using utimensat with
path null when implmenting futimens. If path is null, call futimes
instead.

Also, add a check for utimensat at configure time.

Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 configure            |   22 ++++++++++++++++++++++
 linux-user/syscall.c |   23 ++++++++++++++---------
 2 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/configure b/configure
index bc89227..c45bd75 100755
--- a/configure
+++ b/configure
@@ -1241,6 +1241,25 @@ EOF
   fi
 fi
 
+# check if utimensat and futimens are supported
+utimens=no
+cat > $TMPC << EOF
+#define _ATFILE_SOURCE
+#define _GNU_SOURCE
+#include <stddef.h>
+#include <fcntl.h>
+
+int main(void)
+{
+    utimensat(AT_FDCWD, "foo", NULL, 0);
+    futimens(0, NULL);
+    return 0;
+}
+EOF
+if $cc $ARCH_CFLAGS -o $TMPE $TMPC 2> /dev/null ; then
+  utimens=yes
+fi
+
 # Check if tools are available to build documentation.
 if [ -x "`which texi2html 2>/dev/null`" ] && \
    [ -x "`which pod2man 2>/dev/null`" ]; then
@@ -1640,6 +1659,9 @@ fi
 if test "$atfile" = "yes" ; then
   echo "#define CONFIG_ATFILE 1" >> $config_h
 fi
+if test "$utimens" = "yes" ; then
+  echo "#define CONFIG_UTIMENSAT 1" >> $config_h
+fi
 if test "$inotify" = "yes" ; then
   echo "#define CONFIG_INOTIFY 1" >> $config_h
 fi
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index f1d844f..b76d6aa 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -406,13 +406,6 @@ static int sys_unlinkat(int dirfd, const char *pathname, int flags)
   return (unlinkat(dirfd, pathname, flags));
 }
 #endif
-#ifdef TARGET_NR_utimensat
-static int sys_utimensat(int dirfd, const char *pathname,
-    const struct timespec times[2], int flags)
-{
-  return (utimensat(dirfd, pathname, times, flags));
-}
-#endif
 #else /* !CONFIG_ATFILE */
 
 /*
@@ -471,12 +464,24 @@ _syscall3(int,sys_symlinkat,const char *,oldpath,
 #if defined(TARGET_NR_unlinkat) && defined(__NR_unlinkat)
 _syscall3(int,sys_unlinkat,int,dirfd,const char *,pathname,int,flags)
 #endif
+
+#endif /* CONFIG_ATFILE */
+
+#ifdef CONFIG_UTIMENSAT
+static int sys_utimensat(int dirfd, const char *pathname,
+    const struct timespec times[2], int flags)
+{
+    if (pathname == NULL)
+        return futimens(dirfd, times);
+    else
+        return utimensat(dirfd, pathname, times, flags);
+}
+#else
 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
 _syscall4(int,sys_utimensat,int,dirfd,const char *,pathname,
           const struct timespec *,tsp,int,flags)
 #endif
-
-#endif /* CONFIG_ATFILE */
+#endif /* CONFIG_UTIMENSAT  */
 
 #ifdef CONFIG_INOTIFY
 #include <sys/inotify.h>
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 08/10] Fix struct termios host - target translation
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
                   ` (5 preceding siblings ...)
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 07/10] linux-user: fix utimensat when used as futimens riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 09/10] linux-user: fix utimensat with NULL timespec riku.voipio
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Arnaud Patard <arnaud.patard@rtp-net.org>

When converting the termios structure between host and target in
target_to_host_termios and host_to_target_termios, the c_cc[] array is
never initialised.
Calling memset() before using it allows to run successfully "stty echo /
stty -echo" on arm-linux-user target (host being x86 and mips).

From: Arnaud Patard <arnaud.patard@rtp-net.org>
Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 linux-user/syscall.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index b76d6aa..f99db77 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -2977,6 +2977,7 @@ static void target_to_host_termios (void *dst, const void *src)
         target_to_host_bitmask(tswap32(target->c_lflag), lflag_tbl);
     host->c_line = target->c_line;
 
+    memset(host->c_cc, 0, sizeof(host->c_cc));
     host->c_cc[VINTR] = target->c_cc[TARGET_VINTR];
     host->c_cc[VQUIT] = target->c_cc[TARGET_VQUIT];
     host->c_cc[VERASE] = target->c_cc[TARGET_VERASE];
@@ -3011,6 +3012,7 @@ static void host_to_target_termios (void *dst, const void *src)
         tswap32(host_to_target_bitmask(host->c_lflag, lflag_tbl));
     target->c_line = host->c_line;
 
+    memset(target->c_cc, 0, sizeof(target->c_cc));
     target->c_cc[TARGET_VINTR] = host->c_cc[VINTR];
     target->c_cc[TARGET_VQUIT] = host->c_cc[VQUIT];
     target->c_cc[TARGET_VERASE] = host->c_cc[VERASE];
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 09/10] linux-user: fix utimensat with NULL timespec
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
                   ` (6 preceding siblings ...)
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 08/10] Fix struct termios host - target translation riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 10/10] Return EOPNOTSUPP instead of ENOSYS for *xattr* syscalls riku.voipio
  2009-04-30  7:09 ` [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat Martin Mohring
  9 siblings, 0 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Riku Voipio <riku.voipio@iki.fi>

don't try to copy timespec from user if is NULL.

Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 linux-user/syscall.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index f99db77..be00c7e 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -6674,17 +6674,22 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
 #if defined(TARGET_NR_utimensat) && defined(__NR_utimensat)
     case TARGET_NR_utimensat:
         {
-            struct timespec ts[2];
-            target_to_host_timespec(ts, arg3);
-            target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
+            struct timespec *tsp, ts[2];
+            if (!arg3) {
+                tsp = NULL;
+            } else {
+                target_to_host_timespec(ts, arg3);
+                target_to_host_timespec(ts+1, arg3+sizeof(struct target_timespec));
+                tsp = ts;
+            }
             if (!arg2)
-                ret = get_errno(sys_utimensat(arg1, NULL, ts, arg4));
+                ret = get_errno(sys_utimensat(arg1, NULL, tsp, arg4));
             else {
                 if (!(p = lock_user_string(arg2))) {
                     ret = -TARGET_EFAULT;
                     goto fail;
                 }
-                ret = get_errno(sys_utimensat(arg1, path(p), ts, arg4));
+                ret = get_errno(sys_utimensat(arg1, path(p), tsp, arg4));
                 unlock_user(p, arg2, 0);
             }
         }
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH 10/10] Return EOPNOTSUPP instead of ENOSYS for *xattr* syscalls
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
                   ` (7 preceding siblings ...)
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 09/10] linux-user: fix utimensat with NULL timespec riku.voipio
@ 2009-04-29 18:03 ` riku.voipio
  2009-04-30  7:09 ` [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat Martin Mohring
  9 siblings, 0 replies; 29+ messages in thread
From: riku.voipio @ 2009-04-29 18:03 UTC (permalink / raw)
  To: qemu-devel

From: Arnaud Patard <arnaud.patard@rtp-net.org>

In current code, we're sending ENOSYS to target when a syscall for the
xattrs is done. This makes applications like ls complain loudly about
that and breaks scripts parsing the output. Moreover, iirc, implemented
features of filesystems are are sending EOPNOTSUPP (I've not checked so
I may be a little bit wrong on that...).
So, I'm proposing to return -EOPNOTSUPP and make ls happy

From: Arnaud Patard <arnaud.patard@rtp-net.org>
Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 linux-user/syscall.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index be00c7e..183f852 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -6577,7 +6577,8 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_removexattr:
     case TARGET_NR_lremovexattr:
     case TARGET_NR_fremovexattr:
-        goto unimplemented_nowarn;
+        ret = -TARGET_EOPNOTSUPP;
+        break;
 #endif
 #ifdef TARGET_NR_set_thread_area
     case TARGET_NR_set_thread_area:
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2]
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2] riku.voipio
@ 2009-04-29 19:50   ` malc
  2009-05-05 13:27     ` [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v3] Riku Voipio
  2009-04-30  7:07   ` [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2] Martin Mohring
  1 sibling, 1 reply; 29+ messages in thread
From: malc @ 2009-04-29 19:50 UTC (permalink / raw)
  To: riku.voipio; +Cc: qemu-devel

On Wed, 29 Apr 2009, riku.voipio@iki.fi wrote:

> From: Mika Westerberg <mika.westerberg@iki.fi>
> 
> 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.
> 
> [v2]: implemented GUEST_BASE with single LEA
> 
> Changed TCG (on x86 and x86_64) to generate single LEA instead

After actually considering the whole picture neither MOV+ADD nor LEA
are needed at all, something like the following (i386 case, and not
tested):

diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index e0fd434..f17bca0 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -22,6 +22,10 @@
  * THE SOFTWARE.
  */
 
+#ifndef CONFIG_USE_GUEST_BASE
+#define GUEST_BASE 0
+#endif
+
 #ifndef NDEBUG
 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
     "%eax",
@@ -572,15 +576,15 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     switch(opc) {
     case 0:
         /* movzbl */
-        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, GUEST_BASE);
         break;
     case 0 | 4:
         /* movsbl */
-        tcg_out_modrm_offset(s, 0xbe | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xbe | P_EXT, data_reg, r0, GUEST_BASE);
         break;
     case 1:
         /* movzwl */
-        tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* rolw $8, data_reg */
             tcg_out8(s, 0x66); 
@@ -590,7 +594,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
         break;
     case 1 | 4:
         /* movswl */
-        tcg_out_modrm_offset(s, 0xbf | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xbf | P_EXT, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* rolw $8, data_reg */
             tcg_out8(s, 0x66); 
@@ -603,7 +607,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
         break;
     case 2:
         /* movl (r0), data_reg */
-        tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* bswap */
             tcg_out_opc(s, (0xc8 + data_reg) | P_EXT);
@@ -615,17 +619,17 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
             r1 = TCG_REG_EDX;
             if (r1 == data_reg)
                 r1 = TCG_REG_EAX;
-            tcg_out_mov(s, r1, r0);
+            tcg_out_mov(s, r1, GUEST_BASE);
             r0 = r1;
         }
         if (!bswap) {
-            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 0);
-            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, 4);
+            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE);
+            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, GUEST_BASE + 4);
         } else {
-            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 4);
+            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE + 4);
             tcg_out_opc(s, (0xc8 + data_reg) | P_EXT);
 
-            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, 0);
+            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, GUEST_BASE);
             /* bswap */
             tcg_out_opc(s, (0xc8 + data_reg2) | P_EXT);
         }
@@ -806,7 +810,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
     switch(opc) {
     case 0:
         /* movb */
-        tcg_out_modrm_offset(s, 0x88, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x88, data_reg, r0, GUEST_BASE);
         break;
     case 1:
         if (bswap) {
@@ -818,7 +822,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
         }
         /* movw */
         tcg_out8(s, 0x66);
-        tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
         break;
     case 2:
         if (bswap) {
@@ -828,21 +832,21 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
             data_reg = r1;
         }
         /* movl */
-        tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
         break;
     case 3:
         if (bswap) {
             tcg_out_mov(s, r1, data_reg2);
             /* bswap data_reg */
             tcg_out_opc(s, (0xc8 + r1) | P_EXT);
-            tcg_out_modrm_offset(s, 0x89, r1, r0, 0);
+            tcg_out_modrm_offset(s, 0x89, r1, r0, GUEST_BASE);
             tcg_out_mov(s, r1, data_reg);
             /* bswap data_reg */
             tcg_out_opc(s, (0xc8 + r1) | P_EXT);
-            tcg_out_modrm_offset(s, 0x89, r1, r0, 4);
+            tcg_out_modrm_offset(s, 0x89, r1, r0, GUEST_BASE + 4);
         } else {
-            tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
-            tcg_out_modrm_offset(s, 0x89, data_reg2, r0, 4);
+            tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
+            tcg_out_modrm_offset(s, 0x89, data_reg2, r0, GUEST_BASE + 4);
         }
         break;
     default:


P.S. BTW revived?

-- 
mailto:av1474@comtv.ru

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2]
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2] riku.voipio
  2009-04-29 19:50   ` malc
@ 2009-04-30  7:07   ` Martin Mohring
  1 sibling, 0 replies; 29+ messages in thread
From: Martin Mohring @ 2009-04-30  7:07 UTC (permalink / raw)
  To: riku.voipio; +Cc: qemu-devel

riku.voipio@iki.fi wrote:
> From: Mika Westerberg <mika.westerberg@iki.fi>
>
> 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.
>   
this is a patch that finally solved my problems on linux systems where
/proc/sys/vm/mmap_min_addr is set to non zero.
from my point of view: pls apply. i have tested it on arm targets Debian
Lenny, Ubuntu 9.04 and Fedora 10. works fine for me.
> [v2]: implemented GUEST_BASE with single LEA
>
> Changed TCG (on x86 and x86_64) to generate single LEA instead
> of MOV+ADD when calculating GUEST_BASE host addresses.
>
> From: Mika Westerberg <mika.westerberg@iki.fi>
> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
> ---
>  configure               |    9 +++++++
>  cpu-all.h               |    6 ++++-
>  linux-user/elfload.c    |   24 ++++++++++++++++++++
>  linux-user/main.c       |   54 +++++++++++++++++++++++++++++++++++++++++++++++
>  linux-user/qemu.h       |    3 ++
>  tcg/i386/tcg-target.c   |   14 ++++++++++++
>  tcg/x86_64/tcg-target.c |   14 ++++++++++++
>  7 files changed, 123 insertions(+), 1 deletions(-)
>   

I combine this patch with patch 1+2 of the same series.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat
  2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
                   ` (8 preceding siblings ...)
  2009-04-29 18:03 ` [Qemu-devel] [PATCH 10/10] Return EOPNOTSUPP instead of ENOSYS for *xattr* syscalls riku.voipio
@ 2009-04-30  7:09 ` Martin Mohring
  9 siblings, 0 replies; 29+ messages in thread
From: Martin Mohring @ 2009-04-30  7:09 UTC (permalink / raw)
  To: riku.voipio; +Cc: qemu-devel

riku.voipio@iki.fi wrote:
> From: Riku Voipio <riku.voipio@iki.fi>
>
> since mmap_find_vma rewrite is being passively refused, drop it
> for now, and just export the existing function.
>
> Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
> ---
>  linux-user/mmap.c |    2 +-
>  linux-user/qemu.h |    1 +
>  2 files changed, 2 insertions(+), 1 deletions(-)
>
> diff --git a/linux-user/mmap.c b/linux-user/mmap.c
> index 6f300a0..aa5813f 100644
> --- a/linux-user/mmap.c
> +++ b/linux-user/mmap.c
> @@ -281,7 +281,7 @@ unsigned long last_brk;
>  */
>  /* page_init() marks pages used by the host as reserved to be sure not
>     to use them. */
> -static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
> +abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size)
>  {
>      abi_ulong addr, addr1, addr_start;
>      int prot;
> diff --git a/linux-user/qemu.h b/linux-user/qemu.h
> index 447caf9..15db106 100644
> --- a/linux-user/qemu.h
> +++ b/linux-user/qemu.h
> @@ -229,6 +229,7 @@ int target_msync(abi_ulong start, abi_ulong len, int flags);
>  extern unsigned long last_brk;
>  void mmap_lock(void);
>  void mmap_unlock(void);
> +abi_ulong mmap_find_vma(abi_ulong, abi_ulong);
>  void cpu_list_lock(void);
>  void cpu_list_unlock(void);
>  #if defined(USE_NPTL)
>   
As I commented I combine this patch with patch 2+6 of the same series.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v3]
  2009-04-29 19:50   ` malc
@ 2009-05-05 13:27     ` Riku Voipio
  2009-05-05 13:53       ` Paul Brook
  0 siblings, 1 reply; 29+ messages in thread
From: Riku Voipio @ 2009-05-05 13:27 UTC (permalink / raw)
  To: malc; +Cc: mika.westerberg, qemu-devel

On Wed, Apr 29, 2009 at 11:50:47PM +0400, malc wrote:
> After actually considering the whole picture neither MOV+ADD nor LEA
> are needed at all, something like the following (i386 case, and not
> tested):

Thanks, tested, found out to work, and implemented case amd64 too.

> P.S. BTW revived?

Well, GUEST_BASE variable was there already, so we though that it
has worked at some point of history, just that it bitrotted and broken.

>From 0f80be6130b37e0beaf4f1c2a2f2ff6633157113 Mon Sep 17 00:00:00 2001
From: Mika Westerberg <mika.westerberg@iki.fi>
Date: Sun, 29 Mar 2009 21:46:34 +0300
Subject: [PATCH 06/16] Revived GUEST_BASE support for usermode emulation targets [v3]

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.

[v3]: implemented GUEST_BASE with offset mods

Using malc's advice, instead of a mov or lea, just adjust
the offset parameter of tcg_out_modrm_offset(). - Riku

Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 configure               |    9 +++++++
 cpu-all.h               |    6 ++++-
 linux-user/elfload.c    |   24 ++++++++++++++++++++
 linux-user/main.c       |   54 +++++++++++++++++++++++++++++++++++++++++++++++
 linux-user/qemu.h       |    3 ++
 tcg/i386/tcg-target.c   |   32 ++++++++++++++--------------
 tcg/x86_64/tcg-target.c |   26 +++++++++++-----------
 7 files changed, 124 insertions(+), 30 deletions(-)

diff --git a/configure b/configure
index 82fb60a..bc89227 100755
--- a/configure
+++ b/configure
@@ -178,6 +178,7 @@ softmmu="yes"
 linux_user="no"
 darwin_user="no"
 bsd_user="no"
+guest_base="no"
 build_docs="no"
 uname_release=""
 curses="yes"
@@ -457,6 +458,8 @@ for opt do
   ;;
   --enable-bsd-user) bsd_user="yes"
   ;;
+  --enable-guest-base) guest_base="yes"
+  ;;
   --enable-uname-release=*) uname_release="$optarg"
   ;;
   --sparc_cpu=*)
@@ -611,6 +614,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"
@@ -1335,6 +1340,7 @@ echo "Documentation     $build_docs"
 [ ! -z "$uname_release" ] && \
 echo "uname -r          $uname_release"
 echo "NPTL support      $nptl"
+echo "GUEST_BASE        $guest_base"
 echo "vde support       $vde"
 echo "AIO support       $aio"
 echo "IO thread         $io_thread"
@@ -2022,6 +2028,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
diff --git a/cpu-all.h b/cpu-all.h
index 676c4a9..4f87ee7 100644
--- 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))
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index dc797bd..b5565dd 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -1466,6 +1466,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;
diff --git a/linux-user/main.c b/linux-user/main.c
index 72734c1..64b1ff0 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -41,6 +41,10 @@
 char *exec_path;
 
 int singlestep;
+#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;
@@ -2229,6 +2233,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"
@@ -2403,6 +2410,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")) {
             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
         } else if (!strcmp(r, "singlestep")) {
@@ -2480,6 +2491,36 @@ 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;
+                qemu_log("kernel mmap_min_addr=%lu\n", mmap_min_addr);
+            }
+            fclose(fp);
+        }
+    }
+#endif /* CONFIG_USE_GUEST_BASE */
+
     /*
      * Prepare copy of argv vector for target.
      */
@@ -2529,6 +2570,19 @@ 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" TARGET_ABI_FMT_lx " becomes\n"
+		"%p and so on.\n"
+                "==========================================================\n",
+                (abi_ulong)0x8000, g2h(0x8000));
+        }
+#endif
         log_page_dump();
 
         qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index ea4a57d..762f31f 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -133,6 +133,9 @@ void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
 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.  */
 /*
diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index e0fd434..b73ab30 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -572,15 +572,15 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     switch(opc) {
     case 0:
         /* movzbl */
-        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, GUEST_BASE);
         break;
     case 0 | 4:
         /* movsbl */
-        tcg_out_modrm_offset(s, 0xbe | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xbe | P_EXT, data_reg, r0, GUEST_BASE);
         break;
     case 1:
         /* movzwl */
-        tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* rolw $8, data_reg */
             tcg_out8(s, 0x66); 
@@ -590,7 +590,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
         break;
     case 1 | 4:
         /* movswl */
-        tcg_out_modrm_offset(s, 0xbf | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xbf | P_EXT, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* rolw $8, data_reg */
             tcg_out8(s, 0x66); 
@@ -603,7 +603,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
         break;
     case 2:
         /* movl (r0), data_reg */
-        tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* bswap */
             tcg_out_opc(s, (0xc8 + data_reg) | P_EXT);
@@ -619,13 +619,13 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
             r0 = r1;
         }
         if (!bswap) {
-            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 0);
-            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, 4);
+            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE);
+            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, GUEST_BASE + 4);
         } else {
-            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 4);
+            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE + 4);
             tcg_out_opc(s, (0xc8 + data_reg) | P_EXT);
 
-            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, 0);
+            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, GUEST_BASE);
             /* bswap */
             tcg_out_opc(s, (0xc8 + data_reg2) | P_EXT);
         }
@@ -806,7 +806,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
     switch(opc) {
     case 0:
         /* movb */
-        tcg_out_modrm_offset(s, 0x88, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x88, data_reg, r0, GUEST_BASE);
         break;
     case 1:
         if (bswap) {
@@ -818,7 +818,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
         }
         /* movw */
         tcg_out8(s, 0x66);
-        tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
         break;
     case 2:
         if (bswap) {
@@ -828,21 +828,21 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
             data_reg = r1;
         }
         /* movl */
-        tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
         break;
     case 3:
         if (bswap) {
             tcg_out_mov(s, r1, data_reg2);
             /* bswap data_reg */
             tcg_out_opc(s, (0xc8 + r1) | P_EXT);
-            tcg_out_modrm_offset(s, 0x89, r1, r0, 0);
+            tcg_out_modrm_offset(s, 0x89, r1, r0, GUEST_BASE);
             tcg_out_mov(s, r1, data_reg);
             /* bswap data_reg */
             tcg_out_opc(s, (0xc8 + r1) | P_EXT);
-            tcg_out_modrm_offset(s, 0x89, r1, r0, 4);
+            tcg_out_modrm_offset(s, 0x89, r1, r0, GUEST_BASE + 4);
         } else {
-            tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
-            tcg_out_modrm_offset(s, 0x89, data_reg2, r0, 4);
+            tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
+            tcg_out_modrm_offset(s, 0x89, data_reg2, r0, GUEST_BASE + 4);
         }
         break;
     default:
diff --git a/tcg/x86_64/tcg-target.c b/tcg/x86_64/tcg-target.c
index 5378e85..e239dcb 100644
--- a/tcg/x86_64/tcg-target.c
+++ b/tcg/x86_64/tcg-target.c
@@ -616,15 +616,15 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     switch(opc) {
     case 0:
         /* movzbl */
-        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, GUEST_BASE);
         break;
     case 0 | 4:
         /* movsbX */
-        tcg_out_modrm_offset(s, 0xbe | P_EXT | rexw, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xbe | P_EXT | rexw, data_reg, r0, GUEST_BASE);
         break;
     case 1:
         /* movzwl */
-        tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* rolw $8, data_reg */
             tcg_out8(s, 0x66); 
@@ -635,7 +635,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     case 1 | 4:
         if (bswap) {
             /* movzwl */
-            tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, 0);
+            tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, GUEST_BASE);
             /* rolw $8, data_reg */
             tcg_out8(s, 0x66); 
             tcg_out_modrm(s, 0xc1, 0, data_reg);
@@ -645,12 +645,12 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
             tcg_out_modrm(s, 0xbf | P_EXT | rexw, data_reg, data_reg);
         } else {
             /* movswX */
-            tcg_out_modrm_offset(s, 0xbf | P_EXT | rexw, data_reg, r0, 0);
+            tcg_out_modrm_offset(s, 0xbf | P_EXT | rexw, data_reg, r0, GUEST_BASE);
         }
         break;
     case 2:
         /* movl (r0), data_reg */
-        tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* bswap */
             tcg_out_opc(s, (0xc8 + (data_reg & 7)) | P_EXT, 0, data_reg, 0);
@@ -659,19 +659,19 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     case 2 | 4:
         if (bswap) {
             /* movl (r0), data_reg */
-            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 0);
+            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE);
             /* bswap */
             tcg_out_opc(s, (0xc8 + (data_reg & 7)) | P_EXT, 0, data_reg, 0);
             /* movslq */
             tcg_out_modrm(s, 0x63 | P_REXW, data_reg, data_reg);
         } else {
             /* movslq */
-            tcg_out_modrm_offset(s, 0x63 | P_REXW, data_reg, r0, 0);
+            tcg_out_modrm_offset(s, 0x63 | P_REXW, data_reg, r0, GUEST_BASE);
         }
         break;
     case 3:
         /* movq (r0), data_reg */
-        tcg_out_modrm_offset(s, 0x8b | P_REXW, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x8b | P_REXW, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* bswap */
             tcg_out_opc(s, (0xc8 + (data_reg & 7)) | P_EXT | P_REXW, 0, data_reg, 0);
@@ -787,7 +787,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
     switch(opc) {
     case 0:
         /* movb */
-        tcg_out_modrm_offset(s, 0x88 | P_REXB, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x88 | P_REXB, data_reg, r0, GUEST_BASE);
         break;
     case 1:
         if (bswap) {
@@ -799,7 +799,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
         }
         /* movw */
         tcg_out8(s, 0x66);
-        tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
         break;
     case 2:
         if (bswap) {
@@ -809,7 +809,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
             data_reg = r1;
         }
         /* movl */
-        tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
         break;
     case 3:
         if (bswap) {
@@ -819,7 +819,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
             data_reg = r1;
         }
         /* movq */
-        tcg_out_modrm_offset(s, 0x89 | P_REXW, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89 | P_REXW, data_reg, r0, GUEST_BASE);
         break;
     default:
         tcg_abort();
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v3]
  2009-05-05 13:27     ` [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v3] Riku Voipio
@ 2009-05-05 13:53       ` Paul Brook
  2009-05-05 14:18         ` Riku Voipio
  0 siblings, 1 reply; 29+ messages in thread
From: Paul Brook @ 2009-05-05 13:53 UTC (permalink / raw)
  To: qemu-devel; +Cc: mika.westerberg, Riku Voipio

On Tuesday 05 May 2009, Riku Voipio wrote:
> +++ b/tcg/x86_64/tcg-target.c
> @@ -616,15 +616,15 @@ static void tcg_out_qemu_ld(TCGContext *s, const
>          /* movzbl */
> -        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, 0);
> +        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, GUEST_BASE);

This breaks when GUEST_BASE is large.
32-bit x86 is probably ok because GUEST_BASE is always a 32-bit value. This is 
not true for x86-64. Incidentally tcg_out_modrm doesn't seem to check this, 
and silently generates broken code.

Paul

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v3]
  2009-05-05 13:53       ` Paul Brook
@ 2009-05-05 14:18         ` Riku Voipio
  2009-05-05 14:34           ` Paul Brook
  2009-05-05 18:02           ` malc
  0 siblings, 2 replies; 29+ messages in thread
From: Riku Voipio @ 2009-05-05 14:18 UTC (permalink / raw)
  To: Paul Brook; +Cc: mika.westerberg, qemu-devel

On Tue, May 05, 2009 at 02:53:31PM +0100, Paul Brook wrote:
> On Tuesday 05 May 2009, Riku Voipio wrote:
> > +++ b/tcg/x86_64/tcg-target.c
> > @@ -616,15 +616,15 @@ static void tcg_out_qemu_ld(TCGContext *s, const
> >          /* movzbl */
> > -        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, 0);
> > +        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, GUEST_BASE);

> This breaks when GUEST_BASE is large.

usually we don't set it to very large, just high enough to overcome
mmap_min_addr limits.

> 32-bit x86 is probably ok because GUEST_BASE is always a 32-bit value. This is 
> not true for x86-64. Incidentally tcg_out_modrm doesn't seem to check this, 
> and silently generates broken code.

Should we rather check for a mazimum size on guest_base or revert to the lea -based
version?

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v3]
  2009-05-05 14:18         ` Riku Voipio
@ 2009-05-05 14:34           ` Paul Brook
  2009-05-05 18:02           ` malc
  1 sibling, 0 replies; 29+ messages in thread
From: Paul Brook @ 2009-05-05 14:34 UTC (permalink / raw)
  To: qemu-devel; +Cc: mika.westerberg, Riku Voipio

On Tuesday 05 May 2009, Riku Voipio wrote:
> On Tue, May 05, 2009 at 02:53:31PM +0100, Paul Brook wrote:
> > On Tuesday 05 May 2009, Riku Voipio wrote:
> > > +++ b/tcg/x86_64/tcg-target.c
> > > @@ -616,15 +616,15 @@ static void tcg_out_qemu_ld(TCGContext *s, const
> > >          /* movzbl */
> > > -        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, 0);
> > > +        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0,
> > > GUEST_BASE);
> >
> > This breaks when GUEST_BASE is large.
>
> usually we don't set it to very large, just high enough to overcome
> mmap_min_addr limits.

I'd expect a common use would be to move a 32-bit guest to an entirely empty 
range of address space. Chances are this is going to be >4G.

> > 32-bit x86 is probably ok because GUEST_BASE is always a 32-bit value.
> > This is not true for x86-64. Incidentally tcg_out_modrm doesn't seem to
> > check this, and silently generates broken code.
>
> Should we rather check for a mazimum size on guest_base or revert to the
> lea -based version?

I expect LEA has the same bug. We definitely need to be able to handle large 
offsets on 64-bit hosts, though obviously we want to avoid the overhead when 
the offset is small.

Paul

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v3]
  2009-05-05 14:18         ` Riku Voipio
  2009-05-05 14:34           ` Paul Brook
@ 2009-05-05 18:02           ` malc
  2009-05-05 20:46             ` [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4] Riku Voipio
  1 sibling, 1 reply; 29+ messages in thread
From: malc @ 2009-05-05 18:02 UTC (permalink / raw)
  To: Riku Voipio; +Cc: mika.westerberg, Paul Brook, qemu-devel

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1944 bytes --]

On Tue, 5 May 2009, Riku Voipio wrote:

> On Tue, May 05, 2009 at 02:53:31PM +0100, Paul Brook wrote:
> > On Tuesday 05 May 2009, Riku Voipio wrote:
> > > +++ b/tcg/x86_64/tcg-target.c
> > > @@ -616,15 +616,15 @@ static void tcg_out_qemu_ld(TCGContext *s, const
> > >          /* movzbl */
> > > -        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, 0);
> > > +        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, GUEST_BASE);
> 
> > This breaks when GUEST_BASE is large.
> 
> usually we don't set it to very large, just high enough to overcome
> mmap_min_addr limits.

Yes, it would break, i failed to considered x86_64, never used it in fact, 
so was unaware that displacements are constrained there.

Anyway, AMD64 Architecture Programmer's Manual Volume 1: Application 
Programming says:

<quote>
Displacements and Immediates. In general, the maximum size of address
displacements and immediate operands is 32 bits. They can be 8, 16, or
32 bits in size, depending on the instruction or, for displacements,
the effective address size. In 64-bit mode, displacements are
sign-extended to 64 bits during use, but their actual size (for value
representation) remains a maximum of 32 bits. The same is true for
immediates in 64-bit mode, when the operand size is 64 bits. However,
support is provided in 64-bit mode for some 64-bit displacement and
immediate forms of the MOV instruction.
</quote>
 
> > 32-bit x86 is probably ok because GUEST_BASE is always a 32-bit value. 
> > This is not true for x86-64. Incidentally tcg_out_modrm doesn't seem 
> > to check this, and silently generates broken code.
> 
> Should we rather check for a mazimum size on guest_base or revert to the 
> lea -based version?

Things should work the way they are in your latest patch on x86 and should
be done the way it's done in the original submission (mov + add) for
x86_64.

-- 
mailto:av1474@comtv.ru

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-05 18:02           ` malc
@ 2009-05-05 20:46             ` Riku Voipio
  2009-05-15  2:25               ` Paul Brook
  0 siblings, 1 reply; 29+ messages in thread
From: Riku Voipio @ 2009-05-05 20:46 UTC (permalink / raw)
  To: malc; +Cc: martin.mohring, mika.westerberg, Paul Brook, qemu-devel

On Tue, May 05, 2009 at 10:02:41PM +0400, malc wrote:
> Things should work the way they are in your latest patch on x86 and should
> be done the way it's done in the original submission (mov + add) for
> x86_64.

Ok.. here we go. not on a amd64 machine right now so I will test
that part tomorrow.

>From dbd6b244f3b9b3e6ddccd09a03955bb79cd7c208 Mon Sep 17 00:00:00 2001
From: Mika Westerberg <mika.westerberg@iki.fi>
Date: Sun, 29 Mar 2009 21:46:34 +0300
Subject: [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]

- 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.

[v3]: implemented GUEST_BASE with offset mods

Using malc's advice, instead of a mov or lea, just adjust
the offset parameter of tcg_out_modrm_offset(). - Riku

[v4]: revert amd64 version to the original mov+add version.

From: Mika Westerberg <mika.westerberg@iki.fi>
Signed-off-by: Riku Voipio <riku.voipio@iki.fi>
---
 configure               |    9 +++++++
 cpu-all.h               |    6 ++++-
 linux-user/elfload.c    |   24 ++++++++++++++++++++
 linux-user/main.c       |   54 +++++++++++++++++++++++++++++++++++++++++++++++
 linux-user/qemu.h       |    3 ++
 tcg/i386/tcg-target.c   |   36 +++++++++++++++++--------------
 tcg/x86_64/tcg-target.c |   12 ++++++++++
 7 files changed, 127 insertions(+), 17 deletions(-)

diff --git a/configure b/configure
index 82fb60a..bc89227 100755
--- a/configure
+++ b/configure
@@ -178,6 +178,7 @@ softmmu="yes"
 linux_user="no"
 darwin_user="no"
 bsd_user="no"
+guest_base="no"
 build_docs="no"
 uname_release=""
 curses="yes"
@@ -457,6 +458,8 @@ for opt do
   ;;
   --enable-bsd-user) bsd_user="yes"
   ;;
+  --enable-guest-base) guest_base="yes"
+  ;;
   --enable-uname-release=*) uname_release="$optarg"
   ;;
   --sparc_cpu=*)
@@ -611,6 +614,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"
@@ -1335,6 +1340,7 @@ echo "Documentation     $build_docs"
 [ ! -z "$uname_release" ] && \
 echo "uname -r          $uname_release"
 echo "NPTL support      $nptl"
+echo "GUEST_BASE        $guest_base"
 echo "vde support       $vde"
 echo "AIO support       $aio"
 echo "IO thread         $io_thread"
@@ -2022,6 +2028,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
diff --git a/cpu-all.h b/cpu-all.h
index 676c4a9..4f87ee7 100644
--- 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))
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index dc797bd..b5565dd 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -1466,6 +1466,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;
diff --git a/linux-user/main.c b/linux-user/main.c
index 72734c1..64b1ff0 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -41,6 +41,10 @@
 char *exec_path;
 
 int singlestep;
+#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;
@@ -2229,6 +2233,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"
@@ -2403,6 +2410,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")) {
             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
         } else if (!strcmp(r, "singlestep")) {
@@ -2480,6 +2491,36 @@ 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;
+                qemu_log("kernel mmap_min_addr=%lu\n", mmap_min_addr);
+            }
+            fclose(fp);
+        }
+    }
+#endif /* CONFIG_USE_GUEST_BASE */
+
     /*
      * Prepare copy of argv vector for target.
      */
@@ -2529,6 +2570,19 @@ 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" TARGET_ABI_FMT_lx " becomes\n"
+		"%p and so on.\n"
+                "==========================================================\n",
+                (abi_ulong)0x8000, g2h(0x8000));
+        }
+#endif
         log_page_dump();
 
         qemu_log("start_brk   0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index ea4a57d..762f31f 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -133,6 +133,9 @@ void init_task_state(TaskState *ts);
 void task_settid(TaskState *);
 void stop_all_tasks(void);
 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.  */
 /*
diff --git a/tcg/i386/tcg-target.c b/tcg/i386/tcg-target.c
index e0fd434..aaec3c7 100644
--- a/tcg/i386/tcg-target.c
+++ b/tcg/i386/tcg-target.c
@@ -22,6 +22,10 @@
  * THE SOFTWARE.
  */
 
+#ifndef CONFIG_USE_GUEST_BASE
+#define GUEST_BASE 0
+#endif
+
 #ifndef NDEBUG
 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
     "%eax",
@@ -572,15 +576,15 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
     switch(opc) {
     case 0:
         /* movzbl */
-        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xb6 | P_EXT, data_reg, r0, GUEST_BASE);
         break;
     case 0 | 4:
         /* movsbl */
-        tcg_out_modrm_offset(s, 0xbe | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xbe | P_EXT, data_reg, r0, GUEST_BASE);
         break;
     case 1:
         /* movzwl */
-        tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xb7 | P_EXT, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* rolw $8, data_reg */
             tcg_out8(s, 0x66); 
@@ -590,7 +594,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
         break;
     case 1 | 4:
         /* movswl */
-        tcg_out_modrm_offset(s, 0xbf | P_EXT, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0xbf | P_EXT, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* rolw $8, data_reg */
             tcg_out8(s, 0x66); 
@@ -603,7 +607,7 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
         break;
     case 2:
         /* movl (r0), data_reg */
-        tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE);
         if (bswap) {
             /* bswap */
             tcg_out_opc(s, (0xc8 + data_reg) | P_EXT);
@@ -619,13 +623,13 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,
             r0 = r1;
         }
         if (!bswap) {
-            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 0);
-            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, 4);
+            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE);
+            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, GUEST_BASE + 4);
         } else {
-            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, 4);
+            tcg_out_modrm_offset(s, 0x8b, data_reg, r0, GUEST_BASE + 4);
             tcg_out_opc(s, (0xc8 + data_reg) | P_EXT);
 
-            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, 0);
+            tcg_out_modrm_offset(s, 0x8b, data_reg2, r0, GUEST_BASE);
             /* bswap */
             tcg_out_opc(s, (0xc8 + data_reg2) | P_EXT);
         }
@@ -806,7 +810,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
     switch(opc) {
     case 0:
         /* movb */
-        tcg_out_modrm_offset(s, 0x88, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x88, data_reg, r0, GUEST_BASE);
         break;
     case 1:
         if (bswap) {
@@ -818,7 +822,7 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
         }
         /* movw */
         tcg_out8(s, 0x66);
-        tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
         break;
     case 2:
         if (bswap) {
@@ -828,21 +832,21 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args,
             data_reg = r1;
         }
         /* movl */
-        tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
+        tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
         break;
     case 3:
         if (bswap) {
             tcg_out_mov(s, r1, data_reg2);
             /* bswap data_reg */
             tcg_out_opc(s, (0xc8 + r1) | P_EXT);
-            tcg_out_modrm_offset(s, 0x89, r1, r0, 0);
+            tcg_out_modrm_offset(s, 0x89, r1, r0, GUEST_BASE);
             tcg_out_mov(s, r1, data_reg);
             /* bswap data_reg */
             tcg_out_opc(s, (0xc8 + r1) | P_EXT);
-            tcg_out_modrm_offset(s, 0x89, r1, r0, 4);
+            tcg_out_modrm_offset(s, 0x89, r1, r0, GUEST_BASE + 4);
         } else {
-            tcg_out_modrm_offset(s, 0x89, data_reg, r0, 0);
-            tcg_out_modrm_offset(s, 0x89, data_reg2, r0, 4);
+            tcg_out_modrm_offset(s, 0x89, data_reg, r0, GUEST_BASE);
+            tcg_out_modrm_offset(s, 0x89, data_reg2, r0, GUEST_BASE + 4);
         }
         break;
     default:
diff --git a/tcg/x86_64/tcg-target.c b/tcg/x86_64/tcg-target.c
index 5378e85..f467cb0 100644
--- a/tcg/x86_64/tcg-target.c
+++ b/tcg/x86_64/tcg-target.c
@@ -604,6 +604,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    
@@ -775,6 +781,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
-- 
1.6.2.1

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-05 20:46             ` [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4] Riku Voipio
@ 2009-05-15  2:25               ` Paul Brook
  2009-05-15  8:41                 ` Martin Mohring
  2009-05-15  9:57                 ` Riku Voipio
  0 siblings, 2 replies; 29+ messages in thread
From: Paul Brook @ 2009-05-15  2:25 UTC (permalink / raw)
  To: qemu-devel; +Cc: martin.mohring, mika.westerberg, Riku Voipio

On Tuesday 05 May 2009, Riku Voipio wrote:
> On Tue, May 05, 2009 at 10:02:41PM +0400, malc wrote:
> > Things should work the way they are in your latest patch on x86 and
> > should be done the way it's done in the original submission (mov + add)
> > for x86_64.
>
> Ok.. here we go. not on a amd64 machine right now so I will test
> that part tomorrow.

I'm pretty sure it's still broken for large offsets.

Paul

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15  2:25               ` Paul Brook
@ 2009-05-15  8:41                 ` Martin Mohring
  2009-05-15  9:50                   ` Paul Brook
  2009-05-15  9:57                 ` Riku Voipio
  1 sibling, 1 reply; 29+ messages in thread
From: Martin Mohring @ 2009-05-15  8:41 UTC (permalink / raw)
  To: Paul Brook; +Cc: martin.mohring, mika.westerberg, Riku Voipio, qemu-devel

Paul Brook wrote:
> On Tuesday 05 May 2009, Riku Voipio wrote:
>   
>> On Tue, May 05, 2009 at 10:02:41PM +0400, malc wrote:
>>     
>>> Things should work the way they are in your latest patch on x86 and
>>> should be done the way it's done in the original submission (mov + add)
>>> for x86_64.
>>>       
>> Ok.. here we go. not on a amd64 machine right now so I will test
>> that part tomorrow.
>>     
>
> I'm pretty sure it's still broken for large offsets.
>
> Paul
>   
Is big offsets the intention of this patch? As far as I understood its
for small offsets (typical 64k - on debian, ubuntu, suse, fedora and all
the others - i checked it because it tested this), so address 0 will not
be used in typical linux configs today to grab null pointer exceptions.

As result of some commits, the patch currently does not apply anymore.

Martin

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15  8:41                 ` Martin Mohring
@ 2009-05-15  9:50                   ` Paul Brook
  0 siblings, 0 replies; 29+ messages in thread
From: Paul Brook @ 2009-05-15  9:50 UTC (permalink / raw)
  To: qemu-devel; +Cc: Martin Mohring, mika.westerberg, Riku Voipio

> > I'm pretty sure it's still broken for large offsets.
> >
> Is big offsets the intention of this patch? As far as I understood its
> for small offsets (typical 64k - on debian, ubuntu, suse, fedora and all
> the others - i checked it because it tested this), so address 0 will not
> be used in typical linux configs today to grab null pointer exceptions.

The intent of the patch is to move the guest virtual address space to a void 
conflicts with the host VM. For a 32-bit guest n a 64-bit host it makes a lot 
of sense to pick a completely unused 4G region (and probably even reserve it 
so the host doesn't start using it), which is almost certainly going to be at 
a high address.

Paul

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15  2:25               ` Paul Brook
  2009-05-15  8:41                 ` Martin Mohring
@ 2009-05-15  9:57                 ` Riku Voipio
  2009-05-15 10:02                   ` Paul Brook
  2009-05-15 10:12                   ` Martin Mohring
  1 sibling, 2 replies; 29+ messages in thread
From: Riku Voipio @ 2009-05-15  9:57 UTC (permalink / raw)
  To: Paul Brook; +Cc: martin.mohring, mika.westerberg, qemu-devel

On Fri, May 15, 2009 at 03:25:31AM +0100, Paul Brook wrote:
> On Tuesday 05 May 2009, Riku Voipio wrote:
> > On Tue, May 05, 2009 at 10:02:41PM +0400, malc wrote:
> > > Things should work the way they are in your latest patch on x86 and
> > > should be done the way it's done in the original submission (mov + add)
> > > for x86_64.

> > Ok.. here we go. not on a amd64 machine right now so I will test
> > that part tomorrow.

> I'm pretty sure it's still broken for large offsets.

It is indeed broken. Since tcg_out_addi maps on x86_64 to tgen_arithi64 which
doesn't accept 64bit values. But as Martin said, the currrent use for this
patch is just to add very minimal offsets.

Martin, I just rebased the linux-user patches, they should apply again
(there was only a conflict in configure):

https://git.maemo.org/projects/qemu/gitweb?p=qemu;a=shortlog;h=refs/heads/linux-user-for-upstream

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15  9:57                 ` Riku Voipio
@ 2009-05-15 10:02                   ` Paul Brook
  2009-05-15 10:09                     ` Paul Brook
  2009-05-15 12:07                     ` malc
  2009-05-15 10:12                   ` Martin Mohring
  1 sibling, 2 replies; 29+ messages in thread
From: Paul Brook @ 2009-05-15 10:02 UTC (permalink / raw)
  To: Riku Voipio; +Cc: martin.mohring, mika.westerberg, qemu-devel

On Friday 15 May 2009, Riku Voipio wrote:
> On Fri, May 15, 2009 at 03:25:31AM +0100, Paul Brook wrote:
> > On Tuesday 05 May 2009, Riku Voipio wrote:
> > > On Tue, May 05, 2009 at 10:02:41PM +0400, malc wrote:
> > > > Things should work the way they are in your latest patch on x86 and
> > > > should be done the way it's done in the original submission (mov +
> > > > add) for x86_64.
> > >
> > > Ok.. here we go. not on a amd64 machine right now so I will test
> > > that part tomorrow.
> >
> > I'm pretty sure it's still broken for large offsets.
>
> It is indeed broken. Since tcg_out_addi maps on x86_64 to tgen_arithi64
> which doesn't accept 64bit values. But as Martin said, the currrent use for
> this patch is just to add very minimal offsets.

I that case you should be using lea for small offsets. The proper sequence for 
large offsets is movq (allows 64-bit immedaise); add.

The offset is a user configurable option, so IMHO you should be supporting 
large offsets.

Paul

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15 10:02                   ` Paul Brook
@ 2009-05-15 10:09                     ` Paul Brook
  2009-05-15 12:07                     ` malc
  1 sibling, 0 replies; 29+ messages in thread
From: Paul Brook @ 2009-05-15 10:09 UTC (permalink / raw)
  To: qemu-devel; +Cc: martin.mohring, mika.westerberg, Riku Voipio

> > It is indeed broken. Since tcg_out_addi maps on x86_64 to tgen_arithi64
> > which doesn't accept 64bit values. But as Martin said, the currrent use
> > for this patch is just to add very minimal offsets.
>
> I that case you should be using lea for small offsets. The proper sequence
> for large offsets is movq (allows 64-bit immedaise); add.
>
> The offset is a user configurable option, so IMHO you should be supporting
> large offsets.

I'll also note that mmap_min_addr need not be a small value. It may be set to 
~4G to flush out 32-bit pointer truncation errors.

Paul

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15  9:57                 ` Riku Voipio
  2009-05-15 10:02                   ` Paul Brook
@ 2009-05-15 10:12                   ` Martin Mohring
  2009-05-15 14:13                     ` Riku Voipio
  1 sibling, 1 reply; 29+ messages in thread
From: Martin Mohring @ 2009-05-15 10:12 UTC (permalink / raw)
  To: Riku Voipio; +Cc: martin.mohring, mika.westerberg, Paul Brook, qemu-devel

Riku Voipio wrote:
> On Fri, May 15, 2009 at 03:25:31AM +0100, Paul Brook wrote:
>   
>> On Tuesday 05 May 2009, Riku Voipio wrote:
>>     
>>> On Tue, May 05, 2009 at 10:02:41PM +0400, malc wrote:
>>>       
>>>> Things should work the way they are in your latest patch on x86 and
>>>> should be done the way it's done in the original submission (mov + add)
>>>> for x86_64.
>>>>         
>
>   
>>> Ok.. here we go. not on a amd64 machine right now so I will test
>>> that part tomorrow.
>>>       
>
>   
>> I'm pretty sure it's still broken for large offsets.
>>     
>
> It is indeed broken. Since tcg_out_addi maps on x86_64 to tgen_arithi64 which
> doesn't accept 64bit values. But as Martin said, the currrent use for this
> patch is just to add very minimal offsets.
>
> Martin, I just rebased the linux-user patches, they should apply again
> (there was only a conflict in configure):
>   
Sorted this out already by myself, tnx though.

Since i am using guest base, what does its brokenness mean? Riku, will
you talk with the author to fix it according to Pauls suggestions? Or
will the patch be accepted under the assumptions/constraints it was
written. Or is the request that a generic solution fixes other
longstanding issues?

Martin

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15 10:02                   ` Paul Brook
  2009-05-15 10:09                     ` Paul Brook
@ 2009-05-15 12:07                     ` malc
  1 sibling, 0 replies; 29+ messages in thread
From: malc @ 2009-05-15 12:07 UTC (permalink / raw)
  To: Paul Brook; +Cc: martin.mohring, mika.westerberg, Riku Voipio, qemu-devel

On Fri, 15 May 2009, Paul Brook wrote:

> On Friday 15 May 2009, Riku Voipio wrote:
> > On Fri, May 15, 2009 at 03:25:31AM +0100, Paul Brook wrote:
> > > On Tuesday 05 May 2009, Riku Voipio wrote:
> > > > On Tue, May 05, 2009 at 10:02:41PM +0400, malc wrote:
> > > > > Things should work the way they are in your latest patch on x86 and
> > > > > should be done the way it's done in the original submission (mov +
> > > > > add) for x86_64.
> > > >
> > > > Ok.. here we go. not on a amd64 machine right now so I will test
> > > > that part tomorrow.
> > >
> > > I'm pretty sure it's still broken for large offsets.
> >
> > It is indeed broken. Since tcg_out_addi maps on x86_64 to tgen_arithi64
> > which doesn't accept 64bit values. But as Martin said, the currrent use for
> > this patch is just to add very minimal offsets.
> 
> I that case you should be using lea for small offsets. The proper sequence for 
> large offsets is movq (allows 64-bit immedaise); add.

For x86_64 it might be worthwile to put the offset onto the stack in tcg
prologue and then just `addq r0, OFFSET_OF_BASE_ON_THE_STACK(rsp)'

> 
> The offset is a user configurable option, so IMHO you should be supporting 
> large offsets.
> 
> Paul
> 

-- 
mailto:av1474@comtv.ru

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15 10:12                   ` Martin Mohring
@ 2009-05-15 14:13                     ` Riku Voipio
  2009-05-15 15:25                       ` Martin Mohring
  0 siblings, 1 reply; 29+ messages in thread
From: Riku Voipio @ 2009-05-15 14:13 UTC (permalink / raw)
  To: Martin Mohring; +Cc: Paul Brook, qemu-devel

On Fri, May 15, 2009 at 12:12:50PM +0200, Martin Mohring wrote:
> Riku Voipio wrote:
> > On Fri, May 15, 2009 at 03:25:31AM +0100, Paul Brook wrote:
> >> I'm pretty sure it's still broken for large offsets.

> > It is indeed broken. Since tcg_out_addi maps on x86_64 to tgen_arithi64 which
> > doesn't accept 64bit values. But as Martin said, the currrent use for this
> > patch is just to add very minimal offsets.

> Since i am using guest base, what does its brokenness mean?

Well, you are not using guest_base with large offsets, so it's not broken
for you.

> Riku, will you talk with the author to fix it according to Pauls suggestions? Or
> will the patch be accepted under the assumptions/constraints it was
> written. Or is the request that a generic solution fixes other
> longstanding issues?

Well.. the patch fixes a real-life issue: qemu linux-user not working
at all with recent kernel defaults. With this patch, there is still
the potential issue - someone can set GUEST_BASE too large by hand.
That of course didn't work before this patch either.

I think short term it's best to put a size cap for guest_base offset
and look later into supporting greater offsets. So at least, qemu
linux-user will be usable out of box for most users.

But, with current "Quod licet Iovi, non licet bovi" philosophy it seems
it is not enough for bovine to provide patches that make qemu better,
but perfect patches are required.

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4]
  2009-05-15 14:13                     ` Riku Voipio
@ 2009-05-15 15:25                       ` Martin Mohring
  0 siblings, 0 replies; 29+ messages in thread
From: Martin Mohring @ 2009-05-15 15:25 UTC (permalink / raw)
  To: Paul Brook; +Cc: Riku Voipio, qemu-devel

Riku Voipio wrote:
> On Fri, May 15, 2009 at 12:12:50PM +0200, Martin Mohring wrote:
>   
>> Riku Voipio wrote:
>>     
>>> On Fri, May 15, 2009 at 03:25:31AM +0100, Paul Brook wrote:
>>>       
>>>> I'm pretty sure it's still broken for large offsets.
>>>>         
>
>   
>>> It is indeed broken. Since tcg_out_addi maps on x86_64 to tgen_arithi64 which
>>> doesn't accept 64bit values. But as Martin said, the currrent use for this
>>> patch is just to add very minimal offsets.
>>>       
>
>   
>> Since i am using guest base, what does its brokenness mean?
>>     
>
> Well, you are not using guest_base with large offsets, so it's not broken
> for you.
>
>   
>> Riku, will you talk with the author to fix it according to Pauls suggestions? Or
>> will the patch be accepted under the assumptions/constraints it was
>> written. Or is the request that a generic solution fixes other
>> longstanding issues?
>>     
>
> Well.. the patch fixes a real-life issue: qemu linux-user not working
> at all with recent kernel defaults. With this patch, there is still
> the potential issue - someone can set GUEST_BASE too large by hand.
> That of course didn't work before this patch either.
>
> I think short term it's best to put a size cap for guest_base offset
> and look later into supporting greater offsets. So at least, qemu
> linux-user will be usable out of box for most users.
>
> But, with current "Quod licet Iovi, non licet bovi" philosophy it seems
> it is not enough for bovine to provide patches that make qemu better,
> but perfect patches are required.
>
>
>   
Yes. you are right. The most important fact, that currently for hosts
where /proc/sys/vm/mmap_min_addr is 65536 (which is on all current linux
distros for x86 - Ubuntu 8.10, Debian Lenny, Fedora 9, openSUSE 11.0 ...
or higher - just to name a few) in this sense *QEMU user mode is in fact
broken*.

But even worse is the fact that this issue is on the table since many
months. Todays 5th rebase since our submission reminded me hard toward
this fact. Please Paul release us from this torture.

Martin

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2009-05-15 15:25 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-04-29 18:03 [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat riku.voipio
2009-04-29 18:03 ` [Qemu-devel] [PATCH 02/10] Implement shm* syscalls and fix 64/32bit errors riku.voipio
2009-04-29 18:03 ` [Qemu-devel] [PATCH 03/10] linux-user: implemented ELF coredump support for ARM target [v2] riku.voipio
2009-04-29 18:03 ` [Qemu-devel] [PATCH 04/10] linux-user: added x86 and x86_64 support for ELF coredump riku.voipio
2009-04-29 18:03 ` [Qemu-devel] [PATCH 05/10] linux-user: strace now handles guest strings correctly riku.voipio
2009-04-29 18:03 ` [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2] riku.voipio
2009-04-29 19:50   ` malc
2009-05-05 13:27     ` [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v3] Riku Voipio
2009-05-05 13:53       ` Paul Brook
2009-05-05 14:18         ` Riku Voipio
2009-05-05 14:34           ` Paul Brook
2009-05-05 18:02           ` malc
2009-05-05 20:46             ` [Qemu-devel] [PATCH] Revived GUEST_BASE support for usermode emulation targets [v4] Riku Voipio
2009-05-15  2:25               ` Paul Brook
2009-05-15  8:41                 ` Martin Mohring
2009-05-15  9:50                   ` Paul Brook
2009-05-15  9:57                 ` Riku Voipio
2009-05-15 10:02                   ` Paul Brook
2009-05-15 10:09                     ` Paul Brook
2009-05-15 12:07                     ` malc
2009-05-15 10:12                   ` Martin Mohring
2009-05-15 14:13                     ` Riku Voipio
2009-05-15 15:25                       ` Martin Mohring
2009-04-30  7:07   ` [Qemu-devel] [PATCH 06/10] Revived GUEST_BASE support for usermode emulation targets [v2] Martin Mohring
2009-04-29 18:03 ` [Qemu-devel] [PATCH 07/10] linux-user: fix utimensat when used as futimens riku.voipio
2009-04-29 18:03 ` [Qemu-devel] [PATCH 08/10] Fix struct termios host - target translation riku.voipio
2009-04-29 18:03 ` [Qemu-devel] [PATCH 09/10] linux-user: fix utimensat with NULL timespec riku.voipio
2009-04-29 18:03 ` [Qemu-devel] [PATCH 10/10] Return EOPNOTSUPP instead of ENOSYS for *xattr* syscalls riku.voipio
2009-04-30  7:09 ` [Qemu-devel] [PATCH 01/10] export mmap_find_vma for shmat Martin Mohring

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).