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