swtch.s revision 271999
1169689Skan/*- 2169689Skan * Copyright (c) 1990 The Regents of the University of California. 3169689Skan * All rights reserved. 4169689Skan * 5169689Skan * This code is derived from software contributed to Berkeley by 6169689Skan * William Jolitz. 7169689Skan * 8169689Skan * Redistribution and use in source and binary forms, with or without 9169689Skan * modification, are permitted provided that the following conditions 10169689Skan * are met: 11169689Skan * 1. Redistributions of source code must retain the above copyright 12169689Skan * notice, this list of conditions and the following disclaimer. 13169689Skan * 2. Redistributions in binary form must reproduce the above copyright 14169689Skan * notice, this list of conditions and the following disclaimer in the 15169689Skan * documentation and/or other materials provided with the distribution. 16169689Skan * 4. Neither the name of the University nor the names of its contributors 17169689Skan * may be used to endorse or promote products derived from this software 18169689Skan * without specific prior written permission. 19169689Skan * 20169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23169689Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30169689Skan * SUCH DAMAGE. 31169689Skan * 32169689Skan * $FreeBSD: stable/10/sys/i386/i386/swtch.s 271999 2014-09-22 20:34:36Z jhb $ 33169689Skan */ 34169689Skan 35169689Skan#include "opt_npx.h" 36169689Skan#include "opt_sched.h" 37169689Skan 38169689Skan#include <machine/asmacros.h> 39169689Skan 40169689Skan#include "assym.s" 41169689Skan 42169689Skan#if defined(SMP) && defined(SCHED_ULE) 43169689Skan#define SETOP xchgl 44169689Skan#define BLOCK_SPIN(reg) \ 45169689Skan movl $blocked_lock,%eax ; \ 46169689Skan 100: ; \ 47169689Skan lock ; \ 48169689Skan cmpxchgl %eax,TD_LOCK(reg) ; \ 49169689Skan jne 101f ; \ 50169689Skan pause ; \ 51169689Skan jmp 100b ; \ 52169689Skan 101: 53169689Skan#else 54169689Skan#define SETOP movl 55169689Skan#define BLOCK_SPIN(reg) 56169689Skan#endif 57169689Skan 58169689Skan/*****************************************************************************/ 59169689Skan/* Scheduling */ 60169689Skan/*****************************************************************************/ 61169689Skan 62169689Skan .text 63169689Skan 64169689Skan/* 65169689Skan * cpu_throw() 66169689Skan * 67169689Skan * This is the second half of cpu_switch(). It is used when the current 68169689Skan * thread is either a dummy or slated to die, and we no longer care 69169689Skan * about its state. This is only a slight optimization and is probably 70169689Skan * not worth it anymore. Note that we need to clear the pm_active bits so 71169689Skan * we do need the old proc if it still exists. 72169689Skan * 0(%esp) = ret 73169689Skan * 4(%esp) = oldtd 74169689Skan * 8(%esp) = newtd 75169689Skan */ 76169689SkanENTRY(cpu_throw) 77169689Skan movl PCPU(CPUID), %esi 78169689Skan movl 4(%esp),%ecx /* Old thread */ 79169689Skan testl %ecx,%ecx /* no thread? */ 80169689Skan jz 1f 81169689Skan /* release bit from old pm_active */ 82169689Skan movl PCPU(CURPMAP), %ebx 83169689Skan#ifdef SMP 84169689Skan lock 85169689Skan#endif 86169689Skan btrl %esi, PM_ACTIVE(%ebx) /* clear old */ 87169689Skan1: 88169689Skan movl 8(%esp),%ecx /* New thread */ 89169689Skan movl TD_PCB(%ecx),%edx 90169689Skan movl PCB_CR3(%edx),%eax 91169689Skan LOAD_CR3(%eax) 92169689Skan /* set bit in new pm_active */ 93169689Skan movl TD_PROC(%ecx),%eax 94169689Skan movl P_VMSPACE(%eax), %ebx 95169689Skan addl $VM_PMAP, %ebx 96169689Skan movl %ebx, PCPU(CURPMAP) 97169689Skan#ifdef SMP 98169689Skan lock 99169689Skan#endif 100169689Skan btsl %esi, PM_ACTIVE(%ebx) /* set new */ 101169689Skan jmp sw1 102169689SkanEND(cpu_throw) 103169689Skan 104169689Skan/* 105169689Skan * cpu_switch(old, new) 106169689Skan * 107169689Skan * Save the current thread state, then select the next thread to run 108169689Skan * and load its state. 109169689Skan * 0(%esp) = ret 110169689Skan * 4(%esp) = oldtd 111169689Skan * 8(%esp) = newtd 112169689Skan * 12(%esp) = newlock 113169689Skan */ 114169689SkanENTRY(cpu_switch) 115169689Skan 116169689Skan /* Switch to new thread. First, save context. */ 117169689Skan movl 4(%esp),%ecx 118169689Skan 119169689Skan#ifdef INVARIANTS 120169689Skan testl %ecx,%ecx /* no thread? */ 121169689Skan jz badsw2 /* no, panic */ 122169689Skan#endif 123169689Skan 124169689Skan movl TD_PCB(%ecx),%edx 125169689Skan 126169689Skan movl (%esp),%eax /* Hardware registers */ 127169689Skan movl %eax,PCB_EIP(%edx) 128169689Skan movl %ebx,PCB_EBX(%edx) 129169689Skan movl %esp,PCB_ESP(%edx) 130169689Skan movl %ebp,PCB_EBP(%edx) 131169689Skan movl %esi,PCB_ESI(%edx) 132169689Skan movl %edi,PCB_EDI(%edx) 133169689Skan mov %gs,PCB_GS(%edx) 134169689Skan pushfl /* PSL */ 135169689Skan popl PCB_PSL(%edx) 136169689Skan /* Test if debug registers should be saved. */ 137169689Skan testl $PCB_DBREGS,PCB_FLAGS(%edx) 138169689Skan jz 1f /* no, skip over */ 139169689Skan movl %dr7,%eax /* yes, do the save */ 140169689Skan movl %eax,PCB_DR7(%edx) 141169689Skan andl $0x0000fc00, %eax /* disable all watchpoints */ 142169689Skan movl %eax,%dr7 143169689Skan movl %dr6,%eax 144169689Skan movl %eax,PCB_DR6(%edx) 145169689Skan movl %dr3,%eax 146169689Skan movl %eax,PCB_DR3(%edx) 147169689Skan movl %dr2,%eax 148169689Skan movl %eax,PCB_DR2(%edx) 149169689Skan movl %dr1,%eax 150169689Skan movl %eax,PCB_DR1(%edx) 151169689Skan movl %dr0,%eax 152169689Skan movl %eax,PCB_DR0(%edx) 153169689Skan1: 154169689Skan 155169689Skan#ifdef DEV_NPX 156169689Skan /* have we used fp, and need a save? */ 157169689Skan cmpl %ecx,PCPU(FPCURTHREAD) 158169689Skan jne 1f 159169689Skan pushl PCB_SAVEFPU(%edx) /* h/w bugs make saving complicated */ 160169689Skan call npxsave /* do it in a big C function */ 161169689Skan popl %eax 162169689Skan1: 163169689Skan#endif 164169689Skan 165169689Skan /* Save is done. Now fire up new thread. Leave old vmspace. */ 166169689Skan movl 4(%esp),%edi 167169689Skan movl 8(%esp),%ecx /* New thread */ 168169689Skan movl 12(%esp),%esi /* New lock */ 169169689Skan#ifdef INVARIANTS 170169689Skan testl %ecx,%ecx /* no thread? */ 171169689Skan jz badsw3 /* no, panic */ 172169689Skan#endif 173169689Skan movl TD_PCB(%ecx),%edx 174169689Skan 175169689Skan /* switch address space */ 176169689Skan movl PCB_CR3(%edx),%eax 177169689Skan#ifdef PAE 178169689Skan cmpl %eax,IdlePDPT /* Kernel address space? */ 179169689Skan#else 180169689Skan cmpl %eax,IdlePTD /* Kernel address space? */ 181169689Skan#endif 182169689Skan je sw0 183169689Skan READ_CR3(%ebx) /* The same address space? */ 184169689Skan cmpl %ebx,%eax 185169689Skan je sw0 186169689Skan LOAD_CR3(%eax) /* new address space */ 187169689Skan movl %esi,%eax 188169689Skan movl PCPU(CPUID),%esi 189169689Skan SETOP %eax,TD_LOCK(%edi) /* Switchout td_lock */ 190169689Skan 191169689Skan /* Release bit from old pmap->pm_active */ 192169689Skan movl PCPU(CURPMAP), %ebx 193169689Skan#ifdef SMP 194169689Skan lock 195169689Skan#endif 196169689Skan btrl %esi, PM_ACTIVE(%ebx) /* clear old */ 197169689Skan 198169689Skan /* Set bit in new pmap->pm_active */ 199169689Skan movl TD_PROC(%ecx),%eax /* newproc */ 200169689Skan movl P_VMSPACE(%eax), %ebx 201169689Skan addl $VM_PMAP, %ebx 202169689Skan movl %ebx, PCPU(CURPMAP) 203169689Skan#ifdef SMP 204169689Skan lock 205169689Skan#endif 206169689Skan btsl %esi, PM_ACTIVE(%ebx) /* set new */ 207169689Skan jmp sw1 208169689Skan 209169689Skansw0: 210169689Skan SETOP %esi,TD_LOCK(%edi) /* Switchout td_lock */ 211169689Skansw1: 212169689Skan BLOCK_SPIN(%ecx) 213169689Skan#ifdef XEN 214169689Skan pushl %eax 215169689Skan pushl %ecx 216169689Skan pushl %edx 217169689Skan call xen_handle_thread_switch 218169689Skan popl %edx 219169689Skan popl %ecx 220169689Skan popl %eax 221169689Skan /* 222169689Skan * 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 movl %cr0,%eax 390 movl %eax,PCB_CR0(%ecx) 391 movl %cr2,%eax 392 movl %eax,PCB_CR2(%ecx) 393 movl %cr4,%eax 394 movl %eax,PCB_CR4(%ecx) 395 396 movl %dr0,%eax 397 movl %eax,PCB_DR0(%ecx) 398 movl %dr1,%eax 399 movl %eax,PCB_DR1(%ecx) 400 movl %dr2,%eax 401 movl %eax,PCB_DR2(%ecx) 402 movl %dr3,%eax 403 movl %eax,PCB_DR3(%ecx) 404 movl %dr6,%eax 405 movl %eax,PCB_DR6(%ecx) 406 movl %dr7,%eax 407 movl %eax,PCB_DR7(%ecx) 408 409 mov %ds,PCB_DS(%ecx) 410 mov %es,PCB_ES(%ecx) 411 mov %fs,PCB_FS(%ecx) 412 mov %ss,PCB_SS(%ecx) 413 414 sgdt PCB_GDT(%ecx) 415 sidt PCB_IDT(%ecx) 416 sldt PCB_LDT(%ecx) 417 str PCB_TR(%ecx) 418 419 movl $1,%eax 420 ret 421END(savectx) 422 423/* 424 * resumectx(pcb) __fastcall 425 * Resuming processor state from pcb. 426 */ 427ENTRY(resumectx) 428 /* Restore GDT. */ 429 lgdt PCB_GDT(%ecx) 430 431 /* Restore segment registers */ 432 movzwl PCB_DS(%ecx),%eax 433 mov %ax,%ds 434 movzwl PCB_ES(%ecx),%eax 435 mov %ax,%es 436 movzwl PCB_FS(%ecx),%eax 437 mov %ax,%fs 438 movzwl PCB_GS(%ecx),%eax 439 movw %ax,%gs 440 movzwl PCB_SS(%ecx),%eax 441 mov %ax,%ss 442 443 /* Restore CR2, CR4, CR3 and CR0 */ 444 movl PCB_CR2(%ecx),%eax 445 movl %eax,%cr2 446 movl PCB_CR4(%ecx),%eax 447 movl %eax,%cr4 448 movl PCB_CR3(%ecx),%eax 449 movl %eax,%cr3 450 movl PCB_CR0(%ecx),%eax 451 movl %eax,%cr0 452 jmp 1f 4531: 454 455 /* Restore descriptor tables */ 456 lidt PCB_IDT(%ecx) 457 lldt PCB_LDT(%ecx) 458 459#define SDT_SYS386TSS 9 460#define SDT_SYS386BSY 11 461 /* Clear "task busy" bit and reload TR */ 462 movl PCPU(TSS_GDT),%eax 463 andb $(~SDT_SYS386BSY | SDT_SYS386TSS),5(%eax) 464 movzwl PCB_TR(%ecx),%eax 465 ltr %ax 466#undef SDT_SYS386TSS 467#undef SDT_SYS386BSY 468 469 /* Restore debug registers */ 470 movl PCB_DR0(%ecx),%eax 471 movl %eax,%dr0 472 movl PCB_DR1(%ecx),%eax 473 movl %eax,%dr1 474 movl PCB_DR2(%ecx),%eax 475 movl %eax,%dr2 476 movl PCB_DR3(%ecx),%eax 477 movl %eax,%dr3 478 movl PCB_DR6(%ecx),%eax 479 movl %eax,%dr6 480 movl PCB_DR7(%ecx),%eax 481 movl %eax,%dr7 482 483 /* Restore other registers */ 484 movl PCB_EDI(%ecx),%edi 485 movl PCB_ESI(%ecx),%esi 486 movl PCB_EBP(%ecx),%ebp 487 movl PCB_ESP(%ecx),%esp 488 movl PCB_EBX(%ecx),%ebx 489 490 /* reload code selector by turning return into intersegmental return */ 491 pushl PCB_EIP(%ecx) 492 movl $KCSEL,4(%esp) 493 xorl %eax,%eax 494 lret 495END(resumectx) 496