monitor/qmp: Update comment for commit 4eaca8de268
[qemu/armbru.git] / backends / hostmem-file.c
blob29e55c91952d859571a676ea50635c6df7d12c67
1 /*
2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
6 * Authors:
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 "sysemu/hostmem.h"
18 #include "sysemu/sysemu.h"
19 #include "qom/object_interfaces.h"
21 /* hostmem-file.c */
22 /**
23 * @TYPE_MEMORY_BACKEND_FILE:
24 * name of backend that uses mmap on a file descriptor
26 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
28 #define MEMORY_BACKEND_FILE(obj) \
29 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
31 typedef struct HostMemoryBackendFile HostMemoryBackendFile;
33 struct HostMemoryBackendFile {
34 HostMemoryBackend parent_obj;
36 char *mem_path;
37 uint64_t align;
38 bool discard_data;
39 bool is_pmem;
42 static void
43 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
45 #ifndef CONFIG_POSIX
46 error_setg(errp, "backend '%s' not supported on this host",
47 object_get_typename(OBJECT(backend)));
48 #else
49 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
50 gchar *name;
52 if (!backend->size) {
53 error_setg(errp, "can't create backend with size 0");
54 return;
56 if (!fb->mem_path) {
57 error_setg(errp, "mem-path property not set");
58 return;
62 * Verify pmem file size since starting a guest with an incorrect size
63 * leads to confusing failures inside the guest.
65 if (fb->is_pmem) {
66 Error *local_err = NULL;
67 uint64_t size;
69 size = qemu_get_pmem_size(fb->mem_path, &local_err);
70 if (!size) {
71 error_propagate(errp, local_err);
72 return;
75 if (backend->size > size) {
76 error_setg(errp, "size property %" PRIu64 " is larger than "
77 "pmem file \"%s\" size %" PRIu64, backend->size,
78 fb->mem_path, size);
79 return;
83 backend->force_prealloc = mem_prealloc;
84 name = host_memory_backend_get_name(backend);
85 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
86 name,
87 backend->size, fb->align,
88 (backend->share ? RAM_SHARED : 0) |
89 (fb->is_pmem ? RAM_PMEM : 0),
90 fb->mem_path, errp);
91 g_free(name);
92 #endif
95 static char *get_mem_path(Object *o, Error **errp)
97 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
99 return g_strdup(fb->mem_path);
102 static void set_mem_path(Object *o, const char *str, Error **errp)
104 HostMemoryBackend *backend = MEMORY_BACKEND(o);
105 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
107 if (host_memory_backend_mr_inited(backend)) {
108 error_setg(errp, "cannot change property 'mem-path' of %s",
109 object_get_typename(o));
110 return;
112 g_free(fb->mem_path);
113 fb->mem_path = g_strdup(str);
116 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
118 return MEMORY_BACKEND_FILE(o)->discard_data;
121 static void file_memory_backend_set_discard_data(Object *o, bool value,
122 Error **errp)
124 MEMORY_BACKEND_FILE(o)->discard_data = value;
127 static void file_memory_backend_get_align(Object *o, Visitor *v,
128 const char *name, void *opaque,
129 Error **errp)
131 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
132 uint64_t val = fb->align;
134 visit_type_size(v, name, &val, errp);
137 static void file_memory_backend_set_align(Object *o, Visitor *v,
138 const char *name, void *opaque,
139 Error **errp)
141 HostMemoryBackend *backend = MEMORY_BACKEND(o);
142 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
143 Error *local_err = NULL;
144 uint64_t val;
146 if (host_memory_backend_mr_inited(backend)) {
147 error_setg(&local_err, "cannot change property '%s' of %s",
148 name, object_get_typename(o));
149 goto out;
152 visit_type_size(v, name, &val, &local_err);
153 if (local_err) {
154 goto out;
156 fb->align = val;
158 out:
159 error_propagate(errp, local_err);
162 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
164 return MEMORY_BACKEND_FILE(o)->is_pmem;
167 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
169 HostMemoryBackend *backend = MEMORY_BACKEND(o);
170 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
172 if (host_memory_backend_mr_inited(backend)) {
174 error_setg(errp, "cannot change property 'pmem' of %s.",
175 object_get_typename(o));
176 return;
179 #ifndef CONFIG_LIBPMEM
180 if (value) {
181 Error *local_err = NULL;
183 error_setg(&local_err,
184 "Lack of libpmem support while setting the 'pmem=on'"
185 " of %s. We can't ensure data persistence.",
186 object_get_typename(o));
187 error_propagate(errp, local_err);
188 return;
190 #endif
192 fb->is_pmem = value;
195 static void file_backend_unparent(Object *obj)
197 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
198 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
200 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
201 void *ptr = memory_region_get_ram_ptr(&backend->mr);
202 uint64_t sz = memory_region_size(&backend->mr);
204 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
208 static void
209 file_backend_class_init(ObjectClass *oc, void *data)
211 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
213 bc->alloc = file_backend_memory_alloc;
214 oc->unparent = file_backend_unparent;
216 object_class_property_add_bool(oc, "discard-data",
217 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data,
218 &error_abort);
219 object_class_property_add_str(oc, "mem-path",
220 get_mem_path, set_mem_path,
221 &error_abort);
222 object_class_property_add(oc, "align", "int",
223 file_memory_backend_get_align,
224 file_memory_backend_set_align,
225 NULL, NULL, &error_abort);
226 object_class_property_add_bool(oc, "pmem",
227 file_memory_backend_get_pmem, file_memory_backend_set_pmem,
228 &error_abort);
231 static void file_backend_instance_finalize(Object *o)
233 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
235 g_free(fb->mem_path);
238 static const TypeInfo file_backend_info = {
239 .name = TYPE_MEMORY_BACKEND_FILE,
240 .parent = TYPE_MEMORY_BACKEND,
241 .class_init = file_backend_class_init,
242 .instance_finalize = file_backend_instance_finalize,
243 .instance_size = sizeof(HostMemoryBackendFile),
246 static void register_types(void)
248 type_register_static(&file_backend_info);
251 type_init(register_types);