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