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

1.40.2.1! gdamore     1: /*     $NetBSD: locore.S,v 1.42 2006/07/08 20:30:00 christos Exp $     */
1.1       fvdl        2:
                      3: /*-
1.32      mycroft     4:  * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
1.1       fvdl        5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Charles M. Hannum.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *        This product includes software developed by the NetBSD
                     21:  *        Foundation, Inc. and its contributors.
                     22:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     23:  *    contributors may be used to endorse or promote products derived
                     24:  *    from this software without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     27:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
                     37:  */
                     38:
                     39: /*-
                     40:  * Copyright (c) 1990 The Regents of the University of California.
                     41:  * All rights reserved.
                     42:  *
                     43:  * This code is derived from software contributed to Berkeley by
                     44:  * William Jolitz.
                     45:  *
                     46:  * Redistribution and use in source and binary forms, with or without
                     47:  * modification, are permitted provided that the following conditions
                     48:  * are met:
                     49:  * 1. Redistributions of source code must retain the above copyright
                     50:  *    notice, this list of conditions and the following disclaimer.
                     51:  * 2. Redistributions in binary form must reproduce the above copyright
                     52:  *    notice, this list of conditions and the following disclaimer in the
                     53:  *    documentation and/or other materials provided with the distribution.
1.12      agc        54:  * 3. Neither the name of the University nor the names of its contributors
1.1       fvdl       55:  *    may be used to endorse or promote products derived from this software
                     56:  *    without specific prior written permission.
                     57:  *
                     58:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     59:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     60:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     61:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     62:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     63:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     64:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     65:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     66:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     67:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     68:  * SUCH DAMAGE.
                     69:  *
                     70:  *     @(#)locore.s    7.3 (Berkeley) 5/13/91
                     71:  */
                     72:
1.18      christos   73: #include "opt_compat_netbsd.h"
                     74: #include "opt_compat_oldboot.h"
1.1       fvdl       75: #include "opt_cputype.h"
                     76: #include "opt_ddb.h"
                     77: #include "opt_ipkdb.h"
1.18      christos   78: #include "opt_lockdebug.h"
1.1       fvdl       79: #include "opt_multiprocessor.h"
                     80: #include "opt_realmem.h"
1.18      christos   81: #include "opt_user_ldt.h"
                     82: #include "opt_vm86.h"
1.1       fvdl       83:
                     84: #include "npx.h"
                     85: #include "assym.h"
                     86: #include "lapic.h"
                     87: #include "ioapic.h"
1.8       fvdl       88: #include "ksyms.h"
1.1       fvdl       89:
                     90: #include <sys/errno.h>
                     91: #include <sys/syscall.h>
                     92:
                     93: #include <machine/cputypes.h>
                     94: #include <machine/segments.h>
                     95: #include <machine/specialreg.h>
                     96: #include <machine/trap.h>
                     97:
                     98: #if NLAPIC > 0
                     99: #include <machine/i82489reg.h>
                    100: #endif
                    101:
1.37      jmmv      102: #include <machine/multiboot.h>
                    103:
1.1       fvdl      104: /* LINTSTUB: include <sys/types.h> */
                    105: /* LINTSTUB: include <machine/cpu.h> */
                    106: /* LINTSTUB: include <sys/systm.h> */
                    107:
                    108: #include <machine/asm.h>
                    109:
                    110: #if defined(MULTIPROCESSOR)
1.30      junyoung  111:
1.5       thorpej   112: #define SET_CURLWP(lwp,cpu)                            \
1.1       fvdl      113:        movl    CPUVAR(SELF),cpu                ;       \
1.5       thorpej   114:        movl    lwp,CPUVAR(CURLWP)      ;       \
                    115:        movl    cpu,L_CPU(lwp)
1.30      junyoung  116:
1.1       fvdl      117: #else
                    118:
1.5       thorpej   119: #define SET_CURLWP(lwp,tcpu)           movl    lwp,CPUVAR(CURLWP)
                    120: #define GET_CURLWP(reg)                        movl    CPUVAR(CURLWP),reg
1.1       fvdl      121:
                    122: #endif
                    123:
                    124: #define SET_CURPCB(reg)                        movl    reg,CPUVAR(CURPCB)
1.24      yamt      125:
1.1       fvdl      126: #define CLEAR_RESCHED(reg)             movl    reg,CPUVAR(RESCHED)
                    127:
                    128: /* XXX temporary kluge; these should not be here */
                    129: /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
                    130: #include <dev/isa/isareg.h>
                    131:
                    132:
                    133: /* Disallow old names for REALBASEMEM */
                    134: #ifdef BIOSBASEMEM
                    135: #error BIOSBASEMEM option deprecated; use REALBASEMEM only if memory size reported by latest boot block is incorrect
                    136: #endif
                    137:
                    138: /* Disallow old names for REALEXTMEM */
                    139: #ifdef EXTMEM_SIZE
                    140: #error EXTMEM_SIZE option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
                    141: #endif
                    142: #ifdef BIOSEXTMEM
                    143: #error BIOSEXTMEM option deprecated; use REALEXTMEM only if memory size reported by latest boot block is incorrect
                    144: #endif
                    145:
                    146: #include <machine/frameasm.h>
                    147:
                    148:
                    149: #ifdef MULTIPROCESSOR
                    150: #include <machine/i82489reg.h>
                    151: #endif
1.30      junyoung  152:
1.1       fvdl      153: /*
                    154:  * Initialization
                    155:  */
                    156:        .data
                    157:
                    158:        .globl  _C_LABEL(cpu)
1.38      jmmv      159:        .globl  _C_LABEL(esym)
                    160:        .globl  _C_LABEL(atdevbase)
1.40      yamt      161:        .globl  _C_LABEL(proc0uarea),_C_LABEL(PDPpaddr)
1.1       fvdl      162:        .globl  _C_LABEL(gdt)
                    163: #ifdef I586_CPU
                    164:        .globl  _C_LABEL(idt)
                    165: #endif
1.30      junyoung  166:        .globl  _C_LABEL(lapic_tpr)
                    167:
1.1       fvdl      168: #if NLAPIC > 0
                    169: #ifdef __ELF__
1.7       thorpej   170:        .align  PAGE_SIZE
1.1       fvdl      171: #else
                    172:        .align  12
                    173: #endif
                    174:        .globl _C_LABEL(local_apic), _C_LABEL(lapic_id)
                    175: _C_LABEL(local_apic):
                    176:        .space  LAPIC_ID
1.30      junyoung  177: _C_LABEL(lapic_id):
1.1       fvdl      178:        .long   0x00000000
                    179:        .space  LAPIC_TPRI-(LAPIC_ID+4)
1.30      junyoung  180: _C_LABEL(lapic_tpr):
1.1       fvdl      181:        .space  LAPIC_PPRI-LAPIC_TPRI
1.30      junyoung  182: _C_LABEL(lapic_ppr):
1.1       fvdl      183:        .space  LAPIC_ISR-LAPIC_PPRI
                    184: _C_LABEL(lapic_isr):
1.7       thorpej   185:        .space  PAGE_SIZE-LAPIC_ISR
1.1       fvdl      186: #else
1.30      junyoung  187: _C_LABEL(lapic_tpr):
1.1       fvdl      188:        .long 0
                    189: #endif
1.30      junyoung  190:
1.1       fvdl      191:
                    192: _C_LABEL(cpu):         .long   0       # are we 386, 386sx, or 486,
                    193:                                        #   or Pentium, or..
                    194: _C_LABEL(atdevbase):   .long   0       # location of start of iomem in virtual
1.40      yamt      195: _C_LABEL(proc0uarea):  .long   0
1.31      junyoung  196: _C_LABEL(PDPpaddr):    .long   0       # paddr of PDP, for libkvm
1.30      junyoung  197:
1.1       fvdl      198:        .space 512
                    199: tmpstk:
                    200:
                    201:
1.35      yamt      202: #define        _RELOC(x)       ((x) - KERNBASE)
1.1       fvdl      203: #define        RELOC(x)        _RELOC(_C_LABEL(x))
                    204:
                    205:        .text
                    206:        .globl  _C_LABEL(kernel_text)
                    207:        .set    _C_LABEL(kernel_text),KERNTEXTOFF
                    208:
                    209:        .globl  start
                    210: start: movw    $0x1234,0x472                   # warm boot
                    211:
1.37      jmmv      212: #if defined(MULTIBOOT)
                    213:        jmp     1f
                    214:
                    215:        .align  4
                    216:        .globl  Multiboot_Header
                    217: _C_LABEL(Multiboot_Header):
                    218: #define MULTIBOOT_HEADER_FLAGS (MULTIBOOT_HEADER_WANT_MEMORY | \
                    219:                                 MULTIBOOT_HEADER_HAS_ADDR)
                    220:        .long   MULTIBOOT_HEADER_MAGIC
                    221:        .long   MULTIBOOT_HEADER_FLAGS
                    222:        .long   -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
                    223:        .long   RELOC(Multiboot_Header)
                    224:        .long   RELOC(start)
                    225:        .long   RELOC(_edata)
                    226:        .long   RELOC(_end) + MULTIBOOT_SYMTAB_SPACE
                    227:        .long   RELOC(start)
                    228:
                    229: 1:
                    230:        /* Check if we are being executed by a Multiboot-compliant boot
                    231:         * loader. */
                    232:        cmpl    $MULTIBOOT_INFO_MAGIC,%eax
                    233:        jne     1f
                    234:
                    235:        /* Indeed, a multiboot-compliat boot loader executed us.  We copy
                    236:         * the received Multiboot information structure into kernel's data
                    237:         * space to process it later -- after we are relocated.  It will
                    238:         * be safer to run complex C code than doing it at this point. */
                    239:        pushl   %ebx            # Address of Multiboot information
                    240:        call    _C_LABEL(multiboot_pre_reloc)
                    241:        addl    $4,%esp
1.38      jmmv      242:        jmp     2f
1.37      jmmv      243: #endif
                    244:
                    245: 1:
1.1       fvdl      246:        /*
1.38      jmmv      247:         * At this point, we know that a NetBSD-specific boot loader
                    248:         * booted this kernel.  The stack carries the following parameters:
1.40.2.1! gdamore   249:         * (boothowto, [bootdev], bootinfo, esym, biosextmem, biosbasemem),
1.38      jmmv      250:         * 4 bytes each.
1.1       fvdl      251:         */
1.38      jmmv      252:        addl    $4,%esp         # Discard return address to boot loader
                    253:        call    _C_LABEL(native_loader)
                    254:        addl    $24,%esp
1.1       fvdl      255:
                    256: 2:
                    257:        /* First, reset the PSL. */
                    258:        pushl   $PSL_MBO
                    259:        popfl
                    260:
                    261:        /* Clear segment registers; always null in proc0. */
                    262:        xorl    %eax,%eax
                    263:        movw    %ax,%fs
                    264:        movw    %ax,%gs
                    265:        decl    %eax
                    266:        movl    %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
                    267:
                    268:        /* Find out our CPU type. */
                    269:
                    270: try386:        /* Try to toggle alignment check flag; does not exist on 386. */
                    271:        pushfl
                    272:        popl    %eax
                    273:        movl    %eax,%ecx
                    274:        orl     $PSL_AC,%eax
                    275:        pushl   %eax
                    276:        popfl
                    277:        pushfl
                    278:        popl    %eax
                    279:        xorl    %ecx,%eax
                    280:        andl    $PSL_AC,%eax
                    281:        pushl   %ecx
                    282:        popfl
                    283:
                    284:        testl   %eax,%eax
                    285:        jnz     try486
                    286:
                    287:        /*
                    288:         * Try the test of a NexGen CPU -- ZF will not change on a DIV
                    289:         * instruction on a NexGen, it will on an i386.  Documented in
                    290:         * Nx586 Processor Recognition Application Note, NexGen, Inc.
                    291:         */
                    292:        movl    $0x5555,%eax
                    293:        xorl    %edx,%edx
                    294:        movl    $2,%ecx
                    295:        divl    %ecx
                    296:        jnz     is386
                    297:
                    298: isnx586:
                    299:        /*
                    300:         * Don't try cpuid, as Nx586s reportedly don't support the
                    301:         * PSL_ID bit.
                    302:         */
                    303:        movl    $CPU_NX586,RELOC(cpu)
                    304:        jmp     2f
                    305:
                    306: is386:
                    307:        movl    $CPU_386,RELOC(cpu)
                    308:        jmp     2f
                    309:
                    310: try486:        /* Try to toggle identification flag; does not exist on early 486s. */
                    311:        pushfl
                    312:        popl    %eax
                    313:        movl    %eax,%ecx
                    314:        xorl    $PSL_ID,%eax
                    315:        pushl   %eax
                    316:        popfl
                    317:        pushfl
                    318:        popl    %eax
                    319:        xorl    %ecx,%eax
                    320:        andl    $PSL_ID,%eax
                    321:        pushl   %ecx
                    322:        popfl
                    323:
                    324:        testl   %eax,%eax
                    325:        jnz     try586
                    326: is486: movl    $CPU_486,RELOC(cpu)
                    327:        /*
                    328:         * Check Cyrix CPU
                    329:         * Cyrix CPUs do not change the undefined flags following
                    330:         * execution of the divide instruction which divides 5 by 2.
                    331:         *
                    332:         * Note: CPUID is enabled on M2, so it passes another way.
                    333:         */
                    334:        pushfl
                    335:        movl    $0x5555, %eax
                    336:        xorl    %edx, %edx
                    337:        movl    $2, %ecx
                    338:        clc
                    339:        divl    %ecx
                    340:        jnc     trycyrix486
                    341:        popfl
                    342:        jmp 2f
                    343: trycyrix486:
                    344:        movl    $CPU_6x86,RELOC(cpu)    # set CPU type
                    345:        /*
                    346:         * Check for Cyrix 486 CPU by seeing if the flags change during a
                    347:         * divide. This is documented in the Cx486SLC/e SMM Programmer's
                    348:         * Guide.
                    349:         */
                    350:        xorl    %edx,%edx
                    351:        cmpl    %edx,%edx               # set flags to known state
                    352:        pushfl
                    353:        popl    %ecx                    # store flags in ecx
                    354:        movl    $-1,%eax
                    355:        movl    $4,%ebx
                    356:        divl    %ebx                    # do a long division
                    357:        pushfl
                    358:        popl    %eax
                    359:        xorl    %ecx,%eax               # are the flags different?
                    360:        testl   $0x8d5,%eax             # only check C|PF|AF|Z|N|V
                    361:        jne     2f                      # yes; must be Cyrix 6x86 CPU
                    362:        movl    $CPU_486DLC,RELOC(cpu)  # set CPU type
                    363:
                    364: #ifndef CYRIX_CACHE_WORKS
                    365:        /* Disable caching of the ISA hole only. */
                    366:        invd
                    367:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    368:        outb    %al,$0x22
                    369:        inb     $0x23,%al
                    370:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    371:        movb    %al,%ah
                    372:        movb    $CCR0,%al
                    373:        outb    %al,$0x22
                    374:        movb    %ah,%al
                    375:        outb    %al,$0x23
                    376:        invd
                    377: #else /* CYRIX_CACHE_WORKS */
                    378:        /* Set cache parameters */
                    379:        invd                            # Start with guaranteed clean cache
                    380:        movb    $CCR0,%al               # Configuration Register index (CCR0)
                    381:        outb    %al,$0x22
                    382:        inb     $0x23,%al
                    383:        andb    $~CCR0_NC0,%al
                    384: #ifndef CYRIX_CACHE_REALLY_WORKS
                    385:        orb     $(CCR0_NC1|CCR0_BARB),%al
                    386: #else
                    387:        orb     $CCR0_NC1,%al
                    388: #endif
                    389:        movb    %al,%ah
                    390:        movb    $CCR0,%al
                    391:        outb    %al,$0x22
                    392:        movb    %ah,%al
                    393:        outb    %al,$0x23
                    394:        /* clear non-cacheable region 1 */
                    395:        movb    $(NCR1+2),%al
                    396:        outb    %al,$0x22
                    397:        movb    $NCR_SIZE_0K,%al
                    398:        outb    %al,$0x23
                    399:        /* clear non-cacheable region 2 */
                    400:        movb    $(NCR2+2),%al
                    401:        outb    %al,$0x22
                    402:        movb    $NCR_SIZE_0K,%al
                    403:        outb    %al,$0x23
                    404:        /* clear non-cacheable region 3 */
                    405:        movb    $(NCR3+2),%al
                    406:        outb    %al,$0x22
                    407:        movb    $NCR_SIZE_0K,%al
                    408:        outb    %al,$0x23
                    409:        /* clear non-cacheable region 4 */
                    410:        movb    $(NCR4+2),%al
                    411:        outb    %al,$0x22
                    412:        movb    $NCR_SIZE_0K,%al
                    413:        outb    %al,$0x23
                    414:        /* enable caching in CR0 */
                    415:        movl    %cr0,%eax
                    416:        andl    $~(CR0_CD|CR0_NW),%eax
                    417:        movl    %eax,%cr0
                    418:        invd
                    419: #endif /* CYRIX_CACHE_WORKS */
                    420:
                    421:        jmp     2f
                    422:
                    423: try586:        /* Use the `cpuid' instruction. */
                    424:        xorl    %eax,%eax
                    425:        cpuid
                    426:        movl    %eax,RELOC(cpu_info_primary)+CPU_INFO_LEVEL
                    427:
                    428: 2:
                    429:        /*
                    430:         * Finished with old stack; load new %esp now instead of later so we
                    431:         * can trace this code without having to worry about the trace trap
                    432:         * clobbering the memory test or the zeroing of the bss+bootstrap page
                    433:         * tables.
                    434:         *
                    435:         * The boot program should check:
                    436:         *      text+data <= &stack_variable - more_space_for_stack
                    437:         *      text+data+bss+pad+space_for_page_tables <= end_of_memory
                    438:         * Oops, the gdt is in the carcass of the boot program so clearing
                    439:         * the rest of memory is still not possible.
                    440:         */
                    441:        movl    $_RELOC(tmpstk),%esp    # bootstrap stack end location
                    442:
                    443: /*
                    444:  * Virtual address space of kernel:
                    445:  *
1.30      junyoung  446:  * text | data | bss | [syms] | page dir | proc0 kstack
1.1       fvdl      447:  *                           0          1       2      3
                    448:  */
1.7       thorpej   449: #define        PROC0PDIR       ((0)              * PAGE_SIZE)
                    450: #define        PROC0STACK      ((1)              * PAGE_SIZE)
                    451: #define        SYSMAP          ((1+UPAGES)       * PAGE_SIZE)
                    452: #define        TABLESIZE       ((1+UPAGES) * PAGE_SIZE) /* + nkpde * PAGE_SIZE */
1.1       fvdl      453:
                    454:        /* Find end of kernel image. */
                    455:        movl    $RELOC(end),%edi
1.8       fvdl      456: #if (NKSYMS || defined(DDB) || defined(LKM)) && !defined(SYMTAB_SPACE)
1.1       fvdl      457:        /* Save the symbols (if loaded). */
                    458:        movl    RELOC(esym),%eax
                    459:        testl   %eax,%eax
                    460:        jz      1f
1.35      yamt      461:        subl    $KERNBASE,%eax
1.1       fvdl      462:        movl    %eax,%edi
                    463: 1:
                    464: #endif
                    465:
                    466:        /* Calculate where to start the bootstrap tables. */
                    467:        movl    %edi,%esi                       # edi = esym ? esym : end
                    468:        addl    $PGOFSET,%esi                   # page align up
                    469:        andl    $~PGOFSET,%esi
                    470:
                    471:        /*
                    472:         * Calculate the size of the kernel page table directory, and
1.29      mycroft   473:         * how many entries it will have.  Adjust nkpde to the actual
                    474:         * kernel size automatically.  Account for the bootstrap tables,
                    475:         * round up, and add an extra 4MB.
1.1       fvdl      476:         */
1.29      mycroft   477:        leal    TABLESIZE+NBPD+PDOFSET(%edi),%eax
                    478:        shrl    $PDSHIFT,%eax
1.1       fvdl      479:        movl    RELOC(nkpde),%ecx               # get nkpde
1.29      mycroft   480:        cmpl    %ecx,%eax
                    481:        jb      1f
                    482:        movl    %eax,%ecx
                    483: 1:     cmpl    $NKPTP_MIN,%ecx                 # larger than min?
1.1       fvdl      484:        jge     1f
                    485:        movl    $NKPTP_MIN,%ecx                 # set at min
                    486:        jmp     2f
                    487: 1:     cmpl    $NKPTP_MAX,%ecx                 # larger than max?
                    488:        jle     2f
                    489:        movl    $NKPTP_MAX,%ecx
1.29      mycroft   490: 2:     movl    %ecx,RELOC(nkpde)
1.1       fvdl      491:
                    492:        /* Clear memory for bootstrap tables. */
                    493:        shll    $PGSHIFT,%ecx
                    494:        addl    $TABLESIZE,%ecx
                    495:        addl    %esi,%ecx                       # end of tables
                    496:        subl    %edi,%ecx                       # size of tables
                    497:        shrl    $2,%ecx
                    498:        xorl    %eax,%eax
                    499:        cld
                    500:        rep
                    501:        stosl
                    502:
                    503: /*
                    504:  * fillkpt
                    505:  *     eax = pte (page frame | control | status)
                    506:  *     ebx = page table address
                    507:  *     ecx = number of pages to map
                    508:  */
                    509: #define        fillkpt         \
                    510: 1:     movl    %eax,(%ebx)     ; \
1.7       thorpej   511:        addl    $PAGE_SIZE,%eax ; /* increment physical address */ \
1.1       fvdl      512:        addl    $4,%ebx         ; /* next pte */ \
                    513:        loop    1b              ;
                    514:
                    515: /*
                    516:  * Build initial page tables.
                    517:  */
                    518:        /* Calculate end of text segment, rounded to a page. */
                    519:        leal    (RELOC(etext)+PGOFSET),%edx
                    520:        andl    $~PGOFSET,%edx
1.30      junyoung  521:
1.1       fvdl      522:        /* Skip over the first 1MB. */
                    523:        movl    $_RELOC(KERNTEXTOFF),%eax
                    524:        movl    %eax,%ecx
                    525:        shrl    $PGSHIFT,%ecx
                    526:        leal    (SYSMAP)(%esi,%ecx,4),%ebx
                    527:
                    528:        /* Map the kernel text read-only. */
                    529:        movl    %edx,%ecx
                    530:        subl    %eax,%ecx
                    531:        shrl    $PGSHIFT,%ecx
                    532:        orl     $(PG_V|PG_KR),%eax
                    533:        fillkpt
                    534:
                    535:        /* Map the data, BSS, and bootstrap tables read-write. */
                    536:        leal    (PG_V|PG_KW)(%edx),%eax
                    537:        movl    RELOC(nkpde),%ecx
                    538:        shll    $PGSHIFT,%ecx
                    539:        addl    $TABLESIZE,%ecx
                    540:        addl    %esi,%ecx                               # end of tables
                    541:        subl    %edx,%ecx                               # subtract end of text
                    542:        shrl    $PGSHIFT,%ecx
                    543:        fillkpt
                    544:
                    545:        /* Map ISA I/O memory. */
                    546:        movl    $(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax   # having these bits set
                    547:        movl    $(IOM_SIZE>>PGSHIFT),%ecx               # for this many pte s,
                    548:        fillkpt
                    549:
                    550: /*
                    551:  * Construct a page table directory.
                    552:  */
                    553:        /* Install PDEs for temporary double map of kernel. */
                    554:        movl    RELOC(nkpde),%ecx                       # for this many pde s,
                    555:        leal    (PROC0PDIR+0*4)(%esi),%ebx              # which is where temp maps!
                    556:        leal    (SYSMAP+PG_V|PG_KW)(%esi),%eax          # pte for KPT in proc 0,
                    557:        fillkpt
                    558:
                    559:        /* Map kernel PDEs. */
                    560:        movl    RELOC(nkpde),%ecx                       # for this many pde s,
                    561:        leal    (PROC0PDIR+PDSLOT_KERN*4)(%esi),%ebx    # kernel pde offset
                    562:        leal    (SYSMAP+PG_V|PG_KW)(%esi),%eax          # pte for KPT in proc 0,
                    563:        fillkpt
                    564:
                    565:        /* Install a PDE recursively mapping page directory as a page table! */
                    566:        leal    (PROC0PDIR+PG_V|PG_KW)(%esi),%eax       # pte for ptd
                    567:        movl    %eax,(PROC0PDIR+PDSLOT_PTE*4)(%esi)     # recursive PD slot
                    568:
1.31      junyoung  569:        /* Save phys. addr of PDP, for libkvm. */
                    570:        movl    %esi,RELOC(PDPpaddr)
1.1       fvdl      571:
                    572:        /* Load base of page directory and enable mapping. */
                    573:        movl    %esi,%eax               # phys address of ptd in proc 0
                    574:        movl    %eax,%cr3               # load ptd addr into mmu
                    575:        movl    %cr0,%eax               # get control word
                    576:                                        # enable paging & NPX emulation
                    577:        orl     $(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_EM|CR0_MP),%eax
                    578:        movl    %eax,%cr0               # and let's page NOW!
                    579:
                    580:        pushl   $begin                  # jump to high mem
                    581:        ret
                    582:
                    583: begin:
1.35      yamt      584:        /* Now running relocated at KERNBASE.  Remove double mapping. */
1.1       fvdl      585:        movl    _C_LABEL(nkpde),%ecx            # for this many pde s,
                    586:        leal    (PROC0PDIR+0*4)(%esi),%ebx      # which is where temp maps!
1.35      yamt      587:        addl    $(KERNBASE), %ebx       # now use relocated address
1.1       fvdl      588: 1:     movl    $0,(%ebx)
                    589:        addl    $4,%ebx # next pde
                    590:        loop    1b
                    591:
                    592:        /* Relocate atdevbase. */
                    593:        movl    _C_LABEL(nkpde),%edx
                    594:        shll    $PGSHIFT,%edx
1.35      yamt      595:        addl    $(TABLESIZE+KERNBASE),%edx
1.1       fvdl      596:        addl    %esi,%edx
                    597:        movl    %edx,_C_LABEL(atdevbase)
                    598:
                    599:        /* Set up bootstrap stack. */
1.35      yamt      600:        leal    (PROC0STACK+KERNBASE)(%esi),%eax
1.40      yamt      601:        movl    %eax,_C_LABEL(proc0uarea)
                    602:        leal    (KSTACK_SIZE-FRAMESIZE)(%eax),%esp
                    603:        movl    %esi,(KSTACK_SIZE+PCB_CR3)(%eax)        # pcb->pcb_cr3
1.1       fvdl      604:        xorl    %ebp,%ebp               # mark end of frames
                    605:
1.37      jmmv      606: #if defined(MULTIBOOT)
                    607:        /* It is now safe to parse the Multiboot information structure
                    608:         * we saved before from C code.  Note that we cannot delay its
                    609:         * parsing any more because initgdt (called below) needs to make
                    610:         * use of this information. */
                    611:        call    _C_LABEL(multiboot_post_reloc)
                    612: #endif
                    613:
1.1       fvdl      614:        subl    $NGDT*8, %esp           # space for temporary gdt
                    615:        pushl   %esp
                    616:        call    _C_LABEL(initgdt)
                    617:        addl    $4,%esp
1.30      junyoung  618:
1.1       fvdl      619:        movl    _C_LABEL(nkpde),%eax
                    620:        shll    $PGSHIFT,%eax
                    621:        addl    $TABLESIZE,%eax
                    622:        addl    %esi,%eax               # skip past stack and page tables
                    623:
                    624:        pushl   %eax
                    625:        call    _C_LABEL(init386)       # wire 386 chip for unix operation
                    626:        addl    $4+NGDT*8,%esp          # pop temporary gdt
                    627:
                    628: #ifdef SAFARI_FIFO_HACK
                    629:        movb    $5,%al
                    630:        movw    $0x37b,%dx
                    631:        outb    %al,%dx
                    632:        movw    $0x37f,%dx
                    633:        inb     %dx,%al
                    634:        movb    %al,%cl
                    635:
                    636:        orb     $1,%cl
                    637:
                    638:        movb    $5,%al
                    639:        movw    $0x37b,%dx
                    640:        outb    %al,%dx
                    641:        movw    $0x37f,%dx
                    642:        movb    %cl,%al
                    643:        outb    %al,%dx
                    644: #endif /* SAFARI_FIFO_HACK */
                    645:
                    646:        call    _C_LABEL(main)
                    647:
                    648: /*
                    649:  * void proc_trampoline(void);
                    650:  * This is a trampoline function pushed onto the stack of a newly created
                    651:  * process in order to do some additional setup.  The trampoline is entered by
                    652:  * cpu_switch()ing to the process, so we abuse the callee-saved registers used
                    653:  * by cpu_switch() to store the information about the stub to call.
                    654:  * NOTE: This function does not have a normal calling sequence!
                    655:  */
                    656: /* LINTSTUB: Func: void proc_trampoline(void) */
                    657: NENTRY(proc_trampoline)
                    658: #ifdef MULTIPROCESSOR
                    659:        call    _C_LABEL(proc_trampoline_mp)
                    660: #endif
                    661:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                    662:        pushl   %ebx
                    663:        call    *%esi
                    664:        addl    $4,%esp
1.24      yamt      665:        DO_DEFERRED_SWITCH(%eax)
1.1       fvdl      666:        INTRFASTEXIT
                    667:        /* NOTREACHED */
                    668:
                    669: /*****************************************************************************/
1.16      christos  670: #ifdef COMPAT_16
1.1       fvdl      671: /*
                    672:  * Signal trampoline; copied to top of user stack.
                    673:  */
                    674: /* LINTSTUB: Var: char sigcode[1], esigcode[1]; */
                    675: NENTRY(sigcode)
                    676:        /*
                    677:         * Handler has returned here as if we called it.  The sigcontext
                    678:         * is on the stack after the 3 args "we" pushed.
                    679:         */
                    680:        leal    12(%esp),%eax           # get pointer to sigcontext
                    681:        movl    %eax,4(%esp)            # put it in the argument slot
                    682:                                        # fake return address already there
1.17      christos  683:        movl    $SYS_compat_16___sigreturn14,%eax
1.1       fvdl      684:        int     $0x80                   # enter kernel with args on stack
                    685:        movl    $SYS_exit,%eax
                    686:        int     $0x80                   # exit if sigreturn fails
                    687:        .globl  _C_LABEL(esigcode)
                    688: _C_LABEL(esigcode):
1.16      christos  689: #endif
1.1       fvdl      690:
                    691: /*****************************************************************************/
                    692:
                    693: /*
                    694:  * The following is i386-specific nonsense.
                    695:  */
                    696:
                    697: /*
                    698:  * void lgdt(struct region_descriptor *rdp);
                    699:  * Load a new GDT pointer (and do any necessary cleanup).
                    700:  * XXX It's somewhat questionable whether reloading all the segment registers
                    701:  * is necessary, since the actual descriptor data is not changed except by
                    702:  * process creation and exit, both of which clean up via task switches.  OTOH,
                    703:  * this only happens at run time when the GDT is resized.
                    704:  */
                    705: /* LINTSTUB: Func: void lgdt(struct region_descriptor *rdp) */
                    706: NENTRY(lgdt)
                    707:        /* Reload the descriptor table. */
                    708:        movl    4(%esp),%eax
                    709:        lgdt    (%eax)
                    710:        /* Flush the prefetch queue. */
                    711:        jmp     1f
                    712:        nop
                    713: 1:     /* Reload "stale" selectors. */
                    714:        movl    $GSEL(GDATA_SEL, SEL_KPL),%eax
                    715:        movw    %ax,%ds
                    716:        movw    %ax,%es
                    717:        movw    %ax,%gs
                    718:        movw    %ax,%ss
                    719:        movl    $GSEL(GCPU_SEL, SEL_KPL),%eax
                    720:        movw    %ax,%fs
                    721:        /* Reload code selector by doing intersegment return. */
                    722:        popl    %eax
                    723:        pushl   $GSEL(GCODE_SEL, SEL_KPL)
                    724:        pushl   %eax
                    725:        lret
                    726:
                    727: /*****************************************************************************/
                    728:
                    729: /*
                    730:  * These functions are primarily used by DDB.
                    731:  */
                    732:
                    733: /* LINTSTUB: Func: int setjmp (label_t *l) */
                    734: ENTRY(setjmp)
                    735:        movl    4(%esp),%eax
                    736:        movl    %ebx,(%eax)             # save ebx
                    737:        movl    %esp,4(%eax)            # save esp
                    738:        movl    %ebp,8(%eax)            # save ebp
                    739:        movl    %esi,12(%eax)           # save esi
                    740:        movl    %edi,16(%eax)           # save edi
                    741:        movl    (%esp),%edx             # get rta
                    742:        movl    %edx,20(%eax)           # save eip
                    743:        xorl    %eax,%eax               # return (0);
                    744:        ret
                    745:
                    746: /* LINTSTUB: Func: void longjmp (label_t *l) */
                    747: ENTRY(longjmp)
                    748:        movl    4(%esp),%eax
                    749:        movl    (%eax),%ebx             # restore ebx
                    750:        movl    4(%eax),%esp            # restore esp
                    751:        movl    8(%eax),%ebp            # restore ebp
                    752:        movl    12(%eax),%esi           # restore esi
                    753:        movl    16(%eax),%edi           # restore edi
                    754:        movl    20(%eax),%edx           # get rta
                    755:        movl    %edx,(%esp)             # put in return frame
                    756:        xorl    %eax,%eax               # return (1);
                    757:        incl    %eax
                    758:        ret
                    759:
                    760: /*****************************************************************************/
                    761:
                    762:        .globl  _C_LABEL(sched_whichqs),_C_LABEL(sched_qs)
                    763:        .globl  _C_LABEL(uvmexp),_C_LABEL(panic)
                    764:
                    765: #ifdef DIAGNOSTIC
                    766: NENTRY(switch_error)
                    767:        pushl   $1f
                    768:        call    _C_LABEL(panic)
                    769:        /* NOTREACHED */
                    770: 1:     .asciz  "cpu_switch"
                    771: #endif /* DIAGNOSTIC */
                    772:
                    773: /*
1.5       thorpej   774:  * void cpu_switch(struct lwp *)
1.39      skrll     775:  * Find a runnable lwp and switch to it.  Wait if necessary.  If the new
                    776:  * lwp is the same as the old one, we short-circuit the context save and
1.1       fvdl      777:  * restore.
1.30      junyoung  778:  *
1.1       fvdl      779:  * Note that the stack frame layout is known to "struct switchframe"
1.30      junyoung  780:  * in <machine/frame.h> and to the code in cpu_fork() which initializes
1.5       thorpej   781:  * it for a new lwp.
1.1       fvdl      782:  */
                    783: ENTRY(cpu_switch)
                    784:        pushl   %ebx
                    785:        pushl   %esi
                    786:        pushl   %edi
                    787:
                    788: #ifdef DEBUG
                    789:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                    790:        jae     1f
1.5       thorpej   791:        pushl   $2f
1.1       fvdl      792:        call    _C_LABEL(panic)
                    793:        /* NOTREACHED */
1.5       thorpej   794: 2:     .asciz  "not splsched() in cpu_switch!"
1.30      junyoung  795: 1:
1.1       fvdl      796: #endif /* DEBUG */
1.30      junyoung  797:
1.5       thorpej   798:        movl    16(%esp),%esi           # current
1.1       fvdl      799:
                    800:        /*
1.5       thorpej   801:         * Clear curlwp so that we don't accumulate system time while idle.
                    802:         * This also insures that schedcpu() will move the old lwp to
1.1       fvdl      803:         * the correct queue if it happens to get called from the spllower()
                    804:         * below and changes the priority.  (See corresponding comment in
                    805:         * userret()).
                    806:         */
1.5       thorpej   807:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl      808:        /*
1.5       thorpej   809:         * First phase: find new lwp.
1.1       fvdl      810:         *
                    811:         * Registers:
                    812:         *   %eax - queue head, scratch, then zero
                    813:         *   %ebx - queue number
                    814:         *   %ecx - cached value of whichqs
1.5       thorpej   815:         *   %edx - next lwp in queue
                    816:         *   %esi - old lwp
                    817:         *   %edi - new lwp
1.1       fvdl      818:         */
                    819:
1.5       thorpej   820:        /* Look for new lwp. */
1.1       fvdl      821:        cli                             # splhigh doesn't do a cli
                    822:        movl    _C_LABEL(sched_whichqs),%ecx
                    823:        bsfl    %ecx,%ebx               # find a full q
                    824:        jnz     switch_dequeue
                    825:
                    826:        /*
                    827:         * idling:      save old context.
                    828:         *
                    829:         * Registers:
                    830:         *   %eax, %ecx - scratch
1.5       thorpej   831:         *   %esi - old lwp, then old pcb
1.1       fvdl      832:         *   %edi - idle pcb
                    833:         */
                    834:
                    835:        pushl   %esi
1.24      yamt      836:        call    _C_LABEL(pmap_deactivate2)      # pmap_deactivate(oldproc)
1.1       fvdl      837:        addl    $4,%esp
                    838:
1.5       thorpej   839:        movl    L_ADDR(%esi),%esi
1.1       fvdl      840:
                    841:        /* Save stack pointers. */
                    842:        movl    %esp,PCB_ESP(%esi)
                    843:        movl    %ebp,PCB_EBP(%esi)
                    844:
                    845:        /* Find idle PCB for this CPU */
                    846: #ifndef MULTIPROCESSOR
1.5       thorpej   847:        movl    $_C_LABEL(lwp0),%ebx
                    848:        movl    L_ADDR(%ebx),%edi
                    849:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl      850: #else
                    851:        movl    CPUVAR(IDLE_PCB),%edi
                    852:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                    853: #endif
1.5       thorpej   854:        movl    $0,CPUVAR(CURLWP)               /* In case we fault... */
1.1       fvdl      855:
                    856:        /* Restore the idle context (avoid interrupts) */
                    857:        cli
                    858:
                    859:        /* Restore stack pointers. */
                    860:        movl    PCB_ESP(%edi),%esp
                    861:        movl    PCB_EBP(%edi),%ebp
                    862:
                    863:        /* Switch TSS. Reset "task busy" flag before loading. */
1.26      yamt      864:        movl    %cr3,%eax
                    865:        movl    %eax,PCB_CR3(%edi)
1.1       fvdl      866: #ifdef MULTIPROCESSOR
                    867:        movl    CPUVAR(GDT),%eax
                    868: #else
                    869:        movl    _C_LABEL(gdt),%eax
                    870: #endif
                    871:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                    872:        ltr     %dx
                    873:
                    874:        /* We're always in the kernel, so we don't need the LDT. */
                    875:
                    876:        /* Restore cr0 (including FPU state). */
                    877:        movl    PCB_CR0(%edi),%ecx
                    878:        movl    %ecx,%cr0
                    879:
                    880:        /* Record new pcb. */
                    881:        SET_CURPCB(%edi)
                    882:
                    883:        xorl    %esi,%esi
                    884:        sti
1.30      junyoung  885: idle_unlock:
                    886: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl      887:        call    _C_LABEL(sched_unlock_idle)
                    888: #endif
                    889:        /* Interrupts are okay again. */
1.2       fvdl      890:        pushl   $IPL_NONE               # spl0()
1.1       fvdl      891:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl      892:        addl    $4,%esp
1.1       fvdl      893:        jmp     idle_start
1.30      junyoung  894: idle_zero:
1.1       fvdl      895:        sti
                    896:        call    _C_LABEL(uvm_pageidlezero)
                    897:        cli
                    898:        cmpl    $0,_C_LABEL(sched_whichqs)
                    899:        jnz     idle_exit
                    900: idle_loop:
                    901:        /* Try to zero some pages. */
                    902:        movl    _C_LABEL(uvm)+UVM_PAGE_IDLE_ZERO,%ecx
                    903:        testl   %ecx,%ecx
                    904:        jnz     idle_zero
                    905:        sti
                    906:        hlt
                    907: NENTRY(mpidle)
1.30      junyoung  908: idle_start:
1.1       fvdl      909:        cli
                    910:        cmpl    $0,_C_LABEL(sched_whichqs)
                    911:        jz      idle_loop
1.30      junyoung  912: idle_exit:
1.1       fvdl      913:        movl    $IPL_HIGH,CPUVAR(ILEVEL)                # splhigh
1.14      fvdl      914:        sti
1.30      junyoung  915: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl      916:        call    _C_LABEL(sched_lock_idle)
                    917: #endif
                    918:        movl    _C_LABEL(sched_whichqs),%ecx
                    919:        bsfl    %ecx,%ebx
                    920:        jz      idle_unlock
                    921:
1.30      junyoung  922: switch_dequeue:
                    923:        /*
1.1       fvdl      924:         * we're running at splhigh(), but it's otherwise okay to take
1.30      junyoung  925:         * interrupts here.
1.1       fvdl      926:         */
                    927:        sti
                    928:        leal    _C_LABEL(sched_qs)(,%ebx,8),%eax # select q
                    929:
1.5       thorpej   930:        movl    L_FORW(%eax),%edi       # unlink from front of process q
1.1       fvdl      931: #ifdef DIAGNOSTIC
                    932:        cmpl    %edi,%eax               # linked to self (i.e. nothing queued)?
                    933:        je      _C_LABEL(switch_error)  # not possible
                    934: #endif /* DIAGNOSTIC */
1.5       thorpej   935:        movl    L_FORW(%edi),%edx
                    936:        movl    %edx,L_FORW(%eax)
                    937:        movl    %eax,L_BACK(%edx)
1.1       fvdl      938:
                    939:        cmpl    %edx,%eax               # q empty?
                    940:        jne     3f
                    941:
                    942:        btrl    %ebx,%ecx               # yes, clear to indicate empty
                    943:        movl    %ecx,_C_LABEL(sched_whichqs) # update q status
                    944:
                    945: 3:     /* We just did it. */
                    946:        xorl    %eax,%eax
                    947:        CLEAR_RESCHED(%eax)
                    948:
1.5       thorpej   949: switch_resume:
1.1       fvdl      950: #ifdef DIAGNOSTIC
1.5       thorpej   951:        cmpl    %eax,L_WCHAN(%edi)      # Waiting for something?
1.1       fvdl      952:        jne     _C_LABEL(switch_error)  # Yes; shouldn't be queued.
1.5       thorpej   953:        cmpb    $LSRUN,L_STAT(%edi)     # In run state?
1.1       fvdl      954:        jne     _C_LABEL(switch_error)  # No; shouldn't be queued.
                    955: #endif /* DIAGNOSTIC */
                    956:
1.5       thorpej   957:        /* Isolate lwp.  XXX Is this necessary? */
                    958:        movl    %eax,L_BACK(%edi)
1.1       fvdl      959:
1.5       thorpej   960:        /* Record new lwp. */
                    961:        movb    $LSONPROC,L_STAT(%edi)  # l->l_stat = LSONPROC
                    962:        SET_CURLWP(%edi,%ecx)
1.1       fvdl      963:
1.5       thorpej   964:        /* Skip context switch if same lwp. */
1.10      fvdl      965:        xorl    %ebx,%ebx
1.1       fvdl      966:        cmpl    %edi,%esi
                    967:        je      switch_return
                    968:
1.5       thorpej   969:        /* If old lwp exited, don't bother. */
1.1       fvdl      970:        testl   %esi,%esi
                    971:        jz      switch_exited
                    972:
                    973:        /*
                    974:         * Second phase: save old context.
                    975:         *
                    976:         * Registers:
                    977:         *   %eax, %ecx - scratch
1.5       thorpej   978:         *   %esi - old lwp, then old pcb
                    979:         *   %edi - new lwp
1.1       fvdl      980:         */
                    981:
                    982:        pushl   %esi
1.24      yamt      983:        call    _C_LABEL(pmap_deactivate2)      # pmap_deactivate(oldproc)
1.1       fvdl      984:        addl    $4,%esp
                    985:
1.5       thorpej   986:        movl    L_ADDR(%esi),%esi
1.1       fvdl      987:
                    988:        /* Save stack pointers. */
                    989:        movl    %esp,PCB_ESP(%esi)
                    990:        movl    %ebp,PCB_EBP(%esi)
                    991:
                    992: switch_exited:
                    993:        /*
                    994:         * Third phase: restore saved context.
                    995:         *
                    996:         * Registers:
                    997:         *   %eax, %ebx, %ecx, %edx - scratch
                    998:         *   %esi - new pcb
1.5       thorpej   999:         *   %edi - new lwp
1.1       fvdl     1000:         */
                   1001:
                   1002:        /* No interrupts while loading new state. */
                   1003:        cli
1.5       thorpej  1004:        movl    L_ADDR(%edi),%esi
1.1       fvdl     1005:
                   1006:        /* Restore stack pointers. */
                   1007:        movl    PCB_ESP(%esi),%esp
                   1008:        movl    PCB_EBP(%esi),%ebp
                   1009:
                   1010: #if 0
                   1011:        /* Don't bother with the rest if switching to a system process. */
1.5       thorpej  1012:        testl   $P_SYSTEM,L_FLAG(%edi); XXX NJWLWP lwp's don't have P_SYSTEM!
1.1       fvdl     1013:        jnz     switch_restored
                   1014: #endif
                   1015:
1.26      yamt     1016:        /* Switch TSS. Reset "task busy" flag before loading. */
                   1017:        movl    %cr3,%eax
                   1018:        movl    %eax,PCB_CR3(%esi) /* XXX should be done by pmap_activate? */
1.1       fvdl     1019: #ifdef MULTIPROCESSOR
                   1020:        movl    CPUVAR(GDT),%eax
1.30      junyoung 1021: #else
1.1       fvdl     1022:        /* Load TSS info. */
                   1023:        movl    _C_LABEL(gdt),%eax
                   1024: #endif
1.5       thorpej  1025:        movl    L_MD_TSS_SEL(%edi),%edx
1.1       fvdl     1026:
                   1027:        andl    $~0x0200,4(%eax,%edx, 1)
                   1028:        ltr     %dx
                   1029:
                   1030:        pushl   %edi
                   1031:        call    _C_LABEL(pmap_activate)         # pmap_activate(p)
                   1032:        addl    $4,%esp
                   1033:
                   1034: #if 0
                   1035: switch_restored:
                   1036: #endif
                   1037:        /* Restore cr0 (including FPU state). */
                   1038:        movl    PCB_CR0(%esi),%ecx
                   1039: #ifdef MULTIPROCESSOR
1.30      junyoung 1040:        /*
1.22      wiz      1041:         * If our floating point registers are on a different CPU,
1.1       fvdl     1042:         * clear CR0_TS so we'll trap rather than reuse bogus state.
                   1043:         */
                   1044:        movl    PCB_FPCPU(%esi),%ebx
                   1045:        cmpl    CPUVAR(SELF),%ebx
                   1046:        jz      1f
                   1047:        orl     $CR0_TS,%ecx
1.30      junyoung 1048: 1:
                   1049: #endif
1.1       fvdl     1050:        movl    %ecx,%cr0
                   1051:
                   1052:        /* Record new pcb. */
                   1053:        SET_CURPCB(%esi)
                   1054:
                   1055:        /* Interrupts are okay again. */
                   1056:        sti
                   1057:
                   1058: /*
                   1059:  *  Check for restartable atomic sequences (RAS)
                   1060:  */
1.5       thorpej  1061:        movl    CPUVAR(CURLWP),%edi
                   1062:        movl    L_PROC(%edi),%esi
1.20      dsl      1063:        cmpl    $0,P_RASLIST(%esi)
                   1064:        jne     2f
1.1       fvdl     1065: 1:
1.10      fvdl     1066:        movl    $1,%ebx
1.1       fvdl     1067:
                   1068: switch_return:
1.30      junyoung 1069: #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
1.1       fvdl     1070:        call    _C_LABEL(sched_unlock_idle)
                   1071: #endif
1.28      yamt     1072:        cmpl    $0,CPUVAR(IPENDING)
                   1073:        jz      3f
1.2       fvdl     1074:        pushl   $IPL_NONE               # spl0()
1.1       fvdl     1075:        call    _C_LABEL(Xspllower)     # process pending interrupts
1.2       fvdl     1076:        addl    $4,%esp
1.28      yamt     1077: 3:
1.1       fvdl     1078:        movl    $IPL_HIGH,CPUVAR(ILEVEL)        # splhigh()
1.9       fvdl     1079:
                   1080:        movl    %ebx,%eax
1.30      junyoung 1081:
1.1       fvdl     1082:        popl    %edi
                   1083:        popl    %esi
                   1084:        popl    %ebx
                   1085:        ret
1.20      dsl      1086:
                   1087: 2:                                     # check RAS list
                   1088:        movl    L_MD_REGS(%edi),%ebx
                   1089:        movl    TF_EIP(%ebx),%eax
                   1090:        pushl   %eax
                   1091:        pushl   %esi
                   1092:        call    _C_LABEL(ras_lookup)
                   1093:        addl    $8,%esp
                   1094:        cmpl    $-1,%eax
                   1095:        je      1b
                   1096:        movl    %eax,TF_EIP(%ebx)
                   1097:        jmp     1b
1.1       fvdl     1098:
                   1099: /*
1.5       thorpej  1100:  * void cpu_switchto(struct lwp *current, struct lwp *next)
                   1101:  * Switch to the specified next LWP.
                   1102:  */
                   1103: ENTRY(cpu_switchto)
                   1104:        pushl   %ebx
                   1105:        pushl   %esi
                   1106:        pushl   %edi
                   1107:
                   1108: #ifdef DEBUG
                   1109:        cmpl    $IPL_SCHED,CPUVAR(ILEVEL)
                   1110:        jae     1f
                   1111:        pushl   $2f
                   1112:        call    _C_LABEL(panic)
                   1113:        /* NOTREACHED */
                   1114: 2:     .asciz  "not splsched() in cpu_switchto!"
                   1115: 1:
                   1116: #endif /* DEBUG */
                   1117:
                   1118:        movl    16(%esp),%esi           # current
                   1119:        movl    20(%esp),%edi           # next
                   1120:
                   1121:        /*
                   1122:         * Clear curlwp so that we don't accumulate system time while idle.
                   1123:         * This also insures that schedcpu() will move the old process to
                   1124:         * the correct queue if it happens to get called from the spllower()
                   1125:         * below and changes the priority.  (See corresponding comment in
                   1126:         * usrret()).
                   1127:         *
                   1128:         * XXX Is this necessary?  We know we won't go idle.
                   1129:         */
                   1130:        movl    $0,CPUVAR(CURLWP)
                   1131:
                   1132:        /*
                   1133:         * We're running at splhigh(), but it's otherwise okay to take
                   1134:         * interrupts here.
                   1135:         */
                   1136:        sti
                   1137:
                   1138:        /* Jump into the middle of cpu_switch */
                   1139:        xorl    %eax,%eax
                   1140:        jmp     switch_resume
                   1141:
                   1142: /*
1.21      jdolecek 1143:  * void cpu_exit(struct lwp *l)
1.30      junyoung 1144:  * Switch to the appropriate idle context (lwp0's if uniprocessor; the CPU's
                   1145:  * if multiprocessor) and deallocate the address space and kernel stack for p.
1.1       fvdl     1146:  * Then jump into cpu_switch(), as if we were in the idle proc all along.
                   1147:  */
                   1148: #ifndef MULTIPROCESSOR
1.5       thorpej  1149:        .globl  _C_LABEL(lwp0)
1.1       fvdl     1150: #endif
1.21      jdolecek 1151: /* LINTSTUB: Func: void cpu_exit(struct lwp *l) */
                   1152: ENTRY(cpu_exit)
1.1       fvdl     1153:        movl    4(%esp),%edi            # old process
                   1154: #ifndef MULTIPROCESSOR
1.5       thorpej  1155:        movl    $_C_LABEL(lwp0),%ebx
                   1156:        movl    L_ADDR(%ebx),%esi
                   1157:        movl    L_MD_TSS_SEL(%ebx),%edx
1.1       fvdl     1158: #else
                   1159:        movl    CPUVAR(IDLE_PCB),%esi
                   1160:        movl    CPUVAR(IDLE_TSS_SEL),%edx
                   1161: #endif
                   1162:        /* In case we fault... */
1.5       thorpej  1163:        movl    $0,CPUVAR(CURLWP)
1.1       fvdl     1164:
                   1165:        /* Restore the idle context. */
                   1166:        cli
                   1167:
                   1168:        /* Restore stack pointers. */
                   1169:        movl    PCB_ESP(%esi),%esp
                   1170:        movl    PCB_EBP(%esi),%ebp
                   1171:
1.26      yamt     1172:        /* Switch TSS. Reset "task busy" flag before loading. */
                   1173:        movl    %cr3,%eax
                   1174:        movl    %eax,PCB_CR3(%esi)
1.1       fvdl     1175: #ifdef MULTIPROCESSOR
                   1176:        movl    CPUVAR(GDT),%eax
1.30      junyoung 1177: #else
1.1       fvdl     1178:        /* Load TSS info. */
                   1179:        movl    _C_LABEL(gdt),%eax
                   1180: #endif
                   1181:
                   1182:        andl    $~0x0200,4-SEL_KPL(%eax,%edx,1)
                   1183:        ltr     %dx
                   1184:
                   1185:        /* We're always in the kernel, so we don't need the LDT. */
                   1186:
                   1187:        /* Restore cr0 (including FPU state). */
                   1188:        movl    PCB_CR0(%esi),%ecx
                   1189:        movl    %ecx,%cr0
                   1190:
                   1191:        /* Record new pcb. */
                   1192:        SET_CURPCB(%esi)
                   1193:
                   1194:        /* Interrupts are okay again. */
                   1195:        sti
                   1196:
                   1197:        /*
1.21      jdolecek 1198:         * Schedule the dead LWP's stack to be freed.
1.1       fvdl     1199:         */
1.21      jdolecek 1200:        pushl   %edi
                   1201:        call    _C_LABEL(lwp_exit2)
1.1       fvdl     1202:        addl    $4,%esp
                   1203:
                   1204:        /* Jump into cpu_switch() with the right state. */
                   1205:        xorl    %esi,%esi
1.5       thorpej  1206:        movl    %esi,CPUVAR(CURLWP)
1.1       fvdl     1207:        jmp     idle_start
                   1208:
                   1209: /*
                   1210:  * void savectx(struct pcb *pcb);
                   1211:  * Update pcb, saving current processor state.
                   1212:  */
                   1213: /* LINTSTUB: Func: void savectx(struct pcb *pcb) */
                   1214: ENTRY(savectx)
                   1215:        movl    4(%esp),%edx            # edx = p->p_addr
1.30      junyoung 1216:
1.1       fvdl     1217:        /* Save stack pointers. */
                   1218:        movl    %esp,PCB_ESP(%edx)
                   1219:        movl    %ebp,PCB_EBP(%edx)
                   1220:
                   1221:        ret
                   1222:
                   1223: /*
                   1224:  * Old call gate entry for syscall
                   1225:  */
                   1226: /* LINTSTUB: Var: char Xosyscall[1]; */
                   1227: IDTVEC(osyscall)
                   1228:        /* Set eflags in trap frame. */
                   1229:        pushfl
                   1230:        popl    8(%esp)
                   1231:        pushl   $7              # size of instruction for restart
                   1232:        jmp     syscall1
                   1233:
                   1234: /*
                   1235:  * Trap gate entry for syscall
                   1236:  */
                   1237: /* LINTSTUB: Var: char Xsyscall[1]; */
                   1238: IDTVEC(syscall)
                   1239:        pushl   $2              # size of instruction for restart
                   1240: syscall1:
                   1241:        pushl   $T_ASTFLT       # trap # for doing ASTs
                   1242:        INTRENTRY
                   1243:
                   1244: #ifdef DIAGNOSTIC
1.24      yamt     1245:        cmpl    $0, CPUVAR(WANT_PMAPLOAD)
                   1246:        jz      1f
                   1247:        pushl   $6f
                   1248:        call    _C_LABEL(printf)
                   1249:        addl    $4, %esp
                   1250: 1:
1.1       fvdl     1251:        movl    CPUVAR(ILEVEL),%ebx
                   1252:        testl   %ebx,%ebx
                   1253:        jz      1f
                   1254:        pushl   $5f
                   1255:        call    _C_LABEL(printf)
                   1256:        addl    $4,%esp
                   1257: #ifdef DDB
                   1258:        int     $3
                   1259: #endif
1.30      junyoung 1260: 1:
1.1       fvdl     1261: #endif /* DIAGNOSTIC */
1.5       thorpej  1262:        movl    CPUVAR(CURLWP),%edx
                   1263:        movl    %esp,L_MD_REGS(%edx)    # save pointer to frame
                   1264:        movl    L_PROC(%edx),%edx
1.15      fvdl     1265:        pushl   %esp
1.1       fvdl     1266:        call    *P_MD_SYSCALL(%edx)     # get pointer to syscall() function
1.15      fvdl     1267:        addl    $4,%esp
1.27      yamt     1268: .Lsyscall_checkast:
1.24      yamt     1269:        /* Check for ASTs on exit to user mode. */
1.1       fvdl     1270:        cli
1.5       thorpej  1271:        CHECK_ASTPENDING(%eax)
1.1       fvdl     1272:        je      1f
                   1273:        /* Always returning to user mode here. */
1.5       thorpej  1274:        CLEAR_ASTPENDING(%eax)
1.1       fvdl     1275:        sti
                   1276:        /* Pushed T_ASTFLT into tf_trapno on entry. */
1.15      fvdl     1277:        pushl   %esp
1.1       fvdl     1278:        call    _C_LABEL(trap)
1.15      fvdl     1279:        addl    $4,%esp
1.27      yamt     1280:        jmp     .Lsyscall_checkast      /* re-check ASTs */
1.24      yamt     1281: 1:     CHECK_DEFERRED_SWITCH(%eax)
                   1282:        jnz     9f
1.1       fvdl     1283: #ifndef DIAGNOSTIC
1.24      yamt     1284:        INTRFASTEXIT
1.1       fvdl     1285: #else /* DIAGNOSTIC */
1.24      yamt     1286:        cmpl    $IPL_NONE,CPUVAR(ILEVEL)
1.1       fvdl     1287:        jne     3f
                   1288:        INTRFASTEXIT
                   1289: 3:     sti
                   1290:        pushl   $4f
                   1291:        call    _C_LABEL(printf)
                   1292:        addl    $4,%esp
                   1293: #ifdef DDB
                   1294:        int     $3
                   1295: #endif /* DDB */
                   1296:        movl    $IPL_NONE,CPUVAR(ILEVEL)
                   1297:        jmp     2b
                   1298: 4:     .asciz  "WARNING: SPL NOT LOWERED ON SYSCALL EXIT\n"
1.30      junyoung 1299: 5:     .asciz  "WARNING: SPL NOT ZERO ON SYSCALL ENTRY\n"
                   1300: 6:     .asciz  "WARNING: WANT PMAPLOAD ON SYSCALL ENTRY\n"
1.1       fvdl     1301: #endif /* DIAGNOSTIC */
1.24      yamt     1302: 9:     sti
                   1303:        call    _C_LABEL(pmap_load)
1.27      yamt     1304:        jmp     .Lsyscall_checkast      /* re-check ASTs */
1.1       fvdl     1305:
                   1306: #if NNPX > 0
                   1307: /*
                   1308:  * Special interrupt handlers.  Someday intr0-intr15 will be used to count
                   1309:  * interrupts.  We'll still need a special exception 16 handler.  The busy
                   1310:  * latch stuff in probintr() can be moved to npxprobe().
                   1311:  */
                   1312:
                   1313: /* LINTSTUB: Func: void probeintr(void) */
                   1314: NENTRY(probeintr)
                   1315:        ss
                   1316:        incl    _C_LABEL(npx_intrs_while_probing)
                   1317:        pushl   %eax
                   1318:        movb    $0x20,%al       # EOI (asm in strings loses cpp features)
                   1319:        outb    %al,$0xa0       # IO_ICU2
                   1320:        outb    %al,$0x20       # IO_ICU1
                   1321:        movb    $0,%al
                   1322:        outb    %al,$0xf0       # clear BUSY# latch
                   1323:        popl    %eax
                   1324:        iret
                   1325:
                   1326: /* LINTSTUB: Func: void probetrap(void) */
                   1327: NENTRY(probetrap)
                   1328:        ss
                   1329:        incl    _C_LABEL(npx_traps_while_probing)
                   1330:        fnclex
                   1331:        iret
                   1332:
                   1333: /* LINTSTUB: Func: int npx586bug1(int a, int b) */
                   1334: NENTRY(npx586bug1)
                   1335:        fildl   4(%esp)         # x
                   1336:        fildl   8(%esp)         # y
                   1337:        fld     %st(1)
                   1338:        fdiv    %st(1),%st      # x/y
                   1339:        fmulp   %st,%st(1)      # (x/y)*y
                   1340:        fsubrp  %st,%st(1)      # x-(x/y)*y
                   1341:        pushl   $0
                   1342:        fistpl  (%esp)
                   1343:        popl    %eax
                   1344:        ret
                   1345: #endif /* NNPX > 0 */

CVSweb <webmaster@jp.NetBSD.org>