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 typedef struct HostMemoryBackendFile HostMemoryBackendFile
;
23 DECLARE_INSTANCE_CHECKER(HostMemoryBackendFile
, MEMORY_BACKEND_FILE
,
24 TYPE_MEMORY_BACKEND_FILE
)
27 struct HostMemoryBackendFile
{
28 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
);
47 error_setg(errp
, "can't create backend with size 0");
51 error_setg(errp
, "mem-path property not set");
55 name
= host_memory_backend_get_name(backend
);
56 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
58 backend
->size
, fb
->align
,
59 (backend
->share
? RAM_SHARED
: 0) |
60 (fb
->is_pmem
? RAM_PMEM
: 0),
66 static char *get_mem_path(Object
*o
, Error
**errp
)
68 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
70 return g_strdup(fb
->mem_path
);
73 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
75 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
76 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
78 if (host_memory_backend_mr_inited(backend
)) {
79 error_setg(errp
, "cannot change property 'mem-path' of %s",
80 object_get_typename(o
));
84 fb
->mem_path
= g_strdup(str
);
87 static bool file_memory_backend_get_discard_data(Object
*o
, Error
**errp
)
89 return MEMORY_BACKEND_FILE(o
)->discard_data
;
92 static void file_memory_backend_set_discard_data(Object
*o
, bool value
,
95 MEMORY_BACKEND_FILE(o
)->discard_data
= value
;
98 static void file_memory_backend_get_align(Object
*o
, Visitor
*v
,
99 const char *name
, void *opaque
,
102 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
103 uint64_t val
= fb
->align
;
105 visit_type_size(v
, name
, &val
, errp
);
108 static void file_memory_backend_set_align(Object
*o
, Visitor
*v
,
109 const char *name
, void *opaque
,
112 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
113 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
116 if (host_memory_backend_mr_inited(backend
)) {
117 error_setg(errp
, "cannot change property '%s' of %s", name
,
118 object_get_typename(o
));
122 if (!visit_type_size(v
, name
, &val
, errp
)) {
128 static bool file_memory_backend_get_pmem(Object
*o
, Error
**errp
)
130 return MEMORY_BACKEND_FILE(o
)->is_pmem
;
133 static void file_memory_backend_set_pmem(Object
*o
, bool value
, Error
**errp
)
135 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
136 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
138 if (host_memory_backend_mr_inited(backend
)) {
139 error_setg(errp
, "cannot change property 'pmem' of %s.",
140 object_get_typename(o
));
144 #ifndef CONFIG_LIBPMEM
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
));
156 static void file_backend_unparent(Object
*obj
)
158 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
159 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
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
);
165 qemu_madvise(ptr
, sz
, QEMU_MADV_REMOVE
);
170 file_backend_class_init(ObjectClass
*oc
, void *data
)
172 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
174 bc
->alloc
= file_backend_memory_alloc
;
175 oc
->unparent
= file_backend_unparent
;
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
,
185 object_class_property_add_bool(oc
, "pmem",
186 file_memory_backend_get_pmem
, file_memory_backend_set_pmem
);
189 static void file_backend_instance_finalize(Object
*o
)
191 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
193 g_free(fb
->mem_path
);
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
),
204 static void register_types(void)
206 type_register_static(&file_backend_info
);
209 type_init(register_types
);