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