From patchwork Wed Apr 21 17:53:26 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cam Macdonell X-Patchwork-Id: 93906 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o3LHraaK009349 for ; Wed, 21 Apr 2010 17:53:36 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756065Ab0DURxd (ORCPT ); Wed, 21 Apr 2010 13:53:33 -0400 Received: from fleet.cs.ualberta.ca ([129.128.22.22]:58893 "EHLO fleet.cs.ualberta.ca" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756045Ab0DURxc (ORCPT ); Wed, 21 Apr 2010 13:53:32 -0400 Received: from localhost.localdomain (st-brides.cs.ualberta.ca [129.128.23.21]) (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 2296828023; Wed, 21 Apr 2010 11:53:32 -0600 (MDT) From: Cam Macdonell To: kvm@vger.kernel.org Cc: qemu-devel@nongnu.org, Cam Macdonell Subject: [PATCH v5 2/5] Support adding a file to qemu's ram allocation Date: Wed, 21 Apr 2010 11:53:26 -0600 Message-Id: <1271872408-22842-3-git-send-email-cam@cs.ualberta.ca> X-Mailer: git-send-email 1.6.2.5 In-Reply-To: <1271872408-22842-2-git-send-email-cam@cs.ualberta.ca> References: <1271872408-22842-1-git-send-email-cam@cs.ualberta.ca> <1271872408-22842-2-git-send-email-cam@cs.ualberta.ca> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Wed, 21 Apr 2010 17:53:38 +0000 (UTC) diff --git a/cpu-common.h b/cpu-common.h index 49c7fb3..d7c7d3a 100644 --- a/cpu-common.h +++ b/cpu-common.h @@ -9,6 +9,7 @@ #include "bswap.h" #include "qemu-queue.h" +#include #if !defined(CONFIG_USER_ONLY) @@ -32,6 +33,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 fd, ram_addr_t addr, int flags, off_t offset); 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 467a0e7..702348e 100644 --- a/exec.c +++ b/exec.c @@ -2811,6 +2811,42 @@ 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, off_t 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); + + if (new_block->host == MAP_FAILED) + exit(1); + +#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;