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-common.h"
13 #include "sysemu/hostmem.h"
14 #include "sysemu/sysemu.h"
15 #include "qom/object_interfaces.h"
19 * @TYPE_MEMORY_BACKEND_FILE:
20 * name of backend that uses mmap on a file descriptor
22 #define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
24 #define MEMORY_BACKEND_FILE(obj) \
25 OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
27 typedef struct HostMemoryBackendFile HostMemoryBackendFile
;
29 struct HostMemoryBackendFile
{
30 HostMemoryBackend parent_obj
;
37 file_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
39 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(backend
);
42 error_setg(errp
, "can't create backend with size 0");
46 error_setg(errp
, "mem-path property not set");
50 error_setg(errp
, "-mem-path not supported on this host");
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
,
63 file_backend_class_init(ObjectClass
*oc
, void *data
)
65 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
67 bc
->alloc
= file_backend_memory_alloc
;
70 static char *get_mem_path(Object
*o
, Error
**errp
)
72 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
74 return g_strdup(fb
->mem_path
);
77 static void set_mem_path(Object
*o
, const char *str
, Error
**errp
)
79 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
80 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
82 if (memory_region_size(&backend
->mr
)) {
83 error_setg(errp
, "cannot change property value");
89 fb
->mem_path
= g_strdup(str
);
92 static bool file_memory_backend_get_share(Object
*o
, Error
**errp
)
94 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
99 static void file_memory_backend_set_share(Object
*o
, bool value
, Error
**errp
)
101 HostMemoryBackend
*backend
= MEMORY_BACKEND(o
);
102 HostMemoryBackendFile
*fb
= MEMORY_BACKEND_FILE(o
);
104 if (memory_region_size(&backend
->mr
)) {
105 error_setg(errp
, "cannot change property value");
112 file_backend_instance_init(Object
*o
)
114 object_property_add_bool(o
, "share",
115 file_memory_backend_get_share
,
116 file_memory_backend_set_share
, NULL
);
117 object_property_add_str(o
, "mem-path", get_mem_path
,
121 static const TypeInfo file_backend_info
= {
122 .name
= TYPE_MEMORY_BACKEND_FILE
,
123 .parent
= TYPE_MEMORY_BACKEND
,
124 .class_init
= file_backend_class_init
,
125 .instance_init
= file_backend_instance_init
,
126 .instance_size
= sizeof(HostMemoryBackendFile
),
129 static void register_types(void)
131 type_register_static(&file_backend_info
);
134 type_init(register_types
);