vm_machdep.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1982, 1986 The Regents of the University of California.
5 * Copyright (c) 1989, 1990 William Jolitz
6 * Copyright (c) 1994 John Dyson
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
11 * Science Department, and William Jolitz.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgement:
23 *	This product includes software developed by the University of
24 *	California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
42 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
43 */
44
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: stable/11/sys/amd64/amd64/vm_machdep.c 330897 2018-03-14 03:19:51Z eadler $");
47
48#include "opt_isa.h"
49#include "opt_cpu.h"
50#include "opt_compat.h"
51
52#include <sys/param.h>
53#include <sys/systm.h>
54#include <sys/bio.h>
55#include <sys/buf.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/smp.h>
65#include <sys/sysctl.h>
66#include <sys/sysent.h>
67#include <sys/unistd.h>
68#include <sys/vnode.h>
69#include <sys/vmmeter.h>
70
71#include <machine/cpu.h>
72#include <machine/md_var.h>
73#include <machine/pcb.h>
74#include <machine/smp.h>
75#include <machine/specialreg.h>
76#include <machine/tss.h>
77
78#include <vm/vm.h>
79#include <vm/vm_extern.h>
80#include <vm/vm_kern.h>
81#include <vm/vm_page.h>
82#include <vm/vm_map.h>
83#include <vm/vm_param.h>
84
85#include <isa/isareg.h>
86
87static void	cpu_reset_real(void);
88#ifdef SMP
89static void	cpu_reset_proxy(void);
90static u_int	cpu_reset_proxyid;
91static volatile u_int	cpu_reset_proxy_active;
92#endif
93
94_Static_assert(OFFSETOF_CURTHREAD == offsetof(struct pcpu, pc_curthread),
95    "OFFSETOF_CURTHREAD does not correspond with offset of pc_curthread.");
96_Static_assert(OFFSETOF_CURPCB == offsetof(struct pcpu, pc_curpcb),
97    "OFFSETOF_CURPCB does not correspond with offset of pc_curpcb.");
98_Static_assert(OFFSETOF_MONITORBUF == offsetof(struct pcpu, pc_monitorbuf),
99    "OFFSETOF_MONINORBUF does not correspond with offset of pc_monitorbuf.");
100
101struct savefpu *
102get_pcb_user_save_td(struct thread *td)
103{
104	vm_offset_t p;
105
106	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
107	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN);
108	KASSERT((p % XSAVE_AREA_ALIGN) == 0, ("Unaligned pcb_user_save area"));
109	return ((struct savefpu *)p);
110}
111
112struct savefpu *
113get_pcb_user_save_pcb(struct pcb *pcb)
114{
115	vm_offset_t p;
116
117	p = (vm_offset_t)(pcb + 1);
118	return ((struct savefpu *)p);
119}
120
121struct pcb *
122get_pcb_td(struct thread *td)
123{
124	vm_offset_t p;
125
126	p = td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
127	    roundup2(cpu_max_ext_state_size, XSAVE_AREA_ALIGN) -
128	    sizeof(struct pcb);
129	return ((struct pcb *)p);
130}
131
132void *
133alloc_fpusave(int flags)
134{
135	void *res;
136	struct savefpu_ymm *sf;
137
138	res = malloc(cpu_max_ext_state_size, M_DEVBUF, flags);
139	if (use_xsave) {
140		sf = (struct savefpu_ymm *)res;
141		bzero(&sf->sv_xstate.sx_hd, sizeof(sf->sv_xstate.sx_hd));
142		sf->sv_xstate.sx_hd.xstate_bv = xsave_mask;
143	}
144	return (res);
145}
146
147/*
148 * Finish a fork operation, with process p2 nearly set up.
149 * Copy and update the pcb, set up the stack so that the child
150 * ready to run and return to user mode.
151 */
152void
153cpu_fork(td1, p2, td2, flags)
154	register struct thread *td1;
155	register struct proc *p2;
156	struct thread *td2;
157	int flags;
158{
159	register struct proc *p1;
160	struct pcb *pcb2;
161	struct mdproc *mdp1, *mdp2;
162	struct proc_ldt *pldt;
163
164	p1 = td1->td_proc;
165	if ((flags & RFPROC) == 0) {
166		if ((flags & RFMEM) == 0) {
167			/* unshare user LDT */
168			mdp1 = &p1->p_md;
169			mtx_lock(&dt_lock);
170			if ((pldt = mdp1->md_ldt) != NULL &&
171			    pldt->ldt_refcnt > 1 &&
172			    user_ldt_alloc(p1, 1) == NULL)
173				panic("could not copy LDT");
174			mtx_unlock(&dt_lock);
175		}
176		return;
177	}
178
179	/* Ensure that td1's pcb is up to date. */
180	fpuexit(td1);
181	update_pcb_bases(td1->td_pcb);
182
183	/* Point the pcb to the top of the stack */
184	pcb2 = get_pcb_td(td2);
185	td2->td_pcb = pcb2;
186
187	/* Copy td1's pcb */
188	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
189
190	/* Properly initialize pcb_save */
191	pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
192	bcopy(get_pcb_user_save_td(td1), get_pcb_user_save_pcb(pcb2),
193	    cpu_max_ext_state_size);
194
195	/* Point mdproc and then copy over td1's contents */
196	mdp2 = &p2->p_md;
197	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
198
199	/*
200	 * Create a new fresh stack for the new process.
201	 * Copy the trap frame for the return to user mode as if from a
202	 * syscall.  This copies most of the user mode register values.
203	 */
204	td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
205	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
206
207	td2->td_frame->tf_rax = 0;		/* Child returns zero */
208	td2->td_frame->tf_rflags &= ~PSL_C;	/* success */
209	td2->td_frame->tf_rdx = 1;
210
211	/*
212	 * If the parent process has the trap bit set (i.e. a debugger had
213	 * single stepped the process to the system call), we need to clear
214	 * the trap flag from the new frame unless the debugger had set PF_FORK
215	 * on the parent.  Otherwise, the child will receive a (likely
216	 * unexpected) SIGTRAP when it executes the first instruction after
217	 * returning  to userland.
218	 */
219	if ((p1->p_pfsflags & PF_FORK) == 0)
220		td2->td_frame->tf_rflags &= ~PSL_T;
221
222	/*
223	 * Set registers for trampoline to user mode.  Leave space for the
224	 * return address on stack.  These are the kernel mode register values.
225	 */
226	pcb2->pcb_r12 = (register_t)fork_return;	/* fork_trampoline argument */
227	pcb2->pcb_rbp = 0;
228	pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
229	pcb2->pcb_rbx = (register_t)td2;		/* fork_trampoline argument */
230	pcb2->pcb_rip = (register_t)fork_trampoline;
231	/*-
232	 * pcb2->pcb_dr*:	cloned above.
233	 * pcb2->pcb_savefpu:	cloned above.
234	 * pcb2->pcb_flags:	cloned above.
235	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
236	 * pcb2->pcb_[fg]sbase:	cloned above
237	 */
238
239	/* Setup to release spin count in fork_exit(). */
240	td2->td_md.md_spinlock_count = 1;
241	td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
242	td2->td_md.md_invl_gen.gen = 0;
243
244	/* As an i386, do not copy io permission bitmap. */
245	pcb2->pcb_tssp = NULL;
246
247	/* New segment registers. */
248	set_pcb_flags_raw(pcb2, PCB_FULL_IRET);
249
250	/* Copy the LDT, if necessary. */
251	mdp1 = &td1->td_proc->p_md;
252	mdp2 = &p2->p_md;
253	mtx_lock(&dt_lock);
254	if (mdp1->md_ldt != NULL) {
255		if (flags & RFMEM) {
256			mdp1->md_ldt->ldt_refcnt++;
257			mdp2->md_ldt = mdp1->md_ldt;
258			bcopy(&mdp1->md_ldt_sd, &mdp2->md_ldt_sd, sizeof(struct
259			    system_segment_descriptor));
260		} else {
261			mdp2->md_ldt = NULL;
262			mdp2->md_ldt = user_ldt_alloc(p2, 0);
263			if (mdp2->md_ldt == NULL)
264				panic("could not copy LDT");
265			amd64_set_ldt_data(td2, 0, max_ldt_segment,
266			    (struct user_segment_descriptor *)
267			    mdp1->md_ldt->ldt_base);
268		}
269	} else
270		mdp2->md_ldt = NULL;
271	mtx_unlock(&dt_lock);
272
273	/*
274	 * Now, cpu_switch() can schedule the new process.
275	 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
276	 * containing the return address when exiting cpu_switch.
277	 * This will normally be to fork_trampoline(), which will have
278	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
279	 * will set up a stack to call fork_return(p, frame); to complete
280	 * the return to user-mode.
281	 */
282}
283
284/*
285 * Intercept the return address from a freshly forked process that has NOT
286 * been scheduled yet.
287 *
288 * This is needed to make kernel threads stay in kernel mode.
289 */
290void
291cpu_fork_kthread_handler(struct thread *td, void (*func)(void *), 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		pmap_pti_remove_kva((vm_offset_t)pcb->pcb_tssp,
346		    (vm_offset_t)pcb->pcb_tssp + ctob(IOPAGES + 1));
347		kmem_free(kernel_arena, (vm_offset_t)pcb->pcb_tssp,
348		    ctob(IOPAGES + 1));
349		pcb->pcb_tssp = NULL;
350	}
351}
352
353void
354cpu_thread_swapin(struct thread *td)
355{
356}
357
358void
359cpu_thread_swapout(struct thread *td)
360{
361}
362
363void
364cpu_thread_alloc(struct thread *td)
365{
366	struct pcb *pcb;
367	struct xstate_hdr *xhdr;
368
369	td->td_pcb = pcb = get_pcb_td(td);
370	td->td_frame = (struct trapframe *)pcb - 1;
371	pcb->pcb_save = get_pcb_user_save_pcb(pcb);
372	if (use_xsave) {
373		xhdr = (struct xstate_hdr *)(pcb->pcb_save + 1);
374		bzero(xhdr, sizeof(*xhdr));
375		xhdr->xstate_bv = xsave_mask;
376	}
377}
378
379void
380cpu_thread_free(struct thread *td)
381{
382
383	cpu_thread_clean(td);
384}
385
386void
387cpu_set_syscall_retval(struct thread *td, int error)
388{
389
390	switch (error) {
391	case 0:
392		td->td_frame->tf_rax = td->td_retval[0];
393		td->td_frame->tf_rdx = td->td_retval[1];
394		td->td_frame->tf_rflags &= ~PSL_C;
395		break;
396
397	case ERESTART:
398		/*
399		 * Reconstruct pc, we know that 'syscall' is 2 bytes,
400		 * lcall $X,y is 7 bytes, int 0x80 is 2 bytes.
401		 * We saved this in tf_err.
402		 * %r10 (which was holding the value of %rcx) is restored
403		 * for the next iteration.
404		 * %r10 restore is only required for freebsd/amd64 processes,
405		 * but shall be innocent for any ia32 ABI.
406		 *
407		 * Require full context restore to get the arguments
408		 * in the registers reloaded at return to usermode.
409		 */
410		td->td_frame->tf_rip -= td->td_frame->tf_err;
411		td->td_frame->tf_r10 = td->td_frame->tf_rcx;
412		set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
413		break;
414
415	case EJUSTRETURN:
416		break;
417
418	default:
419		td->td_frame->tf_rax = SV_ABI_ERRNO(td->td_proc, error);
420		td->td_frame->tf_rflags |= PSL_C;
421		break;
422	}
423}
424
425/*
426 * Initialize machine state, mostly pcb and trap frame for a new
427 * thread, about to return to userspace.  Put enough state in the new
428 * thread's PCB to get it to go back to the fork_return(), which
429 * finalizes the thread state and handles peculiarities of the first
430 * return to userspace for the new thread.
431 */
432void
433cpu_copy_thread(struct thread *td, struct thread *td0)
434{
435	struct pcb *pcb2;
436
437	/* Point the pcb to the top of the stack. */
438	pcb2 = td->td_pcb;
439
440	/*
441	 * Copy the upcall pcb.  This loads kernel regs.
442	 * Those not loaded individually below get their default
443	 * values here.
444	 */
445	update_pcb_bases(td0->td_pcb);
446	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
447	clear_pcb_flags(pcb2, PCB_FPUINITDONE | PCB_USERFPUINITDONE |
448	    PCB_KERNFPU);
449	pcb2->pcb_save = get_pcb_user_save_pcb(pcb2);
450	bcopy(get_pcb_user_save_td(td0), pcb2->pcb_save,
451	    cpu_max_ext_state_size);
452	set_pcb_flags_raw(pcb2, PCB_FULL_IRET);
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_rflags &= ~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_r12 = (register_t)fork_return;	    /* trampoline arg */
472	pcb2->pcb_rbp = 0;
473	pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *);	/* trampoline arg */
474	pcb2->pcb_rbx = (register_t)td;			    /* trampoline arg */
475	pcb2->pcb_rip = (register_t)fork_trampoline;
476	/*
477	 * If we didn't copy the pcb, we'd need to do the following registers:
478	 * pcb2->pcb_dr*:	cloned above.
479	 * pcb2->pcb_savefpu:	cloned above.
480	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
481	 * pcb2->pcb_[fg]sbase: cloned above
482	 */
483
484	/* Setup to release spin count in fork_exit(). */
485	td->td_md.md_spinlock_count = 1;
486	td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
487}
488
489/*
490 * Set that machine state for performing an upcall that starts
491 * the entry function with the given argument.
492 */
493void
494cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
495    stack_t *stack)
496{
497
498	/*
499	 * Do any extra cleaning that needs to be done.
500	 * The thread may have optional components
501	 * that are not present in a fresh thread.
502	 * This may be a recycled thread so make it look
503	 * as though it's newly allocated.
504	 */
505	cpu_thread_clean(td);
506
507#ifdef COMPAT_FREEBSD32
508	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
509		/*
510		 * Set the trap frame to point at the beginning of the entry
511		 * function.
512		 */
513		td->td_frame->tf_rbp = 0;
514		td->td_frame->tf_rsp =
515		   (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
516		td->td_frame->tf_rip = (uintptr_t)entry;
517
518		/* Return address sentinel value to stop stack unwinding. */
519		suword32((void *)td->td_frame->tf_rsp, 0);
520
521		/* Pass the argument to the entry point. */
522		suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
523		    (uint32_t)(uintptr_t)arg);
524
525		return;
526	}
527#endif
528
529	/*
530	 * Set the trap frame to point at the beginning of the uts
531	 * function.
532	 */
533	td->td_frame->tf_rbp = 0;
534	td->td_frame->tf_rsp =
535	    ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
536	td->td_frame->tf_rsp -= 8;
537	td->td_frame->tf_rip = (register_t)entry;
538	td->td_frame->tf_ds = _udatasel;
539	td->td_frame->tf_es = _udatasel;
540	td->td_frame->tf_fs = _ufssel;
541	td->td_frame->tf_gs = _ugssel;
542	td->td_frame->tf_flags = TF_HASSEGS;
543
544	/* Return address sentinel value to stop stack unwinding. */
545	suword((void *)td->td_frame->tf_rsp, 0);
546
547	/* Pass the argument to the entry point. */
548	td->td_frame->tf_rdi = (register_t)arg;
549}
550
551int
552cpu_set_user_tls(struct thread *td, void *tls_base)
553{
554	struct pcb *pcb;
555
556	if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
557		return (EINVAL);
558
559	pcb = td->td_pcb;
560	set_pcb_flags(pcb, PCB_FULL_IRET);
561#ifdef COMPAT_FREEBSD32
562	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
563		pcb->pcb_gsbase = (register_t)tls_base;
564		return (0);
565	}
566#endif
567	pcb->pcb_fsbase = (register_t)tls_base;
568	return (0);
569}
570
571#ifdef SMP
572static void
573cpu_reset_proxy()
574{
575	cpuset_t tcrp;
576
577	cpu_reset_proxy_active = 1;
578	while (cpu_reset_proxy_active == 1)
579		ia32_pause(); /* Wait for other cpu to see that we've started */
580
581	CPU_SETOF(cpu_reset_proxyid, &tcrp);
582	stop_cpus(tcrp);
583	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
584	DELAY(1000000);
585	cpu_reset_real();
586}
587#endif
588
589void
590cpu_reset()
591{
592#ifdef SMP
593	cpuset_t map;
594	u_int cnt;
595
596	if (smp_started) {
597		map = all_cpus;
598		CPU_CLR(PCPU_GET(cpuid), &map);
599		CPU_NAND(&map, &stopped_cpus);
600		if (!CPU_EMPTY(&map)) {
601			printf("cpu_reset: Stopping other CPUs\n");
602			stop_cpus(map);
603		}
604
605		if (PCPU_GET(cpuid) != 0) {
606			cpu_reset_proxyid = PCPU_GET(cpuid);
607			cpustop_restartfunc = cpu_reset_proxy;
608			cpu_reset_proxy_active = 0;
609			printf("cpu_reset: Restarting BSP\n");
610
611			/* Restart CPU #0. */
612			CPU_SETOF(0, &started_cpus);
613			wmb();
614
615			cnt = 0;
616			while (cpu_reset_proxy_active == 0 && cnt < 10000000) {
617				ia32_pause();
618				cnt++;	/* Wait for BSP to announce restart */
619			}
620			if (cpu_reset_proxy_active == 0)
621				printf("cpu_reset: Failed to restart BSP\n");
622			enable_intr();
623			cpu_reset_proxy_active = 2;
624
625			while (1)
626				ia32_pause();
627			/* NOTREACHED */
628		}
629
630		DELAY(1000000);
631	}
632#endif
633	cpu_reset_real();
634	/* NOTREACHED */
635}
636
637static void
638cpu_reset_real()
639{
640	struct region_descriptor null_idt;
641	int b;
642
643	disable_intr();
644
645	/*
646	 * Attempt to do a CPU reset via the keyboard controller,
647	 * do not turn off GateA20, as any machine that fails
648	 * to do the reset here would then end up in no man's land.
649	 */
650	outb(IO_KBD + 4, 0xFE);
651	DELAY(500000);	/* wait 0.5 sec to see if that did it */
652
653	/*
654	 * Attempt to force a reset via the Reset Control register at
655	 * I/O port 0xcf9.  Bit 2 forces a system reset when it
656	 * transitions from 0 to 1.  Bit 1 selects the type of reset
657	 * to attempt: 0 selects a "soft" reset, and 1 selects a
658	 * "hard" reset.  We try a "hard" reset.  The first write sets
659	 * bit 1 to select a "hard" reset and clears bit 2.  The
660	 * second write forces a 0 -> 1 transition in bit 2 to trigger
661	 * a reset.
662	 */
663	outb(0xcf9, 0x2);
664	outb(0xcf9, 0x6);
665	DELAY(500000);  /* wait 0.5 sec to see if that did it */
666
667	/*
668	 * Attempt to force a reset via the Fast A20 and Init register
669	 * at I/O port 0x92.  Bit 1 serves as an alternate A20 gate.
670	 * Bit 0 asserts INIT# when set to 1.  We are careful to only
671	 * preserve bit 1 while setting bit 0.  We also must clear bit
672	 * 0 before setting it if it isn't already clear.
673	 */
674	b = inb(0x92);
675	if (b != 0xff) {
676		if ((b & 0x1) != 0)
677			outb(0x92, b & 0xfe);
678		outb(0x92, b | 0x1);
679		DELAY(500000);  /* wait 0.5 sec to see if that did it */
680	}
681
682	printf("No known reset method worked, attempting CPU shutdown\n");
683	DELAY(1000000);	/* wait 1 sec for printf to complete */
684
685	/* Wipe the IDT. */
686	null_idt.rd_limit = 0;
687	null_idt.rd_base = 0;
688	lidt(&null_idt);
689
690	/* "good night, sweet prince .... <THUNK!>" */
691	breakpoint();
692
693	/* NOTREACHED */
694	while(1);
695}
696
697/*
698 * Software interrupt handler for queued VM system processing.
699 */
700void
701swi_vm(void *dummy)
702{
703	if (busdma_swi_pending != 0)
704		busdma_swi();
705}
706
707/*
708 * Tell whether this address is in some physical memory region.
709 * Currently used by the kernel coredump code in order to avoid
710 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
711 * or other unpredictable behaviour.
712 */
713
714int
715is_physical_memory(vm_paddr_t addr)
716{
717
718#ifdef DEV_ISA
719	/* The ISA ``memory hole''. */
720	if (addr >= 0xa0000 && addr < 0x100000)
721		return 0;
722#endif
723
724	/*
725	 * stuff other tests for known memory-mapped devices (PCI?)
726	 * here
727	 */
728
729	return 1;
730}
731