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 "sysemu/hostmem.h"
16 #include "sysemu/sysemu.h"
17 #include "qom/object_interfaces.h"
21 * @TYPE_MEMORY_BACKEND_FILE:
22 * name of backend that uses mmap on a file descriptor
24 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
26 #define MEMORY_BACKEND_FILE(obj) \
27 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
29 typedef struct HostMemoryBackendFile HostMemoryBackendFile
;
31 struct HostMemoryBackendFile
{
32 HostMemoryBackend parent_obj
;
41 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
43 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 error_setg(errp
, "-mem-path not supported on this host");
56 if (!host_memory_backend_mr_inited(backend
)) {
58 backend
->force_prealloc
= mem_prealloc
;
59 path
= object_get_canonical_path(OBJECT(backend
));
60 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
62 backend
->size
, fb
->align
, fb
->share
,
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 value");
86 fb
->mem_path
= g_strdup(str
);
89 static bool file_memory_backend_get_share(Object
*o
, Error
**errp
)
91 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
96 static void file_memory_backend_set_share(Object
*o
, bool value
, Error
**errp
)
98 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
99 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
101 if (host_memory_backend_mr_inited(backend
)) {
102 error_setg(errp
, "cannot change property value");
108 static bool file_memory_backend_get_discard_data(Object
*o
, Error
**errp
)
110 return MEMORY_BACKEND_FILE(o
)->discard_data
;
113 static void file_memory_backend_set_discard_data(Object
*o
, bool value
,
116 MEMORY_BACKEND_FILE(o
)->discard_data
= value
;
119 static void file_memory_backend_get_align(Object
*o
, Visitor
*v
,
120 const char *name
, void *opaque
,
123 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
124 uint64_t val
= fb
->align
;
126 visit_type_size(v
, name
, &val
, errp
);
129 static void file_memory_backend_set_align(Object
*o
, Visitor
*v
,
130 const char *name
, void *opaque
,
133 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
134 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
135 Error
*local_err
= NULL
;
138 if (host_memory_backend_mr_inited(backend
)) {
139 error_setg(&local_err
, "cannot change property value");
143 visit_type_size(v
, name
, &val
, &local_err
);
150 error_propagate(errp
, local_err
);
153 static void file_backend_unparent(Object
*obj
)
155 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
156 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
158 if (host_memory_backend_mr_inited(backend
) && fb
->discard_data
) {
159 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
160 uint64_t sz
= memory_region_size(&backend
->mr
);
162 qemu_madvise(ptr
, sz
, QEMU_MADV_REMOVE
);
167 file_backend_class_init(ObjectClass
*oc
, void *data
)
169 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
171 bc
->alloc
= file_backend_memory_alloc
;
172 oc
->unparent
= file_backend_unparent
;
174 object_class_property_add_bool(oc
, "share",
175 file_memory_backend_get_share
, file_memory_backend_set_share
,
177 object_class_property_add_bool(oc
, "discard-data",
178 file_memory_backend_get_discard_data
, file_memory_backend_set_discard_data
,
180 object_class_property_add_str(oc
, "mem-path",
181 get_mem_path
, set_mem_path
,
183 object_class_property_add(oc
, "align", "int",
184 file_memory_backend_get_align
,
185 file_memory_backend_set_align
,
186 NULL
, NULL
, &error_abort
);
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
);