From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KUIGK-0006cZ-1t for qemu-devel@nongnu.org; Sat, 16 Aug 2008 05:39:56 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KUIGH-0006a2-Uz for qemu-devel@nongnu.org; Sat, 16 Aug 2008 05:39:54 -0400 Received: from [199.232.76.173] (port=39145 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KUIGH-0006Za-C0 for qemu-devel@nongnu.org; Sat, 16 Aug 2008 05:39:53 -0400 Received: from fmmailgate03.web.de ([217.72.192.234]:58032) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KUIGG-0001CA-Nz for qemu-devel@nongnu.org; Sat, 16 Aug 2008 05:39:53 -0400 Received: from smtp08.web.de (fmsmtp08.dlan.cinetic.de [172.20.5.216]) by fmmailgate03.web.de (Postfix) with ESMTP id BAFD7E6D5798 for ; Sat, 16 Aug 2008 11:39:51 +0200 (CEST) Received: from [88.64.23.108] (helo=[192.168.1.198]) by smtp08.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.109 #226) id 1KUIGF-0006wd-00 for qemu-devel@nongnu.org; Sat, 16 Aug 2008 11:39:51 +0200 Resent-To: qemu-devel@nongnu.org Resent-Message-Id: <48A6A067.8020101@web.de> Message-ID: <48A69DD7.7020104@web.de> Date: Sat, 16 Aug 2008 11:28:55 +0200 From: Jan Kiszka MIME-Version: 1.0 References: <48A69B64.7050001@web.de> In-Reply-To: <48A69B64.7050001@web.de> Content-Type: text/plain; charset=ISO-8859-15 Content-Transfer-Encoding: 7bit Sender: jan.kiszka@web.de Subject: [Qemu-devel] [PATCH 2/5] linux-user: Introduce qemu_vmalloc_guest_safe Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Add qemu_vmalloc_guest_safe to allocate memory from a region that are reachable by user-space guests. Addresses returned by this function can safely be provided to h2g. Signed-off-by: Jan Kiszka --- linux-user/mmap.c | 39 +++++++++++++++++++++++++++++++++++++++ linux-user/qemu.h | 1 + 2 files changed, 40 insertions(+) Index: b/linux-user/mmap.c =================================================================== --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -94,6 +94,45 @@ void *qemu_vmalloc(size_t size) return p; } +static abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size); + +void *qemu_vmalloc_guest_safe(size_t size) +{ +#if TARGET_LONG_BIT < HOST_LONG_BITS + void *p; + unsigned long addr; + abi_ulong guest_start; + + mmap_lock(); + + /* Ensure we allocate from the guest-reachable rage */ + size = HOST_PAGE_ALIGN(size); + guest_start = mmap_find_vma(0, size); + if (guest_start == (abi_ulong)-1) { + p = NULL; + goto unlock_exit; + } + + /* Use map and mark the pages as used. */ + p = mmap(g2h(guest_start), size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (p == MAP_FAILED) { + p = NULL; + goto unlock_exit; + } + + addr = (unsigned long)p; + page_set_flags(addr & TARGET_PAGE_MASK, TARGET_PAGE_ALIGN(addr + size), + PAGE_RESERVED); + +unlock_exit: + mmap_unlock(); + return p; +#else + return qemu_vmalloc(size); +#endif +} + void *qemu_malloc(size_t size) { char * p; Index: b/linux-user/qemu.h =================================================================== --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -247,6 +247,7 @@ void mmap_unlock(void); void mmap_fork_start(void); void mmap_fork_end(int child); #endif +void *qemu_vmalloc_guest_safe(size_t size); /* user access */