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