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

Cache object: 5d4660d9ff4a247e2fb99fc9d87dba07


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