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