~ knotless Search/
↑↓ move↵ open⇧↵ all results

socialweb.computer/socialweb-computer-kcp

socialweb.computer: a compute market on atproto (kcp)

1 collaborator
0
0
36 lines · 1.2 KB Raw History
//go:build linux

package vmruntime

import (
	"os"
	"regexp"
)

var sgxFlag = regexp.MustCompile(`(?m)^flags\s*:.*\bsgx\b`)

// HostSGXDisabled reports whether the CPU advertises sgx while the host kernel does not drive it: the sgx flag
// is in /proc/cpuinfo and /dev/sgx_enclave is absent (the kernel logs "SGX launch control CPU feature is not
// available" and creates no enclave device). On such a host KVM still reports the SGX CPUID leaves, and QEMU
// asserts in sgx_epc_get_section while it builds them (cpu_x86_cpuid under kvm_arch_init_vcpu), so every VM
// segfaults at start.
func HostSGXDisabled() bool {
	cpuinfo, err := os.ReadFile("/proc/cpuinfo")
	if err != nil || !sgxFlag.Match(cpuinfo) {
		return false
	}
	_, err = os.Stat("/dev/sgx_enclave")
	return err != nil
}

// qemuSGXOff is the host-passthrough feature customization that hides SGX from the guest
// (https://qemu-project.gitlab.io/qemu/system/qemu-cpu-models.html): sgx is CPUID.7.0:EBX bit 2, sgxlc its
// launch-control bit in ECX. Every name is accepted by the bundled QEMU 8.2 (`-cpu host,?`).
const qemuSGXOff = ",sgx=off,sgxlc=off"

func qemuCPU(sgxDisabled bool) string {
	if sgxDisabled {
		return "host" + qemuSGXOff
	}
	return "host"
}