xref: /openbmc/qemu/backends/hostmem-file.c (revision 06831001)
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 
22 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile, MEMORY_BACKEND_FILE)
23 
24 
25 struct HostMemoryBackendFile {
26     HostMemoryBackend parent_obj;
27 
28     char *mem_path;
29     uint64_t align;
30     uint64_t offset;
31     bool discard_data;
32     bool is_pmem;
33     bool readonly;
34 };
35 
36 static void
37 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
38 {
39 #ifndef CONFIG_POSIX
40     error_setg(errp, "backend '%s' not supported on this host",
41                object_get_typename(OBJECT(backend)));
42 #else
43     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
44     uint32_t ram_flags;
45     gchar *name;
46 
47     if (!backend->size) {
48         error_setg(errp, "can't create backend with size 0");
49         return;
50     }
51     if (!fb->mem_path) {
52         error_setg(errp, "mem-path property not set");
53         return;
54     }
55 
56     name = host_memory_backend_get_name(backend);
57     ram_flags = backend->share ? RAM_SHARED : 0;
58     ram_flags |= backend->reserve ? 0 : RAM_NORESERVE;
59     ram_flags |= fb->is_pmem ? RAM_PMEM : 0;
60     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
61                                      backend->size, fb->align, ram_flags,
62                                      fb->mem_path, fb->offset, fb->readonly,
63                                      errp);
64     g_free(name);
65 #endif
66 }
67 
68 static char *get_mem_path(Object *o, Error **errp)
69 {
70     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
71 
72     return g_strdup(fb->mem_path);
73 }
74 
75 static void set_mem_path(Object *o, const char *str, Error **errp)
76 {
77     HostMemoryBackend *backend = MEMORY_BACKEND(o);
78     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
79 
80     if (host_memory_backend_mr_inited(backend)) {
81         error_setg(errp, "cannot change property 'mem-path' of %s",
82                    object_get_typename(o));
83         return;
84     }
85     g_free(fb->mem_path);
86     fb->mem_path = g_strdup(str);
87 }
88 
89 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
90 {
91     return MEMORY_BACKEND_FILE(o)->discard_data;
92 }
93 
94 static void file_memory_backend_set_discard_data(Object *o, bool value,
95                                                Error **errp)
96 {
97     MEMORY_BACKEND_FILE(o)->discard_data = value;
98 }
99 
100 static void file_memory_backend_get_align(Object *o, Visitor *v,
101                                           const char *name, void *opaque,
102                                           Error **errp)
103 {
104     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
105     uint64_t val = fb->align;
106 
107     visit_type_size(v, name, &val, errp);
108 }
109 
110 static void file_memory_backend_set_align(Object *o, Visitor *v,
111                                           const char *name, void *opaque,
112                                           Error **errp)
113 {
114     HostMemoryBackend *backend = MEMORY_BACKEND(o);
115     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
116     uint64_t val;
117 
118     if (host_memory_backend_mr_inited(backend)) {
119         error_setg(errp, "cannot change property '%s' of %s", name,
120                    object_get_typename(o));
121         return;
122     }
123 
124     if (!visit_type_size(v, name, &val, errp)) {
125         return;
126     }
127     fb->align = val;
128 }
129 
130 static void file_memory_backend_get_offset(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->offset;
136 
137     visit_type_size(v, name, &val, errp);
138 }
139 
140 static void file_memory_backend_set_offset(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->offset = val;
158 }
159 
160 #ifdef CONFIG_LIBPMEM
161 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
162 {
163     return MEMORY_BACKEND_FILE(o)->is_pmem;
164 }
165 
166 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
167 {
168     HostMemoryBackend *backend = MEMORY_BACKEND(o);
169     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
170 
171     if (host_memory_backend_mr_inited(backend)) {
172         error_setg(errp, "cannot change property 'pmem' of %s.",
173                    object_get_typename(o));
174         return;
175     }
176 
177     fb->is_pmem = value;
178 }
179 #endif /* CONFIG_LIBPMEM */
180 
181 static bool file_memory_backend_get_readonly(Object *obj, Error **errp)
182 {
183     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
184 
185     return fb->readonly;
186 }
187 
188 static void file_memory_backend_set_readonly(Object *obj, bool value,
189                                              Error **errp)
190 {
191     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
192     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
193 
194     if (host_memory_backend_mr_inited(backend)) {
195         error_setg(errp, "cannot change property 'readonly' of %s.",
196                    object_get_typename(obj));
197         return;
198     }
199 
200     fb->readonly = value;
201 }
202 
203 static void file_backend_unparent(Object *obj)
204 {
205     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
206     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
207 
208     if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
209         void *ptr = memory_region_get_ram_ptr(&backend->mr);
210         uint64_t sz = memory_region_size(&backend->mr);
211 
212         qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
213     }
214 }
215 
216 static void
217 file_backend_class_init(ObjectClass *oc, void *data)
218 {
219     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
220 
221     bc->alloc = file_backend_memory_alloc;
222     oc->unparent = file_backend_unparent;
223 
224     object_class_property_add_bool(oc, "discard-data",
225         file_memory_backend_get_discard_data, file_memory_backend_set_discard_data);
226     object_class_property_add_str(oc, "mem-path",
227         get_mem_path, set_mem_path);
228     object_class_property_add(oc, "align", "int",
229         file_memory_backend_get_align,
230         file_memory_backend_set_align,
231         NULL, NULL);
232     object_class_property_add(oc, "offset", "int",
233         file_memory_backend_get_offset,
234         file_memory_backend_set_offset,
235         NULL, NULL);
236     object_class_property_set_description(oc, "offset",
237         "Offset into the target file (ex: 1G)");
238 #ifdef CONFIG_LIBPMEM
239     object_class_property_add_bool(oc, "pmem",
240         file_memory_backend_get_pmem, file_memory_backend_set_pmem);
241 #endif
242     object_class_property_add_bool(oc, "readonly",
243         file_memory_backend_get_readonly,
244         file_memory_backend_set_readonly);
245 }
246 
247 static void file_backend_instance_finalize(Object *o)
248 {
249     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
250 
251     g_free(fb->mem_path);
252 }
253 
254 static const TypeInfo file_backend_info = {
255     .name = TYPE_MEMORY_BACKEND_FILE,
256     .parent = TYPE_MEMORY_BACKEND,
257     .class_init = file_backend_class_init,
258     .instance_finalize = file_backend_instance_finalize,
259     .instance_size = sizeof(HostMemoryBackendFile),
260 };
261 
262 static void register_types(void)
263 {
264     type_register_static(&file_backend_info);
265 }
266 
267 type_init(register_types);
268