From patchwork Thu Mar 25 06:08:29 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cam Macdonell X-Patchwork-Id: 48496 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9C58EB7C98 for ; Thu, 25 Mar 2010 17:13:16 +1100 (EST) Received: from localhost ([127.0.0.1]:45302 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NugJc-0008BG-67 for incoming@patchwork.ozlabs.org; Thu, 25 Mar 2010 02:13:12 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NugIk-0008B9-Bf for qemu-devel@nongnu.org; Thu, 25 Mar 2010 02:12:18 -0400 Received: from [140.186.70.92] (port=47768 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NugIh-0008B0-J3 for qemu-devel@nongnu.org; Thu, 25 Mar 2010 02:12:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NugIf-0003nD-IC for qemu-devel@nongnu.org; Thu, 25 Mar 2010 02:12:14 -0400 Received: from fleet.cs.ualberta.ca ([129.128.22.22]:54442) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NugIf-0003lX-As for qemu-devel@nongnu.org; Thu, 25 Mar 2010 02:12:13 -0400 Received: from localhost.localdomain (botha10.cs.ualberta.ca [129.128.22.140]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp-auth.cs.ualberta.ca (Postfix) with ESMTP id 420F32801E; Thu, 25 Mar 2010 00:12:01 -0600 (MDT) From: Cam Macdonell To: kvm@vger.kernel.org Date: Thu, 25 Mar 2010 00:08:29 -0600 Message-Id: <1269497310-21858-2-git-send-email-cam@cs.ualberta.ca> X-Mailer: git-send-email 1.6.6.1 In-Reply-To: <1269497310-21858-1-git-send-email-cam@cs.ualberta.ca> References: <1269497310-21858-1-git-send-email-cam@cs.ualberta.ca> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: Cam Macdonell , qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH v3 1/2] Support adding a file to qemu's ram allocation X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This avoids the need of using qemu_ram_alloc and mmap with MAP_FIXED to map a host file into guest RAM. This function mmaps the opened file anywhere and adds the memory to the ram blocks. Usage is qemu_ram_mmap(fd, size, MAP_SHARED, offset); --- cpu-common.h | 1 + exec.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 0 deletions(-) diff --git a/cpu-common.h b/cpu-common.h index 6cae15b..dffe4e7 100644 --- a/cpu-common.h +++ b/cpu-common.h @@ -32,6 +32,7 @@ static inline void cpu_register_physical_memory(target_phys_addr_t start_addr, } ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr); +ram_addr_t qemu_ram_mmap(int, ram_addr_t, int, int); ram_addr_t qemu_ram_alloc(ram_addr_t); void qemu_ram_free(ram_addr_t addr); /* This should only be used for ram local to a device. */ diff --git a/exec.c b/exec.c index 3b4426e..6f4e747 100644 --- a/exec.c +++ b/exec.c @@ -2727,6 +2727,39 @@ static void *file_ram_alloc(ram_addr_t memory, const char *path) } #endif +ram_addr_t qemu_ram_mmap(int fd, ram_addr_t size, int flags, int offset) +{ + RAMBlock *new_block; + + size = TARGET_PAGE_ALIGN(size); + new_block = qemu_malloc(sizeof(*new_block)); + + // map the file passed as a parameter to be this part of memory + new_block->host = mmap(0, size, PROT_READ|PROT_WRITE, flags, fd, offset); + +#ifdef MADV_MERGEABLE + madvise(new_block->host, size, MADV_MERGEABLE); +#endif + + new_block->offset = last_ram_offset; + new_block->length = size; + + new_block->next = ram_blocks; + ram_blocks = new_block; + + phys_ram_dirty = qemu_realloc(phys_ram_dirty, + (last_ram_offset + size) >> TARGET_PAGE_BITS); + memset(phys_ram_dirty + (last_ram_offset >> TARGET_PAGE_BITS), + 0xff, size >> TARGET_PAGE_BITS); + + last_ram_offset += size; + + if (kvm_enabled()) + kvm_setup_guest_memory(new_block->host, size); + + return new_block->offset; +} + ram_addr_t qemu_ram_alloc(ram_addr_t size) { RAMBlock *new_block;