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
;
40 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
42 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 error_setg(errp
, "-mem-path not supported on this host");
55 if (!host_memory_backend_mr_inited(backend
)) {
57 backend
->force_prealloc
= mem_prealloc
;
58 path
= object_get_canonical_path(OBJECT(backend
));
59 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
61 backend
->size
, fb
->align
, backend
->share
,
68 static char *get_mem_path(Object
*o
, Error
**errp
)
70 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
72 return g_strdup(fb
->mem_path
);
75 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
77 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
78 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
80 if (host_memory_backend_mr_inited(backend
)) {
81 error_setg(errp
, "cannot change property value");
85 fb
->mem_path
= g_strdup(str
);
88 static bool file_memory_backend_get_discard_data(Object
*o
, Error
**errp
)
90 return MEMORY_BACKEND_FILE(o
)->discard_data
;
93 static void file_memory_backend_set_discard_data(Object
*o
, bool value
,
96 MEMORY_BACKEND_FILE(o
)->discard_data
= value
;
99 static void file_memory_backend_get_align(Object
*o
, Visitor
*v
,
100 const char *name
, void *opaque
,
103 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
104 uint64_t val
= fb
->align
;
106 visit_type_size(v
, name
, &val
, errp
);
109 static void file_memory_backend_set_align(Object
*o
, Visitor
*v
,
110 const char *name
, void *opaque
,
113 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
114 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
115 Error
*local_err
= NULL
;
118 if (host_memory_backend_mr_inited(backend
)) {
119 error_setg(&local_err
, "cannot change property value");
123 visit_type_size(v
, name
, &val
, &local_err
);
130 error_propagate(errp
, local_err
);
133 static void file_backend_unparent(Object
*obj
)
135 HostMemoryBackend
*backend
= MEMORY_BACKEND(obj
);
136 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(obj
);
138 if (host_memory_backend_mr_inited(backend
) && fb
->discard_data
) {
139 void *ptr
= memory_region_get_ram_ptr(&backend
->mr
);
140 uint64_t sz
= memory_region_size(&backend
->mr
);
142 qemu_madvise(ptr
, sz
, QEMU_MADV_REMOVE
);
147 file_backend_class_init(ObjectClass
*oc
, void *data
)
149 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
151 bc
->alloc
= file_backend_memory_alloc
;
152 oc
->unparent
= file_backend_unparent
;
154 object_class_property_add_bool(oc
, "discard-data",
155 file_memory_backend_get_discard_data
, file_memory_backend_set_discard_data
,
157 object_class_property_add_str(oc
, "mem-path",
158 get_mem_path
, set_mem_path
,
160 object_class_property_add(oc
, "align", "int",
161 file_memory_backend_get_align
,
162 file_memory_backend_set_align
,
163 NULL
, NULL
, &error_abort
);
166 static void file_backend_instance_finalize(Object
*o
)
168 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
170 g_free(fb
->mem_path
);
173 static const TypeInfo file_backend_info
= {
174 .name
= TYPE_MEMORY_BACKEND_FILE
,
175 .parent
= TYPE_MEMORY_BACKEND
,
176 .class_init
= file_backend_class_init
,
177 .instance_finalize
= file_backend_instance_finalize
,
178 .instance_size
= sizeof(HostMemoryBackendFile
),
181 static void register_types(void)
183 type_register_static(&file_backend_info
);
186 type_init(register_types
);