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