From: Alex Williamson <alex.williamson@redhat.com>
To: qemu-devel@nongnu.org, anthony@codemonkey.ws, blauwirbel@gmail.com
Cc: kvm@vger.kernel.org, alex.williamson@redhat.com
Subject: [PATCH v4 1/2] Minimal RAM API support
Date: Mon, 13 Dec 2010 14:24:30 -0700 [thread overview]
Message-ID: <20101213212430.2472.23807.stgit@s20.home> (raw)
In-Reply-To: <20101213212059.2472.17879.stgit@s20.home>
This adds a minimum chunk of Anthony's RAM API support so that we
can identify actual VM RAM versus all the other things that make
use of qemu_ram_alloc.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
Makefile.objs | 1 +
cpu-common.h | 2 +
memory.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
memory.h | 44 +++++++++++++++++++++++++++
4 files changed, 141 insertions(+), 0 deletions(-)
create mode 100644 memory.c
create mode 100644 memory.h
diff --git a/Makefile.objs b/Makefile.objs
index cebb945..47f3c3a 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -172,6 +172,7 @@ hw-obj-y += pci.o pci_bridge.o msix.o msi.o
hw-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o
hw-obj-$(CONFIG_PCI) += ioh3420.o xio3130_upstream.o xio3130_downstream.o
hw-obj-y += watchdog.o
+hw-obj-y += memory.o
hw-obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
hw-obj-$(CONFIG_ECC) += ecc.o
hw-obj-$(CONFIG_NAND) += nand.o
diff --git a/cpu-common.h b/cpu-common.h
index 6d4a898..f08f93b 100644
--- a/cpu-common.h
+++ b/cpu-common.h
@@ -29,6 +29,8 @@ enum device_endian {
/* address in the RAM (different from a physical address) */
typedef unsigned long ram_addr_t;
+#include "memory.h"
+
/* memory API */
typedef void CPUWriteMemoryFunc(void *opaque, target_phys_addr_t addr, uint32_t value);
diff --git a/memory.c b/memory.c
new file mode 100644
index 0000000..07cb020
--- /dev/null
+++ b/memory.c
@@ -0,0 +1,94 @@
+/*
+ * RAM API
+ *
+ * Copyright Red Hat, Inc. 2010
+ *
+ * Authors:
+ * Alex Williamson <alex.williamson@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+#include "memory.h"
+#include "range.h"
+
+typedef struct RamSlot {
+ target_phys_addr_t start_addr;
+ ram_addr_t size;
+ ram_addr_t offset;
+ QLIST_ENTRY(RamSlot) next;
+} RamSlot;
+
+static QLIST_HEAD(ram_slot_list, RamSlot) ram_slot_list =
+ QLIST_HEAD_INITIALIZER(ram_slot_list);
+
+static RamSlot *ram_find_slot(target_phys_addr_t start_addr, ram_addr_t size)
+{
+ RamSlot *slot;
+
+ QLIST_FOREACH(slot, &ram_slot_list, next) {
+ if (slot->start_addr == start_addr && slot->size == size) {
+ return slot;
+ }
+
+ if (ranges_overlap(start_addr, size, slot->start_addr, slot->size)) {
+ hw_error("Ram range overlaps existing slot\n");
+ }
+ }
+
+ return NULL;
+}
+
+int ram_register(target_phys_addr_t start_addr, ram_addr_t size,
+ ram_addr_t phys_offset)
+{
+ RamSlot *slot;
+
+ if (!size) {
+ return -EINVAL;
+ }
+
+ assert(!ram_find_slot(start_addr, size));
+
+ slot = qemu_malloc(sizeof(RamSlot));
+
+ slot->start_addr = start_addr;
+ slot->size = size;
+ slot->offset = phys_offset;
+
+ QLIST_INSERT_HEAD(&ram_slot_list, slot, next);
+
+ cpu_register_physical_memory(slot->start_addr, slot->size, slot->offset);
+
+ return 0;
+}
+
+void ram_unregister(target_phys_addr_t start_addr, ram_addr_t size)
+{
+ RamSlot *slot;
+
+ if (!size) {
+ return;
+ }
+
+ slot = ram_find_slot(start_addr, size);
+ assert(slot != NULL);
+
+ QLIST_REMOVE(slot, next);
+ qemu_free(slot);
+ cpu_register_physical_memory(start_addr, size, IO_MEM_UNASSIGNED);
+}
+
+int ram_for_each_slot(void *opaque, ram_for_each_slot_fn fn)
+{
+ RamSlot *slot;
+
+ QLIST_FOREACH(slot, &ram_slot_list, next) {
+ int ret = fn(opaque, slot->start_addr, slot->size, slot->offset);
+ if (ret) {
+ return ret;
+ }
+ }
+ return 0;
+}
diff --git a/memory.h b/memory.h
new file mode 100644
index 0000000..98c85ea
--- /dev/null
+++ b/memory.h
@@ -0,0 +1,44 @@
+#ifndef QEMU_MEMORY_H
+#define QEMU_MEMORY_H
+/*
+ * RAM API
+ *
+ * Copyright Red Hat, Inc. 2010
+ *
+ * Authors:
+ * Alex Williamson <alex.williamson@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "cpu-common.h"
+
+typedef int (*ram_for_each_slot_fn)(void *opaque,
+ target_phys_addr_t start_addr,
+ ram_addr_t size,
+ ram_addr_t phys_offset);
+
+/**
+ * ram_register() : Register a region of guest physical memory
+ *
+ * The new region must not overlap an existing region.
+ */
+int ram_register(target_phys_addr_t start_addr, ram_addr_t size,
+ ram_addr_t phys_offset);
+
+/**
+ * ram_unregister() : Unregister a region of guest physical memory
+ */
+void ram_unregister(target_phys_addr_t start_addr, ram_addr_t size);
+
+/**
+ * ram_for_each_slot() : Call fn() on each registered region
+ *
+ * Stop on non-zero return from fn().
+ */
+int ram_for_each_slot(void *opaque, ram_for_each_slot_fn fn);
+
+#endif /* QEMU_MEMORY_H */
WARNING: multiple messages have this Message-ID (diff)
From: Alex Williamson <alex.williamson@redhat.com>
To: qemu-devel@nongnu.org, anthony@codemonkey.ws, blauwirbel@gmail.com
Cc: alex.williamson@redhat.com, kvm@vger.kernel.org
Subject: [Qemu-devel] [PATCH v4 1/2] Minimal RAM API support
Date: Mon, 13 Dec 2010 14:24:30 -0700 [thread overview]
Message-ID: <20101213212430.2472.23807.stgit@s20.home> (raw)
In-Reply-To: <20101213212059.2472.17879.stgit@s20.home>
This adds a minimum chunk of Anthony's RAM API support so that we
can identify actual VM RAM versus all the other things that make
use of qemu_ram_alloc.
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
Makefile.objs | 1 +
cpu-common.h | 2 +
memory.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
memory.h | 44 +++++++++++++++++++++++++++
4 files changed, 141 insertions(+), 0 deletions(-)
create mode 100644 memory.c
create mode 100644 memory.h
diff --git a/Makefile.objs b/Makefile.objs
index cebb945..47f3c3a 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -172,6 +172,7 @@ hw-obj-y += pci.o pci_bridge.o msix.o msi.o
hw-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o
hw-obj-$(CONFIG_PCI) += ioh3420.o xio3130_upstream.o xio3130_downstream.o
hw-obj-y += watchdog.o
+hw-obj-y += memory.o
hw-obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
hw-obj-$(CONFIG_ECC) += ecc.o
hw-obj-$(CONFIG_NAND) += nand.o
diff --git a/cpu-common.h b/cpu-common.h
index 6d4a898..f08f93b 100644
--- a/cpu-common.h
+++ b/cpu-common.h
@@ -29,6 +29,8 @@ enum device_endian {
/* address in the RAM (different from a physical address) */
typedef unsigned long ram_addr_t;
+#include "memory.h"
+
/* memory API */
typedef void CPUWriteMemoryFunc(void *opaque, target_phys_addr_t addr, uint32_t value);
diff --git a/memory.c b/memory.c
new file mode 100644
index 0000000..07cb020
--- /dev/null
+++ b/memory.c
@@ -0,0 +1,94 @@
+/*
+ * RAM API
+ *
+ * Copyright Red Hat, Inc. 2010
+ *
+ * Authors:
+ * Alex Williamson <alex.williamson@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+#include "memory.h"
+#include "range.h"
+
+typedef struct RamSlot {
+ target_phys_addr_t start_addr;
+ ram_addr_t size;
+ ram_addr_t offset;
+ QLIST_ENTRY(RamSlot) next;
+} RamSlot;
+
+static QLIST_HEAD(ram_slot_list, RamSlot) ram_slot_list =
+ QLIST_HEAD_INITIALIZER(ram_slot_list);
+
+static RamSlot *ram_find_slot(target_phys_addr_t start_addr, ram_addr_t size)
+{
+ RamSlot *slot;
+
+ QLIST_FOREACH(slot, &ram_slot_list, next) {
+ if (slot->start_addr == start_addr && slot->size == size) {
+ return slot;
+ }
+
+ if (ranges_overlap(start_addr, size, slot->start_addr, slot->size)) {
+ hw_error("Ram range overlaps existing slot\n");
+ }
+ }
+
+ return NULL;
+}
+
+int ram_register(target_phys_addr_t start_addr, ram_addr_t size,
+ ram_addr_t phys_offset)
+{
+ RamSlot *slot;
+
+ if (!size) {
+ return -EINVAL;
+ }
+
+ assert(!ram_find_slot(start_addr, size));
+
+ slot = qemu_malloc(sizeof(RamSlot));
+
+ slot->start_addr = start_addr;
+ slot->size = size;
+ slot->offset = phys_offset;
+
+ QLIST_INSERT_HEAD(&ram_slot_list, slot, next);
+
+ cpu_register_physical_memory(slot->start_addr, slot->size, slot->offset);
+
+ return 0;
+}
+
+void ram_unregister(target_phys_addr_t start_addr, ram_addr_t size)
+{
+ RamSlot *slot;
+
+ if (!size) {
+ return;
+ }
+
+ slot = ram_find_slot(start_addr, size);
+ assert(slot != NULL);
+
+ QLIST_REMOVE(slot, next);
+ qemu_free(slot);
+ cpu_register_physical_memory(start_addr, size, IO_MEM_UNASSIGNED);
+}
+
+int ram_for_each_slot(void *opaque, ram_for_each_slot_fn fn)
+{
+ RamSlot *slot;
+
+ QLIST_FOREACH(slot, &ram_slot_list, next) {
+ int ret = fn(opaque, slot->start_addr, slot->size, slot->offset);
+ if (ret) {
+ return ret;
+ }
+ }
+ return 0;
+}
diff --git a/memory.h b/memory.h
new file mode 100644
index 0000000..98c85ea
--- /dev/null
+++ b/memory.h
@@ -0,0 +1,44 @@
+#ifndef QEMU_MEMORY_H
+#define QEMU_MEMORY_H
+/*
+ * RAM API
+ *
+ * Copyright Red Hat, Inc. 2010
+ *
+ * Authors:
+ * Alex Williamson <alex.williamson@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "cpu-common.h"
+
+typedef int (*ram_for_each_slot_fn)(void *opaque,
+ target_phys_addr_t start_addr,
+ ram_addr_t size,
+ ram_addr_t phys_offset);
+
+/**
+ * ram_register() : Register a region of guest physical memory
+ *
+ * The new region must not overlap an existing region.
+ */
+int ram_register(target_phys_addr_t start_addr, ram_addr_t size,
+ ram_addr_t phys_offset);
+
+/**
+ * ram_unregister() : Unregister a region of guest physical memory
+ */
+void ram_unregister(target_phys_addr_t start_addr, ram_addr_t size);
+
+/**
+ * ram_for_each_slot() : Call fn() on each registered region
+ *
+ * Stop on non-zero return from fn().
+ */
+int ram_for_each_slot(void *opaque, ram_for_each_slot_fn fn);
+
+#endif /* QEMU_MEMORY_H */
next prev parent reply other threads:[~2010-12-13 21:24 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-12-13 21:24 [PATCH v4 0/2] Minimal RAM API support Alex Williamson
2010-12-13 21:24 ` [Qemu-devel] " Alex Williamson
2010-12-13 21:24 ` Alex Williamson [this message]
2010-12-13 21:24 ` [Qemu-devel] [PATCH v4 1/2] " Alex Williamson
2010-12-15 17:23 ` Paul Brook
2010-12-15 17:23 ` Paul Brook
2010-12-15 19:11 ` Alex Williamson
2010-12-15 19:11 ` Alex Williamson
2010-12-15 19:34 ` Anthony Liguori
2010-12-15 19:34 ` Anthony Liguori
2010-12-13 21:24 ` [PATCH v4 2/2] RAM API: Make use of it for x86 PC Alex Williamson
2010-12-13 21:24 ` [Qemu-devel] " Alex Williamson
2010-12-14 9:18 ` Avi Kivity
2010-12-14 9:18 ` [Qemu-devel] " Avi Kivity
2010-12-14 15:16 ` Alex Williamson
2010-12-14 15:16 ` [Qemu-devel] " Alex Williamson
2010-12-14 15:18 ` Anthony Liguori
2010-12-14 15:18 ` [Qemu-devel] " Anthony Liguori
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=20101213212430.2472.23807.stgit@s20.home \
--to=alex.williamson@redhat.com \
--cc=anthony@codemonkey.ws \
--cc=blauwirbel@gmail.com \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.