xref: /freebsd/sys/i386/i386/vm_machdep.c (revision bcccd559e20d22c29805034b912659ffd0b97813)
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 #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/kernel.h>
56 #include <sys/ktr.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/mutex.h>
61 #include <sys/pioctl.h>
62 #include <sys/proc.h>
63 #include <sys/sysent.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 #include <isa/isareg.h>
92 
93 #ifndef NSFBUFS
94 #define	NSFBUFS		(512 + maxusers * 16)
95 #endif
96 
97 _Static_assert(OFFSETOF_CURTHREAD == offsetof(struct pcpu, pc_curthread),
98     "OFFSETOF_CURTHREAD does not correspond with offset of pc_curthread.");
99 _Static_assert(OFFSETOF_CURPCB == offsetof(struct pcpu, pc_curpcb),
100     "OFFSETOF_CURPCB does not correspond with offset of pc_curpcb.");
101 _Static_assert(__OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf),
102     "__OFFSETOF_MONINORBUF does not correspond with offset of pc_monitorbuf.");
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 
111 union savefpu *
112 get_pcb_user_save_td(struct thread *td)
113 {
114 	vm_offset_t p;
115 
116 	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
117 	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
118 	KASSERT((p % XSAVE_AREA_ALIGN) == 0, ("Unaligned pcb_user_save area"));
119 	return ((union savefpu *)p);
120 }
121 
122 union savefpu *
123 get_pcb_user_save_pcb(struct pcb *pcb)
124 {
125 	vm_offset_t p;
126 
127 	p = (vm_offset_t)(pcb + 1);
128 	return ((union savefpu *)p);
129 }
130 
131 struct pcb *
132 get_pcb_td(struct thread *td)
133 {
134 	vm_offset_t p;
135 
136 	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
137 	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) -
138 	    sizeof(struct pcb);
139 	return ((struct pcb *)p);
140 }
141 
142 void *
143 alloc_fpusave(int flags)
144 {
145 	void *res;
146 	struct savefpu_ymm *sf;
147 
148 	res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
149 	if (use_xsave) {
150 		sf = (struct savefpu_ymm *)res;
151 		bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
152 		sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
153 	}
154 	return (res);
155 }
156 /*
157  * Finish a fork operation, with process p2 nearly set up.
158  * Copy and update the pcb, set up the stack so that the child
159  * ready to run and return to user mode.
160  */
161 void
162 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
163 {
164 	struct proc *p1;
165 	struct pcb *pcb2;
166 	struct mdproc *mdp2;
167 
168 	p1 = td1->td_proc;
169 	if ((flags & RFPROC) == 0) {
170 		if ((flags & RFMEM) == 0) {
171 			/* unshare user LDT */
172 			struct mdproc *mdp1 = &p1->p_md;
173 			struct proc_ldt *pldt, *pldt1;
174 
175 			mtx_lock_spin(&dt_lock);
176 			if ((pldt1 = mdp1->md_ldt) != NULL &&
177 			    pldt1->ldt_refcnt > 1) {
178 				pldt = user_ldt_alloc(mdp1, pldt1->ldt_len);
179 				if (pldt == NULL)
180 					panic("could not copy LDT");
181 				mdp1->md_ldt = pldt;
182 				set_user_ldt(mdp1);
183 				user_ldt_deref(pldt1);
184 			} else
185 				mtx_unlock_spin(&dt_lock);
186 		}
187 		return;
188 	}
189 
190 	/* Ensure that td1's pcb is up to date. */
191 	if (td1 == curthread)
192 		td1->td_pcb->pcb_gs = rgs();
193 	critical_enter();
194 	if (PCPU_GET(fpcurthread) == td1)
195 		npxsave(td1->td_pcb->pcb_save);
196 	critical_exit();
197 
198 	/* Point the pcb to the top of the stack */
199 	pcb2 = get_pcb_td(td2);
200 	td2->td_pcb = pcb2;
201 
202 	/* Copy td1's pcb */
203 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
204 
205 	/* Properly initialize pcb_save */
206 	pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
207 	bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
208 	    cpu_max_ext_state_size);
209 
210 	/* Point mdproc and then copy over td1's contents */
211 	mdp2 = &p2->p_md;
212 	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
213 
214 	/*
215 	 * Create a new fresh stack for the new process.
216 	 * Copy the trap frame for the return to user mode as if from a
217 	 * syscall.  This copies most of the user mode register values.
218 	 * The -16 is so we can expand the trapframe if we go to vm86.
219 	 */
220 	td2->td_frame = (struct trapframe *)((caddr_t)td2->td_pcb - 16) - 1;
221 	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
222 
223 	td2->td_frame->tf_eax = 0;		/* Child returns zero */
224 	td2->td_frame->tf_eflags &= ~PSL_C;	/* success */
225 	td2->td_frame->tf_edx = 1;
226 
227 	/*
228 	 * If the parent process has the trap bit set (i.e. a debugger had
229 	 * single stepped the process to the system call), we need to clear
230 	 * the trap flag from the new frame unless the debugger had set PF_FORK
231 	 * on the parent.  Otherwise, the child will receive a (likely
232 	 * unexpected) SIGTRAP when it executes the first instruction after
233 	 * returning  to userland.
234 	 */
235 	if ((p1->p_pfsflags & PF_FORK) == 0)
236 		td2->td_frame->tf_eflags &= ~PSL_T;
237 
238 	/*
239 	 * Set registers for trampoline to user mode.  Leave space for the
240 	 * return address on stack.  These are the kernel mode register values.
241 	 */
242 #if defined(PAE) || defined(PAE_TABLES)
243 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdpt);
244 #else
245 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir);
246 #endif
247 	pcb2->pcb_edi = 0;
248 	pcb2->pcb_esi = (int)fork_return;	/* fork_trampoline argument */
249 	pcb2->pcb_ebp = 0;
250 	pcb2->pcb_esp = (int)td2->td_frame - sizeof(void *);
251 	pcb2->pcb_ebx = (int)td2;		/* fork_trampoline argument */
252 	pcb2->pcb_eip = (int)fork_trampoline;
253 	/*-
254 	 * pcb2->pcb_dr*:	cloned above.
255 	 * pcb2->pcb_savefpu:	cloned above.
256 	 * pcb2->pcb_flags:	cloned above.
257 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
258 	 * pcb2->pcb_gs:	cloned above.
259 	 * pcb2->pcb_ext:	cleared below.
260 	 */
261 
262 	/*
263 	 * XXX don't copy the i/o pages.  this should probably be fixed.
264 	 */
265 	pcb2->pcb_ext = 0;
266 
267 	/* Copy the LDT, if necessary. */
268 	mtx_lock_spin(&dt_lock);
269 	if (mdp2->md_ldt != NULL) {
270 		if (flags & RFMEM) {
271 			mdp2->md_ldt->ldt_refcnt++;
272 		} else {
273 			mdp2->md_ldt = user_ldt_alloc(mdp2,
274 			    mdp2->md_ldt->ldt_len);
275 			if (mdp2->md_ldt == NULL)
276 				panic("could not copy LDT");
277 		}
278 	}
279 	mtx_unlock_spin(&dt_lock);
280 
281 	/* Setup to release spin count in fork_exit(). */
282 	td2->td_md.md_spinlock_count = 1;
283 	td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
284 
285 	/*
286 	 * Now, cpu_switch() can schedule the new process.
287 	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
288 	 * containing the return address when exiting cpu_switch.
289 	 * This will normally be to fork_trampoline(), which will have
290 	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
291 	 * will set up a stack to call fork_return(p, frame); to complete
292 	 * the return to user-mode.
293 	 */
294 }
295 
296 /*
297  * Intercept the return address from a freshly forked process that has NOT
298  * been scheduled yet.
299  *
300  * This is needed to make kernel threads stay in kernel mode.
301  */
302 void
303 cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), void *arg)
304 {
305 	/*
306 	 * Note that the trap frame follows the args, so the function
307 	 * is really called like this:  func(arg, frame);
308 	 */
309 	td->td_pcb->pcb_esi = (int) func;	/* function */
310 	td->td_pcb->pcb_ebx = (int) arg;	/* first arg */
311 }
312 
313 void
314 cpu_exit(struct thread *td)
315 {
316 
317 	/*
318 	 * If this process has a custom LDT, release it.  Reset pc->pcb_gs
319 	 * and %gs before we free it in case they refer to an LDT entry.
320 	 */
321 	mtx_lock_spin(&dt_lock);
322 	if (td->td_proc->p_md.md_ldt) {
323 		td->td_pcb->pcb_gs = _udatasel;
324 		load_gs(_udatasel);
325 		user_ldt_free(td);
326 	} else
327 		mtx_unlock_spin(&dt_lock);
328 }
329 
330 void
331 cpu_thread_exit(struct thread *td)
332 {
333 
334 	critical_enter();
335 	if (td == PCPU_GET(fpcurthread))
336 		npxdrop();
337 	critical_exit();
338 
339 	/* Disable any hardware breakpoints. */
340 	if (td->td_pcb->pcb_flags & PCB_DBREGS) {
341 		reset_dbregs();
342 		td->td_pcb->pcb_flags &= ~PCB_DBREGS;
343 	}
344 }
345 
346 void
347 cpu_thread_clean(struct thread *td)
348 {
349 	struct pcb *pcb;
350 
351 	pcb = td->td_pcb;
352 	if (pcb->pcb_ext != NULL) {
353 		/* if (pcb->pcb_ext->ext_refcount-- == 1) ?? */
354 		/*
355 		 * XXX do we need to move the TSS off the allocated pages
356 		 * before freeing them?  (not done here)
357 		 */
358 		kmem_free(kernel_arena, (vm_offset_t)pcb->pcb_ext,
359 		    ctob(IOPAGES + 1));
360 		pcb->pcb_ext = NULL;
361 	}
362 }
363 
364 void
365 cpu_thread_swapin(struct thread *td)
366 {
367 }
368 
369 void
370 cpu_thread_swapout(struct thread *td)
371 {
372 }
373 
374 void
375 cpu_thread_alloc(struct thread *td)
376 {
377 	struct pcb *pcb;
378 	struct xstate_hdr *xhdr;
379 
380 	td->td_pcb = pcb = get_pcb_td(td);
381 	td->td_frame = (struct trapframe *)((caddr_t)pcb - 16) - 1;
382 	pcb->pcb_ext = NULL;
383 	pcb->pcb_save = get_pcb_user_save_pcb(pcb);
384 	if (use_xsave) {
385 		xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
386 		bzero(xhdr, sizeof(*xhdr));
387 		xhdr->xstate_bv = xsave_mask;
388 	}
389 }
390 
391 void
392 cpu_thread_free(struct thread *td)
393 {
394 
395 	cpu_thread_clean(td);
396 }
397 
398 void
399 cpu_set_syscall_retval(struct thread *td, int error)
400 {
401 
402 	switch (error) {
403 	case 0:
404 		td->td_frame->tf_eax = td->td_retval[0];
405 		td->td_frame->tf_edx = td->td_retval[1];
406 		td->td_frame->tf_eflags &= ~PSL_C;
407 		break;
408 
409 	case ERESTART:
410 		/*
411 		 * Reconstruct pc, assuming lcall $X,y is 7 bytes, int
412 		 * 0x80 is 2 bytes. We saved this in tf_err.
413 		 */
414 		td->td_frame->tf_eip -= td->td_frame->tf_err;
415 		break;
416 
417 	case EJUSTRETURN:
418 		break;
419 
420 	default:
421 		td->td_frame->tf_eax = SV_ABI_ERRNO(td->td_proc, error);
422 		td->td_frame->tf_eflags |= PSL_C;
423 		break;
424 	}
425 }
426 
427 /*
428  * Initialize machine state, mostly pcb and trap frame for a new
429  * thread, about to return to userspace.  Put enough state in the new
430  * thread's PCB to get it to go back to the fork_return(), which
431  * finalizes the thread state and handles peculiarities of the first
432  * return to userspace for the new thread.
433  */
434 void
435 cpu_copy_thread(struct thread *td, struct thread *td0)
436 {
437 	struct pcb *pcb2;
438 
439 	/* Point the pcb to the top of the stack. */
440 	pcb2 = td->td_pcb;
441 
442 	/*
443 	 * Copy the upcall pcb.  This loads kernel regs.
444 	 * Those not loaded individually below get their default
445 	 * values here.
446 	 */
447 	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
448 	pcb2->pcb_flags &= ~(PCB_NPXINITDONE | PCB_NPXUSERINITDONE |
449 	    PCB_KERNNPX);
450 	pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
451 	bcopy(get_pcb_user_save_td(td0), pcb2->pcb_save,
452 	    cpu_max_ext_state_size);
453 
454 	/*
455 	 * Create a new fresh stack for the new thread.
456 	 */
457 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
458 
459 	/* If the current thread has the trap bit set (i.e. a debugger had
460 	 * single stepped the process to the system call), we need to clear
461 	 * the trap flag from the new frame. Otherwise, the new thread will
462 	 * receive a (likely unexpected) SIGTRAP when it executes the first
463 	 * instruction after returning to userland.
464 	 */
465 	td->td_frame->tf_eflags &= ~PSL_T;
466 
467 	/*
468 	 * Set registers for trampoline to user mode.  Leave space for the
469 	 * return address on stack.  These are the kernel mode register values.
470 	 */
471 	pcb2->pcb_edi = 0;
472 	pcb2->pcb_esi = (int)fork_return;		    /* trampoline arg */
473 	pcb2->pcb_ebp = 0;
474 	pcb2->pcb_esp = (int)td->td_frame - sizeof(void *); /* trampoline arg */
475 	pcb2->pcb_ebx = (int)td;			    /* trampoline arg */
476 	pcb2->pcb_eip = (int)fork_trampoline;
477 	pcb2->pcb_gs = rgs();
478 	/*
479 	 * If we didn't copy the pcb, we'd need to do the following registers:
480 	 * pcb2->pcb_cr3:	cloned above.
481 	 * pcb2->pcb_dr*:	cloned above.
482 	 * pcb2->pcb_savefpu:	cloned above.
483 	 * pcb2->pcb_flags:	cloned above.
484 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
485 	 * pcb2->pcb_gs:	cloned above.
486 	 * pcb2->pcb_ext:	cleared below.
487 	 */
488 	pcb2->pcb_ext = NULL;
489 
490 	/* Setup to release spin count in fork_exit(). */
491 	td->td_md.md_spinlock_count = 1;
492 	td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
493 }
494 
495 /*
496  * Set that machine state for performing an upcall that starts
497  * the entry function with the given argument.
498  */
499 void
500 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
501     stack_t *stack)
502 {
503 
504 	/*
505 	 * Do any extra cleaning that needs to be done.
506 	 * The thread may have optional components
507 	 * that are not present in a fresh thread.
508 	 * This may be a recycled thread so make it look
509 	 * as though it's newly allocated.
510 	 */
511 	cpu_thread_clean(td);
512 
513 	/*
514 	 * Set the trap frame to point at the beginning of the entry
515 	 * function.
516 	 */
517 	td->td_frame->tf_ebp = 0;
518 	td->td_frame->tf_esp =
519 	    (((int)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
520 	td->td_frame->tf_eip = (int)entry;
521 
522 	/* Return address sentinel value to stop stack unwinding. */
523 	suword((void *)td->td_frame->tf_esp, 0);
524 
525 	/* Pass the argument to the entry point. */
526 	suword((void *)(td->td_frame->tf_esp + sizeof(void *)),
527 	    (int)arg);
528 }
529 
530 int
531 cpu_set_user_tls(struct thread *td, void *tls_base)
532 {
533 	struct segment_descriptor sd;
534 	uint32_t base;
535 
536 	/*
537 	 * Construct a descriptor and store it in the pcb for
538 	 * the next context switch.  Also store it in the gdt
539 	 * so that the load of tf_fs into %fs will activate it
540 	 * at return to userland.
541 	 */
542 	base = (uint32_t)tls_base;
543 	sd.sd_lobase = base & 0xffffff;
544 	sd.sd_hibase = (base >> 24) & 0xff;
545 	sd.sd_lolimit = 0xffff;	/* 4GB limit, wraps around */
546 	sd.sd_hilimit = 0xf;
547 	sd.sd_type  = SDT_MEMRWA;
548 	sd.sd_dpl   = SEL_UPL;
549 	sd.sd_p     = 1;
550 	sd.sd_xx    = 0;
551 	sd.sd_def32 = 1;
552 	sd.sd_gran  = 1;
553 	critical_enter();
554 	/* set %gs */
555 	td->td_pcb->pcb_gsd = sd;
556 	if (td == curthread) {
557 		PCPU_GET(fsgs_gdt)[1] = sd;
558 		load_gs(GSEL(GUGS_SEL, SEL_UPL));
559 	}
560 	critical_exit();
561 	return (0);
562 }
563 
564 /*
565  * Convert kernel VA to physical address
566  */
567 vm_paddr_t
568 kvtop(void *addr)
569 {
570 	vm_paddr_t pa;
571 
572 	pa = pmap_kextract((vm_offset_t)addr);
573 	if (pa == 0)
574 		panic("kvtop: zero page frame");
575 	return (pa);
576 }
577 
578 #ifdef SMP
579 static void
580 cpu_reset_proxy()
581 {
582 	cpuset_t tcrp;
583 
584 	cpu_reset_proxy_active = 1;
585 	while (cpu_reset_proxy_active == 1)
586 		;	/* Wait for other cpu to see that we've started */
587 	CPU_SETOF(cpu_reset_proxyid, &tcrp);
588 	stop_cpus(tcrp);
589 	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
590 	DELAY(1000000);
591 	cpu_reset_real();
592 }
593 #endif
594 
595 void
596 cpu_reset()
597 {
598 #ifdef SMP
599 	cpuset_t map;
600 	u_int cnt;
601 
602 	if (smp_started) {
603 		map = all_cpus;
604 		CPU_CLR(PCPU_GET(cpuid), &map);
605 		CPU_NAND(&map, &stopped_cpus);
606 		if (!CPU_EMPTY(&map)) {
607 			printf("cpu_reset: Stopping other CPUs\n");
608 			stop_cpus(map);
609 		}
610 
611 		if (PCPU_GET(cpuid) != 0) {
612 			cpu_reset_proxyid = PCPU_GET(cpuid);
613 			cpustop_restartfunc = cpu_reset_proxy;
614 			cpu_reset_proxy_active = 0;
615 			printf("cpu_reset: Restarting BSP\n");
616 
617 			/* Restart CPU #0. */
618 			/* XXX: restart_cpus(1 << 0); */
619 			CPU_SETOF(0, &started_cpus);
620 			wmb();
621 
622 			cnt = 0;
623 			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
624 				cnt++;	/* Wait for BSP to announce restart */
625 			if (cpu_reset_proxy_active == 0)
626 				printf("cpu_reset: Failed to restart BSP\n");
627 			enable_intr();
628 			cpu_reset_proxy_active = 2;
629 
630 			while (1);
631 			/* NOTREACHED */
632 		}
633 
634 		DELAY(1000000);
635 	}
636 #endif
637 	cpu_reset_real();
638 	/* NOTREACHED */
639 }
640 
641 static void
642 cpu_reset_real()
643 {
644 	struct region_descriptor null_idt;
645 	int b;
646 
647 	disable_intr();
648 #ifdef CPU_ELAN
649 	if (elan_mmcr != NULL)
650 		elan_mmcr->RESCFG = 1;
651 #endif
652 
653 	if (cpu == CPU_GEODE1100) {
654 		/* Attempt Geode's own reset */
655 		outl(0xcf8, 0x80009044ul);
656 		outl(0xcfc, 0xf);
657 	}
658 
659 #if !defined(BROKEN_KEYBOARD_RESET)
660 	/*
661 	 * Attempt to do a CPU reset via the keyboard controller,
662 	 * do not turn off GateA20, as any machine that fails
663 	 * to do the reset here would then end up in no man's land.
664 	 */
665 	outb(IO_KBD + 4, 0xFE);
666 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
667 #endif
668 
669 	/*
670 	 * Attempt to force a reset via the Reset Control register at
671 	 * I/O port 0xcf9.  Bit 2 forces a system reset when it
672 	 * transitions from 0 to 1.  Bit 1 selects the type of reset
673 	 * to attempt: 0 selects a "soft" reset, and 1 selects a
674 	 * "hard" reset.  We try a "hard" reset.  The first write sets
675 	 * bit 1 to select a "hard" reset and clears bit 2.  The
676 	 * second write forces a 0 -> 1 transition in bit 2 to trigger
677 	 * a reset.
678 	 */
679 	outb(0xcf9, 0x2);
680 	outb(0xcf9, 0x6);
681 	DELAY(500000);  /* wait 0.5 sec to see if that did it */
682 
683 	/*
684 	 * Attempt to force a reset via the Fast A20 and Init register
685 	 * at I/O port 0x92.  Bit 1 serves as an alternate A20 gate.
686 	 * Bit 0 asserts INIT# when set to 1.  We are careful to only
687 	 * preserve bit 1 while setting bit 0.  We also must clear bit
688 	 * 0 before setting it if it isn't already clear.
689 	 */
690 	b = inb(0x92);
691 	if (b != 0xff) {
692 		if ((b & 0x1) != 0)
693 			outb(0x92, b & 0xfe);
694 		outb(0x92, b | 0x1);
695 		DELAY(500000);  /* wait 0.5 sec to see if that did it */
696 	}
697 
698 	printf("No known reset method worked, attempting CPU shutdown\n");
699 	DELAY(1000000); /* wait 1 sec for printf to complete */
700 
701 	/* Wipe the IDT. */
702 	null_idt.rd_limit = 0;
703 	null_idt.rd_base = 0;
704 	lidt(&null_idt);
705 
706 	/* "good night, sweet prince .... <THUNK!>" */
707 	breakpoint();
708 
709 	/* NOTREACHED */
710 	while(1);
711 }
712 
713 /*
714  * Get an sf_buf from the freelist.  May block if none are available.
715  */
716 void
717 sf_buf_map(struct sf_buf *sf, int flags)
718 {
719 	pt_entry_t opte, *ptep;
720 
721 	/*
722 	 * Update the sf_buf's virtual-to-physical mapping, flushing the
723 	 * virtual address from the TLB.  Since the reference count for
724 	 * the sf_buf's old mapping was zero, that mapping is not
725 	 * currently in use.  Consequently, there is no need to exchange
726 	 * the old and new PTEs atomically, even under PAE.
727 	 */
728 	ptep = vtopte(sf->kva);
729 	opte = *ptep;
730 	*ptep = VM_PAGE_TO_PHYS(sf->m) | pgeflag | PG_RW | PG_V |
731 	    pmap_cache_bits(sf->m->md.pat_mode, 0);
732 
733 	/*
734 	 * Avoid unnecessary TLB invalidations: If the sf_buf's old
735 	 * virtual-to-physical mapping was not used, then any processor
736 	 * that has invalidated the sf_buf's virtual address from its TLB
737 	 * since the last used mapping need not invalidate again.
738 	 */
739 #ifdef SMP
740 	if ((opte & (PG_V | PG_A)) ==  (PG_V | PG_A))
741 		CPU_ZERO(&sf->cpumask);
742 
743 	sf_buf_shootdown(sf, flags);
744 #else
745 	if ((opte & (PG_V | PG_A)) ==  (PG_V | PG_A))
746 		pmap_invalidate_page(kernel_pmap, sf->kva);
747 #endif
748 }
749 
750 #ifdef SMP
751 void
752 sf_buf_shootdown(struct sf_buf *sf, int flags)
753 {
754 	cpuset_t other_cpus;
755 	u_int cpuid;
756 
757 	sched_pin();
758 	cpuid = PCPU_GET(cpuid);
759 	if (!CPU_ISSET(cpuid, &sf->cpumask)) {
760 		CPU_SET(cpuid, &sf->cpumask);
761 		invlpg(sf->kva);
762 	}
763 	if ((flags & SFB_CPUPRIVATE) == 0) {
764 		other_cpus = all_cpus;
765 		CPU_CLR(cpuid, &other_cpus);
766 		CPU_NAND(&other_cpus, &sf->cpumask);
767 		if (!CPU_EMPTY(&other_cpus)) {
768 			CPU_OR(&sf->cpumask, &other_cpus);
769 			smp_masked_invlpg(other_cpus, sf->kva);
770 		}
771 	}
772 	sched_unpin();
773 }
774 #endif
775 
776 /*
777  * MD part of sf_buf_free().
778  */
779 int
780 sf_buf_unmap(struct sf_buf *sf)
781 {
782 
783 	return (0);
784 }
785 
786 static void
787 sf_buf_invalidate(struct sf_buf *sf)
788 {
789 	vm_page_t m = sf->m;
790 
791 	/*
792 	 * Use pmap_qenter to update the pte for
793 	 * existing mapping, in particular, the PAT
794 	 * settings are recalculated.
795 	 */
796 	pmap_qenter(sf->kva, &m, 1);
797 	pmap_invalidate_cache_range(sf->kva, sf->kva + PAGE_SIZE, FALSE);
798 }
799 
800 /*
801  * Invalidate the cache lines that may belong to the page, if
802  * (possibly old) mapping of the page by sf buffer exists.  Returns
803  * TRUE when mapping was found and cache invalidated.
804  */
805 boolean_t
806 sf_buf_invalidate_cache(vm_page_t m)
807 {
808 
809 	return (sf_buf_process_page(m, sf_buf_invalidate));
810 }
811 
812 /*
813  * Software interrupt handler for queued VM system processing.
814  */
815 void
816 swi_vm(void *dummy)
817 {
818 	if (busdma_swi_pending != 0)
819 		busdma_swi();
820 }
821 
822 /*
823  * Tell whether this address is in some physical memory region.
824  * Currently used by the kernel coredump code in order to avoid
825  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
826  * or other unpredictable behaviour.
827  */
828 
829 int
830 is_physical_memory(vm_paddr_t addr)
831 {
832 
833 #ifdef DEV_ISA
834 	/* The ISA ``memory hole''. */
835 	if (addr >= 0xa0000 && addr < 0x100000)
836 		return 0;
837 #endif
838 
839 	/*
840 	 * stuff other tests for known memory-mapped devices (PCI?)
841 	 * here
842 	 */
843 
844 	return 1;
845 }
846