[BACK]Return to locore.S CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / arch / i386 / i386

Annotation of src/sys/arch/i386/i386/locore.S, Revision 1.43

1.43    ! mrg         1: /*     $NetBSD: locore.S,v 1.42 2006/07/08 20:30:00 christos Exp $     */
1.1       fvdl        2:
                      3: /*-
1.32      mycroft     4:  * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
1.1       fvdl        5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Charles M. Hannum.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *        This product includes software developed by the NetBSD
                     21:  *        Foundation, Inc. and its contributors.
                     22:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     23:  *    contributors may be used to endorse or promote products derived
                     24:  *    from this software without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     27:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
                     37:  */
                     38:
                     39: /*-
                     40:  * Copyright (c) 1990 The Regents of the University of California.
                     41:  * All rights reserved.
                     42:  *
                     43:  * This code is derived from software contributed to Berkeley by
                     44:  * William Jolitz.
                     45:  *
                     46:  * Redistribution and use in source and binary forms, with or without
                     47:  * modification, are permitted provided that the following conditions
                     48:  * are met:
                     49:  * 1. Redistributions of source code must retain the above copyright
                     50:  *    notice, this list of conditions and the following disclaimer.
                     51:  * 2. Redistributions in binary form must reproduce the above copyright
                     52:  *    notice, this list of conditions and the following disclaimer in the
                     53:  *    documentation and/or other materials provided with the distribution.
1.12      agc        54:  * 3. Neither the name of the University nor the names of its contributors
1.1       fvdl       55:  *    may be used to endorse or promote products derived from this software
                     56:  *    without specific prior written permission.
                     57:  *
                     58:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     59:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     60:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     61:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     62:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     63:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     64:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     65:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     66:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     67:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     68:  * SUCH DAMAGE.
                     69:  *
                     70:  *     @(#)locore.s    7.3 (Berkeley) 5/13/91
                     71:  */
                     72:
1.18      christos   73: #include "opt_compat_netbsd.h"
                     74: #include "opt_compat_oldboot.h"
1.1       fvdl       75: #include "opt_cputype.h"
                     76: #include "opt_ddb.h"
                     77: #include "opt_ipkdb.h"
1.18      christos   78: #include "opt_lockdebug.h"
1.1       fvdl       79: #include "opt_multiprocessor.h"
                     80: #include "opt_realmem.h"
1.18      christos   81: #include "opt_user_ldt.h"
                     82: #include "opt_vm86.h"
1.1       fvdl       83:
                     84: #include "npx.h"
                     85: #include "assym.h"
                     86: #include "lapic.h"
                     87: #include "ioapic.h"
1.8       fvdl       88: #include "ksyms.h"
1.1       fvdl       89:
                     90: #include <sys/errno.h>
                     91: #include <sys/syscall.h>
                     92:
                     93: #include <machine/cputypes.h>
                     94: #include <machine/segments.h>
                     95: #include <machine/specialreg.h>
                     96: #include <machine/trap.h>
                     97:
                     98: #if NLAPIC > 0
                     99: #include <machine/i82489reg.h>
                    100: #endif
                    101:
1.37      jmmv      102: #include <machine/multiboot.h>
                    103:
1.1       fvdl      104: /* LINTSTUB: include <sys/types.h> */
                    105: /* LINTSTUB: include <machine/cpu.h> */
                    106: /* LINTSTUB: include <sys/systm.h> */
                    107:
                    108: #include <machine/asm.h>
                    109:
                    110: #if defined(MULTIPROCESSOR)
1.30      junyoung  111:
1.5       thorpej   112: #define SET_CURLWP(lwp,cpu)                            \
1.1       fvdl      113:        movl    CPUVAR(SELF),cpu                ;       \
1.5       thorpej   114:        movl    lwp,CPUVAR(CURLWP)      ;       \
                    115:        movl    cpu,L_CPU(lwp)
1.30      junyoung  116:
1.1       fvdl      117: #else
                    118:
1.5       thorpej   119: #define SET_CURLWP(lwp,tcpu)           movl    lwp,CPUVAR(CURLWP)
                    120: #define GET_CURLWP(reg)                        movl    CPUVAR(CURLWP),reg
1.1       fvdl      121:
                    122: #endif
                    123:
                    124: #define SET_CURPCB(reg)                        movl    reg,CPUVAR(CURPCB)
1.24      yamt      125:
1.1       fvdl      126: #define CLEAR_RESCHED(reg)             movl    reg,CPUVAR(RESCHED)
                    127:
                    128: /* XXX temporary kluge; these should not be here */
                    129: /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
                    130: #include <dev/isa/isareg.h>
                    131:
                    132:
                    133: /* Disallow old names for REALBASEMEM */
                    134: #ifdef BIOSBASEMEM
                    135: #error BIOSBASEMEM option deprecated; use REALBASEMEM only if memory size reported by latest boot block is incorrect
                    136: #endif
                    137:
                    138: /* Disallow old names for REALEXTMEM */
                    139: #ifdef EXTMEM_SIZE
                    140: #error EXTMEM_SIZE option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
                    141: #endif
                    142: #ifdef BIOSEXTMEM
                    143: #error BIOSEXTMEM option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
                    144: #endif
                    145:
                    146: #include <machine/frameasm.h>
                    147:
                    148:
                    149: #ifdef MULTIPROCESSOR
                    150: #include <machine/i82489reg.h>
                    151: #endif
1.30      junyoung  152:
1.1       fvdl      153: /*
                    154:  * Initialization
                    155:  */
                    156:        .data
                    157:
                    158:        .globl  _C_LABEL(cpu)
1.38      jmmv      159:        .globl  _C_LABEL(esym)
                    160:        .globl  _C_LABEL(atdevbase)
1.40      yamt      161:        .globl  _C_LABEL(proc0uarea),_C_LABEL(PDPpaddr)
1.1       fvdl      162:        .globl  _C_LABEL(gdt)
                    163: #ifdef I586_CPU
                    164:        .globl  _C_LABEL(idt)
                    165: #endif
1.30      junyoung  166:        .globl  _C_LABEL(lapic_tpr)
                    167:
1.1       fvdl      168: #if NLAPIC > 0
                    169: #ifdef __ELF__
1.7       thorpej   170:        .align  PAGE_SIZE
1.1       fvdl      171: #else
                    172:        .align  12
                    173: #endif
                    174:        .globl _C_LABEL(local_apic), _C_LABEL(lapic_id)
                    175: _C_LABEL(local_apic):
                    176:        .space  LAPIC_ID
1.30      junyoung  177: _C_LABEL(lapic_id):
1.1       fvdl      178:        .long   0x00000000
                    179:        .space  LAPIC_TPRI-(LAPIC_ID+4)
1.30      junyoung  180: _C_LABEL(lapic_tpr):
1.1       fvdl      181:        .space  LAPIC_PPRI-LAPIC_TPRI
1.30      junyoung  182: _C_LABEL(lapic_ppr):
1.1       fvdl      183:        .space  LAPIC_ISR-LAPIC_PPRI
                    184: _C_LABEL(lapic_isr):
1.7       thorpej   185:        .space  PAGE_SIZE-LAPIC_ISR
1.1       fvdl      186: #else
1.30      junyoung  187: _C_LABEL(lapic_tpr):
1.1       fvdl      188:        .long 0
                    189: #endif
1.30      junyoung  190:
1.1       fvdl      191:
                    192: _C_LABEL(cpu):         .long   0       # are we 386, 386sx, or 486,
                    193:                                        #   or Pentium, or..
                    194: _C_LABEL(atdevbase):   .long   0       # location of start of iomem in virtual
1.40      yamt      195: _C_LABEL(proc0uarea):  .long   0
1.31      junyoung  196: _C_LABEL(PDPpaddr):    .long   0       # paddr of PDP, for libkvm
1.30      junyoung  197:
1.1       fvdl      198:        .space 512
                    199: tmpstk:
                    200:
                    201:
1.35      yamt      202: #define        _RELOC(x)       ((x) - KERNBASE)
1.1       fvdl      203: #define        RELOC(x)        _RELOC(_C_LABEL(x))
                    204:
                    205:        .text
                    206:        .globl  _C_LABEL(kernel_text)
                    207:        .set    _C_LABEL(kernel_text),KERNTEXTOFF
                    208:
                    209:        .globl  start
                    210: start: movw    $0x1234,0x472                   # warm boot
                    211:
1.37      jmmv      212: #if defined(MULTIBOOT)
                    213:        jmp     1f
                    214:
                    215:        .align  4
                    216:        .globl  Multiboot_Header
                    217: _C_LABEL(Multiboot_Header):
                    218: #define MULTIBOOT_HEADER_FLAGS (MULTIBOOT_HEADER_WANT_MEMORY | \
                    219:                                 MULTIBOOT_HEADER_HAS_ADDR)
                    220:        .long   MULTIBOOT_HEADER_MAGIC
                    221:        .long   MULTIBOOT_HEADER_FLAGS
                    222:        .long   -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
                    223:        .long   RELOC(Multiboot_Header)
                    224:        .long   RELOC(start)
                    225:        .long   RELOC(_edata)
                    226:        .long   RELOC(_end) + MULTIBOOT_SYMTAB_SPACE
                    227:        .long   RELOC(start)
                    228:
                    229: 1:
                    230:        /* Check if we are being executed by a Multiboot-compliant boot
                    231:         * loader. */
                    232:        cmpl    $MULTIBOOT_INFO_MAGIC,%eax
                    233:        jne     1f
                    234:
1.43    ! mrg       235:        /*
        !           236:         * Indeed, a multiboot-compliant boot loader executed us.  We copy
1.37      jmmv      237:         * the received Multiboot information structure into kernel's data
                    238:         * space to process it later -- after we are relocated.  It will
1.43    ! mrg       239:         * be safer to run complex C code than doing it at this point.
        !           240:         */
1.37      jmmv      241:        pushl   %ebx            # Address of Multiboot information
                    242:        call    _C_LABEL(multiboot_pre_reloc)
                    243:        addl    $4,%esp
1.38      jmmv      244:        jmp     2f
1.37      jmmv      245: #endif
                    246:
                    247: 1:
1.1       fvdl      248:        /*
1.38      jmmv      249:         * At this point, we know that a NetBSD-specific boot loader
                    250:         * booted this kernel.  The stack carries the following parameters:
1.41      jmmv      251:         * (boothowto, [bootdev], bootinfo, esym, biosextmem, biosbasemem),
1.38      jmmv      252:         * 4 bytes each.
1.1       fvdl      253:         */
1.38      jmmv      254:        addl    $4,%esp         # Discard return address to boot loader
                    255:        call    _C_LABEL(native_loader)
                    256:        addl    $24,%esp
1.1       fvdl      257:
                    258: 2:
                    259:        /* First, reset the PSL. */
                    260:        pushl   $PSL_MBO
                    261:        popfl
                    262:
                    263:        /* Clear segment registers; always null in proc0. */
                    264:        xorl    %eax,%eax
                    265:        movw    %ax,%fs
                    266:        movw    %ax,%gs
                    267:        decl    %eax
                    268:        movl    %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
                    269:
                    270:        /* Find out our CPU type. */
                    271:
                    272: try386:        /* Try to toggle alignment check flag; does not exist on 386. */
                    273:        pushfl
                    274:        popl    %eax
                    275:        movl    %eax,%ecx
                    276:        orl     $PSL_AC,%eax
                    277:        pushl   %eax
                    278:        popfl
                    279:        pushfl
                    280:        popl    %eax
                    281:        xorl    %ecx,%eax
                    282:        andl    $PSL_AC,%eax
                    283:        pushl   %ecx
                    284:        popfl
                    285:
                    286:        testl   %eax,%eax
                    287:        jnz     try486
                    288:
                    289:        /*
                    290:         * Try the test of a NexGen CPU -- ZF will not change on a DIV
                    291:         * instruction on a NexGen, it will on an i386.  Documented in
                    292:         * Nx586 Processor Recognition Application Note, NexGen, Inc.
                    293:         */
                    294:        movl    $0x5555,%eax
                    295:        xorl    %edx,%edx
                    296:        movl    $2,%ecx
                    297:        divl    %ecx
                    298:        jnz     is386
                    299:
                    300: isnx586:
                    301:        /*
                    302:         * Don't try cpuid, as Nx586s reportedly don't support the
                    303:         * PSL_ID bit.
                    304:         */
                    305:        movl    $CPU_NX586,RELOC(cpu)
                    306:        jmp     2f
                    307:
                    308: is386:
                    309:        movl    $CPU_386,RELOC(cpu)
                    310:        jmp     2f
                    311:
                    312: try486:        /* Try to toggle identification flag; does not exist on early 486s. */
                    313:        pushfl
                    314:        popl    %eax
                    315:        movl    %eax,%ecx
                    316:        xorl    $PSL_ID,%eax
                    317:        pushl   %eax
                    318:        popfl
                    319:        pushfl
                    320:        popl    %eax
                    321:        xorl    %ecx,%eax
                    322:        andl    $PSL_ID,%eax
                    323:        pushl   %ecx
                    324:        popfl
                    325:
                    326:        testl   %eax,%eax
                    327:        jnz     try586
                    328: is486: movl    $CPU_486,RELOC(cpu)
                    329:        /*
                    330:         * Check Cyrix CPU
                    331:         * Cyrix CPUs do not change the undefined flags following
                    332:         * execution of the divide instruction which divides 5 by 2.
                    333:         *
                    334:         * Note: CPUID is enabled on M2, so it passes another way.
                    335:         */
                    336:        pushfl
                    337:        movl    $0x5555, %eax
                    338:        xorl    %edx, %edx
                    339:        movl    $2, %ecx
                    340:        clc
                    341:        divl    %ecx
                    342:        jnc     trycyrix486
                    343:        popfl
                    344:        jmp 2f
                    345: trycyrix486:
                    346:        movl    $CPU_6x86,RELOC(cpu)    # set CPU type
                    347:        /*
                    348:         * Check for Cyrix 486 CPU by seeing if the flags change during a
                    349:         * divide. This is documented in the Cx486SLC/e SMM Programmer's
                    350:         * Guide.
                    351:         */
                    352:        xorl    %edx,%edx
                    353:        cmpl    %edx,%edx               # set flags to known state
                    354:        pushfl
                    355:        popl    %ecx                    # store flags in ecx
                    356:        movl    $-1,%eax
                    357:        movl    $4,%ebx
                    358:        divl    %ebx                    # do a long division
                    359:        pushfl
                    360:        popl    %eax
                    361:        xorl    %ecx,%eax               # are the flags different?
                    362:        testl   $0x8d5,%eax             # only check C|PF|AF|Z|N|V
                    363:        jne     2f                      # yes; must be Cyrix 6x86 CPU
                    364:        movl    $CPU_486DLC,RELOC(cpu)  # set CPU type
                    365:
                    366: #ifndef CYRIX_CACHE_WORKS
                    367:        /* Disable caching of the ISA hole only. */
                    368:        invd
                    369:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    370:        outb    %al,$0x22
                    371:        inb     $0x23,%al
                    372:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    373:        movb    %al,%ah
                    374:        movb    $CCR0,%al
                    375:        outb    %al,$0x22
                    376:        movb    %ah,%al
                    377:        outb    %al,$0x23
                    378:        invd
                    379: #else /* CYRIX_CACHE_WORKS */
                    380:        /* Set cache parameters */
                    381:        invd                            # Start with guaranteed clean cache
                    382:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    383:        outb    %al,$0x22
                    384:        inb     $0x23,%al
                    385:        andb    $~CCR0_NC0,%al
                    386: #ifndef CYRIX_CACHE_REALLY_WORKS
                    387:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    388: #else
                    389:        orb     $CCR0_NC1,%al
                    390: #endif
                    391:        movb    %al,%ah
                    392:        movb    $CCR0,%al
                    393:        outb    %al,$0x22
                    394:        movb    %ah,%al
                    395:        outb    %al,$0x23
                    396:        /* clear non-cacheable region 1 */
                    397:        movb    $(NCR1+2),%al
                    398:        outb    %al,$0x22
                    399:        movb    $NCR_SIZE_0K,%al
                    400:        outb    %al,$0x23
                    401:        /* clear non-cacheable region 2 */
                    402:        movb    $(NCR2+2),%al
                    403:        outb    %al,$0x22
                    404:        movb    $NCR_SIZE_0K,%al
                    405:        outb    %al,$0x23
                    406:        /* clear non-cacheable region 3 */
                    407:        movb    $(NCR3+2),%al
                    408:        outb    %al,$0x22
                    409:        movb    $NCR_SIZE_0K,%al
                    410:        outb    %al,$0x23
                    411:        /* clear non-cacheable region 4 */
                    412:        movb    $(NCR4+2),%al
                    413:        outb    %al,$0x22
                    414:        movb    $NCR_SIZE_0K,%al
                    415:        outb    %al,$0x23
                    416:        /* enable caching in CR0 */
                    417:        movl    %cr0,%eax
                    418:        andl    $~(CR0_CD|CR0_NW),%eax
                    419:        movl    %eax,%cr0
                    420:        invd
                    421: #endif /* CYRIX_CACHE_WORKS */
                    422:
                    423:        jmp     2f
                    424:
                    425: try586:        /* Use the `cpuid' instruction. */
                    426:        xorl    %eax,%eax
                    427:        cpuid
                    428:        movl    %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
                    429:
                    430: 2:
                    431:        /*
                    432:         * Finished with old stack; load new %esp now instead of later so we
                    433:         * can trace this code without having to worry about the trace trap
                    434:         * clobbering the memory test or the zeroing of the bss+bootstrap page
                    435:         * tables.
                    436:         *
                    437:         * The boot program should check:
                    438:         *      text+data <= &stack_variable - more_space_for_stack
                    439:         *      text+data+bss+pad+space_for_page_tables <= end_of_memory
                    440:         * Oops, the gdt is in the carcass of the boot program so clearing
                    441:         * the rest of memory is still not possible.
                    442:         */
                    443:        movl    $_RELOC(tmpstk),%esp    # bootstrap stack end location
                    444:
                    445: /*
                    446:  * Virtual address space of kernel:
                    447:  *
1.30      junyoung  448:  * text | data | bss | [syms] | page dir | proc0 kstack
1.1       fvdl      449:  *                           0          1       2      3
                    450:  */
1.7       thorpej   451: #define        PROC0PDIR       ((0)              * PAGE_SIZE)
                    452: #define        PROC0STACK      ((1)              * PAGE_SIZE)
                    453: #define        SYSMAP          ((1+UPAGES)       * PAGE_SIZE)
                    454: #define        TABLESIZE       ((1+UPAGES) * PAGE_SIZE) /* + nkpde * PAGE_SIZE */
1.1       fvdl      455:
                    456:        /* Find end of kernel image. */
                    457:        movl    $RELOC(end),%edi
1.8       fvdl      458: #if (NKSYMS || defined(DDB) || defined(LKM)) && !defined(SYMTAB_SPACE)
1.1       fvdl      459:        /* Save the symbols (if loaded). */
                    460:        movl    RELOC(esym),%eax
                    461:        testl   %eax,%eax
                    462:        jz      1f
1.35      yamt      463:        subl    $KERNBASE,%eax
1.1       fvdl      464:        movl    %eax,%edi
                    465: 1:
                    466: #endif
                    467:
                    468:        /* Calculate where to start the bootstrap tables. */
                    469:        movl    %edi,%esi                       # edi = esym ? esym : end
                    470:        addl    $PGOFSET,%esi                   # page align up
                    471:        andl    $~PGOFSET,%esi
                    472:
                    473:        /*
                    474:         * Calculate the size of the kernel page table directory, and
1.29      mycroft   475:         * how many entries it will have.  Adjust nkpde to the actual
                    476:         * kernel size automatically.  Account for the bootstrap tables,
                    477:         * round up, and add an extra 4MB.
1.1       fvdl      478:         */
1.29      mycroft   479:        leal    TABLESIZE+NBPD+PDOFSET(%edi),%eax
                    480:        shrl    $PDSHIFT,%eax
1.1       fvdl      481:        movl    RELOC(nkpde),%ecx               # get nkpde
1.29      mycroft   482:        cmpl    %ecx,%eax
                    483:        jb      1f
                    484:        movl    %eax,%ecx
                    485: 1:     cmpl    $NKPTP_MIN,%ecx                 # larger than min?
1.1       fvdl      486:        jge     1f
                    487:        movl    $NKPTP_MIN,%ecx                 # set at min
                    488:        jmp     2f
                    489: 1:     cmpl    $NKPTP_MAX,%ecx                 # larger than max?
                    490:        jle     2f
                    491:        movl    $NKPTP_MAX,%ecx
1.29      mycroft   492: 2:     movl    %ecx,RELOC(nkpde)
1.1       fvdl      493:
                    494:        /* Clear memory for bootstrap tables. */
                    495:        shll    $PGSHIFT,%ecx
                    496:        addl    $TABLESIZE,%ecx
                    497:        addl    %esi,%ecx                       # end of tables
                    498:        subl    %edi,%ecx                       # size of tables
                    499:        shrl    $2,%ecx
                    500:        xorl    %eax,%eax
                    501:        cld
                    502:        rep
                    503:        stosl
                    504:
                    505: /*
                    506:  * fillkpt
                    507:  *     eax = pte (page frame | control | status)
                    508:  *     ebx = page table address
                    509:  *     ecx = number of pages to map
                    510:  */
                    511: #define        fillkpt         \
                    512: 1:     movl    %eax,(%ebx)     ; \
1.7       thorpej   513:        addl    $PAGE_SIZE,%eax ; /* increment physical address */ \
1.1       fvdl      514:        addl    $4,%ebx         ; /* next pte */ \
                    515:        loop    1b              ;
                    516:
                    517: /*
                    518:  * Build initial page tables.
                    519:  */
                    520:        /* Calculate end of text segment, rounded to a page. */
                    521:        leal    (RELOC(etext)+PGOFSET),%edx
                    522:        andl    $~PGOFSET,%edx
1.30      junyoung  523:
1.1       fvdl      524:        /* Skip over the first 1MB. */
                    525:        movl    $_RELOC(KERNTEXTOFF),%eax
                    526:        movl    %eax,%ecx
                    527:        shrl    $PGSHIFT,%ecx
                    528:        leal    (SYSMAP)(%esi,%ecx,4),%ebx
                    529:
                    530:        /* Map the kernel text read-only. */
                    531:        movl    %edx,%ecx
                    532:        subl    %eax,%ecx
                    533:        shrl    $PGSHIFT,%ecx
                    534:        orl     $(PG_V|PG_KR),%eax
                    535:        fillkpt
                    536:
                    537:        /* Map the data, BSS, and bootstrap tables read-write. */
                    538:        leal    (PG_V|PG_KW)(%edx),%eax
                    539:        movl    RELOC(nkpde),%ecx
                    540:        shll    $PGSHIFT,%ecx
                    541:        addl    $TABLESIZE,%ecx
                    542:        addl    %esi,%ecx                               # end of tables
                    543:        subl    %edx,%ecx                               # subtract end of text
                    544:        shrl    $PGSHIFT,%ecx
                    545:        fillkpt
                    546:
                    547:        /* Map ISA I/O memory. */
                    548:        movl    $(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax   # having these bits set
                    549:        movl    $(IOM_SIZE>>PGSHIFT),%ecx               # for this many pte s,
                    550:        fillkpt
                    551:
                    552: /*
                    553:  * Construct a page table directory.
                    554:  */
                    555:        /* Install PDEs for temporary double map of kernel. */
                    556:        movl    RELOC(nkpde),%ecx                       # for this many pde s,
                    557:        leal    (PROC0PDIR+0*4)(%esi),%ebx              # which is where temp maps!
                    558:        leal    (SYSMAP+PG_V|PG_KW)(%esi),%eax          # pte for KPT in proc 0,
                    559:        fillkpt
                    560:
                    561:        /* Map kernel PDEs. */
                    562:        movl    RELOC(nkpde),%ecx                       # for this many pde s,
                    563:        leal    (PROC0PDIR+PDSLOT_KERN*4)(%esi),%ebx    # kernel pde offset
                    564:        leal    (SYSMAP+PG_V|PG_KW)(%esi),%eax          # pte for KPT in proc 0,
                    565:        fillkpt
                    566:
                    567:        /* Install a PDE recursively mapping page directory as a page table! */
                    568:        leal    (PROC0PDIR+PG_V|PG_KW)(%esi),%eax       # pte for ptd
                    569:        movl    %eax,(PROC0PDIR+PDSLOT_PTE*4)(%esi)     # recursive PD slot
                    570:
1.31      junyoung  571:        /* Save phys. addr of PDP, for libkvm. */
                    572:        movl    %esi,RELOC(PDPpaddr)
1.1       fvdl      573:
                    574:        /* Load base of page directory and enable mapping. */
                    575:        movl    %esi,%eax               # phys address of ptd in proc 0
                    576:        movl    %eax,%cr3               # load ptd addr into mmu
                    577:        movl    %cr0,%eax               # get control word
                    578:                                        # enable paging & NPX emulation
                    579:        orl     $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_EM|CR0_MP),%eax
                    580:        movl    %eax,%cr0               # and let's page NOW!
                    581:
                    582:        pushl   $begin                  # jump to high mem
                    583:        ret
                    584:
                    585: begin:
1.35      yamt      586:        /* Now running relocated at KERNBASE.  Remove double mapping. */
1.1       fvdl      587:        movl    _C_LABEL(nkpde),%ecx            # for this many pde s,
                    588:        leal    (PROC0PDIR+0*4)(%esi),%ebx      # which is where temp maps!
1.35      yamt      589:        addl    $(KERNBASE), %ebx       # now use relocated address
1.1       fvdl      590: 1:     movl    $0,(%ebx)
                    591:        addl    $4,%ebx # next pde
                    592:        loop    1b
                    593:
                    594:        /* Relocate atdevbase. */
                    595:        movl    _C_LABEL(nkpde),%edx
                    596:        shll    $PGSHIFT,%edx
1.35      yamt      597:        addl    $(TABLESIZE+KERNBASE),%edx
1.1       fvdl      598:        addl    %esi,%edx
                    599:        movl    %edx,_C_LABEL(atdevbase)
                    600:
                    601:        /* Set up bootstrap stack. */
1.35      yamt      602:        leal    (PROC0STACK+KERNBASE)(%esi),%eax
1.40      yamt      603:        movl    %eax,_C_LABEL(proc0uarea)
                    604:        leal    (KSTACK_SIZE-FRAMESIZE)(%eax),%esp
                    605:        movl    %esi,(KSTACK_SIZE+PCB_CR3)(%eax)        # pcb->pcb_cr3
1.1       fvdl      606:        xorl    %ebp,%ebp               # mark end of frames
                    607:
1.37      jmmv      608: #if defined(MULTIBOOT)
                    609:        /* It is now safe to parse the Multiboot information structure
                    610:         * we saved before from C code.  Note that we cannot delay its
                    611:         * parsing any more because initgdt (called below) needs to make
                    612:         * use of this information. */
                    613:        call    _C_LABEL(multiboot_post_reloc)
                    614: #endif
                    615:
1.1       fvdl      616:        subl    $NGDT*8, %esp           # space for temporary gdt
                    617:        pushl   %esp
                    618:        call    _C_LABEL(initgdt)
                    619:        addl    $4,%esp
1.30      junyoung  620:
1.1       fvdl      621:        movl    _C_LABEL(nkpde),%eax
                    622:        shll    $PGSHIFT,%eax
                    623:        addl    $TABLESIZE,%eax
                    624:        addl    %esi,%eax               # skip past stack and page tables
                    625:
                    626:        pushl   %eax
                    627:        call    _C_LABEL(init386)       # wire 386 chip for unix operation
                    628:        addl    $4+NGDT*8,%esp          # pop temporary gdt
                    629:
                    630: #ifdef SAFARI_FIFO_HACK
                    631:        movb    $5,%al
                    632:        movw    $0x37b,%dx
                    633:        outb    %al,%dx
                    634:        movw    $0x37f,%dx
                    635:        inb     %dx,%al
                    636:        movb    %al,%cl
                    637:
                    638:        orb     $1,%cl
                    639:
                    640:        movb    $5,%al
                    641:        movw    $0x37b,%dx
                    642:        outb    %al,%dx
                    643:        movw    $0x37f,%dx
                    644:        movb    %cl,%al
                    645:        outb    %al,%dx
                    646: #endif /* SAFARI_FIFO_HACK */
                    647:
                    648:        call    _C_LABEL(main)
                    649:
                    650: /*
                    651:  * void proc_trampoline(void);
                    652:  * This is a trampoline function pushed onto the stack of a newly created
                    653:  * process in order to do some additional setup.  The trampoline is entered by
                    654:  * cpu_switch()ing to the process, so we abuse the callee-saved registers used
                    655:  * by cpu_switch() to store the information about the stub to call.
                    656:  * NOTE: This function does not have a normal calling sequence!
                    657:  */
                    658: /* LINTSTUB: Func: void proc_trampoline(void) */
                    659: NENTRY(proc_trampoline)
                    660: #ifdef MULTIPROCESSOR
                    661:        call    _C_LABEL(proc_trampoline_mp)
                    662: #endif
                    663:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                    664:        pushl   %ebx
                    665:        call    *%esi
                    666:        addl    $4,%esp
1.24      yamt      667:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl      668:        INTRFASTEXIT
                    669:        /* NOTREACHED */
                    670:
                    671: /*****************************************************************************/
1.16      christos  672: #ifdef COMPAT_16
1.1       fvdl      673: /*
                    674:  * Signal trampoline; copied to top of user stack.
                    675:  */
                    676: /* LINTSTUB: Var: char sigcode[1], esigcode[1]; */
                    677: NENTRY(sigcode)
                    678:        /*
                    679:         * Handler has returned here as if we called it.  The sigcontext
                    680:         * is on the stack after the 3 args "we" pushed.
                    681:         */
                    682:        leal    12(%esp),%eax           # get pointer to sigcontext
                    683:        movl    %eax,4(%esp)            # put it in the argument slot
                    684:                                        # fake return address already there
1.17      christos  685:        movl    $SYS_compat_16___sigreturn14,%eax
1.1       fvdl      686:        int     $0x80                   # enter kernel with args on stack
                    687:        movl    $SYS_exit,%eax
                    688:        int     $0x80                   # exit if sigreturn fails
                    689:        .globl  _C_LABEL(esigcode)
                    690: _C_LABEL(esigcode):
1.16      christos  691: #endif
1.1       fvdl      692:
                    693: /*****************************************************************************/
                    694:
                    695: /*
                    696:  * The following is i386-specific nonsense.
                    697:  */
                    698:
                    699: /*
                    700:  * void lgdt(struct region_descriptor *rdp);
                    701:  * Load a new GDT pointer (and do any necessary cleanup).
                    702:  * XXX It's somewhat questionable whether reloading all the segment registers
                    703:  * is necessary, since the actual descriptor data is not changed except by
                    704:  * process creation and exit, both of which clean up via task switches.  OTOH,
                    705:  * this only happens at run time when the GDT is resized.
                    706:  */
                    707: /* LINTSTUB: Func: void lgdt(struct region_descriptor *rdp) */
                    708: NENTRY(lgdt)
                    709:        /* Reload the descriptor table. */
                    710:        movl    4(%esp),%eax
                    711:        lgdt    (%eax)
                    712:        /* Flush the prefetch queue. */
                    713:        jmp     1f
                    714:        nop
                    715: 1:     /* Reload "stale" selectors. */
                    716:        movl    $GSEL(GDATA_SEL, SEL_KPL),%eax
                    717:        movw    %ax,%ds
                    718:        movw    %ax,%es
                    719:        movw    %ax,%gs
                    720:        movw    %ax,%ss
                    721:        movl    $GSEL(GCPU_SEL, SEL_KPL),%eax
                    722:        movw    %ax,%fs
                    723:        /* Reload code selector by doing intersegment return. */
                    724:        popl    %eax
                    725:        pushl   $GSEL(GCODE_SEL, SEL_KPL)
                    726:        pushl   %eax
                    727:        lret
                    728:
                    729: /*****************************************************************************/
                    730:
                    731: /*
                    732:  * These functions are primarily used by DDB.
                    733:  */
                    734:
                    735: /* LINTSTUB: Func: int setjmp (label_t *l) */
                    736: ENTRY(setjmp)
                    737:        movl    4(%esp),%eax
                    738:        movl    %ebx,(%eax)             # save ebx
                    739:        movl    %esp,4(%eax)            # save esp
                    740:        movl    %ebp,8(%eax)            # save ebp
                    741:        movl    %esi,12(%eax)           # save esi
                    742:        movl    %edi,16(%eax)           # save edi
                    743:        movl    (%esp),%edx             # get rta
                    744:        movl    %edx,20(%eax)           # save eip
                    745:        xorl    %eax,%eax               # return (0);
                    746:        ret
                    747:
                    748: /* LINTSTUB: Func: void longjmp (label_t *l) */
                    749: ENTRY(longjmp)
                    750:        movl    4(%esp),%eax
                    751:        movl    (%eax),%ebx             # restore ebx
                    752:        movl    4(%eax),%esp            # restore esp
                    753:        movl    8(%eax),%ebp            # restore ebp
                    754:        movl    12(%eax),%esi           # restore esi
                    755:        movl    16(%eax),%edi           # restore edi
                    756:        movl    20(%eax),%edx           # get rta
                    757:        movl    %edx,(%esp)             # put in return frame
                    758:        xorl    %eax,%eax               # return (1);
                    759:        incl    %eax
                    760:        ret
                    761:
                    762: /*****************************************************************************/
                    763:
                    764:        .globl  _C_LABEL(sched_whichqs),_C_LABEL(sched_qs)
                    765:        .globl  _C_LABEL(uvmexp),_C_LABEL(panic)
                    766:
                    767: #ifdef DIAGNOSTIC
                    768: NENTRY(switch_error)
                    769:        pushl   $1f
                    770:        call    _C_LABEL(panic)
                    771:        /* NOTREACHED */
                    772: 1:     .asciz  "cpu_switch"
                    773: #endif /* DIAGNOSTIC */
                    774:
                    775: /*
1.5       thorpej   776:  * void cpu_switch(struct lwp *)
1.39      skrll     777:  * Find a runnable lwp and switch to it.  Wait if necessary.  If the new
                    778:  * lwp is the same as the old one, we short-circuit the context save and
1.1       fvdl      779:  * restore.
1.30      junyoung  780:  *
1.1       fvdl      781:  * Note that the stack frame layout is known to "struct switchframe"
1.30      junyoung  782:  * in <machine/frame.h> and to the code in cpu_fork() which initializes
1.5       thorpej   783:  * it for a new lwp.
1.1       fvdl      784:  */
                    785: ENTRY(cpu_switch)
                    786:        pushl   %ebx
                    787:        pushl   %esi
                    788:        pushl   %edi
                    789:
                    790: #ifdef DEBUG
                    791:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                    792:        jae     1f
1.5       thorpej   793:        pushl   $2f
1.1       fvdl      794:        call    _C_LABEL(panic)
                    795:        /* NOTREACHED */
1.5       thorpej   796: 2:     .asciz  "not splsched() in cpu_switch!"
1.30      junyoung  797: 1:
1.1       fvdl      798: #endif /* DEBUG */
1.30      junyoung  799:
1.5       thorpej   800:        movl    16(%esp),%esi           # current
1.1       fvdl      801:
                    802:        /*
1.5       thorpej   803:         * Clear curlwp so that we don't accumulate system time while idle.
                    804:         * This also insures that schedcpu() will move the old lwp to
1.1       fvdl      805:         * the correct queue if it happens to get called from the spllower()
                    806:         * below and changes the priority.  (See corresponding comment in
                    807:         * userret()).
                    808:         */
1.5       thorpej   809:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl      810:        /*
1.5       thorpej   811:         * First phase: find new lwp.
1.1       fvdl      812:         *
                    813:         * Registers:
                    814:         *   %eax - queue head, scratch, then zero
                    815:         *   %ebx - queue number
                    816:         *   %ecx - cached value of whichqs
1.5       thorpej   817:         *   %edx - next lwp in queue
                    818:         *   %esi - old lwp
                    819:         *   %edi - new lwp
1.1       fvdl      820:         */
                    821:
1.5       thorpej   822:        /* Look for new lwp. */
1.1       fvdl      823:        cli                             # splhigh doesn't do a cli
                    824:        movl    _C_LABEL(sched_whichqs),%ecx
                    825:        bsfl    %ecx,%ebx               # find a full q
                    826:        jnz     switch_dequeue
                    827:
                    828:        /*
                    829:         * idling:      save old context.
                    830:         *
                    831:         * Registers:
                    832:         *   %eax, %ecx - scratch
1.5       thorpej   833:         *   %esi - old lwp, then old pcb
1.1       fvdl      834:         *   %edi - idle pcb
                    835:         */
                    836:
                    837:        pushl   %esi
1.24      yamt      838:        call    _C_LABEL(pmap_deactivate2)      # pmap_deactivate(oldproc)
1.1       fvdl      839:        addl    $4,%esp
                    840:
1.5       thorpej   841:        movl    L_ADDR(%esi),%esi
1.1       fvdl      842:
                    843:        /* Save stack pointers. */
                    844:        movl    %esp,PCB_ESP(%esi)
                    845:        movl    %ebp,PCB_EBP(%esi)
                    846:
                    847:        /* Find idle PCB for this CPU */
                    848: #ifndef MULTIPROCESSOR
1.5       thorpej   849:        movl    $_C_LABEL(lwp0),%ebx
                    850:        movl    L_ADDR(%ebx),%edi
                    851:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl      852: #else
                    853:        movl    CPUVAR(IDLE_PCB),%edi
                    854:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                    855: #endif
1.5       thorpej   856:        movl    $0,CPUVAR(CURLWP)               /* In case we fault... */
1.1       fvdl      857:
                    858:        /* Restore the idle context (avoid interrupts) */
                    859:        cli
                    860:
                    861:        /* Restore stack pointers. */
                    862:        movl    PCB_ESP(%edi),%esp
                    863:        movl    PCB_EBP(%edi),%ebp
                    864:
                    865:        /* Switch TSS. Reset "task busy" flag before loading. */
1.26      yamt      866:        movl    %cr3,%eax
                    867:        movl    %eax,PCB_CR3(%edi)
1.1       fvdl      868: #ifdef MULTIPROCESSOR
                    869:        movl    CPUVAR(GDT),%eax
                    870: #else
                    871:        movl    _C_LABEL(gdt),%eax
                    872: #endif
                    873:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                    874:        ltr     %dx
                    875:
                    876:        /* We're always in the kernel, so we don't need the LDT. */
                    877:
                    878:        /* Restore cr0 (including FPU state). */
                    879:        movl    PCB_CR0(%edi),%ecx
                    880:        movl    %ecx,%cr0
                    881:
                    882:        /* Record new pcb. */
                    883:        SET_CURPCB(%edi)
                    884:
                    885:        xorl    %esi,%esi
                    886:        sti
1.30      junyoung  887: idle_unlock:
                    888: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl      889:        call    _C_LABEL(sched_unlock_idle)
                    890: #endif
                    891:        /* Interrupts are okay again. */
1.2       fvdl      892:        pushl   $IPL_NONE               # spl0()
1.1       fvdl      893:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl      894:        addl    $4,%esp
1.1       fvdl      895:        jmp     idle_start
1.30      junyoung  896: idle_zero:
1.1       fvdl      897:        sti
                    898:        call    _C_LABEL(uvm_pageidlezero)
                    899:        cli
                    900:        cmpl    $0,_C_LABEL(sched_whichqs)
                    901:        jnz     idle_exit
                    902: idle_loop:
                    903:        /* Try to zero some pages. */
                    904:        movl    _C_LABEL(uvm)+UVM_PAGE_IDLE_ZERO,%ecx
                    905:        testl   %ecx,%ecx
                    906:        jnz     idle_zero
                    907:        sti
                    908:        hlt
                    909: NENTRY(mpidle)
1.30      junyoung  910: idle_start:
1.1       fvdl      911:        cli
                    912:        cmpl    $0,_C_LABEL(sched_whichqs)
                    913:        jz      idle_loop
1.30      junyoung  914: idle_exit:
1.1       fvdl      915:        movl    $IPL_HIGH,CPUVAR(ILEVEL)                # splhigh
1.14      fvdl      916:        sti
1.30      junyoung  917: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl      918:        call    _C_LABEL(sched_lock_idle)
                    919: #endif
                    920:        movl    _C_LABEL(sched_whichqs),%ecx
                    921:        bsfl    %ecx,%ebx
                    922:        jz      idle_unlock
                    923:
1.30      junyoung  924: switch_dequeue:
                    925:        /*
1.1       fvdl      926:         * we're running at splhigh(), but it's otherwise okay to take
1.30      junyoung  927:         * interrupts here.
1.1       fvdl      928:         */
                    929:        sti
                    930:        leal    _C_LABEL(sched_qs)(,%ebx,8),%eax # select q
                    931:
1.5       thorpej   932:        movl    L_FORW(%eax),%edi       # unlink from front of process q
1.1       fvdl      933: #ifdef DIAGNOSTIC
                    934:        cmpl    %edi,%eax               # linked to self (i.e. nothing queued)?
                    935:        je      _C_LABEL(switch_error)  # not possible
                    936: #endif /* DIAGNOSTIC */
1.5       thorpej   937:        movl    L_FORW(%edi),%edx
                    938:        movl    %edx,L_FORW(%eax)
                    939:        movl    %eax,L_BACK(%edx)
1.1       fvdl      940:
                    941:        cmpl    %edx,%eax               # q empty?
                    942:        jne     3f
                    943:
                    944:        btrl    %ebx,%ecx               # yes, clear to indicate empty
                    945:        movl    %ecx,_C_LABEL(sched_whichqs) # update q status
                    946:
                    947: 3:     /* We just did it. */
                    948:        xorl    %eax,%eax
                    949:        CLEAR_RESCHED(%eax)
                    950:
1.5       thorpej   951: switch_resume:
1.1       fvdl      952: #ifdef DIAGNOSTIC
1.5       thorpej   953:        cmpl    %eax,L_WCHAN(%edi)      # Waiting for something?
1.1       fvdl      954:        jne     _C_LABEL(switch_error)  # Yes; shouldn't be queued.
1.5       thorpej   955:        cmpb    $LSRUN,L_STAT(%edi)     # In run state?
1.1       fvdl      956:        jne     _C_LABEL(switch_error)  # No; shouldn't be queued.
                    957: #endif /* DIAGNOSTIC */
                    958:
1.5       thorpej   959:        /* Isolate lwp.  XXX Is this necessary? */
                    960:        movl    %eax,L_BACK(%edi)
1.1       fvdl      961:
1.5       thorpej   962:        /* Record new lwp. */
                    963:        movb    $LSONPROC,L_STAT(%edi)  # l->l_stat = LSONPROC
                    964:        SET_CURLWP(%edi,%ecx)
1.1       fvdl      965:
1.5       thorpej   966:        /* Skip context switch if same lwp. */
1.10      fvdl      967:        xorl    %ebx,%ebx
1.1       fvdl      968:        cmpl    %edi,%esi
                    969:        je      switch_return
                    970:
1.5       thorpej   971:        /* If old lwp exited, don't bother. */
1.1       fvdl      972:        testl   %esi,%esi
                    973:        jz      switch_exited
                    974:
                    975:        /*
                    976:         * Second phase: save old context.
                    977:         *
                    978:         * Registers:
                    979:         *   %eax, %ecx - scratch
1.5       thorpej   980:         *   %esi - old lwp, then old pcb
                    981:         *   %edi - new lwp
1.1       fvdl      982:         */
                    983:
                    984:        pushl   %esi
1.24      yamt      985:        call    _C_LABEL(pmap_deactivate2)      # pmap_deactivate(oldproc)
1.1       fvdl      986:        addl    $4,%esp
                    987:
1.5       thorpej   988:        movl    L_ADDR(%esi),%esi
1.1       fvdl      989:
                    990:        /* Save stack pointers. */
                    991:        movl    %esp,PCB_ESP(%esi)
                    992:        movl    %ebp,PCB_EBP(%esi)
                    993:
                    994: switch_exited:
                    995:        /*
                    996:         * Third phase: restore saved context.
                    997:         *
                    998:         * Registers:
                    999:         *   %eax, %ebx, %ecx, %edx - scratch
                   1000:         *   %esi - new pcb
1.5       thorpej  1001:         *   %edi - new lwp
1.1       fvdl     1002:         */
                   1003:
                   1004:        /* No interrupts while loading new state. */
                   1005:        cli
1.5       thorpej  1006:        movl    L_ADDR(%edi),%esi
1.1       fvdl     1007:
                   1008:        /* Restore stack pointers. */
                   1009:        movl    PCB_ESP(%esi),%esp
                   1010:        movl    PCB_EBP(%esi),%ebp
                   1011:
                   1012: #if 0
                   1013:        /* Don't bother with the rest if switching to a system process. */
1.5       thorpej  1014:        testl   $P_SYSTEM,L_FLAG(%edi); XXX NJWLWP lwp's don't have P_SYSTEM!
1.1       fvdl     1015:        jnz     switch_restored
                   1016: #endif
                   1017:
1.26      yamt     1018:        /* Switch TSS. Reset "task busy" flag before loading. */
                   1019:        movl    %cr3,%eax
                   1020:        movl    %eax,PCB_CR3(%esi) /* XXX should be done by pmap_activate? */
1.1       fvdl     1021: #ifdef MULTIPROCESSOR
                   1022:        movl    CPUVAR(GDT),%eax
1.30      junyoung 1023: #else
1.1       fvdl     1024:        /* Load TSS info. */
                   1025:        movl    _C_LABEL(gdt),%eax
                   1026: #endif
1.5       thorpej  1027:        movl    L_MD_TSS_SEL(%edi),%edx
1.1       fvdl     1028:
                   1029:        andl    $~0x0200,4(%eax,%edx, 1)
                   1030:        ltr     %dx
                   1031:
                   1032:        pushl   %edi
                   1033:        call    _C_LABEL(pmap_activate)         # pmap_activate(p)
                   1034:        addl    $4,%esp
                   1035:
                   1036: #if 0
                   1037: switch_restored:
                   1038: #endif
                   1039:        /* Restore cr0 (including FPU state). */
                   1040:        movl    PCB_CR0(%esi),%ecx
                   1041: #ifdef MULTIPROCESSOR
1.30      junyoung 1042:        /*
1.22      wiz      1043:         * If our floating point registers are on a different CPU,
1.1       fvdl     1044:         * clear CR0_TS so we'll trap rather than reuse bogus state.
                   1045:         */
                   1046:        movl    PCB_FPCPU(%esi),%ebx
                   1047:        cmpl    CPUVAR(SELF),%ebx
                   1048:        jz      1f
                   1049:        orl     $CR0_TS,%ecx
1.30      junyoung 1050: 1:
                   1051: #endif
1.1       fvdl     1052:        movl    %ecx,%cr0
                   1053:
                   1054:        /* Record new pcb. */
                   1055:        SET_CURPCB(%esi)
                   1056:
                   1057:        /* Interrupts are okay again. */
                   1058:        sti
                   1059:
                   1060: /*
                   1061:  *  Check for restartable atomic sequences (RAS)
                   1062:  */
1.5       thorpej  1063:        movl    CPUVAR(CURLWP),%edi
                   1064:        movl    L_PROC(%edi),%esi
1.20      dsl      1065:        cmpl    $0,P_RASLIST(%esi)
                   1066:        jne     2f
1.1       fvdl     1067: 1:
1.10      fvdl     1068:        movl    $1,%ebx
1.1       fvdl     1069:
                   1070: switch_return:
1.30      junyoung 1071: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl     1072:        call    _C_LABEL(sched_unlock_idle)
                   1073: #endif
1.28      yamt     1074:        cmpl    $0,CPUVAR(IPENDING)
                   1075:        jz      3f
1.2       fvdl     1076:        pushl   $IPL_NONE               # spl0()
1.1       fvdl     1077:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl     1078:        addl    $4,%esp
1.28      yamt     1079: 3:
1.1       fvdl     1080:        movl    $IPL_HIGH,CPUVAR(ILEVEL)        # splhigh()
1.9       fvdl     1081:
                   1082:        movl    %ebx,%eax
1.30      junyoung 1083:
1.1       fvdl     1084:        popl    %edi
                   1085:        popl    %esi
                   1086:        popl    %ebx
                   1087:        ret
1.20      dsl      1088:
                   1089: 2:                                     # check RAS list
                   1090:        movl    L_MD_REGS(%edi),%ebx
                   1091:        movl    TF_EIP(%ebx),%eax
                   1092:        pushl   %eax
                   1093:        pushl   %esi
                   1094:        call    _C_LABEL(ras_lookup)
                   1095:        addl    $8,%esp
                   1096:        cmpl    $-1,%eax
                   1097:        je      1b
                   1098:        movl    %eax,TF_EIP(%ebx)
                   1099:        jmp     1b
1.1       fvdl     1100:
                   1101: /*
1.5       thorpej  1102:  * void cpu_switchto(struct lwp *current, struct lwp *next)
                   1103:  * Switch to the specified next LWP.
                   1104:  */
                   1105: ENTRY(cpu_switchto)
                   1106:        pushl   %ebx
                   1107:        pushl   %esi
                   1108:        pushl   %edi
                   1109:
                   1110: #ifdef DEBUG
                   1111:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                   1112:        jae     1f
                   1113:        pushl   $2f
                   1114:        call    _C_LABEL(panic)
                   1115:        /* NOTREACHED */
                   1116: 2:     .asciz  "not splsched() in cpu_switchto!"
                   1117: 1:
                   1118: #endif /* DEBUG */
                   1119:
                   1120:        movl    16(%esp),%esi           # current
                   1121:        movl    20(%esp),%edi           # next
                   1122:
                   1123:        /*
                   1124:         * Clear curlwp so that we don't accumulate system time while idle.
                   1125:         * This also insures that schedcpu() will move the old process to
                   1126:         * the correct queue if it happens to get called from the spllower()
                   1127:         * below and changes the priority.  (See corresponding comment in
                   1128:         * usrret()).
                   1129:         *
                   1130:         * XXX Is this necessary?  We know we won't go idle.
                   1131:         */
                   1132:        movl    $0,CPUVAR(CURLWP)
                   1133:
                   1134:        /*
                   1135:         * We're running at splhigh(), but it's otherwise okay to take
                   1136:         * interrupts here.
                   1137:         */
                   1138:        sti
                   1139:
                   1140:        /* Jump into the middle of cpu_switch */
                   1141:        xorl    %eax,%eax
                   1142:        jmp     switch_resume
                   1143:
                   1144: /*
1.21      jdolecek 1145:  * void cpu_exit(struct lwp *l)
1.30      junyoung 1146:  * Switch to the appropriate idle context (lwp0's if uniprocessor; the CPU's
                   1147:  * if multiprocessor) and deallocate the address space and kernel stack for p.
1.1       fvdl     1148:  * Then jump into cpu_switch(), as if we were in the idle proc all along.
                   1149:  */
                   1150: #ifndef MULTIPROCESSOR
1.5       thorpej  1151:        .globl  _C_LABEL(lwp0)
1.1       fvdl     1152: #endif
1.21      jdolecek 1153: /* LINTSTUB: Func: void cpu_exit(struct lwp *l) */
                   1154: ENTRY(cpu_exit)
1.1       fvdl     1155:        movl    4(%esp),%edi            # old process
                   1156: #ifndef MULTIPROCESSOR
1.5       thorpej  1157:        movl    $_C_LABEL(lwp0),%ebx
                   1158:        movl    L_ADDR(%ebx),%esi
                   1159:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl     1160: #else
                   1161:        movl    CPUVAR(IDLE_PCB),%esi
                   1162:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                   1163: #endif
                   1164:        /* In case we fault... */
1.5       thorpej  1165:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl     1166:
                   1167:        /* Restore the idle context. */
                   1168:        cli
                   1169:
                   1170:        /* Restore stack pointers. */
                   1171:        movl    PCB_ESP(%esi),%esp
                   1172:        movl    PCB_EBP(%esi),%ebp
                   1173:
1.26      yamt     1174:        /* Switch TSS. Reset "task busy" flag before loading. */
                   1175:        movl    %cr3,%eax
                   1176:        movl    %eax,PCB_CR3(%esi)
1.1       fvdl     1177: #ifdef MULTIPROCESSOR
                   1178:        movl    CPUVAR(GDT),%eax
1.30      junyoung 1179: #else
1.1       fvdl     1180:        /* Load TSS info. */
                   1181:        movl    _C_LABEL(gdt),%eax
                   1182: #endif
                   1183:
                   1184:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                   1185:        ltr     %dx
                   1186:
                   1187:        /* We're always in the kernel, so we don't need the LDT. */
                   1188:
                   1189:        /* Restore cr0 (including FPU state). */
                   1190:        movl    PCB_CR0(%esi),%ecx
                   1191:        movl    %ecx,%cr0
                   1192:
                   1193:        /* Record new pcb. */
                   1194:        SET_CURPCB(%esi)
                   1195:
                   1196:        /* Interrupts are okay again. */
                   1197:        sti
                   1198:
                   1199:        /*
1.21      jdolecek 1200:         * Schedule the dead LWP's stack to be freed.
1.1       fvdl     1201:         */
1.21      jdolecek 1202:        pushl   %edi
                   1203:        call    _C_LABEL(lwp_exit2)
1.1       fvdl     1204:        addl    $4,%esp
                   1205:
                   1206:        /* Jump into cpu_switch() with the right state. */
                   1207:        xorl    %esi,%esi
1.5       thorpej  1208:        movl    %esi,CPUVAR(CURLWP)
1.1       fvdl     1209:        jmp     idle_start
                   1210:
                   1211: /*
                   1212:  * void savectx(struct pcb *pcb);
                   1213:  * Update pcb, saving current processor state.
                   1214:  */
                   1215: /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
                   1216: ENTRY(savectx)
                   1217:        movl    4(%esp),%edx            # edx = p->p_addr
1.30      junyoung 1218:
1.1       fvdl     1219:        /* Save stack pointers. */
                   1220:        movl    %esp,PCB_ESP(%edx)
                   1221:        movl    %ebp,PCB_EBP(%edx)
                   1222:
                   1223:        ret
                   1224:
                   1225: /*
                   1226:  * Old call gate entry for syscall
                   1227:  */
                   1228: /* LINTSTUB: Var: char Xosyscall[1]; */
                   1229: IDTVEC(osyscall)
                   1230:        /* Set eflags in trap frame. */
                   1231:        pushfl
                   1232:        popl    8(%esp)
                   1233:        pushl   $7              # size of instruction for restart
                   1234:        jmp     syscall1
                   1235:
                   1236: /*
                   1237:  * Trap gate entry for syscall
                   1238:  */
                   1239: /* LINTSTUB: Var: char Xsyscall[1]; */
                   1240: IDTVEC(syscall)
                   1241:        pushl   $2              # size of instruction for restart
                   1242: syscall1:
                   1243:        pushl   $T_ASTFLT       # trap # for doing ASTs
                   1244:        INTRENTRY
                   1245:
                   1246: #ifdef DIAGNOSTIC
1.24      yamt     1247:        cmpl    $0, CPUVAR(WANT_PMAPLOAD)
                   1248:        jz      1f
                   1249:        pushl   $6f
                   1250:        call    _C_LABEL(printf)
                   1251:        addl    $4, %esp
                   1252: 1:
1.1       fvdl     1253:        movl    CPUVAR(ILEVEL),%ebx
                   1254:        testl   %ebx,%ebx
                   1255:        jz      1f
                   1256:        pushl   $5f
                   1257:        call    _C_LABEL(printf)
                   1258:        addl    $4,%esp
                   1259: #ifdef DDB
                   1260:        int     $3
                   1261: #endif
1.30      junyoung 1262: 1:
1.1       fvdl     1263: #endif /* DIAGNOSTIC */
1.5       thorpej  1264:        movl    CPUVAR(CURLWP),%edx
                   1265:        movl    %esp,L_MD_REGS(%edx)    # save pointer to frame
                   1266:        movl    L_PROC(%edx),%edx
1.15      fvdl     1267:        pushl   %esp
1.1       fvdl     1268:        call    *P_MD_SYSCALL(%edx)     # get pointer to syscall() function
1.15      fvdl     1269:        addl    $4,%esp
1.27      yamt     1270: .Lsyscall_checkast:
1.24      yamt     1271:        /* Check for ASTs on exit to user mode. */
1.1       fvdl     1272:        cli
1.5       thorpej  1273:        CHECK_ASTPENDING(%eax)
1.1       fvdl     1274:        je      1f
                   1275:        /* Always returning to user mode here. */
1.5       thorpej  1276:        CLEAR_ASTPENDING(%eax)
1.1       fvdl     1277:        sti
                   1278:        /* Pushed T_ASTFLT into tf_trapno on entry. */
1.15      fvdl     1279:        pushl   %esp
1.1       fvdl     1280:        call    _C_LABEL(trap)
1.15      fvdl     1281:        addl    $4,%esp
1.27      yamt     1282:        jmp     .Lsyscall_checkast      /* re-check ASTs */
1.24      yamt     1283: 1:     CHECK_DEFERRED_SWITCH(%eax)
                   1284:        jnz     9f
1.1       fvdl     1285: #ifndef DIAGNOSTIC
1.24      yamt     1286:        INTRFASTEXIT
1.1       fvdl     1287: #else /* DIAGNOSTIC */
1.24      yamt     1288:        cmpl    $IPL_NONE,CPUVAR(ILEVEL)
1.1       fvdl     1289:        jne     3f
                   1290:        INTRFASTEXIT
                   1291: 3:     sti
                   1292:        pushl   $4f
                   1293:        call    _C_LABEL(printf)
                   1294:        addl    $4,%esp
                   1295: #ifdef DDB
                   1296:        int     $3
                   1297: #endif /* DDB */
                   1298:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                   1299:        jmp     2b
                   1300: 4:     .asciz  "WARNING: SPL NOT LOWERED ON SYSCALL EXIT\n"
1.30      junyoung 1301: 5:     .asciz  "WARNING: SPL NOT ZERO ON SYSCALL ENTRY\n"
                   1302: 6:     .asciz  "WARNING: WANT PMAPLOAD ON SYSCALL ENTRY\n"
1.1       fvdl     1303: #endif /* DIAGNOSTIC */
1.24      yamt     1304: 9:     sti
                   1305:        call    _C_LABEL(pmap_load)
1.27      yamt     1306:        jmp     .Lsyscall_checkast      /* re-check ASTs */
1.1       fvdl     1307:
                   1308: #if NNPX > 0
                   1309: /*
                   1310:  * Special interrupt handlers.  Someday intr0-intr15 will be used to count
                   1311:  * interrupts.  We'll still need a special exception 16 handler.  The busy
                   1312:  * latch stuff in probintr() can be moved to npxprobe().
                   1313:  */
                   1314:
                   1315: /* LINTSTUB: Func: void probeintr(void) */
                   1316: NENTRY(probeintr)
                   1317:        ss
                   1318:        incl    _C_LABEL(npx_intrs_while_probing)
                   1319:        pushl   %eax
                   1320:        movb    $0x20,%al       # EOI (asm in strings loses cpp features)
                   1321:        outb    %al,$0xa0       # IO_ICU2
                   1322:        outb    %al,$0x20       # IO_ICU1
                   1323:        movb    $0,%al
                   1324:        outb    %al,$0xf0       # clear BUSY# latch
                   1325:        popl    %eax
                   1326:        iret
                   1327:
                   1328: /* LINTSTUB: Func: void probetrap(void) */
                   1329: NENTRY(probetrap)
                   1330:        ss
                   1331:        incl    _C_LABEL(npx_traps_while_probing)
                   1332:        fnclex
                   1333:        iret
                   1334:
                   1335: /* LINTSTUB: Func: int npx586bug1(int a, int b) */
                   1336: NENTRY(npx586bug1)
                   1337:        fildl   4(%esp)         # x
                   1338:        fildl   8(%esp)         # y
                   1339:        fld     %st(1)
                   1340:        fdiv    %st(1),%st      # x/y
                   1341:        fmulp   %st,%st(1)      # (x/y)*y
                   1342:        fsubrp  %st,%st(1)      # x-(x/y)*y
                   1343:        pushl   $0
                   1344:        fistpl  (%esp)
                   1345:        popl    %eax
                   1346:        ret
                   1347: #endif /* NNPX > 0 */

CVSweb <webmaster@jp.NetBSD.org>