vm_machdep.c revision 83682
1/*- 2 * Copyright (c) 1982, 1986 The Regents of the University of California. 3 * Copyright (c) 1989, 1990 William Jolitz 4 * Copyright (c) 1994 John Dyson 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * the Systems Programming Group of the University of Utah Computer 9 * Science Department, and William Jolitz. 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 University of 22 * California, Berkeley and its contributors. 23 * 4. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 * 39 * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 40 * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 41 * $FreeBSD: head/sys/powerpc/aim/vm_machdep.c 83682 2001-09-20 00:47:17Z mp $ 42 */ 43/* 44 * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. 45 * All rights reserved. 46 * 47 * Author: Chris G. Demetriou 48 * 49 * Permission to use, copy, modify and distribute this software and 50 * its documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 */ 69 70#include <sys/param.h> 71#include <sys/systm.h> 72#include <sys/proc.h> 73#include <sys/malloc.h> 74#include <sys/bio.h> 75#include <sys/buf.h> 76#include <sys/mutex.h> 77#include <sys/vnode.h> 78#include <sys/vmmeter.h> 79#include <sys/kernel.h> 80#include <sys/sysctl.h> 81#include <sys/unistd.h> 82 83#include <machine/clock.h> 84#include <machine/cpu.h> 85#include <machine/fpu.h> 86#include <machine/md_var.h> 87 88#include <dev/ofw/openfirm.h> 89 90#include <vm/vm.h> 91#include <vm/vm_param.h> 92#include <sys/lock.h> 93#include <vm/vm_kern.h> 94#include <vm/vm_page.h> 95#include <vm/vm_map.h> 96#include <vm/vm_extern.h> 97 98#include <sys/user.h> 99 100/* 101 * quick version of vm_fault 102 */ 103int 104vm_fault_quick(v, prot) 105 caddr_t v; 106 int prot; 107{ 108 int r; 109 if (prot & VM_PROT_WRITE) 110 r = subyte(v, fubyte(v)); 111 else 112 r = fubyte(v); 113 return(r); 114} 115 116/* 117 * Finish a fork operation, with process p2 nearly set up. 118 * Copy and update the pcb, set up the stack so that the child 119 * ready to run and return to user mode. 120 */ 121void 122cpu_fork(td1, p2, flags) 123 register struct thread *td1; 124 register struct proc *p2; 125 int flags; 126{ 127 /* XXX: coming soon... */ 128} 129 130/* 131 * Intercept the return address from a freshly forked process that has NOT 132 * been scheduled yet. 133 * 134 * This is needed to make kernel threads stay in kernel mode. 135 */ 136void 137cpu_set_fork_handler(td, func, arg) 138 struct thread *td; 139 void (*func) __P((void *)); 140 void *arg; 141{ 142 /* 143 * Note that the trap frame follows the args, so the function 144 * is really called like this: func(arg, frame); 145 */ 146#if 0 /* XXX */ 147 td->p_addr->u_pcb.pcb_context[0] = (u_long) func; 148 td->p_addr->u_pcb.pcb_context[2] = (u_long) arg; 149#endif 150} 151 152/* 153 * cpu_exit is called as the last action during exit. 154 * We release the address space of the process, block interrupts, 155 * and call switch_exit. switch_exit switches to proc0's PCB and stack, 156 * then jumps into the middle of cpu_switch, as if it were switching 157 * from proc0. 158 */ 159void 160cpu_exit(td) 161 register struct thread *td; 162{ 163} 164 165void 166cpu_wait(td) 167 struct proc *td; 168{ 169} 170 171/* Temporary helper */ 172void 173cpu_throw(void) 174{ 175 176 cpu_switch(); 177 panic("cpu_throw() didn't"); 178} 179 180/* 181 * Dump the machine specific header information at the start of a core dump. 182 */ 183int 184cpu_coredump(td, vp, cred) 185 struct thread *td; 186 struct vnode *vp; 187 struct ucred *cred; 188{ 189 struct proc *p = td->td_proc; 190 191 return (vn_rdwr(UIO_WRITE, vp, (caddr_t)p->p_uarea, ctob(UAREA_PAGES), 192 (off_t)0, UIO_SYSSPACE, IO_UNIT, cred, (int *)NULL, p)); 193} 194 195/* 196 * Map an IO request into kernel virtual address space. 197 * 198 * All requests are (re)mapped into kernel VA space. 199 * Notice that we use b_bufsize for the size of the buffer 200 * to be mapped. b_bcount might be modified by the driver. 201 */ 202void 203vmapbuf(bp) 204 register struct buf *bp; 205{ 206 register caddr_t addr, v, kva; 207 vm_offset_t pa; 208 209 GIANT_REQUIRED; 210 211 if ((bp->b_flags & B_PHYS) == 0) 212 panic("vmapbuf"); 213 214 for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data); 215 addr < bp->b_data + bp->b_bufsize; 216 addr += PAGE_SIZE, v += PAGE_SIZE) { 217 /* 218 * Do the vm_fault if needed; do the copy-on-write thing 219 * when reading stuff off device into memory. 220 */ 221 vm_fault_quick(addr, 222 (bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 223 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 224 if (pa == 0) 225 panic("vmapbuf: page not present"); 226 vm_page_hold(PHYS_TO_VM_PAGE(pa)); 227 pmap_kenter((vm_offset_t) v, pa); 228 } 229 230 kva = bp->b_saveaddr; 231 bp->b_saveaddr = bp->b_data; 232 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 233} 234 235/* 236 * Free the io map PTEs associated with this IO operation. 237 * We also invalidate the TLB entries and restore the original b_addr. 238 */ 239void 240vunmapbuf(bp) 241 register struct buf *bp; 242{ 243 register caddr_t addr; 244 vm_offset_t pa; 245 246 GIANT_REQUIRED; 247 248 if ((bp->b_flags & B_PHYS) == 0) 249 panic("vunmapbuf"); 250 251 for (addr = (caddr_t)trunc_page(bp->b_data); 252 addr < bp->b_data + bp->b_bufsize; 253 addr += PAGE_SIZE) { 254 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 255 pmap_kremove((vm_offset_t) addr); 256 vm_page_unhold(PHYS_TO_VM_PAGE(pa)); 257 } 258 259 bp->b_data = bp->b_saveaddr; 260} 261 262/* 263 * Reset back to firmware. 264 */ 265void 266cpu_reset() 267{ 268 OF_exit(); 269} 270 271int 272grow_stack(p, sp) 273 struct proc *p; 274 size_t sp; 275{ 276 int rv; 277 278 rv = vm_map_growstack (p, sp); 279 if (rv != KERN_SUCCESS) 280 return (0); 281 282 return (1); 283} 284 285/* 286 * Software interrupt handler for queued VM system processing. 287 */ 288void 289swi_vm(void *dummy) 290{ 291#if 0 /* XXX: Don't have busdma stuff yet */ 292 if (busdma_swi_pending != 0) 293 busdma_swi(); 294#endif 295} 296 297/* 298 * Tell whether this address is in some physical memory region. 299 * Currently used by the kernel coredump code in order to avoid 300 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 301 * or other unpredictable behaviour. 302 */ 303 304 305int 306is_physical_memory(addr) 307 vm_offset_t addr; 308{ 309 /* 310 * stuff other tests for known memory-mapped devices (PCI?) 311 * here 312 */ 313 314 return 1; 315} 316