accel/tcg: Improve can_do_io management
[qemu/armbru.git] / backends / hostmem-file.c
blobac3e433cbddbca395472f929fc6da8228316ef0e
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 "qemu/madvise.h"
18 #include "sysemu/hostmem.h"
19 #include "qom/object_interfaces.h"
20 #include "qom/object.h"
21 #include "qapi/visitor.h"
22 #include "qapi/qapi-visit-common.h"
24 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile, MEMORY_BACKEND_FILE)
27 struct HostMemoryBackendFile {
28 HostMemoryBackend parent_obj;
30 char *mem_path;
31 uint64_t align;
32 uint64_t offset;
33 bool discard_data;
34 bool is_pmem;
35 bool readonly;
36 OnOffAuto rom;
39 static bool
40 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
42 #ifndef CONFIG_POSIX
43 error_setg(errp, "backend '%s' not supported on this host",
44 object_get_typename(OBJECT(backend)));
45 return false;
46 #else
47 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
48 g_autofree gchar *name = NULL;
49 uint32_t ram_flags;
51 if (!backend->size) {
52 error_setg(errp, "can't create backend with size 0");
53 return false;
55 if (!fb->mem_path) {
56 error_setg(errp, "mem-path property not set");
57 return false;
60 switch (fb->rom) {
61 case ON_OFF_AUTO_AUTO:
62 /* Traditionally, opening the file readonly always resulted in ROM. */
63 fb->rom = fb->readonly ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
64 break;
65 case ON_OFF_AUTO_ON:
66 if (!fb->readonly) {
67 error_setg(errp, "property 'rom' = 'on' is not supported with"
68 " 'readonly' = 'off'");
69 return false;
71 break;
72 case ON_OFF_AUTO_OFF:
73 if (fb->readonly && backend->share) {
74 error_setg(errp, "property 'rom' = 'off' is incompatible with"
75 " 'readonly' = 'on' and 'share' = 'on'");
76 return false;
78 break;
79 default:
80 g_assert_not_reached();
83 name = host_memory_backend_get_name(backend);
84 ram_flags = backend->share ? RAM_SHARED : 0;
85 ram_flags |= fb->readonly ? RAM_READONLY_FD : 0;
86 ram_flags |= fb->rom == ON_OFF_AUTO_ON ? RAM_READONLY : 0;
87 ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
88 ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
89 ram_flags |= RAM_NAMED_FILE;
90 return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
91 backend->size, fb->align, ram_flags,
92 fb->mem_path, fb->offset, errp);
93 #endif
96 static char *get_mem_path(Object *o, Error **errp)
98 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
100 return g_strdup(fb->mem_path);
103 static void set_mem_path(Object *o, const char *str, Error **errp)
105 HostMemoryBackend *backend = MEMORY_BACKEND(o);
106 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
108 if (host_memory_backend_mr_inited(backend)) {
109 error_setg(errp, "cannot change property 'mem-path' of %s",
110 object_get_typename(o));
111 return;
113 g_free(fb->mem_path);
114 fb->mem_path = g_strdup(str);
117 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
119 return MEMORY_BACKEND_FILE(o)->discard_data;
122 static void file_memory_backend_set_discard_data(Object *o, bool value,
123 Error **errp)
125 MEMORY_BACKEND_FILE(o)->discard_data = value;
128 static void file_memory_backend_get_align(Object *o, Visitor *v,
129 const char *name, void *opaque,
130 Error **errp)
132 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
133 uint64_t val = fb->align;
135 visit_type_size(v, name, &val, errp);
138 static void file_memory_backend_set_align(Object *o, Visitor *v,
139 const char *name, void *opaque,
140 Error **errp)
142 HostMemoryBackend *backend = MEMORY_BACKEND(o);
143 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
144 uint64_t val;
146 if (host_memory_backend_mr_inited(backend)) {
147 error_setg(errp, "cannot change property '%s' of %s", name,
148 object_get_typename(o));
149 return;
152 if (!visit_type_size(v, name, &val, errp)) {
153 return;
155 fb->align = val;
158 static void file_memory_backend_get_offset(Object *o, Visitor *v,
159 const char *name, void *opaque,
160 Error **errp)
162 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
163 uint64_t val = fb->offset;
165 visit_type_size(v, name, &val, errp);
168 static void file_memory_backend_set_offset(Object *o, Visitor *v,
169 const char *name, void *opaque,
170 Error **errp)
172 HostMemoryBackend *backend = MEMORY_BACKEND(o);
173 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
174 uint64_t val;
176 if (host_memory_backend_mr_inited(backend)) {
177 error_setg(errp, "cannot change property '%s' of %s", name,
178 object_get_typename(o));
179 return;
182 if (!visit_type_size(v, name, &val, errp)) {
183 return;
185 fb->offset = val;
188 #ifdef CONFIG_LIBPMEM
189 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
191 return MEMORY_BACKEND_FILE(o)->is_pmem;
194 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
196 HostMemoryBackend *backend = MEMORY_BACKEND(o);
197 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
199 if (host_memory_backend_mr_inited(backend)) {
200 error_setg(errp, "cannot change property 'pmem' of %s.",
201 object_get_typename(o));
202 return;
205 fb->is_pmem = value;
207 #endif /* CONFIG_LIBPMEM */
209 static bool file_memory_backend_get_readonly(Object *obj, Error **errp)
211 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
213 return fb->readonly;
216 static void file_memory_backend_set_readonly(Object *obj, bool value,
217 Error **errp)
219 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
220 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
222 if (host_memory_backend_mr_inited(backend)) {
223 error_setg(errp, "cannot change property 'readonly' of %s.",
224 object_get_typename(obj));
225 return;
228 fb->readonly = value;
231 static void file_memory_backend_get_rom(Object *obj, Visitor *v,
232 const char *name, void *opaque,
233 Error **errp)
235 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
236 OnOffAuto rom = fb->rom;
238 visit_type_OnOffAuto(v, name, &rom, errp);
241 static void file_memory_backend_set_rom(Object *obj, Visitor *v,
242 const char *name, void *opaque,
243 Error **errp)
245 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
246 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
248 if (host_memory_backend_mr_inited(backend)) {
249 error_setg(errp, "cannot change property '%s' of %s.", name,
250 object_get_typename(obj));
251 return;
254 visit_type_OnOffAuto(v, name, &fb->rom, errp);
257 static void file_backend_unparent(Object *obj)
259 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
260 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
262 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
263 void *ptr = memory_region_get_ram_ptr(&backend->mr);
264 uint64_t sz = memory_region_size(&backend->mr);
266 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
270 static void
271 file_backend_class_init(ObjectClass *oc, void *data)
273 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
275 bc->alloc = file_backend_memory_alloc;
276 oc->unparent = file_backend_unparent;
278 object_class_property_add_bool(oc, "discard-data",
279 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data);
280 object_class_property_add_str(oc, "mem-path",
281 get_mem_path, set_mem_path);
282 object_class_property_add(oc, "align", "int",
283 file_memory_backend_get_align,
284 file_memory_backend_set_align,
285 NULL, NULL);
286 object_class_property_add(oc, "offset", "int",
287 file_memory_backend_get_offset,
288 file_memory_backend_set_offset,
289 NULL, NULL);
290 object_class_property_set_description(oc, "offset",
291 "Offset into the target file (ex: 1G)");
292 #ifdef CONFIG_LIBPMEM
293 object_class_property_add_bool(oc, "pmem",
294 file_memory_backend_get_pmem, file_memory_backend_set_pmem);
295 #endif
296 object_class_property_add_bool(oc, "readonly",
297 file_memory_backend_get_readonly,
298 file_memory_backend_set_readonly);
299 object_class_property_add(oc, "rom", "OnOffAuto",
300 file_memory_backend_get_rom, file_memory_backend_set_rom, NULL, NULL);
301 object_class_property_set_description(oc, "rom",
302 "Whether to create Read Only Memory (ROM)");
305 static void file_backend_instance_finalize(Object *o)
307 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
309 g_free(fb->mem_path);
312 static const TypeInfo file_backend_info = {
313 .name = TYPE_MEMORY_BACKEND_FILE,
314 .parent = TYPE_MEMORY_BACKEND,
315 .class_init = file_backend_class_init,
316 .instance_finalize = file_backend_instance_finalize,
317 .instance_size = sizeof(HostMemoryBackendFile),
320 static void register_types(void)
322 type_register_static(&file_backend_info);
325 type_init(register_types);