2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <machine/asmacros.h>
41 /*****************************************************************************/
43 /*****************************************************************************/
50 * This is the second half of cpu_switch(). It is used when the current
51 * thread is either a dummy or slated to die, and we no longer care
52 * about its state. This is only a slight optimization and is probably
53 * not worth it anymore. Note that we need to clear the pm_active bits so
54 * we do need the old proc if it still exists.
60 movl PCPU(CPUID), %esi
61 movl 4(%esp),%ecx /* Old thread */
62 testl %ecx,%ecx /* no thread? */
64 /* release bit from old pm_active */
65 movl PCPU(CURPMAP), %ebx
69 btrl %esi, PM_ACTIVE(%ebx) /* clear old */
71 movl 8(%esp),%ecx /* New thread */
72 movl TD_PCB(%ecx),%edx
73 movl PCB_CR3(%edx),%eax
74 movl %eax,%cr3 /* new address space */
75 /* set bit in new pm_active */
76 movl TD_PROC(%ecx),%eax
77 movl P_VMSPACE(%eax), %ebx
79 movl %ebx, PCPU(CURPMAP)
83 btsl %esi, PM_ACTIVE(%ebx) /* set new */
87 * cpu_switch(old, new)
89 * Save the current thread state, then select the next thread to run
97 /* Switch to new thread. First, save context. */
101 testl %ecx,%ecx /* no thread? */
102 jz badsw2 /* no, panic */
105 movl TD_PCB(%ecx),%edx
107 movl (%esp),%eax /* Hardware registers */
108 movl %eax,PCB_EIP(%edx)
109 movl %ebx,PCB_EBX(%edx)
110 movl %esp,PCB_ESP(%edx)
111 movl %ebp,PCB_EBP(%edx)
112 movl %esi,PCB_ESI(%edx)
113 movl %edi,PCB_EDI(%edx)
114 movl %gs,PCB_GS(%edx)
117 /* Test if debug registers should be saved. */
118 testl $PCB_DBREGS,PCB_FLAGS(%edx)
119 jz 1f /* no, skip over */
120 movl %dr7,%eax /* yes, do the save */
121 movl %eax,PCB_DR7(%edx)
122 andl $0x0000fc00, %eax /* disable all watchpoints */
125 movl %eax,PCB_DR6(%edx)
127 movl %eax,PCB_DR3(%edx)
129 movl %eax,PCB_DR2(%edx)
131 movl %eax,PCB_DR1(%edx)
133 movl %eax,PCB_DR0(%edx)
137 /* have we used fp, and need a save? */
138 cmpl %ecx,PCPU(FPCURTHREAD)
140 addl $PCB_SAVEFPU,%edx /* h/w bugs make saving complicated */
142 call npxsave /* do it in a big C function */
147 /* Save is done. Now fire up new thread. Leave old vmspace. */
149 movl 8(%esp),%ecx /* New thread */
151 testl %ecx,%ecx /* no thread? */
152 jz badsw3 /* no, panic */
154 movl TD_PCB(%ecx),%edx
155 movl PCPU(CPUID), %esi
157 /* switch address space */
158 movl PCB_CR3(%edx),%eax
160 cmpl %eax,IdlePDPT /* Kernel address space? */
162 cmpl %eax,IdlePTD /* Kernel address space? */
165 movl %cr3,%ebx /* The same address space? */
168 movl %eax,%cr3 /* new address space */
170 /* Release bit from old pmap->pm_active */
171 movl PCPU(CURPMAP), %ebx
175 btrl %esi, PM_ACTIVE(%ebx) /* clear old */
177 /* Set bit in new pmap->pm_active */
178 movl TD_PROC(%ecx),%eax /* newproc */
179 movl P_VMSPACE(%eax), %ebx
181 movl %ebx, PCPU(CURPMAP)
185 btsl %esi, PM_ACTIVE(%ebx) /* set new */
189 * At this point, we've switched address spaces and are ready
190 * to load up the rest of the next context.
192 cmpl $0, PCB_EXT(%edx) /* has pcb extension? */
193 je 1f /* If not, use the default */
194 movl $1, PCPU(PRIVATE_TSS) /* mark use of private tss */
195 movl PCB_EXT(%edx), %edi /* new tss descriptor */
196 jmp 2f /* Load it up */
199 * Use the common default TSS instead of our own.
200 * Set our stack pointer into the TSS, it's set to just
201 * below the PCB. In C, common_tss.tss_esp0 = &pcb - 16;
203 leal -16(%edx), %ebx /* leave space for vm86 */
204 movl %ebx, PCPU(COMMON_TSS) + TSS_ESP0
207 * Test this CPU's bit in the bitmap to see if this
208 * CPU was using a private TSS.
210 cmpl $0, PCPU(PRIVATE_TSS) /* Already using the common? */
211 je 3f /* if so, skip reloading */
212 movl $0, PCPU(PRIVATE_TSS)
213 PCPU_ADDR(COMMON_TSSD, %edi)
215 /* Move correct tss descriptor into GDT slot, then reload tr. */
216 movl PCPU(TSS_GDT), %ebx /* entry in GDT */
221 movl $GPROC0_SEL*8, %esi /* GSEL(GPROC0_SEL, SEL_KPL) */
225 /* Copy the %fs and %gs selectors into this pcpu gdt */
226 leal PCB_FSD(%edx), %esi
227 movl PCPU(FSGS_GDT), %edi
228 movl 0(%esi), %eax /* %fs selector */
232 movl 8(%esi), %eax /* %gs selector, comes straight after */
237 /* Restore context. */
238 movl PCB_EBX(%edx),%ebx
239 movl PCB_ESP(%edx),%esp
240 movl PCB_EBP(%edx),%ebp
241 movl PCB_ESI(%edx),%esi
242 movl PCB_EDI(%edx),%edi
243 movl PCB_EIP(%edx),%eax
248 movl %edx, PCPU(CURPCB)
249 movl %ecx, PCPU(CURTHREAD) /* into next thread */
252 * Determine the LDT to use and load it if is the default one and
253 * that is not the current one.
255 movl TD_PROC(%ecx),%eax
256 cmpl $0,P_MD+MD_LDT(%eax)
258 movl _default_ldt,%eax
259 cmpl PCPU(CURRENTLDT),%eax
262 movl %eax,PCPU(CURRENTLDT)
265 /* Load the LDT when it is not the default one. */
266 pushl %edx /* Preserve pointer to pcb. */
267 addl $P_MD,%eax /* Pointer to mdproc is arg. */
274 /* This must be done after loading the user LDT. */
275 .globl cpu_switch_load_gs
277 movl PCB_GS(%edx),%gs
279 /* Test if debug registers should be restored. */
280 testl $PCB_DBREGS,PCB_FLAGS(%edx)
284 * Restore debug registers. The special code for dr7 is to
285 * preserve the current values of its reserved bits.
287 movl PCB_DR6(%edx),%eax
289 movl PCB_DR3(%edx),%eax
291 movl PCB_DR2(%edx),%eax
293 movl PCB_DR1(%edx),%eax
295 movl PCB_DR0(%edx),%eax
298 andl $0x0000fc00,%eax
299 movl PCB_DR7(%edx),%ecx
300 andl $~0x0000fc00,%ecx
311 sw0_1: .asciz "cpu_throw: no newthread supplied"
317 sw0_2: .asciz "cpu_switch: no curthread supplied"
323 sw0_3: .asciz "cpu_switch: no newthread supplied"
328 * Update pcb, saving current processor state.
334 /* Save caller's return address. Child won't execute this routine. */
336 movl %eax,PCB_EIP(%ecx)
339 movl %eax,PCB_CR3(%ecx)
341 movl %ebx,PCB_EBX(%ecx)
342 movl %esp,PCB_ESP(%ecx)
343 movl %ebp,PCB_EBP(%ecx)
344 movl %esi,PCB_ESI(%ecx)
345 movl %edi,PCB_EDI(%ecx)
346 movl %gs,PCB_GS(%ecx)
352 * If fpcurthread == NULL, then the npx h/w state is irrelevant and the
353 * state had better already be in the pcb. This is true for forks
354 * but not for dumps (the old book-keeping with FP flags in the pcb
355 * always lost for dumps because the dump pcb has 0 flags).
357 * If fpcurthread != NULL, then we have to save the npx h/w state to
358 * fpcurthread's pcb and copy it to the requested pcb, or save to the
359 * requested pcb and reload. Copying is easier because we would
360 * have to handle h/w bugs for reloading. We used to lose the
361 * parent's npx state for forks by forgetting to reload.
365 movl PCPU(FPCURTHREAD),%eax
370 movl TD_PCB(%eax),%eax
371 leal PCB_SAVEFPU(%eax),%eax
379 pushl $PCB_SAVEFPU_SIZE
380 leal PCB_SAVEFPU(%ecx),%ecx