2 * QEMU host SGX EPC memory backend
4 * Copyright (C) 2019 Intel Corporation
7 * Sean Christopherson <sean.j.christopherson@intel.com>
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.
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"
21 sgx_epc_backend_memory_alloc(HostMemoryBackend
*backend
, Error
**errp
)
23 g_autofree
char *name
= NULL
;
28 error_setg(errp
, "can't create backend with size 0");
32 fd
= qemu_open_old("/dev/sgx_vepc", O_RDWR
);
34 error_setg_errno(errp
, errno
,
35 "failed to open /dev/sgx_vepc to alloc SGX EPC");
39 backend
->aligned
= true;
40 name
= object_get_canonical_path(OBJECT(backend
));
41 ram_flags
= (backend
->share
? RAM_SHARED
: 0) | RAM_PROTECTED
;
42 return memory_region_init_ram_from_fd(&backend
->mr
, OBJECT(backend
), name
,
43 backend
->size
, ram_flags
, fd
, 0, errp
);
46 static void sgx_epc_backend_instance_init(Object
*obj
)
48 HostMemoryBackend
*m
= MEMORY_BACKEND(obj
);
55 static void sgx_epc_backend_class_init(ObjectClass
*oc
, void *data
)
57 HostMemoryBackendClass
*bc
= MEMORY_BACKEND_CLASS(oc
);
59 bc
->alloc
= sgx_epc_backend_memory_alloc
;
62 static const TypeInfo sgx_epc_backed_info
= {
63 .name
= TYPE_MEMORY_BACKEND_EPC
,
64 .parent
= TYPE_MEMORY_BACKEND
,
65 .instance_init
= sgx_epc_backend_instance_init
,
66 .class_init
= sgx_epc_backend_class_init
,
67 .instance_size
= sizeof(HostMemoryBackendEpc
),
70 static void register_types(void)
72 int fd
= qemu_open_old("/dev/sgx_vepc", O_RDWR
);
76 type_register_static(&sgx_epc_backed_info
);
80 type_init(register_types
);