From patchwork Fri Aug 3 22:40:53 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 175057 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 588792C0080 for ; Sat, 4 Aug 2012 08:42:09 +1000 (EST) Received: from localhost ([::1]:45186 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SxQZL-0001Tv-EI for incoming@patchwork.ozlabs.org; Fri, 03 Aug 2012 18:42:07 -0400 Received: from eggs.gnu.org ([208.118.235.92]:39122) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SxQYT-0007uD-VY for qemu-devel@nongnu.org; Fri, 03 Aug 2012 18:41:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SxQYS-00041A-JL for qemu-devel@nongnu.org; Fri, 03 Aug 2012 18:41:13 -0400 Received: from mail-pb0-f45.google.com ([209.85.160.45]:58948) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SxQYS-0003zU-Dr for qemu-devel@nongnu.org; Fri, 03 Aug 2012 18:41:12 -0400 Received: by mail-pb0-f45.google.com with SMTP id ro12so1909554pbb.4 for ; Fri, 03 Aug 2012 15:41:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; bh=1brU2RWSZY/nnob3Hl1Ue3LooEatPmYFkeHWE8/dJcY=; b=UvfIZdPBVjuj3u9JzS0UCU6yN/tZuoAzjQTLhMrLS3LIzsJuobqMDjf/W3vFC0XMLp 3h0omGTd2Sfdb75YJs7O1AL8hEfhovxR6RBJR6SpECuI5OqJmdpFKAVBelI3W3DU2zJM M+ItaF289gQijqAUiRI/uY28GBDZq4p0eESsniZeIgm+V6DsNjCpt35bRlkfZvSxSMNC CyOVUNGWqf2YL6b7OsqxOAX+uEe84+1ukvknHu2qA7Dm1zUncmg7uFZHrpZpOse9WO+x 4WbSmBBQPs/VhKXNb1Z/t1J07TrigWdg3/+9HuGRtwyFMJJahRMUEgPESk3sqg45YgXN L+8w== Received: by 10.66.85.201 with SMTP id j9mr1746542paz.40.1344033672035; Fri, 03 Aug 2012 15:41:12 -0700 (PDT) Received: from anchor.twiddle.home ([173.160.232.49]) by mx.google.com with ESMTPS id qd2sm3721626pbb.29.2012.08.03.15.41.11 (version=TLSv1/SSLv3 cipher=OTHER); Fri, 03 Aug 2012 15:41:11 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 3 Aug 2012 15:40:53 -0700 Message-Id: <1344033657-9135-7-git-send-email-rth@twiddle.net> X-Mailer: git-send-email 1.7.7.6 In-Reply-To: <1344033657-9135-1-git-send-email-rth@twiddle.net> References: <1344033657-9135-1-git-send-email-rth@twiddle.net> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.160.45 Subject: [Qemu-devel] [PATCH 06/10] linux-user: Allocate the right amount of space for non-fixed file maps X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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; }