xref: /openbmc/qemu/backends/hostmem.c (revision 07a32d6b)
1 /*
2  * QEMU Host Memory Backend
3  *
4  * Copyright (C) 2013-2014 Red Hat Inc
5  *
6  * Authors:
7  *   Igor Mammedov <imammedo@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 #include "sysemu/hostmem.h"
13 #include "sysemu/sysemu.h"
14 #include "qapi/visitor.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qemu/config-file.h"
17 #include "qom/object_interfaces.h"
18 
19 static void
20 hostmemory_backend_get_size(Object *obj, Visitor *v, void *opaque,
21                             const char *name, Error **errp)
22 {
23     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
24     uint64_t value = backend->size;
25 
26     visit_type_size(v, &value, name, errp);
27 }
28 
29 static void
30 hostmemory_backend_set_size(Object *obj, Visitor *v, void *opaque,
31                             const char *name, Error **errp)
32 {
33     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
34     Error *local_err = NULL;
35     uint64_t value;
36 
37     if (memory_region_size(&backend->mr)) {
38         error_setg(&local_err, "cannot change property value");
39         goto out;
40     }
41 
42     visit_type_size(v, &value, name, &local_err);
43     if (local_err) {
44         goto out;
45     }
46     if (!value) {
47         error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
48                    PRIu64 "'", object_get_typename(obj), name, value);
49         goto out;
50     }
51     backend->size = value;
52 out:
53     error_propagate(errp, local_err);
54 }
55 
56 static void hostmemory_backend_init(Object *obj)
57 {
58     object_property_add(obj, "size", "int",
59                         hostmemory_backend_get_size,
60                         hostmemory_backend_set_size, NULL, NULL, NULL);
61 }
62 
63 static void hostmemory_backend_finalize(Object *obj)
64 {
65     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
66 
67     if (memory_region_size(&backend->mr)) {
68         memory_region_destroy(&backend->mr);
69     }
70 }
71 
72 MemoryRegion *
73 host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
74 {
75     return memory_region_size(&backend->mr) ? &backend->mr : NULL;
76 }
77 
78 static const TypeInfo hostmemory_backend_info = {
79     .name = TYPE_MEMORY_BACKEND,
80     .parent = TYPE_OBJECT,
81     .abstract = true,
82     .class_size = sizeof(HostMemoryBackendClass),
83     .instance_size = sizeof(HostMemoryBackend),
84     .instance_init = hostmemory_backend_init,
85     .instance_finalize = hostmemory_backend_finalize,
86     .interfaces = (InterfaceInfo[]) {
87         { TYPE_USER_CREATABLE },
88         { }
89     }
90 };
91 
92 static void register_types(void)
93 {
94     type_register_static(&hostmemory_backend_info);
95 }
96 
97 type_init(register_types);
98