2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 #include "opt_atpic.h"
45 #include "opt_compat.h"
50 #include "opt_kstack_pages.h"
51 #include "opt_maxmem.h"
52 #include "opt_mp_watchdog.h"
53 #include "opt_perfmon.h"
54 #include "opt_platform.h"
57 #include <sys/param.h>
59 #include <sys/systm.h>
63 #include <sys/callout.h>
66 #include <sys/eventhandler.h>
68 #include <sys/imgact.h>
70 #include <sys/kernel.h>
72 #include <sys/linker.h>
74 #include <sys/malloc.h>
75 #include <sys/memrange.h>
76 #include <sys/msgbuf.h>
77 #include <sys/mutex.h>
79 #include <sys/ptrace.h>
80 #include <sys/reboot.h>
81 #include <sys/rwlock.h>
82 #include <sys/sched.h>
83 #include <sys/signalvar.h>
87 #include <sys/syscallsubr.h>
88 #include <sys/sysctl.h>
89 #include <sys/sysent.h>
90 #include <sys/sysproto.h>
91 #include <sys/ucontext.h>
92 #include <sys/vmmeter.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_map.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_pager.h>
101 #include <vm/vm_param.h>
105 #error KDB must be enabled in order for DDB to work!
108 #include <ddb/db_sym.h>
113 #include <net/netisr.h>
115 #include <machine/bootinfo.h>
116 #include <machine/clock.h>
117 #include <machine/cpu.h>
118 #include <machine/cputypes.h>
119 #include <machine/intr_machdep.h>
121 #include <machine/md_var.h>
122 #include <machine/metadata.h>
123 #include <machine/mp_watchdog.h>
124 #include <machine/pc/bios.h>
125 #include <machine/pcb.h>
126 #include <machine/pcb_ext.h>
127 #include <machine/proc.h>
128 #include <machine/reg.h>
129 #include <machine/sigframe.h>
130 #include <machine/specialreg.h>
131 #include <machine/vm86.h>
132 #include <x86/init.h>
134 #include <machine/perfmon.h>
137 #include <machine/smp.h>
144 #include <x86/apicvar.h>
148 #include <x86/isa/icu.h>
152 #include <machine/xbox.h>
154 int arch_i386_is_xbox = 0;
155 uint32_t arch_i386_xbox_memsize = 0;
158 /* Sanity check for __curthread() */
159 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
161 extern register_t init386(int first);
162 extern void dblfault_handler(void);
164 static void cpu_startup(void *);
165 static void fpstate_drop(struct thread *td);
166 static void get_fpcontext(struct thread *td, mcontext_t *mcp,
167 char *xfpusave, size_t xfpusave_len);
168 static int set_fpcontext(struct thread *td, mcontext_t *mcp,
169 char *xfpustate, size_t xfpustate_len);
170 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
172 /* Intel ICH registers */
173 #define ICH_PMBASE 0x400
174 #define ICH_SMI_EN ICH_PMBASE + 0x30
176 int _udatasel, _ucodesel;
182 static void osendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
184 #ifdef COMPAT_FREEBSD4
185 static void freebsd4_sendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
192 FEATURE(pae, "Physical Address Extensions");
196 * The number of PHYSMAP entries must be one less than the number of
197 * PHYSSEG entries because the PHYSMAP entry that spans the largest
198 * physical address that is accessible by ISA DMA is split into two
201 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
203 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
204 vm_paddr_t dump_avail[PHYSMAP_SIZE + 2];
206 /* must be 2 less so 0 0 can signal end of chunks */
207 #define PHYS_AVAIL_ARRAY_END (nitems(phys_avail) - 2)
208 #define DUMP_AVAIL_ARRAY_END (nitems(dump_avail) - 2)
210 struct kva_md_info kmi;
212 static struct trapframe proc0_tf;
213 struct pcpu __pcpu[MAXCPU];
217 struct mem_range_softc mem_range_softc;
219 /* Default init_ops implementation. */
220 struct init_ops init_ops = {
221 .early_clock_source_init = i8254_init,
222 .early_delay = i8254_delay,
224 .msi_init = msi_init,
236 * On MacBooks, we need to disallow the legacy USB circuit to
237 * generate an SMI# because this can cause several problems,
238 * namely: incorrect CPU frequency detection and failure to
240 * We do this by disabling a bit in the SMI_EN (SMI Control and
241 * Enable register) of the Intel ICH LPC Interface Bridge.
243 sysenv = kern_getenv("smbios.system.product");
244 if (sysenv != NULL) {
245 if (strncmp(sysenv, "MacBook1,1", 10) == 0 ||
246 strncmp(sysenv, "MacBook3,1", 10) == 0 ||
247 strncmp(sysenv, "MacBook4,1", 10) == 0 ||
248 strncmp(sysenv, "MacBookPro1,1", 13) == 0 ||
249 strncmp(sysenv, "MacBookPro1,2", 13) == 0 ||
250 strncmp(sysenv, "MacBookPro3,1", 13) == 0 ||
251 strncmp(sysenv, "MacBookPro4,1", 13) == 0 ||
252 strncmp(sysenv, "Macmini1,1", 10) == 0) {
254 printf("Disabling LEGACY_USB_EN bit on "
256 outl(ICH_SMI_EN, inl(ICH_SMI_EN) & ~0x8);
262 * Good {morning,afternoon,evening,night}.
266 panicifcpuunsupported();
272 * Display physical memory if SMBIOS reports reasonable amount.
275 sysenv = kern_getenv("smbios.memory.enabled");
276 if (sysenv != NULL) {
277 memsize = (uintmax_t)strtoul(sysenv, (char **)NULL, 10) << 10;
280 if (memsize < ptoa((uintmax_t)vm_cnt.v_free_count))
281 memsize = ptoa((uintmax_t)Maxmem);
282 printf("real memory = %ju (%ju MB)\n", memsize, memsize >> 20);
283 realmem = atop(memsize);
286 * Display any holes after the first chunk of extended memory.
291 printf("Physical memory chunk(s):\n");
292 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
295 size = phys_avail[indx + 1] - phys_avail[indx];
297 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
298 (uintmax_t)phys_avail[indx],
299 (uintmax_t)phys_avail[indx + 1] - 1,
300 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
304 vm_ksubmap_init(&kmi);
306 printf("avail memory = %ju (%ju MB)\n",
307 ptoa((uintmax_t)vm_cnt.v_free_count),
308 ptoa((uintmax_t)vm_cnt.v_free_count) / 1048576);
311 * Set up buffers, so they can be used to read disk labels.
314 vm_pager_bufferinit();
319 * Send an interrupt to process.
321 * Stack is set up to allow sigcode stored
322 * at top to call routine, followed by call
323 * to sigreturn routine below. After sigreturn
324 * resets the signal mask, the stack, and the
325 * frame pointer, it returns to the user
330 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
332 struct osigframe sf, *fp;
336 struct trapframe *regs;
342 PROC_LOCK_ASSERT(p, MA_OWNED);
343 sig = ksi->ksi_signo;
345 mtx_assert(&psp->ps_mtx, MA_OWNED);
347 oonstack = sigonstack(regs->tf_esp);
349 /* Allocate space for the signal handler context. */
350 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
351 SIGISMEMBER(psp->ps_sigonstack, sig)) {
352 fp = (struct osigframe *)((uintptr_t)td->td_sigstk.ss_sp +
353 td->td_sigstk.ss_size - sizeof(struct osigframe));
354 #if defined(COMPAT_43)
355 td->td_sigstk.ss_flags |= SS_ONSTACK;
358 fp = (struct osigframe *)regs->tf_esp - 1;
360 /* Build the argument list for the signal handler. */
362 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
363 bzero(&sf.sf_siginfo, sizeof(sf.sf_siginfo));
364 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
365 /* Signal handler installed with SA_SIGINFO. */
366 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
367 sf.sf_siginfo.si_signo = sig;
368 sf.sf_siginfo.si_code = ksi->ksi_code;
369 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
372 /* Old FreeBSD-style arguments. */
373 sf.sf_arg2 = ksi->ksi_code;
374 sf.sf_addr = (register_t)ksi->ksi_addr;
375 sf.sf_ahu.sf_handler = catcher;
377 mtx_unlock(&psp->ps_mtx);
380 /* Save most if not all of trap frame. */
381 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
382 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
383 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
384 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
385 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
386 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
387 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
388 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
389 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
390 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
391 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
392 sf.sf_siginfo.si_sc.sc_gs = rgs();
393 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
395 /* Build the signal context to be used by osigreturn(). */
396 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
397 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
398 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
399 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
400 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
401 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
402 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
403 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
406 * If we're a vm86 process, we want to save the segment registers.
407 * We also change eflags to be our emulated eflags, not the actual
410 if (regs->tf_eflags & PSL_VM) {
411 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
412 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
413 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
415 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
416 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
417 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
418 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
420 if (vm86->vm86_has_vme == 0)
421 sf.sf_siginfo.si_sc.sc_ps =
422 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
423 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
425 /* See sendsig() for comments. */
426 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
430 * Copy the sigframe out to the user's stack.
432 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
434 printf("process %ld has trashed its stack\n", (long)p->p_pid);
440 regs->tf_esp = (int)fp;
441 if (p->p_sysent->sv_sigcode_base != 0) {
442 regs->tf_eip = p->p_sysent->sv_sigcode_base + szsigcode -
445 /* a.out sysentvec does not use shared page */
446 regs->tf_eip = p->p_sysent->sv_psstrings - szosigcode;
448 regs->tf_eflags &= ~(PSL_T | PSL_D);
449 regs->tf_cs = _ucodesel;
450 regs->tf_ds = _udatasel;
451 regs->tf_es = _udatasel;
452 regs->tf_fs = _udatasel;
454 regs->tf_ss = _udatasel;
456 mtx_lock(&psp->ps_mtx);
458 #endif /* COMPAT_43 */
460 #ifdef COMPAT_FREEBSD4
462 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
464 struct sigframe4 sf, *sfp;
468 struct trapframe *regs;
474 PROC_LOCK_ASSERT(p, MA_OWNED);
475 sig = ksi->ksi_signo;
477 mtx_assert(&psp->ps_mtx, MA_OWNED);
479 oonstack = sigonstack(regs->tf_esp);
481 /* Save user context. */
482 bzero(&sf, sizeof(sf));
483 sf.sf_uc.uc_sigmask = *mask;
484 sf.sf_uc.uc_stack = td->td_sigstk;
485 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
486 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
487 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
488 sf.sf_uc.uc_mcontext.mc_gs = rgs();
489 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
490 bzero(sf.sf_uc.uc_mcontext.mc_fpregs,
491 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
492 bzero(sf.sf_uc.uc_mcontext.__spare__,
493 sizeof(sf.sf_uc.uc_mcontext.__spare__));
494 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
496 /* Allocate space for the signal handler context. */
497 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
498 SIGISMEMBER(psp->ps_sigonstack, sig)) {
499 sfp = (struct sigframe4 *)((uintptr_t)td->td_sigstk.ss_sp +
500 td->td_sigstk.ss_size - sizeof(struct sigframe4));
501 #if defined(COMPAT_43)
502 td->td_sigstk.ss_flags |= SS_ONSTACK;
505 sfp = (struct sigframe4 *)regs->tf_esp - 1;
507 /* Build the argument list for the signal handler. */
509 sf.sf_ucontext = (register_t)&sfp->sf_uc;
510 bzero(&sf.sf_si, sizeof(sf.sf_si));
511 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
512 /* Signal handler installed with SA_SIGINFO. */
513 sf.sf_siginfo = (register_t)&sfp->sf_si;
514 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
516 /* Fill in POSIX parts */
517 sf.sf_si.si_signo = sig;
518 sf.sf_si.si_code = ksi->ksi_code;
519 sf.sf_si.si_addr = ksi->ksi_addr;
521 /* Old FreeBSD-style arguments. */
522 sf.sf_siginfo = ksi->ksi_code;
523 sf.sf_addr = (register_t)ksi->ksi_addr;
524 sf.sf_ahu.sf_handler = catcher;
526 mtx_unlock(&psp->ps_mtx);
530 * If we're a vm86 process, we want to save the segment registers.
531 * We also change eflags to be our emulated eflags, not the actual
534 if (regs->tf_eflags & PSL_VM) {
535 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
536 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
538 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
539 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
540 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
541 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
543 if (vm86->vm86_has_vme == 0)
544 sf.sf_uc.uc_mcontext.mc_eflags =
545 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
546 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
549 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
550 * syscalls made by the signal handler. This just avoids
551 * wasting time for our lazy fixup of such faults. PSL_NT
552 * does nothing in vm86 mode, but vm86 programs can set it
553 * almost legitimately in probes for old cpu types.
555 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
559 * Copy the sigframe out to the user's stack.
561 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
563 printf("process %ld has trashed its stack\n", (long)p->p_pid);
569 regs->tf_esp = (int)sfp;
570 regs->tf_eip = p->p_sysent->sv_sigcode_base + szsigcode -
572 regs->tf_eflags &= ~(PSL_T | PSL_D);
573 regs->tf_cs = _ucodesel;
574 regs->tf_ds = _udatasel;
575 regs->tf_es = _udatasel;
576 regs->tf_fs = _udatasel;
577 regs->tf_ss = _udatasel;
579 mtx_lock(&psp->ps_mtx);
581 #endif /* COMPAT_FREEBSD4 */
584 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
586 struct sigframe sf, *sfp;
591 struct trapframe *regs;
592 struct segment_descriptor *sdp;
600 PROC_LOCK_ASSERT(p, MA_OWNED);
601 sig = ksi->ksi_signo;
603 mtx_assert(&psp->ps_mtx, MA_OWNED);
604 #ifdef COMPAT_FREEBSD4
605 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
606 freebsd4_sendsig(catcher, ksi, mask);
611 if (SIGISMEMBER(psp->ps_osigset, sig)) {
612 osendsig(catcher, ksi, mask);
617 oonstack = sigonstack(regs->tf_esp);
619 if (cpu_max_ext_state_size > sizeof(union savefpu) && use_xsave) {
620 xfpusave_len = cpu_max_ext_state_size - sizeof(union savefpu);
621 xfpusave = __builtin_alloca(xfpusave_len);
627 /* Save user context. */
628 bzero(&sf, sizeof(sf));
629 sf.sf_uc.uc_sigmask = *mask;
630 sf.sf_uc.uc_stack = td->td_sigstk;
631 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
632 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
633 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
634 sf.sf_uc.uc_mcontext.mc_gs = rgs();
635 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
636 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
637 get_fpcontext(td, &sf.sf_uc.uc_mcontext, xfpusave, xfpusave_len);
640 * Unconditionally fill the fsbase and gsbase into the mcontext.
642 sdp = &td->td_pcb->pcb_fsd;
643 sf.sf_uc.uc_mcontext.mc_fsbase = sdp->sd_hibase << 24 |
645 sdp = &td->td_pcb->pcb_gsd;
646 sf.sf_uc.uc_mcontext.mc_gsbase = sdp->sd_hibase << 24 |
648 bzero(sf.sf_uc.uc_mcontext.mc_spare2,
649 sizeof(sf.sf_uc.uc_mcontext.mc_spare2));
650 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
652 /* Allocate space for the signal handler context. */
653 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
654 SIGISMEMBER(psp->ps_sigonstack, sig)) {
655 sp = (char *)td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
656 #if defined(COMPAT_43)
657 td->td_sigstk.ss_flags |= SS_ONSTACK;
660 sp = (char *)regs->tf_esp - 128;
661 if (xfpusave != NULL) {
663 sp = (char *)((unsigned int)sp & ~0x3F);
664 sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp;
666 sp -= sizeof(struct sigframe);
668 /* Align to 16 bytes. */
669 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
671 /* Build the argument list for the signal handler. */
673 sf.sf_ucontext = (register_t)&sfp->sf_uc;
674 bzero(&sf.sf_si, sizeof(sf.sf_si));
675 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
676 /* Signal handler installed with SA_SIGINFO. */
677 sf.sf_siginfo = (register_t)&sfp->sf_si;
678 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
680 /* Fill in POSIX parts */
681 sf.sf_si = ksi->ksi_info;
682 sf.sf_si.si_signo = sig; /* maybe a translated signal */
684 /* Old FreeBSD-style arguments. */
685 sf.sf_siginfo = ksi->ksi_code;
686 sf.sf_addr = (register_t)ksi->ksi_addr;
687 sf.sf_ahu.sf_handler = catcher;
689 mtx_unlock(&psp->ps_mtx);
693 * If we're a vm86 process, we want to save the segment registers.
694 * We also change eflags to be our emulated eflags, not the actual
697 if (regs->tf_eflags & PSL_VM) {
698 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
699 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
701 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
702 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
703 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
704 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
706 if (vm86->vm86_has_vme == 0)
707 sf.sf_uc.uc_mcontext.mc_eflags =
708 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
709 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
712 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
713 * syscalls made by the signal handler. This just avoids
714 * wasting time for our lazy fixup of such faults. PSL_NT
715 * does nothing in vm86 mode, but vm86 programs can set it
716 * almost legitimately in probes for old cpu types.
718 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
722 * Copy the sigframe out to the user's stack.
724 if (copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
725 (xfpusave != NULL && copyout(xfpusave,
726 (void *)sf.sf_uc.uc_mcontext.mc_xfpustate, xfpusave_len)
729 printf("process %ld has trashed its stack\n", (long)p->p_pid);
735 regs->tf_esp = (int)sfp;
736 regs->tf_eip = p->p_sysent->sv_sigcode_base;
737 if (regs->tf_eip == 0)
738 regs->tf_eip = p->p_sysent->sv_psstrings - szsigcode;
739 regs->tf_eflags &= ~(PSL_T | PSL_D);
740 regs->tf_cs = _ucodesel;
741 regs->tf_ds = _udatasel;
742 regs->tf_es = _udatasel;
743 regs->tf_fs = _udatasel;
744 regs->tf_ss = _udatasel;
746 mtx_lock(&psp->ps_mtx);
750 * System call to cleanup state after a signal
751 * has been taken. Reset signal mask and
752 * stack state from context left by sendsig (above).
753 * Return to previous pc and psl as specified by
754 * context left by sendsig. Check carefully to
755 * make sure that the user has not modified the
756 * state to gain improper privileges.
764 struct osigreturn_args /* {
765 struct osigcontext *sigcntxp;
768 struct osigcontext sc;
769 struct trapframe *regs;
770 struct osigcontext *scp;
775 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
780 if (eflags & PSL_VM) {
781 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
782 struct vm86_kernel *vm86;
785 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
786 * set up the vm86 area, and we can't enter vm86 mode.
788 if (td->td_pcb->pcb_ext == 0)
790 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
791 if (vm86->vm86_inited == 0)
794 /* Go back to user mode if both flags are set. */
795 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
796 ksiginfo_init_trap(&ksi);
797 ksi.ksi_signo = SIGBUS;
798 ksi.ksi_code = BUS_OBJERR;
799 ksi.ksi_addr = (void *)regs->tf_eip;
800 trapsignal(td, &ksi);
803 if (vm86->vm86_has_vme) {
804 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
805 (eflags & VME_USERCHANGE) | PSL_VM;
807 vm86->vm86_eflags = eflags; /* save VIF, VIP */
808 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
809 (eflags & VM_USERCHANGE) | PSL_VM;
811 tf->tf_vm86_ds = scp->sc_ds;
812 tf->tf_vm86_es = scp->sc_es;
813 tf->tf_vm86_fs = scp->sc_fs;
814 tf->tf_vm86_gs = scp->sc_gs;
815 tf->tf_ds = _udatasel;
816 tf->tf_es = _udatasel;
817 tf->tf_fs = _udatasel;
820 * Don't allow users to change privileged or reserved flags.
822 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
827 * Don't allow users to load a valid privileged %cs. Let the
828 * hardware check for invalid selectors, excess privilege in
829 * other selectors, invalid %eip's and invalid %esp's.
831 if (!CS_SECURE(scp->sc_cs)) {
832 ksiginfo_init_trap(&ksi);
833 ksi.ksi_signo = SIGBUS;
834 ksi.ksi_code = BUS_OBJERR;
835 ksi.ksi_trapno = T_PROTFLT;
836 ksi.ksi_addr = (void *)regs->tf_eip;
837 trapsignal(td, &ksi);
840 regs->tf_ds = scp->sc_ds;
841 regs->tf_es = scp->sc_es;
842 regs->tf_fs = scp->sc_fs;
845 /* Restore remaining registers. */
846 regs->tf_eax = scp->sc_eax;
847 regs->tf_ebx = scp->sc_ebx;
848 regs->tf_ecx = scp->sc_ecx;
849 regs->tf_edx = scp->sc_edx;
850 regs->tf_esi = scp->sc_esi;
851 regs->tf_edi = scp->sc_edi;
852 regs->tf_cs = scp->sc_cs;
853 regs->tf_ss = scp->sc_ss;
854 regs->tf_isp = scp->sc_isp;
855 regs->tf_ebp = scp->sc_fp;
856 regs->tf_esp = scp->sc_sp;
857 regs->tf_eip = scp->sc_pc;
858 regs->tf_eflags = eflags;
860 #if defined(COMPAT_43)
861 if (scp->sc_onstack & 1)
862 td->td_sigstk.ss_flags |= SS_ONSTACK;
864 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
866 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
868 return (EJUSTRETURN);
870 #endif /* COMPAT_43 */
872 #ifdef COMPAT_FREEBSD4
877 freebsd4_sigreturn(td, uap)
879 struct freebsd4_sigreturn_args /* {
880 const ucontext4 *sigcntxp;
884 struct trapframe *regs;
885 struct ucontext4 *ucp;
886 int cs, eflags, error;
889 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
894 eflags = ucp->uc_mcontext.mc_eflags;
895 if (eflags & PSL_VM) {
896 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
897 struct vm86_kernel *vm86;
900 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
901 * set up the vm86 area, and we can't enter vm86 mode.
903 if (td->td_pcb->pcb_ext == 0)
905 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
906 if (vm86->vm86_inited == 0)
909 /* Go back to user mode if both flags are set. */
910 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
911 ksiginfo_init_trap(&ksi);
912 ksi.ksi_signo = SIGBUS;
913 ksi.ksi_code = BUS_OBJERR;
914 ksi.ksi_addr = (void *)regs->tf_eip;
915 trapsignal(td, &ksi);
917 if (vm86->vm86_has_vme) {
918 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
919 (eflags & VME_USERCHANGE) | PSL_VM;
921 vm86->vm86_eflags = eflags; /* save VIF, VIP */
922 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
923 (eflags & VM_USERCHANGE) | PSL_VM;
925 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
926 tf->tf_eflags = eflags;
927 tf->tf_vm86_ds = tf->tf_ds;
928 tf->tf_vm86_es = tf->tf_es;
929 tf->tf_vm86_fs = tf->tf_fs;
930 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
931 tf->tf_ds = _udatasel;
932 tf->tf_es = _udatasel;
933 tf->tf_fs = _udatasel;
936 * Don't allow users to change privileged or reserved flags.
938 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
939 uprintf("pid %d (%s): freebsd4_sigreturn eflags = 0x%x\n",
940 td->td_proc->p_pid, td->td_name, eflags);
945 * Don't allow users to load a valid privileged %cs. Let the
946 * hardware check for invalid selectors, excess privilege in
947 * other selectors, invalid %eip's and invalid %esp's.
949 cs = ucp->uc_mcontext.mc_cs;
950 if (!CS_SECURE(cs)) {
951 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
952 td->td_proc->p_pid, td->td_name, cs);
953 ksiginfo_init_trap(&ksi);
954 ksi.ksi_signo = SIGBUS;
955 ksi.ksi_code = BUS_OBJERR;
956 ksi.ksi_trapno = T_PROTFLT;
957 ksi.ksi_addr = (void *)regs->tf_eip;
958 trapsignal(td, &ksi);
962 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
965 #if defined(COMPAT_43)
966 if (ucp->uc_mcontext.mc_onstack & 1)
967 td->td_sigstk.ss_flags |= SS_ONSTACK;
969 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
971 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
972 return (EJUSTRETURN);
974 #endif /* COMPAT_FREEBSD4 */
980 sys_sigreturn(td, uap)
982 struct sigreturn_args /* {
983 const struct __ucontext *sigcntxp;
988 struct trapframe *regs;
991 size_t xfpustate_len;
992 int cs, eflags, error, ret;
997 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
1001 if ((ucp->uc_mcontext.mc_flags & ~_MC_FLAG_MASK) != 0) {
1002 uprintf("pid %d (%s): sigreturn mc_flags %x\n", p->p_pid,
1003 td->td_name, ucp->uc_mcontext.mc_flags);
1006 regs = td->td_frame;
1007 eflags = ucp->uc_mcontext.mc_eflags;
1008 if (eflags & PSL_VM) {
1009 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
1010 struct vm86_kernel *vm86;
1013 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
1014 * set up the vm86 area, and we can't enter vm86 mode.
1016 if (td->td_pcb->pcb_ext == 0)
1018 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
1019 if (vm86->vm86_inited == 0)
1022 /* Go back to user mode if both flags are set. */
1023 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
1024 ksiginfo_init_trap(&ksi);
1025 ksi.ksi_signo = SIGBUS;
1026 ksi.ksi_code = BUS_OBJERR;
1027 ksi.ksi_addr = (void *)regs->tf_eip;
1028 trapsignal(td, &ksi);
1031 if (vm86->vm86_has_vme) {
1032 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
1033 (eflags & VME_USERCHANGE) | PSL_VM;
1035 vm86->vm86_eflags = eflags; /* save VIF, VIP */
1036 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
1037 (eflags & VM_USERCHANGE) | PSL_VM;
1039 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
1040 tf->tf_eflags = eflags;
1041 tf->tf_vm86_ds = tf->tf_ds;
1042 tf->tf_vm86_es = tf->tf_es;
1043 tf->tf_vm86_fs = tf->tf_fs;
1044 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
1045 tf->tf_ds = _udatasel;
1046 tf->tf_es = _udatasel;
1047 tf->tf_fs = _udatasel;
1050 * Don't allow users to change privileged or reserved flags.
1052 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
1053 uprintf("pid %d (%s): sigreturn eflags = 0x%x\n",
1054 td->td_proc->p_pid, td->td_name, eflags);
1059 * Don't allow users to load a valid privileged %cs. Let the
1060 * hardware check for invalid selectors, excess privilege in
1061 * other selectors, invalid %eip's and invalid %esp's.
1063 cs = ucp->uc_mcontext.mc_cs;
1064 if (!CS_SECURE(cs)) {
1065 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
1066 td->td_proc->p_pid, td->td_name, cs);
1067 ksiginfo_init_trap(&ksi);
1068 ksi.ksi_signo = SIGBUS;
1069 ksi.ksi_code = BUS_OBJERR;
1070 ksi.ksi_trapno = T_PROTFLT;
1071 ksi.ksi_addr = (void *)regs->tf_eip;
1072 trapsignal(td, &ksi);
1076 if ((uc.uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) {
1077 xfpustate_len = uc.uc_mcontext.mc_xfpustate_len;
1078 if (xfpustate_len > cpu_max_ext_state_size -
1079 sizeof(union savefpu)) {
1081 "pid %d (%s): sigreturn xfpusave_len = 0x%zx\n",
1082 p->p_pid, td->td_name, xfpustate_len);
1085 xfpustate = __builtin_alloca(xfpustate_len);
1086 error = copyin((const void *)uc.uc_mcontext.mc_xfpustate,
1087 xfpustate, xfpustate_len);
1090 "pid %d (%s): sigreturn copying xfpustate failed\n",
1091 p->p_pid, td->td_name);
1098 ret = set_fpcontext(td, &ucp->uc_mcontext, xfpustate,
1102 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1105 #if defined(COMPAT_43)
1106 if (ucp->uc_mcontext.mc_onstack & 1)
1107 td->td_sigstk.ss_flags |= SS_ONSTACK;
1109 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1112 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
1113 return (EJUSTRETURN);
1117 * Reset registers to default values on exec.
1120 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
1122 struct trapframe *regs = td->td_frame;
1123 struct pcb *pcb = td->td_pcb;
1125 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1126 pcb->pcb_gs = _udatasel;
1129 mtx_lock_spin(&dt_lock);
1130 if (td->td_proc->p_md.md_ldt)
1133 mtx_unlock_spin(&dt_lock);
1135 bzero((char *)regs, sizeof(struct trapframe));
1136 regs->tf_eip = imgp->entry_addr;
1137 regs->tf_esp = stack;
1138 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1139 regs->tf_ss = _udatasel;
1140 regs->tf_ds = _udatasel;
1141 regs->tf_es = _udatasel;
1142 regs->tf_fs = _udatasel;
1143 regs->tf_cs = _ucodesel;
1145 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1146 regs->tf_ebx = imgp->ps_strings;
1149 * Reset the hardware debug registers if they were in use.
1150 * They won't have any meaning for the newly exec'd process.
1152 if (pcb->pcb_flags & PCB_DBREGS) {
1159 if (pcb == curpcb) {
1161 * Clear the debug registers on the running
1162 * CPU, otherwise they will end up affecting
1163 * the next process we switch to.
1167 pcb->pcb_flags &= ~PCB_DBREGS;
1170 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1173 * Drop the FP state if we hold it, so that the process gets a
1174 * clean FP state if it uses the FPU again.
1179 * XXX - Linux emulator
1180 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1183 td->td_retval[1] = 0;
1194 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1196 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1197 * instructions. We must set the CR0_MP bit and use the CR0_TS
1198 * bit to control the trap, because setting the CR0_EM bit does
1199 * not cause WAIT instructions to trap. It's important to trap
1200 * WAIT instructions - otherwise the "wait" variants of no-wait
1201 * control instructions would degenerate to the "no-wait" variants
1202 * after FP context switches but work correctly otherwise. It's
1203 * particularly important to trap WAITs when there is no NPX -
1204 * otherwise the "wait" variants would always degenerate.
1206 * Try setting CR0_NE to get correct error reporting on 486DX's.
1207 * Setting it should fail or do nothing on lesser processors.
1209 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1214 u_long bootdev; /* not a struct cdev *- encoding is different */
1215 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1216 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1218 static char bootmethod[16] = "BIOS";
1219 SYSCTL_STRING(_machdep, OID_AUTO, bootmethod, CTLFLAG_RD, bootmethod, 0,
1220 "System firmware boot method");
1223 * Initialize 386 and configure to run kernel
1227 * Initialize segments & interrupt table
1232 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1233 union descriptor ldt[NLDT]; /* local descriptor table */
1234 static struct gate_descriptor idt0[NIDT];
1235 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1236 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1237 struct mtx dt_lock; /* lock for GDT and LDT */
1239 static struct i386tss dblfault_tss;
1240 static char dblfault_stack[PAGE_SIZE];
1242 extern vm_offset_t proc0kstack;
1246 * software prototypes -- in more palatable form.
1248 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1249 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1251 struct soft_segment_descriptor gdt_segs[] = {
1252 /* GNULL_SEL 0 Null Descriptor */
1258 .ssd_xx = 0, .ssd_xx1 = 0,
1261 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1263 .ssd_limit = 0xfffff,
1264 .ssd_type = SDT_MEMRWA,
1267 .ssd_xx = 0, .ssd_xx1 = 0,
1270 /* GUFS_SEL 2 %fs Descriptor for user */
1272 .ssd_limit = 0xfffff,
1273 .ssd_type = SDT_MEMRWA,
1276 .ssd_xx = 0, .ssd_xx1 = 0,
1279 /* GUGS_SEL 3 %gs Descriptor for user */
1281 .ssd_limit = 0xfffff,
1282 .ssd_type = SDT_MEMRWA,
1285 .ssd_xx = 0, .ssd_xx1 = 0,
1288 /* GCODE_SEL 4 Code Descriptor for kernel */
1290 .ssd_limit = 0xfffff,
1291 .ssd_type = SDT_MEMERA,
1294 .ssd_xx = 0, .ssd_xx1 = 0,
1297 /* GDATA_SEL 5 Data Descriptor for kernel */
1299 .ssd_limit = 0xfffff,
1300 .ssd_type = SDT_MEMRWA,
1303 .ssd_xx = 0, .ssd_xx1 = 0,
1306 /* GUCODE_SEL 6 Code Descriptor for user */
1308 .ssd_limit = 0xfffff,
1309 .ssd_type = SDT_MEMERA,
1312 .ssd_xx = 0, .ssd_xx1 = 0,
1315 /* GUDATA_SEL 7 Data Descriptor for user */
1317 .ssd_limit = 0xfffff,
1318 .ssd_type = SDT_MEMRWA,
1321 .ssd_xx = 0, .ssd_xx1 = 0,
1324 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1325 { .ssd_base = 0x400,
1326 .ssd_limit = 0xfffff,
1327 .ssd_type = SDT_MEMRWA,
1330 .ssd_xx = 0, .ssd_xx1 = 0,
1333 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1336 .ssd_limit = sizeof(struct i386tss)-1,
1337 .ssd_type = SDT_SYS386TSS,
1340 .ssd_xx = 0, .ssd_xx1 = 0,
1343 /* GLDT_SEL 10 LDT Descriptor */
1344 { .ssd_base = (int) ldt,
1345 .ssd_limit = sizeof(ldt)-1,
1346 .ssd_type = SDT_SYSLDT,
1349 .ssd_xx = 0, .ssd_xx1 = 0,
1352 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1353 { .ssd_base = (int) ldt,
1354 .ssd_limit = (512 * sizeof(union descriptor)-1),
1355 .ssd_type = SDT_SYSLDT,
1358 .ssd_xx = 0, .ssd_xx1 = 0,
1361 /* GPANIC_SEL 12 Panic Tss Descriptor */
1362 { .ssd_base = (int) &dblfault_tss,
1363 .ssd_limit = sizeof(struct i386tss)-1,
1364 .ssd_type = SDT_SYS386TSS,
1367 .ssd_xx = 0, .ssd_xx1 = 0,
1370 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1372 .ssd_limit = 0xfffff,
1373 .ssd_type = SDT_MEMERA,
1376 .ssd_xx = 0, .ssd_xx1 = 0,
1379 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1381 .ssd_limit = 0xfffff,
1382 .ssd_type = SDT_MEMERA,
1385 .ssd_xx = 0, .ssd_xx1 = 0,
1388 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1390 .ssd_limit = 0xfffff,
1391 .ssd_type = SDT_MEMRWA,
1394 .ssd_xx = 0, .ssd_xx1 = 0,
1397 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1399 .ssd_limit = 0xfffff,
1400 .ssd_type = SDT_MEMRWA,
1403 .ssd_xx = 0, .ssd_xx1 = 0,
1406 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1408 .ssd_limit = 0xfffff,
1409 .ssd_type = SDT_MEMRWA,
1412 .ssd_xx = 0, .ssd_xx1 = 0,
1415 /* GNDIS_SEL 18 NDIS Descriptor */
1421 .ssd_xx = 0, .ssd_xx1 = 0,
1426 static struct soft_segment_descriptor ldt_segs[] = {
1427 /* Null Descriptor - overwritten by call gate */
1433 .ssd_xx = 0, .ssd_xx1 = 0,
1436 /* Null Descriptor - overwritten by call gate */
1442 .ssd_xx = 0, .ssd_xx1 = 0,
1445 /* Null Descriptor - overwritten by call gate */
1451 .ssd_xx = 0, .ssd_xx1 = 0,
1454 /* Code Descriptor for user */
1456 .ssd_limit = 0xfffff,
1457 .ssd_type = SDT_MEMERA,
1460 .ssd_xx = 0, .ssd_xx1 = 0,
1463 /* Null Descriptor - overwritten by call gate */
1469 .ssd_xx = 0, .ssd_xx1 = 0,
1472 /* Data Descriptor for user */
1474 .ssd_limit = 0xfffff,
1475 .ssd_type = SDT_MEMRWA,
1478 .ssd_xx = 0, .ssd_xx1 = 0,
1484 setidt(idx, func, typ, dpl, selec)
1491 struct gate_descriptor *ip;
1494 ip->gd_looffset = (int)func;
1495 ip->gd_selector = selec;
1501 ip->gd_hioffset = ((int)func)>>16 ;
1505 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1506 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1507 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1508 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1510 #ifdef KDTRACE_HOOKS
1514 IDTVEC(xen_intr_upcall),
1516 IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1520 * Display the index and function name of any IDT entries that don't use
1521 * the default 'rsvd' entry point.
1523 DB_SHOW_COMMAND(idt, db_show_idt)
1525 struct gate_descriptor *ip;
1530 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
1531 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1532 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1533 db_printf("%3d\t", idx);
1534 db_printsym(func, DB_STGY_PROC);
1541 /* Show privileged registers. */
1542 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
1544 uint64_t idtr, gdtr;
1547 db_printf("idtr\t0x%08x/%04x\n",
1548 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
1550 db_printf("gdtr\t0x%08x/%04x\n",
1551 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
1552 db_printf("ldtr\t0x%04x\n", rldt());
1553 db_printf("tr\t0x%04x\n", rtr());
1554 db_printf("cr0\t0x%08x\n", rcr0());
1555 db_printf("cr2\t0x%08x\n", rcr2());
1556 db_printf("cr3\t0x%08x\n", rcr3());
1557 db_printf("cr4\t0x%08x\n", rcr4());
1558 if (rcr4() & CR4_XSAVE)
1559 db_printf("xcr0\t0x%016llx\n", rxcr(0));
1560 if (amd_feature & (AMDID_NX | AMDID_LM))
1561 db_printf("EFER\t0x%016llx\n", rdmsr(MSR_EFER));
1562 if (cpu_feature2 & (CPUID2_VMX | CPUID2_SMX))
1563 db_printf("FEATURES_CTL\t0x%016llx\n",
1564 rdmsr(MSR_IA32_FEATURE_CONTROL));
1565 if ((cpu_vendor_id == CPU_VENDOR_INTEL ||
1566 cpu_vendor_id == CPU_VENDOR_AMD) && CPUID_TO_FAMILY(cpu_id) >= 6)
1567 db_printf("DEBUG_CTL\t0x%016llx\n", rdmsr(MSR_DEBUGCTLMSR));
1568 if (cpu_feature & CPUID_PAT)
1569 db_printf("PAT\t0x%016llx\n", rdmsr(MSR_PAT));
1572 DB_SHOW_COMMAND(dbregs, db_show_dbregs)
1575 db_printf("dr0\t0x%08x\n", rdr0());
1576 db_printf("dr1\t0x%08x\n", rdr1());
1577 db_printf("dr2\t0x%08x\n", rdr2());
1578 db_printf("dr3\t0x%08x\n", rdr3());
1579 db_printf("dr6\t0x%08x\n", rdr6());
1580 db_printf("dr7\t0x%08x\n", rdr7());
1586 struct segment_descriptor *sd;
1587 struct soft_segment_descriptor *ssd;
1589 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1590 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1591 ssd->ssd_type = sd->sd_type;
1592 ssd->ssd_dpl = sd->sd_dpl;
1593 ssd->ssd_p = sd->sd_p;
1594 ssd->ssd_def32 = sd->sd_def32;
1595 ssd->ssd_gran = sd->sd_gran;
1599 add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
1602 int i, insert_idx, physmap_idx;
1604 physmap_idx = *physmap_idxp;
1610 if (base > 0xffffffff) {
1611 printf("%uK of memory above 4GB ignored\n",
1612 (u_int)(length / 1024));
1618 * Find insertion point while checking for overlap. Start off by
1619 * assuming the new entry will be added to the end.
1621 insert_idx = physmap_idx + 2;
1622 for (i = 0; i <= physmap_idx; i += 2) {
1623 if (base < physmap[i + 1]) {
1624 if (base + length <= physmap[i]) {
1628 if (boothowto & RB_VERBOSE)
1630 "Overlapping memory regions, ignoring second region\n");
1635 /* See if we can prepend to the next entry. */
1636 if (insert_idx <= physmap_idx && base + length == physmap[insert_idx]) {
1637 physmap[insert_idx] = base;
1641 /* See if we can append to the previous entry. */
1642 if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
1643 physmap[insert_idx - 1] += length;
1648 *physmap_idxp = physmap_idx;
1649 if (physmap_idx == PHYSMAP_SIZE) {
1651 "Too many segments in the physical address map, giving up\n");
1656 * Move the last 'N' entries down to make room for the new
1659 for (i = physmap_idx; i > insert_idx; i -= 2) {
1660 physmap[i] = physmap[i - 2];
1661 physmap[i + 1] = physmap[i - 1];
1664 /* Insert the new entry. */
1665 physmap[insert_idx] = base;
1666 physmap[insert_idx + 1] = base + length;
1671 add_smap_entry(struct bios_smap *smap, vm_paddr_t *physmap, int *physmap_idxp)
1673 if (boothowto & RB_VERBOSE)
1674 printf("SMAP type=%02x base=%016llx len=%016llx\n",
1675 smap->type, smap->base, smap->length);
1677 if (smap->type != SMAP_TYPE_MEMORY)
1680 return (add_physmap_entry(smap->base, smap->length, physmap,
1685 add_smap_entries(struct bios_smap *smapbase, vm_paddr_t *physmap,
1688 struct bios_smap *smap, *smapend;
1691 * Memory map from INT 15:E820.
1693 * subr_module.c says:
1694 * "Consumer may safely assume that size value precedes data."
1695 * ie: an int32_t immediately precedes SMAP.
1697 smapsize = *((u_int32_t *)smapbase - 1);
1698 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
1700 for (smap = smapbase; smap < smapend; smap++)
1701 if (!add_smap_entry(smap, physmap, physmap_idxp))
1712 if (basemem > 640) {
1713 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1719 * XXX if biosbasemem is now < 640, there is a `hole'
1720 * between the end of base memory and the start of
1721 * ISA memory. The hole may be empty or it may
1722 * contain BIOS code or data. Map it read/write so
1723 * that the BIOS can write to it. (Memory from 0 to
1724 * the physical end of the kernel is mapped read-only
1725 * to begin with and then parts of it are remapped.
1726 * The parts that aren't remapped form holes that
1727 * remain read-only and are unused by the kernel.
1728 * The base memory area is below the physical end of
1729 * the kernel and right now forms a read-only hole.
1730 * The part of it from PAGE_SIZE to
1731 * (trunc_page(biosbasemem * 1024) - 1) will be
1732 * remapped and used by the kernel later.)
1734 * This code is similar to the code used in
1735 * pmap_mapdev, but since no memory needs to be
1736 * allocated we simply change the mapping.
1738 for (pa = trunc_page(basemem * 1024);
1739 pa < ISA_HOLE_START; pa += PAGE_SIZE)
1740 pmap_kenter(KERNBASE + pa, pa);
1743 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
1744 * the vm86 page table so that vm86 can scribble on them using
1745 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
1746 * page 0, at least as initialized here?
1748 pte = (pt_entry_t *)vm86paddr;
1749 for (i = basemem / 4; i < 160; i++)
1750 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1754 * Populate the (physmap) array with base/bound pairs describing the
1755 * available physical memory in the system, then test this memory and
1756 * build the phys_avail array describing the actually-available memory.
1758 * If we cannot accurately determine the physical memory map, then use
1759 * value from the 0xE801 call, and failing that, the RTC.
1761 * Total memory size may be set by the kernel environment variable
1762 * hw.physmem or the compile-time define MAXMEM.
1764 * XXX first should be vm_paddr_t.
1767 getmemsize(int first)
1769 int has_smap, off, physmap_idx, pa_indx, da_indx;
1771 vm_paddr_t physmap[PHYSMAP_SIZE];
1773 quad_t dcons_addr, dcons_size, physmem_tunable;
1774 int hasbrokenint12, i, res;
1776 struct vm86frame vmf;
1777 struct vm86context vmc;
1779 struct bios_smap *smap, *smapbase;
1784 if (arch_i386_is_xbox) {
1786 * We queried the memory size before, so chop off 4MB for
1787 * the framebuffer and inform the OS of this.
1790 physmap[1] = (arch_i386_xbox_memsize * 1024 * 1024) - XBOX_FB_SIZE;
1795 bzero(&vmf, sizeof(vmf));
1796 bzero(physmap, sizeof(physmap));
1800 * Check if the loader supplied an SMAP memory map. If so,
1801 * use that and do not make any VM86 calls.
1804 kmdp = preload_search_by_type("elf kernel");
1806 kmdp = preload_search_by_type("elf32 kernel");
1807 smapbase = (struct bios_smap *)preload_search_info(kmdp,
1808 MODINFO_METADATA | MODINFOMD_SMAP);
1809 if (smapbase != NULL) {
1810 add_smap_entries(smapbase, physmap, &physmap_idx);
1816 * Some newer BIOSes have a broken INT 12H implementation
1817 * which causes a kernel panic immediately. In this case, we
1818 * need use the SMAP to determine the base memory size.
1821 TUNABLE_INT_FETCH("hw.hasbrokenint12", &hasbrokenint12);
1822 if (hasbrokenint12 == 0) {
1823 /* Use INT12 to determine base memory size. */
1824 vm86_intcall(0x12, &vmf);
1825 basemem = vmf.vmf_ax;
1830 * Fetch the memory map with INT 15:E820. Map page 1 R/W into
1831 * the kernel page table so we can use it as a buffer. The
1832 * kernel will unmap this page later.
1834 pmap_kenter(KERNBASE + (1 << PAGE_SHIFT), 1 << PAGE_SHIFT);
1836 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1837 res = vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1838 KASSERT(res != 0, ("vm86_getptr() failed: address not found"));
1842 vmf.vmf_eax = 0xE820;
1843 vmf.vmf_edx = SMAP_SIG;
1844 vmf.vmf_ecx = sizeof(struct bios_smap);
1845 i = vm86_datacall(0x15, &vmf, &vmc);
1846 if (i || vmf.vmf_eax != SMAP_SIG)
1849 if (!add_smap_entry(smap, physmap, &physmap_idx))
1851 } while (vmf.vmf_ebx != 0);
1855 * If we didn't fetch the "base memory" size from INT12,
1856 * figure it out from the SMAP (or just guess).
1859 for (i = 0; i <= physmap_idx; i += 2) {
1860 if (physmap[i] == 0x00000000) {
1861 basemem = physmap[i + 1] / 1024;
1866 /* XXX: If we couldn't find basemem from SMAP, just guess. */
1872 if (physmap[1] != 0)
1876 * If we failed to find an SMAP, figure out the extended
1877 * memory size. We will then build a simple memory map with
1878 * two segments, one for "base memory" and the second for
1879 * "extended memory". Note that "extended memory" starts at a
1880 * physical address of 1MB and that both basemem and extmem
1881 * are in units of 1KB.
1883 * First, try to fetch the extended memory size via INT 15:E801.
1885 vmf.vmf_ax = 0xE801;
1886 if (vm86_intcall(0x15, &vmf) == 0) {
1887 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1890 * If INT15:E801 fails, this is our last ditch effort
1891 * to determine the extended memory size. Currently
1892 * we prefer the RTC value over INT15:88.
1896 vm86_intcall(0x15, &vmf);
1897 extmem = vmf.vmf_ax;
1899 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1904 * Special hack for chipsets that still remap the 384k hole when
1905 * there's 16MB of memory - this really confuses people that
1906 * are trying to use bus mastering ISA controllers with the
1907 * "16MB limit"; they only have 16MB, but the remapping puts
1908 * them beyond the limit.
1910 * If extended memory is between 15-16MB (16-17MB phys address range),
1913 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1917 physmap[1] = basemem * 1024;
1919 physmap[physmap_idx] = 0x100000;
1920 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1924 * Now, physmap contains a map of physical memory.
1928 /* make hole for AP bootstrap code */
1929 physmap[1] = mp_bootaddress(physmap[1]);
1933 * Maxmem isn't the "maximum memory", it's one larger than the
1934 * highest page of the physical address space. It should be
1935 * called something like "Maxphyspage". We may adjust this
1936 * based on ``hw.physmem'' and the results of the memory test.
1938 * This is especially confusing when it is much larger than the
1939 * memory size and is displayed as "realmem".
1941 Maxmem = atop(physmap[physmap_idx + 1]);
1944 Maxmem = MAXMEM / 4;
1947 if (TUNABLE_QUAD_FETCH("hw.physmem", &physmem_tunable))
1948 Maxmem = atop(physmem_tunable);
1951 * If we have an SMAP, don't allow MAXMEM or hw.physmem to extend
1952 * the amount of memory in the system.
1954 if (has_smap && Maxmem > atop(physmap[physmap_idx + 1]))
1955 Maxmem = atop(physmap[physmap_idx + 1]);
1958 * By default enable the memory test on real hardware, and disable
1959 * it if we appear to be running in a VM. This avoids touching all
1960 * pages unnecessarily, which doesn't matter on real hardware but is
1961 * bad for shared VM hosts. Use a general name so that
1962 * one could eventually do more with the code than just disable it.
1964 memtest = (vm_guest > VM_GUEST_NO) ? 0 : 1;
1965 TUNABLE_ULONG_FETCH("hw.memtest.tests", &memtest);
1967 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1968 (boothowto & RB_VERBOSE))
1969 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1972 * If Maxmem has been increased beyond what the system has detected,
1973 * extend the last memory segment to the new limit.
1975 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1976 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1978 /* call pmap initialization to make new kernel address space */
1979 pmap_bootstrap(first);
1982 * Size up each available chunk of physical memory.
1984 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1987 phys_avail[pa_indx++] = physmap[0];
1988 phys_avail[pa_indx] = physmap[0];
1989 dump_avail[da_indx] = physmap[0];
1993 * Get dcons buffer address
1995 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
1996 getenv_quad("dcons.size", &dcons_size) == 0)
2000 * physmap is in bytes, so when converting to page boundaries,
2001 * round up the start address and round down the end address.
2003 for (i = 0; i <= physmap_idx; i += 2) {
2006 end = ptoa((vm_paddr_t)Maxmem);
2007 if (physmap[i + 1] < end)
2008 end = trunc_page(physmap[i + 1]);
2009 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
2010 int tmp, page_bad, full;
2011 int *ptr = (int *)CADDR3;
2015 * block out kernel memory as not available.
2017 if (pa >= KERNLOAD && pa < first)
2021 * block out dcons buffer
2024 && pa >= trunc_page(dcons_addr)
2025 && pa < dcons_addr + dcons_size)
2033 * map page into kernel: valid, read/write,non-cacheable
2035 *pte = pa | PG_V | PG_RW | PG_N;
2040 * Test for alternating 1's and 0's
2042 *(volatile int *)ptr = 0xaaaaaaaa;
2043 if (*(volatile int *)ptr != 0xaaaaaaaa)
2046 * Test for alternating 0's and 1's
2048 *(volatile int *)ptr = 0x55555555;
2049 if (*(volatile int *)ptr != 0x55555555)
2054 *(volatile int *)ptr = 0xffffffff;
2055 if (*(volatile int *)ptr != 0xffffffff)
2060 *(volatile int *)ptr = 0x0;
2061 if (*(volatile int *)ptr != 0x0)
2064 * Restore original value.
2070 * Adjust array of valid/good pages.
2072 if (page_bad == TRUE)
2075 * If this good page is a continuation of the
2076 * previous set of good pages, then just increase
2077 * the end pointer. Otherwise start a new chunk.
2078 * Note that "end" points one higher than end,
2079 * making the range >= start and < end.
2080 * If we're also doing a speculative memory
2081 * test and we at or past the end, bump up Maxmem
2082 * so that we keep going. The first bad page
2083 * will terminate the loop.
2085 if (phys_avail[pa_indx] == pa) {
2086 phys_avail[pa_indx] += PAGE_SIZE;
2089 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
2091 "Too many holes in the physical address space, giving up\n");
2096 phys_avail[pa_indx++] = pa; /* start */
2097 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
2101 if (dump_avail[da_indx] == pa) {
2102 dump_avail[da_indx] += PAGE_SIZE;
2105 if (da_indx == DUMP_AVAIL_ARRAY_END) {
2109 dump_avail[da_indx++] = pa; /* start */
2110 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
2122 * The last chunk must contain at least one page plus the message
2123 * buffer to avoid complicating other code (message buffer address
2124 * calculation, etc.).
2126 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
2127 round_page(msgbufsize) >= phys_avail[pa_indx]) {
2128 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
2129 phys_avail[pa_indx--] = 0;
2130 phys_avail[pa_indx--] = 0;
2133 Maxmem = atop(phys_avail[pa_indx]);
2135 /* Trim off space for the message buffer. */
2136 phys_avail[pa_indx] -= round_page(msgbufsize);
2138 /* Map the message buffer. */
2139 for (off = 0; off < round_page(msgbufsize); off += PAGE_SIZE)
2140 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
2148 db_fetch_ksymtab(bootinfo.bi_symtab, bootinfo.bi_esymtab);
2152 if (boothowto & RB_KDB)
2153 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
2160 struct gate_descriptor *gdp;
2161 int gsel_tss, metadata_missing, x, pa;
2163 struct xstate_hdr *xhdr;
2166 thread0.td_kstack = proc0kstack;
2167 thread0.td_kstack_pages = TD0_KSTACK_PAGES;
2170 * This may be done better later if it gets more high level
2171 * components in it. If so just link td->td_proc here.
2173 proc_linkup0(&proc0, &thread0);
2175 metadata_missing = 0;
2176 if (bootinfo.bi_modulep) {
2177 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
2178 preload_bootstrap_relocate(KERNBASE);
2180 metadata_missing = 1;
2183 if (bootinfo.bi_envp != 0)
2184 init_static_kenv((char *)bootinfo.bi_envp + KERNBASE, 0);
2186 init_static_kenv(NULL, 0);
2188 /* Init basic tunables, hz etc */
2192 * Make gdt memory segments. All segments cover the full 4GB
2193 * of address space and permissions are enforced at page level.
2195 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
2196 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
2197 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
2198 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
2199 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
2200 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
2203 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
2204 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2205 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2207 for (x = 0; x < NGDT; x++)
2208 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2210 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2211 r_gdt.rd_base = (int) gdt;
2212 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2215 pcpu_init(pc, 0, sizeof(struct pcpu));
2216 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2217 pmap_kenter(pa + KERNBASE, pa);
2218 dpcpu_init((void *)(first + KERNBASE), 0);
2219 first += DPCPU_SIZE;
2220 PCPU_SET(prvspace, pc);
2221 PCPU_SET(curthread, &thread0);
2222 /* Non-late cninit() and printf() can be moved up to here. */
2225 * Initialize mutexes.
2227 * icu_lock: in order to allow an interrupt to occur in a critical
2228 * section, to set pcpu->ipending (etc...) properly, we
2229 * must be able to get the icu lock, so it can't be
2233 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2235 /* make ldt memory segments */
2236 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2237 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2238 for (x = 0; x < nitems(ldt_segs); x++)
2239 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2241 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2243 PCPU_SET(currentldt, _default_ldt);
2246 for (x = 0; x < NIDT; x++)
2247 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2248 GSEL(GCODE_SEL, SEL_KPL));
2249 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2250 GSEL(GCODE_SEL, SEL_KPL));
2251 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2252 GSEL(GCODE_SEL, SEL_KPL));
2253 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
2254 GSEL(GCODE_SEL, SEL_KPL));
2255 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2256 GSEL(GCODE_SEL, SEL_KPL));
2257 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2258 GSEL(GCODE_SEL, SEL_KPL));
2259 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2260 GSEL(GCODE_SEL, SEL_KPL));
2261 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2262 GSEL(GCODE_SEL, SEL_KPL));
2263 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2264 , GSEL(GCODE_SEL, SEL_KPL));
2265 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2266 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2267 GSEL(GCODE_SEL, SEL_KPL));
2268 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2269 GSEL(GCODE_SEL, SEL_KPL));
2270 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2271 GSEL(GCODE_SEL, SEL_KPL));
2272 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2273 GSEL(GCODE_SEL, SEL_KPL));
2274 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2275 GSEL(GCODE_SEL, SEL_KPL));
2276 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2277 GSEL(GCODE_SEL, SEL_KPL));
2278 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2279 GSEL(GCODE_SEL, SEL_KPL));
2280 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2281 GSEL(GCODE_SEL, SEL_KPL));
2282 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2283 GSEL(GCODE_SEL, SEL_KPL));
2284 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2285 GSEL(GCODE_SEL, SEL_KPL));
2286 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2287 GSEL(GCODE_SEL, SEL_KPL));
2288 #ifdef KDTRACE_HOOKS
2289 setidt(IDT_DTRACE_RET, &IDTVEC(dtrace_ret), SDT_SYS386TGT, SEL_UPL,
2290 GSEL(GCODE_SEL, SEL_KPL));
2293 setidt(IDT_EVTCHN, &IDTVEC(xen_intr_upcall), SDT_SYS386IGT, SEL_UPL,
2294 GSEL(GCODE_SEL, SEL_KPL));
2297 r_idt.rd_limit = sizeof(idt0) - 1;
2298 r_idt.rd_base = (int) idt;
2303 * The following code queries the PCI ID of 0:0:0. For the XBOX,
2304 * This should be 0x10de / 0x02a5.
2306 * This is exactly what Linux does.
2308 outl(0xcf8, 0x80000000);
2309 if (inl(0xcfc) == 0x02a510de) {
2310 arch_i386_is_xbox = 1;
2311 pic16l_setled(XBOX_LED_GREEN);
2314 * We are an XBOX, but we may have either 64MB or 128MB of
2315 * memory. The PCI host bridge should be programmed for this,
2316 * so we just query it.
2318 outl(0xcf8, 0x80000084);
2319 arch_i386_xbox_memsize = (inl(0xcfc) == 0x7FFFFFF) ? 128 : 64;
2324 * Initialize the clock before the console so that console
2325 * initialization can use DELAY().
2329 finishidentcpu(); /* Final stage of CPU initialization */
2330 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2331 GSEL(GCODE_SEL, SEL_KPL));
2332 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2333 GSEL(GCODE_SEL, SEL_KPL));
2334 initializecpu(); /* Initialize CPU registers */
2335 initializecpucache();
2337 /* pointer to selector slot for %fs/%gs */
2338 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2340 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2341 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2342 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2343 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2344 #if defined(PAE) || defined(PAE_TABLES)
2345 dblfault_tss.tss_cr3 = (int)IdlePDPT;
2347 dblfault_tss.tss_cr3 = (int)IdlePTD;
2349 dblfault_tss.tss_eip = (int)dblfault_handler;
2350 dblfault_tss.tss_eflags = PSL_KERNEL;
2351 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2352 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2353 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2354 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2355 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2357 /* Initialize the tss (except for the final esp0) early for vm86. */
2358 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2359 thread0.td_kstack_pages * PAGE_SIZE - 16);
2360 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2361 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2362 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2363 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2364 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2367 /* Initialize the PIC early for vm86 calls. */
2373 /* Reset and mask the atpics and leave them shut down. */
2377 * Point the ICU spurious interrupt vectors at the APIC spurious
2378 * interrupt handler.
2380 setidt(IDT_IO_INTS + 7, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
2381 GSEL(GCODE_SEL, SEL_KPL));
2382 setidt(IDT_IO_INTS + 15, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
2383 GSEL(GCODE_SEL, SEL_KPL));
2388 * The console and kdb should be initialized even earlier than here,
2389 * but some console drivers don't work until after getmemsize().
2390 * Default to late console initialization to support these drivers.
2391 * This loses mainly printf()s in getmemsize() and early debugging.
2394 TUNABLE_INT_FETCH("debug.late_console", &late_console);
2395 if (!late_console) {
2402 init_param2(physmem);
2404 /* now running on new page tables, configured,and u/iom is accessible */
2409 if (metadata_missing)
2410 printf("WARNING: loader(8) metadata is missing!\n");
2415 msgbufinit(msgbufp, msgbufsize);
2418 * Set up thread0 pcb after npxinit calculated pcb + fpu save
2419 * area size. Zero out the extended state header in fpu save
2422 thread0.td_pcb = get_pcb_td(&thread0);
2423 thread0.td_pcb->pcb_save = get_pcb_user_save_td(&thread0);
2424 bzero(get_pcb_user_save_td(&thread0), cpu_max_ext_state_size);
2426 xhdr = (struct xstate_hdr *)(get_pcb_user_save_td(&thread0) +
2428 xhdr->xstate_bv = xsave_mask;
2430 PCPU_SET(curpcb, thread0.td_pcb);
2431 /* Move esp0 in the tss to its final place. */
2432 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2433 PCPU_SET(common_tss.tss_esp0, (vm_offset_t)thread0.td_pcb - 16);
2434 gdt[GPROC0_SEL].sd.sd_type = SDT_SYS386TSS; /* clear busy bit */
2437 /* make a call gate to reenter kernel with */
2438 gdp = &ldt[LSYS5CALLS_SEL].gd;
2440 x = (int) &IDTVEC(lcall_syscall);
2441 gdp->gd_looffset = x;
2442 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2444 gdp->gd_type = SDT_SYS386CGT;
2445 gdp->gd_dpl = SEL_UPL;
2447 gdp->gd_hioffset = x >> 16;
2449 /* XXX does this work? */
2451 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2453 /* transfer to user mode */
2455 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2456 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2458 /* setup proc 0's pcb */
2459 thread0.td_pcb->pcb_flags = 0;
2460 #if defined(PAE) || defined(PAE_TABLES)
2461 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
2463 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2465 thread0.td_pcb->pcb_ext = 0;
2466 thread0.td_frame = &proc0_tf;
2474 /* Location of kernel stack for locore */
2475 return ((register_t)thread0.td_pcb);
2479 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2482 pcpu->pc_acpi_id = 0xffffffff;
2486 smap_sysctl_handler(SYSCTL_HANDLER_ARGS)
2488 struct bios_smap *smapbase;
2489 struct bios_smap_xattr smap;
2492 int count, error, i;
2494 /* Retrieve the system memory map from the loader. */
2495 kmdp = preload_search_by_type("elf kernel");
2497 kmdp = preload_search_by_type("elf32 kernel");
2498 smapbase = (struct bios_smap *)preload_search_info(kmdp,
2499 MODINFO_METADATA | MODINFOMD_SMAP);
2500 if (smapbase == NULL)
2502 smapattr = (uint32_t *)preload_search_info(kmdp,
2503 MODINFO_METADATA | MODINFOMD_SMAP_XATTR);
2504 count = *((u_int32_t *)smapbase - 1) / sizeof(*smapbase);
2506 for (i = 0; i < count; i++) {
2507 smap.base = smapbase[i].base;
2508 smap.length = smapbase[i].length;
2509 smap.type = smapbase[i].type;
2510 if (smapattr != NULL)
2511 smap.xattr = smapattr[i];
2514 error = SYSCTL_OUT(req, &smap, sizeof(smap));
2518 SYSCTL_PROC(_machdep, OID_AUTO, smap, CTLTYPE_OPAQUE|CTLFLAG_RD, NULL, 0,
2519 smap_sysctl_handler, "S,bios_smap_xattr", "Raw BIOS SMAP data");
2522 spinlock_enter(void)
2528 if (td->td_md.md_spinlock_count == 0) {
2529 flags = intr_disable();
2530 td->td_md.md_spinlock_count = 1;
2531 td->td_md.md_saved_flags = flags;
2533 td->td_md.md_spinlock_count++;
2545 flags = td->td_md.md_saved_flags;
2546 td->td_md.md_spinlock_count--;
2547 if (td->td_md.md_spinlock_count == 0)
2548 intr_restore(flags);
2551 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2552 static void f00f_hack(void *unused);
2553 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
2556 f00f_hack(void *unused)
2558 struct gate_descriptor *new_idt;
2566 printf("Intel Pentium detected, installing workaround for F00F bug\n");
2568 tmp = kmem_malloc(kernel_arena, PAGE_SIZE * 2, M_WAITOK | M_ZERO);
2570 panic("kmem_malloc returned 0");
2572 /* Put the problematic entry (#6) at the end of the lower page. */
2573 new_idt = (struct gate_descriptor*)
2574 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
2575 bcopy(idt, new_idt, sizeof(idt0));
2576 r_idt.rd_base = (u_int)new_idt;
2579 pmap_protect(kernel_pmap, tmp, tmp + PAGE_SIZE, VM_PROT_READ);
2581 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2584 * Construct a PCB from a trapframe. This is called from kdb_trap() where
2585 * we want to start a backtrace from the function that caused us to enter
2586 * the debugger. We have the context in the trapframe, but base the trace
2587 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
2588 * enough for a backtrace.
2591 makectx(struct trapframe *tf, struct pcb *pcb)
2594 pcb->pcb_edi = tf->tf_edi;
2595 pcb->pcb_esi = tf->tf_esi;
2596 pcb->pcb_ebp = tf->tf_ebp;
2597 pcb->pcb_ebx = tf->tf_ebx;
2598 pcb->pcb_eip = tf->tf_eip;
2599 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
2600 pcb->pcb_gs = rgs();
2604 ptrace_set_pc(struct thread *td, u_long addr)
2607 td->td_frame->tf_eip = addr;
2612 ptrace_single_step(struct thread *td)
2614 td->td_frame->tf_eflags |= PSL_T;
2619 ptrace_clear_single_step(struct thread *td)
2621 td->td_frame->tf_eflags &= ~PSL_T;
2626 fill_regs(struct thread *td, struct reg *regs)
2629 struct trapframe *tp;
2633 regs->r_gs = pcb->pcb_gs;
2634 return (fill_frame_regs(tp, regs));
2638 fill_frame_regs(struct trapframe *tp, struct reg *regs)
2640 regs->r_fs = tp->tf_fs;
2641 regs->r_es = tp->tf_es;
2642 regs->r_ds = tp->tf_ds;
2643 regs->r_edi = tp->tf_edi;
2644 regs->r_esi = tp->tf_esi;
2645 regs->r_ebp = tp->tf_ebp;
2646 regs->r_ebx = tp->tf_ebx;
2647 regs->r_edx = tp->tf_edx;
2648 regs->r_ecx = tp->tf_ecx;
2649 regs->r_eax = tp->tf_eax;
2650 regs->r_eip = tp->tf_eip;
2651 regs->r_cs = tp->tf_cs;
2652 regs->r_eflags = tp->tf_eflags;
2653 regs->r_esp = tp->tf_esp;
2654 regs->r_ss = tp->tf_ss;
2659 set_regs(struct thread *td, struct reg *regs)
2662 struct trapframe *tp;
2665 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2666 !CS_SECURE(regs->r_cs))
2669 tp->tf_fs = regs->r_fs;
2670 tp->tf_es = regs->r_es;
2671 tp->tf_ds = regs->r_ds;
2672 tp->tf_edi = regs->r_edi;
2673 tp->tf_esi = regs->r_esi;
2674 tp->tf_ebp = regs->r_ebp;
2675 tp->tf_ebx = regs->r_ebx;
2676 tp->tf_edx = regs->r_edx;
2677 tp->tf_ecx = regs->r_ecx;
2678 tp->tf_eax = regs->r_eax;
2679 tp->tf_eip = regs->r_eip;
2680 tp->tf_cs = regs->r_cs;
2681 tp->tf_eflags = regs->r_eflags;
2682 tp->tf_esp = regs->r_esp;
2683 tp->tf_ss = regs->r_ss;
2684 pcb->pcb_gs = regs->r_gs;
2689 fill_fpregs(struct thread *td, struct fpreg *fpregs)
2692 KASSERT(td == curthread || TD_IS_SUSPENDED(td) ||
2693 P_SHOULDSTOP(td->td_proc),
2694 ("not suspended thread %p", td));
2697 npx_fill_fpregs_xmm(&get_pcb_user_save_td(td)->sv_xmm,
2698 (struct save87 *)fpregs);
2700 bcopy(&get_pcb_user_save_td(td)->sv_87, fpregs,
2706 set_fpregs(struct thread *td, struct fpreg *fpregs)
2710 npx_set_fpregs_xmm((struct save87 *)fpregs,
2711 &get_pcb_user_save_td(td)->sv_xmm);
2713 bcopy(fpregs, &get_pcb_user_save_td(td)->sv_87,
2720 * Get machine context.
2723 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
2725 struct trapframe *tp;
2726 struct segment_descriptor *sdp;
2730 PROC_LOCK(curthread->td_proc);
2731 mcp->mc_onstack = sigonstack(tp->tf_esp);
2732 PROC_UNLOCK(curthread->td_proc);
2733 mcp->mc_gs = td->td_pcb->pcb_gs;
2734 mcp->mc_fs = tp->tf_fs;
2735 mcp->mc_es = tp->tf_es;
2736 mcp->mc_ds = tp->tf_ds;
2737 mcp->mc_edi = tp->tf_edi;
2738 mcp->mc_esi = tp->tf_esi;
2739 mcp->mc_ebp = tp->tf_ebp;
2740 mcp->mc_isp = tp->tf_isp;
2741 mcp->mc_eflags = tp->tf_eflags;
2742 if (flags & GET_MC_CLEAR_RET) {
2745 mcp->mc_eflags &= ~PSL_C;
2747 mcp->mc_eax = tp->tf_eax;
2748 mcp->mc_edx = tp->tf_edx;
2750 mcp->mc_ebx = tp->tf_ebx;
2751 mcp->mc_ecx = tp->tf_ecx;
2752 mcp->mc_eip = tp->tf_eip;
2753 mcp->mc_cs = tp->tf_cs;
2754 mcp->mc_esp = tp->tf_esp;
2755 mcp->mc_ss = tp->tf_ss;
2756 mcp->mc_len = sizeof(*mcp);
2757 get_fpcontext(td, mcp, NULL, 0);
2758 sdp = &td->td_pcb->pcb_fsd;
2759 mcp->mc_fsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2760 sdp = &td->td_pcb->pcb_gsd;
2761 mcp->mc_gsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2763 mcp->mc_xfpustate = 0;
2764 mcp->mc_xfpustate_len = 0;
2765 bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2));
2770 * Set machine context.
2772 * However, we don't set any but the user modifiable flags, and we won't
2773 * touch the cs selector.
2776 set_mcontext(struct thread *td, mcontext_t *mcp)
2778 struct trapframe *tp;
2783 if (mcp->mc_len != sizeof(*mcp) ||
2784 (mcp->mc_flags & ~_MC_FLAG_MASK) != 0)
2786 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
2787 (tp->tf_eflags & ~PSL_USERCHANGE);
2788 if (mcp->mc_flags & _MC_HASFPXSTATE) {
2789 if (mcp->mc_xfpustate_len > cpu_max_ext_state_size -
2790 sizeof(union savefpu))
2792 xfpustate = __builtin_alloca(mcp->mc_xfpustate_len);
2793 ret = copyin((void *)mcp->mc_xfpustate, xfpustate,
2794 mcp->mc_xfpustate_len);
2799 ret = set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
2802 tp->tf_fs = mcp->mc_fs;
2803 tp->tf_es = mcp->mc_es;
2804 tp->tf_ds = mcp->mc_ds;
2805 tp->tf_edi = mcp->mc_edi;
2806 tp->tf_esi = mcp->mc_esi;
2807 tp->tf_ebp = mcp->mc_ebp;
2808 tp->tf_ebx = mcp->mc_ebx;
2809 tp->tf_edx = mcp->mc_edx;
2810 tp->tf_ecx = mcp->mc_ecx;
2811 tp->tf_eax = mcp->mc_eax;
2812 tp->tf_eip = mcp->mc_eip;
2813 tp->tf_eflags = eflags;
2814 tp->tf_esp = mcp->mc_esp;
2815 tp->tf_ss = mcp->mc_ss;
2816 td->td_pcb->pcb_gs = mcp->mc_gs;
2821 get_fpcontext(struct thread *td, mcontext_t *mcp, char *xfpusave,
2822 size_t xfpusave_len)
2824 size_t max_len, len;
2826 mcp->mc_ownedfp = npxgetregs(td);
2827 bcopy(get_pcb_user_save_td(td), &mcp->mc_fpstate[0],
2828 sizeof(mcp->mc_fpstate));
2829 mcp->mc_fpformat = npxformat();
2830 if (!use_xsave || xfpusave_len == 0)
2832 max_len = cpu_max_ext_state_size - sizeof(union savefpu);
2834 if (len > max_len) {
2836 bzero(xfpusave + max_len, len - max_len);
2838 mcp->mc_flags |= _MC_HASFPXSTATE;
2839 mcp->mc_xfpustate_len = len;
2840 bcopy(get_pcb_user_save_td(td) + 1, xfpusave, len);
2844 set_fpcontext(struct thread *td, mcontext_t *mcp, char *xfpustate,
2845 size_t xfpustate_len)
2847 union savefpu *fpstate;
2850 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2852 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2853 mcp->mc_fpformat != _MC_FPFMT_XMM)
2855 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) {
2856 /* We don't care what state is left in the FPU or PCB. */
2859 } else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2860 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2861 fpstate = (union savefpu *)&mcp->mc_fpstate;
2863 fpstate->sv_xmm.sv_env.en_mxcsr &= cpu_mxcsr_mask;
2864 error = npxsetregs(td, fpstate, xfpustate, xfpustate_len);
2871 fpstate_drop(struct thread *td)
2874 KASSERT(PCB_USER_FPU(td->td_pcb), ("fpstate_drop: kernel-owned fpu"));
2876 if (PCPU_GET(fpcurthread) == td)
2879 * XXX force a full drop of the npx. The above only drops it if we
2880 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
2882 * XXX I don't much like npxgetregs()'s semantics of doing a full
2883 * drop. Dropping only to the pcb matches fnsave's behaviour.
2884 * We only need to drop to !PCB_INITDONE in sendsig(). But
2885 * sendsig() is the only caller of npxgetregs()... perhaps we just
2886 * have too many layers.
2888 curthread->td_pcb->pcb_flags &= ~(PCB_NPXINITDONE |
2889 PCB_NPXUSERINITDONE);
2894 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2899 dbregs->dr[0] = rdr0();
2900 dbregs->dr[1] = rdr1();
2901 dbregs->dr[2] = rdr2();
2902 dbregs->dr[3] = rdr3();
2903 dbregs->dr[6] = rdr6();
2904 dbregs->dr[7] = rdr7();
2907 dbregs->dr[0] = pcb->pcb_dr0;
2908 dbregs->dr[1] = pcb->pcb_dr1;
2909 dbregs->dr[2] = pcb->pcb_dr2;
2910 dbregs->dr[3] = pcb->pcb_dr3;
2911 dbregs->dr[6] = pcb->pcb_dr6;
2912 dbregs->dr[7] = pcb->pcb_dr7;
2920 set_dbregs(struct thread *td, struct dbreg *dbregs)
2926 load_dr0(dbregs->dr[0]);
2927 load_dr1(dbregs->dr[1]);
2928 load_dr2(dbregs->dr[2]);
2929 load_dr3(dbregs->dr[3]);
2930 load_dr6(dbregs->dr[6]);
2931 load_dr7(dbregs->dr[7]);
2934 * Don't let an illegal value for dr7 get set. Specifically,
2935 * check for undefined settings. Setting these bit patterns
2936 * result in undefined behaviour and can lead to an unexpected
2939 for (i = 0; i < 4; i++) {
2940 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
2942 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
2949 * Don't let a process set a breakpoint that is not within the
2950 * process's address space. If a process could do this, it
2951 * could halt the system by setting a breakpoint in the kernel
2952 * (if ddb was enabled). Thus, we need to check to make sure
2953 * that no breakpoints are being enabled for addresses outside
2954 * process's address space.
2956 * XXX - what about when the watched area of the user's
2957 * address space is written into from within the kernel
2958 * ... wouldn't that still cause a breakpoint to be generated
2959 * from within kernel mode?
2962 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
2963 /* dr0 is enabled */
2964 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2968 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
2969 /* dr1 is enabled */
2970 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2974 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
2975 /* dr2 is enabled */
2976 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2980 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
2981 /* dr3 is enabled */
2982 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2986 pcb->pcb_dr0 = dbregs->dr[0];
2987 pcb->pcb_dr1 = dbregs->dr[1];
2988 pcb->pcb_dr2 = dbregs->dr[2];
2989 pcb->pcb_dr3 = dbregs->dr[3];
2990 pcb->pcb_dr6 = dbregs->dr[6];
2991 pcb->pcb_dr7 = dbregs->dr[7];
2993 pcb->pcb_flags |= PCB_DBREGS;
3000 * Return > 0 if a hardware breakpoint has been hit, and the
3001 * breakpoint was in user space. Return 0, otherwise.
3004 user_dbreg_trap(void)
3006 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
3007 u_int32_t bp; /* breakpoint bits extracted from dr6 */
3008 int nbp; /* number of breakpoints that triggered */
3009 caddr_t addr[4]; /* breakpoint addresses */
3013 if ((dr7 & 0x000000ff) == 0) {
3015 * all GE and LE bits in the dr7 register are zero,
3016 * thus the trap couldn't have been caused by the
3017 * hardware debug registers
3024 bp = dr6 & 0x0000000f;
3028 * None of the breakpoint bits are set meaning this
3029 * trap was not caused by any of the debug registers
3035 * at least one of the breakpoints were hit, check to see
3036 * which ones and if any of them are user space addresses
3040 addr[nbp++] = (caddr_t)rdr0();
3043 addr[nbp++] = (caddr_t)rdr1();
3046 addr[nbp++] = (caddr_t)rdr2();
3049 addr[nbp++] = (caddr_t)rdr3();
3052 for (i = 0; i < nbp; i++) {
3053 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
3055 * addr[i] is in user space
3062 * None of the breakpoints are in user space.
3070 * Provide inb() and outb() as functions. They are normally only available as
3071 * inline functions, thus cannot be called from the debugger.
3074 /* silence compiler warnings */
3075 u_char inb_(u_short);
3076 void outb_(u_short, u_char);
3085 outb_(u_short port, u_char data)