vm_machdep.c revision 65557
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/amd64/amd64/vm_machdep.c 65557 2000-09-07 01:33:02Z jasone $ 42 */ 43 44#include "npx.h" 45#include "opt_user_ldt.h" 46#ifdef PC98 47#include "opt_pc98.h" 48#endif 49#include "opt_reset.h" 50 51#include <sys/param.h> 52#include <sys/systm.h> 53#include <sys/malloc.h> 54#include <sys/proc.h> 55#include <sys/bio.h> 56#include <sys/buf.h> 57#include <sys/vnode.h> 58#include <sys/vmmeter.h> 59#include <sys/kernel.h> 60#include <sys/ktr.h> 61#include <sys/sysctl.h> 62#include <sys/unistd.h> 63 64#include <machine/clock.h> 65#include <machine/cpu.h> 66#include <machine/md_var.h> 67#include <machine/mutex.h> 68#ifdef SMP 69#include <machine/smp.h> 70#endif 71#include <machine/pcb.h> 72#include <machine/pcb_ext.h> 73#include <machine/vm86.h> 74 75#include <vm/vm.h> 76#include <vm/vm_param.h> 77#include <sys/lock.h> 78#include <vm/vm_kern.h> 79#include <vm/vm_page.h> 80#include <vm/vm_map.h> 81#include <vm/vm_extern.h> 82 83#include <sys/user.h> 84 85#ifdef PC98 86#include <pc98/pc98/pc98.h> 87#else 88#include <i386/isa/isa.h> 89#endif 90 91static void cpu_reset_real __P((void)); 92#ifdef SMP 93static void cpu_reset_proxy __P((void)); 94static u_int cpu_reset_proxyid; 95static volatile u_int cpu_reset_proxy_active; 96#endif 97extern int _ucodesel, _udatasel; 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 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(p1, p2, flags) 123 register struct proc *p1, *p2; 124 int flags; 125{ 126 struct pcb *pcb2; 127 128 if ((flags & RFPROC) == 0) { 129#ifdef USER_LDT 130 if ((flags & RFMEM) == 0) { 131 /* unshare user LDT */ 132 struct pcb *pcb1 = &p1->p_addr->u_pcb; 133 struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt; 134 if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) { 135 pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len); 136 user_ldt_free(pcb1); 137 pcb1->pcb_ldt = pcb_ldt; 138 set_user_ldt(pcb1); 139 } 140 } 141#endif 142 return; 143 } 144 145#if NNPX > 0 146 /* Ensure that p1's pcb is up to date. */ 147 if (npxproc == p1) 148 npxsave(&p1->p_addr->u_pcb.pcb_savefpu); 149#endif 150 151 /* Copy p1's pcb. */ 152 p2->p_addr->u_pcb = p1->p_addr->u_pcb; 153 pcb2 = &p2->p_addr->u_pcb; 154 155 /* 156 * Create a new fresh stack for the new process. 157 * Copy the trap frame for the return to user mode as if from a 158 * syscall. This copies the user mode register values. 159 */ 160 p2->p_md.md_regs = (struct trapframe *) 161 ((int)p2->p_addr + UPAGES * PAGE_SIZE - 16) - 1; 162 bcopy(p1->p_md.md_regs, p2->p_md.md_regs, sizeof(*p2->p_md.md_regs)); 163 164 /* 165 * Set registers for trampoline to user mode. Leave space for the 166 * return address on stack. These are the kernel mode register values. 167 */ 168 pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir); 169 pcb2->pcb_edi = 0; 170 pcb2->pcb_esi = (int)fork_return; /* fork_trampoline argument */ 171 pcb2->pcb_ebp = 0; 172 pcb2->pcb_esp = (int)p2->p_md.md_regs - sizeof(void *); 173 pcb2->pcb_ebx = (int)p2; /* fork_trampoline argument */ 174 pcb2->pcb_eip = (int)fork_trampoline; 175 /* 176 * pcb2->pcb_ldt: duplicated below, if necessary. 177 * pcb2->pcb_savefpu: cloned above. 178 * pcb2->pcb_flags: cloned above (always 0 here?). 179 * pcb2->pcb_onfault: cloned above (always NULL here?). 180 */ 181 182 pcb2->pcb_schednest = 0; 183 184 /* 185 * XXX don't copy the i/o pages. this should probably be fixed. 186 */ 187 pcb2->pcb_ext = 0; 188 189#ifdef USER_LDT 190 /* Copy the LDT, if necessary. */ 191 if (pcb2->pcb_ldt != 0) { 192 if (flags & RFMEM) { 193 pcb2->pcb_ldt->ldt_refcnt++; 194 } else { 195 pcb2->pcb_ldt = user_ldt_alloc(pcb2, 196 pcb2->pcb_ldt->ldt_len); 197 } 198 } 199#endif 200 201 /* 202 * Now, cpu_switch() can schedule the new process. 203 * pcb_esp is loaded pointing to the cpu_switch() stack frame 204 * containing the return address when exiting cpu_switch. 205 * This will normally be to fork_trampoline(), which will have 206 * %ebx loaded with the new proc's pointer. fork_trampoline() 207 * will set up a stack to call fork_return(p, frame); to complete 208 * the return to user-mode. 209 */ 210} 211 212/* 213 * Intercept the return address from a freshly forked process that has NOT 214 * been scheduled yet. 215 * 216 * This is needed to make kernel threads stay in kernel mode. 217 */ 218void 219cpu_set_fork_handler(p, func, arg) 220 struct proc *p; 221 void (*func) __P((void *)); 222 void *arg; 223{ 224 /* 225 * Note that the trap frame follows the args, so the function 226 * is really called like this: func(arg, frame); 227 */ 228 p->p_addr->u_pcb.pcb_esi = (int) func; /* function */ 229 p->p_addr->u_pcb.pcb_ebx = (int) arg; /* first arg */ 230} 231 232void 233cpu_exit(p) 234 register struct proc *p; 235{ 236 struct pcb *pcb = &p->p_addr->u_pcb; 237 238#if NNPX > 0 239 npxexit(p); 240#endif /* NNPX */ 241 if (pcb->pcb_ext != 0) { 242 /* 243 * XXX do we need to move the TSS off the allocated pages 244 * before freeing them? (not done here) 245 */ 246 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext, 247 ctob(IOPAGES + 1)); 248 pcb->pcb_ext = 0; 249 } 250#ifdef USER_LDT 251 user_ldt_free(pcb); 252#endif 253 if (pcb->pcb_flags & PCB_DBREGS) { 254 /* 255 * disable all hardware breakpoints 256 */ 257 reset_dbregs(); 258 pcb->pcb_flags &= ~PCB_DBREGS; 259 } 260 mtx_enter(&sched_lock, MTX_SPIN); 261 mtx_exit(&Giant, MTX_DEF | MTX_NOSWITCH); 262 mtx_assert(&Giant, MA_NOTOWNED); 263 cnt.v_swtch++; 264 cpu_switch(); 265 panic("cpu_exit"); 266} 267 268void 269cpu_wait(p) 270 struct proc *p; 271{ 272 /* drop per-process resources */ 273 pmap_dispose_proc(p); 274 275 /* and clean-out the vmspace */ 276 vmspace_free(p->p_vmspace); 277} 278 279/* 280 * Dump the machine specific header information at the start of a core dump. 281 */ 282int 283cpu_coredump(p, vp, cred) 284 struct proc *p; 285 struct vnode *vp; 286 struct ucred *cred; 287{ 288 int error; 289 caddr_t tempuser; 290 291 tempuser = malloc(ctob(UPAGES), M_TEMP, M_WAITOK); 292 if (!tempuser) 293 return EINVAL; 294 295 bzero(tempuser, ctob(UPAGES)); 296 bcopy(p->p_addr, tempuser, sizeof(struct user)); 297 bcopy(p->p_md.md_regs, 298 tempuser + ((caddr_t) p->p_md.md_regs - (caddr_t) p->p_addr), 299 sizeof(struct trapframe)); 300 301 error = vn_rdwr(UIO_WRITE, vp, (caddr_t) tempuser, 302 ctob(UPAGES), 303 (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, 304 cred, (int *)NULL, p); 305 306 free(tempuser, M_TEMP); 307 308 return error; 309} 310 311#ifdef notyet 312static void 313setredzone(pte, vaddr) 314 u_short *pte; 315 caddr_t vaddr; 316{ 317/* eventually do this by setting up an expand-down stack segment 318 for ss0: selector, allowing stack access down to top of u. 319 this means though that protection violations need to be handled 320 thru a double fault exception that must do an integral task 321 switch to a known good context, within which a dump can be 322 taken. a sensible scheme might be to save the initial context 323 used by sched (that has physical memory mapped 1:1 at bottom) 324 and take the dump while still in mapped mode */ 325} 326#endif 327 328/* 329 * Convert kernel VA to physical address 330 */ 331u_long 332kvtop(void *addr) 333{ 334 vm_offset_t va; 335 336 va = pmap_kextract((vm_offset_t)addr); 337 if (va == 0) 338 panic("kvtop: zero page frame"); 339 return((int)va); 340} 341 342/* 343 * Map an IO request into kernel virtual address space. 344 * 345 * All requests are (re)mapped into kernel VA space. 346 * Notice that we use b_bufsize for the size of the buffer 347 * to be mapped. b_bcount might be modified by the driver. 348 */ 349void 350vmapbuf(bp) 351 register struct buf *bp; 352{ 353 register caddr_t addr, v, kva; 354 vm_offset_t pa; 355 356 if ((bp->b_flags & B_PHYS) == 0) 357 panic("vmapbuf"); 358 359 for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data); 360 addr < bp->b_data + bp->b_bufsize; 361 addr += PAGE_SIZE, v += PAGE_SIZE) { 362 /* 363 * Do the vm_fault if needed; do the copy-on-write thing 364 * when reading stuff off device into memory. 365 */ 366 vm_fault_quick(addr, 367 (bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ); 368 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 369 if (pa == 0) 370 panic("vmapbuf: page not present"); 371 vm_page_hold(PHYS_TO_VM_PAGE(pa)); 372 pmap_kenter((vm_offset_t) v, pa); 373 } 374 375 kva = bp->b_saveaddr; 376 bp->b_saveaddr = bp->b_data; 377 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK); 378} 379 380/* 381 * Free the io map PTEs associated with this IO operation. 382 * We also invalidate the TLB entries and restore the original b_addr. 383 */ 384void 385vunmapbuf(bp) 386 register struct buf *bp; 387{ 388 register caddr_t addr; 389 vm_offset_t pa; 390 391 if ((bp->b_flags & B_PHYS) == 0) 392 panic("vunmapbuf"); 393 394 for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data); 395 addr < bp->b_data + bp->b_bufsize; 396 addr += PAGE_SIZE) { 397 pa = trunc_page(pmap_kextract((vm_offset_t) addr)); 398 pmap_kremove((vm_offset_t) addr); 399 vm_page_unhold(PHYS_TO_VM_PAGE(pa)); 400 } 401 402 bp->b_data = bp->b_saveaddr; 403} 404 405/* 406 * Force reset the processor by invalidating the entire address space! 407 */ 408 409#ifdef SMP 410static void 411cpu_reset_proxy() 412{ 413 414 cpu_reset_proxy_active = 1; 415 while (cpu_reset_proxy_active == 1) 416 ; /* Wait for other cpu to see that we've started */ 417 stop_cpus((1<<cpu_reset_proxyid)); 418 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid); 419 DELAY(1000000); 420 cpu_reset_real(); 421} 422#endif 423 424void 425cpu_reset() 426{ 427#ifdef SMP 428 if (smp_active == 0) { 429 cpu_reset_real(); 430 /* NOTREACHED */ 431 } else { 432 433 u_int map; 434 int cnt; 435 printf("cpu_reset called on cpu#%d\n",cpuid); 436 437 map = other_cpus & ~ stopped_cpus; 438 439 if (map != 0) { 440 printf("cpu_reset: Stopping other CPUs\n"); 441 stop_cpus(map); /* Stop all other CPUs */ 442 } 443 444 if (cpuid == 0) { 445 DELAY(1000000); 446 cpu_reset_real(); 447 /* NOTREACHED */ 448 } else { 449 /* We are not BSP (CPU #0) */ 450 451 cpu_reset_proxyid = cpuid; 452 cpustop_restartfunc = cpu_reset_proxy; 453 cpu_reset_proxy_active = 0; 454 printf("cpu_reset: Restarting BSP\n"); 455 started_cpus = (1<<0); /* Restart CPU #0 */ 456 457 cnt = 0; 458 while (cpu_reset_proxy_active == 0 && cnt < 10000000) 459 cnt++; /* Wait for BSP to announce restart */ 460 if (cpu_reset_proxy_active == 0) 461 printf("cpu_reset: Failed to restart BSP\n"); 462 enable_intr(); 463 cpu_reset_proxy_active = 2; 464 465 while (1); 466 /* NOTREACHED */ 467 } 468 } 469#else 470 cpu_reset_real(); 471#endif 472} 473 474static void 475cpu_reset_real() 476{ 477 478#ifdef PC98 479 /* 480 * Attempt to do a CPU reset via CPU reset port. 481 */ 482 disable_intr(); 483 if ((inb(0x35) & 0xa0) != 0xa0) { 484 outb(0x37, 0x0f); /* SHUT0 = 0. */ 485 outb(0x37, 0x0b); /* SHUT1 = 0. */ 486 } 487 outb(0xf0, 0x00); /* Reset. */ 488#else 489 /* 490 * Attempt to do a CPU reset via the keyboard controller, 491 * do not turn of the GateA20, as any machine that fails 492 * to do the reset here would then end up in no man's land. 493 */ 494 495#if !defined(BROKEN_KEYBOARD_RESET) 496 outb(IO_KBD + 4, 0xFE); 497 DELAY(500000); /* wait 0.5 sec to see if that did it */ 498 printf("Keyboard reset did not work, attempting CPU shutdown\n"); 499 DELAY(1000000); /* wait 1 sec for printf to complete */ 500#endif 501#endif /* PC98 */ 502 /* force a shutdown by unmapping entire address space ! */ 503 bzero((caddr_t) PTD, PAGE_SIZE); 504 505 /* "good night, sweet prince .... <THUNK!>" */ 506 invltlb(); 507 /* NOTREACHED */ 508 while(1); 509} 510 511int 512grow_stack(p, sp) 513 struct proc *p; 514 u_int sp; 515{ 516 int rv; 517 518 rv = vm_map_growstack (p, sp); 519 if (rv != KERN_SUCCESS) 520 return (0); 521 522 return (1); 523} 524 525SYSCTL_DECL(_vm_stats_misc); 526 527static int cnt_prezero; 528 529SYSCTL_INT(_vm_stats_misc, OID_AUTO, 530 cnt_prezero, CTLFLAG_RD, &cnt_prezero, 0, ""); 531 532/* 533 * Implement the pre-zeroed page mechanism. 534 * This routine is called from the idle loop. 535 */ 536 537#define ZIDLE_LO(v) ((v) * 2 / 3) 538#define ZIDLE_HI(v) ((v) * 4 / 5) 539 540int 541vm_page_zero_idle() 542{ 543 static int free_rover; 544 static int zero_state; 545 vm_page_t m; 546 int s, intrsave; 547 548 /* 549 * Attempt to maintain approximately 1/2 of our free pages in a 550 * PG_ZERO'd state. Add some hysteresis to (attempt to) avoid 551 * generally zeroing a page when the system is near steady-state. 552 * Otherwise we might get 'flutter' during disk I/O / IPC or 553 * fast sleeps. We also do not want to be continuously zeroing 554 * pages because doing so may flush our L1 and L2 caches too much. 555 */ 556 557 if (zero_state && vm_page_zero_count >= ZIDLE_LO(cnt.v_free_count)) 558 return(0); 559 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 560 return(0); 561 562 if (mtx_try_enter(&Giant, MTX_DEF)) { 563 s = splvm(); 564 intrsave = save_intr(); 565 enable_intr(); 566 zero_state = 0; 567 m = vm_page_list_find(PQ_FREE, free_rover, FALSE); 568 if (m != NULL && (m->flags & PG_ZERO) == 0) { 569 vm_page_queues[m->queue].lcnt--; 570 TAILQ_REMOVE(&vm_page_queues[m->queue].pl, m, pageq); 571 m->queue = PQ_NONE; 572 splx(s); 573 pmap_zero_page(VM_PAGE_TO_PHYS(m)); 574 (void)splvm(); 575 vm_page_flag_set(m, PG_ZERO); 576 m->queue = PQ_FREE + m->pc; 577 vm_page_queues[m->queue].lcnt++; 578 TAILQ_INSERT_TAIL(&vm_page_queues[m->queue].pl, m, 579 pageq); 580 ++vm_page_zero_count; 581 ++cnt_prezero; 582 if (vm_page_zero_count >= ZIDLE_HI(cnt.v_free_count)) 583 zero_state = 1; 584 } 585 free_rover = (free_rover + PQ_PRIME2) & PQ_L2_MASK; 586 splx(s); 587 restore_intr(intrsave); 588 mtx_exit(&Giant, MTX_DEF); 589 return (1); 590 } 591 /* 592 * We have to enable interrupts for a moment if the try_mplock fails 593 * in order to potentially take an IPI. XXX this should be in 594 * swtch.s 595 */ 596 __asm __volatile("sti; nop; cli" : : : "memory"); 597 return (0); 598} 599 600/* 601 * Software interrupt handler for queued VM system processing. 602 */ 603void 604swi_vm() 605{ 606 if (busdma_swi_pending != 0) 607 busdma_swi(); 608} 609 610/* 611 * Tell whether this address is in some physical memory region. 612 * Currently used by the kernel coredump code in order to avoid 613 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 614 * or other unpredictable behaviour. 615 */ 616 617#include "isa.h" 618 619int 620is_physical_memory(addr) 621 vm_offset_t addr; 622{ 623 624#if NISA > 0 625 /* The ISA ``memory hole''. */ 626 if (addr >= 0xa0000 && addr < 0x100000) 627 return 0; 628#endif 629 630 /* 631 * stuff other tests for known memory-mapped devices (PCI?) 632 * here 633 */ 634 635 return 1; 636} 637