vm_machdep.c revision 140257
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 140257 2005-01-14 20:16:41Z jhb $"); 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/vnode.h> 66#include <sys/vmmeter.h> 67 68#include <machine/cpu.h> 69#include <machine/md_var.h> 70#include <machine/pcb.h> 71 72#include <vm/vm.h> 73#include <vm/vm_extern.h> 74#include <vm/vm_kern.h> 75#include <vm/vm_page.h> 76#include <vm/vm_map.h> 77#include <vm/vm_param.h> 78 79#include <amd64/isa/isa.h> 80 81static void cpu_reset_real(void); 82#ifdef SMP 83static void cpu_reset_proxy(void); 84static u_int cpu_reset_proxyid; 85static volatile u_int cpu_reset_proxy_active; 86#endif 87 88/* 89 * Finish a fork operation, with process p2 nearly set up. 90 * Copy and update the pcb, set up the stack so that the child 91 * ready to run and return to user mode. 92 */ 93void 94cpu_fork(td1, p2, td2, flags) 95 register struct thread *td1; 96 register struct proc *p2; 97 struct thread *td2; 98 int flags; 99{ 100 register struct proc *p1; 101 struct pcb *pcb2; 102 struct mdproc *mdp2; 103 104 p1 = td1->td_proc; 105 if ((flags & RFPROC) == 0) 106 return; 107 108 /* Ensure that p1's pcb is up to date. */ 109 fpuexit(td1); 110 111 /* Point the pcb to the top of the stack */ 112 pcb2 = (struct pcb *)(td2->td_kstack + 113 td2->td_kstack_pages * PAGE_SIZE) - 1; 114 td2->td_pcb = pcb2; 115 116 /* Copy p1's pcb */ 117 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 118 119 /* Point mdproc and then copy over td1's contents */ 120 mdp2 = &p2->p_md; 121 bcopy(&p1->p_md, mdp2, sizeof(*mdp2)); 122 123 /* 124 * Create a new fresh stack for the new process. 125 * Copy the trap frame for the return to user mode as if from a 126 * syscall. This copies most of the user mode register values. 127 */ 128 td2->td_frame = (struct trapframe *)td2->td_pcb - 1; 129 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 130 131 td2->td_frame->tf_rax = 0; /* Child returns zero */ 132 td2->td_frame->tf_rflags &= ~PSL_C; /* success */ 133 td2->td_frame->tf_rdx = 1; 134 135 /* 136 * Set registers for trampoline to user mode. Leave space for the 137 * return address on stack. These are the kernel mode register values. 138 */ 139 pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4); 140 pcb2->pcb_r12 = (register_t)fork_return; /* fork_trampoline argument */ 141 pcb2->pcb_rbp = 0; 142 pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *); 143 pcb2->pcb_rbx = (register_t)td2; /* fork_trampoline argument */ 144 pcb2->pcb_rip = (register_t)fork_trampoline; 145 pcb2->pcb_rflags = td2->td_frame->tf_rflags & ~PSL_I; /* ints disabled */ 146 /*- 147 * pcb2->pcb_dr*: cloned above. 148 * pcb2->pcb_savefpu: cloned above. 149 * pcb2->pcb_flags: cloned above. 150 * pcb2->pcb_onfault: cloned above (always NULL here?). 151 * pcb2->pcb_[fg]sbase: cloned above 152 */ 153 154 /* 155 * Now, cpu_switch() can schedule the new process. 156 * pcb_rsp is loaded pointing to the cpu_switch() stack frame 157 * containing the return address when exiting cpu_switch. 158 * This will normally be to fork_trampoline(), which will have 159 * %ebx loaded with the new proc's pointer. fork_trampoline() 160 * will set up a stack to call fork_return(p, frame); to complete 161 * the return to user-mode. 162 */ 163} 164 165/* 166 * Intercept the return address from a freshly forked process that has NOT 167 * been scheduled yet. 168 * 169 * This is needed to make kernel threads stay in kernel mode. 170 */ 171void 172cpu_set_fork_handler(td, func, arg) 173 struct thread *td; 174 void (*func)(void *); 175 void *arg; 176{ 177 /* 178 * Note that the trap frame follows the args, so the function 179 * is really called like this: func(arg, frame); 180 */ 181 td->td_pcb->pcb_r12 = (long) func; /* function */ 182 td->td_pcb->pcb_rbx = (long) arg; /* first arg */ 183} 184 185void 186cpu_exit(struct thread *td) 187{ 188} 189 190void 191cpu_thread_exit(struct thread *td) 192{ 193 struct pcb *pcb = td->td_pcb; 194 195 if (td == PCPU_GET(fpcurthread)) 196 fpudrop(); 197 if (pcb->pcb_flags & PCB_DBREGS) { 198 /* disable all hardware breakpoints */ 199 reset_dbregs(); 200 pcb->pcb_flags &= ~PCB_DBREGS; 201 } 202} 203 204void 205cpu_thread_clean(struct thread *td) 206{ 207} 208 209void 210cpu_thread_swapin(struct thread *td) 211{ 212} 213 214void 215cpu_thread_swapout(struct thread *td) 216{ 217} 218 219void 220cpu_thread_setup(struct thread *td) 221{ 222 223 td->td_pcb = (struct pcb *)(td->td_kstack + 224 td->td_kstack_pages * PAGE_SIZE) - 1; 225 td->td_frame = (struct trapframe *)td->td_pcb - 1; 226} 227 228/* 229 * Initialize machine state (pcb and trap frame) for a new thread about to 230 * upcall. Pu t enough state in the new thread's PCB to get it to go back 231 * userret(), where we can intercept it again to set the return (upcall) 232 * Address and stack, along with those from upcals that are from other sources 233 * such as those generated in thread_userret() itself. 234 */ 235void 236cpu_set_upcall(struct thread *td, struct thread *td0) 237{ 238 struct pcb *pcb2; 239 240 /* Point the pcb to the top of the stack. */ 241 pcb2 = td->td_pcb; 242 243 /* 244 * Copy the upcall pcb. This loads kernel regs. 245 * Those not loaded individually below get their default 246 * values here. 247 * 248 * XXXKSE It might be a good idea to simply skip this as 249 * the values of the other registers may be unimportant. 250 * This would remove any requirement for knowing the KSE 251 * at this time (see the matching comment below for 252 * more analysis) (need a good safe default). 253 */ 254 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2)); 255 pcb2->pcb_flags &= ~PCB_FPUINITDONE; 256 257 /* 258 * Create a new fresh stack for the new thread. 259 * Don't forget to set this stack value into whatever supplies 260 * the address for the fault handlers. 261 * The contexts are filled in at the time we actually DO the 262 * upcall as only then do we know which KSE we got. 263 */ 264 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe)); 265 266 /* 267 * Set registers for trampoline to user mode. Leave space for the 268 * return address on stack. These are the kernel mode register values. 269 */ 270 pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4); 271 pcb2->pcb_r12 = (register_t)fork_return; /* trampoline arg */ 272 pcb2->pcb_rbp = 0; 273 pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *); /* trampoline arg */ 274 pcb2->pcb_rbx = (register_t)td; /* trampoline arg */ 275 pcb2->pcb_rip = (register_t)fork_trampoline; 276 pcb2->pcb_rflags = PSL_KERNEL; /* ints disabled */ 277 /* 278 * If we didn't copy the pcb, we'd need to do the following registers: 279 * pcb2->pcb_dr*: cloned above. 280 * pcb2->pcb_savefpu: cloned above. 281 * pcb2->pcb_rflags: cloned above. 282 * pcb2->pcb_onfault: cloned above (always NULL here?). 283 * pcb2->pcb_[fg]sbase: cloned above 284 */ 285} 286 287/* 288 * Set that machine state for performing an upcall that has to 289 * be done in thread_userret() so that those upcalls generated 290 * in thread_userret() itself can be done as well. 291 */ 292void 293cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku) 294{ 295 296 /* 297 * Do any extra cleaning that needs to be done. 298 * The thread may have optional components 299 * that are not present in a fresh thread. 300 * This may be a recycled thread so make it look 301 * as though it's newly allocated. 302 */ 303 cpu_thread_clean(td); 304 305 /* 306 * Set the trap frame to point at the beginning of the uts 307 * function. 308 */ 309 td->td_frame->tf_rbp = 0; 310 td->td_frame->tf_rsp = 311 ((register_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size) & ~0x0f; 312 td->td_frame->tf_rsp -= 8; 313 td->td_frame->tf_rbp = 0; 314 td->td_frame->tf_rip = (register_t)ku->ku_func; 315 316 /* 317 * Pass the address of the mailbox for this kse to the uts 318 * function as a parameter on the stack. 319 */ 320 td->td_frame->tf_rdi = (register_t)ku->ku_mailbox; 321} 322 323#ifdef SMP 324static void 325cpu_reset_proxy() 326{ 327 328 cpu_reset_proxy_active = 1; 329 while (cpu_reset_proxy_active == 1) 330 ; /* Wait for other cpu to see that we've started */ 331 stop_cpus((1<<cpu_reset_proxyid)); 332 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid); 333 DELAY(1000000); 334 cpu_reset_real(); 335} 336#endif 337 338void 339cpu_reset() 340{ 341#ifdef SMP 342 u_int cnt, map; 343 344 if (smp_active) { 345 map = PCPU_GET(other_cpus) & ~stopped_cpus; 346 if (map != 0) { 347 printf("cpu_reset: Stopping other CPUs\n"); 348 stop_cpus(map); 349 } 350 351 if (PCPU_GET(cpuid) != 0) { 352 cpu_reset_proxyid = PCPU_GET(cpuid); 353 cpustop_restartfunc = cpu_reset_proxy; 354 cpu_reset_proxy_active = 0; 355 printf("cpu_reset: Restarting BSP\n"); 356 started_cpus = (1<<0); /* Restart CPU #0 */ 357 358 cnt = 0; 359 while (cpu_reset_proxy_active == 0 && cnt < 10000000) 360 cnt++; /* Wait for BSP to announce restart */ 361 if (cpu_reset_proxy_active == 0) 362 printf("cpu_reset: Failed to restart BSP\n"); 363 enable_intr(); 364 cpu_reset_proxy_active = 2; 365 366 while (1); 367 /* NOTREACHED */ 368 } 369 370 DELAY(1000000); 371 } 372#endif 373 cpu_reset_real(); 374 /* NOTREACHED */ 375} 376 377static void 378cpu_reset_real() 379{ 380 381 /* 382 * Attempt to do a CPU reset via the keyboard controller, 383 * do not turn off GateA20, as any machine that fails 384 * to do the reset here would then end up in no man's land. 385 */ 386 outb(IO_KBD + 4, 0xFE); 387 DELAY(500000); /* wait 0.5 sec to see if that did it */ 388 printf("Keyboard reset did not work, attempting CPU shutdown\n"); 389 DELAY(1000000); /* wait 1 sec for printf to complete */ 390 391 /* Force a shutdown by unmapping entire address space. */ 392 bzero((caddr_t)PML4map, PAGE_SIZE); 393 394 /* "good night, sweet prince .... <THUNK!>" */ 395 invltlb(); 396 /* NOTREACHED */ 397 while(1); 398} 399 400/* 401 * Allocate an sf_buf for the given vm_page. On this machine, however, there 402 * is no sf_buf object. Instead, an opaque pointer to the given vm_page is 403 * returned. 404 */ 405struct sf_buf * 406sf_buf_alloc(struct vm_page *m, int pri) 407{ 408 409 return ((struct sf_buf *)m); 410} 411 412/* 413 * Free the sf_buf. In fact, do nothing because there are no resources 414 * associated with the sf_buf. 415 */ 416void 417sf_buf_free(struct sf_buf *sf) 418{ 419} 420 421/* 422 * Software interrupt handler for queued VM system processing. 423 */ 424void 425swi_vm(void *dummy) 426{ 427 if (busdma_swi_pending != 0) 428 busdma_swi(); 429} 430 431/* 432 * Tell whether this address is in some physical memory region. 433 * Currently used by the kernel coredump code in order to avoid 434 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 435 * or other unpredictable behaviour. 436 */ 437 438int 439is_physical_memory(vm_paddr_t addr) 440{ 441 442#ifdef DEV_ISA 443 /* The ISA ``memory hole''. */ 444 if (addr >= 0xa0000 && addr < 0x100000) 445 return 0; 446#endif 447 448 /* 449 * stuff other tests for known memory-mapped devices (PCI?) 450 * here 451 */ 452 453 return 1; 454} 455