vm_machdep.c revision 301428
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: stable/10/sys/amd64/amd64/vm_machdep.c 301428 2016-06-05 07:34:10Z dchagin $");
45
46#include "opt_isa.h"
47#include "opt_cpu.h"
48#include "opt_compat.h"
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/bio.h>
53#include <sys/buf.h>
54#include <sys/kernel.h>
55#include <sys/ktr.h>
56#include <sys/lock.h>
57#include <sys/malloc.h>
58#include <sys/mbuf.h>
59#include <sys/mutex.h>
60#include <sys/pioctl.h>
61#include <sys/proc.h>
62#include <sys/smp.h>
63#include <sys/sysctl.h>
64#include <sys/sysent.h>
65#include <sys/unistd.h>
66#include <sys/vnode.h>
67#include <sys/vmmeter.h>
68
69#include <machine/cpu.h>
70#include <machine/md_var.h>
71#include <machine/pcb.h>
72#include <machine/smp.h>
73#include <machine/specialreg.h>
74#include <machine/tss.h>
75
76#include <vm/vm.h>
77#include <vm/vm_extern.h>
78#include <vm/vm_kern.h>
79#include <vm/vm_page.h>
80#include <vm/vm_map.h>
81#include <vm/vm_param.h>
82
83#include <x86/isa/isa.h>
84
85static void	cpu_reset_real(void);
86#ifdef SMP
87static void	cpu_reset_proxy(void);
88static u_int	cpu_reset_proxyid;
89static volatile u_int	cpu_reset_proxy_active;
90#endif
91
92_Static_assert(OFFSETOF_CURTHREAD == offsetof(struct pcpu, pc_curthread),
93    "OFFSETOF_CURTHREAD does not correspond with offset of pc_curthread.");
94_Static_assert(OFFSETOF_CURPCB == offsetof(struct pcpu, pc_curpcb),
95    "OFFSETOF_CURPCB does not correspond with offset of pc_curpcb.");
96
97struct savefpu *
98get_pcb_user_save_td(struct thread *td)
99{
100	vm_offset_t p;
101
102	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
103	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
104	KASSERT((p % XSAVE_AREA_ALIGN) == 0, ("Unaligned pcb_user_save area"));
105	return ((struct savefpu *)p);
106}
107
108struct savefpu *
109get_pcb_user_save_pcb(struct pcb *pcb)
110{
111	vm_offset_t p;
112
113	p = (vm_offset_t)(pcb + 1);
114	return ((struct savefpu *)p);
115}
116
117struct pcb *
118get_pcb_td(struct thread *td)
119{
120	vm_offset_t p;
121
122	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
123	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) -
124	    sizeof(struct pcb);
125	return ((struct pcb *)p);
126}
127
128void *
129alloc_fpusave(int flags)
130{
131	void *res;
132	struct savefpu_ymm *sf;
133
134	res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
135	if (use_xsave) {
136		sf = (struct savefpu_ymm *)res;
137		bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
138		sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
139	}
140	return (res);
141}
142
143/*
144 * Finish a fork operation, with process p2 nearly set up.
145 * Copy and update the pcb, set up the stack so that the child
146 * ready to run and return to user mode.
147 */
148void
149cpu_fork(td1, p2, td2, flags)
150	register struct thread *td1;
151	register struct proc *p2;
152	struct thread *td2;
153	int flags;
154{
155	register struct proc *p1;
156	struct pcb *pcb2;
157	struct mdproc *mdp1, *mdp2;
158	struct proc_ldt *pldt;
159	pmap_t pmap2;
160
161	p1 = td1->td_proc;
162	if ((flags & RFPROC) == 0) {
163		if ((flags & RFMEM) == 0) {
164			/* unshare user LDT */
165			mdp1 = &p1->p_md;
166			mtx_lock(&dt_lock);
167			if ((pldt = mdp1->md_ldt) != NULL &&
168			    pldt->ldt_refcnt > 1 &&
169			    user_ldt_alloc(p1, 1) == NULL)
170				panic("could not copy LDT");
171			mtx_unlock(&dt_lock);
172		}
173		return;
174	}
175
176	/* Ensure that td1's pcb is up to date. */
177	fpuexit(td1);
178
179	/* Point the pcb to the top of the stack */
180	pcb2 = get_pcb_td(td2);
181	td2->td_pcb = pcb2;
182
183	/* Copy td1's pcb */
184	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
185
186	/* Properly initialize pcb_save */
187	pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
188	bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
189	    cpu_max_ext_state_size);
190
191	/* Point mdproc and then copy over td1's contents */
192	mdp2 = &p2->p_md;
193	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
194
195	/*
196	 * Create a new fresh stack for the new process.
197	 * Copy the trap frame for the return to user mode as if from a
198	 * syscall.  This copies most of the user mode register values.
199	 */
200	td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
201	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
202
203	td2->td_frame->tf_rax = 0;		/* Child returns zero */
204	td2->td_frame->tf_rflags &= ~PSL_C;	/* success */
205	td2->td_frame->tf_rdx = 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_rflags &= ~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	pmap2 = vmspace_pmap(p2->p_vmspace);
223	pcb2->pcb_cr3 = pmap2->pm_cr3;
224	pcb2->pcb_r12 = (register_t)fork_return;	/* fork_trampoline argument */
225	pcb2->pcb_rbp = 0;
226	pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
227	pcb2->pcb_rbx = (register_t)td2;		/* fork_trampoline argument */
228	pcb2->pcb_rip = (register_t)fork_trampoline;
229	/*-
230	 * pcb2->pcb_dr*:	cloned above.
231	 * pcb2->pcb_savefpu:	cloned above.
232	 * pcb2->pcb_flags:	cloned above.
233	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
234	 * pcb2->pcb_[fg]sbase:	cloned above
235	 */
236
237	/* Setup to release spin count in fork_exit(). */
238	td2->td_md.md_spinlock_count = 1;
239	td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
240
241	/* As an i386, do not copy io permission bitmap. */
242	pcb2->pcb_tssp = NULL;
243
244	/* New segment registers. */
245	set_pcb_flags(pcb2, PCB_FULL_IRET);
246
247	/* Copy the LDT, if necessary. */
248	mdp1 = &td1->td_proc->p_md;
249	mdp2 = &p2->p_md;
250	mtx_lock(&dt_lock);
251	if (mdp1->md_ldt != NULL) {
252		if (flags & RFMEM) {
253			mdp1->md_ldt->ldt_refcnt++;
254			mdp2->md_ldt = mdp1->md_ldt;
255			bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct
256			    system_segment_descriptor));
257		} else {
258			mdp2->md_ldt = NULL;
259			mdp2->md_ldt = user_ldt_alloc(p2, 0);
260			if (mdp2->md_ldt == NULL)
261				panic("could not copy LDT");
262			amd64_set_ldt_data(td2, 0, max_ldt_segment,
263			    (struct user_segment_descriptor *)
264			    mdp1->md_ldt->ldt_base);
265		}
266	} else
267		mdp2->md_ldt = NULL;
268	mtx_unlock(&dt_lock);
269
270	/*
271	 * Now, cpu_switch() can schedule the new process.
272	 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
273	 * containing the return address when exiting cpu_switch.
274	 * This will normally be to fork_trampoline(), which will have
275	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
276	 * will set up a stack to call fork_return(p, frame); to complete
277	 * the return to user-mode.
278	 */
279}
280
281/*
282 * Intercept the return address from a freshly forked process that has NOT
283 * been scheduled yet.
284 *
285 * This is needed to make kernel threads stay in kernel mode.
286 */
287void
288cpu_set_fork_handler(td, func, arg)
289	struct thread *td;
290	void (*func)(void *);
291	void *arg;
292{
293	/*
294	 * Note that the trap frame follows the args, so the function
295	 * is really called like this:  func(arg, frame);
296	 */
297	td->td_pcb->pcb_r12 = (long) func;	/* function */
298	td->td_pcb->pcb_rbx = (long) arg;	/* first arg */
299}
300
301void
302cpu_exit(struct thread *td)
303{
304
305	/*
306	 * If this process has a custom LDT, release it.
307	 */
308	mtx_lock(&dt_lock);
309	if (td->td_proc->p_md.md_ldt != 0)
310		user_ldt_free(td);
311	else
312		mtx_unlock(&dt_lock);
313}
314
315void
316cpu_thread_exit(struct thread *td)
317{
318	struct pcb *pcb;
319
320	critical_enter();
321	if (td == PCPU_GET(fpcurthread))
322		fpudrop();
323	critical_exit();
324
325	pcb = td->td_pcb;
326
327	/* Disable any hardware breakpoints. */
328	if (pcb->pcb_flags & PCB_DBREGS) {
329		reset_dbregs();
330		clear_pcb_flags(pcb, PCB_DBREGS);
331	}
332}
333
334void
335cpu_thread_clean(struct thread *td)
336{
337	struct pcb *pcb;
338
339	pcb = td->td_pcb;
340
341	/*
342	 * Clean TSS/iomap
343	 */
344	if (pcb->pcb_tssp != NULL) {
345		kmem_free(kernel_arena, (vm_offset_t)pcb->pcb_tssp,
346		    ctob(IOPAGES + 1));
347		pcb->pcb_tssp = NULL;
348	}
349}
350
351void
352cpu_thread_swapin(struct thread *td)
353{
354}
355
356void
357cpu_thread_swapout(struct thread *td)
358{
359}
360
361void
362cpu_thread_alloc(struct thread *td)
363{
364	struct pcb *pcb;
365	struct xstate_hdr *xhdr;
366
367	td->td_pcb = pcb = get_pcb_td(td);
368	td->td_frame = (struct trapframe *)pcb - 1;
369	pcb->pcb_save = get_pcb_user_save_pcb(pcb);
370	if (use_xsave) {
371		xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
372		bzero(xhdr, sizeof(*xhdr));
373		xhdr->xstate_bv = xsave_mask;
374	}
375}
376
377void
378cpu_thread_free(struct thread *td)
379{
380
381	cpu_thread_clean(td);
382}
383
384void
385cpu_set_syscall_retval(struct thread *td, int error)
386{
387
388	switch (error) {
389	case 0:
390		td->td_frame->tf_rax = td->td_retval[0];
391		td->td_frame->tf_rdx = td->td_retval[1];
392		td->td_frame->tf_rflags &= ~PSL_C;
393		break;
394
395	case ERESTART:
396		/*
397		 * Reconstruct pc, we know that 'syscall' is 2 bytes,
398		 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
399		 * We saved this in tf_err.
400		 * %r10 (which was holding the value of %rcx) is restored
401		 * for the next iteration.
402		 * %r10 restore is only required for freebsd/amd64 processes,
403		 * but shall be innocent for any ia32 ABI.
404		 *
405		 * Require full context restore to get the arguments
406		 * in the registers reloaded at return to usermode.
407		 */
408		td->td_frame->tf_rip -= td->td_frame->tf_err;
409		td->td_frame->tf_r10 = td->td_frame->tf_rcx;
410		set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
411		break;
412
413	case EJUSTRETURN:
414		break;
415
416	default:
417		td->td_frame->tf_rax = SV_ABI_ERRNO(td->td_proc, error);
418		td->td_frame->tf_rflags |= PSL_C;
419		break;
420	}
421}
422
423/*
424 * Initialize machine state (pcb and trap frame) for a new thread about to
425 * upcall. Put enough state in the new thread's PCB to get it to go back
426 * userret(), where we can intercept it again to set the return (upcall)
427 * Address and stack, along with those from upcals that are from other sources
428 * such as those generated in thread_userret() itself.
429 */
430void
431cpu_set_upcall(struct thread *td, struct thread *td0)
432{
433	struct pcb *pcb2;
434
435	/* Point the pcb to the top of the stack. */
436	pcb2 = td->td_pcb;
437
438	/*
439	 * Copy the upcall pcb.  This loads kernel regs.
440	 * Those not loaded individually below get their default
441	 * values here.
442	 */
443	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
444	clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE |
445	    PCB_KERNFPU);
446	pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
447	bcopy(get_pcb_user_save_td(td0), pcb2->pcb_save,
448	    cpu_max_ext_state_size);
449	set_pcb_flags(pcb2, PCB_FULL_IRET);
450
451	/*
452	 * Create a new fresh stack for the new thread.
453	 */
454	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
455
456	/* If the current thread has the trap bit set (i.e. a debugger had
457	 * single stepped the process to the system call), we need to clear
458	 * the trap flag from the new frame. Otherwise, the new thread will
459	 * receive a (likely unexpected) SIGTRAP when it executes the first
460	 * instruction after returning to userland.
461	 */
462	td->td_frame->tf_rflags &= ~PSL_T;
463
464	/*
465	 * Set registers for trampoline to user mode.  Leave space for the
466	 * return address on stack.  These are the kernel mode register values.
467	 */
468	pcb2->pcb_r12 = (register_t)fork_return;	    /* trampoline arg */
469	pcb2->pcb_rbp = 0;
470	pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *);	/* trampoline arg */
471	pcb2->pcb_rbx = (register_t)td;			    /* trampoline arg */
472	pcb2->pcb_rip = (register_t)fork_trampoline;
473	/*
474	 * If we didn't copy the pcb, we'd need to do the following registers:
475	 * pcb2->pcb_cr3:	cloned above.
476	 * pcb2->pcb_dr*:	cloned above.
477	 * pcb2->pcb_savefpu:	cloned above.
478	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
479	 * pcb2->pcb_[fg]sbase: cloned above
480	 */
481
482	/* Setup to release spin count in fork_exit(). */
483	td->td_md.md_spinlock_count = 1;
484	td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
485}
486
487/*
488 * Set that machine state for performing an upcall that has to
489 * be done in thread_userret() so that those upcalls generated
490 * in thread_userret() itself can be done as well.
491 */
492void
493cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
494	stack_t *stack)
495{
496
497	/*
498	 * Do any extra cleaning that needs to be done.
499	 * The thread may have optional components
500	 * that are not present in a fresh thread.
501	 * This may be a recycled thread so make it look
502	 * as though it's newly allocated.
503	 */
504	cpu_thread_clean(td);
505
506#ifdef COMPAT_FREEBSD32
507	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
508		/*
509	 	 * Set the trap frame to point at the beginning of the uts
510		 * function.
511		 */
512		td->td_frame->tf_rbp = 0;
513		td->td_frame->tf_rsp =
514		   (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
515		td->td_frame->tf_rip = (uintptr_t)entry;
516
517		/*
518		 * Pass the address of the mailbox for this kse to the uts
519		 * function as a parameter on the stack.
520		 */
521		suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
522		    (uint32_t)(uintptr_t)arg);
523
524		return;
525	}
526#endif
527
528	/*
529	 * Set the trap frame to point at the beginning of the uts
530	 * function.
531	 */
532	td->td_frame->tf_rbp = 0;
533	td->td_frame->tf_rsp =
534	    ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
535	td->td_frame->tf_rsp -= 8;
536	td->td_frame->tf_rip = (register_t)entry;
537	td->td_frame->tf_ds = _udatasel;
538	td->td_frame->tf_es = _udatasel;
539	td->td_frame->tf_fs = _ufssel;
540	td->td_frame->tf_gs = _ugssel;
541	td->td_frame->tf_flags = TF_HASSEGS;
542
543	/*
544	 * Pass the address of the mailbox for this kse to the uts
545	 * function as a parameter on the stack.
546	 */
547	td->td_frame->tf_rdi = (register_t)arg;
548}
549
550int
551cpu_set_user_tls(struct thread *td, void *tls_base)
552{
553	struct pcb *pcb;
554
555	if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
556		return (EINVAL);
557
558	pcb = td->td_pcb;
559	set_pcb_flags(pcb, PCB_FULL_IRET);
560#ifdef COMPAT_FREEBSD32
561	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
562		pcb->pcb_gsbase = (register_t)tls_base;
563		return (0);
564	}
565#endif
566	pcb->pcb_fsbase = (register_t)tls_base;
567	return (0);
568}
569
570#ifdef SMP
571static void
572cpu_reset_proxy()
573{
574	cpuset_t tcrp;
575
576	cpu_reset_proxy_active = 1;
577	while (cpu_reset_proxy_active == 1)
578		ia32_pause(); /* Wait for other cpu to see that we've started */
579
580	CPU_SETOF(cpu_reset_proxyid, &tcrp);
581	stop_cpus(tcrp);
582	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
583	DELAY(1000000);
584	cpu_reset_real();
585}
586#endif
587
588void
589cpu_reset()
590{
591#ifdef SMP
592	cpuset_t map;
593	u_int cnt;
594
595	if (smp_started) {
596		map = all_cpus;
597		CPU_CLR(PCPU_GET(cpuid), &map);
598		CPU_NAND(&map, &stopped_cpus);
599		if (!CPU_EMPTY(&map)) {
600			printf("cpu_reset: Stopping other CPUs\n");
601			stop_cpus(map);
602		}
603
604		if (PCPU_GET(cpuid) != 0) {
605			cpu_reset_proxyid = PCPU_GET(cpuid);
606			cpustop_restartfunc = cpu_reset_proxy;
607			cpu_reset_proxy_active = 0;
608			printf("cpu_reset: Restarting BSP\n");
609
610			/* Restart CPU #0. */
611			CPU_SETOF(0, &started_cpus);
612			wmb();
613
614			cnt = 0;
615			while (cpu_reset_proxy_active == 0 && cnt < 10000000) {
616				ia32_pause();
617				cnt++;	/* Wait for BSP to announce restart */
618			}
619			if (cpu_reset_proxy_active == 0)
620				printf("cpu_reset: Failed to restart BSP\n");
621			enable_intr();
622			cpu_reset_proxy_active = 2;
623
624			while (1)
625				ia32_pause();
626			/* NOTREACHED */
627		}
628
629		DELAY(1000000);
630	}
631#endif
632	cpu_reset_real();
633	/* NOTREACHED */
634}
635
636static void
637cpu_reset_real()
638{
639	struct region_descriptor null_idt;
640	int b;
641
642	disable_intr();
643
644	/*
645	 * Attempt to do a CPU reset via the keyboard controller,
646	 * do not turn off GateA20, as any machine that fails
647	 * to do the reset here would then end up in no man's land.
648	 */
649	outb(IO_KBD + 4, 0xFE);
650	DELAY(500000);	/* wait 0.5 sec to see if that did it */
651
652	/*
653	 * Attempt to force a reset via the Reset Control register at
654	 * I/O port 0xcf9.  Bit 2 forces a system reset when it
655	 * transitions from 0 to 1.  Bit 1 selects the type of reset
656	 * to attempt: 0 selects a "soft" reset, and 1 selects a
657	 * "hard" reset.  We try a "hard" reset.  The first write sets
658	 * bit 1 to select a "hard" reset and clears bit 2.  The
659	 * second write forces a 0 -> 1 transition in bit 2 to trigger
660	 * a reset.
661	 */
662	outb(0xcf9, 0x2);
663	outb(0xcf9, 0x6);
664	DELAY(500000);  /* wait 0.5 sec to see if that did it */
665
666	/*
667	 * Attempt to force a reset via the Fast A20 and Init register
668	 * at I/O port 0x92.  Bit 1 serves as an alternate A20 gate.
669	 * Bit 0 asserts INIT# when set to 1.  We are careful to only
670	 * preserve bit 1 while setting bit 0.  We also must clear bit
671	 * 0 before setting it if it isn't already clear.
672	 */
673	b = inb(0x92);
674	if (b != 0xff) {
675		if ((b & 0x1) != 0)
676			outb(0x92, b & 0xfe);
677		outb(0x92, b | 0x1);
678		DELAY(500000);  /* wait 0.5 sec to see if that did it */
679	}
680
681	printf("No known reset method worked, attempting CPU shutdown\n");
682	DELAY(1000000);	/* wait 1 sec for printf to complete */
683
684	/* Wipe the IDT. */
685	null_idt.rd_limit = 0;
686	null_idt.rd_base = 0;
687	lidt(&null_idt);
688
689	/* "good night, sweet prince .... <THUNK!>" */
690	breakpoint();
691
692	/* NOTREACHED */
693	while(1);
694}
695
696/*
697 * Software interrupt handler for queued VM system processing.
698 */
699void
700swi_vm(void *dummy)
701{
702	if (busdma_swi_pending != 0)
703		busdma_swi();
704}
705
706/*
707 * Tell whether this address is in some physical memory region.
708 * Currently used by the kernel coredump code in order to avoid
709 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
710 * or other unpredictable behaviour.
711 */
712
713int
714is_physical_memory(vm_paddr_t addr)
715{
716
717#ifdef DEV_ISA
718	/* The ISA ``memory hole''. */
719	if (addr >= 0xa0000 && addr < 0x100000)
720		return 0;
721#endif
722
723	/*
724	 * stuff other tests for known memory-mapped devices (PCI?)
725	 * here
726	 */
727
728	return 1;
729}
730