vm_machdep.c revision 173615
196845Smarkm/*- 296845Smarkm * Copyright (c) 1982, 1986 The Regents of the University of California. 396845Smarkm * Copyright (c) 1989, 1990 William Jolitz 496845Smarkm * Copyright (c) 1994 John Dyson 596845Smarkm * All rights reserved. 696845Smarkm * 796845Smarkm * This code is derived from software contributed to Berkeley by 896845Smarkm * the Systems Programming Group of the University of Utah Computer 996845Smarkm * Science Department, and William Jolitz. 1096845Smarkm * 1196845Smarkm * Redistribution and use in source and binary forms, with or without 1296845Smarkm * modification, are permitted provided that the following conditions 1396845Smarkm * are met: 1496845Smarkm * 1. Redistributions of source code must retain the above copyright 1596845Smarkm * notice, this list of conditions and the following disclaimer. 1696845Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1796845Smarkm * notice, this list of conditions and the following disclaimer in the 1896845Smarkm * documentation and/or other materials provided with the distribution. 1996845Smarkm * 3. All advertising materials mentioning features or use of this software 2096845Smarkm * must display the following acknowledgement: 2196845Smarkm * This product includes software developed by the University of 2296845Smarkm * California, Berkeley and its contributors. 2396845Smarkm * 4. Neither the name of the University nor the names of its contributors 2496845Smarkm * may be used to endorse or promote products derived from this software 2596845Smarkm * without specific prior written permission. 2696845Smarkm * 27263104Seadler * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28263104Seadler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2996845Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3096845Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3196845Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32263104Seadler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3396845Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3496845Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3596845Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3696845Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3796845Smarkm * SUCH DAMAGE. 3896845Smarkm * 3996845Smarkm * from: @(#)vm_machdep.c 7.3 (Berkeley) 5/13/91 4096845Smarkm * Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$ 4196863Sru */ 4296845Smarkm 4396845Smarkm#include <sys/cdefs.h> 4496845Smarkm__FBSDID("$FreeBSD: head/sys/amd64/amd64/vm_machdep.c 173615 2007-11-14 20:21:54Z marcel $"); 4596863Sru 4696845Smarkm#include "opt_isa.h" 4796845Smarkm#include "opt_cpu.h" 4896845Smarkm#include "opt_compat.h" 4996845Smarkm 5096845Smarkm#include <sys/param.h> 5196845Smarkm#include <sys/systm.h> 5296845Smarkm#include <sys/bio.h> 5396845Smarkm#include <sys/buf.h> 5496845Smarkm#include <sys/kse.h> 5596845Smarkm#include <sys/kernel.h> 5696845Smarkm#include <sys/ktr.h> 5796845Smarkm#include <sys/lock.h> 5896863Sru#include <sys/malloc.h> 5996845Smarkm#include <sys/mbuf.h> 6096845Smarkm#include <sys/mutex.h> 6196845Smarkm#include <sys/pioctl.h> 6296845Smarkm#include <sys/proc.h> 6396845Smarkm#include <sys/sf_buf.h> 6496845Smarkm#include <sys/smp.h> 6596863Sru#include <sys/sysctl.h> 6696863Sru#include <sys/unistd.h> 6796845Smarkm#include <sys/vnode.h> 6896845Smarkm#include <sys/vmmeter.h> 6996845Smarkm 7096845Smarkm#include <machine/cpu.h> 7196845Smarkm#include <machine/md_var.h> 7296863Sru#include <machine/pcb.h> 7396863Sru#include <machine/specialreg.h> 7496845Smarkm 7596845Smarkm#include <vm/vm.h> 7696845Smarkm#include <vm/vm_extern.h> 7796845Smarkm#include <vm/vm_kern.h> 7896845Smarkm#include <vm/vm_page.h> 7996845Smarkm#include <vm/vm_map.h> 8096845Smarkm#include <vm/vm_param.h> 8196845Smarkm 8296845Smarkm#include <amd64/isa/isa.h> 8396863Sru 8496845Smarkm#ifdef COMPAT_IA32 8596845Smarkm 8696845Smarkmextern struct sysentvec ia32_freebsd_sysvec; 8796845Smarkm 8896845Smarkm#endif 8996845Smarkm 9096845Smarkmstatic void cpu_reset_real(void); 9196845Smarkm#ifdef SMP 9296845Smarkmstatic void cpu_reset_proxy(void); 9396858Srustatic u_int cpu_reset_proxyid; 9496845Smarkmstatic volatile u_int cpu_reset_proxy_active; 9596845Smarkm#endif 9696845Smarkm 9796845Smarkm/* 9896858Sru * Finish a fork operation, with process p2 nearly set up. 9996858Sru * Copy and update the pcb, set up the stack so that the child 10096858Sru * ready to run and return to user mode. 101263104Seadler */ 102263104Seadlervoid 103263104Seadlercpu_fork(td1, p2, td2, flags) 10496845Smarkm register struct thread *td1; 10596845Smarkm register struct proc *p2; 10696845Smarkm struct thread *td2; 10796845Smarkm int flags; 10896845Smarkm{ 10996845Smarkm register struct proc *p1; 11096845Smarkm struct pcb *pcb2; 11196845Smarkm struct mdproc *mdp2; 11296845Smarkm 11396845Smarkm p1 = td1->td_proc; 11496845Smarkm if ((flags & RFPROC) == 0) 11596845Smarkm return; 11696845Smarkm 117140368Sru /* Ensure that p1's pcb is up to date. */ 11896845Smarkm fpuexit(td1); 11996845Smarkm 12096863Sru /* Point the pcb to the top of the stack */ 12196863Sru pcb2 = (struct pcb *)(td2->td_kstack + 12296845Smarkm td2->td_kstack_pages * PAGE_SIZE) - 1; 12396863Sru td2->td_pcb = pcb2; 12496845Smarkm 12596845Smarkm /* Copy p1's pcb */ 12696845Smarkm bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 12796845Smarkm 12896863Sru /* Point mdproc and then copy over td1's contents */ 12996863Sru mdp2 = &p2->p_md; 13096863Sru bcopy(&p1->p_md, mdp2, sizeof(*mdp2)); 13196863Sru 13296863Sru /* 13396863Sru * Create a new fresh stack for the new process. 13496863Sru * Copy the trap frame for the return to user mode as if from a 13596863Sru * syscall. This copies most of the user mode register values. 136263104Seadler */ 137 td2->td_frame = (struct trapframe *)td2->td_pcb - 1; 138 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 139 140 td2->td_frame->tf_rax = 0; /* Child returns zero */ 141 td2->td_frame->tf_rflags &= ~PSL_C; /* success */ 142 td2->td_frame->tf_rdx = 1; 143 144 /* 145 * If the parent process has the trap bit set (i.e. a debugger had 146 * single stepped the process to the system call), we need to clear 147 * the trap flag from the new frame unless the debugger had set PF_FORK 148 * on the parent. Otherwise, the child will receive a (likely 149 * unexpected) SIGTRAP when it executes the first instruction after 150 * returning to userland. 151 */ 152 if ((p1->p_pfsflags & PF_FORK) == 0) 153 td2->td_frame->tf_rflags &= ~PSL_T; 154 155 /* 156 * Set registers for trampoline to user mode. Leave space for the 157 * return address on stack. These are the kernel mode register values. 158 */ 159 pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4); 160 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */ 161 pcb2->pcb_rbp = 0; 162 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *); 163 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */ 164 pcb2->pcb_rip = (register_t)fork_trampoline; 165 /*- 166 * pcb2->pcb_dr*: cloned above. 167 * pcb2->pcb_savefpu: cloned above. 168 * pcb2->pcb_flags: cloned above. 169 * pcb2->pcb_onfault: cloned above (always NULL here?). 170 * pcb2->pcb_[fg]sbase: cloned above 171 */ 172 173 /* Setup to release spin count in fork_exit(). */ 174 td2->td_md.md_spinlock_count = 1; 175 td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I; 176 177 /* 178 * Now, cpu_switch() can schedule the new process. 179 * pcb_rsp is loaded pointing to the cpu_switch() stack frame 180 * containing the return address when exiting cpu_switch. 181 * This will normally be to fork_trampoline(), which will have 182 * %ebx loaded with the new proc's pointer. fork_trampoline() 183 * will set up a stack to call fork_return(p, frame); to complete 184 * the return to user-mode. 185 */ 186} 187 188/* 189 * Intercept the return address from a freshly forked process that has NOT 190 * been scheduled yet. 191 * 192 * This is needed to make kernel threads stay in kernel mode. 193 */ 194void 195cpu_set_fork_handler(td, func, arg) 196 struct thread *td; 197 void (*func)(void *); 198 void *arg; 199{ 200 /* 201 * Note that the trap frame follows the args, so the function 202 * is really called like this: func(arg, frame); 203 */ 204 td->td_pcb->pcb_r12 = (long) func; /* function */ 205 td->td_pcb->pcb_rbx = (long) arg; /* first arg */ 206} 207 208void 209cpu_exit(struct thread *td) 210{ 211} 212 213void 214cpu_thread_exit(struct thread *td) 215{ 216 217 if (td == PCPU_GET(fpcurthread)) 218 fpudrop(); 219 220 /* Disable any hardware breakpoints. */ 221 if (td->td_pcb->pcb_flags & PCB_DBREGS) { 222 reset_dbregs(); 223 td->td_pcb->pcb_flags &= ~PCB_DBREGS; 224 } 225} 226 227void 228cpu_thread_clean(struct thread *td) 229{ 230} 231 232void 233cpu_thread_swapin(struct thread *td) 234{ 235} 236 237void 238cpu_thread_swapout(struct thread *td) 239{ 240} 241 242void 243cpu_thread_alloc(struct thread *td) 244{ 245 246 td->td_pcb = (struct pcb *)(td->td_kstack + 247 td->td_kstack_pages * PAGE_SIZE) - 1; 248 td->td_frame = (struct trapframe *)td->td_pcb - 1; 249} 250 251void 252cpu_thread_free(struct thread *td) 253{ 254} 255 256/* 257 * Initialize machine state (pcb and trap frame) for a new thread about to 258 * upcall. Put enough state in the new thread's PCB to get it to go back 259 * userret(), where we can intercept it again to set the return (upcall) 260 * Address and stack, along with those from upcals that are from other sources 261 * such as those generated in thread_userret() itself. 262 */ 263void 264cpu_set_upcall(struct thread *td, struct thread *td0) 265{ 266 struct pcb *pcb2; 267 268 /* Point the pcb to the top of the stack. */ 269 pcb2 = td->td_pcb; 270 271 /* 272 * Copy the upcall pcb. This loads kernel regs. 273 * Those not loaded individually below get their default 274 * values here. 275 * 276 * XXXKSE It might be a good idea to simply skip this as 277 * the values of the other registers may be unimportant. 278 * This would remove any requirement for knowing the KSE 279 * at this time (see the matching comment below for 280 * more analysis) (need a good safe default). 281 */ 282 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2)); 283 pcb2->pcb_flags &= ~PCB_FPUINITDONE; 284 285 /* 286 * Create a new fresh stack for the new thread. 287 * Don't forget to set this stack value into whatever supplies 288 * the address for the fault handlers. 289 * The contexts are filled in at the time we actually DO the 290 * upcall as only then do we know which KSE we got. 291 */ 292 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe)); 293 294 /* 295 * Set registers for trampoline to user mode. Leave space for the 296 * return address on stack. These are the kernel mode register values. 297 */ 298 pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4); 299 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */ 300 pcb2->pcb_rbp = 0; 301 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */ 302 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */ 303 pcb2->pcb_rip = (register_t)fork_trampoline; 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_onfault: cloned above (always NULL here?). 309 * pcb2->pcb_[fg]sbase: cloned above 310 */ 311 312 /* Setup to release spin count in fork_exit(). */ 313 td->td_md.md_spinlock_count = 1; 314 td->td_md.md_saved_flags = PSL_KERNEL | PSL_I; 315} 316 317/* 318 * Set that machine state for performing an upcall that has to 319 * be done in thread_userret() so that those upcalls generated 320 * in thread_userret() itself can be done as well. 321 */ 322void 323cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg, 324 stack_t *stack) 325{ 326 327 /* 328 * Do any extra cleaning that needs to be done. 329 * The thread may have optional components 330 * that are not present in a fresh thread. 331 * This may be a recycled thread so make it look 332 * as though it's newly allocated. 333 */ 334 cpu_thread_clean(td); 335 336#ifdef COMPAT_IA32 337 if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) { 338 /* 339 * Set the trap frame to point at the beginning of the uts 340 * function. 341 */ 342 td->td_frame->tf_rbp = 0; 343 td->td_frame->tf_rsp = 344 (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4; 345 td->td_frame->tf_rip = (uintptr_t)entry; 346 347 /* 348 * Pass the address of the mailbox for this kse to the uts 349 * function as a parameter on the stack. 350 */ 351 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)), 352 (uint32_t)(uintptr_t)arg); 353 354 return; 355 } 356#endif 357 358 /* 359 * Set the trap frame to point at the beginning of the uts 360 * function. 361 */ 362 td->td_frame->tf_rbp = 0; 363 td->td_frame->tf_rsp = 364 ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f; 365 td->td_frame->tf_rsp -= 8; 366 td->td_frame->tf_rip = (register_t)entry; 367 368 /* 369 * Pass the address of the mailbox for this kse to the uts 370 * function as a parameter on the stack. 371 */ 372 td->td_frame->tf_rdi = (register_t)arg; 373} 374 375int 376cpu_set_user_tls(struct thread *td, void *tls_base) 377{ 378 379 if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS) 380 return (EINVAL); 381 382#ifdef COMPAT_IA32 383 if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) { 384 if (td == curthread) { 385 critical_enter(); 386 td->td_pcb->pcb_gsbase = (register_t)tls_base; 387 wrmsr(MSR_KGSBASE, td->td_pcb->pcb_gsbase); 388 critical_exit(); 389 } else { 390 td->td_pcb->pcb_gsbase = (register_t)tls_base; 391 } 392 return (0); 393 } 394#endif 395 if (td == curthread) { 396 critical_enter(); 397 td->td_pcb->pcb_fsbase = (register_t)tls_base; 398 wrmsr(MSR_FSBASE, td->td_pcb->pcb_fsbase); 399 critical_exit(); 400 } else { 401 td->td_pcb->pcb_fsbase = (register_t)tls_base; 402 } 403 return (0); 404} 405 406#ifdef SMP 407static void 408cpu_reset_proxy() 409{ 410 411 cpu_reset_proxy_active = 1; 412 while (cpu_reset_proxy_active == 1) 413 ; /* Wait for other cpu to see that we've started */ 414 stop_cpus((1<<cpu_reset_proxyid)); 415 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid); 416 DELAY(1000000); 417 cpu_reset_real(); 418} 419#endif 420 421void 422cpu_reset() 423{ 424#ifdef SMP 425 u_int cnt, map; 426 427 if (smp_active) { 428 map = PCPU_GET(other_cpus) & ~stopped_cpus; 429 if (map != 0) { 430 printf("cpu_reset: Stopping other CPUs\n"); 431 stop_cpus(map); 432 } 433 434 if (PCPU_GET(cpuid) != 0) { 435 cpu_reset_proxyid = PCPU_GET(cpuid); 436 cpustop_restartfunc = cpu_reset_proxy; 437 cpu_reset_proxy_active = 0; 438 printf("cpu_reset: Restarting BSP\n"); 439 440 /* Restart CPU #0. */ 441 atomic_store_rel_int(&started_cpus, 1 << 0); 442 443 cnt = 0; 444 while (cpu_reset_proxy_active == 0 && cnt < 10000000) 445 cnt++; /* Wait for BSP to announce restart */ 446 if (cpu_reset_proxy_active == 0) 447 printf("cpu_reset: Failed to restart BSP\n"); 448 enable_intr(); 449 cpu_reset_proxy_active = 2; 450 451 while (1); 452 /* NOTREACHED */ 453 } 454 455 DELAY(1000000); 456 } 457#endif 458 cpu_reset_real(); 459 /* NOTREACHED */ 460} 461 462static void 463cpu_reset_real() 464{ 465 struct region_descriptor null_idt; 466 int b; 467 468 disable_intr(); 469 470 /* 471 * Attempt to do a CPU reset via the keyboard controller, 472 * do not turn off GateA20, as any machine that fails 473 * to do the reset here would then end up in no man's land. 474 */ 475 outb(IO_KBD + 4, 0xFE); 476 DELAY(500000); /* wait 0.5 sec to see if that did it */ 477 478 /* 479 * Attempt to force a reset via the Reset Control register at 480 * I/O port 0xcf9. Bit 2 forces a system reset when it is 481 * written as 1. Bit 1 selects the type of reset to attempt: 482 * 0 selects a "soft" reset, and 1 selects a "hard" reset. We 483 * try to do a "soft" reset first, and then a "hard" reset. 484 */ 485 outb(0xcf9, 0x2); 486 outb(0xcf9, 0x6); 487 DELAY(500000); /* wait 0.5 sec to see if that did it */ 488 489 /* 490 * Attempt to force a reset via the Fast A20 and Init register 491 * at I/O port 0x92. Bit 1 serves as an alternate A20 gate. 492 * Bit 0 asserts INIT# when set to 1. We are careful to only 493 * preserve bit 1 while setting bit 0. We also must clear bit 494 * 0 before setting it if it isn't already clear. 495 */ 496 b = inb(0x92); 497 if (b != 0xff) { 498 if ((b & 0x1) != 0) 499 outb(0x92, b & 0xfe); 500 outb(0x92, b | 0x1); 501 DELAY(500000); /* wait 0.5 sec to see if that did it */ 502 } 503 504 printf("No known reset method worked, attempting CPU shutdown\n"); 505 DELAY(1000000); /* wait 1 sec for printf to complete */ 506 507 /* Wipe the IDT. */ 508 null_idt.rd_limit = 0; 509 null_idt.rd_base = 0; 510 lidt(&null_idt); 511 512 /* "good night, sweet prince .... <THUNK!>" */ 513 breakpoint(); 514 515 /* NOTREACHED */ 516 while(1); 517} 518 519/* 520 * Allocate an sf_buf for the given vm_page. On this machine, however, there 521 * is no sf_buf object. Instead, an opaque pointer to the given vm_page is 522 * returned. 523 */ 524struct sf_buf * 525sf_buf_alloc(struct vm_page *m, int pri) 526{ 527 528 return ((struct sf_buf *)m); 529} 530 531/* 532 * Free the sf_buf. In fact, do nothing because there are no resources 533 * associated with the sf_buf. 534 */ 535void 536sf_buf_free(struct sf_buf *sf) 537{ 538} 539 540/* 541 * Software interrupt handler for queued VM system processing. 542 */ 543void 544swi_vm(void *dummy) 545{ 546 if (busdma_swi_pending != 0) 547 busdma_swi(); 548} 549 550/* 551 * Tell whether this address is in some physical memory region. 552 * Currently used by the kernel coredump code in order to avoid 553 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 554 * or other unpredictable behaviour. 555 */ 556 557int 558is_physical_memory(vm_paddr_t addr) 559{ 560 561#ifdef DEV_ISA 562 /* The ISA ``memory hole''. */ 563 if (addr >= 0xa0000 && addr < 0x100000) 564 return 0; 565#endif 566 567 /* 568 * stuff other tests for known memory-mapped devices (PCI?) 569 * here 570 */ 571 572 return 1; 573} 574