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

1.30    ! junyoung    1: /*     $NetBSD: locore.S,v 1.29 2004/08/05 15:25:29 mycroft 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)
1.30    ! junyoung  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.30    ! junyoung  118:
1.1       fvdl      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:
1.30    ! junyoung  126: #define GET_CURPCB(reg)                        movl    CPUVAR(CURPCB),reg
1.1       fvdl      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
1.30    ! junyoung  155:
1.1       fvdl      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
1.30    ! junyoung  194:        .globl  _C_LABEL(lapic_tpr)
        !           195:
1.1       fvdl      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
1.30    ! junyoung  205: _C_LABEL(lapic_id):
1.1       fvdl      206:        .long   0x00000000
                    207:        .space  LAPIC_TPRI-(LAPIC_ID+4)
1.30    ! junyoung  208: _C_LABEL(lapic_tpr):
1.1       fvdl      209:        .space  LAPIC_PPRI-LAPIC_TPRI
1.30    ! junyoung  210: _C_LABEL(lapic_ppr):
1.1       fvdl      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
1.30    ! junyoung  215: _C_LABEL(lapic_tpr):
1.1       fvdl      216:        .long 0
                    217: #endif
1.30    ! junyoung  218:
1.1       fvdl      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
1.30    ! junyoung  236:
1.1       fvdl      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:  *
1.30    ! junyoung  500:  * text | data | bss | [syms] | page dir | proc0 kstack
1.1       fvdl      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
1.29      mycroft   527:         * how many entries it will have.  Adjust nkpde to the actual
                    528:         * kernel size automatically.  Account for the bootstrap tables,
                    529:         * round up, and add an extra 4MB.
1.1       fvdl      530:         */
1.29      mycroft   531:        leal    TABLESIZE+NBPD+PDOFSET(%edi),%eax
                    532:        shrl    $PDSHIFT,%eax
1.1       fvdl      533:        movl    RELOC(nkpde),%ecx               # get nkpde
1.29      mycroft   534:        cmpl    %ecx,%eax
                    535:        jb      1f
                    536:        movl    %eax,%ecx
                    537: 1:     cmpl    $NKPTP_MIN,%ecx                 # larger than min?
1.1       fvdl      538:        jge     1f
                    539:        movl    $NKPTP_MIN,%ecx                 # set at min
                    540:        jmp     2f
                    541: 1:     cmpl    $NKPTP_MAX,%ecx                 # larger than max?
                    542:        jle     2f
                    543:        movl    $NKPTP_MAX,%ecx
1.29      mycroft   544: 2:     movl    %ecx,RELOC(nkpde)
1.1       fvdl      545:
                    546:        /* Clear memory for bootstrap tables. */
                    547:        shll    $PGSHIFT,%ecx
                    548:        addl    $TABLESIZE,%ecx
                    549:        addl    %esi,%ecx                       # end of tables
                    550:        subl    %edi,%ecx                       # size of tables
                    551:        shrl    $2,%ecx
                    552:        xorl    %eax,%eax
                    553:        cld
                    554:        rep
                    555:        stosl
                    556:
                    557: /*
                    558:  * fillkpt
                    559:  *     eax = pte (page frame | control | status)
                    560:  *     ebx = page table address
                    561:  *     ecx = number of pages to map
                    562:  */
                    563: #define        fillkpt         \
                    564: 1:     movl    %eax,(%ebx)     ; \
1.7       thorpej   565:        addl    $PAGE_SIZE,%eax ; /* increment physical address */ \
1.1       fvdl      566:        addl    $4,%ebx         ; /* next pte */ \
                    567:        loop    1b              ;
                    568:
                    569: /*
                    570:  * Build initial page tables.
                    571:  */
                    572:        /* Calculate end of text segment, rounded to a page. */
                    573:        leal    (RELOC(etext)+PGOFSET),%edx
                    574:        andl    $~PGOFSET,%edx
1.30    ! junyoung  575:
1.1       fvdl      576:        /* Skip over the first 1MB. */
                    577:        movl    $_RELOC(KERNTEXTOFF),%eax
                    578:        movl    %eax,%ecx
                    579:        shrl    $PGSHIFT,%ecx
                    580:        leal    (SYSMAP)(%esi,%ecx,4),%ebx
                    581:
                    582:        /* Map the kernel text read-only. */
                    583:        movl    %edx,%ecx
                    584:        subl    %eax,%ecx
                    585:        shrl    $PGSHIFT,%ecx
                    586:        orl     $(PG_V|PG_KR),%eax
                    587:        fillkpt
                    588:
                    589:        /* Map the data, BSS, and bootstrap tables read-write. */
                    590:        leal    (PG_V|PG_KW)(%edx),%eax
                    591:        movl    RELOC(nkpde),%ecx
                    592:        shll    $PGSHIFT,%ecx
                    593:        addl    $TABLESIZE,%ecx
                    594:        addl    %esi,%ecx                               # end of tables
                    595:        subl    %edx,%ecx                               # subtract end of text
                    596:        shrl    $PGSHIFT,%ecx
                    597:        fillkpt
                    598:
                    599:        /* Map ISA I/O memory. */
                    600:        movl    $(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax   # having these bits set
                    601:        movl    $(IOM_SIZE>>PGSHIFT),%ecx               # for this many pte s,
                    602:        fillkpt
                    603:
                    604: /*
                    605:  * Construct a page table directory.
                    606:  */
                    607:        /* Install PDEs for temporary double map of kernel. */
                    608:        movl    RELOC(nkpde),%ecx                       # for this many pde s,
                    609:        leal    (PROC0PDIR+0*4)(%esi),%ebx              # which is where temp maps!
                    610:        leal    (SYSMAP+PG_V|PG_KW)(%esi),%eax          # pte for KPT in proc 0,
                    611:        fillkpt
                    612:
                    613:        /* Map kernel PDEs. */
                    614:        movl    RELOC(nkpde),%ecx                       # for this many pde s,
                    615:        leal    (PROC0PDIR+PDSLOT_KERN*4)(%esi),%ebx    # kernel pde offset
                    616:        leal    (SYSMAP+PG_V|PG_KW)(%esi),%eax          # pte for KPT in proc 0,
                    617:        fillkpt
                    618:
                    619:        /* Install a PDE recursively mapping page directory as a page table! */
                    620:        leal    (PROC0PDIR+PG_V|PG_KW)(%esi),%eax       # pte for ptd
                    621:        movl    %eax,(PROC0PDIR+PDSLOT_PTE*4)(%esi)     # recursive PD slot
                    622:
                    623:        /* Save phys. addr of PTD, for libkvm. */
                    624:        movl    %esi,RELOC(PTDpaddr)
                    625:
                    626:        /* Load base of page directory and enable mapping. */
                    627:        movl    %esi,%eax               # phys address of ptd in proc 0
                    628:        movl    %eax,%cr3               # load ptd addr into mmu
                    629:        movl    %cr0,%eax               # get control word
                    630:                                        # enable paging & NPX emulation
                    631:        orl     $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_EM|CR0_MP),%eax
                    632:        movl    %eax,%cr0               # and let's page NOW!
                    633:
                    634:        pushl   $begin                  # jump to high mem
                    635:        ret
                    636:
                    637: begin:
1.13      christos  638:        /* Now running relocated at KERNBASE_LOCORE.  Remove double mapping. */
1.1       fvdl      639:        movl    _C_LABEL(nkpde),%ecx            # for this many pde s,
                    640:        leal    (PROC0PDIR+0*4)(%esi),%ebx      # which is where temp maps!
1.13      christos  641:        addl    $(KERNBASE_LOCORE), %ebx        # now use relocated address
1.1       fvdl      642: 1:     movl    $0,(%ebx)
                    643:        addl    $4,%ebx # next pde
                    644:        loop    1b
                    645:
                    646:        /* Relocate atdevbase. */
                    647:        movl    _C_LABEL(nkpde),%edx
                    648:        shll    $PGSHIFT,%edx
1.13      christos  649:        addl    $(TABLESIZE+KERNBASE_LOCORE),%edx
1.1       fvdl      650:        addl    %esi,%edx
                    651:        movl    %edx,_C_LABEL(atdevbase)
                    652:
                    653:        /* Set up bootstrap stack. */
1.13      christos  654:        leal    (PROC0STACK+KERNBASE_LOCORE)(%esi),%eax
1.1       fvdl      655:        movl    %eax,_C_LABEL(proc0paddr)
                    656:        leal    (USPACE-FRAMESIZE)(%eax),%esp
                    657:        movl    %esi,PCB_CR3(%eax)      # pcb->pcb_cr3
                    658:        xorl    %ebp,%ebp               # mark end of frames
                    659:
                    660:        subl    $NGDT*8, %esp           # space for temporary gdt
                    661:        pushl   %esp
                    662:        call    _C_LABEL(initgdt)
                    663:        addl    $4,%esp
1.30    ! junyoung  664:
1.1       fvdl      665:        movl    _C_LABEL(nkpde),%eax
                    666:        shll    $PGSHIFT,%eax
                    667:        addl    $TABLESIZE,%eax
                    668:        addl    %esi,%eax               # skip past stack and page tables
                    669:
                    670:        pushl   %eax
                    671:        call    _C_LABEL(init386)       # wire 386 chip for unix operation
                    672:        addl    $4+NGDT*8,%esp          # pop temporary gdt
                    673:
                    674: #ifdef SAFARI_FIFO_HACK
                    675:        movb    $5,%al
                    676:        movw    $0x37b,%dx
                    677:        outb    %al,%dx
                    678:        movw    $0x37f,%dx
                    679:        inb     %dx,%al
                    680:        movb    %al,%cl
                    681:
                    682:        orb     $1,%cl
                    683:
                    684:        movb    $5,%al
                    685:        movw    $0x37b,%dx
                    686:        outb    %al,%dx
                    687:        movw    $0x37f,%dx
                    688:        movb    %cl,%al
                    689:        outb    %al,%dx
                    690: #endif /* SAFARI_FIFO_HACK */
                    691:
                    692:        call    _C_LABEL(main)
                    693:
                    694: /*
                    695:  * void proc_trampoline(void);
                    696:  * This is a trampoline function pushed onto the stack of a newly created
                    697:  * process in order to do some additional setup.  The trampoline is entered by
                    698:  * cpu_switch()ing to the process, so we abuse the callee-saved registers used
                    699:  * by cpu_switch() to store the information about the stub to call.
                    700:  * NOTE: This function does not have a normal calling sequence!
                    701:  */
                    702: /* LINTSTUB: Func: void proc_trampoline(void) */
                    703: NENTRY(proc_trampoline)
                    704: #ifdef MULTIPROCESSOR
                    705:        call    _C_LABEL(proc_trampoline_mp)
                    706: #endif
                    707:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                    708:        pushl   %ebx
                    709:        call    *%esi
                    710:        addl    $4,%esp
1.24      yamt      711:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl      712:        INTRFASTEXIT
                    713:        /* NOTREACHED */
                    714:
                    715: /*****************************************************************************/
1.16      christos  716: #ifdef COMPAT_16
1.1       fvdl      717: /*
                    718:  * Signal trampoline; copied to top of user stack.
                    719:  */
                    720: /* LINTSTUB: Var: char sigcode[1], esigcode[1]; */
                    721: NENTRY(sigcode)
                    722:        /*
                    723:         * Handler has returned here as if we called it.  The sigcontext
                    724:         * is on the stack after the 3 args "we" pushed.
                    725:         */
                    726:        leal    12(%esp),%eax           # get pointer to sigcontext
                    727:        movl    %eax,4(%esp)            # put it in the argument slot
                    728:                                        # fake return address already there
1.17      christos  729:        movl    $SYS_compat_16___sigreturn14,%eax
1.1       fvdl      730:        int     $0x80                   # enter kernel with args on stack
                    731:        movl    $SYS_exit,%eax
                    732:        int     $0x80                   # exit if sigreturn fails
                    733:        .globl  _C_LABEL(esigcode)
                    734: _C_LABEL(esigcode):
1.16      christos  735: #endif
1.1       fvdl      736:
                    737: /*****************************************************************************/
                    738:
                    739: /*
                    740:  * The following primitives are used to fill and copy regions of memory.
                    741:  */
                    742:
                    743: /*
                    744:  * XXX No section 9 man page for fillw.
                    745:  * fillw seems to be very sparsely used (only in pccons it seems.)
                    746:  * One wonders if it couldn't be done without.
                    747:  * -- Perry Metzger, May 7, 2001
                    748:  */
                    749: /*
                    750:  * void fillw(short pattern, void *addr, size_t len);
                    751:  * Write len copies of pattern at addr.
                    752:  */
                    753: /* LINTSTUB: Func: void fillw(short pattern, void *addr, size_t len) */
                    754: ENTRY(fillw)
                    755:        pushl   %edi
                    756:        movl    8(%esp),%eax
                    757:        movl    12(%esp),%edi
                    758:        movw    %ax,%cx
                    759:        rorl    $16,%eax
                    760:        movw    %cx,%ax
                    761:        cld
                    762:        movl    16(%esp),%ecx
                    763:        shrl    %ecx                    # do longwords
                    764:        rep
                    765:        stosl
                    766:        movl    16(%esp),%ecx
                    767:        andl    $1,%ecx                 # do remainder
                    768:        rep
                    769:        stosw
                    770:        popl    %edi
                    771:        ret
                    772:
                    773: /*
                    774:  * int kcopy(const void *from, void *to, size_t len);
                    775:  * Copy len bytes, abort on fault.
                    776:  */
                    777: /* LINTSTUB: Func: int kcopy(const void *from, void *to, size_t len) */
                    778: ENTRY(kcopy)
                    779:        pushl   %esi
                    780:        pushl   %edi
                    781:        GET_CURPCB(%eax)                # load curpcb into eax and set on-fault
                    782:        pushl   PCB_ONFAULT(%eax)
1.24      yamt      783:        movl    $_C_LABEL(kcopy_fault), PCB_ONFAULT(%eax)
1.1       fvdl      784:
                    785:        movl    16(%esp),%esi
                    786:        movl    20(%esp),%edi
                    787:        movl    24(%esp),%ecx
                    788:        movl    %edi,%eax
                    789:        subl    %esi,%eax
                    790:        cmpl    %ecx,%eax               # overlapping?
                    791:        jb      1f
                    792:        cld                             # nope, copy forward
                    793:        shrl    $2,%ecx                 # copy by 32-bit words
                    794:        rep
                    795:        movsl
                    796:        movl    24(%esp),%ecx
                    797:        andl    $3,%ecx                 # any bytes left?
                    798:        rep
                    799:        movsb
                    800:
                    801:        GET_CURPCB(%edx)                # XXX save curpcb?
                    802:        popl    PCB_ONFAULT(%edx)
                    803:        popl    %edi
                    804:        popl    %esi
                    805:        xorl    %eax,%eax
                    806:        ret
                    807:
                    808:        ALIGN_TEXT
                    809: 1:     addl    %ecx,%edi               # copy backward
                    810:        addl    %ecx,%esi
                    811:        std
                    812:        andl    $3,%ecx                 # any fractional bytes?
                    813:        decl    %edi
                    814:        decl    %esi
                    815:        rep
                    816:        movsb
                    817:        movl    24(%esp),%ecx           # copy remainder by 32-bit words
                    818:        shrl    $2,%ecx
                    819:        subl    $3,%esi
                    820:        subl    $3,%edi
                    821:        rep
                    822:        movsl
                    823:        cld
                    824:
                    825:        GET_CURPCB(%edx)
                    826:        popl    PCB_ONFAULT(%edx)
                    827:        popl    %edi
                    828:        popl    %esi
                    829:        xorl    %eax,%eax
                    830:        ret
                    831:
                    832: /*****************************************************************************/
                    833:
                    834: /*
                    835:  * The following primitives are used to copy data in and out of the user's
                    836:  * address space.
                    837:  */
                    838:
                    839: /*
                    840:  * Default to the lowest-common-denominator.  We will improve it
                    841:  * later.
                    842:  */
                    843: #if defined(I386_CPU)
                    844: #define        DEFAULT_COPYOUT         _C_LABEL(i386_copyout)
                    845: #define        DEFAULT_COPYIN          _C_LABEL(i386_copyin)
                    846: #elif defined(I486_CPU)
                    847: #define        DEFAULT_COPYOUT         _C_LABEL(i486_copyout)
                    848: #define        DEFAULT_COPYIN          _C_LABEL(i386_copyin)
                    849: #elif defined(I586_CPU)
                    850: #define        DEFAULT_COPYOUT         _C_LABEL(i486_copyout)  /* XXX */
                    851: #define        DEFAULT_COPYIN          _C_LABEL(i386_copyin)   /* XXX */
                    852: #elif defined(I686_CPU)
                    853: #define        DEFAULT_COPYOUT         _C_LABEL(i486_copyout)  /* XXX */
                    854: #define        DEFAULT_COPYIN          _C_LABEL(i386_copyin)   /* XXX */
                    855: #endif
                    856:
                    857:        .data
                    858:
                    859:        .globl  _C_LABEL(copyout_func)
                    860: _C_LABEL(copyout_func):
                    861:        .long   DEFAULT_COPYOUT
                    862:
                    863:        .globl  _C_LABEL(copyin_func)
                    864: _C_LABEL(copyin_func):
                    865:        .long   DEFAULT_COPYIN
                    866:
                    867:        .text
                    868:
                    869: /*
                    870:  * int copyout(const void *from, void *to, size_t len);
                    871:  * Copy len bytes into the user's address space.
                    872:  * see copyout(9)
                    873:  */
                    874: /* LINTSTUB: Func: int copyout(const void *kaddr, void *uaddr, size_t len) */
                    875: ENTRY(copyout)
1.24      yamt      876:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl      877:        jmp     *_C_LABEL(copyout_func)
                    878:
                    879: #if defined(I386_CPU)
                    880: /* LINTSTUB: Func: int i386_copyout(const void *kaddr, void *uaddr, size_t len) */
                    881: ENTRY(i386_copyout)
                    882:        pushl   %esi
                    883:        pushl   %edi
                    884:        pushl   $0
1.30    ! junyoung  885:
1.1       fvdl      886:        movl    16(%esp),%esi
                    887:        movl    20(%esp),%edi
                    888:        movl    24(%esp),%eax
                    889:
                    890:        /*
                    891:         * We check that the end of the destination buffer is not past the end
                    892:         * of the user's address space.  If it's not, then we only need to
                    893:         * check that each page is writable.  The 486 will do this for us; the
                    894:         * 386 will not.  (We assume that pages in user space that are not
                    895:         * writable by the user are not writable by the kernel either.)
                    896:         */
                    897:        movl    %edi,%edx
                    898:        addl    %eax,%edx
                    899:        jc      _C_LABEL(copy_efault)
                    900:        cmpl    $VM_MAXUSER_ADDRESS,%edx
                    901:        ja      _C_LABEL(copy_efault)
                    902:
                    903:        testl   %eax,%eax               # anything to do?
                    904:        jz      3f
                    905:
                    906:        /*
                    907:         * We have to check each PTE for (write) permission, since the CPU
                    908:         * doesn't do it for us.
                    909:         */
                    910:
                    911:        /* Compute number of pages. */
                    912:        movl    %edi,%ecx
                    913:        andl    $PGOFSET,%ecx
                    914:        addl    %eax,%ecx
                    915:        decl    %ecx
                    916:        shrl    $PGSHIFT,%ecx
                    917:
                    918:        /* Compute PTE offset for start address. */
                    919:        shrl    $PGSHIFT,%edi
                    920:
                    921:        GET_CURPCB(%edx)
                    922:        movl    $2f,PCB_ONFAULT(%edx)
                    923:
                    924: 1:     /* Check PTE for each page. */
                    925:        testb   $PG_RW,_C_LABEL(PTmap)(,%edi,4)
                    926:        jz      2f
1.30    ! junyoung  927:
1.1       fvdl      928: 4:     incl    %edi
                    929:        decl    %ecx
                    930:        jns     1b
                    931:
                    932:        movl    20(%esp),%edi
                    933:        movl    24(%esp),%eax
                    934:        jmp     3f
1.30    ! junyoung  935:
1.1       fvdl      936: 2:     /* Simulate a trap. */
                    937:        pushl   %ecx
                    938:        movl    %edi,%eax
                    939:        shll    $PGSHIFT,%eax
                    940:        pushl   %eax
                    941:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                    942:        addl    $4,%esp                 # pop argument
                    943:        popl    %ecx
                    944:        testl   %eax,%eax               # if not ok, return EFAULT
                    945:        jz      4b
                    946:        jmp     _C_LABEL(copy_efault)
                    947:
                    948: 3:     GET_CURPCB(%edx)
                    949:        movl    $_C_LABEL(copy_fault),PCB_ONFAULT(%edx)
                    950:
                    951:        /* bcopy(%esi, %edi, %eax); */
                    952:        cld
                    953:        movl    %eax,%ecx
                    954:        shrl    $2,%ecx
                    955:        rep
                    956:        movsl
                    957:        movl    %eax,%ecx
                    958:        andl    $3,%ecx
                    959:        rep
                    960:        movsb
                    961:
                    962:        popl    PCB_ONFAULT(%edx)
                    963:        popl    %edi
                    964:        popl    %esi
                    965:        xorl    %eax,%eax
                    966:        ret
                    967: #endif /* I386_CPU */
                    968:
                    969: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                    970: /* LINTSTUB: Func: int i486_copyout(const void *kaddr, void *uaddr, size_t len) */
                    971: ENTRY(i486_copyout)
                    972:        pushl   %esi
                    973:        pushl   %edi
                    974:        pushl   $0
1.30    ! junyoung  975:
1.1       fvdl      976:        movl    16(%esp),%esi
                    977:        movl    20(%esp),%edi
                    978:        movl    24(%esp),%eax
                    979:
                    980:        /*
                    981:         * We check that the end of the destination buffer is not past the end
                    982:         * of the user's address space.
                    983:         */
                    984:        movl    %edi,%edx
                    985:        addl    %eax,%edx
                    986:        jc      _C_LABEL(copy_efault)
                    987:        cmpl    $VM_MAXUSER_ADDRESS,%edx
                    988:        ja      _C_LABEL(copy_efault)
                    989:
                    990:        GET_CURPCB(%edx)
                    991:        movl    $_C_LABEL(copy_fault),PCB_ONFAULT(%edx)
                    992:
                    993:        /* bcopy(%esi, %edi, %eax); */
                    994:        cld
                    995:        movl    %eax,%ecx
                    996:        shrl    $2,%ecx
                    997:        rep
                    998:        movsl
                    999:        movl    %eax,%ecx
                   1000:        andl    $3,%ecx
                   1001:        rep
                   1002:        movsb
                   1003:
                   1004:        popl    PCB_ONFAULT(%edx)
                   1005:        popl    %edi
                   1006:        popl    %esi
                   1007:        xorl    %eax,%eax
                   1008:        ret
                   1009: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1010:
                   1011: /*
                   1012:  * int copyin(const void *from, void *to, size_t len);
                   1013:  * Copy len bytes from the user's address space.
                   1014:  * see copyin(9)
                   1015:  */
                   1016: /* LINTSTUB: Func: int copyin(const void *uaddr, void *kaddr, size_t len) */
                   1017: ENTRY(copyin)
1.24      yamt     1018:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1019:        jmp     *_C_LABEL(copyin_func)
                   1020:
                   1021: #if defined(I386_CPU) || defined(I486_CPU) || defined(I586_CPU) || \
                   1022:     defined(I686_CPU)
                   1023: /* LINTSTUB: Func: int i386_copyin(const void *uaddr, void *kaddr, size_t len) */
                   1024: ENTRY(i386_copyin)
                   1025:        pushl   %esi
                   1026:        pushl   %edi
                   1027:        GET_CURPCB(%eax)
                   1028:        pushl   $0
                   1029:        movl    $_C_LABEL(copy_fault),PCB_ONFAULT(%eax)
1.30    ! junyoung 1030:
1.1       fvdl     1031:        movl    16(%esp),%esi
                   1032:        movl    20(%esp),%edi
                   1033:        movl    24(%esp),%eax
                   1034:
                   1035:        /*
                   1036:         * We check that the end of the destination buffer is not past the end
                   1037:         * of the user's address space.  If it's not, then we only need to
                   1038:         * check that each page is readable, and the CPU will do that for us.
                   1039:         */
                   1040:        movl    %esi,%edx
                   1041:        addl    %eax,%edx
                   1042:        jc      _C_LABEL(copy_efault)
                   1043:        cmpl    $VM_MAXUSER_ADDRESS,%edx
                   1044:        ja      _C_LABEL(copy_efault)
                   1045:
                   1046:        /* bcopy(%esi, %edi, %eax); */
                   1047:        cld
                   1048:        movl    %eax,%ecx
                   1049:        shrl    $2,%ecx
                   1050:        rep
                   1051:        movsl
                   1052:        movl    %eax,%ecx
                   1053:        andl    $3,%ecx
                   1054:        rep
                   1055:        movsb
                   1056:
                   1057:        GET_CURPCB(%edx)
                   1058:        popl    PCB_ONFAULT(%edx)
                   1059:        popl    %edi
                   1060:        popl    %esi
                   1061:        xorl    %eax,%eax
                   1062:        ret
                   1063: #endif /* I386_CPU || I486_CPU || I586_CPU || I686_CPU */
                   1064:
                   1065: /* LINTSTUB: Ignore */
                   1066: NENTRY(copy_efault)
                   1067:        movl    $EFAULT,%eax
                   1068:
1.24      yamt     1069: /*
                   1070:  * kcopy_fault is used by kcopy and copy_fault is used by copyin/out.
                   1071:  *
                   1072:  * they're distinguished for lazy pmap switching.  see trap().
                   1073:  */
                   1074: /* LINTSTUB: Ignore */
                   1075: NENTRY(kcopy_fault)
                   1076:        GET_CURPCB(%edx)
                   1077:        popl    PCB_ONFAULT(%edx)
                   1078:        popl    %edi
                   1079:        popl    %esi
                   1080:        ret
                   1081:
1.1       fvdl     1082: /* LINTSTUB: Ignore */
                   1083: NENTRY(copy_fault)
                   1084:        GET_CURPCB(%edx)
                   1085:        popl    PCB_ONFAULT(%edx)
                   1086:        popl    %edi
                   1087:        popl    %esi
                   1088:        ret
                   1089:
                   1090: /*
                   1091:  * int copyoutstr(const void *from, void *to, size_t maxlen, size_t *lencopied);
                   1092:  * Copy a NUL-terminated string, at most maxlen characters long, into the
                   1093:  * user's address space.  Return the number of characters copied (including the
                   1094:  * NUL) in *lencopied.  If the string is too long, return ENAMETOOLONG; else
                   1095:  * return 0 or EFAULT.
                   1096:  * see copyoutstr(9)
                   1097:  */
                   1098: /* LINTSTUB: Func: int copyoutstr(const void *kaddr, void *uaddr, size_t len, size_t *done) */
                   1099: ENTRY(copyoutstr)
                   1100:        pushl   %esi
                   1101:        pushl   %edi
                   1102:
1.24      yamt     1103:        DO_DEFERRED_SWITCH(%eax)
                   1104:
1.1       fvdl     1105:        movl    12(%esp),%esi           # esi = from
                   1106:        movl    16(%esp),%edi           # edi = to
                   1107:        movl    20(%esp),%edx           # edx = maxlen
                   1108:
                   1109: #if defined(I386_CPU)
                   1110: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1111:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1112:        jne     5f
                   1113: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1114:
                   1115:        /* Compute number of bytes in first page. */
                   1116:        movl    %edi,%eax
                   1117:        andl    $PGOFSET,%eax
1.7       thorpej  1118:        movl    $PAGE_SIZE,%ecx
                   1119:        subl    %eax,%ecx               # ecx = PAGE_SIZE - (src % PAGE_SIZE)
1.1       fvdl     1120:
                   1121:        GET_CURPCB(%eax)
                   1122:        movl    $6f,PCB_ONFAULT(%eax)
                   1123:
                   1124: 1:     /*
                   1125:         * Once per page, check that we are still within the bounds of user
                   1126:         * space, and check for a write fault.
                   1127:         */
                   1128:        cmpl    $VM_MAXUSER_ADDRESS,%edi
                   1129:        jae     _C_LABEL(copystr_efault)
                   1130:
                   1131:        /* Compute PTE offset. */
                   1132:        movl    %edi,%eax
                   1133:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1134:
                   1135:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1136:        jnz     2f
                   1137:
                   1138: 6:     /* Simulate a trap. */
                   1139:        pushl   %edx
                   1140:        pushl   %edi
                   1141:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1142:        addl    $4,%esp                 # clear argument from stack
                   1143:        popl    %edx
                   1144:        testl   %eax,%eax
                   1145:        jnz     _C_LABEL(copystr_efault)
                   1146:
                   1147: 2:     /* Copy up to end of this page. */
                   1148:        subl    %ecx,%edx               # predecrement total count
                   1149:        jnc     3f
                   1150:        addl    %edx,%ecx               # ecx += (edx - ecx) = edx
                   1151:        xorl    %edx,%edx
                   1152:
                   1153: 3:     decl    %ecx
                   1154:        js      4f
                   1155:        lodsb
                   1156:        stosb
                   1157:        testb   %al,%al
                   1158:        jnz     3b
                   1159:
                   1160:        /* Success -- 0 byte reached. */
                   1161:        addl    %ecx,%edx               # add back residual for this page
                   1162:        xorl    %eax,%eax
                   1163:        jmp     copystr_return
                   1164:
                   1165: 4:     /* Go to next page, if any. */
1.7       thorpej  1166:        movl    $PAGE_SIZE,%ecx
1.1       fvdl     1167:        testl   %edx,%edx
                   1168:        jnz     1b
                   1169:
                   1170:        /* edx is zero -- return ENAMETOOLONG. */
                   1171:        movl    $ENAMETOOLONG,%eax
                   1172:        jmp     copystr_return
                   1173: #endif /* I386_CPU */
                   1174:
                   1175: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1176: 5:     GET_CURPCB(%eax)
                   1177:        movl    $_C_LABEL(copystr_fault),PCB_ONFAULT(%eax)
                   1178:        /*
                   1179:         * Get min(%edx, VM_MAXUSER_ADDRESS-%edi).
                   1180:         */
                   1181:        movl    $VM_MAXUSER_ADDRESS,%eax
                   1182:        subl    %edi,%eax
                   1183:        cmpl    %edx,%eax
                   1184:        jae     1f
                   1185:        movl    %eax,%edx
                   1186:        movl    %eax,20(%esp)
                   1187:
                   1188: 1:     incl    %edx
                   1189:        cld
                   1190:
                   1191: 1:     decl    %edx
                   1192:        jz      2f
                   1193:        lodsb
                   1194:        stosb
                   1195:        testb   %al,%al
                   1196:        jnz     1b
                   1197:
                   1198:        /* Success -- 0 byte reached. */
                   1199:        decl    %edx
                   1200:        xorl    %eax,%eax
                   1201:        jmp     copystr_return
                   1202:
                   1203: 2:     /* edx is zero -- return EFAULT or ENAMETOOLONG. */
                   1204:        cmpl    $VM_MAXUSER_ADDRESS,%edi
                   1205:        jae     _C_LABEL(copystr_efault)
                   1206:        movl    $ENAMETOOLONG,%eax
                   1207:        jmp     copystr_return
                   1208: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1209:
                   1210: /*
                   1211:  * int copyinstr(const void *from, void *to, size_t maxlen, size_t *lencopied);
                   1212:  * Copy a NUL-terminated string, at most maxlen characters long, from the
                   1213:  * user's address space.  Return the number of characters copied (including the
                   1214:  * NUL) in *lencopied.  If the string is too long, return ENAMETOOLONG; else
                   1215:  * return 0 or EFAULT.
                   1216:  * see copyinstr(9)
                   1217:  */
                   1218: /* LINTSTUB: Func: int copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) */
                   1219: ENTRY(copyinstr)
                   1220:        pushl   %esi
                   1221:        pushl   %edi
1.30    ! junyoung 1222:
1.24      yamt     1223:        DO_DEFERRED_SWITCH(%eax)
                   1224:
1.1       fvdl     1225:        GET_CURPCB(%ecx)
                   1226:        movl    $_C_LABEL(copystr_fault),PCB_ONFAULT(%ecx)
                   1227:
                   1228:        movl    12(%esp),%esi           # %esi = from
                   1229:        movl    16(%esp),%edi           # %edi = to
                   1230:        movl    20(%esp),%edx           # %edx = maxlen
                   1231:
                   1232:        /*
                   1233:         * Get min(%edx, VM_MAXUSER_ADDRESS-%esi).
                   1234:         */
                   1235:        movl    $VM_MAXUSER_ADDRESS,%eax
                   1236:        subl    %esi,%eax
                   1237:        cmpl    %edx,%eax
                   1238:        jae     1f
                   1239:        movl    %eax,%edx
                   1240:        movl    %eax,20(%esp)
                   1241:
                   1242: 1:     incl    %edx
                   1243:        cld
                   1244:
                   1245: 1:     decl    %edx
                   1246:        jz      2f
                   1247:        lodsb
                   1248:        stosb
                   1249:        testb   %al,%al
                   1250:        jnz     1b
                   1251:
                   1252:        /* Success -- 0 byte reached. */
                   1253:        decl    %edx
                   1254:        xorl    %eax,%eax
                   1255:        jmp     copystr_return
                   1256:
                   1257: 2:     /* edx is zero -- return EFAULT or ENAMETOOLONG. */
                   1258:        cmpl    $VM_MAXUSER_ADDRESS,%esi
                   1259:        jae     _C_LABEL(copystr_efault)
                   1260:        movl    $ENAMETOOLONG,%eax
                   1261:        jmp     copystr_return
                   1262:
                   1263: /* LINTSTUB: Ignore */
                   1264: NENTRY(copystr_efault)
                   1265:        movl    $EFAULT,%eax
                   1266:
                   1267: /* LINTSTUB: Ignore */
                   1268: NENTRY(copystr_fault)
                   1269: copystr_return:
                   1270:        /* Set *lencopied and return %eax. */
                   1271:        GET_CURPCB(%ecx)
                   1272:        movl    $0,PCB_ONFAULT(%ecx)
                   1273:        movl    20(%esp),%ecx
                   1274:        subl    %edx,%ecx
                   1275:        movl    24(%esp),%edx
                   1276:        testl   %edx,%edx
                   1277:        jz      8f
                   1278:        movl    %ecx,(%edx)
                   1279:
                   1280: 8:     popl    %edi
                   1281:        popl    %esi
                   1282:        ret
                   1283:
                   1284: /*
                   1285:  * int copystr(const void *from, void *to, size_t maxlen, size_t *lencopied);
                   1286:  * Copy a NUL-terminated string, at most maxlen characters long.  Return the
                   1287:  * number of characters copied (including the NUL) in *lencopied.  If the
                   1288:  * string is too long, return ENAMETOOLONG; else return 0.
                   1289:  * see copystr(9)
                   1290:  */
                   1291: /* LINTSTUB: Func: int copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done) */
                   1292: ENTRY(copystr)
                   1293:        pushl   %esi
                   1294:        pushl   %edi
                   1295:
                   1296:        movl    12(%esp),%esi           # esi = from
                   1297:        movl    16(%esp),%edi           # edi = to
                   1298:        movl    20(%esp),%edx           # edx = maxlen
                   1299:        incl    %edx
                   1300:        cld
                   1301:
                   1302: 1:     decl    %edx
                   1303:        jz      4f
                   1304:        lodsb
                   1305:        stosb
                   1306:        testb   %al,%al
                   1307:        jnz     1b
                   1308:
                   1309:        /* Success -- 0 byte reached. */
                   1310:        decl    %edx
                   1311:        xorl    %eax,%eax
                   1312:        jmp     6f
                   1313:
                   1314: 4:     /* edx is zero -- return ENAMETOOLONG. */
                   1315:        movl    $ENAMETOOLONG,%eax
                   1316:
                   1317: 6:     /* Set *lencopied and return %eax. */
                   1318:        movl    20(%esp),%ecx
                   1319:        subl    %edx,%ecx
                   1320:        movl    24(%esp),%edx
                   1321:        testl   %edx,%edx
                   1322:        jz      7f
                   1323:        movl    %ecx,(%edx)
                   1324:
                   1325: 7:     popl    %edi
                   1326:        popl    %esi
                   1327:        ret
                   1328:
                   1329: /*
                   1330:  * long fuword(const void *uaddr);
                   1331:  * Fetch an int from the user's address space.
                   1332:  * see fuword(9)
                   1333:  */
                   1334: /* LINTSTUB: Func: long fuword(const void *base) */
                   1335: ENTRY(fuword)
1.24      yamt     1336:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1337:        movl    4(%esp),%edx
                   1338:        cmpl    $VM_MAXUSER_ADDRESS-4,%edx
                   1339:        ja      _C_LABEL(fusuaddrfault)
                   1340:        GET_CURPCB(%ecx)
                   1341:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1342:        movl    (%edx),%eax
                   1343:        movl    $0,PCB_ONFAULT(%ecx)
                   1344:        ret
1.30    ! junyoung 1345:
1.1       fvdl     1346: /*
                   1347:  * int fusword(const void *uaddr);
                   1348:  * Fetch a short from the user's address space.
                   1349:  * see fusword(9)
                   1350:  */
                   1351: /* LINTSTUB: Func: int fusword(const void *base) */
                   1352: ENTRY(fusword)
1.24      yamt     1353:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1354:        movl    4(%esp),%edx
                   1355:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1356:        ja      _C_LABEL(fusuaddrfault)
                   1357:        GET_CURPCB(%ecx)
                   1358:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1359:        movzwl  (%edx),%eax
                   1360:        movl    $0,PCB_ONFAULT(%ecx)
                   1361:        ret
1.30    ! junyoung 1362:
1.1       fvdl     1363: /*
                   1364:  * int fuswintr(const void *uaddr);
                   1365:  * Fetch a short from the user's address space.  Can be called during an
                   1366:  * interrupt.
                   1367:  * see fuswintr(9)
                   1368:  */
                   1369: /* LINTSTUB: Func: int fuswintr(const void *base) */
                   1370: ENTRY(fuswintr)
1.24      yamt     1371:        cmpl    $TLBSTATE_VALID, CPUVAR(TLBSTATE)
                   1372:        jnz     _C_LABEL(fusuaddrfault)
1.1       fvdl     1373:        movl    4(%esp),%edx
                   1374:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1375:        ja      _C_LABEL(fusuaddrfault)
1.5       thorpej  1376:        movl    CPUVAR(CURLWP),%ecx
                   1377:        movl    L_ADDR(%ecx),%ecx
1.1       fvdl     1378:        movl    $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
                   1379:        movzwl  (%edx),%eax
                   1380:        movl    $0,PCB_ONFAULT(%ecx)
                   1381:        ret
1.30    ! junyoung 1382:
1.1       fvdl     1383: /*
                   1384:  * int fubyte(const void *uaddr);
                   1385:  * Fetch a byte from the user's address space.
                   1386:  * see fubyte(9)
                   1387:  */
                   1388: /* LINTSTUB: Func: int fubyte(const void *base) */
                   1389: ENTRY(fubyte)
1.24      yamt     1390:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1391:        movl    4(%esp),%edx
                   1392:        cmpl    $VM_MAXUSER_ADDRESS-1,%edx
                   1393:        ja      _C_LABEL(fusuaddrfault)
                   1394:        GET_CURPCB(%ecx)
                   1395:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1396:        movzbl  (%edx),%eax
                   1397:        movl    $0,PCB_ONFAULT(%ecx)
                   1398:        ret
                   1399:
                   1400: /*
                   1401:  * Handle faults from [fs]u*().  Clean up and return -1.
                   1402:  */
                   1403: /* LINTSTUB: Ignore */
                   1404: NENTRY(fusufault)
                   1405:        movl    $0,PCB_ONFAULT(%ecx)
                   1406:        movl    $-1,%eax
                   1407:        ret
                   1408:
                   1409: /*
                   1410:  * Handle faults from [fs]u*().  Clean up and return -1.  This differs from
                   1411:  * fusufault() in that trap() will recognize it and return immediately rather
                   1412:  * than trying to page fault.
                   1413:  */
                   1414: /* LINTSTUB: Ignore */
                   1415: NENTRY(fusubail)
                   1416:        movl    $0,PCB_ONFAULT(%ecx)
                   1417:        movl    $-1,%eax
                   1418:        ret
                   1419:
                   1420: /*
                   1421:  * Handle earlier faults from [fs]u*(), due to our of range addresses.
                   1422:  */
                   1423: /* LINTSTUB: Ignore */
                   1424: NENTRY(fusuaddrfault)
                   1425:        movl    $-1,%eax
                   1426:        ret
                   1427:
                   1428: /*
                   1429:  * int suword(void *uaddr, long x);
                   1430:  * Store an int in the user's address space.
                   1431:  * see suword(9)
                   1432:  */
                   1433: /* LINTSTUB: Func: int suword(void *base, long c) */
                   1434: ENTRY(suword)
1.24      yamt     1435:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1436:        movl    4(%esp),%edx
                   1437:        cmpl    $VM_MAXUSER_ADDRESS-4,%edx
                   1438:        ja      _C_LABEL(fusuaddrfault)
                   1439:
                   1440: #if defined(I386_CPU)
                   1441: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1442:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1443:        jne     2f
                   1444: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1445:
                   1446:        GET_CURPCB(%eax)
                   1447:        movl    $3f,PCB_ONFAULT(%eax)
                   1448:
                   1449:        movl    %edx,%eax
                   1450:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1451:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1452:        jnz     1f
                   1453:
                   1454: 3:     /* Simulate a trap. */
                   1455:        pushl   %edx
                   1456:        pushl   %edx
                   1457:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1458:        addl    $4,%esp                 # clear parameter from the stack
                   1459:        popl    %edx
                   1460:        GET_CURPCB(%ecx)
                   1461:        testl   %eax,%eax
                   1462:        jnz     _C_LABEL(fusufault)
                   1463:
                   1464: 1:     /* XXX also need to check the following 3 bytes for validity! */
                   1465: #endif
                   1466:
                   1467: 2:     GET_CURPCB(%ecx)
                   1468:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1469:
                   1470:        movl    8(%esp),%eax
                   1471:        movl    %eax,(%edx)
                   1472:        xorl    %eax,%eax
                   1473:        movl    %eax,PCB_ONFAULT(%ecx)
                   1474:        ret
1.30    ! junyoung 1475:
1.1       fvdl     1476: /*
                   1477:  * int susword(void *uaddr, short x);
                   1478:  * Store a short in the user's address space.
                   1479:  * see susword(9)
                   1480:  */
                   1481: /* LINTSTUB: Func: int susword(void *base, short c) */
                   1482: ENTRY(susword)
1.24      yamt     1483:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1484:        movl    4(%esp),%edx
                   1485:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1486:        ja      _C_LABEL(fusuaddrfault)
                   1487:
                   1488: #if defined(I386_CPU)
                   1489: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1490:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1491:        jne     2f
                   1492: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1493:
                   1494:        GET_CURPCB(%eax)
                   1495:        movl    $3f,PCB_ONFAULT(%eax)
                   1496:
                   1497:        movl    %edx,%eax
                   1498:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1499:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1500:        jnz     1f
                   1501:
                   1502: 3:     /* Simulate a trap. */
                   1503:        pushl   %edx
                   1504:        pushl   %edx
                   1505:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1506:        addl    $4,%esp                 # clear parameter from the stack
                   1507:        popl    %edx
                   1508:        GET_CURPCB(%ecx)
                   1509:        testl   %eax,%eax
                   1510:        jnz     _C_LABEL(fusufault)
                   1511:
                   1512: 1:     /* XXX also need to check the following byte for validity! */
                   1513: #endif
                   1514:
                   1515: 2:     GET_CURPCB(%ecx)
                   1516:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1517:
                   1518:        movl    8(%esp),%eax
                   1519:        movw    %ax,(%edx)
                   1520:        xorl    %eax,%eax
                   1521:        movl    %eax,PCB_ONFAULT(%ecx)
                   1522:        ret
                   1523:
                   1524: /*
                   1525:  * int suswintr(void *uaddr, short x);
                   1526:  * Store a short in the user's address space.  Can be called during an
                   1527:  * interrupt.
                   1528:  * see suswintr(9)
                   1529:  */
                   1530: /* LINTSTUB: Func: int suswintr(void *base, short c) */
                   1531: ENTRY(suswintr)
1.24      yamt     1532:        cmpl    $TLBSTATE_VALID, CPUVAR(TLBSTATE)
                   1533:        jnz     _C_LABEL(fusuaddrfault)
1.1       fvdl     1534:        movl    4(%esp),%edx
                   1535:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1536:        ja      _C_LABEL(fusuaddrfault)
1.5       thorpej  1537:        movl    CPUVAR(CURLWP),%ecx
                   1538:        movl    L_ADDR(%ecx),%ecx
1.1       fvdl     1539:        movl    $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
                   1540:
                   1541: #if defined(I386_CPU)
                   1542: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1543:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1544:        jne     2f
                   1545: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1546:
                   1547:        movl    %edx,%eax
                   1548:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1549:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1550:        jnz     1f
                   1551:
                   1552:        /* Simulate a trap. */
                   1553:        jmp     _C_LABEL(fusubail)
                   1554:
                   1555: 1:     /* XXX also need to check the following byte for validity! */
                   1556: #endif
                   1557:
                   1558: 2:     movl    8(%esp),%eax
                   1559:        movw    %ax,(%edx)
                   1560:        xorl    %eax,%eax
                   1561:        movl    %eax,PCB_ONFAULT(%ecx)
                   1562:        ret
                   1563:
                   1564: /*
                   1565:  * int subyte(void *uaddr, char x);
                   1566:  * Store a byte in the user's address space.
                   1567:  * see subyte(9)
                   1568:  */
                   1569: /* LINTSTUB: Func: int subyte(void *base, int c) */
                   1570: ENTRY(subyte)
1.24      yamt     1571:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1572:        movl    4(%esp),%edx
                   1573:        cmpl    $VM_MAXUSER_ADDRESS-1,%edx
                   1574:        ja      _C_LABEL(fusuaddrfault)
                   1575:
                   1576: #if defined(I386_CPU)
                   1577: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1578:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1579:        jne     2f
                   1580: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1581:
1.30    ! junyoung 1582:        GET_CURPCB(%eax)
1.1       fvdl     1583:        movl    $3f,PCB_ONFAULT(%eax)
                   1584:
                   1585:        movl    %edx,%eax
                   1586:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1587:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1588:        jnz     1f
                   1589:
                   1590: 3:     /* Simulate a trap. */
                   1591:        pushl   %edx
                   1592:        pushl   %edx
                   1593:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1594:        addl    $4,%esp                 # clear parameter from the stack
                   1595:        popl    %edx
                   1596:        GET_CURPCB(%ecx)
                   1597:        testl   %eax,%eax
                   1598:        jnz     _C_LABEL(fusufault)
                   1599:
                   1600: 1:
                   1601: #endif
                   1602:
                   1603: 2:     GET_CURPCB(%ecx)
                   1604:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1605:
                   1606:        movb    8(%esp),%al
                   1607:        movb    %al,(%edx)
                   1608:        xorl    %eax,%eax
                   1609:        movl    %eax,PCB_ONFAULT(%ecx)
                   1610:        ret
                   1611:
                   1612: /*****************************************************************************/
                   1613:
                   1614: /*
                   1615:  * The following is i386-specific nonsense.
                   1616:  */
                   1617:
                   1618: /*
                   1619:  * void lgdt(struct region_descriptor *rdp);
                   1620:  * Load a new GDT pointer (and do any necessary cleanup).
                   1621:  * XXX It's somewhat questionable whether reloading all the segment registers
                   1622:  * is necessary, since the actual descriptor data is not changed except by
                   1623:  * process creation and exit, both of which clean up via task switches.  OTOH,
                   1624:  * this only happens at run time when the GDT is resized.
                   1625:  */
                   1626: /* LINTSTUB: Func: void lgdt(struct region_descriptor *rdp) */
                   1627: NENTRY(lgdt)
                   1628:        /* Reload the descriptor table. */
                   1629:        movl    4(%esp),%eax
                   1630:        lgdt    (%eax)
                   1631:        /* Flush the prefetch queue. */
                   1632:        jmp     1f
                   1633:        nop
                   1634: 1:     /* Reload "stale" selectors. */
                   1635:        movl    $GSEL(GDATA_SEL, SEL_KPL),%eax
                   1636:        movw    %ax,%ds
                   1637:        movw    %ax,%es
                   1638:        movw    %ax,%gs
                   1639:        movw    %ax,%ss
                   1640:        movl    $GSEL(GCPU_SEL, SEL_KPL),%eax
                   1641:        movw    %ax,%fs
                   1642:        /* Reload code selector by doing intersegment return. */
                   1643:        popl    %eax
                   1644:        pushl   $GSEL(GCODE_SEL, SEL_KPL)
                   1645:        pushl   %eax
                   1646:        lret
                   1647:
                   1648: /*****************************************************************************/
                   1649:
                   1650: /*
                   1651:  * These functions are primarily used by DDB.
                   1652:  */
                   1653:
                   1654: /* LINTSTUB: Func: int setjmp (label_t *l) */
                   1655: ENTRY(setjmp)
                   1656:        movl    4(%esp),%eax
                   1657:        movl    %ebx,(%eax)             # save ebx
                   1658:        movl    %esp,4(%eax)            # save esp
                   1659:        movl    %ebp,8(%eax)            # save ebp
                   1660:        movl    %esi,12(%eax)           # save esi
                   1661:        movl    %edi,16(%eax)           # save edi
                   1662:        movl    (%esp),%edx             # get rta
                   1663:        movl    %edx,20(%eax)           # save eip
                   1664:        xorl    %eax,%eax               # return (0);
                   1665:        ret
                   1666:
                   1667: /* LINTSTUB: Func: void longjmp (label_t *l) */
                   1668: ENTRY(longjmp)
                   1669:        movl    4(%esp),%eax
                   1670:        movl    (%eax),%ebx             # restore ebx
                   1671:        movl    4(%eax),%esp            # restore esp
                   1672:        movl    8(%eax),%ebp            # restore ebp
                   1673:        movl    12(%eax),%esi           # restore esi
                   1674:        movl    16(%eax),%edi           # restore edi
                   1675:        movl    20(%eax),%edx           # get rta
                   1676:        movl    %edx,(%esp)             # put in return frame
                   1677:        xorl    %eax,%eax               # return (1);
                   1678:        incl    %eax
                   1679:        ret
                   1680:
                   1681: /*****************************************************************************/
                   1682:
                   1683:        .globl  _C_LABEL(sched_whichqs),_C_LABEL(sched_qs)
                   1684:        .globl  _C_LABEL(uvmexp),_C_LABEL(panic)
                   1685:
                   1686: #ifdef DIAGNOSTIC
                   1687: NENTRY(switch_error)
                   1688:        pushl   $1f
                   1689:        call    _C_LABEL(panic)
                   1690:        /* NOTREACHED */
                   1691: 1:     .asciz  "cpu_switch"
                   1692: #endif /* DIAGNOSTIC */
                   1693:
                   1694: /*
1.5       thorpej  1695:  * void cpu_switch(struct lwp *)
1.1       fvdl     1696:  * Find a runnable process and switch to it.  Wait if necessary.  If the new
                   1697:  * process is the same as the old one, we short-circuit the context save and
                   1698:  * restore.
1.30    ! junyoung 1699:  *
1.1       fvdl     1700:  * Note that the stack frame layout is known to "struct switchframe"
1.30    ! junyoung 1701:  * in <machine/frame.h> and to the code in cpu_fork() which initializes
1.5       thorpej  1702:  * it for a new lwp.
1.1       fvdl     1703:  */
                   1704: ENTRY(cpu_switch)
                   1705:        pushl   %ebx
                   1706:        pushl   %esi
                   1707:        pushl   %edi
                   1708:
                   1709: #ifdef DEBUG
                   1710:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                   1711:        jae     1f
1.5       thorpej  1712:        pushl   $2f
1.1       fvdl     1713:        call    _C_LABEL(panic)
                   1714:        /* NOTREACHED */
1.5       thorpej  1715: 2:     .asciz  "not splsched() in cpu_switch!"
1.30    ! junyoung 1716: 1:
1.1       fvdl     1717: #endif /* DEBUG */
1.30    ! junyoung 1718:
1.5       thorpej  1719:        movl    16(%esp),%esi           # current
1.1       fvdl     1720:
                   1721:        /*
1.5       thorpej  1722:         * Clear curlwp so that we don't accumulate system time while idle.
                   1723:         * This also insures that schedcpu() will move the old lwp to
1.1       fvdl     1724:         * the correct queue if it happens to get called from the spllower()
                   1725:         * below and changes the priority.  (See corresponding comment in
                   1726:         * userret()).
                   1727:         */
1.5       thorpej  1728:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl     1729:        /*
1.5       thorpej  1730:         * First phase: find new lwp.
1.1       fvdl     1731:         *
                   1732:         * Registers:
                   1733:         *   %eax - queue head, scratch, then zero
                   1734:         *   %ebx - queue number
                   1735:         *   %ecx - cached value of whichqs
1.5       thorpej  1736:         *   %edx - next lwp in queue
                   1737:         *   %esi - old lwp
                   1738:         *   %edi - new lwp
1.1       fvdl     1739:         */
                   1740:
1.5       thorpej  1741:        /* Look for new lwp. */
1.1       fvdl     1742:        cli                             # splhigh doesn't do a cli
                   1743:        movl    _C_LABEL(sched_whichqs),%ecx
                   1744:        bsfl    %ecx,%ebx               # find a full q
                   1745:        jnz     switch_dequeue
                   1746:
                   1747:        /*
                   1748:         * idling:      save old context.
                   1749:         *
                   1750:         * Registers:
                   1751:         *   %eax, %ecx - scratch
1.5       thorpej  1752:         *   %esi - old lwp, then old pcb
1.1       fvdl     1753:         *   %edi - idle pcb
                   1754:         */
                   1755:
                   1756:        pushl   %esi
1.24      yamt     1757:        call    _C_LABEL(pmap_deactivate2)      # pmap_deactivate(oldproc)
1.1       fvdl     1758:        addl    $4,%esp
                   1759:
1.5       thorpej  1760:        movl    L_ADDR(%esi),%esi
1.1       fvdl     1761:
                   1762:        /* Save stack pointers. */
                   1763:        movl    %esp,PCB_ESP(%esi)
                   1764:        movl    %ebp,PCB_EBP(%esi)
                   1765:
                   1766:        /* Find idle PCB for this CPU */
                   1767: #ifndef MULTIPROCESSOR
1.5       thorpej  1768:        movl    $_C_LABEL(lwp0),%ebx
                   1769:        movl    L_ADDR(%ebx),%edi
                   1770:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl     1771: #else
                   1772:        movl    CPUVAR(IDLE_PCB),%edi
                   1773:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                   1774: #endif
1.5       thorpej  1775:        movl    $0,CPUVAR(CURLWP)               /* In case we fault... */
1.1       fvdl     1776:
                   1777:        /* Restore the idle context (avoid interrupts) */
                   1778:        cli
                   1779:
                   1780:        /* Restore stack pointers. */
                   1781:        movl    PCB_ESP(%edi),%esp
                   1782:        movl    PCB_EBP(%edi),%ebp
                   1783:
                   1784:        /* Switch TSS. Reset "task busy" flag before loading. */
1.26      yamt     1785:        movl    %cr3,%eax
                   1786:        movl    %eax,PCB_CR3(%edi)
1.1       fvdl     1787: #ifdef MULTIPROCESSOR
                   1788:        movl    CPUVAR(GDT),%eax
                   1789: #else
                   1790:        movl    _C_LABEL(gdt),%eax
                   1791: #endif
                   1792:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                   1793:        ltr     %dx
                   1794:
                   1795:        /* We're always in the kernel, so we don't need the LDT. */
                   1796:
                   1797:        /* Restore cr0 (including FPU state). */
                   1798:        movl    PCB_CR0(%edi),%ecx
                   1799:        movl    %ecx,%cr0
                   1800:
                   1801:        /* Record new pcb. */
                   1802:        SET_CURPCB(%edi)
                   1803:
                   1804:        xorl    %esi,%esi
                   1805:        sti
1.30    ! junyoung 1806: idle_unlock:
        !          1807: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl     1808:        call    _C_LABEL(sched_unlock_idle)
                   1809: #endif
                   1810:        /* Interrupts are okay again. */
1.2       fvdl     1811:        pushl   $IPL_NONE               # spl0()
1.1       fvdl     1812:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl     1813:        addl    $4,%esp
1.1       fvdl     1814:        jmp     idle_start
1.30    ! junyoung 1815: idle_zero:
1.1       fvdl     1816:        sti
                   1817:        call    _C_LABEL(uvm_pageidlezero)
                   1818:        cli
                   1819:        cmpl    $0,_C_LABEL(sched_whichqs)
                   1820:        jnz     idle_exit
                   1821: idle_loop:
                   1822:        /* Try to zero some pages. */
                   1823:        movl    _C_LABEL(uvm)+UVM_PAGE_IDLE_ZERO,%ecx
                   1824:        testl   %ecx,%ecx
                   1825:        jnz     idle_zero
                   1826:        sti
                   1827:        hlt
                   1828: NENTRY(mpidle)
1.30    ! junyoung 1829: idle_start:
1.1       fvdl     1830:        cli
                   1831:        cmpl    $0,_C_LABEL(sched_whichqs)
                   1832:        jz      idle_loop
1.30    ! junyoung 1833: idle_exit:
1.1       fvdl     1834:        movl    $IPL_HIGH,CPUVAR(ILEVEL)                # splhigh
1.14      fvdl     1835:        sti
1.30    ! junyoung 1836: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl     1837:        call    _C_LABEL(sched_lock_idle)
                   1838: #endif
                   1839:        movl    _C_LABEL(sched_whichqs),%ecx
                   1840:        bsfl    %ecx,%ebx
                   1841:        jz      idle_unlock
                   1842:
1.30    ! junyoung 1843: switch_dequeue:
        !          1844:        /*
1.1       fvdl     1845:         * we're running at splhigh(), but it's otherwise okay to take
1.30    ! junyoung 1846:         * interrupts here.
1.1       fvdl     1847:         */
                   1848:        sti
                   1849:        leal    _C_LABEL(sched_qs)(,%ebx,8),%eax # select q
                   1850:
1.5       thorpej  1851:        movl    L_FORW(%eax),%edi       # unlink from front of process q
1.1       fvdl     1852: #ifdef DIAGNOSTIC
                   1853:        cmpl    %edi,%eax               # linked to self (i.e. nothing queued)?
                   1854:        je      _C_LABEL(switch_error)  # not possible
                   1855: #endif /* DIAGNOSTIC */
1.5       thorpej  1856:        movl    L_FORW(%edi),%edx
                   1857:        movl    %edx,L_FORW(%eax)
                   1858:        movl    %eax,L_BACK(%edx)
1.1       fvdl     1859:
                   1860:        cmpl    %edx,%eax               # q empty?
                   1861:        jne     3f
                   1862:
                   1863:        btrl    %ebx,%ecx               # yes, clear to indicate empty
                   1864:        movl    %ecx,_C_LABEL(sched_whichqs) # update q status
                   1865:
                   1866: 3:     /* We just did it. */
                   1867:        xorl    %eax,%eax
                   1868:        CLEAR_RESCHED(%eax)
                   1869:
1.5       thorpej  1870: switch_resume:
1.1       fvdl     1871: #ifdef DIAGNOSTIC
1.5       thorpej  1872:        cmpl    %eax,L_WCHAN(%edi)      # Waiting for something?
1.1       fvdl     1873:        jne     _C_LABEL(switch_error)  # Yes; shouldn't be queued.
1.5       thorpej  1874:        cmpb    $LSRUN,L_STAT(%edi)     # In run state?
1.1       fvdl     1875:        jne     _C_LABEL(switch_error)  # No; shouldn't be queued.
                   1876: #endif /* DIAGNOSTIC */
                   1877:
1.5       thorpej  1878:        /* Isolate lwp.  XXX Is this necessary? */
                   1879:        movl    %eax,L_BACK(%edi)
1.1       fvdl     1880:
1.5       thorpej  1881:        /* Record new lwp. */
                   1882:        movb    $LSONPROC,L_STAT(%edi)  # l->l_stat = LSONPROC
                   1883:        SET_CURLWP(%edi,%ecx)
1.1       fvdl     1884:
1.5       thorpej  1885:        /* Skip context switch if same lwp. */
1.10      fvdl     1886:        xorl    %ebx,%ebx
1.1       fvdl     1887:        cmpl    %edi,%esi
                   1888:        je      switch_return
                   1889:
1.5       thorpej  1890:        /* If old lwp exited, don't bother. */
1.1       fvdl     1891:        testl   %esi,%esi
                   1892:        jz      switch_exited
                   1893:
                   1894:        /*
                   1895:         * Second phase: save old context.
                   1896:         *
                   1897:         * Registers:
                   1898:         *   %eax, %ecx - scratch
1.5       thorpej  1899:         *   %esi - old lwp, then old pcb
                   1900:         *   %edi - new lwp
1.1       fvdl     1901:         */
                   1902:
                   1903:        pushl   %esi
1.24      yamt     1904:        call    _C_LABEL(pmap_deactivate2)      # pmap_deactivate(oldproc)
1.1       fvdl     1905:        addl    $4,%esp
                   1906:
1.5       thorpej  1907:        movl    L_ADDR(%esi),%esi
1.1       fvdl     1908:
                   1909:        /* Save stack pointers. */
                   1910:        movl    %esp,PCB_ESP(%esi)
                   1911:        movl    %ebp,PCB_EBP(%esi)
                   1912:
                   1913: switch_exited:
                   1914:        /*
                   1915:         * Third phase: restore saved context.
                   1916:         *
                   1917:         * Registers:
                   1918:         *   %eax, %ebx, %ecx, %edx - scratch
                   1919:         *   %esi - new pcb
1.5       thorpej  1920:         *   %edi - new lwp
1.1       fvdl     1921:         */
                   1922:
                   1923:        /* No interrupts while loading new state. */
                   1924:        cli
1.5       thorpej  1925:        movl    L_ADDR(%edi),%esi
1.1       fvdl     1926:
                   1927:        /* Restore stack pointers. */
                   1928:        movl    PCB_ESP(%esi),%esp
                   1929:        movl    PCB_EBP(%esi),%ebp
                   1930:
                   1931: #if 0
                   1932:        /* Don't bother with the rest if switching to a system process. */
1.5       thorpej  1933:        testl   $P_SYSTEM,L_FLAG(%edi); XXX NJWLWP lwp's don't have P_SYSTEM!
1.1       fvdl     1934:        jnz     switch_restored
                   1935: #endif
                   1936:
1.26      yamt     1937:        /* Switch TSS. Reset "task busy" flag before loading. */
                   1938:        movl    %cr3,%eax
                   1939:        movl    %eax,PCB_CR3(%esi) /* XXX should be done by pmap_activate? */
1.1       fvdl     1940: #ifdef MULTIPROCESSOR
                   1941:        movl    CPUVAR(GDT),%eax
1.30    ! junyoung 1942: #else
1.1       fvdl     1943:        /* Load TSS info. */
                   1944:        movl    _C_LABEL(gdt),%eax
                   1945: #endif
1.5       thorpej  1946:        movl    L_MD_TSS_SEL(%edi),%edx
1.1       fvdl     1947:
                   1948:        andl    $~0x0200,4(%eax,%edx, 1)
                   1949:        ltr     %dx
                   1950:
                   1951:        pushl   %edi
                   1952:        call    _C_LABEL(pmap_activate)         # pmap_activate(p)
                   1953:        addl    $4,%esp
                   1954:
                   1955: #if 0
                   1956: switch_restored:
                   1957: #endif
                   1958:        /* Restore cr0 (including FPU state). */
                   1959:        movl    PCB_CR0(%esi),%ecx
                   1960: #ifdef MULTIPROCESSOR
1.30    ! junyoung 1961:        /*
1.22      wiz      1962:         * If our floating point registers are on a different CPU,
1.1       fvdl     1963:         * clear CR0_TS so we'll trap rather than reuse bogus state.
                   1964:         */
                   1965:        movl    PCB_FPCPU(%esi),%ebx
                   1966:        cmpl    CPUVAR(SELF),%ebx
                   1967:        jz      1f
                   1968:        orl     $CR0_TS,%ecx
1.30    ! junyoung 1969: 1:
        !          1970: #endif
1.1       fvdl     1971:        movl    %ecx,%cr0
                   1972:
                   1973:        /* Record new pcb. */
                   1974:        SET_CURPCB(%esi)
                   1975:
                   1976:        /* Interrupts are okay again. */
                   1977:        sti
                   1978:
                   1979: /*
                   1980:  *  Check for restartable atomic sequences (RAS)
                   1981:  */
1.5       thorpej  1982:        movl    CPUVAR(CURLWP),%edi
                   1983:        movl    L_PROC(%edi),%esi
1.20      dsl      1984:        cmpl    $0,P_RASLIST(%esi)
                   1985:        jne     2f
1.1       fvdl     1986: 1:
1.10      fvdl     1987:        movl    $1,%ebx
1.1       fvdl     1988:
                   1989: switch_return:
1.30    ! junyoung 1990: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl     1991:        call    _C_LABEL(sched_unlock_idle)
                   1992: #endif
1.28      yamt     1993:        cmpl    $0,CPUVAR(IPENDING)
                   1994:        jz      3f
1.2       fvdl     1995:        pushl   $IPL_NONE               # spl0()
1.1       fvdl     1996:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl     1997:        addl    $4,%esp
1.28      yamt     1998: 3:
1.1       fvdl     1999:        movl    $IPL_HIGH,CPUVAR(ILEVEL)        # splhigh()
1.9       fvdl     2000:
                   2001:        movl    %ebx,%eax
1.30    ! junyoung 2002:
1.1       fvdl     2003:        popl    %edi
                   2004:        popl    %esi
                   2005:        popl    %ebx
                   2006:        ret
1.20      dsl      2007:
                   2008: 2:                                     # check RAS list
                   2009:        movl    L_MD_REGS(%edi),%ebx
                   2010:        movl    TF_EIP(%ebx),%eax
                   2011:        pushl   %eax
                   2012:        pushl   %esi
                   2013:        call    _C_LABEL(ras_lookup)
                   2014:        addl    $8,%esp
                   2015:        cmpl    $-1,%eax
                   2016:        je      1b
                   2017:        movl    %eax,TF_EIP(%ebx)
                   2018:        jmp     1b
1.1       fvdl     2019:
                   2020: /*
1.5       thorpej  2021:  * void cpu_switchto(struct lwp *current, struct lwp *next)
                   2022:  * Switch to the specified next LWP.
                   2023:  */
                   2024: ENTRY(cpu_switchto)
                   2025:        pushl   %ebx
                   2026:        pushl   %esi
                   2027:        pushl   %edi
                   2028:
                   2029: #ifdef DEBUG
                   2030:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                   2031:        jae     1f
                   2032:        pushl   $2f
                   2033:        call    _C_LABEL(panic)
                   2034:        /* NOTREACHED */
                   2035: 2:     .asciz  "not splsched() in cpu_switchto!"
                   2036: 1:
                   2037: #endif /* DEBUG */
                   2038:
                   2039:        movl    16(%esp),%esi           # current
                   2040:        movl    20(%esp),%edi           # next
                   2041:
                   2042:        /*
                   2043:         * Clear curlwp so that we don't accumulate system time while idle.
                   2044:         * This also insures that schedcpu() will move the old process to
                   2045:         * the correct queue if it happens to get called from the spllower()
                   2046:         * below and changes the priority.  (See corresponding comment in
                   2047:         * usrret()).
                   2048:         *
                   2049:         * XXX Is this necessary?  We know we won't go idle.
                   2050:         */
                   2051:        movl    $0,CPUVAR(CURLWP)
                   2052:
                   2053:        /*
                   2054:         * We're running at splhigh(), but it's otherwise okay to take
                   2055:         * interrupts here.
                   2056:         */
                   2057:        sti
                   2058:
                   2059:        /* Jump into the middle of cpu_switch */
                   2060:        xorl    %eax,%eax
                   2061:        jmp     switch_resume
                   2062:
                   2063: /*
1.21      jdolecek 2064:  * void cpu_exit(struct lwp *l)
1.30    ! junyoung 2065:  * Switch to the appropriate idle context (lwp0's if uniprocessor; the CPU's
        !          2066:  * if multiprocessor) and deallocate the address space and kernel stack for p.
1.1       fvdl     2067:  * Then jump into cpu_switch(), as if we were in the idle proc all along.
                   2068:  */
                   2069: #ifndef MULTIPROCESSOR
1.5       thorpej  2070:        .globl  _C_LABEL(lwp0)
1.1       fvdl     2071: #endif
                   2072:        .globl  _C_LABEL(uvmspace_free),_C_LABEL(kernel_map)
                   2073:        .globl  _C_LABEL(uvm_km_free),_C_LABEL(tss_free)
1.21      jdolecek 2074: /* LINTSTUB: Func: void cpu_exit(struct lwp *l) */
                   2075: ENTRY(cpu_exit)
1.1       fvdl     2076:        movl    4(%esp),%edi            # old process
                   2077: #ifndef MULTIPROCESSOR
1.5       thorpej  2078:        movl    $_C_LABEL(lwp0),%ebx
                   2079:        movl    L_ADDR(%ebx),%esi
                   2080:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl     2081: #else
                   2082:        movl    CPUVAR(IDLE_PCB),%esi
                   2083:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                   2084: #endif
                   2085:        /* In case we fault... */
1.5       thorpej  2086:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl     2087:
                   2088:        /* Restore the idle context. */
                   2089:        cli
                   2090:
                   2091:        /* Restore stack pointers. */
                   2092:        movl    PCB_ESP(%esi),%esp
                   2093:        movl    PCB_EBP(%esi),%ebp
                   2094:
1.26      yamt     2095:        /* Switch TSS. Reset "task busy" flag before loading. */
                   2096:        movl    %cr3,%eax
                   2097:        movl    %eax,PCB_CR3(%esi)
1.1       fvdl     2098: #ifdef MULTIPROCESSOR
                   2099:        movl    CPUVAR(GDT),%eax
1.30    ! junyoung 2100: #else
1.1       fvdl     2101:        /* Load TSS info. */
                   2102:        movl    _C_LABEL(gdt),%eax
                   2103: #endif
                   2104:
                   2105:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                   2106:        ltr     %dx
                   2107:
                   2108:        /* We're always in the kernel, so we don't need the LDT. */
                   2109:
                   2110:        /* Restore cr0 (including FPU state). */
                   2111:        movl    PCB_CR0(%esi),%ecx
                   2112:        movl    %ecx,%cr0
                   2113:
                   2114:        /* Record new pcb. */
                   2115:        SET_CURPCB(%esi)
                   2116:
                   2117:        /* Interrupts are okay again. */
                   2118:        sti
                   2119:
                   2120:        /*
1.21      jdolecek 2121:         * Schedule the dead LWP's stack to be freed.
1.1       fvdl     2122:         */
1.21      jdolecek 2123:        pushl   %edi
                   2124:        call    _C_LABEL(lwp_exit2)
1.1       fvdl     2125:        addl    $4,%esp
                   2126:
                   2127:        /* Jump into cpu_switch() with the right state. */
                   2128:        xorl    %esi,%esi
1.5       thorpej  2129:        movl    %esi,CPUVAR(CURLWP)
1.1       fvdl     2130:        jmp     idle_start
                   2131:
                   2132: /*
                   2133:  * void savectx(struct pcb *pcb);
                   2134:  * Update pcb, saving current processor state.
                   2135:  */
                   2136: /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
                   2137: ENTRY(savectx)
                   2138:        movl    4(%esp),%edx            # edx = p->p_addr
1.30    ! junyoung 2139:
1.1       fvdl     2140:        /* Save stack pointers. */
                   2141:        movl    %esp,PCB_ESP(%edx)
                   2142:        movl    %ebp,PCB_EBP(%edx)
                   2143:
                   2144:        ret
                   2145:
                   2146: /*
                   2147:  * Old call gate entry for syscall
                   2148:  */
                   2149: /* LINTSTUB: Var: char Xosyscall[1]; */
                   2150: IDTVEC(osyscall)
                   2151:        /* Set eflags in trap frame. */
                   2152:        pushfl
                   2153:        popl    8(%esp)
                   2154:        pushl   $7              # size of instruction for restart
                   2155:        jmp     syscall1
                   2156:
                   2157: /*
                   2158:  * Trap gate entry for syscall
                   2159:  */
                   2160: /* LINTSTUB: Var: char Xsyscall[1]; */
                   2161: IDTVEC(syscall)
                   2162:        pushl   $2              # size of instruction for restart
                   2163: syscall1:
                   2164:        pushl   $T_ASTFLT       # trap # for doing ASTs
                   2165:        INTRENTRY
                   2166:
                   2167: #ifdef DIAGNOSTIC
1.24      yamt     2168:        cmpl    $0, CPUVAR(WANT_PMAPLOAD)
                   2169:        jz      1f
                   2170:        pushl   $6f
                   2171:        call    _C_LABEL(printf)
                   2172:        addl    $4, %esp
                   2173: 1:
1.1       fvdl     2174:        movl    CPUVAR(ILEVEL),%ebx
                   2175:        testl   %ebx,%ebx
                   2176:        jz      1f
                   2177:        pushl   $5f
                   2178:        call    _C_LABEL(printf)
                   2179:        addl    $4,%esp
                   2180: #ifdef DDB
                   2181:        int     $3
                   2182: #endif
1.30    ! junyoung 2183: 1:
1.1       fvdl     2184: #endif /* DIAGNOSTIC */
1.5       thorpej  2185:        movl    CPUVAR(CURLWP),%edx
                   2186:        movl    %esp,L_MD_REGS(%edx)    # save pointer to frame
                   2187:        movl    L_PROC(%edx),%edx
1.15      fvdl     2188:        pushl   %esp
1.1       fvdl     2189:        call    *P_MD_SYSCALL(%edx)     # get pointer to syscall() function
1.15      fvdl     2190:        addl    $4,%esp
1.27      yamt     2191: .Lsyscall_checkast:
1.24      yamt     2192:        /* Check for ASTs on exit to user mode. */
1.1       fvdl     2193:        cli
1.5       thorpej  2194:        CHECK_ASTPENDING(%eax)
1.1       fvdl     2195:        je      1f
                   2196:        /* Always returning to user mode here. */
1.5       thorpej  2197:        CLEAR_ASTPENDING(%eax)
1.1       fvdl     2198:        sti
                   2199:        /* Pushed T_ASTFLT into tf_trapno on entry. */
1.15      fvdl     2200:        pushl   %esp
1.1       fvdl     2201:        call    _C_LABEL(trap)
1.15      fvdl     2202:        addl    $4,%esp
1.27      yamt     2203:        jmp     .Lsyscall_checkast      /* re-check ASTs */
1.24      yamt     2204: 1:     CHECK_DEFERRED_SWITCH(%eax)
                   2205:        jnz     9f
1.1       fvdl     2206: #ifndef DIAGNOSTIC
1.24      yamt     2207:        INTRFASTEXIT
1.1       fvdl     2208: #else /* DIAGNOSTIC */
1.24      yamt     2209:        cmpl    $IPL_NONE,CPUVAR(ILEVEL)
1.1       fvdl     2210:        jne     3f
                   2211:        INTRFASTEXIT
                   2212: 3:     sti
                   2213:        pushl   $4f
                   2214:        call    _C_LABEL(printf)
                   2215:        addl    $4,%esp
                   2216: #ifdef DDB
                   2217:        int     $3
                   2218: #endif /* DDB */
                   2219:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                   2220:        jmp     2b
                   2221: 4:     .asciz  "WARNING: SPL NOT LOWERED ON SYSCALL EXIT\n"
1.30    ! junyoung 2222: 5:     .asciz  "WARNING: SPL NOT ZERO ON SYSCALL ENTRY\n"
        !          2223: 6:     .asciz  "WARNING: WANT PMAPLOAD ON SYSCALL ENTRY\n"
1.1       fvdl     2224: #endif /* DIAGNOSTIC */
1.24      yamt     2225: 9:     sti
                   2226:        call    _C_LABEL(pmap_load)
1.27      yamt     2227:        jmp     .Lsyscall_checkast      /* re-check ASTs */
1.1       fvdl     2228:
                   2229: #if NNPX > 0
                   2230: /*
                   2231:  * Special interrupt handlers.  Someday intr0-intr15 will be used to count
                   2232:  * interrupts.  We'll still need a special exception 16 handler.  The busy
                   2233:  * latch stuff in probintr() can be moved to npxprobe().
                   2234:  */
                   2235:
                   2236: /* LINTSTUB: Func: void probeintr(void) */
                   2237: NENTRY(probeintr)
                   2238:        ss
                   2239:        incl    _C_LABEL(npx_intrs_while_probing)
                   2240:        pushl   %eax
                   2241:        movb    $0x20,%al       # EOI (asm in strings loses cpp features)
                   2242:        outb    %al,$0xa0       # IO_ICU2
                   2243:        outb    %al,$0x20       # IO_ICU1
                   2244:        movb    $0,%al
                   2245:        outb    %al,$0xf0       # clear BUSY# latch
                   2246:        popl    %eax
                   2247:        iret
                   2248:
                   2249: /* LINTSTUB: Func: void probetrap(void) */
                   2250: NENTRY(probetrap)
                   2251:        ss
                   2252:        incl    _C_LABEL(npx_traps_while_probing)
                   2253:        fnclex
                   2254:        iret
                   2255:
                   2256: /* LINTSTUB: Func: int npx586bug1(int a, int b) */
                   2257: NENTRY(npx586bug1)
                   2258:        fildl   4(%esp)         # x
                   2259:        fildl   8(%esp)         # y
                   2260:        fld     %st(1)
                   2261:        fdiv    %st(1),%st      # x/y
                   2262:        fmulp   %st,%st(1)      # (x/y)*y
                   2263:        fsubrp  %st,%st(1)      # x-(x/y)*y
                   2264:        pushl   $0
                   2265:        fistpl  (%esp)
                   2266:        popl    %eax
                   2267:        ret
                   2268: #endif /* NNPX > 0 */

CVSweb <webmaster@jp.NetBSD.org>