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