[BACK]Return to vm_machdep.c CVS log [TXT][DIR] Up to [local] / sys / arch / amd64 / amd64

Annotation of sys/arch/amd64/amd64/vm_machdep.c, Revision 1.1

1.1     ! nbrk        1: /*     $OpenBSD: vm_machdep.c,v 1.10 2007/05/27 20:59:25 miod Exp $    */
        !             2: /*     $NetBSD: vm_machdep.c,v 1.1 2003/04/26 18:39:33 fvdl Exp $      */
        !             3:
        !             4: /*-
        !             5:  * Copyright (c) 1995 Charles M. Hannum.  All rights reserved.
        !             6:  * Copyright (c) 1982, 1986 The Regents of the University of California.
        !             7:  * Copyright (c) 1989, 1990 William Jolitz
        !             8:  * All rights reserved.
        !             9:  *
        !            10:  * This code is derived from software contributed to Berkeley by
        !            11:  * the Systems Programming Group of the University of Utah Computer
        !            12:  * Science Department, and William Jolitz.
        !            13:  *
        !            14:  * Redistribution and use in source and binary forms, with or without
        !            15:  * modification, are permitted provided that the following conditions
        !            16:  * are met:
        !            17:  * 1. Redistributions of source code must retain the above copyright
        !            18:  *    notice, this list of conditions and the following disclaimer.
        !            19:  * 2. Redistributions in binary form must reproduce the above copyright
        !            20:  *    notice, this list of conditions and the following disclaimer in the
        !            21:  *    documentation and/or other materials provided with the distribution.
        !            22:  * 3. Neither the name of the University nor the names of its contributors
        !            23:  *    may be used to endorse or promote products derived from this software
        !            24:  *    without specific prior written permission.
        !            25:  *
        !            26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            36:  * SUCH DAMAGE.
        !            37:  *
        !            38:  *     @(#)vm_machdep.c        7.3 (Berkeley) 5/13/91
        !            39:  */
        !            40:
        !            41: /*
        !            42:  *     Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
        !            43:  */
        !            44:
        !            45: #include <sys/param.h>
        !            46: #include <sys/systm.h>
        !            47: #include <sys/proc.h>
        !            48: #include <sys/malloc.h>
        !            49: #include <sys/vnode.h>
        !            50: #include <sys/buf.h>
        !            51: #include <sys/user.h>
        !            52: #include <sys/core.h>
        !            53: #include <sys/exec.h>
        !            54: #include <sys/ptrace.h>
        !            55: #include <sys/signalvar.h>
        !            56:
        !            57: #include <uvm/uvm_extern.h>
        !            58:
        !            59: #include <machine/cpu.h>
        !            60: #include <machine/gdt.h>
        !            61: #include <machine/reg.h>
        !            62: #include <machine/specialreg.h>
        !            63: #include <machine/fpu.h>
        !            64: #include <machine/mtrr.h>
        !            65:
        !            66: void setredzone(struct proc *);
        !            67:
        !            68: /*
        !            69:  * Finish a fork operation, with process p2 nearly set up.
        !            70:  * Copy and update the kernel stack and pcb, making the child
        !            71:  * ready to run, and marking it so that it can return differently
        !            72:  * than the parent.  Returns 1 in the child process, 0 in the parent.
        !            73:  * We currently double-map the user area so that the stack is at the same
        !            74:  * address in each process; in the future we will probably relocate
        !            75:  * the frame pointers on the stack after copying.
        !            76:  */
        !            77: void
        !            78: cpu_fork(struct proc *p1, struct proc *p2, void *stack, size_t stacksize,
        !            79:     void (*func)(void *), void *arg)
        !            80: {
        !            81:        struct pcb *pcb = &p2->p_addr->u_pcb;
        !            82:        struct trapframe *tf;
        !            83:        struct switchframe *sf;
        !            84:
        !            85:        /*
        !            86:         * If fpuproc != p1, then the fpu h/w state is irrelevant and the
        !            87:         * state had better already be in the pcb.  This is true for forks
        !            88:         * but not for dumps.
        !            89:         *
        !            90:         * If fpuproc == p1, then we have to save the fpu h/w state to
        !            91:         * p1's pcb so that we can copy it.
        !            92:         */
        !            93:        if (p1->p_addr->u_pcb.pcb_fpcpu != NULL)
        !            94:                fpusave_proc(p1, 1);
        !            95:
        !            96:        p2->p_md.md_flags = p1->p_md.md_flags;
        !            97:
        !            98:        /* Copy pcb from proc p1 to p2. */
        !            99:        if (p1 == curproc) {
        !           100:                /* Sync the PCB before we copy it. */
        !           101:                savectx(curpcb);
        !           102:        }
        !           103: #ifdef DIAGNOSTIC
        !           104:        else if (p1 != &proc0)
        !           105:                panic("cpu_fork: curproc");
        !           106: #endif
        !           107:        *pcb = p1->p_addr->u_pcb;
        !           108:
        !           109:        /*
        !           110:         * Preset these so that gdt_compact() doesn't get confused if called
        !           111:         * during the allocations below.
        !           112:         *
        !           113:         * Note: pcb_ldt_sel is handled in the pmap_activate() call when
        !           114:         * we run the new process.
        !           115:         */
        !           116:        p2->p_md.md_tss_sel = GSEL(GNULL_SEL, SEL_KPL);
        !           117:
        !           118:        /*
        !           119:         * Activate the address space.  Note this will refresh pcb_ldt_sel.
        !           120:         */
        !           121:        pmap_activate(p2);
        !           122:
        !           123:        /* Fix up the TSS. */
        !           124:        pcb->pcb_tss.tss_rsp0 = (u_int64_t)p2->p_addr + USPACE - 16;
        !           125:        pcb->pcb_tss.tss_ist[0] = (u_int64_t)p2->p_addr + PAGE_SIZE - 16;
        !           126:        p2->p_md.md_tss_sel = tss_alloc(pcb);
        !           127:
        !           128:        /*
        !           129:         * Copy the trapframe.
        !           130:         */
        !           131:        p2->p_md.md_regs = tf = (struct trapframe *)pcb->pcb_tss.tss_rsp0 - 1;
        !           132:        *tf = *p1->p_md.md_regs;
        !           133:
        !           134:        setredzone(p2);
        !           135:
        !           136:        /*
        !           137:         * If specified, give the child a different stack.
        !           138:         */
        !           139:        if (stack != NULL)
        !           140:                tf->tf_rsp = (u_int64_t)stack + stacksize;
        !           141:
        !           142:        sf = (struct switchframe *)tf - 1;
        !           143:        sf->sf_r12 = (u_int64_t)func;
        !           144:        sf->sf_r13 = (u_int64_t)arg;
        !           145:        /* XXX fork of init(8) returns via proc_trampoline() */
        !           146:        if (p2->p_pid == 1)
        !           147:                sf->sf_rip = (u_int64_t)proc_trampoline;
        !           148:        else
        !           149:                sf->sf_rip = (u_int64_t)child_trampoline;
        !           150:        pcb->pcb_rsp = (u_int64_t)sf;
        !           151:        pcb->pcb_rbp = 0;
        !           152: }
        !           153:
        !           154: /*
        !           155:  * cpu_exit is called as the last action during exit.
        !           156:  *
        !           157:  * We clean up a little and then call switch_exit() with the old proc as an
        !           158:  * argument.  switch_exit() first switches to proc0's context, and finally
        !           159:  * jumps into switch() to wait for another process to wake up.
        !           160:  */
        !           161: void
        !           162: cpu_exit(struct proc *p)
        !           163: {
        !           164:
        !           165:        /* If we were using the FPU, forget about it. */
        !           166:        if (p->p_addr->u_pcb.pcb_fpcpu != NULL)
        !           167:                fpusave_proc(p, 0);
        !           168:
        !           169:        if (p->p_md.md_flags & MDP_USEDMTRR)
        !           170:                mtrr_clean(p);
        !           171:
        !           172:        /*
        !           173:         * No need to do user LDT cleanup here; it's handled in
        !           174:         * pmap_destroy().
        !           175:         */
        !           176:
        !           177:        switch_exit(p, exit2);
        !           178: }
        !           179:
        !           180: /*
        !           181:  * cpu_wait is called from reaper() to let machine-dependent
        !           182:  * code free machine-dependent resources that couldn't be freed
        !           183:  * in cpu_exit().
        !           184:  */
        !           185: void
        !           186: cpu_wait(struct proc *p)
        !           187: {
        !           188:        /* Nuke the TSS. */
        !           189:        tss_free(p->p_md.md_tss_sel);
        !           190: }
        !           191:
        !           192: /*
        !           193:  * Dump the machine specific segment at the start of a core dump.
        !           194:  */
        !           195: struct md_core {
        !           196:        struct reg intreg;
        !           197:        struct fpreg freg;
        !           198: };
        !           199:
        !           200: int
        !           201: cpu_coredump(struct proc *p, struct vnode *vp, struct ucred *cred,
        !           202:     struct core *chdr)
        !           203: {
        !           204:        struct md_core md_core;
        !           205:        struct coreseg cseg;
        !           206:        int error;
        !           207:
        !           208:        CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0);
        !           209:        chdr->c_hdrsize = ALIGN(sizeof(*chdr));
        !           210:        chdr->c_seghdrsize = ALIGN(sizeof(cseg));
        !           211:        chdr->c_cpusize = sizeof(md_core);
        !           212:
        !           213:        /* Save integer registers. */
        !           214:        error = process_read_regs(p, &md_core.intreg);
        !           215:        if (error)
        !           216:                return error;
        !           217:
        !           218:        /* Save floating point registers. */
        !           219:        error = process_read_fpregs(p, &md_core.freg);
        !           220:        if (error)
        !           221:                return error;
        !           222:
        !           223:        CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
        !           224:        cseg.c_addr = 0;
        !           225:        cseg.c_size = chdr->c_cpusize;
        !           226:
        !           227:        error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&cseg, chdr->c_seghdrsize,
        !           228:            (off_t)chdr->c_hdrsize, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred,
        !           229:            NULL, p);
        !           230:        if (error)
        !           231:                return error;
        !           232:
        !           233:        error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&md_core, sizeof(md_core),
        !           234:            (off_t)(chdr->c_hdrsize + chdr->c_seghdrsize), UIO_SYSSPACE,
        !           235:            IO_NODELOCKED|IO_UNIT, cred, NULL, p);
        !           236:        if (error)
        !           237:                return error;
        !           238:
        !           239:        chdr->c_nseg++;
        !           240:        return 0;
        !           241: }
        !           242:
        !           243: /*
        !           244:  * Set a red zone in the kernel stack after the u. area.
        !           245:  */
        !           246: void
        !           247: setredzone(struct proc *p)
        !           248: {
        !           249: #if 0
        !           250:        pmap_remove(pmap_kernel(), (vaddr_t)p->p_addr + PAGE_SIZE,
        !           251:            (vaddr_t)p->p_addr + 2 * PAGE_SIZE);
        !           252:        pmap_update(pmap_kernel());
        !           253: #endif
        !           254: }
        !           255:
        !           256: /*
        !           257:  * Map a user I/O request into kernel virtual address space.
        !           258:  * Note: the pages are already locked by uvm_vslock(), so we
        !           259:  * do not need to pass an access_type to pmap_enter().
        !           260:  */
        !           261: void
        !           262: vmapbuf(struct buf *bp, vsize_t len)
        !           263: {
        !           264:        vaddr_t faddr, taddr, off;
        !           265:        paddr_t fpa;
        !           266:
        !           267:        if ((bp->b_flags & B_PHYS) == 0)
        !           268:                panic("vmapbuf");
        !           269:        faddr = trunc_page((vaddr_t)bp->b_saveaddr = bp->b_data);
        !           270:        off = (vaddr_t)bp->b_data - faddr;
        !           271:        len = round_page(off + len);
        !           272:        taddr= uvm_km_valloc_wait(phys_map, len);
        !           273:        bp->b_data = (caddr_t)(taddr + off);
        !           274:        /*
        !           275:         * The region is locked, so we expect that pmap_pte() will return
        !           276:         * non-NULL.
        !           277:         * XXX: unwise to expect this in a multithreaded environment.
        !           278:         * anything can happen to a pmap between the time we lock a
        !           279:         * region, release the pmap lock, and then relock it for
        !           280:         * the pmap_extract().
        !           281:         *
        !           282:         * no need to flush TLB since we expect nothing to be mapped
        !           283:         * where we we just allocated (TLB will be flushed when our
        !           284:         * mapping is removed).
        !           285:         */
        !           286:        while (len) {
        !           287:                (void) pmap_extract(vm_map_pmap(&bp->b_proc->p_vmspace->vm_map),
        !           288:                    faddr, &fpa);
        !           289:                pmap_kenter_pa(taddr, fpa, VM_PROT_READ|VM_PROT_WRITE);
        !           290:                faddr += PAGE_SIZE;
        !           291:                taddr += PAGE_SIZE;
        !           292:                len -= PAGE_SIZE;
        !           293:        }
        !           294: }
        !           295:
        !           296: /*
        !           297:  * Unmap a previously-mapped user I/O request.
        !           298:  */
        !           299: void
        !           300: vunmapbuf(struct buf *bp, vsize_t len)
        !           301: {
        !           302:        vaddr_t addr, off;
        !           303:
        !           304:        if ((bp->b_flags & B_PHYS) == 0)
        !           305:                panic("vunmapbuf");
        !           306:        addr = trunc_page((vaddr_t)bp->b_data);
        !           307:        off = (vaddr_t)bp->b_data - addr;
        !           308:        len = round_page(off + len);
        !           309:        pmap_kremove(addr, len);
        !           310:        pmap_update(pmap_kernel());
        !           311:        uvm_km_free_wakeup(phys_map, addr, len);
        !           312:        bp->b_data = bp->b_saveaddr;
        !           313:        bp->b_saveaddr = 0;
        !           314: }

CVSweb