1c5348c6aSSean ChristophersonSoftware Guard eXtensions (SGX) 2c5348c6aSSean Christopherson=============================== 3c5348c6aSSean Christopherson 4c5348c6aSSean ChristophersonOverview 5c5348c6aSSean Christopherson-------- 6c5348c6aSSean Christopherson 7c5348c6aSSean ChristophersonIntel Software Guard eXtensions (SGX) is a set of instructions and mechanisms 8c5348c6aSSean Christophersonfor memory accesses in order to provide security accesses for sensitive 9*f6822feeSStefan Weilapplications and data. SGX allows an application to use its particular 10c5348c6aSSean Christophersonaddress space as an *enclave*, which is a protected area provides confidentiality 11c5348c6aSSean Christophersonand integrity even in the presence of privileged malware. Accesses to the 12c5348c6aSSean Christophersonenclave memory area from any software not resident in the enclave are prevented, 13c5348c6aSSean Christophersonincluding those from privileged software. 14c5348c6aSSean Christopherson 15c5348c6aSSean ChristophersonVirtual SGX 16c5348c6aSSean Christopherson----------- 17c5348c6aSSean Christopherson 18c5348c6aSSean ChristophersonSGX feature is exposed to guest via SGX CPUID. Looking at SGX CPUID, we can 19c5348c6aSSean Christophersonreport the same CPUID info to guest as on host for most of SGX CPUID. With 20c5348c6aSSean Christophersonreporting the same CPUID guest is able to use full capacity of SGX, and KVM 21c5348c6aSSean Christophersondoesn't need to emulate those info. 22c5348c6aSSean Christopherson 235135fe71SPhilippe Mathieu-DaudéThe guest's EPC base and size are determined by QEMU, and KVM needs QEMU to 24c5348c6aSSean Christophersonnotify such info to it before it can initialize SGX for guest. 25c5348c6aSSean Christopherson 26c5348c6aSSean ChristophersonVirtual EPC 27c5348c6aSSean Christopherson~~~~~~~~~~~ 28c5348c6aSSean Christopherson 295135fe71SPhilippe Mathieu-DaudéBy default, QEMU does not assign EPC to a VM, i.e. fully enabling SGX in a VM 30c5348c6aSSean Christophersonrequires explicit allocation of EPC to the VM. Similar to other specialized 31c5348c6aSSean Christophersonmemory types, e.g. hugetlbfs, EPC is exposed as a memory backend. 32c5348c6aSSean Christopherson 33c5348c6aSSean ChristophersonSGX EPC is enumerated through CPUID, i.e. EPC "devices" need to be realized 34c5348c6aSSean Christophersonprior to realizing the vCPUs themselves, which occurs long before generic 35c5348c6aSSean Christophersondevices are parsed and realized. This limitation means that EPC does not 36c5348c6aSSean Christophersonrequire -maxmem as EPC is not treated as {cold,hot}plugged memory. 37c5348c6aSSean Christopherson 385135fe71SPhilippe Mathieu-DaudéQEMU does not artificially restrict the number of EPC sections exposed to a 395135fe71SPhilippe Mathieu-Daudéguest, e.g. QEMU will happily allow you to create 64 1M EPC sections. Be aware 40c5348c6aSSean Christophersonthat some kernels may not recognize all EPC sections, e.g. the Linux SGX driver 41c5348c6aSSean Christophersonis hardwired to support only 8 EPC sections. 42c5348c6aSSean Christopherson 435135fe71SPhilippe Mathieu-DaudéThe following QEMU snippet creates two EPC sections, with 64M pre-allocated 44c5348c6aSSean Christophersonto the VM and an additional 28M mapped but not allocated:: 45c5348c6aSSean Christopherson 46c5348c6aSSean Christopherson -object memory-backend-epc,id=mem1,size=64M,prealloc=on \ 47c5348c6aSSean Christopherson -object memory-backend-epc,id=mem2,size=28M \ 48c5348c6aSSean Christopherson -M sgx-epc.0.memdev=mem1,sgx-epc.1.memdev=mem2 49c5348c6aSSean Christopherson 50c5348c6aSSean ChristophersonNote: 51c5348c6aSSean Christopherson 52c5348c6aSSean ChristophersonThe size and location of the virtual EPC are far less restricted compared 53c5348c6aSSean Christophersonto physical EPC. Because physical EPC is protected via range registers, 54c5348c6aSSean Christophersonthe size of the physical EPC must be a power of two (though software sees 55c5348c6aSSean Christophersona subset of the full EPC, e.g. 92M or 128M) and the EPC must be naturally 56c5348c6aSSean Christophersonaligned. KVM SGX's virtual EPC is purely a software construct and only 575135fe71SPhilippe Mathieu-Daudérequires the size and location to be page aligned. QEMU enforces the EPC 58c5348c6aSSean Christophersonsize is a multiple of 4k and will ensure the base of the EPC is 4k aligned. 59c5348c6aSSean ChristophersonTo simplify the implementation, EPC is always located above 4g in the guest 60c5348c6aSSean Christophersonphysical address space. 61c5348c6aSSean Christopherson 62c5348c6aSSean ChristophersonMigration 63c5348c6aSSean Christopherson~~~~~~~~~ 64c5348c6aSSean Christopherson 655135fe71SPhilippe Mathieu-DaudéQEMU/KVM doesn't prevent live migrating SGX VMs, although from hardware's 66c5348c6aSSean Christophersonperspective, SGX doesn't support live migration, since both EPC and the SGX 67c5348c6aSSean Christophersonkey hierarchy are bound to the physical platform. However live migration 68c5348c6aSSean Christophersoncan be supported in the sense if guest software stack can support recreating 69c5348c6aSSean Christophersonenclaves when it suffers sudden lose of EPC; and if guest enclaves can detect 70c5348c6aSSean ChristophersonSGX keys being changed, and handle gracefully. For instance, when ERESUME fails 71c5348c6aSSean Christophersonwith #PF.SGX, guest software can gracefully detect it and recreate enclaves; 72c5348c6aSSean Christophersonand when enclave fails to unseal sensitive information from outside, it can 73c5348c6aSSean Christophersondetect such error and sensitive information can be provisioned to it again. 74c5348c6aSSean Christopherson 75c5348c6aSSean ChristophersonCPUID 76c5348c6aSSean Christopherson~~~~~ 77c5348c6aSSean Christopherson 78c5348c6aSSean ChristophersonDue to its myriad dependencies, SGX is currently not listed as supported 795135fe71SPhilippe Mathieu-Daudéin any of QEMU's built-in CPU configuration. To expose SGX (and SGX Launch 80ca0a0d12SJohn SnowControl) to a guest, you must either use ``-cpu host`` to pass-through the 81c5348c6aSSean Christophersonhost CPU model, or explicitly enable SGX when using a built-in CPU model, 82ca0a0d12SJohn Snowe.g. via ``-cpu <model>,+sgx`` or ``-cpu <model>,+sgx,+sgxlc``. 83c5348c6aSSean Christopherson 84c5348c6aSSean ChristophersonAll SGX sub-features enumerated through CPUID, e.g. SGX2, MISCSELECT, 85c5348c6aSSean ChristophersonATTRIBUTES, etc... can be restricted via CPUID flags. Be aware that enforcing 86c5348c6aSSean Christophersonrestriction of MISCSELECT, ATTRIBUTES and XFRM requires intercepting ECREATE, 87c5348c6aSSean Christophersoni.e. may marginally reduce SGX performance in the guest. All SGX sub-features 88c5348c6aSSean Christophersoncontrolled via -cpu are prefixed with "sgx", e.g.:: 89c5348c6aSSean Christopherson 90c5348c6aSSean Christopherson $ qemu-system-x86_64 -cpu help | xargs printf "%s\n" | grep sgx 91c5348c6aSSean Christopherson sgx 92c5348c6aSSean Christopherson sgx-debug 93c5348c6aSSean Christopherson sgx-encls-c 94c5348c6aSSean Christopherson sgx-enclv 95c5348c6aSSean Christopherson sgx-exinfo 96c5348c6aSSean Christopherson sgx-kss 97c5348c6aSSean Christopherson sgx-mode64 98c5348c6aSSean Christopherson sgx-provisionkey 99c5348c6aSSean Christopherson sgx-tokenkey 100c5348c6aSSean Christopherson sgx1 101c5348c6aSSean Christopherson sgx2 102c5348c6aSSean Christopherson sgxlc 103c5348c6aSSean Christopherson 1045135fe71SPhilippe Mathieu-DaudéThe following QEMU snippet passes through the host CPU but restricts access to 105c5348c6aSSean Christophersonthe provision and EINIT token keys:: 106c5348c6aSSean Christopherson 107c5348c6aSSean Christopherson -cpu host,-sgx-provisionkey,-sgx-tokenkey 108c5348c6aSSean Christopherson 109c5348c6aSSean ChristophersonSGX sub-features cannot be emulated, i.e. sub-features that are not present 110c5348c6aSSean Christophersonin hardware cannot be forced on via '-cpu'. 111c5348c6aSSean Christopherson 112c5348c6aSSean ChristophersonVirtualize SGX Launch Control 113c5348c6aSSean Christopherson~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 114c5348c6aSSean Christopherson 1155135fe71SPhilippe Mathieu-DaudéQEMU SGX support for Launch Control (LC) is passive, in the sense that it 1165135fe71SPhilippe Mathieu-Daudédoes not actively change the LC configuration. QEMU SGX provides the user 117c5348c6aSSean Christophersonthe ability to set/clear the CPUID flag (and by extension the associated 118c5348c6aSSean ChristophersonIA32_FEATURE_CONTROL MSR bit in fw_cfg) and saves/restores the LE Hash MSRs 1195135fe71SPhilippe Mathieu-Daudéwhen getting/putting guest state, but QEMU does not add new controls to 120c5348c6aSSean Christophersondirectly modify the LC configuration. Similar to hardware behavior, locking 121c5348c6aSSean Christophersonthe LC configuration to a non-Intel value is left to guest firmware. Unlike 122c5348c6aSSean Christophersonhost bios setting for SGX launch control(LC), there is no special bios setting 123c5348c6aSSean Christophersonfor SGX guest by our design. If host is in locked mode, we can still allow 124c5348c6aSSean Christophersoncreating VM with SGX. 125c5348c6aSSean Christopherson 126c5348c6aSSean ChristophersonFeature Control 127c5348c6aSSean Christopherson~~~~~~~~~~~~~~~ 128c5348c6aSSean Christopherson 1295135fe71SPhilippe Mathieu-DaudéQEMU SGX updates the ``etc/msr_feature_control`` fw_cfg entry to set the SGX 130c5348c6aSSean Christopherson(bit 18) and SGX LC (bit 17) flags based on their respective CPUID support, 131c5348c6aSSean Christophersoni.e. existing guest firmware will automatically set SGX and SGX LC accordingly, 132c5348c6aSSean Christophersonassuming said firmware supports fw_cfg.msr_feature_control. 133c5348c6aSSean Christopherson 134c5348c6aSSean ChristophersonLaunching a guest 135c5348c6aSSean Christopherson----------------- 136c5348c6aSSean Christopherson 137c5348c6aSSean ChristophersonTo launch a SGX guest: 138c5348c6aSSean Christopherson 139c5348c6aSSean Christopherson.. parsed-literal:: 140c5348c6aSSean Christopherson 141c5348c6aSSean Christopherson |qemu_system_x86| \\ 142c5348c6aSSean Christopherson -cpu host,+sgx-provisionkey \\ 143c5348c6aSSean Christopherson -object memory-backend-epc,id=mem1,size=64M,prealloc=on \\ 144d1889b36SYang Zhong -M sgx-epc.0.memdev=mem1,sgx-epc.0.node=0 145c5348c6aSSean Christopherson 146c5348c6aSSean ChristophersonUtilizing SGX in the guest requires a kernel/OS with SGX support. 147c5348c6aSSean ChristophersonThe support can be determined in guest by:: 148c5348c6aSSean Christopherson 149c5348c6aSSean Christopherson $ grep sgx /proc/cpuinfo 150c5348c6aSSean Christopherson 151c5348c6aSSean Christophersonand SGX epc info by:: 152c5348c6aSSean Christopherson 153c5348c6aSSean Christopherson $ dmesg | grep sgx 154d1889b36SYang Zhong [ 0.182807] sgx: EPC section 0x140000000-0x143ffffff 155d1889b36SYang Zhong [ 0.183695] sgx: [Firmware Bug]: Unable to map EPC section to online node. Fallback to the NUMA node 0. 156d1889b36SYang Zhong 157d1889b36SYang ZhongTo launch a SGX numa guest: 158d1889b36SYang Zhong 159d1889b36SYang Zhong.. parsed-literal:: 160d1889b36SYang Zhong 161d1889b36SYang Zhong |qemu_system_x86| \\ 162d1889b36SYang Zhong -cpu host,+sgx-provisionkey \\ 163d1889b36SYang Zhong -object memory-backend-ram,size=2G,host-nodes=0,policy=bind,id=node0 \\ 164d1889b36SYang Zhong -object memory-backend-epc,id=mem0,size=64M,prealloc=on,host-nodes=0,policy=bind \\ 165d1889b36SYang Zhong -numa node,nodeid=0,cpus=0-1,memdev=node0 \\ 166d1889b36SYang Zhong -object memory-backend-ram,size=2G,host-nodes=1,policy=bind,id=node1 \\ 167d1889b36SYang Zhong -object memory-backend-epc,id=mem1,size=28M,prealloc=on,host-nodes=1,policy=bind \\ 168d1889b36SYang Zhong -numa node,nodeid=1,cpus=2-3,memdev=node1 \\ 169d1889b36SYang Zhong -M sgx-epc.0.memdev=mem0,sgx-epc.0.node=0,sgx-epc.1.memdev=mem1,sgx-epc.1.node=1 170d1889b36SYang Zhong 171d1889b36SYang Zhongand SGX epc numa info by:: 172d1889b36SYang Zhong 173d1889b36SYang Zhong $ dmesg | grep sgx 174d1889b36SYang Zhong [ 0.369937] sgx: EPC section 0x180000000-0x183ffffff 175d1889b36SYang Zhong [ 0.370259] sgx: EPC section 0x184000000-0x185bfffff 176d1889b36SYang Zhong 177d1889b36SYang Zhong $ dmesg | grep SRAT 178d1889b36SYang Zhong [ 0.009981] ACPI: SRAT: Node 0 PXM 0 [mem 0x180000000-0x183ffffff] 179d1889b36SYang Zhong [ 0.009982] ACPI: SRAT: Node 1 PXM 1 [mem 0x184000000-0x185bfffff] 180c5348c6aSSean Christopherson 181c5348c6aSSean ChristophersonReferences 182c5348c6aSSean Christopherson---------- 183c5348c6aSSean Christopherson 184c5348c6aSSean Christopherson- `SGX Homepage <https://software.intel.com/sgx>`__ 185c5348c6aSSean Christopherson 186c5348c6aSSean Christopherson- `SGX SDK <https://github.com/intel/linux-sgx.git>`__ 187c5348c6aSSean Christopherson 188c5348c6aSSean Christopherson- SGX specification: Intel SDM Volume 3 189