blob: 6c024d6217d23441f6894e550ab9d3144d93cdb5 [file] [log] [blame]
Sean Christophersonc6c02322021-07-19 19:21:05 +08001/*
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 */
Sean Christophersonc6c02322021-07-19 19:21:05 +080012
13#include "qemu/osdep.h"
Markus Armbruster2ca10fa2023-02-02 14:38:27 +010014#include <sys/ioctl.h>
Sean Christophersonc6c02322021-07-19 19:21:05 +080015#include "qom/object_interfaces.h"
16#include "qapi/error.h"
17#include "sysemu/hostmem.h"
18#include "hw/i386/hostmem-epc.h"
19
Philippe Mathieu-Daudéfdb63cf2023-11-20 13:50:52 +010020static bool
Sean Christophersonc6c02322021-07-19 19:21:05 +080021sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
22{
Philippe Mathieu-Daudé2d7a1eb2023-11-20 12:59:15 +010023 g_autofree char *name = NULL;
Sean Christophersonc6c02322021-07-19 19:21:05 +080024 uint32_t ram_flags;
Sean Christophersonc6c02322021-07-19 19:21:05 +080025 int fd;
26
27 if (!backend->size) {
28 error_setg(errp, "can't create backend with size 0");
Philippe Mathieu-Daudéfdb63cf2023-11-20 13:50:52 +010029 return false;
Sean Christophersonc6c02322021-07-19 19:21:05 +080030 }
31
Zhao Liu514d3032024-07-15 16:21:53 +080032 fd = qemu_open("/dev/sgx_vepc", O_RDWR, errp);
Sean Christophersonc6c02322021-07-19 19:21:05 +080033 if (fd < 0) {
Philippe Mathieu-Daudéfdb63cf2023-11-20 13:50:52 +010034 return false;
Sean Christophersonc6c02322021-07-19 19:21:05 +080035 }
36
Michal Privoznik5d9a9a62024-06-05 12:44:58 +020037 backend->aligned = true;
Sean Christophersonc6c02322021-07-19 19:21:05 +080038 name = object_get_canonical_path(OBJECT(backend));
39 ram_flags = (backend->share ? RAM_SHARED : 0) | RAM_PROTECTED;
Philippe Mathieu-Daudéfdb63cf2023-11-20 13:50:52 +010040 return memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), name,
41 backend->size, ram_flags, fd, 0, errp);
Sean Christophersonc6c02322021-07-19 19:21:05 +080042}
43
44static 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
53static 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
60static 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
68static 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
78type_init(register_types);