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