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

1.234   ! thorpej     1: /*     $NetBSD: machdep.c,v 1.233 1997/06/12 15:46:32 mrg Exp $        */
1.231     thorpej     2:
                      3: /*-
                      4:  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
                      8:  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
                      9:  * NASA Ames Research Center.
                     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: /*-
1.196     mycroft    41:  * Copyright (c) 1993, 1994, 1995, 1996 Charles M. Hannum.  All rights reserved.
1.35      cgd        42:  * Copyright (c) 1992 Terrence R. Lambert.
1.1       cgd        43:  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
                     44:  * All rights reserved.
                     45:  *
                     46:  * This code is derived from software contributed to Berkeley by
                     47:  * William Jolitz.
                     48:  *
                     49:  * Redistribution and use in source and binary forms, with or without
                     50:  * modification, are permitted provided that the following conditions
                     51:  * are met:
                     52:  * 1. Redistributions of source code must retain the above copyright
                     53:  *    notice, this list of conditions and the following disclaimer.
                     54:  * 2. Redistributions in binary form must reproduce the above copyright
                     55:  *    notice, this list of conditions and the following disclaimer in the
                     56:  *    documentation and/or other materials provided with the distribution.
                     57:  * 3. All advertising materials mentioning features or use of this software
                     58:  *    must display the following acknowledgement:
                     59:  *     This product includes software developed by the University of
                     60:  *     California, Berkeley and its contributors.
                     61:  * 4. Neither the name of the University nor the names of its contributors
                     62:  *    may be used to endorse or promote products derived from this software
                     63:  *    without specific prior written permission.
                     64:  *
                     65:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     66:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     67:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     68:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     69:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     70:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     71:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     72:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     73:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     74:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     75:  * SUCH DAMAGE.
                     76:  *
1.125     cgd        77:  *     @(#)machdep.c   7.4 (Berkeley) 6/3/91
1.1       cgd        78:  */
                     79:
1.59      mycroft    80: #include <sys/param.h>
                     81: #include <sys/systm.h>
                     82: #include <sys/signalvar.h>
                     83: #include <sys/kernel.h>
                     84: #include <sys/map.h>
                     85: #include <sys/proc.h>
                     86: #include <sys/user.h>
                     87: #include <sys/exec.h>
                     88: #include <sys/buf.h>
                     89: #include <sys/reboot.h>
                     90: #include <sys/conf.h>
                     91: #include <sys/file.h>
                     92: #include <sys/callout.h>
                     93: #include <sys/malloc.h>
                     94: #include <sys/mbuf.h>
                     95: #include <sys/msgbuf.h>
                     96: #include <sys/mount.h>
                     97: #include <sys/vnode.h>
1.92      mycroft    98: #include <sys/device.h>
1.204     thorpej    99: #include <sys/extent.h>
1.104     cgd       100: #include <sys/sysctl.h>
1.123     cgd       101: #include <sys/syscallargs.h>
1.57      cgd       102: #ifdef SYSVMSG
1.59      mycroft   103: #include <sys/msg.h>
1.57      cgd       104: #endif
                    105: #ifdef SYSVSEM
1.59      mycroft   106: #include <sys/sem.h>
                    107: #endif
                    108: #ifdef SYSVSHM
                    109: #include <sys/shm.h>
1.57      cgd       110: #endif
                    111:
1.104     cgd       112: #include <dev/cons.h>
                    113:
1.59      mycroft   114: #include <vm/vm.h>
                    115: #include <vm/vm_kern.h>
                    116: #include <vm/vm_page.h>
1.31      cgd       117:
1.200     christos  118: #include <sys/sysctl.h>
                    119:
1.231     thorpej   120: #define _I386_BUS_DMA_PRIVATE
                    121: #include <machine/bus.h>
                    122:
1.59      mycroft   123: #include <machine/cpu.h>
                    124: #include <machine/cpufunc.h>
1.178     mycroft   125: #include <machine/gdt.h>
1.149     mycroft   126: #include <machine/pio.h>
1.59      mycroft   127: #include <machine/psl.h>
                    128: #include <machine/reg.h>
                    129: #include <machine/specialreg.h>
1.43      brezak    130:
1.146     cgd       131: #include <dev/isa/isareg.h>
                    132: #include <dev/isa/isavar.h>
1.164     cgd       133: #include <dev/ic/i8042reg.h>
                    134: #include <dev/ic/mc146818reg.h>
1.146     cgd       135: #include <i386/isa/isa_machdep.h>
1.158     cgd       136: #include <i386/isa/nvram.h>
1.43      brezak    137:
1.200     christos  138: #ifdef DDB
                    139: #include <machine/db_machdep.h>
                    140: #include <ddb/db_access.h>
                    141: #include <ddb/db_sym.h>
                    142: #include <ddb/db_extern.h>
                    143: #endif
                    144:
1.184     mycroft   145: #ifdef VM86
                    146: #include <machine/vm86.h>
                    147: #endif
                    148:
1.207     jtk       149: #include "apm.h"
                    150:
                    151: #if NAPM > 0
                    152: #include <machine/apmvar.h>
                    153: #endif
                    154:
1.59      mycroft   155: #include "isa.h"
1.231     thorpej   156: #include "isadma.h"
1.59      mycroft   157: #include "npx.h"
1.161     mycroft   158: #if NNPX > 0
                    159: extern struct proc *npxproc;
                    160: #endif
1.2       cgd       161:
1.104     cgd       162: /* the following is used externally (sysctl_hw) */
                    163: char machine[] = "i386";               /* cpu "architecture" */
1.232     veego     164: char machine_arch[] = "i386";          /* machine == machine_arch */
1.104     cgd       165:
1.1       cgd       166: /*
                    167:  * Declare these as initialized data so we can patch them.
                    168:  */
                    169: int    nswbuf = 0;
                    170: #ifdef NBUF
                    171: int    nbuf = NBUF;
                    172: #else
                    173: int    nbuf = 0;
                    174: #endif
                    175: #ifdef BUFPAGES
                    176: int    bufpages = BUFPAGES;
                    177: #else
                    178: int    bufpages = 0;
                    179: #endif
                    180:
1.59      mycroft   181: int    physmem;
1.163     cgd       182: int    dumpmem_low;
                    183: int    dumpmem_high;
1.59      mycroft   184: int    boothowto;
                    185: int    cpu_class;
                    186:
                    187: struct msgbuf *msgbufp;
                    188: int    msgbufmapped;
                    189:
                    190: vm_map_t buffer_map;
1.48      brezak    191:
1.203     mycroft   192: extern int biosbasemem, biosextmem;
1.59      mycroft   193: extern vm_offset_t avail_start, avail_end;
                    194: static vm_offset_t hole_start, hole_end;
                    195: static vm_offset_t avail_next;
1.1       cgd       196:
1.204     thorpej   197: /*
1.205     thorpej   198:  * Extent maps to manage I/O and ISA memory hole space.  Allocate
                    199:  * storage for 8 regions in each, initially.  Later, ioport_malloc_safe
                    200:  * will indicate that it's safe to use malloc() to dynamically allocate
                    201:  * region descriptors.
1.213     thorpej   202:  *
                    203:  * N.B. At least two regions are _always_ allocated from the iomem
                    204:  * extent map; (0 -> ISA hole) and (end of ISA hole -> end of RAM).
                    205:  *
                    206:  * The extent maps are not static!  Machine-dependent ISA and EISA
                    207:  * routines need access to them for bus address space allocation.
1.204     thorpej   208:  */
1.211     thorpej   209: static long ioport_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8) / sizeof(long)];
                    210: static long iomem_ex_storage[EXTENT_FIXED_STORAGE_SIZE(8) / sizeof(long)];
1.213     thorpej   211: struct extent *ioport_ex;
                    212: struct extent *iomem_ex;
1.204     thorpej   213: static ioport_malloc_safe;
                    214:
1.200     christos  215: caddr_t        allocsys __P((caddr_t));
                    216: void   dumpsys __P((void));
                    217: void   identifycpu __P((void));
                    218: void   init386 __P((vm_offset_t));
                    219: void   consinit __P((void));
                    220: #ifdef COMPAT_NOMID
                    221: static int exec_nomid  __P((struct proc *, struct exec_package *));
                    222: #endif
1.59      mycroft   223:
1.213     thorpej   224: int    bus_mem_add_mapping __P((bus_addr_t, bus_size_t,
                    225:            int, bus_space_handle_t *));
                    226:
1.59      mycroft   227: /*
                    228:  * Machine-dependent startup code
                    229:  */
1.32      andrew    230: void
1.1       cgd       231: cpu_startup()
                    232: {
1.59      mycroft   233:        unsigned i;
                    234:        caddr_t v;
                    235:        int sz;
                    236:        int base, residual;
1.1       cgd       237:        vm_offset_t minaddr, maxaddr;
                    238:        vm_size_t size;
1.180     mycroft   239:        struct pcb *pcb;
                    240:        int x;
1.207     jtk       241: #if NAPM > 0
                    242:        extern int biostramp_image_size;
                    243:        extern u_char biostramp_image[];
                    244: #endif
1.1       cgd       245:
                    246:        /*
                    247:         * Initialize error message buffer (at end of core).
                    248:         */
                    249:        /* avail_end was pre-decremented in pmap_bootstrap to compensate */
1.59      mycroft   250:        for (i = 0; i < btoc(sizeof(struct msgbuf)); i++)
1.143     mycroft   251:                pmap_enter(pmap_kernel(),
1.108     cgd       252:                    (vm_offset_t)((caddr_t)msgbufp + i * NBPG),
                    253:                    avail_end + i * NBPG, VM_PROT_ALL, TRUE);
1.1       cgd       254:        msgbufmapped = 1;
                    255:
1.210     christos  256:        printf(version);
1.16      cgd       257:        identifycpu();
1.210     christos  258:        printf("real mem  = %d\n", ctob(physmem));
1.1       cgd       259:
                    260:        /*
1.59      mycroft   261:         * Find out how much space we need, allocate it,
                    262:         * and then give everything true virtual addresses.
1.1       cgd       263:         */
1.59      mycroft   264:        sz = (int)allocsys((caddr_t)0);
                    265:        if ((v = (caddr_t)kmem_alloc(kernel_map, round_page(sz))) == 0)
                    266:                panic("startup: no room for tables");
                    267:        if (allocsys(v) - v != sz)
1.1       cgd       268:                panic("startup: table size inconsistency");
1.50      cgd       269:
1.36      cgd       270:        /*
                    271:         * Now allocate buffers proper.  They are different than the above
                    272:         * in that they usually occupy more virtual memory than physical.
                    273:         */
                    274:        size = MAXBSIZE * nbuf;
1.68      mycroft   275:        buffer_map = kmem_suballoc(kernel_map, (vm_offset_t *)&buffers,
1.108     cgd       276:                                   &maxaddr, size, TRUE);
1.36      cgd       277:        minaddr = (vm_offset_t)buffers;
                    278:        if (vm_map_find(buffer_map, vm_object_allocate(size), (vm_offset_t)0,
                    279:                        &minaddr, size, FALSE) != KERN_SUCCESS)
                    280:                panic("startup: cannot allocate buffers");
1.54      cgd       281:        if ((bufpages / nbuf) >= btoc(MAXBSIZE)) {
                    282:                /* don't want to alloc more physical mem than needed */
                    283:                bufpages = btoc(MAXBSIZE) * nbuf;
                    284:        }
1.36      cgd       285:        base = bufpages / nbuf;
                    286:        residual = bufpages % nbuf;
                    287:        for (i = 0; i < nbuf; i++) {
                    288:                vm_size_t curbufsize;
                    289:                vm_offset_t curbuf;
                    290:
                    291:                /*
                    292:                 * First <residual> buffers get (base+1) physical pages
                    293:                 * allocated for them.  The rest get (base) physical pages.
                    294:                 *
                    295:                 * The rest of each buffer occupies virtual space,
                    296:                 * but has no physical memory allocated for it.
                    297:                 */
                    298:                curbuf = (vm_offset_t)buffers + i * MAXBSIZE;
                    299:                curbufsize = CLBYTES * (i < residual ? base+1 : base);
                    300:                vm_map_pageable(buffer_map, curbuf, curbuf+curbufsize, FALSE);
                    301:                vm_map_simplify(buffer_map, curbuf);
                    302:        }
1.41      cgd       303:
1.1       cgd       304:        /*
1.36      cgd       305:         * Allocate a submap for exec arguments.  This map effectively
                    306:         * limits the number of processes exec'ing at any time.
1.1       cgd       307:         */
1.59      mycroft   308:        exec_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
1.79      mycroft   309:                                 16*NCARGS, TRUE);
1.59      mycroft   310:
1.1       cgd       311:        /*
                    312:         * Allocate a submap for physio
                    313:         */
                    314:        phys_map = kmem_suballoc(kernel_map, &minaddr, &maxaddr,
                    315:                                 VM_PHYS_SIZE, TRUE);
                    316:
                    317:        /*
1.229     thorpej   318:         * Finally, allocate mbuf cluster submap.
1.1       cgd       319:         */
1.68      mycroft   320:        mb_map = kmem_suballoc(kernel_map, (vm_offset_t *)&mbutl, &maxaddr,
1.73      mycroft   321:            VM_MBUF_SIZE, FALSE);
1.59      mycroft   322:
1.1       cgd       323:        /*
                    324:         * Initialize callouts
                    325:         */
                    326:        callfree = callout;
                    327:        for (i = 1; i < ncallout; i++)
                    328:                callout[i-1].c_next = &callout[i];
                    329:
1.210     christos  330:        printf("avail mem = %ld\n", ptoa(cnt.v_free_count));
                    331:        printf("using %d buffers containing %d bytes of memory\n",
1.36      cgd       332:                nbuf, bufpages * CLBYTES);
1.1       cgd       333:
                    334:        /*
                    335:         * Set up buffers, so they can be used to read disk labels.
                    336:         */
                    337:        bufinit();
                    338:
1.207     jtk       339: #if NAPM > 0
                    340:        /*
                    341:         * this should be caught at kernel build time, but put it here
                    342:         * in case someone tries to fake it out...
                    343:         */
                    344: #ifdef DIAGNOSTIC
                    345:        if (biostramp_image_size > NBPG)
                    346:            panic("biostramp_image_size too big: %x vs. %x\n",
                    347:                  biostramp_image_size, NBPG);
                    348: #endif
                    349:        pmap_enter(pmap_kernel(),
                    350:                   (vm_offset_t)APM_BIOSTRAMP, /* virtual */
                    351:                   (vm_offset_t)APM_BIOSTRAMP, /* physical */
                    352:                   VM_PROT_ALL,         /* protection */
                    353:                   TRUE);               /* wired down */
                    354:        bcopy(biostramp_image, (caddr_t)APM_BIOSTRAMP, biostramp_image_size);
                    355: #ifdef DEBUG
1.210     christos  356:        printf("biostramp installed @ %x\n", APM_BIOSTRAMP);
1.207     jtk       357: #endif
                    358: #endif
1.1       cgd       359:        /*
                    360:         * Configure the system.
                    361:         */
1.204     thorpej   362:        ioport_malloc_safe = 1;
1.1       cgd       363:        configure();
1.153     mycroft   364:
                    365:        /*
1.180     mycroft   366:         * Set up proc0's TSS and LDT.
1.153     mycroft   367:         */
1.180     mycroft   368:        curpcb = pcb = &proc0.p_addr->u_pcb;
                    369:        pcb->pcb_flags = 0;
                    370:        pcb->pcb_tss.tss_ioopt =
                    371:            ((caddr_t)pcb->pcb_iomap - (caddr_t)&pcb->pcb_tss) << 16;
                    372:        for (x = 0; x < sizeof(pcb->pcb_iomap) / 4; x++)
                    373:                pcb->pcb_iomap[x] = 0xffffffff;
                    374:
                    375:        pcb->pcb_ldt_sel = GSEL(GLDT_SEL, SEL_KPL);
                    376:        pcb->pcb_cr0 = rcr0();
                    377:        pcb->pcb_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
                    378:        pcb->pcb_tss.tss_esp0 = (int)proc0.p_addr + USPACE - 16;
                    379:        tss_alloc(pcb);
1.181     mycroft   380:
1.180     mycroft   381:        ltr(pcb->pcb_tss_sel);
1.181     mycroft   382:        lldt(pcb->pcb_ldt_sel);
1.180     mycroft   383:
                    384:        proc0.p_md.md_regs = (struct trapframe *)pcb->pcb_tss.tss_esp0 - 1;
                    385:
1.16      cgd       386: }
                    387:
1.59      mycroft   388: /*
                    389:  * Allocate space for system data structures.  We are given
                    390:  * a starting virtual address and we return a final virtual
                    391:  * address; along the way we set each data structure pointer.
                    392:  *
                    393:  * We call allocsys() with 0 to find out how much space we want,
                    394:  * allocate that much and fill it with zeroes, and then call
                    395:  * allocsys() again with the correct base virtual address.
                    396:  */
                    397: caddr_t
                    398: allocsys(v)
                    399:        register caddr_t v;
                    400: {
                    401:
                    402: #define        valloc(name, type, num) \
                    403:            v = (caddr_t)(((name) = (type *)v) + (num))
                    404: #ifdef REAL_CLISTS
                    405:        valloc(cfree, struct cblock, nclist);
                    406: #endif
                    407:        valloc(callout, struct callout, ncallout);
                    408: #ifdef SYSVSHM
                    409:        valloc(shmsegs, struct shmid_ds, shminfo.shmmni);
                    410: #endif
                    411: #ifdef SYSVSEM
                    412:        valloc(sema, struct semid_ds, seminfo.semmni);
                    413:        valloc(sem, struct sem, seminfo.semmns);
                    414:        /* This is pretty disgusting! */
                    415:        valloc(semu, int, (seminfo.semmnu * seminfo.semusz) / sizeof(int));
                    416: #endif
                    417: #ifdef SYSVMSG
                    418:        valloc(msgpool, char, msginfo.msgmax);
                    419:        valloc(msgmaps, struct msgmap, msginfo.msgseg);
                    420:        valloc(msghdrs, struct msg, msginfo.msgtql);
                    421:        valloc(msqids, struct msqid_ds, msginfo.msgmni);
                    422: #endif
                    423:
                    424:        /*
1.77      mycroft   425:         * Determine how many buffers to allocate.  We use 10% of the
                    426:         * first 2MB of memory, and 5% of the rest, with a minimum of 16
                    427:         * buffers.  We allocate 1/2 as many swap buffer headers as file
                    428:         * i/o buffers.
1.59      mycroft   429:         */
                    430:        if (bufpages == 0)
1.77      mycroft   431:                if (physmem < btoc(2 * 1024 * 1024))
                    432:                        bufpages = physmem / (10 * CLSIZE);
                    433:                else
                    434:                        bufpages = (btoc(2 * 1024 * 1024) + physmem) /
                    435:                            (20 * CLSIZE);
1.59      mycroft   436:        if (nbuf == 0) {
                    437:                nbuf = bufpages;
                    438:                if (nbuf < 16)
                    439:                        nbuf = 16;
                    440:        }
1.218     fvdl      441:
                    442:        /*
                    443:         * XXX stopgap measure to prevent wasting too much KVM on
                    444:         * the sparsely filled buffer cache.
                    445:         */
                    446:        if (nbuf * MAXBSIZE > VM_MAX_KERNEL_BUF)
                    447:                nbuf = VM_MAX_KERNEL_BUF / MAXBSIZE;
                    448:
1.59      mycroft   449:        if (nswbuf == 0) {
                    450:                nswbuf = (nbuf / 2) &~ 1;       /* force even */
                    451:                if (nswbuf > 256)
                    452:                        nswbuf = 256;           /* sanity */
                    453:        }
                    454:        valloc(swbuf, struct buf, nswbuf);
                    455:        valloc(buf, struct buf, nbuf);
                    456:        return v;
                    457: }
1.18      cgd       458:
1.104     cgd       459: /*
                    460:  * Info for CTL_HW
                    461:  */
                    462: char   cpu_model[120];
                    463: extern char version[];
                    464:
1.216     fvdl      465: /*
                    466:  * Note: these are just the ones that may not have a cpuid instruction.
                    467:  * We deal with the rest in a different way.
                    468:  */
                    469: struct cpu_nocpuid_nameclass i386_nocpuid_cpus[] = {
                    470:        { CPUVENDOR_INTEL, "Intel", "386SX",    CPUCLASS_386 }, /* CPU_386SX */
                    471:        { CPUVENDOR_INTEL, "Intel", "386DX",    CPUCLASS_386 }, /* CPU_386   */
                    472:        { CPUVENDOR_INTEL, "Intel", "486SX",    CPUCLASS_486 }, /* CPU_486SX */
                    473:        { CPUVENDOR_INTEL, "Intel", "486DX",    CPUCLASS_486 }, /* CPU_486   */
                    474:        { CPUVENDOR_CYRIX, "Cyrix", "486DLC",   CPUCLASS_486 }, /* CPU_486DLC */
1.217     fvdl      475:        { CPUVENDOR_NEXGEN,"NexGen","586",      CPUCLASS_386 }, /* CPU_NX586 */
1.216     fvdl      476: };
                    477:
                    478: const char *classnames[] = {
                    479:        "386",
                    480:        "486",
                    481:        "586",
                    482:        "686"
                    483: };
                    484:
                    485: const char *modifiers[] = {
                    486:        "",
                    487:        "OverDrive ",
                    488:        "Dual ",
                    489:        ""
1.18      cgd       490: };
                    491:
1.216     fvdl      492: struct cpu_cpuid_nameclass i386_cpuid_cpus[] = {
                    493:        {
                    494:                "GenuineIntel",
                    495:                CPUVENDOR_INTEL,
                    496:                "Intel",
                    497:                /* Family 4 */
                    498:                { {
                    499:                        CPUCLASS_486,
                    500:                        {
1.219     perry     501:                                "486DX", "486DX", "486SX", "486DX2", "486SL",
1.216     fvdl      502:                                "486SX2", 0, "486DX2 W/B Enhanced",
                    503:                                "486DX4", 0, 0, 0, 0, 0, 0, 0,
                    504:                                "486"           /* Default */
                    505:                        }
                    506:                },
                    507:                /* Family 5 */
                    508:                {
                    509:                        CPUCLASS_586,
                    510:                        {
                    511:                                0, "Pentium", "Pentium (P54C)",
                    512:                                "Pentium (P24T)", "Pentium", "Pentium", 0,
                    513:                                "Pentium (P54C)", 0, 0, 0, 0, 0, 0, 0, 0,
                    514:                                "Pentium"       /* Default */
                    515:                        }
                    516:                },
                    517:                /* Family 6 */
                    518:                {
                    519:                        CPUCLASS_686,
                    520:                        {
                    521:                                0, "Pentium Pro", 0, 0, "Pentium Pro", 0, 0,
                    522:                                0, 0, 0, 0, 0, 0, 0, 0, 0,
                    523:                                "Pentium Pro"   /* Default */
                    524:                        }
                    525:                } }
                    526:        },
                    527:        {
                    528:                "AuthenticAMD",
                    529:                CPUVENDOR_AMD,
                    530:                "AMD",
                    531:                /* Family 4 */
                    532:                { {
                    533:                        CPUCLASS_486,
                    534:                        {
                    535:                                0, 0, 0, "Am486DX2 W/T",
                    536:                                0, 0, 0, "Am486DX2 W/B",
                    537:                                "Am486DX4 W/T or Am5x86 W/T 150",
                    538:                                "Am486DX4 W/B or Am5x86 W/B 150", 0, 0,
                    539:                                0, 0, "Am5x86 W/T 133/160",
                    540:                                "Am5x86 W/B 133/160",
                    541:                                "Am486 or Am5x86"       /* Default */
                    542:                        },
                    543:                },
                    544:                /* Family 5 */
                    545:                {
                    546:                        CPUCLASS_586,
                    547:                        {
                    548:                                "K5", "K5", 0, 0, 0, 0, "K6",
                    549:                                0, 0, 0, 0, 0, 0, 0, 0, 0,
                    550:                                "K5 or K6",             /* Default */
                    551:                        },
                    552:                },
                    553:                /* Family 6, not yet available from AMD */
                    554:                {
                    555:                        CPUCLASS_686,
                    556:                        {
                    557:                                0, 0, 0, 0, 0, 0, 0,
                    558:                                0, 0, 0, 0, 0, 0, 0, 0, 0,
                    559:                                "Pentium Pro compatible"        /* Default */
                    560:                        },
                    561:                } }
                    562:        },
                    563:        {
                    564:                "CyrixInstead",
                    565:                CPUVENDOR_CYRIX,
                    566:                "Cyrix",
                    567:                /* Family 4 */
                    568:                { {
                    569:                        CPUCLASS_486,
                    570:                        {
                    571:                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    572:                                "486"           /* Default */
                    573:                        },
                    574:                },
                    575:                /* Family 5 */
                    576:                {
                    577:                        CPUCLASS_586,
                    578:                        {
                    579:                                0, 0, "6x86", 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    580:                                0, 0, 0, 0,
                    581:                                "6x86"          /* Default */
                    582:                        }
                    583:                },
                    584:                /* Family 6, not yet available from Cyrix */
                    585:                {
                    586:                        CPUCLASS_686,
                    587:                        {
                    588:                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    589:                                "Pentium Pro compatible"        /* Default */
                    590:                        }
                    591:                } }
                    592:        }
                    593: };
                    594:
                    595: #define CPUDEBUG
                    596:
1.195     mycroft   597: void
1.59      mycroft   598: identifycpu()
1.16      cgd       599: {
1.86      mycroft   600:        extern char cpu_vendor[];
1.216     fvdl      601:        extern int cpu_id;
                    602:        const char *name, *modifier, *vendorname;
                    603:        int class = CPUCLASS_386, vendor, i, max;
                    604:        int family, model, step, modif;
                    605:        struct cpu_cpuid_nameclass *cpup = NULL;
1.86      mycroft   606:
1.216     fvdl      607:        if (cpuid_level == -1) {
1.59      mycroft   608: #ifdef DIAGNOSTIC
1.216     fvdl      609:                if (cpu < 0 || cpu >=
                    610:                    (sizeof i386_nocpuid_cpus/sizeof(struct cpu_nocpuid_nameclass)))
                    611:                        panic("unknown cpu type %d\n", cpu);
                    612: #endif
                    613:                name = i386_nocpuid_cpus[cpu].cpu_name;
                    614:                vendor = i386_nocpuid_cpus[cpu].cpu_vendor;
                    615:                vendorname = i386_nocpuid_cpus[cpu].cpu_vendorname;
                    616:                class = i386_nocpuid_cpus[cpu].cpu_class;
                    617:                modifier = "";
                    618:        } else {
                    619:                max = sizeof (i386_cpuid_cpus) / sizeof (i386_cpuid_cpus[0]);
                    620:                modif = (cpu_id >> 12) & 3;
                    621:                family = (cpu_id >> 8) & 15;
                    622:                if (family < CPU_MINFAMILY)
                    623:                        panic("identifycpu: strange family value");
                    624:                model = (cpu_id >> 4) & 15;
                    625:                step = cpu_id & 15;
                    626: #ifdef CPUDEBUG
                    627:                printf("cpu0: family %x model %x step %x\n", family, model,
                    628:                        step);
                    629: #endif
                    630:
                    631:                for (i = 0; i < max; i++) {
                    632:                        if (!strncmp(cpu_vendor,
                    633:                            i386_cpuid_cpus[i].cpu_id, 12)) {
                    634:                                cpup = &i386_cpuid_cpus[i];
                    635:                                break;
                    636:                        }
                    637:                }
                    638:
                    639:                if (cpup == NULL) {
                    640:                        vendor = CPUVENDOR_UNKNOWN;
                    641:                        if (cpu_vendor[0] != '\0')
                    642:                                vendorname = &cpu_vendor[0];
                    643:                        else
                    644:                                vendorname = "Unknown";
                    645:                        if (family > CPU_MAXFAMILY)
                    646:                                family = CPU_MAXFAMILY;
                    647:                        class = family - 3;
                    648:                        modifier = "";
                    649:                        name = "";
                    650:                } else {
                    651:                        vendor = cpup->cpu_vendor;
                    652:                        vendorname = cpup->cpu_vendorname;
                    653:                        modifier = modifiers[modif];
                    654:                        if (family > CPU_MAXFAMILY) {
                    655:                                family = CPU_MAXFAMILY;
                    656:                                model = CPU_DEFMODEL;
                    657:                        } else if (model > CPU_MAXMODEL)
                    658:                                model = CPU_DEFMODEL;
                    659:                        i = family - CPU_MINFAMILY;
                    660:                        name = cpup->cpu_family[i].cpu_models[model];
                    661:                        if (name == NULL)
                    662:                            name = cpup->cpu_family[i].cpu_models[CPU_DEFMODEL];
                    663:                        class = cpup->cpu_family[i].cpu_class;
                    664:                }
1.104     cgd       665:        }
                    666:
1.220     perry     667:        sprintf(cpu_model, "%s %s%s (%s-class)%s", vendorname, modifier, name,
                    668:                classnames[class], cpu_feature & 0x800000 ? " with MMX" : "");
1.216     fvdl      669:        printf("cpu0: %s\n", cpu_model);
                    670:
                    671:        cpu_class = class;
1.18      cgd       672:
1.16      cgd       673:        /*
                    674:         * Now that we have told the user what they have,
                    675:         * let them know if that machine type isn't configured.
                    676:         */
1.24      cgd       677:        switch (cpu_class) {
1.216     fvdl      678: #if !defined(I386_CPU) && !defined(I486_CPU) && !defined(I586_CPU) && !defined(I686_CPU)
1.100     mycroft   679: #error No CPU classes configured.
                    680: #endif
1.216     fvdl      681: #ifndef I686_CPU
                    682:        case CPUCLASS_686:
                    683:                printf("NOTICE: this kernel does not support Pentium Pro CPU class\n");
                    684: #ifdef I586_CPU
                    685:                printf("NOTICE: lowering CPU class to i586\n");
                    686:                cpu_class = CPUCLASS_586;
                    687:                break;
                    688: #endif
                    689: #endif
1.165     mycroft   690: #ifndef I586_CPU
1.118     mycroft   691:        case CPUCLASS_586:
1.210     christos  692:                printf("NOTICE: this kernel does not support Pentium CPU class\n");
1.165     mycroft   693: #ifdef I486_CPU
1.210     christos  694:                printf("NOTICE: lowering CPU class to i486\n");
1.118     mycroft   695:                cpu_class = CPUCLASS_486;
                    696:                break;
1.16      cgd       697: #endif
1.165     mycroft   698: #endif
                    699: #ifndef I486_CPU
1.18      cgd       700:        case CPUCLASS_486:
1.210     christos  701:                printf("NOTICE: this kernel does not support i486 CPU class\n");
1.165     mycroft   702: #ifdef I386_CPU
1.210     christos  703:                printf("NOTICE: lowering CPU class to i386\n");
1.118     mycroft   704:                cpu_class = CPUCLASS_386;
                    705:                break;
                    706: #endif
1.165     mycroft   707: #endif
                    708: #ifndef I386_CPU
1.118     mycroft   709:        case CPUCLASS_386:
1.210     christos  710:                printf("NOTICE: this kernel does not support i386 CPU class\n");
1.187     mycroft   711:                panic("no appropriate CPU class available");
1.59      mycroft   712: #endif
1.16      cgd       713:        default:
                    714:                break;
1.121     mycroft   715:        }
                    716:
                    717:        if (cpu == CPU_486DLC) {
                    718: #ifndef CYRIX_CACHE_WORKS
1.210     christos  719:                printf("WARNING: CYRIX 486DLC CACHE UNCHANGED.\n");
1.121     mycroft   720: #else
                    721: #ifndef CYRIX_CACHE_REALLY_WORKS
1.210     christos  722:                printf("WARNING: CYRIX 486DLC CACHE ENABLED IN HOLD-FLUSH MODE.\n");
1.121     mycroft   723: #else
1.210     christos  724:                printf("WARNING: CYRIX 486DLC CACHE ENABLED.\n");
1.121     mycroft   725: #endif
                    726: #endif
1.16      cgd       727:        }
1.147     mycroft   728:
1.216     fvdl      729: #if defined(I486_CPU) || defined(I586_CPU) || defined(I686_CPU)
1.147     mycroft   730:        /*
1.199     mycroft   731:         * On a 486 or above, enable ring 0 write protection.
1.147     mycroft   732:         */
                    733:        if (cpu_class >= CPUCLASS_486)
1.199     mycroft   734:                lcr0(rcr0() | CR0_WP);
1.147     mycroft   735: #endif
1.1       cgd       736: }
                    737:
1.104     cgd       738: /*
                    739:  * machine dependent system variables.
                    740:  */
1.195     mycroft   741: int
1.104     cgd       742: cpu_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
                    743:        int *name;
                    744:        u_int namelen;
                    745:        void *oldp;
                    746:        size_t *oldlenp;
                    747:        void *newp;
                    748:        size_t newlen;
                    749:        struct proc *p;
                    750: {
                    751:        dev_t consdev;
                    752:
                    753:        /* all sysctl names at this level are terminal */
                    754:        if (namelen != 1)
                    755:                return (ENOTDIR);               /* overloaded */
                    756:
                    757:        switch (name[0]) {
                    758:        case CPU_CONSDEV:
                    759:                if (cn_tab != NULL)
                    760:                        consdev = cn_tab->cn_dev;
                    761:                else
                    762:                        consdev = NODEV;
                    763:                return (sysctl_rdstruct(oldp, oldlenp, newp, &consdev,
                    764:                    sizeof consdev));
1.215     fvdl      765:
                    766:        case CPU_BIOSBASEMEM:
                    767:                return (sysctl_rdint(oldp, oldlenp, newp, biosbasemem));
                    768:
                    769:        case CPU_BIOSEXTMEM:
                    770:                return (sysctl_rdint(oldp, oldlenp, newp, biosextmem));
                    771:
                    772:        case CPU_NKPDE:
                    773:                return (sysctl_rdint(oldp, oldlenp, newp, nkpde));
                    774:
1.104     cgd       775:        default:
                    776:                return (EOPNOTSUPP);
                    777:        }
                    778:        /* NOTREACHED */
                    779: }
                    780:
1.151     christos  781: #ifdef COMPAT_IBCS2
1.200     christos  782: void ibcs2_sendsig __P((sig_t, int, int, u_long));
                    783:
1.151     christos  784: void
                    785: ibcs2_sendsig(catcher, sig, mask, code)
                    786:        sig_t catcher;
                    787:        int sig, mask;
                    788:        u_long code;
                    789: {
1.168     mycroft   790:        extern int bsd_to_ibcs2_sig[];
                    791:
                    792:        sendsig(catcher, bsd_to_ibcs2_sig[sig], mask, code);
1.151     christos  793: }
                    794: #endif
                    795:
1.1       cgd       796: /*
                    797:  * Send an interrupt to process.
                    798:  *
                    799:  * Stack is set up to allow sigcode stored
                    800:  * in u. to call routine, followed by kcall
                    801:  * to sigreturn routine below.  After sigreturn
                    802:  * resets the signal mask, the stack, and the
                    803:  * frame pointer, it returns to the user
                    804:  * specified pc, psl.
                    805:  */
                    806: void
                    807: sendsig(catcher, sig, mask, code)
                    808:        sig_t catcher;
                    809:        int sig, mask;
1.126     cgd       810:        u_long code;
1.1       cgd       811: {
                    812:        register struct proc *p = curproc;
1.59      mycroft   813:        register struct trapframe *tf;
1.82      ws        814:        struct sigframe *fp, frame;
1.104     cgd       815:        struct sigacts *psp = p->p_sigacts;
1.48      brezak    816:        int oonstack;
1.22      cgd       817:        extern char sigcode[], esigcode[];
1.1       cgd       818:
1.135     christos  819:        /*
                    820:         * Build the argument list for the signal handler.
                    821:         */
1.151     christos  822:        frame.sf_signum = sig;
1.135     christos  823:
1.154     mycroft   824:        tf = p->p_md.md_regs;
1.171     mycroft   825:        oonstack = psp->ps_sigstk.ss_flags & SS_ONSTACK;
1.135     christos  826:
1.1       cgd       827:        /*
1.82      ws        828:         * Allocate space for the signal handler context.
1.1       cgd       829:         */
1.105     mycroft   830:        if ((psp->ps_flags & SAS_ALTSTACK) && !oonstack &&
1.104     cgd       831:            (psp->ps_sigonstack & sigmask(sig))) {
1.183     jtc       832:                fp = (struct sigframe *)(psp->ps_sigstk.ss_sp +
1.104     cgd       833:                    psp->ps_sigstk.ss_size - sizeof(struct sigframe));
1.171     mycroft   834:                psp->ps_sigstk.ss_flags |= SS_ONSTACK;
1.1       cgd       835:        } else {
1.91      mycroft   836:                fp = (struct sigframe *)tf->tf_esp - 1;
1.1       cgd       837:        }
                    838:
1.82      ws        839:        frame.sf_code = code;
                    840:        frame.sf_scp = &fp->sf_sc;
                    841:        frame.sf_handler = catcher;
                    842:
                    843:        /*
                    844:         * Build the signal context to be used by sigreturn.
                    845:         */
1.184     mycroft   846:        frame.sf_sc.sc_err = tf->tf_err;
                    847:        frame.sf_sc.sc_trapno = tf->tf_trapno;
1.82      ws        848:        frame.sf_sc.sc_onstack = oonstack;
                    849:        frame.sf_sc.sc_mask = mask;
1.157     mycroft   850: #ifdef VM86
                    851:        if (tf->tf_eflags & PSL_VM) {
                    852:                frame.sf_sc.sc_gs = tf->tf_vm86_gs;
                    853:                frame.sf_sc.sc_fs = tf->tf_vm86_fs;
                    854:                frame.sf_sc.sc_es = tf->tf_vm86_es;
                    855:                frame.sf_sc.sc_ds = tf->tf_vm86_ds;
1.196     mycroft   856:                frame.sf_sc.sc_eflags = get_vflags(p);
1.157     mycroft   857:        } else
                    858: #endif
                    859:        {
                    860:                __asm("movl %%gs,%w0" : "=r" (frame.sf_sc.sc_gs));
                    861:                __asm("movl %%fs,%w0" : "=r" (frame.sf_sc.sc_fs));
                    862:                frame.sf_sc.sc_es = tf->tf_es;
                    863:                frame.sf_sc.sc_ds = tf->tf_ds;
1.184     mycroft   864:                frame.sf_sc.sc_eflags = tf->tf_eflags;
1.157     mycroft   865:        }
1.184     mycroft   866:        frame.sf_sc.sc_edi = tf->tf_edi;
                    867:        frame.sf_sc.sc_esi = tf->tf_esi;
                    868:        frame.sf_sc.sc_ebp = tf->tf_ebp;
                    869:        frame.sf_sc.sc_ebx = tf->tf_ebx;
                    870:        frame.sf_sc.sc_edx = tf->tf_edx;
                    871:        frame.sf_sc.sc_ecx = tf->tf_ecx;
                    872:        frame.sf_sc.sc_eax = tf->tf_eax;
                    873:        frame.sf_sc.sc_eip = tf->tf_eip;
                    874:        frame.sf_sc.sc_cs = tf->tf_cs;
                    875:        frame.sf_sc.sc_esp = tf->tf_esp;
                    876:        frame.sf_sc.sc_ss = tf->tf_ss;
1.1       cgd       877:
1.87      mycroft   878:        if (copyout(&frame, fp, sizeof(frame)) != 0) {
1.1       cgd       879:                /*
                    880:                 * Process has trashed its stack; give it an illegal
                    881:                 * instruction to halt it in its tracks.
                    882:                 */
1.93      mycroft   883:                sigexit(p, SIGILL);
                    884:                /* NOTREACHED */
1.1       cgd       885:        }
                    886:
1.73      mycroft   887:        /*
1.59      mycroft   888:         * Build context to run handler in.
                    889:         */
1.185     mycroft   890:        __asm("movl %w0,%%gs" : : "r" (GSEL(GUDATA_SEL, SEL_UPL)));
                    891:        __asm("movl %w0,%%fs" : : "r" (GSEL(GUDATA_SEL, SEL_UPL)));
                    892:        tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL);
                    893:        tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
1.59      mycroft   894:        tf->tf_eip = (int)(((char *)PS_STRINGS) - (esigcode - sigcode));
1.185     mycroft   895:        tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
1.198     mycroft   896:        tf->tf_eflags &= ~(PSL_T|PSL_VM|PSL_AC);
1.185     mycroft   897:        tf->tf_esp = (int)fp;
1.177     mycroft   898:        tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
1.1       cgd       899: }
                    900:
                    901: /*
                    902:  * System call to cleanup state after a signal
                    903:  * has been taken.  Reset signal mask and
                    904:  * stack state from context left by sendsig (above).
                    905:  * Return to previous pc and psl as specified by
                    906:  * context left by sendsig. Check carefully to
                    907:  * make sure that the user has not modified the
1.110     mycroft   908:  * psl to gain improper privileges or to cause
1.1       cgd       909:  * a machine fault.
                    910:  */
1.195     mycroft   911: int
1.173     mycroft   912: sys_sigreturn(p, v, retval)
1.1       cgd       913:        struct proc *p;
1.172     thorpej   914:        void *v;
                    915:        register_t *retval;
                    916: {
1.173     mycroft   917:        struct sys_sigreturn_args /* {
1.123     cgd       918:                syscallarg(struct sigcontext *) sigcntxp;
1.172     thorpej   919:        } */ *uap = v;
1.82      ws        920:        struct sigcontext *scp, context;
1.59      mycroft   921:        register struct trapframe *tf;
                    922:
1.154     mycroft   923:        tf = p->p_md.md_regs;
1.1       cgd       924:
1.27      cgd       925:        /*
1.59      mycroft   926:         * The trampoline code hands us the context.
                    927:         * It is unsafe to keep track of it ourselves, in the event that a
                    928:         * program jumps out of a signal handler.
1.27      cgd       929:         */
1.123     cgd       930:        scp = SCARG(uap, sigcntxp);
1.87      mycroft   931:        if (copyin((caddr_t)scp, &context, sizeof(*scp)) != 0)
1.122     mycroft   932:                return (EFAULT);
1.1       cgd       933:
1.74      brezak    934:        /*
1.59      mycroft   935:         * Restore signal context.
                    936:         */
1.157     mycroft   937: #ifdef VM86
                    938:        if (context.sc_eflags & PSL_VM) {
                    939:                tf->tf_vm86_gs = context.sc_gs;
                    940:                tf->tf_vm86_fs = context.sc_fs;
                    941:                tf->tf_vm86_es = context.sc_es;
                    942:                tf->tf_vm86_ds = context.sc_ds;
1.196     mycroft   943:                set_vflags(p, context.sc_eflags);
1.157     mycroft   944:        } else
                    945: #endif
                    946:        {
1.196     mycroft   947:                /*
                    948:                 * Check for security violations.  If we're returning to
                    949:                 * protected mode, the CPU will validate the segment registers
                    950:                 * automatically and generate a trap on violations.  We handle
                    951:                 * the trap, rather than doing all of the checking here.
                    952:                 */
                    953:                if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
                    954:                    !USERMODE(context.sc_cs, context.sc_eflags))
                    955:                        return (EINVAL);
                    956:
1.157     mycroft   957:                /* %fs and %gs were restored by the trampoline. */
                    958:                tf->tf_es = context.sc_es;
                    959:                tf->tf_ds = context.sc_ds;
1.184     mycroft   960:                tf->tf_eflags = context.sc_eflags;
1.157     mycroft   961:        }
1.184     mycroft   962:        tf->tf_edi = context.sc_edi;
                    963:        tf->tf_esi = context.sc_esi;
                    964:        tf->tf_ebp = context.sc_ebp;
                    965:        tf->tf_ebx = context.sc_ebx;
                    966:        tf->tf_edx = context.sc_edx;
                    967:        tf->tf_ecx = context.sc_ecx;
                    968:        tf->tf_eax = context.sc_eax;
                    969:        tf->tf_eip = context.sc_eip;
                    970:        tf->tf_cs = context.sc_cs;
                    971:        tf->tf_esp = context.sc_esp;
                    972:        tf->tf_ss = context.sc_ss;
1.196     mycroft   973:
                    974:        if (context.sc_onstack & 01)
                    975:                p->p_sigacts->ps_sigstk.ss_flags |= SS_ONSTACK;
                    976:        else
                    977:                p->p_sigacts->ps_sigstk.ss_flags &= ~SS_ONSTACK;
                    978:        p->p_sigmask = context.sc_mask & ~sigcantmask;
1.72      mycroft   979:
1.122     mycroft   980:        return (EJUSTRETURN);
1.37      cgd       981: }
                    982:
1.1       cgd       983: int    waittime = -1;
                    984: struct pcb dumppcb;
                    985:
1.32      andrew    986: void
1.228     gwr       987: cpu_reboot(howto, bootstr)
1.193     mycroft   988:        int howto;
1.206     mrg       989:        char *bootstr;
1.1       cgd       990: {
                    991:        extern int cold;
                    992:
1.106     mycroft   993:        if (cold) {
1.193     mycroft   994:                howto |= RB_HALT;
                    995:                goto haltsys;
1.1       cgd       996:        }
1.193     mycroft   997:
1.106     mycroft   998:        boothowto = howto;
1.193     mycroft   999:        if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
1.1       cgd      1000:                waittime = 0;
1.150     mycroft  1001:                vfs_shutdown();
1.59      mycroft  1002:                /*
                   1003:                 * If we've been adjusting the clock, the todr
                   1004:                 * will be out of synch; adjust it now.
                   1005:                 */
                   1006:                resettodr();
1.1       cgd      1007:        }
1.193     mycroft  1008:
                   1009:        /* Disable interrupts. */
1.1       cgd      1010:        splhigh();
1.193     mycroft  1011:
                   1012:        /* Do a dump if requested. */
                   1013:        if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
                   1014:                dumpsys();
                   1015:
                   1016: haltsys:
                   1017:        doshutdownhooks();
                   1018:
1.152     cgd      1019:        if (howto & RB_HALT) {
1.208     jtk      1020: #if NAPM > 0 && !defined(APM_NO_POWEROFF)
                   1021:                /* turn off, if we can.  But try to turn disk off and
                   1022:                 * wait a bit first--some disk drives are slow to clean up
                   1023:                 * and users have reported disk corruption.
                   1024:                 */
                   1025:                delay(500000);
                   1026:                apm_set_powstate(APM_DEV_DISK(0xff), APM_SYS_OFF);
                   1027:                delay(500000);
                   1028:                apm_set_powstate(APM_DEV_ALLDEVS, APM_SYS_OFF);
                   1029: #endif
1.210     christos 1030:                printf("\n");
                   1031:                printf("The operating system has halted.\n");
                   1032:                printf("Please press any key to reboot.\n\n");
1.12      cgd      1033:                cngetc();
1.1       cgd      1034:        }
1.193     mycroft  1035:
1.210     christos 1036:        printf("rebooting...\n");
1.1       cgd      1037:        cpu_reset();
                   1038:        for(;;) ;
                   1039:        /*NOTREACHED*/
                   1040: }
                   1041:
1.116     gwr      1042: /*
                   1043:  * These variables are needed by /sbin/savecore
                   1044:  */
                   1045: u_long dumpmag = 0x8fca0101;   /* magic number */
                   1046: int    dumpsize = 0;           /* pages */
                   1047: long   dumplo = 0;             /* blocks */
                   1048:
                   1049: /*
1.228     gwr      1050:  * This is called by main to set dumplo and dumpsize.
1.116     gwr      1051:  * Dumps always skip the first CLBYTES of disk space
                   1052:  * in case there might be a disk label stored there.
                   1053:  * If there is extra space, put dump at the end to
                   1054:  * reduce the chance that swapping trashes it.
                   1055:  */
                   1056: void
1.228     gwr      1057: cpu_dumpconf()
1.116     gwr      1058: {
                   1059:        int nblks;      /* size of dump area */
                   1060:        int maj;
                   1061:
                   1062:        if (dumpdev == NODEV)
                   1063:                return;
                   1064:        maj = major(dumpdev);
                   1065:        if (maj < 0 || maj >= nblkdev)
                   1066:                panic("dumpconf: bad dumpdev=0x%x", dumpdev);
1.134     mycroft  1067:        if (bdevsw[maj].d_psize == NULL)
1.116     gwr      1068:                return;
1.134     mycroft  1069:        nblks = (*bdevsw[maj].d_psize)(dumpdev);
1.116     gwr      1070:        if (nblks <= ctod(1))
                   1071:                return;
                   1072:
1.163     cgd      1073:        dumpsize = btoc(IOM_END + ctob(dumpmem_high));
1.116     gwr      1074:
1.134     mycroft  1075:        /* Always skip the first CLBYTES, in case there is a label there. */
                   1076:        if (dumplo < ctod(1))
1.116     gwr      1077:                dumplo = ctod(1);
1.134     mycroft  1078:
                   1079:        /* Put dump at end of partition, and make it fit. */
                   1080:        if (dumpsize > dtoc(nblks - dumplo))
1.116     gwr      1081:                dumpsize = dtoc(nblks - dumplo);
1.134     mycroft  1082:        if (dumplo < nblks - ctod(dumpsize))
                   1083:                dumplo = nblks - ctod(dumpsize);
1.116     gwr      1084: }
                   1085:
1.1       cgd      1086: /*
                   1087:  * Doadump comes here after turning off memory management and
                   1088:  * getting on the dump stack, either when called above, or by
                   1089:  * the auto-restart code.
                   1090:  */
1.163     cgd      1091: #define BYTES_PER_DUMP  NBPG   /* must be a multiple of pagesize XXX small */
                   1092: static vm_offset_t dumpspace;
                   1093:
                   1094: vm_offset_t
                   1095: reserve_dumppages(p)
                   1096:        vm_offset_t p;
                   1097: {
                   1098:
                   1099:        dumpspace = p;
                   1100:        return (p + BYTES_PER_DUMP);
                   1101: }
                   1102:
1.32      andrew   1103: void
1.1       cgd      1104: dumpsys()
                   1105: {
1.163     cgd      1106:        unsigned bytes, i, n;
                   1107:        int maddr, psize;
                   1108:        daddr_t blkno;
                   1109:        int (*dump) __P((dev_t, daddr_t, caddr_t, size_t));
1.200     christos 1110:        int error;
1.193     mycroft  1111:
                   1112:        /* Save registers. */
                   1113:        savectx(&dumppcb);
1.1       cgd      1114:
1.163     cgd      1115:        msgbufmapped = 0;       /* don't record dump msgs in msgbuf */
1.1       cgd      1116:        if (dumpdev == NODEV)
                   1117:                return;
1.163     cgd      1118:
                   1119:        /*
                   1120:         * For dumps during autoconfiguration,
                   1121:         * if dump device has already configured...
                   1122:         */
                   1123:        if (dumpsize == 0)
1.228     gwr      1124:                cpu_dumpconf();
1.163     cgd      1125:        if (dumplo < 0)
                   1126:                return;
1.210     christos 1127:        printf("\ndumping to dev %x, offset %ld\n", dumpdev, dumplo);
1.134     mycroft  1128:
1.163     cgd      1129:        psize = (*bdevsw[major(dumpdev)].d_psize)(dumpdev);
1.210     christos 1130:        printf("dump ");
1.163     cgd      1131:        if (psize == -1) {
1.210     christos 1132:                printf("area unavailable\n");
1.163     cgd      1133:                return;
                   1134:        }
                   1135:
                   1136: #if 0  /* XXX this doesn't work.  grr. */
                   1137:         /* toss any characters present prior to dump */
                   1138:        while (sget() != NULL); /*syscons and pccons differ */
                   1139: #endif
                   1140:
                   1141:        bytes = ctob(dumpmem_high) + IOM_END;
                   1142:        maddr = 0;
                   1143:        blkno = dumplo;
                   1144:        dump = bdevsw[major(dumpdev)].d_dump;
1.200     christos 1145:        error = 0;
1.163     cgd      1146:        for (i = 0; i < bytes; i += n) {
                   1147:                /*
                   1148:                 * Avoid dumping the ISA memory hole, and areas that
                   1149:                 * BIOS claims aren't in low memory.
                   1150:                 */
                   1151:                if (i >= ctob(dumpmem_low) && i < IOM_END) {
                   1152:                        n = IOM_END - i;
                   1153:                        maddr += n;
                   1154:                        blkno += btodb(n);
                   1155:                        continue;
                   1156:                }
                   1157:
                   1158:                /* Print out how many MBs we to go. */
                   1159:                n = bytes - i;
                   1160:                if (n && (n % (1024*1024)) == 0)
1.210     christos 1161:                        printf("%d ", n / (1024 * 1024));
1.163     cgd      1162:
                   1163:                /* Limit size for next transfer. */
                   1164:                if (n > BYTES_PER_DUMP)
                   1165:                        n =  BYTES_PER_DUMP;
                   1166:
                   1167:                (void) pmap_map(dumpspace, maddr, maddr + n, VM_PROT_READ);
                   1168:                error = (*dump)(dumpdev, blkno, (caddr_t)dumpspace, n);
                   1169:                if (error)
                   1170:                        break;
                   1171:                maddr += n;
                   1172:                blkno += btodb(n);                      /* XXX? */
                   1173:
                   1174: #if 0  /* XXX this doesn't work.  grr. */
                   1175:                /* operator aborting dump? */
                   1176:                if (sget() != NULL) {
                   1177:                        error = EINTR;
                   1178:                        break;
                   1179:                }
                   1180: #endif
                   1181:        }
                   1182:
                   1183:        switch (error) {
1.1       cgd      1184:
                   1185:        case ENXIO:
1.210     christos 1186:                printf("device bad\n");
1.1       cgd      1187:                break;
                   1188:
                   1189:        case EFAULT:
1.210     christos 1190:                printf("device not ready\n");
1.1       cgd      1191:                break;
                   1192:
                   1193:        case EINVAL:
1.210     christos 1194:                printf("area improper\n");
1.1       cgd      1195:                break;
                   1196:
                   1197:        case EIO:
1.210     christos 1198:                printf("i/o error\n");
1.1       cgd      1199:                break;
                   1200:
                   1201:        case EINTR:
1.210     christos 1202:                printf("aborted from console\n");
1.1       cgd      1203:                break;
                   1204:
1.163     cgd      1205:        case 0:
1.210     christos 1206:                printf("succeeded\n");
1.163     cgd      1207:                break;
                   1208:
1.1       cgd      1209:        default:
1.210     christos 1210:                printf("error %d\n", error);
1.1       cgd      1211:                break;
                   1212:        }
1.210     christos 1213:        printf("\n\n");
1.163     cgd      1214:        delay(5000000);         /* 5 seconds */
1.1       cgd      1215: }
                   1216:
                   1217: /*
                   1218:  * Clear registers on exec
                   1219:  */
1.33      cgd      1220: void
1.151     christos 1221: setregs(p, pack, stack, retval)
1.1       cgd      1222:        struct proc *p;
1.151     christos 1223:        struct exec_package *pack;
1.21      cgd      1224:        u_long stack;
1.123     cgd      1225:        register_t *retval;
1.1       cgd      1226: {
1.178     mycroft  1227:        register struct pcb *pcb = &p->p_addr->u_pcb;
1.59      mycroft  1228:        register struct trapframe *tf;
1.1       cgd      1229:
1.161     mycroft  1230: #if NNPX > 0
                   1231:        /* If we were using the FPU, forget about it. */
                   1232:        if (npxproc == p)
1.166     mycroft  1233:                npxdrop();
1.161     mycroft  1234: #endif
1.166     mycroft  1235:
1.178     mycroft  1236: #ifdef USER_LDT
                   1237:        if (pcb->pcb_flags & PCB_USER_LDT)
                   1238:                i386_user_cleanup(pcb);
                   1239: #endif
                   1240:
1.167     mycroft  1241:        p->p_md.md_flags &= ~MDP_USEDFPU;
1.178     mycroft  1242:        pcb->pcb_flags = 0;
1.59      mycroft  1243:
1.154     mycroft  1244:        tf = p->p_md.md_regs;
1.178     mycroft  1245:        __asm("movl %w0,%%gs" : : "r" (LSEL(LUDATA_SEL, SEL_UPL)));
                   1246:        __asm("movl %w0,%%fs" : : "r" (LSEL(LUDATA_SEL, SEL_UPL)));
1.154     mycroft  1247:        tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
                   1248:        tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
                   1249:        tf->tf_ebp = 0;
1.160     mycroft  1250:        tf->tf_ebx = (int)PS_STRINGS;
1.154     mycroft  1251:        tf->tf_eip = pack->ep_entry;
                   1252:        tf->tf_cs = LSEL(LUCODE_SEL, SEL_UPL);
                   1253:        tf->tf_eflags = PSL_USERSET;
                   1254:        tf->tf_esp = stack;
                   1255:        tf->tf_ss = LSEL(LUDATA_SEL, SEL_UPL);
                   1256:
1.59      mycroft  1257:        retval[1] = 0;
1.1       cgd      1258: }
                   1259:
                   1260: /*
1.55      brezak   1261:  * Initialize segments and descriptor tables
1.1       cgd      1262:  */
                   1263:
1.179     mycroft  1264: union descriptor gdt[NGDT];
1.59      mycroft  1265: union descriptor ldt[NLDT];
                   1266: struct gate_descriptor idt[NIDT];
1.49      brezak   1267:
1.178     mycroft  1268: extern  struct user *proc0paddr;
1.49      brezak   1269:
1.178     mycroft  1270: void
                   1271: setgate(gd, func, args, type, dpl)
                   1272:        struct gate_descriptor *gd;
                   1273:        void *func;
                   1274:        int args, type, dpl;
                   1275: {
1.1       cgd      1276:
1.178     mycroft  1277:        gd->gd_looffset = (int)func;
                   1278:        gd->gd_selector = GSEL(GCODE_SEL, SEL_KPL);
                   1279:        gd->gd_stkcpy = args;
                   1280:        gd->gd_xx = 0;
                   1281:        gd->gd_type = type;
                   1282:        gd->gd_dpl = dpl;
                   1283:        gd->gd_p = 1;
                   1284:        gd->gd_hioffset = (int)func >> 16;
                   1285: }
                   1286:
                   1287: void
                   1288: setregion(rd, base, limit)
                   1289:        struct region_descriptor *rd;
                   1290:        void *base;
                   1291:        size_t limit;
                   1292: {
                   1293:
                   1294:        rd->rd_limit = (int)limit;
                   1295:        rd->rd_base = (int)base;
                   1296: }
1.1       cgd      1297:
1.174     mycroft  1298: void
                   1299: setsegment(sd, base, limit, type, dpl, def32, gran)
                   1300:        struct segment_descriptor *sd;
                   1301:        void *base;
                   1302:        size_t limit;
                   1303:        int type, dpl, def32, gran;
                   1304: {
1.1       cgd      1305:
1.174     mycroft  1306:        sd->sd_lolimit = (int)limit;
                   1307:        sd->sd_lobase = (int)base;
                   1308:        sd->sd_type = type;
                   1309:        sd->sd_dpl = dpl;
                   1310:        sd->sd_p = 1;
                   1311:        sd->sd_hilimit = (int)limit >> 16;
                   1312:        sd->sd_xx = 0;
                   1313:        sd->sd_def32 = def32;
                   1314:        sd->sd_gran = gran;
                   1315:        sd->sd_hibase = (int)base >> 24;
                   1316: }
1.1       cgd      1317:
                   1318: #define        IDTVEC(name)    __CONCAT(X, name)
1.225     mycroft  1319: extern IDTVEC(syscall), IDTVEC(osyscall);
                   1320: extern *IDTVEC(exceptions)[];
1.1       cgd      1321:
1.59      mycroft  1322: void
1.43      brezak   1323: init386(first_avail)
                   1324:        vm_offset_t first_avail;
1.2       cgd      1325: {
1.132     mycroft  1326:        int x;
1.148     mycroft  1327:        struct region_descriptor region;
                   1328:        extern void consinit __P((void));
1.1       cgd      1329:
                   1330:        proc0.p_addr = proc0paddr;
                   1331:
1.204     thorpej  1332:        /*
1.205     thorpej  1333:         * Initialize the I/O port and I/O mem extent maps.
                   1334:         * Note: we don't have to check the return value since
                   1335:         * creation of a fixed extent map will never fail (since
                   1336:         * descriptor storage has already been allocated).
1.213     thorpej  1337:         *
                   1338:         * N.B. The iomem extent manages _all_ physical addresses
                   1339:         * on the machine.  When the amount of RAM is found, the two
                   1340:         * extents of RAM are allocated from the map (0 -> ISA hole
                   1341:         * and end of ISA hole -> end of RAM).
1.204     thorpej  1342:         */
                   1343:        ioport_ex = extent_create("ioport", 0x0, 0xffff, M_DEVBUF,
1.212     fvdl     1344:            (caddr_t)ioport_ex_storage, sizeof(ioport_ex_storage),
1.211     thorpej  1345:            EX_NOCOALESCE|EX_NOWAIT);
1.213     thorpej  1346:        iomem_ex = extent_create("iomem", 0x0, 0xffffffff, M_DEVBUF,
1.212     fvdl     1347:            (caddr_t)iomem_ex_storage, sizeof(iomem_ex_storage),
1.211     thorpej  1348:            EX_NOCOALESCE|EX_NOWAIT);
1.204     thorpej  1349:
1.84      cgd      1350:        consinit();     /* XXX SHOULD NOT BE DONE HERE */
1.1       cgd      1351:
1.179     mycroft  1352:        /* make gdt gates and memory segments */
1.174     mycroft  1353:        setsegment(&gdt[GCODE_SEL].sd, 0, 0xfffff, SDT_MEMERA, SEL_KPL, 1, 1);
                   1354:        setsegment(&gdt[GDATA_SEL].sd, 0, 0xfffff, SDT_MEMRWA, SEL_KPL, 1, 1);
1.180     mycroft  1355:        setsegment(&gdt[GLDT_SEL].sd, ldt, sizeof(ldt) - 1, SDT_SYSLDT, SEL_KPL,
                   1356:            0, 0);
1.174     mycroft  1357:        setsegment(&gdt[GUCODE_SEL].sd, 0, i386_btop(VM_MAXUSER_ADDRESS) - 1,
                   1358:            SDT_MEMERA, SEL_UPL, 1, 1);
                   1359:        setsegment(&gdt[GUDATA_SEL].sd, 0, i386_btop(VM_MAXUSER_ADDRESS) - 1,
                   1360:            SDT_MEMRWA, SEL_UPL, 1, 1);
1.207     jtk      1361:        /* bios trampoline GDT entries */
                   1362:        setsegment(&gdt[GBIOSCODE_SEL].sd, 0, 0xfffff, SDT_MEMERA, SEL_KPL, 0, 0);
                   1363:        setsegment(&gdt[GBIOSDATA_SEL].sd, 0, 0xfffff, SDT_MEMRWA, SEL_KPL, 0, 0);
1.154     mycroft  1364:
1.179     mycroft  1365:        /* make ldt gates and memory segments */
1.148     mycroft  1366:        setgate(&ldt[LSYS5CALLS_SEL].gd, &IDTVEC(osyscall), 1, SDT_SYS386CGT,
                   1367:            SEL_UPL);
1.174     mycroft  1368:        ldt[LUCODE_SEL] = gdt[GUCODE_SEL];
                   1369:        ldt[LUDATA_SEL] = gdt[GUDATA_SEL];
                   1370:        ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
1.148     mycroft  1371:
1.1       cgd      1372:        /* exceptions */
1.225     mycroft  1373:        for (x = 0; x < 32; x++)
                   1374:                setgate(&idt[x], IDTVEC(exceptions)[x], 0, SDT_SYS386TGT,
                   1375:                    x == 3 ? SEL_UPL : SEL_KPL);
                   1376:
                   1377:        /* new-style interrupt gate for syscalls */
1.124     mycroft  1378:        setgate(&idt[128], &IDTVEC(syscall), 0, SDT_SYS386TGT, SEL_UPL);
1.1       cgd      1379:
1.178     mycroft  1380:        setregion(&region, gdt, sizeof(gdt) - 1);
                   1381:        lgdt(&region);
                   1382:        setregion(&region, idt, sizeof(idt) - 1);
                   1383:        lidt(&region);
                   1384:
1.95      mycroft  1385: #if NISA > 0
1.1       cgd      1386:        isa_defaultirq();
                   1387: #endif
1.124     mycroft  1388:
1.222     mycroft  1389:        splraise(-1);
1.124     mycroft  1390:        enable_intr();
                   1391:
1.59      mycroft  1392:        /*
1.203     mycroft  1393:         * Use BIOS values passed in from the boot program.
1.192     thorpej  1394:         *
                   1395:         * XXX Not only does probing break certain 386 AT relics, but
                   1396:         * not all BIOSes (Dell, Compaq, others) report the correct
                   1397:         * amount of extended memory.
1.1       cgd      1398:         */
1.215     fvdl     1399:        avail_end = biosextmem ? IOM_END + biosextmem * 1024
                   1400:            : biosbasemem * 1024;       /* just temporary use */
1.138     mycroft  1401:
1.213     thorpej  1402:        /*
                   1403:         * Allocate the physical addresses used by RAM from the iomem
                   1404:         * extent map.  This is done before the addresses are
                   1405:         * page rounded just to make sure we get them all.
                   1406:         */
                   1407:        if (extent_alloc_region(iomem_ex, 0, IOM_BEGIN, EX_NOWAIT)) {
                   1408:                /* XXX What should we do? */
                   1409:                printf("WARNING: CAN'T ALLOCATE BASE RAM FROM IOMEM EXTENT MAP!\n");
                   1410:        }
                   1411:        if (avail_end > IOM_END && extent_alloc_region(iomem_ex, IOM_END,
                   1412:            (avail_end - IOM_END), EX_NOWAIT)) {
                   1413:                /* XXX What should we do? */
                   1414:                printf("WARNING: CAN'T ALLOCATE EXTENDED MEMORY FROM IOMEM EXTENT MAP!\n");
                   1415:        }
                   1416:
1.148     mycroft  1417:        /* Round down to whole pages. */
1.138     mycroft  1418:        biosbasemem &= -(NBPG / 1024);
                   1419:        biosextmem &= -(NBPG / 1024);
1.59      mycroft  1420:
1.231     thorpej  1421: #if NISADMA > 0
                   1422:        /*
                   1423:         * Some motherboards/BIOSes remap the 384K of RAM that would
                   1424:         * normally be covered by the ISA hole to the end of memory
                   1425:         * so that it can be used.  However, on a 16M system, this
                   1426:         * would cause bounce buffers to be allocated and used.
                   1427:         * This is not desirable behaviour, as more than 384K of
                   1428:         * bounce buffers might be allocated.  As a work-around,
                   1429:         * we round memory down to the nearest 1M boundary if
                   1430:         * we're using any isadma devices and the remapped memory
                   1431:         * is what puts us over 16M.
                   1432:         */
                   1433:        if (biosextmem > (15*1024) && biosextmem < (16*1024)) {
                   1434:                printf("Warning: ignoring %dk of remapped memory\n",
                   1435:                    biosextmem - (15*1024));
                   1436:                biosextmem = (15*1024);
                   1437:        }
                   1438: #endif
                   1439:
1.207     jtk      1440: #if NAPM > 0
                   1441:        avail_start = 2*NBPG;   /* save us a page! */
                   1442: #else
1.59      mycroft  1443:        avail_start = NBPG;     /* BIOS leaves data in low memory */
1.43      brezak   1444:                                /* and VM system doesn't work with phys 0 */
1.207     jtk      1445: #endif
1.59      mycroft  1446:        avail_end = biosextmem ? IOM_END + biosextmem * 1024
1.73      mycroft  1447:            : biosbasemem * 1024;
                   1448:
1.59      mycroft  1449:        /* number of pages of physmem addr space */
                   1450:        physmem = btoc((biosbasemem + biosextmem) * 1024);
1.163     cgd      1451:        dumpmem_low = btoc(biosbasemem * 1024);
                   1452:        dumpmem_high = btoc(biosextmem * 1024);
1.2       cgd      1453:
1.43      brezak   1454:        /*
1.59      mycroft  1455:         * Initialize for pmap_free_pages and pmap_next_page.
                   1456:         * These guys should be page-aligned.
1.43      brezak   1457:         */
                   1458:        hole_start = biosbasemem * 1024;
1.59      mycroft  1459:        /* we load right after the I/O hole; adjust hole_end to compensate */
1.148     mycroft  1460:        hole_end = round_page(first_avail);
1.43      brezak   1461:        avail_next = avail_start;
1.73      mycroft  1462:
1.139     mycroft  1463:        if (physmem < btoc(2 * 1024 * 1024)) {
1.223     sommerfe 1464:                printf("warning: too little memory available; "
                   1465:                       "have %d bytes, want %d bytes\n"
                   1466:                       "running in degraded mode\n"
                   1467:                       "press a key to confirm\n\n",
                   1468:                       ctob(physmem), 2*1024*1024);
1.20      deraadt  1469:                cngetc();
1.2       cgd      1470:        }
1.59      mycroft  1471:
1.1       cgd      1472:        /* call pmap initialization to make new kernel address space */
1.59      mycroft  1473:        pmap_bootstrap((vm_offset_t)atdevbase + IOM_SIZE);
1.190     mycroft  1474:
                   1475: #ifdef DDB
                   1476:        ddb_init();
                   1477:        if (boothowto & RB_KDB)
                   1478:                Debugger();
                   1479: #endif
                   1480: #ifdef KGDB
                   1481:        if (boothowto & RB_KDB)
                   1482:                kgdb_connect(0);
                   1483: #endif
1.1       cgd      1484: }
                   1485:
1.94      mycroft  1486: struct queue {
                   1487:        struct queue *q_next, *q_prev;
                   1488: };
                   1489:
1.1       cgd      1490: /*
1.73      mycroft  1491:  * insert an element into a queue
1.1       cgd      1492:  */
1.94      mycroft  1493: void
1.188     christos 1494: _insque(v1, v2)
                   1495:        void *v1;
                   1496:        void *v2;
1.94      mycroft  1497: {
1.188     christos 1498:        register struct queue *elem = v1, *head = v2;
1.94      mycroft  1499:        register struct queue *next;
                   1500:
                   1501:        next = head->q_next;
                   1502:        elem->q_next = next;
                   1503:        head->q_next = elem;
                   1504:        elem->q_prev = head;
                   1505:        next->q_prev = elem;
1.1       cgd      1506: }
                   1507:
                   1508: /*
                   1509:  * remove an element from a queue
                   1510:  */
1.94      mycroft  1511: void
1.188     christos 1512: _remque(v)
                   1513:        void *v;
1.94      mycroft  1514: {
1.188     christos 1515:        register struct queue *elem = v;
1.94      mycroft  1516:        register struct queue *next, *prev;
                   1517:
                   1518:        next = elem->q_next;
                   1519:        prev = elem->q_prev;
                   1520:        next->q_prev = prev;
                   1521:        prev->q_next = next;
                   1522:        elem->q_prev = 0;
1.1       cgd      1523: }
                   1524:
1.107     deraadt  1525: #ifdef COMPAT_NOMID
                   1526: static int
                   1527: exec_nomid(p, epp)
1.59      mycroft  1528:        struct proc *p;
                   1529:        struct exec_package *epp;
1.31      cgd      1530: {
1.59      mycroft  1531:        int error;
                   1532:        u_long midmag, magic;
                   1533:        u_short mid;
1.80      cgd      1534:        struct exec *execp = epp->ep_hdr;
1.31      cgd      1535:
1.80      cgd      1536:        /* check on validity of epp->ep_hdr performed by exec_out_makecmds */
                   1537:
                   1538:        midmag = ntohl(execp->a_midmag);
1.59      mycroft  1539:        mid = (midmag >> 16) & 0xffff;
                   1540:        magic = midmag & 0xffff;
                   1541:
                   1542:        if (magic == 0) {
1.80      cgd      1543:                magic = (execp->a_midmag & 0xffff);
1.59      mycroft  1544:                mid = MID_ZERO;
                   1545:        }
                   1546:
                   1547:        midmag = mid << 16 | magic;
                   1548:
                   1549:        switch (midmag) {
                   1550:        case (MID_ZERO << 16) | ZMAGIC:
                   1551:                /*
                   1552:                 * 386BSD's ZMAGIC format:
                   1553:                 */
1.202     christos 1554:                error = exec_aout_prep_oldzmagic(p, epp);
1.59      mycroft  1555:                break;
                   1556:
                   1557:        case (MID_ZERO << 16) | QMAGIC:
                   1558:                /*
                   1559:                 * BSDI's QMAGIC format:
                   1560:                 * same as new ZMAGIC format, but with different magic number
                   1561:                 */
                   1562:                error = exec_aout_prep_zmagic(p, epp);
                   1563:                break;
                   1564:
1.202     christos 1565:        case (MID_ZERO << 16) | NMAGIC:
                   1566:                /*
                   1567:                 * BSDI's NMAGIC format:
                   1568:                 * same as NMAGIC format, but with different magic number
                   1569:                 * and with text starting at 0.
                   1570:                 */
                   1571:                error = exec_aout_prep_oldnmagic(p, epp);
                   1572:                break;
                   1573:
                   1574:        case (MID_ZERO << 16) | OMAGIC:
                   1575:                /*
                   1576:                 * BSDI's OMAGIC format:
                   1577:                 * same as OMAGIC format, but with different magic number
                   1578:                 * and with text starting at 0.
                   1579:                 */
                   1580:                error = exec_aout_prep_oldomagic(p, epp);
                   1581:                break;
                   1582:
1.59      mycroft  1583:        default:
                   1584:                error = ENOEXEC;
                   1585:        }
                   1586:
                   1587:        return error;
1.107     deraadt  1588: }
1.31      cgd      1589: #endif
1.107     deraadt  1590:
                   1591: /*
                   1592:  * cpu_exec_aout_makecmds():
                   1593:  *     cpu-dependent a.out format hook for execve().
                   1594:  *
                   1595:  * Determine of the given exec package refers to something which we
                   1596:  * understand and, if so, set up the vmcmds for it.
                   1597:  *
                   1598:  * On the i386, old (386bsd) ZMAGIC binaries and BSDI QMAGIC binaries
                   1599:  * if COMPAT_NOMID is given as a kernel option.
                   1600:  */
                   1601: int
                   1602: cpu_exec_aout_makecmds(p, epp)
                   1603:        struct proc *p;
                   1604:        struct exec_package *epp;
                   1605: {
                   1606:        int error = ENOEXEC;
                   1607:
                   1608: #ifdef COMPAT_NOMID
                   1609:        if ((error = exec_nomid(p, epp)) == 0)
                   1610:                return error;
                   1611: #endif /* ! COMPAT_NOMID */
                   1612:
                   1613:        return error;
1.31      cgd      1614: }
1.43      brezak   1615:
1.139     mycroft  1616: u_int
1.59      mycroft  1617: pmap_free_pages()
                   1618: {
                   1619:
1.139     mycroft  1620:        if (avail_next <= hole_start)
                   1621:                return ((hole_start - avail_next) / NBPG +
                   1622:                        (avail_end - hole_end) / NBPG);
                   1623:        else
                   1624:                return ((avail_end - avail_next) / NBPG);
1.59      mycroft  1625: }
                   1626:
                   1627: int
                   1628: pmap_next_page(addrp)
                   1629:        vm_offset_t *addrp;
                   1630: {
                   1631:
1.139     mycroft  1632:        if (avail_next + NBPG > avail_end)
1.59      mycroft  1633:                return FALSE;
1.73      mycroft  1634:
1.139     mycroft  1635:        if (avail_next + NBPG > hole_start && avail_next < hole_end)
1.59      mycroft  1636:                avail_next = hole_end;
1.73      mycroft  1637:
1.59      mycroft  1638:        *addrp = avail_next;
                   1639:        avail_next += NBPG;
                   1640:        return TRUE;
                   1641: }
                   1642:
1.189     christos 1643: int
1.59      mycroft  1644: pmap_page_index(pa)
                   1645:        vm_offset_t pa;
                   1646: {
                   1647:
                   1648:        if (pa >= avail_start && pa < hole_start)
                   1649:                return i386_btop(pa - avail_start);
                   1650:        if (pa >= hole_end && pa < avail_end)
                   1651:                return i386_btop(pa - hole_end + hole_start - avail_start);
                   1652:        return -1;
1.84      cgd      1653: }
                   1654:
                   1655: /*
                   1656:  * consinit:
                   1657:  * initialize the system console.
1.94      mycroft  1658:  * XXX - shouldn't deal with this initted thing, but then,
1.84      cgd      1659:  * it shouldn't be called from init386 either.
                   1660:  */
                   1661: void
                   1662: consinit()
                   1663: {
1.94      mycroft  1664:        static int initted;
                   1665:
                   1666:        if (initted)
                   1667:                return;
                   1668:        initted = 1;
                   1669:        cninit();
1.149     mycroft  1670: }
                   1671:
                   1672: void
                   1673: cpu_reset()
                   1674: {
                   1675:        struct region_descriptor region;
                   1676:
1.224     mycroft  1677:        disable_intr();
                   1678:
1.227     mycroft  1679:        /*
                   1680:         * The keyboard controller has 4 random output pins, one of which is
                   1681:         * connected to the RESET pin on the CPU in many PCs.  We tell the
                   1682:         * keyboard controller to pulse this line a couple of times.
                   1683:         */
1.149     mycroft  1684:        outb(KBCMDP, KBC_PULSE0);
1.226     mycroft  1685:        delay(100000);
1.149     mycroft  1686:        outb(KBCMDP, KBC_PULSE0);
1.226     mycroft  1687:        delay(100000);
1.149     mycroft  1688:
                   1689:        /*
1.224     mycroft  1690:         * Try to cause a triple fault and watchdog reset by making the IDT
                   1691:         * invalid and causing a fault.
1.149     mycroft  1692:         */
1.224     mycroft  1693:        bzero((caddr_t)idt, sizeof(idt));
                   1694:        setregion(&region, idt, sizeof(idt) - 1);
1.149     mycroft  1695:        lidt(&region);
1.224     mycroft  1696:        __asm __volatile("divl %0,%1" : : "q" (0), "a" (0));
1.149     mycroft  1697:
1.224     mycroft  1698: #if 0
1.149     mycroft  1699:        /*
                   1700:         * Try to cause a triple fault and watchdog reset by unmapping the
1.224     mycroft  1701:         * entire address space and doing a TLB flush.
1.149     mycroft  1702:         */
                   1703:        bzero((caddr_t)PTD, NBPG);
                   1704:        pmap_update();
1.224     mycroft  1705: #endif
1.149     mycroft  1706:
                   1707:        for (;;);
1.194     cgd      1708: }
                   1709:
                   1710: int
1.213     thorpej  1711: bus_space_map(t, bpa, size, cacheable, bshp)
                   1712:        bus_space_tag_t t;
                   1713:        bus_addr_t bpa;
                   1714:        bus_size_t size;
1.194     cgd      1715:        int cacheable;
1.213     thorpej  1716:        bus_space_handle_t *bshp;
1.194     cgd      1717: {
1.213     thorpej  1718:        int error;
                   1719:        struct extent *ex;
                   1720:
                   1721:        /*
                   1722:         * Pick the appropriate extent map.
                   1723:         */
                   1724:        switch (t) {
                   1725:        case I386_BUS_SPACE_IO:
                   1726:                ex = ioport_ex;
                   1727:                break;
                   1728:
                   1729:        case I386_BUS_SPACE_MEM:
                   1730:                ex = iomem_ex;
                   1731:                break;
                   1732:
                   1733:        default:
                   1734:                panic("bus_space_map: bad bus space tag");
                   1735:        }
1.205     thorpej  1736:
                   1737:        /*
                   1738:         * Before we go any further, let's make sure that this
1.213     thorpej  1739:         * region is available.
                   1740:         */
                   1741:        error = extent_alloc_region(ex, bpa, size,
                   1742:            EX_NOWAIT | (ioport_malloc_safe ? EX_MALLOCOK : 0));
                   1743:        if (error)
                   1744:                return (error);
                   1745:
                   1746:        /*
                   1747:         * For I/O space, that's all she wrote.
                   1748:         */
                   1749:        if (t == I386_BUS_SPACE_IO) {
                   1750:                *bshp = bpa;
                   1751:                return (0);
                   1752:        }
                   1753:
                   1754:        /*
                   1755:         * For memory space, map the bus physical address to
                   1756:         * a kernel virtual address.
                   1757:         */
                   1758:        error = bus_mem_add_mapping(bpa, size, cacheable, bshp);
                   1759:        if (error) {
                   1760:                if (extent_free(ex, bpa, size, EX_NOWAIT |
                   1761:                    (ioport_malloc_safe ? EX_MALLOCOK : 0))) {
                   1762:                        printf("bus_space_map: pa 0x%lx, size 0x%lx\n",
                   1763:                            bpa, size);
                   1764:                        printf("bus_space_map: can't free region\n");
                   1765:                }
                   1766:        }
                   1767:
                   1768:        return (error);
                   1769: }
                   1770:
                   1771: int
                   1772: bus_space_alloc(t, rstart, rend, size, alignment, boundary, cacheable,
                   1773:     bpap, bshp)
                   1774:        bus_space_tag_t t;
                   1775:        bus_addr_t rstart, rend;
1.214     thorpej  1776:        bus_size_t size, alignment, boundary;
1.213     thorpej  1777:        int cacheable;
                   1778:        bus_addr_t *bpap;
                   1779:        bus_space_handle_t *bshp;
                   1780: {
                   1781:        struct extent *ex;
                   1782:        u_long bpa;
                   1783:        int error;
                   1784:
                   1785:        /*
                   1786:         * Pick the appropriate extent map.
                   1787:         */
                   1788:        switch (t) {
                   1789:        case I386_BUS_SPACE_IO:
                   1790:                ex = ioport_ex;
                   1791:                break;
                   1792:
                   1793:        case I386_BUS_SPACE_MEM:
                   1794:                ex = iomem_ex;
                   1795:                break;
                   1796:
                   1797:        default:
                   1798:                panic("bus_space_alloc: bad bus space tag");
                   1799:        }
                   1800:
                   1801:        /*
                   1802:         * Sanity check the allocation against the extent's boundaries.
                   1803:         */
                   1804:        if (rstart < ex->ex_start || rend > ex->ex_end)
                   1805:                panic("bus_space_alloc: bad region start/end");
                   1806:
                   1807:        /*
                   1808:         * Do the requested allocation.
                   1809:         */
                   1810:        error = extent_alloc_subregion(ex, rstart, rend, size, alignment,
                   1811:            boundary, EX_NOWAIT | (ioport_malloc_safe ?  EX_MALLOCOK : 0),
                   1812:            &bpa);
                   1813:
                   1814:        if (error)
                   1815:                return (error);
                   1816:
                   1817:        /*
                   1818:         * For I/O space, that's all she wrote.
                   1819:         */
                   1820:        if (t == I386_BUS_SPACE_IO) {
                   1821:                *bshp = *bpap = bpa;
                   1822:                return (0);
                   1823:        }
                   1824:
                   1825:        /*
                   1826:         * For memory space, map the bus physical address to
                   1827:         * a kernel virtual address.
                   1828:         */
                   1829:        error = bus_mem_add_mapping(bpa, size, cacheable, bshp);
                   1830:        if (error) {
                   1831:                if (extent_free(iomem_ex, bpa, size, EX_NOWAIT |
                   1832:                    (ioport_malloc_safe ? EX_MALLOCOK : 0))) {
                   1833:                        printf("bus_space_alloc: pa 0x%lx, size 0x%lx\n",
                   1834:                            bpa, size);
                   1835:                        printf("bus_space_alloc: can't free region\n");
                   1836:                }
1.205     thorpej  1837:        }
1.194     cgd      1838:
1.213     thorpej  1839:        *bpap = bpa;
                   1840:
                   1841:        return (error);
                   1842: }
                   1843:
                   1844: int
                   1845: bus_mem_add_mapping(bpa, size, cacheable, bshp)
                   1846:        bus_addr_t bpa;
                   1847:        bus_size_t size;
                   1848:        int cacheable;
                   1849:        bus_space_handle_t *bshp;
                   1850: {
                   1851:        u_long pa, endpa;
                   1852:        vm_offset_t va;
                   1853:
1.194     cgd      1854:        pa = i386_trunc_page(bpa);
1.230     perry    1855:        endpa = i386_round_page(bpa + size);
1.201     thorpej  1856:
                   1857: #ifdef DIAGNOSTIC
                   1858:        if (endpa <= pa)
1.213     thorpej  1859:                panic("bus_mem_add_mapping: overflow");
1.201     thorpej  1860: #endif
1.194     cgd      1861:
                   1862:        va = kmem_alloc_pageable(kernel_map, endpa - pa);
1.213     thorpej  1863:        if (va == 0)
                   1864:                return (ENOMEM);
                   1865:
                   1866:        *bshp = (bus_space_handle_t)(va + (bpa & PGOFSET));
1.194     cgd      1867:
                   1868:        for (; pa < endpa; pa += NBPG, va += NBPG) {
1.213     thorpej  1869:                pmap_enter(pmap_kernel(), va, pa,
                   1870:                    VM_PROT_READ | VM_PROT_WRITE, TRUE);
1.205     thorpej  1871:                if (!cacheable)
                   1872:                        pmap_changebit(pa, PG_N, ~0);
                   1873:                else
                   1874:                        pmap_changebit(pa, 0, ~PG_N);
                   1875:        }
1.194     cgd      1876:
1.205     thorpej  1877:        return 0;
1.194     cgd      1878: }
                   1879:
                   1880: void
1.213     thorpej  1881: bus_space_unmap(t, bsh, size)
                   1882:        bus_space_tag_t t;
                   1883:        bus_space_handle_t bsh;
                   1884:        bus_size_t size;
                   1885: {
                   1886:        struct extent *ex;
                   1887:        u_long va, endva;
                   1888:        bus_addr_t bpa;
                   1889:
                   1890:        /*
                   1891:         * Find the correct extent and bus physical address.
                   1892:         */
                   1893:        switch (t) {
                   1894:        case I386_BUS_SPACE_IO:
                   1895:                ex = ioport_ex;
                   1896:                bpa = bsh;
                   1897:                break;
1.194     cgd      1898:
1.213     thorpej  1899:        case I386_BUS_SPACE_MEM:
                   1900:                ex = iomem_ex;
                   1901:                va = i386_trunc_page(bsh);
1.230     perry    1902:                endva = i386_round_page(bsh + size);
1.201     thorpej  1903:
                   1904: #ifdef DIAGNOSTIC
1.213     thorpej  1905:                if (endva <= va)
                   1906:                        panic("bus_space_unmap: overflow");
1.201     thorpej  1907: #endif
                   1908:
1.213     thorpej  1909:                bpa = pmap_extract(pmap_kernel(), va) + (bsh & PGOFSET);
                   1910:
                   1911:                /*
                   1912:                 * Free the kernel virtual mapping.
                   1913:                 */
                   1914:                kmem_free(kernel_map, va, endva - va);
                   1915:                break;
                   1916:
                   1917:        default:
                   1918:                panic("bus_space_unmap: bad bus space tag");
1.205     thorpej  1919:        }
                   1920:
1.213     thorpej  1921:        if (extent_free(ex, bpa, size,
                   1922:            EX_NOWAIT | (ioport_malloc_safe ? EX_MALLOCOK : 0))) {
                   1923:                printf("bus_space_unmap: %s 0x%lx, size 0x%lx\n",
                   1924:                    (t == I386_BUS_SPACE_IO) ? "port" : "pa", bpa, size);
                   1925:                printf("bus_space_unmap: can't free region\n");
                   1926:        }
1.204     thorpej  1927: }
                   1928:
1.213     thorpej  1929: void
                   1930: bus_space_free(t, bsh, size)
                   1931:        bus_space_tag_t t;
                   1932:        bus_space_handle_t bsh;
                   1933:        bus_size_t size;
1.204     thorpej  1934: {
                   1935:
1.213     thorpej  1936:        /* bus_space_unmap() does all that we need to do. */
                   1937:        bus_space_unmap(t, bsh, size);
1.204     thorpej  1938: }
                   1939:
1.213     thorpej  1940: int
                   1941: bus_space_subregion(t, bsh, offset, size, nbshp)
                   1942:        bus_space_tag_t t;
                   1943:        bus_space_handle_t bsh;
                   1944:        bus_size_t offset, size;
                   1945:        bus_space_handle_t *nbshp;
1.204     thorpej  1946: {
                   1947:
1.213     thorpej  1948:        *nbshp = bsh + offset;
1.231     thorpej  1949:        return (0);
                   1950: }
                   1951:
                   1952: /*
                   1953:  * Common function for DMA map creation.  May be called by bus-specific
                   1954:  * DMA map creation functions.
                   1955:  */
                   1956: int
                   1957: _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
                   1958:        bus_dma_tag_t t;
                   1959:        bus_size_t size;
                   1960:        int nsegments;
                   1961:        bus_size_t maxsegsz;
                   1962:        bus_size_t boundary;
                   1963:        int flags;
                   1964:        bus_dmamap_t *dmamp;
                   1965: {
                   1966:        struct i386_bus_dmamap *map;
                   1967:        void *mapstore;
                   1968:        size_t mapsize;
                   1969:
                   1970:        /*
                   1971:         * Allocate and initialize the DMA map.  The end of the map
                   1972:         * is a variable-sized array of segments, so we allocate enough
                   1973:         * room for them in one shot.
                   1974:         *
                   1975:         * Note we don't preserve the WAITOK or NOWAIT flags.  Preservation
                   1976:         * of ALLOCNOW notifies others that we've reserved these resources,
                   1977:         * and they are not to be freed.
                   1978:         *
                   1979:         * The bus_dmamap_t includes one bus_dma_segment_t, hence
                   1980:         * the (nsegments - 1).
                   1981:         */
                   1982:        mapsize = sizeof(struct i386_bus_dmamap) +
                   1983:            (sizeof(bus_dma_segment_t) * (nsegments - 1));
                   1984:        if ((mapstore = malloc(mapsize, M_DEVBUF,
                   1985:            (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL)
                   1986:                return (ENOMEM);
                   1987:
                   1988:        bzero(mapstore, mapsize);
                   1989:        map = (struct i386_bus_dmamap *)mapstore;
                   1990:        map->_dm_size = size;
                   1991:        map->_dm_segcnt = nsegments;
                   1992:        map->_dm_maxsegsz = maxsegsz;
                   1993:        map->_dm_boundary = boundary;
                   1994:        map->_dm_flags = flags & ~(BUS_DMA_WAITOK|BUS_DMA_NOWAIT);
                   1995:        map->dm_nsegs = 0;              /* no valid mappings */
                   1996:
                   1997:        *dmamp = map;
                   1998:        return (0);
                   1999: }
                   2000:
                   2001: /*
                   2002:  * Common function for DMA map destruction.  May be called by bus-specific
                   2003:  * DMA map destruction functions.
                   2004:  */
                   2005: void
                   2006: _bus_dmamap_destroy(t, map)
                   2007:        bus_dma_tag_t t;
                   2008:        bus_dmamap_t map;
                   2009: {
                   2010:
                   2011:        free(map, M_DEVBUF);
                   2012: }
                   2013:
                   2014: /*
                   2015:  * Common function for loading a DMA map with a linear buffer.  May
                   2016:  * be called by bus-specific DMA map load functions.
                   2017:  */
                   2018: int
                   2019: _bus_dmamap_load(t, map, buf, buflen, p, flags)
                   2020:        bus_dma_tag_t t;
                   2021:        bus_dmamap_t map;
                   2022:        void *buf;
                   2023:        bus_size_t buflen;
                   2024:        struct proc *p;
                   2025:        int flags;
                   2026: {
                   2027:        bus_size_t sgsize;
                   2028:        bus_addr_t curaddr, lastaddr;
                   2029:        caddr_t vaddr = buf;
                   2030:        int first, seg;
1.234   ! thorpej  2031:        pmap_t pmap;
1.231     thorpej  2032:
                   2033:        /*
                   2034:         * Make sure that on error condition we return "no valid mappings".
                   2035:         */
                   2036:        map->dm_nsegs = 0;
                   2037:
1.234   ! thorpej  2038:        if (buflen > map->_dm_size)
        !          2039:                return (EINVAL);
        !          2040:
1.231     thorpej  2041:        /*
                   2042:         * XXX Need to implement "don't dma across this boundry".
                   2043:         */
                   2044:
1.234   ! thorpej  2045:        if (p != NULL)
        !          2046:                pmap = p->p_vmspace->vm_map.pmap;
        !          2047:        else
        !          2048:                pmap = pmap_kernel();
        !          2049:
1.231     thorpej  2050:        lastaddr = ~0;          /* XXX gcc */
                   2051:        for (first = 1, seg = 0; buflen > 0 && seg < map->_dm_segcnt; ) {
                   2052:                /*
                   2053:                 * Get the physical address for this segment.
                   2054:                 */
1.234   ! thorpej  2055:                curaddr = (bus_addr_t)pmap_extract(pmap, (vm_offset_t)vaddr);
1.231     thorpej  2056:
                   2057:                /*
                   2058:                 * Compute the segment size, and adjust counts.
                   2059:                 */
                   2060:                sgsize = NBPG - ((u_long)vaddr & PGOFSET);
                   2061:                if (buflen < sgsize)
                   2062:                        sgsize = buflen;
                   2063:
                   2064:                /*
                   2065:                 * Insert chunk into a segment, coalescing with
                   2066:                 * previous segment if possible.
                   2067:                 */
                   2068:                if (first) {
                   2069:                        map->dm_segs[seg].ds_addr = curaddr;
                   2070:                        map->dm_segs[seg].ds_len = sgsize;
                   2071:                        first = 0;
                   2072:                } else {
                   2073:                        if (curaddr == lastaddr &&
                   2074:                            (map->dm_segs[seg].ds_len + sgsize) <=
                   2075:                             map->_dm_maxsegsz)
                   2076:                                map->dm_segs[seg].ds_len += sgsize;
                   2077:                        else {
                   2078:                                seg++;
                   2079:                                map->dm_segs[seg].ds_addr = curaddr;
                   2080:                                map->dm_segs[seg].ds_len = sgsize;
                   2081:                        }
                   2082:                }
                   2083:
                   2084:                lastaddr = curaddr + sgsize;
                   2085:                vaddr += sgsize;
                   2086:                buflen -= sgsize;
                   2087:        }
                   2088:
                   2089:        /*
                   2090:         * Did we fit?
                   2091:         */
                   2092:        if (buflen != 0)
                   2093:                return (EFBIG);         /* XXX better return value here? */
                   2094:
                   2095:        map->dm_nsegs = seg + 1;
                   2096:        return (0);
                   2097: }
                   2098:
                   2099: /*
                   2100:  * Like _bus_dmamap_load(), but for mbufs.
                   2101:  */
                   2102: int
                   2103: _bus_dmamap_load_mbuf(t, map, m, flags)
                   2104:        bus_dma_tag_t t;
                   2105:        bus_dmamap_t map;
                   2106:        struct mbuf *m;
                   2107:        int flags;
                   2108: {
                   2109:
                   2110:        panic("_bus_dmamap_load: not implemented");
                   2111: }
                   2112:
                   2113: /*
                   2114:  * Like _bus_dmamap_load(), but for uios.
                   2115:  */
                   2116: int
                   2117: _bus_dmamap_load_uio(t, map, uio, flags)
                   2118:        bus_dma_tag_t t;
                   2119:        bus_dmamap_t map;
                   2120:        struct uio *uio;
                   2121:        int flags;
                   2122: {
                   2123:
                   2124:        panic("_bus_dmamap_load_uio: not implemented");
                   2125: }
                   2126:
                   2127: /*
                   2128:  * Like _bus_dmamap_load(), but for raw memory allocated with
                   2129:  * bus_dmamem_alloc().
                   2130:  */
                   2131: int
                   2132: _bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
                   2133:        bus_dma_tag_t t;
                   2134:        bus_dmamap_t map;
                   2135:        bus_dma_segment_t *segs;
                   2136:        int nsegs;
                   2137:        bus_size_t size;
                   2138:        int flags;
                   2139: {
                   2140:
                   2141:        panic("_bus_dmamap_load_raw: not implemented");
                   2142: }
                   2143:
                   2144: /*
                   2145:  * Common function for unloading a DMA map.  May be called by
                   2146:  * bus-specific DMA map unload functions.
                   2147:  */
                   2148: void
                   2149: _bus_dmamap_unload(t, map)
                   2150:        bus_dma_tag_t t;
                   2151:        bus_dmamap_t map;
                   2152: {
                   2153:
                   2154:        /*
                   2155:         * No resources to free; just mark the mappings as
                   2156:         * invalid.
                   2157:         */
                   2158:        map->dm_nsegs = 0;
                   2159: }
                   2160:
                   2161: /*
                   2162:  * Common function for DMA map synchronization.  May be called
                   2163:  * by bus-specific DMA map synchronization functions.
                   2164:  */
                   2165: void
                   2166: _bus_dmamap_sync(t, map, op)
                   2167:        bus_dma_tag_t t;
                   2168:        bus_dmamap_t map;
                   2169:        bus_dmasync_op_t op;
                   2170: {
                   2171:
                   2172:        /* Nothing to do here. */
                   2173: }
                   2174:
                   2175: /*
                   2176:  * Common function for DMA-safe memory allocation.  May be called
                   2177:  * by bus-specific DMA memory allocation functions.
                   2178:  */
                   2179: int
                   2180: _bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
                   2181:        bus_dma_tag_t t;
                   2182:        bus_size_t size, alignment, boundary;
                   2183:        bus_dma_segment_t *segs;
                   2184:        int nsegs;
                   2185:        int *rsegs;
                   2186:        int flags;
                   2187: {
                   2188:
                   2189:        return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
                   2190:            segs, nsegs, rsegs, flags, 0, trunc_page(avail_end)));
                   2191: }
                   2192:
                   2193: /*
                   2194:  * Common function for freeing DMA-safe memory.  May be called by
                   2195:  * bus-specific DMA memory free functions.
                   2196:  */
                   2197: void
                   2198: _bus_dmamem_free(t, segs, nsegs)
                   2199:        bus_dma_tag_t t;
                   2200:        bus_dma_segment_t *segs;
                   2201:        int nsegs;
                   2202: {
                   2203:        vm_page_t m;
                   2204:        bus_addr_t addr;
                   2205:        struct pglist mlist;
                   2206:        int curseg;
                   2207:
                   2208:        /*
                   2209:         * Build a list of pages to free back to the VM system.
                   2210:         */
                   2211:        TAILQ_INIT(&mlist);
                   2212:        for (curseg = 0; curseg < nsegs; curseg++) {
                   2213:                for (addr = segs[curseg].ds_addr;
                   2214:                    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
                   2215:                    addr += PAGE_SIZE) {
                   2216:                        m = PHYS_TO_VM_PAGE(addr);
                   2217:                        TAILQ_INSERT_TAIL(&mlist, m, pageq);
                   2218:                }
                   2219:        }
                   2220:
                   2221:        vm_page_free_memory(&mlist);
                   2222: }
                   2223:
                   2224: /*
                   2225:  * Common function for mapping DMA-safe memory.  May be called by
                   2226:  * bus-specific DMA memory map functions.
                   2227:  */
                   2228: int
                   2229: _bus_dmamem_map(t, segs, nsegs, size, kvap, flags)
                   2230:        bus_dma_tag_t t;
                   2231:        bus_dma_segment_t *segs;
                   2232:        int nsegs;
                   2233:        size_t size;
                   2234:        caddr_t *kvap;
                   2235:        int flags;
                   2236: {
                   2237:        vm_offset_t va;
                   2238:        bus_addr_t addr;
                   2239:        int curseg;
                   2240:
                   2241:        size = round_page(size);
                   2242:        va = kmem_alloc_pageable(kmem_map, size);
                   2243:        if (va == 0)
                   2244:                return (ENOMEM);
                   2245:
                   2246:        *kvap = (caddr_t)va;
                   2247:
                   2248:        for (curseg = 0; curseg < nsegs; curseg++) {
                   2249:                for (addr = segs[curseg].ds_addr;
                   2250:                    addr < (segs[curseg].ds_addr + segs[curseg].ds_len);
                   2251:                    addr += NBPG, va += NBPG, size -= NBPG) {
                   2252:                        if (size == 0)
                   2253:                                panic("_bus_dmamem_map: size botch");
                   2254:                        pmap_enter(pmap_kernel(), va, addr,
                   2255:                            VM_PROT_READ | VM_PROT_WRITE, TRUE);
                   2256: #if 0
                   2257:                        if (flags & BUS_DMAMEM_NOSYNC)
                   2258:                                pmap_changebit(addr, PG_N, ~0);
                   2259:                        else
                   2260:                                pmap_changebit(addr, 0, ~PG_N);
                   2261: #endif
                   2262:                }
                   2263:        }
                   2264:
                   2265:        return (0);
                   2266: }
                   2267:
                   2268: /*
                   2269:  * Common function for unmapping DMA-safe memory.  May be called by
                   2270:  * bus-specific DMA memory unmapping functions.
                   2271:  */
                   2272: void
                   2273: _bus_dmamem_unmap(t, kva, size)
                   2274:        bus_dma_tag_t t;
                   2275:        caddr_t kva;
                   2276:        size_t size;
                   2277: {
                   2278:
                   2279: #ifdef DIAGNOSTIC
                   2280:        if ((u_long)kva & PGOFSET)
                   2281:                panic("_bus_dmamem_unmap");
                   2282: #endif
                   2283:
                   2284:        size = round_page(size);
                   2285:        kmem_free(kmem_map, (vm_offset_t)kva, size);
                   2286: }
                   2287:
                   2288: /*
                   2289:  * Common functin for mmap(2)'ing DMA-safe memory.  May be called by
                   2290:  * bus-specific DMA mmap(2)'ing functions.
                   2291:  */
                   2292: int
                   2293: _bus_dmamem_mmap(t, segs, nsegs, off, prot, flags)
                   2294:        bus_dma_tag_t t;
                   2295:        bus_dma_segment_t *segs;
                   2296:        int nsegs, off, prot, flags;
                   2297: {
                   2298:
                   2299:        panic("_bus_dmamem_mmap: not implemented");
                   2300: }
                   2301:
                   2302: /**********************************************************************
                   2303:  * DMA utility functions
                   2304:  **********************************************************************/
                   2305:
                   2306: /*
                   2307:  * Allocate physical memory from the given physical address range.
                   2308:  * Called by DMA-safe memory allocation methods.
                   2309:  */
                   2310: int
                   2311: _bus_dmamem_alloc_range(t, size, alignment, boundary, segs, nsegs, rsegs,
                   2312:     flags, low, high)
                   2313:        bus_dma_tag_t t;
                   2314:        bus_size_t size, alignment, boundary;
                   2315:        bus_dma_segment_t *segs;
                   2316:        int nsegs;
                   2317:        int *rsegs;
                   2318:        int flags;
                   2319:        vm_offset_t low;
                   2320:        vm_offset_t high;
                   2321: {
                   2322:        vm_offset_t curaddr, lastaddr;
                   2323:        vm_page_t m;
                   2324:        struct pglist mlist;
                   2325:        int curseg, error;
                   2326:
                   2327:        /* Always round the size. */
                   2328:        size = round_page(size);
                   2329:
                   2330:        /*
                   2331:         * Allocate pages from the VM system.
                   2332:         */
                   2333:        TAILQ_INIT(&mlist);
                   2334:        error = vm_page_alloc_memory(size, low, high,
                   2335:            alignment, boundary, &mlist, nsegs, (flags & BUS_DMA_NOWAIT) == 0);
                   2336:        if (error)
                   2337:                return (error);
                   2338:
                   2339:        /*
                   2340:         * Compute the location, size, and number of segments actually
                   2341:         * returned by the VM code.
                   2342:         */
                   2343:        m = mlist.tqh_first;
                   2344:        curseg = 0;
                   2345:        lastaddr = segs[curseg].ds_addr = VM_PAGE_TO_PHYS(m);
                   2346:        segs[curseg].ds_len = PAGE_SIZE;
                   2347:        m = m->pageq.tqe_next;
                   2348:
                   2349:        for (; m != NULL; m = m->pageq.tqe_next) {
                   2350:                curaddr = VM_PAGE_TO_PHYS(m);
                   2351: #ifdef DIAGNOSTIC
                   2352:                if (curaddr < low || curaddr >= high) {
                   2353:                        printf("vm_page_alloc_memory returned non-sensical"
                   2354:                            " address 0x%lx\n", curaddr);
                   2355:                        panic("_bus_dmamem_alloc_range");
                   2356:                }
                   2357: #endif
                   2358:                if (curaddr == (lastaddr + PAGE_SIZE))
                   2359:                        segs[curseg].ds_len += PAGE_SIZE;
                   2360:                else {
                   2361:                        curseg++;
                   2362:                        segs[curseg].ds_addr = curaddr;
                   2363:                        segs[curseg].ds_len = PAGE_SIZE;
                   2364:                }
                   2365:                lastaddr = curaddr;
                   2366:        }
                   2367:
                   2368:        *rsegs = curseg + 1;
                   2369:
1.213     thorpej  2370:        return (0);
1.45      cgd      2371: }

CVSweb <webmaster@jp.NetBSD.org>