qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Cam Macdonell <cam@cs.ualberta.ca>
To: qemu-devel@nongnu.org
Cc: Cam Macdonell <cam@cs.ualberta.ca>, kvm@vger.kernel.org
Subject: [Qemu-devel] [PATCH] Support adding a file to qemu's ram allocation
Date: Fri,  5 Mar 2010 16:52:40 -0700	[thread overview]
Message-ID: <1267833161-25267-1-git-send-email-cam@cs.ualberta.ca> (raw)

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_add_file_to_ram(fd, size, MAP_SHARED);
---
 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 326513d..2d95079 100644
--- a/cpu-common.h
+++ b/cpu-common.h
@@ -30,6 +30,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_add_file_to_ram(int, ram_addr_t, 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 69003c2..955adee 100644
--- a/exec.c
+++ b/exec.c
@@ -2623,6 +2623,39 @@ static void *file_ram_alloc(ram_addr_t memory, const char *path)
 
 extern const char *mem_path;
 
+ram_addr_t qemu_add_file_to_ram(int fd, ram_addr_t size, int flags)
+{
+    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, 0);
+
+#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;
-- 
1.6.0.6

             reply	other threads:[~2010-03-05 23:52 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-05 23:52 Cam Macdonell [this message]
2010-03-05 23:52 ` [Qemu-devel] [PATCH] Inter-VM shared memory PCI device Cam Macdonell
2010-03-07 22:53   ` Paul Brook
2010-03-08  1:45     ` Jamie Lokier
2010-03-08  9:48       ` Alexander Graf
2010-03-08  9:54         ` Jamie Lokier
2010-03-08 10:57           ` Alexander Graf
2010-03-09 21:41           ` Anthony Liguori
2010-03-08 13:04       ` Paul Brook
2010-03-09 19:00         ` Jamie Lokier
2010-03-08  9:52     ` Avi Kivity
2010-03-08 13:03       ` Paul Brook
2010-03-08 13:16         ` Avi Kivity
2010-03-09 20:11           ` Jamie Lokier
2010-03-09 21:44           ` Anthony Liguori
2010-03-10  9:25             ` Avi Kivity
2010-03-10 17:13               ` Anthony Liguori
2010-03-10 17:30                 ` Avi Kivity
2010-03-10 17:41                   ` Paul Brook
2010-03-11  6:33                     ` Avi Kivity
2010-03-11 12:21                       ` Paul Brook
2010-03-09 20:12         ` Jamie Lokier
2010-03-10  0:03           ` Paul Brook
2010-03-10  4:38             ` Cam Macdonell
2010-03-10  9:29               ` Avi Kivity
2010-03-10 11:13                 ` Paul Brook
2010-03-11  3:10             ` Jamie Lokier
2010-03-11  4:37               ` Nick Piggin
2010-03-11 14:38                 ` malc
2010-03-08  9:56   ` [Qemu-devel] " Avi Kivity
2010-03-08 17:57     ` Cam Macdonell
2010-03-09 10:29       ` Avi Kivity
     [not found]         ` <8286e4ee1003090724m1ef0b571g8b705a24e36e1753@mail.gmail.com>
2010-03-09 15:27           ` [Qemu-devel] " Cam Macdonell
2010-03-09 17:28             ` [Qemu-devel] " Avi Kivity
2010-03-09 17:34               ` Anthony Liguori
2010-03-09 18:34               ` Cam Macdonell
2010-03-10  9:21                 ` Avi Kivity
2010-03-10 16:36                   ` Cam Macdonell
2010-03-11  6:49                     ` Avi Kivity
2010-03-09 12:49       ` Arnd Bergmann
2010-03-09 13:03         ` Avi Kivity
2010-03-09 16:44           ` Cam Macdonell
2010-03-10 14:04             ` Arnd Bergmann
2010-03-11  6:50               ` Avi Kivity
2010-03-11 12:57                 ` Arnd Bergmann
2010-03-11 13:07                   ` Avi Kivity
2010-03-11 14:32                     ` Arnd Bergmann
2010-03-08  9:53 ` [Qemu-devel] Re: [PATCH] Support adding a file to qemu's ram allocation Avi Kivity

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=1267833161-25267-1-git-send-email-cam@cs.ualberta.ca \
    --to=cam@cs.ualberta.ca \
    --cc=kvm@vger.kernel.org \
    --cc=qemu-devel@nongnu.org \
    /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).