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 13 #include "qemu/osdep.h" 14 #include "qapi/error.h" 15 #include "qemu/error-report.h" 16 #include "qemu/module.h" 17 #include "sysemu/hostmem.h" 18 #include "qom/object_interfaces.h" 19 #include "qom/object.h" 20 21 OBJECT_DECLARE_SIMPLE_TYPE(HostMemoryBackendFile, MEMORY_BACKEND_FILE) 22 23 24 struct HostMemoryBackendFile { 25 HostMemoryBackend parent_obj; 26 27 char *mem_path; 28 uint64_t align; 29 bool discard_data; 30 bool is_pmem; 31 bool readonly; 32 }; 33 34 static void 35 file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) 36 { 37 #ifndef CONFIG_POSIX 38 error_setg(errp, "backend '%s' not supported on this host", 39 object_get_typename(OBJECT(backend))); 40 #else 41 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); 42 gchar *name; 43 44 if (!backend->size) { 45 error_setg(errp, "can't create backend with size 0"); 46 return; 47 } 48 if (!fb->mem_path) { 49 error_setg(errp, "mem-path property not set"); 50 return; 51 } 52 53 name = host_memory_backend_get_name(backend); 54 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), 55 name, 56 backend->size, fb->align, 57 (backend->share ? RAM_SHARED : 0) | 58 (fb->is_pmem ? RAM_PMEM : 0), 59 fb->mem_path, fb->readonly, errp); 60 g_free(name); 61 #endif 62 } 63 64 static char *get_mem_path(Object *o, Error **errp) 65 { 66 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 67 68 return g_strdup(fb->mem_path); 69 } 70 71 static void set_mem_path(Object *o, const char *str, Error **errp) 72 { 73 HostMemoryBackend *backend = MEMORY_BACKEND(o); 74 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 75 76 if (host_memory_backend_mr_inited(backend)) { 77 error_setg(errp, "cannot change property 'mem-path' of %s", 78 object_get_typename(o)); 79 return; 80 } 81 g_free(fb->mem_path); 82 fb->mem_path = g_strdup(str); 83 } 84 85 static bool file_memory_backend_get_discard_data(Object *o, Error **errp) 86 { 87 return MEMORY_BACKEND_FILE(o)->discard_data; 88 } 89 90 static void file_memory_backend_set_discard_data(Object *o, bool value, 91 Error **errp) 92 { 93 MEMORY_BACKEND_FILE(o)->discard_data = value; 94 } 95 96 static void file_memory_backend_get_align(Object *o, Visitor *v, 97 const char *name, void *opaque, 98 Error **errp) 99 { 100 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 101 uint64_t val = fb->align; 102 103 visit_type_size(v, name, &val, errp); 104 } 105 106 static void file_memory_backend_set_align(Object *o, Visitor *v, 107 const char *name, void *opaque, 108 Error **errp) 109 { 110 HostMemoryBackend *backend = MEMORY_BACKEND(o); 111 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 112 uint64_t val; 113 114 if (host_memory_backend_mr_inited(backend)) { 115 error_setg(errp, "cannot change property '%s' of %s", name, 116 object_get_typename(o)); 117 return; 118 } 119 120 if (!visit_type_size(v, name, &val, errp)) { 121 return; 122 } 123 fb->align = val; 124 } 125 126 #ifdef CONFIG_LIBPMEM 127 static bool file_memory_backend_get_pmem(Object *o, Error **errp) 128 { 129 return MEMORY_BACKEND_FILE(o)->is_pmem; 130 } 131 132 static void file_memory_backend_set_pmem(Object *o, bool value, Error **errp) 133 { 134 HostMemoryBackend *backend = MEMORY_BACKEND(o); 135 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 136 137 if (host_memory_backend_mr_inited(backend)) { 138 error_setg(errp, "cannot change property 'pmem' of %s.", 139 object_get_typename(o)); 140 return; 141 } 142 143 fb->is_pmem = value; 144 } 145 #endif /* CONFIG_LIBPMEM */ 146 147 static bool file_memory_backend_get_readonly(Object *obj, Error **errp) 148 { 149 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); 150 151 return fb->readonly; 152 } 153 154 static void file_memory_backend_set_readonly(Object *obj, bool value, 155 Error **errp) 156 { 157 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 158 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); 159 160 if (host_memory_backend_mr_inited(backend)) { 161 error_setg(errp, "cannot change property 'readonly' of %s.", 162 object_get_typename(obj)); 163 return; 164 } 165 166 fb->readonly = value; 167 } 168 169 static void file_backend_unparent(Object *obj) 170 { 171 HostMemoryBackend *backend = MEMORY_BACKEND(obj); 172 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(obj); 173 174 if (host_memory_backend_mr_inited(backend) && fb->discard_data) { 175 void *ptr = memory_region_get_ram_ptr(&backend->mr); 176 uint64_t sz = memory_region_size(&backend->mr); 177 178 qemu_madvise(ptr, sz, QEMU_MADV_REMOVE); 179 } 180 } 181 182 static void 183 file_backend_class_init(ObjectClass *oc, void *data) 184 { 185 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); 186 187 bc->alloc = file_backend_memory_alloc; 188 oc->unparent = file_backend_unparent; 189 190 object_class_property_add_bool(oc, "discard-data", 191 file_memory_backend_get_discard_data, file_memory_backend_set_discard_data); 192 object_class_property_add_str(oc, "mem-path", 193 get_mem_path, set_mem_path); 194 object_class_property_add(oc, "align", "int", 195 file_memory_backend_get_align, 196 file_memory_backend_set_align, 197 NULL, NULL); 198 #ifdef CONFIG_LIBPMEM 199 object_class_property_add_bool(oc, "pmem", 200 file_memory_backend_get_pmem, file_memory_backend_set_pmem); 201 #endif 202 object_class_property_add_bool(oc, "readonly", 203 file_memory_backend_get_readonly, 204 file_memory_backend_set_readonly); 205 } 206 207 static void file_backend_instance_finalize(Object *o) 208 { 209 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); 210 211 g_free(fb->mem_path); 212 } 213 214 static const TypeInfo file_backend_info = { 215 .name = TYPE_MEMORY_BACKEND_FILE, 216 .parent = TYPE_MEMORY_BACKEND, 217 .class_init = file_backend_class_init, 218 .instance_finalize = file_backend_instance_finalize, 219 .instance_size = sizeof(HostMemoryBackendFile), 220 }; 221 222 static void register_types(void) 223 { 224 type_register_static(&file_backend_info); 225 } 226 227 type_init(register_types); 228