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
;
35 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
38 error_setg(errp
, "backend '%s' not supported on this host",
39 object_get_typename(OBJECT(backend
)));
41 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(backend
);
45 error_setg(errp
, "can't create backend with size 0");
49 error_setg(errp
, "mem-path property not set");
53 name
= host_memory_backend_get_name(backend
);
54 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
56 backend
->size
, fb
->align
,
57 (backend
->share
? RAM_SHARED
: 0) |
58 (fb
->is_pmem
? RAM_PMEM
: 0),
64 static char *get_mem_path(Object
*o
, Error
**errp
)
66 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
68 return g_strdup(fb
->mem_path
);
71 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
73 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
74 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
76 if (host_memory_backend_mr_inited(backend
)) {
77 error_setg(errp
, "cannot change property 'mem-path' of %s",
78 object_get_typename(o
));
82 fb
->mem_path
= g_strdup(str
);
85 static bool file_memory_backend_get_discard_data(Object
*o
, Error
**errp
)
87 return MEMORY_BACKEND_FILE(o
)->discard_data
;
90 static void file_memory_backend_set_discard_data(Object
*o
, bool value
,
93 MEMORY_BACKEND_FILE(o
)->discard_data
= value
;
96 static void file_memory_backend_get_align(Object
*o
, Visitor
*v
,
97 const char *name
, void *opaque
,
100 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
101 uint64_t val
= fb
->align
;
103 visit_type_size(v
, name
, &val
, errp
);
106 static void file_memory_backend_set_align(Object
*o
, Visitor
*v
,
107 const char *name
, void *opaque
,
110 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
111 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
114 if (host_memory_backend_mr_inited(backend
)) {
115 error_setg(errp
, "cannot change property '%s' of %s", name
,
116 object_get_typename(o
));
120 if (!visit_type_size(v
, name
, &val
, errp
)) {
126 static bool file_memory_backend_get_pmem(Object
*o
, Error
**errp
)
128 return MEMORY_BACKEND_FILE(o
)->is_pmem
;
131 static void file_memory_backend_set_pmem(Object
*o
, bool value
, Error
**errp
)
133 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
134 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
136 if (host_memory_backend_mr_inited(backend
)) {
137 error_setg(errp
, "cannot change property 'pmem' of %s.",
138 object_get_typename(o
));
142 #ifndef CONFIG_LIBPMEM
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
));
154 static void file_backend_unparent(Object
*obj
)
156 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
157 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
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
);
163 qemu_madvise(ptr
, sz
, QEMU_MADV_REMOVE
);
168 file_backend_class_init(ObjectClass
*oc
, void *data
)
170 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
172 bc
->alloc
= file_backend_memory_alloc
;
173 oc
->unparent
= file_backend_unparent
;
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
,
183 object_class_property_add_bool(oc
, "pmem",
184 file_memory_backend_get_pmem
, file_memory_backend_set_pmem
);
187 static void file_backend_instance_finalize(Object
*o
)
189 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
191 g_free(fb
->mem_path
);
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
),
202 static void register_types(void)
204 type_register_static(&file_backend_info
);
207 type_init(register_types
);