vm_machdep.c revision 107719
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/ia64/ia64/vm_machdep.c 107719 2002-12-10 02:33:45Z 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/vnode.h> 77#include <sys/vmmeter.h> 78#include <sys/kernel.h> 79#include <sys/sysctl.h> 80#include <sys/unistd.h> 81 82#include <machine/clock.h> 83#include <machine/cpu.h> 84#include <machine/fpu.h> 85#include <machine/md_var.h> 86 87#include <vm/vm.h> 88#include <vm/vm_param.h> 89#include <sys/lock.h> 90#include <vm/vm_kern.h> 91#include <vm/vm_page.h> 92#include <vm/vm_map.h> 93#include <vm/vm_extern.h> 94 95#include <sys/user.h> 96 97#include <i386/include/psl.h> 98 99/* 100 * quick version of vm_fault 101 */ 102int 103vm_fault_quick(v, prot) 104 caddr_t v; 105 int prot; 106{ 107 int r; 108 if (prot & VM_PROT_WRITE) 109 r = subyte(v, fubyte(v)); 110 else 111 r = fubyte(v); 112 return(r); 113} 114 115void 116cpu_thread_exit(struct thread *td) 117{ 118} 119 120void 121cpu_thread_clean(struct thread *td) 122{ 123} 124 125void 126cpu_thread_setup(struct thread *td) 127{ 128} 129 130void 131cpu_set_upcall(struct thread *td, void *pcb) 132{ 133} 134 135void 136cpu_set_upcall_kse(struct thread *td, struct kse *ke) 137{ 138} 139 140/* 141 * Finish a fork operation, with process p2 nearly set up. 142 * Copy and update the pcb, set up the stack so that the child 143 * ready to run and return to user mode. 144 */ 145void 146cpu_fork(td1, p2, td2, flags) 147 register struct thread *td1; 148 register struct proc *p2; 149 register struct thread *td2; 150 int flags; 151{ 152 struct proc *p1; 153 struct trapframe *p2tf; 154 u_int64_t bspstore, *p1bs, *p2bs, rnatloc, rnat; 155 156 KASSERT(td1 == curthread || td1 == &thread0, 157 ("cpu_fork: p1 not curproc and not proc0")); 158 159 if ((flags & RFPROC) == 0) 160 return; 161 162 p1 = td1->td_proc; 163 td2->td_pcb = (struct pcb *) 164 (td2->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1; 165 td2->td_md.md_flags = td1->td_md.md_flags & (MDP_FPUSED | MDP_UAC_MASK); 166 167 /* 168 * Copy floating point state from the FP chip to the PCB 169 * if this process has state stored there. 170 */ 171 ia64_fpstate_save(td1, 0); 172 173 /* 174 * Copy pcb and stack from proc p1 to p2. We do this as 175 * cheaply as possible, copying only the active part of the 176 * stack. The stack and pcb need to agree. Make sure that the 177 * new process has FEN disabled. 178 */ 179 bcopy(td1->td_pcb, td2->td_pcb, sizeof(struct pcb)); 180 181 /* 182 * Set the floating point state. 183 */ 184#if 0 185 if ((td2->td_pcb->pcb_fp_control & IEEE_INHERIT) == 0) { 186 td2->td_pcb->pcb_fp_control = 0; 187 td2->td_pcb->pcb_fp.fpr_cr = (FPCR_DYN_NORMAL 188 | FPCR_INVD | FPCR_DZED 189 | FPCR_OVFD | FPCR_INED 190 | FPCR_UNFD); 191 } 192#endif 193 194 /* 195 * Arrange for a non-local goto when the new process 196 * is started, to resume here, returning nonzero from setjmp. 197 */ 198#ifdef DIAGNOSTIC 199 if (td1 == curthread) 200 ia64_fpstate_check(td1); 201#endif 202 203 /* 204 * create the child's kernel stack, from scratch. 205 * 206 * Pick a stack pointer, leaving room for a trapframe; 207 * copy trapframe from parent so return to user mode 208 * will be to right address, with correct registers. Clear the 209 * high-fp enable for the new process so that it is forced to 210 * load its state from the pcb. 211 */ 212 td2->td_frame = (struct trapframe *)td2->td_pcb - 1; 213 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 214 td2->td_frame->tf_cr_ipsr |= IA64_PSR_DFH; 215 216 /* 217 * Set up return-value registers as fork() libc stub expects. 218 */ 219 p2tf = td2->td_frame; 220 if (p2tf->tf_cr_ipsr & IA64_PSR_IS) { 221 p2tf->tf_r[FRAME_R8] = 0; /* child returns zero (eax) */ 222 p2tf->tf_r[FRAME_R10] = 1; /* is child (edx) */ 223 td2->td_pcb->pcb_ar_eflag &= ~PSL_C; /* no error */ 224 } else { 225 p2tf->tf_r[FRAME_R8] = 0; /* child's pid (linux) */ 226 p2tf->tf_r[FRAME_R9] = 1; /* is child (FreeBSD) */ 227 p2tf->tf_r[FRAME_R10] = 0; /* no error */ 228 } 229 230 /* 231 * Turn off RSE for a moment and work out our current 232 * ar.bspstore. This assumes that td1==curthread. Also 233 * flush dirty regs to ensure that the user's stacked 234 * regs are written out to backing store. 235 * 236 * We could cope with td1!=curthread by digging values 237 * out of its PCB but I don't see the point since 238 * current usage only allows &thread0 when creating kernel 239 * threads and &thread0 doesn't have any dirty regs. 240 */ 241 242 p1bs = (u_int64_t *)td1->td_kstack; 243 p2bs = (u_int64_t *)td2->td_kstack; 244 245 if (td1 == curthread) { 246 __asm __volatile("mov ar.rsc=0;;"); 247 __asm __volatile("flushrs;;" ::: "memory"); 248 __asm __volatile("mov %0=ar.bspstore" : "=r"(bspstore)); 249 } else { 250 bspstore = (u_int64_t) p1bs; 251 } 252 253 /* 254 * Copy enough of td1's backing store to include all 255 * the user's stacked regs. 256 */ 257 bcopy(p1bs, p2bs, td1->td_frame->tf_ndirty); 258 /* 259 * To calculate the ar.rnat for td2, we need to decide 260 * if td1's ar.bspstore has advanced past the place 261 * where the last ar.rnat which covers the user's 262 * saved registers would be placed. If so, we read 263 * that one from memory, otherwise we take td1's 264 * current ar.rnat. If we are simply spawning a new kthread 265 * from &thread0 we don't care about ar.rnat. 266 */ 267 if (td1 == curthread) { 268 rnatloc = (u_int64_t)p1bs + td1->td_frame->tf_ndirty; 269 rnatloc |= 0x1f8; 270 if (bspstore > rnatloc) 271 rnat = *(u_int64_t *) rnatloc; 272 else 273 __asm __volatile("mov %0=ar.rnat;;" : "=r"(rnat)); 274 275 /* 276 * Switch the RSE back on. 277 */ 278 __asm __volatile("mov ar.rsc=3;;"); 279 } else { 280 rnat = 0; 281 } 282 283 /* 284 * Setup the child's pcb so that its ar.bspstore 285 * starts just above the region which we copied. This 286 * should work since the child will normally return 287 * straight into exception_restore. Also initialise its 288 * pmap to the containing proc's vmspace. 289 */ 290 td2->td_pcb->pcb_ar_bsp = (u_int64_t)p2bs + td1->td_frame->tf_ndirty; 291 td2->td_pcb->pcb_ar_rnat = rnat; 292 td2->td_pcb->pcb_ar_pfs = 0; 293 td2->td_pcb->pcb_current_pmap = (u_int64_t) 294 vmspace_pmap(td2->td_proc->p_vmspace); 295 296 /* 297 * Arrange for continuation at fork_return(), which 298 * will return to exception_restore(). Note that the 299 * child process doesn't stay in the kernel for long! 300 * 301 * The extra 16 bytes subtracted from sp is part of the ia64 302 * ABI - a function can assume that the 16 bytes above sp are 303 * available as scratch space. 304 */ 305 td2->td_pcb->pcb_sp = (u_int64_t)p2tf - 16; 306 td2->td_pcb->pcb_r[PCB_R4] = (u_int64_t)fork_return; 307 td2->td_pcb->pcb_r[PCB_R5] = FDESC_FUNC(exception_restore); 308 td2->td_pcb->pcb_r[PCB_R6] = (u_int64_t)td2; 309 td2->td_pcb->pcb_rp = FDESC_FUNC(fork_trampoline); 310} 311 312/* 313 * Intercept the return address from a freshly forked process that has NOT 314 * been scheduled yet. 315 * 316 * This is needed to make kernel threads stay in kernel mode. 317 */ 318void 319cpu_set_fork_handler(td, func, arg) 320 struct thread *td; 321 void (*func)(void *); 322 void *arg; 323{ 324 td->td_pcb->pcb_r[PCB_R4] = (u_int64_t) func; 325 td->td_pcb->pcb_r[PCB_R6] = (u_int64_t) arg; 326} 327 328/* 329 * cpu_exit is called as the last action during exit. 330 * We drop the fp state (if we have it) and switch to a live one. 331 * When the proc is reaped, cpu_wait() will gc the VM state. 332 */ 333void 334cpu_exit(td) 335 register struct thread *td; 336{ 337 338 ia64_fpstate_drop(td); 339} 340 341void 342cpu_sched_exit(td) 343 register struct thread *td; 344{ 345} 346 347void 348cpu_wait(p) 349 struct proc *p; 350{ 351} 352 353/* 354 * Map an IO request into kernel virtual address space. 355 * 356 * All requests are (re)mapped into kernel VA space. 357 * Notice that we use b_bufsize for the size of the buffer 358 * to be mapped. b_bcount might be modified by the driver. 359 */ 360void 361vmapbuf(bp) 362 register struct buf *bp; 363{ 364 register caddr_t addr, v, kva; 365 vm_offset_t pa; 366 367 GIANT_REQUIRED; 368 369 if ((bp->b_flags & B_PHYS) == 0) 370 panic("vmapbuf"); 371 372 for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data); 373 addr < bp->b_data + bp->b_bufsize; 374 addr += PAGE_SIZE, v += PAGE_SIZE) { 375 /* 376 * Do the vm_fault if needed; do the copy-on-write thing 377 * when reading stuff off device into memory. 378 */ 379 vm_fault_quick((addr >= bp->b_data) ? addr : bp->b_data, 380 (bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 381 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 382 if (pa == 0) 383 panic("vmapbuf: page not present"); 384 vm_page_hold(PHYS_TO_VM_PAGE(pa)); 385 pmap_kenter((vm_offset_t) v, pa); 386 } 387 388 kva = bp->b_saveaddr; 389 bp->b_saveaddr = bp->b_data; 390 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 391} 392 393/* 394 * Free the io map PTEs associated with this IO operation. 395 * We also invalidate the TLB entries and restore the original b_addr. 396 */ 397void 398vunmapbuf(bp) 399 register struct buf *bp; 400{ 401 register caddr_t addr; 402 vm_offset_t pa; 403 404 GIANT_REQUIRED; 405 406 if ((bp->b_flags & B_PHYS) == 0) 407 panic("vunmapbuf"); 408 409 vm_page_lock_queues(); 410 for (addr = (caddr_t)trunc_page(bp->b_data); 411 addr < bp->b_data + bp->b_bufsize; 412 addr += PAGE_SIZE) { 413 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 414 pmap_kremove((vm_offset_t) addr); 415 vm_page_unhold(PHYS_TO_VM_PAGE(pa)); 416 } 417 vm_page_unlock_queues(); 418 419 bp->b_data = bp->b_saveaddr; 420} 421 422/* 423 * Force reset the processor by invalidating the entire address space! 424 */ 425void 426cpu_reset() 427{ 428 429 cpu_boot(0); 430} 431 432/* 433 * Software interrupt handler for queued VM system processing. 434 */ 435void 436swi_vm(void *dummy) 437{ 438#if 0 439 if (busdma_swi_pending != 0) 440 busdma_swi(); 441#endif 442} 443 444/* 445 * Tell whether this address is in some physical memory region. 446 * Currently used by the kernel coredump code in order to avoid 447 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 448 * or other unpredictable behaviour. 449 */ 450 451 452int 453is_physical_memory(addr) 454 vm_offset_t addr; 455{ 456 /* 457 * stuff other tests for known memory-mapped devices (PCI?) 458 * here 459 */ 460 461 return 1; 462} 463