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

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

CVSweb <webmaster@jp.NetBSD.org>