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

1.18    ! christos    1: /*     $NetBSD: locore.S,v 1.17 2003/09/10 19:48:49 christos Exp $     */
1.1       fvdl        2:
                      3: /*-
                      4:  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
                      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"
1.18    ! christos   77: #include "opt_dummy_nops.h"
1.1       fvdl       78: #include "opt_ipkdb.h"
1.18    ! christos   79: #include "opt_lockdebug.h"
1.1       fvdl       80: #include "opt_multiprocessor.h"
                     81: #include "opt_realmem.h"
1.18    ! christos   82: #include "opt_user_ldt.h"
        !            83: #include "opt_vm86.h"
1.1       fvdl       84:
                     85: #include "npx.h"
                     86: #include "assym.h"
                     87: #include "apm.h"
                     88: #include "lapic.h"
                     89: #include "ioapic.h"
1.8       fvdl       90: #include "ksyms.h"
1.1       fvdl       91:
                     92: #include <sys/errno.h>
                     93: #include <sys/syscall.h>
                     94:
                     95: #include <machine/cputypes.h>
                     96: #include <machine/param.h>
                     97: #include <machine/pte.h>
                     98: #include <machine/segments.h>
                     99: #include <machine/specialreg.h>
                    100: #include <machine/trap.h>
                    101: #include <machine/bootinfo.h>
                    102:
                    103: #if NLAPIC > 0
                    104: #include <machine/i82489reg.h>
                    105: #endif
                    106:
                    107: /* LINTSTUB: include <sys/types.h> */
                    108: /* LINTSTUB: include <machine/cpu.h> */
                    109: /* LINTSTUB: include <sys/systm.h> */
                    110:
                    111: #include <machine/asm.h>
                    112:
                    113: #if defined(MULTIPROCESSOR)
                    114:
1.5       thorpej   115: #define SET_CURLWP(lwp,cpu)                            \
1.1       fvdl      116:        movl    CPUVAR(SELF),cpu                ;       \
1.5       thorpej   117:        movl    lwp,CPUVAR(CURLWP)      ;       \
                    118:        movl    cpu,L_CPU(lwp)
1.1       fvdl      119:
                    120: #else
                    121:
1.5       thorpej   122: #define SET_CURLWP(lwp,tcpu)           movl    lwp,CPUVAR(CURLWP)
                    123: #define GET_CURLWP(reg)                        movl    CPUVAR(CURLWP),reg
1.1       fvdl      124:
                    125: #endif
                    126:
                    127: #define GET_CURPCB(reg)                        movl    CPUVAR(CURPCB),reg
                    128: #define SET_CURPCB(reg)                        movl    reg,CPUVAR(CURPCB)
                    129:
                    130: #define CLEAR_RESCHED(reg)             movl    reg,CPUVAR(RESCHED)
                    131:
                    132: /* XXX temporary kluge; these should not be here */
                    133: /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
                    134: #include <dev/isa/isareg.h>
                    135:
                    136:
                    137: /* Disallow old names for REALBASEMEM */
                    138: #ifdef BIOSBASEMEM
                    139: #error BIOSBASEMEM option deprecated; use REALBASEMEM only if memory size reported by latest boot block is incorrect
                    140: #endif
                    141:
                    142: /* Disallow old names for REALEXTMEM */
                    143: #ifdef EXTMEM_SIZE
                    144: #error EXTMEM_SIZE option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
                    145: #endif
                    146: #ifdef BIOSEXTMEM
                    147: #error BIOSEXTMEM option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
                    148: #endif
                    149:
                    150: #include <machine/frameasm.h>
                    151:
                    152:
                    153: #ifdef MULTIPROCESSOR
                    154: #include <machine/i82489reg.h>
                    155: #endif
                    156:
                    157: /*
                    158:  * PTmap is recursive pagemap at top of virtual address space.
                    159:  * Within PTmap, the page directory can be found (third indirection).
                    160:  *
                    161:  * XXX 4 == sizeof pde
                    162:  */
                    163:        .set    _C_LABEL(PTmap),(PDSLOT_PTE << PDSHIFT)
1.7       thorpej   164:        .set    _C_LABEL(PTD),(_C_LABEL(PTmap) + PDSLOT_PTE * PAGE_SIZE)
1.1       fvdl      165:        .set    _C_LABEL(PTDpde),(_C_LABEL(PTD) + PDSLOT_PTE * 4)
                    166:
                    167: /*
                    168:  * APTmap, APTD is the alternate recursive pagemap.
                    169:  * It's used when modifying another process's page tables.
                    170:  *
                    171:  * XXX 4 == sizeof pde
                    172:  */
                    173:        .set    _C_LABEL(APTmap),(PDSLOT_APTE << PDSHIFT)
1.7       thorpej   174:        .set    _C_LABEL(APTD),(_C_LABEL(APTmap) + PDSLOT_APTE * PAGE_SIZE)
1.1       fvdl      175:        .set    _C_LABEL(APTDpde),(_C_LABEL(PTD) + PDSLOT_APTE * 4)
                    176:
                    177:
                    178: /*
                    179:  * Initialization
                    180:  */
                    181:        .data
                    182:
                    183:        .globl  _C_LABEL(cpu)
                    184:        .globl  _C_LABEL(esym),_C_LABEL(boothowto)
                    185:        .globl  _C_LABEL(bootinfo),_C_LABEL(atdevbase)
                    186: #ifdef COMPAT_OLDBOOT
                    187:        .globl  _C_LABEL(bootdev)
                    188: #endif
                    189:        .globl  _C_LABEL(proc0paddr),_C_LABEL(PTDpaddr)
                    190:        .globl  _C_LABEL(biosbasemem),_C_LABEL(biosextmem)
                    191:        .globl  _C_LABEL(gdt)
                    192: #ifdef I586_CPU
                    193:        .globl  _C_LABEL(idt)
                    194: #endif
                    195:        .globl  _C_LABEL(lapic_tpr)
                    196:
                    197: #if NLAPIC > 0
                    198: #ifdef __ELF__
1.7       thorpej   199:        .align  PAGE_SIZE
1.1       fvdl      200: #else
                    201:        .align  12
                    202: #endif
                    203:        .globl _C_LABEL(local_apic), _C_LABEL(lapic_id)
                    204: _C_LABEL(local_apic):
                    205:        .space  LAPIC_ID
                    206: _C_LABEL(lapic_id):
                    207:        .long   0x00000000
                    208:        .space  LAPIC_TPRI-(LAPIC_ID+4)
                    209: _C_LABEL(lapic_tpr):
                    210:        .space  LAPIC_PPRI-LAPIC_TPRI
                    211: _C_LABEL(lapic_ppr):
                    212:        .space  LAPIC_ISR-LAPIC_PPRI
                    213: _C_LABEL(lapic_isr):
1.7       thorpej   214:        .space  PAGE_SIZE-LAPIC_ISR
1.1       fvdl      215: #else
                    216: _C_LABEL(lapic_tpr):
                    217:        .long 0
                    218: #endif
                    219:
                    220:
                    221: _C_LABEL(cpu):         .long   0       # are we 386, 386sx, or 486,
                    222:                                        #   or Pentium, or..
                    223: _C_LABEL(esym):                .long   0       # ptr to end of syms
                    224: _C_LABEL(atdevbase):   .long   0       # location of start of iomem in virtual
                    225: _C_LABEL(proc0paddr):  .long   0
                    226: _C_LABEL(PTDpaddr):    .long   0       # paddr of PTD, for libkvm
                    227: #ifndef REALBASEMEM
                    228: _C_LABEL(biosbasemem): .long   0       # base memory reported by BIOS
                    229: #else
                    230: _C_LABEL(biosbasemem): .long   REALBASEMEM
                    231: #endif
                    232: #ifndef REALEXTMEM
                    233: _C_LABEL(biosextmem):  .long   0       # extended memory reported by BIOS
                    234: #else
                    235: _C_LABEL(biosextmem):  .long   REALEXTMEM
                    236: #endif
                    237:
                    238:        .space 512
                    239: tmpstk:
                    240:
                    241:
1.13      christos  242: #define        _RELOC(x)       ((x) - KERNBASE_LOCORE)
1.1       fvdl      243: #define        RELOC(x)        _RELOC(_C_LABEL(x))
                    244:
                    245:        .text
                    246:        .globl  _C_LABEL(kernel_text)
                    247:        .set    _C_LABEL(kernel_text),KERNTEXTOFF
                    248:
                    249:        .globl  start
                    250: start: movw    $0x1234,0x472                   # warm boot
                    251:
                    252:        /*
                    253:         * Load parameters from stack
                    254:         * (howto, [bootdev], bootinfo, esym, basemem, extmem).
                    255:         */
                    256:        movl    4(%esp),%eax
                    257:        movl    %eax,RELOC(boothowto)
                    258: #ifdef COMPAT_OLDBOOT
                    259:        movl    8(%esp),%eax
                    260:        movl    %eax,RELOC(bootdev)
                    261: #endif
                    262:        movl    12(%esp),%eax
                    263:
                    264:        testl   %eax, %eax
                    265:        jz      1f
                    266:        movl    (%eax), %ebx            /* number of entries */
                    267:        movl    $RELOC(bootinfo), %edi
                    268:        movl    %ebx, (%edi)
                    269:        addl    $4, %edi
                    270: 2:
                    271:        testl   %ebx, %ebx
                    272:        jz      1f
                    273:        addl    $4, %eax
                    274:        movl    (%eax), %ecx            /* address of entry */
                    275:        pushl   %eax
                    276:        pushl   (%ecx)                  /* len */
                    277:        pushl   %ecx
                    278:        pushl   %edi
                    279:        addl    (%ecx), %edi            /* update dest pointer */
                    280:        cmpl    $_RELOC(_C_LABEL(bootinfo) + BOOTINFO_MAXSIZE), %edi
                    281:        jg      2f
                    282:        call    _C_LABEL(memcpy)
                    283:        addl    $12, %esp
                    284:        popl    %eax
                    285:        subl    $1, %ebx
                    286:        jmp     2b
                    287: 2:     /* cleanup for overflow case */
                    288:        addl    $16, %esp
                    289:        movl    $RELOC(bootinfo), %edi
                    290:        subl    %ebx, (%edi)            /* correct number of entries */
                    291: 1:
                    292:
                    293:        movl    16(%esp),%eax
                    294:        testl   %eax,%eax
                    295:        jz      1f
1.13      christos  296:        addl    $KERNBASE_LOCORE,%eax
1.1       fvdl      297: 1:     movl    %eax,RELOC(esym)
                    298:
                    299:        movl    RELOC(biosextmem),%eax
                    300:        testl   %eax,%eax
                    301:        jnz     1f
                    302:        movl    20(%esp),%eax
                    303:        movl    %eax,RELOC(biosextmem)
                    304: 1:
                    305:        movl    RELOC(biosbasemem),%eax
                    306:        testl   %eax,%eax
                    307:        jnz     1f
                    308:        movl    24(%esp),%eax
                    309:        movl    %eax,RELOC(biosbasemem)
                    310: 1:
                    311:
                    312:        /* First, reset the PSL. */
                    313:        pushl   $PSL_MBO
                    314:        popfl
                    315:
                    316:        /* Clear segment registers; always null in proc0. */
                    317:        xorl    %eax,%eax
                    318:        movw    %ax,%fs
                    319:        movw    %ax,%gs
                    320:        decl    %eax
                    321:        movl    %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
                    322:
                    323:        /* Find out our CPU type. */
                    324:
                    325: try386:        /* Try to toggle alignment check flag; does not exist on 386. */
                    326:        pushfl
                    327:        popl    %eax
                    328:        movl    %eax,%ecx
                    329:        orl     $PSL_AC,%eax
                    330:        pushl   %eax
                    331:        popfl
                    332:        pushfl
                    333:        popl    %eax
                    334:        xorl    %ecx,%eax
                    335:        andl    $PSL_AC,%eax
                    336:        pushl   %ecx
                    337:        popfl
                    338:
                    339:        testl   %eax,%eax
                    340:        jnz     try486
                    341:
                    342:        /*
                    343:         * Try the test of a NexGen CPU -- ZF will not change on a DIV
                    344:         * instruction on a NexGen, it will on an i386.  Documented in
                    345:         * Nx586 Processor Recognition Application Note, NexGen, Inc.
                    346:         */
                    347:        movl    $0x5555,%eax
                    348:        xorl    %edx,%edx
                    349:        movl    $2,%ecx
                    350:        divl    %ecx
                    351:        jnz     is386
                    352:
                    353: isnx586:
                    354:        /*
                    355:         * Don't try cpuid, as Nx586s reportedly don't support the
                    356:         * PSL_ID bit.
                    357:         */
                    358:        movl    $CPU_NX586,RELOC(cpu)
                    359:        jmp     2f
                    360:
                    361: is386:
                    362:        movl    $CPU_386,RELOC(cpu)
                    363:        jmp     2f
                    364:
                    365: try486:        /* Try to toggle identification flag; does not exist on early 486s. */
                    366:        pushfl
                    367:        popl    %eax
                    368:        movl    %eax,%ecx
                    369:        xorl    $PSL_ID,%eax
                    370:        pushl   %eax
                    371:        popfl
                    372:        pushfl
                    373:        popl    %eax
                    374:        xorl    %ecx,%eax
                    375:        andl    $PSL_ID,%eax
                    376:        pushl   %ecx
                    377:        popfl
                    378:
                    379:        testl   %eax,%eax
                    380:        jnz     try586
                    381: is486: movl    $CPU_486,RELOC(cpu)
                    382:        /*
                    383:         * Check Cyrix CPU
                    384:         * Cyrix CPUs do not change the undefined flags following
                    385:         * execution of the divide instruction which divides 5 by 2.
                    386:         *
                    387:         * Note: CPUID is enabled on M2, so it passes another way.
                    388:         */
                    389:        pushfl
                    390:        movl    $0x5555, %eax
                    391:        xorl    %edx, %edx
                    392:        movl    $2, %ecx
                    393:        clc
                    394:        divl    %ecx
                    395:        jnc     trycyrix486
                    396:        popfl
                    397:        jmp 2f
                    398: trycyrix486:
                    399:        movl    $CPU_6x86,RELOC(cpu)    # set CPU type
                    400:        /*
                    401:         * Check for Cyrix 486 CPU by seeing if the flags change during a
                    402:         * divide. This is documented in the Cx486SLC/e SMM Programmer's
                    403:         * Guide.
                    404:         */
                    405:        xorl    %edx,%edx
                    406:        cmpl    %edx,%edx               # set flags to known state
                    407:        pushfl
                    408:        popl    %ecx                    # store flags in ecx
                    409:        movl    $-1,%eax
                    410:        movl    $4,%ebx
                    411:        divl    %ebx                    # do a long division
                    412:        pushfl
                    413:        popl    %eax
                    414:        xorl    %ecx,%eax               # are the flags different?
                    415:        testl   $0x8d5,%eax             # only check C|PF|AF|Z|N|V
                    416:        jne     2f                      # yes; must be Cyrix 6x86 CPU
                    417:        movl    $CPU_486DLC,RELOC(cpu)  # set CPU type
                    418:
                    419: #ifndef CYRIX_CACHE_WORKS
                    420:        /* Disable caching of the ISA hole only. */
                    421:        invd
                    422:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    423:        outb    %al,$0x22
                    424:        inb     $0x23,%al
                    425:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    426:        movb    %al,%ah
                    427:        movb    $CCR0,%al
                    428:        outb    %al,$0x22
                    429:        movb    %ah,%al
                    430:        outb    %al,$0x23
                    431:        invd
                    432: #else /* CYRIX_CACHE_WORKS */
                    433:        /* Set cache parameters */
                    434:        invd                            # Start with guaranteed clean cache
                    435:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    436:        outb    %al,$0x22
                    437:        inb     $0x23,%al
                    438:        andb    $~CCR0_NC0,%al
                    439: #ifndef CYRIX_CACHE_REALLY_WORKS
                    440:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    441: #else
                    442:        orb     $CCR0_NC1,%al
                    443: #endif
                    444:        movb    %al,%ah
                    445:        movb    $CCR0,%al
                    446:        outb    %al,$0x22
                    447:        movb    %ah,%al
                    448:        outb    %al,$0x23
                    449:        /* clear non-cacheable region 1 */
                    450:        movb    $(NCR1+2),%al
                    451:        outb    %al,$0x22
                    452:        movb    $NCR_SIZE_0K,%al
                    453:        outb    %al,$0x23
                    454:        /* clear non-cacheable region 2 */
                    455:        movb    $(NCR2+2),%al
                    456:        outb    %al,$0x22
                    457:        movb    $NCR_SIZE_0K,%al
                    458:        outb    %al,$0x23
                    459:        /* clear non-cacheable region 3 */
                    460:        movb    $(NCR3+2),%al
                    461:        outb    %al,$0x22
                    462:        movb    $NCR_SIZE_0K,%al
                    463:        outb    %al,$0x23
                    464:        /* clear non-cacheable region 4 */
                    465:        movb    $(NCR4+2),%al
                    466:        outb    %al,$0x22
                    467:        movb    $NCR_SIZE_0K,%al
                    468:        outb    %al,$0x23
                    469:        /* enable caching in CR0 */
                    470:        movl    %cr0,%eax
                    471:        andl    $~(CR0_CD|CR0_NW),%eax
                    472:        movl    %eax,%cr0
                    473:        invd
                    474: #endif /* CYRIX_CACHE_WORKS */
                    475:
                    476:        jmp     2f
                    477:
                    478: try586:        /* Use the `cpuid' instruction. */
                    479:        xorl    %eax,%eax
                    480:        cpuid
                    481:        movl    %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
                    482:
                    483: 2:
                    484:        /*
                    485:         * Finished with old stack; load new %esp now instead of later so we
                    486:         * can trace this code without having to worry about the trace trap
                    487:         * clobbering the memory test or the zeroing of the bss+bootstrap page
                    488:         * tables.
                    489:         *
                    490:         * The boot program should check:
                    491:         *      text+data <= &stack_variable - more_space_for_stack
                    492:         *      text+data+bss+pad+space_for_page_tables <= end_of_memory
                    493:         * Oops, the gdt is in the carcass of the boot program so clearing
                    494:         * the rest of memory is still not possible.
                    495:         */
                    496:        movl    $_RELOC(tmpstk),%esp    # bootstrap stack end location
                    497:
                    498: /*
                    499:  * Virtual address space of kernel:
                    500:  *
                    501:  * text | data | bss | [syms] | page dir | proc0 kstack
                    502:  *                           0          1       2      3
                    503:  */
1.7       thorpej   504: #define        PROC0PDIR       ((0)              * PAGE_SIZE)
                    505: #define        PROC0STACK      ((1)              * PAGE_SIZE)
                    506: #define        SYSMAP          ((1+UPAGES)       * PAGE_SIZE)
                    507: #define        TABLESIZE       ((1+UPAGES) * PAGE_SIZE) /* + nkpde * PAGE_SIZE */
1.1       fvdl      508:
                    509:        /* Find end of kernel image. */
                    510:        movl    $RELOC(end),%edi
1.8       fvdl      511: #if (NKSYMS || defined(DDB) || defined(LKM)) && !defined(SYMTAB_SPACE)
1.1       fvdl      512:        /* Save the symbols (if loaded). */
                    513:        movl    RELOC(esym),%eax
                    514:        testl   %eax,%eax
                    515:        jz      1f
1.13      christos  516:        subl    $KERNBASE_LOCORE,%eax
1.1       fvdl      517:        movl    %eax,%edi
                    518: 1:
                    519: #endif
                    520:
                    521:        /* Calculate where to start the bootstrap tables. */
                    522:        movl    %edi,%esi                       # edi = esym ? esym : end
                    523:        addl    $PGOFSET,%esi                   # page align up
                    524:        andl    $~PGOFSET,%esi
                    525:
                    526:        /*
                    527:         * Calculate the size of the kernel page table directory, and
                    528:         * how many entries it will have.
                    529:         */
                    530:        movl    RELOC(nkpde),%ecx               # get nkpde
                    531:        cmpl    $NKPTP_MIN,%ecx                 # larger than min?
                    532:        jge     1f
                    533:        movl    $NKPTP_MIN,%ecx                 # set at min
                    534:        jmp     2f
                    535: 1:     cmpl    $NKPTP_MAX,%ecx                 # larger than max?
                    536:        jle     2f
                    537:        movl    $NKPTP_MAX,%ecx
                    538: 2:
                    539:
                    540:        /* Clear memory for bootstrap tables. */
                    541:        shll    $PGSHIFT,%ecx
                    542:        addl    $TABLESIZE,%ecx
                    543:        addl    %esi,%ecx                       # end of tables
                    544:        subl    %edi,%ecx                       # size of tables
                    545:        shrl    $2,%ecx
                    546:        xorl    %eax,%eax
                    547:        cld
                    548:        rep
                    549:        stosl
                    550:
                    551: /*
                    552:  * fillkpt
                    553:  *     eax = pte (page frame | control | status)
                    554:  *     ebx = page table address
                    555:  *     ecx = number of pages to map
                    556:  */
                    557: #define        fillkpt         \
                    558: 1:     movl    %eax,(%ebx)     ; \
1.7       thorpej   559:        addl    $PAGE_SIZE,%eax ; /* increment physical address */ \
1.1       fvdl      560:        addl    $4,%ebx         ; /* next pte */ \
                    561:        loop    1b              ;
                    562:
                    563: /*
                    564:  * Build initial page tables.
                    565:  */
                    566:        /* Calculate end of text segment, rounded to a page. */
                    567:        leal    (RELOC(etext)+PGOFSET),%edx
                    568:        andl    $~PGOFSET,%edx
                    569:
                    570:        /* Skip over the first 1MB. */
                    571:        movl    $_RELOC(KERNTEXTOFF),%eax
                    572:        movl    %eax,%ecx
                    573:        shrl    $PGSHIFT,%ecx
                    574:        leal    (SYSMAP)(%esi,%ecx,4),%ebx
                    575:
                    576:        /* Map the kernel text read-only. */
                    577:        movl    %edx,%ecx
                    578:        subl    %eax,%ecx
                    579:        shrl    $PGSHIFT,%ecx
                    580:        orl     $(PG_V|PG_KR),%eax
                    581:        fillkpt
                    582:
                    583:        /* Map the data, BSS, and bootstrap tables read-write. */
                    584:        leal    (PG_V|PG_KW)(%edx),%eax
                    585:        movl    RELOC(nkpde),%ecx
                    586:        shll    $PGSHIFT,%ecx
                    587:        addl    $TABLESIZE,%ecx
                    588:        addl    %esi,%ecx                               # end of tables
                    589:        subl    %edx,%ecx                               # subtract end of text
                    590:        shrl    $PGSHIFT,%ecx
                    591:        fillkpt
                    592:
                    593:        /* Map ISA I/O memory. */
                    594:        movl    $(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax   # having these bits set
                    595:        movl    $(IOM_SIZE>>PGSHIFT),%ecx               # for this many pte s,
                    596:        fillkpt
                    597:
                    598: /*
                    599:  * Construct a page table directory.
                    600:  */
                    601:        /* Install PDEs for temporary double map of kernel. */
                    602:        movl    RELOC(nkpde),%ecx                       # for this many pde s,
                    603:        leal    (PROC0PDIR+0*4)(%esi),%ebx              # which is where temp maps!
                    604:        leal    (SYSMAP+PG_V|PG_KW)(%esi),%eax          # pte for KPT in proc 0,
                    605:        fillkpt
                    606:
                    607:        /* Map kernel PDEs. */
                    608:        movl    RELOC(nkpde),%ecx                       # for this many pde s,
                    609:        leal    (PROC0PDIR+PDSLOT_KERN*4)(%esi),%ebx    # kernel pde offset
                    610:        leal    (SYSMAP+PG_V|PG_KW)(%esi),%eax          # pte for KPT in proc 0,
                    611:        fillkpt
                    612:
                    613:        /* Install a PDE recursively mapping page directory as a page table! */
                    614:        leal    (PROC0PDIR+PG_V|PG_KW)(%esi),%eax       # pte for ptd
                    615:        movl    %eax,(PROC0PDIR+PDSLOT_PTE*4)(%esi)     # recursive PD slot
                    616:
                    617:        /* Save phys. addr of PTD, for libkvm. */
                    618:        movl    %esi,RELOC(PTDpaddr)
                    619:
                    620:        /* Load base of page directory and enable mapping. */
                    621:        movl    %esi,%eax               # phys address of ptd in proc 0
                    622:        movl    %eax,%cr3               # load ptd addr into mmu
                    623:        movl    %cr0,%eax               # get control word
                    624:                                        # enable paging & NPX emulation
                    625:        orl     $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_EM|CR0_MP),%eax
                    626:        movl    %eax,%cr0               # and let's page NOW!
                    627:
                    628:        pushl   $begin                  # jump to high mem
                    629:        ret
                    630:
                    631: begin:
1.13      christos  632:        /* Now running relocated at KERNBASE_LOCORE.  Remove double mapping. */
1.1       fvdl      633:        movl    _C_LABEL(nkpde),%ecx            # for this many pde s,
                    634:        leal    (PROC0PDIR+0*4)(%esi),%ebx      # which is where temp maps!
1.13      christos  635:        addl    $(KERNBASE_LOCORE), %ebx        # now use relocated address
1.1       fvdl      636: 1:     movl    $0,(%ebx)
                    637:        addl    $4,%ebx # next pde
                    638:        loop    1b
                    639:
                    640:        /* Relocate atdevbase. */
                    641:        movl    _C_LABEL(nkpde),%edx
                    642:        shll    $PGSHIFT,%edx
1.13      christos  643:        addl    $(TABLESIZE+KERNBASE_LOCORE),%edx
1.1       fvdl      644:        addl    %esi,%edx
                    645:        movl    %edx,_C_LABEL(atdevbase)
                    646:
                    647:        /* Set up bootstrap stack. */
1.13      christos  648:        leal    (PROC0STACK+KERNBASE_LOCORE)(%esi),%eax
1.1       fvdl      649:        movl    %eax,_C_LABEL(proc0paddr)
                    650:        leal    (USPACE-FRAMESIZE)(%eax),%esp
                    651:        movl    %esi,PCB_CR3(%eax)      # pcb->pcb_cr3
                    652:        xorl    %ebp,%ebp               # mark end of frames
                    653:
                    654:        subl    $NGDT*8, %esp           # space for temporary gdt
                    655:        pushl   %esp
                    656:        call    _C_LABEL(initgdt)
                    657:        addl    $4,%esp
                    658:
                    659:        movl    _C_LABEL(nkpde),%eax
                    660:        shll    $PGSHIFT,%eax
                    661:        addl    $TABLESIZE,%eax
                    662:        addl    %esi,%eax               # skip past stack and page tables
                    663:
                    664:        pushl   %eax
                    665:        call    _C_LABEL(init386)       # wire 386 chip for unix operation
                    666:        addl    $4+NGDT*8,%esp          # pop temporary gdt
                    667:
                    668: #ifdef SAFARI_FIFO_HACK
                    669:        movb    $5,%al
                    670:        movw    $0x37b,%dx
                    671:        outb    %al,%dx
                    672:        movw    $0x37f,%dx
                    673:        inb     %dx,%al
                    674:        movb    %al,%cl
                    675:
                    676:        orb     $1,%cl
                    677:
                    678:        movb    $5,%al
                    679:        movw    $0x37b,%dx
                    680:        outb    %al,%dx
                    681:        movw    $0x37f,%dx
                    682:        movb    %cl,%al
                    683:        outb    %al,%dx
                    684: #endif /* SAFARI_FIFO_HACK */
                    685:
                    686:        call    _C_LABEL(main)
                    687:
                    688: /*
                    689:  * void proc_trampoline(void);
                    690:  * This is a trampoline function pushed onto the stack of a newly created
                    691:  * process in order to do some additional setup.  The trampoline is entered by
                    692:  * cpu_switch()ing to the process, so we abuse the callee-saved registers used
                    693:  * by cpu_switch() to store the information about the stub to call.
                    694:  * NOTE: This function does not have a normal calling sequence!
                    695:  */
                    696: /* LINTSTUB: Func: void proc_trampoline(void) */
                    697: NENTRY(proc_trampoline)
                    698: #ifdef MULTIPROCESSOR
                    699:        call    _C_LABEL(proc_trampoline_mp)
                    700: #endif
                    701:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                    702:        pushl   %ebx
                    703:        call    *%esi
                    704:        addl    $4,%esp
                    705:        INTRFASTEXIT
                    706:        /* NOTREACHED */
                    707:
                    708: /*****************************************************************************/
1.16      christos  709: #ifdef COMPAT_16
1.1       fvdl      710: /*
                    711:  * Signal trampoline; copied to top of user stack.
                    712:  */
                    713: /* LINTSTUB: Var: char sigcode[1], esigcode[1]; */
                    714: NENTRY(sigcode)
                    715:        /*
                    716:         * Handler has returned here as if we called it.  The sigcontext
                    717:         * is on the stack after the 3 args "we" pushed.
                    718:         */
                    719:        leal    12(%esp),%eax           # get pointer to sigcontext
                    720:        movl    %eax,4(%esp)            # put it in the argument slot
                    721:                                        # fake return address already there
1.17      christos  722:        movl    $SYS_compat_16___sigreturn14,%eax
1.1       fvdl      723:        int     $0x80                   # enter kernel with args on stack
                    724:        movl    $SYS_exit,%eax
                    725:        int     $0x80                   # exit if sigreturn fails
                    726:        .globl  _C_LABEL(esigcode)
                    727: _C_LABEL(esigcode):
1.16      christos  728: #endif
1.1       fvdl      729:
                    730: /*****************************************************************************/
                    731:
                    732: /*
                    733:  * The following primitives are used to fill and copy regions of memory.
                    734:  */
                    735:
                    736: /*
                    737:  * XXX No section 9 man page for fillw.
                    738:  * fillw seems to be very sparsely used (only in pccons it seems.)
                    739:  * One wonders if it couldn't be done without.
                    740:  * -- Perry Metzger, May 7, 2001
                    741:  */
                    742: /*
                    743:  * void fillw(short pattern, void *addr, size_t len);
                    744:  * Write len copies of pattern at addr.
                    745:  */
                    746: /* LINTSTUB: Func: void fillw(short pattern, void *addr, size_t len) */
                    747: ENTRY(fillw)
                    748:        pushl   %edi
                    749:        movl    8(%esp),%eax
                    750:        movl    12(%esp),%edi
                    751:        movw    %ax,%cx
                    752:        rorl    $16,%eax
                    753:        movw    %cx,%ax
                    754:        cld
                    755:        movl    16(%esp),%ecx
                    756:        shrl    %ecx                    # do longwords
                    757:        rep
                    758:        stosl
                    759:        movl    16(%esp),%ecx
                    760:        andl    $1,%ecx                 # do remainder
                    761:        rep
                    762:        stosw
                    763:        popl    %edi
                    764:        ret
                    765:
                    766: /*
                    767:  * int kcopy(const void *from, void *to, size_t len);
                    768:  * Copy len bytes, abort on fault.
                    769:  */
                    770: /* LINTSTUB: Func: int kcopy(const void *from, void *to, size_t len) */
                    771: ENTRY(kcopy)
                    772:        pushl   %esi
                    773:        pushl   %edi
                    774:        GET_CURPCB(%eax)                # load curpcb into eax and set on-fault
                    775:        pushl   PCB_ONFAULT(%eax)
                    776:        movl    $_C_LABEL(copy_fault), PCB_ONFAULT(%eax)
                    777:
                    778:        movl    16(%esp),%esi
                    779:        movl    20(%esp),%edi
                    780:        movl    24(%esp),%ecx
                    781:        movl    %edi,%eax
                    782:        subl    %esi,%eax
                    783:        cmpl    %ecx,%eax               # overlapping?
                    784:        jb      1f
                    785:        cld                             # nope, copy forward
                    786:        shrl    $2,%ecx                 # copy by 32-bit words
                    787:        rep
                    788:        movsl
                    789:        movl    24(%esp),%ecx
                    790:        andl    $3,%ecx                 # any bytes left?
                    791:        rep
                    792:        movsb
                    793:
                    794:        GET_CURPCB(%edx)                # XXX save curpcb?
                    795:        popl    PCB_ONFAULT(%edx)
                    796:        popl    %edi
                    797:        popl    %esi
                    798:        xorl    %eax,%eax
                    799:        ret
                    800:
                    801:        ALIGN_TEXT
                    802: 1:     addl    %ecx,%edi               # copy backward
                    803:        addl    %ecx,%esi
                    804:        std
                    805:        andl    $3,%ecx                 # any fractional bytes?
                    806:        decl    %edi
                    807:        decl    %esi
                    808:        rep
                    809:        movsb
                    810:        movl    24(%esp),%ecx           # copy remainder by 32-bit words
                    811:        shrl    $2,%ecx
                    812:        subl    $3,%esi
                    813:        subl    $3,%edi
                    814:        rep
                    815:        movsl
                    816:        cld
                    817:
                    818:        GET_CURPCB(%edx)
                    819:        popl    PCB_ONFAULT(%edx)
                    820:        popl    %edi
                    821:        popl    %esi
                    822:        xorl    %eax,%eax
                    823:        ret
                    824:
                    825: /*****************************************************************************/
                    826:
                    827: /*
                    828:  * The following primitives are used to copy data in and out of the user's
                    829:  * address space.
                    830:  */
                    831:
                    832: /*
                    833:  * Default to the lowest-common-denominator.  We will improve it
                    834:  * later.
                    835:  */
                    836: #if defined(I386_CPU)
                    837: #define        DEFAULT_COPYOUT         _C_LABEL(i386_copyout)
                    838: #define        DEFAULT_COPYIN          _C_LABEL(i386_copyin)
                    839: #elif defined(I486_CPU)
                    840: #define        DEFAULT_COPYOUT         _C_LABEL(i486_copyout)
                    841: #define        DEFAULT_COPYIN          _C_LABEL(i386_copyin)
                    842: #elif defined(I586_CPU)
                    843: #define        DEFAULT_COPYOUT         _C_LABEL(i486_copyout)  /* XXX */
                    844: #define        DEFAULT_COPYIN          _C_LABEL(i386_copyin)   /* XXX */
                    845: #elif defined(I686_CPU)
                    846: #define        DEFAULT_COPYOUT         _C_LABEL(i486_copyout)  /* XXX */
                    847: #define        DEFAULT_COPYIN          _C_LABEL(i386_copyin)   /* XXX */
                    848: #endif
                    849:
                    850:        .data
                    851:
                    852:        .globl  _C_LABEL(copyout_func)
                    853: _C_LABEL(copyout_func):
                    854:        .long   DEFAULT_COPYOUT
                    855:
                    856:        .globl  _C_LABEL(copyin_func)
                    857: _C_LABEL(copyin_func):
                    858:        .long   DEFAULT_COPYIN
                    859:
                    860:        .text
                    861:
                    862: /*
                    863:  * int copyout(const void *from, void *to, size_t len);
                    864:  * Copy len bytes into the user's address space.
                    865:  * see copyout(9)
                    866:  */
                    867: /* LINTSTUB: Func: int copyout(const void *kaddr, void *uaddr, size_t len) */
                    868: ENTRY(copyout)
                    869:        jmp     *_C_LABEL(copyout_func)
                    870:
                    871: #if defined(I386_CPU)
                    872: /* LINTSTUB: Func: int i386_copyout(const void *kaddr, void *uaddr, size_t len) */
                    873: ENTRY(i386_copyout)
                    874:        pushl   %esi
                    875:        pushl   %edi
                    876:        pushl   $0
                    877:
                    878:        movl    16(%esp),%esi
                    879:        movl    20(%esp),%edi
                    880:        movl    24(%esp),%eax
                    881:
                    882:        /*
                    883:         * We check that the end of the destination buffer is not past the end
                    884:         * of the user's address space.  If it's not, then we only need to
                    885:         * check that each page is writable.  The 486 will do this for us; the
                    886:         * 386 will not.  (We assume that pages in user space that are not
                    887:         * writable by the user are not writable by the kernel either.)
                    888:         */
                    889:        movl    %edi,%edx
                    890:        addl    %eax,%edx
                    891:        jc      _C_LABEL(copy_efault)
                    892:        cmpl    $VM_MAXUSER_ADDRESS,%edx
                    893:        ja      _C_LABEL(copy_efault)
                    894:
                    895:        testl   %eax,%eax               # anything to do?
                    896:        jz      3f
                    897:
                    898:        /*
                    899:         * We have to check each PTE for (write) permission, since the CPU
                    900:         * doesn't do it for us.
                    901:         */
                    902:
                    903:        /* Compute number of pages. */
                    904:        movl    %edi,%ecx
                    905:        andl    $PGOFSET,%ecx
                    906:        addl    %eax,%ecx
                    907:        decl    %ecx
                    908:        shrl    $PGSHIFT,%ecx
                    909:
                    910:        /* Compute PTE offset for start address. */
                    911:        shrl    $PGSHIFT,%edi
                    912:
                    913:        GET_CURPCB(%edx)
                    914:        movl    $2f,PCB_ONFAULT(%edx)
                    915:
                    916: 1:     /* Check PTE for each page. */
                    917:        testb   $PG_RW,_C_LABEL(PTmap)(,%edi,4)
                    918:        jz      2f
                    919:
                    920: 4:     incl    %edi
                    921:        decl    %ecx
                    922:        jns     1b
                    923:
                    924:        movl    20(%esp),%edi
                    925:        movl    24(%esp),%eax
                    926:        jmp     3f
                    927:
                    928: 2:     /* Simulate a trap. */
                    929:        pushl   %ecx
                    930:        movl    %edi,%eax
                    931:        shll    $PGSHIFT,%eax
                    932:        pushl   %eax
                    933:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                    934:        addl    $4,%esp                 # pop argument
                    935:        popl    %ecx
                    936:        testl   %eax,%eax               # if not ok, return EFAULT
                    937:        jz      4b
                    938:        jmp     _C_LABEL(copy_efault)
                    939:
                    940: 3:     GET_CURPCB(%edx)
                    941:        movl    $_C_LABEL(copy_fault),PCB_ONFAULT(%edx)
                    942:
                    943:        /* bcopy(%esi, %edi, %eax); */
                    944:        cld
                    945:        movl    %eax,%ecx
                    946:        shrl    $2,%ecx
                    947:        rep
                    948:        movsl
                    949:        movl    %eax,%ecx
                    950:        andl    $3,%ecx
                    951:        rep
                    952:        movsb
                    953:
                    954:        popl    PCB_ONFAULT(%edx)
                    955:        popl    %edi
                    956:        popl    %esi
                    957:        xorl    %eax,%eax
                    958:        ret
                    959: #endif /* I386_CPU */
                    960:
                    961: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                    962: /* LINTSTUB: Func: int i486_copyout(const void *kaddr, void *uaddr, size_t len) */
                    963: ENTRY(i486_copyout)
                    964:        pushl   %esi
                    965:        pushl   %edi
                    966:        pushl   $0
                    967:
                    968:        movl    16(%esp),%esi
                    969:        movl    20(%esp),%edi
                    970:        movl    24(%esp),%eax
                    971:
                    972:        /*
                    973:         * We check that the end of the destination buffer is not past the end
                    974:         * of the user's address space.
                    975:         */
                    976:        movl    %edi,%edx
                    977:        addl    %eax,%edx
                    978:        jc      _C_LABEL(copy_efault)
                    979:        cmpl    $VM_MAXUSER_ADDRESS,%edx
                    980:        ja      _C_LABEL(copy_efault)
                    981:
                    982:        GET_CURPCB(%edx)
                    983:        movl    $_C_LABEL(copy_fault),PCB_ONFAULT(%edx)
                    984:
                    985:        /* bcopy(%esi, %edi, %eax); */
                    986:        cld
                    987:        movl    %eax,%ecx
                    988:        shrl    $2,%ecx
                    989:        rep
                    990:        movsl
                    991:        movl    %eax,%ecx
                    992:        andl    $3,%ecx
                    993:        rep
                    994:        movsb
                    995:
                    996:        popl    PCB_ONFAULT(%edx)
                    997:        popl    %edi
                    998:        popl    %esi
                    999:        xorl    %eax,%eax
                   1000:        ret
                   1001: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1002:
                   1003: /*
                   1004:  * int copyin(const void *from, void *to, size_t len);
                   1005:  * Copy len bytes from the user's address space.
                   1006:  * see copyin(9)
                   1007:  */
                   1008: /* LINTSTUB: Func: int copyin(const void *uaddr, void *kaddr, size_t len) */
                   1009: ENTRY(copyin)
                   1010:        jmp     *_C_LABEL(copyin_func)
                   1011:
                   1012: #if defined(I386_CPU) || defined(I486_CPU) || defined(I586_CPU) || \
                   1013:     defined(I686_CPU)
                   1014: /* LINTSTUB: Func: int i386_copyin(const void *uaddr, void *kaddr, size_t len) */
                   1015: ENTRY(i386_copyin)
                   1016:        pushl   %esi
                   1017:        pushl   %edi
                   1018:        GET_CURPCB(%eax)
                   1019:        pushl   $0
                   1020:        movl    $_C_LABEL(copy_fault),PCB_ONFAULT(%eax)
                   1021:
                   1022:        movl    16(%esp),%esi
                   1023:        movl    20(%esp),%edi
                   1024:        movl    24(%esp),%eax
                   1025:
                   1026:        /*
                   1027:         * We check that the end of the destination buffer is not past the end
                   1028:         * of the user's address space.  If it's not, then we only need to
                   1029:         * check that each page is readable, and the CPU will do that for us.
                   1030:         */
                   1031:        movl    %esi,%edx
                   1032:        addl    %eax,%edx
                   1033:        jc      _C_LABEL(copy_efault)
                   1034:        cmpl    $VM_MAXUSER_ADDRESS,%edx
                   1035:        ja      _C_LABEL(copy_efault)
                   1036:
                   1037:        /* bcopy(%esi, %edi, %eax); */
                   1038:        cld
                   1039:        movl    %eax,%ecx
                   1040:        shrl    $2,%ecx
                   1041:        rep
                   1042:        movsl
                   1043:        movl    %eax,%ecx
                   1044:        andl    $3,%ecx
                   1045:        rep
                   1046:        movsb
                   1047:
                   1048:        GET_CURPCB(%edx)
                   1049:        popl    PCB_ONFAULT(%edx)
                   1050:        popl    %edi
                   1051:        popl    %esi
                   1052:        xorl    %eax,%eax
                   1053:        ret
                   1054: #endif /* I386_CPU || I486_CPU || I586_CPU || I686_CPU */
                   1055:
                   1056: /* LINTSTUB: Ignore */
                   1057: NENTRY(copy_efault)
                   1058:        movl    $EFAULT,%eax
                   1059:
                   1060: /* LINTSTUB: Ignore */
                   1061: NENTRY(copy_fault)
                   1062:        GET_CURPCB(%edx)
                   1063:        popl    PCB_ONFAULT(%edx)
                   1064:        popl    %edi
                   1065:        popl    %esi
                   1066:        ret
                   1067:
                   1068: /*
                   1069:  * int copyoutstr(const void *from, void *to, size_t maxlen, size_t *lencopied);
                   1070:  * Copy a NUL-terminated string, at most maxlen characters long, into the
                   1071:  * user's address space.  Return the number of characters copied (including the
                   1072:  * NUL) in *lencopied.  If the string is too long, return ENAMETOOLONG; else
                   1073:  * return 0 or EFAULT.
                   1074:  * see copyoutstr(9)
                   1075:  */
                   1076: /* LINTSTUB: Func: int copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done) */
                   1077: ENTRY(copyoutstr)
                   1078:        pushl   %esi
                   1079:        pushl   %edi
                   1080:
                   1081:        movl    12(%esp),%esi           # esi = from
                   1082:        movl    16(%esp),%edi           # edi = to
                   1083:        movl    20(%esp),%edx           # edx = maxlen
                   1084:
                   1085: #if defined(I386_CPU)
                   1086: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1087:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1088:        jne     5f
                   1089: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1090:
                   1091:        /* Compute number of bytes in first page. */
                   1092:        movl    %edi,%eax
                   1093:        andl    $PGOFSET,%eax
1.7       thorpej  1094:        movl    $PAGE_SIZE,%ecx
                   1095:        subl    %eax,%ecx               # ecx = PAGE_SIZE - (src % PAGE_SIZE)
1.1       fvdl     1096:
                   1097:        GET_CURPCB(%eax)
                   1098:        movl    $6f,PCB_ONFAULT(%eax)
                   1099:
                   1100: 1:     /*
                   1101:         * Once per page, check that we are still within the bounds of user
                   1102:         * space, and check for a write fault.
                   1103:         */
                   1104:        cmpl    $VM_MAXUSER_ADDRESS,%edi
                   1105:        jae     _C_LABEL(copystr_efault)
                   1106:
                   1107:        /* Compute PTE offset. */
                   1108:        movl    %edi,%eax
                   1109:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1110:
                   1111:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1112:        jnz     2f
                   1113:
                   1114: 6:     /* Simulate a trap. */
                   1115:        pushl   %edx
                   1116:        pushl   %edi
                   1117:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1118:        addl    $4,%esp                 # clear argument from stack
                   1119:        popl    %edx
                   1120:        testl   %eax,%eax
                   1121:        jnz     _C_LABEL(copystr_efault)
                   1122:
                   1123: 2:     /* Copy up to end of this page. */
                   1124:        subl    %ecx,%edx               # predecrement total count
                   1125:        jnc     3f
                   1126:        addl    %edx,%ecx               # ecx += (edx - ecx) = edx
                   1127:        xorl    %edx,%edx
                   1128:
                   1129: 3:     decl    %ecx
                   1130:        js      4f
                   1131:        lodsb
                   1132:        stosb
                   1133:        testb   %al,%al
                   1134:        jnz     3b
                   1135:
                   1136:        /* Success -- 0 byte reached. */
                   1137:        addl    %ecx,%edx               # add back residual for this page
                   1138:        xorl    %eax,%eax
                   1139:        jmp     copystr_return
                   1140:
                   1141: 4:     /* Go to next page, if any. */
1.7       thorpej  1142:        movl    $PAGE_SIZE,%ecx
1.1       fvdl     1143:        testl   %edx,%edx
                   1144:        jnz     1b
                   1145:
                   1146:        /* edx is zero -- return ENAMETOOLONG. */
                   1147:        movl    $ENAMETOOLONG,%eax
                   1148:        jmp     copystr_return
                   1149: #endif /* I386_CPU */
                   1150:
                   1151: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1152: 5:     GET_CURPCB(%eax)
                   1153:        movl    $_C_LABEL(copystr_fault),PCB_ONFAULT(%eax)
                   1154:        /*
                   1155:         * Get min(%edx, VM_MAXUSER_ADDRESS-%edi).
                   1156:         */
                   1157:        movl    $VM_MAXUSER_ADDRESS,%eax
                   1158:        subl    %edi,%eax
                   1159:        cmpl    %edx,%eax
                   1160:        jae     1f
                   1161:        movl    %eax,%edx
                   1162:        movl    %eax,20(%esp)
                   1163:
                   1164: 1:     incl    %edx
                   1165:        cld
                   1166:
                   1167: 1:     decl    %edx
                   1168:        jz      2f
                   1169:        lodsb
                   1170:        stosb
                   1171:        testb   %al,%al
                   1172:        jnz     1b
                   1173:
                   1174:        /* Success -- 0 byte reached. */
                   1175:        decl    %edx
                   1176:        xorl    %eax,%eax
                   1177:        jmp     copystr_return
                   1178:
                   1179: 2:     /* edx is zero -- return EFAULT or ENAMETOOLONG. */
                   1180:        cmpl    $VM_MAXUSER_ADDRESS,%edi
                   1181:        jae     _C_LABEL(copystr_efault)
                   1182:        movl    $ENAMETOOLONG,%eax
                   1183:        jmp     copystr_return
                   1184: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1185:
                   1186: /*
                   1187:  * int copyinstr(const void *from, void *to, size_t maxlen, size_t *lencopied);
                   1188:  * Copy a NUL-terminated string, at most maxlen characters long, from the
                   1189:  * user's address space.  Return the number of characters copied (including the
                   1190:  * NUL) in *lencopied.  If the string is too long, return ENAMETOOLONG; else
                   1191:  * return 0 or EFAULT.
                   1192:  * see copyinstr(9)
                   1193:  */
                   1194: /* LINTSTUB: Func: int copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) */
                   1195: ENTRY(copyinstr)
                   1196:        pushl   %esi
                   1197:        pushl   %edi
                   1198:        GET_CURPCB(%ecx)
                   1199:        movl    $_C_LABEL(copystr_fault),PCB_ONFAULT(%ecx)
                   1200:
                   1201:        movl    12(%esp),%esi           # %esi = from
                   1202:        movl    16(%esp),%edi           # %edi = to
                   1203:        movl    20(%esp),%edx           # %edx = maxlen
                   1204:
                   1205:        /*
                   1206:         * Get min(%edx, VM_MAXUSER_ADDRESS-%esi).
                   1207:         */
                   1208:        movl    $VM_MAXUSER_ADDRESS,%eax
                   1209:        subl    %esi,%eax
                   1210:        cmpl    %edx,%eax
                   1211:        jae     1f
                   1212:        movl    %eax,%edx
                   1213:        movl    %eax,20(%esp)
                   1214:
                   1215: 1:     incl    %edx
                   1216:        cld
                   1217:
                   1218: 1:     decl    %edx
                   1219:        jz      2f
                   1220:        lodsb
                   1221:        stosb
                   1222:        testb   %al,%al
                   1223:        jnz     1b
                   1224:
                   1225:        /* Success -- 0 byte reached. */
                   1226:        decl    %edx
                   1227:        xorl    %eax,%eax
                   1228:        jmp     copystr_return
                   1229:
                   1230: 2:     /* edx is zero -- return EFAULT or ENAMETOOLONG. */
                   1231:        cmpl    $VM_MAXUSER_ADDRESS,%esi
                   1232:        jae     _C_LABEL(copystr_efault)
                   1233:        movl    $ENAMETOOLONG,%eax
                   1234:        jmp     copystr_return
                   1235:
                   1236: /* LINTSTUB: Ignore */
                   1237: NENTRY(copystr_efault)
                   1238:        movl    $EFAULT,%eax
                   1239:
                   1240: /* LINTSTUB: Ignore */
                   1241: NENTRY(copystr_fault)
                   1242: copystr_return:
                   1243:        /* Set *lencopied and return %eax. */
                   1244:        GET_CURPCB(%ecx)
                   1245:        movl    $0,PCB_ONFAULT(%ecx)
                   1246:        movl    20(%esp),%ecx
                   1247:        subl    %edx,%ecx
                   1248:        movl    24(%esp),%edx
                   1249:        testl   %edx,%edx
                   1250:        jz      8f
                   1251:        movl    %ecx,(%edx)
                   1252:
                   1253: 8:     popl    %edi
                   1254:        popl    %esi
                   1255:        ret
                   1256:
                   1257: /*
                   1258:  * int copystr(const void *from, void *to, size_t maxlen, size_t *lencopied);
                   1259:  * Copy a NUL-terminated string, at most maxlen characters long.  Return the
                   1260:  * number of characters copied (including the NUL) in *lencopied.  If the
                   1261:  * string is too long, return ENAMETOOLONG; else return 0.
                   1262:  * see copystr(9)
                   1263:  */
                   1264: /* LINTSTUB: Func: int copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done) */
                   1265: ENTRY(copystr)
                   1266:        pushl   %esi
                   1267:        pushl   %edi
                   1268:
                   1269:        movl    12(%esp),%esi           # esi = from
                   1270:        movl    16(%esp),%edi           # edi = to
                   1271:        movl    20(%esp),%edx           # edx = maxlen
                   1272:        incl    %edx
                   1273:        cld
                   1274:
                   1275: 1:     decl    %edx
                   1276:        jz      4f
                   1277:        lodsb
                   1278:        stosb
                   1279:        testb   %al,%al
                   1280:        jnz     1b
                   1281:
                   1282:        /* Success -- 0 byte reached. */
                   1283:        decl    %edx
                   1284:        xorl    %eax,%eax
                   1285:        jmp     6f
                   1286:
                   1287: 4:     /* edx is zero -- return ENAMETOOLONG. */
                   1288:        movl    $ENAMETOOLONG,%eax
                   1289:
                   1290: 6:     /* Set *lencopied and return %eax. */
                   1291:        movl    20(%esp),%ecx
                   1292:        subl    %edx,%ecx
                   1293:        movl    24(%esp),%edx
                   1294:        testl   %edx,%edx
                   1295:        jz      7f
                   1296:        movl    %ecx,(%edx)
                   1297:
                   1298: 7:     popl    %edi
                   1299:        popl    %esi
                   1300:        ret
                   1301:
                   1302: /*
                   1303:  * long fuword(const void *uaddr);
                   1304:  * Fetch an int from the user's address space.
                   1305:  * see fuword(9)
                   1306:  */
                   1307: /* LINTSTUB: Func: long fuword(const void *base) */
                   1308: ENTRY(fuword)
                   1309:        movl    4(%esp),%edx
                   1310:        cmpl    $VM_MAXUSER_ADDRESS-4,%edx
                   1311:        ja      _C_LABEL(fusuaddrfault)
                   1312:        GET_CURPCB(%ecx)
                   1313:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1314:        movl    (%edx),%eax
                   1315:        movl    $0,PCB_ONFAULT(%ecx)
                   1316:        ret
                   1317:
                   1318: /*
                   1319:  * int fusword(const void *uaddr);
                   1320:  * Fetch a short from the user's address space.
                   1321:  * see fusword(9)
                   1322:  */
                   1323: /* LINTSTUB: Func: int fusword(const void *base) */
                   1324: ENTRY(fusword)
                   1325:        movl    4(%esp),%edx
                   1326:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1327:        ja      _C_LABEL(fusuaddrfault)
                   1328:        GET_CURPCB(%ecx)
                   1329:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1330:        movzwl  (%edx),%eax
                   1331:        movl    $0,PCB_ONFAULT(%ecx)
                   1332:        ret
                   1333:
                   1334: /*
                   1335:  * int fuswintr(const void *uaddr);
                   1336:  * Fetch a short from the user's address space.  Can be called during an
                   1337:  * interrupt.
                   1338:  * see fuswintr(9)
                   1339:  */
                   1340: /* LINTSTUB: Func: int fuswintr(const void *base) */
                   1341: ENTRY(fuswintr)
                   1342:        movl    4(%esp),%edx
                   1343:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1344:        ja      _C_LABEL(fusuaddrfault)
1.5       thorpej  1345:        movl    CPUVAR(CURLWP),%ecx
                   1346:        movl    L_ADDR(%ecx),%ecx
1.1       fvdl     1347:        movl    $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
                   1348:        movzwl  (%edx),%eax
                   1349:        movl    $0,PCB_ONFAULT(%ecx)
                   1350:        ret
                   1351:
                   1352: /*
                   1353:  * int fubyte(const void *uaddr);
                   1354:  * Fetch a byte from the user's address space.
                   1355:  * see fubyte(9)
                   1356:  */
                   1357: /* LINTSTUB: Func: int fubyte(const void *base) */
                   1358: ENTRY(fubyte)
                   1359:        movl    4(%esp),%edx
                   1360:        cmpl    $VM_MAXUSER_ADDRESS-1,%edx
                   1361:        ja      _C_LABEL(fusuaddrfault)
                   1362:        GET_CURPCB(%ecx)
                   1363:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1364:        movzbl  (%edx),%eax
                   1365:        movl    $0,PCB_ONFAULT(%ecx)
                   1366:        ret
                   1367:
                   1368: /*
                   1369:  * Handle faults from [fs]u*().  Clean up and return -1.
                   1370:  */
                   1371: /* LINTSTUB: Ignore */
                   1372: NENTRY(fusufault)
                   1373:        movl    $0,PCB_ONFAULT(%ecx)
                   1374:        movl    $-1,%eax
                   1375:        ret
                   1376:
                   1377: /*
                   1378:  * Handle faults from [fs]u*().  Clean up and return -1.  This differs from
                   1379:  * fusufault() in that trap() will recognize it and return immediately rather
                   1380:  * than trying to page fault.
                   1381:  */
                   1382: /* LINTSTUB: Ignore */
                   1383: NENTRY(fusubail)
                   1384:        movl    $0,PCB_ONFAULT(%ecx)
                   1385:        movl    $-1,%eax
                   1386:        ret
                   1387:
                   1388: /*
                   1389:  * Handle earlier faults from [fs]u*(), due to our of range addresses.
                   1390:  */
                   1391: /* LINTSTUB: Ignore */
                   1392: NENTRY(fusuaddrfault)
                   1393:        movl    $-1,%eax
                   1394:        ret
                   1395:
                   1396: /*
                   1397:  * int suword(void *uaddr, long x);
                   1398:  * Store an int in the user's address space.
                   1399:  * see suword(9)
                   1400:  */
                   1401: /* LINTSTUB: Func: int suword(void *base, long c) */
                   1402: ENTRY(suword)
                   1403:        movl    4(%esp),%edx
                   1404:        cmpl    $VM_MAXUSER_ADDRESS-4,%edx
                   1405:        ja      _C_LABEL(fusuaddrfault)
                   1406:
                   1407: #if defined(I386_CPU)
                   1408: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1409:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1410:        jne     2f
                   1411: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1412:
                   1413:        GET_CURPCB(%eax)
                   1414:        movl    $3f,PCB_ONFAULT(%eax)
                   1415:
                   1416:        movl    %edx,%eax
                   1417:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1418:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1419:        jnz     1f
                   1420:
                   1421: 3:     /* Simulate a trap. */
                   1422:        pushl   %edx
                   1423:        pushl   %edx
                   1424:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1425:        addl    $4,%esp                 # clear parameter from the stack
                   1426:        popl    %edx
                   1427:        GET_CURPCB(%ecx)
                   1428:        testl   %eax,%eax
                   1429:        jnz     _C_LABEL(fusufault)
                   1430:
                   1431: 1:     /* XXX also need to check the following 3 bytes for validity! */
                   1432: #endif
                   1433:
                   1434: 2:     GET_CURPCB(%ecx)
                   1435:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1436:
                   1437:        movl    8(%esp),%eax
                   1438:        movl    %eax,(%edx)
                   1439:        xorl    %eax,%eax
                   1440:        movl    %eax,PCB_ONFAULT(%ecx)
                   1441:        ret
                   1442:
                   1443: /*
                   1444:  * int susword(void *uaddr, short x);
                   1445:  * Store a short in the user's address space.
                   1446:  * see susword(9)
                   1447:  */
                   1448: /* LINTSTUB: Func: int susword(void *base, short c) */
                   1449: ENTRY(susword)
                   1450:        movl    4(%esp),%edx
                   1451:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1452:        ja      _C_LABEL(fusuaddrfault)
                   1453:
                   1454: #if defined(I386_CPU)
                   1455: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1456:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1457:        jne     2f
                   1458: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1459:
                   1460:        GET_CURPCB(%eax)
                   1461:        movl    $3f,PCB_ONFAULT(%eax)
                   1462:
                   1463:        movl    %edx,%eax
                   1464:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1465:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1466:        jnz     1f
                   1467:
                   1468: 3:     /* Simulate a trap. */
                   1469:        pushl   %edx
                   1470:        pushl   %edx
                   1471:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1472:        addl    $4,%esp                 # clear parameter from the stack
                   1473:        popl    %edx
                   1474:        GET_CURPCB(%ecx)
                   1475:        testl   %eax,%eax
                   1476:        jnz     _C_LABEL(fusufault)
                   1477:
                   1478: 1:     /* XXX also need to check the following byte for validity! */
                   1479: #endif
                   1480:
                   1481: 2:     GET_CURPCB(%ecx)
                   1482:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1483:
                   1484:        movl    8(%esp),%eax
                   1485:        movw    %ax,(%edx)
                   1486:        xorl    %eax,%eax
                   1487:        movl    %eax,PCB_ONFAULT(%ecx)
                   1488:        ret
                   1489:
                   1490: /*
                   1491:  * int suswintr(void *uaddr, short x);
                   1492:  * Store a short in the user's address space.  Can be called during an
                   1493:  * interrupt.
                   1494:  * see suswintr(9)
                   1495:  */
                   1496: /* LINTSTUB: Func: int suswintr(void *base, short c) */
                   1497: ENTRY(suswintr)
                   1498:        movl    4(%esp),%edx
                   1499:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1500:        ja      _C_LABEL(fusuaddrfault)
1.5       thorpej  1501:        movl    CPUVAR(CURLWP),%ecx
                   1502:        movl    L_ADDR(%ecx),%ecx
1.1       fvdl     1503:        movl    $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
                   1504:
                   1505: #if defined(I386_CPU)
                   1506: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1507:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1508:        jne     2f
                   1509: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1510:
                   1511:        movl    %edx,%eax
                   1512:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1513:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1514:        jnz     1f
                   1515:
                   1516:        /* Simulate a trap. */
                   1517:        jmp     _C_LABEL(fusubail)
                   1518:
                   1519: 1:     /* XXX also need to check the following byte for validity! */
                   1520: #endif
                   1521:
                   1522: 2:     movl    8(%esp),%eax
                   1523:        movw    %ax,(%edx)
                   1524:        xorl    %eax,%eax
                   1525:        movl    %eax,PCB_ONFAULT(%ecx)
                   1526:        ret
                   1527:
                   1528: /*
                   1529:  * int subyte(void *uaddr, char x);
                   1530:  * Store a byte in the user's address space.
                   1531:  * see subyte(9)
                   1532:  */
                   1533: /* LINTSTUB: Func: int subyte(void *base, int c) */
                   1534: ENTRY(subyte)
                   1535:        movl    4(%esp),%edx
                   1536:        cmpl    $VM_MAXUSER_ADDRESS-1,%edx
                   1537:        ja      _C_LABEL(fusuaddrfault)
                   1538:
                   1539: #if defined(I386_CPU)
                   1540: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1541:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1542:        jne     2f
                   1543: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1544:
                   1545:        GET_CURPCB(%eax)
                   1546:        movl    $3f,PCB_ONFAULT(%eax)
                   1547:
                   1548:        movl    %edx,%eax
                   1549:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1550:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1551:        jnz     1f
                   1552:
                   1553: 3:     /* Simulate a trap. */
                   1554:        pushl   %edx
                   1555:        pushl   %edx
                   1556:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1557:        addl    $4,%esp                 # clear parameter from the stack
                   1558:        popl    %edx
                   1559:        GET_CURPCB(%ecx)
                   1560:        testl   %eax,%eax
                   1561:        jnz     _C_LABEL(fusufault)
                   1562:
                   1563: 1:
                   1564: #endif
                   1565:
                   1566: 2:     GET_CURPCB(%ecx)
                   1567:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1568:
                   1569:        movb    8(%esp),%al
                   1570:        movb    %al,(%edx)
                   1571:        xorl    %eax,%eax
                   1572:        movl    %eax,PCB_ONFAULT(%ecx)
                   1573:        ret
                   1574:
                   1575: /*****************************************************************************/
                   1576:
                   1577: /*
                   1578:  * The following is i386-specific nonsense.
                   1579:  */
                   1580:
                   1581: /*
                   1582:  * void lgdt(struct region_descriptor *rdp);
                   1583:  * Load a new GDT pointer (and do any necessary cleanup).
                   1584:  * XXX It's somewhat questionable whether reloading all the segment registers
                   1585:  * is necessary, since the actual descriptor data is not changed except by
                   1586:  * process creation and exit, both of which clean up via task switches.  OTOH,
                   1587:  * this only happens at run time when the GDT is resized.
                   1588:  */
                   1589: /* LINTSTUB: Func: void lgdt(struct region_descriptor *rdp) */
                   1590: NENTRY(lgdt)
                   1591:        /* Reload the descriptor table. */
                   1592:        movl    4(%esp),%eax
                   1593:        lgdt    (%eax)
                   1594:        /* Flush the prefetch queue. */
                   1595:        jmp     1f
                   1596:        nop
                   1597: 1:     /* Reload "stale" selectors. */
                   1598:        movl    $GSEL(GDATA_SEL, SEL_KPL),%eax
                   1599:        movw    %ax,%ds
                   1600:        movw    %ax,%es
                   1601:        movw    %ax,%gs
                   1602:        movw    %ax,%ss
                   1603:        movl    $GSEL(GCPU_SEL, SEL_KPL),%eax
                   1604:        movw    %ax,%fs
                   1605:        /* Reload code selector by doing intersegment return. */
                   1606:        popl    %eax
                   1607:        pushl   $GSEL(GCODE_SEL, SEL_KPL)
                   1608:        pushl   %eax
                   1609:        lret
                   1610:
                   1611: /*****************************************************************************/
                   1612:
                   1613: /*
                   1614:  * These functions are primarily used by DDB.
                   1615:  */
                   1616:
                   1617: /* LINTSTUB: Func: int setjmp (label_t *l) */
                   1618: ENTRY(setjmp)
                   1619:        movl    4(%esp),%eax
                   1620:        movl    %ebx,(%eax)             # save ebx
                   1621:        movl    %esp,4(%eax)            # save esp
                   1622:        movl    %ebp,8(%eax)            # save ebp
                   1623:        movl    %esi,12(%eax)           # save esi
                   1624:        movl    %edi,16(%eax)           # save edi
                   1625:        movl    (%esp),%edx             # get rta
                   1626:        movl    %edx,20(%eax)           # save eip
                   1627:        xorl    %eax,%eax               # return (0);
                   1628:        ret
                   1629:
                   1630: /* LINTSTUB: Func: void longjmp (label_t *l) */
                   1631: ENTRY(longjmp)
                   1632:        movl    4(%esp),%eax
                   1633:        movl    (%eax),%ebx             # restore ebx
                   1634:        movl    4(%eax),%esp            # restore esp
                   1635:        movl    8(%eax),%ebp            # restore ebp
                   1636:        movl    12(%eax),%esi           # restore esi
                   1637:        movl    16(%eax),%edi           # restore edi
                   1638:        movl    20(%eax),%edx           # get rta
                   1639:        movl    %edx,(%esp)             # put in return frame
                   1640:        xorl    %eax,%eax               # return (1);
                   1641:        incl    %eax
                   1642:        ret
                   1643:
                   1644: /*****************************************************************************/
                   1645:
                   1646:        .globl  _C_LABEL(sched_whichqs),_C_LABEL(sched_qs)
                   1647:        .globl  _C_LABEL(uvmexp),_C_LABEL(panic)
                   1648:
                   1649: #ifdef DIAGNOSTIC
                   1650: NENTRY(switch_error)
                   1651:        pushl   $1f
                   1652:        call    _C_LABEL(panic)
                   1653:        /* NOTREACHED */
                   1654: 1:     .asciz  "cpu_switch"
                   1655: #endif /* DIAGNOSTIC */
                   1656:
                   1657: /*
1.5       thorpej  1658:  * void cpu_switch(struct lwp *)
1.1       fvdl     1659:  * Find a runnable process and switch to it.  Wait if necessary.  If the new
                   1660:  * process is the same as the old one, we short-circuit the context save and
                   1661:  * restore.
                   1662:  *
                   1663:  * Note that the stack frame layout is known to "struct switchframe"
                   1664:  * in <machine/frame.h> and to the code in cpu_fork() which initializes
1.5       thorpej  1665:  * it for a new lwp.
1.1       fvdl     1666:  */
                   1667: ENTRY(cpu_switch)
                   1668:        pushl   %ebx
                   1669:        pushl   %esi
                   1670:        pushl   %edi
                   1671:
                   1672: #ifdef DEBUG
                   1673:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                   1674:        jae     1f
1.5       thorpej  1675:        pushl   $2f
1.1       fvdl     1676:        call    _C_LABEL(panic)
                   1677:        /* NOTREACHED */
1.5       thorpej  1678: 2:     .asciz  "not splsched() in cpu_switch!"
1.1       fvdl     1679: 1:
                   1680: #endif /* DEBUG */
                   1681:
1.5       thorpej  1682:        movl    16(%esp),%esi           # current
1.1       fvdl     1683:
                   1684:        /*
1.5       thorpej  1685:         * Clear curlwp so that we don't accumulate system time while idle.
                   1686:         * This also insures that schedcpu() will move the old lwp to
1.1       fvdl     1687:         * the correct queue if it happens to get called from the spllower()
                   1688:         * below and changes the priority.  (See corresponding comment in
                   1689:         * userret()).
                   1690:         */
1.5       thorpej  1691:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl     1692:        /*
1.5       thorpej  1693:         * First phase: find new lwp.
1.1       fvdl     1694:         *
                   1695:         * Registers:
                   1696:         *   %eax - queue head, scratch, then zero
                   1697:         *   %ebx - queue number
                   1698:         *   %ecx - cached value of whichqs
1.5       thorpej  1699:         *   %edx - next lwp in queue
                   1700:         *   %esi - old lwp
                   1701:         *   %edi - new lwp
1.1       fvdl     1702:         */
                   1703:
1.5       thorpej  1704:        /* Look for new lwp. */
1.1       fvdl     1705:        cli                             # splhigh doesn't do a cli
                   1706:        movl    _C_LABEL(sched_whichqs),%ecx
                   1707:        bsfl    %ecx,%ebx               # find a full q
                   1708:        jnz     switch_dequeue
                   1709:
                   1710:        /*
                   1711:         * idling:      save old context.
                   1712:         *
                   1713:         * Registers:
                   1714:         *   %eax, %ecx - scratch
1.5       thorpej  1715:         *   %esi - old lwp, then old pcb
1.1       fvdl     1716:         *   %edi - idle pcb
                   1717:         */
                   1718:
                   1719:        pushl   %esi
                   1720:        call    _C_LABEL(pmap_deactivate)       # pmap_deactivate(oldproc)
                   1721:        addl    $4,%esp
                   1722:
1.5       thorpej  1723:        movl    L_ADDR(%esi),%esi
1.1       fvdl     1724:
                   1725:        /* Save stack pointers. */
                   1726:        movl    %esp,PCB_ESP(%esi)
                   1727:        movl    %ebp,PCB_EBP(%esi)
                   1728:
                   1729:        /* Find idle PCB for this CPU */
                   1730: #ifndef MULTIPROCESSOR
1.5       thorpej  1731:        movl    $_C_LABEL(lwp0),%ebx
                   1732:        movl    L_ADDR(%ebx),%edi
                   1733:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl     1734: #else
                   1735:        movl    CPUVAR(IDLE_PCB),%edi
                   1736:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                   1737: #endif
1.5       thorpej  1738:        movl    $0,CPUVAR(CURLWP)               /* In case we fault... */
1.1       fvdl     1739:
                   1740:        /* Restore the idle context (avoid interrupts) */
                   1741:        cli
                   1742:
                   1743:        /* Restore stack pointers. */
                   1744:        movl    PCB_ESP(%edi),%esp
                   1745:        movl    PCB_EBP(%edi),%ebp
                   1746:
                   1747:
                   1748:        /* Switch address space. */
                   1749:        movl    PCB_CR3(%edi),%ecx
                   1750:        movl    %ecx,%cr3
                   1751:
                   1752:        /* Switch TSS. Reset "task busy" flag before loading. */
                   1753: #ifdef MULTIPROCESSOR
                   1754:        movl    CPUVAR(GDT),%eax
                   1755: #else
                   1756:        movl    _C_LABEL(gdt),%eax
                   1757: #endif
                   1758:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                   1759:        ltr     %dx
                   1760:
                   1761:        /* We're always in the kernel, so we don't need the LDT. */
                   1762:
                   1763:        /* Restore cr0 (including FPU state). */
                   1764:        movl    PCB_CR0(%edi),%ecx
                   1765:        movl    %ecx,%cr0
                   1766:
                   1767:        /* Record new pcb. */
                   1768:        SET_CURPCB(%edi)
                   1769:
                   1770:        xorl    %esi,%esi
                   1771:        sti
                   1772: idle_unlock:
                   1773: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   1774:        call    _C_LABEL(sched_unlock_idle)
                   1775: #endif
                   1776:        /* Interrupts are okay again. */
1.2       fvdl     1777:        pushl   $IPL_NONE               # spl0()
1.1       fvdl     1778:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl     1779:        addl    $4,%esp
1.1       fvdl     1780:        jmp     idle_start
                   1781: idle_zero:
                   1782:        sti
                   1783:        call    _C_LABEL(uvm_pageidlezero)
                   1784:        cli
                   1785:        cmpl    $0,_C_LABEL(sched_whichqs)
                   1786:        jnz     idle_exit
                   1787: idle_loop:
                   1788:        /* Try to zero some pages. */
                   1789:        movl    _C_LABEL(uvm)+UVM_PAGE_IDLE_ZERO,%ecx
                   1790:        testl   %ecx,%ecx
                   1791:        jnz     idle_zero
                   1792:        sti
                   1793:        hlt
                   1794: NENTRY(mpidle)
                   1795: idle_start:
                   1796:        cli
                   1797:        cmpl    $0,_C_LABEL(sched_whichqs)
                   1798:        jz      idle_loop
                   1799: idle_exit:
                   1800:        movl    $IPL_HIGH,CPUVAR(ILEVEL)                # splhigh
1.14      fvdl     1801:        sti
1.1       fvdl     1802: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   1803:        call    _C_LABEL(sched_lock_idle)
                   1804: #endif
                   1805:        movl    _C_LABEL(sched_whichqs),%ecx
                   1806:        bsfl    %ecx,%ebx
                   1807:        jz      idle_unlock
                   1808:
                   1809: switch_dequeue:
                   1810:        /*
                   1811:         * we're running at splhigh(), but it's otherwise okay to take
                   1812:         * interrupts here.
                   1813:         */
                   1814:        sti
                   1815:        leal    _C_LABEL(sched_qs)(,%ebx,8),%eax # select q
                   1816:
1.5       thorpej  1817:        movl    L_FORW(%eax),%edi       # unlink from front of process q
1.1       fvdl     1818: #ifdef DIAGNOSTIC
                   1819:        cmpl    %edi,%eax               # linked to self (i.e. nothing queued)?
                   1820:        je      _C_LABEL(switch_error)  # not possible
                   1821: #endif /* DIAGNOSTIC */
1.5       thorpej  1822:        movl    L_FORW(%edi),%edx
                   1823:        movl    %edx,L_FORW(%eax)
                   1824:        movl    %eax,L_BACK(%edx)
1.1       fvdl     1825:
                   1826:        cmpl    %edx,%eax               # q empty?
                   1827:        jne     3f
                   1828:
                   1829:        btrl    %ebx,%ecx               # yes, clear to indicate empty
                   1830:        movl    %ecx,_C_LABEL(sched_whichqs) # update q status
                   1831:
                   1832: 3:     /* We just did it. */
                   1833:        xorl    %eax,%eax
                   1834:        CLEAR_RESCHED(%eax)
                   1835:
1.5       thorpej  1836: switch_resume:
1.1       fvdl     1837: #ifdef DIAGNOSTIC
1.5       thorpej  1838:        cmpl    %eax,L_WCHAN(%edi)      # Waiting for something?
1.1       fvdl     1839:        jne     _C_LABEL(switch_error)  # Yes; shouldn't be queued.
1.5       thorpej  1840:        cmpb    $LSRUN,L_STAT(%edi)     # In run state?
1.1       fvdl     1841:        jne     _C_LABEL(switch_error)  # No; shouldn't be queued.
                   1842: #endif /* DIAGNOSTIC */
                   1843:
1.5       thorpej  1844:        /* Isolate lwp.  XXX Is this necessary? */
                   1845:        movl    %eax,L_BACK(%edi)
1.1       fvdl     1846:
1.5       thorpej  1847:        /* Record new lwp. */
                   1848:        movb    $LSONPROC,L_STAT(%edi)  # l->l_stat = LSONPROC
                   1849:        SET_CURLWP(%edi,%ecx)
1.1       fvdl     1850:
1.5       thorpej  1851:        /* Skip context switch if same lwp. */
1.10      fvdl     1852:        xorl    %ebx,%ebx
1.1       fvdl     1853:        cmpl    %edi,%esi
                   1854:        je      switch_return
                   1855:
1.5       thorpej  1856:        /* If old lwp exited, don't bother. */
1.1       fvdl     1857:        testl   %esi,%esi
                   1858:        jz      switch_exited
                   1859:
                   1860:        /*
                   1861:         * Second phase: save old context.
                   1862:         *
                   1863:         * Registers:
                   1864:         *   %eax, %ecx - scratch
1.5       thorpej  1865:         *   %esi - old lwp, then old pcb
                   1866:         *   %edi - new lwp
1.1       fvdl     1867:         */
                   1868:
                   1869:        pushl   %esi
                   1870:        call    _C_LABEL(pmap_deactivate)       # pmap_deactivate(oldproc)
                   1871:        addl    $4,%esp
                   1872:
1.5       thorpej  1873:        movl    L_ADDR(%esi),%esi
1.1       fvdl     1874:
                   1875:        /* Save stack pointers. */
                   1876:        movl    %esp,PCB_ESP(%esi)
                   1877:        movl    %ebp,PCB_EBP(%esi)
                   1878:
                   1879: switch_exited:
                   1880:        /*
                   1881:         * Third phase: restore saved context.
                   1882:         *
                   1883:         * Registers:
                   1884:         *   %eax, %ebx, %ecx, %edx - scratch
                   1885:         *   %esi - new pcb
1.5       thorpej  1886:         *   %edi - new lwp
1.1       fvdl     1887:         */
                   1888:
                   1889:        /* No interrupts while loading new state. */
                   1890:        cli
1.5       thorpej  1891:        movl    L_ADDR(%edi),%esi
1.1       fvdl     1892:
                   1893:        /* Restore stack pointers. */
                   1894:        movl    PCB_ESP(%esi),%esp
                   1895:        movl    PCB_EBP(%esi),%ebp
                   1896:
                   1897: #if 0
                   1898:        /* Don't bother with the rest if switching to a system process. */
1.5       thorpej  1899:        testl   $P_SYSTEM,L_FLAG(%edi); XXX NJWLWP lwp's don't have P_SYSTEM!
1.1       fvdl     1900:        jnz     switch_restored
                   1901: #endif
                   1902:
                   1903: #ifdef MULTIPROCESSOR
                   1904:        movl    CPUVAR(GDT),%eax
                   1905: #else
                   1906:        /* Load TSS info. */
                   1907:        movl    _C_LABEL(gdt),%eax
                   1908: #endif
1.5       thorpej  1909:        movl    L_MD_TSS_SEL(%edi),%edx
1.1       fvdl     1910:
                   1911:        /* Switch TSS. Reset "task busy" flag before loading. */
                   1912:        andl    $~0x0200,4(%eax,%edx, 1)
                   1913:        ltr     %dx
                   1914:
                   1915:        pushl   %edi
                   1916:        call    _C_LABEL(pmap_activate)         # pmap_activate(p)
                   1917:        addl    $4,%esp
                   1918:
                   1919: #if 0
                   1920: switch_restored:
                   1921: #endif
                   1922:        /* Restore cr0 (including FPU state). */
                   1923:        movl    PCB_CR0(%esi),%ecx
                   1924: #ifdef MULTIPROCESSOR
                   1925:        /*
                   1926:         * If our floating point registers are on a different cpu,
                   1927:         * clear CR0_TS so we'll trap rather than reuse bogus state.
                   1928:         */
                   1929:        movl    PCB_FPCPU(%esi),%ebx
                   1930:        cmpl    CPUVAR(SELF),%ebx
                   1931:        jz      1f
                   1932:        orl     $CR0_TS,%ecx
                   1933: 1:
                   1934: #endif
                   1935:        movl    %ecx,%cr0
                   1936:
                   1937:        /* Record new pcb. */
                   1938:        SET_CURPCB(%esi)
                   1939:
                   1940:        /* Interrupts are okay again. */
                   1941:        sti
                   1942:
                   1943: /*
                   1944:  *  Check for restartable atomic sequences (RAS)
                   1945:  */
1.5       thorpej  1946:        movl    CPUVAR(CURLWP),%edi
                   1947:        movl    L_PROC(%edi),%esi
                   1948:        cmpl    $0,P_NRAS(%esi)
1.1       fvdl     1949:        je      1f
1.5       thorpej  1950:        movl    L_MD_REGS(%edi),%ebx
1.4       gmcgarry 1951:        movl    TF_EIP(%ebx),%eax
1.1       fvdl     1952:        pushl   %eax
1.5       thorpej  1953:        pushl   %esi
1.1       fvdl     1954:        call    _C_LABEL(ras_lookup)
                   1955:        addl    $8,%esp
                   1956:        cmpl    $-1,%eax
                   1957:        je      1f
1.4       gmcgarry 1958:        movl    %eax,TF_EIP(%ebx)
1.1       fvdl     1959: 1:
1.10      fvdl     1960:        movl    $1,%ebx
1.1       fvdl     1961:
                   1962: switch_return:
                   1963: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   1964:        call    _C_LABEL(sched_unlock_idle)
                   1965: #endif
1.2       fvdl     1966:        pushl   $IPL_NONE               # spl0()
1.1       fvdl     1967:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl     1968:        addl    $4,%esp
1.1       fvdl     1969:        movl    $IPL_HIGH,CPUVAR(ILEVEL)        # splhigh()
1.9       fvdl     1970:
                   1971:        movl    %ebx,%eax
1.1       fvdl     1972:
                   1973:        popl    %edi
                   1974:        popl    %esi
                   1975:        popl    %ebx
                   1976:        ret
                   1977:
                   1978: /*
1.5       thorpej  1979:  * void cpu_switchto(struct lwp *current, struct lwp *next)
                   1980:  * Switch to the specified next LWP.
                   1981:  */
                   1982: ENTRY(cpu_switchto)
                   1983:        pushl   %ebx
                   1984:        pushl   %esi
                   1985:        pushl   %edi
                   1986:
                   1987: #ifdef DEBUG
                   1988:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                   1989:        jae     1f
                   1990:        pushl   $2f
                   1991:        call    _C_LABEL(panic)
                   1992:        /* NOTREACHED */
                   1993: 2:     .asciz  "not splsched() in cpu_switchto!"
                   1994: 1:
                   1995: #endif /* DEBUG */
                   1996:
                   1997:        movl    16(%esp),%esi           # current
                   1998:        movl    20(%esp),%edi           # next
                   1999:
                   2000:        /*
                   2001:         * Clear curlwp so that we don't accumulate system time while idle.
                   2002:         * This also insures that schedcpu() will move the old process to
                   2003:         * the correct queue if it happens to get called from the spllower()
                   2004:         * below and changes the priority.  (See corresponding comment in
                   2005:         * usrret()).
                   2006:         *
                   2007:         * XXX Is this necessary?  We know we won't go idle.
                   2008:         */
                   2009:        movl    $0,CPUVAR(CURLWP)
                   2010:
                   2011:        /*
                   2012:         * We're running at splhigh(), but it's otherwise okay to take
                   2013:         * interrupts here.
                   2014:         */
                   2015:        sti
                   2016:
                   2017:        /* Jump into the middle of cpu_switch */
                   2018:        xorl    %eax,%eax
                   2019:        jmp     switch_resume
                   2020:
                   2021: /*
                   2022:  * void switch_exit(struct lwp *l, void (*exit)(struct lwp *));
                   2023:  * Switch to the appropriate idle context (lwp0's if uniprocessor; the cpu's
1.1       fvdl     2024:  * if multiprocessor) and deallocate the address space and kernel stack for p.
                   2025:  * Then jump into cpu_switch(), as if we were in the idle proc all along.
                   2026:  */
                   2027: #ifndef MULTIPROCESSOR
1.5       thorpej  2028:        .globl  _C_LABEL(lwp0)
1.1       fvdl     2029: #endif
                   2030:        .globl  _C_LABEL(uvmspace_free),_C_LABEL(kernel_map)
                   2031:        .globl  _C_LABEL(uvm_km_free),_C_LABEL(tss_free)
1.6       drochner 2032: /* LINTSTUB: Func: void switch_exit(struct lwp *l, void (*exit)(struct lwp *)) */
1.1       fvdl     2033: ENTRY(switch_exit)
                   2034:        movl    4(%esp),%edi            # old process
1.5       thorpej  2035:        movl    8(%esp),%eax            # exit func
1.1       fvdl     2036: #ifndef MULTIPROCESSOR
1.5       thorpej  2037:        movl    $_C_LABEL(lwp0),%ebx
                   2038:        movl    L_ADDR(%ebx),%esi
                   2039:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl     2040: #else
                   2041:        movl    CPUVAR(IDLE_PCB),%esi
                   2042:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                   2043: #endif
                   2044:        /* In case we fault... */
1.5       thorpej  2045:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl     2046:
                   2047:        /* Restore the idle context. */
                   2048:        cli
                   2049:
                   2050:        /* Restore stack pointers. */
                   2051:        movl    PCB_ESP(%esi),%esp
                   2052:        movl    PCB_EBP(%esi),%ebp
                   2053:
1.5       thorpej  2054:        /* Save exit func. */
                   2055:        pushl   %eax
                   2056:
1.1       fvdl     2057:        /* Load TSS info. */
                   2058: #ifdef MULTIPROCESSOR
                   2059:        movl    CPUVAR(GDT),%eax
                   2060: #else
                   2061:        /* Load TSS info. */
                   2062:        movl    _C_LABEL(gdt),%eax
                   2063: #endif
                   2064:
                   2065:        /* Switch address space. */
                   2066:        movl    PCB_CR3(%esi),%ecx
                   2067:        movl    %ecx,%cr3
                   2068:
                   2069:        /* Switch TSS. */
                   2070:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                   2071:        ltr     %dx
                   2072:
                   2073:        /* We're always in the kernel, so we don't need the LDT. */
                   2074:
                   2075:        /* Restore cr0 (including FPU state). */
                   2076:        movl    PCB_CR0(%esi),%ecx
                   2077:        movl    %ecx,%cr0
                   2078:
                   2079:        /* Record new pcb. */
                   2080:        SET_CURPCB(%esi)
                   2081:
                   2082:        /* Interrupts are okay again. */
                   2083:        sti
                   2084:
                   2085:        /*
                   2086:         * Schedule the dead process's vmspace and stack to be freed.
                   2087:         */
1.5       thorpej  2088:        movl    0(%esp),%eax            /* %eax = exit func */
                   2089:        movl    %edi,0(%esp)            /* {lwp_}exit2(l) */
                   2090:        call    *%eax
1.1       fvdl     2091:        addl    $4,%esp
                   2092:
                   2093:        /* Jump into cpu_switch() with the right state. */
                   2094:        xorl    %esi,%esi
1.5       thorpej  2095:        movl    %esi,CPUVAR(CURLWP)
1.1       fvdl     2096:        jmp     idle_start
                   2097:
                   2098: /*
                   2099:  * void savectx(struct pcb *pcb);
                   2100:  * Update pcb, saving current processor state.
                   2101:  */
                   2102: /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
                   2103: ENTRY(savectx)
                   2104:        movl    4(%esp),%edx            # edx = p->p_addr
                   2105:
                   2106:        /* Save stack pointers. */
                   2107:        movl    %esp,PCB_ESP(%edx)
                   2108:        movl    %ebp,PCB_EBP(%edx)
                   2109:
                   2110:        ret
                   2111:
                   2112: /*
                   2113:  * Old call gate entry for syscall
                   2114:  */
                   2115: /* LINTSTUB: Var: char Xosyscall[1]; */
                   2116: IDTVEC(osyscall)
                   2117:        /* Set eflags in trap frame. */
                   2118:        pushfl
                   2119:        popl    8(%esp)
                   2120:        pushl   $7              # size of instruction for restart
                   2121:        jmp     syscall1
                   2122:
                   2123: /*
                   2124:  * Trap gate entry for syscall
                   2125:  */
                   2126: /* LINTSTUB: Var: char Xsyscall[1]; */
                   2127: IDTVEC(syscall)
                   2128:        pushl   $2              # size of instruction for restart
                   2129: syscall1:
                   2130:        pushl   $T_ASTFLT       # trap # for doing ASTs
                   2131:        INTRENTRY
                   2132:
                   2133: #ifdef DIAGNOSTIC
                   2134:        movl    CPUVAR(ILEVEL),%ebx
                   2135:        testl   %ebx,%ebx
                   2136:        jz      1f
                   2137:        pushl   $5f
                   2138:        call    _C_LABEL(printf)
                   2139:        addl    $4,%esp
                   2140: #ifdef DDB
                   2141:        int     $3
                   2142: #endif
                   2143: 1:
                   2144: #endif /* DIAGNOSTIC */
1.5       thorpej  2145:        movl    CPUVAR(CURLWP),%edx
                   2146:        movl    %esp,L_MD_REGS(%edx)    # save pointer to frame
                   2147:        movl    L_PROC(%edx),%edx
1.15      fvdl     2148:        pushl   %esp
1.1       fvdl     2149:        call    *P_MD_SYSCALL(%edx)     # get pointer to syscall() function
1.15      fvdl     2150:        addl    $4,%esp
1.1       fvdl     2151: 2:     /* Check for ASTs on exit to user mode. */
                   2152:        cli
1.5       thorpej  2153:        CHECK_ASTPENDING(%eax)
1.1       fvdl     2154:        je      1f
                   2155:        /* Always returning to user mode here. */
1.5       thorpej  2156:        CLEAR_ASTPENDING(%eax)
1.1       fvdl     2157:        sti
                   2158:        /* Pushed T_ASTFLT into tf_trapno on entry. */
1.15      fvdl     2159:        pushl   %esp
1.1       fvdl     2160:        call    _C_LABEL(trap)
1.15      fvdl     2161:        addl    $4,%esp
1.1       fvdl     2162:        jmp     2b
                   2163: #ifndef DIAGNOSTIC
                   2164: 1:     INTRFASTEXIT
                   2165: #else /* DIAGNOSTIC */
                   2166: 1:     cmpl    $IPL_NONE,CPUVAR(ILEVEL)
                   2167:        jne     3f
                   2168:        INTRFASTEXIT
                   2169: 3:     sti
                   2170:        pushl   $4f
                   2171:        call    _C_LABEL(printf)
                   2172:        addl    $4,%esp
                   2173: #ifdef DDB
                   2174:        int     $3
                   2175: #endif /* DDB */
                   2176:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                   2177:        jmp     2b
                   2178: 4:     .asciz  "WARNING: SPL NOT LOWERED ON SYSCALL EXIT\n"
                   2179: 5:     .asciz  "WARNING: SPL NOT ZERO ON SYSCALL ENTRY\n"
                   2180: #endif /* DIAGNOSTIC */
                   2181:
                   2182: #if NNPX > 0
                   2183: /*
                   2184:  * Special interrupt handlers.  Someday intr0-intr15 will be used to count
                   2185:  * interrupts.  We'll still need a special exception 16 handler.  The busy
                   2186:  * latch stuff in probintr() can be moved to npxprobe().
                   2187:  */
                   2188:
                   2189: /* LINTSTUB: Func: void probeintr(void) */
                   2190: NENTRY(probeintr)
                   2191:        ss
                   2192:        incl    _C_LABEL(npx_intrs_while_probing)
                   2193:        pushl   %eax
                   2194:        movb    $0x20,%al       # EOI (asm in strings loses cpp features)
                   2195:        outb    %al,$0xa0       # IO_ICU2
                   2196:        outb    %al,$0x20       # IO_ICU1
                   2197:        movb    $0,%al
                   2198:        outb    %al,$0xf0       # clear BUSY# latch
                   2199:        popl    %eax
                   2200:        iret
                   2201:
                   2202: /* LINTSTUB: Func: void probetrap(void) */
                   2203: NENTRY(probetrap)
                   2204:        ss
                   2205:        incl    _C_LABEL(npx_traps_while_probing)
                   2206:        fnclex
                   2207:        iret
                   2208:
                   2209: /* LINTSTUB: Func: int npx586bug1(int a, int b) */
                   2210: NENTRY(npx586bug1)
                   2211:        fildl   4(%esp)         # x
                   2212:        fildl   8(%esp)         # y
                   2213:        fld     %st(1)
                   2214:        fdiv    %st(1),%st      # x/y
                   2215:        fmulp   %st,%st(1)      # (x/y)*y
                   2216:        fsubrp  %st,%st(1)      # x-(x/y)*y
                   2217:        pushl   $0
                   2218:        fistpl  (%esp)
                   2219:        popl    %eax
                   2220:        ret
                   2221: #endif /* NNPX > 0 */

CVSweb <webmaster@jp.NetBSD.org>