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

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

CVSweb <webmaster@jp.NetBSD.org>