qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Richard Henderson <rth@twiddle.net>
To: qemu-devel@nongnu.org
Cc: riku.voipio@iki.fi
Subject: [Qemu-devel] [PATCH 06/10] linux-user: Allocate the right amount of space for non-fixed file maps
Date: Wed, 25 Jul 2012 15:10:34 -0700	[thread overview]
Message-ID: <1343254238-4727-7-git-send-email-rth@twiddle.net> (raw)
In-Reply-To: <1343254238-4727-1-git-send-email-rth@twiddle.net>

If we let the kernel handle the implementation of mmap_find_vma,
via an anon mmap, we must use the size as indicated by the user
and not the size truncated to the filesize.

This happens often in ld.so, where we initially mmap the file to
the size of the text+data+bss to reserve an area, then mmap+fixed
over the top to properly handle data and bss.

Signed-off-by: Richard Henderson <rth@twiddle.net>
---
 linux-user/mmap.c |   30 +++++++++++++++++++-----------
 1 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/linux-user/mmap.c b/linux-user/mmap.c
index d9468fe..b412e3f 100644
--- a/linux-user/mmap.c
+++ b/linux-user/mmap.c
@@ -382,7 +382,6 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
                      int flags, int fd, abi_ulong offset)
 {
     abi_ulong ret, end, real_start, real_end, retaddr, host_offset, host_len;
-    unsigned long host_start;
 
     mmap_lock();
 #ifdef DEBUG_MMAP
@@ -421,6 +420,19 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     if (len == 0)
         goto the_end;
     real_start = start & qemu_host_page_mask;
+    host_offset = offset & qemu_host_page_mask;
+
+    /* If the user is asking for the kernel to find a location, do that
+       before we truncate the length for mapping files below.  */
+    if (!(flags & MAP_FIXED)) {
+        host_len = len + offset - host_offset;
+        host_len = HOST_PAGE_ALIGN(host_len);
+        start = mmap_find_vma(real_start, host_len);
+        if (start == (abi_ulong)-1) {
+            errno = ENOMEM;
+            goto fail;
+        }
+    }
 
     /* When mapping files into a memory area larger than the file, accesses
        to pages beyond the file size will cause a SIGBUS. 
@@ -453,27 +465,23 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot,
     }
 
     if (!(flags & MAP_FIXED)) {
-        abi_ulong mmap_start;
+        unsigned long host_start;
         void *p;
-        host_offset = offset & qemu_host_page_mask;
+
         host_len = len + offset - host_offset;
         host_len = HOST_PAGE_ALIGN(host_len);
-        mmap_start = mmap_find_vma(real_start, host_len);
-        if (mmap_start == (abi_ulong)-1) {
-            errno = ENOMEM;
-            goto fail;
-        }
+
         /* Note: we prefer to control the mapping address. It is
            especially important if qemu_host_page_size >
            qemu_real_host_page_size */
-        p = mmap(g2h(mmap_start),
-                 host_len, prot, flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
+        p = mmap(g2h(start), host_len, prot,
+                 flags | MAP_FIXED | MAP_ANONYMOUS, -1, 0);
         if (p == MAP_FAILED)
             goto fail;
         /* update start so that it points to the file position at 'offset' */
         host_start = (unsigned long)p;
         if (!(flags & MAP_ANONYMOUS)) {
-            p = mmap(g2h(mmap_start), len, prot, 
+            p = mmap(g2h(start), len, prot,
                      flags | MAP_FIXED, fd, host_offset);
             host_start += offset - host_offset;
         }
-- 
1.7.7.6

  parent reply	other threads:[~2012-07-25 22:10 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-25 22:10 [Qemu-devel] [PATCH v4 00/10] {alpha-}linux user improvements Richard Henderson
2012-07-25 22:10 ` [Qemu-devel] [PATCH 01/10] alpha-linux-user: Fix signal handling Richard Henderson
2012-08-02 14:07   ` Peter Maydell
2012-07-25 22:10 ` [Qemu-devel] [PATCH 02/10] alpha-linux-user: Work around hosted mmap allocation problems Richard Henderson
2012-07-25 22:10 ` [Qemu-devel] [PATCH 03/10] alpha-linux-user: Handle TARGET_SSI_IEEE_RAISE_EXCEPTION properly Richard Henderson
2012-08-02 14:11   ` Peter Maydell
2012-07-25 22:10 ` [Qemu-devel] [PATCH 04/10] linux-user: Sync fcntl.h bits with the kernel Richard Henderson
2012-08-02 14:34   ` Peter Maydell
2012-08-02 15:17     ` Richard Henderson
2012-07-25 22:10 ` [Qemu-devel] [PATCH 05/10] linux-user: Handle O_SYNC, O_NOATIME, O_CLOEXEC, O_PATH Richard Henderson
2012-08-02 14:38   ` Peter Maydell
2012-07-25 22:10 ` Richard Henderson [this message]
2012-07-25 22:10 ` [Qemu-devel] [PATCH 07/10] linux-user: Translate pipe2 flags; add to strace Richard Henderson
2012-08-02 14:40   ` Peter Maydell
2012-07-25 22:10 ` [Qemu-devel] [PATCH 08/10] alpha-linux-user: Fix a3 error return with v0 error bypass Richard Henderson
2012-07-25 22:10 ` [Qemu-devel] [PATCH 09/10] alpha-linux-user: Properly handle the non-rt sigprocmask syscall Richard Henderson
2012-08-02 14:41   ` Peter Maydell
2012-07-25 22:10 ` [Qemu-devel] [PATCH 10/10] alpha-linux-user: Fix the getpriority syscall Richard Henderson
2012-08-02 14:48   ` Peter Maydell
2012-08-02 15:23     ` Richard Henderson
2012-08-01 23:24 ` [Qemu-devel] [PATCH v4 00/10] {alpha-}linux user improvements Richard Henderson
  -- strict thread matches above, loose matches on Subject: below --
2012-08-03 22:40 [Qemu-devel] [PATCH v5 " Richard Henderson
2012-08-03 22:40 ` [Qemu-devel] [PATCH 06/10] linux-user: Allocate the right amount of space for non-fixed file maps Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1343254238-4727-7-git-send-email-rth@twiddle.net \
    --to=rth@twiddle.net \
    --cc=qemu-devel@nongnu.org \
    --cc=riku.voipio@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).