Skip to content

Commit f89c8c5

Browse files
authored
Add AMD Memory Encrypt detection (#140)
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>
1 parent af2b49a commit f89c8c5

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

cmd/cpuid/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,7 @@ func main() {
8282
if cpuid.CPU.SGX.Available {
8383
fmt.Printf("SGX: %+v\n", cpuid.CPU.SGX)
8484
}
85+
if cpuid.CPU.AMDMemEncryption.Available {
86+
fmt.Printf("AMD Memory Encryption: %+v\n", cpuid.CPU.AMDMemEncryption)
87+
}
8588
}

cpuid.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,11 @@ type CPUInfo struct {
309309
L2 int // L2 Cache (per core or shared). Will be -1 if undetected
310310
L3 int // L3 Cache (per core, per ccx or shared). Will be -1 if undetected
311311
}
312-
SGX SGXSupport
313-
AVX10Level uint8
314-
maxFunc uint32
315-
maxExFunc uint32
312+
SGX SGXSupport
313+
AMDMemEncryption AMDMemEncryptionSupport
314+
AVX10Level uint8
315+
maxFunc uint32
316+
maxExFunc uint32
316317
}
317318

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

1083+
type AMDMemEncryptionSupport struct {
1084+
Available bool
1085+
CBitPossition uint32
1086+
NumVMPL uint32
1087+
PhysAddrReduction uint32
1088+
NumEntryptedGuests uint32
1089+
MinSevNoEsAsid uint32
1090+
}
1091+
1092+
func hasAMDMemEncryption(available bool) (rval AMDMemEncryptionSupport) {
1093+
rval.Available = available
1094+
if !available {
1095+
return
1096+
}
1097+
1098+
_, b, c, d := cpuidex(0x8000001f, 0)
1099+
1100+
rval.CBitPossition = b & 0x3f
1101+
rval.PhysAddrReduction = (b >> 6) & 0x3F
1102+
rval.NumVMPL = (b >> 12) & 0xf
1103+
rval.NumEntryptedGuests = c
1104+
rval.MinSevNoEsAsid = d
1105+
1106+
return
1107+
}
1108+
10821109
func support() flagSet {
10831110
var fs flagSet
10841111
mfi := maxFunctionID()

cpuid_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ func TestSGX(t *testing.T) {
136136
}
137137
}
138138

139+
// TestAMDMemEncryption tests AMDMemEncryption detection
140+
func TestAMDMemEncryption(t *testing.T) {
141+
got := CPU.AMDMemEncryption.Available
142+
expected := CPU.featureSet.inSet(SME) || CPU.featureSet.inSet(SEV)
143+
if got != expected {
144+
t.Fatalf("AMDMemEncryption: expected %v, got %v", expected, got)
145+
}
146+
t.Log("AMDMemEncryption Support:", got)
147+
}
148+
139149
func TestHas(t *testing.T) {
140150
Detect()
141151
defer Detect()

detect_x86.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func addInfo(c *CPUInfo, safe bool) {
2727
c.Family, c.Model, c.Stepping = familyModel()
2828
c.featureSet = support()
2929
c.SGX = hasSGX(c.featureSet.inSet(SGX), c.featureSet.inSet(SGXLC))
30+
c.AMDMemEncryption = hasAMDMemEncryption(c.featureSet.inSet(SME) || c.featureSet.inSet(SEV))
3031
c.ThreadsPerCore = threadsPerCore()
3132
c.LogicalCores = logicalCores()
3233
c.PhysicalCores = physicalCores()

0 commit comments

Comments
 (0)