The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/amd64/amd64/cpu_switch.S

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2003 Peter Wemm.
    3  * Copyright (c) 1990 The Regents of the University of California.
    4  * All rights reserved.
    5  *
    6  * This code is derived from software contributed to Berkeley by
    7  * William Jolitz.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions
   11  * are met:
   12  * 1. Redistributions of source code must retain the above copyright
   13  *    notice, this list of conditions and the following disclaimer.
   14  * 2. Redistributions in binary form must reproduce the above copyright
   15  *    notice, this list of conditions and the following disclaimer in the
   16  *    documentation and/or other materials provided with the distribution.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  * $FreeBSD: releng/11.1/sys/amd64/amd64/cpu_switch.S 335465 2018-06-21 05:17:13Z gordon $
   34  */
   35 
   36 #include <machine/asmacros.h>
   37 #include <machine/specialreg.h>
   38 
   39 #include "assym.s"
   40 #include "opt_sched.h"
   41 
   42 /*****************************************************************************/
   43 /* Scheduling                                                                */
   44 /*****************************************************************************/
   45 
   46         .text
   47 
   48 #ifdef SMP
   49 #define LK      lock ;
   50 #else
   51 #define LK
   52 #endif
   53 
   54 #if defined(SCHED_ULE) && defined(SMP)
   55 #define SETLK   xchgq
   56 #else
   57 #define SETLK   movq
   58 #endif
   59 
   60 /*
   61  * cpu_throw()
   62  *
   63  * This is the second half of cpu_switch(). It is used when the current
   64  * thread is either a dummy or slated to die, and we no longer care
   65  * about its state.  This is only a slight optimization and is probably
   66  * not worth it anymore.  Note that we need to clear the pm_active bits so
   67  * we do need the old proc if it still exists.
   68  * %rdi = oldtd
   69  * %rsi = newtd
   70  */
   71 ENTRY(cpu_throw)
   72         movq    %rsi,%r12
   73         movq    %rsi,%rdi
   74         call    pmap_activate_sw
   75         jmp     sw1
   76 END(cpu_throw)
   77 
   78 /*
   79  * cpu_switch(old, new, mtx)
   80  *
   81  * Save the current thread state, then select the next thread to run
   82  * and load its state.
   83  * %rdi = oldtd
   84  * %rsi = newtd
   85  * %rdx = mtx
   86  */
   87 ENTRY(cpu_switch)
   88         /* Switch to new thread.  First, save context. */
   89         movq    TD_PCB(%rdi),%r8
   90         orl     $PCB_FULL_IRET,PCB_FLAGS(%r8)
   91 
   92         movq    (%rsp),%rax                     /* Hardware registers */
   93         movq    %r15,PCB_R15(%r8)
   94         movq    %r14,PCB_R14(%r8)
   95         movq    %r13,PCB_R13(%r8)
   96         movq    %r12,PCB_R12(%r8)
   97         movq    %rbp,PCB_RBP(%r8)
   98         movq    %rsp,PCB_RSP(%r8)
   99         movq    %rbx,PCB_RBX(%r8)
  100         movq    %rax,PCB_RIP(%r8)
  101 
  102         testl   $PCB_DBREGS,PCB_FLAGS(%r8)
  103         jnz     store_dr                        /* static predict not taken */
  104 done_store_dr:
  105 
  106         /* have we used fp, and need a save? */
  107         cmpq    %rdi,PCPU(FPCURTHREAD)
  108         jne     2f
  109         movq    PCB_SAVEFPU(%r8),%r8
  110         clts
  111         cmpl    $0,use_xsave(%rip)
  112         jne     1f
  113         fxsave  (%r8)
  114         jmp     2f
  115 1:      movq    %rdx,%rcx
  116         movl    xsave_mask,%eax
  117         movl    xsave_mask+4,%edx
  118         .globl  ctx_switch_xsave
  119 ctx_switch_xsave:
  120         /* This is patched to xsaveopt if supported, see fpuinit_bsp1() */
  121         xsave   (%r8)
  122         movq    %rcx,%rdx
  123 2:
  124         /* Save is done.  Now fire up new thread. Leave old vmspace. */
  125         movq    %rsi,%r12
  126         movq    %rdi,%r13
  127         movq    %rdx,%r15
  128         movq    %rsi,%rdi
  129         callq   pmap_activate_sw
  130         SETLK   %r15,TD_LOCK(%r13)              /* Release the old thread */
  131 sw1:
  132         movq    TD_PCB(%r12),%r8
  133 #if defined(SCHED_ULE) && defined(SMP)
  134         /* Wait for the new thread to become unblocked */
  135         movq    $blocked_lock, %rdx
  136 1:
  137         movq    TD_LOCK(%r12),%rcx
  138         cmpq    %rcx, %rdx
  139         pause
  140         je      1b
  141 #endif
  142         /*
  143          * At this point, we've switched address spaces and are ready
  144          * to load up the rest of the next context.
  145          */
  146 
  147         /* Skip loading user fsbase/gsbase for kthreads */
  148         testl   $TDP_KTHREAD,TD_PFLAGS(%r12)
  149         jnz     do_kthread
  150 
  151         /*
  152          * Load ldt register
  153          */
  154         movq    TD_PROC(%r12),%rcx
  155         cmpq    $0, P_MD+MD_LDT(%rcx)
  156         jne     do_ldt
  157         xorl    %eax,%eax
  158 ld_ldt: lldt    %ax
  159 
  160         /* Restore fs base in GDT */
  161         movl    PCB_FSBASE(%r8),%eax
  162         movq    PCPU(FS32P),%rdx
  163         movw    %ax,2(%rdx)
  164         shrl    $16,%eax
  165         movb    %al,4(%rdx)
  166         shrl    $8,%eax
  167         movb    %al,7(%rdx)
  168 
  169         /* Restore gs base in GDT */
  170         movl    PCB_GSBASE(%r8),%eax
  171         movq    PCPU(GS32P),%rdx
  172         movw    %ax,2(%rdx)
  173         shrl    $16,%eax
  174         movb    %al,4(%rdx)
  175         shrl    $8,%eax
  176         movb    %al,7(%rdx)
  177 
  178 do_kthread:
  179         /* Do we need to reload tss ? */
  180         movq    PCPU(TSSP),%rax
  181         movq    PCB_TSSP(%r8),%rdx
  182         testq   %rdx,%rdx
  183         cmovzq  PCPU(COMMONTSSP),%rdx
  184         cmpq    %rax,%rdx
  185         jne     do_tss
  186 done_tss:
  187         movq    %r8,PCPU(RSP0)
  188         movq    %r8,PCPU(CURPCB)
  189         /* Update the COMMON_TSS_RSP0 pointer for the next interrupt */
  190         cmpb    $0,pti(%rip)
  191         jne     1f
  192         movq    %r8,COMMON_TSS_RSP0(%rdx)
  193 1:      movq    %r12,PCPU(CURTHREAD)            /* into next thread */
  194 
  195         /* Test if debug registers should be restored. */
  196         testl   $PCB_DBREGS,PCB_FLAGS(%r8)
  197         jnz     load_dr                         /* static predict not taken */
  198 done_load_dr:
  199 
  200         /* Restore context. */
  201         movq    PCB_R15(%r8),%r15
  202         movq    PCB_R14(%r8),%r14
  203         movq    PCB_R13(%r8),%r13
  204         movq    PCB_R12(%r8),%r12
  205         movq    PCB_RBP(%r8),%rbp
  206         movq    PCB_RSP(%r8),%rsp
  207         movq    PCB_RBX(%r8),%rbx
  208         movq    PCB_RIP(%r8),%rax
  209         movq    %rax,(%rsp)
  210         movq    PCPU(CURTHREAD),%rdi
  211         call    fpu_activate_sw
  212         ret
  213 
  214         /*
  215          * We order these strangely for several reasons.
  216          * 1: I wanted to use static branch prediction hints
  217          * 2: Most athlon64/opteron cpus don't have them.  They define
  218          *    a forward branch as 'predict not taken'.  Intel cores have
  219          *    the 'rep' prefix to invert this.
  220          * So, to make it work on both forms of cpu we do the detour.
  221          * We use jumps rather than call in order to avoid the stack.
  222          */
  223 
  224 store_dr:
  225         movq    %dr7,%rax                       /* yes, do the save */
  226         movq    %dr0,%r15
  227         movq    %dr1,%r14
  228         movq    %dr2,%r13
  229         movq    %dr3,%r12
  230         movq    %dr6,%r11
  231         movq    %r15,PCB_DR0(%r8)
  232         movq    %r14,PCB_DR1(%r8)
  233         movq    %r13,PCB_DR2(%r8)
  234         movq    %r12,PCB_DR3(%r8)
  235         movq    %r11,PCB_DR6(%r8)
  236         movq    %rax,PCB_DR7(%r8)
  237         andq    $0x0000fc00, %rax               /* disable all watchpoints */
  238         movq    %rax,%dr7
  239         jmp     done_store_dr
  240 
  241 load_dr:
  242         movq    %dr7,%rax
  243         movq    PCB_DR0(%r8),%r15
  244         movq    PCB_DR1(%r8),%r14
  245         movq    PCB_DR2(%r8),%r13
  246         movq    PCB_DR3(%r8),%r12
  247         movq    PCB_DR6(%r8),%r11
  248         movq    PCB_DR7(%r8),%rcx
  249         movq    %r15,%dr0
  250         movq    %r14,%dr1
  251         /* Preserve reserved bits in %dr7 */
  252         andq    $0x0000fc00,%rax
  253         andq    $~0x0000fc00,%rcx
  254         movq    %r13,%dr2
  255         movq    %r12,%dr3
  256         orq     %rcx,%rax
  257         movq    %r11,%dr6
  258         movq    %rax,%dr7
  259         jmp     done_load_dr
  260 
  261 do_tss: movq    %rdx,PCPU(TSSP)
  262         movq    %rdx,%rcx
  263         movq    PCPU(TSS),%rax
  264         movw    %cx,2(%rax)
  265         shrq    $16,%rcx
  266         movb    %cl,4(%rax)
  267         shrq    $8,%rcx
  268         movb    %cl,7(%rax)
  269         shrq    $8,%rcx
  270         movl    %ecx,8(%rax)
  271         movb    $0x89,5(%rax)   /* unset busy */
  272         cmpb    $0,pti(%rip)
  273         je      1f
  274         movq    PCPU(PRVSPACE),%rax
  275         addq    $PC_PTI_STACK+PC_PTI_STACK_SZ*8,%rax
  276         movq    %rax,COMMON_TSS_RSP0(%rdx)
  277 1:      movl    $TSSSEL,%eax
  278         ltr     %ax
  279         jmp     done_tss
  280 
  281 do_ldt: movq    PCPU(LDT),%rax
  282         movq    P_MD+MD_LDT_SD(%rcx),%rdx
  283         movq    %rdx,(%rax)
  284         movq    P_MD+MD_LDT_SD+8(%rcx),%rdx
  285         movq    %rdx,8(%rax)
  286         movl    $LDTSEL,%eax
  287         jmp     ld_ldt
  288 END(cpu_switch)
  289 
  290 /*
  291  * savectx(pcb)
  292  * Update pcb, saving current processor state.
  293  */
  294 ENTRY(savectx)
  295         /* Save caller's return address. */
  296         movq    (%rsp),%rax
  297         movq    %rax,PCB_RIP(%rdi)
  298 
  299         movq    %rbx,PCB_RBX(%rdi)
  300         movq    %rsp,PCB_RSP(%rdi)
  301         movq    %rbp,PCB_RBP(%rdi)
  302         movq    %r12,PCB_R12(%rdi)
  303         movq    %r13,PCB_R13(%rdi)
  304         movq    %r14,PCB_R14(%rdi)
  305         movq    %r15,PCB_R15(%rdi)
  306 
  307         movq    %cr0,%rax
  308         movq    %rax,PCB_CR0(%rdi)
  309         movq    %cr2,%rax
  310         movq    %rax,PCB_CR2(%rdi)
  311         movq    %cr3,%rax
  312         movq    %rax,PCB_CR3(%rdi)
  313         movq    %cr4,%rax
  314         movq    %rax,PCB_CR4(%rdi)
  315 
  316         movq    %dr0,%rax
  317         movq    %rax,PCB_DR0(%rdi)
  318         movq    %dr1,%rax
  319         movq    %rax,PCB_DR1(%rdi)
  320         movq    %dr2,%rax
  321         movq    %rax,PCB_DR2(%rdi)
  322         movq    %dr3,%rax
  323         movq    %rax,PCB_DR3(%rdi)
  324         movq    %dr6,%rax
  325         movq    %rax,PCB_DR6(%rdi)
  326         movq    %dr7,%rax
  327         movq    %rax,PCB_DR7(%rdi)
  328 
  329         movl    $MSR_FSBASE,%ecx
  330         rdmsr
  331         movl    %eax,PCB_FSBASE(%rdi)
  332         movl    %edx,PCB_FSBASE+4(%rdi)
  333         movl    $MSR_GSBASE,%ecx
  334         rdmsr
  335         movl    %eax,PCB_GSBASE(%rdi)
  336         movl    %edx,PCB_GSBASE+4(%rdi)
  337         movl    $MSR_KGSBASE,%ecx
  338         rdmsr
  339         movl    %eax,PCB_KGSBASE(%rdi)
  340         movl    %edx,PCB_KGSBASE+4(%rdi)
  341         movl    $MSR_EFER,%ecx
  342         rdmsr
  343         movl    %eax,PCB_EFER(%rdi)
  344         movl    %edx,PCB_EFER+4(%rdi)
  345         movl    $MSR_STAR,%ecx
  346         rdmsr
  347         movl    %eax,PCB_STAR(%rdi)
  348         movl    %edx,PCB_STAR+4(%rdi)
  349         movl    $MSR_LSTAR,%ecx
  350         rdmsr
  351         movl    %eax,PCB_LSTAR(%rdi)
  352         movl    %edx,PCB_LSTAR+4(%rdi)
  353         movl    $MSR_CSTAR,%ecx
  354         rdmsr
  355         movl    %eax,PCB_CSTAR(%rdi)
  356         movl    %edx,PCB_CSTAR+4(%rdi)
  357         movl    $MSR_SF_MASK,%ecx
  358         rdmsr
  359         movl    %eax,PCB_SFMASK(%rdi)
  360         movl    %edx,PCB_SFMASK+4(%rdi)
  361 
  362         sgdt    PCB_GDT(%rdi)
  363         sidt    PCB_IDT(%rdi)
  364         sldt    PCB_LDT(%rdi)
  365         str     PCB_TR(%rdi)
  366 
  367         movl    $1,%eax
  368         ret
  369 END(savectx)
  370 
  371 /*
  372  * resumectx(pcb)
  373  * Resuming processor state from pcb.
  374  */     
  375 ENTRY(resumectx)
  376         /* Switch to KPML4phys. */
  377         movq    KPML4phys,%rax
  378         movq    %rax,%cr3
  379 
  380         /* Force kernel segment registers. */
  381         movl    $KDSEL,%eax
  382         movw    %ax,%ds
  383         movw    %ax,%es
  384         movw    %ax,%ss
  385         movl    $KUF32SEL,%eax
  386         movw    %ax,%fs
  387         movl    $KUG32SEL,%eax
  388         movw    %ax,%gs
  389 
  390         movl    $MSR_FSBASE,%ecx
  391         movl    PCB_FSBASE(%rdi),%eax
  392         movl    4 + PCB_FSBASE(%rdi),%edx
  393         wrmsr
  394         movl    $MSR_GSBASE,%ecx
  395         movl    PCB_GSBASE(%rdi),%eax
  396         movl    4 + PCB_GSBASE(%rdi),%edx
  397         wrmsr
  398         movl    $MSR_KGSBASE,%ecx
  399         movl    PCB_KGSBASE(%rdi),%eax
  400         movl    4 + PCB_KGSBASE(%rdi),%edx
  401         wrmsr
  402 
  403         /* Restore EFER one more time. */
  404         movl    $MSR_EFER,%ecx
  405         movl    PCB_EFER(%rdi),%eax
  406         wrmsr
  407 
  408         /* Restore fast syscall stuff. */
  409         movl    $MSR_STAR,%ecx
  410         movl    PCB_STAR(%rdi),%eax
  411         movl    4 + PCB_STAR(%rdi),%edx
  412         wrmsr
  413         movl    $MSR_LSTAR,%ecx
  414         movl    PCB_LSTAR(%rdi),%eax
  415         movl    4 + PCB_LSTAR(%rdi),%edx
  416         wrmsr
  417         movl    $MSR_CSTAR,%ecx
  418         movl    PCB_CSTAR(%rdi),%eax
  419         movl    4 + PCB_CSTAR(%rdi),%edx
  420         wrmsr
  421         movl    $MSR_SF_MASK,%ecx
  422         movl    PCB_SFMASK(%rdi),%eax
  423         wrmsr
  424 
  425         /* Restore CR0, CR2, CR4 and CR3. */
  426         movq    PCB_CR0(%rdi),%rax
  427         movq    %rax,%cr0
  428         movq    PCB_CR2(%rdi),%rax
  429         movq    %rax,%cr2
  430         movq    PCB_CR4(%rdi),%rax
  431         movq    %rax,%cr4
  432         movq    PCB_CR3(%rdi),%rax
  433         movq    %rax,%cr3
  434 
  435         /* Restore descriptor tables. */
  436         lidt    PCB_IDT(%rdi)
  437         lldt    PCB_LDT(%rdi)
  438 
  439 #define SDT_SYSTSS      9
  440 #define SDT_SYSBSY      11
  441 
  442         /* Clear "task busy" bit and reload TR. */
  443         movq    PCPU(TSS),%rax
  444         andb    $(~SDT_SYSBSY | SDT_SYSTSS),5(%rax)
  445         movw    PCB_TR(%rdi),%ax
  446         ltr     %ax
  447 
  448 #undef  SDT_SYSTSS
  449 #undef  SDT_SYSBSY
  450 
  451         /* Restore debug registers. */
  452         movq    PCB_DR0(%rdi),%rax
  453         movq    %rax,%dr0
  454         movq    PCB_DR1(%rdi),%rax
  455         movq    %rax,%dr1
  456         movq    PCB_DR2(%rdi),%rax
  457         movq    %rax,%dr2
  458         movq    PCB_DR3(%rdi),%rax
  459         movq    %rax,%dr3
  460         movq    PCB_DR6(%rdi),%rax
  461         movq    %rax,%dr6
  462         movq    PCB_DR7(%rdi),%rax
  463         movq    %rax,%dr7
  464 
  465         /* Restore other callee saved registers. */
  466         movq    PCB_R15(%rdi),%r15
  467         movq    PCB_R14(%rdi),%r14
  468         movq    PCB_R13(%rdi),%r13
  469         movq    PCB_R12(%rdi),%r12
  470         movq    PCB_RBP(%rdi),%rbp
  471         movq    PCB_RSP(%rdi),%rsp
  472         movq    PCB_RBX(%rdi),%rbx
  473 
  474         /* Restore return address. */
  475         movq    PCB_RIP(%rdi),%rax
  476         movq    %rax,(%rsp)
  477 
  478         xorl    %eax,%eax
  479         ret
  480 END(resumectx)

Cache object: 14d0ba12d434a280aeb8de2392159054


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.