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/6.0/sys/amd64/amd64/cpu_switch.S 133897 2004-08-16 22:53:29Z peter $
   34  */
   35 
   36 #include <machine/asmacros.h>
   37 #include <machine/specialreg.h>
   38 
   39 #include "assym.s"
   40 
   41 /*****************************************************************************/
   42 /* Scheduling                                                                */
   43 /*****************************************************************************/
   44 
   45         .text
   46 
   47 #ifdef SMP
   48 #define LK      lock ;
   49 #else
   50 #define LK
   51 #endif
   52 
   53 /*
   54  * cpu_throw()
   55  *
   56  * This is the second half of cpu_switch(). It is used when the current
   57  * thread is either a dummy or slated to die, and we no longer care
   58  * about its state.  This is only a slight optimization and is probably
   59  * not worth it anymore.  Note that we need to clear the pm_active bits so
   60  * we do need the old proc if it still exists.
   61  * %rdi = oldtd
   62  * %rsi = newtd
   63  */
   64 ENTRY(cpu_throw)
   65         movl    PCPU(CPUID), %eax
   66         testq   %rdi,%rdi                       /* no thread? */
   67         jz      1f
   68         /* release bit from old pm_active */
   69         movq    TD_PROC(%rdi), %rdx             /* oldtd->td_proc */
   70         movq    P_VMSPACE(%rdx), %rdx           /* proc->p_vmspace */
   71         LK btrl %eax, VM_PMAP+PM_ACTIVE(%rdx)   /* clear old */
   72 1:
   73         movq    TD_PCB(%rsi),%rdx               /* newtd->td_proc */
   74         movq    PCB_CR3(%rdx),%rdx
   75         movq    %rdx,%cr3                       /* new address space */
   76         /* set bit in new pm_active */
   77         movq    TD_PROC(%rsi),%rdx
   78         movq    P_VMSPACE(%rdx), %rdx
   79         LK btsl %eax, VM_PMAP+PM_ACTIVE(%rdx)   /* set new */
   80         jmp     sw1
   81 
   82 /*
   83  * cpu_switch(old, new)
   84  *
   85  * Save the current thread state, then select the next thread to run
   86  * and load its state.
   87  * %rdi = oldtd
   88  * %rsi = newtd
   89  */
   90 ENTRY(cpu_switch)
   91         /* Switch to new thread.  First, save context. */
   92         movq    TD_PCB(%rdi),%r8
   93 
   94         movq    (%rsp),%rax                     /* Hardware registers */
   95         movq    %rax,PCB_RIP(%r8)
   96         movq    %rbx,PCB_RBX(%r8)
   97         movq    %rsp,PCB_RSP(%r8)
   98         movq    %rbp,PCB_RBP(%r8)
   99         movq    %r12,PCB_R12(%r8)
  100         movq    %r13,PCB_R13(%r8)
  101         movq    %r14,PCB_R14(%r8)
  102         movq    %r15,PCB_R15(%r8)
  103         pushfq                                  /* PSL */
  104         popq    PCB_RFLAGS(%r8)
  105 
  106         testl   $PCB_32BIT,PCB_FLAGS(%r8)
  107         jz      1f                              /* no, skip over */
  108 
  109         /* Save segment selector numbers */
  110         movl    %ds,PCB_DS(%r8)
  111         movl    %es,PCB_ES(%r8)
  112         movl    %fs,PCB_FS(%r8)
  113         movl    %gs,PCB_GS(%r8)
  114         jmp     2f
  115 1:
  116 
  117         /* Save userland %fs */
  118         movl    $MSR_FSBASE,%ecx
  119         rdmsr
  120         movl    %eax,PCB_FSBASE(%r8)
  121         movl    %edx,PCB_FSBASE+4(%r8)
  122 
  123         /* Save userland %gs */
  124         movl    $MSR_KGSBASE,%ecx
  125         rdmsr
  126         movl    %eax,PCB_GSBASE(%r8)
  127         movl    %edx,PCB_GSBASE+4(%r8)
  128 2:
  129 
  130         /* Test if debug registers should be saved. */
  131         testl   $PCB_DBREGS,PCB_FLAGS(%r8)
  132         jz      1f                              /* no, skip over */
  133         movq    %dr7,%rax                       /* yes, do the save */
  134         movq    %rax,PCB_DR7(%r8)
  135         andq    $0x0000fc00, %rax               /* disable all watchpoints */
  136         movq    %rax,%dr7
  137         movq    %dr6,%rax
  138         movq    %rax,PCB_DR6(%r8)
  139         movq    %dr3,%rax
  140         movq    %rax,PCB_DR3(%r8)
  141         movq    %dr2,%rax
  142         movq    %rax,PCB_DR2(%r8)
  143         movq    %dr1,%rax
  144         movq    %rax,PCB_DR1(%r8)
  145         movq    %dr0,%rax
  146         movq    %rax,PCB_DR0(%r8)
  147 1:
  148 
  149         /* have we used fp, and need a save? */
  150         cmpq    %rdi,PCPU(FPCURTHREAD)
  151         jne     1f
  152         addq    $PCB_SAVEFPU,%r8
  153         clts
  154         fxsave  (%r8)
  155         smsw    %ax
  156         orb     $CR0_TS,%al
  157         lmsw    %ax
  158         xorq    %rax,%rax
  159         movq    %rax,PCPU(FPCURTHREAD)
  160 1:
  161 
  162         /* Save is done.  Now fire up new thread. Leave old vmspace. */
  163         movq    TD_PCB(%rsi),%r8
  164 
  165         /* switch address space */
  166         movq    PCB_CR3(%r8),%rdx
  167         movq    %cr3,%rax
  168         cmpq    %rdx,%rax                       /* Same address space? */
  169         je      sw1
  170         movq    %rdx,%cr3                       /* new address space */
  171 
  172         movl    PCPU(CPUID), %eax
  173         /* Release bit from old pmap->pm_active */
  174         movq    TD_PROC(%rdi), %rdx             /* oldproc */
  175         movq    P_VMSPACE(%rdx), %rdx
  176         LK btrl %eax, VM_PMAP+PM_ACTIVE(%rdx)   /* clear old */
  177 
  178         /* Set bit in new pmap->pm_active */
  179         movq    TD_PROC(%rsi),%rdx              /* newproc */
  180         movq    P_VMSPACE(%rdx), %rdx
  181         LK btsl %eax, VM_PMAP+PM_ACTIVE(%rdx)   /* set new */
  182 
  183 sw1:
  184         /*
  185          * At this point, we've switched address spaces and are ready
  186          * to load up the rest of the next context.
  187          */
  188         movq    TD_PCB(%rsi),%r8
  189 
  190         testl   $PCB_32BIT,PCB_FLAGS(%r8)
  191         jz      1f                              /* no, skip over */
  192 
  193         /* Restore segment selector numbers */
  194         movl    PCB_DS(%r8),%ds
  195         movl    PCB_ES(%r8),%es
  196         movl    PCB_FS(%r8),%fs
  197 
  198         /* Restore userland %gs while preserving kernel gsbase */
  199         movl    $MSR_GSBASE,%ecx
  200         rdmsr
  201         movl    PCB_GS(%r8),%gs
  202         wrmsr
  203         jmp     2f
  204 1:
  205 
  206         /* Restore userland %fs */
  207         movl    $MSR_FSBASE,%ecx
  208         movl    PCB_FSBASE(%r8),%eax
  209         movl    PCB_FSBASE+4(%r8),%edx
  210         wrmsr
  211 
  212         /* Restore userland %gs */
  213         movl    $MSR_KGSBASE,%ecx
  214         movl    PCB_GSBASE(%r8),%eax
  215         movl    PCB_GSBASE+4(%r8),%edx
  216         wrmsr
  217 2:
  218 
  219         /* Update the TSS_RSP0 pointer for the next interrupt */
  220         movq    PCPU(TSSP), %rax
  221         addq    $COMMON_TSS_RSP0, %rax
  222         leaq    -16(%r8), %rbx
  223         movq    %rbx, (%rax)
  224         movq    %rbx, PCPU(RSP0)
  225 
  226         /* Restore context. */
  227         movq    PCB_RBX(%r8),%rbx
  228         movq    PCB_RSP(%r8),%rsp
  229         movq    PCB_RBP(%r8),%rbp
  230         movq    PCB_R12(%r8),%r12
  231         movq    PCB_R13(%r8),%r13
  232         movq    PCB_R14(%r8),%r14
  233         movq    PCB_R15(%r8),%r15
  234         movq    PCB_RIP(%r8),%rax
  235         movq    %rax,(%rsp)
  236         pushq   PCB_RFLAGS(%r8)
  237         popfq
  238 
  239         movq    %r8, PCPU(CURPCB)
  240         movq    %rsi, PCPU(CURTHREAD)           /* into next thread */
  241 
  242         /* Test if debug registers should be restored. */
  243         testl   $PCB_DBREGS,PCB_FLAGS(%r8)
  244         jz      1f
  245         movq    PCB_DR6(%r8),%rax
  246         movq    %rax,%dr6
  247         movq    PCB_DR3(%r8),%rax
  248         movq    %rax,%dr3
  249         movq    PCB_DR2(%r8),%rax
  250         movq    %rax,%dr2
  251         movq    PCB_DR1(%r8),%rax
  252         movq    %rax,%dr1
  253         movq    PCB_DR0(%r8),%rax
  254         movq    %rax,%dr0
  255         /* But preserve reserved bits in %dr7 */
  256         movq    %dr7,%rax
  257         andq    $0x0000fc00,%rax
  258         movq    PCB_DR7(%r8),%rcx
  259         andq    $~0x0000fc00,%rcx
  260         orq     %rcx,%rax
  261         movq    %rax,%dr7
  262 1:
  263         ret
  264 
  265 /*
  266  * savectx(pcb)
  267  * Update pcb, saving current processor state.
  268  */
  269 ENTRY(savectx)
  270         /* Fetch PCB. */
  271         movq    %rdi,%rcx
  272 
  273         /* Save caller's return address. */
  274         movq    (%rsp),%rax
  275         movq    %rax,PCB_RIP(%rcx)
  276 
  277         movq    %cr3,%rax
  278         movq    %rax,PCB_CR3(%rcx)
  279 
  280         movq    %rbx,PCB_RBX(%rcx)
  281         movq    %rsp,PCB_RSP(%rcx)
  282         movq    %rbp,PCB_RBP(%rcx)
  283         movq    %r12,PCB_R12(%rcx)
  284         movq    %r13,PCB_R13(%rcx)
  285         movq    %r14,PCB_R14(%rcx)
  286         movq    %r15,PCB_R15(%rcx)
  287         pushfq
  288         popq    PCB_RFLAGS(%rcx)
  289 
  290         /*
  291          * If fpcurthread == NULL, then the fpu h/w state is irrelevant and the
  292          * state had better already be in the pcb.  This is true for forks
  293          * but not for dumps (the old book-keeping with FP flags in the pcb
  294          * always lost for dumps because the dump pcb has 0 flags).
  295          *
  296          * If fpcurthread != NULL, then we have to save the fpu h/w state to
  297          * fpcurthread's pcb and copy it to the requested pcb, or save to the
  298          * requested pcb and reload.  Copying is easier because we would
  299          * have to handle h/w bugs for reloading.  We used to lose the
  300          * parent's fpu state for forks by forgetting to reload.
  301          */
  302         pushfq
  303         cli
  304         movq    PCPU(FPCURTHREAD),%rax
  305         testq   %rax,%rax
  306         je      1f
  307 
  308         movq    TD_PCB(%rax),%rdi
  309         leaq    PCB_SAVEFPU(%rdi),%rdi
  310         clts
  311         fxsave  (%rdi)
  312         smsw    %ax
  313         orb     $CR0_TS,%al
  314         lmsw    %ax
  315 
  316         movq    $PCB_SAVEFPU_SIZE,%rdx  /* arg 3 */
  317         leaq    PCB_SAVEFPU(%rcx),%rsi  /* arg 2 */
  318         /* arg 1 (%rdi) already loaded */
  319         call    bcopy
  320 1:
  321         popfq
  322 
  323         ret

Cache object: 45f9e86ec23e5eada7e79dc4ddd18361


[ 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.