linux-user: add hppa magic numbers in qemu-binfmt-conf.sh
[qemu/ar7.git] / backends / hostmem-file.c
blob42efb2f28a221a89c41f79fefe8dbf92cabf0e0b
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 char *mem_path;
38 static void
39 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
41 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
43 if (!backend->size) {
44 error_setg(errp, "can't create backend with size 0");
45 return;
47 if (!fb->mem_path) {
48 error_setg(errp, "mem-path property not set");
49 return;
51 #ifndef CONFIG_LINUX
52 error_setg(errp, "-mem-path not supported on this host");
53 #else
54 if (!memory_region_size(&backend->mr)) {
55 gchar *path;
56 backend->force_prealloc = mem_prealloc;
57 path = object_get_canonical_path(OBJECT(backend));
58 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
59 path,
60 backend->size, fb->share,
61 fb->mem_path, errp);
62 g_free(path);
64 #endif
67 static char *get_mem_path(Object *o, Error **errp)
69 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
71 return g_strdup(fb->mem_path);
74 static void set_mem_path(Object *o, const char *str, Error **errp)
76 HostMemoryBackend *backend = MEMORY_BACKEND(o);
77 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
79 if (memory_region_size(&backend->mr)) {
80 error_setg(errp, "cannot change property value");
81 return;
83 g_free(fb->mem_path);
84 fb->mem_path = g_strdup(str);
87 static bool file_memory_backend_get_share(Object *o, Error **errp)
89 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
91 return fb->share;
94 static void file_memory_backend_set_share(Object *o, bool value, Error **errp)
96 HostMemoryBackend *backend = MEMORY_BACKEND(o);
97 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
99 if (memory_region_size(&backend->mr)) {
100 error_setg(errp, "cannot change property value");
101 return;
103 fb->share = value;
106 static void
107 file_backend_class_init(ObjectClass *oc, void *data)
109 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
111 bc->alloc = file_backend_memory_alloc;
113 object_class_property_add_bool(oc, "share",
114 file_memory_backend_get_share, file_memory_backend_set_share,
115 &error_abort);
116 object_class_property_add_str(oc, "mem-path",
117 get_mem_path, set_mem_path,
118 &error_abort);
121 static void file_backend_instance_finalize(Object *o)
123 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
125 g_free(fb->mem_path);
128 static const TypeInfo file_backend_info = {
129 .name = TYPE_MEMORY_BACKEND_FILE,
130 .parent = TYPE_MEMORY_BACKEND,
131 .class_init = file_backend_class_init,
132 .instance_finalize = file_backend_instance_finalize,
133 .instance_size = sizeof(HostMemoryBackendFile),
136 static void register_types(void)
138 type_register_static(&file_backend_info);
141 type_init(register_types);