xref: /openbmc/qemu/backends/hostmem-epc.c (revision eed0e8ff)
1 /*
2  * QEMU host SGX EPC memory backend
3  *
4  * Copyright (C) 2019 Intel Corporation
5  *
6  * Authors:
7  *   Sean Christopherson <sean.j.christopherson@intel.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 
13 #include "qemu/osdep.h"
14 #include <sys/ioctl.h>
15 #include "qom/object_interfaces.h"
16 #include "qapi/error.h"
17 #include "sysemu/hostmem.h"
18 #include "hw/i386/hostmem-epc.h"
19 
20 static bool
21 sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
22 {
23     g_autofree char *name = NULL;
24     uint32_t ram_flags;
25     int fd;
26 
27     if (!backend->size) {
28         error_setg(errp, "can't create backend with size 0");
29         return false;
30     }
31 
32     fd = qemu_open("/dev/sgx_vepc", O_RDWR, errp);
33     if (fd < 0) {
34         return false;
35     }
36 
37     backend->aligned = true;
38     name = object_get_canonical_path(OBJECT(backend));
39     ram_flags = (backend->share ? RAM_SHARED : 0) | RAM_PROTECTED;
40     return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
41                                           backend->size, ram_flags, fd, 0, errp);
42 }
43 
44 static void sgx_epc_backend_instance_init(Object *obj)
45 {
46     HostMemoryBackend *m = MEMORY_BACKEND(obj);
47 
48     m->share = true;
49     m->merge = false;
50     m->dump = false;
51 }
52 
53 static void sgx_epc_backend_class_init(ObjectClass *oc, void *data)
54 {
55     HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
56 
57     bc->alloc = sgx_epc_backend_memory_alloc;
58 }
59 
60 static const TypeInfo sgx_epc_backed_info = {
61     .name = TYPE_MEMORY_BACKEND_EPC,
62     .parent = TYPE_MEMORY_BACKEND,
63     .instance_init = sgx_epc_backend_instance_init,
64     .class_init = sgx_epc_backend_class_init,
65     .instance_size = sizeof(HostMemoryBackendEpc),
66 };
67 
68 static void register_types(void)
69 {
70     int fd = qemu_open_old("/dev/sgx_vepc", O_RDWR);
71     if (fd >= 0) {
72         close(fd);
73 
74         type_register_static(&sgx_epc_backed_info);
75     }
76 }
77 
78 type_init(register_types);
79