1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build goexperiment.runtimesecret
6
7 // Note: this assembly file is used for testing only.
8 // We need to access registers directly to properly test
9 // that secrets are erased and go test doesn't like to conditionally
10 // include assembly files.
11 // These functions defined in the package proper and we
12 // rely on the linker to prune these away in regular builds
13
14 #include "go_asm.h"
15 #include "funcdata.h"
16
17 TEXT ·loadRegisters(SB),0,$0-8
18 MOVQ p+0(FP), AX
19
20 MOVQ (AX), R10
21 MOVQ (AX), R11
22 MOVQ (AX), R12
23 MOVQ (AX), R13
24
25 MOVOU (AX), X1
26 MOVOU (AX), X2
27 MOVOU (AX), X3
28 MOVOU (AX), X4
29
30 CMPB internal∕cpu·X86+const_offsetX86HasAVX(SB), $1
31 JNE return
32
33 VMOVDQU (AX), Y5
34 VMOVDQU (AX), Y6
35 VMOVDQU (AX), Y7
36 VMOVDQU (AX), Y8
37
38 CMPB internal∕cpu·X86+const_offsetX86HasAVX512(SB), $1
39 JNE return
40
41 VMOVUPD (AX), Z14
42 VMOVUPD (AX), Z15
43 VMOVUPD (AX), Z16
44 VMOVUPD (AX), Z17
45
46 KMOVQ (AX), K2
47 KMOVQ (AX), K3
48 KMOVQ (AX), K4
49 KMOVQ (AX), K5
50
51 return:
52 RET
53
54 TEXT ·spillRegisters(SB),0,$0-16
55 MOVQ p+0(FP), AX
56 MOVQ AX, BX
57
58 MOVQ R10, (AX)
59 MOVQ R11, 8(AX)
60 MOVQ R12, 16(AX)
61 MOVQ R13, 24(AX)
62 ADDQ $32, AX
63
64 MOVOU X1, (AX)
65 MOVOU X2, 16(AX)
66 MOVOU X3, 32(AX)
67 MOVOU X4, 48(AX)
68 ADDQ $64, AX
69
70 CMPB internal∕cpu·X86+const_offsetX86HasAVX(SB), $1
71 JNE return
72
73 VMOVDQU Y5, (AX)
74 VMOVDQU Y6, 32(AX)
75 VMOVDQU Y7, 64(AX)
76 VMOVDQU Y8, 96(AX)
77 ADDQ $128, AX
78
79 CMPB internal∕cpu·X86+const_offsetX86HasAVX512(SB), $1
80 JNE return
81
82 VMOVUPD Z14, (AX)
83 ADDQ $64, AX
84 VMOVUPD Z15, (AX)
85 ADDQ $64, AX
86 VMOVUPD Z16, (AX)
87 ADDQ $64, AX
88 VMOVUPD Z17, (AX)
89 ADDQ $64, AX
90
91 KMOVQ K2, (AX)
92 ADDQ $8, AX
93 KMOVQ K3, (AX)
94 ADDQ $8, AX
95 KMOVQ K4, (AX)
96 ADDQ $8, AX
97 KMOVQ K5, (AX)
98 ADDQ $8, AX
99
100 return:
101 SUBQ BX, AX
102 MOVQ AX, ret+8(FP)
103 RET
104
105 TEXT ·useSecret(SB),0,$64-24
106 NO_LOCAL_POINTERS
107
108 // Load secret into AX
109 MOVQ secret_base+0(FP), AX
110 MOVQ (AX), AX
111
112 // Scatter secret all across registers.
113 // Increment low byte so we can tell which register
114 // a leaking secret came from.
115 ADDQ $2, AX // add 2 so Rn has secret #n.
116 MOVQ AX, BX
117 INCQ AX
118 MOVQ AX, CX
119 INCQ AX
120 MOVQ AX, DX
121 INCQ AX
122 MOVQ AX, SI
123 INCQ AX
124 MOVQ AX, DI
125 INCQ AX
126 MOVQ AX, BP
127 INCQ AX
128 MOVQ AX, R8
129 INCQ AX
130 MOVQ AX, R9
131 INCQ AX
132 MOVQ AX, R10
133 INCQ AX
134 MOVQ AX, R11
135 INCQ AX
136 MOVQ AX, R12
137 INCQ AX
138 MOVQ AX, R13
139 INCQ AX
140 MOVQ AX, R14
141 INCQ AX
142 MOVQ AX, R15
143
144 CMPB internal∕cpu·X86+const_offsetX86HasAVX512(SB), $1
145 JNE noavx512
146 VMOVUPD (SP), Z0
147 VMOVUPD (SP), Z1
148 VMOVUPD (SP), Z2
149 VMOVUPD (SP), Z3
150 VMOVUPD (SP), Z4
151 VMOVUPD (SP), Z5
152 VMOVUPD (SP), Z6
153 VMOVUPD (SP), Z7
154 VMOVUPD (SP), Z8
155 VMOVUPD (SP), Z9
156 VMOVUPD (SP), Z10
157 VMOVUPD (SP), Z11
158 VMOVUPD (SP), Z12
159 VMOVUPD (SP), Z13
160 VMOVUPD (SP), Z14
161 VMOVUPD (SP), Z15
162 VMOVUPD (SP), Z16
163 VMOVUPD (SP), Z17
164 VMOVUPD (SP), Z18
165 VMOVUPD (SP), Z19
166 VMOVUPD (SP), Z20
167 VMOVUPD (SP), Z21
168 VMOVUPD (SP), Z22
169 VMOVUPD (SP), Z23
170 VMOVUPD (SP), Z24
171 VMOVUPD (SP), Z25
172 VMOVUPD (SP), Z26
173 VMOVUPD (SP), Z27
174 VMOVUPD (SP), Z28
175 VMOVUPD (SP), Z29
176 VMOVUPD (SP), Z30
177 VMOVUPD (SP), Z31
178
179 noavx512:
180 MOVOU (SP), X0
181 MOVOU (SP), X1
182 MOVOU (SP), X2
183 MOVOU (SP), X3
184 MOVOU (SP), X4
185 MOVOU (SP), X5
186 MOVOU (SP), X6
187 MOVOU (SP), X7
188 MOVOU (SP), X8
189 MOVOU (SP), X9
190 MOVOU (SP), X10
191 MOVOU (SP), X11
192 MOVOU (SP), X12
193 MOVOU (SP), X13
194 MOVOU (SP), X14
195 MOVOU (SP), X15
196
197 // Put secret on the stack.
198 INCQ AX
199 MOVQ AX, (SP)
200 MOVQ AX, 8(SP)
201 MOVQ AX, 16(SP)
202 MOVQ AX, 24(SP)
203 MOVQ AX, 32(SP)
204 MOVQ AX, 40(SP)
205 MOVQ AX, 48(SP)
206 MOVQ AX, 56(SP)
207
208 // Delay a bit. This makes it more likely that
209 // we will be the target of a signal while
210 // registers contain secrets.
211 // It also tests the path from G stack to M stack
212 // to scheduler and back.
213 CALL runtime∕secret·delay(SB)
214
215 RET
216
View as plain text