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