Skip to content

Commit 0922e3e

Browse files
sparquesdeadprogram
authored andcommitted
add device/uefi and device/amd64
1 parent bd61913 commit 0922e3e

13 files changed

Lines changed: 936 additions & 0 deletions

File tree

src/device/amd64/cpu.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//go:build amd64
2+
3+
package amd64
4+
5+
const (
6+
CPUIDTimeStampCounter = 0x15
7+
CPUIDProcessorFrequency = 0x16
8+
)
9+
10+
type CPUExtendedFamily uint16
11+
12+
const (
13+
CPUFamilyIntelCore CPUExtendedFamily = 6
14+
)
15+
16+
//export asmPause
17+
func AsmPause()
18+
19+
//export asmReadRdtsc
20+
func AsmReadRdtsc() uint64
21+
22+
//export asmCpuid
23+
func AsmCpuid(index uint32, registerEax *uint32, registerEbx *uint32, registerEcx *uint32) int
24+
25+
var maxCpuidIndex uint32
26+
var stdVendorName0 uint32
27+
var stdCpuid1Eax uint32
28+
29+
func init() {
30+
AsmCpuid(0, &maxCpuidIndex, &stdVendorName0, nil)
31+
AsmCpuid(1, &stdCpuid1Eax, nil, nil)
32+
}
33+
34+
func getExtendedCPUFamily() CPUExtendedFamily {
35+
family := CPUExtendedFamily((stdCpuid1Eax >> 8) & 0x0f)
36+
family += CPUExtendedFamily((stdCpuid1Eax >> 20) & 0xff)
37+
return family
38+
}
39+
40+
func isIntel() bool {
41+
return stdVendorName0 == 0x756e6547
42+
}
43+
44+
func isIntelFamilyCore() bool {
45+
return isIntel() && getExtendedCPUFamily() == CPUFamilyIntelCore
46+
}
47+
48+
func InternalGetPerformanceCounterFrequency() uint64 {
49+
if maxCpuidIndex >= CPUIDTimeStampCounter {
50+
return cpuidCoreClockCalculateTSCFrequency()
51+
}
52+
return 0
53+
}
54+
55+
func cpuidCoreClockCalculateTSCFrequency() uint64 {
56+
var eax uint32
57+
var ebx uint32
58+
var ecx uint32
59+
60+
AsmCpuid(CPUIDTimeStampCounter, &eax, &ebx, &ecx)
61+
if eax == 0 || ebx == 0 {
62+
return 0
63+
}
64+
65+
coreCrystalFrequency := uint64(ecx)
66+
if coreCrystalFrequency == 0 {
67+
if !isIntelFamilyCore() {
68+
return 0
69+
}
70+
coreCrystalFrequency = 24000000
71+
}
72+
73+
return ((coreCrystalFrequency * uint64(ebx)) + (uint64(eax) / 2)) / uint64(eax)
74+
}

src/device/amd64/cpu_amd64.S

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.section .text
2+
3+
.global asmPause
4+
asmPause:
5+
pause
6+
ret
7+
8+
.global asmReadRdtsc
9+
asmReadRdtsc:
10+
rdtsc
11+
shlq $0x20, %rdx
12+
orq %rdx, %rax
13+
ret
14+
15+
.global asmCpuid
16+
asmCpuid:
17+
pushq %rbx
18+
19+
mov %ecx, %eax
20+
pushq %rax
21+
22+
pushq %rdx
23+
cpuid
24+
25+
test %r9, %r9
26+
jz .SkipEcx
27+
mov %ecx, (%r9)
28+
.SkipEcx:
29+
popq %rcx
30+
jrcxz .SkipEax
31+
mov %eax, (%rcx)
32+
.SkipEax:
33+
mov %r8, %rcx
34+
jrcxz .SkipEbx
35+
mov %ebx, (%rcx)
36+
.SkipEbx:
37+
popq %rax
38+
popq %rbx
39+
ret

src/device/uefi/arch_x86.go

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

src/device/uefi/asm_amd64.S

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
.section .text
2+
3+
.global uefiCall0
4+
uefiCall0:
5+
pushq %rbp
6+
movq %rsp, %rbp
7+
subq $0x20, %rsp
8+
callq *%rcx
9+
movq %rbp, %rsp
10+
popq %rbp
11+
ret
12+
13+
.global uefiCall1
14+
uefiCall1:
15+
pushq %rbp
16+
movq %rsp, %rbp
17+
subq $0x20, %rsp
18+
movq %rcx, %rax
19+
movq %rdx, %rcx
20+
callq *%rax
21+
movq %rbp, %rsp
22+
popq %rbp
23+
ret
24+
25+
.global uefiCall2
26+
uefiCall2:
27+
pushq %rbp
28+
movq %rsp, %rbp
29+
subq $0x20, %rsp
30+
movq %rcx, %rax
31+
movq %rdx, %rcx
32+
movq %r8, %rdx
33+
callq *%rax
34+
movq %rbp, %rsp
35+
popq %rbp
36+
ret
37+
38+
.global uefiCall3
39+
uefiCall3:
40+
pushq %rbp
41+
movq %rsp, %rbp
42+
subq $0x20, %rsp
43+
movq %rcx, %rax
44+
movq %rdx, %rcx
45+
movq %r8, %rdx
46+
movq %r9, %r8
47+
callq *%rax
48+
movq %rbp, %rsp
49+
popq %rbp
50+
ret
51+
52+
.global uefiCall4
53+
uefiCall4:
54+
pushq %rbp
55+
movq %rsp, %rbp
56+
subq $0x20, %rsp
57+
movq %rcx, %rax
58+
movq %rdx, %rcx
59+
movq %r8, %rdx
60+
movq %r9, %r8
61+
movq 0x30(%rbp), %r9
62+
callq *%rax
63+
movq %rbp, %rsp
64+
popq %rbp
65+
ret
66+
67+
.global uefiCall5
68+
uefiCall5:
69+
pushq %rbp
70+
movq %rsp, %rbp
71+
subq $0x30, %rsp
72+
movq %rcx, %rax
73+
movq %rdx, %rcx
74+
movq %r8, %rdx
75+
movq %r9, %r8
76+
movq 0x30(%rbp), %r9
77+
movq 0x38(%rbp), %r10
78+
movq %r10, 0x20(%rsp)
79+
callq *%rax
80+
movq %rbp, %rsp
81+
popq %rbp
82+
ret
83+
84+
.global uefiCall6
85+
uefiCall6:
86+
pushq %rbp
87+
movq %rsp, %rbp
88+
subq $0x30, %rsp
89+
movq %rcx, %rax
90+
movq %rdx, %rcx
91+
movq %r8, %rdx
92+
movq %r9, %r8
93+
movq 0x30(%rbp), %r9
94+
movq 0x38(%rbp), %r10
95+
movq %r10, 0x20(%rsp)
96+
movq 0x40(%rbp), %r10
97+
movq %r10, 0x28(%rsp)
98+
callq *%rax
99+
movq %rbp, %rsp
100+
popq %rbp
101+
ret
102+
103+
.global uefiCall7
104+
uefiCall7:
105+
pushq %rbp
106+
movq %rsp, %rbp
107+
subq $0x40, %rsp
108+
movq %rcx, %rax
109+
movq %rdx, %rcx
110+
movq %r8, %rdx
111+
movq %r9, %r8
112+
movq 0x30(%rbp), %r9
113+
movq 0x38(%rbp), %r10
114+
movq %r10, 0x20(%rsp)
115+
movq 0x40(%rbp), %r10
116+
movq %r10, 0x28(%rsp)
117+
movq 0x48(%rbp), %r10
118+
movq %r10, 0x30(%rsp)
119+
callq *%rax
120+
movq %rbp, %rsp
121+
popq %rbp
122+
ret
123+
124+
.global uefiCall8
125+
uefiCall8:
126+
pushq %rbp
127+
movq %rsp, %rbp
128+
subq $0x40, %rsp
129+
movq %rcx, %rax
130+
movq %rdx, %rcx
131+
movq %r8, %rdx
132+
movq %r9, %r8
133+
movq 0x30(%rbp), %r9
134+
movq 0x38(%rbp), %r10
135+
movq %r10, 0x20(%rsp)
136+
movq 0x40(%rbp), %r10
137+
movq %r10, 0x28(%rsp)
138+
movq 0x48(%rbp), %r10
139+
movq %r10, 0x30(%rsp)
140+
movq 0x50(%rbp), %r10
141+
movq %r10, 0x38(%rsp)
142+
callq *%rax
143+
movq %rbp, %rsp
144+
popq %rbp
145+
ret
146+
147+
.global uefiCall9
148+
uefiCall9:
149+
pushq %rbp
150+
movq %rsp, %rbp
151+
subq $0x50, %rsp
152+
movq %rcx, %rax
153+
movq %rdx, %rcx
154+
movq %r8, %rdx
155+
movq %r9, %r8
156+
movq 0x30(%rbp), %r9
157+
movq 0x38(%rbp), %r10
158+
movq %r10, 0x20(%rsp)
159+
movq 0x40(%rbp), %r10
160+
movq %r10, 0x28(%rsp)
161+
movq 0x48(%rbp), %r10
162+
movq %r10, 0x30(%rsp)
163+
movq 0x50(%rbp), %r10
164+
movq %r10, 0x38(%rsp)
165+
movq 0x58(%rbp), %r10
166+
movq %r10, 0x40(%rsp)
167+
callq *%rax
168+
movq %rbp, %rsp
169+
popq %rbp
170+
ret
171+
172+
.global uefiCall10
173+
uefiCall10:
174+
pushq %rbp
175+
movq %rsp, %rbp
176+
subq $0x50, %rsp
177+
movq %rcx, %rax
178+
movq %rdx, %rcx
179+
movq %r8, %rdx
180+
movq %r9, %r8
181+
movq 0x30(%rbp), %r9
182+
movq 0x38(%rbp), %r10
183+
movq %r10, 0x20(%rsp)
184+
movq 0x40(%rbp), %r10
185+
movq %r10, 0x28(%rsp)
186+
movq 0x48(%rbp), %r10
187+
movq %r10, 0x30(%rsp)
188+
movq 0x50(%rbp), %r10
189+
movq %r10, 0x38(%rsp)
190+
movq 0x58(%rbp), %r10
191+
movq %r10, 0x40(%rsp)
192+
movq 0x60(%rbp), %r10
193+
movq %r10, 0x48(%rsp)
194+
callq *%rax
195+
movq %rbp, %rsp
196+
popq %rbp
197+
ret
198+
199+
.global ___chkstk_ms
200+
___chkstk_ms:
201+
ret

src/device/uefi/call.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package uefi
2+
3+
//go:nosplit
4+
//export uefiCall0
5+
func UefiCall0(fn uintptr) EFI_STATUS
6+
7+
//go:nosplit
8+
//go:export uefiCall1
9+
func UefiCall1(fn uintptr, a uintptr) EFI_STATUS
10+
11+
//go:nosplit
12+
//go:export uefiCall2
13+
func UefiCall2(fn uintptr, a uintptr, b uintptr) EFI_STATUS
14+
15+
//go:nosplit
16+
//go:export uefiCall3
17+
func UefiCall3(fn uintptr, a uintptr, b uintptr, c uintptr) EFI_STATUS
18+
19+
//go:nosplit
20+
//go:export uefiCall4
21+
func UefiCall4(fn uintptr, a uintptr, b uintptr, c uintptr, d uintptr) EFI_STATUS
22+
23+
//go:nosplit
24+
//go:export uefiCall5
25+
func UefiCall5(fn uintptr, a uintptr, b uintptr, c uintptr, d uintptr, e uintptr) EFI_STATUS
26+
27+
//go:nosplit
28+
//go:export uefiCall6
29+
func UefiCall6(fn uintptr, a uintptr, b uintptr, c uintptr, d uintptr, e uintptr, f uintptr) EFI_STATUS
30+
31+
//go:nosplit
32+
//go:export uefiCall7
33+
func UefiCall7(fn uintptr, a uintptr, b uintptr, c uintptr, d uintptr, e uintptr, f uintptr, g uintptr) EFI_STATUS
34+
35+
//go:nosplit
36+
//go:export uefiCall8
37+
func UefiCall8(fn uintptr, a uintptr, b uintptr, c uintptr, d uintptr, e uintptr, f uintptr, g uintptr, h uintptr) EFI_STATUS
38+
39+
//go:nosplit
40+
//go:export uefiCall9
41+
func UefiCall9(fn uintptr, a uintptr, b uintptr, c uintptr, d uintptr, e uintptr, f uintptr, g uintptr, h uintptr, i uintptr) EFI_STATUS
42+
43+
//go:nosplit
44+
//go:export uefiCall10
45+
func UefiCall10(fn uintptr, a uintptr, b uintptr, c uintptr, d uintptr, e uintptr, f uintptr, g uintptr, h uintptr, i uintptr, j uintptr) EFI_STATUS

0 commit comments

Comments
 (0)