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