From e4879b9f8a299673d922feeee812cead509943bc Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Wed, 28 Jun 2017 20:16:58 +0100 Subject: [PATCH] Support the CPUID instruction --- src/gopheros/kernel/cpu/cpu_amd64.go | 17 +++++++++++++ src/gopheros/kernel/cpu/cpu_amd64.s | 9 +++++++ src/gopheros/kernel/cpu/cpu_amd64_test.go | 29 +++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/gopheros/kernel/cpu/cpu_amd64_test.go diff --git a/src/gopheros/kernel/cpu/cpu_amd64.go b/src/gopheros/kernel/cpu/cpu_amd64.go index c757266..4d5cac4 100644 --- a/src/gopheros/kernel/cpu/cpu_amd64.go +++ b/src/gopheros/kernel/cpu/cpu_amd64.go @@ -1,5 +1,9 @@ package cpu +var ( + cpuidFn = ID +) + // EnableInterrupts enables interrupt handling. func EnableInterrupts() @@ -21,3 +25,16 @@ func ActivePDT() uintptr // ReadCR2 returns the value stored in the CR2 register. func ReadCR2() uint64 + +// ID returns information about the CPU and its features. It +// is implemented as a CPUID instruction with EAX=leaf and +// returns the values in EAX, EBX, ECX and EDX. +func ID(leaf uint32) (uint32, uint32, uint32, uint32) + +// IsIntel returns true if the code is running on an Intel processor. +func IsIntel() bool { + _, ebx, ecx, edx := cpuidFn(0) + return ebx == 0x756e6547 && // "Genu" + edx == 0x49656e69 && // "ineI" + ecx == 0x6c65746e // "ntel" +} diff --git a/src/gopheros/kernel/cpu/cpu_amd64.s b/src/gopheros/kernel/cpu/cpu_amd64.s index b654195..554b9b5 100644 --- a/src/gopheros/kernel/cpu/cpu_amd64.s +++ b/src/gopheros/kernel/cpu/cpu_amd64.s @@ -31,3 +31,12 @@ TEXT ·ReadCR2(SB),NOSPLIT,$0 MOVQ CR2, AX MOVQ AX, ret+0(FP) RET + +TEXT ·ID(SB),NOSPLIT,$0 + MOVQ leaf+0(FP), AX + CPUID + MOVL AX, ret+0(FP) + MOVL BX, ret+4(FP) + MOVL CX, ret+8(FP) + MOVL DX, ret+12(FP) + RET diff --git a/src/gopheros/kernel/cpu/cpu_amd64_test.go b/src/gopheros/kernel/cpu/cpu_amd64_test.go new file mode 100644 index 0000000..8b951ef --- /dev/null +++ b/src/gopheros/kernel/cpu/cpu_amd64_test.go @@ -0,0 +1,29 @@ +package cpu + +import "testing" + +func TestIsIntel(t *testing.T) { + defer func() { + cpuidFn = ID + }() + + specs := []struct { + eax, ebx, ecx, edx uint32 + exp bool + }{ + // CPUID output from an Intel CPU + {0xd, 0x756e6547, 0x6c65746e, 0x49656e69, true}, + // CPUID output from an AMD Athlon CPU + {0x1, 68747541, 0x444d4163, 0x69746e65, false}, + } + + for specIndex, spec := range specs { + cpuidFn = func(_ uint32) (uint32, uint32, uint32, uint32) { + return spec.eax, spec.ebx, spec.ecx, spec.edx + } + + if got := IsIntel(); got != spec.exp { + t.Errorf("[spec %d] expected IsIntel to return %t; got %t", specIndex, spec.exp, got) + } + } +}