blob: 92659c119d12478969a81abdf95d6ee167d96d66 [file] [log] [blame]
Paolo Bonzini52330e12014-06-10 19:15:21 +08001/*
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 */
Paolo Bonzinia35ba7b2014-06-10 19:15:23 +080012#include "qemu-common.h"
Paolo Bonzini52330e12014-06-10 19:15:21 +080013#include "sysemu/hostmem.h"
Paolo Bonzinia35ba7b2014-06-10 19:15:23 +080014#include "sysemu/sysemu.h"
Paolo Bonzini52330e12014-06-10 19:15:21 +080015#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
27typedef struct HostMemoryBackendFile HostMemoryBackendFile;
28
29struct HostMemoryBackendFile {
30 HostMemoryBackend parent_obj;
31 char *mem_path;
32};
33
34static void
35file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
36{
37 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
38
39 if (!backend->size) {
40 error_setg(errp, "can't create backend with size 0");
41 return;
42 }
43 if (!fb->mem_path) {
44 error_setg(errp, "mem_path property not set");
45 return;
46 }
47#ifndef CONFIG_LINUX
48 error_setg(errp, "-mem-path not supported on this host");
49#else
50 if (!memory_region_size(&backend->mr)) {
Paolo Bonzinia35ba7b2014-06-10 19:15:23 +080051 backend->force_prealloc = mem_prealloc;
Paolo Bonzini52330e12014-06-10 19:15:21 +080052 memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
53 object_get_canonical_path(OBJECT(backend)),
54 backend->size,
55 fb->mem_path, errp);
56 }
57#endif
58}
59
60static void
61file_backend_class_init(ObjectClass *oc, void *data)
62{
63 HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
64
65 bc->alloc = file_backend_memory_alloc;
66}
67
68static char *get_mem_path(Object *o, Error **errp)
69{
70 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
71
72 return g_strdup(fb->mem_path);
73}
74
75static void set_mem_path(Object *o, const char *str, Error **errp)
76{
77 HostMemoryBackend *backend = MEMORY_BACKEND(o);
78 HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
79
80 if (memory_region_size(&backend->mr)) {
81 error_setg(errp, "cannot change property value");
82 return;
83 }
84 if (fb->mem_path) {
85 g_free(fb->mem_path);
86 }
87 fb->mem_path = g_strdup(str);
88}
89
90static void
91file_backend_instance_init(Object *o)
92{
93 object_property_add_str(o, "mem-path", get_mem_path,
94 set_mem_path, NULL);
95}
96
97static const TypeInfo file_backend_info = {
98 .name = TYPE_MEMORY_BACKEND_FILE,
99 .parent = TYPE_MEMORY_BACKEND,
100 .class_init = file_backend_class_init,
101 .instance_init = file_backend_instance_init,
102 .instance_size = sizeof(HostMemoryBackendFile),
103};
104
105static void register_types(void)
106{
107 type_register_static(&file_backend_info);
108}
109
110type_init(register_types);