tests/boot-serial: Add tests for PowerPC Mac machines
[qemu.git] / backends / hostmem-file.c
blobe319ec1ad8aff043d54141807de1ec1ac8ec12e6
1 /*
2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
6 * Authors:
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"
19 /* hostmem-file.c */
20 /**
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;
34 bool share;
35 bool discard_data;
36 char *mem_path;
37 uint64_t align;
40 static void
41 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
43 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
45 if (!backend->size) {
46 error_setg(errp, "can't create backend with size 0");
47 return;
49 if (!fb->mem_path) {
50 error_setg(errp, "mem-path property not set");
51 return;
53 #ifndef CONFIG_LINUX
54 error_setg(errp, "-mem-path not supported on this host");
55 #else
56 if (!host_memory_backend_mr_inited(backend)) {
57 gchar *path;
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),
61 path,
62 backend->size, fb->align, fb->share,
63 fb->mem_path, errp);
64 g_free(path);
66 #endif
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");
83 return;
85 g_free(fb->mem_path);
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);
93 return fb->share;
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");
103 return;
105 fb->share = 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,
114 Error **errp)
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,
121 Error **errp)
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,
131 Error **errp)
133 HostMemoryBackend *backend = MEMORY_BACKEND(o);
134 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
135 Error *local_err = NULL;
136 uint64_t val;
138 if (host_memory_backend_mr_inited(backend)) {
139 error_setg(&local_err, "cannot change property value");
140 goto out;
143 visit_type_size(v, name, &val, &local_err);
144 if (local_err) {
145 goto out;
147 fb->align = val;
149 out:
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);
166 static void
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,
176 &error_abort);
177 object_class_property_add_bool(oc, "discard-data",
178 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data,
179 &error_abort);
180 object_class_property_add_str(oc, "mem-path",
181 get_mem_path, set_mem_path,
182 &error_abort);
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);