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

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

CVSweb <webmaster@jp.NetBSD.org>