blob: 17387748952c38113fa9314d69919e5c73187589 [file] [log] [blame]
Igor Mammedov1f070482014-06-06 17:54:29 +02001/*
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
19static void
Hu Tao58f46622014-06-10 19:15:18 +080020host_memory_backend_get_size(Object *obj, Visitor *v, void *opaque,
21 const char *name, Error **errp)
Igor Mammedov1f070482014-06-06 17:54:29 +020022{
23 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
24 uint64_t value = backend->size;
25
26 visit_type_size(v, &value, name, errp);
27}
28
29static void
Hu Tao58f46622014-06-10 19:15:18 +080030host_memory_backend_set_size(Object *obj, Visitor *v, void *opaque,
31 const char *name, Error **errp)
Igor Mammedov1f070482014-06-06 17:54:29 +020032{
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;
52out:
53 error_propagate(errp, local_err);
54}
55
Hu Tao58f46622014-06-10 19:15:18 +080056static void host_memory_backend_init(Object *obj)
Igor Mammedov1f070482014-06-06 17:54:29 +020057{
58 object_property_add(obj, "size", "int",
Hu Tao58f46622014-06-10 19:15:18 +080059 host_memory_backend_get_size,
60 host_memory_backend_set_size, NULL, NULL, NULL);
Igor Mammedov1f070482014-06-06 17:54:29 +020061}
62
Hu Tao58f46622014-06-10 19:15:18 +080063static void host_memory_backend_finalize(Object *obj)
Igor Mammedov1f070482014-06-06 17:54:29 +020064{
65 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
66
67 if (memory_region_size(&backend->mr)) {
68 memory_region_destroy(&backend->mr);
69 }
70}
71
72MemoryRegion *
73host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
74{
75 return memory_region_size(&backend->mr) ? &backend->mr : NULL;
76}
77
Hu Taobd9262d2014-06-10 19:15:19 +080078static void
79host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
80{
81 HostMemoryBackend *backend = MEMORY_BACKEND(uc);
82 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
83
84 if (bc->alloc) {
85 bc->alloc(backend, errp);
86 }
87}
88
89static void
90host_memory_backend_class_init(ObjectClass *oc, void *data)
91{
92 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
93
94 ucc->complete = host_memory_backend_memory_complete;
95}
96
Hu Tao58f46622014-06-10 19:15:18 +080097static const TypeInfo host_memory_backend_info = {
Igor Mammedov1f070482014-06-06 17:54:29 +020098 .name = TYPE_MEMORY_BACKEND,
99 .parent = TYPE_OBJECT,
100 .abstract = true,
101 .class_size = sizeof(HostMemoryBackendClass),
Hu Taobd9262d2014-06-10 19:15:19 +0800102 .class_init = host_memory_backend_class_init,
Igor Mammedov1f070482014-06-06 17:54:29 +0200103 .instance_size = sizeof(HostMemoryBackend),
Hu Tao58f46622014-06-10 19:15:18 +0800104 .instance_init = host_memory_backend_init,
105 .instance_finalize = host_memory_backend_finalize,
Igor Mammedov1f070482014-06-06 17:54:29 +0200106 .interfaces = (InterfaceInfo[]) {
107 { TYPE_USER_CREATABLE },
108 { }
109 }
110};
111
112static void register_types(void)
113{
Hu Tao58f46622014-06-10 19:15:18 +0800114 type_register_static(&host_memory_backend_info);
Igor Mammedov1f070482014-06-06 17:54:29 +0200115}
116
117type_init(register_types);