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$"); 45 46 #include "opt_isa.h" 47 #include "opt_npx.h" 48 #ifdef PC98 49 #include "opt_pc98.h" 50 #endif 51 #include "opt_reset.h" 52 #include "opt_cpu.h" 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/bio.h> 57 #include <sys/buf.h> 58 #include <sys/kse.h> 59 #include <sys/kernel.h> 60 #include <sys/ktr.h> 61 #include <sys/lock.h> 62 #include <sys/malloc.h> 63 #include <sys/mbuf.h> 64 #include <sys/mutex.h> 65 #include <sys/pioctl.h> 66 #include <sys/proc.h> 67 #include <sys/sf_buf.h> 68 #include <sys/smp.h> 69 #include <sys/sysctl.h> 70 #include <sys/unistd.h> 71 #include <sys/vnode.h> 72 #include <sys/vmmeter.h> 73 74 #include <machine/cpu.h> 75 #include <machine/cputypes.h> 76 #include <machine/md_var.h> 77 #include <machine/pcb.h> 78 #include <machine/pcb_ext.h> 79 #include <machine/vm86.h> 80 81 #ifdef CPU_ELAN 82 #include <machine/elan_mmcr.h> 83 #endif 84 85 #include <vm/vm.h> 86 #include <vm/vm_extern.h> 87 #include <vm/vm_kern.h> 88 #include <vm/vm_page.h> 89 #include <vm/vm_map.h> 90 #include <vm/vm_param.h> 91 92 #ifdef PC98 93 #include <pc98/pc98/pc98.h> 94 #else 95 #include <i386/isa/isa.h> 96 #endif 97 98 #ifndef NSFBUFS 99 #define NSFBUFS (512 + maxusers * 16) 100 #endif 101 102 static void cpu_reset_real(void); 103 #ifdef SMP 104 static void cpu_reset_proxy(void); 105 static u_int cpu_reset_proxyid; 106 static volatile u_int cpu_reset_proxy_active; 107 #endif 108 static void sf_buf_init(void *arg); 109 SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL) 110 111 LIST_HEAD(sf_head, sf_buf); 112 113 /* 114 * A hash table of active sendfile(2) buffers 115 */ 116 static struct sf_head *sf_buf_active; 117 static u_long sf_buf_hashmask; 118 119 #define SF_BUF_HASH(m) (((m) - vm_page_array) & sf_buf_hashmask) 120 121 static TAILQ_HEAD(, sf_buf) sf_buf_freelist; 122 static u_int sf_buf_alloc_want; 123 124 /* 125 * A lock used to synchronize access to the hash table and free list 126 */ 127 static struct mtx sf_buf_lock; 128 129 extern int _ucodesel, _udatasel; 130 131 /* 132 * Finish a fork operation, with process p2 nearly set up. 133 * Copy and update the pcb, set up the stack so that the child 134 * ready to run and return to user mode. 135 */ 136 void 137 cpu_fork(td1, p2, td2, flags) 138 register struct thread *td1; 139 register struct proc *p2; 140 struct thread *td2; 141 int flags; 142 { 143 register struct proc *p1; 144 struct pcb *pcb2; 145 struct mdproc *mdp2; 146 #ifdef DEV_NPX 147 register_t savecrit; 148 #endif 149 150 p1 = td1->td_proc; 151 if ((flags & RFPROC) == 0) { 152 if ((flags & RFMEM) == 0) { 153 /* unshare user LDT */ 154 struct mdproc *mdp1 = &p1->p_md; 155 struct proc_ldt *pldt = mdp1->md_ldt; 156 if (pldt && pldt->ldt_refcnt > 1) { 157 pldt = user_ldt_alloc(mdp1, pldt->ldt_len); 158 if (pldt == NULL) 159 panic("could not copy LDT"); 160 mdp1->md_ldt = pldt; 161 set_user_ldt(mdp1); 162 user_ldt_free(td1); 163 } 164 } 165 return; 166 } 167 168 /* Ensure that p1's pcb is up to date. */ 169 #ifdef DEV_NPX 170 if (td1 == curthread) 171 td1->td_pcb->pcb_gs = rgs(); 172 savecrit = intr_disable(); 173 if (PCPU_GET(fpcurthread) == td1) 174 npxsave(&td1->td_pcb->pcb_save); 175 intr_restore(savecrit); 176 #endif 177 178 /* Point the pcb to the top of the stack */ 179 pcb2 = (struct pcb *)(td2->td_kstack + 180 td2->td_kstack_pages * PAGE_SIZE) - 1; 181 td2->td_pcb = pcb2; 182 183 /* Copy p1's pcb */ 184 bcopy(td1->td_pcb, pcb2, sizeof(*pcb2)); 185 186 /* Point mdproc and then copy over td1's contents */ 187 mdp2 = &p2->p_md; 188 bcopy(&p1->p_md, mdp2, sizeof(*mdp2)); 189 190 /* 191 * Create a new fresh stack for the new process. 192 * Copy the trap frame for the return to user mode as if from a 193 * syscall. This copies most of the user mode register values. 194 * The -16 is so we can expand the trapframe if we go to vm86. 195 */ 196 td2->td_frame = (struct trapframe *)((caddr_t)td2->td_pcb - 16) - 1; 197 bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe)); 198 199 td2->td_frame->tf_eax = 0; /* Child returns zero */ 200 td2->td_frame->tf_eflags &= ~PSL_C; /* success */ 201 td2->td_frame->tf_edx = 1; 202 203 /* 204 * If the parent process has the trap bit set (i.e. a debugger had 205 * single stepped the process to the system call), we need to clear 206 * the trap flag from the new frame unless the debugger had set PF_FORK 207 * on the parent. Otherwise, the child will receive a (likely 208 * unexpected) SIGTRAP when it executes the first instruction after 209 * returning to userland. 210 */ 211 if ((p1->p_pfsflags & PF_FORK) == 0) 212 td2->td_frame->tf_eflags &= ~PSL_T; 213 214 /* 215 * Set registers for trampoline to user mode. Leave space for the 216 * return address on stack. These are the kernel mode register values. 217 */ 218 #ifdef PAE 219 pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdpt); 220 #else 221 pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir); 222 #endif 223 pcb2->pcb_edi = 0; 224 pcb2->pcb_esi = (int)fork_return; /* fork_trampoline argument */ 225 pcb2->pcb_ebp = 0; 226 pcb2->pcb_esp = (int)td2->td_frame - sizeof(void *); 227 pcb2->pcb_ebx = (int)td2; /* fork_trampoline argument */ 228 pcb2->pcb_eip = (int)fork_trampoline; 229 pcb2->pcb_psl = PSL_KERNEL; /* ints disabled */ 230 pcb2->pcb_gs = rgs(); 231 /*- 232 * pcb2->pcb_dr*: cloned above. 233 * pcb2->pcb_savefpu: cloned above. 234 * pcb2->pcb_flags: cloned above. 235 * pcb2->pcb_onfault: cloned above (always NULL here?). 236 * pcb2->pcb_gs: cloned above. 237 * pcb2->pcb_ext: cleared below. 238 */ 239 240 /* 241 * XXX don't copy the i/o pages. this should probably be fixed. 242 */ 243 pcb2->pcb_ext = 0; 244 245 /* Copy the LDT, if necessary. */ 246 mtx_lock_spin(&sched_lock); 247 if (mdp2->md_ldt != NULL) { 248 if (flags & RFMEM) { 249 mdp2->md_ldt->ldt_refcnt++; 250 } else { 251 mdp2->md_ldt = user_ldt_alloc(mdp2, 252 mdp2->md_ldt->ldt_len); 253 if (mdp2->md_ldt == NULL) 254 panic("could not copy LDT"); 255 } 256 } 257 mtx_unlock_spin(&sched_lock); 258 259 /* 260 * Now, cpu_switch() can schedule the new process. 261 * pcb_esp is loaded pointing to the cpu_switch() stack frame 262 * containing the return address when exiting cpu_switch. 263 * This will normally be to fork_trampoline(), which will have 264 * %ebx loaded with the new proc's pointer. fork_trampoline() 265 * will set up a stack to call fork_return(p, frame); to complete 266 * the return to user-mode. 267 */ 268 } 269 270 /* 271 * Intercept the return address from a freshly forked process that has NOT 272 * been scheduled yet. 273 * 274 * This is needed to make kernel threads stay in kernel mode. 275 */ 276 void 277 cpu_set_fork_handler(td, func, arg) 278 struct thread *td; 279 void (*func)(void *); 280 void *arg; 281 { 282 /* 283 * Note that the trap frame follows the args, so the function 284 * is really called like this: func(arg, frame); 285 */ 286 td->td_pcb->pcb_esi = (int) func; /* function */ 287 td->td_pcb->pcb_ebx = (int) arg; /* first arg */ 288 } 289 290 void 291 cpu_exit(struct thread *td) 292 { 293 struct mdproc *mdp; 294 295 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */ 296 mdp = &td->td_proc->p_md; 297 if (mdp->md_ldt) { 298 td->td_pcb->pcb_gs = _udatasel; 299 load_gs(_udatasel); 300 user_ldt_free(td); 301 } 302 } 303 304 void 305 cpu_thread_exit(struct thread *td) 306 { 307 struct pcb *pcb = td->td_pcb; 308 309 #ifdef DEV_NPX 310 if (td == PCPU_GET(fpcurthread)) 311 npxdrop(); 312 #endif 313 if (pcb->pcb_flags & PCB_DBREGS) { 314 /* disable all hardware breakpoints */ 315 reset_dbregs(); 316 pcb->pcb_flags &= ~PCB_DBREGS; 317 } 318 } 319 320 void 321 cpu_thread_clean(struct thread *td) 322 { 323 struct pcb *pcb; 324 325 pcb = td->td_pcb; 326 if (pcb->pcb_ext != 0) { 327 /* XXXKSE XXXSMP not SMP SAFE.. what locks do we have? */ 328 /* if (pcb->pcb_ext->ext_refcount-- == 1) ?? */ 329 /* 330 * XXX do we need to move the TSS off the allocated pages 331 * before freeing them? (not done here) 332 */ 333 kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext, 334 ctob(IOPAGES + 1)); 335 pcb->pcb_ext = 0; 336 } 337 } 338 339 void 340 cpu_thread_swapin(struct thread *td) 341 { 342 } 343 344 void 345 cpu_thread_swapout(struct thread *td) 346 { 347 } 348 349 void 350 cpu_thread_setup(struct thread *td) 351 { 352 353 td->td_pcb = (struct pcb *)(td->td_kstack + 354 td->td_kstack_pages * PAGE_SIZE) - 1; 355 td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb - 16) - 1; 356 td->td_pcb->pcb_ext = NULL; 357 } 358 359 /* 360 * Initialize machine state (pcb and trap frame) for a new thread about to 361 * upcall. Put enough state in the new thread's PCB to get it to go back 362 * userret(), where we can intercept it again to set the return (upcall) 363 * Address and stack, along with those from upcals that are from other sources 364 * such as those generated in thread_userret() itself. 365 */ 366 void 367 cpu_set_upcall(struct thread *td, struct thread *td0) 368 { 369 struct pcb *pcb2; 370 371 /* Point the pcb to the top of the stack. */ 372 pcb2 = td->td_pcb; 373 374 /* 375 * Copy the upcall pcb. This loads kernel regs. 376 * Those not loaded individually below get their default 377 * values here. 378 * 379 * XXXKSE It might be a good idea to simply skip this as 380 * the values of the other registers may be unimportant. 381 * This would remove any requirement for knowing the KSE 382 * at this time (see the matching comment below for 383 * more analysis) (need a good safe default). 384 */ 385 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2)); 386 pcb2->pcb_flags &= ~(PCB_NPXTRAP|PCB_NPXINITDONE); 387 388 /* 389 * Create a new fresh stack for the new thread. 390 * The -16 is so we can expand the trapframe if we go to vm86. 391 * Don't forget to set this stack value into whatever supplies 392 * the address for the fault handlers. 393 * The contexts are filled in at the time we actually DO the 394 * upcall as only then do we know which KSE we got. 395 */ 396 bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe)); 397 398 /* 399 * Set registers for trampoline to user mode. Leave space for the 400 * return address on stack. These are the kernel mode register values. 401 */ 402 #ifdef PAE 403 pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdpt); 404 #else 405 pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdir); 406 #endif 407 pcb2->pcb_edi = 0; 408 pcb2->pcb_esi = (int)fork_return; /* trampoline arg */ 409 pcb2->pcb_ebp = 0; 410 pcb2->pcb_esp = (int)td->td_frame - sizeof(void *); /* trampoline arg */ 411 pcb2->pcb_ebx = (int)td; /* trampoline arg */ 412 pcb2->pcb_eip = (int)fork_trampoline; 413 pcb2->pcb_psl &= ~(PSL_I); /* interrupts must be disabled */ 414 pcb2->pcb_gs = rgs(); 415 /* 416 * If we didn't copy the pcb, we'd need to do the following registers: 417 * pcb2->pcb_dr*: cloned above. 418 * pcb2->pcb_savefpu: cloned above. 419 * pcb2->pcb_flags: cloned above. 420 * pcb2->pcb_onfault: cloned above (always NULL here?). 421 * pcb2->pcb_gs: cloned above. XXXKSE ??? 422 * pcb2->pcb_ext: cleared below. 423 */ 424 pcb2->pcb_ext = NULL; 425 } 426 427 /* 428 * Set that machine state for performing an upcall that has to 429 * be done in thread_userret() so that those upcalls generated 430 * in thread_userret() itself can be done as well. 431 */ 432 void 433 cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku) 434 { 435 436 /* 437 * Do any extra cleaning that needs to be done. 438 * The thread may have optional components 439 * that are not present in a fresh thread. 440 * This may be a recycled thread so make it look 441 * as though it's newly allocated. 442 */ 443 cpu_thread_clean(td); 444 445 /* 446 * Set the trap frame to point at the beginning of the uts 447 * function. 448 */ 449 td->td_frame->tf_ebp = 0; 450 td->td_frame->tf_esp = 451 (int)ku->ku_stack.ss_sp + ku->ku_stack.ss_size - 16; 452 td->td_frame->tf_eip = (int)ku->ku_func; 453 454 /* 455 * Pass the address of the mailbox for this kse to the uts 456 * function as a parameter on the stack. 457 */ 458 suword((void *)(td->td_frame->tf_esp + sizeof(void *)), 459 (int)ku->ku_mailbox); 460 } 461 462 /* 463 * Convert kernel VA to physical address 464 */ 465 vm_paddr_t 466 kvtop(void *addr) 467 { 468 vm_paddr_t pa; 469 470 pa = pmap_kextract((vm_offset_t)addr); 471 if (pa == 0) 472 panic("kvtop: zero page frame"); 473 return (pa); 474 } 475 476 #ifdef SMP 477 static void 478 cpu_reset_proxy() 479 { 480 481 cpu_reset_proxy_active = 1; 482 while (cpu_reset_proxy_active == 1) 483 ; /* Wait for other cpu to see that we've started */ 484 stop_cpus((1<<cpu_reset_proxyid)); 485 printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid); 486 DELAY(1000000); 487 cpu_reset_real(); 488 } 489 #endif 490 491 void 492 cpu_reset() 493 { 494 #ifdef SMP 495 u_int cnt, map; 496 497 if (smp_active) { 498 map = PCPU_GET(other_cpus) & ~stopped_cpus; 499 if (map != 0) { 500 printf("cpu_reset: Stopping other CPUs\n"); 501 stop_cpus(map); 502 } 503 504 if (PCPU_GET(cpuid) != 0) { 505 cpu_reset_proxyid = PCPU_GET(cpuid); 506 cpustop_restartfunc = cpu_reset_proxy; 507 cpu_reset_proxy_active = 0; 508 printf("cpu_reset: Restarting BSP\n"); 509 started_cpus = (1<<0); /* Restart CPU #0 */ 510 511 cnt = 0; 512 while (cpu_reset_proxy_active == 0 && cnt < 10000000) 513 cnt++; /* Wait for BSP to announce restart */ 514 if (cpu_reset_proxy_active == 0) 515 printf("cpu_reset: Failed to restart BSP\n"); 516 enable_intr(); 517 cpu_reset_proxy_active = 2; 518 519 while (1); 520 /* NOTREACHED */ 521 } 522 523 DELAY(1000000); 524 } 525 #endif 526 cpu_reset_real(); 527 /* NOTREACHED */ 528 } 529 530 static void 531 cpu_reset_real() 532 { 533 534 #ifdef CPU_ELAN 535 if (elan_mmcr != NULL) 536 elan_mmcr->RESCFG = 1; 537 #endif 538 539 if (cpu == CPU_GEODE1100) { 540 /* Attempt Geode's own reset */ 541 outl(0xcf8, 0x80009044ul); 542 outl(0xcfc, 0xf); 543 } 544 545 #ifdef PC98 546 /* 547 * Attempt to do a CPU reset via CPU reset port. 548 */ 549 disable_intr(); 550 if ((inb(0x35) & 0xa0) != 0xa0) { 551 outb(0x37, 0x0f); /* SHUT0 = 0. */ 552 outb(0x37, 0x0b); /* SHUT1 = 0. */ 553 } 554 outb(0xf0, 0x00); /* Reset. */ 555 #else 556 #if !defined(BROKEN_KEYBOARD_RESET) 557 /* 558 * Attempt to do a CPU reset via the keyboard controller, 559 * do not turn off GateA20, as any machine that fails 560 * to do the reset here would then end up in no man's land. 561 */ 562 outb(IO_KBD + 4, 0xFE); 563 DELAY(500000); /* wait 0.5 sec to see if that did it */ 564 printf("Keyboard reset did not work, attempting CPU shutdown\n"); 565 DELAY(1000000); /* wait 1 sec for printf to complete */ 566 #endif 567 #endif /* PC98 */ 568 569 /* Force a shutdown by unmapping entire address space. */ 570 bzero((caddr_t)PTD, NBPTD); 571 572 /* "good night, sweet prince .... <THUNK!>" */ 573 invltlb(); 574 /* NOTREACHED */ 575 while(1); 576 } 577 578 /* 579 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-)) 580 */ 581 static void 582 sf_buf_init(void *arg) 583 { 584 struct sf_buf *sf_bufs; 585 vm_offset_t sf_base; 586 int i; 587 588 nsfbufs = NSFBUFS; 589 TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs); 590 591 sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask); 592 TAILQ_INIT(&sf_buf_freelist); 593 sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE); 594 sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP, 595 M_NOWAIT | M_ZERO); 596 for (i = 0; i < nsfbufs; i++) { 597 sf_bufs[i].kva = sf_base + i * PAGE_SIZE; 598 TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry); 599 } 600 sf_buf_alloc_want = 0; 601 mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF); 602 } 603 604 /* 605 * Get an sf_buf from the freelist. Will block if none are available. 606 */ 607 struct sf_buf * 608 sf_buf_alloc(struct vm_page *m, int flags) 609 { 610 pt_entry_t opte, *ptep; 611 struct sf_head *hash_list; 612 struct sf_buf *sf; 613 int error; 614 615 hash_list = &sf_buf_active[SF_BUF_HASH(m)]; 616 mtx_lock(&sf_buf_lock); 617 LIST_FOREACH(sf, hash_list, list_entry) { 618 if (sf->m == m) { 619 sf->ref_count++; 620 if (sf->ref_count == 1) { 621 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry); 622 nsfbufsused++; 623 nsfbufspeak = imax(nsfbufspeak, nsfbufsused); 624 } 625 goto done; 626 } 627 } 628 while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) { 629 if (flags & SFB_NOWAIT) 630 goto done; 631 sf_buf_alloc_want++; 632 mbstat.sf_allocwait++; 633 error = msleep(&sf_buf_freelist, &sf_buf_lock, 634 (flags & SFB_CATCH) ? PCATCH | PVM : PVM, "sfbufa", 0); 635 sf_buf_alloc_want--; 636 637 /* 638 * If we got a signal, don't risk going back to sleep. 639 */ 640 if (error) 641 goto done; 642 } 643 TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry); 644 if (sf->m != NULL) 645 LIST_REMOVE(sf, list_entry); 646 LIST_INSERT_HEAD(hash_list, sf, list_entry); 647 sf->ref_count = 1; 648 sf->m = m; 649 nsfbufsused++; 650 nsfbufspeak = imax(nsfbufspeak, nsfbufsused); 651 652 /* 653 * Update the sf_buf's virtual-to-physical mapping, flushing the 654 * virtual address from the TLB only if the PTE implies that the old 655 * mapping has been used. Since the reference count for the sf_buf's 656 * old mapping was zero, that mapping is not currently in use. 657 * Consequently, there is no need to exchange the old and new PTEs 658 * atomically, even under PAE. 659 */ 660 ptep = vtopte(sf->kva); 661 opte = *ptep; 662 *ptep = VM_PAGE_TO_PHYS(m) | pgeflag | PG_RW | PG_V; 663 if ((opte & (PG_A | PG_V)) == (PG_A | PG_V)) 664 pmap_invalidate_page(kernel_pmap, sf->kva); 665 done: 666 mtx_unlock(&sf_buf_lock); 667 return (sf); 668 } 669 670 /* 671 * Remove a reference from the given sf_buf, adding it to the free 672 * list when its reference count reaches zero. A freed sf_buf still, 673 * however, retains its virtual-to-physical mapping until it is 674 * recycled or reactivated by sf_buf_alloc(9). 675 */ 676 void 677 sf_buf_free(struct sf_buf *sf) 678 { 679 680 mtx_lock(&sf_buf_lock); 681 sf->ref_count--; 682 if (sf->ref_count == 0) { 683 TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry); 684 nsfbufsused--; 685 if (sf_buf_alloc_want > 0) 686 wakeup_one(&sf_buf_freelist); 687 } 688 mtx_unlock(&sf_buf_lock); 689 } 690 691 /* 692 * Software interrupt handler for queued VM system processing. 693 */ 694 void 695 swi_vm(void *dummy) 696 { 697 if (busdma_swi_pending != 0) 698 busdma_swi(); 699 } 700 701 /* 702 * Tell whether this address is in some physical memory region. 703 * Currently used by the kernel coredump code in order to avoid 704 * dumping the ``ISA memory hole'' which could cause indefinite hangs, 705 * or other unpredictable behaviour. 706 */ 707 708 int 709 is_physical_memory(vm_paddr_t addr) 710 { 711 712 #ifdef DEV_ISA 713 /* The ISA ``memory hole''. */ 714 if (addr >= 0xa0000 && addr < 0x100000) 715 return 0; 716 #endif 717 718 /* 719 * stuff other tests for known memory-mapped devices (PCI?) 720 * here 721 */ 722 723 return 1; 724 } 725