1*c6c02320SSean Christopherson /* 2*c6c02320SSean Christopherson * QEMU host SGX EPC memory backend 3*c6c02320SSean Christopherson * 4*c6c02320SSean Christopherson * Copyright (C) 2019 Intel Corporation 5*c6c02320SSean Christopherson * 6*c6c02320SSean Christopherson * Authors: 7*c6c02320SSean Christopherson * Sean Christopherson <sean.j.christopherson@intel.com> 8*c6c02320SSean Christopherson * 9*c6c02320SSean Christopherson * This work is licensed under the terms of the GNU GPL, version 2 or later. 10*c6c02320SSean Christopherson * See the COPYING file in the top-level directory. 11*c6c02320SSean Christopherson */ 12*c6c02320SSean Christopherson #include <sys/ioctl.h> 13*c6c02320SSean Christopherson 14*c6c02320SSean Christopherson #include "qemu/osdep.h" 15*c6c02320SSean Christopherson #include "qemu-common.h" 16*c6c02320SSean Christopherson #include "qom/object_interfaces.h" 17*c6c02320SSean Christopherson #include "qapi/error.h" 18*c6c02320SSean Christopherson #include "sysemu/hostmem.h" 19*c6c02320SSean Christopherson #include "hw/i386/hostmem-epc.h" 20*c6c02320SSean Christopherson 21*c6c02320SSean Christopherson static void 22*c6c02320SSean Christopherson sgx_epc_backend_memory_alloc(HostMemoryBackend *backend, Error **errp) 23*c6c02320SSean Christopherson { 24*c6c02320SSean Christopherson uint32_t ram_flags; 25*c6c02320SSean Christopherson char *name; 26*c6c02320SSean Christopherson int fd; 27*c6c02320SSean Christopherson 28*c6c02320SSean Christopherson if (!backend->size) { 29*c6c02320SSean Christopherson error_setg(errp, "can't create backend with size 0"); 30*c6c02320SSean Christopherson return; 31*c6c02320SSean Christopherson } 32*c6c02320SSean Christopherson 33*c6c02320SSean Christopherson fd = qemu_open_old("/dev/sgx_vepc", O_RDWR); 34*c6c02320SSean Christopherson if (fd < 0) { 35*c6c02320SSean Christopherson error_setg_errno(errp, errno, 36*c6c02320SSean Christopherson "failed to open /dev/sgx_vepc to alloc SGX EPC"); 37*c6c02320SSean Christopherson return; 38*c6c02320SSean Christopherson } 39*c6c02320SSean Christopherson 40*c6c02320SSean Christopherson name = object_get_canonical_path(OBJECT(backend)); 41*c6c02320SSean Christopherson ram_flags = (backend->share ? RAM_SHARED : 0) | RAM_PROTECTED; 42*c6c02320SSean Christopherson memory_region_init_ram_from_fd(&backend->mr, OBJECT(backend), 43*c6c02320SSean Christopherson name, backend->size, ram_flags, 44*c6c02320SSean Christopherson fd, 0, errp); 45*c6c02320SSean Christopherson g_free(name); 46*c6c02320SSean Christopherson } 47*c6c02320SSean Christopherson 48*c6c02320SSean Christopherson static void sgx_epc_backend_instance_init(Object *obj) 49*c6c02320SSean Christopherson { 50*c6c02320SSean Christopherson HostMemoryBackend *m = MEMORY_BACKEND(obj); 51*c6c02320SSean Christopherson 52*c6c02320SSean Christopherson m->share = true; 53*c6c02320SSean Christopherson m->merge = false; 54*c6c02320SSean Christopherson m->dump = false; 55*c6c02320SSean Christopherson } 56*c6c02320SSean Christopherson 57*c6c02320SSean Christopherson static void sgx_epc_backend_class_init(ObjectClass *oc, void *data) 58*c6c02320SSean Christopherson { 59*c6c02320SSean Christopherson HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc); 60*c6c02320SSean Christopherson 61*c6c02320SSean Christopherson bc->alloc = sgx_epc_backend_memory_alloc; 62*c6c02320SSean Christopherson } 63*c6c02320SSean Christopherson 64*c6c02320SSean Christopherson static const TypeInfo sgx_epc_backed_info = { 65*c6c02320SSean Christopherson .name = TYPE_MEMORY_BACKEND_EPC, 66*c6c02320SSean Christopherson .parent = TYPE_MEMORY_BACKEND, 67*c6c02320SSean Christopherson .instance_init = sgx_epc_backend_instance_init, 68*c6c02320SSean Christopherson .class_init = sgx_epc_backend_class_init, 69*c6c02320SSean Christopherson .instance_size = sizeof(HostMemoryBackendEpc), 70*c6c02320SSean Christopherson }; 71*c6c02320SSean Christopherson 72*c6c02320SSean Christopherson static void register_types(void) 73*c6c02320SSean Christopherson { 74*c6c02320SSean Christopherson int fd = qemu_open_old("/dev/sgx_vepc", O_RDWR); 75*c6c02320SSean Christopherson if (fd >= 0) { 76*c6c02320SSean Christopherson close(fd); 77*c6c02320SSean Christopherson 78*c6c02320SSean Christopherson type_register_static(&sgx_epc_backed_info); 79*c6c02320SSean Christopherson } 80*c6c02320SSean Christopherson } 81*c6c02320SSean Christopherson 82*c6c02320SSean Christopherson type_init(register_types); 83