hw/display/bcm2835_fb: Validate config settings
[qemu.git] / backends / hostmem-file.c
blob2476dcb4359731e5347154dca52e28499611edc1
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.
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu-common.h"
15 #include "qemu/error-report.h"
16 #include "sysemu/hostmem.h"
17 #include "sysemu/sysemu.h"
18 #include "qom/object_interfaces.h"
20 /* hostmem-file.c */
21 /**
22 * @TYPE_MEMORY_BACKEND_FILE:
23 * name of backend that uses mmap on a file descriptor
25 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
27 #define MEMORY_BACKEND_FILE(obj) \
28 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
30 typedef struct HostMemoryBackendFile HostMemoryBackendFile;
32 struct HostMemoryBackendFile {
33 HostMemoryBackend parent_obj;
35 char *mem_path;
36 uint64_t align;
37 bool discard_data;
38 bool is_pmem;
41 static void
42 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
44 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
46 if (!backend->size) {
47 error_setg(errp, "can't create backend with size 0");
48 return;
50 if (!fb->mem_path) {
51 error_setg(errp, "mem-path property not set");
52 return;
54 #ifndef CONFIG_LINUX
55 error_setg(errp, "-mem-path not supported on this host");
56 #else
57 if (!host_memory_backend_mr_inited(backend)) {
58 gchar *path;
59 backend->force_prealloc = mem_prealloc;
60 path = object_get_canonical_path(OBJECT(backend));
61 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
62 path,
63 backend->size, fb->align,
64 (backend->share ? RAM_SHARED : 0) |
65 (fb->is_pmem ? RAM_PMEM : 0),
66 fb->mem_path, errp);
67 g_free(path);
69 #endif
72 static char *get_mem_path(Object *o, Error **errp)
74 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
76 return g_strdup(fb->mem_path);
79 static void set_mem_path(Object *o, const char *str, Error **errp)
81 HostMemoryBackend *backend = MEMORY_BACKEND(o);
82 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
84 if (host_memory_backend_mr_inited(backend)) {
85 error_setg(errp, "cannot change property value");
86 return;
88 g_free(fb->mem_path);
89 fb->mem_path = g_strdup(str);
92 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
94 return MEMORY_BACKEND_FILE(o)->discard_data;
97 static void file_memory_backend_set_discard_data(Object *o, bool value,
98 Error **errp)
100 MEMORY_BACKEND_FILE(o)->discard_data = value;
103 static void file_memory_backend_get_align(Object *o, Visitor *v,
104 const char *name, void *opaque,
105 Error **errp)
107 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
108 uint64_t val = fb->align;
110 visit_type_size(v, name, &val, errp);
113 static void file_memory_backend_set_align(Object *o, Visitor *v,
114 const char *name, void *opaque,
115 Error **errp)
117 HostMemoryBackend *backend = MEMORY_BACKEND(o);
118 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
119 Error *local_err = NULL;
120 uint64_t val;
122 if (host_memory_backend_mr_inited(backend)) {
123 error_setg(&local_err, "cannot change property value");
124 goto out;
127 visit_type_size(v, name, &val, &local_err);
128 if (local_err) {
129 goto out;
131 fb->align = val;
133 out:
134 error_propagate(errp, local_err);
137 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
139 return MEMORY_BACKEND_FILE(o)->is_pmem;
142 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
144 HostMemoryBackend *backend = MEMORY_BACKEND(o);
145 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
147 if (host_memory_backend_mr_inited(backend)) {
148 error_setg(errp, "cannot change property 'pmem' of %s '%s'",
149 object_get_typename(o),
150 object_get_canonical_path_component(o));
151 return;
154 #ifndef CONFIG_LIBPMEM
155 if (value) {
156 Error *local_err = NULL;
157 error_setg(&local_err,
158 "Lack of libpmem support while setting the 'pmem=on'"
159 " of %s '%s'. We can't ensure data persistence.",
160 object_get_typename(o),
161 object_get_canonical_path_component(o));
162 error_propagate(errp, local_err);
163 return;
165 #endif
167 fb->is_pmem = value;
170 static void file_backend_unparent(Object *obj)
172 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
173 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
175 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
176 void *ptr = memory_region_get_ram_ptr(&backend->mr);
177 uint64_t sz = memory_region_size(&backend->mr);
179 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
183 static void
184 file_backend_class_init(ObjectClass *oc, void *data)
186 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
188 bc->alloc = file_backend_memory_alloc;
189 oc->unparent = file_backend_unparent;
191 object_class_property_add_bool(oc, "discard-data",
192 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data,
193 &error_abort);
194 object_class_property_add_str(oc, "mem-path",
195 get_mem_path, set_mem_path,
196 &error_abort);
197 object_class_property_add(oc, "align", "int",
198 file_memory_backend_get_align,
199 file_memory_backend_set_align,
200 NULL, NULL, &error_abort);
201 object_class_property_add_bool(oc, "pmem",
202 file_memory_backend_get_pmem, file_memory_backend_set_pmem,
203 &error_abort);
206 static void file_backend_instance_finalize(Object *o)
208 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
210 g_free(fb->mem_path);
213 static const TypeInfo file_backend_info = {
214 .name = TYPE_MEMORY_BACKEND_FILE,
215 .parent = TYPE_MEMORY_BACKEND,
216 .class_init = file_backend_class_init,
217 .instance_finalize = file_backend_instance_finalize,
218 .instance_size = sizeof(HostMemoryBackendFile),
221 static void register_types(void)
223 type_register_static(&file_backend_info);
226 type_init(register_types);