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_atalk.h"
45 #include "opt_atpic.h"
46 #include "opt_compat.h"
52 #include "opt_kstack_pages.h"
53 #include "opt_maxmem.h"
54 #include "opt_mp_watchdog.h"
56 #include "opt_perfmon.h"
57 #include "opt_platform.h"
59 #include "opt_kdtrace.h"
61 #include <sys/param.h>
63 #include <sys/systm.h>
67 #include <sys/callout.h>
70 #include <sys/eventhandler.h>
72 #include <sys/imgact.h>
74 #include <sys/kernel.h>
76 #include <sys/linker.h>
78 #include <sys/malloc.h>
79 #include <sys/memrange.h>
80 #include <sys/msgbuf.h>
81 #include <sys/mutex.h>
83 #include <sys/ptrace.h>
84 #include <sys/reboot.h>
85 #include <sys/rwlock.h>
86 #include <sys/sched.h>
87 #include <sys/signalvar.h>
91 #include <sys/syscallsubr.h>
92 #include <sys/sysctl.h>
93 #include <sys/sysent.h>
94 #include <sys/sysproto.h>
95 #include <sys/ucontext.h>
96 #include <sys/vmmeter.h>
99 #include <vm/vm_extern.h>
100 #include <vm/vm_kern.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_pager.h>
105 #include <vm/vm_param.h>
109 #error KDB must be enabled in order for DDB to work!
112 #include <ddb/db_sym.h>
117 #include <net/netisr.h>
119 #include <machine/bootinfo.h>
120 #include <machine/clock.h>
121 #include <machine/cpu.h>
122 #include <machine/cputypes.h>
123 #include <machine/intr_machdep.h>
125 #include <machine/md_var.h>
126 #include <machine/metadata.h>
127 #include <machine/mp_watchdog.h>
128 #include <machine/pc/bios.h>
129 #include <machine/pcb.h>
130 #include <machine/pcb_ext.h>
131 #include <machine/proc.h>
132 #include <machine/reg.h>
133 #include <machine/sigframe.h>
134 #include <machine/specialreg.h>
135 #include <machine/vm86.h>
137 #include <machine/perfmon.h>
140 #include <machine/smp.h>
147 #include <machine/apicvar.h>
151 #include <x86/isa/icu.h>
155 #include <machine/xbox.h>
157 int arch_i386_is_xbox = 0;
158 uint32_t arch_i386_xbox_memsize = 0;
163 #include <xen/xen-os.h>
164 #include <xen/hypervisor.h>
165 #include <machine/xen/xenvar.h>
166 #include <machine/xen/xenfunc.h>
167 #include <xen/xen_intr.h>
169 void Xhypervisor_callback(void);
170 void failsafe_callback(void);
172 extern trap_info_t trap_table[];
173 struct proc_ldt default_proc_ldt;
174 extern int init_first;
176 extern unsigned long physfree;
179 /* Sanity check for __curthread() */
180 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
182 extern void init386(int first);
183 extern void dblfault_handler(void);
185 extern void printcpuinfo(void); /* XXX header file */
186 extern void finishidentcpu(void);
187 extern void panicifcpuunsupported(void);
189 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
190 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
192 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
193 #define CPU_ENABLE_SSE
196 static void cpu_startup(void *);
197 static void fpstate_drop(struct thread *td);
198 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
199 static int set_fpcontext(struct thread *td, const mcontext_t *mcp);
200 #ifdef CPU_ENABLE_SSE
201 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
202 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
203 #endif /* CPU_ENABLE_SSE */
204 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
207 extern vm_offset_t ksym_start, ksym_end;
210 /* Intel ICH registers */
211 #define ICH_PMBASE 0x400
212 #define ICH_SMI_EN ICH_PMBASE + 0x30
214 int _udatasel, _ucodesel;
220 static void osendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
222 #ifdef COMPAT_FREEBSD4
223 static void freebsd4_sendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
230 FEATURE(pae, "Physical Address Extensions");
234 * The number of PHYSMAP entries must be one less than the number of
235 * PHYSSEG entries because the PHYSMAP entry that spans the largest
236 * physical address that is accessible by ISA DMA is split into two
239 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
241 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
242 vm_paddr_t dump_avail[PHYSMAP_SIZE + 2];
244 /* must be 2 less so 0 0 can signal end of chunks */
245 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(phys_avail[0])) - 2)
246 #define DUMP_AVAIL_ARRAY_END ((sizeof(dump_avail) / sizeof(dump_avail[0])) - 2)
248 struct kva_md_info kmi;
250 static struct trapframe proc0_tf;
251 struct pcpu __pcpu[MAXCPU];
255 struct mem_range_softc mem_range_softc;
265 * On MacBooks, we need to disallow the legacy USB circuit to
266 * generate an SMI# because this can cause several problems,
267 * namely: incorrect CPU frequency detection and failure to
269 * We do this by disabling a bit in the SMI_EN (SMI Control and
270 * Enable register) of the Intel ICH LPC Interface Bridge.
272 sysenv = getenv("smbios.system.product");
273 if (sysenv != NULL) {
274 if (strncmp(sysenv, "MacBook1,1", 10) == 0 ||
275 strncmp(sysenv, "MacBook3,1", 10) == 0 ||
276 strncmp(sysenv, "MacBook4,1", 10) == 0 ||
277 strncmp(sysenv, "MacBookPro1,1", 13) == 0 ||
278 strncmp(sysenv, "MacBookPro1,2", 13) == 0 ||
279 strncmp(sysenv, "MacBookPro3,1", 13) == 0 ||
280 strncmp(sysenv, "MacBookPro4,1", 13) == 0 ||
281 strncmp(sysenv, "Macmini1,1", 10) == 0) {
283 printf("Disabling LEGACY_USB_EN bit on "
285 outl(ICH_SMI_EN, inl(ICH_SMI_EN) & ~0x8);
291 * Good {morning,afternoon,evening,night}.
295 panicifcpuunsupported();
301 * Display physical memory if SMBIOS reports reasonable amount.
304 sysenv = getenv("smbios.memory.enabled");
305 if (sysenv != NULL) {
306 memsize = (uintmax_t)strtoul(sysenv, (char **)NULL, 10) << 10;
309 if (memsize < ptoa((uintmax_t)cnt.v_free_count))
310 memsize = ptoa((uintmax_t)Maxmem);
311 printf("real memory = %ju (%ju MB)\n", memsize, memsize >> 20);
312 realmem = atop(memsize);
315 * Display any holes after the first chunk of extended memory.
320 printf("Physical memory chunk(s):\n");
321 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
324 size = phys_avail[indx + 1] - phys_avail[indx];
326 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
327 (uintmax_t)phys_avail[indx],
328 (uintmax_t)phys_avail[indx + 1] - 1,
329 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
333 vm_ksubmap_init(&kmi);
335 printf("avail memory = %ju (%ju MB)\n",
336 ptoa((uintmax_t)cnt.v_free_count),
337 ptoa((uintmax_t)cnt.v_free_count) / 1048576);
340 * Set up buffers, so they can be used to read disk labels.
343 vm_pager_bufferinit();
350 * Send an interrupt to process.
352 * Stack is set up to allow sigcode stored
353 * at top to call routine, followed by kcall
354 * to sigreturn routine below. After sigreturn
355 * resets the signal mask, the stack, and the
356 * frame pointer, it returns to the user
361 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
363 struct osigframe sf, *fp;
367 struct trapframe *regs;
373 PROC_LOCK_ASSERT(p, MA_OWNED);
374 sig = ksi->ksi_signo;
376 mtx_assert(&psp->ps_mtx, MA_OWNED);
378 oonstack = sigonstack(regs->tf_esp);
380 /* Allocate space for the signal handler context. */
381 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
382 SIGISMEMBER(psp->ps_sigonstack, sig)) {
383 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
384 td->td_sigstk.ss_size - sizeof(struct osigframe));
385 #if defined(COMPAT_43)
386 td->td_sigstk.ss_flags |= SS_ONSTACK;
389 fp = (struct osigframe *)regs->tf_esp - 1;
391 /* Translate the signal if appropriate. */
392 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
393 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
395 /* Build the argument list for the signal handler. */
397 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
398 bzero(&sf.sf_siginfo, sizeof(sf.sf_siginfo));
399 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
400 /* Signal handler installed with SA_SIGINFO. */
401 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
402 sf.sf_siginfo.si_signo = sig;
403 sf.sf_siginfo.si_code = ksi->ksi_code;
404 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
407 /* Old FreeBSD-style arguments. */
408 sf.sf_arg2 = ksi->ksi_code;
409 sf.sf_addr = (register_t)ksi->ksi_addr;
410 sf.sf_ahu.sf_handler = catcher;
412 mtx_unlock(&psp->ps_mtx);
415 /* Save most if not all of trap frame. */
416 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
417 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
418 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
419 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
420 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
421 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
422 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
423 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
424 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
425 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
426 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
427 sf.sf_siginfo.si_sc.sc_gs = rgs();
428 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
430 /* Build the signal context to be used by osigreturn(). */
431 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
432 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
433 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
434 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
435 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
436 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
437 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
438 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
441 * If we're a vm86 process, we want to save the segment registers.
442 * We also change eflags to be our emulated eflags, not the actual
445 if (regs->tf_eflags & PSL_VM) {
446 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
447 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
448 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
450 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
451 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
452 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
453 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
455 if (vm86->vm86_has_vme == 0)
456 sf.sf_siginfo.si_sc.sc_ps =
457 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
458 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
460 /* See sendsig() for comments. */
461 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
465 * Copy the sigframe out to the user's stack.
467 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
469 printf("process %ld has trashed its stack\n", (long)p->p_pid);
475 regs->tf_esp = (int)fp;
476 if (p->p_sysent->sv_sigcode_base != 0) {
477 regs->tf_eip = p->p_sysent->sv_sigcode_base + szsigcode -
480 /* a.out sysentvec does not use shared page */
481 regs->tf_eip = p->p_sysent->sv_psstrings - szosigcode;
483 regs->tf_eflags &= ~(PSL_T | PSL_D);
484 regs->tf_cs = _ucodesel;
485 regs->tf_ds = _udatasel;
486 regs->tf_es = _udatasel;
487 regs->tf_fs = _udatasel;
489 regs->tf_ss = _udatasel;
491 mtx_lock(&psp->ps_mtx);
493 #endif /* COMPAT_43 */
495 #ifdef COMPAT_FREEBSD4
497 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
499 struct sigframe4 sf, *sfp;
503 struct trapframe *regs;
509 PROC_LOCK_ASSERT(p, MA_OWNED);
510 sig = ksi->ksi_signo;
512 mtx_assert(&psp->ps_mtx, MA_OWNED);
514 oonstack = sigonstack(regs->tf_esp);
516 /* Save user context. */
517 bzero(&sf, sizeof(sf));
518 sf.sf_uc.uc_sigmask = *mask;
519 sf.sf_uc.uc_stack = td->td_sigstk;
520 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
521 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
522 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
523 sf.sf_uc.uc_mcontext.mc_gs = rgs();
524 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
525 bzero(sf.sf_uc.uc_mcontext.mc_fpregs,
526 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
527 bzero(sf.sf_uc.uc_mcontext.__spare__,
528 sizeof(sf.sf_uc.uc_mcontext.__spare__));
529 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
531 /* Allocate space for the signal handler context. */
532 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
533 SIGISMEMBER(psp->ps_sigonstack, sig)) {
534 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
535 td->td_sigstk.ss_size - sizeof(struct sigframe4));
536 #if defined(COMPAT_43)
537 td->td_sigstk.ss_flags |= SS_ONSTACK;
540 sfp = (struct sigframe4 *)regs->tf_esp - 1;
542 /* Translate the signal if appropriate. */
543 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
544 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
546 /* Build the argument list for the signal handler. */
548 sf.sf_ucontext = (register_t)&sfp->sf_uc;
549 bzero(&sf.sf_si, sizeof(sf.sf_si));
550 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
551 /* Signal handler installed with SA_SIGINFO. */
552 sf.sf_siginfo = (register_t)&sfp->sf_si;
553 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
555 /* Fill in POSIX parts */
556 sf.sf_si.si_signo = sig;
557 sf.sf_si.si_code = ksi->ksi_code;
558 sf.sf_si.si_addr = ksi->ksi_addr;
560 /* Old FreeBSD-style arguments. */
561 sf.sf_siginfo = ksi->ksi_code;
562 sf.sf_addr = (register_t)ksi->ksi_addr;
563 sf.sf_ahu.sf_handler = catcher;
565 mtx_unlock(&psp->ps_mtx);
569 * If we're a vm86 process, we want to save the segment registers.
570 * We also change eflags to be our emulated eflags, not the actual
573 if (regs->tf_eflags & PSL_VM) {
574 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
575 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
577 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
578 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
579 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
580 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
582 if (vm86->vm86_has_vme == 0)
583 sf.sf_uc.uc_mcontext.mc_eflags =
584 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
585 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
588 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
589 * syscalls made by the signal handler. This just avoids
590 * wasting time for our lazy fixup of such faults. PSL_NT
591 * does nothing in vm86 mode, but vm86 programs can set it
592 * almost legitimately in probes for old cpu types.
594 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
598 * Copy the sigframe out to the user's stack.
600 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
602 printf("process %ld has trashed its stack\n", (long)p->p_pid);
608 regs->tf_esp = (int)sfp;
609 regs->tf_eip = p->p_sysent->sv_sigcode_base + szsigcode -
611 regs->tf_eflags &= ~(PSL_T | PSL_D);
612 regs->tf_cs = _ucodesel;
613 regs->tf_ds = _udatasel;
614 regs->tf_es = _udatasel;
615 regs->tf_fs = _udatasel;
616 regs->tf_ss = _udatasel;
618 mtx_lock(&psp->ps_mtx);
620 #endif /* COMPAT_FREEBSD4 */
623 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
625 struct sigframe sf, *sfp;
630 struct trapframe *regs;
631 struct segment_descriptor *sdp;
637 PROC_LOCK_ASSERT(p, MA_OWNED);
638 sig = ksi->ksi_signo;
640 mtx_assert(&psp->ps_mtx, MA_OWNED);
641 #ifdef COMPAT_FREEBSD4
642 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
643 freebsd4_sendsig(catcher, ksi, mask);
648 if (SIGISMEMBER(psp->ps_osigset, sig)) {
649 osendsig(catcher, ksi, mask);
654 oonstack = sigonstack(regs->tf_esp);
656 /* Save user context. */
657 bzero(&sf, sizeof(sf));
658 sf.sf_uc.uc_sigmask = *mask;
659 sf.sf_uc.uc_stack = td->td_sigstk;
660 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
661 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
662 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
663 sf.sf_uc.uc_mcontext.mc_gs = rgs();
664 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
665 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
666 get_fpcontext(td, &sf.sf_uc.uc_mcontext);
669 * Unconditionally fill the fsbase and gsbase into the mcontext.
671 sdp = &td->td_pcb->pcb_fsd;
672 sf.sf_uc.uc_mcontext.mc_fsbase = sdp->sd_hibase << 24 |
674 sdp = &td->td_pcb->pcb_gsd;
675 sf.sf_uc.uc_mcontext.mc_gsbase = sdp->sd_hibase << 24 |
677 sf.sf_uc.uc_mcontext.mc_flags = 0;
678 bzero(sf.sf_uc.uc_mcontext.mc_spare2,
679 sizeof(sf.sf_uc.uc_mcontext.mc_spare2));
680 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
682 /* Allocate space for the signal handler context. */
683 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
684 SIGISMEMBER(psp->ps_sigonstack, sig)) {
685 sp = td->td_sigstk.ss_sp +
686 td->td_sigstk.ss_size - sizeof(struct sigframe);
687 #if defined(COMPAT_43)
688 td->td_sigstk.ss_flags |= SS_ONSTACK;
691 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
692 /* Align to 16 bytes. */
693 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
695 /* Translate the signal if appropriate. */
696 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
697 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
699 /* Build the argument list for the signal handler. */
701 sf.sf_ucontext = (register_t)&sfp->sf_uc;
702 bzero(&sf.sf_si, sizeof(sf.sf_si));
703 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
704 /* Signal handler installed with SA_SIGINFO. */
705 sf.sf_siginfo = (register_t)&sfp->sf_si;
706 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
708 /* Fill in POSIX parts */
709 sf.sf_si = ksi->ksi_info;
710 sf.sf_si.si_signo = sig; /* maybe a translated signal */
712 /* Old FreeBSD-style arguments. */
713 sf.sf_siginfo = ksi->ksi_code;
714 sf.sf_addr = (register_t)ksi->ksi_addr;
715 sf.sf_ahu.sf_handler = catcher;
717 mtx_unlock(&psp->ps_mtx);
721 * If we're a vm86 process, we want to save the segment registers.
722 * We also change eflags to be our emulated eflags, not the actual
725 if (regs->tf_eflags & PSL_VM) {
726 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
727 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
729 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
730 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
731 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
732 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
734 if (vm86->vm86_has_vme == 0)
735 sf.sf_uc.uc_mcontext.mc_eflags =
736 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
737 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
740 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
741 * syscalls made by the signal handler. This just avoids
742 * wasting time for our lazy fixup of such faults. PSL_NT
743 * does nothing in vm86 mode, but vm86 programs can set it
744 * almost legitimately in probes for old cpu types.
746 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
750 * Copy the sigframe out to the user's stack.
752 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
754 printf("process %ld has trashed its stack\n", (long)p->p_pid);
760 regs->tf_esp = (int)sfp;
761 regs->tf_eip = p->p_sysent->sv_sigcode_base;
762 if (regs->tf_eip == 0)
763 regs->tf_eip = p->p_sysent->sv_psstrings - szsigcode;
764 regs->tf_eflags &= ~(PSL_T | PSL_D);
765 regs->tf_cs = _ucodesel;
766 regs->tf_ds = _udatasel;
767 regs->tf_es = _udatasel;
768 regs->tf_fs = _udatasel;
769 regs->tf_ss = _udatasel;
771 mtx_lock(&psp->ps_mtx);
775 * System call to cleanup state after a signal
776 * has been taken. Reset signal mask and
777 * stack state from context left by sendsig (above).
778 * Return to previous pc and psl as specified by
779 * context left by sendsig. Check carefully to
780 * make sure that the user has not modified the
781 * state to gain improper privileges.
789 struct osigreturn_args /* {
790 struct osigcontext *sigcntxp;
793 struct osigcontext sc;
794 struct trapframe *regs;
795 struct osigcontext *scp;
800 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
805 if (eflags & PSL_VM) {
806 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
807 struct vm86_kernel *vm86;
810 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
811 * set up the vm86 area, and we can't enter vm86 mode.
813 if (td->td_pcb->pcb_ext == 0)
815 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
816 if (vm86->vm86_inited == 0)
819 /* Go back to user mode if both flags are set. */
820 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
821 ksiginfo_init_trap(&ksi);
822 ksi.ksi_signo = SIGBUS;
823 ksi.ksi_code = BUS_OBJERR;
824 ksi.ksi_addr = (void *)regs->tf_eip;
825 trapsignal(td, &ksi);
828 if (vm86->vm86_has_vme) {
829 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
830 (eflags & VME_USERCHANGE) | PSL_VM;
832 vm86->vm86_eflags = eflags; /* save VIF, VIP */
833 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
834 (eflags & VM_USERCHANGE) | PSL_VM;
836 tf->tf_vm86_ds = scp->sc_ds;
837 tf->tf_vm86_es = scp->sc_es;
838 tf->tf_vm86_fs = scp->sc_fs;
839 tf->tf_vm86_gs = scp->sc_gs;
840 tf->tf_ds = _udatasel;
841 tf->tf_es = _udatasel;
842 tf->tf_fs = _udatasel;
845 * Don't allow users to change privileged or reserved flags.
847 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
852 * Don't allow users to load a valid privileged %cs. Let the
853 * hardware check for invalid selectors, excess privilege in
854 * other selectors, invalid %eip's and invalid %esp's.
856 if (!CS_SECURE(scp->sc_cs)) {
857 ksiginfo_init_trap(&ksi);
858 ksi.ksi_signo = SIGBUS;
859 ksi.ksi_code = BUS_OBJERR;
860 ksi.ksi_trapno = T_PROTFLT;
861 ksi.ksi_addr = (void *)regs->tf_eip;
862 trapsignal(td, &ksi);
865 regs->tf_ds = scp->sc_ds;
866 regs->tf_es = scp->sc_es;
867 regs->tf_fs = scp->sc_fs;
870 /* Restore remaining registers. */
871 regs->tf_eax = scp->sc_eax;
872 regs->tf_ebx = scp->sc_ebx;
873 regs->tf_ecx = scp->sc_ecx;
874 regs->tf_edx = scp->sc_edx;
875 regs->tf_esi = scp->sc_esi;
876 regs->tf_edi = scp->sc_edi;
877 regs->tf_cs = scp->sc_cs;
878 regs->tf_ss = scp->sc_ss;
879 regs->tf_isp = scp->sc_isp;
880 regs->tf_ebp = scp->sc_fp;
881 regs->tf_esp = scp->sc_sp;
882 regs->tf_eip = scp->sc_pc;
883 regs->tf_eflags = eflags;
885 #if defined(COMPAT_43)
886 if (scp->sc_onstack & 1)
887 td->td_sigstk.ss_flags |= SS_ONSTACK;
889 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
891 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
893 return (EJUSTRETURN);
895 #endif /* COMPAT_43 */
897 #ifdef COMPAT_FREEBSD4
902 freebsd4_sigreturn(td, uap)
904 struct freebsd4_sigreturn_args /* {
905 const ucontext4 *sigcntxp;
909 struct trapframe *regs;
910 struct ucontext4 *ucp;
911 int cs, eflags, error;
914 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
919 eflags = ucp->uc_mcontext.mc_eflags;
920 if (eflags & PSL_VM) {
921 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
922 struct vm86_kernel *vm86;
925 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
926 * set up the vm86 area, and we can't enter vm86 mode.
928 if (td->td_pcb->pcb_ext == 0)
930 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
931 if (vm86->vm86_inited == 0)
934 /* Go back to user mode if both flags are set. */
935 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
936 ksiginfo_init_trap(&ksi);
937 ksi.ksi_signo = SIGBUS;
938 ksi.ksi_code = BUS_OBJERR;
939 ksi.ksi_addr = (void *)regs->tf_eip;
940 trapsignal(td, &ksi);
942 if (vm86->vm86_has_vme) {
943 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
944 (eflags & VME_USERCHANGE) | PSL_VM;
946 vm86->vm86_eflags = eflags; /* save VIF, VIP */
947 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
948 (eflags & VM_USERCHANGE) | PSL_VM;
950 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
951 tf->tf_eflags = eflags;
952 tf->tf_vm86_ds = tf->tf_ds;
953 tf->tf_vm86_es = tf->tf_es;
954 tf->tf_vm86_fs = tf->tf_fs;
955 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
956 tf->tf_ds = _udatasel;
957 tf->tf_es = _udatasel;
958 tf->tf_fs = _udatasel;
961 * Don't allow users to change privileged or reserved flags.
963 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
964 uprintf("pid %d (%s): freebsd4_sigreturn eflags = 0x%x\n",
965 td->td_proc->p_pid, td->td_name, eflags);
970 * Don't allow users to load a valid privileged %cs. Let the
971 * hardware check for invalid selectors, excess privilege in
972 * other selectors, invalid %eip's and invalid %esp's.
974 cs = ucp->uc_mcontext.mc_cs;
975 if (!CS_SECURE(cs)) {
976 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
977 td->td_proc->p_pid, td->td_name, cs);
978 ksiginfo_init_trap(&ksi);
979 ksi.ksi_signo = SIGBUS;
980 ksi.ksi_code = BUS_OBJERR;
981 ksi.ksi_trapno = T_PROTFLT;
982 ksi.ksi_addr = (void *)regs->tf_eip;
983 trapsignal(td, &ksi);
987 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
990 #if defined(COMPAT_43)
991 if (ucp->uc_mcontext.mc_onstack & 1)
992 td->td_sigstk.ss_flags |= SS_ONSTACK;
994 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
996 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
997 return (EJUSTRETURN);
999 #endif /* COMPAT_FREEBSD4 */
1005 sys_sigreturn(td, uap)
1007 struct sigreturn_args /* {
1008 const struct __ucontext *sigcntxp;
1012 struct trapframe *regs;
1014 int cs, eflags, error, ret;
1017 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
1021 regs = td->td_frame;
1022 eflags = ucp->uc_mcontext.mc_eflags;
1023 if (eflags & PSL_VM) {
1024 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
1025 struct vm86_kernel *vm86;
1028 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
1029 * set up the vm86 area, and we can't enter vm86 mode.
1031 if (td->td_pcb->pcb_ext == 0)
1033 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
1034 if (vm86->vm86_inited == 0)
1037 /* Go back to user mode if both flags are set. */
1038 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
1039 ksiginfo_init_trap(&ksi);
1040 ksi.ksi_signo = SIGBUS;
1041 ksi.ksi_code = BUS_OBJERR;
1042 ksi.ksi_addr = (void *)regs->tf_eip;
1043 trapsignal(td, &ksi);
1046 if (vm86->vm86_has_vme) {
1047 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
1048 (eflags & VME_USERCHANGE) | PSL_VM;
1050 vm86->vm86_eflags = eflags; /* save VIF, VIP */
1051 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
1052 (eflags & VM_USERCHANGE) | PSL_VM;
1054 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
1055 tf->tf_eflags = eflags;
1056 tf->tf_vm86_ds = tf->tf_ds;
1057 tf->tf_vm86_es = tf->tf_es;
1058 tf->tf_vm86_fs = tf->tf_fs;
1059 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
1060 tf->tf_ds = _udatasel;
1061 tf->tf_es = _udatasel;
1062 tf->tf_fs = _udatasel;
1065 * Don't allow users to change privileged or reserved flags.
1067 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
1068 uprintf("pid %d (%s): sigreturn eflags = 0x%x\n",
1069 td->td_proc->p_pid, td->td_name, eflags);
1074 * Don't allow users to load a valid privileged %cs. Let the
1075 * hardware check for invalid selectors, excess privilege in
1076 * other selectors, invalid %eip's and invalid %esp's.
1078 cs = ucp->uc_mcontext.mc_cs;
1079 if (!CS_SECURE(cs)) {
1080 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
1081 td->td_proc->p_pid, td->td_name, cs);
1082 ksiginfo_init_trap(&ksi);
1083 ksi.ksi_signo = SIGBUS;
1084 ksi.ksi_code = BUS_OBJERR;
1085 ksi.ksi_trapno = T_PROTFLT;
1086 ksi.ksi_addr = (void *)regs->tf_eip;
1087 trapsignal(td, &ksi);
1091 ret = set_fpcontext(td, &ucp->uc_mcontext);
1094 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1097 #if defined(COMPAT_43)
1098 if (ucp->uc_mcontext.mc_onstack & 1)
1099 td->td_sigstk.ss_flags |= SS_ONSTACK;
1101 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1104 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
1105 return (EJUSTRETURN);
1109 * Machine dependent boot() routine
1111 * I haven't seen anything to put here yet
1112 * Possibly some stuff might be grafted back here from boot()
1120 * Flush the D-cache for non-DMA I/O so that the I-cache can
1121 * be made coherent later.
1124 cpu_flush_dcache(void *ptr, size_t len)
1126 /* Not applicable */
1129 /* Get current clock frequency for the given cpu id. */
1131 cpu_est_clockrate(int cpu_id, uint64_t *rate)
1133 uint64_t tsc1, tsc2;
1134 uint64_t acnt, mcnt, perf;
1137 if (pcpu_find(cpu_id) == NULL || rate == NULL)
1139 if ((cpu_feature & CPUID_TSC) == 0)
1140 return (EOPNOTSUPP);
1143 * If TSC is P-state invariant and APERF/MPERF MSRs do not exist,
1144 * DELAY(9) based logic fails.
1146 if (tsc_is_invariant && !tsc_perf_stat)
1147 return (EOPNOTSUPP);
1151 /* Schedule ourselves on the indicated cpu. */
1152 thread_lock(curthread);
1153 sched_bind(curthread, cpu_id);
1154 thread_unlock(curthread);
1158 /* Calibrate by measuring a short delay. */
1159 reg = intr_disable();
1160 if (tsc_is_invariant) {
1161 wrmsr(MSR_MPERF, 0);
1162 wrmsr(MSR_APERF, 0);
1165 mcnt = rdmsr(MSR_MPERF);
1166 acnt = rdmsr(MSR_APERF);
1169 perf = 1000 * acnt / mcnt;
1170 *rate = (tsc2 - tsc1) * perf;
1176 *rate = (tsc2 - tsc1) * 1000;
1181 thread_lock(curthread);
1182 sched_unbind(curthread);
1183 thread_unlock(curthread);
1196 HYPERVISOR_sched_op(SCHEDOP_block, 0);
1202 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
1205 int scheduler_running;
1208 cpu_idle_hlt(sbintime_t sbt)
1211 scheduler_running = 1;
1218 * Shutdown the CPU as much as possible
1229 void (*cpu_idle_hook)(sbintime_t) = NULL; /* ACPI idle hook. */
1230 static int cpu_ident_amdc1e = 0; /* AMD C1E supported. */
1231 static int idle_mwait = 1; /* Use MONITOR/MWAIT for short idle. */
1232 TUNABLE_INT("machdep.idle_mwait", &idle_mwait);
1233 SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RW, &idle_mwait,
1234 0, "Use MONITOR/MWAIT for short idle");
1236 #define STATE_RUNNING 0x0
1237 #define STATE_MWAIT 0x1
1238 #define STATE_SLEEPING 0x2
1241 cpu_idle_acpi(sbintime_t sbt)
1245 state = (int *)PCPU_PTR(monitorbuf);
1246 *state = STATE_SLEEPING;
1248 /* See comments in cpu_idle_hlt(). */
1250 if (sched_runnable())
1252 else if (cpu_idle_hook)
1255 __asm __volatile("sti; hlt");
1256 *state = STATE_RUNNING;
1261 cpu_idle_hlt(sbintime_t sbt)
1265 state = (int *)PCPU_PTR(monitorbuf);
1266 *state = STATE_SLEEPING;
1269 * Since we may be in a critical section from cpu_idle(), if
1270 * an interrupt fires during that critical section we may have
1271 * a pending preemption. If the CPU halts, then that thread
1272 * may not execute until a later interrupt awakens the CPU.
1273 * To handle this race, check for a runnable thread after
1274 * disabling interrupts and immediately return if one is
1275 * found. Also, we must absolutely guarentee that hlt is
1276 * the next instruction after sti. This ensures that any
1277 * interrupt that fires after the call to disable_intr() will
1278 * immediately awaken the CPU from hlt. Finally, please note
1279 * that on x86 this works fine because of interrupts enabled only
1280 * after the instruction following sti takes place, while IF is set
1281 * to 1 immediately, allowing hlt instruction to acknowledge the
1285 if (sched_runnable())
1288 __asm __volatile("sti; hlt");
1289 *state = STATE_RUNNING;
1294 * MWAIT cpu power states. Lower 4 bits are sub-states.
1296 #define MWAIT_C0 0xf0
1297 #define MWAIT_C1 0x00
1298 #define MWAIT_C2 0x10
1299 #define MWAIT_C3 0x20
1300 #define MWAIT_C4 0x30
1303 cpu_idle_mwait(sbintime_t sbt)
1307 state = (int *)PCPU_PTR(monitorbuf);
1308 *state = STATE_MWAIT;
1310 /* See comments in cpu_idle_hlt(). */
1312 if (sched_runnable()) {
1314 *state = STATE_RUNNING;
1317 cpu_monitor(state, 0, 0);
1318 if (*state == STATE_MWAIT)
1319 __asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0));
1322 *state = STATE_RUNNING;
1326 cpu_idle_spin(sbintime_t sbt)
1331 state = (int *)PCPU_PTR(monitorbuf);
1332 *state = STATE_RUNNING;
1335 * The sched_runnable() call is racy but as long as there is
1336 * a loop missing it one time will have just a little impact if any
1337 * (and it is much better than missing the check at all).
1339 for (i = 0; i < 1000; i++) {
1340 if (sched_runnable())
1347 * C1E renders the local APIC timer dead, so we disable it by
1348 * reading the Interrupt Pending Message register and clearing
1349 * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
1352 * "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors"
1353 * #32559 revision 3.00+
1355 #define MSR_AMDK8_IPM 0xc0010055
1356 #define AMDK8_SMIONCMPHALT (1ULL << 27)
1357 #define AMDK8_C1EONCMPHALT (1ULL << 28)
1358 #define AMDK8_CMPHALT (AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT)
1361 cpu_probe_amdc1e(void)
1365 * Detect the presence of C1E capability mostly on latest
1366 * dual-cores (or future) k8 family.
1368 if (cpu_vendor_id == CPU_VENDOR_AMD &&
1369 (cpu_id & 0x00000f00) == 0x00000f00 &&
1370 (cpu_id & 0x0fff0000) >= 0x00040000) {
1371 cpu_ident_amdc1e = 1;
1376 void (*cpu_idle_fn)(sbintime_t) = cpu_idle_hlt;
1378 void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi;
1387 sbintime_t sbt = -1;
1389 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d",
1391 #if defined(MP_WATCHDOG) && !defined(XEN)
1392 ap_watchdog(PCPU_GET(cpuid));
1395 /* If we are busy - try to use fast methods. */
1397 if ((cpu_feature2 & CPUID2_MON) && idle_mwait) {
1398 cpu_idle_mwait(busy);
1404 /* If we have time - switch timers into idle mode. */
1407 sbt = cpu_idleclock();
1411 /* Apply AMD APIC timer C1E workaround. */
1412 if (cpu_ident_amdc1e && cpu_disable_deep_sleep) {
1413 msr = rdmsr(MSR_AMDK8_IPM);
1414 if (msr & AMDK8_CMPHALT)
1415 wrmsr(MSR_AMDK8_IPM, msr & ~AMDK8_CMPHALT);
1419 /* Call main idle method. */
1422 /* Switch timers mack into active mode. */
1430 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done",
1435 cpu_idle_wakeup(int cpu)
1440 pcpu = pcpu_find(cpu);
1441 state = (int *)pcpu->pc_monitorbuf;
1443 * This doesn't need to be atomic since missing the race will
1444 * simply result in unnecessary IPIs.
1446 if (*state == STATE_SLEEPING)
1448 if (*state == STATE_MWAIT)
1449 *state = STATE_RUNNING;
1454 * Ordered by speed/power consumption.
1460 { cpu_idle_spin, "spin" },
1461 { cpu_idle_mwait, "mwait" },
1462 { cpu_idle_hlt, "hlt" },
1463 { cpu_idle_acpi, "acpi" },
1468 idle_sysctl_available(SYSCTL_HANDLER_ARGS)
1474 avail = malloc(256, M_TEMP, M_WAITOK);
1476 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1477 if (strstr(idle_tbl[i].id_name, "mwait") &&
1478 (cpu_feature2 & CPUID2_MON) == 0)
1480 if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
1481 cpu_idle_hook == NULL)
1483 p += sprintf(p, "%s%s", p != avail ? ", " : "",
1484 idle_tbl[i].id_name);
1486 error = sysctl_handle_string(oidp, avail, 0, req);
1487 free(avail, M_TEMP);
1491 SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
1492 0, 0, idle_sysctl_available, "A", "list of available idle functions");
1495 idle_sysctl(SYSCTL_HANDLER_ARGS)
1503 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1504 if (idle_tbl[i].id_fn == cpu_idle_fn) {
1505 p = idle_tbl[i].id_name;
1509 strncpy(buf, p, sizeof(buf));
1510 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1511 if (error != 0 || req->newptr == NULL)
1513 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1514 if (strstr(idle_tbl[i].id_name, "mwait") &&
1515 (cpu_feature2 & CPUID2_MON) == 0)
1517 if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
1518 cpu_idle_hook == NULL)
1520 if (strcmp(idle_tbl[i].id_name, buf))
1522 cpu_idle_fn = idle_tbl[i].id_fn;
1528 SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
1529 idle_sysctl, "A", "currently selected idle function");
1532 * Reset registers to default values on exec.
1535 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
1537 struct trapframe *regs = td->td_frame;
1538 struct pcb *pcb = td->td_pcb;
1540 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1541 pcb->pcb_gs = _udatasel;
1544 mtx_lock_spin(&dt_lock);
1545 if (td->td_proc->p_md.md_ldt)
1548 mtx_unlock_spin(&dt_lock);
1550 bzero((char *)regs, sizeof(struct trapframe));
1551 regs->tf_eip = imgp->entry_addr;
1552 regs->tf_esp = stack;
1553 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1554 regs->tf_ss = _udatasel;
1555 regs->tf_ds = _udatasel;
1556 regs->tf_es = _udatasel;
1557 regs->tf_fs = _udatasel;
1558 regs->tf_cs = _ucodesel;
1560 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1561 regs->tf_ebx = imgp->ps_strings;
1564 * Reset the hardware debug registers if they were in use.
1565 * They won't have any meaning for the newly exec'd process.
1567 if (pcb->pcb_flags & PCB_DBREGS) {
1574 if (pcb == curpcb) {
1576 * Clear the debug registers on the running
1577 * CPU, otherwise they will end up affecting
1578 * the next process we switch to.
1582 pcb->pcb_flags &= ~PCB_DBREGS;
1586 * Initialize the math emulator (if any) for the current process.
1587 * Actually, just clear the bit that says that the emulator has
1588 * been initialized. Initialization is delayed until the process
1589 * traps to the emulator (if it is done at all) mainly because
1590 * emulators don't provide an entry point for initialization.
1592 td->td_pcb->pcb_flags &= ~FP_SOFTFP;
1593 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1596 * Drop the FP state if we hold it, so that the process gets a
1597 * clean FP state if it uses the FPU again.
1602 * XXX - Linux emulator
1603 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1606 td->td_retval[1] = 0;
1617 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1619 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1620 * instructions. We must set the CR0_MP bit and use the CR0_TS
1621 * bit to control the trap, because setting the CR0_EM bit does
1622 * not cause WAIT instructions to trap. It's important to trap
1623 * WAIT instructions - otherwise the "wait" variants of no-wait
1624 * control instructions would degenerate to the "no-wait" variants
1625 * after FP context switches but work correctly otherwise. It's
1626 * particularly important to trap WAITs when there is no NPX -
1627 * otherwise the "wait" variants would always degenerate.
1629 * Try setting CR0_NE to get correct error reporting on 486DX's.
1630 * Setting it should fail or do nothing on lesser processors.
1632 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1637 u_long bootdev; /* not a struct cdev *- encoding is different */
1638 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1639 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1641 static char bootmethod[16] = "BIOS";
1642 SYSCTL_STRING(_machdep, OID_AUTO, bootmethod, CTLFLAG_RD, bootmethod, 0,
1643 "System firmware boot method");
1646 * Initialize 386 and configure to run kernel
1650 * Initialize segments & interrupt table
1656 union descriptor *gdt;
1657 union descriptor *ldt;
1659 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1660 union descriptor ldt[NLDT]; /* local descriptor table */
1662 static struct gate_descriptor idt0[NIDT];
1663 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1664 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1665 struct mtx dt_lock; /* lock for GDT and LDT */
1667 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1668 extern int has_f00f_bug;
1671 static struct i386tss dblfault_tss;
1672 static char dblfault_stack[PAGE_SIZE];
1674 extern vm_offset_t proc0kstack;
1678 * software prototypes -- in more palatable form.
1680 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1681 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1683 struct soft_segment_descriptor gdt_segs[] = {
1684 /* GNULL_SEL 0 Null Descriptor */
1690 .ssd_xx = 0, .ssd_xx1 = 0,
1693 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1695 .ssd_limit = 0xfffff,
1696 .ssd_type = SDT_MEMRWA,
1699 .ssd_xx = 0, .ssd_xx1 = 0,
1702 /* GUFS_SEL 2 %fs Descriptor for user */
1704 .ssd_limit = 0xfffff,
1705 .ssd_type = SDT_MEMRWA,
1708 .ssd_xx = 0, .ssd_xx1 = 0,
1711 /* GUGS_SEL 3 %gs Descriptor for user */
1713 .ssd_limit = 0xfffff,
1714 .ssd_type = SDT_MEMRWA,
1717 .ssd_xx = 0, .ssd_xx1 = 0,
1720 /* GCODE_SEL 4 Code Descriptor for kernel */
1722 .ssd_limit = 0xfffff,
1723 .ssd_type = SDT_MEMERA,
1726 .ssd_xx = 0, .ssd_xx1 = 0,
1729 /* GDATA_SEL 5 Data Descriptor for kernel */
1731 .ssd_limit = 0xfffff,
1732 .ssd_type = SDT_MEMRWA,
1735 .ssd_xx = 0, .ssd_xx1 = 0,
1738 /* GUCODE_SEL 6 Code Descriptor for user */
1740 .ssd_limit = 0xfffff,
1741 .ssd_type = SDT_MEMERA,
1744 .ssd_xx = 0, .ssd_xx1 = 0,
1747 /* GUDATA_SEL 7 Data Descriptor for user */
1749 .ssd_limit = 0xfffff,
1750 .ssd_type = SDT_MEMRWA,
1753 .ssd_xx = 0, .ssd_xx1 = 0,
1756 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1757 { .ssd_base = 0x400,
1758 .ssd_limit = 0xfffff,
1759 .ssd_type = SDT_MEMRWA,
1762 .ssd_xx = 0, .ssd_xx1 = 0,
1766 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1769 .ssd_limit = sizeof(struct i386tss)-1,
1770 .ssd_type = SDT_SYS386TSS,
1773 .ssd_xx = 0, .ssd_xx1 = 0,
1776 /* GLDT_SEL 10 LDT Descriptor */
1777 { .ssd_base = (int) ldt,
1778 .ssd_limit = sizeof(ldt)-1,
1779 .ssd_type = SDT_SYSLDT,
1782 .ssd_xx = 0, .ssd_xx1 = 0,
1785 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1786 { .ssd_base = (int) ldt,
1787 .ssd_limit = (512 * sizeof(union descriptor)-1),
1788 .ssd_type = SDT_SYSLDT,
1791 .ssd_xx = 0, .ssd_xx1 = 0,
1794 /* GPANIC_SEL 12 Panic Tss Descriptor */
1795 { .ssd_base = (int) &dblfault_tss,
1796 .ssd_limit = sizeof(struct i386tss)-1,
1797 .ssd_type = SDT_SYS386TSS,
1800 .ssd_xx = 0, .ssd_xx1 = 0,
1803 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1805 .ssd_limit = 0xfffff,
1806 .ssd_type = SDT_MEMERA,
1809 .ssd_xx = 0, .ssd_xx1 = 0,
1812 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1814 .ssd_limit = 0xfffff,
1815 .ssd_type = SDT_MEMERA,
1818 .ssd_xx = 0, .ssd_xx1 = 0,
1821 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1823 .ssd_limit = 0xfffff,
1824 .ssd_type = SDT_MEMRWA,
1827 .ssd_xx = 0, .ssd_xx1 = 0,
1830 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1832 .ssd_limit = 0xfffff,
1833 .ssd_type = SDT_MEMRWA,
1836 .ssd_xx = 0, .ssd_xx1 = 0,
1839 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1841 .ssd_limit = 0xfffff,
1842 .ssd_type = SDT_MEMRWA,
1845 .ssd_xx = 0, .ssd_xx1 = 0,
1848 /* GNDIS_SEL 18 NDIS Descriptor */
1854 .ssd_xx = 0, .ssd_xx1 = 0,
1860 static struct soft_segment_descriptor ldt_segs[] = {
1861 /* Null Descriptor - overwritten by call gate */
1867 .ssd_xx = 0, .ssd_xx1 = 0,
1870 /* Null Descriptor - overwritten by call gate */
1876 .ssd_xx = 0, .ssd_xx1 = 0,
1879 /* Null Descriptor - overwritten by call gate */
1885 .ssd_xx = 0, .ssd_xx1 = 0,
1888 /* Code Descriptor for user */
1890 .ssd_limit = 0xfffff,
1891 .ssd_type = SDT_MEMERA,
1894 .ssd_xx = 0, .ssd_xx1 = 0,
1897 /* Null Descriptor - overwritten by call gate */
1903 .ssd_xx = 0, .ssd_xx1 = 0,
1906 /* Data Descriptor for user */
1908 .ssd_limit = 0xfffff,
1909 .ssd_type = SDT_MEMRWA,
1912 .ssd_xx = 0, .ssd_xx1 = 0,
1918 setidt(idx, func, typ, dpl, selec)
1925 struct gate_descriptor *ip;
1928 ip->gd_looffset = (int)func;
1929 ip->gd_selector = selec;
1935 ip->gd_hioffset = ((int)func)>>16 ;
1939 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1940 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1941 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1942 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1944 #ifdef KDTRACE_HOOKS
1948 IDTVEC(xen_intr_upcall),
1950 IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1954 * Display the index and function name of any IDT entries that don't use
1955 * the default 'rsvd' entry point.
1957 DB_SHOW_COMMAND(idt, db_show_idt)
1959 struct gate_descriptor *ip;
1964 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
1965 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1966 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1967 db_printf("%3d\t", idx);
1968 db_printsym(func, DB_STGY_PROC);
1975 /* Show privileged registers. */
1976 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
1978 uint64_t idtr, gdtr;
1981 db_printf("idtr\t0x%08x/%04x\n",
1982 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
1984 db_printf("gdtr\t0x%08x/%04x\n",
1985 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
1986 db_printf("ldtr\t0x%04x\n", rldt());
1987 db_printf("tr\t0x%04x\n", rtr());
1988 db_printf("cr0\t0x%08x\n", rcr0());
1989 db_printf("cr2\t0x%08x\n", rcr2());
1990 db_printf("cr3\t0x%08x\n", rcr3());
1991 db_printf("cr4\t0x%08x\n", rcr4());
1997 struct segment_descriptor *sd;
1998 struct soft_segment_descriptor *ssd;
2000 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
2001 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
2002 ssd->ssd_type = sd->sd_type;
2003 ssd->ssd_dpl = sd->sd_dpl;
2004 ssd->ssd_p = sd->sd_p;
2005 ssd->ssd_def32 = sd->sd_def32;
2006 ssd->ssd_gran = sd->sd_gran;
2011 add_smap_entry(struct bios_smap *smap, vm_paddr_t *physmap, int *physmap_idxp)
2013 int i, insert_idx, physmap_idx;
2015 physmap_idx = *physmap_idxp;
2017 if (boothowto & RB_VERBOSE)
2018 printf("SMAP type=%02x base=%016llx len=%016llx\n",
2019 smap->type, smap->base, smap->length);
2021 if (smap->type != SMAP_TYPE_MEMORY)
2024 if (smap->length == 0)
2028 if (smap->base > 0xffffffff) {
2029 printf("%uK of memory above 4GB ignored\n",
2030 (u_int)(smap->length / 1024));
2036 * Find insertion point while checking for overlap. Start off by
2037 * assuming the new entry will be added to the end.
2039 insert_idx = physmap_idx + 2;
2040 for (i = 0; i <= physmap_idx; i += 2) {
2041 if (smap->base < physmap[i + 1]) {
2042 if (smap->base + smap->length <= physmap[i]) {
2046 if (boothowto & RB_VERBOSE)
2048 "Overlapping memory regions, ignoring second region\n");
2053 /* See if we can prepend to the next entry. */
2054 if (insert_idx <= physmap_idx &&
2055 smap->base + smap->length == physmap[insert_idx]) {
2056 physmap[insert_idx] = smap->base;
2060 /* See if we can append to the previous entry. */
2061 if (insert_idx > 0 && smap->base == physmap[insert_idx - 1]) {
2062 physmap[insert_idx - 1] += smap->length;
2067 *physmap_idxp = physmap_idx;
2068 if (physmap_idx == PHYSMAP_SIZE) {
2070 "Too many segments in the physical address map, giving up\n");
2075 * Move the last 'N' entries down to make room for the new
2078 for (i = physmap_idx; i > insert_idx; i -= 2) {
2079 physmap[i] = physmap[i - 2];
2080 physmap[i + 1] = physmap[i - 1];
2083 /* Insert the new entry. */
2084 physmap[insert_idx] = smap->base;
2085 physmap[insert_idx + 1] = smap->base + smap->length;
2096 if (basemem > 640) {
2097 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
2103 * XXX if biosbasemem is now < 640, there is a `hole'
2104 * between the end of base memory and the start of
2105 * ISA memory. The hole may be empty or it may
2106 * contain BIOS code or data. Map it read/write so
2107 * that the BIOS can write to it. (Memory from 0 to
2108 * the physical end of the kernel is mapped read-only
2109 * to begin with and then parts of it are remapped.
2110 * The parts that aren't remapped form holes that
2111 * remain read-only and are unused by the kernel.
2112 * The base memory area is below the physical end of
2113 * the kernel and right now forms a read-only hole.
2114 * The part of it from PAGE_SIZE to
2115 * (trunc_page(biosbasemem * 1024) - 1) will be
2116 * remapped and used by the kernel later.)
2118 * This code is similar to the code used in
2119 * pmap_mapdev, but since no memory needs to be
2120 * allocated we simply change the mapping.
2122 for (pa = trunc_page(basemem * 1024);
2123 pa < ISA_HOLE_START; pa += PAGE_SIZE)
2124 pmap_kenter(KERNBASE + pa, pa);
2127 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
2128 * the vm86 page table so that vm86 can scribble on them using
2129 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
2130 * page 0, at least as initialized here?
2132 pte = (pt_entry_t *)vm86paddr;
2133 for (i = basemem / 4; i < 160; i++)
2134 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
2139 * Populate the (physmap) array with base/bound pairs describing the
2140 * available physical memory in the system, then test this memory and
2141 * build the phys_avail array describing the actually-available memory.
2143 * If we cannot accurately determine the physical memory map, then use
2144 * value from the 0xE801 call, and failing that, the RTC.
2146 * Total memory size may be set by the kernel environment variable
2147 * hw.physmem or the compile-time define MAXMEM.
2149 * XXX first should be vm_paddr_t.
2152 getmemsize(int first)
2154 int has_smap, off, physmap_idx, pa_indx, da_indx;
2155 u_long physmem_tunable, memtest;
2156 vm_paddr_t physmap[PHYSMAP_SIZE];
2158 quad_t dcons_addr, dcons_size;
2160 int hasbrokenint12, i, res;
2162 struct vm86frame vmf;
2163 struct vm86context vmc;
2165 struct bios_smap *smap, *smapbase, *smapend;
2172 Maxmem = xen_start_info->nr_pages - init_first;
2175 physmap[0] = init_first << PAGE_SHIFT;
2176 physmap[1] = ptoa(Maxmem) - round_page(msgbufsize);
2180 if (arch_i386_is_xbox) {
2182 * We queried the memory size before, so chop off 4MB for
2183 * the framebuffer and inform the OS of this.
2186 physmap[1] = (arch_i386_xbox_memsize * 1024 * 1024) - XBOX_FB_SIZE;
2191 bzero(&vmf, sizeof(vmf));
2192 bzero(physmap, sizeof(physmap));
2196 * Check if the loader supplied an SMAP memory map. If so,
2197 * use that and do not make any VM86 calls.
2201 kmdp = preload_search_by_type("elf kernel");
2203 kmdp = preload_search_by_type("elf32 kernel");
2205 smapbase = (struct bios_smap *)preload_search_info(kmdp,
2206 MODINFO_METADATA | MODINFOMD_SMAP);
2207 if (smapbase != NULL) {
2209 * subr_module.c says:
2210 * "Consumer may safely assume that size value precedes data."
2211 * ie: an int32_t immediately precedes SMAP.
2213 smapsize = *((u_int32_t *)smapbase - 1);
2214 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
2217 for (smap = smapbase; smap < smapend; smap++)
2218 if (!add_smap_entry(smap, physmap, &physmap_idx))
2224 * Some newer BIOSes have a broken INT 12H implementation
2225 * which causes a kernel panic immediately. In this case, we
2226 * need use the SMAP to determine the base memory size.
2229 TUNABLE_INT_FETCH("hw.hasbrokenint12", &hasbrokenint12);
2230 if (hasbrokenint12 == 0) {
2231 /* Use INT12 to determine base memory size. */
2232 vm86_intcall(0x12, &vmf);
2233 basemem = vmf.vmf_ax;
2238 * Fetch the memory map with INT 15:E820. Map page 1 R/W into
2239 * the kernel page table so we can use it as a buffer. The
2240 * kernel will unmap this page later.
2242 pmap_kenter(KERNBASE + (1 << PAGE_SHIFT), 1 << PAGE_SHIFT);
2244 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
2245 res = vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
2246 KASSERT(res != 0, ("vm86_getptr() failed: address not found"));
2250 vmf.vmf_eax = 0xE820;
2251 vmf.vmf_edx = SMAP_SIG;
2252 vmf.vmf_ecx = sizeof(struct bios_smap);
2253 i = vm86_datacall(0x15, &vmf, &vmc);
2254 if (i || vmf.vmf_eax != SMAP_SIG)
2257 if (!add_smap_entry(smap, physmap, &physmap_idx))
2259 } while (vmf.vmf_ebx != 0);
2263 * If we didn't fetch the "base memory" size from INT12,
2264 * figure it out from the SMAP (or just guess).
2267 for (i = 0; i <= physmap_idx; i += 2) {
2268 if (physmap[i] == 0x00000000) {
2269 basemem = physmap[i + 1] / 1024;
2274 /* XXX: If we couldn't find basemem from SMAP, just guess. */
2280 if (physmap[1] != 0)
2284 * If we failed to find an SMAP, figure out the extended
2285 * memory size. We will then build a simple memory map with
2286 * two segments, one for "base memory" and the second for
2287 * "extended memory". Note that "extended memory" starts at a
2288 * physical address of 1MB and that both basemem and extmem
2289 * are in units of 1KB.
2291 * First, try to fetch the extended memory size via INT 15:E801.
2293 vmf.vmf_ax = 0xE801;
2294 if (vm86_intcall(0x15, &vmf) == 0) {
2295 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
2298 * If INT15:E801 fails, this is our last ditch effort
2299 * to determine the extended memory size. Currently
2300 * we prefer the RTC value over INT15:88.
2304 vm86_intcall(0x15, &vmf);
2305 extmem = vmf.vmf_ax;
2307 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
2312 * Special hack for chipsets that still remap the 384k hole when
2313 * there's 16MB of memory - this really confuses people that
2314 * are trying to use bus mastering ISA controllers with the
2315 * "16MB limit"; they only have 16MB, but the remapping puts
2316 * them beyond the limit.
2318 * If extended memory is between 15-16MB (16-17MB phys address range),
2321 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
2325 physmap[1] = basemem * 1024;
2327 physmap[physmap_idx] = 0x100000;
2328 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
2333 * Now, physmap contains a map of physical memory.
2337 /* make hole for AP bootstrap code */
2338 physmap[1] = mp_bootaddress(physmap[1]);
2342 * Maxmem isn't the "maximum memory", it's one larger than the
2343 * highest page of the physical address space. It should be
2344 * called something like "Maxphyspage". We may adjust this
2345 * based on ``hw.physmem'' and the results of the memory test.
2347 Maxmem = atop(physmap[physmap_idx + 1]);
2350 Maxmem = MAXMEM / 4;
2353 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
2354 Maxmem = atop(physmem_tunable);
2357 * If we have an SMAP, don't allow MAXMEM or hw.physmem to extend
2358 * the amount of memory in the system.
2360 if (has_smap && Maxmem > atop(physmap[physmap_idx + 1]))
2361 Maxmem = atop(physmap[physmap_idx + 1]);
2364 * By default enable the memory test on real hardware, and disable
2365 * it if we appear to be running in a VM. This avoids touching all
2366 * pages unnecessarily, which doesn't matter on real hardware but is
2367 * bad for shared VM hosts. Use a general name so that
2368 * one could eventually do more with the code than just disable it.
2370 memtest = (vm_guest > VM_GUEST_NO) ? 0 : 1;
2371 TUNABLE_ULONG_FETCH("hw.memtest.tests", &memtest);
2373 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
2374 (boothowto & RB_VERBOSE))
2375 printf("Physical memory use set to %ldK\n", Maxmem * 4);
2378 * If Maxmem has been increased beyond what the system has detected,
2379 * extend the last memory segment to the new limit.
2381 if (atop(physmap[physmap_idx + 1]) < Maxmem)
2382 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
2384 /* call pmap initialization to make new kernel address space */
2385 pmap_bootstrap(first);
2388 * Size up each available chunk of physical memory.
2390 physmap[0] = PAGE_SIZE; /* mask off page 0 */
2393 phys_avail[pa_indx++] = physmap[0];
2394 phys_avail[pa_indx] = physmap[0];
2395 dump_avail[da_indx] = physmap[0];
2399 * Get dcons buffer address
2401 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
2402 getenv_quad("dcons.size", &dcons_size) == 0)
2407 * physmap is in bytes, so when converting to page boundaries,
2408 * round up the start address and round down the end address.
2410 for (i = 0; i <= physmap_idx; i += 2) {
2413 end = ptoa((vm_paddr_t)Maxmem);
2414 if (physmap[i + 1] < end)
2415 end = trunc_page(physmap[i + 1]);
2416 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
2417 int tmp, page_bad, full;
2418 int *ptr = (int *)CADDR3;
2422 * block out kernel memory as not available.
2424 if (pa >= KERNLOAD && pa < first)
2428 * block out dcons buffer
2431 && pa >= trunc_page(dcons_addr)
2432 && pa < dcons_addr + dcons_size)
2440 * map page into kernel: valid, read/write,non-cacheable
2442 *pte = pa | PG_V | PG_RW | PG_N;
2447 * Test for alternating 1's and 0's
2449 *(volatile int *)ptr = 0xaaaaaaaa;
2450 if (*(volatile int *)ptr != 0xaaaaaaaa)
2453 * Test for alternating 0's and 1's
2455 *(volatile int *)ptr = 0x55555555;
2456 if (*(volatile int *)ptr != 0x55555555)
2461 *(volatile int *)ptr = 0xffffffff;
2462 if (*(volatile int *)ptr != 0xffffffff)
2467 *(volatile int *)ptr = 0x0;
2468 if (*(volatile int *)ptr != 0x0)
2471 * Restore original value.
2477 * Adjust array of valid/good pages.
2479 if (page_bad == TRUE)
2482 * If this good page is a continuation of the
2483 * previous set of good pages, then just increase
2484 * the end pointer. Otherwise start a new chunk.
2485 * Note that "end" points one higher than end,
2486 * making the range >= start and < end.
2487 * If we're also doing a speculative memory
2488 * test and we at or past the end, bump up Maxmem
2489 * so that we keep going. The first bad page
2490 * will terminate the loop.
2492 if (phys_avail[pa_indx] == pa) {
2493 phys_avail[pa_indx] += PAGE_SIZE;
2496 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
2498 "Too many holes in the physical address space, giving up\n");
2503 phys_avail[pa_indx++] = pa; /* start */
2504 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
2508 if (dump_avail[da_indx] == pa) {
2509 dump_avail[da_indx] += PAGE_SIZE;
2512 if (da_indx == DUMP_AVAIL_ARRAY_END) {
2516 dump_avail[da_indx++] = pa; /* start */
2517 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
2527 phys_avail[0] = physfree;
2528 phys_avail[1] = xen_start_info->nr_pages*PAGE_SIZE;
2530 dump_avail[1] = xen_start_info->nr_pages*PAGE_SIZE;
2536 * The last chunk must contain at least one page plus the message
2537 * buffer to avoid complicating other code (message buffer address
2538 * calculation, etc.).
2540 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
2541 round_page(msgbufsize) >= phys_avail[pa_indx]) {
2542 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
2543 phys_avail[pa_indx--] = 0;
2544 phys_avail[pa_indx--] = 0;
2547 Maxmem = atop(phys_avail[pa_indx]);
2549 /* Trim off space for the message buffer. */
2550 phys_avail[pa_indx] -= round_page(msgbufsize);
2552 /* Map the message buffer. */
2553 for (off = 0; off < round_page(msgbufsize); off += PAGE_SIZE)
2554 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
2561 #define MTOPSIZE (1<<(14 + PAGE_SHIFT))
2567 unsigned long gdtmachpfn;
2568 int error, gsel_tss, metadata_missing, x, pa;
2571 struct callback_register event = {
2572 .type = CALLBACKTYPE_event,
2573 .address = {GSEL(GCODE_SEL, SEL_KPL), (unsigned long)Xhypervisor_callback },
2575 struct callback_register failsafe = {
2576 .type = CALLBACKTYPE_failsafe,
2577 .address = {GSEL(GCODE_SEL, SEL_KPL), (unsigned long)failsafe_callback },
2580 thread0.td_kstack = proc0kstack;
2581 thread0.td_kstack_pages = KSTACK_PAGES;
2582 kstack0_sz = thread0.td_kstack_pages * PAGE_SIZE;
2583 thread0.td_pcb = (struct pcb *)(thread0.td_kstack + kstack0_sz) - 1;
2586 * This may be done better later if it gets more high level
2587 * components in it. If so just link td->td_proc here.
2589 proc_linkup0(&proc0, &thread0);
2591 metadata_missing = 0;
2592 if (xen_start_info->mod_start) {
2593 preload_metadata = (caddr_t)xen_start_info->mod_start;
2594 preload_bootstrap_relocate(KERNBASE);
2596 metadata_missing = 1;
2599 kern_envp = static_env;
2600 else if ((caddr_t)xen_start_info->cmd_line)
2601 kern_envp = xen_setbootenv((caddr_t)xen_start_info->cmd_line);
2603 boothowto |= xen_boothowto(kern_envp);
2605 /* Init basic tunables, hz etc */
2609 * XEN occupies a portion of the upper virtual address space
2610 * At its base it manages an array mapping machine page frames
2611 * to physical page frames - hence we need to be able to
2612 * access 4GB - (64MB - 4MB + 64k)
2614 gdt_segs[GPRIV_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2615 gdt_segs[GUFS_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2616 gdt_segs[GUGS_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2617 gdt_segs[GCODE_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2618 gdt_segs[GDATA_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2619 gdt_segs[GUCODE_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2620 gdt_segs[GUDATA_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2621 gdt_segs[GBIOSLOWMEM_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2624 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2625 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2627 PT_SET_MA(gdt, xpmap_ptom(VTOP(gdt)) | PG_V | PG_RW);
2628 bzero(gdt, PAGE_SIZE);
2629 for (x = 0; x < NGDT; x++)
2630 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2632 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2634 gdtmachpfn = vtomach(gdt) >> PAGE_SHIFT;
2635 PT_SET_MA(gdt, xpmap_ptom(VTOP(gdt)) | PG_V);
2636 PANIC_IF(HYPERVISOR_set_gdt(&gdtmachpfn, 512) != 0);
2640 if ((error = HYPERVISOR_set_trap_table(trap_table)) != 0) {
2641 panic("set_trap_table failed - error %d\n", error);
2644 error = HYPERVISOR_callback_op(CALLBACKOP_register, &event);
2646 error = HYPERVISOR_callback_op(CALLBACKOP_register, &failsafe);
2647 #if CONFIG_XEN_COMPAT <= 0x030002
2648 if (error == -ENOXENSYS)
2649 HYPERVISOR_set_callbacks(GSEL(GCODE_SEL, SEL_KPL),
2650 (unsigned long)Xhypervisor_callback,
2651 GSEL(GCODE_SEL, SEL_KPL), (unsigned long)failsafe_callback);
2653 pcpu_init(pc, 0, sizeof(struct pcpu));
2654 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2655 pmap_kenter(pa + KERNBASE, pa);
2656 dpcpu_init((void *)(first + KERNBASE), 0);
2657 first += DPCPU_SIZE;
2658 physfree += DPCPU_SIZE;
2659 init_first += DPCPU_SIZE / PAGE_SIZE;
2661 PCPU_SET(prvspace, pc);
2662 PCPU_SET(curthread, &thread0);
2663 PCPU_SET(curpcb, thread0.td_pcb);
2666 * Initialize mutexes.
2668 * icu_lock: in order to allow an interrupt to occur in a critical
2669 * section, to set pcpu->ipending (etc...) properly, we
2670 * must be able to get the icu lock, so it can't be
2674 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2676 /* make ldt memory segments */
2677 PT_SET_MA(ldt, xpmap_ptom(VTOP(ldt)) | PG_V | PG_RW);
2678 bzero(ldt, PAGE_SIZE);
2679 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2680 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2681 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2682 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2684 default_proc_ldt.ldt_base = (caddr_t)ldt;
2685 default_proc_ldt.ldt_len = 6;
2686 _default_ldt = (int)&default_proc_ldt;
2687 PCPU_SET(currentldt, _default_ldt);
2688 PT_SET_MA(ldt, *vtopte((unsigned long)ldt) & ~PG_RW);
2689 xen_set_ldt((unsigned long) ldt, (sizeof ldt_segs / sizeof ldt_segs[0]));
2691 #if defined(XEN_PRIVILEGED)
2693 * Initialize the i8254 before the console so that console
2694 * initialization can use DELAY().
2700 * Initialize the console before we print anything out.
2704 if (metadata_missing)
2705 printf("WARNING: loader(8) metadata is missing!\n");
2712 /* Reset and mask the atpics and leave them shut down. */
2716 * Point the ICU spurious interrupt vectors at the APIC spurious
2717 * interrupt handler.
2719 setidt(IDT_IO_INTS + 7, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
2720 GSEL(GCODE_SEL, SEL_KPL));
2721 setidt(IDT_IO_INTS + 15, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
2722 GSEL(GCODE_SEL, SEL_KPL));
2727 ksym_start = bootinfo.bi_symtab;
2728 ksym_end = bootinfo.bi_esymtab;
2734 if (boothowto & RB_KDB)
2735 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
2738 finishidentcpu(); /* Final stage of CPU initialization */
2739 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2740 GSEL(GCODE_SEL, SEL_KPL));
2741 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2742 GSEL(GCODE_SEL, SEL_KPL));
2743 initializecpu(); /* Initialize CPU registers */
2745 /* make an initial tss so cpu can get interrupt stack on syscall! */
2746 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2747 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2748 kstack0_sz - sizeof(struct pcb) - 16);
2749 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2750 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2751 HYPERVISOR_stack_switch(GSEL(GDATA_SEL, SEL_KPL),
2752 PCPU_GET(common_tss.tss_esp0));
2754 /* pointer to selector slot for %fs/%gs */
2755 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2757 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2758 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2759 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2760 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2762 dblfault_tss.tss_cr3 = (int)IdlePDPT;
2764 dblfault_tss.tss_cr3 = (int)IdlePTD;
2766 dblfault_tss.tss_eip = (int)dblfault_handler;
2767 dblfault_tss.tss_eflags = PSL_KERNEL;
2768 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2769 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2770 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2771 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2772 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2776 init_param2(physmem);
2778 /* now running on new page tables, configured,and u/iom is accessible */
2780 msgbufinit(msgbufp, msgbufsize);
2781 /* transfer to user mode */
2783 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2784 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2786 /* setup proc 0's pcb */
2787 thread0.td_pcb->pcb_flags = 0;
2789 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
2791 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2793 thread0.td_pcb->pcb_ext = 0;
2794 thread0.td_frame = &proc0_tf;
2795 thread0.td_pcb->pcb_fsd = PCPU_GET(fsgs_gdt)[0];
2796 thread0.td_pcb->pcb_gsd = PCPU_GET(fsgs_gdt)[1];
2806 struct gate_descriptor *gdp;
2807 int gsel_tss, metadata_missing, x, pa;
2811 thread0.td_kstack = proc0kstack;
2812 thread0.td_kstack_pages = KSTACK_PAGES;
2813 kstack0_sz = thread0.td_kstack_pages * PAGE_SIZE;
2814 thread0.td_pcb = (struct pcb *)(thread0.td_kstack + kstack0_sz) - 1;
2817 * This may be done better later if it gets more high level
2818 * components in it. If so just link td->td_proc here.
2820 proc_linkup0(&proc0, &thread0);
2822 metadata_missing = 0;
2823 if (bootinfo.bi_modulep) {
2824 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
2825 preload_bootstrap_relocate(KERNBASE);
2827 metadata_missing = 1;
2830 kern_envp = static_env;
2831 else if (bootinfo.bi_envp)
2832 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
2834 /* Init basic tunables, hz etc */
2838 * Make gdt memory segments. All segments cover the full 4GB
2839 * of address space and permissions are enforced at page level.
2841 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
2842 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
2843 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
2844 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
2845 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
2846 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
2849 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
2850 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2851 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2853 for (x = 0; x < NGDT; x++)
2854 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2856 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2857 r_gdt.rd_base = (int) gdt;
2858 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2861 pcpu_init(pc, 0, sizeof(struct pcpu));
2862 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2863 pmap_kenter(pa + KERNBASE, pa);
2864 dpcpu_init((void *)(first + KERNBASE), 0);
2865 first += DPCPU_SIZE;
2866 PCPU_SET(prvspace, pc);
2867 PCPU_SET(curthread, &thread0);
2868 PCPU_SET(curpcb, thread0.td_pcb);
2871 * Initialize mutexes.
2873 * icu_lock: in order to allow an interrupt to occur in a critical
2874 * section, to set pcpu->ipending (etc...) properly, we
2875 * must be able to get the icu lock, so it can't be
2879 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2881 /* make ldt memory segments */
2882 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2883 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2884 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2885 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2887 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2889 PCPU_SET(currentldt, _default_ldt);
2892 for (x = 0; x < NIDT; x++)
2893 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2894 GSEL(GCODE_SEL, SEL_KPL));
2895 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2896 GSEL(GCODE_SEL, SEL_KPL));
2897 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2898 GSEL(GCODE_SEL, SEL_KPL));
2899 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
2900 GSEL(GCODE_SEL, SEL_KPL));
2901 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2902 GSEL(GCODE_SEL, SEL_KPL));
2903 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2904 GSEL(GCODE_SEL, SEL_KPL));
2905 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2906 GSEL(GCODE_SEL, SEL_KPL));
2907 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2908 GSEL(GCODE_SEL, SEL_KPL));
2909 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2910 , GSEL(GCODE_SEL, SEL_KPL));
2911 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2912 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2913 GSEL(GCODE_SEL, SEL_KPL));
2914 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2915 GSEL(GCODE_SEL, SEL_KPL));
2916 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2917 GSEL(GCODE_SEL, SEL_KPL));
2918 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2919 GSEL(GCODE_SEL, SEL_KPL));
2920 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2921 GSEL(GCODE_SEL, SEL_KPL));
2922 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2923 GSEL(GCODE_SEL, SEL_KPL));
2924 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2925 GSEL(GCODE_SEL, SEL_KPL));
2926 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2927 GSEL(GCODE_SEL, SEL_KPL));
2928 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2929 GSEL(GCODE_SEL, SEL_KPL));
2930 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2931 GSEL(GCODE_SEL, SEL_KPL));
2932 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2933 GSEL(GCODE_SEL, SEL_KPL));
2934 #ifdef KDTRACE_HOOKS
2935 setidt(IDT_DTRACE_RET, &IDTVEC(dtrace_ret), SDT_SYS386TGT, SEL_UPL,
2936 GSEL(GCODE_SEL, SEL_KPL));
2939 setidt(IDT_EVTCHN, &IDTVEC(xen_intr_upcall), SDT_SYS386IGT, SEL_UPL,
2940 GSEL(GCODE_SEL, SEL_KPL));
2943 r_idt.rd_limit = sizeof(idt0) - 1;
2944 r_idt.rd_base = (int) idt;
2949 * The following code queries the PCI ID of 0:0:0. For the XBOX,
2950 * This should be 0x10de / 0x02a5.
2952 * This is exactly what Linux does.
2954 outl(0xcf8, 0x80000000);
2955 if (inl(0xcfc) == 0x02a510de) {
2956 arch_i386_is_xbox = 1;
2957 pic16l_setled(XBOX_LED_GREEN);
2960 * We are an XBOX, but we may have either 64MB or 128MB of
2961 * memory. The PCI host bridge should be programmed for this,
2962 * so we just query it.
2964 outl(0xcf8, 0x80000084);
2965 arch_i386_xbox_memsize = (inl(0xcfc) == 0x7FFFFFF) ? 128 : 64;
2970 * Initialize the i8254 before the console so that console
2971 * initialization can use DELAY().
2976 * Initialize the console before we print anything out.
2980 if (metadata_missing)
2981 printf("WARNING: loader(8) metadata is missing!\n");
2988 /* Reset and mask the atpics and leave them shut down. */
2992 * Point the ICU spurious interrupt vectors at the APIC spurious
2993 * interrupt handler.
2995 setidt(IDT_IO_INTS + 7, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
2996 GSEL(GCODE_SEL, SEL_KPL));
2997 setidt(IDT_IO_INTS + 15, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
2998 GSEL(GCODE_SEL, SEL_KPL));
3003 ksym_start = bootinfo.bi_symtab;
3004 ksym_end = bootinfo.bi_esymtab;
3010 if (boothowto & RB_KDB)
3011 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
3014 finishidentcpu(); /* Final stage of CPU initialization */
3015 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
3016 GSEL(GCODE_SEL, SEL_KPL));
3017 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
3018 GSEL(GCODE_SEL, SEL_KPL));
3019 initializecpu(); /* Initialize CPU registers */
3021 /* make an initial tss so cpu can get interrupt stack on syscall! */
3022 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
3023 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
3024 kstack0_sz - sizeof(struct pcb) - 16);
3025 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
3026 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
3027 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
3028 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
3029 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
3032 /* pointer to selector slot for %fs/%gs */
3033 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
3035 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
3036 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
3037 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
3038 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
3040 dblfault_tss.tss_cr3 = (int)IdlePDPT;
3042 dblfault_tss.tss_cr3 = (int)IdlePTD;
3044 dblfault_tss.tss_eip = (int)dblfault_handler;
3045 dblfault_tss.tss_eflags = PSL_KERNEL;
3046 dblfault_tss.tss_ds = dblfault_tss.tss_es =
3047 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
3048 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
3049 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
3050 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
3054 init_param2(physmem);
3056 /* now running on new page tables, configured,and u/iom is accessible */
3058 msgbufinit(msgbufp, msgbufsize);
3060 /* make a call gate to reenter kernel with */
3061 gdp = &ldt[LSYS5CALLS_SEL].gd;
3063 x = (int) &IDTVEC(lcall_syscall);
3064 gdp->gd_looffset = x;
3065 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
3067 gdp->gd_type = SDT_SYS386CGT;
3068 gdp->gd_dpl = SEL_UPL;
3070 gdp->gd_hioffset = x >> 16;
3072 /* XXX does this work? */
3074 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
3075 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
3077 /* transfer to user mode */
3079 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
3080 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
3082 /* setup proc 0's pcb */
3083 thread0.td_pcb->pcb_flags = 0;
3085 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
3087 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
3089 thread0.td_pcb->pcb_ext = 0;
3090 thread0.td_frame = &proc0_tf;
3101 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
3104 pcpu->pc_acpi_id = 0xffffffff;
3108 smap_sysctl_handler(SYSCTL_HANDLER_ARGS)
3110 struct bios_smap *smapbase;
3111 struct bios_smap_xattr smap;
3114 int count, error, i;
3116 /* Retrieve the system memory map from the loader. */
3117 kmdp = preload_search_by_type("elf kernel");
3119 kmdp = preload_search_by_type("elf32 kernel");
3120 smapbase = (struct bios_smap *)preload_search_info(kmdp,
3121 MODINFO_METADATA | MODINFOMD_SMAP);
3122 if (smapbase == NULL)
3124 smapattr = (uint32_t *)preload_search_info(kmdp,
3125 MODINFO_METADATA | MODINFOMD_SMAP_XATTR);
3126 count = *((u_int32_t *)smapbase - 1) / sizeof(*smapbase);
3128 for (i = 0; i < count; i++) {
3129 smap.base = smapbase[i].base;
3130 smap.length = smapbase[i].length;
3131 smap.type = smapbase[i].type;
3132 if (smapattr != NULL)
3133 smap.xattr = smapattr[i];
3136 error = SYSCTL_OUT(req, &smap, sizeof(smap));
3140 SYSCTL_PROC(_machdep, OID_AUTO, smap, CTLTYPE_OPAQUE|CTLFLAG_RD, NULL, 0,
3141 smap_sysctl_handler, "S,bios_smap_xattr", "Raw BIOS SMAP data");
3144 spinlock_enter(void)
3150 if (td->td_md.md_spinlock_count == 0) {
3151 flags = intr_disable();
3152 td->td_md.md_spinlock_count = 1;
3153 td->td_md.md_saved_flags = flags;
3155 td->td_md.md_spinlock_count++;
3167 flags = td->td_md.md_saved_flags;
3168 td->td_md.md_spinlock_count--;
3169 if (td->td_md.md_spinlock_count == 0)
3170 intr_restore(flags);
3173 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
3174 static void f00f_hack(void *unused);
3175 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
3178 f00f_hack(void *unused)
3180 struct gate_descriptor *new_idt;
3188 printf("Intel Pentium detected, installing workaround for F00F bug\n");
3190 tmp = kmem_malloc(kernel_arena, PAGE_SIZE * 2, M_WAITOK | M_ZERO);
3192 panic("kmem_malloc returned 0");
3194 /* Put the problematic entry (#6) at the end of the lower page. */
3195 new_idt = (struct gate_descriptor*)
3196 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
3197 bcopy(idt, new_idt, sizeof(idt0));
3198 r_idt.rd_base = (u_int)new_idt;
3201 pmap_protect(kernel_pmap, tmp, tmp + PAGE_SIZE, VM_PROT_READ);
3203 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
3206 * Construct a PCB from a trapframe. This is called from kdb_trap() where
3207 * we want to start a backtrace from the function that caused us to enter
3208 * the debugger. We have the context in the trapframe, but base the trace
3209 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
3210 * enough for a backtrace.
3213 makectx(struct trapframe *tf, struct pcb *pcb)
3216 pcb->pcb_edi = tf->tf_edi;
3217 pcb->pcb_esi = tf->tf_esi;
3218 pcb->pcb_ebp = tf->tf_ebp;
3219 pcb->pcb_ebx = tf->tf_ebx;
3220 pcb->pcb_eip = tf->tf_eip;
3221 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
3225 ptrace_set_pc(struct thread *td, u_long addr)
3228 td->td_frame->tf_eip = addr;
3233 ptrace_single_step(struct thread *td)
3235 td->td_frame->tf_eflags |= PSL_T;
3240 ptrace_clear_single_step(struct thread *td)
3242 td->td_frame->tf_eflags &= ~PSL_T;
3247 fill_regs(struct thread *td, struct reg *regs)
3250 struct trapframe *tp;
3254 regs->r_gs = pcb->pcb_gs;
3255 return (fill_frame_regs(tp, regs));
3259 fill_frame_regs(struct trapframe *tp, struct reg *regs)
3261 regs->r_fs = tp->tf_fs;
3262 regs->r_es = tp->tf_es;
3263 regs->r_ds = tp->tf_ds;
3264 regs->r_edi = tp->tf_edi;
3265 regs->r_esi = tp->tf_esi;
3266 regs->r_ebp = tp->tf_ebp;
3267 regs->r_ebx = tp->tf_ebx;
3268 regs->r_edx = tp->tf_edx;
3269 regs->r_ecx = tp->tf_ecx;
3270 regs->r_eax = tp->tf_eax;
3271 regs->r_eip = tp->tf_eip;
3272 regs->r_cs = tp->tf_cs;
3273 regs->r_eflags = tp->tf_eflags;
3274 regs->r_esp = tp->tf_esp;
3275 regs->r_ss = tp->tf_ss;
3280 set_regs(struct thread *td, struct reg *regs)
3283 struct trapframe *tp;
3286 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
3287 !CS_SECURE(regs->r_cs))
3290 tp->tf_fs = regs->r_fs;
3291 tp->tf_es = regs->r_es;
3292 tp->tf_ds = regs->r_ds;
3293 tp->tf_edi = regs->r_edi;
3294 tp->tf_esi = regs->r_esi;
3295 tp->tf_ebp = regs->r_ebp;
3296 tp->tf_ebx = regs->r_ebx;
3297 tp->tf_edx = regs->r_edx;
3298 tp->tf_ecx = regs->r_ecx;
3299 tp->tf_eax = regs->r_eax;
3300 tp->tf_eip = regs->r_eip;
3301 tp->tf_cs = regs->r_cs;
3302 tp->tf_eflags = regs->r_eflags;
3303 tp->tf_esp = regs->r_esp;
3304 tp->tf_ss = regs->r_ss;
3305 pcb->pcb_gs = regs->r_gs;
3309 #ifdef CPU_ENABLE_SSE
3311 fill_fpregs_xmm(sv_xmm, sv_87)
3312 struct savexmm *sv_xmm;
3313 struct save87 *sv_87;
3315 register struct env87 *penv_87 = &sv_87->sv_env;
3316 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
3319 bzero(sv_87, sizeof(*sv_87));
3321 /* FPU control/status */
3322 penv_87->en_cw = penv_xmm->en_cw;
3323 penv_87->en_sw = penv_xmm->en_sw;
3324 penv_87->en_tw = penv_xmm->en_tw;
3325 penv_87->en_fip = penv_xmm->en_fip;
3326 penv_87->en_fcs = penv_xmm->en_fcs;
3327 penv_87->en_opcode = penv_xmm->en_opcode;
3328 penv_87->en_foo = penv_xmm->en_foo;
3329 penv_87->en_fos = penv_xmm->en_fos;
3332 for (i = 0; i < 8; ++i)
3333 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
3337 set_fpregs_xmm(sv_87, sv_xmm)
3338 struct save87 *sv_87;
3339 struct savexmm *sv_xmm;
3341 register struct env87 *penv_87 = &sv_87->sv_env;
3342 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
3345 /* FPU control/status */
3346 penv_xmm->en_cw = penv_87->en_cw;
3347 penv_xmm->en_sw = penv_87->en_sw;
3348 penv_xmm->en_tw = penv_87->en_tw;
3349 penv_xmm->en_fip = penv_87->en_fip;
3350 penv_xmm->en_fcs = penv_87->en_fcs;
3351 penv_xmm->en_opcode = penv_87->en_opcode;
3352 penv_xmm->en_foo = penv_87->en_foo;
3353 penv_xmm->en_fos = penv_87->en_fos;
3356 for (i = 0; i < 8; ++i)
3357 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
3359 #endif /* CPU_ENABLE_SSE */
3362 fill_fpregs(struct thread *td, struct fpreg *fpregs)
3365 KASSERT(td == curthread || TD_IS_SUSPENDED(td) ||
3366 P_SHOULDSTOP(td->td_proc),
3367 ("not suspended thread %p", td));
3371 bzero(fpregs, sizeof(*fpregs));
3373 #ifdef CPU_ENABLE_SSE
3375 fill_fpregs_xmm(&td->td_pcb->pcb_user_save.sv_xmm,
3376 (struct save87 *)fpregs);
3378 #endif /* CPU_ENABLE_SSE */
3379 bcopy(&td->td_pcb->pcb_user_save.sv_87, fpregs,
3385 set_fpregs(struct thread *td, struct fpreg *fpregs)
3388 #ifdef CPU_ENABLE_SSE
3390 set_fpregs_xmm((struct save87 *)fpregs,
3391 &td->td_pcb->pcb_user_save.sv_xmm);
3393 #endif /* CPU_ENABLE_SSE */
3394 bcopy(fpregs, &td->td_pcb->pcb_user_save.sv_87,
3403 * Get machine context.
3406 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
3408 struct trapframe *tp;
3409 struct segment_descriptor *sdp;
3413 PROC_LOCK(curthread->td_proc);
3414 mcp->mc_onstack = sigonstack(tp->tf_esp);
3415 PROC_UNLOCK(curthread->td_proc);
3416 mcp->mc_gs = td->td_pcb->pcb_gs;
3417 mcp->mc_fs = tp->tf_fs;
3418 mcp->mc_es = tp->tf_es;
3419 mcp->mc_ds = tp->tf_ds;
3420 mcp->mc_edi = tp->tf_edi;
3421 mcp->mc_esi = tp->tf_esi;
3422 mcp->mc_ebp = tp->tf_ebp;
3423 mcp->mc_isp = tp->tf_isp;
3424 mcp->mc_eflags = tp->tf_eflags;
3425 if (flags & GET_MC_CLEAR_RET) {
3428 mcp->mc_eflags &= ~PSL_C;
3430 mcp->mc_eax = tp->tf_eax;
3431 mcp->mc_edx = tp->tf_edx;
3433 mcp->mc_ebx = tp->tf_ebx;
3434 mcp->mc_ecx = tp->tf_ecx;
3435 mcp->mc_eip = tp->tf_eip;
3436 mcp->mc_cs = tp->tf_cs;
3437 mcp->mc_esp = tp->tf_esp;
3438 mcp->mc_ss = tp->tf_ss;
3439 mcp->mc_len = sizeof(*mcp);
3440 get_fpcontext(td, mcp);
3441 sdp = &td->td_pcb->pcb_fsd;
3442 mcp->mc_fsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
3443 sdp = &td->td_pcb->pcb_gsd;
3444 mcp->mc_gsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
3446 bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2));
3451 * Set machine context.
3453 * However, we don't set any but the user modifiable flags, and we won't
3454 * touch the cs selector.
3457 set_mcontext(struct thread *td, const mcontext_t *mcp)
3459 struct trapframe *tp;
3463 if (mcp->mc_len != sizeof(*mcp))
3465 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
3466 (tp->tf_eflags & ~PSL_USERCHANGE);
3467 if ((ret = set_fpcontext(td, mcp)) == 0) {
3468 tp->tf_fs = mcp->mc_fs;
3469 tp->tf_es = mcp->mc_es;
3470 tp->tf_ds = mcp->mc_ds;
3471 tp->tf_edi = mcp->mc_edi;
3472 tp->tf_esi = mcp->mc_esi;
3473 tp->tf_ebp = mcp->mc_ebp;
3474 tp->tf_ebx = mcp->mc_ebx;
3475 tp->tf_edx = mcp->mc_edx;
3476 tp->tf_ecx = mcp->mc_ecx;
3477 tp->tf_eax = mcp->mc_eax;
3478 tp->tf_eip = mcp->mc_eip;
3479 tp->tf_eflags = eflags;
3480 tp->tf_esp = mcp->mc_esp;
3481 tp->tf_ss = mcp->mc_ss;
3482 td->td_pcb->pcb_gs = mcp->mc_gs;
3489 get_fpcontext(struct thread *td, mcontext_t *mcp)
3493 mcp->mc_fpformat = _MC_FPFMT_NODEV;
3494 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
3495 bzero(mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
3497 mcp->mc_ownedfp = npxgetregs(td);
3498 bcopy(&td->td_pcb->pcb_user_save, &mcp->mc_fpstate[0],
3499 sizeof(mcp->mc_fpstate));
3500 mcp->mc_fpformat = npxformat();
3505 set_fpcontext(struct thread *td, const mcontext_t *mcp)
3508 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
3510 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
3511 mcp->mc_fpformat != _MC_FPFMT_XMM)
3513 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
3514 /* We don't care what state is left in the FPU or PCB. */
3516 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
3517 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
3519 #ifdef CPU_ENABLE_SSE
3521 ((union savefpu *)&mcp->mc_fpstate)->sv_xmm.sv_env.
3522 en_mxcsr &= cpu_mxcsr_mask;
3524 npxsetregs(td, (union savefpu *)&mcp->mc_fpstate);
3532 fpstate_drop(struct thread *td)
3535 KASSERT(PCB_USER_FPU(td->td_pcb), ("fpstate_drop: kernel-owned fpu"));
3538 if (PCPU_GET(fpcurthread) == td)
3542 * XXX force a full drop of the npx. The above only drops it if we
3543 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
3545 * XXX I don't much like npxgetregs()'s semantics of doing a full
3546 * drop. Dropping only to the pcb matches fnsave's behaviour.
3547 * We only need to drop to !PCB_INITDONE in sendsig(). But
3548 * sendsig() is the only caller of npxgetregs()... perhaps we just
3549 * have too many layers.
3551 curthread->td_pcb->pcb_flags &= ~(PCB_NPXINITDONE |
3552 PCB_NPXUSERINITDONE);
3557 fill_dbregs(struct thread *td, struct dbreg *dbregs)
3562 dbregs->dr[0] = rdr0();
3563 dbregs->dr[1] = rdr1();
3564 dbregs->dr[2] = rdr2();
3565 dbregs->dr[3] = rdr3();
3566 dbregs->dr[4] = rdr4();
3567 dbregs->dr[5] = rdr5();
3568 dbregs->dr[6] = rdr6();
3569 dbregs->dr[7] = rdr7();
3572 dbregs->dr[0] = pcb->pcb_dr0;
3573 dbregs->dr[1] = pcb->pcb_dr1;
3574 dbregs->dr[2] = pcb->pcb_dr2;
3575 dbregs->dr[3] = pcb->pcb_dr3;
3578 dbregs->dr[6] = pcb->pcb_dr6;
3579 dbregs->dr[7] = pcb->pcb_dr7;
3585 set_dbregs(struct thread *td, struct dbreg *dbregs)
3591 load_dr0(dbregs->dr[0]);
3592 load_dr1(dbregs->dr[1]);
3593 load_dr2(dbregs->dr[2]);
3594 load_dr3(dbregs->dr[3]);
3595 load_dr4(dbregs->dr[4]);
3596 load_dr5(dbregs->dr[5]);
3597 load_dr6(dbregs->dr[6]);
3598 load_dr7(dbregs->dr[7]);
3601 * Don't let an illegal value for dr7 get set. Specifically,
3602 * check for undefined settings. Setting these bit patterns
3603 * result in undefined behaviour and can lead to an unexpected
3606 for (i = 0; i < 4; i++) {
3607 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
3609 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
3616 * Don't let a process set a breakpoint that is not within the
3617 * process's address space. If a process could do this, it
3618 * could halt the system by setting a breakpoint in the kernel
3619 * (if ddb was enabled). Thus, we need to check to make sure
3620 * that no breakpoints are being enabled for addresses outside
3621 * process's address space.
3623 * XXX - what about when the watched area of the user's
3624 * address space is written into from within the kernel
3625 * ... wouldn't that still cause a breakpoint to be generated
3626 * from within kernel mode?
3629 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
3630 /* dr0 is enabled */
3631 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
3635 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
3636 /* dr1 is enabled */
3637 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
3641 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
3642 /* dr2 is enabled */
3643 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
3647 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
3648 /* dr3 is enabled */
3649 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
3653 pcb->pcb_dr0 = dbregs->dr[0];
3654 pcb->pcb_dr1 = dbregs->dr[1];
3655 pcb->pcb_dr2 = dbregs->dr[2];
3656 pcb->pcb_dr3 = dbregs->dr[3];
3657 pcb->pcb_dr6 = dbregs->dr[6];
3658 pcb->pcb_dr7 = dbregs->dr[7];
3660 pcb->pcb_flags |= PCB_DBREGS;
3667 * Return > 0 if a hardware breakpoint has been hit, and the
3668 * breakpoint was in user space. Return 0, otherwise.
3671 user_dbreg_trap(void)
3673 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
3674 u_int32_t bp; /* breakpoint bits extracted from dr6 */
3675 int nbp; /* number of breakpoints that triggered */
3676 caddr_t addr[4]; /* breakpoint addresses */
3680 if ((dr7 & 0x000000ff) == 0) {
3682 * all GE and LE bits in the dr7 register are zero,
3683 * thus the trap couldn't have been caused by the
3684 * hardware debug registers
3691 bp = dr6 & 0x0000000f;
3695 * None of the breakpoint bits are set meaning this
3696 * trap was not caused by any of the debug registers
3702 * at least one of the breakpoints were hit, check to see
3703 * which ones and if any of them are user space addresses
3707 addr[nbp++] = (caddr_t)rdr0();
3710 addr[nbp++] = (caddr_t)rdr1();
3713 addr[nbp++] = (caddr_t)rdr2();
3716 addr[nbp++] = (caddr_t)rdr3();
3719 for (i = 0; i < nbp; i++) {
3720 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
3722 * addr[i] is in user space
3729 * None of the breakpoints are in user space.
3737 * Provide inb() and outb() as functions. They are normally only available as
3738 * inline functions, thus cannot be called from the debugger.
3741 /* silence compiler warnings */
3742 u_char inb_(u_short);
3743 void outb_(u_short, u_char);
3752 outb_(u_short port, u_char data)