Skip to content

Commit 968c948

Browse files
committed
refactor: remove runtime_uefi_x86.go
1 parent 5ee303e commit 968c948

File tree

4 files changed

+54
-68
lines changed

4 files changed

+54
-68
lines changed

src/device/x86/cpu.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -140,48 +140,3 @@ func CpuidCoreClockCalculateTscFrequency() uint64 {
140140
TscFrequency = ((CoreXtalFrequency * uint64(RegEbx)) + (uint64(RegEax) / 2)) / uint64(RegEax)
141141
return TscFrequency
142142
}
143-
144-
func ConvertTimeInNanoSecond(frequency uint64, ticks uint64) int64 {
145-
// Ticks
146-
// Time = --------- x 1,000,000,000
147-
// Frequency
148-
nanoSeconds := (ticks / frequency) * 1000000000
149-
remainder := ticks % frequency
150-
151-
// Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
152-
// Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
153-
// i.e. highest bit set in Remainder should <= 33.
154-
//
155-
shift := highBitSet64(remainder) - 32
156-
if shift < 0 {
157-
shift = 0
158-
}
159-
remainder = remainder >> shift
160-
frequency = frequency >> shift
161-
nanoSeconds += remainder * 1000000000 / frequency
162-
163-
return int64(nanoSeconds)
164-
}
165-
166-
func ConvertTicksFromNanoSeconds(frequency uint64, nano uint64) uint64 {
167-
return nano * frequency / 1000000000
168-
}
169-
170-
func highBitSet64(operand uint64) int {
171-
if operand == (operand & 0xffffffff) {
172-
return highBitSet32(uint32(operand))
173-
}
174-
return highBitSet32(uint32(operand>>32)) + 32
175-
}
176-
177-
func highBitSet32(operand uint32) int {
178-
if operand == 0 {
179-
return -1
180-
}
181-
bitIndex := 32
182-
for operand > 0 {
183-
bitIndex--
184-
operand <<= 1
185-
}
186-
return bitIndex
187-
}

src/machine/uefi/arch_x86.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ func Ticks() uint64 {
88
return x86.AsmReadRdtsc()
99
}
1010

11+
func CpuPause() {
12+
x86.AsmPause()
13+
}
14+
1115
func getTscFrequency() uint64 {
1216
return x86.InternalGetPerformanceCounterFrequency()
1317
}

src/runtime/runtime_uefi.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,69 @@ func machineTicks() uint64 {
1212
return uefi.Ticks()
1313
}
1414

15-
type timeUnit uint64
15+
type timeUnit int64
1616

1717
// ticks returns the number of ticks (microseconds) elapsed since power up.
1818
func ticks() timeUnit {
1919
t := machineTicks()
2020
return timeUnit(t)
2121
}
2222

23+
func nanosecondsToTicks(ns int64) timeUnit {
24+
return timeUnit(ns * int64(uefi.TicksFrequency()) / 1000000000)
25+
}
26+
27+
func ticksToNanoseconds(ticks timeUnit) int64 {
28+
frequency := timeUnit(uefi.TicksFrequency())
29+
30+
// Ticks
31+
// Time = --------- x 1,000,000,000
32+
// Frequency
33+
nanoSeconds := (ticks / frequency) * 1000000000
34+
remainder := ticks % frequency
35+
36+
// Ensure (Remainder * 1,000,000,000) will not overflow 64-bit.
37+
// Since 2^29 < 1,000,000,000 = 0x3B9ACA00 < 2^30, Remainder should < 2^(64-30) = 2^34,
38+
// i.e. highest bit set in Remainder should <= 33.
39+
//
40+
shift := highBitSet64(uint64(remainder)) - 32
41+
if shift < 0 {
42+
shift = 0
43+
}
44+
remainder = remainder >> shift
45+
frequency = frequency >> shift
46+
nanoSeconds += remainder * 1000000000 / frequency
47+
48+
return int64(nanoSeconds)
49+
}
50+
51+
func highBitSet64(operand uint64) int {
52+
if operand == (operand & 0xffffffff) {
53+
return highBitSet32(uint32(operand))
54+
}
55+
return highBitSet32(uint32(operand>>32)) + 32
56+
}
57+
58+
func highBitSet32(operand uint32) int {
59+
if operand == 0 {
60+
return -1
61+
}
62+
bitIndex := 32
63+
for operand > 0 {
64+
bitIndex--
65+
operand <<= 1
66+
}
67+
return bitIndex
68+
}
69+
2370
func sleepTicks(d timeUnit) {
2471
if d == 0 {
2572
return
2673
}
2774

2875
sleepUntil := ticks() + d
2976
for ticks() < sleepUntil {
30-
cpuPause()
77+
uefi.CpuPause()
3178
}
3279
}
3380

@@ -113,7 +160,7 @@ func init() {
113160
}
114161

115162
var waitForEventsFunction WaitForEvents = func() {
116-
cpuPause()
163+
uefi.CpuPause()
117164
}
118165

119166
// SetWaitForEvents

src/runtime/runtime_uefi_x86.go

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

0 commit comments

Comments
 (0)