xref: /openbmc/qemu/backends/hostmem-file.c (revision f8ed85ac)
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  */
12 #include "qemu-common.h"
13 #include "sysemu/hostmem.h"
14 #include "sysemu/sysemu.h"
15 #include "qom/object_interfaces.h"
16 
17 /* hostmem-file.c */
18 /**
19  * @TYPE_MEMORY_BACKEND_FILE:
20  * name of backend that uses mmap on a file descriptor
21  */
22 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
23 
24 #define MEMORY_BACKEND_FILE(obj) \
25     OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
26 
27 typedef struct HostMemoryBackendFile HostMemoryBackendFile;
28 
29 struct HostMemoryBackendFile {
30     HostMemoryBackend parent_obj;
31 
32     bool share;
33     char *mem_path;
34 };
35 
36 static void
37 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
38 {
39     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
40 
41     if (!backend->size) {
42         error_setg(errp, "can't create backend with size 0");
43         return;
44     }
45     if (!fb->mem_path) {
46         error_setg(errp, "mem-path property not set");
47         return;
48     }
49 #ifndef CONFIG_LINUX
50     error_setg(errp, "-mem-path not supported on this host");
51 #else
52     if (!memory_region_size(&backend->mr)) {
53         backend->force_prealloc = mem_prealloc;
54         memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
55                                  object_get_canonical_path(OBJECT(backend)),
56                                  backend->size, fb->share,
57                                  fb->mem_path, errp);
58     }
59 #endif
60 }
61 
62 static void
63 file_backend_class_init(ObjectClass *oc, void *data)
64 {
65     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
66 
67     bc->alloc = file_backend_memory_alloc;
68 }
69 
70 static char *get_mem_path(Object *o, Error **errp)
71 {
72     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
73 
74     return g_strdup(fb->mem_path);
75 }
76 
77 static void set_mem_path(Object *o, const char *str, Error **errp)
78 {
79     HostMemoryBackend *backend = MEMORY_BACKEND(o);
80     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
81 
82     if (memory_region_size(&backend->mr)) {
83         error_setg(errp, "cannot change property value");
84         return;
85     }
86     g_free(fb->mem_path);
87     fb->mem_path = g_strdup(str);
88 }
89 
90 static bool file_memory_backend_get_share(Object *o, Error **errp)
91 {
92     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
93 
94     return fb->share;
95 }
96 
97 static void file_memory_backend_set_share(Object *o, bool value, Error **errp)
98 {
99     HostMemoryBackend *backend = MEMORY_BACKEND(o);
100     HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
101 
102     if (memory_region_size(&backend->mr)) {
103         error_setg(errp, "cannot change property value");
104         return;
105     }
106     fb->share = value;
107 }
108 
109 static void
110 file_backend_instance_init(Object *o)
111 {
112     object_property_add_bool(o, "share",
113                         file_memory_backend_get_share,
114                         file_memory_backend_set_share, NULL);
115     object_property_add_str(o, "mem-path", get_mem_path,
116                             set_mem_path, NULL);
117 }
118 
119 static const TypeInfo file_backend_info = {
120     .name = TYPE_MEMORY_BACKEND_FILE,
121     .parent = TYPE_MEMORY_BACKEND,
122     .class_init = file_backend_class_init,
123     .instance_init = file_backend_instance_init,
124     .instance_size = sizeof(HostMemoryBackendFile),
125 };
126 
127 static void register_types(void)
128 {
129     type_register_static(&file_backend_info);
130 }
131 
132 type_init(register_types);
133