2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
7 * Paolo Bonzini <pbonzini@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 "qapi/error.h"
15 #include "qemu/error-report.h"
16 #include "qemu/module.h"
17 #include "qemu/madvise.h"
18 #include "sysemu/hostmem.h"
19 #include "qom/object_interfaces.h"
20 #include "qom/object.h"
22 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile, MEMORY_BACKEND_FILE)
25 struct HostMemoryBackendFile {
26 HostMemoryBackend parent_obj;
36 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
39 error_setg(errp, "backend '%s' not supported on this host",
40 object_get_typename(OBJECT(backend)));
42 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
47 error_setg(errp, "can't create backend with size 0");
51 error_setg(errp, "mem-path property not set");
55 name = host_memory_backend_get_name(backend);
56 ram_flags = backend->share ? RAM_SHARED : 0;
57 ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
58 ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
59 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
60 backend->size, fb->align, ram_flags,
61 fb->mem_path, fb->readonly, errp);
66 static char *get_mem_path(Object *o, Error **errp)
68 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
70 return g_strdup(fb->mem_path);
73 static void set_mem_path(Object *o, const char *str, Error **errp)
75 HostMemoryBackend *backend = MEMORY_BACKEND(o);
76 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
78 if (host_memory_backend_mr_inited(backend)) {
79 error_setg(errp, "cannot change property 'mem-path' of %s",
80 object_get_typename(o));
84 fb->mem_path = g_strdup(str);
87 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
89 return MEMORY_BACKEND_FILE(o)->discard_data;
92 static void file_memory_backend_set_discard_data(Object *o, bool value,
95 MEMORY_BACKEND_FILE(o)->discard_data = value;
98 static void file_memory_backend_get_align(Object *o, Visitor *v,
99 const char *name, void *opaque,
102 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
103 uint64_t val = fb->align;
105 visit_type_size(v, name, &val, errp);
108 static void file_memory_backend_set_align(Object *o, Visitor *v,
109 const char *name, void *opaque,
112 HostMemoryBackend *backend = MEMORY_BACKEND(o);
113 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
116 if (host_memory_backend_mr_inited(backend)) {
117 error_setg(errp, "cannot change property '%s' of %s", name,
118 object_get_typename(o));
122 if (!visit_type_size(v, name, &val, errp)) {
128 #ifdef CONFIG_LIBPMEM
129 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
131 return MEMORY_BACKEND_FILE(o)->is_pmem;
134 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
136 HostMemoryBackend *backend = MEMORY_BACKEND(o);
137 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
139 if (host_memory_backend_mr_inited(backend)) {
140 error_setg(errp, "cannot change property 'pmem' of %s.",
141 object_get_typename(o));
147 #endif /* CONFIG_LIBPMEM */
149 static bool file_memory_backend_get_readonly(Object *obj, Error **errp)
151 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
156 static void file_memory_backend_set_readonly(Object *obj, bool value,
159 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
160 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
162 if (host_memory_backend_mr_inited(backend)) {
163 error_setg(errp, "cannot change property 'readonly' of %s.",
164 object_get_typename(obj));
168 fb->readonly = value;
171 static void file_backend_unparent(Object *obj)
173 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
174 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
176 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
177 void *ptr = memory_region_get_ram_ptr(&backend->mr);
178 uint64_t sz = memory_region_size(&backend->mr);
180 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
185 file_backend_class_init(ObjectClass *oc, void *data)
187 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
189 bc->alloc = file_backend_memory_alloc;
190 oc->unparent = file_backend_unparent;
192 object_class_property_add_bool(oc, "discard-data",
193 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data);
194 object_class_property_add_str(oc, "mem-path",
195 get_mem_path, set_mem_path);
196 object_class_property_add(oc, "align", "int",
197 file_memory_backend_get_align,
198 file_memory_backend_set_align,
200 #ifdef CONFIG_LIBPMEM
201 object_class_property_add_bool(oc, "pmem",
202 file_memory_backend_get_pmem, file_memory_backend_set_pmem);
204 object_class_property_add_bool(oc, "readonly",
205 file_memory_backend_get_readonly,
206 file_memory_backend_set_readonly);
209 static void file_backend_instance_finalize(Object *o)
211 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
213 g_free(fb->mem_path);
216 static const TypeInfo file_backend_info = {
217 .name = TYPE_MEMORY_BACKEND_FILE,
218 .parent = TYPE_MEMORY_BACKEND,
219 .class_init = file_backend_class_init,
220 .instance_finalize = file_backend_instance_finalize,
221 .instance_size = sizeof(HostMemoryBackendFile),
224 static void register_types(void)
226 type_register_static(&file_backend_info);
229 type_init(register_types);