xref: /openbmc/qemu/backends/hostmem-file.c (revision effd60c8)
1 /*
2  * QEMU Host Memory Backend for hugetlbfs
3  *
4  * Copyright (C) 2013-2014 Red Hat Inc
5  *
6  * Authors:
7  *   Paolo Bonzini <pbonzini@redhat.com>
8  *
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.
11  */
12 
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"
23 
24 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile, MEMORY_BACKEND_FILE)
25 
26 
27 struct HostMemoryBackendFile {
28     HostMemoryBackend parent_obj;
29 
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;
37 };
38 
39 static bool
40 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
41 {
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;
50 
51     if (!backend->size) {
52         error_setg(errp, "can't create backend with size 0");
53         return false;
54     }
55     if (!fb->mem_path) {
56         error_setg(errp, "mem-path property not set");
57         return false;
58     }
59 
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;
70         }
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;
77         }
78         break;
79     default:
80         g_assert_not_reached();
81     }
82 
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
94 }
95 
96 static char *get_mem_path(Object *o, Error **errp)
97 {
98     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
99 
100     return g_strdup(fb->mem_path);
101 }
102 
103 static void set_mem_path(Object *o, const char *str, Error **errp)
104 {
105     HostMemoryBackend *backend = MEMORY_BACKEND(o);
106     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
107 
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;
112     }
113     g_free(fb->mem_path);
114     fb->mem_path = g_strdup(str);
115 }
116 
117 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
118 {
119     return MEMORY_BACKEND_FILE(o)->discard_data;
120 }
121 
122 static void file_memory_backend_set_discard_data(Object *o, bool value,
123                                                Error **errp)
124 {
125     MEMORY_BACKEND_FILE(o)->discard_data = value;
126 }
127 
128 static void file_memory_backend_get_align(Object *o, Visitor *v,
129                                           const char *name, void *opaque,
130                                           Error **errp)
131 {
132     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
133     uint64_t val = fb->align;
134 
135     visit_type_size(v, name, &val, errp);
136 }
137 
138 static void file_memory_backend_set_align(Object *o, Visitor *v,
139                                           const char *name, void *opaque,
140                                           Error **errp)
141 {
142     HostMemoryBackend *backend = MEMORY_BACKEND(o);
143     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
144     uint64_t val;
145 
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;
150     }
151 
152     if (!visit_type_size(v, name, &val, errp)) {
153         return;
154     }
155     fb->align = val;
156 }
157 
158 static void file_memory_backend_get_offset(Object *o, Visitor *v,
159                                           const char *name, void *opaque,
160                                           Error **errp)
161 {
162     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
163     uint64_t val = fb->offset;
164 
165     visit_type_size(v, name, &val, errp);
166 }
167 
168 static void file_memory_backend_set_offset(Object *o, Visitor *v,
169                                           const char *name, void *opaque,
170                                           Error **errp)
171 {
172     HostMemoryBackend *backend = MEMORY_BACKEND(o);
173     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
174     uint64_t val;
175 
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;
180     }
181 
182     if (!visit_type_size(v, name, &val, errp)) {
183         return;
184     }
185     fb->offset = val;
186 }
187 
188 #ifdef CONFIG_LIBPMEM
189 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
190 {
191     return MEMORY_BACKEND_FILE(o)->is_pmem;
192 }
193 
194 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
195 {
196     HostMemoryBackend *backend = MEMORY_BACKEND(o);
197     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
198 
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;
203     }
204 
205     fb->is_pmem = value;
206 }
207 #endif /* CONFIG_LIBPMEM */
208 
209 static bool file_memory_backend_get_readonly(Object *obj, Error **errp)
210 {
211     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
212 
213     return fb->readonly;
214 }
215 
216 static void file_memory_backend_set_readonly(Object *obj, bool value,
217                                              Error **errp)
218 {
219     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
220     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
221 
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;
226     }
227 
228     fb->readonly = value;
229 }
230 
231 static void file_memory_backend_get_rom(Object *obj, Visitor *v,
232                                         const char *name, void *opaque,
233                                         Error **errp)
234 {
235     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
236     OnOffAuto rom = fb->rom;
237 
238     visit_type_OnOffAuto(v, name, &rom, errp);
239 }
240 
241 static void file_memory_backend_set_rom(Object *obj, Visitor *v,
242                                         const char *name, void *opaque,
243                                         Error **errp)
244 {
245     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
246     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
247 
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;
252     }
253 
254     visit_type_OnOffAuto(v, name, &fb->rom, errp);
255 }
256 
257 static void file_backend_unparent(Object *obj)
258 {
259     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
260     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
261 
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);
265 
266         qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
267     }
268 }
269 
270 static void
271 file_backend_class_init(ObjectClass *oc, void *data)
272 {
273     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
274 
275     bc->alloc = file_backend_memory_alloc;
276     oc->unparent = file_backend_unparent;
277 
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)");
303 }
304 
305 static void file_backend_instance_finalize(Object *o)
306 {
307     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
308 
309     g_free(fb->mem_path);
310 }
311 
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),
318 };
319 
320 static void register_types(void)
321 {
322     type_register_static(&file_backend_info);
323 }
324 
325 type_init(register_types);
326