/src/qemu/backends/hostmem-file.c
Line | Count | Source |
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 "system/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 | 0 | { |
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 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); |
48 | 0 | g_autofree gchar *name = NULL; |
49 | 0 | uint32_t ram_flags; |
50 | |
|
51 | 0 | if (!backend->size) { |
52 | 0 | error_setg(errp, "can't create backend with size 0"); |
53 | 0 | return false; |
54 | 0 | } |
55 | 0 | if (!fb->mem_path) { |
56 | 0 | error_setg(errp, "mem-path property not set"); |
57 | 0 | return false; |
58 | 0 | } |
59 | | |
60 | 0 | switch (fb->rom) { |
61 | 0 | case ON_OFF_AUTO_AUTO: |
62 | | /* Traditionally, opening the file readonly always resulted in ROM. */ |
63 | 0 | fb->rom = fb->readonly ? ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF; |
64 | 0 | break; |
65 | 0 | case ON_OFF_AUTO_ON: |
66 | 0 | if (!fb->readonly) { |
67 | 0 | error_setg(errp, "property 'rom' = 'on' is not supported with" |
68 | 0 | " 'readonly' = 'off'"); |
69 | 0 | return false; |
70 | 0 | } |
71 | 0 | break; |
72 | 0 | case ON_OFF_AUTO_OFF: |
73 | 0 | if (fb->readonly && backend->share) { |
74 | 0 | error_setg(errp, "property 'rom' = 'off' is incompatible with" |
75 | 0 | " 'readonly' = 'on' and 'share' = 'on'"); |
76 | 0 | return false; |
77 | 0 | } |
78 | 0 | break; |
79 | 0 | default: |
80 | 0 | g_assert_not_reached(); |
81 | 0 | } |
82 | | |
83 | 0 | backend->aligned = true; |
84 | 0 | name = host_memory_backend_get_name(backend); |
85 | 0 | ram_flags = backend->share ? RAM_SHARED : RAM_PRIVATE; |
86 | 0 | ram_flags |= fb->readonly ? RAM_READONLY_FD : 0; |
87 | 0 | ram_flags |= fb->rom == ON_OFF_AUTO_ON ? RAM_READONLY : 0; |
88 | 0 | ram_flags |= backend->reserve ? 0 : RAM_NORESERVE; |
89 | 0 | ram_flags |= backend->guest_memfd_private ? RAM_GUEST_MEMFD_PRIVATE : 0; |
90 | 0 | ram_flags |= fb->is_pmem ? RAM_PMEM : 0; |
91 | 0 | ram_flags |= RAM_NAMED_FILE; |
92 | 0 | return memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name, |
93 | 0 | backend->size, fb->align, ram_flags, |
94 | 0 | fb->mem_path, fb->offset, errp); |
95 | 0 | #endif |
96 | 0 | } |
97 | | |
98 | | static char *get_mem_path(Object *o, Error **errp) |
99 | 0 | { |
100 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
101 | |
|
102 | 0 | return g_strdup(fb->mem_path); |
103 | 0 | } |
104 | | |
105 | | static void set_mem_path(Object *o, const char *str, Error **errp) |
106 | 0 | { |
107 | 0 | HostMemoryBackend *backend = MEMORY_BACKEND(o); |
108 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
109 | |
|
110 | 0 | if (host_memory_backend_mr_inited(backend)) { |
111 | 0 | error_setg(errp, "cannot change property 'mem-path' of %s", |
112 | 0 | object_get_typename(o)); |
113 | 0 | return; |
114 | 0 | } |
115 | 0 | g_free(fb->mem_path); |
116 | 0 | fb->mem_path = g_strdup(str); |
117 | 0 | } |
118 | | |
119 | | static bool file_memory_backend_get_discard_data(Object *o, Error **errp) |
120 | 0 | { |
121 | 0 | return MEMORY_BACKEND_FILE(o)->discard_data; |
122 | 0 | } |
123 | | |
124 | | static void file_memory_backend_set_discard_data(Object *o, bool value, |
125 | | Error **errp) |
126 | 0 | { |
127 | 0 | MEMORY_BACKEND_FILE(o)->discard_data = value; |
128 | 0 | } |
129 | | |
130 | | static void file_memory_backend_get_align(Object *o, Visitor *v, |
131 | | const char *name, void *opaque, |
132 | | Error **errp) |
133 | 0 | { |
134 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
135 | 0 | uint64_t val = fb->align; |
136 | |
|
137 | 0 | visit_type_uint64(v, name, &val, errp); |
138 | 0 | } |
139 | | |
140 | | static void file_memory_backend_set_align(Object *o, Visitor *v, |
141 | | const char *name, void *opaque, |
142 | | Error **errp) |
143 | 0 | { |
144 | 0 | HostMemoryBackend *backend = MEMORY_BACKEND(o); |
145 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
146 | 0 | uint64_t val; |
147 | |
|
148 | 0 | if (host_memory_backend_mr_inited(backend)) { |
149 | 0 | error_setg(errp, "cannot change property '%s' of %s", name, |
150 | 0 | object_get_typename(o)); |
151 | 0 | return; |
152 | 0 | } |
153 | | |
154 | 0 | if (!visit_type_uint64(v, name, &val, errp)) { |
155 | 0 | return; |
156 | 0 | } |
157 | 0 | fb->align = val; |
158 | 0 | } |
159 | | |
160 | | static void file_memory_backend_get_offset(Object *o, Visitor *v, |
161 | | const char *name, void *opaque, |
162 | | Error **errp) |
163 | 0 | { |
164 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
165 | 0 | uint64_t val = fb->offset; |
166 | |
|
167 | 0 | visit_type_size(v, name, &val, errp); |
168 | 0 | } |
169 | | |
170 | | static void file_memory_backend_set_offset(Object *o, Visitor *v, |
171 | | const char *name, void *opaque, |
172 | | Error **errp) |
173 | 0 | { |
174 | 0 | HostMemoryBackend *backend = MEMORY_BACKEND(o); |
175 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
176 | 0 | uint64_t val; |
177 | |
|
178 | 0 | if (host_memory_backend_mr_inited(backend)) { |
179 | 0 | error_setg(errp, "cannot change property '%s' of %s", name, |
180 | 0 | object_get_typename(o)); |
181 | 0 | return; |
182 | 0 | } |
183 | | |
184 | 0 | if (!visit_type_size(v, name, &val, errp)) { |
185 | 0 | return; |
186 | 0 | } |
187 | 0 | fb->offset = val; |
188 | 0 | } |
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 | 0 | { |
213 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); |
214 | |
|
215 | 0 | return fb->readonly; |
216 | 0 | } |
217 | | |
218 | | static void file_memory_backend_set_readonly(Object *obj, bool value, |
219 | | Error **errp) |
220 | 0 | { |
221 | 0 | HostMemoryBackend *backend = MEMORY_BACKEND(obj); |
222 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); |
223 | |
|
224 | 0 | if (host_memory_backend_mr_inited(backend)) { |
225 | 0 | error_setg(errp, "cannot change property 'readonly' of %s.", |
226 | 0 | object_get_typename(obj)); |
227 | 0 | return; |
228 | 0 | } |
229 | | |
230 | 0 | fb->readonly = value; |
231 | 0 | } |
232 | | |
233 | | static void file_memory_backend_get_rom(Object *obj, Visitor *v, |
234 | | const char *name, void *opaque, |
235 | | Error **errp) |
236 | 0 | { |
237 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); |
238 | 0 | OnOffAuto rom = fb->rom; |
239 | |
|
240 | 0 | visit_type_OnOffAuto(v, name, &rom, errp); |
241 | 0 | } |
242 | | |
243 | | static void file_memory_backend_set_rom(Object *obj, Visitor *v, |
244 | | const char *name, void *opaque, |
245 | | Error **errp) |
246 | 0 | { |
247 | 0 | HostMemoryBackend *backend = MEMORY_BACKEND(obj); |
248 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); |
249 | |
|
250 | 0 | if (host_memory_backend_mr_inited(backend)) { |
251 | 0 | error_setg(errp, "cannot change property '%s' of %s.", name, |
252 | 0 | object_get_typename(obj)); |
253 | 0 | return; |
254 | 0 | } |
255 | | |
256 | 0 | visit_type_OnOffAuto(v, name, &fb->rom, errp); |
257 | 0 | } |
258 | | |
259 | | static void file_backend_unparent(Object *obj) |
260 | 0 | { |
261 | 0 | HostMemoryBackend *backend = MEMORY_BACKEND(obj); |
262 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); |
263 | |
|
264 | 0 | if (host_memory_backend_mr_inited(backend) && fb->discard_data) { |
265 | 0 | void *ptr = memory_region_get_ram_ptr(&backend->mr); |
266 | 0 | uint64_t sz = memory_region_size(&backend->mr); |
267 | |
|
268 | 0 | qemu_madvise(ptr, sz, QEMU_MADV_REMOVE); |
269 | 0 | } |
270 | 0 | } |
271 | | |
272 | | static void |
273 | | file_backend_class_init(ObjectClass *oc, const void *data) |
274 | 61 | { |
275 | 61 | HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); |
276 | | |
277 | 61 | bc->alloc = file_backend_memory_alloc; |
278 | 61 | oc->unparent = file_backend_unparent; |
279 | | |
280 | 61 | object_class_property_add_bool(oc, "discard-data", |
281 | 61 | file_memory_backend_get_discard_data, file_memory_backend_set_discard_data); |
282 | 61 | object_class_property_add_str(oc, "mem-path", |
283 | 61 | get_mem_path, set_mem_path); |
284 | 61 | object_class_property_add(oc, "align", "uint64", |
285 | 61 | file_memory_backend_get_align, |
286 | 61 | file_memory_backend_set_align, |
287 | 61 | NULL, NULL); |
288 | 61 | object_class_property_add(oc, "offset", "int", |
289 | 61 | file_memory_backend_get_offset, |
290 | 61 | file_memory_backend_set_offset, |
291 | 61 | NULL, NULL); |
292 | 61 | object_class_property_set_description(oc, "offset", |
293 | 61 | "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 | 61 | object_class_property_add_bool(oc, "readonly", |
299 | 61 | file_memory_backend_get_readonly, |
300 | 61 | file_memory_backend_set_readonly); |
301 | 61 | object_class_property_add(oc, "rom", "OnOffAuto", |
302 | 61 | file_memory_backend_get_rom, file_memory_backend_set_rom, NULL, NULL); |
303 | 61 | object_class_property_set_description(oc, "rom", |
304 | 61 | "Whether to create Read Only Memory (ROM)"); |
305 | 61 | } |
306 | | |
307 | | static void file_backend_instance_finalize(Object *o) |
308 | 0 | { |
309 | 0 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
310 | |
|
311 | 0 | g_free(fb->mem_path); |
312 | 0 | } |
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 | 61 | { |
324 | 61 | type_register_static(&file_backend_info); |
325 | 61 | } |
326 | | |
327 | | type_init(register_types); |