target/i386: Add HMP and QMP interfaces for SGX
[qemu/armbru.git] / hw / i386 / sgx.c
blobea753985750d9aa8762f9c01accfbdeb5937831d
1 /*
2 * SGX common code
4 * Copyright (C) 2021 Intel Corporation
6 * Authors:
7 * Yang Zhong<yang.zhong@intel.com>
8 * Sean Christopherson <sean.j.christopherson@intel.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "hw/i386/pc.h"
15 #include "hw/i386/sgx-epc.h"
16 #include "hw/mem/memory-device.h"
17 #include "monitor/qdev.h"
18 #include "qapi/error.h"
19 #include "exec/address-spaces.h"
20 #include "hw/i386/sgx.h"
22 SGXInfo *sgx_get_info(Error **errp)
24 SGXInfo *info = NULL;
25 X86MachineState *x86ms;
26 PCMachineState *pcms =
27 (PCMachineState *)object_dynamic_cast(qdev_get_machine(),
28 TYPE_PC_MACHINE);
29 if (!pcms) {
30 error_setg(errp, "SGX is only supported on PC machines");
31 return NULL;
34 x86ms = X86_MACHINE(pcms);
35 if (!x86ms->sgx_epc_list) {
36 error_setg(errp, "No EPC regions defined, SGX not available");
37 return NULL;
40 SGXEPCState *sgx_epc = &pcms->sgx_epc;
41 info = g_new0(SGXInfo, 1);
43 info->sgx = true;
44 info->sgx1 = true;
45 info->sgx2 = true;
46 info->flc = true;
47 info->section_size = sgx_epc->size;
49 return info;
52 int sgx_epc_get_section(int section_nr, uint64_t *addr, uint64_t *size)
54 PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
55 SGXEPCDevice *epc;
57 if (pcms->sgx_epc.size == 0 || pcms->sgx_epc.nr_sections <= section_nr) {
58 return 1;
61 epc = pcms->sgx_epc.sections[section_nr];
63 *addr = epc->addr;
64 *size = memory_device_get_region_size(MEMORY_DEVICE(epc), &error_fatal);
66 return 0;
69 void pc_machine_init_sgx_epc(PCMachineState *pcms)
71 SGXEPCState *sgx_epc = &pcms->sgx_epc;
72 X86MachineState *x86ms = X86_MACHINE(pcms);
73 SgxEPCList *list = NULL;
74 Object *obj;
76 memset(sgx_epc, 0, sizeof(SGXEPCState));
77 if (!x86ms->sgx_epc_list) {
78 return;
81 sgx_epc->base = 0x100000000ULL + x86ms->above_4g_mem_size;
83 memory_region_init(&sgx_epc->mr, OBJECT(pcms), "sgx-epc", UINT64_MAX);
84 memory_region_add_subregion(get_system_memory(), sgx_epc->base,
85 &sgx_epc->mr);
87 for (list = x86ms->sgx_epc_list; list; list = list->next) {
88 obj = object_new("sgx-epc");
90 /* set the memdev link with memory backend */
91 object_property_parse(obj, SGX_EPC_MEMDEV_PROP, list->value->memdev,
92 &error_fatal);
93 object_property_set_bool(obj, "realized", true, &error_fatal);
94 object_unref(obj);
97 if ((sgx_epc->base + sgx_epc->size) < sgx_epc->base) {
98 error_report("Size of all 'sgx-epc' =0x%"PRIu64" causes EPC to wrap",
99 sgx_epc->size);
100 exit(EXIT_FAILURE);
103 memory_region_set_size(&sgx_epc->mr, sgx_epc->size);