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

1.25.2.1  tron        1: /*     $NetBSD$        */
1.1       fvdl        2:
                      3: /*-
                      4:  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Charles M. Hannum.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *        This product includes software developed by the NetBSD
                     21:  *        Foundation, Inc. and its contributors.
                     22:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     23:  *    contributors may be used to endorse or promote products derived
                     24:  *    from this software without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     27:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
                     37:  */
                     38:
                     39: /*-
                     40:  * Copyright (c) 1990 The Regents of the University of California.
                     41:  * All rights reserved.
                     42:  *
                     43:  * This code is derived from software contributed to Berkeley by
                     44:  * William Jolitz.
                     45:  *
                     46:  * Redistribution and use in source and binary forms, with or without
                     47:  * modification, are permitted provided that the following conditions
                     48:  * are met:
                     49:  * 1. Redistributions of source code must retain the above copyright
                     50:  *    notice, this list of conditions and the following disclaimer.
                     51:  * 2. Redistributions in binary form must reproduce the above copyright
                     52:  *    notice, this list of conditions and the following disclaimer in the
                     53:  *    documentation and/or other materials provided with the distribution.
1.12      agc        54:  * 3. Neither the name of the University nor the names of its contributors
1.1       fvdl       55:  *    may be used to endorse or promote products derived from this software
                     56:  *    without specific prior written permission.
                     57:  *
                     58:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     59:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     60:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     61:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     62:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     63:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     64:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     65:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     66:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     67:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     68:  * SUCH DAMAGE.
                     69:  *
                     70:  *     @(#)locore.s    7.3 (Berkeley) 5/13/91
                     71:  */
                     72:
1.18      christos   73: #include "opt_compat_netbsd.h"
                     74: #include "opt_compat_oldboot.h"
1.1       fvdl       75: #include "opt_cputype.h"
                     76: #include "opt_ddb.h"
                     77: #include "opt_ipkdb.h"
1.18      christos   78: #include "opt_lockdebug.h"
1.1       fvdl       79: #include "opt_multiprocessor.h"
                     80: #include "opt_realmem.h"
1.18      christos   81: #include "opt_user_ldt.h"
                     82: #include "opt_vm86.h"
1.1       fvdl       83:
                     84: #include "npx.h"
                     85: #include "assym.h"
                     86: #include "apm.h"
                     87: #include "lapic.h"
                     88: #include "ioapic.h"
1.8       fvdl       89: #include "ksyms.h"
1.1       fvdl       90:
                     91: #include <sys/errno.h>
                     92: #include <sys/syscall.h>
                     93:
                     94: #include <machine/cputypes.h>
                     95: #include <machine/param.h>
                     96: #include <machine/pte.h>
                     97: #include <machine/segments.h>
                     98: #include <machine/specialreg.h>
                     99: #include <machine/trap.h>
                    100: #include <machine/bootinfo.h>
                    101:
                    102: #if NLAPIC > 0
                    103: #include <machine/i82489reg.h>
                    104: #endif
                    105:
                    106: /* LINTSTUB: include <sys/types.h> */
                    107: /* LINTSTUB: include <machine/cpu.h> */
                    108: /* LINTSTUB: include <sys/systm.h> */
                    109:
                    110: #include <machine/asm.h>
                    111:
                    112: #if defined(MULTIPROCESSOR)
                    113:
1.5       thorpej   114: #define SET_CURLWP(lwp,cpu)                            \
1.1       fvdl      115:        movl    CPUVAR(SELF),cpu                ;       \
1.5       thorpej   116:        movl    lwp,CPUVAR(CURLWP)      ;       \
                    117:        movl    cpu,L_CPU(lwp)
1.1       fvdl      118:
                    119: #else
                    120:
1.5       thorpej   121: #define SET_CURLWP(lwp,tcpu)           movl    lwp,CPUVAR(CURLWP)
                    122: #define GET_CURLWP(reg)                        movl    CPUVAR(CURLWP),reg
1.1       fvdl      123:
                    124: #endif
                    125:
                    126: #define GET_CURPCB(reg)                        movl    CPUVAR(CURPCB),reg
                    127: #define SET_CURPCB(reg)                        movl    reg,CPUVAR(CURPCB)
1.24      yamt      128:
1.1       fvdl      129: #define CLEAR_RESCHED(reg)             movl    reg,CPUVAR(RESCHED)
                    130:
                    131: /* XXX temporary kluge; these should not be here */
                    132: /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
                    133: #include <dev/isa/isareg.h>
                    134:
                    135:
                    136: /* Disallow old names for REALBASEMEM */
                    137: #ifdef BIOSBASEMEM
                    138: #error BIOSBASEMEM option deprecated; use REALBASEMEM only if memory size reported by latest boot block is incorrect
                    139: #endif
                    140:
                    141: /* Disallow old names for REALEXTMEM */
                    142: #ifdef EXTMEM_SIZE
                    143: #error EXTMEM_SIZE option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
                    144: #endif
                    145: #ifdef BIOSEXTMEM
                    146: #error BIOSEXTMEM option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
                    147: #endif
                    148:
                    149: #include <machine/frameasm.h>
                    150:
                    151:
                    152: #ifdef MULTIPROCESSOR
                    153: #include <machine/i82489reg.h>
                    154: #endif
                    155:
                    156: /*
                    157:  * PTmap is recursive pagemap at top of virtual address space.
                    158:  * Within PTmap, the page directory can be found (third indirection).
                    159:  *
                    160:  * XXX 4 == sizeof pde
                    161:  */
                    162:        .set    _C_LABEL(PTmap),(PDSLOT_PTE << PDSHIFT)
1.7       thorpej   163:        .set    _C_LABEL(PTD),(_C_LABEL(PTmap) + PDSLOT_PTE * PAGE_SIZE)
1.1       fvdl      164:        .set    _C_LABEL(PTDpde),(_C_LABEL(PTD) + PDSLOT_PTE * 4)
                    165:
                    166: /*
                    167:  * APTmap, APTD is the alternate recursive pagemap.
                    168:  * It's used when modifying another process's page tables.
                    169:  *
                    170:  * XXX 4 == sizeof pde
                    171:  */
                    172:        .set    _C_LABEL(APTmap),(PDSLOT_APTE << PDSHIFT)
1.7       thorpej   173:        .set    _C_LABEL(APTD),(_C_LABEL(APTmap) + PDSLOT_APTE * PAGE_SIZE)
1.1       fvdl      174:        .set    _C_LABEL(APTDpde),(_C_LABEL(PTD) + PDSLOT_APTE * 4)
                    175:
                    176:
                    177: /*
                    178:  * Initialization
                    179:  */
                    180:        .data
                    181:
                    182:        .globl  _C_LABEL(cpu)
                    183:        .globl  _C_LABEL(esym),_C_LABEL(boothowto)
                    184:        .globl  _C_LABEL(bootinfo),_C_LABEL(atdevbase)
                    185: #ifdef COMPAT_OLDBOOT
                    186:        .globl  _C_LABEL(bootdev)
                    187: #endif
                    188:        .globl  _C_LABEL(proc0paddr),_C_LABEL(PTDpaddr)
                    189:        .globl  _C_LABEL(biosbasemem),_C_LABEL(biosextmem)
                    190:        .globl  _C_LABEL(gdt)
                    191: #ifdef I586_CPU
                    192:        .globl  _C_LABEL(idt)
                    193: #endif
                    194:        .globl  _C_LABEL(lapic_tpr)
                    195:
                    196: #if NLAPIC > 0
                    197: #ifdef __ELF__
1.7       thorpej   198:        .align  PAGE_SIZE
1.1       fvdl      199: #else
                    200:        .align  12
                    201: #endif
                    202:        .globl _C_LABEL(local_apic), _C_LABEL(lapic_id)
                    203: _C_LABEL(local_apic):
                    204:        .space  LAPIC_ID
                    205: _C_LABEL(lapic_id):
                    206:        .long   0x00000000
                    207:        .space  LAPIC_TPRI-(LAPIC_ID+4)
                    208: _C_LABEL(lapic_tpr):
                    209:        .space  LAPIC_PPRI-LAPIC_TPRI
                    210: _C_LABEL(lapic_ppr):
                    211:        .space  LAPIC_ISR-LAPIC_PPRI
                    212: _C_LABEL(lapic_isr):
1.7       thorpej   213:        .space  PAGE_SIZE-LAPIC_ISR
1.1       fvdl      214: #else
                    215: _C_LABEL(lapic_tpr):
                    216:        .long 0
                    217: #endif
                    218:
                    219:
                    220: _C_LABEL(cpu):         .long   0       # are we 386, 386sx, or 486,
                    221:                                        #   or Pentium, or..
                    222: _C_LABEL(esym):                .long   0       # ptr to end of syms
                    223: _C_LABEL(atdevbase):   .long   0       # location of start of iomem in virtual
                    224: _C_LABEL(proc0paddr):  .long   0
                    225: _C_LABEL(PTDpaddr):    .long   0       # paddr of PTD, for libkvm
                    226: #ifndef REALBASEMEM
                    227: _C_LABEL(biosbasemem): .long   0       # base memory reported by BIOS
                    228: #else
                    229: _C_LABEL(biosbasemem): .long   REALBASEMEM
                    230: #endif
                    231: #ifndef REALEXTMEM
                    232: _C_LABEL(biosextmem):  .long   0       # extended memory reported by BIOS
                    233: #else
                    234: _C_LABEL(biosextmem):  .long   REALEXTMEM
                    235: #endif
                    236:
                    237:        .space 512
                    238: tmpstk:
                    239:
                    240:
1.13      christos  241: #define        _RELOC(x)       ((x) - KERNBASE_LOCORE)
1.1       fvdl      242: #define        RELOC(x)        _RELOC(_C_LABEL(x))
                    243:
                    244:        .text
                    245:        .globl  _C_LABEL(kernel_text)
                    246:        .set    _C_LABEL(kernel_text),KERNTEXTOFF
                    247:
                    248:        .globl  start
                    249: start: movw    $0x1234,0x472                   # warm boot
                    250:
                    251:        /*
                    252:         * Load parameters from stack
                    253:         * (howto, [bootdev], bootinfo, esym, basemem, extmem).
                    254:         */
                    255:        movl    4(%esp),%eax
                    256:        movl    %eax,RELOC(boothowto)
                    257: #ifdef COMPAT_OLDBOOT
                    258:        movl    8(%esp),%eax
                    259:        movl    %eax,RELOC(bootdev)
                    260: #endif
                    261:        movl    12(%esp),%eax
                    262:
                    263:        testl   %eax, %eax
                    264:        jz      1f
                    265:        movl    (%eax), %ebx            /* number of entries */
                    266:        movl    $RELOC(bootinfo), %edi
                    267:        movl    %ebx, (%edi)
                    268:        addl    $4, %edi
                    269: 2:
                    270:        testl   %ebx, %ebx
                    271:        jz      1f
                    272:        addl    $4, %eax
                    273:        movl    (%eax), %ecx            /* address of entry */
                    274:        pushl   %eax
                    275:        pushl   (%ecx)                  /* len */
                    276:        pushl   %ecx
                    277:        pushl   %edi
                    278:        addl    (%ecx), %edi            /* update dest pointer */
                    279:        cmpl    $_RELOC(_C_LABEL(bootinfo) + BOOTINFO_MAXSIZE), %edi
                    280:        jg      2f
                    281:        call    _C_LABEL(memcpy)
                    282:        addl    $12, %esp
                    283:        popl    %eax
                    284:        subl    $1, %ebx
                    285:        jmp     2b
                    286: 2:     /* cleanup for overflow case */
                    287:        addl    $16, %esp
                    288:        movl    $RELOC(bootinfo), %edi
                    289:        subl    %ebx, (%edi)            /* correct number of entries */
                    290: 1:
                    291:
                    292:        movl    16(%esp),%eax
                    293:        testl   %eax,%eax
                    294:        jz      1f
1.13      christos  295:        addl    $KERNBASE_LOCORE,%eax
1.1       fvdl      296: 1:     movl    %eax,RELOC(esym)
                    297:
                    298:        movl    RELOC(biosextmem),%eax
                    299:        testl   %eax,%eax
                    300:        jnz     1f
                    301:        movl    20(%esp),%eax
                    302:        movl    %eax,RELOC(biosextmem)
                    303: 1:
                    304:        movl    RELOC(biosbasemem),%eax
                    305:        testl   %eax,%eax
                    306:        jnz     1f
                    307:        movl    24(%esp),%eax
                    308:        movl    %eax,RELOC(biosbasemem)
                    309: 1:
                    310:
                    311:        /* First, reset the PSL. */
                    312:        pushl   $PSL_MBO
                    313:        popfl
                    314:
                    315:        /* Clear segment registers; always null in proc0. */
                    316:        xorl    %eax,%eax
                    317:        movw    %ax,%fs
                    318:        movw    %ax,%gs
                    319:        decl    %eax
                    320:        movl    %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
                    321:
                    322:        /* Find out our CPU type. */
                    323:
                    324: try386:        /* Try to toggle alignment check flag; does not exist on 386. */
                    325:        pushfl
                    326:        popl    %eax
                    327:        movl    %eax,%ecx
                    328:        orl     $PSL_AC,%eax
                    329:        pushl   %eax
                    330:        popfl
                    331:        pushfl
                    332:        popl    %eax
                    333:        xorl    %ecx,%eax
                    334:        andl    $PSL_AC,%eax
                    335:        pushl   %ecx
                    336:        popfl
                    337:
                    338:        testl   %eax,%eax
                    339:        jnz     try486
                    340:
                    341:        /*
                    342:         * Try the test of a NexGen CPU -- ZF will not change on a DIV
                    343:         * instruction on a NexGen, it will on an i386.  Documented in
                    344:         * Nx586 Processor Recognition Application Note, NexGen, Inc.
                    345:         */
                    346:        movl    $0x5555,%eax
                    347:        xorl    %edx,%edx
                    348:        movl    $2,%ecx
                    349:        divl    %ecx
                    350:        jnz     is386
                    351:
                    352: isnx586:
                    353:        /*
                    354:         * Don't try cpuid, as Nx586s reportedly don't support the
                    355:         * PSL_ID bit.
                    356:         */
                    357:        movl    $CPU_NX586,RELOC(cpu)
                    358:        jmp     2f
                    359:
                    360: is386:
                    361:        movl    $CPU_386,RELOC(cpu)
                    362:        jmp     2f
                    363:
                    364: try486:        /* Try to toggle identification flag; does not exist on early 486s. */
                    365:        pushfl
                    366:        popl    %eax
                    367:        movl    %eax,%ecx
                    368:        xorl    $PSL_ID,%eax
                    369:        pushl   %eax
                    370:        popfl
                    371:        pushfl
                    372:        popl    %eax
                    373:        xorl    %ecx,%eax
                    374:        andl    $PSL_ID,%eax
                    375:        pushl   %ecx
                    376:        popfl
                    377:
                    378:        testl   %eax,%eax
                    379:        jnz     try586
                    380: is486: movl    $CPU_486,RELOC(cpu)
                    381:        /*
                    382:         * Check Cyrix CPU
                    383:         * Cyrix CPUs do not change the undefined flags following
                    384:         * execution of the divide instruction which divides 5 by 2.
                    385:         *
                    386:         * Note: CPUID is enabled on M2, so it passes another way.
                    387:         */
                    388:        pushfl
                    389:        movl    $0x5555, %eax
                    390:        xorl    %edx, %edx
                    391:        movl    $2, %ecx
                    392:        clc
                    393:        divl    %ecx
                    394:        jnc     trycyrix486
                    395:        popfl
                    396:        jmp 2f
                    397: trycyrix486:
                    398:        movl    $CPU_6x86,RELOC(cpu)    # set CPU type
                    399:        /*
                    400:         * Check for Cyrix 486 CPU by seeing if the flags change during a
                    401:         * divide. This is documented in the Cx486SLC/e SMM Programmer's
                    402:         * Guide.
                    403:         */
                    404:        xorl    %edx,%edx
                    405:        cmpl    %edx,%edx               # set flags to known state
                    406:        pushfl
                    407:        popl    %ecx                    # store flags in ecx
                    408:        movl    $-1,%eax
                    409:        movl    $4,%ebx
                    410:        divl    %ebx                    # do a long division
                    411:        pushfl
                    412:        popl    %eax
                    413:        xorl    %ecx,%eax               # are the flags different?
                    414:        testl   $0x8d5,%eax             # only check C|PF|AF|Z|N|V
                    415:        jne     2f                      # yes; must be Cyrix 6x86 CPU
                    416:        movl    $CPU_486DLC,RELOC(cpu)  # set CPU type
                    417:
                    418: #ifndef CYRIX_CACHE_WORKS
                    419:        /* Disable caching of the ISA hole only. */
                    420:        invd
                    421:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    422:        outb    %al,$0x22
                    423:        inb     $0x23,%al
                    424:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    425:        movb    %al,%ah
                    426:        movb    $CCR0,%al
                    427:        outb    %al,$0x22
                    428:        movb    %ah,%al
                    429:        outb    %al,$0x23
                    430:        invd
                    431: #else /* CYRIX_CACHE_WORKS */
                    432:        /* Set cache parameters */
                    433:        invd                            # Start with guaranteed clean cache
                    434:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    435:        outb    %al,$0x22
                    436:        inb     $0x23,%al
                    437:        andb    $~CCR0_NC0,%al
                    438: #ifndef CYRIX_CACHE_REALLY_WORKS
                    439:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    440: #else
                    441:        orb     $CCR0_NC1,%al
                    442: #endif
                    443:        movb    %al,%ah
                    444:        movb    $CCR0,%al
                    445:        outb    %al,$0x22
                    446:        movb    %ah,%al
                    447:        outb    %al,$0x23
                    448:        /* clear non-cacheable region 1 */
                    449:        movb    $(NCR1+2),%al
                    450:        outb    %al,$0x22
                    451:        movb    $NCR_SIZE_0K,%al
                    452:        outb    %al,$0x23
                    453:        /* clear non-cacheable region 2 */
                    454:        movb    $(NCR2+2),%al
                    455:        outb    %al,$0x22
                    456:        movb    $NCR_SIZE_0K,%al
                    457:        outb    %al,$0x23
                    458:        /* clear non-cacheable region 3 */
                    459:        movb    $(NCR3+2),%al
                    460:        outb    %al,$0x22
                    461:        movb    $NCR_SIZE_0K,%al
                    462:        outb    %al,$0x23
                    463:        /* clear non-cacheable region 4 */
                    464:        movb    $(NCR4+2),%al
                    465:        outb    %al,$0x22
                    466:        movb    $NCR_SIZE_0K,%al
                    467:        outb    %al,$0x23
                    468:        /* enable caching in CR0 */
                    469:        movl    %cr0,%eax
                    470:        andl    $~(CR0_CD|CR0_NW),%eax
                    471:        movl    %eax,%cr0
                    472:        invd
                    473: #endif /* CYRIX_CACHE_WORKS */
                    474:
                    475:        jmp     2f
                    476:
                    477: try586:        /* Use the `cpuid' instruction. */
                    478:        xorl    %eax,%eax
                    479:        cpuid
                    480:        movl    %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
                    481:
                    482: 2:
                    483:        /*
                    484:         * Finished with old stack; load new %esp now instead of later so we
                    485:         * can trace this code without having to worry about the trace trap
                    486:         * clobbering the memory test or the zeroing of the bss+bootstrap page
                    487:         * tables.
                    488:         *
                    489:         * The boot program should check:
                    490:         *      text+data <= &stack_variable - more_space_for_stack
                    491:         *      text+data+bss+pad+space_for_page_tables <= end_of_memory
                    492:         * Oops, the gdt is in the carcass of the boot program so clearing
                    493:         * the rest of memory is still not possible.
                    494:         */
                    495:        movl    $_RELOC(tmpstk),%esp    # bootstrap stack end location
                    496:
                    497: /*
                    498:  * Virtual address space of kernel:
                    499:  *
                    500:  * text | data | bss | [syms] | page dir | proc0 kstack
                    501:  *                           0          1       2      3
                    502:  */
1.7       thorpej   503: #define        PROC0PDIR       ((0)              * PAGE_SIZE)
                    504: #define        PROC0STACK      ((1)              * PAGE_SIZE)
                    505: #define        SYSMAP          ((1+UPAGES)       * PAGE_SIZE)
                    506: #define        TABLESIZE       ((1+UPAGES) * PAGE_SIZE) /* + nkpde * PAGE_SIZE */
1.1       fvdl      507:
                    508:        /* Find end of kernel image. */
                    509:        movl    $RELOC(end),%edi
1.8       fvdl      510: #if (NKSYMS || defined(DDB) || defined(LKM)) && !defined(SYMTAB_SPACE)
1.1       fvdl      511:        /* Save the symbols (if loaded). */
                    512:        movl    RELOC(esym),%eax
                    513:        testl   %eax,%eax
                    514:        jz      1f
1.13      christos  515:        subl    $KERNBASE_LOCORE,%eax
1.1       fvdl      516:        movl    %eax,%edi
                    517: 1:
                    518: #endif
                    519:
                    520:        /* Calculate where to start the bootstrap tables. */
                    521:        movl    %edi,%esi                       # edi = esym ? esym : end
                    522:        addl    $PGOFSET,%esi                   # page align up
                    523:        andl    $~PGOFSET,%esi
                    524:
                    525:        /*
                    526:         * Calculate the size of the kernel page table directory, and
1.25.2.2  tron      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.25.2.2  tron      531:        leal    TABLESIZE+NBPD+PDOFSET(%edi),%eax
                    532:        shrl    $PDSHIFT,%eax
1.1       fvdl      533:        movl    RELOC(nkpde),%ecx               # get nkpde
1.25.2.2  tron      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.25.2.2  tron      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
                    575:
                    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
                    664:
                    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
                    885:
                    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
                    927:
                    928: 4:     incl    %edi
                    929:        decl    %ecx
                    930:        jns     1b
                    931:
                    932:        movl    20(%esp),%edi
                    933:        movl    24(%esp),%eax
                    934:        jmp     3f
                    935:
                    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
                    975:
                    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)
                   1030:
                   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
1.25.2.3! tron     1183:        jc      _C_LABEL(copystr_efault)
1.1       fvdl     1184:        cmpl    %edx,%eax
                   1185:        jae     1f
                   1186:        movl    %eax,%edx
                   1187:        movl    %eax,20(%esp)
                   1188:
                   1189: 1:     incl    %edx
                   1190:        cld
                   1191:
                   1192: 1:     decl    %edx
                   1193:        jz      2f
                   1194:        lodsb
                   1195:        stosb
                   1196:        testb   %al,%al
                   1197:        jnz     1b
                   1198:
                   1199:        /* Success -- 0 byte reached. */
                   1200:        decl    %edx
                   1201:        xorl    %eax,%eax
                   1202:        jmp     copystr_return
                   1203:
                   1204: 2:     /* edx is zero -- return EFAULT or ENAMETOOLONG. */
                   1205:        cmpl    $VM_MAXUSER_ADDRESS,%edi
                   1206:        jae     _C_LABEL(copystr_efault)
                   1207:        movl    $ENAMETOOLONG,%eax
                   1208:        jmp     copystr_return
                   1209: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1210:
                   1211: /*
                   1212:  * int copyinstr(const void *from, void *to, size_t maxlen, size_t *lencopied);
                   1213:  * Copy a NUL-terminated string, at most maxlen characters long, from the
                   1214:  * user's address space.  Return the number of characters copied (including the
                   1215:  * NUL) in *lencopied.  If the string is too long, return ENAMETOOLONG; else
                   1216:  * return 0 or EFAULT.
                   1217:  * see copyinstr(9)
                   1218:  */
                   1219: /* LINTSTUB: Func: int copyinstr(const void *uaddr, void *kaddr, size_t len, size_t *done) */
                   1220: ENTRY(copyinstr)
                   1221:        pushl   %esi
                   1222:        pushl   %edi
1.24      yamt     1223:
                   1224:        DO_DEFERRED_SWITCH(%eax)
                   1225:
1.1       fvdl     1226:        GET_CURPCB(%ecx)
                   1227:        movl    $_C_LABEL(copystr_fault),PCB_ONFAULT(%ecx)
                   1228:
                   1229:        movl    12(%esp),%esi           # %esi = from
                   1230:        movl    16(%esp),%edi           # %edi = to
                   1231:        movl    20(%esp),%edx           # %edx = maxlen
                   1232:
                   1233:        /*
                   1234:         * Get min(%edx, VM_MAXUSER_ADDRESS-%esi).
                   1235:         */
                   1236:        movl    $VM_MAXUSER_ADDRESS,%eax
                   1237:        subl    %esi,%eax
1.25.2.3! tron     1238:        jc      _C_LABEL(copystr_efault)
1.1       fvdl     1239:        cmpl    %edx,%eax
                   1240:        jae     1f
                   1241:        movl    %eax,%edx
                   1242:        movl    %eax,20(%esp)
                   1243:
                   1244: 1:     incl    %edx
                   1245:        cld
                   1246:
                   1247: 1:     decl    %edx
                   1248:        jz      2f
                   1249:        lodsb
                   1250:        stosb
                   1251:        testb   %al,%al
                   1252:        jnz     1b
                   1253:
                   1254:        /* Success -- 0 byte reached. */
                   1255:        decl    %edx
                   1256:        xorl    %eax,%eax
                   1257:        jmp     copystr_return
                   1258:
                   1259: 2:     /* edx is zero -- return EFAULT or ENAMETOOLONG. */
                   1260:        cmpl    $VM_MAXUSER_ADDRESS,%esi
                   1261:        jae     _C_LABEL(copystr_efault)
                   1262:        movl    $ENAMETOOLONG,%eax
                   1263:        jmp     copystr_return
                   1264:
                   1265: /* LINTSTUB: Ignore */
                   1266: NENTRY(copystr_efault)
                   1267:        movl    $EFAULT,%eax
                   1268:
                   1269: /* LINTSTUB: Ignore */
                   1270: NENTRY(copystr_fault)
                   1271: copystr_return:
                   1272:        /* Set *lencopied and return %eax. */
                   1273:        GET_CURPCB(%ecx)
                   1274:        movl    $0,PCB_ONFAULT(%ecx)
                   1275:        movl    20(%esp),%ecx
                   1276:        subl    %edx,%ecx
                   1277:        movl    24(%esp),%edx
                   1278:        testl   %edx,%edx
                   1279:        jz      8f
                   1280:        movl    %ecx,(%edx)
                   1281:
                   1282: 8:     popl    %edi
                   1283:        popl    %esi
                   1284:        ret
                   1285:
                   1286: /*
                   1287:  * int copystr(const void *from, void *to, size_t maxlen, size_t *lencopied);
                   1288:  * Copy a NUL-terminated string, at most maxlen characters long.  Return the
                   1289:  * number of characters copied (including the NUL) in *lencopied.  If the
                   1290:  * string is too long, return ENAMETOOLONG; else return 0.
                   1291:  * see copystr(9)
                   1292:  */
                   1293: /* LINTSTUB: Func: int copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done) */
                   1294: ENTRY(copystr)
                   1295:        pushl   %esi
                   1296:        pushl   %edi
                   1297:
                   1298:        movl    12(%esp),%esi           # esi = from
                   1299:        movl    16(%esp),%edi           # edi = to
                   1300:        movl    20(%esp),%edx           # edx = maxlen
                   1301:        incl    %edx
                   1302:        cld
                   1303:
                   1304: 1:     decl    %edx
                   1305:        jz      4f
                   1306:        lodsb
                   1307:        stosb
                   1308:        testb   %al,%al
                   1309:        jnz     1b
                   1310:
                   1311:        /* Success -- 0 byte reached. */
                   1312:        decl    %edx
                   1313:        xorl    %eax,%eax
                   1314:        jmp     6f
                   1315:
                   1316: 4:     /* edx is zero -- return ENAMETOOLONG. */
                   1317:        movl    $ENAMETOOLONG,%eax
                   1318:
                   1319: 6:     /* Set *lencopied and return %eax. */
                   1320:        movl    20(%esp),%ecx
                   1321:        subl    %edx,%ecx
                   1322:        movl    24(%esp),%edx
                   1323:        testl   %edx,%edx
                   1324:        jz      7f
                   1325:        movl    %ecx,(%edx)
                   1326:
                   1327: 7:     popl    %edi
                   1328:        popl    %esi
                   1329:        ret
                   1330:
                   1331: /*
                   1332:  * long fuword(const void *uaddr);
                   1333:  * Fetch an int from the user's address space.
                   1334:  * see fuword(9)
                   1335:  */
                   1336: /* LINTSTUB: Func: long fuword(const void *base) */
                   1337: ENTRY(fuword)
1.24      yamt     1338:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1339:        movl    4(%esp),%edx
                   1340:        cmpl    $VM_MAXUSER_ADDRESS-4,%edx
                   1341:        ja      _C_LABEL(fusuaddrfault)
                   1342:        GET_CURPCB(%ecx)
                   1343:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1344:        movl    (%edx),%eax
                   1345:        movl    $0,PCB_ONFAULT(%ecx)
                   1346:        ret
                   1347:
                   1348: /*
                   1349:  * int fusword(const void *uaddr);
                   1350:  * Fetch a short from the user's address space.
                   1351:  * see fusword(9)
                   1352:  */
                   1353: /* LINTSTUB: Func: int fusword(const void *base) */
                   1354: ENTRY(fusword)
1.24      yamt     1355:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1356:        movl    4(%esp),%edx
                   1357:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1358:        ja      _C_LABEL(fusuaddrfault)
                   1359:        GET_CURPCB(%ecx)
                   1360:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1361:        movzwl  (%edx),%eax
                   1362:        movl    $0,PCB_ONFAULT(%ecx)
                   1363:        ret
                   1364:
                   1365: /*
                   1366:  * int fuswintr(const void *uaddr);
                   1367:  * Fetch a short from the user's address space.  Can be called during an
                   1368:  * interrupt.
                   1369:  * see fuswintr(9)
                   1370:  */
                   1371: /* LINTSTUB: Func: int fuswintr(const void *base) */
                   1372: ENTRY(fuswintr)
1.24      yamt     1373:        cmpl    $TLBSTATE_VALID, CPUVAR(TLBSTATE)
                   1374:        jnz     _C_LABEL(fusuaddrfault)
1.1       fvdl     1375:        movl    4(%esp),%edx
                   1376:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1377:        ja      _C_LABEL(fusuaddrfault)
1.5       thorpej  1378:        movl    CPUVAR(CURLWP),%ecx
                   1379:        movl    L_ADDR(%ecx),%ecx
1.1       fvdl     1380:        movl    $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
                   1381:        movzwl  (%edx),%eax
                   1382:        movl    $0,PCB_ONFAULT(%ecx)
                   1383:        ret
                   1384:
                   1385: /*
                   1386:  * int fubyte(const void *uaddr);
                   1387:  * Fetch a byte from the user's address space.
                   1388:  * see fubyte(9)
                   1389:  */
                   1390: /* LINTSTUB: Func: int fubyte(const void *base) */
                   1391: ENTRY(fubyte)
1.24      yamt     1392:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1393:        movl    4(%esp),%edx
                   1394:        cmpl    $VM_MAXUSER_ADDRESS-1,%edx
                   1395:        ja      _C_LABEL(fusuaddrfault)
                   1396:        GET_CURPCB(%ecx)
                   1397:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1398:        movzbl  (%edx),%eax
                   1399:        movl    $0,PCB_ONFAULT(%ecx)
                   1400:        ret
                   1401:
                   1402: /*
                   1403:  * Handle faults from [fs]u*().  Clean up and return -1.
                   1404:  */
                   1405: /* LINTSTUB: Ignore */
                   1406: NENTRY(fusufault)
                   1407:        movl    $0,PCB_ONFAULT(%ecx)
                   1408:        movl    $-1,%eax
                   1409:        ret
                   1410:
                   1411: /*
                   1412:  * Handle faults from [fs]u*().  Clean up and return -1.  This differs from
                   1413:  * fusufault() in that trap() will recognize it and return immediately rather
                   1414:  * than trying to page fault.
                   1415:  */
                   1416: /* LINTSTUB: Ignore */
                   1417: NENTRY(fusubail)
                   1418:        movl    $0,PCB_ONFAULT(%ecx)
                   1419:        movl    $-1,%eax
                   1420:        ret
                   1421:
                   1422: /*
                   1423:  * Handle earlier faults from [fs]u*(), due to our of range addresses.
                   1424:  */
                   1425: /* LINTSTUB: Ignore */
                   1426: NENTRY(fusuaddrfault)
                   1427:        movl    $-1,%eax
                   1428:        ret
                   1429:
                   1430: /*
                   1431:  * int suword(void *uaddr, long x);
                   1432:  * Store an int in the user's address space.
                   1433:  * see suword(9)
                   1434:  */
                   1435: /* LINTSTUB: Func: int suword(void *base, long c) */
                   1436: ENTRY(suword)
1.24      yamt     1437:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1438:        movl    4(%esp),%edx
                   1439:        cmpl    $VM_MAXUSER_ADDRESS-4,%edx
                   1440:        ja      _C_LABEL(fusuaddrfault)
                   1441:
                   1442: #if defined(I386_CPU)
                   1443: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1444:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1445:        jne     2f
                   1446: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1447:
                   1448:        GET_CURPCB(%eax)
                   1449:        movl    $3f,PCB_ONFAULT(%eax)
                   1450:
                   1451:        movl    %edx,%eax
                   1452:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1453:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1454:        jnz     1f
                   1455:
                   1456: 3:     /* Simulate a trap. */
                   1457:        pushl   %edx
                   1458:        pushl   %edx
                   1459:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1460:        addl    $4,%esp                 # clear parameter from the stack
                   1461:        popl    %edx
                   1462:        GET_CURPCB(%ecx)
                   1463:        testl   %eax,%eax
                   1464:        jnz     _C_LABEL(fusufault)
                   1465:
                   1466: 1:     /* XXX also need to check the following 3 bytes for validity! */
                   1467: #endif
                   1468:
                   1469: 2:     GET_CURPCB(%ecx)
                   1470:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1471:
                   1472:        movl    8(%esp),%eax
                   1473:        movl    %eax,(%edx)
                   1474:        xorl    %eax,%eax
                   1475:        movl    %eax,PCB_ONFAULT(%ecx)
                   1476:        ret
                   1477:
                   1478: /*
                   1479:  * int susword(void *uaddr, short x);
                   1480:  * Store a short in the user's address space.
                   1481:  * see susword(9)
                   1482:  */
                   1483: /* LINTSTUB: Func: int susword(void *base, short c) */
                   1484: ENTRY(susword)
1.24      yamt     1485:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1486:        movl    4(%esp),%edx
                   1487:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1488:        ja      _C_LABEL(fusuaddrfault)
                   1489:
                   1490: #if defined(I386_CPU)
                   1491: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1492:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1493:        jne     2f
                   1494: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1495:
                   1496:        GET_CURPCB(%eax)
                   1497:        movl    $3f,PCB_ONFAULT(%eax)
                   1498:
                   1499:        movl    %edx,%eax
                   1500:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1501:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1502:        jnz     1f
                   1503:
                   1504: 3:     /* Simulate a trap. */
                   1505:        pushl   %edx
                   1506:        pushl   %edx
                   1507:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1508:        addl    $4,%esp                 # clear parameter from the stack
                   1509:        popl    %edx
                   1510:        GET_CURPCB(%ecx)
                   1511:        testl   %eax,%eax
                   1512:        jnz     _C_LABEL(fusufault)
                   1513:
                   1514: 1:     /* XXX also need to check the following byte for validity! */
                   1515: #endif
                   1516:
                   1517: 2:     GET_CURPCB(%ecx)
                   1518:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1519:
                   1520:        movl    8(%esp),%eax
                   1521:        movw    %ax,(%edx)
                   1522:        xorl    %eax,%eax
                   1523:        movl    %eax,PCB_ONFAULT(%ecx)
                   1524:        ret
                   1525:
                   1526: /*
                   1527:  * int suswintr(void *uaddr, short x);
                   1528:  * Store a short in the user's address space.  Can be called during an
                   1529:  * interrupt.
                   1530:  * see suswintr(9)
                   1531:  */
                   1532: /* LINTSTUB: Func: int suswintr(void *base, short c) */
                   1533: ENTRY(suswintr)
1.24      yamt     1534:        cmpl    $TLBSTATE_VALID, CPUVAR(TLBSTATE)
                   1535:        jnz     _C_LABEL(fusuaddrfault)
1.1       fvdl     1536:        movl    4(%esp),%edx
                   1537:        cmpl    $VM_MAXUSER_ADDRESS-2,%edx
                   1538:        ja      _C_LABEL(fusuaddrfault)
1.5       thorpej  1539:        movl    CPUVAR(CURLWP),%ecx
                   1540:        movl    L_ADDR(%ecx),%ecx
1.1       fvdl     1541:        movl    $_C_LABEL(fusubail),PCB_ONFAULT(%ecx)
                   1542:
                   1543: #if defined(I386_CPU)
                   1544: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1545:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1546:        jne     2f
                   1547: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1548:
                   1549:        movl    %edx,%eax
                   1550:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1551:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1552:        jnz     1f
                   1553:
                   1554:        /* Simulate a trap. */
                   1555:        jmp     _C_LABEL(fusubail)
                   1556:
                   1557: 1:     /* XXX also need to check the following byte for validity! */
                   1558: #endif
                   1559:
                   1560: 2:     movl    8(%esp),%eax
                   1561:        movw    %ax,(%edx)
                   1562:        xorl    %eax,%eax
                   1563:        movl    %eax,PCB_ONFAULT(%ecx)
                   1564:        ret
                   1565:
                   1566: /*
                   1567:  * int subyte(void *uaddr, char x);
                   1568:  * Store a byte in the user's address space.
                   1569:  * see subyte(9)
                   1570:  */
                   1571: /* LINTSTUB: Func: int subyte(void *base, int c) */
                   1572: ENTRY(subyte)
1.24      yamt     1573:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl     1574:        movl    4(%esp),%edx
                   1575:        cmpl    $VM_MAXUSER_ADDRESS-1,%edx
                   1576:        ja      _C_LABEL(fusuaddrfault)
                   1577:
                   1578: #if defined(I386_CPU)
                   1579: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
                   1580:        cmpl    $CPUCLASS_386,_C_LABEL(cpu_class)
                   1581:        jne     2f
                   1582: #endif /* I486_CPU || I586_CPU || I686_CPU */
                   1583:
                   1584:        GET_CURPCB(%eax)
                   1585:        movl    $3f,PCB_ONFAULT(%eax)
                   1586:
                   1587:        movl    %edx,%eax
                   1588:        shrl    $PGSHIFT,%eax           # calculate pte address
                   1589:        testb   $PG_RW,_C_LABEL(PTmap)(,%eax,4)
                   1590:        jnz     1f
                   1591:
                   1592: 3:     /* Simulate a trap. */
                   1593:        pushl   %edx
                   1594:        pushl   %edx
                   1595:        call    _C_LABEL(trapwrite)     # trapwrite(addr)
                   1596:        addl    $4,%esp                 # clear parameter from the stack
                   1597:        popl    %edx
                   1598:        GET_CURPCB(%ecx)
                   1599:        testl   %eax,%eax
                   1600:        jnz     _C_LABEL(fusufault)
                   1601:
                   1602: 1:
                   1603: #endif
                   1604:
                   1605: 2:     GET_CURPCB(%ecx)
                   1606:        movl    $_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
                   1607:
                   1608:        movb    8(%esp),%al
                   1609:        movb    %al,(%edx)
                   1610:        xorl    %eax,%eax
                   1611:        movl    %eax,PCB_ONFAULT(%ecx)
                   1612:        ret
                   1613:
                   1614: /*****************************************************************************/
                   1615:
                   1616: /*
                   1617:  * The following is i386-specific nonsense.
                   1618:  */
                   1619:
                   1620: /*
                   1621:  * void lgdt(struct region_descriptor *rdp);
                   1622:  * Load a new GDT pointer (and do any necessary cleanup).
                   1623:  * XXX It's somewhat questionable whether reloading all the segment registers
                   1624:  * is necessary, since the actual descriptor data is not changed except by
                   1625:  * process creation and exit, both of which clean up via task switches.  OTOH,
                   1626:  * this only happens at run time when the GDT is resized.
                   1627:  */
                   1628: /* LINTSTUB: Func: void lgdt(struct region_descriptor *rdp) */
                   1629: NENTRY(lgdt)
                   1630:        /* Reload the descriptor table. */
                   1631:        movl    4(%esp),%eax
                   1632:        lgdt    (%eax)
                   1633:        /* Flush the prefetch queue. */
                   1634:        jmp     1f
                   1635:        nop
                   1636: 1:     /* Reload "stale" selectors. */
                   1637:        movl    $GSEL(GDATA_SEL, SEL_KPL),%eax
                   1638:        movw    %ax,%ds
                   1639:        movw    %ax,%es
                   1640:        movw    %ax,%gs
                   1641:        movw    %ax,%ss
                   1642:        movl    $GSEL(GCPU_SEL, SEL_KPL),%eax
                   1643:        movw    %ax,%fs
                   1644:        /* Reload code selector by doing intersegment return. */
                   1645:        popl    %eax
                   1646:        pushl   $GSEL(GCODE_SEL, SEL_KPL)
                   1647:        pushl   %eax
                   1648:        lret
                   1649:
                   1650: /*****************************************************************************/
                   1651:
                   1652: /*
                   1653:  * These functions are primarily used by DDB.
                   1654:  */
                   1655:
                   1656: /* LINTSTUB: Func: int setjmp (label_t *l) */
                   1657: ENTRY(setjmp)
                   1658:        movl    4(%esp),%eax
                   1659:        movl    %ebx,(%eax)             # save ebx
                   1660:        movl    %esp,4(%eax)            # save esp
                   1661:        movl    %ebp,8(%eax)            # save ebp
                   1662:        movl    %esi,12(%eax)           # save esi
                   1663:        movl    %edi,16(%eax)           # save edi
                   1664:        movl    (%esp),%edx             # get rta
                   1665:        movl    %edx,20(%eax)           # save eip
                   1666:        xorl    %eax,%eax               # return (0);
                   1667:        ret
                   1668:
                   1669: /* LINTSTUB: Func: void longjmp (label_t *l) */
                   1670: ENTRY(longjmp)
                   1671:        movl    4(%esp),%eax
                   1672:        movl    (%eax),%ebx             # restore ebx
                   1673:        movl    4(%eax),%esp            # restore esp
                   1674:        movl    8(%eax),%ebp            # restore ebp
                   1675:        movl    12(%eax),%esi           # restore esi
                   1676:        movl    16(%eax),%edi           # restore edi
                   1677:        movl    20(%eax),%edx           # get rta
                   1678:        movl    %edx,(%esp)             # put in return frame
                   1679:        xorl    %eax,%eax               # return (1);
                   1680:        incl    %eax
                   1681:        ret
                   1682:
                   1683: /*****************************************************************************/
                   1684:
                   1685:        .globl  _C_LABEL(sched_whichqs),_C_LABEL(sched_qs)
                   1686:        .globl  _C_LABEL(uvmexp),_C_LABEL(panic)
                   1687:
                   1688: #ifdef DIAGNOSTIC
                   1689: NENTRY(switch_error)
                   1690:        pushl   $1f
                   1691:        call    _C_LABEL(panic)
                   1692:        /* NOTREACHED */
                   1693: 1:     .asciz  "cpu_switch"
                   1694: #endif /* DIAGNOSTIC */
                   1695:
                   1696: /*
1.5       thorpej  1697:  * void cpu_switch(struct lwp *)
1.1       fvdl     1698:  * Find a runnable process and switch to it.  Wait if necessary.  If the new
                   1699:  * process is the same as the old one, we short-circuit the context save and
                   1700:  * restore.
                   1701:  *
                   1702:  * Note that the stack frame layout is known to "struct switchframe"
                   1703:  * in <machine/frame.h> and to the code in cpu_fork() which initializes
1.5       thorpej  1704:  * it for a new lwp.
1.1       fvdl     1705:  */
                   1706: ENTRY(cpu_switch)
                   1707:        pushl   %ebx
                   1708:        pushl   %esi
                   1709:        pushl   %edi
                   1710:
                   1711: #ifdef DEBUG
                   1712:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                   1713:        jae     1f
1.5       thorpej  1714:        pushl   $2f
1.1       fvdl     1715:        call    _C_LABEL(panic)
                   1716:        /* NOTREACHED */
1.5       thorpej  1717: 2:     .asciz  "not splsched() in cpu_switch!"
1.1       fvdl     1718: 1:
                   1719: #endif /* DEBUG */
                   1720:
1.5       thorpej  1721:        movl    16(%esp),%esi           # current
1.1       fvdl     1722:
                   1723:        /*
1.5       thorpej  1724:         * Clear curlwp so that we don't accumulate system time while idle.
                   1725:         * This also insures that schedcpu() will move the old lwp to
1.1       fvdl     1726:         * the correct queue if it happens to get called from the spllower()
                   1727:         * below and changes the priority.  (See corresponding comment in
                   1728:         * userret()).
                   1729:         */
1.5       thorpej  1730:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl     1731:        /*
1.5       thorpej  1732:         * First phase: find new lwp.
1.1       fvdl     1733:         *
                   1734:         * Registers:
                   1735:         *   %eax - queue head, scratch, then zero
                   1736:         *   %ebx - queue number
                   1737:         *   %ecx - cached value of whichqs
1.5       thorpej  1738:         *   %edx - next lwp in queue
                   1739:         *   %esi - old lwp
                   1740:         *   %edi - new lwp
1.1       fvdl     1741:         */
                   1742:
1.5       thorpej  1743:        /* Look for new lwp. */
1.1       fvdl     1744:        cli                             # splhigh doesn't do a cli
                   1745:        movl    _C_LABEL(sched_whichqs),%ecx
                   1746:        bsfl    %ecx,%ebx               # find a full q
                   1747:        jnz     switch_dequeue
                   1748:
                   1749:        /*
                   1750:         * idling:      save old context.
                   1751:         *
                   1752:         * Registers:
                   1753:         *   %eax, %ecx - scratch
1.5       thorpej  1754:         *   %esi - old lwp, then old pcb
1.1       fvdl     1755:         *   %edi - idle pcb
                   1756:         */
                   1757:
                   1758:        pushl   %esi
1.24      yamt     1759:        call    _C_LABEL(pmap_deactivate2)      # pmap_deactivate(oldproc)
1.1       fvdl     1760:        addl    $4,%esp
                   1761:
1.5       thorpej  1762:        movl    L_ADDR(%esi),%esi
1.1       fvdl     1763:
                   1764:        /* Save stack pointers. */
                   1765:        movl    %esp,PCB_ESP(%esi)
                   1766:        movl    %ebp,PCB_EBP(%esi)
                   1767:
                   1768:        /* Find idle PCB for this CPU */
                   1769: #ifndef MULTIPROCESSOR
1.5       thorpej  1770:        movl    $_C_LABEL(lwp0),%ebx
                   1771:        movl    L_ADDR(%ebx),%edi
                   1772:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl     1773: #else
                   1774:        movl    CPUVAR(IDLE_PCB),%edi
                   1775:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                   1776: #endif
1.5       thorpej  1777:        movl    $0,CPUVAR(CURLWP)               /* In case we fault... */
1.1       fvdl     1778:
                   1779:        /* Restore the idle context (avoid interrupts) */
                   1780:        cli
                   1781:
                   1782:        /* Restore stack pointers. */
                   1783:        movl    PCB_ESP(%edi),%esp
                   1784:        movl    PCB_EBP(%edi),%ebp
                   1785:
                   1786:        /* Switch TSS. Reset "task busy" flag before loading. */
1.25.2.1  tron     1787:        movl    %cr3,%eax
                   1788:        movl    %eax,PCB_CR3(%edi)
1.1       fvdl     1789: #ifdef MULTIPROCESSOR
                   1790:        movl    CPUVAR(GDT),%eax
                   1791: #else
                   1792:        movl    _C_LABEL(gdt),%eax
                   1793: #endif
                   1794:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                   1795:        ltr     %dx
                   1796:
                   1797:        /* We're always in the kernel, so we don't need the LDT. */
                   1798:
                   1799:        /* Restore cr0 (including FPU state). */
                   1800:        movl    PCB_CR0(%edi),%ecx
                   1801:        movl    %ecx,%cr0
                   1802:
                   1803:        /* Record new pcb. */
                   1804:        SET_CURPCB(%edi)
                   1805:
                   1806:        xorl    %esi,%esi
                   1807:        sti
                   1808: idle_unlock:
                   1809: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   1810:        call    _C_LABEL(sched_unlock_idle)
                   1811: #endif
                   1812:        /* Interrupts are okay again. */
1.2       fvdl     1813:        pushl   $IPL_NONE               # spl0()
1.1       fvdl     1814:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl     1815:        addl    $4,%esp
1.1       fvdl     1816:        jmp     idle_start
                   1817: idle_zero:
                   1818:        sti
                   1819:        call    _C_LABEL(uvm_pageidlezero)
                   1820:        cli
                   1821:        cmpl    $0,_C_LABEL(sched_whichqs)
                   1822:        jnz     idle_exit
                   1823: idle_loop:
                   1824:        /* Try to zero some pages. */
                   1825:        movl    _C_LABEL(uvm)+UVM_PAGE_IDLE_ZERO,%ecx
                   1826:        testl   %ecx,%ecx
                   1827:        jnz     idle_zero
                   1828:        sti
                   1829:        hlt
                   1830: NENTRY(mpidle)
                   1831: idle_start:
                   1832:        cli
                   1833:        cmpl    $0,_C_LABEL(sched_whichqs)
                   1834:        jz      idle_loop
                   1835: idle_exit:
                   1836:        movl    $IPL_HIGH,CPUVAR(ILEVEL)                # splhigh
1.14      fvdl     1837:        sti
1.1       fvdl     1838: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   1839:        call    _C_LABEL(sched_lock_idle)
                   1840: #endif
                   1841:        movl    _C_LABEL(sched_whichqs),%ecx
                   1842:        bsfl    %ecx,%ebx
                   1843:        jz      idle_unlock
                   1844:
                   1845: switch_dequeue:
                   1846:        /*
                   1847:         * we're running at splhigh(), but it's otherwise okay to take
                   1848:         * interrupts here.
                   1849:         */
                   1850:        sti
                   1851:        leal    _C_LABEL(sched_qs)(,%ebx,8),%eax # select q
                   1852:
1.5       thorpej  1853:        movl    L_FORW(%eax),%edi       # unlink from front of process q
1.1       fvdl     1854: #ifdef DIAGNOSTIC
                   1855:        cmpl    %edi,%eax               # linked to self (i.e. nothing queued)?
                   1856:        je      _C_LABEL(switch_error)  # not possible
                   1857: #endif /* DIAGNOSTIC */
1.5       thorpej  1858:        movl    L_FORW(%edi),%edx
                   1859:        movl    %edx,L_FORW(%eax)
                   1860:        movl    %eax,L_BACK(%edx)
1.1       fvdl     1861:
                   1862:        cmpl    %edx,%eax               # q empty?
                   1863:        jne     3f
                   1864:
                   1865:        btrl    %ebx,%ecx               # yes, clear to indicate empty
                   1866:        movl    %ecx,_C_LABEL(sched_whichqs) # update q status
                   1867:
                   1868: 3:     /* We just did it. */
                   1869:        xorl    %eax,%eax
                   1870:        CLEAR_RESCHED(%eax)
                   1871:
1.5       thorpej  1872: switch_resume:
1.1       fvdl     1873: #ifdef DIAGNOSTIC
1.5       thorpej  1874:        cmpl    %eax,L_WCHAN(%edi)      # Waiting for something?
1.1       fvdl     1875:        jne     _C_LABEL(switch_error)  # Yes; shouldn't be queued.
1.5       thorpej  1876:        cmpb    $LSRUN,L_STAT(%edi)     # In run state?
1.1       fvdl     1877:        jne     _C_LABEL(switch_error)  # No; shouldn't be queued.
                   1878: #endif /* DIAGNOSTIC */
                   1879:
1.5       thorpej  1880:        /* Isolate lwp.  XXX Is this necessary? */
                   1881:        movl    %eax,L_BACK(%edi)
1.1       fvdl     1882:
1.5       thorpej  1883:        /* Record new lwp. */
                   1884:        movb    $LSONPROC,L_STAT(%edi)  # l->l_stat = LSONPROC
                   1885:        SET_CURLWP(%edi,%ecx)
1.1       fvdl     1886:
1.5       thorpej  1887:        /* Skip context switch if same lwp. */
1.10      fvdl     1888:        xorl    %ebx,%ebx
1.1       fvdl     1889:        cmpl    %edi,%esi
                   1890:        je      switch_return
                   1891:
1.5       thorpej  1892:        /* If old lwp exited, don't bother. */
1.1       fvdl     1893:        testl   %esi,%esi
                   1894:        jz      switch_exited
                   1895:
                   1896:        /*
                   1897:         * Second phase: save old context.
                   1898:         *
                   1899:         * Registers:
                   1900:         *   %eax, %ecx - scratch
1.5       thorpej  1901:         *   %esi - old lwp, then old pcb
                   1902:         *   %edi - new lwp
1.1       fvdl     1903:         */
                   1904:
                   1905:        pushl   %esi
1.24      yamt     1906:        call    _C_LABEL(pmap_deactivate2)      # pmap_deactivate(oldproc)
1.1       fvdl     1907:        addl    $4,%esp
                   1908:
1.5       thorpej  1909:        movl    L_ADDR(%esi),%esi
1.1       fvdl     1910:
                   1911:        /* Save stack pointers. */
                   1912:        movl    %esp,PCB_ESP(%esi)
                   1913:        movl    %ebp,PCB_EBP(%esi)
                   1914:
                   1915: switch_exited:
                   1916:        /*
                   1917:         * Third phase: restore saved context.
                   1918:         *
                   1919:         * Registers:
                   1920:         *   %eax, %ebx, %ecx, %edx - scratch
                   1921:         *   %esi - new pcb
1.5       thorpej  1922:         *   %edi - new lwp
1.1       fvdl     1923:         */
                   1924:
                   1925:        /* No interrupts while loading new state. */
                   1926:        cli
1.5       thorpej  1927:        movl    L_ADDR(%edi),%esi
1.1       fvdl     1928:
                   1929:        /* Restore stack pointers. */
                   1930:        movl    PCB_ESP(%esi),%esp
                   1931:        movl    PCB_EBP(%esi),%ebp
                   1932:
                   1933: #if 0
                   1934:        /* Don't bother with the rest if switching to a system process. */
1.5       thorpej  1935:        testl   $P_SYSTEM,L_FLAG(%edi); XXX NJWLWP lwp's don't have P_SYSTEM!
1.1       fvdl     1936:        jnz     switch_restored
                   1937: #endif
                   1938:
1.25.2.1  tron     1939:        /* Switch TSS. Reset "task busy" flag before loading. */
                   1940:        movl    %cr3,%eax
                   1941:        movl    %eax,PCB_CR3(%esi) /* XXX should be done by pmap_activate? */
1.1       fvdl     1942: #ifdef MULTIPROCESSOR
                   1943:        movl    CPUVAR(GDT),%eax
                   1944: #else
                   1945:        /* Load TSS info. */
                   1946:        movl    _C_LABEL(gdt),%eax
                   1947: #endif
1.5       thorpej  1948:        movl    L_MD_TSS_SEL(%edi),%edx
1.1       fvdl     1949:
                   1950:        andl    $~0x0200,4(%eax,%edx, 1)
                   1951:        ltr     %dx
                   1952:
                   1953:        pushl   %edi
                   1954:        call    _C_LABEL(pmap_activate)         # pmap_activate(p)
                   1955:        addl    $4,%esp
                   1956:
                   1957: #if 0
                   1958: switch_restored:
                   1959: #endif
                   1960:        /* Restore cr0 (including FPU state). */
                   1961:        movl    PCB_CR0(%esi),%ecx
                   1962: #ifdef MULTIPROCESSOR
                   1963:        /*
1.22      wiz      1964:         * If our floating point registers are on a different CPU,
1.1       fvdl     1965:         * clear CR0_TS so we'll trap rather than reuse bogus state.
                   1966:         */
                   1967:        movl    PCB_FPCPU(%esi),%ebx
                   1968:        cmpl    CPUVAR(SELF),%ebx
                   1969:        jz      1f
                   1970:        orl     $CR0_TS,%ecx
                   1971: 1:
                   1972: #endif
                   1973:        movl    %ecx,%cr0
                   1974:
                   1975:        /* Record new pcb. */
                   1976:        SET_CURPCB(%esi)
                   1977:
                   1978:        /* Interrupts are okay again. */
                   1979:        sti
                   1980:
                   1981: /*
                   1982:  *  Check for restartable atomic sequences (RAS)
                   1983:  */
1.5       thorpej  1984:        movl    CPUVAR(CURLWP),%edi
                   1985:        movl    L_PROC(%edi),%esi
1.20      dsl      1986:        cmpl    $0,P_RASLIST(%esi)
                   1987:        jne     2f
1.1       fvdl     1988: 1:
1.10      fvdl     1989:        movl    $1,%ebx
1.1       fvdl     1990:
                   1991: switch_return:
                   1992: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
                   1993:        call    _C_LABEL(sched_unlock_idle)
                   1994: #endif
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.1       fvdl     1998:        movl    $IPL_HIGH,CPUVAR(ILEVEL)        # splhigh()
1.9       fvdl     1999:
                   2000:        movl    %ebx,%eax
1.1       fvdl     2001:
                   2002:        popl    %edi
                   2003:        popl    %esi
                   2004:        popl    %ebx
                   2005:        ret
1.20      dsl      2006:
                   2007: 2:                                     # check RAS list
                   2008:        movl    L_MD_REGS(%edi),%ebx
                   2009:        movl    TF_EIP(%ebx),%eax
                   2010:        pushl   %eax
                   2011:        pushl   %esi
                   2012:        call    _C_LABEL(ras_lookup)
                   2013:        addl    $8,%esp
                   2014:        cmpl    $-1,%eax
                   2015:        je      1b
                   2016:        movl    %eax,TF_EIP(%ebx)
                   2017:        jmp     1b
1.1       fvdl     2018:
                   2019: /*
1.5       thorpej  2020:  * void cpu_switchto(struct lwp *current, struct lwp *next)
                   2021:  * Switch to the specified next LWP.
                   2022:  */
                   2023: ENTRY(cpu_switchto)
                   2024:        pushl   %ebx
                   2025:        pushl   %esi
                   2026:        pushl   %edi
                   2027:
                   2028: #ifdef DEBUG
                   2029:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                   2030:        jae     1f
                   2031:        pushl   $2f
                   2032:        call    _C_LABEL(panic)
                   2033:        /* NOTREACHED */
                   2034: 2:     .asciz  "not splsched() in cpu_switchto!"
                   2035: 1:
                   2036: #endif /* DEBUG */
                   2037:
                   2038:        movl    16(%esp),%esi           # current
                   2039:        movl    20(%esp),%edi           # next
                   2040:
                   2041:        /*
                   2042:         * Clear curlwp so that we don't accumulate system time while idle.
                   2043:         * This also insures that schedcpu() will move the old process to
                   2044:         * the correct queue if it happens to get called from the spllower()
                   2045:         * below and changes the priority.  (See corresponding comment in
                   2046:         * usrret()).
                   2047:         *
                   2048:         * XXX Is this necessary?  We know we won't go idle.
                   2049:         */
                   2050:        movl    $0,CPUVAR(CURLWP)
                   2051:
                   2052:        /*
                   2053:         * We're running at splhigh(), but it's otherwise okay to take
                   2054:         * interrupts here.
                   2055:         */
                   2056:        sti
                   2057:
                   2058:        /* Jump into the middle of cpu_switch */
                   2059:        xorl    %eax,%eax
                   2060:        jmp     switch_resume
                   2061:
                   2062: /*
1.21      jdolecek 2063:  * void cpu_exit(struct lwp *l)
1.22      wiz      2064:  * Switch to the appropriate idle context (lwp0's if uniprocessor; the CPU's
1.1       fvdl     2065:  * if multiprocessor) and deallocate the address space and kernel stack for p.
                   2066:  * Then jump into cpu_switch(), as if we were in the idle proc all along.
                   2067:  */
                   2068: #ifndef MULTIPROCESSOR
1.5       thorpej  2069:        .globl  _C_LABEL(lwp0)
1.1       fvdl     2070: #endif
                   2071:        .globl  _C_LABEL(uvmspace_free),_C_LABEL(kernel_map)
                   2072:        .globl  _C_LABEL(uvm_km_free),_C_LABEL(tss_free)
1.21      jdolecek 2073: /* LINTSTUB: Func: void cpu_exit(struct lwp *l) */
                   2074: ENTRY(cpu_exit)
1.1       fvdl     2075:        movl    4(%esp),%edi            # old process
                   2076: #ifndef MULTIPROCESSOR
1.5       thorpej  2077:        movl    $_C_LABEL(lwp0),%ebx
                   2078:        movl    L_ADDR(%ebx),%esi
                   2079:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl     2080: #else
                   2081:        movl    CPUVAR(IDLE_PCB),%esi
                   2082:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                   2083: #endif
                   2084:        /* In case we fault... */
1.5       thorpej  2085:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl     2086:
                   2087:        /* Restore the idle context. */
                   2088:        cli
                   2089:
                   2090:        /* Restore stack pointers. */
                   2091:        movl    PCB_ESP(%esi),%esp
                   2092:        movl    PCB_EBP(%esi),%ebp
                   2093:
1.25.2.1  tron     2094:        /* Switch TSS. Reset "task busy" flag before loading. */
                   2095:        movl    %cr3,%eax
                   2096:        movl    %eax,PCB_CR3(%esi)
1.1       fvdl     2097: #ifdef MULTIPROCESSOR
                   2098:        movl    CPUVAR(GDT),%eax
                   2099: #else
                   2100:        /* Load TSS info. */
                   2101:        movl    _C_LABEL(gdt),%eax
                   2102: #endif
                   2103:
                   2104:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                   2105:        ltr     %dx
                   2106:
                   2107:        /* We're always in the kernel, so we don't need the LDT. */
                   2108:
                   2109:        /* Restore cr0 (including FPU state). */
                   2110:        movl    PCB_CR0(%esi),%ecx
                   2111:        movl    %ecx,%cr0
                   2112:
                   2113:        /* Record new pcb. */
                   2114:        SET_CURPCB(%esi)
                   2115:
                   2116:        /* Interrupts are okay again. */
                   2117:        sti
                   2118:
                   2119:        /*
1.21      jdolecek 2120:         * Schedule the dead LWP's stack to be freed.
1.1       fvdl     2121:         */
1.21      jdolecek 2122:        pushl   %edi
                   2123:        call    _C_LABEL(lwp_exit2)
1.1       fvdl     2124:        addl    $4,%esp
                   2125:
                   2126:        /* Jump into cpu_switch() with the right state. */
                   2127:        xorl    %esi,%esi
1.5       thorpej  2128:        movl    %esi,CPUVAR(CURLWP)
1.1       fvdl     2129:        jmp     idle_start
                   2130:
                   2131: /*
                   2132:  * void savectx(struct pcb *pcb);
                   2133:  * Update pcb, saving current processor state.
                   2134:  */
                   2135: /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
                   2136: ENTRY(savectx)
                   2137:        movl    4(%esp),%edx            # edx = p->p_addr
                   2138:
                   2139:        /* Save stack pointers. */
                   2140:        movl    %esp,PCB_ESP(%edx)
                   2141:        movl    %ebp,PCB_EBP(%edx)
                   2142:
                   2143:        ret
                   2144:
                   2145: /*
                   2146:  * Old call gate entry for syscall
                   2147:  */
                   2148: /* LINTSTUB: Var: char Xosyscall[1]; */
                   2149: IDTVEC(osyscall)
                   2150:        /* Set eflags in trap frame. */
                   2151:        pushfl
                   2152:        popl    8(%esp)
                   2153:        pushl   $7              # size of instruction for restart
                   2154:        jmp     syscall1
                   2155:
                   2156: /*
                   2157:  * Trap gate entry for syscall
                   2158:  */
                   2159: /* LINTSTUB: Var: char Xsyscall[1]; */
                   2160: IDTVEC(syscall)
                   2161:        pushl   $2              # size of instruction for restart
                   2162: syscall1:
                   2163:        pushl   $T_ASTFLT       # trap # for doing ASTs
                   2164:        INTRENTRY
                   2165:
                   2166: #ifdef DIAGNOSTIC
1.24      yamt     2167:        cmpl    $0, CPUVAR(WANT_PMAPLOAD)
                   2168:        jz      1f
                   2169:        pushl   $6f
                   2170:        call    _C_LABEL(printf)
                   2171:        addl    $4, %esp
                   2172: 1:
1.1       fvdl     2173:        movl    CPUVAR(ILEVEL),%ebx
                   2174:        testl   %ebx,%ebx
                   2175:        jz      1f
                   2176:        pushl   $5f
                   2177:        call    _C_LABEL(printf)
                   2178:        addl    $4,%esp
                   2179: #ifdef DDB
                   2180:        int     $3
                   2181: #endif
                   2182: 1:
                   2183: #endif /* DIAGNOSTIC */
1.5       thorpej  2184:        movl    CPUVAR(CURLWP),%edx
                   2185:        movl    %esp,L_MD_REGS(%edx)    # save pointer to frame
                   2186:        movl    L_PROC(%edx),%edx
1.15      fvdl     2187:        pushl   %esp
1.1       fvdl     2188:        call    *P_MD_SYSCALL(%edx)     # get pointer to syscall() function
1.15      fvdl     2189:        addl    $4,%esp
1.24      yamt     2190: syscall_checkast:
                   2191:        /* Check for ASTs on exit to user mode. */
1.1       fvdl     2192:        cli
1.5       thorpej  2193:        CHECK_ASTPENDING(%eax)
1.1       fvdl     2194:        je      1f
                   2195:        /* Always returning to user mode here. */
1.5       thorpej  2196:        CLEAR_ASTPENDING(%eax)
1.1       fvdl     2197:        sti
                   2198:        /* Pushed T_ASTFLT into tf_trapno on entry. */
1.15      fvdl     2199:        pushl   %esp
1.1       fvdl     2200:        call    _C_LABEL(trap)
1.15      fvdl     2201:        addl    $4,%esp
1.24      yamt     2202:        jmp     syscall_checkast        /* re-check ASTs */
                   2203: 1:     CHECK_DEFERRED_SWITCH(%eax)
                   2204:        jnz     9f
1.1       fvdl     2205: #ifndef DIAGNOSTIC
1.24      yamt     2206:        INTRFASTEXIT
1.1       fvdl     2207: #else /* DIAGNOSTIC */
1.24      yamt     2208:        cmpl    $IPL_NONE,CPUVAR(ILEVEL)
1.1       fvdl     2209:        jne     3f
                   2210:        INTRFASTEXIT
                   2211: 3:     sti
                   2212:        pushl   $4f
                   2213:        call    _C_LABEL(printf)
                   2214:        addl    $4,%esp
                   2215: #ifdef DDB
                   2216:        int     $3
                   2217: #endif /* DDB */
                   2218:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                   2219:        jmp     2b
                   2220: 4:     .asciz  "WARNING: SPL NOT LOWERED ON SYSCALL EXIT\n"
                   2221: 5:     .asciz  "WARNING: SPL NOT ZERO ON SYSCALL ENTRY\n"
1.24      yamt     2222: 6:     .asciz  "WARNING: WANT PMAPLOAD ON SYSCALL ENTRY\n"
1.1       fvdl     2223: #endif /* DIAGNOSTIC */
1.24      yamt     2224: 9:     sti
                   2225:        call    _C_LABEL(pmap_load)
                   2226:        jmp     syscall_checkast        /* re-check ASTs */
1.1       fvdl     2227:
                   2228: #if NNPX > 0
                   2229: /*
                   2230:  * Special interrupt handlers.  Someday intr0-intr15 will be used to count
                   2231:  * interrupts.  We'll still need a special exception 16 handler.  The busy
                   2232:  * latch stuff in probintr() can be moved to npxprobe().
                   2233:  */
                   2234:
                   2235: /* LINTSTUB: Func: void probeintr(void) */
                   2236: NENTRY(probeintr)
                   2237:        ss
                   2238:        incl    _C_LABEL(npx_intrs_while_probing)
                   2239:        pushl   %eax
                   2240:        movb    $0x20,%al       # EOI (asm in strings loses cpp features)
                   2241:        outb    %al,$0xa0       # IO_ICU2
                   2242:        outb    %al,$0x20       # IO_ICU1
                   2243:        movb    $0,%al
                   2244:        outb    %al,$0xf0       # clear BUSY# latch
                   2245:        popl    %eax
                   2246:        iret
                   2247:
                   2248: /* LINTSTUB: Func: void probetrap(void) */
                   2249: NENTRY(probetrap)
                   2250:        ss
                   2251:        incl    _C_LABEL(npx_traps_while_probing)
                   2252:        fnclex
                   2253:        iret
                   2254:
                   2255: /* LINTSTUB: Func: int npx586bug1(int a, int b) */
                   2256: NENTRY(npx586bug1)
                   2257:        fildl   4(%esp)         # x
                   2258:        fildl   8(%esp)         # y
                   2259:        fld     %st(1)
                   2260:        fdiv    %st(1),%st      # x/y
                   2261:        fmulp   %st,%st(1)      # (x/y)*y
                   2262:        fsubrp  %st,%st(1)      # x-(x/y)*y
                   2263:        pushl   $0
                   2264:        fistpl  (%esp)
                   2265:        popl    %eax
                   2266:        ret
                   2267: #endif /* NNPX > 0 */

CVSweb <webmaster@jp.NetBSD.org>