target/i386: Remove unused KVM stubs
[qemu/armbru.git] / backends / hostmem-file.c
blobb4335a80e6da7d9c654f2935916fc191cae1436d
1 /*
2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
6 * Authors:
7 * Paolo Bonzini <pbonzini@redhat.com>
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.
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"
22 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile, MEMORY_BACKEND_FILE)
25 struct HostMemoryBackendFile {
26 HostMemoryBackend parent_obj;
28 char *mem_path;
29 uint64_t align;
30 uint64_t offset;
31 bool discard_data;
32 bool is_pmem;
33 bool readonly;
36 static void
37 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
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;
47 if (!backend->size) {
48 error_setg(errp, "can't create backend with size 0");
49 return;
51 if (!fb->mem_path) {
52 error_setg(errp, "mem-path property not set");
53 return;
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 ram_flags |= RAM_NAMED_FILE;
61 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), name,
62 backend->size, fb->align, ram_flags,
63 fb->mem_path, fb->offset, fb->readonly,
64 errp);
65 g_free(name);
66 #endif
69 static char *get_mem_path(Object *o, Error **errp)
71 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
73 return g_strdup(fb->mem_path);
76 static void set_mem_path(Object *o, const char *str, Error **errp)
78 HostMemoryBackend *backend = MEMORY_BACKEND(o);
79 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
81 if (host_memory_backend_mr_inited(backend)) {
82 error_setg(errp, "cannot change property 'mem-path' of %s",
83 object_get_typename(o));
84 return;
86 g_free(fb->mem_path);
87 fb->mem_path = g_strdup(str);
90 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
92 return MEMORY_BACKEND_FILE(o)->discard_data;
95 static void file_memory_backend_set_discard_data(Object *o, bool value,
96 Error **errp)
98 MEMORY_BACKEND_FILE(o)->discard_data = value;
101 static void file_memory_backend_get_align(Object *o, Visitor *v,
102 const char *name, void *opaque,
103 Error **errp)
105 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
106 uint64_t val = fb->align;
108 visit_type_size(v, name, &val, errp);
111 static void file_memory_backend_set_align(Object *o, Visitor *v,
112 const char *name, void *opaque,
113 Error **errp)
115 HostMemoryBackend *backend = MEMORY_BACKEND(o);
116 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
117 uint64_t val;
119 if (host_memory_backend_mr_inited(backend)) {
120 error_setg(errp, "cannot change property '%s' of %s", name,
121 object_get_typename(o));
122 return;
125 if (!visit_type_size(v, name, &val, errp)) {
126 return;
128 fb->align = val;
131 static void file_memory_backend_get_offset(Object *o, Visitor *v,
132 const char *name, void *opaque,
133 Error **errp)
135 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
136 uint64_t val = fb->offset;
138 visit_type_size(v, name, &val, errp);
141 static void file_memory_backend_set_offset(Object *o, Visitor *v,
142 const char *name, void *opaque,
143 Error **errp)
145 HostMemoryBackend *backend = MEMORY_BACKEND(o);
146 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
147 uint64_t val;
149 if (host_memory_backend_mr_inited(backend)) {
150 error_setg(errp, "cannot change property '%s' of %s", name,
151 object_get_typename(o));
152 return;
155 if (!visit_type_size(v, name, &val, errp)) {
156 return;
158 fb->offset = val;
161 #ifdef CONFIG_LIBPMEM
162 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
164 return MEMORY_BACKEND_FILE(o)->is_pmem;
167 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
169 HostMemoryBackend *backend = MEMORY_BACKEND(o);
170 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
172 if (host_memory_backend_mr_inited(backend)) {
173 error_setg(errp, "cannot change property 'pmem' of %s.",
174 object_get_typename(o));
175 return;
178 fb->is_pmem = value;
180 #endif /* CONFIG_LIBPMEM */
182 static bool file_memory_backend_get_readonly(Object *obj, Error **errp)
184 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
186 return fb->readonly;
189 static void file_memory_backend_set_readonly(Object *obj, bool value,
190 Error **errp)
192 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
193 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
195 if (host_memory_backend_mr_inited(backend)) {
196 error_setg(errp, "cannot change property 'readonly' of %s.",
197 object_get_typename(obj));
198 return;
201 fb->readonly = value;
204 static void file_backend_unparent(Object *obj)
206 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
207 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
209 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
210 void *ptr = memory_region_get_ram_ptr(&backend->mr);
211 uint64_t sz = memory_region_size(&backend->mr);
213 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
217 static void
218 file_backend_class_init(ObjectClass *oc, void *data)
220 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
222 bc->alloc = file_backend_memory_alloc;
223 oc->unparent = file_backend_unparent;
225 object_class_property_add_bool(oc, "discard-data",
226 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data);
227 object_class_property_add_str(oc, "mem-path",
228 get_mem_path, set_mem_path);
229 object_class_property_add(oc, "align", "int",
230 file_memory_backend_get_align,
231 file_memory_backend_set_align,
232 NULL, NULL);
233 object_class_property_add(oc, "offset", "int",
234 file_memory_backend_get_offset,
235 file_memory_backend_set_offset,
236 NULL, NULL);
237 object_class_property_set_description(oc, "offset",
238 "Offset into the target file (ex: 1G)");
239 #ifdef CONFIG_LIBPMEM
240 object_class_property_add_bool(oc, "pmem",
241 file_memory_backend_get_pmem, file_memory_backend_set_pmem);
242 #endif
243 object_class_property_add_bool(oc, "readonly",
244 file_memory_backend_get_readonly,
245 file_memory_backend_set_readonly);
248 static void file_backend_instance_finalize(Object *o)
250 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
252 g_free(fb->mem_path);
255 static const TypeInfo file_backend_info = {
256 .name = TYPE_MEMORY_BACKEND_FILE,
257 .parent = TYPE_MEMORY_BACKEND,
258 .class_init = file_backend_class_init,
259 .instance_finalize = file_backend_instance_finalize,
260 .instance_size = sizeof(HostMemoryBackendFile),
263 static void register_types(void)
265 type_register_static(&file_backend_info);
268 type_init(register_types);