Skip to content

Commit

Permalink
refactor: remove non-essential protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
jclab-joseph committed Nov 21, 2023
1 parent 6aab335 commit 9a558c7
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 32 deletions.
1 change: 1 addition & 0 deletions backup/src/machine/uefi/efidevp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package uefi
1 change: 1 addition & 0 deletions backup/src/runtime/runtime_uefi_x86.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package runtime
71 changes: 63 additions & 8 deletions src/device/x86/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,72 @@ const (
CPUID_PROCESSOR_FREQUENCY = 0x16
)

type CpuExtendedFamily uint16

const (
// AMD
CPU_FAMILY_AMD_11H CpuExtendedFamily = 0x11
// Intel
CPU_FAMILY_INTEL_CORE CpuExtendedFamily = 6
CPU_MODEL_NEHALEM CpuExtendedFamily = 0x1e
CPU_MODEL_NEHALEM_EP CpuExtendedFamily = 0x1a
CPU_MODEL_NEHALEM_EX CpuExtendedFamily = 0x2e
CPU_MODEL_WESTMERE CpuExtendedFamily = 0x25
CPU_MODEL_WESTMERE_EP CpuExtendedFamily = 0x2c
CPU_MODEL_WESTMERE_EX CpuExtendedFamily = 0x2f
CPU_MODEL_SANDYBRIDGE CpuExtendedFamily = 0x2a
CPU_MODEL_SANDYBRIDGE_EP CpuExtendedFamily = 0x2d
CPU_MODEL_IVYBRIDGE_EP CpuExtendedFamily = 0x3a
CPU_MODEL_HASWELL_E3 CpuExtendedFamily = 0x3c
CPU_MODEL_HASWELL_E7 CpuExtendedFamily = 0x3f
CPU_MODEL_BROADWELL CpuExtendedFamily = 0x3d
)

func GetExtendedCpuFamily() CpuExtendedFamily {
var family CpuExtendedFamily
family = CpuExtendedFamily((stdCpuid1Eax >> 8) & 0x0f)
family += CpuExtendedFamily((stdCpuid1Eax >> 20) & 0xff)
return family
}

//export asmPause
func AsmPause()

//export asmReadRdtsc
func AsmReadRdtsc() uint64

//export asmCpuid
func AsmCpuid(index uint32, registerEax *uint32, registerRbx *uint32, registerEcx *uint32, registerEdx *uint32) int
func AsmCpuid(index uint32, registerEax *uint32, registerEbx *uint32, registerEcx *uint32, registerEdx *uint32) int

var maxCpuidIndex uint32
var stdVendorName0 uint32
var stdCpuid1Eax uint32
var stdCpuid1Ebx uint32
var stdCpuid1Ecx uint32
var stdCpuid1Edx uint32

func init() {
AsmCpuid(0, &maxCpuidIndex, nil, nil, nil)
AsmCpuid(0, &maxCpuidIndex, &stdVendorName0, nil, nil)
AsmCpuid(1, &stdCpuid1Eax, &stdCpuid1Ebx, &stdCpuid1Ecx, &stdCpuid1Edx)
}

func GetMaxCpuidIndex() uint32 {
return maxCpuidIndex
}

func InternalGetPerformanceCounterFrequency() uint64 {
//FIXME: how to find it?
//See https://github.com/torvalds/linux/blob/master/arch/x86/kernel/tsc.c
func IsIntel() bool {
return stdVendorName0 == 0x756e6547
}

func IsIntelFamilyCore() bool {
return IsIntel() && GetExtendedCpuFamily() == CPU_FAMILY_INTEL_CORE
}

func IsAmd() bool {
return stdVendorName0 == 0x68747541
}

func InternalGetPerformanceCounterFrequency() uint64 {
if maxCpuidIndex >= CPUID_TIME_STAMP_COUNTER {
return CpuidCoreClockCalculateTscFrequency()
}
Expand All @@ -73,9 +116,21 @@ func CpuidCoreClockCalculateTscFrequency() uint64 {
//
if RegEcx == 0 {
// Specifies CPUID Leaf 0x15 Time Stamp Counter and Nominal Core Crystal Clock Frequency.
// Intel Xeon Processor Scalable Family with CPUID signature 06_55H = 25000000 (25MHz)
// 6th and 7th generation Intel Core processors and Intel Xeon W Processor Family = 24000000 (24MHz)
// Intel Atom processors based on Goldmont Microarchitecture with CPUID signature 06_5CH = 19200000 (19.2MHz)
// https://github.com/torvalds/linux/blob/master/tools/power/x86/turbostat/turbostat.c
if IsIntelFamilyCore() {
switch GetExtendedCpuFamily() {
case 0x5F: // INTEL_FAM6_ATOM_GOLDMONT_D
CoreXtalFrequency = 25000000
case 0x5C: // INTEL_FAM6_ATOM_GOLDMONT
CoreXtalFrequency = 19200000
case 0x7A: // INTEL_FAM6_ATOM_GOLDMONT_PLUS
CoreXtalFrequency = 19200000
default:
return 0
}
} else {
return 0
}
CoreXtalFrequency = 24000000
} else {
CoreXtalFrequency = uint64(RegEcx)
Expand Down
9 changes: 0 additions & 9 deletions src/machine/uefi/eventloop.go

This file was deleted.

4 changes: 2 additions & 2 deletions src/machine/uefi/uefi_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1215,8 +1215,8 @@ func (p *EFI_BOOT_SERVICES) LocateHandleBuffer(SearchType EFI_LOCATE_SEARCH_TYPE
// ..............................Registration.
// @retval EFI_INVALID_PARAMETER Interface is NULL.
// ..............................Protocol is NULL.
func (p *EFI_BOOT_SERVICES) LocateProtocol(Protocol *EFI_GUID, Registration *VOID, Interface **VOID) EFI_STATUS {
return UefiCall3(p.locateProtocol, uintptr(unsafe.Pointer(Protocol)), uintptr(unsafe.Pointer(Registration)), uintptr(unsafe.Pointer(Interface)))
func (p *EFI_BOOT_SERVICES) LocateProtocol(Protocol *EFI_GUID, Registration *VOID, InterfacePtr unsafe.Pointer) EFI_STATUS {
return UefiCall3(p.locateProtocol, uintptr(unsafe.Pointer(Protocol)), uintptr(unsafe.Pointer(Registration)), uintptr(InterfacePtr))
}

// InstallMultipleProtocolInterfaces
Expand Down
32 changes: 28 additions & 4 deletions src/machine/uefi/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package uefi

import (
"device/x86"
"unicode/utf16"
"unicode/utf8"
"unsafe"
)

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

func RT() *EFI_RUNTIME_SERVICES {
return systemTable.RuntimeServices
}

func GetImageHandle() EFI_HANDLE {
return EFI_HANDLE(imageHandle)
}
Expand All @@ -40,6 +38,32 @@ func TicksFrequency() uint64 {
return GetTscFrequency()
}

func UTF16ToString(input []CHAR16) string {
return UTF16PtrLenToString(&input[0], len(input))
}

func UTF16PtrToString(input *CHAR16) string {
pointer := uintptr(unsafe.Pointer(input))
length := 0
for *(*CHAR16)(unsafe.Pointer(pointer)) != 0 {
length++
pointer += 2
}
return UTF16PtrLenToString(input, length)
}

func UTF16PtrLenToString(input *CHAR16, length int) string {
var output []byte = make([]byte, 0, length)
var u8buf [4]byte
inputSlice := unsafe.Slice((*uint16)(unsafe.Pointer(input)), length)
decodedRunes := utf16.Decode(inputSlice)
for _, r := range decodedRunes {
chunkLength := utf8.EncodeRune(u8buf[:], r)
output = append(output, u8buf[:chunkLength]...)
}
return string(output)
}

var hexConst [16]CHAR16 = [16]CHAR16{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}

var printBuf [256]CHAR16
Expand Down
9 changes: 0 additions & 9 deletions src/runtime/runtime_uefi.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,6 @@ func init() {
sec, nsec := efiTime.GetEpoch()
timeOffset = sec*1000000000 + int64(nsec) - mono
}

go func() {
// Event Loop
uefi.DebugPrint("HELLO WORLD", 0)
for {
uefi.EventLoop()
Gosched()
}
}()
}

func waitForEvents() {
Expand Down

0 comments on commit 9a558c7

Please sign in to comment.