vm_machdep.c revision 105611
136865Sdfr/*- 236865Sdfr * Copyright (c) 1982, 1986 The Regents of the University of California. 336865Sdfr * Copyright (c) 1989, 1990 William Jolitz 436865Sdfr * Copyright (c) 1994 John Dyson 536865Sdfr * All rights reserved. 636865Sdfr * 736865Sdfr * This code is derived from software contributed to Berkeley by 836865Sdfr * the Systems Programming Group of the University of Utah Computer 936865Sdfr * Science Department, and William Jolitz. 1036865Sdfr * 1136865Sdfr * Redistribution and use in source and binary forms, with or without 1236865Sdfr * modification, are permitted provided that the following conditions 1336865Sdfr * are met: 1436865Sdfr * 1. Redistributions of source code must retain the above copyright 1536865Sdfr * notice, this list of conditions and the following disclaimer. 1636865Sdfr * 2. Redistributions in binary form must reproduce the above copyright 1736865Sdfr * notice, this list of conditions and the following disclaimer in the 1836865Sdfr * documentation and/or other materials provided with the distribution. 1936865Sdfr * 3. All advertising materials mentioning features or use of this software 2036865Sdfr * must display the following acknowledgement: 2136865Sdfr * This product includes software developed by the University of 2236865Sdfr * California, Berkeley and its contributors. 2336865Sdfr * 4. Neither the name of the University nor the names of its contributors 2436865Sdfr * may be used to endorse or promote products derived from this software 2536865Sdfr * without specific prior written permission. 2636865Sdfr * 2736865Sdfr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2836865Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2936865Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3036865Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3136865Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3236865Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3336865Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3436865Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3536865Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3636865Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3736865Sdfr * SUCH DAMAGE. 3836865Sdfr * 3936865Sdfr * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 4036865Sdfr * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 4150477Speter * $FreeBSD: head/sys/powerpc/aim/vm_machdep.c 105611 2002-10-21 05:27:41Z grehan $ 4236865Sdfr */ 4336865Sdfr/* 4436865Sdfr * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. 4536865Sdfr * All rights reserved. 4636865Sdfr * 4736865Sdfr * Author: Chris G. Demetriou 4836865Sdfr * 4936865Sdfr * Permission to use, copy, modify and distribute this software and 5036865Sdfr * its documentation is hereby granted, provided that both the copyright 5136865Sdfr * notice and this permission notice appear in all copies of the 5236865Sdfr * software, derivative works or modified versions, and any portions 5336865Sdfr * thereof, and that both notices appear in supporting documentation. 5436865Sdfr * 5536865Sdfr * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 5636865Sdfr * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 5736865Sdfr * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 5836865Sdfr * 5936865Sdfr * Carnegie Mellon requests users of this software to return to 6036865Sdfr * 6136865Sdfr * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 6236865Sdfr * School of Computer Science 6336865Sdfr * Carnegie Mellon University 6436865Sdfr * Pittsburgh PA 15213-3890 6536865Sdfr * 6636865Sdfr * any improvements or extensions that they make and grant Carnegie the 6736865Sdfr * rights to redistribute these changes. 6836865Sdfr */ 6936865Sdfr 7036865Sdfr#include <sys/param.h> 7136865Sdfr#include <sys/systm.h> 7236865Sdfr#include <sys/proc.h> 7336865Sdfr#include <sys/malloc.h> 7460041Sphk#include <sys/bio.h> 7536865Sdfr#include <sys/buf.h> 7685201Smp#include <sys/ktr.h> 7785201Smp#include <sys/lock.h> 7867365Sjhb#include <sys/mutex.h> 7936865Sdfr#include <sys/vnode.h> 8036865Sdfr#include <sys/vmmeter.h> 8136865Sdfr#include <sys/kernel.h> 8236865Sdfr#include <sys/sysctl.h> 8354207Speter#include <sys/unistd.h> 8436865Sdfr 8536865Sdfr#include <machine/clock.h> 8636865Sdfr#include <machine/cpu.h> 8741499Sdfr#include <machine/fpu.h> 8897397Sbenno#include <machine/frame.h> 8936865Sdfr#include <machine/md_var.h> 9036865Sdfr 9178342Sbenno#include <dev/ofw/openfirm.h> 9278342Sbenno 9336865Sdfr#include <vm/vm.h> 9436865Sdfr#include <vm/vm_param.h> 9536865Sdfr#include <vm/vm_kern.h> 9636865Sdfr#include <vm/vm_page.h> 9736865Sdfr#include <vm/vm_map.h> 9836865Sdfr#include <vm/vm_extern.h> 9936865Sdfr 10036865Sdfr#include <sys/user.h> 10136865Sdfr 10236865Sdfr/* 10336865Sdfr * quick version of vm_fault 10436865Sdfr */ 10551474Sdillonint 10636865Sdfrvm_fault_quick(v, prot) 10736865Sdfr caddr_t v; 10836865Sdfr int prot; 10936865Sdfr{ 11051474Sdillon int r; 11136865Sdfr if (prot & VM_PROT_WRITE) 11251474Sdillon r = subyte(v, fubyte(v)); 11336865Sdfr else 11451474Sdillon r = fubyte(v); 11551474Sdillon return(r); 11636865Sdfr} 11736865Sdfr 11836865Sdfr/* 11936865Sdfr * Finish a fork operation, with process p2 nearly set up. 12036865Sdfr * Copy and update the pcb, set up the stack so that the child 12136865Sdfr * ready to run and return to user mode. 12236865Sdfr */ 12336865Sdfrvoid 12490361Sjuliancpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags) 12536865Sdfr{ 12684977Sbenno struct proc *p1; 12784977Sbenno struct trapframe *tf; 12884977Sbenno struct callframe *cf; 12990643Sbenno struct pcb *pcb; 13084977Sbenno 13191467Sbenno KASSERT(td1 == curthread || td1 == &thread0, 13290643Sbenno ("cpu_fork: p1 not curproc and not proc0")); 13384977Sbenno CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags); 13484977Sbenno 13584977Sbenno if ((flags & RFPROC) == 0) 13684977Sbenno return; 13784977Sbenno 13884977Sbenno p1 = td1->td_proc; 13984977Sbenno 14091467Sbenno pcb = (struct pcb *)((td2->td_kstack + KSTACK_PAGES * PAGE_SIZE - 14191467Sbenno sizeof(struct pcb)) & ~0x2fU); 14290643Sbenno td2->td_pcb = pcb; 14384977Sbenno 14490643Sbenno /* Copy the pcb */ 14590643Sbenno bcopy(td1->td_pcb, pcb, sizeof(struct pcb)); 14684977Sbenno 14784977Sbenno /* 14890643Sbenno * Create a fresh stack for the new process. 14984977Sbenno * Copy the trap frame for the return to user mode as if from a 15084977Sbenno * syscall. This copies most of the user mode register values. 15184977Sbenno */ 15290643Sbenno tf = (struct trapframe *)pcb - 1; 15390643Sbenno bcopy(td1->td_frame, tf, sizeof(*tf)); 15484977Sbenno 15597397Sbenno /* Set up trap frame. */ 15697397Sbenno tf->fixreg[FIRSTARG] = 0; 15797397Sbenno tf->fixreg[FIRSTARG + 1] = 0; 15897397Sbenno tf->cr &= ~0x10000000; 15990643Sbenno 16090643Sbenno td2->td_frame = tf; 16190643Sbenno 16290643Sbenno cf = (struct callframe *)tf - 1; 16391467Sbenno cf->cf_func = (register_t)fork_return; 16491467Sbenno cf->cf_arg0 = (register_t)td2; 16591467Sbenno cf->cf_arg1 = (register_t)tf; 16684977Sbenno 16791467Sbenno pcb->pcb_sp = (register_t)cf; 16891467Sbenno pcb->pcb_lr = (register_t)fork_trampoline; 169105611Sgrehan pcb->pcb_usr = kernel_pmap->pm_sr[USER_SR]; 17084977Sbenno 17190643Sbenno /* 17290643Sbenno * Now cpu_switch() can schedule the new process. 17390643Sbenno */ 17436865Sdfr} 17536865Sdfr 17636865Sdfr/* 17736865Sdfr * Intercept the return address from a freshly forked process that has NOT 17836865Sdfr * been scheduled yet. 17936865Sdfr * 18036865Sdfr * This is needed to make kernel threads stay in kernel mode. 18136865Sdfr */ 18236865Sdfrvoid 18383682Smpcpu_set_fork_handler(td, func, arg) 18483682Smp struct thread *td; 18592842Salfred void (*func)(void *); 18648391Speter void *arg; 18736865Sdfr{ 18884977Sbenno struct callframe *cf; 18984977Sbenno 19084977Sbenno CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x", 19184977Sbenno (u_int)td, (u_int)func, (u_int)arg); 19284977Sbenno 19391467Sbenno cf = (struct callframe *)td->td_pcb->pcb_sp; 19484977Sbenno 19591467Sbenno cf->cf_func = (register_t)func; 19691467Sbenno cf->cf_arg0 = (register_t)arg; 19736865Sdfr} 19836865Sdfr 19936865Sdfr/* 20036865Sdfr * cpu_exit is called as the last action during exit. 20136865Sdfr * We release the address space of the process, block interrupts, 20236865Sdfr * and call switch_exit. switch_exit switches to proc0's PCB and stack, 20336865Sdfr * then jumps into the middle of cpu_switch, as if it were switching 20436865Sdfr * from proc0. 20536865Sdfr */ 20636865Sdfrvoid 20783682Smpcpu_exit(td) 20883682Smp register struct thread *td; 20936865Sdfr{ 21036865Sdfr} 21136865Sdfr 21236865Sdfrvoid 21398765Sjakecpu_sched_exit(td) 21498765Sjake register struct thread *td; 21598765Sjake{ 21698765Sjake} 21798765Sjake 21898765Sjakevoid 21983682Smpcpu_wait(td) 22083682Smp struct proc *td; 22136865Sdfr{ 22283276Speter} 22336865Sdfr 22483276Speter/* Temporary helper */ 22583276Spetervoid 22683276Spetercpu_throw(void) 22783276Speter{ 22883276Speter 22983276Speter cpu_switch(); 23083276Speter panic("cpu_throw() didn't"); 23136865Sdfr} 23236865Sdfr 23336865Sdfr/* 23436865Sdfr * Map an IO request into kernel virtual address space. 23536865Sdfr * 23636865Sdfr * All requests are (re)mapped into kernel VA space. 23736865Sdfr * Notice that we use b_bufsize for the size of the buffer 23836865Sdfr * to be mapped. b_bcount might be modified by the driver. 23936865Sdfr */ 24036865Sdfrvoid 241103609Sgrehanvmapbuf(struct buf *bp) 24236865Sdfr{ 243103609Sgrehan caddr_t addr, kva; 24436865Sdfr vm_offset_t pa; 245103609Sgrehan int pidx; 246103609Sgrehan struct vm_page *m; 247103609Sgrehan pmap_t pmap; 24836865Sdfr 24979224Sdillon GIANT_REQUIRED; 25079224Sdillon 25136865Sdfr if ((bp->b_flags & B_PHYS) == 0) 25236865Sdfr panic("vmapbuf"); 25336865Sdfr 254103609Sgrehan pmap = &curproc->p_vmspace->vm_pmap; 255103609Sgrehan for (addr = (caddr_t)trunc_page(bp->b_data), pidx = 0; 25636865Sdfr addr < bp->b_data + bp->b_bufsize; 257103609Sgrehan addr += PAGE_SIZE, pidx++) { 25836865Sdfr /* 25936865Sdfr * Do the vm_fault if needed; do the copy-on-write thing 26036865Sdfr * when reading stuff off device into memory. 26136865Sdfr */ 26284381Smjacob vm_fault_quick((addr >= bp->b_data) ? addr : bp->b_data, 26358345Sphk (bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 264103609Sgrehan pa = trunc_page(pmap_extract(pmap, (vm_offset_t) addr)); 26536865Sdfr if (pa == 0) 26636865Sdfr panic("vmapbuf: page not present"); 267103609Sgrehan m = PHYS_TO_VM_PAGE(pa); 268103609Sgrehan vm_page_hold(m); 269103609Sgrehan bp->b_pages[pidx] = m; 27036865Sdfr } 271103609Sgrehan if (pidx > btoc(MAXPHYS)) 272103609Sgrehan panic("vmapbuf: mapped more than MAXPHYS"); 273103609Sgrehan pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx); 27436865Sdfr 27536865Sdfr kva = bp->b_saveaddr; 276103609Sgrehan bp->b_npages = pidx; 27736865Sdfr bp->b_saveaddr = bp->b_data; 27836865Sdfr bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 27936865Sdfr} 28036865Sdfr 28136865Sdfr/* 28236865Sdfr * Free the io map PTEs associated with this IO operation. 28336865Sdfr * We also invalidate the TLB entries and restore the original b_addr. 28436865Sdfr */ 28536865Sdfrvoid 286103609Sgrehanvunmapbuf(struct buf *bp) 28736865Sdfr{ 288103609Sgrehan int pidx; 289103609Sgrehan int npages; 29036865Sdfr 29179224Sdillon GIANT_REQUIRED; 29279224Sdillon 29336865Sdfr if ((bp->b_flags & B_PHYS) == 0) 29436865Sdfr panic("vunmapbuf"); 29536865Sdfr 296103609Sgrehan npages = bp->b_npages; 297103609Sgrehan pmap_qremove(trunc_page((vm_offset_t)bp->b_data), 298103609Sgrehan npages); 299103609Sgrehan for (pidx = 0; pidx < npages; pidx++) 300103609Sgrehan vm_page_unhold(bp->b_pages[pidx]); 30136865Sdfr 30236865Sdfr bp->b_data = bp->b_saveaddr; 30336865Sdfr} 30436865Sdfr 30536865Sdfr/* 30665557Sjasone * Reset back to firmware. 30736865Sdfr */ 30836865Sdfrvoid 30936865Sdfrcpu_reset() 31036865Sdfr{ 31178342Sbenno OF_exit(); 31236865Sdfr} 31336865Sdfr 31436865Sdfr/* 31536865Sdfr * Software interrupt handler for queued VM system processing. 31636865Sdfr */ 31736865Sdfrvoid 31867551Sjhbswi_vm(void *dummy) 31936865Sdfr{ 32078342Sbenno#if 0 /* XXX: Don't have busdma stuff yet */ 32136865Sdfr if (busdma_swi_pending != 0) 32236865Sdfr busdma_swi(); 32378342Sbenno#endif 32436865Sdfr} 32536865Sdfr 32636865Sdfr/* 32736865Sdfr * Tell whether this address is in some physical memory region. 32836865Sdfr * Currently used by the kernel coredump code in order to avoid 32936865Sdfr * dumping the ``ISA memory hole'' which could cause indefinite hangs, 33036865Sdfr * or other unpredictable behaviour. 33136865Sdfr */ 33236865Sdfr 33336865Sdfr 33436865Sdfrint 33536865Sdfris_physical_memory(addr) 33636865Sdfr vm_offset_t addr; 33736865Sdfr{ 33836865Sdfr /* 33936865Sdfr * stuff other tests for known memory-mapped devices (PCI?) 34036865Sdfr * here 34136865Sdfr */ 34236865Sdfr 34336865Sdfr return 1; 34436865Sdfr} 34599659Sbenno 34699659Sbenno/* 34799659Sbenno * KSE functions 34899659Sbenno */ 34999659Sbennovoid 35099659Sbennocpu_thread_exit(struct thread *td) 35199659Sbenno{ 35299659Sbenno 35399659Sbenno return; 35499659Sbenno} 35599659Sbenno 35699659Sbennovoid 35799659Sbennocpu_thread_setup(struct thread *td) 35899659Sbenno{ 35999659Sbenno 36099659Sbenno return; 36199659Sbenno} 36299659Sbenno 36399659Sbennovoid 36499659Sbennocpu_set_upcall(struct thread *td, void *pcb) 36599659Sbenno{ 36699659Sbenno 36799659Sbenno return; 36899659Sbenno} 36999659Sbenno 37099659Sbennovoid 371103609Sgrehancpu_set_upcall_kse(struct thread *td, struct kse *ke) 37299659Sbenno{ 37399659Sbenno 37499659Sbenno return; 37599659Sbenno} 376