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