]>
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)) { | |
a35ba7be | 55 | backend->force_prealloc = mem_prealloc; |
52330e1a PB |
56 | memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), |
57 | object_get_canonical_path(OBJECT(backend)), | |
dbcb8981 | 58 | backend->size, fb->share, |
52330e1a PB |
59 | fb->mem_path, errp); |
60 | } | |
61 | #endif | |
62 | } | |
63 | ||
64 | static void | |
65 | file_backend_class_init(ObjectClass *oc, void *data) | |
66 | { | |
67 | HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); | |
68 | ||
69 | bc->alloc = file_backend_memory_alloc; | |
70 | } | |
71 | ||
72 | static char *get_mem_path(Object *o, Error **errp) | |
73 | { | |
74 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
75 | ||
76 | return g_strdup(fb->mem_path); | |
77 | } | |
78 | ||
79 | static void set_mem_path(Object *o, const char *str, Error **errp) | |
80 | { | |
81 | HostMemoryBackend *backend = MEMORY_BACKEND(o); | |
82 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
83 | ||
84 | if (memory_region_size(&backend->mr)) { | |
85 | error_setg(errp, "cannot change property value"); | |
86 | return; | |
87 | } | |
ef1e1e07 | 88 | g_free(fb->mem_path); |
52330e1a PB |
89 | fb->mem_path = g_strdup(str); |
90 | } | |
91 | ||
dbcb8981 PB |
92 | static bool file_memory_backend_get_share(Object *o, Error **errp) |
93 | { | |
94 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
95 | ||
96 | return fb->share; | |
97 | } | |
98 | ||
99 | static void file_memory_backend_set_share(Object *o, bool value, Error **errp) | |
100 | { | |
101 | HostMemoryBackend *backend = MEMORY_BACKEND(o); | |
102 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); | |
103 | ||
104 | if (memory_region_size(&backend->mr)) { | |
105 | error_setg(errp, "cannot change property value"); | |
106 | return; | |
107 | } | |
108 | fb->share = value; | |
109 | } | |
110 | ||
52330e1a PB |
111 | static void |
112 | file_backend_instance_init(Object *o) | |
113 | { | |
dbcb8981 PB |
114 | object_property_add_bool(o, "share", |
115 | file_memory_backend_get_share, | |
116 | file_memory_backend_set_share, NULL); | |
52330e1a PB |
117 | object_property_add_str(o, "mem-path", get_mem_path, |
118 | set_mem_path, NULL); | |
119 | } | |
120 | ||
121 | static const TypeInfo file_backend_info = { | |
122 | .name = TYPE_MEMORY_BACKEND_FILE, | |
123 | .parent = TYPE_MEMORY_BACKEND, | |
124 | .class_init = file_backend_class_init, | |
125 | .instance_init = file_backend_instance_init, | |
126 | .instance_size = sizeof(HostMemoryBackendFile), | |
127 | }; | |
128 | ||
129 | static void register_types(void) | |
130 | { | |
131 | type_register_static(&file_backend_info); | |
132 | } | |
133 | ||
134 | type_init(register_types); |