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

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

CVSweb <webmaster@jp.NetBSD.org>