Skip to content

Commit

Permalink
Add AMD Memory Encrypt detection (#140)
Browse files Browse the repository at this point in the history
This patch detects additional information of the AMD Memory Encryption
feature.

[0]:
https://www.amd.com/content/dam/amd/en/documents/processor-tech-docs/programmer-references/24594.pdf
table E.4.17

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
  • Loading branch information
Lencerf authored Nov 13, 2023
1 parent af2b49a commit f89c8c5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/cpuid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@ func main() {
if cpuid.CPU.SGX.Available {
fmt.Printf("SGX: %+v\n", cpuid.CPU.SGX)
}
if cpuid.CPU.AMDMemEncryption.Available {
fmt.Printf("AMD Memory Encryption: %+v\n", cpuid.CPU.AMDMemEncryption)
}
}
35 changes: 31 additions & 4 deletions cpuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,11 @@ type CPUInfo struct {
L2 int // L2 Cache (per core or shared). Will be -1 if undetected
L3 int // L3 Cache (per core, per ccx or shared). Will be -1 if undetected
}
SGX SGXSupport
AVX10Level uint8
maxFunc uint32
maxExFunc uint32
SGX SGXSupport
AMDMemEncryption AMDMemEncryptionSupport
AVX10Level uint8
maxFunc uint32
maxExFunc uint32
}

var cpuid func(op uint32) (eax, ebx, ecx, edx uint32)
Expand Down Expand Up @@ -1079,6 +1080,32 @@ func hasSGX(available, lc bool) (rval SGXSupport) {
return
}

type AMDMemEncryptionSupport struct {
Available bool
CBitPossition uint32
NumVMPL uint32
PhysAddrReduction uint32
NumEntryptedGuests uint32
MinSevNoEsAsid uint32
}

func hasAMDMemEncryption(available bool) (rval AMDMemEncryptionSupport) {
rval.Available = available
if !available {
return
}

_, b, c, d := cpuidex(0x8000001f, 0)

rval.CBitPossition = b & 0x3f
rval.PhysAddrReduction = (b >> 6) & 0x3F
rval.NumVMPL = (b >> 12) & 0xf
rval.NumEntryptedGuests = c
rval.MinSevNoEsAsid = d

return
}

func support() flagSet {
var fs flagSet
mfi := maxFunctionID()
Expand Down
10 changes: 10 additions & 0 deletions cpuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ func TestSGX(t *testing.T) {
}
}

// TestAMDMemEncryption tests AMDMemEncryption detection
func TestAMDMemEncryption(t *testing.T) {
got := CPU.AMDMemEncryption.Available
expected := CPU.featureSet.inSet(SME) || CPU.featureSet.inSet(SEV)
if got != expected {
t.Fatalf("AMDMemEncryption: expected %v, got %v", expected, got)
}
t.Log("AMDMemEncryption Support:", got)
}

func TestHas(t *testing.T) {
Detect()
defer Detect()
Expand Down
1 change: 1 addition & 0 deletions detect_x86.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func addInfo(c *CPUInfo, safe bool) {
c.Family, c.Model, c.Stepping = familyModel()
c.featureSet = support()
c.SGX = hasSGX(c.featureSet.inSet(SGX), c.featureSet.inSet(SGXLC))
c.AMDMemEncryption = hasAMDMemEncryption(c.featureSet.inSet(SME) || c.featureSet.inSet(SEV))
c.ThreadsPerCore = threadsPerCore()
c.LogicalCores = logicalCores()
c.PhysicalCores = physicalCores()
Expand Down

0 comments on commit f89c8c5

Please sign in to comment.