Paolo Bonzini | 52330e1 | 2014-06-10 19:15:21 +0800 | [diff] [blame^] | 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 "sysemu/hostmem.h" |
| 13 | #include "qom/object_interfaces.h" |
| 14 | |
| 15 | /* hostmem-file.c */ |
| 16 | /** |
| 17 | * @TYPE_MEMORY_BACKEND_FILE: |
| 18 | * name of backend that uses mmap on a file descriptor |
| 19 | */ |
| 20 | #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file" |
| 21 | |
| 22 | #define MEMORY_BACKEND_FILE(obj) \ |
| 23 | OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE) |
| 24 | |
| 25 | typedef struct HostMemoryBackendFile HostMemoryBackendFile; |
| 26 | |
| 27 | struct HostMemoryBackendFile { |
| 28 | HostMemoryBackend parent_obj; |
| 29 | char *mem_path; |
| 30 | }; |
| 31 | |
| 32 | static void |
| 33 | file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) |
| 34 | { |
| 35 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend); |
| 36 | |
| 37 | if (!backend->size) { |
| 38 | error_setg(errp, "can't create backend with size 0"); |
| 39 | return; |
| 40 | } |
| 41 | if (!fb->mem_path) { |
| 42 | error_setg(errp, "mem_path property not set"); |
| 43 | return; |
| 44 | } |
| 45 | #ifndef CONFIG_LINUX |
| 46 | error_setg(errp, "-mem-path not supported on this host"); |
| 47 | #else |
| 48 | if (!memory_region_size(&backend->mr)) { |
| 49 | memory_region_init_ram_from_file(&backend->mr, OBJECT(backend), |
| 50 | object_get_canonical_path(OBJECT(backend)), |
| 51 | backend->size, |
| 52 | fb->mem_path, errp); |
| 53 | } |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | file_backend_class_init(ObjectClass *oc, void *data) |
| 59 | { |
| 60 | HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); |
| 61 | |
| 62 | bc->alloc = file_backend_memory_alloc; |
| 63 | } |
| 64 | |
| 65 | static char *get_mem_path(Object *o, Error **errp) |
| 66 | { |
| 67 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
| 68 | |
| 69 | return g_strdup(fb->mem_path); |
| 70 | } |
| 71 | |
| 72 | static void set_mem_path(Object *o, const char *str, Error **errp) |
| 73 | { |
| 74 | HostMemoryBackend *backend = MEMORY_BACKEND(o); |
| 75 | HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o); |
| 76 | |
| 77 | if (memory_region_size(&backend->mr)) { |
| 78 | error_setg(errp, "cannot change property value"); |
| 79 | return; |
| 80 | } |
| 81 | if (fb->mem_path) { |
| 82 | g_free(fb->mem_path); |
| 83 | } |
| 84 | fb->mem_path = g_strdup(str); |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | file_backend_instance_init(Object *o) |
| 89 | { |
| 90 | object_property_add_str(o, "mem-path", get_mem_path, |
| 91 | set_mem_path, NULL); |
| 92 | } |
| 93 | |
| 94 | static const TypeInfo file_backend_info = { |
| 95 | .name = TYPE_MEMORY_BACKEND_FILE, |
| 96 | .parent = TYPE_MEMORY_BACKEND, |
| 97 | .class_init = file_backend_class_init, |
| 98 | .instance_init = file_backend_instance_init, |
| 99 | .instance_size = sizeof(HostMemoryBackendFile), |
| 100 | }; |
| 101 | |
| 102 | static void register_types(void) |
| 103 | { |
| 104 | type_register_static(&file_backend_info); |
| 105 | } |
| 106 | |
| 107 | type_init(register_types); |