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

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

CVSweb <webmaster@jp.NetBSD.org>