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

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

CVSweb <webmaster@jp.NetBSD.org>