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