]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/amd64/vm_machdep.c
x86 cpu_reset: if failed to switch to BSP proceed to cpu_reset_real
[FreeBSD/FreeBSD.git] / sys / amd64 / amd64 / vm_machdep.c
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$");
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
87 static void     cpu_reset_real(void);
88 #ifdef SMP
89 static void     cpu_reset_proxy(void);
90 static u_int    cpu_reset_proxyid;
91 static 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
101 struct savefpu *
102 get_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
112 struct savefpu *
113 get_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
121 struct pcb *
122 get_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
132 void *
133 alloc_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  */
152 void
153 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
154 {
155         struct proc *p1;
156         struct pcb *pcb2;
157         struct mdproc *mdp1, *mdp2;
158         struct proc_ldt *pldt;
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         update_pcb_bases(td1->td_pcb);
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         pcb2->pcb_r12 = (register_t)fork_return;        /* fork_trampoline argument */
223         pcb2->pcb_rbp = 0;
224         pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
225         pcb2->pcb_rbx = (register_t)td2;                /* fork_trampoline argument */
226         pcb2->pcb_rip = (register_t)fork_trampoline;
227         /*-
228          * pcb2->pcb_dr*:       cloned above.
229          * pcb2->pcb_savefpu:   cloned above.
230          * pcb2->pcb_flags:     cloned above.
231          * pcb2->pcb_onfault:   cloned above (always NULL here?).
232          * pcb2->pcb_[fg]sbase: cloned above
233          */
234
235         /* Setup to release spin count in fork_exit(). */
236         td2->td_md.md_spinlock_count = 1;
237         td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
238         td2->td_md.md_invl_gen.gen = 0;
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_raw(pcb2, PCB_FULL_IRET);
245
246         /* Copy the LDT, if necessary. */
247         mdp1 = &td1->td_proc->p_md;
248         mdp2 = &p2->p_md;
249         if (mdp1->md_ldt == NULL) {
250                 mdp2->md_ldt = NULL;
251                 return;
252         }
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  */
290 void
291 cpu_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
301 void
302 cpu_exit(struct thread *td)
303 {
304
305         /*
306          * If this process has a custom LDT, release it.
307          */
308         if (td->td_proc->p_md.md_ldt != NULL)
309                 user_ldt_free(td);
310 }
311
312 void
313 cpu_thread_exit(struct thread *td)
314 {
315         struct pcb *pcb;
316
317         critical_enter();
318         if (td == PCPU_GET(fpcurthread))
319                 fpudrop();
320         critical_exit();
321
322         pcb = td->td_pcb;
323
324         /* Disable any hardware breakpoints. */
325         if (pcb->pcb_flags & PCB_DBREGS) {
326                 reset_dbregs();
327                 clear_pcb_flags(pcb, PCB_DBREGS);
328         }
329 }
330
331 void
332 cpu_thread_clean(struct thread *td)
333 {
334         struct pcb *pcb;
335
336         pcb = td->td_pcb;
337
338         /*
339          * Clean TSS/iomap
340          */
341         if (pcb->pcb_tssp != NULL) {
342                 pmap_pti_remove_kva((vm_offset_t)pcb->pcb_tssp,
343                     (vm_offset_t)pcb->pcb_tssp + ctob(IOPAGES + 1));
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                 td->td_frame->tf_rax = SV_ABI_ERRNO(td->td_proc, error);
417                 td->td_frame->tf_rflags |= PSL_C;
418                 break;
419         }
420 }
421
422 /*
423  * Initialize machine state, mostly pcb and trap frame for a new
424  * thread, about to return to userspace.  Put enough state in the new
425  * thread's PCB to get it to go back to the fork_return(), which
426  * finalizes the thread state and handles peculiarities of the first
427  * return to userspace for the new thread.
428  */
429 void
430 cpu_copy_thread(struct thread *td, struct thread *td0)
431 {
432         struct pcb *pcb2;
433
434         /* Point the pcb to the top of the stack. */
435         pcb2 = td->td_pcb;
436
437         /*
438          * Copy the upcall pcb.  This loads kernel regs.
439          * Those not loaded individually below get their default
440          * values here.
441          */
442         update_pcb_bases(td0->td_pcb);
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_raw(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_dr*:       cloned above.
476          * pcb2->pcb_savefpu:   cloned above.
477          * pcb2->pcb_onfault:   cloned above (always NULL here?).
478          * pcb2->pcb_[fg]sbase: cloned above
479          */
480
481         /* Setup to release spin count in fork_exit(). */
482         td->td_md.md_spinlock_count = 1;
483         td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
484 }
485
486 /*
487  * Set that machine state for performing an upcall that starts
488  * the entry function with the given argument.
489  */
490 void
491 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
492     stack_t *stack)
493 {
494
495         /* 
496          * Do any extra cleaning that needs to be done.
497          * The thread may have optional components
498          * that are not present in a fresh thread.
499          * This may be a recycled thread so make it look
500          * as though it's newly allocated.
501          */
502         cpu_thread_clean(td);
503
504 #ifdef COMPAT_FREEBSD32
505         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
506                 /*
507                  * Set the trap frame to point at the beginning of the entry
508                  * function.
509                  */
510                 td->td_frame->tf_rbp = 0;
511                 td->td_frame->tf_rsp =
512                    (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
513                 td->td_frame->tf_rip = (uintptr_t)entry;
514
515                 /* Return address sentinel value to stop stack unwinding. */
516                 suword32((void *)td->td_frame->tf_rsp, 0);
517
518                 /* Pass the argument to the entry point. */
519                 suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
520                     (uint32_t)(uintptr_t)arg);
521
522                 return;
523         }
524 #endif
525
526         /*
527          * Set the trap frame to point at the beginning of the uts
528          * function.
529          */
530         td->td_frame->tf_rbp = 0;
531         td->td_frame->tf_rsp =
532             ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
533         td->td_frame->tf_rsp -= 8;
534         td->td_frame->tf_rip = (register_t)entry;
535         td->td_frame->tf_ds = _udatasel;
536         td->td_frame->tf_es = _udatasel;
537         td->td_frame->tf_fs = _ufssel;
538         td->td_frame->tf_gs = _ugssel;
539         td->td_frame->tf_flags = TF_HASSEGS;
540
541         /* Return address sentinel value to stop stack unwinding. */
542         suword((void *)td->td_frame->tf_rsp, 0);
543
544         /* Pass the argument to the entry point. */
545         td->td_frame->tf_rdi = (register_t)arg;
546 }
547
548 int
549 cpu_set_user_tls(struct thread *td, void *tls_base)
550 {
551         struct pcb *pcb;
552
553         if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
554                 return (EINVAL);
555
556         pcb = td->td_pcb;
557         set_pcb_flags(pcb, PCB_FULL_IRET);
558 #ifdef COMPAT_FREEBSD32
559         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
560                 pcb->pcb_gsbase = (register_t)tls_base;
561                 return (0);
562         }
563 #endif
564         pcb->pcb_fsbase = (register_t)tls_base;
565         return (0);
566 }
567
568 #ifdef SMP
569 static void
570 cpu_reset_proxy()
571 {
572
573         cpu_reset_proxy_active = 1;
574         while (cpu_reset_proxy_active == 1)
575                 ia32_pause(); /* Wait for other cpu to see that we've started */
576
577         printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
578         DELAY(1000000);
579         cpu_reset_real();
580 }
581 #endif
582
583 void
584 cpu_reset()
585 {
586 #ifdef SMP
587         cpuset_t map;
588         u_int cnt;
589
590         if (smp_started) {
591                 map = all_cpus;
592                 CPU_CLR(PCPU_GET(cpuid), &map);
593                 CPU_NAND(&map, &stopped_cpus);
594                 if (!CPU_EMPTY(&map)) {
595                         printf("cpu_reset: Stopping other CPUs\n");
596                         stop_cpus(map);
597                 }
598
599                 if (PCPU_GET(cpuid) != 0) {
600                         cpu_reset_proxyid = PCPU_GET(cpuid);
601                         cpustop_restartfunc = cpu_reset_proxy;
602                         cpu_reset_proxy_active = 0;
603                         printf("cpu_reset: Restarting BSP\n");
604
605                         /* Restart CPU #0. */
606                         CPU_SETOF(0, &started_cpus);
607                         wmb();
608
609                         cnt = 0;
610                         while (cpu_reset_proxy_active == 0 && cnt < 10000000) {
611                                 ia32_pause();
612                                 cnt++;  /* Wait for BSP to announce restart */
613                         }
614                         if (cpu_reset_proxy_active == 0) {
615                                 printf("cpu_reset: Failed to restart BSP\n");
616                         } else {
617                                 cpu_reset_proxy_active = 2;
618                                 while (1)
619                                         ia32_pause();
620                                 /* NOTREACHED */
621                         }
622                 }
623
624                 DELAY(1000000);
625         }
626 #endif
627         cpu_reset_real();
628         /* NOTREACHED */
629 }
630
631 static void
632 cpu_reset_real()
633 {
634         struct region_descriptor null_idt;
635         int b;
636
637         disable_intr();
638
639         /*
640          * Attempt to do a CPU reset via the keyboard controller,
641          * do not turn off GateA20, as any machine that fails
642          * to do the reset here would then end up in no man's land.
643          */
644         outb(IO_KBD + 4, 0xFE);
645         DELAY(500000);  /* wait 0.5 sec to see if that did it */
646
647         /*
648          * Attempt to force a reset via the Reset Control register at
649          * I/O port 0xcf9.  Bit 2 forces a system reset when it
650          * transitions from 0 to 1.  Bit 1 selects the type of reset
651          * to attempt: 0 selects a "soft" reset, and 1 selects a
652          * "hard" reset.  We try a "hard" reset.  The first write sets
653          * bit 1 to select a "hard" reset and clears bit 2.  The
654          * second write forces a 0 -> 1 transition in bit 2 to trigger
655          * a reset.
656          */
657         outb(0xcf9, 0x2);
658         outb(0xcf9, 0x6);
659         DELAY(500000);  /* wait 0.5 sec to see if that did it */
660
661         /*
662          * Attempt to force a reset via the Fast A20 and Init register
663          * at I/O port 0x92.  Bit 1 serves as an alternate A20 gate.
664          * Bit 0 asserts INIT# when set to 1.  We are careful to only
665          * preserve bit 1 while setting bit 0.  We also must clear bit
666          * 0 before setting it if it isn't already clear.
667          */
668         b = inb(0x92);
669         if (b != 0xff) {
670                 if ((b & 0x1) != 0)
671                         outb(0x92, b & 0xfe);
672                 outb(0x92, b | 0x1);
673                 DELAY(500000);  /* wait 0.5 sec to see if that did it */
674         }
675
676         printf("No known reset method worked, attempting CPU shutdown\n");
677         DELAY(1000000); /* wait 1 sec for printf to complete */
678
679         /* Wipe the IDT. */
680         null_idt.rd_limit = 0;
681         null_idt.rd_base = 0;
682         lidt(&null_idt);
683
684         /* "good night, sweet prince .... <THUNK!>" */
685         breakpoint();
686
687         /* NOTREACHED */
688         while(1);
689 }
690
691 /*
692  * Software interrupt handler for queued VM system processing.
693  */   
694 void  
695 swi_vm(void *dummy) 
696 {     
697         if (busdma_swi_pending != 0)
698                 busdma_swi();
699 }
700
701 /*
702  * Tell whether this address is in some physical memory region.
703  * Currently used by the kernel coredump code in order to avoid
704  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
705  * or other unpredictable behaviour.
706  */
707
708 int
709 is_physical_memory(vm_paddr_t addr)
710 {
711
712 #ifdef DEV_ISA
713         /* The ISA ``memory hole''. */
714         if (addr >= 0xa0000 && addr < 0x100000)
715                 return 0;
716 #endif
717
718         /*
719          * stuff other tests for known memory-mapped devices (PCI?)
720          * here
721          */
722
723         return 1;
724 }