xref: /openbmc/qemu/backends/hostmem-file.c (revision a78b1299)
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 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu-common.h"
15 #include "sysemu/hostmem.h"
16 #include "sysemu/sysemu.h"
17 #include "qom/object_interfaces.h"
18 
19 /* hostmem-file.c */
20 /**
21  * @TYPE_MEMORY_BACKEND_FILE:
22  * name of backend that uses mmap on a file descriptor
23  */
24 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
25 
26 #define MEMORY_BACKEND_FILE(obj) \
27     OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
28 
29 typedef struct HostMemoryBackendFile HostMemoryBackendFile;
30 
31 struct HostMemoryBackendFile {
32     HostMemoryBackend parent_obj;
33 
34     bool share;
35     bool discard_data;
36     char *mem_path;
37     uint64_t align;
38 };
39 
40 static void
41 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
42 {
43     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
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 #ifndef CONFIG_LINUX
54     error_setg(errp, "-mem-path not supported on this host");
55 #else
56     if (!host_memory_backend_mr_inited(backend)) {
57         gchar *path;
58         backend->force_prealloc = mem_prealloc;
59         path = object_get_canonical_path(OBJECT(backend));
60         memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
61                                  path,
62                                  backend->size, fb->align, fb->share,
63                                  fb->mem_path, errp);
64         g_free(path);
65     }
66 #endif
67 }
68 
69 static char *get_mem_path(Object *o, Error **errp)
70 {
71     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
72 
73     return g_strdup(fb->mem_path);
74 }
75 
76 static void set_mem_path(Object *o, const char *str, Error **errp)
77 {
78     HostMemoryBackend *backend = MEMORY_BACKEND(o);
79     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
80 
81     if (host_memory_backend_mr_inited(backend)) {
82         error_setg(errp, "cannot change property value");
83         return;
84     }
85     g_free(fb->mem_path);
86     fb->mem_path = g_strdup(str);
87 }
88 
89 static bool file_memory_backend_get_share(Object *o, Error **errp)
90 {
91     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
92 
93     return fb->share;
94 }
95 
96 static void file_memory_backend_set_share(Object *o, bool value, Error **errp)
97 {
98     HostMemoryBackend *backend = MEMORY_BACKEND(o);
99     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
100 
101     if (host_memory_backend_mr_inited(backend)) {
102         error_setg(errp, "cannot change property value");
103         return;
104     }
105     fb->share = value;
106 }
107 
108 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
109 {
110     return MEMORY_BACKEND_FILE(o)->discard_data;
111 }
112 
113 static void file_memory_backend_set_discard_data(Object *o, bool value,
114                                                Error **errp)
115 {
116     MEMORY_BACKEND_FILE(o)->discard_data = value;
117 }
118 
119 static void file_memory_backend_get_align(Object *o, Visitor *v,
120                                           const char *name, void *opaque,
121                                           Error **errp)
122 {
123     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
124     uint64_t val = fb->align;
125 
126     visit_type_size(v, name, &val, errp);
127 }
128 
129 static void file_memory_backend_set_align(Object *o, Visitor *v,
130                                           const char *name, void *opaque,
131                                           Error **errp)
132 {
133     HostMemoryBackend *backend = MEMORY_BACKEND(o);
134     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
135     Error *local_err = NULL;
136     uint64_t val;
137 
138     if (host_memory_backend_mr_inited(backend)) {
139         error_setg(&local_err, "cannot change property value");
140         goto out;
141     }
142 
143     visit_type_size(v, name, &val, &local_err);
144     if (local_err) {
145         goto out;
146     }
147     fb->align = val;
148 
149  out:
150     error_propagate(errp, local_err);
151 }
152 
153 static void file_backend_unparent(Object *obj)
154 {
155     HostMemoryBackend *backend = MEMORY_BACKEND(obj);
156     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
157 
158     if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
159         void *ptr = memory_region_get_ram_ptr(&backend->mr);
160         uint64_t sz = memory_region_size(&backend->mr);
161 
162         qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
163     }
164 }
165 
166 static void
167 file_backend_class_init(ObjectClass *oc, void *data)
168 {
169     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
170 
171     bc->alloc = file_backend_memory_alloc;
172     oc->unparent = file_backend_unparent;
173 
174     object_class_property_add_bool(oc, "share",
175         file_memory_backend_get_share, file_memory_backend_set_share,
176         &error_abort);
177     object_class_property_add_bool(oc, "discard-data",
178         file_memory_backend_get_discard_data, file_memory_backend_set_discard_data,
179         &error_abort);
180     object_class_property_add_str(oc, "mem-path",
181         get_mem_path, set_mem_path,
182         &error_abort);
183     object_class_property_add(oc, "align", "int",
184         file_memory_backend_get_align,
185         file_memory_backend_set_align,
186         NULL, NULL, &error_abort);
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