2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
7 * Paolo Bonzini <pbonzini@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/error-report.h"
16 #include "qemu/module.h"
17 #include "qemu/madvise.h"
18 #include "sysemu/hostmem.h"
19 #include "qom/object_interfaces.h"
20 #include "qom/object.h"
22 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile
, MEMORY_BACKEND_FILE
)
25 struct HostMemoryBackendFile
{
26 HostMemoryBackend parent_obj
;
37 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
40 error_setg(errp
, "backend '%s' not supported on this host",
41 object_get_typename(OBJECT(backend
)));
43 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(backend
);
48 error_setg(errp
, "can't create backend with size 0");
52 error_setg(errp
, "mem-path property not set");
56 name
= host_memory_backend_get_name(backend
);
57 ram_flags
= backend
->share
? RAM_SHARED
: 0;
58 ram_flags
|= backend
->reserve
? 0 : RAM_NORESERVE
;
59 ram_flags
|= fb
->is_pmem
? RAM_PMEM
: 0;
60 ram_flags
|= RAM_NAMED_FILE
;
61 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
), name
,
62 backend
->size
, fb
->align
, ram_flags
,
63 fb
->mem_path
, fb
->offset
, fb
->readonly
,
69 static char *get_mem_path(Object
*o
, Error
**errp
)
71 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
73 return g_strdup(fb
->mem_path
);
76 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
78 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
79 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
81 if (host_memory_backend_mr_inited(backend
)) {
82 error_setg(errp
, "cannot change property 'mem-path' of %s",
83 object_get_typename(o
));
87 fb
->mem_path
= g_strdup(str
);
90 static bool file_memory_backend_get_discard_data(Object
*o
, Error
**errp
)
92 return MEMORY_BACKEND_FILE(o
)->discard_data
;
95 static void file_memory_backend_set_discard_data(Object
*o
, bool value
,
98 MEMORY_BACKEND_FILE(o
)->discard_data
= value
;
101 static void file_memory_backend_get_align(Object
*o
, Visitor
*v
,
102 const char *name
, void *opaque
,
105 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
106 uint64_t val
= fb
->align
;
108 visit_type_size(v
, name
, &val
, errp
);
111 static void file_memory_backend_set_align(Object
*o
, Visitor
*v
,
112 const char *name
, void *opaque
,
115 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
116 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
119 if (host_memory_backend_mr_inited(backend
)) {
120 error_setg(errp
, "cannot change property '%s' of %s", name
,
121 object_get_typename(o
));
125 if (!visit_type_size(v
, name
, &val
, errp
)) {
131 static void file_memory_backend_get_offset(Object
*o
, Visitor
*v
,
132 const char *name
, void *opaque
,
135 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
136 uint64_t val
= fb
->offset
;
138 visit_type_size(v
, name
, &val
, errp
);
141 static void file_memory_backend_set_offset(Object
*o
, Visitor
*v
,
142 const char *name
, void *opaque
,
145 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
146 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
149 if (host_memory_backend_mr_inited(backend
)) {
150 error_setg(errp
, "cannot change property '%s' of %s", name
,
151 object_get_typename(o
));
155 if (!visit_type_size(v
, name
, &val
, errp
)) {
161 #ifdef CONFIG_LIBPMEM
162 static bool file_memory_backend_get_pmem(Object
*o
, Error
**errp
)
164 return MEMORY_BACKEND_FILE(o
)->is_pmem
;
167 static void file_memory_backend_set_pmem(Object
*o
, bool value
, Error
**errp
)
169 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
170 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
172 if (host_memory_backend_mr_inited(backend
)) {
173 error_setg(errp
, "cannot change property 'pmem' of %s.",
174 object_get_typename(o
));
180 #endif /* CONFIG_LIBPMEM */
182 static bool file_memory_backend_get_readonly(Object
*obj
, Error
**errp
)
184 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
189 static void file_memory_backend_set_readonly(Object
*obj
, bool value
,
192 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
193 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
195 if (host_memory_backend_mr_inited(backend
)) {
196 error_setg(errp
, "cannot change property 'readonly' of %s.",
197 object_get_typename(obj
));
201 fb
->readonly
= value
;
204 static void file_backend_unparent(Object
*obj
)
206 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
207 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
209 if (host_memory_backend_mr_inited(backend
) && fb
->discard_data
) {
210 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
211 uint64_t sz
= memory_region_size(&backend
->mr
);
213 qemu_madvise(ptr
, sz
, QEMU_MADV_REMOVE
);
218 file_backend_class_init(ObjectClass
*oc
, void *data
)
220 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
222 bc
->alloc
= file_backend_memory_alloc
;
223 oc
->unparent
= file_backend_unparent
;
225 object_class_property_add_bool(oc
, "discard-data",
226 file_memory_backend_get_discard_data
, file_memory_backend_set_discard_data
);
227 object_class_property_add_str(oc
, "mem-path",
228 get_mem_path
, set_mem_path
);
229 object_class_property_add(oc
, "align", "int",
230 file_memory_backend_get_align
,
231 file_memory_backend_set_align
,
233 object_class_property_add(oc
, "offset", "int",
234 file_memory_backend_get_offset
,
235 file_memory_backend_set_offset
,
237 object_class_property_set_description(oc
, "offset",
238 "Offset into the target file (ex: 1G)");
239 #ifdef CONFIG_LIBPMEM
240 object_class_property_add_bool(oc
, "pmem",
241 file_memory_backend_get_pmem
, file_memory_backend_set_pmem
);
243 object_class_property_add_bool(oc
, "readonly",
244 file_memory_backend_get_readonly
,
245 file_memory_backend_set_readonly
);
248 static void file_backend_instance_finalize(Object
*o
)
250 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
252 g_free(fb
->mem_path
);
255 static const TypeInfo file_backend_info
= {
256 .name
= TYPE_MEMORY_BACKEND_FILE
,
257 .parent
= TYPE_MEMORY_BACKEND
,
258 .class_init
= file_backend_class_init
,
259 .instance_finalize
= file_backend_instance_finalize
,
260 .instance_size
= sizeof(HostMemoryBackendFile
),
263 static void register_types(void)
265 type_register_static(&file_backend_info
);
268 type_init(register_types
);