aboutsummaryrefslogtreecommitdiff
path: root/backends/hostmem.c
diff options
context:
space:
mode:
authorIgor Mammedov <imammedo@redhat.com>2014-06-06 17:54:29 +0200
committerMichael S. Tsirkin <mst@redhat.com>2014-06-18 21:10:30 +0300
commit1f07048933d9c21acb40bce18af4f3ad6bae59b6 (patch)
tree2533a08b489ebf6abb7b4d6f56619fa13a987ce5 /backends/hostmem.c
parent2d9c2725f7bd08ffaf824b8c823f1423258ec78d (diff)
add memdev backend infrastructure
Provides framework for splitting host RAM allocation/ policies into a separate backend that could be used by devices. Initially only legacy RAM backend is provided, which uses memory_region_init_ram() allocator and compatible with every CLI option that affects memory_region_init_ram(). Signed-off-by: Igor Mammedov <imammedo@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Diffstat (limited to 'backends/hostmem.c')
-rw-r--r--backends/hostmem.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/backends/hostmem.c b/backends/hostmem.c
new file mode 100644
index 0000000000..2f578acc81
--- /dev/null
+++ b/backends/hostmem.c
@@ -0,0 +1,97 @@
+/*
+ * QEMU Host Memory Backend
+ *
+ * Copyright (C) 2013-2014 Red Hat Inc
+ *
+ * Authors:
+ * Igor Mammedov <imammedo@redhat.com>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#include "sysemu/hostmem.h"
+#include "sysemu/sysemu.h"
+#include "qapi/visitor.h"
+#include "qapi/qmp/qerror.h"
+#include "qemu/config-file.h"
+#include "qom/object_interfaces.h"
+
+static void
+hostmemory_backend_get_size(Object *obj, Visitor *v, void *opaque,
+ const char *name, Error **errp)
+{
+ HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+ uint64_t value = backend->size;
+
+ visit_type_size(v, &value, name, errp);
+}
+
+static void
+hostmemory_backend_set_size(Object *obj, Visitor *v, void *opaque,
+ const char *name, Error **errp)
+{
+ HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+ Error *local_err = NULL;
+ uint64_t value;
+
+ if (memory_region_size(&backend->mr)) {
+ error_setg(&local_err, "cannot change property value");
+ goto out;
+ }
+
+ visit_type_size(v, &value, name, &local_err);
+ if (local_err) {
+ goto out;
+ }
+ if (!value) {
+ error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
+ PRIu64 "'", object_get_typename(obj), name, value);
+ goto out;
+ }
+ backend->size = value;
+out:
+ error_propagate(errp, local_err);
+}
+
+static void hostmemory_backend_init(Object *obj)
+{
+ object_property_add(obj, "size", "int",
+ hostmemory_backend_get_size,
+ hostmemory_backend_set_size, NULL, NULL, NULL);
+}
+
+static void hostmemory_backend_finalize(Object *obj)
+{
+ HostMemoryBackend *backend = MEMORY_BACKEND(obj);
+
+ if (memory_region_size(&backend->mr)) {
+ memory_region_destroy(&backend->mr);
+ }
+}
+
+MemoryRegion *
+host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
+{
+ return memory_region_size(&backend->mr) ? &backend->mr : NULL;
+}
+
+static const TypeInfo hostmemory_backend_info = {
+ .name = TYPE_MEMORY_BACKEND,
+ .parent = TYPE_OBJECT,
+ .abstract = true,
+ .class_size = sizeof(HostMemoryBackendClass),
+ .instance_size = sizeof(HostMemoryBackend),
+ .instance_init = hostmemory_backend_init,
+ .instance_finalize = hostmemory_backend_finalize,
+ .interfaces = (InterfaceInfo[]) {
+ { TYPE_USER_CREATABLE },
+ { }
+ }
+};
+
+static void register_types(void)
+{
+ type_register_static(&hostmemory_backend_info);
+}
+
+type_init(register_types);