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