vm_machdep.c revision 128100
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 */ 42 43#include <sys/cdefs.h> 44__FBSDID("$FreeBSD: head/sys/amd64/amd64/vm_machdep.c 128100 2004-04-11 04:26:58Z alc $"); 45 46#include "opt_isa.h" 47#include "opt_cpu.h" 48 49#include <sys/param.h> 50#include <sys/systm.h> 51#include <sys/bio.h> 52#include <sys/buf.h> 53#include <sys/kse.h> 54#include <sys/kernel.h> 55#include <sys/ktr.h> 56#include <sys/lock.h> 57#include <sys/malloc.h> 58#include <sys/mbuf.h> 59#include <sys/mutex.h> 60#include <sys/proc.h> 61#include <sys/sf_buf.h> 62#include <sys/smp.h> 63#include <sys/sysctl.h> 64#include <sys/unistd.h> 65#include <sys/user.h> 66#include <sys/vnode.h> 67#include <sys/vmmeter.h> 68 69#include <machine/cpu.h> 70#include <machine/md_var.h> 71#include <machine/pcb.h> 72 73#include <vm/vm.h> 74#include <vm/vm_extern.h> 75#include <vm/vm_kern.h> 76#include <vm/vm_page.h> 77#include <vm/vm_map.h> 78#include <vm/vm_param.h> 79 80#include <amd64/isa/isa.h> 81 82static void cpu_reset_real(void); 83#ifdef SMP 84static void cpu_reset_proxy(void); 85static u_int cpu_reset_proxyid; 86static volatile u_int cpu_reset_proxy_active; 87#endif 88static void sf_buf_init(void *arg); 89SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL) 90 91/* 92 * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the 93 * sf_freelist head with the sf_lock mutex. 94 */ 95static struct { 96 SLIST_HEAD(, sf_buf) sf_head; 97 struct mtx sf_lock; 98} sf_freelist; 99 100static u_int sf_buf_alloc_want; 101 102/* 103 * Finish a fork operation, with process p2 nearly set up. 104 * Copy and update the pcb, set up the stack so that the child 105 * ready to run and return to user mode. 106 */ 107void 108cpu_fork(td1, p2, td2, flags) 109 register struct thread *td1; 110 register struct proc *p2; 111 struct thread *td2; 112 int flags; 113{ 114 register struct proc *p1; 115 struct pcb *pcb2; 116 struct mdproc *mdp2; 117 118 p1 = td1->td_proc; 119 if ((flags & RFPROC) == 0) 120 return; 121 122 /* Ensure that p1's pcb is up to date. */ 123 fpuexit(td1); 124 125 /* Point the pcb to the top of the stack */ 126 pcb2 = (struct pcb *)(td2->td_kstack + 127 td2->td_kstack_pages * PAGE_SIZE) - 1; 128 td2->td_pcb = pcb2; 129 130 /* Copy p1's pcb */ 131 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 132 133 /* Point mdproc and then copy over td1's contents */ 134 mdp2 = &p2->p_md; 135 bcopy(&p1->p_md, mdp2, sizeof(*mdp2)); 136 137 /* 138 * Create a new fresh stack for the new process. 139 * Copy the trap frame for the return to user mode as if from a 140 * syscall. This copies most of the user mode register values. 141 */ 142 td2->td_frame = (struct trapframe *)td2->td_pcb - 1; 143 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 144 145 td2->td_frame->tf_rax = 0; /* Child returns zero */ 146 td2->td_frame->tf_rflags &= ~PSL_C; /* success */ 147 td2->td_frame->tf_rdx = 1; 148 149 /* 150 * Set registers for trampoline to user mode. Leave space for the 151 * return address on stack. These are the kernel mode register values. 152 */ 153 pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4); 154 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */ 155 pcb2->pcb_rbp = 0; 156 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *); 157 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */ 158 pcb2->pcb_rip = (register_t)fork_trampoline; 159 pcb2->pcb_rflags = td2->td_frame->tf_rflags & ~PSL_I; /* ints disabled */ 160 /*- 161 * pcb2->pcb_dr*: cloned above. 162 * pcb2->pcb_savefpu: cloned above. 163 * pcb2->pcb_flags: cloned above. 164 * pcb2->pcb_onfault: cloned above (always NULL here?). 165 * pcb2->pcb_[fg]sbase: cloned above 166 */ 167 168 /* 169 * Now, cpu_switch() can schedule the new process. 170 * pcb_rsp is loaded pointing to the cpu_switch() stack frame 171 * containing the return address when exiting cpu_switch. 172 * This will normally be to fork_trampoline(), which will have 173 * %ebx loaded with the new proc's pointer. fork_trampoline() 174 * will set up a stack to call fork_return(p, frame); to complete 175 * the return to user-mode. 176 */ 177} 178 179/* 180 * Intercept the return address from a freshly forked process that has NOT 181 * been scheduled yet. 182 * 183 * This is needed to make kernel threads stay in kernel mode. 184 */ 185void 186cpu_set_fork_handler(td, func, arg) 187 struct thread *td; 188 void (*func)(void *); 189 void *arg; 190{ 191 /* 192 * Note that the trap frame follows the args, so the function 193 * is really called like this: func(arg, frame); 194 */ 195 td->td_pcb->pcb_r12 = (long) func; /* function */ 196 td->td_pcb->pcb_rbx = (long) arg; /* first arg */ 197} 198 199void 200cpu_exit(struct thread *td) 201{ 202 struct pcb *pcb = td->td_pcb; 203 204 if (pcb->pcb_flags & PCB_DBREGS) { 205 /* disable all hardware breakpoints */ 206 reset_dbregs(); 207 pcb->pcb_flags &= ~PCB_DBREGS; 208 } 209} 210 211void 212cpu_thread_exit(struct thread *td) 213{ 214 struct pcb *pcb = td->td_pcb; 215 216 if (td == PCPU_GET(fpcurthread)) 217 fpudrop(); 218 if (pcb->pcb_flags & PCB_DBREGS) { 219 /* disable all hardware breakpoints */ 220 reset_dbregs(); 221 pcb->pcb_flags &= ~PCB_DBREGS; 222 } 223} 224 225void 226cpu_thread_clean(struct thread *td) 227{ 228} 229 230void 231cpu_thread_swapin(struct thread *td) 232{ 233} 234 235void 236cpu_thread_swapout(struct thread *td) 237{ 238} 239 240void 241cpu_sched_exit(td) 242 register struct thread *td; 243{ 244} 245 246void 247cpu_thread_setup(struct thread *td) 248{ 249 250 td->td_pcb = (struct pcb *)(td->td_kstack + 251 td->td_kstack_pages * PAGE_SIZE) - 1; 252 td->td_frame = (struct trapframe *)td->td_pcb - 1; 253} 254 255/* 256 * Initialize machine state (pcb and trap frame) for a new thread about to 257 * upcall. Pu t enough state in the new thread's PCB to get it to go back 258 * userret(), where we can intercept it again to set the return (upcall) 259 * Address and stack, along with those from upcals that are from other sources 260 * such as those generated in thread_userret() itself. 261 */ 262void 263cpu_set_upcall(struct thread *td, struct thread *td0) 264{ 265 struct pcb *pcb2; 266 267 /* Point the pcb to the top of the stack. */ 268 pcb2 = td->td_pcb; 269 270 /* 271 * Copy the upcall pcb. This loads kernel regs. 272 * Those not loaded individually below get their default 273 * values here. 274 * 275 * XXXKSE It might be a good idea to simply skip this as 276 * the values of the other registers may be unimportant. 277 * This would remove any requirement for knowing the KSE 278 * at this time (see the matching comment below for 279 * more analysis) (need a good safe default). 280 */ 281 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2)); 282 pcb2->pcb_flags &= ~PCB_FPUINITDONE; 283 284 /* 285 * Create a new fresh stack for the new thread. 286 * Don't forget to set this stack value into whatever supplies 287 * the address for the fault handlers. 288 * The contexts are filled in at the time we actually DO the 289 * upcall as only then do we know which KSE we got. 290 */ 291 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe)); 292 293 /* 294 * Set registers for trampoline to user mode. Leave space for the 295 * return address on stack. These are the kernel mode register values. 296 */ 297 pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4); 298 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */ 299 pcb2->pcb_rbp = 0; 300 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */ 301 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */ 302 pcb2->pcb_rip = (register_t)fork_trampoline; 303 pcb2->pcb_rflags = PSL_KERNEL; /* ints disabled */ 304 /* 305 * If we didn't copy the pcb, we'd need to do the following registers: 306 * pcb2->pcb_dr*: cloned above. 307 * pcb2->pcb_savefpu: cloned above. 308 * pcb2->pcb_rflags: cloned above. 309 * pcb2->pcb_onfault: cloned above (always NULL here?). 310 * pcb2->pcb_[fg]sbase: cloned above 311 */ 312} 313 314/* 315 * Set that machine state for performing an upcall that has to 316 * be done in thread_userret() so that those upcalls generated 317 * in thread_userret() itself can be done as well. 318 */ 319void 320cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku) 321{ 322 323 /* 324 * Do any extra cleaning that needs to be done. 325 * The thread may have optional components 326 * that are not present in a fresh thread. 327 * This may be a recycled thread so make it look 328 * as though it's newly allocated. 329 */ 330 cpu_thread_clean(td); 331 332 /* 333 * Set the trap frame to point at the beginning of the uts 334 * function. 335 */ 336 td->td_frame->tf_rsp = 337 ((register_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size) & ~0x0f; 338 td->td_frame->tf_rsp -= 8; 339 td->td_frame->tf_rip = (register_t)ku->ku_func; 340 341 /* 342 * Pass the address of the mailbox for this kse to the uts 343 * function as a parameter on the stack. 344 */ 345 td->td_frame->tf_rdi = (register_t)ku->ku_mailbox; 346} 347 348 349/* 350 * Force reset the processor by invalidating the entire address space! 351 */ 352 353#ifdef SMP 354static void 355cpu_reset_proxy() 356{ 357 358 cpu_reset_proxy_active = 1; 359 while (cpu_reset_proxy_active == 1) 360 ; /* Wait for other cpu to see that we've started */ 361 stop_cpus((1<<cpu_reset_proxyid)); 362 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid); 363 DELAY(1000000); 364 cpu_reset_real(); 365} 366#endif 367 368void 369cpu_reset() 370{ 371#ifdef SMP 372 if (smp_active == 0) { 373 cpu_reset_real(); 374 /* NOTREACHED */ 375 } else { 376 377 u_int map; 378 int cnt; 379 printf("cpu_reset called on cpu#%d\n", PCPU_GET(cpuid)); 380 381 map = PCPU_GET(other_cpus) & ~ stopped_cpus; 382 383 if (map != 0) { 384 printf("cpu_reset: Stopping other CPUs\n"); 385 stop_cpus(map); /* Stop all other CPUs */ 386 } 387 388 if (PCPU_GET(cpuid) == 0) { 389 DELAY(1000000); 390 cpu_reset_real(); 391 /* NOTREACHED */ 392 } else { 393 /* We are not BSP (CPU #0) */ 394 395 cpu_reset_proxyid = PCPU_GET(cpuid); 396 cpustop_restartfunc = cpu_reset_proxy; 397 cpu_reset_proxy_active = 0; 398 printf("cpu_reset: Restarting BSP\n"); 399 started_cpus = (1<<0); /* Restart CPU #0 */ 400 401 cnt = 0; 402 while (cpu_reset_proxy_active == 0 && cnt < 10000000) 403 cnt++; /* Wait for BSP to announce restart */ 404 if (cpu_reset_proxy_active == 0) 405 printf("cpu_reset: Failed to restart BSP\n"); 406 enable_intr(); 407 cpu_reset_proxy_active = 2; 408 409 while (1); 410 /* NOTREACHED */ 411 } 412 } 413#else 414 cpu_reset_real(); 415#endif 416} 417 418static void 419cpu_reset_real() 420{ 421 422 /* 423 * Attempt to do a CPU reset via the keyboard controller, 424 * do not turn of the GateA20, as any machine that fails 425 * to do the reset here would then end up in no man's land. 426 */ 427 428 outb(IO_KBD + 4, 0xFE); 429 DELAY(500000); /* wait 0.5 sec to see if that did it */ 430 printf("Keyboard reset did not work, attempting CPU shutdown\n"); 431 DELAY(1000000); /* wait 1 sec for printf to complete */ 432 /* force a shutdown by unmapping entire address space ! */ 433 bzero((caddr_t)PML4map, PAGE_SIZE); 434 435 /* "good night, sweet prince .... <THUNK!>" */ 436 invltlb(); 437 /* NOTREACHED */ 438 while(1); 439} 440 441/* 442 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-)) 443 */ 444static void 445sf_buf_init(void *arg) 446{ 447 struct sf_buf *sf_bufs; 448 int i; 449 450 mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF); 451 SLIST_INIT(&sf_freelist.sf_head); 452 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, 453 M_NOWAIT | M_ZERO); 454 for (i = 0; i < nsfbufs; i++) 455 SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list); 456 sf_buf_alloc_want = 0; 457} 458 459/* 460 * Get an sf_buf from the freelist. Will block if none are available. 461 */ 462struct sf_buf * 463sf_buf_alloc(struct vm_page *m, int pri) 464{ 465 struct sf_buf *sf; 466 int error; 467 468 mtx_lock(&sf_freelist.sf_lock); 469 while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) { 470 sf_buf_alloc_want++; 471 mbstat.sf_allocwait++; 472 error = msleep(&sf_freelist, &sf_freelist.sf_lock, PVM | pri, 473 "sfbufa", 0); 474 sf_buf_alloc_want--; 475 476 /* 477 * If we got a signal, don't risk going back to sleep. 478 */ 479 if (error) 480 break; 481 } 482 if (sf != NULL) { 483 SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list); 484 sf->m = m; 485 nsfbufsused++; 486 nsfbufspeak = imax(nsfbufspeak, nsfbufsused); 487 } 488 mtx_unlock(&sf_freelist.sf_lock); 489 return (sf); 490} 491 492/* 493 * Release resources back to the system. 494 */ 495void 496sf_buf_free(struct sf_buf *sf) 497{ 498 499 mtx_lock(&sf_freelist.sf_lock); 500 SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list); 501 nsfbufsused--; 502 if (sf_buf_alloc_want > 0) 503 wakeup_one(&sf_freelist); 504 mtx_unlock(&sf_freelist.sf_lock); 505} 506 507/* 508 * Software interrupt handler for queued VM system processing. 509 */ 510void 511swi_vm(void *dummy) 512{ 513 if (busdma_swi_pending != 0) 514 busdma_swi(); 515} 516 517/* 518 * Tell whether this address is in some physical memory region. 519 * Currently used by the kernel coredump code in order to avoid 520 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 521 * or other unpredictable behaviour. 522 */ 523 524int 525is_physical_memory(vm_paddr_t addr) 526{ 527 528#ifdef DEV_ISA 529 /* The ISA ``memory hole''. */ 530 if (addr >= 0xa0000 && addr < 0x100000) 531 return 0; 532#endif 533 534 /* 535 * stuff other tests for known memory-mapped devices (PCI?) 536 * here 537 */ 538 539 return 1; 540} 541