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