scsi: Ignore executable for in-tree builds
[qemu/kevin.git] / backends / hostmem-file.c
blobe44c319915acac43183176ce993c515932948dd8
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;
39 static void
40 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
42 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
44 if (!backend->size) {
45 error_setg(errp, "can't create backend with size 0");
46 return;
48 if (!fb->mem_path) {
49 error_setg(errp, "mem-path property not set");
50 return;
52 #ifndef CONFIG_LINUX
53 error_setg(errp, "-mem-path not supported on this host");
54 #else
55 if (!host_memory_backend_mr_inited(backend)) {
56 gchar *path;
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),
60 path,
61 backend->size, fb->share,
62 fb->mem_path, errp);
63 g_free(path);
65 #endif
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");
82 return;
84 g_free(fb->mem_path);
85 fb->mem_path = g_strdup(str);
88 static bool file_memory_backend_get_share(Object *o, Error **errp)
90 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
92 return fb->share;
95 static void file_memory_backend_set_share(Object *o, bool value, Error **errp)
97 HostMemoryBackend *backend = MEMORY_BACKEND(o);
98 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
100 if (host_memory_backend_mr_inited(backend)) {
101 error_setg(errp, "cannot change property value");
102 return;
104 fb->share = value;
107 static bool file_memory_backend_get_discard_data(Object *o, Error **errp)
109 return MEMORY_BACKEND_FILE(o)->discard_data;
112 static void file_memory_backend_set_discard_data(Object *o, bool value,
113 Error **errp)
115 MEMORY_BACKEND_FILE(o)->discard_data = value;
118 static void file_backend_unparent(Object *obj)
120 HostMemoryBackend *backend = MEMORY_BACKEND(obj);
121 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj);
123 if (host_memory_backend_mr_inited(backend) && fb->discard_data) {
124 void *ptr = memory_region_get_ram_ptr(&backend->mr);
125 uint64_t sz = memory_region_size(&backend->mr);
127 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE);
131 static void
132 file_backend_class_init(ObjectClass *oc, void *data)
134 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
136 bc->alloc = file_backend_memory_alloc;
137 oc->unparent = file_backend_unparent;
139 object_class_property_add_bool(oc, "share",
140 file_memory_backend_get_share, file_memory_backend_set_share,
141 &error_abort);
142 object_class_property_add_bool(oc, "discard-data",
143 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data,
144 &error_abort);
145 object_class_property_add_str(oc, "mem-path",
146 get_mem_path, set_mem_path,
147 &error_abort);
150 static void file_backend_instance_finalize(Object *o)
152 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
154 g_free(fb->mem_path);
157 static const TypeInfo file_backend_info = {
158 .name = TYPE_MEMORY_BACKEND_FILE,
159 .parent = TYPE_MEMORY_BACKEND,
160 .class_init = file_backend_class_init,
161 .instance_finalize = file_backend_instance_finalize,
162 .instance_size = sizeof(HostMemoryBackendFile),
165 static void register_types(void)
167 type_register_static(&file_backend_info);
170 type_init(register_types);