2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1992 Terrence R. Lambert.
5 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
39 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
46 #include "opt_atpic.h"
47 #include "opt_compat.h"
52 #include "opt_kstack_pages.h"
53 #include "opt_maxmem.h"
54 #include "opt_mp_watchdog.h"
55 #include "opt_perfmon.h"
56 #include "opt_platform.h"
58 #include <sys/param.h>
60 #include <sys/systm.h>
64 #include <sys/callout.h>
67 #include <sys/eventhandler.h>
69 #include <sys/imgact.h>
71 #include <sys/kernel.h>
73 #include <sys/linker.h>
75 #include <sys/malloc.h>
76 #include <sys/memrange.h>
77 #include <sys/msgbuf.h>
78 #include <sys/mutex.h>
80 #include <sys/ptrace.h>
81 #include <sys/reboot.h>
82 #include <sys/rwlock.h>
83 #include <sys/sched.h>
84 #include <sys/signalvar.h>
88 #include <sys/syscallsubr.h>
89 #include <sys/sysctl.h>
90 #include <sys/sysent.h>
91 #include <sys/sysproto.h>
92 #include <sys/ucontext.h>
93 #include <sys/vmmeter.h>
96 #include <vm/vm_extern.h>
97 #include <vm/vm_kern.h>
98 #include <vm/vm_page.h>
99 #include <vm/vm_map.h>
100 #include <vm/vm_object.h>
101 #include <vm/vm_pager.h>
102 #include <vm/vm_param.h>
106 #error KDB must be enabled in order for DDB to work!
109 #include <ddb/db_sym.h>
114 #include <net/netisr.h>
116 #include <machine/bootinfo.h>
117 #include <machine/clock.h>
118 #include <machine/cpu.h>
119 #include <machine/cputypes.h>
120 #include <machine/intr_machdep.h>
122 #include <machine/md_var.h>
123 #include <machine/metadata.h>
124 #include <machine/mp_watchdog.h>
125 #include <machine/pc/bios.h>
126 #include <machine/pcb.h>
127 #include <machine/pcb_ext.h>
128 #include <machine/proc.h>
129 #include <machine/reg.h>
130 #include <machine/sigframe.h>
131 #include <machine/specialreg.h>
132 #include <machine/vm86.h>
133 #include <x86/init.h>
135 #include <machine/perfmon.h>
138 #include <machine/smp.h>
145 #include <x86/apicvar.h>
149 #include <x86/isa/icu.h>
152 /* Sanity check for __curthread() */
153 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
155 extern register_t init386(int first);
156 extern void dblfault_handler(void);
158 static void cpu_startup(void *);
159 static void fpstate_drop(struct thread *td);
160 static void get_fpcontext(struct thread *td, mcontext_t *mcp,
161 char *xfpusave, size_t xfpusave_len);
162 static int set_fpcontext(struct thread *td, mcontext_t *mcp,
163 char *xfpustate, size_t xfpustate_len);
164 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
166 /* Intel ICH registers */
167 #define ICH_PMBASE 0x400
168 #define ICH_SMI_EN ICH_PMBASE + 0x30
170 int _udatasel, _ucodesel;
176 static void osendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
178 #ifdef COMPAT_FREEBSD4
179 static void freebsd4_sendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
186 FEATURE(pae, "Physical Address Extensions");
190 * The number of PHYSMAP entries must be one less than the number of
191 * PHYSSEG entries because the PHYSMAP entry that spans the largest
192 * physical address that is accessible by ISA DMA is split into two
195 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
197 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
198 vm_paddr_t dump_avail[PHYSMAP_SIZE + 2];
200 /* must be 2 less so 0 0 can signal end of chunks */
201 #define PHYS_AVAIL_ARRAY_END (nitems(phys_avail) - 2)
202 #define DUMP_AVAIL_ARRAY_END (nitems(dump_avail) - 2)
204 struct kva_md_info kmi;
206 static struct trapframe proc0_tf;
207 struct pcpu __pcpu[MAXCPU];
211 struct mem_range_softc mem_range_softc;
213 /* Default init_ops implementation. */
214 struct init_ops init_ops = {
215 .early_clock_source_init = i8254_init,
216 .early_delay = i8254_delay,
218 .msi_init = msi_init,
230 * On MacBooks, we need to disallow the legacy USB circuit to
231 * generate an SMI# because this can cause several problems,
232 * namely: incorrect CPU frequency detection and failure to
234 * We do this by disabling a bit in the SMI_EN (SMI Control and
235 * Enable register) of the Intel ICH LPC Interface Bridge.
237 sysenv = kern_getenv("smbios.system.product");
238 if (sysenv != NULL) {
239 if (strncmp(sysenv, "MacBook1,1", 10) == 0 ||
240 strncmp(sysenv, "MacBook3,1", 10) == 0 ||
241 strncmp(sysenv, "MacBook4,1", 10) == 0 ||
242 strncmp(sysenv, "MacBookPro1,1", 13) == 0 ||
243 strncmp(sysenv, "MacBookPro1,2", 13) == 0 ||
244 strncmp(sysenv, "MacBookPro3,1", 13) == 0 ||
245 strncmp(sysenv, "MacBookPro4,1", 13) == 0 ||
246 strncmp(sysenv, "Macmini1,1", 10) == 0) {
248 printf("Disabling LEGACY_USB_EN bit on "
250 outl(ICH_SMI_EN, inl(ICH_SMI_EN) & ~0x8);
256 * Good {morning,afternoon,evening,night}.
260 panicifcpuunsupported();
266 * Display physical memory if SMBIOS reports reasonable amount.
269 sysenv = kern_getenv("smbios.memory.enabled");
270 if (sysenv != NULL) {
271 memsize = (uintmax_t)strtoul(sysenv, (char **)NULL, 10) << 10;
274 if (memsize < ptoa((uintmax_t)vm_free_count()))
275 memsize = ptoa((uintmax_t)Maxmem);
276 printf("real memory = %ju (%ju MB)\n", memsize, memsize >> 20);
277 realmem = atop(memsize);
280 * Display any holes after the first chunk of extended memory.
285 printf("Physical memory chunk(s):\n");
286 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
289 size = phys_avail[indx + 1] - phys_avail[indx];
291 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
292 (uintmax_t)phys_avail[indx],
293 (uintmax_t)phys_avail[indx + 1] - 1,
294 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
298 vm_ksubmap_init(&kmi);
300 printf("avail memory = %ju (%ju MB)\n",
301 ptoa((uintmax_t)vm_free_count()),
302 ptoa((uintmax_t)vm_free_count()) / 1048576);
305 * Set up buffers, so they can be used to read disk labels.
308 vm_pager_bufferinit();
313 * Send an interrupt to process.
315 * Stack is set up to allow sigcode stored
316 * at top to call routine, followed by call
317 * to sigreturn routine below. After sigreturn
318 * resets the signal mask, the stack, and the
319 * frame pointer, it returns to the user
324 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
326 struct osigframe sf, *fp;
330 struct trapframe *regs;
336 PROC_LOCK_ASSERT(p, MA_OWNED);
337 sig = ksi->ksi_signo;
339 mtx_assert(&psp->ps_mtx, MA_OWNED);
341 oonstack = sigonstack(regs->tf_esp);
343 /* Allocate space for the signal handler context. */
344 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
345 SIGISMEMBER(psp->ps_sigonstack, sig)) {
346 fp = (struct osigframe *)((uintptr_t)td->td_sigstk.ss_sp +
347 td->td_sigstk.ss_size - sizeof(struct osigframe));
348 #if defined(COMPAT_43)
349 td->td_sigstk.ss_flags |= SS_ONSTACK;
352 fp = (struct osigframe *)regs->tf_esp - 1;
354 /* Build the argument list for the signal handler. */
356 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
357 bzero(&sf.sf_siginfo, sizeof(sf.sf_siginfo));
358 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
359 /* Signal handler installed with SA_SIGINFO. */
360 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
361 sf.sf_siginfo.si_signo = sig;
362 sf.sf_siginfo.si_code = ksi->ksi_code;
363 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
366 /* Old FreeBSD-style arguments. */
367 sf.sf_arg2 = ksi->ksi_code;
368 sf.sf_addr = (register_t)ksi->ksi_addr;
369 sf.sf_ahu.sf_handler = catcher;
371 mtx_unlock(&psp->ps_mtx);
374 /* Save most if not all of trap frame. */
375 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
376 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
377 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
378 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
379 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
380 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
381 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
382 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
383 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
384 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
385 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
386 sf.sf_siginfo.si_sc.sc_gs = rgs();
387 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
389 /* Build the signal context to be used by osigreturn(). */
390 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
391 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
392 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
393 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
394 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
395 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
396 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
397 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
400 * If we're a vm86 process, we want to save the segment registers.
401 * We also change eflags to be our emulated eflags, not the actual
404 if (regs->tf_eflags & PSL_VM) {
405 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
406 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
407 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
409 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
410 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
411 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
412 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
414 if (vm86->vm86_has_vme == 0)
415 sf.sf_siginfo.si_sc.sc_ps =
416 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
417 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
419 /* See sendsig() for comments. */
420 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
424 * Copy the sigframe out to the user's stack.
426 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
431 regs->tf_esp = (int)fp;
432 if (p->p_sysent->sv_sigcode_base != 0) {
433 regs->tf_eip = p->p_sysent->sv_sigcode_base + szsigcode -
436 /* a.out sysentvec does not use shared page */
437 regs->tf_eip = p->p_sysent->sv_psstrings - szosigcode;
439 regs->tf_eflags &= ~(PSL_T | PSL_D);
440 regs->tf_cs = _ucodesel;
441 regs->tf_ds = _udatasel;
442 regs->tf_es = _udatasel;
443 regs->tf_fs = _udatasel;
445 regs->tf_ss = _udatasel;
447 mtx_lock(&psp->ps_mtx);
449 #endif /* COMPAT_43 */
451 #ifdef COMPAT_FREEBSD4
453 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
455 struct sigframe4 sf, *sfp;
459 struct trapframe *regs;
465 PROC_LOCK_ASSERT(p, MA_OWNED);
466 sig = ksi->ksi_signo;
468 mtx_assert(&psp->ps_mtx, MA_OWNED);
470 oonstack = sigonstack(regs->tf_esp);
472 /* Save user context. */
473 bzero(&sf, sizeof(sf));
474 sf.sf_uc.uc_sigmask = *mask;
475 sf.sf_uc.uc_stack = td->td_sigstk;
476 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
477 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
478 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
479 sf.sf_uc.uc_mcontext.mc_gs = rgs();
480 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
481 bzero(sf.sf_uc.uc_mcontext.mc_fpregs,
482 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
483 bzero(sf.sf_uc.uc_mcontext.__spare__,
484 sizeof(sf.sf_uc.uc_mcontext.__spare__));
485 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
487 /* Allocate space for the signal handler context. */
488 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
489 SIGISMEMBER(psp->ps_sigonstack, sig)) {
490 sfp = (struct sigframe4 *)((uintptr_t)td->td_sigstk.ss_sp +
491 td->td_sigstk.ss_size - sizeof(struct sigframe4));
492 #if defined(COMPAT_43)
493 td->td_sigstk.ss_flags |= SS_ONSTACK;
496 sfp = (struct sigframe4 *)regs->tf_esp - 1;
498 /* Build the argument list for the signal handler. */
500 sf.sf_ucontext = (register_t)&sfp->sf_uc;
501 bzero(&sf.sf_si, sizeof(sf.sf_si));
502 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
503 /* Signal handler installed with SA_SIGINFO. */
504 sf.sf_siginfo = (register_t)&sfp->sf_si;
505 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
507 /* Fill in POSIX parts */
508 sf.sf_si.si_signo = sig;
509 sf.sf_si.si_code = ksi->ksi_code;
510 sf.sf_si.si_addr = ksi->ksi_addr;
512 /* Old FreeBSD-style arguments. */
513 sf.sf_siginfo = ksi->ksi_code;
514 sf.sf_addr = (register_t)ksi->ksi_addr;
515 sf.sf_ahu.sf_handler = catcher;
517 mtx_unlock(&psp->ps_mtx);
521 * If we're a vm86 process, we want to save the segment registers.
522 * We also change eflags to be our emulated eflags, not the actual
525 if (regs->tf_eflags & PSL_VM) {
526 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
527 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
529 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
530 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
531 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
532 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
534 if (vm86->vm86_has_vme == 0)
535 sf.sf_uc.uc_mcontext.mc_eflags =
536 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
537 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
540 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
541 * syscalls made by the signal handler. This just avoids
542 * wasting time for our lazy fixup of such faults. PSL_NT
543 * does nothing in vm86 mode, but vm86 programs can set it
544 * almost legitimately in probes for old cpu types.
546 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
550 * Copy the sigframe out to the user's stack.
552 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
557 regs->tf_esp = (int)sfp;
558 regs->tf_eip = p->p_sysent->sv_sigcode_base + szsigcode -
560 regs->tf_eflags &= ~(PSL_T | PSL_D);
561 regs->tf_cs = _ucodesel;
562 regs->tf_ds = _udatasel;
563 regs->tf_es = _udatasel;
564 regs->tf_fs = _udatasel;
565 regs->tf_ss = _udatasel;
567 mtx_lock(&psp->ps_mtx);
569 #endif /* COMPAT_FREEBSD4 */
572 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
574 struct sigframe sf, *sfp;
579 struct trapframe *regs;
580 struct segment_descriptor *sdp;
588 PROC_LOCK_ASSERT(p, MA_OWNED);
589 sig = ksi->ksi_signo;
591 mtx_assert(&psp->ps_mtx, MA_OWNED);
592 #ifdef COMPAT_FREEBSD4
593 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
594 freebsd4_sendsig(catcher, ksi, mask);
599 if (SIGISMEMBER(psp->ps_osigset, sig)) {
600 osendsig(catcher, ksi, mask);
605 oonstack = sigonstack(regs->tf_esp);
607 if (cpu_max_ext_state_size > sizeof(union savefpu) && use_xsave) {
608 xfpusave_len = cpu_max_ext_state_size - sizeof(union savefpu);
609 xfpusave = __builtin_alloca(xfpusave_len);
615 /* Save user context. */
616 bzero(&sf, sizeof(sf));
617 sf.sf_uc.uc_sigmask = *mask;
618 sf.sf_uc.uc_stack = td->td_sigstk;
619 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
620 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
621 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
622 sf.sf_uc.uc_mcontext.mc_gs = rgs();
623 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
624 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
625 get_fpcontext(td, &sf.sf_uc.uc_mcontext, xfpusave, xfpusave_len);
628 * Unconditionally fill the fsbase and gsbase into the mcontext.
630 sdp = &td->td_pcb->pcb_fsd;
631 sf.sf_uc.uc_mcontext.mc_fsbase = sdp->sd_hibase << 24 |
633 sdp = &td->td_pcb->pcb_gsd;
634 sf.sf_uc.uc_mcontext.mc_gsbase = sdp->sd_hibase << 24 |
636 bzero(sf.sf_uc.uc_mcontext.mc_spare2,
637 sizeof(sf.sf_uc.uc_mcontext.mc_spare2));
638 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
640 /* Allocate space for the signal handler context. */
641 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
642 SIGISMEMBER(psp->ps_sigonstack, sig)) {
643 sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
644 #if defined(COMPAT_43)
645 td->td_sigstk.ss_flags |= SS_ONSTACK;
648 sp = (char *)regs->tf_esp - 128;
649 if (xfpusave != NULL) {
651 sp = (char *)((unsigned int)sp & ~0x3F);
652 sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp;
654 sp -= sizeof(struct sigframe);
656 /* Align to 16 bytes. */
657 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
659 /* Build the argument list for the signal handler. */
661 sf.sf_ucontext = (register_t)&sfp->sf_uc;
662 bzero(&sf.sf_si, sizeof(sf.sf_si));
663 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
664 /* Signal handler installed with SA_SIGINFO. */
665 sf.sf_siginfo = (register_t)&sfp->sf_si;
666 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
668 /* Fill in POSIX parts */
669 sf.sf_si = ksi->ksi_info;
670 sf.sf_si.si_signo = sig; /* maybe a translated signal */
672 /* Old FreeBSD-style arguments. */
673 sf.sf_siginfo = ksi->ksi_code;
674 sf.sf_addr = (register_t)ksi->ksi_addr;
675 sf.sf_ahu.sf_handler = catcher;
677 mtx_unlock(&psp->ps_mtx);
681 * If we're a vm86 process, we want to save the segment registers.
682 * We also change eflags to be our emulated eflags, not the actual
685 if (regs->tf_eflags & PSL_VM) {
686 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
687 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
689 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
690 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
691 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
692 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
694 if (vm86->vm86_has_vme == 0)
695 sf.sf_uc.uc_mcontext.mc_eflags =
696 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
697 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
700 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
701 * syscalls made by the signal handler. This just avoids
702 * wasting time for our lazy fixup of such faults. PSL_NT
703 * does nothing in vm86 mode, but vm86 programs can set it
704 * almost legitimately in probes for old cpu types.
706 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
710 * Copy the sigframe out to the user's stack.
712 if (copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
713 (xfpusave != NULL && copyout(xfpusave,
714 (void *)sf.sf_uc.uc_mcontext.mc_xfpustate, xfpusave_len)
720 regs->tf_esp = (int)sfp;
721 regs->tf_eip = p->p_sysent->sv_sigcode_base;
722 if (regs->tf_eip == 0)
723 regs->tf_eip = p->p_sysent->sv_psstrings - szsigcode;
724 regs->tf_eflags &= ~(PSL_T | PSL_D);
725 regs->tf_cs = _ucodesel;
726 regs->tf_ds = _udatasel;
727 regs->tf_es = _udatasel;
728 regs->tf_fs = _udatasel;
729 regs->tf_ss = _udatasel;
731 mtx_lock(&psp->ps_mtx);
735 * System call to cleanup state after a signal
736 * has been taken. Reset signal mask and
737 * stack state from context left by sendsig (above).
738 * Return to previous pc and psl as specified by
739 * context left by sendsig. Check carefully to
740 * make sure that the user has not modified the
741 * state to gain improper privileges.
749 struct osigreturn_args /* {
750 struct osigcontext *sigcntxp;
753 struct osigcontext sc;
754 struct trapframe *regs;
755 struct osigcontext *scp;
760 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
765 if (eflags & PSL_VM) {
766 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
767 struct vm86_kernel *vm86;
770 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
771 * set up the vm86 area, and we can't enter vm86 mode.
773 if (td->td_pcb->pcb_ext == 0)
775 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
776 if (vm86->vm86_inited == 0)
779 /* Go back to user mode if both flags are set. */
780 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
781 ksiginfo_init_trap(&ksi);
782 ksi.ksi_signo = SIGBUS;
783 ksi.ksi_code = BUS_OBJERR;
784 ksi.ksi_addr = (void *)regs->tf_eip;
785 trapsignal(td, &ksi);
788 if (vm86->vm86_has_vme) {
789 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
790 (eflags & VME_USERCHANGE) | PSL_VM;
792 vm86->vm86_eflags = eflags; /* save VIF, VIP */
793 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
794 (eflags & VM_USERCHANGE) | PSL_VM;
796 tf->tf_vm86_ds = scp->sc_ds;
797 tf->tf_vm86_es = scp->sc_es;
798 tf->tf_vm86_fs = scp->sc_fs;
799 tf->tf_vm86_gs = scp->sc_gs;
800 tf->tf_ds = _udatasel;
801 tf->tf_es = _udatasel;
802 tf->tf_fs = _udatasel;
805 * Don't allow users to change privileged or reserved flags.
807 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
812 * Don't allow users to load a valid privileged %cs. Let the
813 * hardware check for invalid selectors, excess privilege in
814 * other selectors, invalid %eip's and invalid %esp's.
816 if (!CS_SECURE(scp->sc_cs)) {
817 ksiginfo_init_trap(&ksi);
818 ksi.ksi_signo = SIGBUS;
819 ksi.ksi_code = BUS_OBJERR;
820 ksi.ksi_trapno = T_PROTFLT;
821 ksi.ksi_addr = (void *)regs->tf_eip;
822 trapsignal(td, &ksi);
825 regs->tf_ds = scp->sc_ds;
826 regs->tf_es = scp->sc_es;
827 regs->tf_fs = scp->sc_fs;
830 /* Restore remaining registers. */
831 regs->tf_eax = scp->sc_eax;
832 regs->tf_ebx = scp->sc_ebx;
833 regs->tf_ecx = scp->sc_ecx;
834 regs->tf_edx = scp->sc_edx;
835 regs->tf_esi = scp->sc_esi;
836 regs->tf_edi = scp->sc_edi;
837 regs->tf_cs = scp->sc_cs;
838 regs->tf_ss = scp->sc_ss;
839 regs->tf_isp = scp->sc_isp;
840 regs->tf_ebp = scp->sc_fp;
841 regs->tf_esp = scp->sc_sp;
842 regs->tf_eip = scp->sc_pc;
843 regs->tf_eflags = eflags;
845 #if defined(COMPAT_43)
846 if (scp->sc_onstack & 1)
847 td->td_sigstk.ss_flags |= SS_ONSTACK;
849 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
851 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
853 return (EJUSTRETURN);
855 #endif /* COMPAT_43 */
857 #ifdef COMPAT_FREEBSD4
862 freebsd4_sigreturn(td, uap)
864 struct freebsd4_sigreturn_args /* {
865 const ucontext4 *sigcntxp;
869 struct trapframe *regs;
870 struct ucontext4 *ucp;
871 int cs, eflags, error;
874 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
879 eflags = ucp->uc_mcontext.mc_eflags;
880 if (eflags & PSL_VM) {
881 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
882 struct vm86_kernel *vm86;
885 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
886 * set up the vm86 area, and we can't enter vm86 mode.
888 if (td->td_pcb->pcb_ext == 0)
890 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
891 if (vm86->vm86_inited == 0)
894 /* Go back to user mode if both flags are set. */
895 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
896 ksiginfo_init_trap(&ksi);
897 ksi.ksi_signo = SIGBUS;
898 ksi.ksi_code = BUS_OBJERR;
899 ksi.ksi_addr = (void *)regs->tf_eip;
900 trapsignal(td, &ksi);
902 if (vm86->vm86_has_vme) {
903 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
904 (eflags & VME_USERCHANGE) | PSL_VM;
906 vm86->vm86_eflags = eflags; /* save VIF, VIP */
907 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
908 (eflags & VM_USERCHANGE) | PSL_VM;
910 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
911 tf->tf_eflags = eflags;
912 tf->tf_vm86_ds = tf->tf_ds;
913 tf->tf_vm86_es = tf->tf_es;
914 tf->tf_vm86_fs = tf->tf_fs;
915 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
916 tf->tf_ds = _udatasel;
917 tf->tf_es = _udatasel;
918 tf->tf_fs = _udatasel;
921 * Don't allow users to change privileged or reserved flags.
923 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
924 uprintf("pid %d (%s): freebsd4_sigreturn eflags = 0x%x\n",
925 td->td_proc->p_pid, td->td_name, eflags);
930 * Don't allow users to load a valid privileged %cs. Let the
931 * hardware check for invalid selectors, excess privilege in
932 * other selectors, invalid %eip's and invalid %esp's.
934 cs = ucp->uc_mcontext.mc_cs;
935 if (!CS_SECURE(cs)) {
936 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
937 td->td_proc->p_pid, td->td_name, cs);
938 ksiginfo_init_trap(&ksi);
939 ksi.ksi_signo = SIGBUS;
940 ksi.ksi_code = BUS_OBJERR;
941 ksi.ksi_trapno = T_PROTFLT;
942 ksi.ksi_addr = (void *)regs->tf_eip;
943 trapsignal(td, &ksi);
947 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
950 #if defined(COMPAT_43)
951 if (ucp->uc_mcontext.mc_onstack & 1)
952 td->td_sigstk.ss_flags |= SS_ONSTACK;
954 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
956 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
957 return (EJUSTRETURN);
959 #endif /* COMPAT_FREEBSD4 */
965 sys_sigreturn(td, uap)
967 struct sigreturn_args /* {
968 const struct __ucontext *sigcntxp;
973 struct trapframe *regs;
976 size_t xfpustate_len;
977 int cs, eflags, error, ret;
982 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
986 if ((ucp->uc_mcontext.mc_flags & ~_MC_FLAG_MASK) != 0) {
987 uprintf("pid %d (%s): sigreturn mc_flags %x\n", p->p_pid,
988 td->td_name, ucp->uc_mcontext.mc_flags);
992 eflags = ucp->uc_mcontext.mc_eflags;
993 if (eflags & PSL_VM) {
994 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
995 struct vm86_kernel *vm86;
998 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
999 * set up the vm86 area, and we can't enter vm86 mode.
1001 if (td->td_pcb->pcb_ext == 0)
1003 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
1004 if (vm86->vm86_inited == 0)
1007 /* Go back to user mode if both flags are set. */
1008 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
1009 ksiginfo_init_trap(&ksi);
1010 ksi.ksi_signo = SIGBUS;
1011 ksi.ksi_code = BUS_OBJERR;
1012 ksi.ksi_addr = (void *)regs->tf_eip;
1013 trapsignal(td, &ksi);
1016 if (vm86->vm86_has_vme) {
1017 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
1018 (eflags & VME_USERCHANGE) | PSL_VM;
1020 vm86->vm86_eflags = eflags; /* save VIF, VIP */
1021 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
1022 (eflags & VM_USERCHANGE) | PSL_VM;
1024 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
1025 tf->tf_eflags = eflags;
1026 tf->tf_vm86_ds = tf->tf_ds;
1027 tf->tf_vm86_es = tf->tf_es;
1028 tf->tf_vm86_fs = tf->tf_fs;
1029 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
1030 tf->tf_ds = _udatasel;
1031 tf->tf_es = _udatasel;
1032 tf->tf_fs = _udatasel;
1035 * Don't allow users to change privileged or reserved flags.
1037 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
1038 uprintf("pid %d (%s): sigreturn eflags = 0x%x\n",
1039 td->td_proc->p_pid, td->td_name, eflags);
1044 * Don't allow users to load a valid privileged %cs. Let the
1045 * hardware check for invalid selectors, excess privilege in
1046 * other selectors, invalid %eip's and invalid %esp's.
1048 cs = ucp->uc_mcontext.mc_cs;
1049 if (!CS_SECURE(cs)) {
1050 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
1051 td->td_proc->p_pid, td->td_name, cs);
1052 ksiginfo_init_trap(&ksi);
1053 ksi.ksi_signo = SIGBUS;
1054 ksi.ksi_code = BUS_OBJERR;
1055 ksi.ksi_trapno = T_PROTFLT;
1056 ksi.ksi_addr = (void *)regs->tf_eip;
1057 trapsignal(td, &ksi);
1061 if ((uc.uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) {
1062 xfpustate_len = uc.uc_mcontext.mc_xfpustate_len;
1063 if (xfpustate_len > cpu_max_ext_state_size -
1064 sizeof(union savefpu)) {
1066 "pid %d (%s): sigreturn xfpusave_len = 0x%zx\n",
1067 p->p_pid, td->td_name, xfpustate_len);
1070 xfpustate = __builtin_alloca(xfpustate_len);
1071 error = copyin((const void *)uc.uc_mcontext.mc_xfpustate,
1072 xfpustate, xfpustate_len);
1075 "pid %d (%s): sigreturn copying xfpustate failed\n",
1076 p->p_pid, td->td_name);
1083 ret = set_fpcontext(td, &ucp->uc_mcontext, xfpustate,
1087 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1090 #if defined(COMPAT_43)
1091 if (ucp->uc_mcontext.mc_onstack & 1)
1092 td->td_sigstk.ss_flags |= SS_ONSTACK;
1094 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1097 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
1098 return (EJUSTRETURN);
1102 * Reset registers to default values on exec.
1105 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
1107 struct trapframe *regs = td->td_frame;
1108 struct pcb *pcb = td->td_pcb;
1110 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1111 pcb->pcb_gs = _udatasel;
1114 mtx_lock_spin(&dt_lock);
1115 if (td->td_proc->p_md.md_ldt)
1118 mtx_unlock_spin(&dt_lock);
1121 * Reset the fs and gs bases. The values from the old address
1122 * space do not make sense for the new program. In particular,
1123 * gsbase might be the TLS base for the old program but the new
1124 * program has no TLS now.
1129 /* Make sure edx is 0x0 on entry. Linux binaries depend on it. */
1130 bzero((char *)regs, sizeof(struct trapframe));
1131 regs->tf_eip = imgp->entry_addr;
1132 regs->tf_esp = stack;
1133 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1134 regs->tf_ss = _udatasel;
1135 regs->tf_ds = _udatasel;
1136 regs->tf_es = _udatasel;
1137 regs->tf_fs = _udatasel;
1138 regs->tf_cs = _ucodesel;
1140 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1141 regs->tf_ebx = imgp->ps_strings;
1144 * Reset the hardware debug registers if they were in use.
1145 * They won't have any meaning for the newly exec'd process.
1147 if (pcb->pcb_flags & PCB_DBREGS) {
1154 if (pcb == curpcb) {
1156 * Clear the debug registers on the running
1157 * CPU, otherwise they will end up affecting
1158 * the next process we switch to.
1162 pcb->pcb_flags &= ~PCB_DBREGS;
1165 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1168 * Drop the FP state if we hold it, so that the process gets a
1169 * clean FP state if it uses the FPU again.
1182 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1184 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1185 * instructions. We must set the CR0_MP bit and use the CR0_TS
1186 * bit to control the trap, because setting the CR0_EM bit does
1187 * not cause WAIT instructions to trap. It's important to trap
1188 * WAIT instructions - otherwise the "wait" variants of no-wait
1189 * control instructions would degenerate to the "no-wait" variants
1190 * after FP context switches but work correctly otherwise. It's
1191 * particularly important to trap WAITs when there is no NPX -
1192 * otherwise the "wait" variants would always degenerate.
1194 * Try setting CR0_NE to get correct error reporting on 486DX's.
1195 * Setting it should fail or do nothing on lesser processors.
1197 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1202 u_long bootdev; /* not a struct cdev *- encoding is different */
1203 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1204 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1206 static char bootmethod[16] = "BIOS";
1207 SYSCTL_STRING(_machdep, OID_AUTO, bootmethod, CTLFLAG_RD, bootmethod, 0,
1208 "System firmware boot method");
1211 * Initialize 386 and configure to run kernel
1215 * Initialize segments & interrupt table
1220 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1221 union descriptor ldt[NLDT]; /* local descriptor table */
1222 static struct gate_descriptor idt0[NIDT];
1223 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1224 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1225 struct mtx dt_lock; /* lock for GDT and LDT */
1227 static struct i386tss dblfault_tss;
1228 static char dblfault_stack[PAGE_SIZE];
1230 extern vm_offset_t proc0kstack;
1234 * software prototypes -- in more palatable form.
1236 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1237 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1239 struct soft_segment_descriptor gdt_segs[] = {
1240 /* GNULL_SEL 0 Null Descriptor */
1246 .ssd_xx = 0, .ssd_xx1 = 0,
1249 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1251 .ssd_limit = 0xfffff,
1252 .ssd_type = SDT_MEMRWA,
1255 .ssd_xx = 0, .ssd_xx1 = 0,
1258 /* GUFS_SEL 2 %fs Descriptor for user */
1260 .ssd_limit = 0xfffff,
1261 .ssd_type = SDT_MEMRWA,
1264 .ssd_xx = 0, .ssd_xx1 = 0,
1267 /* GUGS_SEL 3 %gs Descriptor for user */
1269 .ssd_limit = 0xfffff,
1270 .ssd_type = SDT_MEMRWA,
1273 .ssd_xx = 0, .ssd_xx1 = 0,
1276 /* GCODE_SEL 4 Code Descriptor for kernel */
1278 .ssd_limit = 0xfffff,
1279 .ssd_type = SDT_MEMERA,
1282 .ssd_xx = 0, .ssd_xx1 = 0,
1285 /* GDATA_SEL 5 Data Descriptor for kernel */
1287 .ssd_limit = 0xfffff,
1288 .ssd_type = SDT_MEMRWA,
1291 .ssd_xx = 0, .ssd_xx1 = 0,
1294 /* GUCODE_SEL 6 Code Descriptor for user */
1296 .ssd_limit = 0xfffff,
1297 .ssd_type = SDT_MEMERA,
1300 .ssd_xx = 0, .ssd_xx1 = 0,
1303 /* GUDATA_SEL 7 Data Descriptor for user */
1305 .ssd_limit = 0xfffff,
1306 .ssd_type = SDT_MEMRWA,
1309 .ssd_xx = 0, .ssd_xx1 = 0,
1312 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1313 { .ssd_base = 0x400,
1314 .ssd_limit = 0xfffff,
1315 .ssd_type = SDT_MEMRWA,
1318 .ssd_xx = 0, .ssd_xx1 = 0,
1321 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1324 .ssd_limit = sizeof(struct i386tss)-1,
1325 .ssd_type = SDT_SYS386TSS,
1328 .ssd_xx = 0, .ssd_xx1 = 0,
1331 /* GLDT_SEL 10 LDT Descriptor */
1332 { .ssd_base = (int) ldt,
1333 .ssd_limit = sizeof(ldt)-1,
1334 .ssd_type = SDT_SYSLDT,
1337 .ssd_xx = 0, .ssd_xx1 = 0,
1340 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1341 { .ssd_base = (int) ldt,
1342 .ssd_limit = (512 * sizeof(union descriptor)-1),
1343 .ssd_type = SDT_SYSLDT,
1346 .ssd_xx = 0, .ssd_xx1 = 0,
1349 /* GPANIC_SEL 12 Panic Tss Descriptor */
1350 { .ssd_base = (int) &dblfault_tss,
1351 .ssd_limit = sizeof(struct i386tss)-1,
1352 .ssd_type = SDT_SYS386TSS,
1355 .ssd_xx = 0, .ssd_xx1 = 0,
1358 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1360 .ssd_limit = 0xfffff,
1361 .ssd_type = SDT_MEMERA,
1364 .ssd_xx = 0, .ssd_xx1 = 0,
1367 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1369 .ssd_limit = 0xfffff,
1370 .ssd_type = SDT_MEMERA,
1373 .ssd_xx = 0, .ssd_xx1 = 0,
1376 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1378 .ssd_limit = 0xfffff,
1379 .ssd_type = SDT_MEMRWA,
1382 .ssd_xx = 0, .ssd_xx1 = 0,
1385 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1387 .ssd_limit = 0xfffff,
1388 .ssd_type = SDT_MEMRWA,
1391 .ssd_xx = 0, .ssd_xx1 = 0,
1394 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1396 .ssd_limit = 0xfffff,
1397 .ssd_type = SDT_MEMRWA,
1400 .ssd_xx = 0, .ssd_xx1 = 0,
1403 /* GNDIS_SEL 18 NDIS Descriptor */
1409 .ssd_xx = 0, .ssd_xx1 = 0,
1414 static struct soft_segment_descriptor ldt_segs[] = {
1415 /* Null Descriptor - overwritten by call gate */
1421 .ssd_xx = 0, .ssd_xx1 = 0,
1424 /* Null Descriptor - overwritten by call gate */
1430 .ssd_xx = 0, .ssd_xx1 = 0,
1433 /* Null Descriptor - overwritten by call gate */
1439 .ssd_xx = 0, .ssd_xx1 = 0,
1442 /* Code Descriptor for user */
1444 .ssd_limit = 0xfffff,
1445 .ssd_type = SDT_MEMERA,
1448 .ssd_xx = 0, .ssd_xx1 = 0,
1451 /* Null Descriptor - overwritten by call gate */
1457 .ssd_xx = 0, .ssd_xx1 = 0,
1460 /* Data Descriptor for user */
1462 .ssd_limit = 0xfffff,
1463 .ssd_type = SDT_MEMRWA,
1466 .ssd_xx = 0, .ssd_xx1 = 0,
1472 setidt(idx, func, typ, dpl, selec)
1479 struct gate_descriptor *ip;
1482 ip->gd_looffset = (int)func;
1483 ip->gd_selector = selec;
1489 ip->gd_hioffset = ((int)func)>>16 ;
1493 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1494 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1495 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1496 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1498 #ifdef KDTRACE_HOOKS
1502 IDTVEC(xen_intr_upcall),
1504 IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1508 * Display the index and function name of any IDT entries that don't use
1509 * the default 'rsvd' entry point.
1511 DB_SHOW_COMMAND(idt, db_show_idt)
1513 struct gate_descriptor *ip;
1518 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
1519 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1520 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1521 db_printf("%3d\t", idx);
1522 db_printsym(func, DB_STGY_PROC);
1529 /* Show privileged registers. */
1530 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
1532 uint64_t idtr, gdtr;
1535 db_printf("idtr\t0x%08x/%04x\n",
1536 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
1538 db_printf("gdtr\t0x%08x/%04x\n",
1539 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
1540 db_printf("ldtr\t0x%04x\n", rldt());
1541 db_printf("tr\t0x%04x\n", rtr());
1542 db_printf("cr0\t0x%08x\n", rcr0());
1543 db_printf("cr2\t0x%08x\n", rcr2());
1544 db_printf("cr3\t0x%08x\n", rcr3());
1545 db_printf("cr4\t0x%08x\n", rcr4());
1546 if (rcr4() & CR4_XSAVE)
1547 db_printf("xcr0\t0x%016llx\n", rxcr(0));
1548 if (amd_feature & (AMDID_NX | AMDID_LM))
1549 db_printf("EFER\t0x%016llx\n", rdmsr(MSR_EFER));
1550 if (cpu_feature2 & (CPUID2_VMX | CPUID2_SMX))
1551 db_printf("FEATURES_CTL\t0x%016llx\n",
1552 rdmsr(MSR_IA32_FEATURE_CONTROL));
1553 if ((cpu_vendor_id == CPU_VENDOR_INTEL ||
1554 cpu_vendor_id == CPU_VENDOR_AMD) && CPUID_TO_FAMILY(cpu_id) >= 6)
1555 db_printf("DEBUG_CTL\t0x%016llx\n", rdmsr(MSR_DEBUGCTLMSR));
1556 if (cpu_feature & CPUID_PAT)
1557 db_printf("PAT\t0x%016llx\n", rdmsr(MSR_PAT));
1560 DB_SHOW_COMMAND(dbregs, db_show_dbregs)
1563 db_printf("dr0\t0x%08x\n", rdr0());
1564 db_printf("dr1\t0x%08x\n", rdr1());
1565 db_printf("dr2\t0x%08x\n", rdr2());
1566 db_printf("dr3\t0x%08x\n", rdr3());
1567 db_printf("dr6\t0x%08x\n", rdr6());
1568 db_printf("dr7\t0x%08x\n", rdr7());
1574 struct segment_descriptor *sd;
1575 struct soft_segment_descriptor *ssd;
1577 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1578 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1579 ssd->ssd_type = sd->sd_type;
1580 ssd->ssd_dpl = sd->sd_dpl;
1581 ssd->ssd_p = sd->sd_p;
1582 ssd->ssd_def32 = sd->sd_def32;
1583 ssd->ssd_gran = sd->sd_gran;
1587 add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
1590 int i, insert_idx, physmap_idx;
1592 physmap_idx = *physmap_idxp;
1598 if (base > 0xffffffff) {
1599 printf("%uK of memory above 4GB ignored\n",
1600 (u_int)(length / 1024));
1606 * Find insertion point while checking for overlap. Start off by
1607 * assuming the new entry will be added to the end.
1609 insert_idx = physmap_idx + 2;
1610 for (i = 0; i <= physmap_idx; i += 2) {
1611 if (base < physmap[i + 1]) {
1612 if (base + length <= physmap[i]) {
1616 if (boothowto & RB_VERBOSE)
1618 "Overlapping memory regions, ignoring second region\n");
1623 /* See if we can prepend to the next entry. */
1624 if (insert_idx <= physmap_idx && base + length == physmap[insert_idx]) {
1625 physmap[insert_idx] = base;
1629 /* See if we can append to the previous entry. */
1630 if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
1631 physmap[insert_idx - 1] += length;
1636 *physmap_idxp = physmap_idx;
1637 if (physmap_idx == PHYSMAP_SIZE) {
1639 "Too many segments in the physical address map, giving up\n");
1644 * Move the last 'N' entries down to make room for the new
1647 for (i = physmap_idx; i > insert_idx; i -= 2) {
1648 physmap[i] = physmap[i - 2];
1649 physmap[i + 1] = physmap[i - 1];
1652 /* Insert the new entry. */
1653 physmap[insert_idx] = base;
1654 physmap[insert_idx + 1] = base + length;
1659 add_smap_entry(struct bios_smap *smap, vm_paddr_t *physmap, int *physmap_idxp)
1661 if (boothowto & RB_VERBOSE)
1662 printf("SMAP type=%02x base=%016llx len=%016llx\n",
1663 smap->type, smap->base, smap->length);
1665 if (smap->type != SMAP_TYPE_MEMORY)
1668 return (add_physmap_entry(smap->base, smap->length, physmap,
1673 add_smap_entries(struct bios_smap *smapbase, vm_paddr_t *physmap,
1676 struct bios_smap *smap, *smapend;
1679 * Memory map from INT 15:E820.
1681 * subr_module.c says:
1682 * "Consumer may safely assume that size value precedes data."
1683 * ie: an int32_t immediately precedes SMAP.
1685 smapsize = *((u_int32_t *)smapbase - 1);
1686 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
1688 for (smap = smapbase; smap < smapend; smap++)
1689 if (!add_smap_entry(smap, physmap, physmap_idxp))
1700 if (basemem > 640) {
1701 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1707 * XXX if biosbasemem is now < 640, there is a `hole'
1708 * between the end of base memory and the start of
1709 * ISA memory. The hole may be empty or it may
1710 * contain BIOS code or data. Map it read/write so
1711 * that the BIOS can write to it. (Memory from 0 to
1712 * the physical end of the kernel is mapped read-only
1713 * to begin with and then parts of it are remapped.
1714 * The parts that aren't remapped form holes that
1715 * remain read-only and are unused by the kernel.
1716 * The base memory area is below the physical end of
1717 * the kernel and right now forms a read-only hole.
1718 * The part of it from PAGE_SIZE to
1719 * (trunc_page(biosbasemem * 1024) - 1) will be
1720 * remapped and used by the kernel later.)
1722 * This code is similar to the code used in
1723 * pmap_mapdev, but since no memory needs to be
1724 * allocated we simply change the mapping.
1726 for (pa = trunc_page(basemem * 1024);
1727 pa < ISA_HOLE_START; pa += PAGE_SIZE)
1728 pmap_kenter(KERNBASE + pa, pa);
1731 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
1732 * the vm86 page table so that vm86 can scribble on them using
1733 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
1734 * page 0, at least as initialized here?
1736 pte = (pt_entry_t *)vm86paddr;
1737 for (i = basemem / 4; i < 160; i++)
1738 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1742 * Populate the (physmap) array with base/bound pairs describing the
1743 * available physical memory in the system, then test this memory and
1744 * build the phys_avail array describing the actually-available memory.
1746 * If we cannot accurately determine the physical memory map, then use
1747 * value from the 0xE801 call, and failing that, the RTC.
1749 * Total memory size may be set by the kernel environment variable
1750 * hw.physmem or the compile-time define MAXMEM.
1752 * XXX first should be vm_paddr_t.
1755 getmemsize(int first)
1757 int has_smap, off, physmap_idx, pa_indx, da_indx;
1759 vm_paddr_t physmap[PHYSMAP_SIZE];
1761 quad_t dcons_addr, dcons_size, physmem_tunable;
1762 int hasbrokenint12, i, res;
1764 struct vm86frame vmf;
1765 struct vm86context vmc;
1767 struct bios_smap *smap, *smapbase;
1771 bzero(&vmf, sizeof(vmf));
1772 bzero(physmap, sizeof(physmap));
1776 * Check if the loader supplied an SMAP memory map. If so,
1777 * use that and do not make any VM86 calls.
1780 kmdp = preload_search_by_type("elf kernel");
1782 kmdp = preload_search_by_type("elf32 kernel");
1783 smapbase = (struct bios_smap *)preload_search_info(kmdp,
1784 MODINFO_METADATA | MODINFOMD_SMAP);
1785 if (smapbase != NULL) {
1786 add_smap_entries(smapbase, physmap, &physmap_idx);
1792 * Some newer BIOSes have a broken INT 12H implementation
1793 * which causes a kernel panic immediately. In this case, we
1794 * need use the SMAP to determine the base memory size.
1797 TUNABLE_INT_FETCH("hw.hasbrokenint12", &hasbrokenint12);
1798 if (hasbrokenint12 == 0) {
1799 /* Use INT12 to determine base memory size. */
1800 vm86_intcall(0x12, &vmf);
1801 basemem = vmf.vmf_ax;
1806 * Fetch the memory map with INT 15:E820. Map page 1 R/W into
1807 * the kernel page table so we can use it as a buffer. The
1808 * kernel will unmap this page later.
1810 pmap_kenter(KERNBASE + (1 << PAGE_SHIFT), 1 << PAGE_SHIFT);
1812 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1813 res = vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1814 KASSERT(res != 0, ("vm86_getptr() failed: address not found"));
1818 vmf.vmf_eax = 0xE820;
1819 vmf.vmf_edx = SMAP_SIG;
1820 vmf.vmf_ecx = sizeof(struct bios_smap);
1821 i = vm86_datacall(0x15, &vmf, &vmc);
1822 if (i || vmf.vmf_eax != SMAP_SIG)
1825 if (!add_smap_entry(smap, physmap, &physmap_idx))
1827 } while (vmf.vmf_ebx != 0);
1831 * If we didn't fetch the "base memory" size from INT12,
1832 * figure it out from the SMAP (or just guess).
1835 for (i = 0; i <= physmap_idx; i += 2) {
1836 if (physmap[i] == 0x00000000) {
1837 basemem = physmap[i + 1] / 1024;
1842 /* XXX: If we couldn't find basemem from SMAP, just guess. */
1848 if (physmap[1] != 0)
1852 * If we failed to find an SMAP, figure out the extended
1853 * memory size. We will then build a simple memory map with
1854 * two segments, one for "base memory" and the second for
1855 * "extended memory". Note that "extended memory" starts at a
1856 * physical address of 1MB and that both basemem and extmem
1857 * are in units of 1KB.
1859 * First, try to fetch the extended memory size via INT 15:E801.
1861 vmf.vmf_ax = 0xE801;
1862 if (vm86_intcall(0x15, &vmf) == 0) {
1863 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1866 * If INT15:E801 fails, this is our last ditch effort
1867 * to determine the extended memory size. Currently
1868 * we prefer the RTC value over INT15:88.
1872 vm86_intcall(0x15, &vmf);
1873 extmem = vmf.vmf_ax;
1875 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1880 * Special hack for chipsets that still remap the 384k hole when
1881 * there's 16MB of memory - this really confuses people that
1882 * are trying to use bus mastering ISA controllers with the
1883 * "16MB limit"; they only have 16MB, but the remapping puts
1884 * them beyond the limit.
1886 * If extended memory is between 15-16MB (16-17MB phys address range),
1889 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1893 physmap[1] = basemem * 1024;
1895 physmap[physmap_idx] = 0x100000;
1896 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1900 * Now, physmap contains a map of physical memory.
1904 /* make hole for AP bootstrap code */
1905 physmap[1] = mp_bootaddress(physmap[1]);
1909 * Maxmem isn't the "maximum memory", it's one larger than the
1910 * highest page of the physical address space. It should be
1911 * called something like "Maxphyspage". We may adjust this
1912 * based on ``hw.physmem'' and the results of the memory test.
1914 * This is especially confusing when it is much larger than the
1915 * memory size and is displayed as "realmem".
1917 Maxmem = atop(physmap[physmap_idx + 1]);
1920 Maxmem = MAXMEM / 4;
1923 if (TUNABLE_QUAD_FETCH("hw.physmem", &physmem_tunable))
1924 Maxmem = atop(physmem_tunable);
1927 * If we have an SMAP, don't allow MAXMEM or hw.physmem to extend
1928 * the amount of memory in the system.
1930 if (has_smap && Maxmem > atop(physmap[physmap_idx + 1]))
1931 Maxmem = atop(physmap[physmap_idx + 1]);
1934 * By default enable the memory test on real hardware, and disable
1935 * it if we appear to be running in a VM. This avoids touching all
1936 * pages unnecessarily, which doesn't matter on real hardware but is
1937 * bad for shared VM hosts. Use a general name so that
1938 * one could eventually do more with the code than just disable it.
1940 memtest = (vm_guest > VM_GUEST_NO) ? 0 : 1;
1941 TUNABLE_ULONG_FETCH("hw.memtest.tests", &memtest);
1943 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1944 (boothowto & RB_VERBOSE))
1945 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1948 * If Maxmem has been increased beyond what the system has detected,
1949 * extend the last memory segment to the new limit.
1951 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1952 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1954 /* call pmap initialization to make new kernel address space */
1955 pmap_bootstrap(first);
1958 * Size up each available chunk of physical memory.
1960 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1963 phys_avail[pa_indx++] = physmap[0];
1964 phys_avail[pa_indx] = physmap[0];
1965 dump_avail[da_indx] = physmap[0];
1969 * Get dcons buffer address
1971 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
1972 getenv_quad("dcons.size", &dcons_size) == 0)
1976 * physmap is in bytes, so when converting to page boundaries,
1977 * round up the start address and round down the end address.
1979 for (i = 0; i <= physmap_idx; i += 2) {
1982 end = ptoa((vm_paddr_t)Maxmem);
1983 if (physmap[i + 1] < end)
1984 end = trunc_page(physmap[i + 1]);
1985 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1986 int tmp, page_bad, full;
1987 int *ptr = (int *)CADDR3;
1991 * block out kernel memory as not available.
1993 if (pa >= KERNLOAD && pa < first)
1997 * block out dcons buffer
2000 && pa >= trunc_page(dcons_addr)
2001 && pa < dcons_addr + dcons_size)
2009 * map page into kernel: valid, read/write,non-cacheable
2011 *pte = pa | PG_V | PG_RW | PG_N;
2016 * Test for alternating 1's and 0's
2018 *(volatile int *)ptr = 0xaaaaaaaa;
2019 if (*(volatile int *)ptr != 0xaaaaaaaa)
2022 * Test for alternating 0's and 1's
2024 *(volatile int *)ptr = 0x55555555;
2025 if (*(volatile int *)ptr != 0x55555555)
2030 *(volatile int *)ptr = 0xffffffff;
2031 if (*(volatile int *)ptr != 0xffffffff)
2036 *(volatile int *)ptr = 0x0;
2037 if (*(volatile int *)ptr != 0x0)
2040 * Restore original value.
2046 * Adjust array of valid/good pages.
2048 if (page_bad == TRUE)
2051 * If this good page is a continuation of the
2052 * previous set of good pages, then just increase
2053 * the end pointer. Otherwise start a new chunk.
2054 * Note that "end" points one higher than end,
2055 * making the range >= start and < end.
2056 * If we're also doing a speculative memory
2057 * test and we at or past the end, bump up Maxmem
2058 * so that we keep going. The first bad page
2059 * will terminate the loop.
2061 if (phys_avail[pa_indx] == pa) {
2062 phys_avail[pa_indx] += PAGE_SIZE;
2065 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
2067 "Too many holes in the physical address space, giving up\n");
2072 phys_avail[pa_indx++] = pa; /* start */
2073 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
2077 if (dump_avail[da_indx] == pa) {
2078 dump_avail[da_indx] += PAGE_SIZE;
2081 if (da_indx == DUMP_AVAIL_ARRAY_END) {
2085 dump_avail[da_indx++] = pa; /* start */
2086 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
2098 * The last chunk must contain at least one page plus the message
2099 * buffer to avoid complicating other code (message buffer address
2100 * calculation, etc.).
2102 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
2103 round_page(msgbufsize) >= phys_avail[pa_indx]) {
2104 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
2105 phys_avail[pa_indx--] = 0;
2106 phys_avail[pa_indx--] = 0;
2109 Maxmem = atop(phys_avail[pa_indx]);
2111 /* Trim off space for the message buffer. */
2112 phys_avail[pa_indx] -= round_page(msgbufsize);
2114 /* Map the message buffer. */
2115 for (off = 0; off < round_page(msgbufsize); off += PAGE_SIZE)
2116 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
2124 db_fetch_ksymtab(bootinfo.bi_symtab, bootinfo.bi_esymtab);
2128 if (boothowto & RB_KDB)
2129 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
2136 struct gate_descriptor *gdp;
2137 int gsel_tss, metadata_missing, x, pa;
2139 struct xstate_hdr *xhdr;
2142 thread0.td_kstack = proc0kstack;
2143 thread0.td_kstack_pages = TD0_KSTACK_PAGES;
2146 * This may be done better later if it gets more high level
2147 * components in it. If so just link td->td_proc here.
2149 proc_linkup0(&proc0, &thread0);
2151 metadata_missing = 0;
2152 if (bootinfo.bi_modulep) {
2153 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
2154 preload_bootstrap_relocate(KERNBASE);
2156 metadata_missing = 1;
2159 if (bootinfo.bi_envp != 0)
2160 init_static_kenv((char *)bootinfo.bi_envp + KERNBASE, 0);
2162 init_static_kenv(NULL, 0);
2164 identify_hypervisor();
2166 /* Init basic tunables, hz etc */
2170 * Make gdt memory segments. All segments cover the full 4GB
2171 * of address space and permissions are enforced at page level.
2173 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
2174 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
2175 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
2176 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
2177 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
2178 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
2181 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
2182 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2183 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2185 for (x = 0; x < NGDT; x++)
2186 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2188 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2189 r_gdt.rd_base = (int) gdt;
2190 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2193 pcpu_init(pc, 0, sizeof(struct pcpu));
2194 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2195 pmap_kenter(pa + KERNBASE, pa);
2196 dpcpu_init((void *)(first + KERNBASE), 0);
2197 first += DPCPU_SIZE;
2198 PCPU_SET(prvspace, pc);
2199 PCPU_SET(curthread, &thread0);
2200 /* Non-late cninit() and printf() can be moved up to here. */
2203 * Initialize mutexes.
2205 * icu_lock: in order to allow an interrupt to occur in a critical
2206 * section, to set pcpu->ipending (etc...) properly, we
2207 * must be able to get the icu lock, so it can't be
2211 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2213 /* make ldt memory segments */
2214 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2215 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2216 for (x = 0; x < nitems(ldt_segs); x++)
2217 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2219 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2221 PCPU_SET(currentldt, _default_ldt);
2224 for (x = 0; x < NIDT; x++)
2225 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2226 GSEL(GCODE_SEL, SEL_KPL));
2227 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2228 GSEL(GCODE_SEL, SEL_KPL));
2229 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2230 GSEL(GCODE_SEL, SEL_KPL));
2231 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
2232 GSEL(GCODE_SEL, SEL_KPL));
2233 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2234 GSEL(GCODE_SEL, SEL_KPL));
2235 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2236 GSEL(GCODE_SEL, SEL_KPL));
2237 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2238 GSEL(GCODE_SEL, SEL_KPL));
2239 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2240 GSEL(GCODE_SEL, SEL_KPL));
2241 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2242 , GSEL(GCODE_SEL, SEL_KPL));
2243 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2244 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2245 GSEL(GCODE_SEL, SEL_KPL));
2246 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2247 GSEL(GCODE_SEL, SEL_KPL));
2248 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2249 GSEL(GCODE_SEL, SEL_KPL));
2250 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2251 GSEL(GCODE_SEL, SEL_KPL));
2252 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2253 GSEL(GCODE_SEL, SEL_KPL));
2254 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2255 GSEL(GCODE_SEL, SEL_KPL));
2256 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2257 GSEL(GCODE_SEL, SEL_KPL));
2258 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2259 GSEL(GCODE_SEL, SEL_KPL));
2260 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2261 GSEL(GCODE_SEL, SEL_KPL));
2262 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2263 GSEL(GCODE_SEL, SEL_KPL));
2264 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2265 GSEL(GCODE_SEL, SEL_KPL));
2266 #ifdef KDTRACE_HOOKS
2267 setidt(IDT_DTRACE_RET, &IDTVEC(dtrace_ret), SDT_SYS386TGT, SEL_UPL,
2268 GSEL(GCODE_SEL, SEL_KPL));
2271 setidt(IDT_EVTCHN, &IDTVEC(xen_intr_upcall), SDT_SYS386IGT, SEL_KPL,
2272 GSEL(GCODE_SEL, SEL_KPL));
2275 r_idt.rd_limit = sizeof(idt0) - 1;
2276 r_idt.rd_base = (int) idt;
2280 * Initialize the clock before the console so that console
2281 * initialization can use DELAY().
2285 finishidentcpu(); /* Final stage of CPU initialization */
2286 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2287 GSEL(GCODE_SEL, SEL_KPL));
2288 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2289 GSEL(GCODE_SEL, SEL_KPL));
2290 initializecpu(); /* Initialize CPU registers */
2291 initializecpucache();
2293 /* pointer to selector slot for %fs/%gs */
2294 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2296 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2297 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2298 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2299 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2300 #if defined(PAE) || defined(PAE_TABLES)
2301 dblfault_tss.tss_cr3 = (int)IdlePDPT;
2303 dblfault_tss.tss_cr3 = (int)IdlePTD;
2305 dblfault_tss.tss_eip = (int)dblfault_handler;
2306 dblfault_tss.tss_eflags = PSL_KERNEL;
2307 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2308 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2309 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2310 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2311 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2313 /* Initialize the tss (except for the final esp0) early for vm86. */
2314 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2315 thread0.td_kstack_pages * PAGE_SIZE - 16);
2316 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2317 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2318 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2319 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2320 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2323 /* Initialize the PIC early for vm86 calls. */
2329 /* Reset and mask the atpics and leave them shut down. */
2333 * Point the ICU spurious interrupt vectors at the APIC spurious
2334 * interrupt handler.
2336 setidt(IDT_IO_INTS + 7, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
2337 GSEL(GCODE_SEL, SEL_KPL));
2338 setidt(IDT_IO_INTS + 15, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
2339 GSEL(GCODE_SEL, SEL_KPL));
2344 * The console and kdb should be initialized even earlier than here,
2345 * but some console drivers don't work until after getmemsize().
2346 * Default to late console initialization to support these drivers.
2347 * This loses mainly printf()s in getmemsize() and early debugging.
2350 TUNABLE_INT_FETCH("debug.late_console", &late_console);
2351 if (!late_console) {
2358 init_param2(physmem);
2360 /* now running on new page tables, configured,and u/iom is accessible */
2365 if (metadata_missing)
2366 printf("WARNING: loader(8) metadata is missing!\n");
2371 msgbufinit(msgbufp, msgbufsize);
2374 * Set up thread0 pcb after npxinit calculated pcb + fpu save
2375 * area size. Zero out the extended state header in fpu save
2378 thread0.td_pcb = get_pcb_td(&thread0);
2379 thread0.td_pcb->pcb_save = get_pcb_user_save_td(&thread0);
2380 bzero(get_pcb_user_save_td(&thread0), cpu_max_ext_state_size);
2382 xhdr = (struct xstate_hdr *)(get_pcb_user_save_td(&thread0) +
2384 xhdr->xstate_bv = xsave_mask;
2386 PCPU_SET(curpcb, thread0.td_pcb);
2387 /* Move esp0 in the tss to its final place. */
2388 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2389 PCPU_SET(common_tss.tss_esp0, (vm_offset_t)thread0.td_pcb - 16);
2390 gdt[GPROC0_SEL].sd.sd_type = SDT_SYS386TSS; /* clear busy bit */
2393 /* make a call gate to reenter kernel with */
2394 gdp = &ldt[LSYS5CALLS_SEL].gd;
2396 x = (int) &IDTVEC(lcall_syscall);
2397 gdp->gd_looffset = x;
2398 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2400 gdp->gd_type = SDT_SYS386CGT;
2401 gdp->gd_dpl = SEL_UPL;
2403 gdp->gd_hioffset = x >> 16;
2405 /* transfer to user mode */
2407 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2408 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2410 /* setup proc 0's pcb */
2411 thread0.td_pcb->pcb_flags = 0;
2412 #if defined(PAE) || defined(PAE_TABLES)
2413 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
2415 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2417 thread0.td_pcb->pcb_ext = 0;
2418 thread0.td_frame = &proc0_tf;
2426 /* Location of kernel stack for locore */
2427 return ((register_t)thread0.td_pcb);
2431 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2434 pcpu->pc_acpi_id = 0xffffffff;
2438 smap_sysctl_handler(SYSCTL_HANDLER_ARGS)
2440 struct bios_smap *smapbase;
2441 struct bios_smap_xattr smap;
2444 int count, error, i;
2446 /* Retrieve the system memory map from the loader. */
2447 kmdp = preload_search_by_type("elf kernel");
2449 kmdp = preload_search_by_type("elf32 kernel");
2450 smapbase = (struct bios_smap *)preload_search_info(kmdp,
2451 MODINFO_METADATA | MODINFOMD_SMAP);
2452 if (smapbase == NULL)
2454 smapattr = (uint32_t *)preload_search_info(kmdp,
2455 MODINFO_METADATA | MODINFOMD_SMAP_XATTR);
2456 count = *((u_int32_t *)smapbase - 1) / sizeof(*smapbase);
2458 for (i = 0; i < count; i++) {
2459 smap.base = smapbase[i].base;
2460 smap.length = smapbase[i].length;
2461 smap.type = smapbase[i].type;
2462 if (smapattr != NULL)
2463 smap.xattr = smapattr[i];
2466 error = SYSCTL_OUT(req, &smap, sizeof(smap));
2470 SYSCTL_PROC(_machdep, OID_AUTO, smap, CTLTYPE_OPAQUE|CTLFLAG_RD, NULL, 0,
2471 smap_sysctl_handler, "S,bios_smap_xattr", "Raw BIOS SMAP data");
2474 spinlock_enter(void)
2480 if (td->td_md.md_spinlock_count == 0) {
2481 flags = intr_disable();
2482 td->td_md.md_spinlock_count = 1;
2483 td->td_md.md_saved_flags = flags;
2485 td->td_md.md_spinlock_count++;
2497 flags = td->td_md.md_saved_flags;
2498 td->td_md.md_spinlock_count--;
2499 if (td->td_md.md_spinlock_count == 0)
2500 intr_restore(flags);
2503 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2504 static void f00f_hack(void *unused);
2505 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
2508 f00f_hack(void *unused)
2510 struct gate_descriptor *new_idt;
2518 printf("Intel Pentium detected, installing workaround for F00F bug\n");
2520 tmp = kmem_malloc(kernel_arena, PAGE_SIZE * 2, M_WAITOK | M_ZERO);
2522 panic("kmem_malloc returned 0");
2524 /* Put the problematic entry (#6) at the end of the lower page. */
2525 new_idt = (struct gate_descriptor*)
2526 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
2527 bcopy(idt, new_idt, sizeof(idt0));
2528 r_idt.rd_base = (u_int)new_idt;
2531 pmap_protect(kernel_pmap, tmp, tmp + PAGE_SIZE, VM_PROT_READ);
2533 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2536 * Construct a PCB from a trapframe. This is called from kdb_trap() where
2537 * we want to start a backtrace from the function that caused us to enter
2538 * the debugger. We have the context in the trapframe, but base the trace
2539 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
2540 * enough for a backtrace.
2543 makectx(struct trapframe *tf, struct pcb *pcb)
2546 pcb->pcb_edi = tf->tf_edi;
2547 pcb->pcb_esi = tf->tf_esi;
2548 pcb->pcb_ebp = tf->tf_ebp;
2549 pcb->pcb_ebx = tf->tf_ebx;
2550 pcb->pcb_eip = tf->tf_eip;
2551 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
2552 pcb->pcb_gs = rgs();
2556 ptrace_set_pc(struct thread *td, u_long addr)
2559 td->td_frame->tf_eip = addr;
2564 ptrace_single_step(struct thread *td)
2566 td->td_frame->tf_eflags |= PSL_T;
2571 ptrace_clear_single_step(struct thread *td)
2573 td->td_frame->tf_eflags &= ~PSL_T;
2578 fill_regs(struct thread *td, struct reg *regs)
2581 struct trapframe *tp;
2585 regs->r_gs = pcb->pcb_gs;
2586 return (fill_frame_regs(tp, regs));
2590 fill_frame_regs(struct trapframe *tp, struct reg *regs)
2592 regs->r_fs = tp->tf_fs;
2593 regs->r_es = tp->tf_es;
2594 regs->r_ds = tp->tf_ds;
2595 regs->r_edi = tp->tf_edi;
2596 regs->r_esi = tp->tf_esi;
2597 regs->r_ebp = tp->tf_ebp;
2598 regs->r_ebx = tp->tf_ebx;
2599 regs->r_edx = tp->tf_edx;
2600 regs->r_ecx = tp->tf_ecx;
2601 regs->r_eax = tp->tf_eax;
2602 regs->r_eip = tp->tf_eip;
2603 regs->r_cs = tp->tf_cs;
2604 regs->r_eflags = tp->tf_eflags;
2605 regs->r_esp = tp->tf_esp;
2606 regs->r_ss = tp->tf_ss;
2611 set_regs(struct thread *td, struct reg *regs)
2614 struct trapframe *tp;
2617 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2618 !CS_SECURE(regs->r_cs))
2621 tp->tf_fs = regs->r_fs;
2622 tp->tf_es = regs->r_es;
2623 tp->tf_ds = regs->r_ds;
2624 tp->tf_edi = regs->r_edi;
2625 tp->tf_esi = regs->r_esi;
2626 tp->tf_ebp = regs->r_ebp;
2627 tp->tf_ebx = regs->r_ebx;
2628 tp->tf_edx = regs->r_edx;
2629 tp->tf_ecx = regs->r_ecx;
2630 tp->tf_eax = regs->r_eax;
2631 tp->tf_eip = regs->r_eip;
2632 tp->tf_cs = regs->r_cs;
2633 tp->tf_eflags = regs->r_eflags;
2634 tp->tf_esp = regs->r_esp;
2635 tp->tf_ss = regs->r_ss;
2636 pcb->pcb_gs = regs->r_gs;
2641 fill_fpregs(struct thread *td, struct fpreg *fpregs)
2644 KASSERT(td == curthread || TD_IS_SUSPENDED(td) ||
2645 P_SHOULDSTOP(td->td_proc),
2646 ("not suspended thread %p", td));
2649 npx_fill_fpregs_xmm(&get_pcb_user_save_td(td)->sv_xmm,
2650 (struct save87 *)fpregs);
2652 bcopy(&get_pcb_user_save_td(td)->sv_87, fpregs,
2658 set_fpregs(struct thread *td, struct fpreg *fpregs)
2662 npx_set_fpregs_xmm((struct save87 *)fpregs,
2663 &get_pcb_user_save_td(td)->sv_xmm);
2665 bcopy(fpregs, &get_pcb_user_save_td(td)->sv_87,
2672 * Get machine context.
2675 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
2677 struct trapframe *tp;
2678 struct segment_descriptor *sdp;
2682 PROC_LOCK(curthread->td_proc);
2683 mcp->mc_onstack = sigonstack(tp->tf_esp);
2684 PROC_UNLOCK(curthread->td_proc);
2685 mcp->mc_gs = td->td_pcb->pcb_gs;
2686 mcp->mc_fs = tp->tf_fs;
2687 mcp->mc_es = tp->tf_es;
2688 mcp->mc_ds = tp->tf_ds;
2689 mcp->mc_edi = tp->tf_edi;
2690 mcp->mc_esi = tp->tf_esi;
2691 mcp->mc_ebp = tp->tf_ebp;
2692 mcp->mc_isp = tp->tf_isp;
2693 mcp->mc_eflags = tp->tf_eflags;
2694 if (flags & GET_MC_CLEAR_RET) {
2697 mcp->mc_eflags &= ~PSL_C;
2699 mcp->mc_eax = tp->tf_eax;
2700 mcp->mc_edx = tp->tf_edx;
2702 mcp->mc_ebx = tp->tf_ebx;
2703 mcp->mc_ecx = tp->tf_ecx;
2704 mcp->mc_eip = tp->tf_eip;
2705 mcp->mc_cs = tp->tf_cs;
2706 mcp->mc_esp = tp->tf_esp;
2707 mcp->mc_ss = tp->tf_ss;
2708 mcp->mc_len = sizeof(*mcp);
2709 get_fpcontext(td, mcp, NULL, 0);
2710 sdp = &td->td_pcb->pcb_fsd;
2711 mcp->mc_fsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2712 sdp = &td->td_pcb->pcb_gsd;
2713 mcp->mc_gsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2715 mcp->mc_xfpustate = 0;
2716 mcp->mc_xfpustate_len = 0;
2717 bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2));
2722 * Set machine context.
2724 * However, we don't set any but the user modifiable flags, and we won't
2725 * touch the cs selector.
2728 set_mcontext(struct thread *td, mcontext_t *mcp)
2730 struct trapframe *tp;
2735 if (mcp->mc_len != sizeof(*mcp) ||
2736 (mcp->mc_flags & ~_MC_FLAG_MASK) != 0)
2738 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
2739 (tp->tf_eflags & ~PSL_USERCHANGE);
2740 if (mcp->mc_flags & _MC_HASFPXSTATE) {
2741 if (mcp->mc_xfpustate_len > cpu_max_ext_state_size -
2742 sizeof(union savefpu))
2744 xfpustate = __builtin_alloca(mcp->mc_xfpustate_len);
2745 ret = copyin((void *)mcp->mc_xfpustate, xfpustate,
2746 mcp->mc_xfpustate_len);
2751 ret = set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
2754 tp->tf_fs = mcp->mc_fs;
2755 tp->tf_es = mcp->mc_es;
2756 tp->tf_ds = mcp->mc_ds;
2757 tp->tf_edi = mcp->mc_edi;
2758 tp->tf_esi = mcp->mc_esi;
2759 tp->tf_ebp = mcp->mc_ebp;
2760 tp->tf_ebx = mcp->mc_ebx;
2761 tp->tf_edx = mcp->mc_edx;
2762 tp->tf_ecx = mcp->mc_ecx;
2763 tp->tf_eax = mcp->mc_eax;
2764 tp->tf_eip = mcp->mc_eip;
2765 tp->tf_eflags = eflags;
2766 tp->tf_esp = mcp->mc_esp;
2767 tp->tf_ss = mcp->mc_ss;
2768 td->td_pcb->pcb_gs = mcp->mc_gs;
2773 get_fpcontext(struct thread *td, mcontext_t *mcp, char *xfpusave,
2774 size_t xfpusave_len)
2776 size_t max_len, len;
2778 mcp->mc_ownedfp = npxgetregs(td);
2779 bcopy(get_pcb_user_save_td(td), &mcp->mc_fpstate[0],
2780 sizeof(mcp->mc_fpstate));
2781 mcp->mc_fpformat = npxformat();
2782 if (!use_xsave || xfpusave_len == 0)
2784 max_len = cpu_max_ext_state_size - sizeof(union savefpu);
2786 if (len > max_len) {
2788 bzero(xfpusave + max_len, len - max_len);
2790 mcp->mc_flags |= _MC_HASFPXSTATE;
2791 mcp->mc_xfpustate_len = len;
2792 bcopy(get_pcb_user_save_td(td) + 1, xfpusave, len);
2796 set_fpcontext(struct thread *td, mcontext_t *mcp, char *xfpustate,
2797 size_t xfpustate_len)
2801 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2803 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2804 mcp->mc_fpformat != _MC_FPFMT_XMM)
2806 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) {
2807 /* We don't care what state is left in the FPU or PCB. */
2810 } else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2811 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2812 error = npxsetregs(td, (union savefpu *)&mcp->mc_fpstate,
2813 xfpustate, xfpustate_len);
2820 fpstate_drop(struct thread *td)
2823 KASSERT(PCB_USER_FPU(td->td_pcb), ("fpstate_drop: kernel-owned fpu"));
2825 if (PCPU_GET(fpcurthread) == td)
2828 * XXX force a full drop of the npx. The above only drops it if we
2829 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
2831 * XXX I don't much like npxgetregs()'s semantics of doing a full
2832 * drop. Dropping only to the pcb matches fnsave's behaviour.
2833 * We only need to drop to !PCB_INITDONE in sendsig(). But
2834 * sendsig() is the only caller of npxgetregs()... perhaps we just
2835 * have too many layers.
2837 curthread->td_pcb->pcb_flags &= ~(PCB_NPXINITDONE |
2838 PCB_NPXUSERINITDONE);
2843 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2848 dbregs->dr[0] = rdr0();
2849 dbregs->dr[1] = rdr1();
2850 dbregs->dr[2] = rdr2();
2851 dbregs->dr[3] = rdr3();
2852 dbregs->dr[6] = rdr6();
2853 dbregs->dr[7] = rdr7();
2856 dbregs->dr[0] = pcb->pcb_dr0;
2857 dbregs->dr[1] = pcb->pcb_dr1;
2858 dbregs->dr[2] = pcb->pcb_dr2;
2859 dbregs->dr[3] = pcb->pcb_dr3;
2860 dbregs->dr[6] = pcb->pcb_dr6;
2861 dbregs->dr[7] = pcb->pcb_dr7;
2869 set_dbregs(struct thread *td, struct dbreg *dbregs)
2875 load_dr0(dbregs->dr[0]);
2876 load_dr1(dbregs->dr[1]);
2877 load_dr2(dbregs->dr[2]);
2878 load_dr3(dbregs->dr[3]);
2879 load_dr6(dbregs->dr[6]);
2880 load_dr7(dbregs->dr[7]);
2883 * Don't let an illegal value for dr7 get set. Specifically,
2884 * check for undefined settings. Setting these bit patterns
2885 * result in undefined behaviour and can lead to an unexpected
2888 for (i = 0; i < 4; i++) {
2889 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
2891 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
2898 * Don't let a process set a breakpoint that is not within the
2899 * process's address space. If a process could do this, it
2900 * could halt the system by setting a breakpoint in the kernel
2901 * (if ddb was enabled). Thus, we need to check to make sure
2902 * that no breakpoints are being enabled for addresses outside
2903 * process's address space.
2905 * XXX - what about when the watched area of the user's
2906 * address space is written into from within the kernel
2907 * ... wouldn't that still cause a breakpoint to be generated
2908 * from within kernel mode?
2911 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
2912 /* dr0 is enabled */
2913 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2917 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
2918 /* dr1 is enabled */
2919 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2923 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
2924 /* dr2 is enabled */
2925 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2929 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
2930 /* dr3 is enabled */
2931 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2935 pcb->pcb_dr0 = dbregs->dr[0];
2936 pcb->pcb_dr1 = dbregs->dr[1];
2937 pcb->pcb_dr2 = dbregs->dr[2];
2938 pcb->pcb_dr3 = dbregs->dr[3];
2939 pcb->pcb_dr6 = dbregs->dr[6];
2940 pcb->pcb_dr7 = dbregs->dr[7];
2942 pcb->pcb_flags |= PCB_DBREGS;
2949 * Return > 0 if a hardware breakpoint has been hit, and the
2950 * breakpoint was in user space. Return 0, otherwise.
2953 user_dbreg_trap(void)
2955 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2956 u_int32_t bp; /* breakpoint bits extracted from dr6 */
2957 int nbp; /* number of breakpoints that triggered */
2958 caddr_t addr[4]; /* breakpoint addresses */
2962 if ((dr7 & 0x000000ff) == 0) {
2964 * all GE and LE bits in the dr7 register are zero,
2965 * thus the trap couldn't have been caused by the
2966 * hardware debug registers
2973 bp = dr6 & 0x0000000f;
2977 * None of the breakpoint bits are set meaning this
2978 * trap was not caused by any of the debug registers
2984 * at least one of the breakpoints were hit, check to see
2985 * which ones and if any of them are user space addresses
2989 addr[nbp++] = (caddr_t)rdr0();
2992 addr[nbp++] = (caddr_t)rdr1();
2995 addr[nbp++] = (caddr_t)rdr2();
2998 addr[nbp++] = (caddr_t)rdr3();
3001 for (i = 0; i < nbp; i++) {
3002 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
3004 * addr[i] is in user space
3011 * None of the breakpoints are in user space.
3019 * Provide inb() and outb() as functions. They are normally only available as
3020 * inline functions, thus cannot be called from the debugger.
3023 /* silence compiler warnings */
3024 u_char inb_(u_short);
3025 void outb_(u_short, u_char);
3034 outb_(u_short port, u_char data)