提交 16012460 编写于 作者: jia zhang's avatar jia zhang

rune/libenclave/intelsgx: Refactor cpuid implementation

Current cpuid is implemented in assembly, which disallows using cgo.
Signed-off-by: jia zhang's avatarJia Zhang <zhang.jia@linux.alibaba.com>
上级 b9fb10ec
......@@ -9,20 +9,6 @@ var (
maxEnclaveSizeBits uint32
)
// CPUID leafs
const (
cpuidExtendedFeatureFlags = 0x7
cpuidSgxFeature = 0x12
)
// CPUID leaf 0x12 sub-leafs
const (
sgxCapabilties = 0
sgxAttributes = 1
sgxEpcBaseSection = 2
maxSgxEpcSections = 8
)
const (
SigStructLength = 1808
EinittokenLength = 304
......@@ -145,11 +131,9 @@ type QuoteBody struct {
Basename [32]byte `struct:"[32]byte"`
}
func cpuid_low(leaf, subLeaf uint32) (eax, ebx, ecx, edx uint32)
// Check whether CPUs support SGX or not
func IsSgxSupported() bool {
_, ebx, _, _ := cpuid_low(cpuidExtendedFeatureFlags, 0)
_, ebx, _, _ := cpuid(cpuidExtendedFeatureFlags, 0)
if (ebx & 0x4) == 0x0 {
return false
}
......@@ -163,7 +147,7 @@ func GetSgxFeatures() {
return
}
eax, ebx, _, edx := cpuid_low(cpuidSgxFeature, sgxCapabilties)
eax, ebx, _, edx := cpuid(cpuidSgxFeature, sgxCapabilties)
if (eax & 0x1) != 0 {
sgx1Supported = true
}
......
package intelsgx // import "github.com/opencontainers/runc/libenclave/intelsgx"
/*
#include <stdlib.h>
#include <unistd.h>
static void cpuid(__uint32_t leaf, __uint32_t sub_leaf,
__uint32_t *eax, __uint32_t *ebx,
__uint32_t *ecx, __uint32_t *edx)
{
asm volatile("cpuid"
: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
: "0"(leaf), "2"(sub_leaf)
: "memory");
}
*/
import "C"
import "unsafe"
// CPUID leafs
const (
cpuidExtendedFeatureFlags = 0x7
cpuidSgxFeature = 0x12
)
// CPUID leaf 0x12 sub-leafs
const (
sgxCapabilties = 0
sgxAttributes = 1
sgxEpcBaseSection = 2
maxSgxEpcSections = 8
)
func cpuid(leaf uint32, subLeaf uint32) (uint32, uint32, uint32, uint32) {
var (
eax uint32
ebx uint32
ecx uint32
edx uint32
)
C.cpuid(C.uint(leaf), C.uint(subLeaf), (*C.uint)(unsafe.Pointer(&eax)),
(*C.uint)(unsafe.Pointer(&ebx)), (*C.uint)(unsafe.Pointer(&ecx)),
(*C.uint)(unsafe.Pointer(&edx)))
return eax, ebx, ecx, edx
}
#include "textflag.h"
// func cpuid_low(leaf, subLeaf uint32) (eax, ebx, ecx, edx uint32)
TEXT ·cpuid_low(SB),NOSPLIT,$0-24
MOVL arg1+0(FP), AX
MOVL arg2+4(FP), CX
CPUID
MOVL AX, eax+8(FP)
MOVL BX, ebx+12(FP)
MOVL CX, ecx+16(FP)
MOVL DX, edx+20(FP)
RET
......@@ -10,7 +10,7 @@ func GetEpcSections() []SgxEpcSection {
sections := []SgxEpcSection{}
for i := 0; i < maxSgxEpcSections; i++ {
eax, ebx, ecx, edx := cpuid_low(cpuidSgxFeature, uint32(sgxEpcBaseSection+i))
eax, ebx, ecx, edx := cpuid(cpuidSgxFeature, uint32(sgxEpcBaseSection+i))
if (eax & 0xf) == 0x0 {
break
......
......@@ -5,7 +5,7 @@ var (
)
func GetSgxLaunchControl() {
_, _, ecx, _ := cpuid_low(cpuidExtendedFeatureFlags, 0)
_, _, ecx, _ := cpuid(cpuidExtendedFeatureFlags, 0)
if (ecx & 0x40000000) != 0 {
sgxLaunchControlSupported = true
}
......
package intelsgx // import "github.com/opencontainers/runc/libenclave/intelsgx"
func getSecsAttributes() (uint32, uint32, uint32, uint32) {
eax, ebx, ecx, edx := cpuid_low(cpuidSgxFeature, sgxAttributes)
eax, ebx, ecx, edx := cpuid(cpuidSgxFeature, sgxAttributes)
return eax, ebx, ecx, edx
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册