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 "sysemu/hostmem.h"
18 #include "sysemu/sysemu.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
;
36 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
39 error_setg(errp
, "backend '%s' not supported on this host",
40 object_get_typename(OBJECT(backend
)));
42 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(backend
);
46 error_setg(errp
, "can't create backend with size 0");
50 error_setg(errp
, "mem-path property not set");
54 name
= host_memory_backend_get_name(backend
);
55 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
57 backend
->size
, fb
->align
,
58 (backend
->share
? RAM_SHARED
: 0) |
59 (fb
->is_pmem
? RAM_PMEM
: 0),
60 fb
->mem_path
, fb
->readonly
, errp
);
65 static char *get_mem_path(Object
*o
, Error
**errp
)
67 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
69 return g_strdup(fb
->mem_path
);
72 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
74 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
75 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
77 if (host_memory_backend_mr_inited(backend
)) {
78 error_setg(errp
, "cannot change property 'mem-path' of %s",
79 object_get_typename(o
));
83 fb
->mem_path
= g_strdup(str
);
86 static bool file_memory_backend_get_discard_data(Object
*o
, Error
**errp
)
88 return MEMORY_BACKEND_FILE(o
)->discard_data
;
91 static void file_memory_backend_set_discard_data(Object
*o
, bool value
,
94 MEMORY_BACKEND_FILE(o
)->discard_data
= value
;
97 static void file_memory_backend_get_align(Object
*o
, Visitor
*v
,
98 const char *name
, void *opaque
,
101 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
102 uint64_t val
= fb
->align
;
104 visit_type_size(v
, name
, &val
, errp
);
107 static void file_memory_backend_set_align(Object
*o
, Visitor
*v
,
108 const char *name
, void *opaque
,
111 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
112 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
115 if (host_memory_backend_mr_inited(backend
)) {
116 error_setg(errp
, "cannot change property '%s' of %s", name
,
117 object_get_typename(o
));
121 if (!visit_type_size(v
, name
, &val
, errp
)) {
127 static bool file_memory_backend_get_pmem(Object
*o
, Error
**errp
)
129 return MEMORY_BACKEND_FILE(o
)->is_pmem
;
132 static void file_memory_backend_set_pmem(Object
*o
, bool value
, Error
**errp
)
134 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
135 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
137 if (host_memory_backend_mr_inited(backend
)) {
138 error_setg(errp
, "cannot change property 'pmem' of %s.",
139 object_get_typename(o
));
143 #ifndef CONFIG_LIBPMEM
145 error_setg(errp
, "Lack of libpmem support while setting the 'pmem=on'"
146 " of %s. We can't ensure data persistence.",
147 object_get_typename(o
));
155 static bool file_memory_backend_get_readonly(Object
*obj
, Error
**errp
)
157 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
162 static void file_memory_backend_set_readonly(Object
*obj
, bool value
,
165 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
166 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
168 if (host_memory_backend_mr_inited(backend
)) {
169 error_setg(errp
, "cannot change property 'readonly' of %s.",
170 object_get_typename(obj
));
174 fb
->readonly
= value
;
177 static void file_backend_unparent(Object
*obj
)
179 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
180 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
182 if (host_memory_backend_mr_inited(backend
) && fb
->discard_data
) {
183 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
184 uint64_t sz
= memory_region_size(&backend
->mr
);
186 qemu_madvise(ptr
, sz
, QEMU_MADV_REMOVE
);
191 file_backend_class_init(ObjectClass
*oc
, void *data
)
193 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
195 bc
->alloc
= file_backend_memory_alloc
;
196 oc
->unparent
= file_backend_unparent
;
198 object_class_property_add_bool(oc
, "discard-data",
199 file_memory_backend_get_discard_data
, file_memory_backend_set_discard_data
);
200 object_class_property_add_str(oc
, "mem-path",
201 get_mem_path
, set_mem_path
);
202 object_class_property_add(oc
, "align", "int",
203 file_memory_backend_get_align
,
204 file_memory_backend_set_align
,
206 object_class_property_add_bool(oc
, "pmem",
207 file_memory_backend_get_pmem
, file_memory_backend_set_pmem
);
208 object_class_property_add_bool(oc
, "readonly",
209 file_memory_backend_get_readonly
,
210 file_memory_backend_set_readonly
);
213 static void file_backend_instance_finalize(Object
*o
)
215 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
217 g_free(fb
->mem_path
);
220 static const TypeInfo file_backend_info
= {
221 .name
= TYPE_MEMORY_BACKEND_FILE
,
222 .parent
= TYPE_MEMORY_BACKEND
,
223 .class_init
= file_backend_class_init
,
224 .instance_finalize
= file_backend_instance_finalize
,
225 .instance_size
= sizeof(HostMemoryBackendFile
),
228 static void register_types(void)
230 type_register_static(&file_backend_info
);
233 type_init(register_types
);