]>
Commit | Line | Data |
---|---|---|
52330e1a PB |
1 | /* |
2 | * QEMU Host Memory Backend for hugetlbfs | |
3 | * | |
4 | * Copyright (C) 2013-2014 Red Hat Inc | |
5 | * | |
6 | * Authors: | |
7 | * Paolo Bonzini <pbonzini@redhat.com> | |
8 | * | |
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. | |
11 | */ | |
9c058332 | 12 | #include "qemu/osdep.h" |
da34e65c | 13 | #include "qapi/error.h" |
a35ba7be | 14 | #include "qemu-common.h" |
52330e1a | 15 | #include "sysemu/hostmem.h" |
a35ba7be | 16 | #include "sysemu/sysemu.h" |
52330e1a PB |
17 | #include "qom/object_interfaces.h" |
18 | ||
19 | /* hostmem-file.c */ | |
20 | /** | |
21 | * @TYPE_MEMORY_BACKEND_FILE: | |
22 | * name of backend that uses mmap on a file descriptor | |
23 | */ | |
24 | #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file" | |
25 | ||
26 | #define MEMORY_BACKEND_FILE(obj) \ | |
27 | OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE) | |
28 | ||
29 | typedef struct HostMemoryBackendFile HostMemoryBackendFile; | |
30 | ||
31 | struct HostMemoryBackendFile { | |
32 | HostMemoryBackend parent_obj; | |
dbcb8981 PB |
33 | |
34 | bool share; | |
52330e1a PB |
35 | char *mem_path; |
36 | }; | |
37 | ||
38 | static void | |
39 | file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) | |
40 | { | |
41 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); | |
42 | ||
43 | if (!backend->size) { | |
44 | error_setg(errp, "can't create backend with size 0"); | |
45 | return; | |
46 | } | |
47 | if (!fb->mem_path) { | |
c2cb2b04 | 48 | error_setg(errp, "mem-path property not set"); |
52330e1a PB |
49 | return; |
50 | } | |
51 | #ifndef CONFIG_LINUX | |
52 | error_setg(errp, "-mem-path not supported on this host"); | |
53 | #else | |
54 | if (!memory_region_size(&backend->mr)) { | |
696b5501 | 55 | gchar *path; |
a35ba7be | 56 | backend->force_prealloc = mem_prealloc; |
696b5501 | 57 | path = object_get_canonical_path(OBJECT(backend)); |
52330e1a | 58 | memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), |
696b5501 | 59 | path, |
dbcb8981 | 60 | backend->size, fb->share, |
52330e1a | 61 | fb->mem_path, errp); |
696b5501 | 62 | g_free(path); |
52330e1a PB |
63 | } |
64 | #endif | |
65 | } | |
66 | ||
67 | static void | |
68 | file_backend_class_init(ObjectClass *oc, void *data) | |
69 | { | |
70 | HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); | |
71 | ||
72 | bc->alloc = file_backend_memory_alloc; | |
73 | } | |
74 | ||
75 | static char *get_mem_path(Object *o, Error **errp) | |
76 | { | |
77 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
78 | ||
79 | return g_strdup(fb->mem_path); | |
80 | } | |
81 | ||
82 | static void set_mem_path(Object *o, const char *str, Error **errp) | |
83 | { | |
84 | HostMemoryBackend *backend = MEMORY_BACKEND(o); | |
85 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
86 | ||
87 | if (memory_region_size(&backend->mr)) { | |
88 | error_setg(errp, "cannot change property value"); | |
89 | return; | |
90 | } | |
ef1e1e07 | 91 | g_free(fb->mem_path); |
52330e1a PB |
92 | fb->mem_path = g_strdup(str); |
93 | } | |
94 | ||
dbcb8981 PB |
95 | static bool file_memory_backend_get_share(Object *o, Error **errp) |
96 | { | |
97 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
98 | ||
99 | return fb->share; | |
100 | } | |
101 | ||
102 | static void file_memory_backend_set_share(Object *o, bool value, Error **errp) | |
103 | { | |
104 | HostMemoryBackend *backend = MEMORY_BACKEND(o); | |
105 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
106 | ||
107 | if (memory_region_size(&backend->mr)) { | |
108 | error_setg(errp, "cannot change property value"); | |
109 | return; | |
110 | } | |
111 | fb->share = value; | |
112 | } | |
113 | ||
52330e1a PB |
114 | static void |
115 | file_backend_instance_init(Object *o) | |
116 | { | |
dbcb8981 PB |
117 | object_property_add_bool(o, "share", |
118 | file_memory_backend_get_share, | |
119 | file_memory_backend_set_share, NULL); | |
52330e1a PB |
120 | object_property_add_str(o, "mem-path", get_mem_path, |
121 | set_mem_path, NULL); | |
122 | } | |
123 | ||
bc78a013 MAL |
124 | static void file_backend_instance_finalize(Object *o) |
125 | { | |
126 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
127 | ||
128 | g_free(fb->mem_path); | |
129 | } | |
130 | ||
52330e1a PB |
131 | static const TypeInfo file_backend_info = { |
132 | .name = TYPE_MEMORY_BACKEND_FILE, | |
133 | .parent = TYPE_MEMORY_BACKEND, | |
134 | .class_init = file_backend_class_init, | |
135 | .instance_init = file_backend_instance_init, | |
bc78a013 | 136 | .instance_finalize = file_backend_instance_finalize, |
52330e1a PB |
137 | .instance_size = sizeof(HostMemoryBackendFile), |
138 | }; | |
139 | ||
140 | static void register_types(void) | |
141 | { | |
142 | type_register_static(&file_backend_info); | |
143 | } | |
144 | ||
145 | type_init(register_types); |