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