xref: /openbmc/qemu/backends/hostmem-file.c (revision 6e2e2e8a)
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 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile, MEMORY_BACKEND_FILE)
23 
24 
25 struct HostMemoryBackendFile {
26     HostMemoryBackend parent_obj;
27 
28     char *mem_path;
29     uint64_t align;
30     bool discard_data;
31     bool is_pmem;
32 };
33 
34 static void
35 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
36 {
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;
43 
44     if (!backend->size) {
45         error_setg(errp, "can't create backend with size 0");
46         return;
47     }
48     if (!fb->mem_path) {
49         error_setg(errp, "mem-path property not set");
50         return;
51     }
52 
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
62 }
63 
64 static char *get_mem_path(Object *o, Error **errp)
65 {
66     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
67 
68     return g_strdup(fb->mem_path);
69 }
70 
71 static void set_mem_path(Object *o, const char *str, Error **errp)
72 {
73     HostMemoryBackend *backend = MEMORY_BACKEND(o);
74     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
75 
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;
80     }
81     g_free(fb->mem_path);
82     fb->mem_path = g_strdup(str);
83 }
84 
85 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
86 {
87     return MEMORY_BACKEND_FILE(o)->discard_data;
88 }
89 
90 static void file_memory_backend_set_discard_data(Object *o, bool value,
91                                                Error **errp)
92 {
93     MEMORY_BACKEND_FILE(o)->discard_data = value;
94 }
95 
96 static void file_memory_backend_get_align(Object *o, Visitor *v,
97                                           const char *name, void *opaque,
98                                           Error **errp)
99 {
100     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
101     uint64_t val = fb->align;
102 
103     visit_type_size(v, name, &val, errp);
104 }
105 
106 static void file_memory_backend_set_align(Object *o, Visitor *v,
107                                           const char *name, void *opaque,
108                                           Error **errp)
109 {
110     HostMemoryBackend *backend = MEMORY_BACKEND(o);
111     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
112     uint64_t val;
113 
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;
118     }
119 
120     if (!visit_type_size(v, name, &val, errp)) {
121         return;
122     }
123     fb->align = val;
124 }
125 
126 static bool file_memory_backend_get_pmem(Object *o, Error **errp)
127 {
128     return MEMORY_BACKEND_FILE(o)->is_pmem;
129 }
130 
131 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp)
132 {
133     HostMemoryBackend *backend = MEMORY_BACKEND(o);
134     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
135 
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;
140     }
141 
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;
148     }
149 #endif
150 
151     fb->is_pmem = value;
152 }
153 
154 static void file_backend_unparent(Object *obj)
155 {
156     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
157     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
158 
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);
162 
163         qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
164     }
165 }
166 
167 static void
168 file_backend_class_init(ObjectClass *oc, void *data)
169 {
170     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
171 
172     bc->alloc = file_backend_memory_alloc;
173     oc->unparent = file_backend_unparent;
174 
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);
185 }
186 
187 static void file_backend_instance_finalize(Object *o)
188 {
189     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
190 
191     g_free(fb->mem_path);
192 }
193 
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),
200 };
201 
202 static void register_types(void)
203 {
204     type_register_static(&file_backend_info);
205 }
206 
207 type_init(register_types);
208