xen_pt: QOMify
[qemu/ar7.git] / backends / hostmem.c
blob61c1ac0843ff08d27626b8fea012c2f1efdada00
1 /*
2 * QEMU Host Memory Backend
4 * Copyright (C) 2013-2014 Red Hat Inc
6 * Authors:
7 * Igor Mammedov <imammedo@redhat.com>
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.
12 #include "sysemu/hostmem.h"
13 #include "qapi/visitor.h"
14 #include "qapi-types.h"
15 #include "qapi-visit.h"
16 #include "qemu/config-file.h"
17 #include "qom/object_interfaces.h"
19 #ifdef CONFIG_NUMA
20 #include <numaif.h>
21 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT);
22 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED);
23 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND);
24 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE);
25 #endif
27 static void
28 host_memory_backend_get_size(Object *obj, Visitor *v, void *opaque,
29 const char *name, Error **errp)
31 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
32 uint64_t value = backend->size;
34 visit_type_size(v, &value, name, errp);
37 static void
38 host_memory_backend_set_size(Object *obj, Visitor *v, void *opaque,
39 const char *name, Error **errp)
41 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
42 Error *local_err = NULL;
43 uint64_t value;
45 if (memory_region_size(&backend->mr)) {
46 error_setg(&local_err, "cannot change property value");
47 goto out;
50 visit_type_size(v, &value, name, &local_err);
51 if (local_err) {
52 goto out;
54 if (!value) {
55 error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
56 PRIu64 "'", object_get_typename(obj), name, value);
57 goto out;
59 backend->size = value;
60 out:
61 error_propagate(errp, local_err);
64 static void
65 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, void *opaque,
66 const char *name, Error **errp)
68 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
69 uint16List *host_nodes = NULL;
70 uint16List **node = &host_nodes;
71 unsigned long value;
73 value = find_first_bit(backend->host_nodes, MAX_NODES);
74 if (value == MAX_NODES) {
75 return;
78 *node = g_malloc0(sizeof(**node));
79 (*node)->value = value;
80 node = &(*node)->next;
82 do {
83 value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
84 if (value == MAX_NODES) {
85 break;
88 *node = g_malloc0(sizeof(**node));
89 (*node)->value = value;
90 node = &(*node)->next;
91 } while (true);
93 visit_type_uint16List(v, &host_nodes, name, errp);
96 static void
97 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, void *opaque,
98 const char *name, Error **errp)
100 #ifdef CONFIG_NUMA
101 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
102 uint16List *l = NULL;
104 visit_type_uint16List(v, &l, name, errp);
106 while (l) {
107 bitmap_set(backend->host_nodes, l->value, 1);
108 l = l->next;
110 #else
111 error_setg(errp, "NUMA node binding are not supported by this QEMU");
112 #endif
115 static int
116 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
118 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
119 return backend->policy;
122 static void
123 host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
125 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
126 backend->policy = policy;
128 #ifndef CONFIG_NUMA
129 if (policy != HOST_MEM_POLICY_DEFAULT) {
130 error_setg(errp, "NUMA policies are not supported by this QEMU");
132 #endif
135 static bool host_memory_backend_get_merge(Object *obj, Error **errp)
137 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
139 return backend->merge;
142 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
144 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
146 if (!memory_region_size(&backend->mr)) {
147 backend->merge = value;
148 return;
151 if (value != backend->merge) {
152 void *ptr = memory_region_get_ram_ptr(&backend->mr);
153 uint64_t sz = memory_region_size(&backend->mr);
155 qemu_madvise(ptr, sz,
156 value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
157 backend->merge = value;
161 static bool host_memory_backend_get_dump(Object *obj, Error **errp)
163 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
165 return backend->dump;
168 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
170 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
172 if (!memory_region_size(&backend->mr)) {
173 backend->dump = value;
174 return;
177 if (value != backend->dump) {
178 void *ptr = memory_region_get_ram_ptr(&backend->mr);
179 uint64_t sz = memory_region_size(&backend->mr);
181 qemu_madvise(ptr, sz,
182 value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
183 backend->dump = value;
187 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
189 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
191 return backend->prealloc || backend->force_prealloc;
194 static void host_memory_backend_set_prealloc(Object *obj, bool value,
195 Error **errp)
197 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
199 if (backend->force_prealloc) {
200 if (value) {
201 error_setg(errp,
202 "remove -mem-prealloc to use the prealloc property");
203 return;
207 if (!memory_region_size(&backend->mr)) {
208 backend->prealloc = value;
209 return;
212 if (value && !backend->prealloc) {
213 int fd = memory_region_get_fd(&backend->mr);
214 void *ptr = memory_region_get_ram_ptr(&backend->mr);
215 uint64_t sz = memory_region_size(&backend->mr);
217 os_mem_prealloc(fd, ptr, sz);
218 backend->prealloc = true;
222 static void host_memory_backend_init(Object *obj)
224 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
226 backend->merge = qemu_opt_get_bool(qemu_get_machine_opts(),
227 "mem-merge", true);
228 backend->dump = qemu_opt_get_bool(qemu_get_machine_opts(),
229 "dump-guest-core", true);
230 backend->prealloc = mem_prealloc;
232 object_property_add_bool(obj, "merge",
233 host_memory_backend_get_merge,
234 host_memory_backend_set_merge, NULL);
235 object_property_add_bool(obj, "dump",
236 host_memory_backend_get_dump,
237 host_memory_backend_set_dump, NULL);
238 object_property_add_bool(obj, "prealloc",
239 host_memory_backend_get_prealloc,
240 host_memory_backend_set_prealloc, NULL);
241 object_property_add(obj, "size", "int",
242 host_memory_backend_get_size,
243 host_memory_backend_set_size, NULL, NULL, NULL);
244 object_property_add(obj, "host-nodes", "int",
245 host_memory_backend_get_host_nodes,
246 host_memory_backend_set_host_nodes, NULL, NULL, NULL);
247 object_property_add_enum(obj, "policy", "HostMemPolicy",
248 HostMemPolicy_lookup,
249 host_memory_backend_get_policy,
250 host_memory_backend_set_policy, NULL);
253 MemoryRegion *
254 host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
256 return memory_region_size(&backend->mr) ? &backend->mr : NULL;
259 static void
260 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
262 HostMemoryBackend *backend = MEMORY_BACKEND(uc);
263 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
264 Error *local_err = NULL;
265 void *ptr;
266 uint64_t sz;
268 if (bc->alloc) {
269 bc->alloc(backend, &local_err);
270 if (local_err) {
271 error_propagate(errp, local_err);
272 return;
275 ptr = memory_region_get_ram_ptr(&backend->mr);
276 sz = memory_region_size(&backend->mr);
278 if (backend->merge) {
279 qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
281 if (!backend->dump) {
282 qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
284 #ifdef CONFIG_NUMA
285 unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
286 /* lastbit == MAX_NODES means maxnode = 0 */
287 unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
288 /* ensure policy won't be ignored in case memory is preallocated
289 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
290 * this doesn't catch hugepage case. */
291 unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
293 /* check for invalid host-nodes and policies and give more verbose
294 * error messages than mbind(). */
295 if (maxnode && backend->policy == MPOL_DEFAULT) {
296 error_setg(errp, "host-nodes must be empty for policy default,"
297 " or you should explicitly specify a policy other"
298 " than default");
299 return;
300 } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
301 error_setg(errp, "host-nodes must be set for policy %s",
302 HostMemPolicy_lookup[backend->policy]);
303 return;
306 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
307 * as argument to mbind() due to an old Linux bug (feature?) which
308 * cuts off the last specified node. This means backend->host_nodes
309 * must have MAX_NODES+1 bits available.
311 assert(sizeof(backend->host_nodes) >=
312 BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
313 assert(maxnode <= MAX_NODES);
314 if (mbind(ptr, sz, backend->policy,
315 maxnode ? backend->host_nodes : NULL, maxnode + 1, flags)) {
316 error_setg_errno(errp, errno,
317 "cannot bind memory to host NUMA nodes");
318 return;
320 #endif
321 /* Preallocate memory after the NUMA policy has been instantiated.
322 * This is necessary to guarantee memory is allocated with
323 * specified NUMA policy in place.
325 if (backend->prealloc) {
326 os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz);
331 static bool
332 host_memory_backend_can_be_deleted(UserCreatable *uc, Error **errp)
334 MemoryRegion *mr;
336 mr = host_memory_backend_get_memory(MEMORY_BACKEND(uc), errp);
337 if (memory_region_is_mapped(mr)) {
338 return false;
339 } else {
340 return true;
344 static void
345 host_memory_backend_class_init(ObjectClass *oc, void *data)
347 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
349 ucc->complete = host_memory_backend_memory_complete;
350 ucc->can_be_deleted = host_memory_backend_can_be_deleted;
353 static const TypeInfo host_memory_backend_info = {
354 .name = TYPE_MEMORY_BACKEND,
355 .parent = TYPE_OBJECT,
356 .abstract = true,
357 .class_size = sizeof(HostMemoryBackendClass),
358 .class_init = host_memory_backend_class_init,
359 .instance_size = sizeof(HostMemoryBackend),
360 .instance_init = host_memory_backend_init,
361 .interfaces = (InterfaceInfo[]) {
362 { TYPE_USER_CREATABLE },
367 static void register_types(void)
369 type_register_static(&host_memory_backend_info);
372 type_init(register_types);