vm_machdep.c revision 90361
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 90361 2002-02-07 20:58:47Z julian $ 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/ktr.h> 77#include <sys/lock.h> 78#include <sys/mutex.h> 79#include <sys/vnode.h> 80#include <sys/vmmeter.h> 81#include <sys/kernel.h> 82#include <sys/sysctl.h> 83#include <sys/unistd.h> 84 85#include <machine/clock.h> 86#include <machine/cpu.h> 87#include <machine/fpu.h> 88#include <machine/md_var.h> 89 90#include <dev/ofw/openfirm.h> 91 92#include <vm/vm.h> 93#include <vm/vm_param.h> 94#include <vm/vm_kern.h> 95#include <vm/vm_page.h> 96#include <vm/vm_map.h> 97#include <vm/vm_extern.h> 98 99#include <sys/user.h> 100 101/* 102 * quick version of vm_fault 103 */ 104int 105vm_fault_quick(v, prot) 106 caddr_t v; 107 int prot; 108{ 109 int r; 110 if (prot & VM_PROT_WRITE) 111 r = subyte(v, fubyte(v)); 112 else 113 r = fubyte(v); 114 return(r); 115} 116 117/* 118 * Finish a fork operation, with process p2 nearly set up. 119 * Copy and update the pcb, set up the stack so that the child 120 * ready to run and return to user mode. 121 */ 122void 123cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags) 124{ 125 struct proc *p1; 126 struct trapframe *tf; 127 struct callframe *cf; 128 struct switchframe *sf; 129 caddr_t stktop1, stktop2; 130 struct pcb *pcb2; 131 132 CTR3(KTR_PROC, "cpu_fork: called td1=%08x p2=%08x flags=%x", (u_int)td1, (u_int)p2, flags); 133 134 if ((flags & RFPROC) == 0) 135 return; 136 137 p1 = td1->td_proc; 138 139 /* Point the pcb to the top of the stack */ 140 pcb2 = (struct pcb *)(td2->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1; 141 td2->td_pcb = pcb2; 142 143 /* Copy p1's pcb. */ 144 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 145 146 /* 147 * Create a new fresh stack for the new process. 148 * Copy the trap frame for the return to user mode as if from a 149 * syscall. This copies most of the user mode register values. 150 */ 151 td2->td_frame = (struct trapframe *)td2->td_pcb - 1; 152 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 153 154 stktop2 = (caddr_t)td2->td_frame; 155 156 cf = (struct callframe *)stktop2; 157 cf->lr = (int)fork_trampoline; 158 159 stktop2 -= 16; 160 cf = (struct callframe *)stktop2; 161 cf->r31 = (register_t)fork_return; 162 cf->r30 = (register_t)td2; 163 164 stktop2 -= roundup(sizeof *sf, 16); 165 sf = (struct switchframe *)stktop2; 166 bzero((void *)sf, sizeof *sf); 167 sf->sp = (int)cf; 168 sf->user_sr = kernel_pmap->pm_sr[USER_SR]; 169 170 pcb2->pcb_sp = (int)stktop2; 171 pcb2->pcb_spl = 0; 172} 173 174/* 175 * Intercept the return address from a freshly forked process that has NOT 176 * been scheduled yet. 177 * 178 * This is needed to make kernel threads stay in kernel mode. 179 */ 180void 181cpu_set_fork_handler(td, func, arg) 182 struct thread *td; 183 void (*func) __P((void *)); 184 void *arg; 185{ 186 struct switchframe *sf; 187 struct callframe *cf; 188 189 CTR3(KTR_PROC, "cpu_set_fork_handler: called with td=%08x func=%08x arg=%08x", 190 (u_int)td, (u_int)func, (u_int)arg); 191 192 sf = (struct switchframe *)td->td_pcb->pcb_sp; 193 cf = (struct callframe *)sf->sp; 194 195 cf->r31 = (register_t)func; 196 cf->r30 = (register_t)arg; 197} 198 199/* 200 * cpu_exit is called as the last action during exit. 201 * We release the address space of the process, block interrupts, 202 * and call switch_exit. switch_exit switches to proc0's PCB and stack, 203 * then jumps into the middle of cpu_switch, as if it were switching 204 * from proc0. 205 */ 206void 207cpu_exit(td) 208 register struct thread *td; 209{ 210} 211 212void 213cpu_wait(td) 214 struct proc *td; 215{ 216} 217 218/* Temporary helper */ 219void 220cpu_throw(void) 221{ 222 223 cpu_switch(); 224 panic("cpu_throw() didn't"); 225} 226 227/* 228 * Dump the machine specific header information at the start of a core dump. 229 */ 230int 231cpu_coredump(td, vp, cred) 232 struct thread *td; 233 struct vnode *vp; 234 struct ucred *cred; 235{ 236 struct proc *p = td->td_proc; 237 238 return (vn_rdwr(UIO_WRITE, vp, (caddr_t)p->p_uarea, ctob(UAREA_PAGES), 239 (off_t)0, UIO_SYSSPACE, IO_UNIT, cred, (int *)NULL, p)); 240} 241 242/* 243 * Map an IO request into kernel virtual address space. 244 * 245 * All requests are (re)mapped into kernel VA space. 246 * Notice that we use b_bufsize for the size of the buffer 247 * to be mapped. b_bcount might be modified by the driver. 248 */ 249void 250vmapbuf(bp) 251 register struct buf *bp; 252{ 253 register caddr_t addr, v, kva; 254 vm_offset_t pa; 255 256 GIANT_REQUIRED; 257 258 if ((bp->b_flags & B_PHYS) == 0) 259 panic("vmapbuf"); 260 261 for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data); 262 addr < bp->b_data + bp->b_bufsize; 263 addr += PAGE_SIZE, v += PAGE_SIZE) { 264 /* 265 * Do the vm_fault if needed; do the copy-on-write thing 266 * when reading stuff off device into memory. 267 */ 268 vm_fault_quick((addr >= bp->b_data) ? addr : bp->b_data, 269 (bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 270 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 271 if (pa == 0) 272 panic("vmapbuf: page not present"); 273 vm_page_hold(PHYS_TO_VM_PAGE(pa)); 274 pmap_kenter((vm_offset_t) v, pa); 275 } 276 277 kva = bp->b_saveaddr; 278 bp->b_saveaddr = bp->b_data; 279 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 280} 281 282/* 283 * Free the io map PTEs associated with this IO operation. 284 * We also invalidate the TLB entries and restore the original b_addr. 285 */ 286void 287vunmapbuf(bp) 288 register struct buf *bp; 289{ 290 register caddr_t addr; 291 vm_offset_t pa; 292 293 GIANT_REQUIRED; 294 295 if ((bp->b_flags & B_PHYS) == 0) 296 panic("vunmapbuf"); 297 298 for (addr = (caddr_t)trunc_page(bp->b_data); 299 addr < bp->b_data + bp->b_bufsize; 300 addr += PAGE_SIZE) { 301 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 302 pmap_kremove((vm_offset_t) addr); 303 vm_page_unhold(PHYS_TO_VM_PAGE(pa)); 304 } 305 306 bp->b_data = bp->b_saveaddr; 307} 308 309/* 310 * Reset back to firmware. 311 */ 312void 313cpu_reset() 314{ 315 OF_exit(); 316} 317 318int 319grow_stack(p, sp) 320 struct proc *p; 321 size_t sp; 322{ 323 int rv; 324 325 rv = vm_map_growstack (p, sp); 326 if (rv != KERN_SUCCESS) 327 return (0); 328 329 return (1); 330} 331 332/* 333 * Software interrupt handler for queued VM system processing. 334 */ 335void 336swi_vm(void *dummy) 337{ 338#if 0 /* XXX: Don't have busdma stuff yet */ 339 if (busdma_swi_pending != 0) 340 busdma_swi(); 341#endif 342} 343 344/* 345 * Tell whether this address is in some physical memory region. 346 * Currently used by the kernel coredump code in order to avoid 347 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 348 * or other unpredictable behaviour. 349 */ 350 351 352int 353is_physical_memory(addr) 354 vm_offset_t addr; 355{ 356 /* 357 * stuff other tests for known memory-mapped devices (PCI?) 358 * here 359 */ 360 361 return 1; 362} 363