[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

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

CVSweb <webmaster@jp.NetBSD.org>