xref: /openbmc/qemu/backends/hostmem-ram.c (revision 7f709ce7)
1 /*
2  * QEMU Host Memory Backend
3  *
4  * Copyright (C) 2013-2014 Red Hat Inc
5  *
6  * Authors:
7  *   Igor Mammedov <imammedo@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 "qemu/osdep.h"
13 #include "sysemu/hostmem.h"
14 #include "qapi/error.h"
15 #include "qom/object_interfaces.h"
16 
17 #define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
18 
19 
20 static void
21 ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
22 {
23     char *path;
24 
25     if (!backend->size) {
26         error_setg(errp, "can't create backend with size 0");
27         return;
28     }
29 
30     path = object_get_canonical_path_component(OBJECT(backend));
31     memory_region_init_ram_nomigrate(&backend->mr, OBJECT(backend), path,
32                            backend->size, errp);
33     g_free(path);
34 }
35 
36 static void
37 ram_backend_class_init(ObjectClass *oc, void *data)
38 {
39     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
40 
41     bc->alloc = ram_backend_memory_alloc;
42 }
43 
44 static const TypeInfo ram_backend_info = {
45     .name = TYPE_MEMORY_BACKEND_RAM,
46     .parent = TYPE_MEMORY_BACKEND,
47     .class_init = ram_backend_class_init,
48 };
49 
50 static void register_types(void)
51 {
52     type_register_static(&ram_backend_info);
53 }
54 
55 type_init(register_types);
56