swtch.s revision 267654
1223695Sdfr/*-
2223695Sdfr * Copyright (c) 1990 The Regents of the University of California.
3223695Sdfr * All rights reserved.
4223695Sdfr *
5223695Sdfr * This code is derived from software contributed to Berkeley by
6223695Sdfr * William Jolitz.
7223695Sdfr *
8223695Sdfr * Redistribution and use in source and binary forms, with or without
9223695Sdfr * modification, are permitted provided that the following conditions
10223695Sdfr * are met:
11223695Sdfr * 1. Redistributions of source code must retain the above copyright
12223695Sdfr *    notice, this list of conditions and the following disclaimer.
13223695Sdfr * 2. Redistributions in binary form must reproduce the above copyright
14223695Sdfr *    notice, this list of conditions and the following disclaimer in the
15223695Sdfr *    documentation and/or other materials provided with the distribution.
16223695Sdfr * 4. Neither the name of the University nor the names of its contributors
17223695Sdfr *    may be used to endorse or promote products derived from this software
18223695Sdfr *    without specific prior written permission.
19223695Sdfr *
20223695Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21223695Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22223695Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23223695Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24223695Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25223695Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26223695Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27223695Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28223695Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29223695Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30223695Sdfr * SUCH DAMAGE.
31223695Sdfr *
32223695Sdfr * $FreeBSD: releng/9.3/sys/i386/i386/swtch.s 235796 2012-05-22 17:44:01Z iwasaki $
33223695Sdfr */
34247047Skib
35223695Sdfr#include "opt_npx.h"
36223695Sdfr#include "opt_sched.h"
37223695Sdfr
38223695Sdfr#include <machine/asmacros.h>
39223695Sdfr
40223695Sdfr#include "assym.s"
41223695Sdfr
42223695Sdfr#if defined(SMP) && defined(SCHED_ULE)
43223695Sdfr#define	SETOP		xchgl
44223695Sdfr#define	BLOCK_SPIN(reg)							\
45223695Sdfr		movl		$blocked_lock,%eax ;			\
46223695Sdfr	100: ;								\
47223695Sdfr		lock ;							\
48267399Sjhb		cmpxchgl	%eax,TD_LOCK(reg) ;			\
49267399Sjhb		jne		101f ;					\
50267399Sjhb		pause ;							\
51223695Sdfr		jmp		100b ;					\
52223695Sdfr	101:
53223695Sdfr#else
54223695Sdfr#define	SETOP		movl
55223695Sdfr#define	BLOCK_SPIN(reg)
56223695Sdfr#endif
57223695Sdfr
58223695Sdfr/*****************************************************************************/
59223695Sdfr/* Scheduling                                                                */
60223695Sdfr/*****************************************************************************/
61223695Sdfr
62223695Sdfr	.text
63267399Sjhb
64223695Sdfr/*
65223695Sdfr * cpu_throw()
66223695Sdfr *
67223695Sdfr * This is the second half of cpu_switch(). It is used when the current
68223695Sdfr * thread is either a dummy or slated to die, and we no longer care
69223695Sdfr * about its state.  This is only a slight optimization and is probably
70223695Sdfr * not worth it anymore.  Note that we need to clear the pm_active bits so
71223695Sdfr * we do need the old proc if it still exists.
72223695Sdfr * 0(%esp) = ret
73223695Sdfr * 4(%esp) = oldtd
74223695Sdfr * 8(%esp) = newtd
75223695Sdfr */
76223695SdfrENTRY(cpu_throw)
77223695Sdfr	movl	PCPU(CPUID), %esi
78223695Sdfr	movl	4(%esp),%ecx			/* Old thread */
79223695Sdfr	testl	%ecx,%ecx			/* no thread? */
80223695Sdfr	jz	1f
81223695Sdfr	/* release bit from old pm_active */
82223695Sdfr	movl	PCPU(CURPMAP), %ebx
83223695Sdfr#ifdef SMP
84267399Sjhb	lock
85267399Sjhb#endif
86267399Sjhb	btrl	%esi, PM_ACTIVE(%ebx)		/* clear old */
87267399Sjhb1:
88267399Sjhb	movl	8(%esp),%ecx			/* New thread */
89267399Sjhb	movl	TD_PCB(%ecx),%edx
90267399Sjhb	movl	PCB_CR3(%edx),%eax
91267399Sjhb	LOAD_CR3(%eax)
92267399Sjhb	/* set bit in new pm_active */
93267399Sjhb	movl	TD_PROC(%ecx),%eax
94267399Sjhb	movl	P_VMSPACE(%eax), %ebx
95267399Sjhb	addl	$VM_PMAP, %ebx
96267399Sjhb	movl	%ebx, PCPU(CURPMAP)
97267399Sjhb#ifdef SMP
98267399Sjhb	lock
99267399Sjhb#endif
100267399Sjhb	btsl	%esi, PM_ACTIVE(%ebx)		/* set new */
101267399Sjhb	jmp	sw1
102267399SjhbEND(cpu_throw)
103267399Sjhb
104267399Sjhb/*
105223695Sdfr * cpu_switch(old, new)
106223695Sdfr *
107223695Sdfr * Save the current thread state, then select the next thread to run
108223695Sdfr * and load its state.
109223695Sdfr * 0(%esp) = ret
110223695Sdfr * 4(%esp) = oldtd
111223695Sdfr * 8(%esp) = newtd
112223695Sdfr * 12(%esp) = newlock
113223695Sdfr */
114223695SdfrENTRY(cpu_switch)
115
116	/* Switch to new thread.  First, save context. */
117	movl	4(%esp),%ecx
118
119#ifdef INVARIANTS
120	testl	%ecx,%ecx			/* no thread? */
121	jz	badsw2				/* no, panic */
122#endif
123
124	movl	TD_PCB(%ecx),%edx
125
126	movl	(%esp),%eax			/* Hardware registers */
127	movl	%eax,PCB_EIP(%edx)
128	movl	%ebx,PCB_EBX(%edx)
129	movl	%esp,PCB_ESP(%edx)
130	movl	%ebp,PCB_EBP(%edx)
131	movl	%esi,PCB_ESI(%edx)
132	movl	%edi,PCB_EDI(%edx)
133	mov	%gs,PCB_GS(%edx)
134	pushfl					/* PSL */
135	popl	PCB_PSL(%edx)
136	/* Test if debug registers should be saved. */
137	testl	$PCB_DBREGS,PCB_FLAGS(%edx)
138	jz      1f                              /* no, skip over */
139	movl    %dr7,%eax                       /* yes, do the save */
140	movl    %eax,PCB_DR7(%edx)
141	andl    $0x0000fc00, %eax               /* disable all watchpoints */
142	movl    %eax,%dr7
143	movl    %dr6,%eax
144	movl    %eax,PCB_DR6(%edx)
145	movl    %dr3,%eax
146	movl    %eax,PCB_DR3(%edx)
147	movl    %dr2,%eax
148	movl    %eax,PCB_DR2(%edx)
149	movl    %dr1,%eax
150	movl    %eax,PCB_DR1(%edx)
151	movl    %dr0,%eax
152	movl    %eax,PCB_DR0(%edx)
1531:
154
155#ifdef DEV_NPX
156	/* have we used fp, and need a save? */
157	cmpl	%ecx,PCPU(FPCURTHREAD)
158	jne	1f
159	pushl	PCB_SAVEFPU(%edx)		/* h/w bugs make saving complicated */
160	call	npxsave				/* do it in a big C function */
161	popl	%eax
1621:
163#endif
164
165	/* Save is done.  Now fire up new thread. Leave old vmspace. */
166	movl	4(%esp),%edi
167	movl	8(%esp),%ecx			/* New thread */
168	movl	12(%esp),%esi			/* New lock */
169#ifdef INVARIANTS
170	testl	%ecx,%ecx			/* no thread? */
171	jz	badsw3				/* no, panic */
172#endif
173	movl	TD_PCB(%ecx),%edx
174
175	/* switch address space */
176	movl	PCB_CR3(%edx),%eax
177#ifdef PAE
178	cmpl	%eax,IdlePDPT			/* Kernel address space? */
179#else
180	cmpl	%eax,IdlePTD			/* Kernel address space? */
181#endif
182	je	sw0
183	READ_CR3(%ebx)				/* The same address space? */
184	cmpl	%ebx,%eax
185	je	sw0
186	LOAD_CR3(%eax)				/* new address space */
187	movl	%esi,%eax
188	movl	PCPU(CPUID),%esi
189	SETOP	%eax,TD_LOCK(%edi)		/* Switchout td_lock */
190
191	/* Release bit from old pmap->pm_active */
192	movl	PCPU(CURPMAP), %ebx
193#ifdef SMP
194	lock
195#endif
196	btrl	%esi, PM_ACTIVE(%ebx)		/* clear old */
197
198	/* Set bit in new pmap->pm_active */
199	movl	TD_PROC(%ecx),%eax		/* newproc */
200	movl	P_VMSPACE(%eax), %ebx
201	addl	$VM_PMAP, %ebx
202	movl	%ebx, PCPU(CURPMAP)
203#ifdef SMP
204	lock
205#endif
206	btsl	%esi, PM_ACTIVE(%ebx)		/* set new */
207	jmp	sw1
208
209sw0:
210	SETOP	%esi,TD_LOCK(%edi)		/* Switchout td_lock */
211sw1:
212	BLOCK_SPIN(%ecx)
213#ifdef XEN
214	pushl	%eax
215	pushl	%ecx
216	pushl	%edx
217	call	xen_handle_thread_switch
218	popl	%edx
219	popl	%ecx
220	popl	%eax
221	/*
222	 * XXX set IOPL
223	 */
224#else
225	/*
226	 * At this point, we've switched address spaces and are ready
227	 * to load up the rest of the next context.
228	 */
229	cmpl	$0, PCB_EXT(%edx)		/* has pcb extension? */
230	je	1f				/* If not, use the default */
231	movl	$1, PCPU(PRIVATE_TSS) 		/* mark use of private tss */
232	movl	PCB_EXT(%edx), %edi		/* new tss descriptor */
233	jmp	2f				/* Load it up */
234
2351:	/*
236	 * Use the common default TSS instead of our own.
237	 * Set our stack pointer into the TSS, it's set to just
238	 * below the PCB.  In C, common_tss.tss_esp0 = &pcb - 16;
239	 */
240	leal	-16(%edx), %ebx			/* leave space for vm86 */
241	movl	%ebx, PCPU(COMMON_TSS) + TSS_ESP0
242
243	/*
244	 * Test this CPU's  bit in the bitmap to see if this
245	 * CPU was using a private TSS.
246	 */
247	cmpl	$0, PCPU(PRIVATE_TSS)		/* Already using the common? */
248	je	3f				/* if so, skip reloading */
249	movl	$0, PCPU(PRIVATE_TSS)
250	PCPU_ADDR(COMMON_TSSD, %edi)
2512:
252	/* Move correct tss descriptor into GDT slot, then reload tr. */
253	movl	PCPU(TSS_GDT), %ebx		/* entry in GDT */
254	movl	0(%edi), %eax
255	movl	4(%edi), %esi
256	movl	%eax, 0(%ebx)
257	movl	%esi, 4(%ebx)
258	movl	$GPROC0_SEL*8, %esi		/* GSEL(GPROC0_SEL, SEL_KPL) */
259	ltr	%si
2603:
261
262	/* Copy the %fs and %gs selectors into this pcpu gdt */
263	leal	PCB_FSD(%edx), %esi
264	movl	PCPU(FSGS_GDT), %edi
265	movl	0(%esi), %eax		/* %fs selector */
266	movl	4(%esi), %ebx
267	movl	%eax, 0(%edi)
268	movl	%ebx, 4(%edi)
269	movl	8(%esi), %eax		/* %gs selector, comes straight after */
270	movl	12(%esi), %ebx
271	movl	%eax, 8(%edi)
272	movl	%ebx, 12(%edi)
273#endif
274	/* Restore context. */
275	movl	PCB_EBX(%edx),%ebx
276	movl	PCB_ESP(%edx),%esp
277	movl	PCB_EBP(%edx),%ebp
278	movl	PCB_ESI(%edx),%esi
279	movl	PCB_EDI(%edx),%edi
280	movl	PCB_EIP(%edx),%eax
281	movl	%eax,(%esp)
282	pushl	PCB_PSL(%edx)
283	popfl
284
285	movl	%edx, PCPU(CURPCB)
286	movl	TD_TID(%ecx),%eax
287	movl	%ecx, PCPU(CURTHREAD)		/* into next thread */
288
289	/*
290	 * Determine the LDT to use and load it if is the default one and
291	 * that is not the current one.
292	 */
293	movl	TD_PROC(%ecx),%eax
294	cmpl    $0,P_MD+MD_LDT(%eax)
295	jnz	1f
296	movl	_default_ldt,%eax
297	cmpl	PCPU(CURRENTLDT),%eax
298	je	2f
299	LLDT(_default_ldt)
300	movl	%eax,PCPU(CURRENTLDT)
301	jmp	2f
3021:
303	/* Load the LDT when it is not the default one. */
304	pushl	%edx				/* Preserve pointer to pcb. */
305	addl	$P_MD,%eax			/* Pointer to mdproc is arg. */
306	pushl	%eax
307	call	set_user_ldt
308	addl	$4,%esp
309	popl	%edx
3102:
311
312	/* This must be done after loading the user LDT. */
313	.globl	cpu_switch_load_gs
314cpu_switch_load_gs:
315	mov	PCB_GS(%edx),%gs
316
317	/* Test if debug registers should be restored. */
318	testl	$PCB_DBREGS,PCB_FLAGS(%edx)
319	jz      1f
320
321	/*
322	 * Restore debug registers.  The special code for dr7 is to
323	 * preserve the current values of its reserved bits.
324	 */
325	movl    PCB_DR6(%edx),%eax
326	movl    %eax,%dr6
327	movl    PCB_DR3(%edx),%eax
328	movl    %eax,%dr3
329	movl    PCB_DR2(%edx),%eax
330	movl    %eax,%dr2
331	movl    PCB_DR1(%edx),%eax
332	movl    %eax,%dr1
333	movl    PCB_DR0(%edx),%eax
334	movl    %eax,%dr0
335	movl	%dr7,%eax
336	andl    $0x0000fc00,%eax
337	movl    PCB_DR7(%edx),%ecx
338	andl	$~0x0000fc00,%ecx
339	orl     %ecx,%eax
340	movl    %eax,%dr7
3411:
342	ret
343
344#ifdef INVARIANTS
345badsw1:
346	pushal
347	pushl	$sw0_1
348	call	panic
349sw0_1:	.asciz	"cpu_throw: no newthread supplied"
350
351badsw2:
352	pushal
353	pushl	$sw0_2
354	call	panic
355sw0_2:	.asciz	"cpu_switch: no curthread supplied"
356
357badsw3:
358	pushal
359	pushl	$sw0_3
360	call	panic
361sw0_3:	.asciz	"cpu_switch: no newthread supplied"
362#endif
363END(cpu_switch)
364
365/*
366 * savectx(pcb)
367 * Update pcb, saving current processor state.
368 */
369ENTRY(savectx)
370	/* Fetch PCB. */
371	movl	4(%esp),%ecx
372
373	/* Save caller's return address.  Child won't execute this routine. */
374	movl	(%esp),%eax
375	movl	%eax,PCB_EIP(%ecx)
376
377	movl	%cr3,%eax
378	movl	%eax,PCB_CR3(%ecx)
379
380	movl	%ebx,PCB_EBX(%ecx)
381	movl	%esp,PCB_ESP(%ecx)
382	movl	%ebp,PCB_EBP(%ecx)
383	movl	%esi,PCB_ESI(%ecx)
384	movl	%edi,PCB_EDI(%ecx)
385	mov	%gs,PCB_GS(%ecx)
386	pushfl
387	popl	PCB_PSL(%ecx)
388
389#ifdef DEV_NPX
390	/*
391	 * If fpcurthread == NULL, then the npx h/w state is irrelevant and the
392	 * state had better already be in the pcb.  This is true for forks
393	 * but not for dumps (the old book-keeping with FP flags in the pcb
394	 * always lost for dumps because the dump pcb has 0 flags).
395	 *
396	 * If fpcurthread != NULL, then we have to save the npx h/w state to
397	 * fpcurthread's pcb and copy it to the requested pcb, or save to the
398	 * requested pcb and reload.  Copying is easier because we would
399	 * have to handle h/w bugs for reloading.  We used to lose the
400	 * parent's npx state for forks by forgetting to reload.
401	 */
402	pushfl
403	CLI
404	movl	PCPU(FPCURTHREAD),%eax
405	testl	%eax,%eax
406	je	1f
407
408	pushl	%ecx
409	movl	TD_PCB(%eax),%eax
410	movl	PCB_SAVEFPU(%eax),%eax
411	pushl	%eax
412	pushl	%eax
413	call	npxsave
414	addl	$4,%esp
415	popl	%eax
416	popl	%ecx
417
418	pushl	$PCB_SAVEFPU_SIZE
419	leal	PCB_USERFPU(%ecx),%ecx
420	pushl	%ecx
421	pushl	%eax
422	call	bcopy
423	addl	$12,%esp
4241:
425	popfl
426#endif	/* DEV_NPX */
427
428	ret
429END(savectx)
430
431/*
432 * suspendctx(pcb)
433 * Update pcb, suspending current processor state.
434 */
435ENTRY(suspendctx)
436	/* Fetch PCB. */
437	movl	4(%esp),%ecx
438
439	/* Save context by calling savectx(). */
440	pushl	%ecx
441	call	savectx
442	addl	$4,%esp
443
444	/* Fetch PCB again. */
445	movl	4(%esp),%ecx
446
447	/* Update caller's return address and stack pointer. */
448	movl	(%esp),%eax
449	movl	%eax,PCB_EIP(%ecx)
450	movl	%esp,PCB_ESP(%ecx)
451
452	/* Save other registers and descriptor tables. */
453	movl	%cr0,%eax
454	movl	%eax,PCB_CR0(%ecx)
455	movl	%cr2,%eax
456	movl	%eax,PCB_CR2(%ecx)
457	movl	%cr4,%eax
458	movl	%eax,PCB_CR4(%ecx)
459
460	movl	%dr0,%eax
461	movl	%eax,PCB_DR0(%ecx)
462	movl	%dr1,%eax
463	movl	%eax,PCB_DR1(%ecx)
464	movl	%dr2,%eax
465	movl	%eax,PCB_DR2(%ecx)
466	movl	%dr3,%eax
467	movl	%eax,PCB_DR3(%ecx)
468	movl	%dr6,%eax
469	movl	%eax,PCB_DR6(%ecx)
470	movl	%dr7,%eax
471	movl	%eax,PCB_DR7(%ecx)
472
473	mov	%ds,PCB_DS(%ecx)
474	mov	%es,PCB_ES(%ecx)
475	mov	%fs,PCB_FS(%ecx)
476	mov	%ss,PCB_SS(%ecx)
477
478	sgdt	PCB_GDT(%ecx)
479	sidt	PCB_IDT(%ecx)
480	sldt	PCB_LDT(%ecx)
481	str	PCB_TR(%ecx)
482
483	movl	$1,%eax
484	ret
485END(suspendctx)
486
487/*
488 * resumectx(pcb in %esi)
489 * Resuming processor state from pcb.
490 */
491ENTRY(resumectx)
492	/* Fetch PCB. */
493	movl	%esi,%ecx
494
495	/* Restore GDT. */
496	lgdt	PCB_GDT(%ecx)
497
498	/* Restore segment registers */
499	movzwl	PCB_DS(%ecx),%eax
500	mov	%ax,%ds
501	movzwl	PCB_ES(%ecx),%eax
502	mov	%ax,%es
503	movzwl	PCB_FS(%ecx),%eax
504	mov	%ax,%fs
505	movzwl	PCB_GS(%ecx),%eax
506	movw	%ax,%gs
507	movzwl	PCB_SS(%ecx),%eax
508	mov	%ax,%ss
509
510	/* Restore CR2, CR4, CR3 and CR0 */
511	movl	PCB_CR2(%ecx),%eax
512	movl	%eax,%cr2
513	movl	PCB_CR4(%ecx),%eax
514	movl	%eax,%cr4
515	movl	PCB_CR3(%ecx),%eax
516	movl	%eax,%cr3
517	movl	PCB_CR0(%ecx),%eax
518	movl	%eax,%cr0
519	jmp	1f
5201:
521
522	/* Restore descriptor tables */
523	lidt	PCB_IDT(%ecx)
524	lldt	PCB_LDT(%ecx)
525
526#define SDT_SYS386TSS	9
527#define SDT_SYS386BSY	11
528	/* Clear "task busy" bit and reload TR */
529	movl	PCPU(TSS_GDT),%eax
530	andb	$(~SDT_SYS386BSY | SDT_SYS386TSS),5(%eax)
531	movzwl	PCB_TR(%ecx),%eax
532	ltr	%ax
533#undef SDT_SYS386TSS
534#undef SDT_SYS386BSY
535
536	/* Restore debug registers */
537	movl	PCB_DR0(%ecx),%eax
538	movl	%eax,%dr0
539	movl	PCB_DR1(%ecx),%eax
540	movl	%eax,%dr1
541	movl	PCB_DR2(%ecx),%eax
542	movl	%eax,%dr2
543	movl	PCB_DR3(%ecx),%eax
544	movl	%eax,%dr3
545	movl	PCB_DR6(%ecx),%eax
546	movl	%eax,%dr6
547	movl	PCB_DR7(%ecx),%eax
548	movl	%eax,%dr7
549
550#ifdef DEV_NPX
551	/* XXX FIX ME */
552#endif
553
554	/* Restore other registers */
555	movl	PCB_EDI(%ecx),%edi
556	movl	PCB_ESI(%ecx),%esi
557	movl	PCB_EBP(%ecx),%ebp
558	movl	PCB_ESP(%ecx),%esp
559	movl	PCB_EBX(%ecx),%ebx
560
561	/* reload code selector by turning return into intersegmental return */
562	pushl	PCB_EIP(%ecx)
563	movl	$KCSEL,4(%esp)
564	xorl	%eax,%eax
565	lret
566END(resumectx)
567