xref: /openbmc/qemu/backends/hostmem-file.c (revision f7160f32)
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 "sysemu/hostmem.h"
18 #include "sysemu/sysemu.h"
19 #include "qom/object_interfaces.h"
20 
21 #define MEMORY_BACKEND_FILE(obj) \
22     OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
23 
24 typedef struct HostMemoryBackendFile HostMemoryBackendFile;
25 
26 struct HostMemoryBackendFile {
27     HostMemoryBackend parent_obj;
28 
29     char *mem_path;
30     uint64_t align;
31     bool discard_data;
32     bool is_pmem;
33 };
34 
35 static void
36 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
37 {
38 #ifndef CONFIG_POSIX
39     error_setg(errp, "backend '%s' not supported on this host",
40                object_get_typename(OBJECT(backend)));
41 #else
42     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
43     gchar *name;
44 
45     if (!backend->size) {
46         error_setg(errp, "can't create backend with size 0");
47         return;
48     }
49     if (!fb->mem_path) {
50         error_setg(errp, "mem-path property not set");
51         return;
52     }
53 
54     name = host_memory_backend_get_name(backend);
55     memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
56                                      name,
57                                      backend->size, fb->align,
58                                      (backend->share ? RAM_SHARED : 0) |
59                                      (fb->is_pmem ? RAM_PMEM : 0),
60                                      fb->mem_path, errp);
61     g_free(name);
62 #endif
63 }
64 
65 static char *get_mem_path(Object *o, Error **errp)
66 {
67     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
68 
69     return g_strdup(fb->mem_path);
70 }
71 
72 static void set_mem_path(Object *o, const char *str, Error **errp)
73 {
74     HostMemoryBackend *backend = MEMORY_BACKEND(o);
75     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
76 
77     if (host_memory_backend_mr_inited(backend)) {
78         error_setg(errp, "cannot change property 'mem-path' of %s",
79                    object_get_typename(o));
80         return;
81     }
82     g_free(fb->mem_path);
83     fb->mem_path = g_strdup(str);
84 }
85 
86 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
87 {
88     return MEMORY_BACKEND_FILE(o)->discard_data;
89 }
90 
91 static void file_memory_backend_set_discard_data(Object *o, bool value,
92                                                Error **errp)
93 {
94     MEMORY_BACKEND_FILE(o)->discard_data = value;
95 }
96 
97 static void file_memory_backend_get_align(Object *o, Visitor *v,
98                                           const char *name, void *opaque,
99                                           Error **errp)
100 {
101     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
102     uint64_t val = fb->align;
103 
104     visit_type_size(v, name, &val, errp);
105 }
106 
107 static void file_memory_backend_set_align(Object *o, Visitor *v,
108                                           const char *name, void *opaque,
109                                           Error **errp)
110 {
111     HostMemoryBackend *backend = MEMORY_BACKEND(o);
112     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
113     uint64_t val;
114 
115     if (host_memory_backend_mr_inited(backend)) {
116         error_setg(errp, "cannot change property '%s' of %s", name,
117                    object_get_typename(o));
118         return;
119     }
120 
121     if (!visit_type_size(v, name, &val, errp)) {
122         return;
123     }
124     fb->align = val;
125 }
126 
127 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
128 {
129     return MEMORY_BACKEND_FILE(o)->is_pmem;
130 }
131 
132 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
133 {
134     HostMemoryBackend *backend = MEMORY_BACKEND(o);
135     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
136 
137     if (host_memory_backend_mr_inited(backend)) {
138         error_setg(errp, "cannot change property 'pmem' of %s.",
139                    object_get_typename(o));
140         return;
141     }
142 
143 #ifndef CONFIG_LIBPMEM
144     if (value) {
145         error_setg(errp, "Lack of libpmem support while setting the 'pmem=on'"
146                    " of %s. We can't ensure data persistence.",
147                    object_get_typename(o));
148         return;
149     }
150 #endif
151 
152     fb->is_pmem = value;
153 }
154 
155 static void file_backend_unparent(Object *obj)
156 {
157     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
158     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
159 
160     if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
161         void *ptr = memory_region_get_ram_ptr(&backend->mr);
162         uint64_t sz = memory_region_size(&backend->mr);
163 
164         qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
165     }
166 }
167 
168 static void
169 file_backend_class_init(ObjectClass *oc, void *data)
170 {
171     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
172 
173     bc->alloc = file_backend_memory_alloc;
174     oc->unparent = file_backend_unparent;
175 
176     object_class_property_add_bool(oc, "discard-data",
177         file_memory_backend_get_discard_data, file_memory_backend_set_discard_data);
178     object_class_property_add_str(oc, "mem-path",
179         get_mem_path, set_mem_path);
180     object_class_property_add(oc, "align", "int",
181         file_memory_backend_get_align,
182         file_memory_backend_set_align,
183         NULL, NULL);
184     object_class_property_add_bool(oc, "pmem",
185         file_memory_backend_get_pmem, file_memory_backend_set_pmem);
186 }
187 
188 static void file_backend_instance_finalize(Object *o)
189 {
190     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
191 
192     g_free(fb->mem_path);
193 }
194 
195 static const TypeInfo file_backend_info = {
196     .name = TYPE_MEMORY_BACKEND_FILE,
197     .parent = TYPE_MEMORY_BACKEND,
198     .class_init = file_backend_class_init,
199     .instance_finalize = file_backend_instance_finalize,
200     .instance_size = sizeof(HostMemoryBackendFile),
201 };
202 
203 static void register_types(void)
204 {
205     type_register_static(&file_backend_info);
206 }
207 
208 type_init(register_types);
209