xref: /freebsd/sys/i386/i386/swtch.S (revision 95ee2897)
1ccd9b49fSElliott Mitchell/*-
2ccd9b49fSElliott Mitchell * Copyright (c) 1990 The Regents of the University of California.
3ccd9b49fSElliott Mitchell * All rights reserved.
4ccd9b49fSElliott Mitchell *
5ccd9b49fSElliott Mitchell * This code is derived from software contributed to Berkeley by
6ccd9b49fSElliott Mitchell * William Jolitz.
7ccd9b49fSElliott Mitchell *
8ccd9b49fSElliott Mitchell * Redistribution and use in source and binary forms, with or without
9ccd9b49fSElliott Mitchell * modification, are permitted provided that the following conditions
10ccd9b49fSElliott Mitchell * are met:
11ccd9b49fSElliott Mitchell * 1. Redistributions of source code must retain the above copyright
12ccd9b49fSElliott Mitchell *    notice, this list of conditions and the following disclaimer.
13ccd9b49fSElliott Mitchell * 2. Redistributions in binary form must reproduce the above copyright
14ccd9b49fSElliott Mitchell *    notice, this list of conditions and the following disclaimer in the
15ccd9b49fSElliott Mitchell *    documentation and/or other materials provided with the distribution.
16ccd9b49fSElliott Mitchell * 3. Neither the name of the University nor the names of its contributors
17ccd9b49fSElliott Mitchell *    may be used to endorse or promote products derived from this software
18ccd9b49fSElliott Mitchell *    without specific prior written permission.
19ccd9b49fSElliott Mitchell *
20ccd9b49fSElliott Mitchell * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21ccd9b49fSElliott Mitchell * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22ccd9b49fSElliott Mitchell * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ccd9b49fSElliott Mitchell * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24ccd9b49fSElliott Mitchell * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25ccd9b49fSElliott Mitchell * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26ccd9b49fSElliott Mitchell * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27ccd9b49fSElliott Mitchell * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28ccd9b49fSElliott Mitchell * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29ccd9b49fSElliott Mitchell * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30ccd9b49fSElliott Mitchell * SUCH DAMAGE.
31ccd9b49fSElliott Mitchell */
32ccd9b49fSElliott Mitchell
33ccd9b49fSElliott Mitchell#include "opt_sched.h"
34ccd9b49fSElliott Mitchell
35ccd9b49fSElliott Mitchell#include <machine/asmacros.h>
36ccd9b49fSElliott Mitchell
37ccd9b49fSElliott Mitchell#include "assym.inc"
38ccd9b49fSElliott Mitchell
39ccd9b49fSElliott Mitchell#if defined(SMP) && defined(SCHED_ULE)
40ccd9b49fSElliott Mitchell#define	SETOP		xchgl
41ccd9b49fSElliott Mitchell#define	BLOCK_SPIN(reg)							\
42ccd9b49fSElliott Mitchell		movl		$blocked_lock,%eax ;			\
43ccd9b49fSElliott Mitchell	100: ;								\
44ccd9b49fSElliott Mitchell		lock ;							\
45ccd9b49fSElliott Mitchell		cmpxchgl	%eax,TD_LOCK(reg) ;			\
46ccd9b49fSElliott Mitchell		jne		101f ;					\
47ccd9b49fSElliott Mitchell		pause ;							\
48ccd9b49fSElliott Mitchell		jmp		100b ;					\
49ccd9b49fSElliott Mitchell	101:
50ccd9b49fSElliott Mitchell#else
51ccd9b49fSElliott Mitchell#define	SETOP		movl
52ccd9b49fSElliott Mitchell#define	BLOCK_SPIN(reg)
53ccd9b49fSElliott Mitchell#endif
54ccd9b49fSElliott Mitchell
55ccd9b49fSElliott Mitchell/*****************************************************************************/
56ccd9b49fSElliott Mitchell/* Scheduling                                                                */
57ccd9b49fSElliott Mitchell/*****************************************************************************/
58ccd9b49fSElliott Mitchell
59ccd9b49fSElliott Mitchell	.text
60ccd9b49fSElliott Mitchell
61ccd9b49fSElliott Mitchell/*
62ccd9b49fSElliott Mitchell * cpu_throw()
63ccd9b49fSElliott Mitchell *
64ccd9b49fSElliott Mitchell * This is the second half of cpu_switch(). It is used when the current
65ccd9b49fSElliott Mitchell * thread is either a dummy or slated to die, and we no longer care
66ccd9b49fSElliott Mitchell * about its state.  This is only a slight optimization and is probably
67ccd9b49fSElliott Mitchell * not worth it anymore.  Note that we need to clear the pm_active bits so
68ccd9b49fSElliott Mitchell * we do need the old proc if it still exists.
69ccd9b49fSElliott Mitchell * 0(%esp) = ret
70ccd9b49fSElliott Mitchell * 4(%esp) = oldtd
71ccd9b49fSElliott Mitchell * 8(%esp) = newtd
72ccd9b49fSElliott Mitchell */
73ccd9b49fSElliott MitchellENTRY(cpu_throw)
74ccd9b49fSElliott Mitchell	movl	PCPU(CPUID), %esi
75ccd9b49fSElliott Mitchell	/* release bit from old pm_active */
76ccd9b49fSElliott Mitchell	movl	PCPU(CURPMAP), %ebx
77ccd9b49fSElliott Mitchell#ifdef SMP
78ccd9b49fSElliott Mitchell	lock
79ccd9b49fSElliott Mitchell#endif
80ccd9b49fSElliott Mitchell	btrl	%esi, PM_ACTIVE(%ebx)		/* clear old */
81ccd9b49fSElliott Mitchell	movl	8(%esp),%ecx			/* New thread */
82ccd9b49fSElliott Mitchell	movl	TD_PCB(%ecx),%edx
83ccd9b49fSElliott Mitchell	/* set bit in new pm_active */
84ccd9b49fSElliott Mitchell	movl	TD_PROC(%ecx),%eax
85ccd9b49fSElliott Mitchell	movl	P_VMSPACE(%eax), %ebx
86ccd9b49fSElliott Mitchell	addl	$VM_PMAP, %ebx
87ccd9b49fSElliott Mitchell	movl	%ebx, PCPU(CURPMAP)
88ccd9b49fSElliott Mitchell#ifdef SMP
89ccd9b49fSElliott Mitchell	lock
90ccd9b49fSElliott Mitchell#endif
91ccd9b49fSElliott Mitchell	btsl	%esi, PM_ACTIVE(%ebx)		/* set new */
92ccd9b49fSElliott Mitchell	jmp	sw1
93ccd9b49fSElliott MitchellEND(cpu_throw)
94ccd9b49fSElliott Mitchell
95ccd9b49fSElliott Mitchell/*
96ccd9b49fSElliott Mitchell * cpu_switch(old, new)
97ccd9b49fSElliott Mitchell *
98ccd9b49fSElliott Mitchell * Save the current thread state, then select the next thread to run
99ccd9b49fSElliott Mitchell * and load its state.
100ccd9b49fSElliott Mitchell * 0(%esp) = ret
101ccd9b49fSElliott Mitchell * 4(%esp) = oldtd
102ccd9b49fSElliott Mitchell * 8(%esp) = newtd
103ccd9b49fSElliott Mitchell * 12(%esp) = newlock
104ccd9b49fSElliott Mitchell */
105ccd9b49fSElliott MitchellENTRY(cpu_switch)
106ccd9b49fSElliott Mitchell
107ccd9b49fSElliott Mitchell	/* Switch to new thread.  First, save context. */
108ccd9b49fSElliott Mitchell	movl	4(%esp),%ecx
109ccd9b49fSElliott Mitchell
110ccd9b49fSElliott Mitchell#ifdef INVARIANTS
111ccd9b49fSElliott Mitchell	testl	%ecx,%ecx			/* no thread? */
112ccd9b49fSElliott Mitchell	jz	badsw2				/* no, panic */
113ccd9b49fSElliott Mitchell#endif
114ccd9b49fSElliott Mitchell
115ccd9b49fSElliott Mitchell	movl	TD_PCB(%ecx),%edx
116ccd9b49fSElliott Mitchell
117ccd9b49fSElliott Mitchell	movl	(%esp),%eax			/* Hardware registers */
118ccd9b49fSElliott Mitchell	movl	%eax,PCB_EIP(%edx)
119ccd9b49fSElliott Mitchell	movl	%ebx,PCB_EBX(%edx)
120ccd9b49fSElliott Mitchell	movl	%esp,PCB_ESP(%edx)
121ccd9b49fSElliott Mitchell	movl	%ebp,PCB_EBP(%edx)
122ccd9b49fSElliott Mitchell	movl	%esi,PCB_ESI(%edx)
123ccd9b49fSElliott Mitchell	movl	%edi,PCB_EDI(%edx)
124ccd9b49fSElliott Mitchell	mov	%gs,PCB_GS(%edx)
125ccd9b49fSElliott Mitchell	/* Test if debug registers should be saved. */
126ccd9b49fSElliott Mitchell	testl	$PCB_DBREGS,PCB_FLAGS(%edx)
127ccd9b49fSElliott Mitchell	jz      1f                              /* no, skip over */
128ccd9b49fSElliott Mitchell	movl    %dr7,%eax                       /* yes, do the save */
129ccd9b49fSElliott Mitchell	movl    %eax,PCB_DR7(%edx)
130ccd9b49fSElliott Mitchell	andl    $0x0000fc00, %eax               /* disable all watchpoints */
131ccd9b49fSElliott Mitchell	movl    %eax,%dr7
132ccd9b49fSElliott Mitchell	movl    %dr6,%eax
133ccd9b49fSElliott Mitchell	movl    %eax,PCB_DR6(%edx)
134ccd9b49fSElliott Mitchell	movl    %dr3,%eax
135ccd9b49fSElliott Mitchell	movl    %eax,PCB_DR3(%edx)
136ccd9b49fSElliott Mitchell	movl    %dr2,%eax
137ccd9b49fSElliott Mitchell	movl    %eax,PCB_DR2(%edx)
138ccd9b49fSElliott Mitchell	movl    %dr1,%eax
139ccd9b49fSElliott Mitchell	movl    %eax,PCB_DR1(%edx)
140ccd9b49fSElliott Mitchell	movl    %dr0,%eax
141ccd9b49fSElliott Mitchell	movl    %eax,PCB_DR0(%edx)
142ccd9b49fSElliott Mitchell1:
143ccd9b49fSElliott Mitchell
144ccd9b49fSElliott Mitchell	/* have we used fp, and need a save? */
145ccd9b49fSElliott Mitchell	cmpl	%ecx,PCPU(FPCURTHREAD)
146ccd9b49fSElliott Mitchell	jne	1f
147ccd9b49fSElliott Mitchell	pushl	PCB_SAVEFPU(%edx)		/* h/w bugs make saving complicated */
148ccd9b49fSElliott Mitchell	call	npxsave				/* do it in a big C function */
149ccd9b49fSElliott Mitchell	popl	%eax
150ccd9b49fSElliott Mitchell1:
151ccd9b49fSElliott Mitchell
152ccd9b49fSElliott Mitchell	/* Save is done.  Now fire up new thread. */
153ccd9b49fSElliott Mitchell	movl	4(%esp),%edi
154ccd9b49fSElliott Mitchell	movl	8(%esp),%ecx			/* New thread */
155ccd9b49fSElliott Mitchell	movl	12(%esp),%esi			/* New lock */
156ccd9b49fSElliott Mitchell#ifdef INVARIANTS
157ccd9b49fSElliott Mitchell	testl	%ecx,%ecx			/* no thread? */
158ccd9b49fSElliott Mitchell	jz	badsw3				/* no, panic */
159ccd9b49fSElliott Mitchell#endif
160ccd9b49fSElliott Mitchell	movl	TD_PCB(%ecx),%edx
161ccd9b49fSElliott Mitchell
162ccd9b49fSElliott Mitchell	/* Switchout td_lock */
163ccd9b49fSElliott Mitchell	movl	%esi,%eax
164ccd9b49fSElliott Mitchell	movl	PCPU(CPUID),%esi
165ccd9b49fSElliott Mitchell	SETOP	%eax,TD_LOCK(%edi)
166ccd9b49fSElliott Mitchell
167ccd9b49fSElliott Mitchell	/* Release bit from old pmap->pm_active */
168ccd9b49fSElliott Mitchell	movl	PCPU(CURPMAP), %ebx
169ccd9b49fSElliott Mitchell#ifdef SMP
170ccd9b49fSElliott Mitchell	lock
171ccd9b49fSElliott Mitchell#endif
172ccd9b49fSElliott Mitchell	btrl	%esi, PM_ACTIVE(%ebx)		/* clear old */
173ccd9b49fSElliott Mitchell
174ccd9b49fSElliott Mitchell	/* Set bit in new pmap->pm_active */
175ccd9b49fSElliott Mitchell	movl	TD_PROC(%ecx),%eax		/* newproc */
176ccd9b49fSElliott Mitchell	movl	P_VMSPACE(%eax), %ebx
177ccd9b49fSElliott Mitchell	addl	$VM_PMAP, %ebx
178ccd9b49fSElliott Mitchell	movl	%ebx, PCPU(CURPMAP)
179ccd9b49fSElliott Mitchell#ifdef SMP
180ccd9b49fSElliott Mitchell	lock
181ccd9b49fSElliott Mitchell#endif
182ccd9b49fSElliott Mitchell	btsl	%esi, PM_ACTIVE(%ebx)		/* set new */
183ccd9b49fSElliott Mitchellsw1:
184ccd9b49fSElliott Mitchell	BLOCK_SPIN(%ecx)
185ccd9b49fSElliott Mitchell	/*
186ccd9b49fSElliott Mitchell	 * At this point, we have managed thread locks and are ready
187ccd9b49fSElliott Mitchell	 * to load up the rest of the next context.
188ccd9b49fSElliott Mitchell	 */
189ccd9b49fSElliott Mitchell
190ccd9b49fSElliott Mitchell	/* Load a pointer to the thread kernel stack into PCPU. */
191ccd9b49fSElliott Mitchell	leal	-VM86_STACK_SPACE(%edx), %eax	/* leave space for vm86 */
192ccd9b49fSElliott Mitchell	movl	%eax, PCPU(KESP0)
193ccd9b49fSElliott Mitchell
194ccd9b49fSElliott Mitchell	cmpl	$0, PCB_EXT(%edx)		/* has pcb extension? */
195ccd9b49fSElliott Mitchell	je	1f				/* If not, use the default */
196ccd9b49fSElliott Mitchell	movl	$1, PCPU(PRIVATE_TSS) 		/* mark use of private tss */
197ccd9b49fSElliott Mitchell	movl	PCB_EXT(%edx), %edi		/* new tss descriptor */
198ccd9b49fSElliott Mitchell	movl	PCPU(TRAMPSTK), %ebx
199ccd9b49fSElliott Mitchell	movl	%ebx, PCB_EXT_TSS+TSS_ESP0(%edi)
200ccd9b49fSElliott Mitchell	jmp	2f				/* Load it up */
201ccd9b49fSElliott Mitchell
202ccd9b49fSElliott Mitchell1:	/*
203ccd9b49fSElliott Mitchell	 * Use the common default TSS instead of our own.
204ccd9b49fSElliott Mitchell	 * Stack pointer in the common TSS points to the trampoline stack
205ccd9b49fSElliott Mitchell	 * already and should be not changed.
206ccd9b49fSElliott Mitchell	 *
207ccd9b49fSElliott Mitchell	 * Test this CPU's flag to see if this CPU was using a private TSS.
208ccd9b49fSElliott Mitchell	 */
209ccd9b49fSElliott Mitchell	cmpl	$0, PCPU(PRIVATE_TSS)		/* Already using the common? */
210ccd9b49fSElliott Mitchell	je	3f				/* if so, skip reloading */
211ccd9b49fSElliott Mitchell	movl	$0, PCPU(PRIVATE_TSS)
212ccd9b49fSElliott Mitchell	PCPU_ADDR(COMMON_TSSD, %edi)
213ccd9b49fSElliott Mitchell2:
214ccd9b49fSElliott Mitchell	/* Move correct tss descriptor into GDT slot, then reload tr. */
215ccd9b49fSElliott Mitchell	movl	PCPU(TSS_GDT), %ebx		/* entry in GDT */
216ccd9b49fSElliott Mitchell	movl	0(%edi), %eax
217ccd9b49fSElliott Mitchell	movl	4(%edi), %esi
218ccd9b49fSElliott Mitchell	movl	%eax, 0(%ebx)
219ccd9b49fSElliott Mitchell	movl	%esi, 4(%ebx)
220ccd9b49fSElliott Mitchell	movl	$GPROC0_SEL*8, %esi		/* GSEL(GPROC0_SEL, SEL_KPL) */
221ccd9b49fSElliott Mitchell	ltr	%si
222ccd9b49fSElliott Mitchell3:
223ccd9b49fSElliott Mitchell
224ccd9b49fSElliott Mitchell	/* Copy the %fs and %gs selectors into this pcpu gdt */
225ccd9b49fSElliott Mitchell	leal	PCB_FSD(%edx), %esi
226ccd9b49fSElliott Mitchell	movl	PCPU(FSGS_GDT), %edi
227ccd9b49fSElliott Mitchell	movl	0(%esi), %eax		/* %fs selector */
228ccd9b49fSElliott Mitchell	movl	4(%esi), %ebx
229ccd9b49fSElliott Mitchell	movl	%eax, 0(%edi)
230ccd9b49fSElliott Mitchell	movl	%ebx, 4(%edi)
231ccd9b49fSElliott Mitchell	movl	8(%esi), %eax		/* %gs selector, comes straight after */
232ccd9b49fSElliott Mitchell	movl	12(%esi), %ebx
233ccd9b49fSElliott Mitchell	movl	%eax, 8(%edi)
234ccd9b49fSElliott Mitchell	movl	%ebx, 12(%edi)
235ccd9b49fSElliott Mitchell
236ccd9b49fSElliott Mitchell	/* Restore context. */
237ccd9b49fSElliott Mitchell	movl	PCB_EBX(%edx),%ebx
238ccd9b49fSElliott Mitchell	movl	PCB_ESP(%edx),%esp
239ccd9b49fSElliott Mitchell	movl	PCB_EBP(%edx),%ebp
240ccd9b49fSElliott Mitchell	movl	PCB_ESI(%edx),%esi
241ccd9b49fSElliott Mitchell	movl	PCB_EDI(%edx),%edi
242ccd9b49fSElliott Mitchell	movl	PCB_EIP(%edx),%eax
243ccd9b49fSElliott Mitchell	movl	%eax,(%esp)
244ccd9b49fSElliott Mitchell
245ccd9b49fSElliott Mitchell	movl	%edx, PCPU(CURPCB)
246ccd9b49fSElliott Mitchell	movl	%ecx, PCPU(CURTHREAD)		/* into next thread */
247ccd9b49fSElliott Mitchell
248ccd9b49fSElliott Mitchell	/*
249ccd9b49fSElliott Mitchell	 * Determine the LDT to use and load it if is the default one and
250ccd9b49fSElliott Mitchell	 * that is not the current one.
251ccd9b49fSElliott Mitchell	 */
252ccd9b49fSElliott Mitchell	movl	TD_PROC(%ecx),%eax
253ccd9b49fSElliott Mitchell	cmpl    $0,P_MD+MD_LDT(%eax)
254ccd9b49fSElliott Mitchell	jnz	1f
255ccd9b49fSElliott Mitchell	movl	_default_ldt,%eax
256ccd9b49fSElliott Mitchell	cmpl	PCPU(CURRENTLDT),%eax
257ccd9b49fSElliott Mitchell	je	2f
258ccd9b49fSElliott Mitchell	lldt	_default_ldt
259ccd9b49fSElliott Mitchell	movl	%eax,PCPU(CURRENTLDT)
260ccd9b49fSElliott Mitchell	jmp	2f
261ccd9b49fSElliott Mitchell1:
262ccd9b49fSElliott Mitchell	/* Load the LDT when it is not the default one. */
263ccd9b49fSElliott Mitchell	pushl	%edx				/* Preserve pointer to pcb. */
264ccd9b49fSElliott Mitchell	addl	$P_MD,%eax			/* Pointer to mdproc is arg. */
265ccd9b49fSElliott Mitchell	pushl	%eax
266ccd9b49fSElliott Mitchell	/*
267ccd9b49fSElliott Mitchell	 * Holding dt_lock prevents context switches, so dt_lock cannot
268ccd9b49fSElliott Mitchell	 * be held now and set_user_ldt() will not deadlock acquiring it.
269ccd9b49fSElliott Mitchell	 */
270ccd9b49fSElliott Mitchell	call	set_user_ldt
271ccd9b49fSElliott Mitchell	addl	$4,%esp
272ccd9b49fSElliott Mitchell	popl	%edx
273ccd9b49fSElliott Mitchell2:
274ccd9b49fSElliott Mitchell
275ccd9b49fSElliott Mitchell	/* This must be done after loading the user LDT. */
276ccd9b49fSElliott Mitchell	.globl	cpu_switch_load_gs
277ccd9b49fSElliott Mitchellcpu_switch_load_gs:
278ccd9b49fSElliott Mitchell	mov	PCB_GS(%edx),%gs
279ccd9b49fSElliott Mitchell
280ccd9b49fSElliott Mitchell	pushl	%edx
281ccd9b49fSElliott Mitchell	pushl	PCPU(CURTHREAD)
282ccd9b49fSElliott Mitchell	call	npxswitch
283ccd9b49fSElliott Mitchell	popl	%edx
284ccd9b49fSElliott Mitchell	popl	%edx
285ccd9b49fSElliott Mitchell
286ccd9b49fSElliott Mitchell	/* Test if debug registers should be restored. */
287ccd9b49fSElliott Mitchell	testl	$PCB_DBREGS,PCB_FLAGS(%edx)
288ccd9b49fSElliott Mitchell	jz      1f
289ccd9b49fSElliott Mitchell
290ccd9b49fSElliott Mitchell	/*
291ccd9b49fSElliott Mitchell	 * Restore debug registers.  The special code for dr7 is to
292ccd9b49fSElliott Mitchell	 * preserve the current values of its reserved bits.
293ccd9b49fSElliott Mitchell	 */
294ccd9b49fSElliott Mitchell	movl    PCB_DR6(%edx),%eax
295ccd9b49fSElliott Mitchell	movl    %eax,%dr6
296ccd9b49fSElliott Mitchell	movl    PCB_DR3(%edx),%eax
297ccd9b49fSElliott Mitchell	movl    %eax,%dr3
298ccd9b49fSElliott Mitchell	movl    PCB_DR2(%edx),%eax
299ccd9b49fSElliott Mitchell	movl    %eax,%dr2
300ccd9b49fSElliott Mitchell	movl    PCB_DR1(%edx),%eax
301ccd9b49fSElliott Mitchell	movl    %eax,%dr1
302ccd9b49fSElliott Mitchell	movl    PCB_DR0(%edx),%eax
303ccd9b49fSElliott Mitchell	movl    %eax,%dr0
304ccd9b49fSElliott Mitchell	movl	%dr7,%eax
305ccd9b49fSElliott Mitchell	andl    $0x0000fc00,%eax
306ccd9b49fSElliott Mitchell	movl    PCB_DR7(%edx),%ecx
307ccd9b49fSElliott Mitchell	andl	$~0x0000fc00,%ecx
308ccd9b49fSElliott Mitchell	orl     %ecx,%eax
309ccd9b49fSElliott Mitchell	movl    %eax,%dr7
310ccd9b49fSElliott Mitchell1:
311ccd9b49fSElliott Mitchell	ret
312ccd9b49fSElliott Mitchell
313ccd9b49fSElliott Mitchell#ifdef INVARIANTS
314ccd9b49fSElliott Mitchellbadsw1:
315ccd9b49fSElliott Mitchell	pushal
316ccd9b49fSElliott Mitchell	pushl	$sw0_1
317ccd9b49fSElliott Mitchell	call	panic
318ccd9b49fSElliott Mitchellsw0_1:	.asciz	"cpu_throw: no newthread supplied"
319ccd9b49fSElliott Mitchell
320ccd9b49fSElliott Mitchellbadsw2:
321ccd9b49fSElliott Mitchell	pushal
322ccd9b49fSElliott Mitchell	pushl	$sw0_2
323ccd9b49fSElliott Mitchell	call	panic
324ccd9b49fSElliott Mitchellsw0_2:	.asciz	"cpu_switch: no curthread supplied"
325ccd9b49fSElliott Mitchell
326ccd9b49fSElliott Mitchellbadsw3:
327ccd9b49fSElliott Mitchell	pushal
328ccd9b49fSElliott Mitchell	pushl	$sw0_3
329ccd9b49fSElliott Mitchell	call	panic
330ccd9b49fSElliott Mitchellsw0_3:	.asciz	"cpu_switch: no newthread supplied"
331ccd9b49fSElliott Mitchell#endif
332ccd9b49fSElliott MitchellEND(cpu_switch)
333ccd9b49fSElliott Mitchell
334ccd9b49fSElliott Mitchell/*
335ccd9b49fSElliott Mitchell * savectx(pcb)
336ccd9b49fSElliott Mitchell * Update pcb, saving current processor state.
337ccd9b49fSElliott Mitchell */
338ccd9b49fSElliott MitchellENTRY(savectx)
339ccd9b49fSElliott Mitchell	/* Fetch PCB. */
340ccd9b49fSElliott Mitchell	movl	4(%esp),%ecx
341ccd9b49fSElliott Mitchell
342ccd9b49fSElliott Mitchell	/* Save caller's return address.  Child won't execute this routine. */
343ccd9b49fSElliott Mitchell	movl	(%esp),%eax
344ccd9b49fSElliott Mitchell	movl	%eax,PCB_EIP(%ecx)
345ccd9b49fSElliott Mitchell
346ccd9b49fSElliott Mitchell	movl	%cr3,%eax
347ccd9b49fSElliott Mitchell	movl	%eax,PCB_CR3(%ecx)
348ccd9b49fSElliott Mitchell
349ccd9b49fSElliott Mitchell	movl	%ebx,PCB_EBX(%ecx)
350ccd9b49fSElliott Mitchell	movl	%esp,PCB_ESP(%ecx)
351ccd9b49fSElliott Mitchell	movl	%ebp,PCB_EBP(%ecx)
352ccd9b49fSElliott Mitchell	movl	%esi,PCB_ESI(%ecx)
353ccd9b49fSElliott Mitchell	movl	%edi,PCB_EDI(%ecx)
354ccd9b49fSElliott Mitchell	mov	%gs,PCB_GS(%ecx)
355ccd9b49fSElliott Mitchell
356ccd9b49fSElliott Mitchell	movl	%cr0,%eax
357ccd9b49fSElliott Mitchell	movl	%eax,PCB_CR0(%ecx)
358ccd9b49fSElliott Mitchell	movl	%cr2,%eax
359ccd9b49fSElliott Mitchell	movl	%eax,PCB_CR2(%ecx)
360ccd9b49fSElliott Mitchell	movl	%cr4,%eax
361ccd9b49fSElliott Mitchell	movl	%eax,PCB_CR4(%ecx)
362ccd9b49fSElliott Mitchell
363ccd9b49fSElliott Mitchell	movl	%dr0,%eax
364ccd9b49fSElliott Mitchell	movl	%eax,PCB_DR0(%ecx)
365ccd9b49fSElliott Mitchell	movl	%dr1,%eax
366ccd9b49fSElliott Mitchell	movl	%eax,PCB_DR1(%ecx)
367ccd9b49fSElliott Mitchell	movl	%dr2,%eax
368ccd9b49fSElliott Mitchell	movl	%eax,PCB_DR2(%ecx)
369ccd9b49fSElliott Mitchell	movl	%dr3,%eax
370ccd9b49fSElliott Mitchell	movl	%eax,PCB_DR3(%ecx)
371ccd9b49fSElliott Mitchell	movl	%dr6,%eax
372ccd9b49fSElliott Mitchell	movl	%eax,PCB_DR6(%ecx)
373ccd9b49fSElliott Mitchell	movl	%dr7,%eax
374ccd9b49fSElliott Mitchell	movl	%eax,PCB_DR7(%ecx)
375ccd9b49fSElliott Mitchell
376ccd9b49fSElliott Mitchell	mov	%ds,PCB_DS(%ecx)
377ccd9b49fSElliott Mitchell	mov	%es,PCB_ES(%ecx)
378ccd9b49fSElliott Mitchell	mov	%fs,PCB_FS(%ecx)
379ccd9b49fSElliott Mitchell	mov	%ss,PCB_SS(%ecx)
380ccd9b49fSElliott Mitchell
381ccd9b49fSElliott Mitchell	sgdt	PCB_GDT(%ecx)
382ccd9b49fSElliott Mitchell	sidt	PCB_IDT(%ecx)
383ccd9b49fSElliott Mitchell	sldt	PCB_LDT(%ecx)
384ccd9b49fSElliott Mitchell	str	PCB_TR(%ecx)
385ccd9b49fSElliott Mitchell
386ccd9b49fSElliott Mitchell	movl	$1,%eax
387ccd9b49fSElliott Mitchell	ret
388ccd9b49fSElliott MitchellEND(savectx)
389ccd9b49fSElliott Mitchell
390ccd9b49fSElliott Mitchell/*
391ccd9b49fSElliott Mitchell * resumectx(pcb) __fastcall
392ccd9b49fSElliott Mitchell * Resuming processor state from pcb.
393ccd9b49fSElliott Mitchell */
394ccd9b49fSElliott MitchellENTRY(resumectx)
395ccd9b49fSElliott Mitchell	/* Restore GDT. */
396ccd9b49fSElliott Mitchell	lgdt	PCB_GDT(%ecx)
397ccd9b49fSElliott Mitchell
398ccd9b49fSElliott Mitchell	/* Restore segment registers */
399ccd9b49fSElliott Mitchell	movzwl	PCB_DS(%ecx),%eax
400ccd9b49fSElliott Mitchell	mov	%ax,%ds
401ccd9b49fSElliott Mitchell	movzwl	PCB_ES(%ecx),%eax
402ccd9b49fSElliott Mitchell	mov	%ax,%es
403ccd9b49fSElliott Mitchell	movzwl	PCB_FS(%ecx),%eax
404ccd9b49fSElliott Mitchell	mov	%ax,%fs
405ccd9b49fSElliott Mitchell	movzwl	PCB_GS(%ecx),%eax
406ccd9b49fSElliott Mitchell	movw	%ax,%gs
407ccd9b49fSElliott Mitchell	movzwl	PCB_SS(%ecx),%eax
408ccd9b49fSElliott Mitchell	mov	%ax,%ss
409ccd9b49fSElliott Mitchell
410ccd9b49fSElliott Mitchell	/* Restore CR2, CR4, CR3 and CR0 */
411ccd9b49fSElliott Mitchell	movl	PCB_CR2(%ecx),%eax
412ccd9b49fSElliott Mitchell	movl	%eax,%cr2
413ccd9b49fSElliott Mitchell	movl	PCB_CR4(%ecx),%eax
414ccd9b49fSElliott Mitchell	movl	%eax,%cr4
415ccd9b49fSElliott Mitchell	movl	PCB_CR3(%ecx),%eax
416ccd9b49fSElliott Mitchell	movl	%eax,%cr3
417ccd9b49fSElliott Mitchell	movl	PCB_CR0(%ecx),%eax
418ccd9b49fSElliott Mitchell	movl	%eax,%cr0
419ccd9b49fSElliott Mitchell	jmp	1f
420ccd9b49fSElliott Mitchell1:
421ccd9b49fSElliott Mitchell
422ccd9b49fSElliott Mitchell	/* Restore descriptor tables */
423ccd9b49fSElliott Mitchell	lidt	PCB_IDT(%ecx)
424ccd9b49fSElliott Mitchell	lldt	PCB_LDT(%ecx)
425ccd9b49fSElliott Mitchell
426ccd9b49fSElliott Mitchell#define SDT_SYS386TSS	9
427ccd9b49fSElliott Mitchell#define SDT_SYS386BSY	11
428ccd9b49fSElliott Mitchell	/* Clear "task busy" bit and reload TR */
429ccd9b49fSElliott Mitchell	movl	PCPU(TSS_GDT),%eax
430ccd9b49fSElliott Mitchell	andb	$(~SDT_SYS386BSY | SDT_SYS386TSS),5(%eax)
431ccd9b49fSElliott Mitchell	movzwl	PCB_TR(%ecx),%eax
432ccd9b49fSElliott Mitchell	ltr	%ax
433ccd9b49fSElliott Mitchell#undef SDT_SYS386TSS
434ccd9b49fSElliott Mitchell#undef SDT_SYS386BSY
435ccd9b49fSElliott Mitchell
436ccd9b49fSElliott Mitchell	/* Restore debug registers */
437ccd9b49fSElliott Mitchell	movl	PCB_DR0(%ecx),%eax
438ccd9b49fSElliott Mitchell	movl	%eax,%dr0
439ccd9b49fSElliott Mitchell	movl	PCB_DR1(%ecx),%eax
440ccd9b49fSElliott Mitchell	movl	%eax,%dr1
441ccd9b49fSElliott Mitchell	movl	PCB_DR2(%ecx),%eax
442ccd9b49fSElliott Mitchell	movl	%eax,%dr2
443ccd9b49fSElliott Mitchell	movl	PCB_DR3(%ecx),%eax
444ccd9b49fSElliott Mitchell	movl	%eax,%dr3
445ccd9b49fSElliott Mitchell	movl	PCB_DR6(%ecx),%eax
446ccd9b49fSElliott Mitchell	movl	%eax,%dr6
447ccd9b49fSElliott Mitchell	movl	PCB_DR7(%ecx),%eax
448ccd9b49fSElliott Mitchell	movl	%eax,%dr7
449ccd9b49fSElliott Mitchell
450ccd9b49fSElliott Mitchell	/* Restore other registers */
451ccd9b49fSElliott Mitchell	movl	PCB_EDI(%ecx),%edi
452ccd9b49fSElliott Mitchell	movl	PCB_ESI(%ecx),%esi
453ccd9b49fSElliott Mitchell	movl	PCB_EBP(%ecx),%ebp
454ccd9b49fSElliott Mitchell	movl	PCB_ESP(%ecx),%esp
455ccd9b49fSElliott Mitchell	movl	PCB_EBX(%ecx),%ebx
456ccd9b49fSElliott Mitchell
457ccd9b49fSElliott Mitchell	/* reload code selector by turning return into intersegmental return */
458ccd9b49fSElliott Mitchell	pushl	PCB_EIP(%ecx)
459ccd9b49fSElliott Mitchell	movl	$KCSEL,4(%esp)
460ccd9b49fSElliott Mitchell	xorl	%eax,%eax
461ccd9b49fSElliott Mitchell	lret
462ccd9b49fSElliott MitchellEND(resumectx)
463