s390x/tcg: Fix RISBHG
[qemu/ar7.git] / backends / hostmem-file.c
blob40e1e5b3e3bb2d9ee6b83a6d5d38bea8bf7884a2
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 "sysemu/hostmem.h"
18 #include "sysemu/sysemu.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 bool discard_data;
31 bool is_pmem;
34 static void
35 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
37 #ifndef CONFIG_POSIX
38 error_setg(errp, "backend '%s' not supported on this host",
39 object_get_typename(OBJECT(backend)));
40 #else
41 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
42 gchar *name;
44 if (!backend->size) {
45 error_setg(errp, "can't create backend with size 0");
46 return;
48 if (!fb->mem_path) {
49 error_setg(errp, "mem-path property not set");
50 return;
53 name = host_memory_backend_get_name(backend);
54 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
55 name,
56 backend->size, fb->align,
57 (backend->share ? RAM_SHARED : 0) |
58 (fb->is_pmem ? RAM_PMEM : 0),
59 fb->mem_path, errp);
60 g_free(name);
61 #endif
64 static char *get_mem_path(Object *o, Error **errp)
66 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
68 return g_strdup(fb->mem_path);
71 static void set_mem_path(Object *o, const char *str, Error **errp)
73 HostMemoryBackend *backend = MEMORY_BACKEND(o);
74 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
76 if (host_memory_backend_mr_inited(backend)) {
77 error_setg(errp, "cannot change property 'mem-path' of %s",
78 object_get_typename(o));
79 return;
81 g_free(fb->mem_path);
82 fb->mem_path = g_strdup(str);
85 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
87 return MEMORY_BACKEND_FILE(o)->discard_data;
90 static void file_memory_backend_set_discard_data(Object *o, bool value,
91 Error **errp)
93 MEMORY_BACKEND_FILE(o)->discard_data = value;
96 static void file_memory_backend_get_align(Object *o, Visitor *v,
97 const char *name, void *opaque,
98 Error **errp)
100 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
101 uint64_t val = fb->align;
103 visit_type_size(v, name, &val, errp);
106 static void file_memory_backend_set_align(Object *o, Visitor *v,
107 const char *name, void *opaque,
108 Error **errp)
110 HostMemoryBackend *backend = MEMORY_BACKEND(o);
111 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
112 uint64_t val;
114 if (host_memory_backend_mr_inited(backend)) {
115 error_setg(errp, "cannot change property '%s' of %s", name,
116 object_get_typename(o));
117 return;
120 if (!visit_type_size(v, name, &val, errp)) {
121 return;
123 fb->align = val;
126 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
128 return MEMORY_BACKEND_FILE(o)->is_pmem;
131 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
133 HostMemoryBackend *backend = MEMORY_BACKEND(o);
134 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
136 if (host_memory_backend_mr_inited(backend)) {
137 error_setg(errp, "cannot change property 'pmem' of %s.",
138 object_get_typename(o));
139 return;
142 #ifndef CONFIG_LIBPMEM
143 if (value) {
144 error_setg(errp, "Lack of libpmem support while setting the 'pmem=on'"
145 " of %s. We can't ensure data persistence.",
146 object_get_typename(o));
147 return;
149 #endif
151 fb->is_pmem = value;
154 static void file_backend_unparent(Object *obj)
156 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
157 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
159 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
160 void *ptr = memory_region_get_ram_ptr(&backend->mr);
161 uint64_t sz = memory_region_size(&backend->mr);
163 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
167 static void
168 file_backend_class_init(ObjectClass *oc, void *data)
170 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
172 bc->alloc = file_backend_memory_alloc;
173 oc->unparent = file_backend_unparent;
175 object_class_property_add_bool(oc, "discard-data",
176 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data);
177 object_class_property_add_str(oc, "mem-path",
178 get_mem_path, set_mem_path);
179 object_class_property_add(oc, "align", "int",
180 file_memory_backend_get_align,
181 file_memory_backend_set_align,
182 NULL, NULL);
183 object_class_property_add_bool(oc, "pmem",
184 file_memory_backend_get_pmem, file_memory_backend_set_pmem);
187 static void file_backend_instance_finalize(Object *o)
189 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
191 g_free(fb->mem_path);
194 static const TypeInfo file_backend_info = {
195 .name = TYPE_MEMORY_BACKEND_FILE,
196 .parent = TYPE_MEMORY_BACKEND,
197 .class_init = file_backend_class_init,
198 .instance_finalize = file_backend_instance_finalize,
199 .instance_size = sizeof(HostMemoryBackendFile),
202 static void register_types(void)
204 type_register_static(&file_backend_info);
207 type_init(register_types);