1/*-
2 * Copyright (c) 2003 Peter Wemm.
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: stable/11/sys/amd64/amd64/cpu_switch.S 361561 2020-05-27 18:55:24Z kib $
34 */
35
36#include <machine/asmacros.h>
37#include <machine/specialreg.h>
38
39#include "assym.s"
40#include "opt_sched.h"
41
42/*****************************************************************************/
43/* Scheduling                                                                */
44/*****************************************************************************/
45
46	.text
47
48#ifdef SMP
49#define LK	lock ;
50#else
51#define LK
52#endif
53
54#if defined(SCHED_ULE) && defined(SMP)
55#define	SETLK	xchgq
56#else
57#define	SETLK	movq
58#endif
59
60/*
61 * cpu_throw()
62 *
63 * This is the second half of cpu_switch(). It is used when the current
64 * thread is either a dummy or slated to die, and we no longer care
65 * about its state.  This is only a slight optimization and is probably
66 * not worth it anymore.  Note that we need to clear the pm_active bits so
67 * we do need the old proc if it still exists.
68 * %rdi = oldtd
69 * %rsi = newtd
70 */
71ENTRY(cpu_throw)
72	movq	%rsi,%r12
73	movq	%rsi,%rdi
74	call	pmap_activate_sw
75	jmp	sw1
76END(cpu_throw)
77
78/*
79 * cpu_switch(old, new, mtx)
80 *
81 * Save the current thread state, then select the next thread to run
82 * and load its state.
83 * %rdi = oldtd
84 * %rsi = newtd
85 * %rdx = mtx
86 */
87ENTRY(cpu_switch)
88	/* Switch to new thread.  First, save context. */
89	movq	TD_PCB(%rdi),%r8
90
91	movq	(%rsp),%rax			/* Hardware registers */
92	movq	%r15,PCB_R15(%r8)
93	movq	%r14,PCB_R14(%r8)
94	movq	%r13,PCB_R13(%r8)
95	movq	%r12,PCB_R12(%r8)
96	movq	%rbp,PCB_RBP(%r8)
97	movq	%rsp,PCB_RSP(%r8)
98	movq	%rbx,PCB_RBX(%r8)
99	movq	%rax,PCB_RIP(%r8)
100
101	testl	$PCB_FULL_IRET,PCB_FLAGS(%r8)
102	jnz	2f
103	orl	$PCB_FULL_IRET,PCB_FLAGS(%r8)
104	testl	$TDP_KTHREAD,TD_PFLAGS(%rdi)
105	jnz	2f
106	testb	$CPUID_STDEXT_FSGSBASE,cpu_stdext_feature(%rip)
107	jz	2f
108	movl	%fs,%eax
109	cmpl	$KUF32SEL,%eax
110	jne	1f
111	rdfsbase %rax
112	movq	%rax,PCB_FSBASE(%r8)
1131:	movl	%gs,%eax
114	cmpl	$KUG32SEL,%eax
115	jne	2f
116	movq	%rdx,%r12
117	movl	$MSR_KGSBASE,%ecx		/* Read user gs base */
118	rdmsr
119	shlq	$32,%rdx
120	orq	%rdx,%rax
121	movq	%rax,PCB_GSBASE(%r8)
122	movq	%r12,%rdx
123
1242:
125	testl	$PCB_DBREGS,PCB_FLAGS(%r8)
126	jnz	store_dr			/* static predict not taken */
127done_store_dr:
128
129	/* have we used fp, and need a save? */
130	cmpq	%rdi,PCPU(FPCURTHREAD)
131	jne	2f
132	movq	PCB_SAVEFPU(%r8),%r8
133	clts
134	cmpl	$0,use_xsave(%rip)
135	jne	1f
136	fxsave	(%r8)
137	jmp	2f
1381:	movq	%rdx,%rcx
139	movl	xsave_mask,%eax
140	movl	xsave_mask+4,%edx
141	.globl	ctx_switch_xsave
142ctx_switch_xsave:
143	/* This is patched to xsaveopt if supported, see fpuinit_bsp1() */
144	xsave	(%r8)
145	movq	%rcx,%rdx
1462:
147	/* Save is done.  Now fire up new thread. Leave old vmspace. */
148	movq	%rsi,%r12
149	movq	%rdi,%r13
150	movq	%rdx,%r15
151	movq	%rsi,%rdi
152	callq	pmap_activate_sw
153	SETLK	%r15,TD_LOCK(%r13)		/* Release the old thread */
154sw1:
155	movq	TD_PCB(%r12),%r8
156#if defined(SCHED_ULE) && defined(SMP)
157	/* Wait for the new thread to become unblocked */
158	movq	$blocked_lock, %rdx
1591:
160	movq	TD_LOCK(%r12),%rcx
161	cmpq	%rcx, %rdx
162	pause
163	je	1b
164#endif
165	/*
166	 * At this point, we've switched address spaces and are ready
167	 * to load up the rest of the next context.
168	 */
169
170	/* Skip loading LDT and user fsbase/gsbase for kthreads */
171	testl	$TDP_KTHREAD,TD_PFLAGS(%r12)
172	jnz	do_kthread
173
174	/*
175	 * Load ldt register
176	 */
177	movq	TD_PROC(%r12),%rcx
178	cmpq	$0, P_MD+MD_LDT(%rcx)
179	jne	do_ldt
180	xorl	%eax,%eax
181ld_ldt:	lldt	%ax
182
183	/* Restore fs base in GDT */
184	movl	PCB_FSBASE(%r8),%eax
185	movq	PCPU(FS32P),%rdx
186	movw	%ax,2(%rdx)
187	shrl	$16,%eax
188	movb	%al,4(%rdx)
189	shrl	$8,%eax
190	movb	%al,7(%rdx)
191
192	/* Restore gs base in GDT */
193	movl	PCB_GSBASE(%r8),%eax
194	movq	PCPU(GS32P),%rdx
195	movw	%ax,2(%rdx)
196	shrl	$16,%eax
197	movb	%al,4(%rdx)
198	shrl	$8,%eax
199	movb	%al,7(%rdx)
200
201do_kthread:
202	/* Do we need to reload tss ? */
203	movq	PCPU(TSSP),%rax
204	movq	PCB_TSSP(%r8),%rdx
205	testq	%rdx,%rdx
206	cmovzq	PCPU(COMMONTSSP),%rdx
207	cmpq	%rax,%rdx
208	jne	do_tss
209done_tss:
210	movq	%r8,PCPU(RSP0)
211	movq	%r8,PCPU(CURPCB)
212	/* Update the TSS_RSP0 pointer for the next interrupt */
213	cmpq	$~0,PCPU(UCR3)
214	je	1f
215	movq	PCPU(PTI_RSP0),%rax
216	movq	%rax,TSS_RSP0(%rdx)
217	jmp	2f
2181:	movq	%r8,TSS_RSP0(%rdx)
2192:	movq	%r12,PCPU(CURTHREAD)		/* into next thread */
220
221	/* Test if debug registers should be restored. */
222	testl	$PCB_DBREGS,PCB_FLAGS(%r8)
223	jnz	load_dr				/* static predict not taken */
224done_load_dr:
225
226	/* Restore context. */
227	movq	PCB_R15(%r8),%r15
228	movq	PCB_R14(%r8),%r14
229	movq	PCB_R13(%r8),%r13
230	movq	PCB_R12(%r8),%r12
231	movq	PCB_RBP(%r8),%rbp
232	movq	PCB_RSP(%r8),%rsp
233	movq	PCB_RBX(%r8),%rbx
234	movq	PCB_RIP(%r8),%rax
235	movq	%rax,(%rsp)
236	movq	PCPU(CURTHREAD),%rdi
237	call	fpu_activate_sw
238	cmpb	$0,cpu_flush_rsb_ctxsw(%rip)
239	jne	rsb_flush
240	ret
241
242	/*
243	 * We order these strangely for several reasons.
244	 * 1: I wanted to use static branch prediction hints
245	 * 2: Most athlon64/opteron cpus don't have them.  They define
246	 *    a forward branch as 'predict not taken'.  Intel cores have
247	 *    the 'rep' prefix to invert this.
248	 * So, to make it work on both forms of cpu we do the detour.
249	 * We use jumps rather than call in order to avoid the stack.
250	 */
251
252store_dr:
253	movq	%dr7,%rax			/* yes, do the save */
254	movq	%dr0,%r15
255	movq	%dr1,%r14
256	movq	%dr2,%r13
257	movq	%dr3,%r12
258	movq	%dr6,%r11
259	movq	%r15,PCB_DR0(%r8)
260	movq	%r14,PCB_DR1(%r8)
261	movq	%r13,PCB_DR2(%r8)
262	movq	%r12,PCB_DR3(%r8)
263	movq	%r11,PCB_DR6(%r8)
264	movq	%rax,PCB_DR7(%r8)
265	andq	$0x0000fc00, %rax		/* disable all watchpoints */
266	movq	%rax,%dr7
267	jmp	done_store_dr
268
269load_dr:
270	movq	%dr7,%rax
271	movq	PCB_DR0(%r8),%r15
272	movq	PCB_DR1(%r8),%r14
273	movq	PCB_DR2(%r8),%r13
274	movq	PCB_DR3(%r8),%r12
275	movq	PCB_DR6(%r8),%r11
276	movq	PCB_DR7(%r8),%rcx
277	movq	%r15,%dr0
278	movq	%r14,%dr1
279	/* Preserve reserved bits in %dr7 */
280	andq	$0x0000fc00,%rax
281	andq	$~0x0000fc00,%rcx
282	movq	%r13,%dr2
283	movq	%r12,%dr3
284	orq	%rcx,%rax
285	movq	%r11,%dr6
286	movq	%rax,%dr7
287	jmp	done_load_dr
288
289do_tss:	movq	%rdx,PCPU(TSSP)
290	movq	%rdx,%rcx
291	movq	PCPU(TSS),%rax
292	movw	%cx,2(%rax)
293	shrq	$16,%rcx
294	movb	%cl,4(%rax)
295	shrq	$8,%rcx
296	movb	%cl,7(%rax)
297	shrq	$8,%rcx
298	movl	%ecx,8(%rax)
299	movb	$0x89,5(%rax)	/* unset busy */
300	movl	$TSSSEL,%eax
301	ltr	%ax
302	jmp	done_tss
303
304do_ldt:	movq	PCPU(LDT),%rax
305	movq	P_MD+MD_LDT_SD(%rcx),%rdx
306	movq	%rdx,(%rax)
307	movq	P_MD+MD_LDT_SD+8(%rcx),%rdx
308	movq	%rdx,8(%rax)
309	movl	$LDTSEL,%eax
310	jmp	ld_ldt
311END(cpu_switch)
312
313/*
314 * savectx(pcb)
315 * Update pcb, saving current processor state.
316 */
317ENTRY(savectx)
318	/* Save caller's return address. */
319	movq	(%rsp),%rax
320	movq	%rax,PCB_RIP(%rdi)
321
322	movq	%rbx,PCB_RBX(%rdi)
323	movq	%rsp,PCB_RSP(%rdi)
324	movq	%rbp,PCB_RBP(%rdi)
325	movq	%r12,PCB_R12(%rdi)
326	movq	%r13,PCB_R13(%rdi)
327	movq	%r14,PCB_R14(%rdi)
328	movq	%r15,PCB_R15(%rdi)
329
330	movq	%cr0,%rax
331	movq	%rax,PCB_CR0(%rdi)
332	movq	%cr2,%rax
333	movq	%rax,PCB_CR2(%rdi)
334	movq	%cr3,%rax
335	movq	%rax,PCB_CR3(%rdi)
336	movq	%cr4,%rax
337	movq	%rax,PCB_CR4(%rdi)
338
339	movq	%dr0,%rax
340	movq	%rax,PCB_DR0(%rdi)
341	movq	%dr1,%rax
342	movq	%rax,PCB_DR1(%rdi)
343	movq	%dr2,%rax
344	movq	%rax,PCB_DR2(%rdi)
345	movq	%dr3,%rax
346	movq	%rax,PCB_DR3(%rdi)
347	movq	%dr6,%rax
348	movq	%rax,PCB_DR6(%rdi)
349	movq	%dr7,%rax
350	movq	%rax,PCB_DR7(%rdi)
351
352	movl	$MSR_FSBASE,%ecx
353	rdmsr
354	movl	%eax,PCB_FSBASE(%rdi)
355	movl	%edx,PCB_FSBASE+4(%rdi)
356	movl	$MSR_GSBASE,%ecx
357	rdmsr
358	movl	%eax,PCB_GSBASE(%rdi)
359	movl	%edx,PCB_GSBASE+4(%rdi)
360	movl	$MSR_KGSBASE,%ecx
361	rdmsr
362	movl	%eax,PCB_KGSBASE(%rdi)
363	movl	%edx,PCB_KGSBASE+4(%rdi)
364	movl	$MSR_EFER,%ecx
365	rdmsr
366	movl	%eax,PCB_EFER(%rdi)
367	movl	%edx,PCB_EFER+4(%rdi)
368	movl	$MSR_STAR,%ecx
369	rdmsr
370	movl	%eax,PCB_STAR(%rdi)
371	movl	%edx,PCB_STAR+4(%rdi)
372	movl	$MSR_LSTAR,%ecx
373	rdmsr
374	movl	%eax,PCB_LSTAR(%rdi)
375	movl	%edx,PCB_LSTAR+4(%rdi)
376	movl	$MSR_CSTAR,%ecx
377	rdmsr
378	movl	%eax,PCB_CSTAR(%rdi)
379	movl	%edx,PCB_CSTAR+4(%rdi)
380	movl	$MSR_SF_MASK,%ecx
381	rdmsr
382	movl	%eax,PCB_SFMASK(%rdi)
383	movl	%edx,PCB_SFMASK+4(%rdi)
384
385	sgdt	PCB_GDT(%rdi)
386	sidt	PCB_IDT(%rdi)
387	sldt	PCB_LDT(%rdi)
388	str	PCB_TR(%rdi)
389
390	movl	$1,%eax
391	ret
392END(savectx)
393
394/*
395 * resumectx(pcb)
396 * Resuming processor state from pcb.
397 */
398ENTRY(resumectx)
399	/* Switch to KPML4phys. */
400	movq	KPML4phys,%rax
401	movq	%rax,%cr3
402
403	/* Force kernel segment registers. */
404	movl	$KDSEL,%eax
405	movw	%ax,%ds
406	movw	%ax,%es
407	movw	%ax,%ss
408	movl	$KUF32SEL,%eax
409	movw	%ax,%fs
410	movl	$KUG32SEL,%eax
411	movw	%ax,%gs
412
413	movl	$MSR_FSBASE,%ecx
414	movl	PCB_FSBASE(%rdi),%eax
415	movl	4 + PCB_FSBASE(%rdi),%edx
416	wrmsr
417	movl	$MSR_GSBASE,%ecx
418	movl	PCB_GSBASE(%rdi),%eax
419	movl	4 + PCB_GSBASE(%rdi),%edx
420	wrmsr
421	movl	$MSR_KGSBASE,%ecx
422	movl	PCB_KGSBASE(%rdi),%eax
423	movl	4 + PCB_KGSBASE(%rdi),%edx
424	wrmsr
425
426	/* Restore EFER one more time. */
427	movl	$MSR_EFER,%ecx
428	movl	PCB_EFER(%rdi),%eax
429	wrmsr
430
431	/* Restore fast syscall stuff. */
432	movl	$MSR_STAR,%ecx
433	movl	PCB_STAR(%rdi),%eax
434	movl	4 + PCB_STAR(%rdi),%edx
435	wrmsr
436	movl	$MSR_LSTAR,%ecx
437	movl	PCB_LSTAR(%rdi),%eax
438	movl	4 + PCB_LSTAR(%rdi),%edx
439	wrmsr
440	movl	$MSR_CSTAR,%ecx
441	movl	PCB_CSTAR(%rdi),%eax
442	movl	4 + PCB_CSTAR(%rdi),%edx
443	wrmsr
444	movl	$MSR_SF_MASK,%ecx
445	movl	PCB_SFMASK(%rdi),%eax
446	wrmsr
447
448	/* Restore CR0, CR2, CR4 and CR3. */
449	movq	PCB_CR0(%rdi),%rax
450	movq	%rax,%cr0
451	movq	PCB_CR2(%rdi),%rax
452	movq	%rax,%cr2
453	movq	PCB_CR4(%rdi),%rax
454	movq	%rax,%cr4
455	movq	PCB_CR3(%rdi),%rax
456	movq	%rax,%cr3
457
458	/* Restore descriptor tables. */
459	lidt	PCB_IDT(%rdi)
460	lldt	PCB_LDT(%rdi)
461
462#define	SDT_SYSTSS	9
463#define	SDT_SYSBSY	11
464
465	/* Clear "task busy" bit and reload TR. */
466	movq	PCPU(TSS),%rax
467	andb	$(~SDT_SYSBSY | SDT_SYSTSS),5(%rax)
468	movw	PCB_TR(%rdi),%ax
469	ltr	%ax
470
471#undef	SDT_SYSTSS
472#undef	SDT_SYSBSY
473
474	/* Restore debug registers. */
475	movq	PCB_DR0(%rdi),%rax
476	movq	%rax,%dr0
477	movq	PCB_DR1(%rdi),%rax
478	movq	%rax,%dr1
479	movq	PCB_DR2(%rdi),%rax
480	movq	%rax,%dr2
481	movq	PCB_DR3(%rdi),%rax
482	movq	%rax,%dr3
483	movq	PCB_DR6(%rdi),%rax
484	movq	%rax,%dr6
485	movq	PCB_DR7(%rdi),%rax
486	movq	%rax,%dr7
487
488	/* Restore other callee saved registers. */
489	movq	PCB_R15(%rdi),%r15
490	movq	PCB_R14(%rdi),%r14
491	movq	PCB_R13(%rdi),%r13
492	movq	PCB_R12(%rdi),%r12
493	movq	PCB_RBP(%rdi),%rbp
494	movq	PCB_RSP(%rdi),%rsp
495	movq	PCB_RBX(%rdi),%rbx
496
497	/* Restore return address. */
498	movq	PCB_RIP(%rdi),%rax
499	movq	%rax,(%rsp)
500
501	xorl	%eax,%eax
502	ret
503END(resumectx)
504