Annotation of sys/arch/mvme68k/mvme68k/vm_machdep.c, Revision 1.1
1.1 ! nbrk 1: /* $OpenBSD: vm_machdep.c,v 1.45 2007/06/20 17:29:36 miod Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1988 University of Utah.
! 5: * Copyright (c) 1982, 1986, 1990, 1993
! 6: * The Regents of the University of California. All rights reserved.
! 7: *
! 8: * This code is derived from software contributed to Berkeley by
! 9: * the Systems Programming Group of the University of Utah Computer
! 10: * Science Department.
! 11: *
! 12: * Redistribution and use in source and binary forms, with or without
! 13: * modification, are permitted provided that the following conditions
! 14: * are met:
! 15: * 1. Redistributions of source code must retain the above copyright
! 16: * notice, this list of conditions and the following disclaimer.
! 17: * 2. Redistributions in binary form must reproduce the above copyright
! 18: * notice, this list of conditions and the following disclaimer in the
! 19: * documentation and/or other materials provided with the distribution.
! 20: * 3. Neither the name of the University nor the names of its contributors
! 21: * may be used to endorse or promote products derived from this software
! 22: * without specific prior written permission.
! 23: *
! 24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 34: * SUCH DAMAGE.
! 35: *
! 36: * from: Utah $Hdr: vm_machdep.c 1.21 91/04/06$
! 37: *
! 38: * @(#)vm_machdep.c 8.6 (Berkeley) 1/12/94
! 39: */
! 40:
! 41: #include <sys/param.h>
! 42: #include <sys/systm.h>
! 43: #include <sys/proc.h>
! 44: #include <sys/signalvar.h>
! 45: #include <sys/malloc.h>
! 46: #include <sys/buf.h>
! 47: #include <sys/vnode.h>
! 48: #include <sys/user.h>
! 49: #include <sys/core.h>
! 50: #include <sys/exec.h>
! 51: #include <sys/ptrace.h>
! 52:
! 53: #include <machine/cpu.h>
! 54: #include <machine/pte.h>
! 55: #include <machine/reg.h>
! 56: #include <machine/frame.h>
! 57:
! 58: #include <uvm/uvm_extern.h>
! 59:
! 60: /*
! 61: * Finish a fork operation, with process p2 nearly set up.
! 62: * Copy and update the kernel stack and pcb, making the child
! 63: * ready to run, and marking it so that it can return differently
! 64: * than the parent. Returns 1 in the child process, 0 in the parent.
! 65: * We currently double-map the user area so that the stack is at the same
! 66: * address in each process; in the future we will probably relocate
! 67: * the frame pointers on the stack after copying.
! 68: */
! 69:
! 70: void
! 71: cpu_fork(p1, p2, stack, stacksize, func, arg)
! 72: struct proc *p1, *p2;
! 73: void *stack;
! 74: size_t stacksize;
! 75: void (*func)(void *);
! 76: void *arg;
! 77: {
! 78: struct pcb *pcb = &p2->p_addr->u_pcb;
! 79: struct trapframe *tf;
! 80: struct switchframe *sf;
! 81: extern struct pcb *curpcb;
! 82:
! 83: p2->p_md.md_flags = p1->p_md.md_flags;
! 84:
! 85: /* Copy pcb from proc p1 to p2. */
! 86: if (p1 == curproc) {
! 87: /* Sync the PCB before we copy it. */
! 88: savectx(curpcb);
! 89: }
! 90: #ifdef DIAGNOSTIC
! 91: else if (p1 != &proc0)
! 92: panic("cpu_fork: curproc");
! 93: #endif
! 94: *pcb = p1->p_addr->u_pcb;
! 95:
! 96: /*
! 97: * Copy the trap frame, and arrange for the child to return directly
! 98: * through return_to_user().
! 99: */
! 100: tf = (struct trapframe *)((u_int)p2->p_addr + USPACE) - 1;
! 101: p2->p_md.md_regs = (int *)tf;
! 102: *tf = *(struct trapframe *)p1->p_md.md_regs;
! 103:
! 104: /*
! 105: * If specified, give the child a different stack.
! 106: */
! 107: if (stack != NULL)
! 108: tf->tf_regs[15] = (u_int)stack + stacksize;
! 109:
! 110: sf = (struct switchframe *)tf - 1;
! 111: sf->sf_pc = (u_int)proc_trampoline;
! 112: pcb->pcb_regs[6] = (int)func; /* A2 */
! 113: pcb->pcb_regs[7] = (int)arg; /* A3 */
! 114: pcb->pcb_regs[11] = (int)sf; /* SSP */
! 115: pcb->pcb_ps = PSL_LOWIPL; /* start kthreads at IPL 0 */
! 116: }
! 117:
! 118: /*
! 119: * cpu_exit is called as the last action during exit.
! 120: * We release the address space and machine-dependent resources,
! 121: * including the memory for the user structure and kernel stack.
! 122: * Once finished, we call switch_exit, which switches to a temporary
! 123: * pcb and stack and never returns. We block memory allocation
! 124: * until switch_exit has made things safe again.
! 125: */
! 126: void
! 127: cpu_exit(p)
! 128: struct proc *p;
! 129: {
! 130:
! 131: splhigh();
! 132: switch_exit(p);
! 133: /* NOTREACHED */
! 134: }
! 135:
! 136: /*
! 137: * Dump the machine specific header information at the start of a core dump.
! 138: */
! 139: struct md_core {
! 140: struct reg intreg;
! 141: struct fpreg freg;
! 142: };
! 143: int
! 144: cpu_coredump(p, vp, cred, chdr)
! 145: struct proc *p;
! 146: struct vnode *vp;
! 147: struct ucred *cred;
! 148: struct core *chdr;
! 149: {
! 150: struct md_core md_core;
! 151: struct coreseg cseg;
! 152: int error;
! 153:
! 154: CORE_SETMAGIC(*chdr, COREMAGIC, MID_MACHINE, 0);
! 155: chdr->c_hdrsize = ALIGN(sizeof(*chdr));
! 156: chdr->c_seghdrsize = ALIGN(sizeof(cseg));
! 157: chdr->c_cpusize = sizeof(md_core);
! 158:
! 159: /* Save integer registers. */
! 160: error = process_read_regs(p, &md_core.intreg);
! 161: if (error)
! 162: return error;
! 163:
! 164: if (fputype) {
! 165: /* Save floating point registers. */
! 166: error = process_read_fpregs(p, &md_core.freg);
! 167: if (error)
! 168: return error;
! 169: } else {
! 170: /* Make sure these are clear. */
! 171: bzero((caddr_t)&md_core.freg, sizeof(md_core.freg));
! 172: }
! 173:
! 174: CORE_SETMAGIC(cseg, CORESEGMAGIC, MID_MACHINE, CORE_CPU);
! 175: cseg.c_addr = 0;
! 176: cseg.c_size = chdr->c_cpusize;
! 177:
! 178: error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&cseg, chdr->c_seghdrsize,
! 179: (off_t)chdr->c_hdrsize, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred,
! 180: NULL, p);
! 181: if (error)
! 182: return error;
! 183:
! 184: error = vn_rdwr(UIO_WRITE, vp, (caddr_t)&md_core, sizeof(md_core),
! 185: (off_t)(chdr->c_hdrsize + chdr->c_seghdrsize), UIO_SYSSPACE,
! 186: IO_NODELOCKED|IO_UNIT, cred, NULL, p);
! 187: if (error)
! 188: return error;
! 189:
! 190: chdr->c_nseg++;
! 191: return 0;
! 192: }
! 193:
! 194: /*
! 195: * Convert kernel VA to physical address
! 196: */
! 197: paddr_t
! 198: kvtop(addr)
! 199: vaddr_t addr;
! 200: {
! 201: paddr_t pa;
! 202:
! 203: if (pmap_extract(pmap_kernel(), addr, &pa) == FALSE)
! 204: panic("kvtop: zero page frame");
! 205:
! 206: return (pa);
! 207: }
! 208:
! 209: /*
! 210: * Map an IO request into kernel virtual address space.
! 211: *
! 212: * XXX we allocate KVA space by using kmem_alloc_wait which we know
! 213: * allocates space without backing physical memory. This implementation
! 214: * is a total crock, the multiple mappings of these physical pages should
! 215: * be reflected in the higher-level VM structures to avoid problems.
! 216: */
! 217: void
! 218: vmapbuf(bp, siz)
! 219: struct buf *bp;
! 220: vsize_t siz;
! 221: {
! 222: int npf;
! 223: caddr_t addr;
! 224: struct proc *p;
! 225: int off;
! 226: vaddr_t kva;
! 227: paddr_t pa;
! 228:
! 229: #ifdef DIAGNOSTIC
! 230: if ((bp->b_flags & B_PHYS) == 0)
! 231: panic("vmapbuf");
! 232: #endif
! 233:
! 234: addr = bp->b_saveaddr = bp->b_data;
! 235: off = (int)addr & PGOFSET;
! 236: p = bp->b_proc;
! 237: npf = btoc(round_page(bp->b_bcount + off));
! 238: kva = uvm_km_valloc_wait(phys_map, ctob(npf));
! 239: bp->b_data = (caddr_t)(kva + off);
! 240: while (npf--) {
! 241: if (pmap_extract(vm_map_pmap(&p->p_vmspace->vm_map),
! 242: (vaddr_t)addr, &pa) == FALSE)
! 243: panic("vmapbuf: null page frame");
! 244: pmap_enter(vm_map_pmap(phys_map), kva, trunc_page(pa),
! 245: VM_PROT_READ|VM_PROT_WRITE, VM_PROT_READ|VM_PROT_WRITE|PMAP_WIRED);
! 246: addr += PAGE_SIZE;
! 247: kva += PAGE_SIZE;
! 248: }
! 249: pmap_update(pmap_kernel());
! 250: }
! 251:
! 252: /*
! 253: * Free the io map PTEs associated with this IO operation.
! 254: */
! 255: void
! 256: vunmapbuf(bp, siz)
! 257: struct buf *bp;
! 258: vsize_t siz;
! 259: {
! 260: caddr_t addr;
! 261: int npf;
! 262: vaddr_t kva;
! 263:
! 264: #ifdef DIAGNOSTIC
! 265: if ((bp->b_flags & B_PHYS) == 0)
! 266: panic("vunmapbuf");
! 267: #endif
! 268:
! 269: addr = bp->b_data;
! 270: npf = btoc(round_page(bp->b_bcount + ((int)addr & PGOFSET)));
! 271: kva = (vaddr_t)((int)addr & ~PGOFSET);
! 272: pmap_remove(vm_map_pmap(phys_map), kva, kva + ctob(npf));
! 273: pmap_update(vm_map_pmap(phys_map));
! 274: uvm_km_free_wakeup(phys_map, kva, ctob(npf));
! 275: bp->b_data = bp->b_saveaddr;
! 276: bp->b_saveaddr = NULL;
! 277: }
CVSweb