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

1.91    ! rmind       1: /*     $NetBSD: locore.S,v 1.90 2009/11/25 14:28:50 rmind Exp $        */
1.53      yamt        2:
                      3: /*
                      4:  * Copyright-o-rama!
                      5:  */
                      6:
                      7: /*
1.62      bouyer      8:  * Copyright (c) 2006 Manuel Bouyer.
                      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:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     20:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     21:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     22:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     23:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     24:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     28:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     29:  *
                     30:  */
                     31:
                     32: /*
1.53      yamt       33:  * Copyright (c) 2001 Wasabi Systems, Inc.
                     34:  * All rights reserved.
                     35:  *
                     36:  * Written by Frank van der Linden for Wasabi Systems, Inc.
                     37:  *
                     38:  * Redistribution and use in source and binary forms, with or without
                     39:  * modification, are permitted provided that the following conditions
                     40:  * are met:
                     41:  * 1. Redistributions of source code must retain the above copyright
                     42:  *    notice, this list of conditions and the following disclaimer.
                     43:  * 2. Redistributions in binary form must reproduce the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer in the
                     45:  *    documentation and/or other materials provided with the distribution.
                     46:  * 3. All advertising materials mentioning features or use of this software
                     47:  *    must display the following acknowledgement:
                     48:  *      This product includes software developed for the NetBSD Project by
                     49:  *      Wasabi Systems, Inc.
                     50:  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
                     51:  *    or promote products derived from this software without specific prior
                     52:  *    written permission.
                     53:  *
                     54:  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
                     55:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     56:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     57:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
                     58:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     59:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     60:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     61:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     62:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     63:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     64:  * POSSIBILITY OF SUCH DAMAGE.
                     65:  */
                     66:
1.1       fvdl       67:
                     68: /*-
1.84      ad         69:  * Copyright (c) 1998, 2000, 2004, 2006, 2007, 2009 The NetBSD Foundation, Inc.
1.1       fvdl       70:  * All rights reserved.
                     71:  *
                     72:  * This code is derived from software contributed to The NetBSD Foundation
1.84      ad         73:  * by Charles M. Hannum, and by Andrew Doran.
1.1       fvdl       74:  *
                     75:  * Redistribution and use in source and binary forms, with or without
                     76:  * modification, are permitted provided that the following conditions
                     77:  * are met:
                     78:  * 1. Redistributions of source code must retain the above copyright
                     79:  *    notice, this list of conditions and the following disclaimer.
                     80:  * 2. Redistributions in binary form must reproduce the above copyright
                     81:  *    notice, this list of conditions and the following disclaimer in the
                     82:  *    documentation and/or other materials provided with the distribution.
                     83:  *
                     84:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     85:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     86:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     87:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     88:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     89:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     90:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     91:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     92:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     93:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     94:  * POSSIBILITY OF SUCH DAMAGE.
                     95:  */
                     96:
                     97: /*-
                     98:  * Copyright (c) 1990 The Regents of the University of California.
                     99:  * All rights reserved.
                    100:  *
                    101:  * This code is derived from software contributed to Berkeley by
                    102:  * William Jolitz.
                    103:  *
                    104:  * Redistribution and use in source and binary forms, with or without
                    105:  * modification, are permitted provided that the following conditions
                    106:  * are met:
                    107:  * 1. Redistributions of source code must retain the above copyright
                    108:  *    notice, this list of conditions and the following disclaimer.
                    109:  * 2. Redistributions in binary form must reproduce the above copyright
                    110:  *    notice, this list of conditions and the following disclaimer in the
                    111:  *    documentation and/or other materials provided with the distribution.
1.12      agc       112:  * 3. Neither the name of the University nor the names of its contributors
1.1       fvdl      113:  *    may be used to endorse or promote products derived from this software
                    114:  *    without specific prior written permission.
                    115:  *
                    116:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                    117:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                    118:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                    119:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                    120:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                    121:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                    122:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                    123:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                    124:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                    125:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                    126:  * SUCH DAMAGE.
                    127:  *
                    128:  *     @(#)locore.s    7.3 (Berkeley) 5/13/91
                    129:  */
                    130:
1.59      lukem     131: #include <machine/asm.h>
1.91    ! rmind     132: __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.90 2009/11/25 14:28:50 rmind Exp $");
1.59      lukem     133:
1.18      christos  134: #include "opt_compat_oldboot.h"
1.1       fvdl      135: #include "opt_ddb.h"
1.83      apb       136: #include "opt_modular.h"
1.78      joerg     137: #include "opt_multiboot.h"
1.1       fvdl      138: #include "opt_realmem.h"
1.18      christos  139: #include "opt_vm86.h"
1.62      bouyer    140: #include "opt_xen.h"
1.1       fvdl      141:
                    142: #include "npx.h"
                    143: #include "assym.h"
                    144: #include "lapic.h"
                    145: #include "ioapic.h"
1.8       fvdl      146: #include "ksyms.h"
1.1       fvdl      147:
                    148: #include <sys/errno.h>
                    149: #include <sys/syscall.h>
                    150:
                    151: #include <machine/cputypes.h>
                    152: #include <machine/segments.h>
                    153: #include <machine/specialreg.h>
                    154: #include <machine/trap.h>
                    155: #include <machine/i82489reg.h>
1.48      yamt      156: #include <machine/frameasm.h>
                    157: #include <machine/i82489reg.h>
1.62      bouyer    158: #ifndef XEN
                    159: #include <machine/multiboot.h>
                    160: #endif
1.1       fvdl      161:
                    162: /* XXX temporary kluge; these should not be here */
                    163: /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
                    164: #include <dev/isa/isareg.h>
                    165:
1.62      bouyer    166: #ifdef XEN
                    167: /*
                    168:  * Xen guest identifier and loader selection
                    169:  */
                    170: .section __xen_guest
                    171:        .ascii  "GUEST_OS=netbsd,GUEST_VER=3.0,XEN_VER=xen-3.0"
                    172: #if defined(DOM0OPS) || !defined(XEN_COMPAT_030001)
                    173:        .ascii  ",VIRT_BASE=0xc0000000" /* KERNBASE */
                    174:        .ascii  ",ELF_PADDR_OFFSET=0xc0000000" /* KERNBASE */
                    175: #else
                    176:        .ascii  ",VIRT_BASE=0xc0100000" /* KERNTEXTOFF */
                    177:        .ascii  ",ELF_PADDR_OFFSET=0xc0100000" /* KERNTEXTOFF */
                    178: #endif
                    179:        .ascii  ",VIRT_ENTRY=0xc0100000" /* KERNTEXTOFF */
                    180: #if !defined(XEN_COMPAT_030001)
                    181:        .ascii  ",HYPERCALL_PAGE=0x00000101"
                    182:                /* (???+HYPERCALL_PAGE_OFFSET)/PAGE_SIZE) */
                    183: #endif
1.66      bouyer    184: #ifdef PAE
                    185:        .ascii  ",PAE=yes[extended-cr3]"
                    186: #endif
1.62      bouyer    187:        .ascii  ",LOADER=generic"
1.80      ad        188: #if (NKSYMS || defined(DDB) || defined(MODULAR)) && !defined(SYMTAB_SPACE)
1.62      bouyer    189:        .ascii  ",BSD_SYMTAB=yes"
                    190: #endif
                    191:        .byte   0
                    192: #endif
                    193:
1.1       fvdl      194: /*
                    195:  * Initialization
                    196:  */
                    197:        .data
                    198:
                    199:        .globl  _C_LABEL(cpu)
1.73      ad        200:        .globl  _C_LABEL(cpuid_level)
1.38      jmmv      201:        .globl  _C_LABEL(esym)
1.71      ad        202:        .globl  _C_LABEL(eblob)
1.38      jmmv      203:        .globl  _C_LABEL(atdevbase)
1.91    ! rmind     204:        .globl  _C_LABEL(lwp0uarea),_C_LABEL(PDPpaddr)
1.1       fvdl      205:        .globl  _C_LABEL(gdt)
                    206:        .globl  _C_LABEL(idt)
1.30      junyoung  207:        .globl  _C_LABEL(lapic_tpr)
                    208:
1.1       fvdl      209: #if NLAPIC > 0
                    210: #ifdef __ELF__
1.7       thorpej   211:        .align  PAGE_SIZE
1.1       fvdl      212: #else
                    213:        .align  12
                    214: #endif
                    215:        .globl _C_LABEL(local_apic), _C_LABEL(lapic_id)
                    216: _C_LABEL(local_apic):
                    217:        .space  LAPIC_ID
1.30      junyoung  218: _C_LABEL(lapic_id):
1.1       fvdl      219:        .long   0x00000000
                    220:        .space  LAPIC_TPRI-(LAPIC_ID+4)
1.30      junyoung  221: _C_LABEL(lapic_tpr):
1.1       fvdl      222:        .space  LAPIC_PPRI-LAPIC_TPRI
1.30      junyoung  223: _C_LABEL(lapic_ppr):
1.1       fvdl      224:        .space  LAPIC_ISR-LAPIC_PPRI
                    225: _C_LABEL(lapic_isr):
1.7       thorpej   226:        .space  PAGE_SIZE-LAPIC_ISR
1.1       fvdl      227: #else
1.30      junyoung  228: _C_LABEL(lapic_tpr):
1.1       fvdl      229:        .long 0
                    230: #endif
1.30      junyoung  231:
1.48      yamt      232: _C_LABEL(cpu):         .long   0       # are we 80486, Pentium, or..
1.73      ad        233: _C_LABEL(cpuid_level): .long   0
1.1       fvdl      234: _C_LABEL(atdevbase):   .long   0       # location of start of iomem in virtual
1.91    ! rmind     235: _C_LABEL(lwp0uarea):   .long   0
1.31      junyoung  236: _C_LABEL(PDPpaddr):    .long   0       # paddr of PDP, for libkvm
1.53      yamt      237: _C_LABEL(tablesize):   .long   0
1.30      junyoung  238:
1.63      bouyer    239:        .space 512
1.1       fvdl      240: tmpstk:
                    241:
1.62      bouyer    242: #ifndef XEN
1.35      yamt      243: #define        _RELOC(x)       ((x) - KERNBASE)
1.62      bouyer    244: #else
                    245: #define        _RELOC(x)       ((x))
                    246: #endif /* XEN */
1.1       fvdl      247: #define        RELOC(x)        _RELOC(_C_LABEL(x))
                    248:
                    249:        .text
                    250:        .globl  _C_LABEL(kernel_text)
                    251:        .set    _C_LABEL(kernel_text),KERNTEXTOFF
                    252:
                    253:        .globl  start
1.62      bouyer    254: #ifndef XEN
1.1       fvdl      255: start: movw    $0x1234,0x472                   # warm boot
                    256:
1.37      jmmv      257: #if defined(MULTIBOOT)
                    258:        jmp     1f
                    259:
                    260:        .align  4
                    261:        .globl  Multiboot_Header
                    262: _C_LABEL(Multiboot_Header):
1.44      jmmv      263: #define MULTIBOOT_HEADER_FLAGS (MULTIBOOT_HEADER_WANT_MEMORY)
1.37      jmmv      264:        .long   MULTIBOOT_HEADER_MAGIC
                    265:        .long   MULTIBOOT_HEADER_FLAGS
                    266:        .long   -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
                    267:
                    268: 1:
                    269:        /* Check if we are being executed by a Multiboot-compliant boot
                    270:         * loader. */
                    271:        cmpl    $MULTIBOOT_INFO_MAGIC,%eax
                    272:        jne     1f
                    273:
1.43      mrg       274:        /*
                    275:         * Indeed, a multiboot-compliant boot loader executed us.  We copy
1.37      jmmv      276:         * the received Multiboot information structure into kernel's data
                    277:         * space to process it later -- after we are relocated.  It will
1.43      mrg       278:         * be safer to run complex C code than doing it at this point.
                    279:         */
1.37      jmmv      280:        pushl   %ebx            # Address of Multiboot information
                    281:        call    _C_LABEL(multiboot_pre_reloc)
                    282:        addl    $4,%esp
1.38      jmmv      283:        jmp     2f
1.37      jmmv      284: #endif
                    285:
                    286: 1:
1.1       fvdl      287:        /*
1.38      jmmv      288:         * At this point, we know that a NetBSD-specific boot loader
                    289:         * booted this kernel.  The stack carries the following parameters:
1.41      jmmv      290:         * (boothowto, [bootdev], bootinfo, esym, biosextmem, biosbasemem),
1.38      jmmv      291:         * 4 bytes each.
1.1       fvdl      292:         */
1.38      jmmv      293:        addl    $4,%esp         # Discard return address to boot loader
                    294:        call    _C_LABEL(native_loader)
                    295:        addl    $24,%esp
1.1       fvdl      296:
                    297: 2:
                    298:        /* First, reset the PSL. */
                    299:        pushl   $PSL_MBO
                    300:        popfl
                    301:
                    302:        /* Clear segment registers; always null in proc0. */
                    303:        xorl    %eax,%eax
                    304:        movw    %ax,%fs
                    305:        movw    %ax,%gs
                    306:        decl    %eax
1.73      ad        307:        movl    %eax,RELOC(cpuid_level)
1.1       fvdl      308:
                    309:        /* Find out our CPU type. */
                    310:
                    311: try386:        /* Try to toggle alignment check flag; does not exist on 386. */
                    312:        pushfl
                    313:        popl    %eax
                    314:        movl    %eax,%ecx
                    315:        orl     $PSL_AC,%eax
                    316:        pushl   %eax
                    317:        popfl
                    318:        pushfl
                    319:        popl    %eax
                    320:        xorl    %ecx,%eax
                    321:        andl    $PSL_AC,%eax
                    322:        pushl   %ecx
                    323:        popfl
                    324:
                    325:        testl   %eax,%eax
                    326:        jnz     try486
                    327:
                    328:        /*
                    329:         * Try the test of a NexGen CPU -- ZF will not change on a DIV
                    330:         * instruction on a NexGen, it will on an i386.  Documented in
                    331:         * Nx586 Processor Recognition Application Note, NexGen, Inc.
                    332:         */
                    333:        movl    $0x5555,%eax
                    334:        xorl    %edx,%edx
                    335:        movl    $2,%ecx
                    336:        divl    %ecx
                    337:        jnz     is386
                    338:
                    339: isnx586:
                    340:        /*
                    341:         * Don't try cpuid, as Nx586s reportedly don't support the
                    342:         * PSL_ID bit.
                    343:         */
                    344:        movl    $CPU_NX586,RELOC(cpu)
                    345:        jmp     2f
                    346:
                    347: is386:
                    348:        movl    $CPU_386,RELOC(cpu)
                    349:        jmp     2f
                    350:
                    351: try486:        /* Try to toggle identification flag; does not exist on early 486s. */
                    352:        pushfl
                    353:        popl    %eax
                    354:        movl    %eax,%ecx
                    355:        xorl    $PSL_ID,%eax
                    356:        pushl   %eax
                    357:        popfl
                    358:        pushfl
                    359:        popl    %eax
                    360:        xorl    %ecx,%eax
                    361:        andl    $PSL_ID,%eax
                    362:        pushl   %ecx
                    363:        popfl
                    364:
                    365:        testl   %eax,%eax
                    366:        jnz     try586
                    367: is486: movl    $CPU_486,RELOC(cpu)
                    368:        /*
                    369:         * Check Cyrix CPU
                    370:         * Cyrix CPUs do not change the undefined flags following
                    371:         * execution of the divide instruction which divides 5 by 2.
                    372:         *
                    373:         * Note: CPUID is enabled on M2, so it passes another way.
                    374:         */
                    375:        pushfl
                    376:        movl    $0x5555, %eax
                    377:        xorl    %edx, %edx
                    378:        movl    $2, %ecx
                    379:        clc
                    380:        divl    %ecx
                    381:        jnc     trycyrix486
                    382:        popfl
                    383:        jmp 2f
                    384: trycyrix486:
                    385:        movl    $CPU_6x86,RELOC(cpu)    # set CPU type
                    386:        /*
                    387:         * Check for Cyrix 486 CPU by seeing if the flags change during a
                    388:         * divide. This is documented in the Cx486SLC/e SMM Programmer's
                    389:         * Guide.
                    390:         */
                    391:        xorl    %edx,%edx
                    392:        cmpl    %edx,%edx               # set flags to known state
                    393:        pushfl
                    394:        popl    %ecx                    # store flags in ecx
                    395:        movl    $-1,%eax
                    396:        movl    $4,%ebx
                    397:        divl    %ebx                    # do a long division
                    398:        pushfl
                    399:        popl    %eax
                    400:        xorl    %ecx,%eax               # are the flags different?
                    401:        testl   $0x8d5,%eax             # only check C|PF|AF|Z|N|V
                    402:        jne     2f                      # yes; must be Cyrix 6x86 CPU
                    403:        movl    $CPU_486DLC,RELOC(cpu)  # set CPU type
                    404:
                    405: #ifndef CYRIX_CACHE_WORKS
                    406:        /* Disable caching of the ISA hole only. */
                    407:        invd
                    408:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    409:        outb    %al,$0x22
                    410:        inb     $0x23,%al
                    411:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    412:        movb    %al,%ah
                    413:        movb    $CCR0,%al
                    414:        outb    %al,$0x22
                    415:        movb    %ah,%al
                    416:        outb    %al,$0x23
                    417:        invd
                    418: #else /* CYRIX_CACHE_WORKS */
                    419:        /* Set cache parameters */
                    420:        invd                            # Start with guaranteed clean cache
                    421:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    422:        outb    %al,$0x22
                    423:        inb     $0x23,%al
                    424:        andb    $~CCR0_NC0,%al
                    425: #ifndef CYRIX_CACHE_REALLY_WORKS
                    426:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    427: #else
                    428:        orb     $CCR0_NC1,%al
                    429: #endif
                    430:        movb    %al,%ah
                    431:        movb    $CCR0,%al
                    432:        outb    %al,$0x22
                    433:        movb    %ah,%al
                    434:        outb    %al,$0x23
                    435:        /* clear non-cacheable region 1 */
                    436:        movb    $(NCR1+2),%al
                    437:        outb    %al,$0x22
                    438:        movb    $NCR_SIZE_0K,%al
                    439:        outb    %al,$0x23
                    440:        /* clear non-cacheable region 2 */
                    441:        movb    $(NCR2+2),%al
                    442:        outb    %al,$0x22
                    443:        movb    $NCR_SIZE_0K,%al
                    444:        outb    %al,$0x23
                    445:        /* clear non-cacheable region 3 */
                    446:        movb    $(NCR3+2),%al
                    447:        outb    %al,$0x22
                    448:        movb    $NCR_SIZE_0K,%al
                    449:        outb    %al,$0x23
                    450:        /* clear non-cacheable region 4 */
                    451:        movb    $(NCR4+2),%al
                    452:        outb    %al,$0x22
                    453:        movb    $NCR_SIZE_0K,%al
                    454:        outb    %al,$0x23
                    455:        /* enable caching in CR0 */
                    456:        movl    %cr0,%eax
                    457:        andl    $~(CR0_CD|CR0_NW),%eax
                    458:        movl    %eax,%cr0
                    459:        invd
                    460: #endif /* CYRIX_CACHE_WORKS */
                    461:
                    462:        jmp     2f
                    463:
                    464: try586:        /* Use the `cpuid' instruction. */
                    465:        xorl    %eax,%eax
                    466:        cpuid
1.73      ad        467:        movl    %eax,RELOC(cpuid_level)
1.1       fvdl      468:
                    469: 2:
                    470:        /*
                    471:         * Finished with old stack; load new %esp now instead of later so we
                    472:         * can trace this code without having to worry about the trace trap
                    473:         * clobbering the memory test or the zeroing of the bss+bootstrap page
                    474:         * tables.
                    475:         *
                    476:         * The boot program should check:
                    477:         *      text+data <= &stack_variable - more_space_for_stack
                    478:         *      text+data+bss+pad+space_for_page_tables <= end_of_memory
                    479:         * Oops, the gdt is in the carcass of the boot program so clearing
                    480:         * the rest of memory is still not possible.
                    481:         */
                    482:        movl    $_RELOC(tmpstk),%esp    # bootstrap stack end location
                    483:
                    484: /*
                    485:  * Virtual address space of kernel:
                    486:  *
1.71      ad        487:  * text | data | bss | [syms] | [blobs] | page dir | proc0 kstack | L1 ptp
                    488:  *                                     0          1       2      3
1.1       fvdl      489:  */
1.53      yamt      490:
                    491: #define        PROC0_PDIR_OFF  0
                    492: #define        PROC0_STK_OFF   (PROC0_PDIR_OFF + PAGE_SIZE)
                    493: #define        PROC0_PTP1_OFF  (PROC0_STK_OFF + UPAGES * PAGE_SIZE)
                    494:
                    495: /*
                    496:  * fillkpt
                    497:  *     eax = pte (page frame | control | status)
                    498:  *     ebx = page table address
                    499:  *     ecx = number of pages to map
                    500:  */
                    501:
                    502: #define fillkpt        \
                    503: 1:     movl    %eax,(%ebx)     ;       /* store phys addr */ \
                    504:        addl    $4,%ebx         ;       /* next pte/pde */ \
                    505:        addl    $PAGE_SIZE,%eax ;       /* next phys page */ \
                    506:        loop    1b              ;  \
                    507:
1.1       fvdl      508:
                    509:        /* Find end of kernel image. */
                    510:        movl    $RELOC(end),%edi
1.71      ad        511:
1.80      ad        512: #if (NKSYMS || defined(DDB) || defined(MODULAR)) && !defined(SYMTAB_SPACE)
1.1       fvdl      513:        /* Save the symbols (if loaded). */
                    514:        movl    RELOC(esym),%eax
                    515:        testl   %eax,%eax
                    516:        jz      1f
1.35      yamt      517:        subl    $KERNBASE,%eax
1.1       fvdl      518:        movl    %eax,%edi
                    519: 1:
                    520: #endif
                    521:
1.71      ad        522:        /* Skip over any modules/blobs. */
                    523:        movl    RELOC(eblob),%eax
                    524:        testl   %eax,%eax
                    525:        jz      1f
                    526:        subl    $KERNBASE,%eax
                    527:        movl    %eax,%edi
                    528: 1:
1.53      yamt      529:        /* Compute sizes */
1.71      ad        530:        movl    %edi,%esi
1.1       fvdl      531:        addl    $PGOFSET,%esi                   # page align up
                    532:        andl    $~PGOFSET,%esi
                    533:
1.53      yamt      534:        /* nkptp[1] = (esi + ~L2_FRAME) >> L2_SHIFT + 1; */
                    535:        movl    %esi,%eax
                    536:        addl    $~L2_FRAME,%eax
                    537:        shrl    $L2_SHIFT,%eax
                    538:        incl    %eax            /* one more ptp for VAs stolen by bootstrap */
                    539: 1:     movl    %eax,RELOC(nkptp)+1*4
                    540:
                    541:        /* tablesize = (1 + UPAGES + nkptp) << PGSHIFT; */
                    542:        addl    $(1+UPAGES),%eax
                    543:        shll    $PGSHIFT,%eax
                    544:        movl    %eax,RELOC(tablesize)
                    545:
                    546:        /* ensure that nkptp covers bootstrap tables */
                    547:        addl    %esi,%eax
                    548:        addl    $~L2_FRAME,%eax
                    549:        shrl    $L2_SHIFT,%eax
                    550:        incl    %eax
                    551:        cmpl    %eax,RELOC(nkptp)+1*4
                    552:        jnz     1b
                    553:
                    554:        /* Clear tables */
                    555:        movl    %esi,%edi
1.1       fvdl      556:        xorl    %eax,%eax
                    557:        cld
1.53      yamt      558:        movl    RELOC(tablesize),%ecx
                    559:        shrl    $2,%ecx
1.1       fvdl      560:        rep
                    561:        stosl
                    562:
1.53      yamt      563:        leal    (PROC0_PTP1_OFF)(%esi), %ebx
1.1       fvdl      564:
                    565: /*
                    566:  * Build initial page tables.
                    567:  */
1.53      yamt      568:        /*
                    569:         * Compute &__data_start - KERNBASE. This can't be > 4G,
                    570:         * or we can't deal with it anyway, since we can't load it in
                    571:         * 32 bit mode. So use the bottom 32 bits.
                    572:         */
                    573:        movl    $RELOC(__data_start),%edx
1.1       fvdl      574:        andl    $~PGOFSET,%edx
1.30      junyoung  575:
1.53      yamt      576:        /*
                    577:         * Skip the first MB.
                    578:         */
1.1       fvdl      579:        movl    $_RELOC(KERNTEXTOFF),%eax
                    580:        movl    %eax,%ecx
1.53      yamt      581:        shrl    $(PGSHIFT-2),%ecx       /* ((n >> PGSHIFT) << 2) for # pdes */
                    582:        addl    %ecx,%ebx
1.1       fvdl      583:
                    584:        /* Map the kernel text read-only. */
                    585:        movl    %edx,%ecx
                    586:        subl    %eax,%ecx
                    587:        shrl    $PGSHIFT,%ecx
                    588:        orl     $(PG_V|PG_KR),%eax
                    589:        fillkpt
                    590:
                    591:        /* Map the data, BSS, and bootstrap tables read-write. */
                    592:        leal    (PG_V|PG_KW)(%edx),%eax
1.53      yamt      593:        movl    RELOC(tablesize),%ecx
1.1       fvdl      594:        addl    %esi,%ecx                               # end of tables
                    595:        subl    %edx,%ecx                               # subtract end of text
                    596:        shrl    $PGSHIFT,%ecx
                    597:        fillkpt
                    598:
1.53      yamt      599:        /* Map ISA I/O mem (later atdevbase) */
1.1       fvdl      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:  */
1.53      yamt      607:        /* Set up top level entries for identity mapping */
                    608:        leal    (PROC0_PDIR_OFF)(%esi),%ebx
                    609:        leal    (PROC0_PTP1_OFF)(%esi),%eax
                    610:        orl     $(PG_V|PG_KW), %eax
                    611:        movl    RELOC(nkptp)+1*4,%ecx
1.1       fvdl      612:        fillkpt
                    613:
1.53      yamt      614:        /* Set up top level entries for actual kernel mapping */
                    615:        leal    (PROC0_PDIR_OFF + L2_SLOT_KERNBASE*4)(%esi),%ebx
                    616:        leal    (PROC0_PTP1_OFF)(%esi),%eax
                    617:        orl     $(PG_V|PG_KW), %eax
                    618:        movl    RELOC(nkptp)+1*4,%ecx
1.1       fvdl      619:        fillkpt
                    620:
                    621:        /* Install a PDE recursively mapping page directory as a page table! */
1.53      yamt      622:        leal    (PROC0_PDIR_OFF + PDIR_SLOT_PTE*4)(%esi),%ebx
                    623:        leal    (PROC0_PDIR_OFF)(%esi),%eax
                    624:        orl     $(PG_V|PG_KW),%eax
                    625:        movl    %eax,(%ebx)
                    626:
1.1       fvdl      627:
1.31      junyoung  628:        /* Save phys. addr of PDP, for libkvm. */
                    629:        movl    %esi,RELOC(PDPpaddr)
1.1       fvdl      630:
1.53      yamt      631:        /*
                    632:         * Startup checklist:
                    633:         * 1. Load %cr3 with pointer to PDIR.
                    634:         */
1.1       fvdl      635:        movl    %esi,%eax               # phys address of ptd in proc 0
                    636:        movl    %eax,%cr3               # load ptd addr into mmu
1.53      yamt      637:
                    638:        /*
                    639:         * 2. Enable paging and the rest of it.
                    640:         */
1.1       fvdl      641:        movl    %cr0,%eax               # get control word
                    642:                                        # enable paging & NPX emulation
                    643:        orl     $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_EM|CR0_MP),%eax
1.77      gmcgarry  644:        movl    %eax,%cr0               # and page NOW!
1.1       fvdl      645:
                    646:        pushl   $begin                  # jump to high mem
                    647:        ret
                    648:
                    649: begin:
1.53      yamt      650:        /*
                    651:         * We have arrived.
                    652:         * There's no need anymore for the identity mapping in low
                    653:         * memory, remove it.
                    654:         */
                    655:        movl    _C_LABEL(nkptp)+1*4,%ecx
                    656:        leal    (PROC0_PDIR_OFF)(%esi),%ebx     # old, phys  address of PDIR
                    657:        addl    $(KERNBASE), %ebx       # new, virtual address of PDIR
1.1       fvdl      658: 1:     movl    $0,(%ebx)
1.53      yamt      659:        addl    $4,%ebx
1.1       fvdl      660:        loop    1b
                    661:
                    662:        /* Relocate atdevbase. */
1.53      yamt      663:        movl    $KERNBASE,%edx
                    664:        addl    _C_LABEL(tablesize),%edx
1.1       fvdl      665:        addl    %esi,%edx
                    666:        movl    %edx,_C_LABEL(atdevbase)
                    667:
                    668:        /* Set up bootstrap stack. */
1.53      yamt      669:        leal    (PROC0_STK_OFF+KERNBASE)(%esi),%eax
1.91    ! rmind     670:        movl    %eax,_C_LABEL(lwp0uarea)
1.40      yamt      671:        leal    (KSTACK_SIZE-FRAMESIZE)(%eax),%esp
                    672:        movl    %esi,(KSTACK_SIZE+PCB_CR3)(%eax)        # pcb->pcb_cr3
1.1       fvdl      673:        xorl    %ebp,%ebp               # mark end of frames
                    674:
1.37      jmmv      675: #if defined(MULTIBOOT)
                    676:        /* It is now safe to parse the Multiboot information structure
                    677:         * we saved before from C code.  Note that we cannot delay its
                    678:         * parsing any more because initgdt (called below) needs to make
                    679:         * use of this information. */
                    680:        call    _C_LABEL(multiboot_post_reloc)
                    681: #endif
                    682:
1.1       fvdl      683:        subl    $NGDT*8, %esp           # space for temporary gdt
                    684:        pushl   %esp
                    685:        call    _C_LABEL(initgdt)
                    686:        addl    $4,%esp
1.30      junyoung  687:
1.53      yamt      688:        movl    _C_LABEL(tablesize),%eax
1.1       fvdl      689:        addl    %esi,%eax               # skip past stack and page tables
                    690:
                    691:        pushl   %eax
                    692:        call    _C_LABEL(init386)       # wire 386 chip for unix operation
                    693:        addl    $4+NGDT*8,%esp          # pop temporary gdt
                    694:
                    695: #ifdef SAFARI_FIFO_HACK
                    696:        movb    $5,%al
                    697:        movw    $0x37b,%dx
                    698:        outb    %al,%dx
                    699:        movw    $0x37f,%dx
                    700:        inb     %dx,%al
                    701:        movb    %al,%cl
                    702:
                    703:        orb     $1,%cl
                    704:
                    705:        movb    $5,%al
                    706:        movw    $0x37b,%dx
                    707:        outb    %al,%dx
                    708:        movw    $0x37f,%dx
                    709:        movb    %cl,%al
                    710:        outb    %al,%dx
                    711: #endif /* SAFARI_FIFO_HACK */
                    712:
                    713:        call    _C_LABEL(main)
1.62      bouyer    714: #else /* XEN */
                    715: start:
                    716:        /* First, reset the PSL. */
                    717:        pushl   $PSL_MBO
                    718:        popfl
                    719:
                    720:        cld
                    721:        movl    %esp, %ebx              # save start of available space
                    722:        movl    $_RELOC(tmpstk),%esp    # bootstrap stack end location
                    723:
                    724:         /* Clear BSS first so that there are no surprises... */
                    725:        xorl    %eax,%eax
                    726:        movl    $RELOC(__bss_start),%edi
                    727:        movl    $RELOC(_end),%ecx
                    728:        subl    %edi,%ecx
                    729:        rep stosb
                    730:
                    731:        /* Copy the necessary stuff from start_info structure. */
                    732:         /* We need to copy shared_info early, so that sti/cli work */
                    733:        movl    $RELOC(start_info_union),%edi
                    734:        movl    $128,%ecx
                    735:        rep movsl
                    736:
                    737:        /* Clear segment registers; always null in proc0. */
                    738:        xorl    %eax,%eax
                    739:        movw    %ax,%fs
                    740:        movw    %ax,%gs
                    741:        decl    %eax
1.73      ad        742:        movl    %eax,RELOC(cpuid_level)
1.62      bouyer    743:
                    744:        xorl    %eax,%eax
                    745:        cpuid
1.73      ad        746:        movl    %eax,RELOC(cpuid_level)
1.62      bouyer    747:
                    748:        call    xen_pmap_bootstrap
                    749:        /*
                    750:         * First avail returned by xen_pmap_bootstrap in %eax
                    751:         */
                    752:        movl    %eax, %esi;
1.91    ! rmind     753:        movl    %esi, _C_LABEL(lwp0uarea)
1.62      bouyer    754:
                    755: #define PROC0PDIR       ((0)              * PAGE_SIZE)
                    756: #define PROC0STACK      ((1)              * PAGE_SIZE)
                    757:
                    758:        /* Set up bootstrap stack. */
                    759:        leal    (KSTACK_SIZE-FRAMESIZE)(%eax),%esp
                    760:        xorl    %ebp,%ebp               # mark end of frames
                    761:
                    762:        addl    $USPACE, %esi
                    763:        subl    $KERNBASE, %esi         #init386 want a physical address
                    764:        pushl   %esi
                    765:        call    _C_LABEL(init386)       # wire 386 chip for unix operation
                    766:        addl    $4,%esp
                    767:        call    _C_LABEL(main)
                    768:
1.88      cegger    769: #if defined(XEN) && !defined(XEN_COMPAT_030001)
1.62      bouyer    770: /* space for the hypercall call page */
                    771: #define HYPERCALL_PAGE_OFFSET 0x1000
                    772: .org HYPERCALL_PAGE_OFFSET
                    773: ENTRY(hypercall_page)
                    774: .skip 0x1000
1.88      cegger    775: #endif /* defined(XEN) && !defined(XEN_COMPAT_030001) */
1.62      bouyer    776:
                    777: /*
                    778:  * void lgdt_finish(void);
                    779:  * Finish load a new GDT pointer (do any necessary cleanup).
                    780:  * XXX It's somewhat questionable whether reloading all the segment registers
                    781:  * is necessary, since the actual descriptor data is not changed except by
                    782:  * process creation and exit, both of which clean up via task switches.  OTOH,
                    783:  * this only happens at run time when the GDT is resized.
                    784:  */
                    785: /* LINTSTUB: Func: void lgdt_finish(void) */
                    786: NENTRY(lgdt_finish)
                    787:        movl    $GSEL(GDATA_SEL, SEL_KPL),%eax
                    788:        movw    %ax,%ds
                    789:        movw    %ax,%es
                    790:        movw    %ax,%gs
                    791:        movw    %ax,%ss
                    792:        movl    $GSEL(GCPU_SEL, SEL_KPL),%eax
                    793:        movw    %ax,%fs
                    794:        /* Reload code selector by doing intersegment return. */
                    795:        popl    %eax
                    796:        pushl   $GSEL(GCODE_SEL, SEL_KPL)
                    797:        pushl   %eax
                    798:        lret
1.72      yamt      799: END(lgdt_finish)
1.62      bouyer    800:
                    801: #endif /* XEN */
1.1       fvdl      802:
                    803: /*
1.48      yamt      804:  * void lwp_trampoline(void);
                    805:  *
1.1       fvdl      806:  * This is a trampoline function pushed onto the stack of a newly created
                    807:  * process in order to do some additional setup.  The trampoline is entered by
                    808:  * cpu_switch()ing to the process, so we abuse the callee-saved registers used
                    809:  * by cpu_switch() to store the information about the stub to call.
                    810:  * NOTE: This function does not have a normal calling sequence!
                    811:  */
1.48      yamt      812: NENTRY(lwp_trampoline)
1.68      ad        813:        movl    %ebp,%edi       /* for .Lsyscall_checkast */
1.48      yamt      814:        xorl    %ebp,%ebp
1.68      ad        815:        pushl   %edi
1.48      yamt      816:        pushl   %eax
                    817:        call    _C_LABEL(lwp_startup)
                    818:        addl    $8,%esp
1.1       fvdl      819:        pushl   %ebx
                    820:        call    *%esi
                    821:        addl    $4,%esp
1.68      ad        822:        jmp     .Lsyscall_checkast
1.1       fvdl      823:        /* NOTREACHED */
1.72      yamt      824: END(lwp_trampoline)
1.1       fvdl      825:
                    826: /*
1.48      yamt      827:  * sigcode()
                    828:  *
                    829:  * Signal trampoline; copied to top of user stack.  Used only for
                    830:  * compatibility with old releases of NetBSD.
1.1       fvdl      831:  */
                    832: NENTRY(sigcode)
                    833:        /*
                    834:         * Handler has returned here as if we called it.  The sigcontext
                    835:         * is on the stack after the 3 args "we" pushed.
                    836:         */
                    837:        leal    12(%esp),%eax           # get pointer to sigcontext
                    838:        movl    %eax,4(%esp)            # put it in the argument slot
                    839:                                        # fake return address already there
1.17      christos  840:        movl    $SYS_compat_16___sigreturn14,%eax
1.1       fvdl      841:        int     $0x80                   # enter kernel with args on stack
                    842:        movl    $SYS_exit,%eax
                    843:        int     $0x80                   # exit if sigreturn fails
                    844:        .globl  _C_LABEL(esigcode)
                    845: _C_LABEL(esigcode):
1.72      yamt      846: END(sigcode)
1.1       fvdl      847:
                    848: /*
1.48      yamt      849:  * int setjmp(label_t *)
                    850:  *
                    851:  * Used primarily by DDB.
1.1       fvdl      852:  */
                    853: ENTRY(setjmp)
                    854:        movl    4(%esp),%eax
                    855:        movl    %ebx,(%eax)             # save ebx
                    856:        movl    %esp,4(%eax)            # save esp
                    857:        movl    %ebp,8(%eax)            # save ebp
                    858:        movl    %esi,12(%eax)           # save esi
                    859:        movl    %edi,16(%eax)           # save edi
                    860:        movl    (%esp),%edx             # get rta
                    861:        movl    %edx,20(%eax)           # save eip
1.48      yamt      862:        xorl    %eax,%eax               # return 0
1.1       fvdl      863:        ret
1.72      yamt      864: END(setjmp)
1.1       fvdl      865:
1.48      yamt      866: /*
                    867:  * int longjmp(label_t *)
                    868:  *
                    869:  * Used primarily by DDB.
                    870:  */
1.1       fvdl      871: ENTRY(longjmp)
                    872:        movl    4(%esp),%eax
                    873:        movl    (%eax),%ebx             # restore ebx
                    874:        movl    4(%eax),%esp            # restore esp
                    875:        movl    8(%eax),%ebp            # restore ebp
                    876:        movl    12(%eax),%esi           # restore esi
                    877:        movl    16(%eax),%edi           # restore edi
                    878:        movl    20(%eax),%edx           # get rta
                    879:        movl    %edx,(%esp)             # put in return frame
1.48      yamt      880:        movl    $1,%eax                 # return 1
1.1       fvdl      881:        ret
1.72      yamt      882: END(longjmp)
1.1       fvdl      883:
1.65      skrll     884: /*
                    885:  * void dumpsys(void)
                    886:  *
                    887:  * Mimic cpu_switchto() for postmortem debugging.
                    888:  */
1.64      skrll     889: ENTRY(dumpsys)
1.65      skrll     890:        pushl   %ebx                    # set up fake switchframe
                    891:        pushl   %esi                    #   and save context
                    892:        pushl   %edi
                    893:        movl    %esp,_C_LABEL(dumppcb)+PCB_ESP
                    894:        movl    %ebp,_C_LABEL(dumppcb)+PCB_EBP
                    895:        call    _C_LABEL(dodumpsys)     # dump!
                    896:        addl    $(3*4), %esp            # unwind switchframe
1.64      skrll     897:        ret
1.72      yamt      898: END(dumpsys)
1.64      skrll     899:
1.1       fvdl      900: /*
1.79      ad        901:  * struct lwp *cpu_switchto(struct lwp *oldlwp, struct *newlwp,
1.58      ad        902:  *                         bool returning)
1.30      junyoung  903:  *
1.48      yamt      904:  *     1. if (oldlwp != NULL), save its context.
                    905:  *     2. then, restore context of newlwp.
                    906:  *
                    907:  * Note that the stack frame layout is known to "struct switchframe" in
                    908:  * <machine/frame.h> and to the code in cpu_lwp_fork() which initializes
1.5       thorpej   909:  * it for a new lwp.
1.1       fvdl      910:  */
1.48      yamt      911: ENTRY(cpu_switchto)
1.1       fvdl      912:        pushl   %ebx
                    913:        pushl   %esi
                    914:        pushl   %edi
                    915:
1.70      ad        916: #if defined(DIAGNOSTIC) && !defined(XEN)
                    917:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                    918:        jbe     0f
1.74      christos  919:        pushl   CPUVAR(ILEVEL)
1.70      ad        920:        pushl   $.Lstr
                    921:        call    _C_LABEL(panic)
1.74      christos  922:        addl    $8,%esp
                    923: .Lstr: .string "cpu_switchto: switching above IPL_SCHED (%d)\0"
1.70      ad        924: 0:
                    925: #endif
                    926:
1.48      yamt      927:        movl    16(%esp),%esi           # oldlwp
                    928:        movl    20(%esp),%edi           # newlwp
1.58      ad        929:        movl    24(%esp),%edx           # returning
1.48      yamt      930:        testl   %esi,%esi
                    931:        jz      1f
1.1       fvdl      932:
1.48      yamt      933:        /* Save old context. */
1.91    ! rmind     934:        movl    L_PCB(%esi),%eax
1.48      yamt      935:        movl    %esp,PCB_ESP(%eax)
                    936:        movl    %ebp,PCB_EBP(%eax)
                    937:
                    938:        /* Switch to newlwp's stack. */
1.91    ! rmind     939: 1:     movl    L_PCB(%edi),%ebx
1.48      yamt      940:        movl    PCB_EBP(%ebx),%ebp
                    941:        movl    PCB_ESP(%ebx),%esp
1.1       fvdl      942:
1.61      ad        943:        /*
                    944:         * Set curlwp.  This must be globally visible in order to permit
                    945:         * non-interlocked mutex release.
                    946:         */
                    947:        movl    %edi,%ecx
                    948:        xchgl   %ecx,CPUVAR(CURLWP)
1.58      ad        949:
                    950:        /* Skip the rest if returning to a pinned LWP. */
                    951:        testl   %edx,%edx
                    952:        jnz     4f
                    953:
1.62      bouyer    954: #ifdef XEN
                    955:        pushl   %edi
                    956:        call    _C_LABEL(i386_switch_context)
                    957:        addl    $4,%esp
                    958: #else /* XEN */
1.60      yamt      959:        /* Switch ring0 esp */
                    960:        movl    PCB_ESP0(%ebx),%eax
                    961:        movl    %eax,CPUVAR(ESP0)
1.1       fvdl      962:
                    963:        /* Don't bother with the rest if switching to a system process. */
1.48      yamt      964:        testl   $LW_SYSTEM,L_FLAG(%edi)
                    965:        jnz     4f
1.1       fvdl      966:
1.54      ad        967:        /* Restore thread-private %fs/%gs descriptors. */
1.60      yamt      968:        movl    CPUVAR(GDT),%ecx
1.55      ad        969:        movl    PCB_FSD(%ebx), %eax
                    970:        movl    PCB_FSD+4(%ebx), %edx
1.54      ad        971:        movl    %eax, (GUFS_SEL*8)(%ecx)
                    972:        movl    %edx, (GUFS_SEL*8+4)(%ecx)
1.55      ad        973:        movl    PCB_GSD(%ebx), %eax
                    974:        movl    PCB_GSD+4(%ebx), %edx
1.54      ad        975:        movl    %eax, (GUGS_SEL*8)(%ecx)
                    976:        movl    %edx, (GUGS_SEL*8+4)(%ecx)
1.62      bouyer    977: #endif /* XEN */
1.54      ad        978:
1.60      yamt      979:        /* Switch I/O bitmap */
                    980:        movl    PCB_IOMAP(%ebx),%eax
                    981:        orl     %eax,%eax
                    982:        jnz,pn  .Lcopy_iobitmap
                    983:        movl    $(IOMAP_INVALOFF << 16),CPUVAR(IOBASE)
                    984: .Liobitmap_done:
                    985:
1.56      ad        986:        /* Is this process using RAS (restartable atomic sequences)? */
                    987:        movl    L_PROC(%edi),%eax
                    988:        cmpl    $0,P_RASLIST(%eax)
                    989:        jne     5f
                    990:
1.48      yamt      991:        /*
1.90      rmind     992:         * Restore cr0 (including FPU state).  Raise the IPL to IPL_HIGH.
1.48      yamt      993:         * FPU IPIs can alter the LWP's saved cr0.  Dropping the priority
                    994:         * is deferred until mi_switch(), when cpu_switchto() returns.
                    995:         */
1.62      bouyer    996: 2:
                    997: #ifndef XEN
1.90      rmind     998:        movl    $IPL_HIGH,CPUVAR(ILEVEL)
1.79      ad        999:        movl    PCB_CR0(%ebx),%ecx              /* has CR0_TS clear */
1.48      yamt     1000:        movl    %cr0,%edx
1.1       fvdl     1001:
1.30      junyoung 1002:        /*
1.22      wiz      1003:         * If our floating point registers are on a different CPU,
1.48      yamt     1004:         * set CR0_TS so we'll trap rather than reuse bogus state.
1.1       fvdl     1005:         */
1.79      ad       1006:        cmpl    CPUVAR(FPCURLWP),%edi
1.48      yamt     1007:        je      3f
1.1       fvdl     1008:        orl     $CR0_TS,%ecx
1.48      yamt     1009:
                   1010:        /* Reloading CR0 is very expensive - avoid if possible. */
                   1011: 3:     cmpl    %edx,%ecx
                   1012:        je      4f
1.1       fvdl     1013:        movl    %ecx,%cr0
1.62      bouyer   1014: #endif /* XEN */
1.1       fvdl     1015:
1.48      yamt     1016:        /* Return to the new LWP, returning 'oldlwp' in %eax. */
                   1017: 4:     movl    %esi,%eax
1.1       fvdl     1018:        popl    %edi
                   1019:        popl    %esi
                   1020:        popl    %ebx
                   1021:        ret
1.20      dsl      1022:
1.48      yamt     1023:        /* Check for restartable atomic sequences (RAS). */
                   1024: 5:     movl    L_MD_REGS(%edi),%ecx
                   1025:        pushl   TF_EIP(%ecx)
1.20      dsl      1026:        pushl   %eax
                   1027:        call    _C_LABEL(ras_lookup)
                   1028:        addl    $8,%esp
                   1029:        cmpl    $-1,%eax
1.48      yamt     1030:        je      2b
1.49      ad       1031:        movl    L_MD_REGS(%edi),%ecx
                   1032:        movl    %eax,TF_EIP(%ecx)
1.48      yamt     1033:        jmp     2b
1.1       fvdl     1034:
1.60      yamt     1035: .Lcopy_iobitmap:
                   1036:        /* Copy I/O bitmap. */
1.86      ad       1037:        incl    _C_LABEL(pmap_iobmp_evcnt)+EV_COUNT
1.60      yamt     1038:        movl    $(IOMAPSIZE/4),%ecx
                   1039:        pushl   %esi
                   1040:        pushl   %edi
                   1041:        movl    %eax,%esi               /* pcb_iomap */
                   1042:        movl    CPUVAR(SELF),%edi
                   1043:        leal    CPU_INFO_IOMAP(%edi),%edi
                   1044:        rep
                   1045:        movsl
                   1046:        popl    %edi
                   1047:        popl    %esi
                   1048:        movl    $((CPU_INFO_IOMAP - CPU_INFO_TSS) << 16),CPUVAR(IOBASE)
                   1049:        jmp     .Liobitmap_done
1.72      yamt     1050: END(cpu_switchto)
1.60      yamt     1051:
1.1       fvdl     1052: /*
                   1053:  * void savectx(struct pcb *pcb);
1.48      yamt     1054:  *
1.1       fvdl     1055:  * Update pcb, saving current processor state.
                   1056:  */
                   1057: ENTRY(savectx)
1.47      skrll    1058:        movl    4(%esp),%edx            # edx = pcb
1.1       fvdl     1059:        movl    %esp,PCB_ESP(%edx)
                   1060:        movl    %ebp,PCB_EBP(%edx)
                   1061:        ret
1.72      yamt     1062: END(savectx)
1.1       fvdl     1063:
                   1064: /*
1.48      yamt     1065:  * osyscall()
                   1066:  *
1.1       fvdl     1067:  * Old call gate entry for syscall
                   1068:  */
                   1069: IDTVEC(osyscall)
1.87      ad       1070: #ifndef XEN
                   1071:        /* XXX we are in trouble! interrupts be off here. */
1.85      ad       1072:        cli                     # must be first instruction
1.87      ad       1073: #endif
1.48      yamt     1074:        pushfl                  # set eflags in trap frame
1.1       fvdl     1075:        popl    8(%esp)
1.85      ad       1076:        orl     $PSL_I,(%esp)   # re-enable ints on return to user
1.1       fvdl     1077:        pushl   $7              # size of instruction for restart
                   1078:        jmp     syscall1
1.72      yamt     1079: IDTVEC_END(osyscall)
1.1       fvdl     1080:
                   1081: /*
1.48      yamt     1082:  * syscall()
                   1083:  *
1.1       fvdl     1084:  * Trap gate entry for syscall
                   1085:  */
                   1086: IDTVEC(syscall)
                   1087:        pushl   $2              # size of instruction for restart
                   1088: syscall1:
                   1089:        pushl   $T_ASTFLT       # trap # for doing ASTs
                   1090:        INTRENTRY
1.85      ad       1091:        STI(%eax)
1.1       fvdl     1092: #ifdef DIAGNOSTIC
                   1093:        movl    CPUVAR(ILEVEL),%ebx
                   1094:        testl   %ebx,%ebx
                   1095:        jz      1f
                   1096:        pushl   $5f
1.84      ad       1097:        call    _C_LABEL(panic)
1.1       fvdl     1098:        addl    $4,%esp
                   1099: #ifdef DDB
                   1100:        int     $3
                   1101: #endif
1.30      junyoung 1102: 1:
1.1       fvdl     1103: #endif /* DIAGNOSTIC */
1.67      ad       1104:        incl    CPUVAR(NSYSCALL)        # count it atomically
1.68      ad       1105:        movl    CPUVAR(CURLWP),%edi
                   1106:        movl    L_PROC(%edi),%edx
                   1107:        movl    %esp,L_MD_REGS(%edi)    # save pointer to frame
1.15      fvdl     1108:        pushl   %esp
1.1       fvdl     1109:        call    *P_MD_SYSCALL(%edx)     # get pointer to syscall() function
1.15      fvdl     1110:        addl    $4,%esp
1.27      yamt     1111: .Lsyscall_checkast:
1.24      yamt     1112:        /* Check for ASTs on exit to user mode. */
1.62      bouyer   1113:        CLI(%eax)
1.68      ad       1114:        movl    L_MD_ASTPENDING(%edi), %eax
                   1115:        orl     CPUVAR(WANT_PMAPLOAD), %eax
1.24      yamt     1116:        jnz     9f
1.62      bouyer   1117: #ifdef XEN
                   1118:        STIC(%eax)
                   1119:        jz      14f
                   1120:        call    _C_LABEL(stipending)
                   1121:        testl   %eax,%eax
                   1122:        jz      14f
                   1123:        /* process pending interrupts */
                   1124:        CLI(%eax)
                   1125:        movl    CPUVAR(ILEVEL), %ebx
                   1126:        movl    $.Lsyscall_resume, %esi # address to resume loop at
                   1127: .Lsyscall_resume:
                   1128:        movl    %ebx,%eax       # get cpl
                   1129:        movl    CPUVAR(IUNMASK)(,%eax,4),%eax
                   1130:        andl    CPUVAR(IPENDING),%eax   # any non-masked bits left?
                   1131:        jz      17f
                   1132:        bsrl    %eax,%eax
                   1133:        btrl    %eax,CPUVAR(IPENDING)
                   1134:        movl    CPUVAR(ISOURCES)(,%eax,4),%eax
                   1135:        jmp     *IS_RESUME(%eax)
                   1136: 17:    movl    %ebx, CPUVAR(ILEVEL)    #restore cpl
                   1137:        jmp     .Lsyscall_checkast
                   1138: 14:
                   1139: #endif /* XEN */
1.1       fvdl     1140: #ifndef DIAGNOSTIC
1.24      yamt     1141:        INTRFASTEXIT
1.1       fvdl     1142: #else /* DIAGNOSTIC */
1.24      yamt     1143:        cmpl    $IPL_NONE,CPUVAR(ILEVEL)
1.1       fvdl     1144:        jne     3f
                   1145:        INTRFASTEXIT
1.62      bouyer   1146: 3:     STI(%eax)
1.1       fvdl     1147:        pushl   $4f
1.84      ad       1148:        call    _C_LABEL(panic)
1.1       fvdl     1149:        addl    $4,%esp
1.58      ad       1150:        pushl   $IPL_NONE
                   1151:        call    _C_LABEL(spllower)
                   1152:        addl    $4,%esp
1.53      yamt     1153:        jmp     .Lsyscall_checkast
1.84      ad       1154: 4:     .asciz  "SPL NOT LOWERED ON SYSCALL EXIT\n"
                   1155: 5:     .asciz  "SPL NOT ZERO ON SYSCALL ENTRY\n"
1.1       fvdl     1156: #endif /* DIAGNOSTIC */
1.68      ad       1157: 9:
                   1158:        cmpl    $0, CPUVAR(WANT_PMAPLOAD)
                   1159:        jz      10f
                   1160:        STI(%eax)
1.24      yamt     1161:        call    _C_LABEL(pmap_load)
1.27      yamt     1162:        jmp     .Lsyscall_checkast      /* re-check ASTs */
1.68      ad       1163: 10:
                   1164:        /* Always returning to user mode here. */
                   1165:        movl    $0, L_MD_ASTPENDING(%edi)
                   1166:        STI(%eax)
                   1167:        /* Pushed T_ASTFLT into tf_trapno on entry. */
                   1168:        pushl   %esp
                   1169:        call    _C_LABEL(trap)
                   1170:        addl    $4,%esp
                   1171:        jmp     .Lsyscall_checkast      /* re-check ASTs */
1.72      yamt     1172: IDTVEC_END(syscall)
1.1       fvdl     1173:
1.81      ad       1174: IDTVEC(svr4_fasttrap)
                   1175:        pushl   $2              # size of instruction for restart
                   1176:        pushl   $T_ASTFLT       # trap # for doing ASTs
                   1177:        INTRENTRY
1.85      ad       1178:        STI(%eax)
1.81      ad       1179:        pushl   $RW_READER
                   1180:        pushl   $_C_LABEL(svr4_fasttrap_lock)
                   1181:        call    _C_LABEL(rw_enter)
                   1182:        addl    $8,%esp
                   1183:        call    *_C_LABEL(svr4_fasttrap_vec)
                   1184:        pushl   $_C_LABEL(svr4_fasttrap_lock)
                   1185:        call    _C_LABEL(rw_exit)
                   1186:        addl    $4,%esp
                   1187: 2:     /* Check for ASTs on exit to user mode. */
                   1188:        cli
                   1189:        CHECK_ASTPENDING(%eax)
                   1190:        je      1f
                   1191:        /* Always returning to user mode here. */
                   1192:        CLEAR_ASTPENDING(%eax)
                   1193:        sti
                   1194:        /* Pushed T_ASTFLT into tf_trapno on entry. */
                   1195:        pushl   %esp
                   1196:        call    _C_LABEL(trap)
                   1197:        addl    $4,%esp
                   1198:        jmp     2b
                   1199: 1:     CHECK_DEFERRED_SWITCH
                   1200:        jnz     9f
                   1201:        INTRFASTEXIT
                   1202: 9:     sti
                   1203:        call    _C_LABEL(pmap_load)
                   1204:        cli
                   1205:        jmp     2b
                   1206:
1.1       fvdl     1207: #if NNPX > 0
                   1208: /*
                   1209:  * Special interrupt handlers.  Someday intr0-intr15 will be used to count
                   1210:  * interrupts.  We'll still need a special exception 16 handler.  The busy
                   1211:  * latch stuff in probintr() can be moved to npxprobe().
                   1212:  */
                   1213:
1.48      yamt     1214: /*
                   1215:  * void probeintr(void)
                   1216:  */
1.1       fvdl     1217: NENTRY(probeintr)
                   1218:        ss
                   1219:        incl    _C_LABEL(npx_intrs_while_probing)
                   1220:        pushl   %eax
                   1221:        movb    $0x20,%al       # EOI (asm in strings loses cpp features)
                   1222:        outb    %al,$0xa0       # IO_ICU2
                   1223:        outb    %al,$0x20       # IO_ICU1
                   1224:        movb    $0,%al
                   1225:        outb    %al,$0xf0       # clear BUSY# latch
                   1226:        popl    %eax
                   1227:        iret
1.72      yamt     1228: END(probeintr)
1.1       fvdl     1229:
1.48      yamt     1230: /*
                   1231:  * void probetrap(void)
                   1232:  */
1.1       fvdl     1233: NENTRY(probetrap)
                   1234:        ss
                   1235:        incl    _C_LABEL(npx_traps_while_probing)
                   1236:        fnclex
                   1237:        iret
1.72      yamt     1238: END(probetrap)
1.1       fvdl     1239:
1.48      yamt     1240: /*
                   1241:  * int npx586bug1(int a, int b)
                   1242:  */
1.1       fvdl     1243: NENTRY(npx586bug1)
                   1244:        fildl   4(%esp)         # x
                   1245:        fildl   8(%esp)         # y
                   1246:        fld     %st(1)
                   1247:        fdiv    %st(1),%st      # x/y
                   1248:        fmulp   %st,%st(1)      # (x/y)*y
                   1249:        fsubrp  %st,%st(1)      # x-(x/y)*y
                   1250:        pushl   $0
                   1251:        fistpl  (%esp)
                   1252:        popl    %eax
                   1253:        ret
1.72      yamt     1254: END(npx586bug1)
1.1       fvdl     1255: #endif /* NNPX > 0 */
1.50      ad       1256:
                   1257: /*
1.76      ad       1258:  * void sse2_idlezero_page(void *pg)
1.50      ad       1259:  *
1.76      ad       1260:  * Zero a page without polluting the cache.  Preemption must be
                   1261:  * disabled by the caller. Abort if a preemption is pending.
1.50      ad       1262:  */
1.76      ad       1263: ENTRY(sse2_idlezero_page)
1.50      ad       1264:        pushl   %ebp
                   1265:        movl    %esp,%ebp
                   1266:        movl    8(%esp), %edx
1.76      ad       1267:        movl    $(PAGE_SIZE/32), %ecx
1.50      ad       1268:        xorl    %eax, %eax
                   1269:        .align  16
                   1270: 1:
1.82      ad       1271:        testl   $RESCHED_KPREEMPT, CPUVAR(RESCHED)
1.76      ad       1272:        jnz     2f
1.50      ad       1273:        movnti  %eax, 0(%edx)
                   1274:        movnti  %eax, 4(%edx)
                   1275:        movnti  %eax, 8(%edx)
                   1276:        movnti  %eax, 12(%edx)
                   1277:        movnti  %eax, 16(%edx)
                   1278:        movnti  %eax, 20(%edx)
                   1279:        movnti  %eax, 24(%edx)
                   1280:        movnti  %eax, 28(%edx)
1.76      ad       1281:        addl    $32, %edx
1.75      ad       1282:        decl    %ecx
1.50      ad       1283:        jnz     1b
                   1284:        sfence
1.76      ad       1285:        incl    %eax
1.50      ad       1286:        pop     %ebp
                   1287:        ret
1.76      ad       1288: 2:
1.50      ad       1289:        sfence
                   1290:        popl    %ebp
                   1291:        ret
1.76      ad       1292: END(sse2_idlezero_page)

CVSweb <webmaster@jp.NetBSD.org>