xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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/proc.h>
66 #include <sys/sf_buf.h>
67 #include <sys/smp.h>
68 #include <sys/sysctl.h>
69 #include <sys/unistd.h>
70 #include <sys/user.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 	 * Set registers for trampoline to user mode.  Leave space for the
205 	 * return address on stack.  These are the kernel mode register values.
206 	 */
207 #ifdef PAE
208 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdpt);
209 #else
210 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir);
211 #endif
212 	pcb2->pcb_edi = 0;
213 	pcb2->pcb_esi = (int)fork_return;	/* fork_trampoline argument */
214 	pcb2->pcb_ebp = 0;
215 	pcb2->pcb_esp = (int)td2->td_frame - sizeof(void *);
216 	pcb2->pcb_ebx = (int)td2;		/* fork_trampoline argument */
217 	pcb2->pcb_eip = (int)fork_trampoline;
218 	pcb2->pcb_psl = PSL_KERNEL;		/* ints disabled */
219 	pcb2->pcb_gs = rgs();
220 	/*-
221 	 * pcb2->pcb_dr*:	cloned above.
222 	 * pcb2->pcb_savefpu:	cloned above.
223 	 * pcb2->pcb_flags:	cloned above.
224 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
225 	 * pcb2->pcb_gs:	cloned above.
226 	 * pcb2->pcb_ext:	cleared below.
227 	 */
228 
229 	/*
230 	 * XXX don't copy the i/o pages.  this should probably be fixed.
231 	 */
232 	pcb2->pcb_ext = 0;
233 
234         /* Copy the LDT, if necessary. */
235 	mtx_lock_spin(&sched_lock);
236         if (mdp2->md_ldt != 0) {
237 		if (flags & RFMEM) {
238 			mdp2->md_ldt->ldt_refcnt++;
239 		} else {
240 			mdp2->md_ldt = user_ldt_alloc(mdp2,
241 			    mdp2->md_ldt->ldt_len);
242 			if (mdp2->md_ldt == NULL)
243 				panic("could not copy LDT");
244 		}
245         }
246 	mtx_unlock_spin(&sched_lock);
247 
248 	/*
249 	 * Now, cpu_switch() can schedule the new process.
250 	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
251 	 * containing the return address when exiting cpu_switch.
252 	 * This will normally be to fork_trampoline(), which will have
253 	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
254 	 * will set up a stack to call fork_return(p, frame); to complete
255 	 * the return to user-mode.
256 	 */
257 }
258 
259 /*
260  * Intercept the return address from a freshly forked process that has NOT
261  * been scheduled yet.
262  *
263  * This is needed to make kernel threads stay in kernel mode.
264  */
265 void
266 cpu_set_fork_handler(td, func, arg)
267 	struct thread *td;
268 	void (*func)(void *);
269 	void *arg;
270 {
271 	/*
272 	 * Note that the trap frame follows the args, so the function
273 	 * is really called like this:  func(arg, frame);
274 	 */
275 	td->td_pcb->pcb_esi = (int) func;	/* function */
276 	td->td_pcb->pcb_ebx = (int) arg;	/* first arg */
277 }
278 
279 void
280 cpu_exit(struct thread *td)
281 {
282 	struct mdproc *mdp;
283 	struct pcb *pcb = td->td_pcb;
284 
285 
286 	/* Reset pc->pcb_gs and %gs before possibly invalidating it. */
287 	mdp = &td->td_proc->p_md;
288 	if (mdp->md_ldt) {
289 		td->td_pcb->pcb_gs = _udatasel;
290 		load_gs(_udatasel);
291 		user_ldt_free(td);
292 	}
293 	if (pcb->pcb_flags & PCB_DBREGS) {
294 		/* disable all hardware breakpoints */
295 		reset_dbregs();
296 		pcb->pcb_flags &= ~PCB_DBREGS;
297 	}
298 }
299 
300 void
301 cpu_thread_exit(struct thread *td)
302 {
303 	struct pcb *pcb = td->td_pcb;
304 #ifdef DEV_NPX
305 	if (td == PCPU_GET(fpcurthread))
306 		npxdrop();
307 #endif
308         if (pcb->pcb_flags & PCB_DBREGS) {
309 		/* disable all hardware breakpoints */
310                 reset_dbregs();
311                 pcb->pcb_flags &= ~PCB_DBREGS;
312         }
313 }
314 
315 void
316 cpu_thread_clean(struct thread *td)
317 {
318 	struct pcb *pcb;
319 
320 	pcb = td->td_pcb;
321 	if (pcb->pcb_ext != 0) {
322 		/* XXXKSE  XXXSMP  not SMP SAFE.. what locks do we have? */
323 		/* if (pcb->pcb_ext->ext_refcount-- == 1) ?? */
324 		/*
325 		 * XXX do we need to move the TSS off the allocated pages
326 		 * before freeing them?  (not done here)
327 		 */
328 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
329 		    ctob(IOPAGES + 1));
330 		pcb->pcb_ext = 0;
331 	}
332 }
333 
334 void
335 cpu_thread_swapin(struct thread *td)
336 {
337 }
338 
339 void
340 cpu_thread_swapout(struct thread *td)
341 {
342 }
343 
344 void
345 cpu_thread_setup(struct thread *td)
346 {
347 
348 	td->td_pcb = (struct pcb *)(td->td_kstack +
349 	    td->td_kstack_pages * PAGE_SIZE) - 1;
350 	td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb - 16) - 1;
351 	td->td_pcb->pcb_ext = NULL;
352 }
353 
354 /*
355  * Initialize machine state (pcb and trap frame) for a new thread about to
356  * upcall. Pu t enough state in the new thread's PCB to get it to go back
357  * userret(), where we can intercept it again to set the return (upcall)
358  * Address and stack, along with those from upcals that are from other sources
359  * such as those generated in thread_userret() itself.
360  */
361 void
362 cpu_set_upcall(struct thread *td, struct thread *td0)
363 {
364 	struct pcb *pcb2;
365 
366 	/* Point the pcb to the top of the stack. */
367 	pcb2 = td->td_pcb;
368 
369 	/*
370 	 * Copy the upcall pcb.  This loads kernel regs.
371 	 * Those not loaded individually below get their default
372 	 * values here.
373 	 *
374 	 * XXXKSE It might be a good idea to simply skip this as
375 	 * the values of the other registers may be unimportant.
376 	 * This would remove any requirement for knowing the KSE
377 	 * at this time (see the matching comment below for
378 	 * more analysis) (need a good safe default).
379 	 */
380 	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
381 	pcb2->pcb_flags &= ~(PCB_NPXTRAP|PCB_NPXINITDONE);
382 
383 	/*
384 	 * Create a new fresh stack for the new thread.
385 	 * The -16 is so we can expand the trapframe if we go to vm86.
386 	 * Don't forget to set this stack value into whatever supplies
387 	 * the address for the fault handlers.
388 	 * The contexts are filled in at the time we actually DO the
389 	 * upcall as only then do we know which KSE we got.
390 	 */
391 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
392 
393 	/*
394 	 * Set registers for trampoline to user mode.  Leave space for the
395 	 * return address on stack.  These are the kernel mode register values.
396 	 */
397 #ifdef PAE
398 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdpt);
399 #else
400 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdir);
401 #endif
402 	pcb2->pcb_edi = 0;
403 	pcb2->pcb_esi = (int)fork_return;		    /* trampoline arg */
404 	pcb2->pcb_ebp = 0;
405 	pcb2->pcb_esp = (int)td->td_frame - sizeof(void *); /* trampoline arg */
406 	pcb2->pcb_ebx = (int)td;			    /* trampoline arg */
407 	pcb2->pcb_eip = (int)fork_trampoline;
408 	pcb2->pcb_psl &= ~(PSL_I);	/* interrupts must be disabled */
409 	pcb2->pcb_gs = rgs();
410 	/*
411 	 * If we didn't copy the pcb, we'd need to do the following registers:
412 	 * pcb2->pcb_dr*:	cloned above.
413 	 * pcb2->pcb_savefpu:	cloned above.
414 	 * pcb2->pcb_flags:	cloned above.
415 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
416 	 * pcb2->pcb_gs:	cloned above.  XXXKSE ???
417 	 * pcb2->pcb_ext:	cleared below.
418 	 */
419 	 pcb2->pcb_ext = NULL;
420 }
421 
422 /*
423  * Set that machine state for performing an upcall that has to
424  * be done in thread_userret() so that those upcalls generated
425  * in thread_userret() itself can be done as well.
426  */
427 void
428 cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
429 {
430 
431 	/*
432 	 * Do any extra cleaning that needs to be done.
433 	 * The thread may have optional components
434 	 * that are not present in a fresh thread.
435 	 * This may be a recycled thread so make it look
436 	 * as though it's newly allocated.
437 	 */
438 	cpu_thread_clean(td);
439 
440 	/*
441 	 * Set the trap frame to point at the beginning of the uts
442 	 * function.
443 	 */
444 	td->td_frame->tf_ebp = 0;
445 	td->td_frame->tf_esp =
446 	    (int)ku->ku_stack.ss_sp + ku->ku_stack.ss_size - 16;
447 	td->td_frame->tf_eip = (int)ku->ku_func;
448 
449 	/*
450 	 * Pass the address of the mailbox for this kse to the uts
451 	 * function as a parameter on the stack.
452 	 */
453 	suword((void *)(td->td_frame->tf_esp + sizeof(void *)),
454 	    (int)ku->ku_mailbox);
455 }
456 
457 /*
458  * Convert kernel VA to physical address
459  */
460 vm_paddr_t
461 kvtop(void *addr)
462 {
463 	vm_paddr_t pa;
464 
465 	pa = pmap_kextract((vm_offset_t)addr);
466 	if (pa == 0)
467 		panic("kvtop: zero page frame");
468 	return (pa);
469 }
470 
471 /*
472  * Force reset the processor by invalidating the entire address space!
473  */
474 
475 #ifdef SMP
476 static void
477 cpu_reset_proxy()
478 {
479 
480 	cpu_reset_proxy_active = 1;
481 	while (cpu_reset_proxy_active == 1)
482 		;	 /* Wait for other cpu to see that we've started */
483 	stop_cpus((1<<cpu_reset_proxyid));
484 	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
485 	DELAY(1000000);
486 	cpu_reset_real();
487 }
488 #endif
489 
490 void
491 cpu_reset()
492 {
493 #ifdef SMP
494 	if (smp_active == 0) {
495 		cpu_reset_real();
496 		/* NOTREACHED */
497 	} else {
498 
499 		u_int map;
500 		int cnt;
501 		printf("cpu_reset called on cpu#%d\n", PCPU_GET(cpuid));
502 
503 		map = PCPU_GET(other_cpus) & ~ stopped_cpus;
504 
505 		if (map != 0) {
506 			printf("cpu_reset: Stopping other CPUs\n");
507 			stop_cpus(map);		/* Stop all other CPUs */
508 		}
509 
510 		if (PCPU_GET(cpuid) == 0) {
511 			DELAY(1000000);
512 			cpu_reset_real();
513 			/* NOTREACHED */
514 		} else {
515 			/* We are not BSP (CPU #0) */
516 
517 			cpu_reset_proxyid = PCPU_GET(cpuid);
518 			cpustop_restartfunc = cpu_reset_proxy;
519 			cpu_reset_proxy_active = 0;
520 			printf("cpu_reset: Restarting BSP\n");
521 			started_cpus = (1<<0);		/* Restart CPU #0 */
522 
523 			cnt = 0;
524 			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
525 				cnt++;	/* Wait for BSP to announce restart */
526 			if (cpu_reset_proxy_active == 0)
527 				printf("cpu_reset: Failed to restart BSP\n");
528 			enable_intr();
529 			cpu_reset_proxy_active = 2;
530 
531 			while (1);
532 			/* NOTREACHED */
533 		}
534 	}
535 #else
536 	cpu_reset_real();
537 #endif
538 }
539 
540 static void
541 cpu_reset_real()
542 {
543 
544 #ifdef CPU_ELAN
545 	if (elan_mmcr != NULL)
546 		elan_mmcr->RESCFG = 1;
547 #endif
548 
549 	if (cpu == CPU_GEODE1100) {
550 		/* Attempt Geode's own reset */
551 		outl(0xcf8, 0x80009044ul);
552 		outl(0xcfc, 0xf);
553 	}
554 
555 #ifdef PC98
556 	/*
557 	 * Attempt to do a CPU reset via CPU reset port.
558 	 */
559 	disable_intr();
560 	if ((inb(0x35) & 0xa0) != 0xa0) {
561 		outb(0x37, 0x0f);		/* SHUT0 = 0. */
562 		outb(0x37, 0x0b);		/* SHUT1 = 0. */
563 	}
564 	outb(0xf0, 0x00);		/* Reset. */
565 #else
566 	/*
567 	 * Attempt to do a CPU reset via the keyboard controller,
568 	 * do not turn of the GateA20, as any machine that fails
569 	 * to do the reset here would then end up in no man's land.
570 	 */
571 
572 #if !defined(BROKEN_KEYBOARD_RESET)
573 	outb(IO_KBD + 4, 0xFE);
574 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
575 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
576 	DELAY(1000000);	/* wait 1 sec for printf to complete */
577 #endif
578 #endif /* PC98 */
579 	/* force a shutdown by unmapping entire address space ! */
580 	bzero((caddr_t)PTD, NBPTD);
581 
582 	/* "good night, sweet prince .... <THUNK!>" */
583 	invltlb();
584 	/* NOTREACHED */
585 	while(1);
586 }
587 
588 /*
589  * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
590  */
591 static void
592 sf_buf_init(void *arg)
593 {
594 	struct sf_buf *sf_bufs;
595 	vm_offset_t sf_base;
596 	int i;
597 
598 	nsfbufs = NSFBUFS;
599 	TUNABLE_INT_FETCH("kern.ipc.nsfbufs", &nsfbufs);
600 
601 	sf_buf_active = hashinit(nsfbufs, M_TEMP, &sf_buf_hashmask);
602 	TAILQ_INIT(&sf_buf_freelist);
603 	sf_base = kmem_alloc_nofault(kernel_map, nsfbufs * PAGE_SIZE);
604 	sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
605 	    M_NOWAIT | M_ZERO);
606 	for (i = 0; i < nsfbufs; i++) {
607 		sf_bufs[i].kva = sf_base + i * PAGE_SIZE;
608 		TAILQ_INSERT_TAIL(&sf_buf_freelist, &sf_bufs[i], free_entry);
609 	}
610 	sf_buf_alloc_want = 0;
611 	mtx_init(&sf_buf_lock, "sf_buf", NULL, MTX_DEF);
612 }
613 
614 /*
615  * Get an sf_buf from the freelist. Will block if none are available.
616  */
617 struct sf_buf *
618 sf_buf_alloc(struct vm_page *m, int pri)
619 {
620 	struct sf_head *hash_list;
621 	struct sf_buf *sf;
622 	int error;
623 
624 	hash_list = &sf_buf_active[SF_BUF_HASH(m)];
625 	mtx_lock(&sf_buf_lock);
626 	LIST_FOREACH(sf, hash_list, list_entry) {
627 		if (sf->m == m) {
628 			sf->ref_count++;
629 			if (sf->ref_count == 1) {
630 				TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
631 				nsfbufsused++;
632 				nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
633 			}
634 			goto done;
635 		}
636 	}
637 	while ((sf = TAILQ_FIRST(&sf_buf_freelist)) == NULL) {
638 		sf_buf_alloc_want++;
639 		mbstat.sf_allocwait++;
640 		error = msleep(&sf_buf_freelist, &sf_buf_lock, PVM | pri,
641 		    "sfbufa", 0);
642 		sf_buf_alloc_want--;
643 
644 		/*
645 		 * If we got a signal, don't risk going back to sleep.
646 		 */
647 		if (error)
648 			goto done;
649 	}
650 	TAILQ_REMOVE(&sf_buf_freelist, sf, free_entry);
651 	if (sf->m != NULL)
652 		LIST_REMOVE(sf, list_entry);
653 	LIST_INSERT_HEAD(hash_list, sf, list_entry);
654 	sf->ref_count = 1;
655 	sf->m = m;
656 	nsfbufsused++;
657 	nsfbufspeak = imax(nsfbufspeak, nsfbufsused);
658 	pmap_qenter(sf->kva, &sf->m, 1);
659 done:
660 	mtx_unlock(&sf_buf_lock);
661 	return (sf);
662 }
663 
664 /*
665  * Remove a reference from the given sf_buf, adding it to the free
666  * list when its reference count reaches zero.  A freed sf_buf still,
667  * however, retains its virtual-to-physical mapping until it is
668  * recycled or reactivated by sf_buf_alloc(9).
669  */
670 void
671 sf_buf_free(struct sf_buf *sf)
672 {
673 
674 	mtx_lock(&sf_buf_lock);
675 	sf->ref_count--;
676 	if (sf->ref_count == 0) {
677 		TAILQ_INSERT_TAIL(&sf_buf_freelist, sf, free_entry);
678 		nsfbufsused--;
679 		if (sf_buf_alloc_want > 0)
680 			wakeup_one(&sf_buf_freelist);
681 	}
682 	mtx_unlock(&sf_buf_lock);
683 }
684 
685 /*
686  * Software interrupt handler for queued VM system processing.
687  */
688 void
689 swi_vm(void *dummy)
690 {
691 	if (busdma_swi_pending != 0)
692 		busdma_swi();
693 }
694 
695 /*
696  * Tell whether this address is in some physical memory region.
697  * Currently used by the kernel coredump code in order to avoid
698  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
699  * or other unpredictable behaviour.
700  */
701 
702 int
703 is_physical_memory(vm_paddr_t addr)
704 {
705 
706 #ifdef DEV_ISA
707 	/* The ISA ``memory hole''. */
708 	if (addr >= 0xa0000 && addr < 0x100000)
709 		return 0;
710 #endif
711 
712 	/*
713 	 * stuff other tests for known memory-mapped devices (PCI?)
714 	 * here
715 	 */
716 
717 	return 1;
718 }
719