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

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

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

CVSweb <webmaster@jp.NetBSD.org>