Skip to content

Commit 9a558c7

Browse files
committed
refactor: remove non-essential protocols
1 parent 6aab335 commit 9a558c7

File tree

7 files changed

+95
-32
lines changed

7 files changed

+95
-32
lines changed

backup/src/machine/uefi/efidevp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package uefi
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package runtime

src/device/x86/cpu.go

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,72 @@ const (
2424
CPUID_PROCESSOR_FREQUENCY = 0x16
2525
)
2626

27+
type CpuExtendedFamily uint16
28+
29+
const (
30+
// AMD
31+
CPU_FAMILY_AMD_11H CpuExtendedFamily = 0x11
32+
// Intel
33+
CPU_FAMILY_INTEL_CORE CpuExtendedFamily = 6
34+
CPU_MODEL_NEHALEM CpuExtendedFamily = 0x1e
35+
CPU_MODEL_NEHALEM_EP CpuExtendedFamily = 0x1a
36+
CPU_MODEL_NEHALEM_EX CpuExtendedFamily = 0x2e
37+
CPU_MODEL_WESTMERE CpuExtendedFamily = 0x25
38+
CPU_MODEL_WESTMERE_EP CpuExtendedFamily = 0x2c
39+
CPU_MODEL_WESTMERE_EX CpuExtendedFamily = 0x2f
40+
CPU_MODEL_SANDYBRIDGE CpuExtendedFamily = 0x2a
41+
CPU_MODEL_SANDYBRIDGE_EP CpuExtendedFamily = 0x2d
42+
CPU_MODEL_IVYBRIDGE_EP CpuExtendedFamily = 0x3a
43+
CPU_MODEL_HASWELL_E3 CpuExtendedFamily = 0x3c
44+
CPU_MODEL_HASWELL_E7 CpuExtendedFamily = 0x3f
45+
CPU_MODEL_BROADWELL CpuExtendedFamily = 0x3d
46+
)
47+
48+
func GetExtendedCpuFamily() CpuExtendedFamily {
49+
var family CpuExtendedFamily
50+
family = CpuExtendedFamily((stdCpuid1Eax >> 8) & 0x0f)
51+
family += CpuExtendedFamily((stdCpuid1Eax >> 20) & 0xff)
52+
return family
53+
}
54+
2755
//export asmPause
2856
func AsmPause()
2957

3058
//export asmReadRdtsc
3159
func AsmReadRdtsc() uint64
3260

3361
//export asmCpuid
34-
func AsmCpuid(index uint32, registerEax *uint32, registerRbx *uint32, registerEcx *uint32, registerEdx *uint32) int
62+
func AsmCpuid(index uint32, registerEax *uint32, registerEbx *uint32, registerEcx *uint32, registerEdx *uint32) int
3563

3664
var maxCpuidIndex uint32
65+
var stdVendorName0 uint32
66+
var stdCpuid1Eax uint32
67+
var stdCpuid1Ebx uint32
68+
var stdCpuid1Ecx uint32
69+
var stdCpuid1Edx uint32
3770

3871
func init() {
39-
AsmCpuid(0, &maxCpuidIndex, nil, nil, nil)
72+
AsmCpuid(0, &maxCpuidIndex, &stdVendorName0, nil, nil)
73+
AsmCpuid(1, &stdCpuid1Eax, &stdCpuid1Ebx, &stdCpuid1Ecx, &stdCpuid1Edx)
4074
}
4175

4276
func GetMaxCpuidIndex() uint32 {
4377
return maxCpuidIndex
4478
}
4579

46-
func InternalGetPerformanceCounterFrequency() uint64 {
47-
//FIXME: how to find it?
48-
//See https://github.com/torvalds/linux/blob/master/arch/x86/kernel/tsc.c
80+
func IsIntel() bool {
81+
return stdVendorName0 == 0x756e6547
82+
}
83+
84+
func IsIntelFamilyCore() bool {
85+
return IsIntel() && GetExtendedCpuFamily() == CPU_FAMILY_INTEL_CORE
86+
}
4987

88+
func IsAmd() bool {
89+
return stdVendorName0 == 0x68747541
90+
}
91+
92+
func InternalGetPerformanceCounterFrequency() uint64 {
5093
if maxCpuidIndex >= CPUID_TIME_STAMP_COUNTER {
5194
return CpuidCoreClockCalculateTscFrequency()
5295
}
@@ -73,9 +116,21 @@ func CpuidCoreClockCalculateTscFrequency() uint64 {
73116
//
74117
if RegEcx == 0 {
75118
// Specifies CPUID Leaf 0x15 Time Stamp Counter and Nominal Core Crystal Clock Frequency.
76-
// Intel Xeon Processor Scalable Family with CPUID signature 06_55H = 25000000 (25MHz)
77-
// 6th and 7th generation Intel Core processors and Intel Xeon W Processor Family = 24000000 (24MHz)
78-
// Intel Atom processors based on Goldmont Microarchitecture with CPUID signature 06_5CH = 19200000 (19.2MHz)
119+
// https://github.com/torvalds/linux/blob/master/tools/power/x86/turbostat/turbostat.c
120+
if IsIntelFamilyCore() {
121+
switch GetExtendedCpuFamily() {
122+
case 0x5F: // INTEL_FAM6_ATOM_GOLDMONT_D
123+
CoreXtalFrequency = 25000000
124+
case 0x5C: // INTEL_FAM6_ATOM_GOLDMONT
125+
CoreXtalFrequency = 19200000
126+
case 0x7A: // INTEL_FAM6_ATOM_GOLDMONT_PLUS
127+
CoreXtalFrequency = 19200000
128+
default:
129+
return 0
130+
}
131+
} else {
132+
return 0
133+
}
79134
CoreXtalFrequency = 24000000
80135
} else {
81136
CoreXtalFrequency = uint64(RegEcx)

src/machine/uefi/eventloop.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/machine/uefi/uefi_spec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,8 +1215,8 @@ func (p *EFI_BOOT_SERVICES) LocateHandleBuffer(SearchType EFI_LOCATE_SEARCH_TYPE
12151215
// ..............................Registration.
12161216
// @retval EFI_INVALID_PARAMETER Interface is NULL.
12171217
// ..............................Protocol is NULL.
1218-
func (p *EFI_BOOT_SERVICES) LocateProtocol(Protocol *EFI_GUID, Registration *VOID, Interface **VOID) EFI_STATUS {
1219-
return UefiCall3(p.locateProtocol, uintptr(unsafe.Pointer(Protocol)), uintptr(unsafe.Pointer(Registration)), uintptr(unsafe.Pointer(Interface)))
1218+
func (p *EFI_BOOT_SERVICES) LocateProtocol(Protocol *EFI_GUID, Registration *VOID, InterfacePtr unsafe.Pointer) EFI_STATUS {
1219+
return UefiCall3(p.locateProtocol, uintptr(unsafe.Pointer(Protocol)), uintptr(unsafe.Pointer(Registration)), uintptr(InterfacePtr))
12201220
}
12211221

12221222
// InstallMultipleProtocolInterfaces

src/machine/uefi/util.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package uefi
44

55
import (
66
"device/x86"
7+
"unicode/utf16"
8+
"unicode/utf8"
79
"unsafe"
810
)
911

@@ -24,10 +26,6 @@ func BS() *EFI_BOOT_SERVICES {
2426
return systemTable.BootServices
2527
}
2628

27-
func RT() *EFI_RUNTIME_SERVICES {
28-
return systemTable.RuntimeServices
29-
}
30-
3129
func GetImageHandle() EFI_HANDLE {
3230
return EFI_HANDLE(imageHandle)
3331
}
@@ -40,6 +38,32 @@ func TicksFrequency() uint64 {
4038
return GetTscFrequency()
4139
}
4240

41+
func UTF16ToString(input []CHAR16) string {
42+
return UTF16PtrLenToString(&input[0], len(input))
43+
}
44+
45+
func UTF16PtrToString(input *CHAR16) string {
46+
pointer := uintptr(unsafe.Pointer(input))
47+
length := 0
48+
for *(*CHAR16)(unsafe.Pointer(pointer)) != 0 {
49+
length++
50+
pointer += 2
51+
}
52+
return UTF16PtrLenToString(input, length)
53+
}
54+
55+
func UTF16PtrLenToString(input *CHAR16, length int) string {
56+
var output []byte = make([]byte, 0, length)
57+
var u8buf [4]byte
58+
inputSlice := unsafe.Slice((*uint16)(unsafe.Pointer(input)), length)
59+
decodedRunes := utf16.Decode(inputSlice)
60+
for _, r := range decodedRunes {
61+
chunkLength := utf8.EncodeRune(u8buf[:], r)
62+
output = append(output, u8buf[:chunkLength]...)
63+
}
64+
return string(output)
65+
}
66+
4367
var hexConst [16]CHAR16 = [16]CHAR16{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
4468

4569
var printBuf [256]CHAR16

src/runtime/runtime_uefi.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,6 @@ func init() {
118118
sec, nsec := efiTime.GetEpoch()
119119
timeOffset = sec*1000000000 + int64(nsec) - mono
120120
}
121-
122-
go func() {
123-
// Event Loop
124-
uefi.DebugPrint("HELLO WORLD", 0)
125-
for {
126-
uefi.EventLoop()
127-
Gosched()
128-
}
129-
}()
130121
}
131122

132123
func waitForEvents() {

0 commit comments

Comments
 (0)