2 * QEMU Host Memory Backend for hugetlbfs
4 * Copyright (C) 2013-2014 Red Hat Inc
7 * Paolo Bonzini <pbonzini@redhat.com>
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.
12 #include "qemu/osdep.h"
13 #include "qapi/error.h"
14 #include "qemu-common.h"
15 #include "sysemu/hostmem.h"
16 #include "sysemu/sysemu.h"
17 #include "qom/object_interfaces.h"
21 * @TYPE_MEMORY_BACKEND_FILE:
22 * name of backend that uses mmap on a file descriptor
24 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
26 #define MEMORY_BACKEND_FILE(obj) \
27 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
29 typedef struct HostMemoryBackendFile HostMemoryBackendFile
;
31 struct HostMemoryBackendFile
{
32 HostMemoryBackend parent_obj
;
39 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
41 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(backend
);
44 error_setg(errp
, "can't create backend with size 0");
48 error_setg(errp
, "mem-path property not set");
52 error_setg(errp
, "-mem-path not supported on this host");
54 if (!memory_region_size(&backend
->mr
)) {
56 backend
->force_prealloc
= mem_prealloc
;
57 path
= object_get_canonical_path(OBJECT(backend
));
58 memory_region_init_ram_from_file(&backend
->mr
, OBJECT(backend
),
60 backend
->size
, fb
->share
,
68 file_backend_class_init(ObjectClass
*oc
, void *data
)
70 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
72 bc
->alloc
= file_backend_memory_alloc
;
75 static char *get_mem_path(Object
*o
, Error
**errp
)
77 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
79 return g_strdup(fb
->mem_path
);
82 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
84 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
85 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
87 if (memory_region_size(&backend
->mr
)) {
88 error_setg(errp
, "cannot change property value");
92 fb
->mem_path
= g_strdup(str
);
95 static bool file_memory_backend_get_share(Object
*o
, Error
**errp
)
97 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
102 static void file_memory_backend_set_share(Object
*o
, bool value
, Error
**errp
)
104 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
105 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
107 if (memory_region_size(&backend
->mr
)) {
108 error_setg(errp
, "cannot change property value");
115 file_backend_instance_init(Object
*o
)
117 object_property_add_bool(o
, "share",
118 file_memory_backend_get_share
,
119 file_memory_backend_set_share
, NULL
);
120 object_property_add_str(o
, "mem-path", get_mem_path
,
124 static const TypeInfo file_backend_info
= {
125 .name
= TYPE_MEMORY_BACKEND_FILE
,
126 .parent
= TYPE_MEMORY_BACKEND
,
127 .class_init
= file_backend_class_init
,
128 .instance_init
= file_backend_instance_init
,
129 .instance_size
= sizeof(HostMemoryBackendFile
),
132 static void register_types(void)
134 type_register_static(&file_backend_info
);
137 type_init(register_types
);