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.
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu-common.h"
15 #include "qemu/error-report.h"
16 #include "sysemu/hostmem.h"
17 #include "sysemu/sysemu.h"
18 #include "qom/object_interfaces.h"
22 * @TYPE_MEMORY_BACKEND_FILE:
23 * name of backend that uses mmap on a file descriptor
25 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
27 #define MEMORY_BACKEND_FILE(obj) \
28 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
30 typedef struct HostMemoryBackendFile HostMemoryBackendFile
;
32 struct HostMemoryBackendFile
{
33 HostMemoryBackend parent_obj
;
42 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
45 error_setg(errp
, "backend '%s' not supported on this host",
46 object_get_typename(OBJECT(backend
)));
48 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(backend
);
52 error_setg(errp
, "can't create backend with size 0");
56 error_setg(errp
, "mem-path property not set");
59 backend
->force_prealloc
= mem_prealloc
;
60 name
= host_memory_backend_get_name(backend
);
61 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
63 backend
->size
, fb
->align
,
64 (backend
->share
? RAM_SHARED
: 0) |
65 (fb
->is_pmem
? RAM_PMEM
: 0),
71 static char *get_mem_path(Object
*o
, Error
**errp
)
73 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
75 return g_strdup(fb
->mem_path
);
78 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
80 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
81 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
83 if (host_memory_backend_mr_inited(backend
)) {
84 error_setg(errp
, "cannot change property 'mem-path' of %s",
85 object_get_typename(o
));
89 fb
->mem_path
= g_strdup(str
);
92 static bool file_memory_backend_get_discard_data(Object
*o
, Error
**errp
)
94 return MEMORY_BACKEND_FILE(o
)->discard_data
;
97 static void file_memory_backend_set_discard_data(Object
*o
, bool value
,
100 MEMORY_BACKEND_FILE(o
)->discard_data
= value
;
103 static void file_memory_backend_get_align(Object
*o
, Visitor
*v
,
104 const char *name
, void *opaque
,
107 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
108 uint64_t val
= fb
->align
;
110 visit_type_size(v
, name
, &val
, errp
);
113 static void file_memory_backend_set_align(Object
*o
, Visitor
*v
,
114 const char *name
, void *opaque
,
117 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
118 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
119 Error
*local_err
= NULL
;
122 if (host_memory_backend_mr_inited(backend
)) {
123 error_setg(&local_err
, "cannot change property '%s' of %s",
124 name
, object_get_typename(o
));
128 visit_type_size(v
, name
, &val
, &local_err
);
135 error_propagate(errp
, local_err
);
138 static bool file_memory_backend_get_pmem(Object
*o
, Error
**errp
)
140 return MEMORY_BACKEND_FILE(o
)->is_pmem
;
143 static void file_memory_backend_set_pmem(Object
*o
, bool value
, Error
**errp
)
145 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
146 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
148 if (host_memory_backend_mr_inited(backend
)) {
150 error_setg(errp
, "cannot change property 'pmem' of %s.",
151 object_get_typename(o
));
155 #ifndef CONFIG_LIBPMEM
157 Error
*local_err
= NULL
;
159 error_setg(&local_err
,
160 "Lack of libpmem support while setting the 'pmem=on'"
161 " of %s. We can't ensure data persistence.",
162 object_get_typename(o
));
163 error_propagate(errp
, local_err
);
171 static void file_backend_unparent(Object
*obj
)
173 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
174 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
176 if (host_memory_backend_mr_inited(backend
) && fb
->discard_data
) {
177 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
178 uint64_t sz
= memory_region_size(&backend
->mr
);
180 qemu_madvise(ptr
, sz
, QEMU_MADV_REMOVE
);
185 file_backend_class_init(ObjectClass
*oc
, void *data
)
187 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
189 bc
->alloc
= file_backend_memory_alloc
;
190 oc
->unparent
= file_backend_unparent
;
192 object_class_property_add_bool(oc
, "discard-data",
193 file_memory_backend_get_discard_data
, file_memory_backend_set_discard_data
,
195 object_class_property_add_str(oc
, "mem-path",
196 get_mem_path
, set_mem_path
,
198 object_class_property_add(oc
, "align", "int",
199 file_memory_backend_get_align
,
200 file_memory_backend_set_align
,
201 NULL
, NULL
, &error_abort
);
202 object_class_property_add_bool(oc
, "pmem",
203 file_memory_backend_get_pmem
, file_memory_backend_set_pmem
,
207 static void file_backend_instance_finalize(Object
*o
)
209 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
211 g_free(fb
->mem_path
);
214 static const TypeInfo file_backend_info
= {
215 .name
= TYPE_MEMORY_BACKEND_FILE
,
216 .parent
= TYPE_MEMORY_BACKEND
,
217 .class_init
= file_backend_class_init
,
218 .instance_finalize
= file_backend_instance_finalize
,
219 .instance_size
= sizeof(HostMemoryBackendFile
),
222 static void register_types(void)
224 type_register_static(&file_backend_info
);
227 type_init(register_types
);