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