vm_machdep.c revision 66458
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 66458 2000-09-29 13:46:07Z dfr $ 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/* 98 * quick version of vm_fault 99 */ 100int 101vm_fault_quick(v, prot) 102 caddr_t v; 103 int prot; 104{ 105 int r; 106 if (prot & VM_PROT_WRITE) 107 r = subyte(v, fubyte(v)); 108 else 109 r = fubyte(v); 110 return(r); 111} 112 113/* 114 * Finish a fork operation, with process p2 nearly set up. 115 * Copy and update the pcb, set up the stack so that the child 116 * ready to run and return to user mode. 117 */ 118void 119cpu_fork(p1, p2, flags) 120 register struct proc *p1, *p2; 121 int flags; 122{ 123 if ((flags & RFPROC) == 0) 124 return; 125 126 p2->p_md.md_tf = p1->p_md.md_tf; 127 p2->p_md.md_flags = p1->p_md.md_flags & (MDP_FPUSED | MDP_UAC_MASK); 128 129 /* 130 * Cache the physical address of the pcb, so we can 131 * swap to it easily. 132 */ 133 p2->p_md.md_pcbpaddr = (void*)vtophys((vm_offset_t)&p2->p_addr->u_pcb); 134 135 /* 136 * Copy floating point state from the FP chip to the PCB 137 * if this process has state stored there. 138 */ 139 ia64_fpstate_save(p1, 0); 140 141 /* 142 * Copy pcb and stack from proc p1 to p2. We do this as 143 * cheaply as possible, copying only the active part of the 144 * stack. The stack and pcb need to agree. Make sure that the 145 * new process has FEN disabled. 146 */ 147 p2->p_addr->u_pcb = p1->p_addr->u_pcb; 148#if 0 149 p2->p_addr->u_pcb.pcb_hw.apcb_usp = ia64_pal_rdusp(); 150 p2->p_addr->u_pcb.pcb_hw.apcb_flags &= ~IA64_PCB_FLAGS_FEN; 151#endif 152 153 /* 154 * Set the floating point state. 155 */ 156#if 0 157 if ((p2->p_addr->u_pcb.pcb_fp_control & IEEE_INHERIT) == 0) { 158 p2->p_addr->u_pcb.pcb_fp_control = 0; 159 p2->p_addr->u_pcb.pcb_fp.fpr_cr = (FPCR_DYN_NORMAL 160 | FPCR_INVD | FPCR_DZED 161 | FPCR_OVFD | FPCR_INED 162 | FPCR_UNFD); 163 } 164#endif 165 166 /* 167 * Arrange for a non-local goto when the new process 168 * is started, to resume here, returning nonzero from setjmp. 169 */ 170#ifdef DIAGNOSTIC 171 if (p1 != curproc) 172 panic("cpu_fork: curproc"); 173 ia64_fpstate_check(p1); 174#endif 175 176 /* 177 * create the child's kernel stack, from scratch. 178 */ 179 { 180 struct user *up = p2->p_addr; 181 struct trapframe *p2tf; 182 183 /* 184 * Pick a stack pointer, leaving room for a trapframe; 185 * copy trapframe from parent so return to user mode 186 * will be to right address, with correct registers. 187 */ 188 p2tf = p2->p_md.md_tf = (struct trapframe *) 189 ((char *)p2->p_addr + USPACE - sizeof(struct trapframe)); 190 bcopy(p1->p_md.md_tf, p2->p_md.md_tf, 191 sizeof(struct trapframe)); 192 193 /* 194 * Set up return-value registers as fork() libc stub expects. 195 */ 196#if 0 197 p2tf->tf_regs[FRAME_V0] = 0; /* child's pid (linux) */ 198 p2tf->tf_regs[FRAME_A3] = 0; /* no error */ 199 p2tf->tf_regs[FRAME_A4] = 1; /* is child (FreeBSD) */ 200 201 /* 202 * Arrange for continuation at child_return(), which 203 * will return to exception_return(). Note that the child 204 * process doesn't stay in the kernel for long! 205 * 206 * This is an inlined version of cpu_set_kpc. 207 */ 208 up->u_pcb.pcb_hw.apcb_ksp = (u_int64_t)p2tf; 209 up->u_pcb.pcb_context[0] = 210 (u_int64_t)child_return; /* s0: pc */ 211 up->u_pcb.pcb_context[1] = 212 (u_int64_t)exception_return; /* s1: ra */ 213 up->u_pcb.pcb_context[2] = (u_long) p2; /* s2: a0 */ 214 up->u_pcb.pcb_context[7] = 215 (u_int64_t)switch_trampoline; /* ra: assembly magic */ 216#endif 217 } 218} 219 220/* 221 * Intercept the return address from a freshly forked process that has NOT 222 * been scheduled yet. 223 * 224 * This is needed to make kernel threads stay in kernel mode. 225 */ 226void 227cpu_set_fork_handler(p, func, arg) 228 struct proc *p; 229 void (*func) __P((void *)); 230 void *arg; 231{ 232#if 0 233 /* 234 * Note that the trap frame follows the args, so the function 235 * is really called like this: func(arg, frame); 236 */ 237 p->p_addr->u_pcb.pcb_context[0] = (u_long) func; 238 p->p_addr->u_pcb.pcb_context[2] = (u_long) arg; 239#endif 240} 241 242/* 243 * cpu_exit is called as the last action during exit. 244 * We drop the fp state (if we have it) and switch to a live one. 245 * When the proc is reaped, cpu_wait() will gc the VM state. 246 */ 247void 248cpu_exit(p) 249 register struct proc *p; 250{ 251 ia64_fpstate_drop(p); 252 253 (void) splhigh(); 254 cnt.v_swtch++; 255 cpu_switch(); 256 panic("cpu_exit"); 257} 258 259void 260cpu_wait(p) 261 struct proc *p; 262{ 263 /* drop per-process resources */ 264 pmap_dispose_proc(p); 265 266 /* and clean-out the vmspace */ 267 vmspace_free(p->p_vmspace); 268} 269 270/* 271 * Dump the machine specific header information at the start of a core dump. 272 */ 273int 274cpu_coredump(p, vp, cred) 275 struct proc *p; 276 struct vnode *vp; 277 struct ucred *cred; 278{ 279 280 return (vn_rdwr(UIO_WRITE, vp, (caddr_t) p->p_addr, ctob(UPAGES), 281 (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, 282 p)); 283} 284 285#ifdef notyet 286static void 287setredzone(pte, vaddr) 288 u_short *pte; 289 caddr_t vaddr; 290{ 291/* eventually do this by setting up an expand-down stack segment 292 for ss0: selector, allowing stack access down to top of u. 293 this means though that protection violations need to be handled 294 thru a double fault exception that must do an integral task 295 switch to a known good context, within which a dump can be 296 taken. a sensible scheme might be to save the initial context 297 used by sched (that has physical memory mapped 1:1 at bottom) 298 and take the dump while still in mapped mode */ 299} 300#endif 301 302/* 303 * Map an IO request into kernel virtual address space. 304 * 305 * All requests are (re)mapped into kernel VA space. 306 * Notice that we use b_bufsize for the size of the buffer 307 * to be mapped. b_bcount might be modified by the driver. 308 */ 309void 310vmapbuf(bp) 311 register struct buf *bp; 312{ 313 register caddr_t addr, v, kva; 314 vm_offset_t pa; 315 316 if ((bp->b_flags & B_PHYS) == 0) 317 panic("vmapbuf"); 318 319 for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page(bp->b_data); 320 addr < bp->b_data + bp->b_bufsize; 321 addr += PAGE_SIZE, v += PAGE_SIZE) { 322 /* 323 * Do the vm_fault if needed; do the copy-on-write thing 324 * when reading stuff off device into memory. 325 */ 326 vm_fault_quick(addr, 327 (bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 328 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 329 if (pa == 0) 330 panic("vmapbuf: page not present"); 331 vm_page_hold(PHYS_TO_VM_PAGE(pa)); 332 pmap_kenter((vm_offset_t) v, pa); 333 } 334 335 kva = bp->b_saveaddr; 336 bp->b_saveaddr = bp->b_data; 337 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 338} 339 340/* 341 * Free the io map PTEs associated with this IO operation. 342 * We also invalidate the TLB entries and restore the original b_addr. 343 */ 344void 345vunmapbuf(bp) 346 register struct buf *bp; 347{ 348 register caddr_t addr; 349 vm_offset_t pa; 350 351 if ((bp->b_flags & B_PHYS) == 0) 352 panic("vunmapbuf"); 353 354 for (addr = (caddr_t)trunc_page(bp->b_data); 355 addr < bp->b_data + bp->b_bufsize; 356 addr += PAGE_SIZE) { 357 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 358 pmap_kremove((vm_offset_t) addr); 359 vm_page_unhold(PHYS_TO_VM_PAGE(pa)); 360 } 361 362 bp->b_data = bp->b_saveaddr; 363} 364 365/* 366 * Force reset the processor by invalidating the entire address space! 367 */ 368void 369cpu_reset() 370{ 371 /* prom_halt(0); */ 372} 373 374int 375grow_stack(p, sp) 376 struct proc *p; 377 size_t sp; 378{ 379 int rv; 380 381 rv = vm_map_growstack (p, sp); 382 if (rv != KERN_SUCCESS) 383 return (0); 384 385 return (1); 386} 387 388 389static int cnt_prezero; 390 391SYSCTL_INT(_machdep, OID_AUTO, cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, ""); 392 393/* 394 * Implement the pre-zeroed page mechanism. 395 * This routine is called from the idle loop. 396 */ 397 398#define ZIDLE_LO(v) ((v) * 2 / 3) 399#define ZIDLE_HI(v) ((v) * 4 / 5) 400 401int 402vm_page_zero_idle() 403{ 404 static int free_rover; 405 static int zero_state; 406 vm_page_t m; 407 int s; 408 409 /* 410 * Attempt to maintain approximately 1/2 of our free pages in a 411 * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid 412 * generally zeroing a page when the system is near steady-state. 413 * Otherwise we might get 'flutter' during disk I/O / IPC or 414 * fast sleeps. We also do not want to be continuously zeroing 415 * pages because doing so may flush our L1 and L2 caches too much. 416 */ 417 418 if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count)) 419 return(0); 420 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 421 return(0); 422 423#ifdef SMP 424 if (try_mplock()) { 425#endif 426 s = splvm(); 427 m = vm_page_list_find(PQ_FREE, free_rover, FALSE); 428 zero_state = 0; 429 if (m != NULL && (m->flags & PG_ZERO) == 0) { 430 vm_page_queues[m->queue].lcnt--; 431 TAILQ_REMOVE(&vm_page_queues[m->queue].pl, m, pageq); 432 m->queue = PQ_NONE; 433 splx(s); 434#if 0 435 rel_mplock(); 436#endif 437 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 438#if 0 439 get_mplock(); 440#endif 441 (void)splvm(); 442 vm_page_flag_set(m, PG_ZERO); 443 m->queue = PQ_FREE + m->pc; 444 vm_page_queues[m->queue].lcnt++; 445 TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m, 446 pageq); 447 ++vm_page_zero_count; 448 ++cnt_prezero; 449 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 450 zero_state = 1; 451 } 452 free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK; 453 splx(s); 454#ifdef SMP 455 rel_mplock(); 456#endif 457 return (1); 458#ifdef SMP 459 } 460#endif 461 return (0); 462} 463 464/* 465 * Software interrupt handler for queued VM system processing. 466 */ 467void 468swi_vm() 469{ 470#if 0 471 if (busdma_swi_pending != 0) 472 busdma_swi(); 473#endif 474} 475 476/* 477 * Tell whether this address is in some physical memory region. 478 * Currently used by the kernel coredump code in order to avoid 479 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 480 * or other unpredictable behaviour. 481 */ 482 483 484int 485is_physical_memory(addr) 486 vm_offset_t addr; 487{ 488 /* 489 * stuff other tests for known memory-mapped devices (PCI?) 490 * here 491 */ 492 493 return 1; 494} 495