hw/core: Collect HMP command handlers in hw/core/
[qemu/kevin.git] / backends / hostmem.c
blob04baf479a113c478f8cfa140a7b6e90da38e72d2
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.
13 #include "qemu/osdep.h"
14 #include "sysemu/hostmem.h"
15 #include "hw/boards.h"
16 #include "qapi/error.h"
17 #include "qapi/qapi-builtin-visit.h"
18 #include "qapi/visitor.h"
19 #include "qemu/config-file.h"
20 #include "qom/object_interfaces.h"
21 #include "qemu/mmap-alloc.h"
23 #ifdef CONFIG_NUMA
24 #include <numaif.h>
25 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT);
26 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED);
27 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND);
28 QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE);
29 #endif
31 char *
32 host_memory_backend_get_name(HostMemoryBackend *backend)
34 if (!backend->use_canonical_path) {
35 return object_get_canonical_path_component(OBJECT(backend));
38 return object_get_canonical_path(OBJECT(backend));
41 static void
42 host_memory_backend_get_size(Object *obj, Visitor *v, const char *name,
43 void *opaque, Error **errp)
45 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
46 uint64_t value = backend->size;
48 visit_type_size(v, name, &value, errp);
51 static void
52 host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
53 void *opaque, Error **errp)
55 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
56 Error *local_err = NULL;
57 uint64_t value;
59 if (host_memory_backend_mr_inited(backend)) {
60 error_setg(&local_err, "cannot change property %s of %s ",
61 name, object_get_typename(obj));
62 goto out;
65 visit_type_size(v, name, &value, &local_err);
66 if (local_err) {
67 goto out;
69 if (!value) {
70 error_setg(&local_err,
71 "property '%s' of %s doesn't take value '%" PRIu64 "'",
72 name, object_get_typename(obj), value);
73 goto out;
75 backend->size = value;
76 out:
77 error_propagate(errp, local_err);
80 static void
81 host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
82 void *opaque, Error **errp)
84 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
85 uint16List *host_nodes = NULL;
86 uint16List **node = &host_nodes;
87 unsigned long value;
89 value = find_first_bit(backend->host_nodes, MAX_NODES);
90 if (value == MAX_NODES) {
91 goto ret;
94 *node = g_malloc0(sizeof(**node));
95 (*node)->value = value;
96 node = &(*node)->next;
98 do {
99 value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
100 if (value == MAX_NODES) {
101 break;
104 *node = g_malloc0(sizeof(**node));
105 (*node)->value = value;
106 node = &(*node)->next;
107 } while (true);
109 ret:
110 visit_type_uint16List(v, name, &host_nodes, errp);
113 static void
114 host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
115 void *opaque, Error **errp)
117 #ifdef CONFIG_NUMA
118 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
119 uint16List *l, *host_nodes = NULL;
121 visit_type_uint16List(v, name, &host_nodes, errp);
123 for (l = host_nodes; l; l = l->next) {
124 if (l->value >= MAX_NODES) {
125 error_setg(errp, "Invalid host-nodes value: %d", l->value);
126 goto out;
130 for (l = host_nodes; l; l = l->next) {
131 bitmap_set(backend->host_nodes, l->value, 1);
134 out:
135 qapi_free_uint16List(host_nodes);
136 #else
137 error_setg(errp, "NUMA node binding are not supported by this QEMU");
138 #endif
141 static int
142 host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
144 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
145 return backend->policy;
148 static void
149 host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
151 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
152 backend->policy = policy;
154 #ifndef CONFIG_NUMA
155 if (policy != HOST_MEM_POLICY_DEFAULT) {
156 error_setg(errp, "NUMA policies are not supported by this QEMU");
158 #endif
161 static bool host_memory_backend_get_merge(Object *obj, Error **errp)
163 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
165 return backend->merge;
168 static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
170 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
172 if (!host_memory_backend_mr_inited(backend)) {
173 backend->merge = value;
174 return;
177 if (value != backend->merge) {
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_MERGEABLE : QEMU_MADV_UNMERGEABLE);
183 backend->merge = value;
187 static bool host_memory_backend_get_dump(Object *obj, Error **errp)
189 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
191 return backend->dump;
194 static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
196 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
198 if (!host_memory_backend_mr_inited(backend)) {
199 backend->dump = value;
200 return;
203 if (value != backend->dump) {
204 void *ptr = memory_region_get_ram_ptr(&backend->mr);
205 uint64_t sz = memory_region_size(&backend->mr);
207 qemu_madvise(ptr, sz,
208 value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
209 backend->dump = value;
213 static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
215 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
217 return backend->prealloc || backend->force_prealloc;
220 static void host_memory_backend_set_prealloc(Object *obj, bool value,
221 Error **errp)
223 Error *local_err = NULL;
224 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
226 if (backend->force_prealloc) {
227 if (value) {
228 error_setg(errp,
229 "remove -mem-prealloc to use the prealloc property");
230 return;
234 if (!host_memory_backend_mr_inited(backend)) {
235 backend->prealloc = value;
236 return;
239 if (value && !backend->prealloc) {
240 int fd = memory_region_get_fd(&backend->mr);
241 void *ptr = memory_region_get_ram_ptr(&backend->mr);
242 uint64_t sz = memory_region_size(&backend->mr);
244 os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
245 if (local_err) {
246 error_propagate(errp, local_err);
247 return;
249 backend->prealloc = true;
253 static void host_memory_backend_init(Object *obj)
255 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
256 MachineState *machine = MACHINE(qdev_get_machine());
258 backend->merge = machine_mem_merge(machine);
259 backend->dump = machine_dump_guest_core(machine);
260 backend->prealloc = mem_prealloc;
263 static void host_memory_backend_post_init(Object *obj)
265 object_apply_compat_props(obj);
268 bool host_memory_backend_mr_inited(HostMemoryBackend *backend)
271 * NOTE: We forbid zero-length memory backend, so here zero means
272 * "we haven't inited the backend memory region yet".
274 return memory_region_size(&backend->mr) != 0;
277 MemoryRegion *host_memory_backend_get_memory(HostMemoryBackend *backend)
279 return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL;
282 void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped)
284 backend->is_mapped = mapped;
287 bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
289 return backend->is_mapped;
292 #ifdef __linux__
293 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
295 Object *obj = OBJECT(memdev);
296 char *path = object_property_get_str(obj, "mem-path", NULL);
297 size_t pagesize = qemu_mempath_getpagesize(path);
299 g_free(path);
300 return pagesize;
302 #else
303 size_t host_memory_backend_pagesize(HostMemoryBackend *memdev)
305 return getpagesize();
307 #endif
309 static void
310 host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
312 HostMemoryBackend *backend = MEMORY_BACKEND(uc);
313 HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
314 Error *local_err = NULL;
315 void *ptr;
316 uint64_t sz;
318 if (bc->alloc) {
319 bc->alloc(backend, &local_err);
320 if (local_err) {
321 goto out;
324 ptr = memory_region_get_ram_ptr(&backend->mr);
325 sz = memory_region_size(&backend->mr);
327 if (backend->merge) {
328 qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
330 if (!backend->dump) {
331 qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
333 #ifdef CONFIG_NUMA
334 unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
335 /* lastbit == MAX_NODES means maxnode = 0 */
336 unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
337 /* ensure policy won't be ignored in case memory is preallocated
338 * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
339 * this doesn't catch hugepage case. */
340 unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
342 /* check for invalid host-nodes and policies and give more verbose
343 * error messages than mbind(). */
344 if (maxnode && backend->policy == MPOL_DEFAULT) {
345 error_setg(errp, "host-nodes must be empty for policy default,"
346 " or you should explicitly specify a policy other"
347 " than default");
348 return;
349 } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
350 error_setg(errp, "host-nodes must be set for policy %s",
351 HostMemPolicy_str(backend->policy));
352 return;
355 /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
356 * as argument to mbind() due to an old Linux bug (feature?) which
357 * cuts off the last specified node. This means backend->host_nodes
358 * must have MAX_NODES+1 bits available.
360 assert(sizeof(backend->host_nodes) >=
361 BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
362 assert(maxnode <= MAX_NODES);
363 if (mbind(ptr, sz, backend->policy,
364 maxnode ? backend->host_nodes : NULL, maxnode + 1, flags)) {
365 if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
366 error_setg_errno(errp, errno,
367 "cannot bind memory to host NUMA nodes");
368 return;
371 #endif
372 /* Preallocate memory after the NUMA policy has been instantiated.
373 * This is necessary to guarantee memory is allocated with
374 * specified NUMA policy in place.
376 if (backend->prealloc) {
377 os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
378 smp_cpus, &local_err);
379 if (local_err) {
380 goto out;
384 out:
385 error_propagate(errp, local_err);
388 static bool
389 host_memory_backend_can_be_deleted(UserCreatable *uc)
391 if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) {
392 return false;
393 } else {
394 return true;
398 static bool host_memory_backend_get_share(Object *o, Error **errp)
400 HostMemoryBackend *backend = MEMORY_BACKEND(o);
402 return backend->share;
405 static void host_memory_backend_set_share(Object *o, bool value, Error **errp)
407 HostMemoryBackend *backend = MEMORY_BACKEND(o);
409 if (host_memory_backend_mr_inited(backend)) {
410 error_setg(errp, "cannot change property value");
411 return;
413 backend->share = value;
416 static bool
417 host_memory_backend_get_use_canonical_path(Object *obj, Error **errp)
419 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
421 return backend->use_canonical_path;
424 static void
425 host_memory_backend_set_use_canonical_path(Object *obj, bool value,
426 Error **errp)
428 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
430 backend->use_canonical_path = value;
433 static void
434 host_memory_backend_class_init(ObjectClass *oc, void *data)
436 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
438 ucc->complete = host_memory_backend_memory_complete;
439 ucc->can_be_deleted = host_memory_backend_can_be_deleted;
441 object_class_property_add_bool(oc, "merge",
442 host_memory_backend_get_merge,
443 host_memory_backend_set_merge, &error_abort);
444 object_class_property_set_description(oc, "merge",
445 "Mark memory as mergeable", &error_abort);
446 object_class_property_add_bool(oc, "dump",
447 host_memory_backend_get_dump,
448 host_memory_backend_set_dump, &error_abort);
449 object_class_property_set_description(oc, "dump",
450 "Set to 'off' to exclude from core dump", &error_abort);
451 object_class_property_add_bool(oc, "prealloc",
452 host_memory_backend_get_prealloc,
453 host_memory_backend_set_prealloc, &error_abort);
454 object_class_property_set_description(oc, "prealloc",
455 "Preallocate memory", &error_abort);
456 object_class_property_add(oc, "size", "int",
457 host_memory_backend_get_size,
458 host_memory_backend_set_size,
459 NULL, NULL, &error_abort);
460 object_class_property_set_description(oc, "size",
461 "Size of the memory region (ex: 500M)", &error_abort);
462 object_class_property_add(oc, "host-nodes", "int",
463 host_memory_backend_get_host_nodes,
464 host_memory_backend_set_host_nodes,
465 NULL, NULL, &error_abort);
466 object_class_property_set_description(oc, "host-nodes",
467 "Binds memory to the list of NUMA host nodes", &error_abort);
468 object_class_property_add_enum(oc, "policy", "HostMemPolicy",
469 &HostMemPolicy_lookup,
470 host_memory_backend_get_policy,
471 host_memory_backend_set_policy, &error_abort);
472 object_class_property_set_description(oc, "policy",
473 "Set the NUMA policy", &error_abort);
474 object_class_property_add_bool(oc, "share",
475 host_memory_backend_get_share, host_memory_backend_set_share,
476 &error_abort);
477 object_class_property_set_description(oc, "share",
478 "Mark the memory as private to QEMU or shared", &error_abort);
479 object_class_property_add_bool(oc, "x-use-canonical-path-for-ramblock-id",
480 host_memory_backend_get_use_canonical_path,
481 host_memory_backend_set_use_canonical_path, &error_abort);
484 static const TypeInfo host_memory_backend_info = {
485 .name = TYPE_MEMORY_BACKEND,
486 .parent = TYPE_OBJECT,
487 .abstract = true,
488 .class_size = sizeof(HostMemoryBackendClass),
489 .class_init = host_memory_backend_class_init,
490 .instance_size = sizeof(HostMemoryBackend),
491 .instance_init = host_memory_backend_init,
492 .instance_post_init = host_memory_backend_post_init,
493 .interfaces = (InterfaceInfo[]) {
494 { TYPE_USER_CREATABLE },
499 static void register_types(void)
501 type_register_static(&host_memory_backend_info);
504 type_init(register_types);