Skip to content

Commit 5ee303e

Browse files
committed
refactor: seperate architecture specific codes
1 parent 2a46aa5 commit 5ee303e

File tree

5 files changed

+51
-30
lines changed

5 files changed

+51
-30
lines changed

src/machine/uefi/arch_x86.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build i386 || amd64
2+
3+
package uefi
4+
5+
import "device/x86"
6+
7+
func Ticks() uint64 {
8+
return x86.AsmReadRdtsc()
9+
}
10+
11+
func getTscFrequency() uint64 {
12+
return x86.InternalGetPerformanceCounterFrequency()
13+
}

src/machine/uefi/clock.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
package uefi
44

55
import (
6-
"device/x86"
76
"sync"
87
)
98

109
var calibrateMutex sync.Mutex
1110
var calculatedFrequency uint64
1211

13-
func GetTscFrequency() uint64 {
14-
frequency := x86.InternalGetPerformanceCounterFrequency()
12+
func TicksFrequency() uint64 {
13+
frequency := getTscFrequency()
1514
if frequency > 0 {
1615
return frequency
1716
}
@@ -37,14 +36,14 @@ func GetTscFrequency() uint64 {
3736
}
3837
defer bs.CloseEvent(event)
3938

40-
st := x86.AsmReadRdtsc()
39+
st := Ticks()
4140
status = bs.SetTimer(event, TimerPeriodic, 250*10000)
4241
if status != EFI_SUCCESS {
4342
DebugPrint("GetTscFrequency) SetTimer Failed", uint64(status))
4443
return 0
4544
}
4645
status = bs.WaitForEvent(1, &event, &index)
47-
diff := x86.AsmReadRdtsc() - st
46+
diff := Ticks() - st
4847

4948
calculatedFrequency = diff * 4
5049

@@ -91,7 +90,6 @@ const (
9190
secondsPerMinute = 60
9291
secondsPerHour = 60 * secondsPerMinute
9392
secondsPerDay = 24 * secondsPerHour
94-
secondsPerWeek = 7 * secondsPerDay
9593
daysPer400Years = 365*400 + 97
9694
daysPer100Years = 365*100 + 24
9795
daysPer4Years = 365*4 + 1
@@ -107,12 +105,9 @@ const (
107105

108106
// Offsets to convert between internal and absolute or Unix times.
109107
absoluteToInternal int64 = (absoluteZeroYear - internalYear) * 365.2425 * secondsPerDay
110-
internalToAbsolute = -absoluteToInternal
111108

112109
unixToInternal int64 = (1969*365 + 1969/4 - 1969/100 + 1969/400) * secondsPerDay
113110
internalToUnix int64 = -unixToInternal
114-
115-
wallToInternal int64 = (1884*365 + 1884/4 - 1884/100 + 1884/400) * secondsPerDay
116111
)
117112

118113
// daysBefore[m] counts the number of days in a non-leap year

src/machine/uefi/util.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package uefi
44

55
import (
6-
"device/x86"
76
"unicode/utf16"
87
"unicode/utf8"
98
"unsafe"
@@ -30,14 +29,6 @@ func GetImageHandle() EFI_HANDLE {
3029
return EFI_HANDLE(imageHandle)
3130
}
3231

33-
func Ticks() uint64 {
34-
return x86.AsmReadRdtsc()
35-
}
36-
37-
func TicksFrequency() uint64 {
38-
return GetTscFrequency()
39-
}
40-
4132
func UTF16ToString(input []CHAR16) string {
4233
return UTF16PtrLenToString(&input[0], len(input))
4334
}

src/runtime/runtime_uefi.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
package runtime
44

55
import (
6-
"device/x86"
76
"machine/uefi"
87
)
98

9+
type WaitForEvents func()
10+
1011
func machineTicks() uint64 {
1112
return uefi.Ticks()
1213
}
@@ -19,23 +20,14 @@ func ticks() timeUnit {
1920
return timeUnit(t)
2021
}
2122

22-
func ticksToNanoseconds(ticks timeUnit) int64 {
23-
return x86.ConvertTimeInNanoSecond(uefi.TicksFrequency(), uint64(ticks))
24-
}
25-
26-
func nanosecondsToTicks(ns int64) timeUnit {
27-
return timeUnit(x86.ConvertTicksFromNanoSeconds(uefi.TicksFrequency(), uint64(ns)))
28-
}
29-
3023
func sleepTicks(d timeUnit) {
3124
if d == 0 {
3225
return
3326
}
3427

35-
// Busy loop
3628
sleepUntil := ticks() + d
3729
for ticks() < sleepUntil {
38-
x86.AsmPause()
30+
cpuPause()
3931
}
4032
}
4133

@@ -120,8 +112,18 @@ func init() {
120112
}
121113
}
122114

115+
var waitForEventsFunction WaitForEvents = func() {
116+
cpuPause()
117+
}
118+
119+
// SetWaitForEvents
120+
// You can implement your own event-loop with BS.CheckEvent or BS.WaitForEvents.
121+
func SetWaitForEvents(f WaitForEvents) {
122+
waitForEventsFunction = f
123+
}
124+
123125
func waitForEvents() {
124-
uefi.DebugPrint("waitForEvents", 0)
126+
waitForEventsFunction()
125127
}
126128

127129
// Must be a separate function to get the correct stack pointer.

src/runtime/runtime_uefi_x86.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//go:build uefi && (i386 || amd64)
2+
3+
package runtime
4+
5+
import (
6+
"device/x86"
7+
"machine/uefi"
8+
)
9+
10+
func ticksToNanoseconds(ticks timeUnit) int64 {
11+
return x86.ConvertTimeInNanoSecond(uefi.TicksFrequency(), uint64(ticks))
12+
}
13+
14+
func nanosecondsToTicks(ns int64) timeUnit {
15+
return timeUnit(x86.ConvertTicksFromNanoSeconds(uefi.TicksFrequency(), uint64(ns)))
16+
}
17+
18+
func cpuPause() {
19+
x86.AsmPause()
20+
}

0 commit comments

Comments
 (0)