[BACK]Return to machdep.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / arch / i386 / i386

Annotation of src/sys/arch/i386/i386/machdep.c, Revision 1.586.2.1

1.586.2.1! tron        1: /*     $NetBSD$        */
1.231     thorpej     2:
                      3: /*-
1.571     jmmv        4:  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006 The NetBSD Foundation, Inc.
1.231     thorpej     5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
1.571     jmmv        8:  * by Charles M. Hannum, by Jason R. Thorpe of the Numerical Aerospace
                      9:  * Simulation Facility, NASA Ames Research Center and by Julio M. Merino Vidal.
1.231     thorpej    10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the NetBSD
                     22:  *     Foundation, Inc. and its contributors.
                     23:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     24:  *    contributors may be used to endorse or promote products derived
                     25:  *    from this software without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     28:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     29:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     30:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     31:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     32:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     33:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     34:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     35:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     36:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     37:  * POSSIBILITY OF SUCH DAMAGE.
                     38:  */
1.125     cgd        39:
1.1       cgd        40: /*-
                     41:  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
                     42:  * All rights reserved.
                     43:  *
                     44:  * This code is derived from software contributed to Berkeley by
                     45:  * William Jolitz.
                     46:  *
                     47:  * Redistribution and use in source and binary forms, with or without
                     48:  * modification, are permitted provided that the following conditions
                     49:  * are met:
                     50:  * 1. Redistributions of source code must retain the above copyright
                     51:  *    notice, this list of conditions and the following disclaimer.
                     52:  * 2. Redistributions in binary form must reproduce the above copyright
                     53:  *    notice, this list of conditions and the following disclaimer in the
                     54:  *    documentation and/or other materials provided with the distribution.
1.528     agc        55:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        56:  *    may be used to endorse or promote products derived from this software
                     57:  *    without specific prior written permission.
                     58:  *
                     59:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     60:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     61:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     62:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     63:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     64:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     65:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     66:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     67:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     68:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     69:  * SUCH DAMAGE.
                     70:  *
1.125     cgd        71:  *     @(#)machdep.c   7.4 (Berkeley) 6/3/91
1.1       cgd        72:  */
1.460     lukem      73:
                     74: #include <sys/cdefs.h>
1.586.2.1! tron       75: __KERNEL_RCSID(0, "$NetBSD$");
1.271     thorpej    76:
1.538     christos   77: #include "opt_beep.h"
                     78: #include "opt_compat_ibcs2.h"
                     79: #include "opt_compat_mach.h"   /* need to get the right segment def */
                     80: #include "opt_compat_netbsd.h"
                     81: #include "opt_compat_svr4.h"
                     82: #include "opt_cpureset_delay.h"
1.271     thorpej    83: #include "opt_cputype.h"
1.309     jonathan   84: #include "opt_ddb.h"
1.377     ws         85: #include "opt_ipkdb.h"
1.443     lukem      86: #include "opt_kgdb.h"
1.455     fvdl       87: #include "opt_mtrr.h"
1.523     martin     88: #include "opt_multiprocessor.h"
1.538     christos   89: #include "opt_realmem.h"
                     90: #include "opt_user_ldt.h"
                     91: #include "opt_vm86.h"
1.1       cgd        92:
1.59      mycroft    93: #include <sys/param.h>
                     94: #include <sys/systm.h>
1.508     thorpej    95: #include <sys/signal.h>
1.59      mycroft    96: #include <sys/signalvar.h>
                     97: #include <sys/kernel.h>
                     98: #include <sys/proc.h>
                     99: #include <sys/user.h>
                    100: #include <sys/exec.h>
                    101: #include <sys/buf.h>
                    102: #include <sys/reboot.h>
                    103: #include <sys/conf.h>
                    104: #include <sys/file.h>
                    105: #include <sys/malloc.h>
                    106: #include <sys/mbuf.h>
                    107: #include <sys/msgbuf.h>
                    108: #include <sys/mount.h>
                    109: #include <sys/vnode.h>
1.204     thorpej   110: #include <sys/extent.h>
1.123     cgd       111: #include <sys/syscallargs.h>
1.291     thorpej   112: #include <sys/core.h>
                    113: #include <sys/kcore.h>
1.508     thorpej   114: #include <sys/ucontext.h>
1.519     nathanw   115: #include <sys/ras.h>
1.508     thorpej   116: #include <sys/sa.h>
                    117: #include <sys/savar.h>
1.522     ragge     118: #include <sys/ksyms.h>
1.57      cgd       119:
1.377     ws        120: #ifdef IPKDB
                    121: #include <ipkdb/ipkdb.h>
                    122: #endif
                    123:
1.235     thorpej   124: #ifdef KGDB
                    125: #include <sys/kgdb.h>
                    126: #endif
                    127:
1.104     cgd       128: #include <dev/cons.h>
1.390     mrg       129:
                    130: #include <uvm/uvm_extern.h>
1.393     fvdl      131: #include <uvm/uvm_page.h>
1.284     mrg       132:
1.200     christos  133: #include <sys/sysctl.h>
                    134:
1.59      mycroft   135: #include <machine/cpu.h>
                    136: #include <machine/cpufunc.h>
1.484     fvdl      137: #include <machine/cpuvar.h>
1.178     mycroft   138: #include <machine/gdt.h>
1.574     kardel    139: #include <machine/kcore.h>
1.149     mycroft   140: #include <machine/pio.h>
1.59      mycroft   141: #include <machine/psl.h>
                    142: #include <machine/reg.h>
                    143: #include <machine/specialreg.h>
1.255     drochner  144: #include <machine/bootinfo.h>
1.455     fvdl      145: #include <machine/mtrr.h>
1.574     kardel    146: #include <x86/x86/tsc.h>
                    147:
1.570     jmmv      148: #include <machine/multiboot.h>
1.43      brezak    149:
1.146     cgd       150: #include <dev/isa/isareg.h>
1.372     drochner  151: #include <machine/isa_machdep.h>
1.164     cgd       152: #include <dev/ic/i8042reg.h>
1.43      brezak    153:
1.200     christos  154: #ifdef DDB
                    155: #include <machine/db_machdep.h>
                    156: #include <ddb/db_extern.h>
                    157: #endif
                    158:
1.184     mycroft   159: #ifdef VM86
                    160: #include <machine/vm86.h>
                    161: #endif
                    162:
1.473     tshiozak  163: #include "acpi.h"
1.576     christos  164: #include "apmbios.h"
1.258     jtk       165: #include "bioscall.h"
1.207     jtk       166:
1.259     jtk       167: #if NBIOSCALL > 0
                    168: #include <machine/bioscall.h>
                    169: #endif
                    170:
1.473     tshiozak  171: #if NACPI > 0
                    172: #include <dev/acpi/acpivar.h>
                    173: #define ACPI_MACHDEP_PRIVATE
                    174: #include <machine/acpi_machdep.h>
                    175: #endif
                    176:
1.576     christos  177: #if NAPMBIOS > 0
1.207     jtk       178: #include <machine/apmvar.h>
1.258     jtk       179: #endif
                    180:
1.59      mycroft   181: #include "isa.h"
1.231     thorpej   182: #include "isadma.h"
1.59      mycroft   183: #include "npx.h"
1.522     ragge     184: #include "ksyms.h"
1.2       cgd       185:
1.384     jdolecek  186: #include "mca.h"
                    187: #if NMCA > 0
                    188: #include <machine/mca_machdep.h>       /* for mca_busprobe() */
                    189: #endif
                    190:
1.484     fvdl      191: #ifdef MULTIPROCESSOR          /* XXX */
                    192: #include <machine/mpbiosvar.h> /* XXX */
                    193: #endif                         /* XXX */
                    194:
1.517     jmmv      195: #ifndef BEEP_ONHALT_COUNT
                    196: #define BEEP_ONHALT_COUNT 3
                    197: #endif
                    198: #ifndef BEEP_ONHALT_PITCH
                    199: #define BEEP_ONHALT_PITCH 1500
                    200: #endif
                    201: #ifndef BEEP_ONHALT_PERIOD
                    202: #define BEEP_ONHALT_PERIOD 250
                    203: #endif
                    204:
1.104     cgd       205: /* the following is used externally (sysctl_hw) */
1.549     wiz       206: char machine[] = "i386";               /* CPU "architecture" */
1.232     veego     207: char machine_arch[] = "i386";          /* machine == machine_arch */
1.104     cgd       208:
1.559     thorpej   209: extern struct bi_devmatch *x86_alldisks;
                    210: extern int x86_ndisks;
1.342     fvdl      211:
1.328     bouyer    212: #ifdef CPURESET_DELAY
                    213: int    cpureset_delay = CPURESET_DELAY;
                    214: #else
                    215: int     cpureset_delay = 2000; /* default to 2s */
                    216: #endif
                    217:
1.455     fvdl      218: #ifdef MTRR
1.513     fvdl      219: struct mtrr_funcs *mtrr_funcs;
1.455     fvdl      220: #endif
                    221:
1.515     fvdl      222: #ifdef COMPAT_NOMID
1.566     christos  223: static int exec_nomid(struct lwp *, struct exec_package *);
1.550     junyoung  224: #endif
1.515     fvdl      225:
1.59      mycroft   226: int    physmem;
1.163     cgd       227: int    dumpmem_low;
                    228: int    dumpmem_high;
1.524     drochner  229: unsigned int cpu_feature;
1.553     lukem     230: unsigned int cpu_feature2;
1.59      mycroft   231: int    cpu_class;
1.428     fvdl      232: int    i386_fpu_present;
                    233: int    i386_fpu_exception;
                    234: int    i386_fpu_fdivbug;
1.59      mycroft   235:
1.451     thorpej   236: int    i386_use_fxsave;
                    237: int    i386_has_sse;
                    238: int    i386_has_sse2;
1.450     thorpej   239:
1.461     christos  240: int    tmx86_has_longrun;
                    241:
1.314     thorpej   242: vaddr_t        msgbuf_vaddr;
1.586.2.1! tron      243: struct {
        !           244:        paddr_t paddr;
        !           245:        psize_t sz;
        !           246: } msgbuf_p_seg[VM_PHYSSEG_MAX];
        !           247: unsigned int msgbuf_p_cnt = 0;
1.314     thorpej   248:
                    249: vaddr_t        idt_vaddr;
                    250: paddr_t        idt_paddr;
                    251:
1.264     mycroft   252: #ifdef I586_CPU
1.314     thorpej   253: vaddr_t        pentium_idt_vaddr;
1.264     mycroft   254: #endif
1.59      mycroft   255:
1.444     chs       256: struct vm_map *exec_map = NULL;
                    257: struct vm_map *mb_map = NULL;
                    258: struct vm_map *phys_map = NULL;
1.48      brezak    259:
1.314     thorpej   260: extern paddr_t avail_start, avail_end;
1.1       cgd       261:
1.542     junyoung  262: void (*delay_func)(int) = i8254_delay;
                    263: void (*initclock_func)(void) = i8254_initclocks;
1.484     fvdl      264:
1.204     thorpej   265: /*
1.291     thorpej   266:  * Size of memory segments, before any memory is stolen.
                    267:  */
                    268: phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
                    269: int    mem_cluster_cnt;
                    270:
1.542     junyoung  271: int    cpu_dump(void);
                    272: int    cpu_dumpsize(void);
                    273: u_long cpu_dump_mempagecnt(void);
                    274: void   dumpsys(void);
                    275: void   init386(paddr_t);
                    276: void   initgdt(union descriptor *);
1.255     drochner  277:
1.568     perry     278: void   add_mem_cluster(uint64_t, uint64_t, uint32_t);
1.433     kanaoka   279:
1.521     dsl       280: extern int time_adjusted;
1.484     fvdl      281:
1.571     jmmv      282: struct bootinfo        bootinfo;
                    283: int *esym;
                    284: extern int boothowto;
                    285:
                    286: /* Base memory reported by BIOS. */
                    287: #ifndef REALBASEMEM
                    288: int    biosbasemem = 0;
                    289: #else
                    290: int    biosbasemem = REALBASEMEM;
                    291: #endif
                    292:
                    293: /* Extended memory reported by BIOS. */
                    294: #ifndef REALEXTMEM
                    295: int    biosextmem = 0;
                    296: #else
                    297: int    biosextmem = REALEXTMEM;
                    298: #endif
                    299:
1.577     mrg       300: /* Set if any boot-loader set biosbasemem/biosextmem. */
                    301: int    biosmem_implicit;
                    302:
1.571     jmmv      303: /* Representation of the bootinfo structure constructed by a NetBSD native
                    304:  * boot loader.  Only be used by native_loader(). */
                    305: struct bootinfo_source {
                    306:        uint32_t bs_naddrs;
                    307:        paddr_t bs_addrs[1]; /* Actually longer. */
                    308: };
                    309:
                    310: /* Only called by locore.h; no need to be in a header file. */
                    311: void   native_loader(int, int, struct bootinfo_source *, paddr_t, int, int);
                    312:
                    313: /*
                    314:  * Called as one of the very first things during system startup (just after
                    315:  * the boot loader gave control to the kernel image), this routine is in
                    316:  * charge of retrieving the parameters passed in by the boot loader and
                    317:  * storing them in the appropriate kernel variables.
                    318:  *
                    319:  * WARNING: Because the kernel has not yet relocated itself to KERNBASE,
                    320:  * special care has to be taken when accessing memory because absolute
                    321:  * addresses (referring to kernel symbols) do not work.  So:
                    322:  *
                    323:  *     1) Avoid jumps to absolute addresses (such as gotos and switches).
                    324:  *     2) To access global variables use their physical address, which
                    325:  *        can be obtained using the RELOC macro.
                    326:  */
                    327: void
1.586     christos  328: native_loader(int bl_boothowto, int bl_bootdev,
1.571     jmmv      329:     struct bootinfo_source *bl_bootinfo, paddr_t bl_esym,
1.572     jmmv      330:     int bl_biosextmem, int bl_biosbasemem)
1.571     jmmv      331: {
                    332: #define RELOC(type, x) ((type)((vaddr_t)(x) - KERNBASE))
                    333:
                    334:        *RELOC(int *, &boothowto) = bl_boothowto;
                    335:
                    336: #ifdef COMPAT_OLDBOOT
                    337:        /*
                    338:         * Pre-1.3 boot loaders gave the boot device as a parameter
                    339:         * (instead of a bootinfo entry).
                    340:         */
                    341:        *RELOC(int *, &bootdev) = bl_bootdev;
                    342: #endif
                    343:
                    344:        /*
                    345:         * The boot loader provides a physical, non-relocated address
                    346:         * for the symbols table's end.  We need to convert it to a
                    347:         * virtual address.
                    348:         */
                    349:        if (bl_esym != 0)
                    350:                *RELOC(int **, &esym) = (int *)((vaddr_t)bl_esym + KERNBASE);
                    351:        else
                    352:                *RELOC(int **, &esym) = 0;
                    353:
                    354:        /*
                    355:         * Copy bootinfo entries (if any) from the boot loader's
                    356:         * representation to the kernel's bootinfo space.
                    357:         */
                    358:        if (bl_bootinfo != NULL) {
                    359:                size_t i;
                    360:                uint8_t *data;
                    361:                struct bootinfo *bidest;
                    362:
                    363:                bidest = RELOC(struct bootinfo *, &bootinfo);
                    364:
                    365:                data = &bidest->bi_data[0];
                    366:
                    367:                for (i = 0; i < bl_bootinfo->bs_naddrs; i++) {
                    368:                        struct btinfo_common *bc;
                    369:
                    370:                        bc = (struct btinfo_common *)(bl_bootinfo->bs_addrs[i]);
                    371:
                    372:                        if ((paddr_t)(data + bc->len) >
                    373:                            (paddr_t)(&bidest->bi_data[0] + BOOTINFO_MAXSIZE))
                    374:                                break;
                    375:
                    376:                        memcpy(data, bc, bc->len);
                    377:                        data += bc->len;
                    378:                }
                    379:                bidest->bi_nentries = i;
                    380:        }
                    381:
                    382:        /*
                    383:         * Configure biosbasemem and biosextmem only if they were not
                    384:         * explicitly given during the kernel's build.
                    385:         */
1.577     mrg       386:        if (*RELOC(int *, &biosbasemem) == 0) {
1.571     jmmv      387:                *RELOC(int *, &biosbasemem) = bl_biosbasemem;
1.577     mrg       388:                *RELOC(int *, &biosmem_implicit) = 1;
                    389:        }
                    390:        if (*RELOC(int *, &biosextmem) == 0) {
1.571     jmmv      391:                *RELOC(int *, &biosextmem) = bl_biosextmem;
1.577     mrg       392:                *RELOC(int *, &biosmem_implicit) = 1;
                    393:        }
1.571     jmmv      394: #undef RELOC
                    395: }
                    396:
1.59      mycroft   397: /*
                    398:  * Machine-dependent startup code
                    399:  */
1.32      andrew    400: void
1.1       cgd       401: cpu_startup()
                    402: {
1.586.2.1! tron      403:        int x, y;
1.314     thorpej   404:        vaddr_t minaddr, maxaddr;
1.586.2.1! tron      405:        psize_t sz;
1.354     lukem     406:        char pbuf[9];
1.1       cgd       407:
1.284     mrg       408:        /*
                    409:         * Initialize error message buffer (et end of core).
                    410:         */
1.586.2.1! tron      411:        if (msgbuf_p_cnt == 0)
        !           412:                panic("msgbuf paddr map has not been set up");
        !           413:        for (x = 0, sz = 0; x < msgbuf_p_cnt; sz += msgbuf_p_seg[x++].sz)
        !           414:                continue;
        !           415:        msgbuf_vaddr = uvm_km_alloc(kernel_map, sz, 0, UVM_KMF_VAONLY);
1.385     thorpej   416:        if (msgbuf_vaddr == 0)
1.284     mrg       417:                panic("failed to valloc msgbuf_vaddr");
1.359     thorpej   418:
1.284     mrg       419:        /* msgbuf_paddr was init'd in pmap */
1.586.2.1! tron      420:        for (y = 0, sz = 0; y < msgbuf_p_cnt; y++) {
        !           421:                for (x = 0; x < btoc(msgbuf_p_seg[y].sz); x++, sz += PAGE_SIZE)
        !           422:                        pmap_kenter_pa((vaddr_t)msgbuf_vaddr + sz,
        !           423:                            msgbuf_p_seg[y].paddr + x * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE);
        !           424:        }
1.456     chris     425:        pmap_update(pmap_kernel());
1.359     thorpej   426:
1.586.2.1! tron      427:        initmsgbuf((caddr_t)msgbuf_vaddr, sz);
1.284     mrg       428:
1.562     lukem     429:        printf("%s%s", copyright, version);
1.484     fvdl      430:
1.570     jmmv      431: #ifdef MULTIBOOT
                    432:        multiboot_print_info();
                    433: #endif
                    434:
1.484     fvdl      435: #ifdef TRAPLOG
                    436:        /*
                    437:         * Enable recording of branch from/to in MSR's
                    438:         */
                    439:        wrmsr(MSR_DEBUGCTLMSR, 0x1);
                    440: #endif
                    441:
1.382     mycroft   442:        format_bytes(pbuf, sizeof(pbuf), ptoa(physmem));
1.354     lukem     443:        printf("total memory = %s\n", pbuf);
1.1       cgd       444:
1.547     pk        445:        minaddr = 0;
1.41      cgd       446:
1.1       cgd       447:        /*
1.36      cgd       448:         * Allocate a submap for exec arguments.  This map effectively
                    449:         * limits the number of processes exec'ing at any time.
1.1       cgd       450:         */
1.284     mrg       451:        exec_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
1.358     thorpej   452:                                   16*NCARGS, VM_MAP_PAGEABLE, FALSE, NULL);
1.59      mycroft   453:
1.1       cgd       454:        /*
                    455:         * Allocate a submap for physio
                    456:         */
1.284     mrg       457:        phys_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
1.358     thorpej   458:                                   VM_PHYS_SIZE, 0, FALSE, NULL);
1.1       cgd       459:
                    460:        /*
1.229     thorpej   461:         * Finally, allocate mbuf cluster submap.
1.1       cgd       462:         */
1.334     thorpej   463:        mb_map = uvm_km_suballoc(kernel_map, &minaddr, &maxaddr,
1.358     thorpej   464:            nmbclusters * mclbytes, VM_MAP_INTRSAFE, FALSE, NULL);
1.1       cgd       465:
1.547     pk        466:        format_bytes(pbuf, sizeof(pbuf), ptoa(uvmexp.free));
1.354     lukem     467:        printf("avail memory = %s\n", pbuf);
1.1       cgd       468:
1.375     drochner  469:        /* Safe for i/o port / memory space allocation to use malloc now. */
1.514     fvdl      470:        x86_bus_space_mallocok();
1.349     thorpej   471: }
                    472:
                    473: /*
                    474:  * Set up proc0's TSS and LDT.
                    475:  */
                    476: void
                    477: i386_proc0_tss_ldt_init()
                    478: {
                    479:        struct pcb *pcb;
                    480:        int x;
1.268     thorpej   481:
1.326     thorpej   482:        gdt_init();
1.484     fvdl      483:
1.508     thorpej   484:        cpu_info_primary.ci_curpcb = pcb = &lwp0.l_addr->u_pcb;
1.484     fvdl      485:
1.326     thorpej   486:        pcb->pcb_tss.tss_ioopt =
                    487:            ((caddr_t)pcb->pcb_iomap - (caddr_t)&pcb->pcb_tss) << 16;
1.484     fvdl      488:
1.326     thorpej   489:        for (x = 0; x < sizeof(pcb->pcb_iomap) / 4; x++)
                    490:                pcb->pcb_iomap[x] = 0xffffffff;
                    491:
1.394     thorpej   492:        pcb->pcb_ldt_sel = pmap_kernel()->pm_ldt_sel = GSEL(GLDT_SEL, SEL_KPL);
1.326     thorpej   493:        pcb->pcb_cr0 = rcr0();
                    494:        pcb->pcb_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
1.573     yamt      495:        pcb->pcb_tss.tss_esp0 = USER_TO_UAREA(lwp0.l_addr) + KSTACK_SIZE - 16;
1.508     thorpej   496:        lwp0.l_md.md_regs = (struct trapframe *)pcb->pcb_tss.tss_esp0 - 1;
                    497:        lwp0.l_md.md_tss_sel = tss_alloc(pcb);
1.326     thorpej   498:
1.508     thorpej   499:        ltr(lwp0.l_md.md_tss_sel);
1.326     thorpej   500:        lldt(pcb->pcb_ldt_sel);
1.484     fvdl      501: }
                    502:
                    503: /*
                    504:  * Set up TSS and LDT for a new PCB.
                    505:  */
                    506:
                    507: void
1.551     junyoung  508: i386_init_pcb_tss_ldt(struct cpu_info *ci)
1.484     fvdl      509: {
                    510:        int x;
                    511:        struct pcb *pcb = ci->ci_idle_pcb;
1.326     thorpej   512:
1.484     fvdl      513:        pcb->pcb_tss.tss_ioopt =
                    514:            ((caddr_t)pcb->pcb_iomap - (caddr_t)&pcb->pcb_tss) << 16;
                    515:        for (x = 0; x < sizeof(pcb->pcb_iomap) / 4; x++)
                    516:                pcb->pcb_iomap[x] = 0xffffffff;
                    517:
                    518:        pcb->pcb_ldt_sel = pmap_kernel()->pm_ldt_sel = GSEL(GLDT_SEL, SEL_KPL);
                    519:        pcb->pcb_cr0 = rcr0();
                    520:
                    521:        ci->ci_idle_tss_sel = tss_alloc(pcb);
1.326     thorpej   522: }
                    523:
                    524: /*
1.545     atatat    525:  * sysctl helper routine for machdep.tm* nodes.
1.484     fvdl      526:  */
1.545     atatat    527: static int
                    528: sysctl_machdep_tm_longrun(SYSCTLFN_ARGS)
1.104     cgd       529: {
1.545     atatat    530:        struct sysctlnode node;
                    531:        int io, error;
                    532:
                    533:        if (!tmx86_has_longrun)
                    534:                return (EOPNOTSUPP);
                    535:
                    536:        node = *rnode;
                    537:        node.sysctl_data = &io;
1.104     cgd       538:
1.545     atatat    539:        switch (rnode->sysctl_num) {
1.461     christos  540:        case CPU_TMLR_MODE:
1.545     atatat    541:                io = (int)(crusoe_longrun = tmx86_get_longrun_mode());
                    542:                break;
1.461     christos  543:        case CPU_TMLR_FREQUENCY:
                    544:                tmx86_get_longrun_status_all();
1.545     atatat    545:                io = crusoe_frequency;
                    546:                break;
1.461     christos  547:        case CPU_TMLR_VOLTAGE:
                    548:                tmx86_get_longrun_status_all();
1.545     atatat    549:                io = crusoe_voltage;
                    550:                break;
1.461     christos  551:        case CPU_TMLR_PERCENTAGE:
                    552:                tmx86_get_longrun_status_all();
1.545     atatat    553:                io = crusoe_percentage;
                    554:                break;
1.104     cgd       555:        default:
                    556:                return (EOPNOTSUPP);
                    557:        }
1.545     atatat    558:
                    559:        error = sysctl_lookup(SYSCTLFN_CALL(&node));
                    560:        if (error || newp == NULL)
                    561:                return (error);
                    562:
                    563:        if (rnode->sysctl_num == CPU_TMLR_MODE) {
                    564:                if (tmx86_set_longrun_mode(io))
                    565:                        crusoe_longrun = (u_int)io;
                    566:                else
                    567:                        return (EINVAL);
                    568:        }
                    569:
                    570:        return (0);
                    571: }
                    572:
                    573: /*
                    574:  * sysctl helper routine for machdep.booted_kernel
                    575:  */
                    576: static int
                    577: sysctl_machdep_booted_kernel(SYSCTLFN_ARGS)
                    578: {
                    579:        struct btinfo_bootpath *bibp;
                    580:        struct sysctlnode node;
                    581:
                    582:        bibp = lookup_bootinfo(BTINFO_BOOTPATH);
                    583:        if(!bibp)
                    584:                return(ENOENT); /* ??? */
                    585:
                    586:        node = *rnode;
                    587:        node.sysctl_data = bibp->bootpath;
                    588:        node.sysctl_size = sizeof(bibp->bootpath);
                    589:        return (sysctl_lookup(SYSCTLFN_CALL(&node)));
                    590: }
                    591:
                    592: /*
                    593:  * sysctl helper routine for machdep.diskinfo
                    594:  */
                    595: static int
                    596: sysctl_machdep_diskinfo(SYSCTLFN_ARGS)
                    597: {
                    598:        struct sysctlnode node;
                    599:
                    600:        node = *rnode;
1.559     thorpej   601:        if (x86_alldisks == NULL)
1.557     dbj       602:                return(EOPNOTSUPP);
1.559     thorpej   603:        node.sysctl_data = x86_alldisks;
1.545     atatat    604:        node.sysctl_size = sizeof(struct disklist) +
1.559     thorpej   605:            (x86_ndisks - 1) * sizeof(struct nativedisk_info);
1.557     dbj       606:        return (sysctl_lookup(SYSCTLFN_CALL(&node)));
1.545     atatat    607: }
                    608:
                    609: /*
                    610:  * machine dependent system variables.
                    611:  */
                    612: SYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
                    613: {
                    614:
1.552     atatat    615:        sysctl_createv(clog, 0, NULL, NULL,
                    616:                       CTLFLAG_PERMANENT,
1.545     atatat    617:                       CTLTYPE_NODE, "machdep", NULL,
                    618:                       NULL, 0, NULL, 0,
                    619:                       CTL_MACHDEP, CTL_EOL);
                    620:
1.552     atatat    621:        sysctl_createv(clog, 0, NULL, NULL,
                    622:                       CTLFLAG_PERMANENT,
1.545     atatat    623:                       CTLTYPE_STRUCT, "console_device", NULL,
                    624:                       sysctl_consdev, 0, NULL, sizeof(dev_t),
                    625:                       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
1.552     atatat    626:        sysctl_createv(clog, 0, NULL, NULL,
                    627:                       CTLFLAG_PERMANENT,
1.545     atatat    628:                       CTLTYPE_INT, "biosbasemem", NULL,
                    629:                       NULL, 0, &biosbasemem, 0,
                    630:                       CTL_MACHDEP, CPU_BIOSBASEMEM, CTL_EOL);
1.552     atatat    631:        sysctl_createv(clog, 0, NULL, NULL,
                    632:                       CTLFLAG_PERMANENT,
1.545     atatat    633:                       CTLTYPE_INT, "biosextmem", NULL,
                    634:                       NULL, 0, &biosextmem, 0,
                    635:                       CTL_MACHDEP, CPU_BIOSEXTMEM, CTL_EOL);
1.552     atatat    636:        sysctl_createv(clog, 0, NULL, NULL,
                    637:                       CTLFLAG_PERMANENT,
1.545     atatat    638:                       CTLTYPE_INT, "nkpde", NULL,
                    639:                       NULL, 0, &nkpde, 0,
                    640:                       CTL_MACHDEP, CPU_NKPDE, CTL_EOL);
1.552     atatat    641:        sysctl_createv(clog, 0, NULL, NULL,
                    642:                       CTLFLAG_PERMANENT,
1.545     atatat    643:                       CTLTYPE_STRING, "booted_kernel", NULL,
                    644:                       sysctl_machdep_booted_kernel, 0, NULL, 0,
                    645:                       CTL_MACHDEP, CPU_BOOTED_KERNEL, CTL_EOL);
1.552     atatat    646:        sysctl_createv(clog, 0, NULL, NULL,
                    647:                       CTLFLAG_PERMANENT,
1.545     atatat    648:                       CTLTYPE_STRUCT, "diskinfo", NULL,
                    649:                       sysctl_machdep_diskinfo, 0, NULL, 0,
                    650:                       CTL_MACHDEP, CPU_DISKINFO, CTL_EOL);
1.552     atatat    651:        sysctl_createv(clog, 0, NULL, NULL,
                    652:                       CTLFLAG_PERMANENT,
1.545     atatat    653:                       CTLTYPE_INT, "fpu_present", NULL,
                    654:                       NULL, 0, &i386_fpu_present, 0,
                    655:                       CTL_MACHDEP, CPU_FPU_PRESENT, CTL_EOL);
1.552     atatat    656:        sysctl_createv(clog, 0, NULL, NULL,
                    657:                       CTLFLAG_PERMANENT,
1.545     atatat    658:                       CTLTYPE_INT, "osfxsr", NULL,
                    659:                       NULL, 0, &i386_use_fxsave, 0,
                    660:                       CTL_MACHDEP, CPU_OSFXSR, CTL_EOL);
1.552     atatat    661:        sysctl_createv(clog, 0, NULL, NULL,
                    662:                       CTLFLAG_PERMANENT,
1.545     atatat    663:                       CTLTYPE_INT, "sse", NULL,
                    664:                       NULL, 0, &i386_has_sse, 0,
                    665:                       CTL_MACHDEP, CPU_SSE, CTL_EOL);
1.552     atatat    666:        sysctl_createv(clog, 0, NULL, NULL,
                    667:                       CTLFLAG_PERMANENT,
1.545     atatat    668:                       CTLTYPE_INT, "sse2", NULL,
                    669:                       NULL, 0, &i386_has_sse2, 0,
                    670:                       CTL_MACHDEP, CPU_SSE2, CTL_EOL);
1.554     lukem     671:        sysctl_createv(clog, 0, NULL, NULL,
                    672:                       CTLFLAG_PERMANENT,
                    673:                       CTLTYPE_STRING, "cpu_brand", NULL,
                    674:                       NULL, 0, &cpu_brand_string, 0,
                    675:                       CTL_MACHDEP, CTL_CREATE, CTL_EOL);
1.552     atatat    676:        sysctl_createv(clog, 0, NULL, NULL,
                    677:                       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1.545     atatat    678:                       CTLTYPE_INT, "tm_longrun_mode", NULL,
                    679:                       sysctl_machdep_tm_longrun, 0, NULL, 0,
                    680:                       CTL_MACHDEP, CPU_TMLR_MODE, CTL_EOL);
1.552     atatat    681:        sysctl_createv(clog, 0, NULL, NULL,
                    682:                       CTLFLAG_PERMANENT,
1.545     atatat    683:                       CTLTYPE_INT, "tm_longrun_frequency", NULL,
                    684:                       sysctl_machdep_tm_longrun, 0, NULL, 0,
                    685:                       CTL_MACHDEP, CPU_TMLR_FREQUENCY, CTL_EOL);
1.552     atatat    686:        sysctl_createv(clog, 0, NULL, NULL,
                    687:                       CTLFLAG_PERMANENT,
1.545     atatat    688:                       CTLTYPE_INT, "tm_longrun_voltage", NULL,
                    689:                       sysctl_machdep_tm_longrun, 0, NULL, 0,
                    690:                       CTL_MACHDEP, CPU_TMLR_VOLTAGE, CTL_EOL);
1.552     atatat    691:        sysctl_createv(clog, 0, NULL, NULL,
                    692:                       CTLFLAG_PERMANENT,
1.545     atatat    693:                       CTLTYPE_INT, "tm_longrun_percentage", NULL,
                    694:                       sysctl_machdep_tm_longrun, 0, NULL, 0,
                    695:                       CTL_MACHDEP, CPU_TMLR_PERCENTAGE, CTL_EOL);
1.104     cgd       696: }
1.151     christos  697:
1.534     christos  698: void *
                    699: getframe(struct lwp *l, int sig, int *onstack)
                    700: {
                    701:        struct proc *p = l->l_proc;
                    702:        struct sigctx *ctx = &p->p_sigctx;
                    703:        struct trapframe *tf = l->l_md.md_regs;
                    704:
                    705:        /* Do we need to jump onto the signal stack? */
                    706:        *onstack = (ctx->ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0
                    707:            && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
                    708:        if (*onstack)
                    709:                return (char *)ctx->ps_sigstk.ss_sp + ctx->ps_sigstk.ss_size;
                    710: #ifdef VM86
                    711:        if (tf->tf_eflags & PSL_VM)
                    712:                return (void *)(tf->tf_esp + (tf->tf_ss << 4));
                    713:        else
                    714: #endif
                    715:                return (void *)tf->tf_esp;
                    716: }
                    717:
                    718: /*
                    719:  * Build context to run handler in.  We invoke the handler
                    720:  * directly, only returning via the trampoline.  Note the
                    721:  * trampoline version numbers are coordinated with machine-
                    722:  * dependent code in libc.
                    723:  */
                    724: void
                    725: buildcontext(struct lwp *l, int sel, void *catcher, void *fp)
                    726: {
                    727:        struct trapframe *tf = l->l_md.md_regs;
                    728:
                    729:        tf->tf_gs = GSEL(GUDATA_SEL, SEL_UPL);
                    730:        tf->tf_fs = GSEL(GUDATA_SEL, SEL_UPL);
                    731:        tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL);
                    732:        tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
                    733:        tf->tf_eip = (int)catcher;
                    734:        tf->tf_cs = GSEL(sel, SEL_UPL);
                    735:        tf->tf_eflags &= ~(PSL_T|PSL_VM|PSL_AC);
                    736:        tf->tf_esp = (int)fp;
                    737:        tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
                    738: }
                    739:
                    740: static void
1.539     christos  741: sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
1.534     christos  742: {
                    743:        struct lwp *l = curlwp;
                    744:        struct proc *p = l->l_proc;
                    745:        struct pmap *pmap = vm_map_pmap(&p->p_vmspace->vm_map);
                    746:        int sel = pmap->pm_hiexec > I386_MAX_EXE_ADDR ?
                    747:            GUCODEBIG_SEL : GUCODE_SEL;
                    748:        struct sigacts *ps = p->p_sigacts;
                    749:        int onstack;
                    750:        int sig = ksi->ksi_signo;
                    751:        struct sigframe_siginfo *fp = getframe(l, sig, &onstack), frame;
                    752:        sig_t catcher = SIGACTION(p, sig).sa_handler;
                    753:        struct trapframe *tf = l->l_md.md_regs;
                    754:
                    755:        fp--;
                    756:
                    757:        /* Build stack frame for signal trampoline. */
                    758:        switch (ps->sa_sigdesc[sig].sd_vers) {
1.537     christos  759:        case 0:         /* handled by sendsig_sigcontext */
                    760:        case 1:         /* handled by sendsig_sigcontext */
1.534     christos  761:        default:        /* unknown version */
                    762:                printf("nsendsig: bad version %d\n",
                    763:                    ps->sa_sigdesc[sig].sd_vers);
                    764:                sigexit(l, SIGILL);
                    765:        case 2:
                    766:                break;
                    767:        }
                    768:
                    769:        frame.sf_ra = (int)ps->sa_sigdesc[sig].sd_tramp;
                    770:        frame.sf_signum = sig;
                    771:        frame.sf_sip = &fp->sf_si;
                    772:        frame.sf_ucp = &fp->sf_uc;
1.540     thorpej   773:        frame.sf_si._info = ksi->ksi_info;
1.534     christos  774:        frame.sf_uc.uc_flags = _UC_SIGMASK|_UC_VM;
                    775:        frame.sf_uc.uc_sigmask = *mask;
                    776:        frame.sf_uc.uc_link = NULL;
                    777:        frame.sf_uc.uc_flags |= (p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK)
                    778:            ? _UC_SETSTACK : _UC_CLRSTACK;
                    779:        memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack));
                    780:        cpu_getmcontext(l, &frame.sf_uc.uc_mcontext, &frame.sf_uc.uc_flags);
                    781:
                    782:        if (tf->tf_eflags & PSL_VM)
                    783:                (*p->p_emul->e_syscall_intern)(p);
                    784:
                    785:        if (copyout(&frame, fp, sizeof(frame)) != 0) {
                    786:                /*
                    787:                 * Process has trashed its stack; give it an illegal
                    788:                 * instruction to halt it in its tracks.
                    789:                 */
                    790:                sigexit(l, SIGILL);
                    791:                /* NOTREACHED */
                    792:        }
                    793:
                    794:        buildcontext(l, sel, catcher, fp);
1.319     mycroft   795:
                    796:        /* Remember that we're now on the signal stack. */
                    797:        if (onstack)
1.425     jdolecek  798:                p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
1.1       cgd       799: }
                    800:
1.534     christos  801: void
1.539     christos  802: sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
1.534     christos  803: {
                    804: #ifdef COMPAT_16
                    805:        if (curproc->p_sigacts->sa_sigdesc[ksi->ksi_signo].sd_vers < 2)
                    806:                sendsig_sigcontext(ksi, mask);
                    807:        else
                    808: #endif
                    809:                sendsig_siginfo(ksi, mask);
                    810: }
1.508     thorpej   811:
1.550     junyoung  812: void
1.534     christos  813: cpu_upcall(struct lwp *l, int type, int nevents, int ninterrupted, void *sas,
                    814:     void *ap, void *sp, sa_upcall_t upcall)
1.508     thorpej   815: {
1.533     chs       816:        struct pmap *pmap = vm_map_pmap(&l->l_proc->p_vmspace->vm_map);
1.508     thorpej   817:        struct saframe *sf, frame;
                    818:        struct trapframe *tf;
                    819:
                    820:        tf = l->l_md.md_regs;
                    821:
                    822:        /* Finally, copy out the rest of the frame. */
                    823:        frame.sa_type = type;
                    824:        frame.sa_sas = sas;
                    825:        frame.sa_events = nevents;
                    826:        frame.sa_interrupted = ninterrupted;
                    827:        frame.sa_arg = ap;
                    828:        frame.sa_ra = 0;
1.550     junyoung  829:
1.508     thorpej   830:        sf = (struct saframe *)sp - 1;
                    831:        if (copyout(&frame, sf, sizeof(frame)) != 0) {
                    832:                /* Copying onto the stack didn't work. Die. */
                    833:                sigexit(l, SIGILL);
                    834:                /* NOTREACHED */
                    835:        }
                    836:
                    837:        tf->tf_eip = (int) upcall;
                    838:        tf->tf_esp = (int) sf;
                    839:        tf->tf_ebp = 0; /* indicate call-frame-top to debuggers */
                    840:        tf->tf_gs = GSEL(GUDATA_SEL, SEL_UPL);
                    841:        tf->tf_fs = GSEL(GUDATA_SEL, SEL_UPL);
                    842:        tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL);
                    843:        tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
1.533     chs       844:        tf->tf_cs = pmap->pm_hiexec > I386_MAX_EXE_ADDR ?
                    845:            GSEL(GUCODEBIG_SEL, SEL_UPL) : GSEL(GUCODE_SEL, SEL_UPL);
1.508     thorpej   846:        tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
                    847:        tf->tf_eflags &= ~(PSL_T|PSL_VM|PSL_AC);
                    848: }
1.37      cgd       849:
1.1       cgd       850: int    waittime = -1;
                    851: struct pcb dumppcb;
                    852:
1.32      andrew    853: void
1.586     christos  854: cpu_reboot(int howto, char *bootstr)
1.1       cgd       855: {
                    856:
1.106     mycroft   857:        if (cold) {
1.193     mycroft   858:                howto |= RB_HALT;
                    859:                goto haltsys;
1.1       cgd       860:        }
1.193     mycroft   861:
1.106     mycroft   862:        boothowto = howto;
1.193     mycroft   863:        if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
1.1       cgd       864:                waittime = 0;
1.150     mycroft   865:                vfs_shutdown();
1.521     dsl       866:                /*
                    867:                 * If we've been adjusting the clock, the todr
                    868:                 * will be out of synch; adjust it now.
                    869:                 */
                    870:                if (time_adjusted != 0)
                    871:                        resettodr();
1.1       cgd       872:        }
1.193     mycroft   873:
                    874:        /* Disable interrupts. */
1.1       cgd       875:        splhigh();
1.193     mycroft   876:
                    877:        /* Do a dump if requested. */
                    878:        if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
                    879:                dumpsys();
                    880:
                    881: haltsys:
                    882:        doshutdownhooks();
                    883:
1.484     fvdl      884: #ifdef MULTIPROCESSOR
1.514     fvdl      885:        x86_broadcast_ipi(X86_IPI_HALT);
1.484     fvdl      886: #endif
                    887:
1.307     thorpej   888:        if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
1.473     tshiozak  889: #if NACPI > 0
1.530     kochi     890:                if (acpi_softc != NULL) {
                    891:                        delay(500000);
                    892:                        acpi_enter_sleep_state(acpi_softc, ACPI_STATE_S5);
                    893:                        printf("WARNING: ACPI powerdown failed!\n");
                    894:                }
1.473     tshiozak  895: #endif
1.576     christos  896: #if NAPMBIOS > 0 && !defined(APM_NO_POWEROFF)
1.208     jtk       897:                /* turn off, if we can.  But try to turn disk off and
                    898:                 * wait a bit first--some disk drives are slow to clean up
                    899:                 * and users have reported disk corruption.
                    900:                 */
                    901:                delay(500000);
1.578     xtraeme   902:                apm_set_powstate(NULL, APM_DEV_DISK(APM_DEV_ALLUNITS), APM_SYS_OFF);
1.208     jtk       903:                delay(500000);
1.576     christos  904:                apm_set_powstate(NULL, APM_DEV_ALLDEVS, APM_SYS_OFF);
1.531     kochi     905:                printf("WARNING: APM powerdown failed!\n");
1.307     thorpej   906:                /*
                    907:                 * RB_POWERDOWN implies RB_HALT... fall into it...
                    908:                 */
1.208     jtk       909: #endif
1.307     thorpej   910:        }
                    911:
                    912:        if (howto & RB_HALT) {
1.210     christos  913:                printf("\n");
                    914:                printf("The operating system has halted.\n");
                    915:                printf("Please press any key to reboot.\n\n");
1.517     jmmv      916:
                    917: #ifdef BEEP_ONHALT
                    918:                {
                    919:                        int c;
                    920:                        for (c = BEEP_ONHALT_COUNT; c > 0; c--) {
                    921:                                sysbeep(BEEP_ONHALT_PITCH,
1.565     simonb    922:                                        BEEP_ONHALT_PERIOD * hz / 1000);
1.517     jmmv      923:                                delay(BEEP_ONHALT_PERIOD * 1000);
                    924:                                sysbeep(0, BEEP_ONHALT_PERIOD * hz / 1000);
                    925:                                delay(BEEP_ONHALT_PERIOD * 1000);
                    926:                        }
                    927:                }
                    928: #endif
                    929:
1.300     drochner  930:                cnpollc(1);     /* for proper keyboard command handling */
1.512     perry     931:                if (cngetc() == 0) {
                    932:                        /* no console attached, so just hlt */
                    933:                        for(;;) {
1.567     perry     934:                                __asm volatile("hlt");
1.512     perry     935:                        }
                    936:                }
1.300     drochner  937:                cnpollc(0);
1.1       cgd       938:        }
1.193     mycroft   939:
1.210     christos  940:        printf("rebooting...\n");
1.328     bouyer    941:        if (cpureset_delay > 0)
                    942:                delay(cpureset_delay * 1000);
1.1       cgd       943:        cpu_reset();
                    944:        for(;;) ;
                    945:        /*NOTREACHED*/
                    946: }
                    947:
1.116     gwr       948: /*
                    949:  * These variables are needed by /sbin/savecore
                    950:  */
1.568     perry     951: uint32_t dumpmag = 0x8fca0101; /* magic number */
1.116     gwr       952: int    dumpsize = 0;           /* pages */
                    953: long   dumplo = 0;             /* blocks */
                    954:
                    955: /*
1.291     thorpej   956:  * cpu_dumpsize: calculate size of machine-dependent kernel core dump headers.
                    957:  */
                    958: int
                    959: cpu_dumpsize()
                    960: {
                    961:        int size;
                    962:
                    963:        size = ALIGN(sizeof(kcore_seg_t)) + ALIGN(sizeof(cpu_kcore_hdr_t)) +
                    964:            ALIGN(mem_cluster_cnt * sizeof(phys_ram_seg_t));
                    965:        if (roundup(size, dbtob(1)) != dbtob(1))
                    966:                return (-1);
                    967:
                    968:        return (1);
                    969: }
                    970:
                    971: /*
                    972:  * cpu_dump_mempagecnt: calculate the size of RAM (in pages) to be dumped.
                    973:  */
                    974: u_long
                    975: cpu_dump_mempagecnt()
                    976: {
                    977:        u_long i, n;
                    978:
                    979:        n = 0;
                    980:        for (i = 0; i < mem_cluster_cnt; i++)
                    981:                n += atop(mem_clusters[i].size);
                    982:        return (n);
                    983: }
                    984:
                    985: /*
                    986:  * cpu_dump: dump the machine-dependent kernel core dump headers.
                    987:  */
                    988: int
                    989: cpu_dump()
                    990: {
1.542     junyoung  991:        int (*dump)(dev_t, daddr_t, caddr_t, size_t);
1.563     christos  992:        char bf[dbtob(1)];
1.291     thorpej   993:        kcore_seg_t *segp;
                    994:        cpu_kcore_hdr_t *cpuhdrp;
                    995:        phys_ram_seg_t *memsegp;
1.481     gehenna   996:        const struct bdevsw *bdev;
1.291     thorpej   997:        int i;
                    998:
1.481     gehenna   999:        bdev = bdevsw_lookup(dumpdev);
                   1000:        if (bdev == NULL)
                   1001:                return (ENXIO);
                   1002:        dump = bdev->d_dump;
1.291     thorpej  1003:
1.563     christos 1004:        memset(bf, 0, sizeof bf);
                   1005:        segp = (kcore_seg_t *)bf;
                   1006:        cpuhdrp = (cpu_kcore_hdr_t *)&bf[ALIGN(sizeof(*segp))];
                   1007:        memsegp = (phys_ram_seg_t *)&bf[ ALIGN(sizeof(*segp)) +
1.291     thorpej  1008:            ALIGN(sizeof(*cpuhdrp))];
                   1009:
                   1010:        /*
                   1011:         * Generate a segment header.
                   1012:         */
                   1013:        CORE_SETMAGIC(*segp, KCORE_MAGIC, MID_MACHINE, CORE_CPU);
                   1014:        segp->c_size = dbtob(1) - ALIGN(sizeof(*segp));
                   1015:
                   1016:        /*
                   1017:         * Add the machine-dependent header info.
                   1018:         */
1.558     junyoung 1019:        cpuhdrp->pdppaddr = PDPpaddr;
1.291     thorpej  1020:        cpuhdrp->nmemsegs = mem_cluster_cnt;
                   1021:
                   1022:        /*
                   1023:         * Fill in the memory segment descriptors.
                   1024:         */
                   1025:        for (i = 0; i < mem_cluster_cnt; i++) {
                   1026:                memsegp[i].start = mem_clusters[i].start;
                   1027:                memsegp[i].size = mem_clusters[i].size;
                   1028:        }
                   1029:
1.563     christos 1030:        return (dump(dumpdev, dumplo, (caddr_t)bf, dbtob(1)));
1.291     thorpej  1031: }
                   1032:
                   1033: /*
1.228     gwr      1034:  * This is called by main to set dumplo and dumpsize.
1.414     thorpej  1035:  * Dumps always skip the first PAGE_SIZE of disk space
1.116     gwr      1036:  * in case there might be a disk label stored there.
                   1037:  * If there is extra space, put dump at the end to
                   1038:  * reduce the chance that swapping trashes it.
                   1039:  */
                   1040: void
1.228     gwr      1041: cpu_dumpconf()
1.116     gwr      1042: {
1.481     gehenna  1043:        const struct bdevsw *bdev;
1.291     thorpej  1044:        int nblks, dumpblks;    /* size of dump area */
1.116     gwr      1045:
                   1046:        if (dumpdev == NODEV)
1.291     thorpej  1047:                goto bad;
1.481     gehenna  1048:        bdev = bdevsw_lookup(dumpdev);
1.582     mrg      1049:        if (bdev == NULL) {
                   1050:                dumpdev = NODEV;
1.583     skrll    1051:                goto bad;
1.582     mrg      1052:        }
1.481     gehenna  1053:        if (bdev->d_psize == NULL)
1.291     thorpej  1054:                goto bad;
1.481     gehenna  1055:        nblks = (*bdev->d_psize)(dumpdev);
1.116     gwr      1056:        if (nblks <= ctod(1))
1.291     thorpej  1057:                goto bad;
1.116     gwr      1058:
1.291     thorpej  1059:        dumpblks = cpu_dumpsize();
                   1060:        if (dumpblks < 0)
                   1061:                goto bad;
                   1062:        dumpblks += ctod(cpu_dump_mempagecnt());
                   1063:
                   1064:        /* If dump won't fit (incl. room for possible label), punt. */
                   1065:        if (dumpblks > (nblks - ctod(1)))
                   1066:                goto bad;
                   1067:
                   1068:        /* Put dump at end of partition */
                   1069:        dumplo = nblks - dumpblks;
                   1070:
                   1071:        /* dumpsize is in page units, and doesn't include headers. */
                   1072:        dumpsize = cpu_dump_mempagecnt();
                   1073:        return;
1.116     gwr      1074:
1.291     thorpej  1075:  bad:
                   1076:        dumpsize = 0;
1.116     gwr      1077: }
                   1078:
1.1       cgd      1079: /*
                   1080:  * Doadump comes here after turning off memory management and
                   1081:  * getting on the dump stack, either when called above, or by
                   1082:  * the auto-restart code.
                   1083:  */
1.414     thorpej  1084: #define BYTES_PER_DUMP  PAGE_SIZE /* must be a multiple of pagesize XXX small */
1.314     thorpej  1085: static vaddr_t dumpspace;
1.163     cgd      1086:
1.314     thorpej  1087: vaddr_t
1.551     junyoung 1088: reserve_dumppages(vaddr_t p)
1.163     cgd      1089: {
                   1090:
                   1091:        dumpspace = p;
                   1092:        return (p + BYTES_PER_DUMP);
                   1093: }
                   1094:
1.32      andrew   1095: void
1.1       cgd      1096: dumpsys()
                   1097: {
1.556     mycroft  1098:        u_long totalbytesleft, bytes, i, n, m, memseg;
1.291     thorpej  1099:        u_long maddr;
                   1100:        int psize;
1.163     cgd      1101:        daddr_t blkno;
1.481     gehenna  1102:        const struct bdevsw *bdev;
1.542     junyoung 1103:        int (*dump)(dev_t, daddr_t, caddr_t, size_t);
1.200     christos 1104:        int error;
1.193     mycroft  1105:
                   1106:        /* Save registers. */
                   1107:        savectx(&dumppcb);
1.1       cgd      1108:
                   1109:        if (dumpdev == NODEV)
                   1110:                return;
1.484     fvdl     1111:
1.481     gehenna  1112:        bdev = bdevsw_lookup(dumpdev);
                   1113:        if (bdev == NULL || bdev->d_psize == NULL)
                   1114:                return;
1.163     cgd      1115:
                   1116:        /*
                   1117:         * For dumps during autoconfiguration,
                   1118:         * if dump device has already configured...
                   1119:         */
                   1120:        if (dumpsize == 0)
1.228     gwr      1121:                cpu_dumpconf();
1.330     jtk      1122:        if (dumplo <= 0 || dumpsize == 0) {
1.275     mycroft  1123:                printf("\ndump to dev %u,%u not possible\n", major(dumpdev),
                   1124:                    minor(dumpdev));
1.163     cgd      1125:                return;
1.275     mycroft  1126:        }
                   1127:        printf("\ndumping to dev %u,%u offset %ld\n", major(dumpdev),
                   1128:            minor(dumpdev), dumplo);
1.134     mycroft  1129:
1.481     gehenna  1130:        psize = (*bdev->d_psize)(dumpdev);
1.210     christos 1131:        printf("dump ");
1.163     cgd      1132:        if (psize == -1) {
1.210     christos 1133:                printf("area unavailable\n");
1.163     cgd      1134:                return;
                   1135:        }
                   1136:
                   1137: #if 0  /* XXX this doesn't work.  grr. */
1.565     simonb   1138:        /* toss any characters present prior to dump */
1.163     cgd      1139:        while (sget() != NULL); /*syscons and pccons differ */
                   1140: #endif
                   1141:
1.291     thorpej  1142:        if ((error = cpu_dump()) != 0)
                   1143:                goto err;
                   1144:
                   1145:        totalbytesleft = ptoa(cpu_dump_mempagecnt());
                   1146:        blkno = dumplo + cpu_dumpsize();
1.481     gehenna  1147:        dump = bdev->d_dump;
1.200     christos 1148:        error = 0;
1.291     thorpej  1149:
                   1150:        for (memseg = 0; memseg < mem_cluster_cnt; memseg++) {
                   1151:                maddr = mem_clusters[memseg].start;
                   1152:                bytes = mem_clusters[memseg].size;
                   1153:
                   1154:                for (i = 0; i < bytes; i += n, totalbytesleft -= n) {
                   1155:                        /* Print out how many MBs we have left to go. */
                   1156:                        if ((totalbytesleft % (1024*1024)) == 0)
                   1157:                                printf("%ld ", totalbytesleft / (1024 * 1024));
                   1158:
                   1159:                        /* Limit size for next transfer. */
                   1160:                        n = bytes - i;
                   1161:                        if (n > BYTES_PER_DUMP)
                   1162:                                n = BYTES_PER_DUMP;
                   1163:
1.556     mycroft  1164:                        for (m = 0; m < n; m += NBPG)
                   1165:                                pmap_kenter_pa(dumpspace + m, maddr + m,
                   1166:                                    VM_PROT_READ);
1.291     thorpej  1167:
                   1168:                        error = (*dump)(dumpdev, blkno, (caddr_t)dumpspace, n);
                   1169:                        if (error)
                   1170:                                goto err;
1.163     cgd      1171:                        maddr += n;
1.291     thorpej  1172:                        blkno += btodb(n);              /* XXX? */
1.163     cgd      1173:
                   1174: #if 0  /* XXX this doesn't work.  grr. */
1.291     thorpej  1175:                        /* operator aborting dump? */
                   1176:                        if (sget() != NULL) {
                   1177:                                error = EINTR;
                   1178:                                break;
                   1179:                        }
                   1180: #endif
1.163     cgd      1181:                }
                   1182:        }
                   1183:
1.291     thorpej  1184:  err:
1.163     cgd      1185:        switch (error) {
1.1       cgd      1186:
                   1187:        case ENXIO:
1.210     christos 1188:                printf("device bad\n");
1.1       cgd      1189:                break;
                   1190:
                   1191:        case EFAULT:
1.210     christos 1192:                printf("device not ready\n");
1.1       cgd      1193:                break;
                   1194:
                   1195:        case EINVAL:
1.210     christos 1196:                printf("area improper\n");
1.1       cgd      1197:                break;
                   1198:
                   1199:        case EIO:
1.210     christos 1200:                printf("i/o error\n");
1.1       cgd      1201:                break;
                   1202:
                   1203:        case EINTR:
1.210     christos 1204:                printf("aborted from console\n");
1.1       cgd      1205:                break;
                   1206:
1.163     cgd      1207:        case 0:
1.210     christos 1208:                printf("succeeded\n");
1.163     cgd      1209:                break;
                   1210:
1.1       cgd      1211:        default:
1.210     christos 1212:                printf("error %d\n", error);
1.1       cgd      1213:                break;
                   1214:        }
1.210     christos 1215:        printf("\n\n");
1.163     cgd      1216:        delay(5000000);         /* 5 seconds */
1.1       cgd      1217: }
                   1218:
                   1219: /*
                   1220:  * Clear registers on exec
                   1221:  */
1.33      cgd      1222: void
1.551     junyoung 1223: setregs(struct lwp *l, struct exec_package *pack, u_long stack)
1.1       cgd      1224: {
1.533     chs      1225:        struct pmap *pmap = vm_map_pmap(&l->l_proc->p_vmspace->vm_map);
1.508     thorpej  1226:        struct pcb *pcb = &l->l_addr->u_pcb;
1.298     mycroft  1227:        struct trapframe *tf;
1.1       cgd      1228:
1.161     mycroft  1229: #if NNPX > 0
                   1230:        /* If we were using the FPU, forget about it. */
1.508     thorpej  1231:        if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
                   1232:                npxsave_lwp(l, 0);
1.161     mycroft  1233: #endif
1.166     mycroft  1234:
1.178     mycroft  1235: #ifdef USER_LDT
1.508     thorpej  1236:        pmap_ldt_cleanup(l);
1.178     mycroft  1237: #endif
                   1238:
1.548     yamt     1239:        l->l_md.md_flags &= ~MDL_USEDFPU;
1.452     thorpej  1240:        if (i386_use_fxsave) {
1.450     thorpej  1241:                pcb->pcb_savefpu.sv_xmm.sv_env.en_cw = __NetBSD_NPXCW__;
1.452     thorpej  1242:                pcb->pcb_savefpu.sv_xmm.sv_env.en_mxcsr = __INITIAL_MXCSR__;
                   1243:        } else
1.450     thorpej  1244:                pcb->pcb_savefpu.sv_87.sv_env.en_cw = __NetBSD_NPXCW__;
1.59      mycroft  1245:
1.508     thorpej  1246:        tf = l->l_md.md_regs;
1.445     sommerfe 1247:        tf->tf_gs = LSEL(LUDATA_SEL, SEL_UPL);
                   1248:        tf->tf_fs = LSEL(LUDATA_SEL, SEL_UPL);
1.154     mycroft  1249:        tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
                   1250:        tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
1.252     mycroft  1251:        tf->tf_edi = 0;
                   1252:        tf->tf_esi = 0;
1.154     mycroft  1253:        tf->tf_ebp = 0;
1.508     thorpej  1254:        tf->tf_ebx = (int)l->l_proc->p_psstr;
1.252     mycroft  1255:        tf->tf_edx = 0;
                   1256:        tf->tf_ecx = 0;
                   1257:        tf->tf_eax = 0;
1.154     mycroft  1258:        tf->tf_eip = pack->ep_entry;
1.533     chs      1259:        tf->tf_cs = pmap->pm_hiexec > I386_MAX_EXE_ADDR ?
                   1260:            LSEL(LUCODEBIG_SEL, SEL_UPL) : LSEL(LUCODE_SEL, SEL_UPL);
1.154     mycroft  1261:        tf->tf_eflags = PSL_USERSET;
                   1262:        tf->tf_esp = stack;
                   1263:        tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
1.1       cgd      1264: }
                   1265:
                   1266: /*
1.55      brezak   1267:  * Initialize segments and descriptor tables
1.1       cgd      1268:  */
                   1269:
1.516     fvdl     1270: union  descriptor *gdt, *ldt;
                   1271: struct gate_descriptor *idt;
1.498     fvdl     1272: char idt_allocmap[NIDT];
                   1273: struct simplelock idt_lock = SIMPLELOCK_INITIALIZER;
1.275     mycroft  1274: #ifdef I586_CPU
                   1275: union  descriptor *pentium_idt;
                   1276: #endif
1.573     yamt     1277: struct user *proc0paddr;
                   1278: extern vaddr_t proc0uarea;
1.49      brezak   1279:
1.178     mycroft  1280: void
1.551     junyoung 1281: setgate(struct gate_descriptor *gd, void *func, int args, int type, int dpl,
                   1282:     int sel)
1.178     mycroft  1283: {
1.1       cgd      1284:
1.178     mycroft  1285:        gd->gd_looffset = (int)func;
1.489     fvdl     1286:        gd->gd_selector = sel;
1.178     mycroft  1287:        gd->gd_stkcpy = args;
                   1288:        gd->gd_xx = 0;
                   1289:        gd->gd_type = type;
                   1290:        gd->gd_dpl = dpl;
                   1291:        gd->gd_p = 1;
                   1292:        gd->gd_hioffset = (int)func >> 16;
                   1293: }
                   1294:
                   1295: void
1.551     junyoung 1296: unsetgate(struct gate_descriptor *gd)
1.484     fvdl     1297: {
                   1298:        gd->gd_p = 0;
                   1299:        gd->gd_hioffset = 0;
                   1300:        gd->gd_looffset = 0;
                   1301:        gd->gd_selector = 0;
                   1302:        gd->gd_xx = 0;
                   1303:        gd->gd_stkcpy = 0;
                   1304:        gd->gd_type = 0;
                   1305:        gd->gd_dpl = 0;
                   1306: }
                   1307:
                   1308:
                   1309: void
1.551     junyoung 1310: setregion(struct region_descriptor *rd, void *base, size_t limit)
1.178     mycroft  1311: {
                   1312:
                   1313:        rd->rd_limit = (int)limit;
                   1314:        rd->rd_base = (int)base;
                   1315: }
1.1       cgd      1316:
1.174     mycroft  1317: void
1.551     junyoung 1318: setsegment(struct segment_descriptor *sd, void *base, size_t limit, int type,
                   1319:     int dpl, int def32, int gran)
1.174     mycroft  1320: {
1.1       cgd      1321:
1.174     mycroft  1322:        sd->sd_lolimit = (int)limit;
                   1323:        sd->sd_lobase = (int)base;
                   1324:        sd->sd_type = type;
                   1325:        sd->sd_dpl = dpl;
                   1326:        sd->sd_p = 1;
                   1327:        sd->sd_hilimit = (int)limit >> 16;
                   1328:        sd->sd_xx = 0;
                   1329:        sd->sd_def32 = def32;
                   1330:        sd->sd_gran = gran;
                   1331:        sd->sd_hibase = (int)base >> 24;
                   1332: }
1.1       cgd      1333:
                   1334: #define        IDTVEC(name)    __CONCAT(X, name)
1.542     junyoung 1335: typedef void (vector)(void);
1.299     mycroft  1336: extern vector IDTVEC(syscall);
                   1337: extern vector IDTVEC(osyscall);
                   1338: extern vector *IDTVEC(exceptions)[];
1.333     christos 1339: #ifdef COMPAT_SVR4
                   1340: extern vector IDTVEC(svr4_fasttrap);
                   1341: #endif /* COMPAT_SVR4 */
1.447     christos 1342: #ifdef COMPAT_MACH
                   1343: extern vector IDTVEC(mach_trap);
                   1344: #endif
1.1       cgd      1345:
1.381     thorpej  1346: #define        KBTOB(x)        ((size_t)(x) * 1024UL)
                   1347:
1.484     fvdl     1348: void cpu_init_idt()
                   1349: {
                   1350:        struct region_descriptor region;
                   1351: #ifdef I586_CPU
                   1352:        setregion(&region, pentium_idt, NIDT * sizeof(idt[0]) - 1);
                   1353: #else
                   1354:        setregion(&region, idt, NIDT * sizeof(idt[0]) - 1);
                   1355: #endif
1.565     simonb   1356:        lidt(&region);
1.484     fvdl     1357: }
                   1358:
1.433     kanaoka  1359: void
1.568     perry    1360: add_mem_cluster(uint64_t seg_start, uint64_t seg_end, uint32_t type)
1.433     kanaoka  1361: {
                   1362:        extern struct extent *iomem_ex;
1.492     kanaoka  1363:        int i;
1.433     kanaoka  1364:
                   1365:        if (seg_end > 0x100000000ULL) {
                   1366:                printf("WARNING: skipping large "
                   1367:                    "memory map entry: "
                   1368:                    "0x%qx/0x%qx/0x%x\n",
                   1369:                    seg_start,
                   1370:                    (seg_end - seg_start),
                   1371:                    type);
                   1372:                return;
                   1373:        }
                   1374:
                   1375:        /*
                   1376:         * XXX Chop the last page off the size so that
                   1377:         * XXX it can fit in avail_end.
                   1378:         */
                   1379:        if (seg_end == 0x100000000ULL)
                   1380:                seg_end -= PAGE_SIZE;
                   1381:
                   1382:        if (seg_end <= seg_start)
                   1383:                return;
                   1384:
1.492     kanaoka  1385:        for (i = 0; i < mem_cluster_cnt; i++) {
                   1386:                if ((mem_clusters[i].start == round_page(seg_start))
                   1387:                    && (mem_clusters[i].size
                   1388:                            == trunc_page(seg_end) - mem_clusters[i].start)) {
                   1389: #ifdef DEBUG_MEMLOAD
                   1390:                        printf("WARNING: skipping duplicate segment entry\n");
                   1391: #endif
                   1392:                        return;
                   1393:                }
                   1394:        }
                   1395:
1.433     kanaoka  1396:        /*
                   1397:         * Allocate the physical addresses used by RAM
                   1398:         * from the iomem extent map.  This is done before
                   1399:         * the addresses are page rounded just to make
                   1400:         * sure we get them all.
                   1401:         */
                   1402:        if (extent_alloc_region(iomem_ex, seg_start,
                   1403:            seg_end - seg_start, EX_NOWAIT)) {
                   1404:                /* XXX What should we do? */
                   1405:                printf("WARNING: CAN'T ALLOCATE "
                   1406:                    "MEMORY SEGMENT "
                   1407:                    "(0x%qx/0x%qx/0x%x) FROM "
                   1408:                    "IOMEM EXTENT MAP!\n",
                   1409:                    seg_start, seg_end - seg_start, type);
1.492     kanaoka  1410:                return;
1.433     kanaoka  1411:        }
                   1412:
                   1413:        /*
                   1414:         * If it's not free memory, skip it.
                   1415:         */
                   1416:        if (type != BIM_Memory)
                   1417:                return;
                   1418:
                   1419:        /* XXX XXX XXX */
                   1420:        if (mem_cluster_cnt >= VM_PHYSSEG_MAX)
1.579     cube     1421:                panic("init386: too many memory segments "
                   1422:                    "(increase VM_PHYSSEG_MAX)");
1.433     kanaoka  1423:
                   1424:        seg_start = round_page(seg_start);
                   1425:        seg_end = trunc_page(seg_end);
                   1426:
                   1427:        if (seg_start == seg_end)
                   1428:                return;
                   1429:
                   1430:        mem_clusters[mem_cluster_cnt].start = seg_start;
                   1431:        mem_clusters[mem_cluster_cnt].size =
                   1432:            seg_end - seg_start;
                   1433:
                   1434:        if (avail_end < seg_end)
                   1435:                avail_end = seg_end;
                   1436:        physmem += atop(mem_clusters[mem_cluster_cnt].size);
                   1437:        mem_cluster_cnt++;
                   1438: }
                   1439:
1.59      mycroft  1440: void
1.484     fvdl     1441: initgdt(union descriptor *tgdt)
                   1442: {
                   1443:        struct region_descriptor region;
                   1444:        gdt = tgdt;
                   1445:        memset(gdt, 0, NGDT*sizeof(*gdt));
                   1446:        /* make gdt gates and memory segments */
                   1447:        setsegment(&gdt[GCODE_SEL].sd, 0, 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1);
                   1448:        setsegment(&gdt[GDATA_SEL].sd, 0, 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1);
1.533     chs      1449:        setsegment(&gdt[GUCODE_SEL].sd, 0, x86_btop(I386_MAX_EXE_ADDR) - 1,
                   1450:            SDT_MEMERA, SEL_UPL, 1, 1);
                   1451:        setsegment(&gdt[GUCODEBIG_SEL].sd, 0, x86_btop(VM_MAXUSER_ADDRESS) - 1,
1.484     fvdl     1452:            SDT_MEMERA, SEL_UPL, 1, 1);
1.514     fvdl     1453:        setsegment(&gdt[GUDATA_SEL].sd, 0, x86_btop(VM_MAXUSER_ADDRESS) - 1,
1.484     fvdl     1454:            SDT_MEMRWA, SEL_UPL, 1, 1);
                   1455: #ifdef COMPAT_MACH
                   1456:        setgate(&gdt[GMACHCALLS_SEL].gd, &IDTVEC(mach_trap), 1,
1.489     fvdl     1457:            SDT_SYS386CGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1.484     fvdl     1458: #endif
                   1459: #if NBIOSCALL > 0
                   1460:        /* bios trampoline GDT entries */
                   1461:        setsegment(&gdt[GBIOSCODE_SEL].sd, 0, 0xfffff, SDT_MEMERA, SEL_KPL, 0,
                   1462:            0);
                   1463:        setsegment(&gdt[GBIOSDATA_SEL].sd, 0, 0xfffff, SDT_MEMRWA, SEL_KPL, 0,
                   1464:            0);
                   1465: #endif
                   1466:        setsegment(&gdt[GCPU_SEL].sd, &cpu_info_primary,
                   1467:            sizeof(struct cpu_info)-1, SDT_MEMRWA, SEL_KPL, 1, 1);
                   1468:
                   1469:        setregion(&region, gdt, NGDT * sizeof(gdt[0]) - 1);
                   1470:        lgdt(&region);
                   1471: }
                   1472:
                   1473: void
1.551     junyoung 1474: init386(paddr_t first_avail)
1.2       cgd      1475: {
1.484     fvdl     1476:        union descriptor *tgdt;
1.542     junyoung 1477:        extern void consinit(void);
1.375     drochner 1478:        extern struct extent *iomem_ex;
1.401     thorpej  1479:        struct btinfo_memmap *bim;
1.381     thorpej  1480:        struct region_descriptor region;
1.401     thorpej  1481:        int x, first16q;
1.568     perry    1482:        uint64_t seg_start, seg_end;
                   1483:        uint64_t seg_start1, seg_end1;
1.473     tshiozak 1484:        paddr_t realmode_reserved_start;
                   1485:        psize_t realmode_reserved_size;
                   1486:        int needs_earlier_install_pte0;
1.436     jdolecek 1487: #if NBIOSCALL > 0
                   1488:        extern int biostramp_image_size;
                   1489:        extern u_char biostramp_image[];
                   1490: #endif
1.1       cgd      1491:
1.484     fvdl     1492:        cpu_probe_features(&cpu_info_primary);
                   1493:        cpu_feature = cpu_info_primary.ci_feature_flags;
1.553     lukem    1494:        cpu_feature2 = cpu_info_primary.ci_feature2_flags;
1.484     fvdl     1495:
1.573     yamt     1496:        proc0paddr = UAREA_TO_USER(proc0uarea);
1.508     thorpej  1497:        lwp0.l_addr = proc0paddr;
                   1498:        cpu_info_primary.ci_curpcb = &lwp0.l_addr->u_pcb;
1.275     mycroft  1499:
1.514     fvdl     1500:        x86_bus_space_init();
1.84      cgd      1501:        consinit();     /* XXX SHOULD NOT BE DONE HERE */
1.401     thorpej  1502:        /*
                   1503:         * Initailize PAGE_SIZE-dependent variables.
                   1504:         */
                   1505:        uvm_setpagesize();
1.450     thorpej  1506:
                   1507:        /*
                   1508:         * Saving SSE registers won't work if the save area isn't
                   1509:         * 16-byte aligned.
                   1510:         */
                   1511:        if (offsetof(struct user, u_pcb.pcb_savefpu) & 0xf)
                   1512:                panic("init386: pcb_savefpu not 16-byte aligned");
1.440     thorpej  1513:
                   1514:        /*
                   1515:         * Start with 2 color bins -- this is just a guess to get us
                   1516:         * started.  We'll recolor when we determine the largest cache
                   1517:         * sizes on the system.
                   1518:         */
                   1519:        uvmexp.ncolors = 2;
1.401     thorpej  1520:
1.473     tshiozak 1521:        /*
1.484     fvdl     1522:         * BIOS leaves data in physical page 0
                   1523:         * Even if it didn't, our VM system doesn't like using zero as a
                   1524:         * physical page number.
                   1525:         * We may also need pages in low memory (one each) for secondary CPU
                   1526:         * startup, for BIOS calls, and for ACPI, plus a page table page to map
                   1527:         * them into the first few pages of the kernel's pmap.
1.473     tshiozak 1528:         */
                   1529:        avail_start = PAGE_SIZE;
                   1530:
                   1531:        /*
                   1532:         * reserve memory for real-mode call
                   1533:         */
                   1534:        needs_earlier_install_pte0 = 0;
                   1535:        realmode_reserved_start = 0;
                   1536:        realmode_reserved_size = 0;
1.414     thorpej  1537: #if NBIOSCALL > 0
1.473     tshiozak 1538:        /* save us a page for trampoline code */
                   1539:        realmode_reserved_size += PAGE_SIZE;
                   1540:        needs_earlier_install_pte0 = 1;
                   1541: #endif
1.484     fvdl     1542: #ifdef MULTIPROCESSOR                                           /* XXX */
                   1543:        KASSERT(avail_start == PAGE_SIZE);                       /* XXX */
                   1544:        if (realmode_reserved_size < MP_TRAMPOLINE)              /* XXX */
                   1545:                realmode_reserved_size = MP_TRAMPOLINE;          /* XXX */
                   1546:        needs_earlier_install_pte0 = 1;                          /* XXX */
                   1547: #endif                                                          /* XXX */
1.473     tshiozak 1548: #if NACPI > 0
                   1549:        /* trampoline code for wake handler */
                   1550:        realmode_reserved_size += ptoa(acpi_md_get_npages_of_wakecode()+1);
                   1551:        needs_earlier_install_pte0 = 1;
                   1552: #endif
                   1553:        if (needs_earlier_install_pte0) {
                   1554:                /* page table for directory entry 0 */
                   1555:                realmode_reserved_size += PAGE_SIZE;
                   1556:        }
                   1557:        if (realmode_reserved_size>0) {
                   1558:                realmode_reserved_start = avail_start;
                   1559:                avail_start += realmode_reserved_size;
                   1560:        }
1.414     thorpej  1561:
1.492     kanaoka  1562: #ifdef DEBUG_MEMLOAD
                   1563:        printf("mem_cluster_count: %d\n", mem_cluster_cnt);
                   1564: #endif
                   1565:
1.401     thorpej  1566:        /*
                   1567:         * Call pmap initialization to make new kernel address space.
                   1568:         * We must do this before loading pages into the VM system.
                   1569:         */
1.314     thorpej  1570:        pmap_bootstrap((vaddr_t)atdevbase + IOM_SIZE);
1.2       cgd      1571:
1.401     thorpej  1572:        /*
                   1573:         * Check to see if we have a memory map from the BIOS (passed
                   1574:         * to us by the boot program.
                   1575:         */
1.577     mrg      1576:        if ((biosmem_implicit || (biosbasemem == 0 && biosextmem == 0)) &&
1.575     mrg      1577:            (bim = lookup_bootinfo(BTINFO_MEMMAP)) != NULL && bim->num > 0) {
1.463     enami    1578: #ifdef DEBUG_MEMLOAD
1.401     thorpej  1579:                printf("BIOS MEMORY MAP (%d ENTRIES):\n", bim->num);
                   1580: #endif
                   1581:                for (x = 0; x < bim->num; x++) {
1.463     enami    1582: #ifdef DEBUG_MEMLOAD
1.401     thorpej  1583:                        printf("    addr 0x%qx  size 0x%qx  type 0x%x\n",
                   1584:                            bim->entry[x].addr,
                   1585:                            bim->entry[x].size,
                   1586:                            bim->entry[x].type);
                   1587: #endif
                   1588:
                   1589:                        /*
                   1590:                         * If the segment is not memory, skip it.
                   1591:                         */
                   1592:                        switch (bim->entry[x].type) {
                   1593:                        case BIM_Memory:
                   1594:                        case BIM_ACPI:
                   1595:                        case BIM_NVS:
                   1596:                                break;
                   1597:                        default:
                   1598:                                continue;
                   1599:                        }
                   1600:
                   1601:                        /*
                   1602:                         * Sanity check the entry.
                   1603:                         * XXX Need to handle uint64_t in extent code
                   1604:                         * XXX and 64-bit physical addresses in i386
                   1605:                         * XXX port.
                   1606:                         */
                   1607:                        seg_start = bim->entry[x].addr;
                   1608:                        seg_end = bim->entry[x].addr + bim->entry[x].size;
                   1609:
1.432     kanaoka  1610:                        /*
1.433     kanaoka  1611:                         *   Avoid Compatibility Holes.
                   1612:                         * XXX  Holes within memory space that allow access
                   1613:                         * XXX to be directed to the PC-compatible frame buffer
1.484     fvdl     1614:                         * XXX (0xa0000-0xbffff),to adapter ROM space
1.433     kanaoka  1615:                         * XXX (0xc0000-0xdffff), and to system BIOS space
                   1616:                         * XXX (0xe0000-0xfffff).
                   1617:                         * XXX  Some laptop(for example,Toshiba Satellite2550X)
                   1618:                         * XXX report this area and occurred problems,
                   1619:                         * XXX so we avoid this area.
1.432     kanaoka  1620:                         */
1.433     kanaoka  1621:                        if (seg_start < 0x100000 && seg_end > 0xa0000) {
                   1622:                                printf("WARNING: memory map entry overlaps "
                   1623:                                    "with ``Compatibility Holes'': "
                   1624:                                    "0x%qx/0x%qx/0x%x\n", seg_start,
                   1625:                                    seg_end - seg_start, bim->entry[x].type);
                   1626:                                add_mem_cluster(seg_start, 0xa0000,
                   1627:                                    bim->entry[x].type);
                   1628:                                add_mem_cluster(0x100000, seg_end,
1.401     thorpej  1629:                                    bim->entry[x].type);
1.433     kanaoka  1630:                        } else
                   1631:                                add_mem_cluster(seg_start, seg_end,
1.401     thorpej  1632:                                    bim->entry[x].type);
                   1633:                }
1.421     aymeric  1634:        }
1.575     mrg      1635:
1.421     aymeric  1636:        /*
                   1637:         * If the loop above didn't find any valid segment, fall back to
                   1638:         * former code.
                   1639:         */
                   1640:        if (mem_cluster_cnt == 0) {
1.401     thorpej  1641:                /*
                   1642:                 * Allocate the physical addresses used by RAM from the iomem
                   1643:                 * extent map.  This is done before the addresses are
                   1644:                 * page rounded just to make sure we get them all.
                   1645:                 */
                   1646:                if (extent_alloc_region(iomem_ex, 0, KBTOB(biosbasemem),
                   1647:                    EX_NOWAIT)) {
                   1648:                        /* XXX What should we do? */
                   1649:                        printf("WARNING: CAN'T ALLOCATE BASE MEMORY FROM "
                   1650:                            "IOMEM EXTENT MAP!\n");
                   1651:                }
                   1652:                mem_clusters[0].start = 0;
                   1653:                mem_clusters[0].size = trunc_page(KBTOB(biosbasemem));
                   1654:                physmem += atop(mem_clusters[0].size);
                   1655:                if (extent_alloc_region(iomem_ex, IOM_END, KBTOB(biosextmem),
                   1656:                    EX_NOWAIT)) {
                   1657:                        /* XXX What should we do? */
                   1658:                        printf("WARNING: CAN'T ALLOCATE EXTENDED MEMORY FROM "
                   1659:                            "IOMEM EXTENT MAP!\n");
                   1660:                }
                   1661: #if NISADMA > 0
                   1662:                /*
                   1663:                 * Some motherboards/BIOSes remap the 384K of RAM that would
                   1664:                 * normally be covered by the ISA hole to the end of memory
                   1665:                 * so that it can be used.  However, on a 16M system, this
                   1666:                 * would cause bounce buffers to be allocated and used.
                   1667:                 * This is not desirable behaviour, as more than 384K of
                   1668:                 * bounce buffers might be allocated.  As a work-around,
                   1669:                 * we round memory down to the nearest 1M boundary if
                   1670:                 * we're using any isadma devices and the remapped memory
                   1671:                 * is what puts us over 16M.
                   1672:                 */
                   1673:                if (biosextmem > (15*1024) && biosextmem < (16*1024)) {
                   1674:                        char pbuf[9];
                   1675:
                   1676:                        format_bytes(pbuf, sizeof(pbuf),
                   1677:                            biosextmem - (15*1024));
                   1678:                        printf("Warning: ignoring %s of remapped memory\n",
                   1679:                            pbuf);
                   1680:                        biosextmem = (15*1024);
                   1681:                }
                   1682: #endif
                   1683:                mem_clusters[1].start = IOM_END;
                   1684:                mem_clusters[1].size = trunc_page(KBTOB(biosextmem));
                   1685:                physmem += atop(mem_clusters[1].size);
                   1686:
                   1687:                mem_cluster_cnt = 2;
                   1688:
                   1689:                avail_end = IOM_END + trunc_page(KBTOB(biosextmem));
                   1690:        }
                   1691:        /*
                   1692:         * If we have 16M of RAM or less, just put it all on
                   1693:         * the default free list.  Otherwise, put the first
                   1694:         * 16M of RAM on a lower priority free list (so that
                   1695:         * all of the ISA DMA'able memory won't be eaten up
                   1696:         * first-off).
                   1697:         */
                   1698:        if (avail_end <= (16 * 1024 * 1024))
                   1699:                first16q = VM_FREELIST_DEFAULT;
                   1700:        else
                   1701:                first16q = VM_FREELIST_FIRST16;
                   1702:
                   1703:        /* Make sure the end of the space used by the kernel is rounded. */
                   1704:        first_avail = round_page(first_avail);
                   1705:
                   1706:        /*
                   1707:         * Now, load the memory clusters (which have already been
                   1708:         * rounded and truncated) into the VM system.
                   1709:         *
                   1710:         * NOTE: WE ASSUME THAT MEMORY STARTS AT 0 AND THAT THE KERNEL
                   1711:         * IS LOADED AT IOM_END (1M).
                   1712:         */
                   1713:        for (x = 0; x < mem_cluster_cnt; x++) {
                   1714:                seg_start = mem_clusters[x].start;
                   1715:                seg_end = mem_clusters[x].start + mem_clusters[x].size;
                   1716:                seg_start1 = 0;
                   1717:                seg_end1 = 0;
                   1718:
                   1719:                /*
                   1720:                 * Skip memory before our available starting point.
                   1721:                 */
                   1722:                if (seg_end <= avail_start)
                   1723:                        continue;
                   1724:
                   1725:                if (avail_start >= seg_start && avail_start < seg_end) {
                   1726:                        if (seg_start != 0)
                   1727:                                panic("init386: memory doesn't start at 0");
                   1728:                        seg_start = avail_start;
                   1729:                        if (seg_start == seg_end)
                   1730:                                continue;
                   1731:                }
                   1732:
                   1733:                /*
                   1734:                 * If this segment contains the kernel, split it
                   1735:                 * in two, around the kernel.
                   1736:                 */
                   1737:                if (seg_start <= IOM_END && first_avail <= seg_end) {
                   1738:                        seg_start1 = first_avail;
                   1739:                        seg_end1 = seg_end;
                   1740:                        seg_end = IOM_END;
                   1741:                }
                   1742:
                   1743:                /* First hunk */
                   1744:                if (seg_start != seg_end) {
1.480     erh      1745:                        if (seg_start < (16 * 1024 * 1024) &&
1.401     thorpej  1746:                            first16q != VM_FREELIST_DEFAULT) {
1.568     perry    1747:                                uint64_t tmp;
1.401     thorpej  1748:
                   1749:                                if (seg_end > (16 * 1024 * 1024))
                   1750:                                        tmp = (16 * 1024 * 1024);
                   1751:                                else
                   1752:                                        tmp = seg_end;
1.492     kanaoka  1753:
                   1754:                                if (tmp != seg_start) {
1.463     enami    1755: #ifdef DEBUG_MEMLOAD
1.492     kanaoka  1756:                                        printf("loading 0x%qx-0x%qx "
                   1757:                                            "(0x%lx-0x%lx)\n",
                   1758:                                            seg_start, tmp,
                   1759:                                            atop(seg_start), atop(tmp));
                   1760: #endif
                   1761:                                        uvm_page_physload(atop(seg_start),
                   1762:                                            atop(tmp), atop(seg_start),
                   1763:                                            atop(tmp), first16q);
                   1764:                                }
1.401     thorpej  1765:                                seg_start = tmp;
                   1766:                        }
1.411     enami    1767:
                   1768:                        if (seg_start != seg_end) {
1.463     enami    1769: #ifdef DEBUG_MEMLOAD
1.411     enami    1770:                                printf("loading 0x%qx-0x%qx (0x%lx-0x%lx)\n",
                   1771:                                    seg_start, seg_end,
                   1772:                                    atop(seg_start), atop(seg_end));
                   1773: #endif
                   1774:                                uvm_page_physload(atop(seg_start),
                   1775:                                    atop(seg_end), atop(seg_start),
                   1776:                                    atop(seg_end), VM_FREELIST_DEFAULT);
                   1777:                        }
1.401     thorpej  1778:                }
                   1779:
                   1780:                /* Second hunk */
                   1781:                if (seg_start1 != seg_end1) {
1.480     erh      1782:                        if (seg_start1 < (16 * 1024 * 1024) &&
1.401     thorpej  1783:                            first16q != VM_FREELIST_DEFAULT) {
1.568     perry    1784:                                uint64_t tmp;
1.401     thorpej  1785:
                   1786:                                if (seg_end1 > (16 * 1024 * 1024))
                   1787:                                        tmp = (16 * 1024 * 1024);
                   1788:                                else
                   1789:                                        tmp = seg_end1;
1.492     kanaoka  1790:
                   1791:                                if (tmp != seg_start1) {
1.463     enami    1792: #ifdef DEBUG_MEMLOAD
1.492     kanaoka  1793:                                        printf("loading 0x%qx-0x%qx "
                   1794:                                            "(0x%lx-0x%lx)\n",
                   1795:                                            seg_start1, tmp,
                   1796:                                            atop(seg_start1), atop(tmp));
                   1797: #endif
                   1798:                                        uvm_page_physload(atop(seg_start1),
                   1799:                                            atop(tmp), atop(seg_start1),
                   1800:                                            atop(tmp), first16q);
                   1801:                                }
1.401     thorpej  1802:                                seg_start1 = tmp;
                   1803:                        }
1.412     enami    1804:
                   1805:                        if (seg_start1 != seg_end1) {
1.463     enami    1806: #ifdef DEBUG_MEMLOAD
1.412     enami    1807:                                printf("loading 0x%qx-0x%qx (0x%lx-0x%lx)\n",
                   1808:                                    seg_start1, seg_end1,
                   1809:                                    atop(seg_start1), atop(seg_end1));
                   1810: #endif
                   1811:                                uvm_page_physload(atop(seg_start1),
                   1812:                                    atop(seg_end1), atop(seg_start1),
                   1813:                                    atop(seg_end1), VM_FREELIST_DEFAULT);
                   1814:                        }
1.401     thorpej  1815:                }
                   1816:        }
                   1817:
                   1818:        /*
                   1819:         * Steal memory for the message buffer (at end of core).
                   1820:         */
                   1821:        {
1.543     mycroft  1822:                struct vm_physseg *vps;
1.401     thorpej  1823:                psize_t sz = round_page(MSGBUFSIZE);
                   1824:                psize_t reqsz = sz;
                   1825:
1.586.2.1! tron     1826:        search_again:
1.401     thorpej  1827:                for (x = 0; x < vm_nphysseg; x++) {
                   1828:                        vps = &vm_physmem[x];
                   1829:                        if (ptoa(vps->avail_end) == avail_end)
1.543     mycroft  1830:                                goto found;
1.401     thorpej  1831:                }
1.543     mycroft  1832:                panic("init386: can't find end of memory");
1.401     thorpej  1833:
1.543     mycroft  1834:        found:
1.401     thorpej  1835:                /* Shrink so it'll fit in the last segment. */
                   1836:                if ((vps->avail_end - vps->avail_start) < atop(sz))
                   1837:                        sz = ptoa(vps->avail_end - vps->avail_start);
                   1838:
                   1839:                vps->avail_end -= atop(sz);
                   1840:                vps->end -= atop(sz);
1.586.2.1! tron     1841:                msgbuf_p_seg[msgbuf_p_cnt].sz = sz;
        !          1842:                msgbuf_p_seg[msgbuf_p_cnt++].paddr = ptoa(vps->avail_end);
1.401     thorpej  1843:
                   1844:                /* Remove the last segment if it now has no pages. */
                   1845:                if (vps->start == vps->end) {
                   1846:                        for (vm_nphysseg--; x < vm_nphysseg; x++)
                   1847:                                vm_physmem[x] = vm_physmem[x + 1];
                   1848:                }
                   1849:
                   1850:                /* Now find where the new avail_end is. */
                   1851:                for (avail_end = 0, x = 0; x < vm_nphysseg; x++)
                   1852:                        if (vm_physmem[x].avail_end > avail_end)
                   1853:                                avail_end = vm_physmem[x].avail_end;
                   1854:                avail_end = ptoa(avail_end);
                   1855:
1.586.2.1! tron     1856:                if (sz != reqsz) {
        !          1857:                        reqsz -= sz;
        !          1858:                        if (msgbuf_p_cnt != VM_PHYSSEG_MAX) {
        !          1859:                /* if still segments available, get memory from next one ... */
        !          1860:                              sz = reqsz;
        !          1861:                              goto search_again;
        !          1862:                        }
1.401     thorpej  1863:                /* Warn if the message buffer had to be shrunk. */
                   1864:                        printf("WARNING: %ld bytes not available for msgbuf "
1.586.2.1! tron     1865:                            "in last cluster (%ld used)\n", (long)MSGBUFSIZE, MSGBUFSIZE - reqsz);
        !          1866:                }
1.401     thorpej  1867:        }
                   1868:
1.473     tshiozak 1869:        /*
                   1870:         * install PT page for the first 4M if needed.
                   1871:         */
                   1872:        if (needs_earlier_install_pte0) {
                   1873:                paddr_t paddr;
                   1874: #ifdef DIAGNOSTIC
                   1875:                if (realmode_reserved_size < PAGE_SIZE) {
                   1876:                        panic("cannot steal memory for first 4M PT page.");
                   1877:                }
                   1878: #endif
                   1879:                paddr=realmode_reserved_start+realmode_reserved_size-PAGE_SIZE;
1.561     yamt     1880:                pmap_kenter_pa((vaddr_t)vtopte(0), paddr,
                   1881:                           VM_PROT_READ|VM_PROT_WRITE);
1.473     tshiozak 1882:                pmap_update(pmap_kernel());
                   1883:                /* make sure it is clean before using */
                   1884:                memset(vtopte(0), 0, PAGE_SIZE);
                   1885:                realmode_reserved_size -= PAGE_SIZE;
                   1886:        }
                   1887:
1.295     drochner 1888: #if NBIOSCALL > 0
1.436     jdolecek 1889:        /*
                   1890:         * this should be caught at kernel build time, but put it here
                   1891:         * in case someone tries to fake it out...
                   1892:         */
                   1893: #ifdef DIAGNOSTIC
1.473     tshiozak 1894:        if (realmode_reserved_start > BIOSTRAMP_BASE ||
1.474     tron     1895:            (realmode_reserved_start+realmode_reserved_size) < (BIOSTRAMP_BASE+
1.473     tshiozak 1896:                                                               PAGE_SIZE)) {
                   1897:            panic("cannot steal memory for PT page of bioscall.");
                   1898:        }
1.436     jdolecek 1899:        if (biostramp_image_size > PAGE_SIZE)
1.483     provos   1900:            panic("biostramp_image_size too big: %x vs. %x",
1.436     jdolecek 1901:                  biostramp_image_size, PAGE_SIZE);
                   1902: #endif
                   1903:        pmap_kenter_pa((vaddr_t)BIOSTRAMP_BASE, /* virtual */
                   1904:                       (paddr_t)BIOSTRAMP_BASE, /* physical */
                   1905:                       VM_PROT_ALL);            /* protection */
1.456     chris    1906:        pmap_update(pmap_kernel());
1.436     jdolecek 1907:        memcpy((caddr_t)BIOSTRAMP_BASE, biostramp_image, biostramp_image_size);
                   1908: #ifdef DEBUG_BIOSCALL
                   1909:        printf("biostramp installed @ %x\n", BIOSTRAMP_BASE);
                   1910: #endif
1.473     tshiozak 1911:        realmode_reserved_size  -= PAGE_SIZE;
                   1912:        realmode_reserved_start += PAGE_SIZE;
                   1913: #endif
                   1914:
                   1915: #if NACPI > 0
                   1916:        /*
                   1917:         * Steal memory for the acpi wake code
                   1918:         */
                   1919:        {
                   1920:                paddr_t paddr, p;
                   1921:                psize_t sz;
                   1922:                int npg;
                   1923:
                   1924:                paddr = realmode_reserved_start;
                   1925:                npg = acpi_md_get_npages_of_wakecode();
                   1926:                sz = ptoa(npg);
                   1927: #ifdef DIAGNOSTIC
                   1928:                if (realmode_reserved_size < sz) {
                   1929:                        panic("cannot steal memory for ACPI wake code.");
                   1930:                }
                   1931: #endif
                   1932:
                   1933:                /* identical mapping */
                   1934:                p = paddr;
                   1935:                for (x=0; x<npg; x++) {
                   1936:                        printf("kenter: 0x%08X\n", (unsigned)p);
                   1937:                        pmap_kenter_pa((vaddr_t)p, p, VM_PROT_ALL);
                   1938:                        p += PAGE_SIZE;
                   1939:                }
                   1940:                pmap_update(pmap_kernel());
                   1941:
                   1942:                acpi_md_install_wakecode(paddr);
                   1943:
                   1944:                realmode_reserved_size  -= sz;
                   1945:                realmode_reserved_start += sz;
                   1946:        }
1.295     drochner 1947: #endif
1.59      mycroft  1948:
1.561     yamt     1949:        pmap_kenter_pa(idt_vaddr, idt_paddr, VM_PROT_READ|VM_PROT_WRITE);
1.456     chris    1950:        pmap_update(pmap_kernel());
1.484     fvdl     1951:        memset((void *)idt_vaddr, 0, PAGE_SIZE);
                   1952:
1.516     fvdl     1953:        idt = (struct gate_descriptor *)idt_vaddr;
1.275     mycroft  1954: #ifdef I586_CPU
1.561     yamt     1955:        pmap_kenter_pa(pentium_idt_vaddr, idt_paddr, VM_PROT_READ);
1.275     mycroft  1956:        pentium_idt = (union descriptor *)pentium_idt_vaddr;
                   1957: #endif
1.484     fvdl     1958:        pmap_update(pmap_kernel());
                   1959:
                   1960:        tgdt = gdt;
1.516     fvdl     1961:        gdt = (union descriptor *)
                   1962:                    ((char *)idt + NIDT * sizeof (struct gate_descriptor));
1.275     mycroft  1963:        ldt = gdt + NGDT;
                   1964:
1.484     fvdl     1965:        memcpy(gdt, tgdt, NGDT*sizeof(*gdt));
1.275     mycroft  1966:
                   1967:        setsegment(&gdt[GLDT_SEL].sd, ldt, NLDT * sizeof(ldt[0]) - 1,
                   1968:            SDT_SYSLDT, SEL_KPL, 0, 0);
                   1969:
                   1970:        /* make ldt gates and memory segments */
                   1971:        setgate(&ldt[LSYS5CALLS_SEL].gd, &IDTVEC(osyscall), 1,
1.489     fvdl     1972:            SDT_SYS386CGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1.447     christos 1973:
1.275     mycroft  1974:        ldt[LUCODE_SEL] = gdt[GUCODE_SEL];
1.533     chs      1975:        ldt[LUCODEBIG_SEL] = gdt[GUCODEBIG_SEL];
1.275     mycroft  1976:        ldt[LUDATA_SEL] = gdt[GUDATA_SEL];
1.324     christos 1977:        ldt[LSOL26CALLS_SEL] = ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1.275     mycroft  1978:
                   1979:        /* exceptions */
1.498     fvdl     1980:        for (x = 0; x < 32; x++) {
1.516     fvdl     1981:                setgate(&idt[x], IDTVEC(exceptions)[x], 0, SDT_SYS386TGT,
1.489     fvdl     1982:                    (x == 3 || x == 4) ? SEL_UPL : SEL_KPL,
                   1983:                    GSEL(GCODE_SEL, SEL_KPL));
1.498     fvdl     1984:                idt_allocmap[x] = 1;
                   1985:        }
1.257     thorpej  1986:
1.275     mycroft  1987:        /* new-style interrupt gate for syscalls */
1.516     fvdl     1988:        setgate(&idt[128], &IDTVEC(syscall), 0, SDT_SYS386TGT, SEL_UPL,
1.489     fvdl     1989:            GSEL(GCODE_SEL, SEL_KPL));
1.498     fvdl     1990:        idt_allocmap[128] = 1;
1.333     christos 1991: #ifdef COMPAT_SVR4
1.516     fvdl     1992:        setgate(&idt[0xd2], &IDTVEC(svr4_fasttrap), 0, SDT_SYS386TGT,
1.489     fvdl     1993:            SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1.498     fvdl     1994:        idt_allocmap[0xd2] = 1;
1.333     christos 1995: #endif /* COMPAT_SVR4 */
1.264     mycroft  1996:
1.275     mycroft  1997:        setregion(&region, gdt, NGDT * sizeof(gdt[0]) - 1);
                   1998:        lgdt(&region);
1.484     fvdl     1999:
                   2000:        cpu_init_idt();
1.264     mycroft  2001:
1.522     ragge    2002: #if NKSYMS || defined(DDB) || defined(LKM)
1.308     tv       2003:        {
                   2004:                extern int end;
1.585     jmmv     2005:                boolean_t loaded;
1.336     christos 2006:                struct btinfo_symtab *symtab;
1.308     tv       2007:
1.522     ragge    2008: #ifdef DDB
1.484     fvdl     2009:                db_machine_init();
1.522     ragge    2010: #endif
1.484     fvdl     2011:
1.585     jmmv     2012: #if defined(MULTIBOOT)
                   2013:                loaded = multiboot_ksyms_init();
                   2014: #else
                   2015:                loaded = FALSE;
                   2016: #endif
                   2017:                if (!loaded) {
                   2018:                    symtab = lookup_bootinfo(BTINFO_SYMTAB);
                   2019:                    if (symtab) {
                   2020:                            symtab->ssym += KERNBASE;
                   2021:                            symtab->esym += KERNBASE;
                   2022:                            ksyms_init(symtab->nsym, (int *)symtab->ssym,
                   2023:                                (int *)symtab->esym);
                   2024:                    } else
                   2025:                            ksyms_init(*(int *)&end, ((int *)&end) + 1, esym);
1.336     christos 2026:                }
1.308     tv       2027:        }
1.522     ragge    2028: #endif
                   2029: #ifdef DDB
1.190     mycroft  2030:        if (boothowto & RB_KDB)
                   2031:                Debugger();
1.377     ws       2032: #endif
                   2033: #ifdef IPKDB
                   2034:        ipkdb_init();
                   2035:        if (boothowto & RB_KDB)
                   2036:                ipkdb_connect(0);
1.190     mycroft  2037: #endif
                   2038: #ifdef KGDB
1.243     drochner 2039:        kgdb_port_init();
1.235     thorpej  2040:        if (boothowto & RB_KDB) {
                   2041:                kgdb_debug_init = 1;
1.242     drochner 2042:                kgdb_connect(1);
1.235     thorpej  2043:        }
1.384     jdolecek 2044: #endif
                   2045:
                   2046: #if NMCA > 0
                   2047:        /* check for MCA bus, needed to be done before ISA stuff - if
                   2048:         * MCA is detected, ISA needs to use level triggered interrupts
                   2049:         * by default */
                   2050:        mca_busprobe();
1.190     mycroft  2051: #endif
1.275     mycroft  2052:
1.498     fvdl     2053:        intr_default_setup();
1.431     thorpej  2054:
                   2055:        /* Initialize software interrupts. */
                   2056:        softintr_init();
1.275     mycroft  2057:
1.498     fvdl     2058:        splraise(IPL_IPI);
1.275     mycroft  2059:        enable_intr();
                   2060:
                   2061:        if (physmem < btoc(2 * 1024 * 1024)) {
                   2062:                printf("warning: too little memory available; "
1.383     mycroft  2063:                       "have %lu bytes, want %lu bytes\n"
1.275     mycroft  2064:                       "running in degraded mode\n"
                   2065:                       "press a key to confirm\n\n",
1.383     mycroft  2066:                       ptoa(physmem), 2*1024*1024UL);
1.275     mycroft  2067:                cngetc();
                   2068:        }
1.507     jdolecek 2069:
                   2070: #ifdef __HAVE_CPU_MAXPROC
                   2071:        /* Make sure maxproc is sane */
                   2072:        if (maxproc > cpu_maxproc())
                   2073:                maxproc = cpu_maxproc();
                   2074: #endif
1.1       cgd      2075: }
                   2076:
1.107     deraadt  2077: #ifdef COMPAT_NOMID
                   2078: static int
1.566     christos 2079: exec_nomid(struct lwp *l, struct exec_package *epp)
1.31      cgd      2080: {
1.59      mycroft  2081:        int error;
                   2082:        u_long midmag, magic;
                   2083:        u_short mid;
1.80      cgd      2084:        struct exec *execp = epp->ep_hdr;
1.31      cgd      2085:
1.80      cgd      2086:        /* check on validity of epp->ep_hdr performed by exec_out_makecmds */
                   2087:
                   2088:        midmag = ntohl(execp->a_midmag);
1.59      mycroft  2089:        mid = (midmag >> 16) & 0xffff;
                   2090:        magic = midmag & 0xffff;
                   2091:
                   2092:        if (magic == 0) {
1.80      cgd      2093:                magic = (execp->a_midmag & 0xffff);
1.59      mycroft  2094:                mid = MID_ZERO;
                   2095:        }
                   2096:
                   2097:        midmag = mid << 16 | magic;
                   2098:
                   2099:        switch (midmag) {
                   2100:        case (MID_ZERO << 16) | ZMAGIC:
                   2101:                /*
                   2102:                 * 386BSD's ZMAGIC format:
                   2103:                 */
1.566     christos 2104:                error = exec_aout_prep_oldzmagic(l, epp);
1.59      mycroft  2105:                break;
                   2106:
                   2107:        case (MID_ZERO << 16) | QMAGIC:
                   2108:                /*
                   2109:                 * BSDI's QMAGIC format:
                   2110:                 * same as new ZMAGIC format, but with different magic number
                   2111:                 */
1.566     christos 2112:                error = exec_aout_prep_zmagic(l, epp);
1.59      mycroft  2113:                break;
                   2114:
1.202     christos 2115:        case (MID_ZERO << 16) | NMAGIC:
                   2116:                /*
                   2117:                 * BSDI's NMAGIC format:
                   2118:                 * same as NMAGIC format, but with different magic number
                   2119:                 * and with text starting at 0.
                   2120:                 */
1.566     christos 2121:                error = exec_aout_prep_oldnmagic(l, epp);
1.202     christos 2122:                break;
                   2123:
                   2124:        case (MID_ZERO << 16) | OMAGIC:
                   2125:                /*
                   2126:                 * BSDI's OMAGIC format:
                   2127:                 * same as OMAGIC format, but with different magic number
                   2128:                 * and with text starting at 0.
                   2129:                 */
1.566     christos 2130:                error = exec_aout_prep_oldomagic(l, epp);
1.202     christos 2131:                break;
                   2132:
1.59      mycroft  2133:        default:
                   2134:                error = ENOEXEC;
                   2135:        }
                   2136:
                   2137:        return error;
1.107     deraadt  2138: }
1.31      cgd      2139: #endif
1.107     deraadt  2140:
                   2141: /*
                   2142:  * cpu_exec_aout_makecmds():
1.549     wiz      2143:  *     CPU-dependent a.out format hook for execve().
1.107     deraadt  2144:  *
                   2145:  * Determine of the given exec package refers to something which we
                   2146:  * understand and, if so, set up the vmcmds for it.
                   2147:  *
                   2148:  * On the i386, old (386bsd) ZMAGIC binaries and BSDI QMAGIC binaries
                   2149:  * if COMPAT_NOMID is given as a kernel option.
                   2150:  */
                   2151: int
1.566     christos 2152: cpu_exec_aout_makecmds(struct lwp *l, struct exec_package *epp)
1.107     deraadt  2153: {
                   2154:        int error = ENOEXEC;
                   2155:
                   2156: #ifdef COMPAT_NOMID
1.566     christos 2157:        if ((error = exec_nomid(l, epp)) == 0)
1.107     deraadt  2158:                return error;
1.581     thorpej  2159: #else
                   2160:        (void) l;
                   2161:        (void) epp;
1.107     deraadt  2162: #endif /* ! COMPAT_NOMID */
                   2163:
                   2164:        return error;
1.31      cgd      2165: }
1.84      cgd      2166:
1.484     fvdl     2167: #include <dev/ic/mc146818reg.h>                /* for NVRAM POST */
                   2168: #include <i386/isa/nvram.h>            /* for NVRAM POST */
                   2169:
1.149     mycroft  2170: void
                   2171: cpu_reset()
                   2172: {
1.555     mycroft  2173:        struct region_descriptor region;
1.149     mycroft  2174:
1.224     mycroft  2175:        disable_intr();
                   2176:
1.227     mycroft  2177:        /*
1.484     fvdl     2178:         * Ensure the NVRAM reset byte contains something vaguely sane.
                   2179:         */
                   2180:
                   2181:        outb(IO_RTC, NVRAM_RESET);
                   2182:        outb(IO_RTC+1, NVRAM_RESET_RST);
                   2183:
                   2184:        /*
1.564     dyoung   2185:         * Reset AMD Geode SC1100.
                   2186:         *
1.565     simonb   2187:         * 1) Write PCI Configuration Address Register (0xcf8) to
                   2188:         *    select Function 0, Register 0x44: Bridge Configuration,
                   2189:         *    GPIO and LPC Configuration Register Space, Reset
                   2190:         *    Control Register.
1.564     dyoung   2191:         *
1.565     simonb   2192:         * 2) Write 0xf to PCI Configuration Data Register (0xcfc)
                   2193:         *    to reset IDE controller, IDE bus, and PCI bus, and
                   2194:         *    to trigger a system-wide reset.
1.564     dyoung   2195:         *
                   2196:         * See AMD Geode SC1100 Processor Data Book, Revision 2.0,
                   2197:         * sections 6.3.1, 6.3.2, and 6.4.1.
                   2198:         */
                   2199:        if (cpu_info_primary.ci_signature == 0x540) {
                   2200:                outl(0xcf8, 0x80009044ul);
                   2201:                outl(0xcfc, 0xf);
1.565     simonb   2202:        }
1.564     dyoung   2203:
                   2204:        /*
1.227     mycroft  2205:         * The keyboard controller has 4 random output pins, one of which is
                   2206:         * connected to the RESET pin on the CPU in many PCs.  We tell the
                   2207:         * keyboard controller to pulse this line a couple of times.
                   2208:         */
1.273     drochner 2209:        outb(IO_KBD + KBCMDP, KBC_PULSE0);
1.226     mycroft  2210:        delay(100000);
1.273     drochner 2211:        outb(IO_KBD + KBCMDP, KBC_PULSE0);
1.226     mycroft  2212:        delay(100000);
1.149     mycroft  2213:
                   2214:        /*
1.224     mycroft  2215:         * Try to cause a triple fault and watchdog reset by making the IDT
                   2216:         * invalid and causing a fault.
1.149     mycroft  2217:         */
1.313     perry    2218:        memset((caddr_t)idt, 0, NIDT * sizeof(idt[0]));
1.555     mycroft  2219:        setregion(&region, idt, NIDT * sizeof(idt[0]) - 1);
1.565     simonb   2220:        lidt(&region);
1.567     perry    2221:        __asm volatile("divl %0,%1" : : "q" (0), "a" (0));
1.149     mycroft  2222:
1.224     mycroft  2223: #if 0
1.149     mycroft  2224:        /*
                   2225:         * Try to cause a triple fault and watchdog reset by unmapping the
1.224     mycroft  2226:         * entire address space and doing a TLB flush.
1.149     mycroft  2227:         */
1.414     thorpej  2228:        memset((caddr_t)PTD, 0, PAGE_SIZE);
1.484     fvdl     2229:        tlbflush();
1.224     mycroft  2230: #endif
1.149     mycroft  2231:
                   2232:        for (;;);
1.45      cgd      2233: }
1.484     fvdl     2234:
1.499     jdolecek 2235: void
1.551     junyoung 2236: cpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flags)
1.508     thorpej  2237: {
                   2238:        const struct trapframe *tf = l->l_md.md_regs;
                   2239:        __greg_t *gr = mcp->__gregs;
1.519     nathanw  2240:        __greg_t ras_eip;
1.508     thorpej  2241:
                   2242:        /* Save register context. */
                   2243: #ifdef VM86
                   2244:        if (tf->tf_eflags & PSL_VM) {
                   2245:                gr[_REG_GS]  = tf->tf_vm86_gs;
                   2246:                gr[_REG_FS]  = tf->tf_vm86_fs;
                   2247:                gr[_REG_ES]  = tf->tf_vm86_es;
                   2248:                gr[_REG_DS]  = tf->tf_vm86_ds;
                   2249:                gr[_REG_EFL] = get_vflags(l);
                   2250:        } else
                   2251: #endif
                   2252:        {
                   2253:                gr[_REG_GS]  = tf->tf_gs;
                   2254:                gr[_REG_FS]  = tf->tf_fs;
                   2255:                gr[_REG_ES]  = tf->tf_es;
                   2256:                gr[_REG_DS]  = tf->tf_ds;
                   2257:                gr[_REG_EFL] = tf->tf_eflags;
                   2258:        }
                   2259:        gr[_REG_EDI]    = tf->tf_edi;
                   2260:        gr[_REG_ESI]    = tf->tf_esi;
                   2261:        gr[_REG_EBP]    = tf->tf_ebp;
                   2262:        gr[_REG_EBX]    = tf->tf_ebx;
                   2263:        gr[_REG_EDX]    = tf->tf_edx;
                   2264:        gr[_REG_ECX]    = tf->tf_ecx;
                   2265:        gr[_REG_EAX]    = tf->tf_eax;
                   2266:        gr[_REG_EIP]    = tf->tf_eip;
                   2267:        gr[_REG_CS]     = tf->tf_cs;
                   2268:        gr[_REG_ESP]    = tf->tf_esp;
                   2269:        gr[_REG_UESP]   = tf->tf_esp;
                   2270:        gr[_REG_SS]     = tf->tf_ss;
                   2271:        gr[_REG_TRAPNO] = tf->tf_trapno;
                   2272:        gr[_REG_ERR]    = tf->tf_err;
1.519     nathanw  2273:
                   2274:        if ((ras_eip = (__greg_t)ras_lookup(l->l_proc,
                   2275:            (caddr_t) gr[_REG_EIP])) != -1)
                   2276:                gr[_REG_EIP] = ras_eip;
                   2277:
1.508     thorpej  2278:        *flags |= _UC_CPU;
                   2279:
                   2280:        /* Save floating point register context, if any. */
1.548     yamt     2281:        if ((l->l_md.md_flags & MDL_USEDFPU) != 0) {
1.508     thorpej  2282: #if NNPX > 0
                   2283:                /*
                   2284:                 * If this process is the current FP owner, dump its
                   2285:                 * context to the PCB first.
                   2286:                 * XXX npxsave() also clears the FPU state; depending on the
                   2287:                 * XXX application this might be a penalty.
                   2288:                 */
                   2289:                if (l->l_addr->u_pcb.pcb_fpcpu) {
                   2290:                        npxsave_lwp(l, 1);
                   2291:                }
                   2292: #endif
                   2293:                if (i386_use_fxsave) {
                   2294:                        memcpy(&mcp->__fpregs.__fp_reg_set.__fp_xmm_state.__fp_xmm,
                   2295:                            &l->l_addr->u_pcb.pcb_savefpu.sv_xmm,
                   2296:                            sizeof (mcp->__fpregs.__fp_reg_set.__fp_xmm_state.__fp_xmm));
                   2297:                        *flags |= _UC_FXSAVE;
                   2298:                } else {
                   2299:                        memcpy(&mcp->__fpregs.__fp_reg_set.__fpchip_state.__fp_state,
                   2300:                            &l->l_addr->u_pcb.pcb_savefpu.sv_87,
                   2301:                            sizeof (mcp->__fpregs.__fp_reg_set.__fpchip_state.__fp_state));
                   2302:                }
                   2303: #if 0
                   2304:                /* Apparently nothing ever touches this. */
                   2305:                ucp->mcp.mc_fp.fp_emcsts = l->l_addr->u_pcb.pcb_saveemc;
                   2306: #endif
                   2307:                *flags |= _UC_FPU;
                   2308:        }
                   2309: }
                   2310:
                   2311: int
1.551     junyoung 2312: cpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
1.508     thorpej  2313: {
                   2314:        struct trapframe *tf = l->l_md.md_regs;
1.584     pooka    2315:        const __greg_t *gr = mcp->__gregs;
1.508     thorpej  2316:
                   2317:        /* Restore register context, if any. */
                   2318:        if ((flags & _UC_CPU) != 0) {
                   2319: #ifdef VM86
1.535     drochner 2320:                if (gr[_REG_EFL] & PSL_VM) {
1.508     thorpej  2321:                        tf->tf_vm86_gs = gr[_REG_GS];
                   2322:                        tf->tf_vm86_fs = gr[_REG_FS];
                   2323:                        tf->tf_vm86_es = gr[_REG_ES];
                   2324:                        tf->tf_vm86_ds = gr[_REG_DS];
                   2325:                        set_vflags(l, gr[_REG_EFL]);
1.534     christos 2326:                        if (flags & _UC_VM) {
1.542     junyoung 2327:                                void syscall_vm86(struct trapframe *);
1.534     christos 2328:                                l->l_proc->p_md.md_syscall = syscall_vm86;
                   2329:                        }
1.508     thorpej  2330:                } else
                   2331: #endif
                   2332:                {
                   2333:                        /*
                   2334:                         * Check for security violations.  If we're returning
                   2335:                         * to protected mode, the CPU will validate the segment
                   2336:                         * registers automatically and generate a trap on
                   2337:                         * violations.  We handle the trap, rather than doing
                   2338:                         * all of the checking here.
                   2339:                         */
1.534     christos 2340:                        if (((gr[_REG_EFL] ^ tf->tf_eflags) & PSL_USERSTATIC) ||
                   2341:                            !USERMODE(gr[_REG_CS], gr[_REG_EFL])) {
                   2342:                                printf("cpu_setmcontext error: uc EFL: 0x%08x"
1.535     drochner 2343:                                    " tf EFL: 0x%08x uc CS: 0x%x\n",
1.508     thorpej  2344:                                    gr[_REG_EFL], tf->tf_eflags, gr[_REG_CS]);
                   2345:                                return (EINVAL);
                   2346:                        }
                   2347:                        tf->tf_gs = gr[_REG_GS];
                   2348:                        tf->tf_fs = gr[_REG_FS];
                   2349:                        tf->tf_es = gr[_REG_ES];
                   2350:                        tf->tf_ds = gr[_REG_DS];
                   2351:                        /* Only change the user-alterable part of eflags */
                   2352:                        tf->tf_eflags &= ~PSL_USER;
                   2353:                        tf->tf_eflags |= (gr[_REG_EFL] & PSL_USER);
                   2354:                }
                   2355:                tf->tf_edi    = gr[_REG_EDI];
                   2356:                tf->tf_esi    = gr[_REG_ESI];
                   2357:                tf->tf_ebp    = gr[_REG_EBP];
                   2358:                tf->tf_ebx    = gr[_REG_EBX];
                   2359:                tf->tf_edx    = gr[_REG_EDX];
                   2360:                tf->tf_ecx    = gr[_REG_ECX];
                   2361:                tf->tf_eax    = gr[_REG_EAX];
                   2362:                tf->tf_eip    = gr[_REG_EIP];
                   2363:                tf->tf_cs     = gr[_REG_CS];
                   2364:                tf->tf_esp    = gr[_REG_UESP];
                   2365:                tf->tf_ss     = gr[_REG_SS];
                   2366:        }
                   2367:
                   2368:        /* Restore floating point register context, if any. */
                   2369:        if ((flags & _UC_FPU) != 0) {
                   2370: #if NNPX > 0
                   2371:                /*
                   2372:                 * If we were using the FPU, forget that we were.
                   2373:                 */
                   2374:                if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
                   2375:                        npxsave_lwp(l, 0);
                   2376: #endif
                   2377:                if (flags & _UC_FXSAVE) {
                   2378:                        if (i386_use_fxsave) {
                   2379:                                memcpy(
                   2380:                                        &l->l_addr->u_pcb.pcb_savefpu.sv_xmm,
                   2381:                                        &mcp->__fpregs.__fp_reg_set.__fp_xmm_state.__fp_xmm,
                   2382:                                        sizeof (&l->l_addr->u_pcb.pcb_savefpu.sv_xmm));
                   2383:                        } else {
                   2384:                                /* This is a weird corner case */
                   2385:                                process_xmm_to_s87((struct savexmm *)
                   2386:                                    &mcp->__fpregs.__fp_reg_set.__fp_xmm_state.__fp_xmm,
                   2387:                                    &l->l_addr->u_pcb.pcb_savefpu.sv_87);
                   2388:                        }
                   2389:                } else {
                   2390:                        if (i386_use_fxsave) {
                   2391:                                process_s87_to_xmm((struct save87 *)
                   2392:                                    &mcp->__fpregs.__fp_reg_set.__fpchip_state.__fp_state,
                   2393:                                    &l->l_addr->u_pcb.pcb_savefpu.sv_xmm);
                   2394:                        } else {
                   2395:                                memcpy(&l->l_addr->u_pcb.pcb_savefpu.sv_87,
                   2396:                                    &mcp->__fpregs.__fp_reg_set.__fpchip_state.__fp_state,
                   2397:                                    sizeof (l->l_addr->u_pcb.pcb_savefpu.sv_87));
                   2398:                        }
                   2399:                }
                   2400:                /* If not set already. */
1.548     yamt     2401:                l->l_md.md_flags |= MDL_USEDFPU;
1.508     thorpej  2402: #if 0
                   2403:                /* Apparently unused. */
                   2404:                l->l_addr->u_pcb.pcb_saveemc = mcp->mc_fp.fp_emcsts;
                   2405: #endif
                   2406:        }
1.534     christos 2407:        if (flags & _UC_SETSTACK)
                   2408:                l->l_proc->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
                   2409:        if (flags & _UC_CLRSTACK)
                   2410:                l->l_proc->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
1.508     thorpej  2411:        return (0);
                   2412: }
                   2413:
                   2414: void
1.499     jdolecek 2415: cpu_initclocks()
1.484     fvdl     2416: {
1.574     kardel   2417:
1.484     fvdl     2418:        (*initclock_func)();
                   2419: }
                   2420:
                   2421: #ifdef MULTIPROCESSOR
1.499     jdolecek 2422: void
                   2423: need_resched(struct cpu_info *ci)
1.484     fvdl     2424: {
1.546     yamt     2425:
                   2426:        if (ci->ci_want_resched)
                   2427:                return;
                   2428:
1.484     fvdl     2429:        ci->ci_want_resched = 1;
1.508     thorpej  2430:        if ((ci)->ci_curlwp != NULL)
                   2431:                aston((ci)->ci_curlwp->l_proc);
1.546     yamt     2432:        else if (ci != curcpu())
                   2433:                x86_send_ipi(ci, 0);
1.484     fvdl     2434: }
                   2435: #endif
                   2436:
                   2437: /*
                   2438:  * Allocate an IDT vector slot within the given range.
                   2439:  * XXX needs locking to avoid MP allocation races.
                   2440:  */
                   2441:
                   2442: int
1.551     junyoung 2443: idt_vec_alloc(int low, int high)
1.484     fvdl     2444: {
                   2445:        int vec;
                   2446:
1.498     fvdl     2447:        simple_lock(&idt_lock);
                   2448:        for (vec = low; vec <= high; vec++) {
                   2449:                if (idt_allocmap[vec] == 0) {
                   2450:                        idt_allocmap[vec] = 1;
                   2451:                        simple_unlock(&idt_lock);
1.484     fvdl     2452:                        return vec;
1.498     fvdl     2453:                }
                   2454:        }
                   2455:        simple_unlock(&idt_lock);
1.484     fvdl     2456:        return 0;
                   2457: }
                   2458:
1.498     fvdl     2459: void
1.551     junyoung 2460: idt_vec_set(int vec, void (*function)(void))
1.484     fvdl     2461: {
1.498     fvdl     2462:        /*
                   2463:         * Vector should be allocated, so no locking needed.
                   2464:         */
                   2465:        KASSERT(idt_allocmap[vec] == 1);
1.516     fvdl     2466:        setgate(&idt[vec], function, 0, SDT_SYS386IGT, SEL_KPL,
1.489     fvdl     2467:            GSEL(GCODE_SEL, SEL_KPL));
1.484     fvdl     2468: }
                   2469:
                   2470: void
1.551     junyoung 2471: idt_vec_free(int vec)
1.484     fvdl     2472: {
1.498     fvdl     2473:        simple_lock(&idt_lock);
1.516     fvdl     2474:        unsetgate(&idt[vec]);
1.498     fvdl     2475:        idt_allocmap[vec] = 0;
                   2476:        simple_unlock(&idt_lock);
1.507     jdolecek 2477: }
                   2478:
                   2479: /*
                   2480:  * Number of processes is limited by number of available GDT slots.
                   2481:  */
                   2482: int
                   2483: cpu_maxproc(void)
                   2484: {
                   2485: #ifdef USER_LDT
                   2486:        return ((MAXGDTSIZ - NGDT) / 2);
                   2487: #else
                   2488:        return (MAXGDTSIZ - NGDT);
                   2489: #endif
1.484     fvdl     2490: }

CVSweb <webmaster@jp.NetBSD.org>