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

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

CVSweb <webmaster@jp.NetBSD.org>