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