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

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

CVSweb <webmaster@jp.NetBSD.org>