1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, Joyent, Inc.
* Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
*/
/* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
/* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
/*
* Copyright (c) 2009, Intel Corporation.
* All rights reserved.
*/
#include <sys/asm_linkage.h>
#include <sys/asm_misc.h>
#include <sys/regset.h>
#include <sys/privregs.h>
#include <sys/x86_archext.h>
#include "assym.h"
/*
* Returns zero if x87 "chip" is present(!)
*/
ENTRY_NP(fpu_initial_probe)
CLTS
fninit
fnstsw %ax
movzbl %al, %eax
ret
SET_SIZE(fpu_initial_probe)
ENTRY_NP(fxsave_insn)
fxsaveq (%rdi)
ret
SET_SIZE(fxsave_insn)
/*
* One of these routines is called from any lwp with floating
* point context as part of the prolog of a context switch.
*/
/*
* These three functions define the Intel "xsave" handling for CPUs with
* different features. Newer AMD CPUs can also use these functions. See the
* 'exception pointers' comment below.
*/
ENTRY_NP(fpxsave_ctxt) /* %rdi is a struct fpu_ctx */
cmpl $FPU_EN, FPU_CTX_FPU_FLAGS(%rdi)
jne 1f
movl $_CONST(FPU_VALID|FPU_EN), FPU_CTX_FPU_FLAGS(%rdi)
movq FPU_CTX_FPU_REGS(%rdi), %rdi /* fpu_regs.kfpu_u.kfpu_fx ptr */
fxsaveq (%rdi)
STTS(%rsi) /* trap on next fpu touch */
1: rep; ret /* use 2 byte return instruction when branch target */
/* AMD Software Optimization Guide - Section 6.2 */
SET_SIZE(fpxsave_ctxt)
ENTRY_NP(xsave_ctxt)
cmpl $FPU_EN, FPU_CTX_FPU_FLAGS(%rdi)
jne 1f
movl $_CONST(FPU_VALID|FPU_EN), FPU_CTX_FPU_FLAGS(%rdi)
movl FPU_CTX_FPU_XSAVE_MASK(%rdi), %eax /* xsave flags in EDX:EAX */
movl FPU_CTX_FPU_XSAVE_MASK+4(%rdi), %edx
movq FPU_CTX_FPU_REGS(%rdi), %rsi /* fpu_regs.kfpu_u.kfpu_xs ptr */
xsave (%rsi)
STTS(%rsi) /* trap on next fpu touch */
1: ret
SET_SIZE(xsave_ctxt)
ENTRY_NP(xsaveopt_ctxt)
cmpl $FPU_EN, FPU_CTX_FPU_FLAGS(%rdi)
jne 1f
movl $_CONST(FPU_VALID|FPU_EN), FPU_CTX_FPU_FLAGS(%rdi)
movl FPU_CTX_FPU_XSAVE_MASK(%rdi), %eax /* xsave flags in EDX:EAX */
movl FPU_CTX_FPU_XSAVE_MASK+4(%rdi), %edx
movq FPU_CTX_FPU_REGS(%rdi), %rsi /* fpu_regs.kfpu_u.kfpu_xs ptr */
xsaveopt (%rsi)
STTS(%rsi) /* trap on next fpu touch */
1: ret
SET_SIZE(xsaveopt_ctxt)
/*
* On certain AMD processors, the "exception pointers" (i.e. the last
* instruction pointer, last data pointer, and last opcode) are saved by the
* fxsave, xsave or xsaveopt instruction ONLY if the exception summary bit is
* set.
*
* On newer CPUs, AMD has changed their behavior to mirror the Intel behavior.
* We can detect this via an AMD specific cpuid feature bit
* (CPUID_AMD_EBX_ERR_PTR_ZERO) and use the simpler Intel-oriented functions.
* Otherwise we use these more complex functions on AMD CPUs. All three follow
* the same logic after the xsave* instruction.
*/
ENTRY_NP(fpxsave_excp_clr_ctxt) /* %rdi is a struct fpu_ctx */
cmpl $FPU_EN, FPU_CTX_FPU_FLAGS(%rdi)
jne 1f
movl $_CONST(FPU_VALID|FPU_EN), FPU_CTX_FPU_FLAGS(%rdi)
movq FPU_CTX_FPU_REGS(%rdi), %rdi /* fpu_regs.kfpu_u.kfpu_fx ptr */
fxsaveq (%rdi)
/*
* To ensure that we don't leak these values into the next context
* on the cpu, we could just issue an fninit here, but that's
* rather slow and so we issue an instruction sequence that
* clears them more quickly, if a little obscurely.
*/
btw $7, FXSAVE_STATE_FSW(%rdi) /* Test saved ES bit */
jnc 0f /* jump if ES = 0 */
fnclex /* clear pending x87 exceptions */
0: ffree %st(7) /* clear tag bit to remove possible stack overflow */
fildl .fpzero_const(%rip)
/* dummy load changes all exception pointers */
STTS(%rsi) /* trap on next fpu touch */
1: rep; ret /* use 2 byte return instruction when branch target */
/* AMD Software Optimization Guide - Section 6.2 */
SET_SIZE(fpxsave_excp_clr_ctxt)
ENTRY_NP(xsave_excp_clr_ctxt)
cmpl $FPU_EN, FPU_CTX_FPU_FLAGS(%rdi)
jne 1f
movl $_CONST(FPU_VALID|FPU_EN), FPU_CTX_FPU_FLAGS(%rdi)
movl FPU_CTX_FPU_XSAVE_MASK(%rdi), %eax
movl FPU_CTX_FPU_XSAVE_MASK+4(%rdi), %edx
movq FPU_CTX_FPU_REGS(%rdi), %rsi /* fpu_regs.kfpu_u.kfpu_xs ptr */
xsave (%rsi)
btw $7, FXSAVE_STATE_FSW(%rsi) /* Test saved ES bit */
jnc 0f /* jump if ES = 0 */
fnclex /* clear pending x87 exceptions */
0: ffree %st(7) /* clear tag bit to remove possible stack overflow */
fildl .fpzero_const(%rip) /* dummy load changes all excp. pointers */
STTS(%rsi) /* trap on next fpu touch */
1: ret
SET_SIZE(xsave_excp_clr_ctxt)
ENTRY_NP(xsaveopt_excp_clr_ctxt)
cmpl $FPU_EN, FPU_CTX_FPU_FLAGS(%rdi)
jne 1f
movl $_CONST(FPU_VALID|FPU_EN), FPU_CTX_FPU_FLAGS(%rdi)
movl FPU_CTX_FPU_XSAVE_MASK(%rdi), %eax
movl FPU_CTX_FPU_XSAVE_MASK+4(%rdi), %edx
movq FPU_CTX_FPU_REGS(%rdi), %rsi /* fpu_regs.kfpu_u.kfpu_xs ptr */
xsaveopt (%rsi)
btw $7, FXSAVE_STATE_FSW(%rsi) /* Test saved ES bit */
jnc 0f /* jump if ES = 0 */
fnclex /* clear pending x87 exceptions */
0: ffree %st(7) /* clear tag bit to remove possible stack overflow */
fildl .fpzero_const(%rip) /* dummy load changes all excp. pointers */
STTS(%rsi) /* trap on next fpu touch */
1: ret
SET_SIZE(xsaveopt_excp_clr_ctxt)
.align 8
.fpzero_const:
.4byte 0x0
.4byte 0x0
ENTRY_NP(fpxsave)
CLTS
fxsaveq (%rdi)
fninit /* clear exceptions, init x87 tags */
STTS(%rdi) /* set TS bit in %cr0 (disable FPU) */
ret
SET_SIZE(fpxsave)
ENTRY_NP(xsave)
CLTS
movl %esi, %eax /* bv mask */
movq %rsi, %rdx
shrq $32, %rdx
xsave (%rdi)
fninit /* clear exceptions, init x87 tags */
STTS(%rdi) /* set TS bit in %cr0 (disable FPU) */
ret
SET_SIZE(xsave)
ENTRY_NP(xsaveopt)
CLTS
movl %esi, %eax /* bv mask */
movq %rsi, %rdx
shrq $32, %rdx
xsaveopt (%rdi)
fninit /* clear exceptions, init x87 tags */
STTS(%rdi) /* set TS bit in %cr0 (disable FPU) */
ret
SET_SIZE(xsaveopt)
/*
* These functions are used when restoring the FPU as part of the epilogue of a
* context switch.
*/
ENTRY(fpxrestore_ctxt)
cmpl $_CONST(FPU_EN|FPU_VALID), FPU_CTX_FPU_FLAGS(%rdi)
jne 1f
movl $_CONST(FPU_EN), FPU_CTX_FPU_FLAGS(%rdi)
movq FPU_CTX_FPU_REGS(%rdi), %rdi /* fpu_regs.kfpu_u.kfpu_fx ptr */
CLTS
fxrstorq (%rdi)
1:
ret
SET_SIZE(fpxrestore_ctxt)
ENTRY(xrestore_ctxt)
cmpl $_CONST(FPU_EN|FPU_VALID), FPU_CTX_FPU_FLAGS(%rdi)
jne 1f
movl $_CONST(FPU_EN), FPU_CTX_FPU_FLAGS(%rdi)
movl FPU_CTX_FPU_XSAVE_MASK(%rdi), %eax /* xsave flags in EDX:EAX */
movl FPU_CTX_FPU_XSAVE_MASK+4(%rdi), %edx
movq FPU_CTX_FPU_REGS(%rdi), %rdi /* fpu_regs.kfpu_u.kfpu_xs ptr */
CLTS
xrstor (%rdi)
1:
ret
SET_SIZE(xrestore_ctxt)
ENTRY_NP(fpxrestore)
CLTS
fxrstorq (%rdi)
ret
SET_SIZE(fpxrestore)
ENTRY_NP(xrestore)
CLTS
movl %esi, %eax /* bv mask */
movq %rsi, %rdx
shrq $32, %rdx
xrstor (%rdi)
ret
SET_SIZE(xrestore)
/*
* Disable the floating point unit.
*/
ENTRY_NP(fpdisable)
STTS(%rdi) /* set TS bit in %cr0 (disable FPU) */
ret
SET_SIZE(fpdisable)
/*
* Initialize the fpu hardware.
*/
ENTRY_NP(fpinit)
CLTS
cmpl $FP_XSAVE, fp_save_mech
je 1f
/* fxsave */
leaq sse_initial(%rip), %rax
fxrstorq (%rax) /* load clean initial state */
ret
1: /* xsave */
leaq avx_initial(%rip), %rcx
xorl %edx, %edx
movl $XFEATURE_AVX, %eax
btl $X86FSET_AVX, x86_featureset
cmovael %edx, %eax
orl $(XFEATURE_LEGACY_FP | XFEATURE_SSE), %eax
xrstor (%rcx)
ret
SET_SIZE(fpinit)
/*
* Clears FPU exception state.
* Returns the FP status word.
*/
ENTRY_NP(fperr_reset)
CLTS
xorl %eax, %eax
fnstsw %ax
fnclex
ret
SET_SIZE(fperr_reset)
ENTRY_NP(fpxerr_reset)
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp /* make some temporary space */
CLTS
stmxcsr (%rsp)
movl (%rsp), %eax
andl $_BITNOT(SSE_MXCSR_EFLAGS), (%rsp)
ldmxcsr (%rsp) /* clear processor exceptions */
leave
ret
SET_SIZE(fpxerr_reset)
ENTRY_NP(fpgetcwsw)
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp /* make some temporary space */
CLTS
fnstsw (%rsp) /* store the status word */
fnstcw 2(%rsp) /* store the control word */
movl (%rsp), %eax /* put both in %eax */
leave
ret
SET_SIZE(fpgetcwsw)
/*
* Returns the MXCSR register.
*/
ENTRY_NP(fpgetmxcsr)
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp /* make some temporary space */
CLTS
stmxcsr (%rsp)
movl (%rsp), %eax
leave
ret
SET_SIZE(fpgetmxcsr)
|