xref: /openbmc/qemu/backends/hostmem-file.c (revision 4f7b1ecb)
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     backend->aligned = true;
84     name = host_memory_backend_get_name(backend);
85     ram_flags = backend->share ? RAM_SHARED : 0;
86     ram_flags |= fb->readonly ? RAM_READONLY_FD : 0;
87     ram_flags |= fb->rom == ON_OFF_AUTO_ON ? RAM_READONLY : 0;
88     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
89     ram_flags |= backend->guest_memfd ? RAM_GUEST_MEMFD : 0;
90     ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
91     ram_flags |= RAM_NAMED_FILE;
92     return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
93                                             backend->size, fb->align, ram_flags,
94                                             fb->mem_path, fb->offset, errp);
95 #endif
96 }
97 
98 static char *get_mem_path(Object *o, Error **errp)
99 {
100     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
101 
102     return g_strdup(fb->mem_path);
103 }
104 
105 static void set_mem_path(Object *o, const char *str, Error **errp)
106 {
107     HostMemoryBackend *backend = MEMORY_BACKEND(o);
108     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
109 
110     if (host_memory_backend_mr_inited(backend)) {
111         error_setg(errp, "cannot change property 'mem-path' of %s",
112                    object_get_typename(o));
113         return;
114     }
115     g_free(fb->mem_path);
116     fb->mem_path = g_strdup(str);
117 }
118 
119 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
120 {
121     return MEMORY_BACKEND_FILE(o)->discard_data;
122 }
123 
124 static void file_memory_backend_set_discard_data(Object *o, bool value,
125                                                Error **errp)
126 {
127     MEMORY_BACKEND_FILE(o)->discard_data = value;
128 }
129 
130 static void file_memory_backend_get_align(Object *o, Visitor *v,
131                                           const char *name, void *opaque,
132                                           Error **errp)
133 {
134     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
135     uint64_t val = fb->align;
136 
137     visit_type_size(v, name, &val, errp);
138 }
139 
140 static void file_memory_backend_set_align(Object *o, Visitor *v,
141                                           const char *name, void *opaque,
142                                           Error **errp)
143 {
144     HostMemoryBackend *backend = MEMORY_BACKEND(o);
145     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
146     uint64_t val;
147 
148     if (host_memory_backend_mr_inited(backend)) {
149         error_setg(errp, "cannot change property '%s' of %s", name,
150                    object_get_typename(o));
151         return;
152     }
153 
154     if (!visit_type_size(v, name, &val, errp)) {
155         return;
156     }
157     fb->align = val;
158 }
159 
160 static void file_memory_backend_get_offset(Object *o, Visitor *v,
161                                           const char *name, void *opaque,
162                                           Error **errp)
163 {
164     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
165     uint64_t val = fb->offset;
166 
167     visit_type_size(v, name, &val, errp);
168 }
169 
170 static void file_memory_backend_set_offset(Object *o, Visitor *v,
171                                           const char *name, void *opaque,
172                                           Error **errp)
173 {
174     HostMemoryBackend *backend = MEMORY_BACKEND(o);
175     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
176     uint64_t val;
177 
178     if (host_memory_backend_mr_inited(backend)) {
179         error_setg(errp, "cannot change property '%s' of %s", name,
180                    object_get_typename(o));
181         return;
182     }
183 
184     if (!visit_type_size(v, name, &val, errp)) {
185         return;
186     }
187     fb->offset = val;
188 }
189 
190 #ifdef CONFIG_LIBPMEM
191 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
192 {
193     return MEMORY_BACKEND_FILE(o)->is_pmem;
194 }
195 
196 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
197 {
198     HostMemoryBackend *backend = MEMORY_BACKEND(o);
199     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
200 
201     if (host_memory_backend_mr_inited(backend)) {
202         error_setg(errp, "cannot change property 'pmem' of %s.",
203                    object_get_typename(o));
204         return;
205     }
206 
207     fb->is_pmem = value;
208 }
209 #endif /* CONFIG_LIBPMEM */
210 
211 static bool file_memory_backend_get_readonly(Object *obj, Error **errp)
212 {
213     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
214 
215     return fb->readonly;
216 }
217 
218 static void file_memory_backend_set_readonly(Object *obj, bool value,
219                                              Error **errp)
220 {
221     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
222     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
223 
224     if (host_memory_backend_mr_inited(backend)) {
225         error_setg(errp, "cannot change property 'readonly' of %s.",
226                    object_get_typename(obj));
227         return;
228     }
229 
230     fb->readonly = value;
231 }
232 
233 static void file_memory_backend_get_rom(Object *obj, Visitor *v,
234                                         const char *name, void *opaque,
235                                         Error **errp)
236 {
237     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
238     OnOffAuto rom = fb->rom;
239 
240     visit_type_OnOffAuto(v, name, &rom, errp);
241 }
242 
243 static void file_memory_backend_set_rom(Object *obj, Visitor *v,
244                                         const char *name, void *opaque,
245                                         Error **errp)
246 {
247     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
248     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
249 
250     if (host_memory_backend_mr_inited(backend)) {
251         error_setg(errp, "cannot change property '%s' of %s.", name,
252                    object_get_typename(obj));
253         return;
254     }
255 
256     visit_type_OnOffAuto(v, name, &fb->rom, errp);
257 }
258 
259 static void file_backend_unparent(Object *obj)
260 {
261     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
262     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
263 
264     if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
265         void *ptr = memory_region_get_ram_ptr(&backend->mr);
266         uint64_t sz = memory_region_size(&backend->mr);
267 
268         qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
269     }
270 }
271 
272 static void
273 file_backend_class_init(ObjectClass *oc, void *data)
274 {
275     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
276 
277     bc->alloc = file_backend_memory_alloc;
278     oc->unparent = file_backend_unparent;
279 
280     object_class_property_add_bool(oc, "discard-data",
281         file_memory_backend_get_discard_data, file_memory_backend_set_discard_data);
282     object_class_property_add_str(oc, "mem-path",
283         get_mem_path, set_mem_path);
284     object_class_property_add(oc, "align", "int",
285         file_memory_backend_get_align,
286         file_memory_backend_set_align,
287         NULL, NULL);
288     object_class_property_add(oc, "offset", "int",
289         file_memory_backend_get_offset,
290         file_memory_backend_set_offset,
291         NULL, NULL);
292     object_class_property_set_description(oc, "offset",
293         "Offset into the target file (ex: 1G)");
294 #ifdef CONFIG_LIBPMEM
295     object_class_property_add_bool(oc, "pmem",
296         file_memory_backend_get_pmem, file_memory_backend_set_pmem);
297 #endif
298     object_class_property_add_bool(oc, "readonly",
299         file_memory_backend_get_readonly,
300         file_memory_backend_set_readonly);
301     object_class_property_add(oc, "rom", "OnOffAuto",
302         file_memory_backend_get_rom, file_memory_backend_set_rom, NULL, NULL);
303     object_class_property_set_description(oc, "rom",
304         "Whether to create Read Only Memory (ROM)");
305 }
306 
307 static void file_backend_instance_finalize(Object *o)
308 {
309     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
310 
311     g_free(fb->mem_path);
312 }
313 
314 static const TypeInfo file_backend_info = {
315     .name = TYPE_MEMORY_BACKEND_FILE,
316     .parent = TYPE_MEMORY_BACKEND,
317     .class_init = file_backend_class_init,
318     .instance_finalize = file_backend_instance_finalize,
319     .instance_size = sizeof(HostMemoryBackendFile),
320 };
321 
322 static void register_types(void)
323 {
324     type_register_static(&file_backend_info);
325 }
326 
327 type_init(register_types);
328