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_compat.h"
51 #include "opt_kstack_pages.h"
52 #include "opt_maxmem.h"
53 #include "opt_msgbuf.h"
55 #include "opt_perfmon.h"
58 #include <sys/param.h>
60 #include <sys/systm.h>
64 #include <sys/callout.h>
67 #include <sys/eventhandler.h>
69 #include <sys/imgact.h>
71 #include <sys/kernel.h>
73 #include <sys/linker.h>
75 #include <sys/malloc.h>
76 #include <sys/memrange.h>
77 #include <sys/msgbuf.h>
78 #include <sys/mutex.h>
80 #include <sys/ptrace.h>
81 #include <sys/reboot.h>
82 #include <sys/sched.h>
83 #include <sys/signalvar.h>
84 #include <sys/sysctl.h>
85 #include <sys/sysent.h>
86 #include <sys/sysproto.h>
87 #include <sys/ucontext.h>
88 #include <sys/vmmeter.h>
91 #include <vm/vm_extern.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_object.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vm_param.h>
101 #error KDB must be enabled in order for DDB to work!
104 #include <ddb/db_sym.h>
109 #include <net/netisr.h>
111 #include <machine/bootinfo.h>
112 #include <machine/clock.h>
113 #include <machine/cpu.h>
114 #include <machine/cputypes.h>
115 #include <machine/intr_machdep.h>
116 #include <machine/mca.h>
117 #include <machine/md_var.h>
118 #include <machine/metadata.h>
119 #include <machine/pc/bios.h>
120 #include <machine/pcb.h>
121 #include <machine/pcb_ext.h>
122 #include <machine/proc.h>
123 #include <machine/reg.h>
124 #include <machine/sigframe.h>
125 #include <machine/specialreg.h>
126 #include <machine/vm86.h>
128 #include <machine/perfmon.h>
131 #include <machine/smp.h>
135 #include <i386/isa/icu.h>
139 #include <machine/xbox.h>
141 int arch_i386_is_xbox = 0;
142 uint32_t arch_i386_xbox_memsize = 0;
147 #include <machine/xen/xen-os.h>
148 #include <xen/hypervisor.h>
149 #include <machine/xen/xen-os.h>
150 #include <machine/xen/xenvar.h>
151 #include <machine/xen/xenfunc.h>
152 #include <xen/xen_intr.h>
154 void Xhypervisor_callback(void);
155 void failsafe_callback(void);
157 extern trap_info_t trap_table[];
158 struct proc_ldt default_proc_ldt;
159 extern int init_first;
161 extern unsigned long physfree;
164 /* Sanity check for __curthread() */
165 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
167 extern void init386(int first);
168 extern void dblfault_handler(void);
170 extern void printcpuinfo(void); /* XXX header file */
171 extern void finishidentcpu(void);
172 extern void panicifcpuunsupported(void);
173 extern void initializecpu(void);
175 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
176 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
178 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
179 #define CPU_ENABLE_SSE
182 static void cpu_startup(void *);
183 static void fpstate_drop(struct thread *td);
184 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
185 static int set_fpcontext(struct thread *td, const mcontext_t *mcp);
186 #ifdef CPU_ENABLE_SSE
187 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
188 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
189 #endif /* CPU_ENABLE_SSE */
190 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
193 extern vm_offset_t ksym_start, ksym_end;
196 /* Intel ICH registers */
197 #define ICH_PMBASE 0x400
198 #define ICH_SMI_EN ICH_PMBASE + 0x30
200 int _udatasel, _ucodesel;
206 static void osendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
208 #ifdef COMPAT_FREEBSD4
209 static void freebsd4_sendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
216 FEATURE(pae, "Physical Address Extensions");
220 * The number of PHYSMAP entries must be one less than the number of
221 * PHYSSEG entries because the PHYSMAP entry that spans the largest
222 * physical address that is accessible by ISA DMA is split into two
225 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
227 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
228 vm_paddr_t dump_avail[PHYSMAP_SIZE + 2];
230 /* must be 2 less so 0 0 can signal end of chunks */
231 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(phys_avail[0])) - 2)
232 #define DUMP_AVAIL_ARRAY_END ((sizeof(dump_avail) / sizeof(dump_avail[0])) - 2)
234 struct kva_md_info kmi;
236 static struct trapframe proc0_tf;
237 struct pcpu __pcpu[MAXCPU];
241 struct mem_range_softc mem_range_softc;
251 * On MacBooks, we need to disallow the legacy USB circuit to
252 * generate an SMI# because this can cause several problems,
253 * namely: incorrect CPU frequency detection and failure to
255 * We do this by disabling a bit in the SMI_EN (SMI Control and
256 * Enable register) of the Intel ICH LPC Interface Bridge.
258 sysenv = getenv("smbios.system.product");
259 if (sysenv != NULL) {
260 if (strncmp(sysenv, "MacBook1,1", 10) == 0 ||
261 strncmp(sysenv, "MacBook3,1", 10) == 0 ||
262 strncmp(sysenv, "MacBookPro1,1", 13) == 0 ||
263 strncmp(sysenv, "MacBookPro1,2", 13) == 0 ||
264 strncmp(sysenv, "MacBookPro3,1", 13) == 0 ||
265 strncmp(sysenv, "Macmini1,1", 10) == 0) {
267 printf("Disabling LEGACY_USB_EN bit on "
269 outl(ICH_SMI_EN, inl(ICH_SMI_EN) & ~0x8);
275 * Good {morning,afternoon,evening,night}.
279 panicifcpuunsupported();
286 * Display physical memory if SMBIOS reports reasonable amount.
289 sysenv = getenv("smbios.memory.enabled");
290 if (sysenv != NULL) {
291 memsize = (uintmax_t)strtoul(sysenv, (char **)NULL, 10) << 10;
294 if (memsize < ptoa((uintmax_t)cnt.v_free_count))
295 memsize = ptoa((uintmax_t)Maxmem);
296 printf("real memory = %ju (%ju MB)\n", memsize, memsize >> 20);
299 * Display any holes after the first chunk of extended memory.
304 printf("Physical memory chunk(s):\n");
305 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
308 size = phys_avail[indx + 1] - phys_avail[indx];
310 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
311 (uintmax_t)phys_avail[indx],
312 (uintmax_t)phys_avail[indx + 1] - 1,
313 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
317 vm_ksubmap_init(&kmi);
319 printf("avail memory = %ju (%ju MB)\n",
320 ptoa((uintmax_t)cnt.v_free_count),
321 ptoa((uintmax_t)cnt.v_free_count) / 1048576);
324 * Set up buffers, so they can be used to read disk labels.
327 vm_pager_bufferinit();
335 * Send an interrupt to process.
337 * Stack is set up to allow sigcode stored
338 * at top to call routine, followed by kcall
339 * to sigreturn routine below. After sigreturn
340 * resets the signal mask, the stack, and the
341 * frame pointer, it returns to the user
346 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
348 struct osigframe sf, *fp;
352 struct trapframe *regs;
358 PROC_LOCK_ASSERT(p, MA_OWNED);
359 sig = ksi->ksi_signo;
361 mtx_assert(&psp->ps_mtx, MA_OWNED);
363 oonstack = sigonstack(regs->tf_esp);
365 /* Allocate space for the signal handler context. */
366 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
367 SIGISMEMBER(psp->ps_sigonstack, sig)) {
368 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
369 td->td_sigstk.ss_size - sizeof(struct osigframe));
370 #if defined(COMPAT_43)
371 td->td_sigstk.ss_flags |= SS_ONSTACK;
374 fp = (struct osigframe *)regs->tf_esp - 1;
376 /* Translate the signal if appropriate. */
377 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
378 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
380 /* Build the argument list for the signal handler. */
382 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
383 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
384 /* Signal handler installed with SA_SIGINFO. */
385 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
386 sf.sf_siginfo.si_signo = sig;
387 sf.sf_siginfo.si_code = ksi->ksi_code;
388 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
390 /* Old FreeBSD-style arguments. */
391 sf.sf_arg2 = ksi->ksi_code;
392 sf.sf_addr = (register_t)ksi->ksi_addr;
393 sf.sf_ahu.sf_handler = catcher;
395 mtx_unlock(&psp->ps_mtx);
398 /* Save most if not all of trap frame. */
399 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
400 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
401 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
402 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
403 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
404 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
405 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
406 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
407 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
408 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
409 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
410 sf.sf_siginfo.si_sc.sc_gs = rgs();
411 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
413 /* Build the signal context to be used by osigreturn(). */
414 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
415 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
416 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
417 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
418 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
419 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
420 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
421 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
424 * If we're a vm86 process, we want to save the segment registers.
425 * We also change eflags to be our emulated eflags, not the actual
428 if (regs->tf_eflags & PSL_VM) {
429 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
430 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
431 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
433 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
434 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
435 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
436 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
438 if (vm86->vm86_has_vme == 0)
439 sf.sf_siginfo.si_sc.sc_ps =
440 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
441 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
443 /* See sendsig() for comments. */
444 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
448 * Copy the sigframe out to the user's stack.
450 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
452 printf("process %ld has trashed its stack\n", (long)p->p_pid);
458 regs->tf_esp = (int)fp;
459 regs->tf_eip = PS_STRINGS - szosigcode;
460 regs->tf_eflags &= ~(PSL_T | PSL_D);
461 regs->tf_cs = _ucodesel;
462 regs->tf_ds = _udatasel;
463 regs->tf_es = _udatasel;
464 regs->tf_fs = _udatasel;
466 regs->tf_ss = _udatasel;
468 mtx_lock(&psp->ps_mtx);
470 #endif /* COMPAT_43 */
472 #ifdef COMPAT_FREEBSD4
474 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
476 struct sigframe4 sf, *sfp;
480 struct trapframe *regs;
486 PROC_LOCK_ASSERT(p, MA_OWNED);
487 sig = ksi->ksi_signo;
489 mtx_assert(&psp->ps_mtx, MA_OWNED);
491 oonstack = sigonstack(regs->tf_esp);
493 /* Save user context. */
494 bzero(&sf, sizeof(sf));
495 sf.sf_uc.uc_sigmask = *mask;
496 sf.sf_uc.uc_stack = td->td_sigstk;
497 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
498 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
499 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
500 sf.sf_uc.uc_mcontext.mc_gs = rgs();
501 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
503 /* Allocate space for the signal handler context. */
504 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
505 SIGISMEMBER(psp->ps_sigonstack, sig)) {
506 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
507 td->td_sigstk.ss_size - sizeof(struct sigframe4));
508 #if defined(COMPAT_43)
509 td->td_sigstk.ss_flags |= SS_ONSTACK;
512 sfp = (struct sigframe4 *)regs->tf_esp - 1;
514 /* Translate the signal if appropriate. */
515 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
516 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
518 /* Build the argument list for the signal handler. */
520 sf.sf_ucontext = (register_t)&sfp->sf_uc;
521 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
522 /* Signal handler installed with SA_SIGINFO. */
523 sf.sf_siginfo = (register_t)&sfp->sf_si;
524 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
526 /* Fill in POSIX parts */
527 sf.sf_si.si_signo = sig;
528 sf.sf_si.si_code = ksi->ksi_code;
529 sf.sf_si.si_addr = ksi->ksi_addr;
531 /* Old FreeBSD-style arguments. */
532 sf.sf_siginfo = ksi->ksi_code;
533 sf.sf_addr = (register_t)ksi->ksi_addr;
534 sf.sf_ahu.sf_handler = catcher;
536 mtx_unlock(&psp->ps_mtx);
540 * If we're a vm86 process, we want to save the segment registers.
541 * We also change eflags to be our emulated eflags, not the actual
544 if (regs->tf_eflags & PSL_VM) {
545 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
546 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
548 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
549 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
550 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
551 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
553 if (vm86->vm86_has_vme == 0)
554 sf.sf_uc.uc_mcontext.mc_eflags =
555 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
556 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
559 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
560 * syscalls made by the signal handler. This just avoids
561 * wasting time for our lazy fixup of such faults. PSL_NT
562 * does nothing in vm86 mode, but vm86 programs can set it
563 * almost legitimately in probes for old cpu types.
565 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
569 * Copy the sigframe out to the user's stack.
571 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
573 printf("process %ld has trashed its stack\n", (long)p->p_pid);
579 regs->tf_esp = (int)sfp;
580 regs->tf_eip = PS_STRINGS - szfreebsd4_sigcode;
581 regs->tf_eflags &= ~(PSL_T | PSL_D);
582 regs->tf_cs = _ucodesel;
583 regs->tf_ds = _udatasel;
584 regs->tf_es = _udatasel;
585 regs->tf_fs = _udatasel;
586 regs->tf_ss = _udatasel;
588 mtx_lock(&psp->ps_mtx);
590 #endif /* COMPAT_FREEBSD4 */
593 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
595 struct sigframe sf, *sfp;
600 struct trapframe *regs;
601 struct segment_descriptor *sdp;
607 PROC_LOCK_ASSERT(p, MA_OWNED);
608 sig = ksi->ksi_signo;
610 mtx_assert(&psp->ps_mtx, MA_OWNED);
611 #ifdef COMPAT_FREEBSD4
612 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
613 freebsd4_sendsig(catcher, ksi, mask);
618 if (SIGISMEMBER(psp->ps_osigset, sig)) {
619 osendsig(catcher, ksi, mask);
624 oonstack = sigonstack(regs->tf_esp);
626 /* Save user context. */
627 bzero(&sf, sizeof(sf));
628 sf.sf_uc.uc_sigmask = *mask;
629 sf.sf_uc.uc_stack = td->td_sigstk;
630 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
631 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
632 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
633 sf.sf_uc.uc_mcontext.mc_gs = rgs();
634 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
635 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
636 get_fpcontext(td, &sf.sf_uc.uc_mcontext);
639 * Unconditionally fill the fsbase and gsbase into the mcontext.
641 sdp = &td->td_pcb->pcb_gsd;
642 sf.sf_uc.uc_mcontext.mc_fsbase = sdp->sd_hibase << 24 |
644 sdp = &td->td_pcb->pcb_fsd;
645 sf.sf_uc.uc_mcontext.mc_gsbase = sdp->sd_hibase << 24 |
648 /* Allocate space for the signal handler context. */
649 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
650 SIGISMEMBER(psp->ps_sigonstack, sig)) {
651 sp = td->td_sigstk.ss_sp +
652 td->td_sigstk.ss_size - sizeof(struct sigframe);
653 #if defined(COMPAT_43)
654 td->td_sigstk.ss_flags |= SS_ONSTACK;
657 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
658 /* Align to 16 bytes. */
659 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
661 /* Translate the signal if appropriate. */
662 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
663 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
665 /* Build the argument list for the signal handler. */
667 sf.sf_ucontext = (register_t)&sfp->sf_uc;
668 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
669 /* Signal handler installed with SA_SIGINFO. */
670 sf.sf_siginfo = (register_t)&sfp->sf_si;
671 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
673 /* Fill in POSIX parts */
674 sf.sf_si = ksi->ksi_info;
675 sf.sf_si.si_signo = sig; /* maybe a translated signal */
677 /* Old FreeBSD-style arguments. */
678 sf.sf_siginfo = ksi->ksi_code;
679 sf.sf_addr = (register_t)ksi->ksi_addr;
680 sf.sf_ahu.sf_handler = catcher;
682 mtx_unlock(&psp->ps_mtx);
686 * If we're a vm86 process, we want to save the segment registers.
687 * We also change eflags to be our emulated eflags, not the actual
690 if (regs->tf_eflags & PSL_VM) {
691 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
692 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
694 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
695 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
696 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
697 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
699 if (vm86->vm86_has_vme == 0)
700 sf.sf_uc.uc_mcontext.mc_eflags =
701 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
702 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
705 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
706 * syscalls made by the signal handler. This just avoids
707 * wasting time for our lazy fixup of such faults. PSL_NT
708 * does nothing in vm86 mode, but vm86 programs can set it
709 * almost legitimately in probes for old cpu types.
711 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
715 * Copy the sigframe out to the user's stack.
717 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
719 printf("process %ld has trashed its stack\n", (long)p->p_pid);
725 regs->tf_esp = (int)sfp;
726 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
727 regs->tf_eflags &= ~(PSL_T | PSL_D);
728 regs->tf_cs = _ucodesel;
729 regs->tf_ds = _udatasel;
730 regs->tf_es = _udatasel;
731 regs->tf_fs = _udatasel;
732 regs->tf_ss = _udatasel;
734 mtx_lock(&psp->ps_mtx);
738 * System call to cleanup state after a signal
739 * has been taken. Reset signal mask and
740 * stack state from context left by sendsig (above).
741 * Return to previous pc and psl as specified by
742 * context left by sendsig. Check carefully to
743 * make sure that the user has not modified the
744 * state to gain improper privileges.
752 struct osigreturn_args /* {
753 struct osigcontext *sigcntxp;
756 struct osigcontext sc;
757 struct trapframe *regs;
758 struct osigcontext *scp;
763 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
768 if (eflags & PSL_VM) {
769 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
770 struct vm86_kernel *vm86;
773 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
774 * set up the vm86 area, and we can't enter vm86 mode.
776 if (td->td_pcb->pcb_ext == 0)
778 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
779 if (vm86->vm86_inited == 0)
782 /* Go back to user mode if both flags are set. */
783 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
784 ksiginfo_init_trap(&ksi);
785 ksi.ksi_signo = SIGBUS;
786 ksi.ksi_code = BUS_OBJERR;
787 ksi.ksi_addr = (void *)regs->tf_eip;
788 trapsignal(td, &ksi);
791 if (vm86->vm86_has_vme) {
792 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
793 (eflags & VME_USERCHANGE) | PSL_VM;
795 vm86->vm86_eflags = eflags; /* save VIF, VIP */
796 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
797 (eflags & VM_USERCHANGE) | PSL_VM;
799 tf->tf_vm86_ds = scp->sc_ds;
800 tf->tf_vm86_es = scp->sc_es;
801 tf->tf_vm86_fs = scp->sc_fs;
802 tf->tf_vm86_gs = scp->sc_gs;
803 tf->tf_ds = _udatasel;
804 tf->tf_es = _udatasel;
805 tf->tf_fs = _udatasel;
808 * Don't allow users to change privileged or reserved flags.
811 * XXX do allow users to change the privileged flag PSL_RF.
812 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
813 * should sometimes set it there too. tf_eflags is kept in
814 * the signal context during signal handling and there is no
815 * other place to remember it, so the PSL_RF bit may be
816 * corrupted by the signal handler without us knowing.
817 * Corruption of the PSL_RF bit at worst causes one more or
818 * one less debugger trap, so allowing it is fairly harmless.
820 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
825 * Don't allow users to load a valid privileged %cs. Let the
826 * hardware check for invalid selectors, excess privilege in
827 * other selectors, invalid %eip's and invalid %esp's.
829 if (!CS_SECURE(scp->sc_cs)) {
830 ksiginfo_init_trap(&ksi);
831 ksi.ksi_signo = SIGBUS;
832 ksi.ksi_code = BUS_OBJERR;
833 ksi.ksi_trapno = T_PROTFLT;
834 ksi.ksi_addr = (void *)regs->tf_eip;
835 trapsignal(td, &ksi);
838 regs->tf_ds = scp->sc_ds;
839 regs->tf_es = scp->sc_es;
840 regs->tf_fs = scp->sc_fs;
843 /* Restore remaining registers. */
844 regs->tf_eax = scp->sc_eax;
845 regs->tf_ebx = scp->sc_ebx;
846 regs->tf_ecx = scp->sc_ecx;
847 regs->tf_edx = scp->sc_edx;
848 regs->tf_esi = scp->sc_esi;
849 regs->tf_edi = scp->sc_edi;
850 regs->tf_cs = scp->sc_cs;
851 regs->tf_ss = scp->sc_ss;
852 regs->tf_isp = scp->sc_isp;
853 regs->tf_ebp = scp->sc_fp;
854 regs->tf_esp = scp->sc_sp;
855 regs->tf_eip = scp->sc_pc;
856 regs->tf_eflags = eflags;
858 #if defined(COMPAT_43)
859 if (scp->sc_onstack & 1)
860 td->td_sigstk.ss_flags |= SS_ONSTACK;
862 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
864 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
866 return (EJUSTRETURN);
868 #endif /* COMPAT_43 */
870 #ifdef COMPAT_FREEBSD4
875 freebsd4_sigreturn(td, uap)
877 struct freebsd4_sigreturn_args /* {
878 const ucontext4 *sigcntxp;
882 struct trapframe *regs;
883 struct ucontext4 *ucp;
884 int cs, eflags, error;
887 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
892 eflags = ucp->uc_mcontext.mc_eflags;
893 if (eflags & PSL_VM) {
894 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
895 struct vm86_kernel *vm86;
898 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
899 * set up the vm86 area, and we can't enter vm86 mode.
901 if (td->td_pcb->pcb_ext == 0)
903 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
904 if (vm86->vm86_inited == 0)
907 /* Go back to user mode if both flags are set. */
908 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
909 ksiginfo_init_trap(&ksi);
910 ksi.ksi_signo = SIGBUS;
911 ksi.ksi_code = BUS_OBJERR;
912 ksi.ksi_addr = (void *)regs->tf_eip;
913 trapsignal(td, &ksi);
915 if (vm86->vm86_has_vme) {
916 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
917 (eflags & VME_USERCHANGE) | PSL_VM;
919 vm86->vm86_eflags = eflags; /* save VIF, VIP */
920 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
921 (eflags & VM_USERCHANGE) | PSL_VM;
923 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
924 tf->tf_eflags = eflags;
925 tf->tf_vm86_ds = tf->tf_ds;
926 tf->tf_vm86_es = tf->tf_es;
927 tf->tf_vm86_fs = tf->tf_fs;
928 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
929 tf->tf_ds = _udatasel;
930 tf->tf_es = _udatasel;
931 tf->tf_fs = _udatasel;
934 * Don't allow users to change privileged or reserved flags.
937 * XXX do allow users to change the privileged flag PSL_RF.
938 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
939 * should sometimes set it there too. tf_eflags is kept in
940 * the signal context during signal handling and there is no
941 * other place to remember it, so the PSL_RF bit may be
942 * corrupted by the signal handler without us knowing.
943 * Corruption of the PSL_RF bit at worst causes one more or
944 * one less debugger trap, so allowing it is fairly harmless.
946 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
947 uprintf("pid %d (%s): freebsd4_sigreturn eflags = 0x%x\n",
948 td->td_proc->p_pid, td->td_name, eflags);
953 * Don't allow users to load a valid privileged %cs. Let the
954 * hardware check for invalid selectors, excess privilege in
955 * other selectors, invalid %eip's and invalid %esp's.
957 cs = ucp->uc_mcontext.mc_cs;
958 if (!CS_SECURE(cs)) {
959 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
960 td->td_proc->p_pid, td->td_name, cs);
961 ksiginfo_init_trap(&ksi);
962 ksi.ksi_signo = SIGBUS;
963 ksi.ksi_code = BUS_OBJERR;
964 ksi.ksi_trapno = T_PROTFLT;
965 ksi.ksi_addr = (void *)regs->tf_eip;
966 trapsignal(td, &ksi);
970 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
973 #if defined(COMPAT_43)
974 if (ucp->uc_mcontext.mc_onstack & 1)
975 td->td_sigstk.ss_flags |= SS_ONSTACK;
977 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
979 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
980 return (EJUSTRETURN);
982 #endif /* COMPAT_FREEBSD4 */
990 struct sigreturn_args /* {
991 const struct __ucontext *sigcntxp;
995 struct trapframe *regs;
997 int cs, eflags, error, ret;
1000 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
1004 regs = td->td_frame;
1005 eflags = ucp->uc_mcontext.mc_eflags;
1006 if (eflags & PSL_VM) {
1007 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
1008 struct vm86_kernel *vm86;
1011 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
1012 * set up the vm86 area, and we can't enter vm86 mode.
1014 if (td->td_pcb->pcb_ext == 0)
1016 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
1017 if (vm86->vm86_inited == 0)
1020 /* Go back to user mode if both flags are set. */
1021 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
1022 ksiginfo_init_trap(&ksi);
1023 ksi.ksi_signo = SIGBUS;
1024 ksi.ksi_code = BUS_OBJERR;
1025 ksi.ksi_addr = (void *)regs->tf_eip;
1026 trapsignal(td, &ksi);
1029 if (vm86->vm86_has_vme) {
1030 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
1031 (eflags & VME_USERCHANGE) | PSL_VM;
1033 vm86->vm86_eflags = eflags; /* save VIF, VIP */
1034 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
1035 (eflags & VM_USERCHANGE) | PSL_VM;
1037 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
1038 tf->tf_eflags = eflags;
1039 tf->tf_vm86_ds = tf->tf_ds;
1040 tf->tf_vm86_es = tf->tf_es;
1041 tf->tf_vm86_fs = tf->tf_fs;
1042 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
1043 tf->tf_ds = _udatasel;
1044 tf->tf_es = _udatasel;
1045 tf->tf_fs = _udatasel;
1048 * Don't allow users to change privileged or reserved flags.
1051 * XXX do allow users to change the privileged flag PSL_RF.
1052 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
1053 * should sometimes set it there too. tf_eflags is kept in
1054 * the signal context during signal handling and there is no
1055 * other place to remember it, so the PSL_RF bit may be
1056 * corrupted by the signal handler without us knowing.
1057 * Corruption of the PSL_RF bit at worst causes one more or
1058 * one less debugger trap, so allowing it is fairly harmless.
1060 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
1061 uprintf("pid %d (%s): sigreturn eflags = 0x%x\n",
1062 td->td_proc->p_pid, td->td_name, eflags);
1067 * Don't allow users to load a valid privileged %cs. Let the
1068 * hardware check for invalid selectors, excess privilege in
1069 * other selectors, invalid %eip's and invalid %esp's.
1071 cs = ucp->uc_mcontext.mc_cs;
1072 if (!CS_SECURE(cs)) {
1073 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
1074 td->td_proc->p_pid, td->td_name, cs);
1075 ksiginfo_init_trap(&ksi);
1076 ksi.ksi_signo = SIGBUS;
1077 ksi.ksi_code = BUS_OBJERR;
1078 ksi.ksi_trapno = T_PROTFLT;
1079 ksi.ksi_addr = (void *)regs->tf_eip;
1080 trapsignal(td, &ksi);
1084 ret = set_fpcontext(td, &ucp->uc_mcontext);
1087 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1090 #if defined(COMPAT_43)
1091 if (ucp->uc_mcontext.mc_onstack & 1)
1092 td->td_sigstk.ss_flags |= SS_ONSTACK;
1094 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1097 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
1098 return (EJUSTRETURN);
1102 * Machine dependent boot() routine
1104 * I haven't seen anything to put here yet
1105 * Possibly some stuff might be grafted back here from boot()
1113 * Flush the D-cache for non-DMA I/O so that the I-cache can
1114 * be made coherent later.
1117 cpu_flush_dcache(void *ptr, size_t len)
1119 /* Not applicable */
1122 /* Get current clock frequency for the given cpu id. */
1124 cpu_est_clockrate(int cpu_id, uint64_t *rate)
1127 uint64_t tsc1, tsc2;
1129 if (pcpu_find(cpu_id) == NULL || rate == NULL)
1132 return (EOPNOTSUPP);
1134 /* If we're booting, trust the rate calibrated moments ago. */
1141 /* Schedule ourselves on the indicated cpu. */
1142 thread_lock(curthread);
1143 sched_bind(curthread, cpu_id);
1144 thread_unlock(curthread);
1147 /* Calibrate by measuring a short delay. */
1148 reg = intr_disable();
1155 thread_lock(curthread);
1156 sched_unbind(curthread);
1157 thread_unlock(curthread);
1161 * Calculate the difference in readings, convert to Mhz, and
1162 * subtract 0.5% of the total. Empirical testing has shown that
1163 * overhead in DELAY() works out to approximately this value.
1166 *rate = tsc2 * 1000 - tsc2 * 5;
1171 void (*cpu_idle_hook)(void) = NULL; /* ACPI idle hook. */
1178 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
1181 int scheduler_running;
1184 cpu_idle_hlt(int busy)
1187 scheduler_running = 1;
1194 * Shutdown the CPU as much as possible
1204 cpu_idle_hlt(int busy)
1207 * we must absolutely guarentee that hlt is the next instruction
1208 * after sti or we introduce a timing window.
1211 if (sched_runnable())
1214 __asm __volatile("sti; hlt");
1219 cpu_idle_acpi(int busy)
1222 if (sched_runnable())
1224 else if (cpu_idle_hook)
1227 __asm __volatile("sti; hlt");
1230 static int cpu_ident_amdc1e = 0;
1233 cpu_probe_amdc1e(void)
1239 * Forget it, if we're not using local APIC timer.
1241 if (resource_disabled("apic", 0) ||
1242 (resource_int_value("apic", 0, "clock", &i) == 0 && i == 0))
1246 * Detect the presence of C1E capability mostly on latest
1247 * dual-cores (or future) k8 family.
1249 if (cpu_vendor_id == CPU_VENDOR_AMD &&
1250 (cpu_id & 0x00000f00) == 0x00000f00 &&
1251 (cpu_id & 0x0fff0000) >= 0x00040000) {
1252 cpu_ident_amdc1e = 1;
1260 * C1E renders the local APIC timer dead, so we disable it by
1261 * reading the Interrupt Pending Message register and clearing
1262 * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
1265 * "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors"
1266 * #32559 revision 3.00+
1268 #define MSR_AMDK8_IPM 0xc0010055
1269 #define AMDK8_SMIONCMPHALT (1ULL << 27)
1270 #define AMDK8_C1EONCMPHALT (1ULL << 28)
1271 #define AMDK8_CMPHALT (AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT)
1274 cpu_idle_amdc1e(int busy)
1278 if (sched_runnable())
1283 msr = rdmsr(MSR_AMDK8_IPM);
1284 if (msr & AMDK8_CMPHALT)
1285 wrmsr(MSR_AMDK8_IPM, msr & ~AMDK8_CMPHALT);
1290 __asm __volatile("sti; hlt");
1295 cpu_idle_spin(int busy)
1301 void (*cpu_idle_fn)(int) = cpu_idle_hlt;
1303 void (*cpu_idle_fn)(int) = cpu_idle_acpi;
1309 #if defined(SMP) && !defined(XEN)
1310 if (mp_grab_cpu_hlt())
1317 * mwait cpu power states. Lower 4 bits are sub-states.
1319 #define MWAIT_C0 0xf0
1320 #define MWAIT_C1 0x00
1321 #define MWAIT_C2 0x10
1322 #define MWAIT_C3 0x20
1323 #define MWAIT_C4 0x30
1325 #define MWAIT_DISABLED 0x0
1326 #define MWAIT_WOKEN 0x1
1327 #define MWAIT_WAITING 0x2
1330 cpu_idle_mwait(int busy)
1334 mwait = (int *)PCPU_PTR(monitorbuf);
1335 *mwait = MWAIT_WAITING;
1336 if (sched_runnable())
1338 cpu_monitor(mwait, 0, 0);
1339 if (*mwait == MWAIT_WAITING)
1340 cpu_mwait(0, MWAIT_C1);
1344 cpu_idle_mwait_hlt(int busy)
1348 mwait = (int *)PCPU_PTR(monitorbuf);
1350 *mwait = MWAIT_DISABLED;
1354 *mwait = MWAIT_WAITING;
1355 if (sched_runnable())
1357 cpu_monitor(mwait, 0, 0);
1358 if (*mwait == MWAIT_WAITING)
1359 cpu_mwait(0, MWAIT_C1);
1363 cpu_idle_wakeup(int cpu)
1368 if (cpu_idle_fn == cpu_idle_spin)
1370 if (cpu_idle_fn != cpu_idle_mwait && cpu_idle_fn != cpu_idle_mwait_hlt)
1372 pcpu = pcpu_find(cpu);
1373 mwait = (int *)pcpu->pc_monitorbuf;
1375 * This doesn't need to be atomic since missing the race will
1376 * simply result in unnecessary IPIs.
1378 if (cpu_idle_fn == cpu_idle_mwait_hlt && *mwait == MWAIT_DISABLED)
1380 *mwait = MWAIT_WOKEN;
1386 * Ordered by speed/power consumption.
1392 { cpu_idle_spin, "spin" },
1393 { cpu_idle_mwait, "mwait" },
1394 { cpu_idle_mwait_hlt, "mwait_hlt" },
1395 { cpu_idle_amdc1e, "amdc1e" },
1396 { cpu_idle_hlt, "hlt" },
1397 { cpu_idle_acpi, "acpi" },
1402 idle_sysctl_available(SYSCTL_HANDLER_ARGS)
1408 avail = malloc(256, M_TEMP, M_WAITOK);
1410 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1411 if (strstr(idle_tbl[i].id_name, "mwait") &&
1412 (cpu_feature2 & CPUID2_MON) == 0)
1414 if (strcmp(idle_tbl[i].id_name, "amdc1e") == 0 &&
1415 cpu_ident_amdc1e == 0)
1417 p += sprintf(p, "%s, ", idle_tbl[i].id_name);
1419 error = sysctl_handle_string(oidp, avail, 0, req);
1420 free(avail, M_TEMP);
1425 idle_sysctl(SYSCTL_HANDLER_ARGS)
1433 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1434 if (idle_tbl[i].id_fn == cpu_idle_fn) {
1435 p = idle_tbl[i].id_name;
1439 strncpy(buf, p, sizeof(buf));
1440 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1441 if (error != 0 || req->newptr == NULL)
1443 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1444 if (strstr(idle_tbl[i].id_name, "mwait") &&
1445 (cpu_feature2 & CPUID2_MON) == 0)
1447 if (strcmp(idle_tbl[i].id_name, "amdc1e") == 0 &&
1448 cpu_ident_amdc1e == 0)
1450 if (strcmp(idle_tbl[i].id_name, buf))
1452 cpu_idle_fn = idle_tbl[i].id_fn;
1458 SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
1459 0, 0, idle_sysctl_available, "A", "list of available idle functions");
1461 SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
1462 idle_sysctl, "A", "currently selected idle function");
1465 * Reset registers to default values on exec.
1468 exec_setregs(td, entry, stack, ps_strings)
1474 struct trapframe *regs = td->td_frame;
1475 struct pcb *pcb = td->td_pcb;
1477 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1478 pcb->pcb_gs = _udatasel;
1481 mtx_lock_spin(&dt_lock);
1482 if (td->td_proc->p_md.md_ldt)
1485 mtx_unlock_spin(&dt_lock);
1487 bzero((char *)regs, sizeof(struct trapframe));
1488 regs->tf_eip = entry;
1489 regs->tf_esp = stack;
1490 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1491 regs->tf_ss = _udatasel;
1492 regs->tf_ds = _udatasel;
1493 regs->tf_es = _udatasel;
1494 regs->tf_fs = _udatasel;
1495 regs->tf_cs = _ucodesel;
1497 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1498 regs->tf_ebx = ps_strings;
1501 * Reset the hardware debug registers if they were in use.
1502 * They won't have any meaning for the newly exec'd process.
1504 if (pcb->pcb_flags & PCB_DBREGS) {
1511 if (pcb == PCPU_GET(curpcb)) {
1513 * Clear the debug registers on the running
1514 * CPU, otherwise they will end up affecting
1515 * the next process we switch to.
1519 pcb->pcb_flags &= ~PCB_DBREGS;
1523 * Initialize the math emulator (if any) for the current process.
1524 * Actually, just clear the bit that says that the emulator has
1525 * been initialized. Initialization is delayed until the process
1526 * traps to the emulator (if it is done at all) mainly because
1527 * emulators don't provide an entry point for initialization.
1529 td->td_pcb->pcb_flags &= ~FP_SOFTFP;
1530 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1533 * Drop the FP state if we hold it, so that the process gets a
1534 * clean FP state if it uses the FPU again.
1539 * XXX - Linux emulator
1540 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1543 td->td_retval[1] = 0;
1554 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1556 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1557 * instructions. We must set the CR0_MP bit and use the CR0_TS
1558 * bit to control the trap, because setting the CR0_EM bit does
1559 * not cause WAIT instructions to trap. It's important to trap
1560 * WAIT instructions - otherwise the "wait" variants of no-wait
1561 * control instructions would degenerate to the "no-wait" variants
1562 * after FP context switches but work correctly otherwise. It's
1563 * particularly important to trap WAITs when there is no NPX -
1564 * otherwise the "wait" variants would always degenerate.
1566 * Try setting CR0_NE to get correct error reporting on 486DX's.
1567 * Setting it should fail or do nothing on lesser processors.
1569 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1574 u_long bootdev; /* not a struct cdev *- encoding is different */
1575 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1576 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1579 * Initialize 386 and configure to run kernel
1583 * Initialize segments & interrupt table
1589 union descriptor *gdt;
1590 union descriptor *ldt;
1592 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1593 union descriptor ldt[NLDT]; /* local descriptor table */
1595 static struct gate_descriptor idt0[NIDT];
1596 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1597 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1598 struct mtx dt_lock; /* lock for GDT and LDT */
1600 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1601 extern int has_f00f_bug;
1604 static struct i386tss dblfault_tss;
1605 static char dblfault_stack[PAGE_SIZE];
1607 extern vm_offset_t proc0kstack;
1611 * software prototypes -- in more palatable form.
1613 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1614 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1616 struct soft_segment_descriptor gdt_segs[] = {
1617 /* GNULL_SEL 0 Null Descriptor */
1623 .ssd_xx = 0, .ssd_xx1 = 0,
1626 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1628 .ssd_limit = 0xfffff,
1629 .ssd_type = SDT_MEMRWA,
1632 .ssd_xx = 0, .ssd_xx1 = 0,
1635 /* GUFS_SEL 2 %fs Descriptor for user */
1637 .ssd_limit = 0xfffff,
1638 .ssd_type = SDT_MEMRWA,
1641 .ssd_xx = 0, .ssd_xx1 = 0,
1644 /* GUGS_SEL 3 %gs Descriptor for user */
1646 .ssd_limit = 0xfffff,
1647 .ssd_type = SDT_MEMRWA,
1650 .ssd_xx = 0, .ssd_xx1 = 0,
1653 /* GCODE_SEL 4 Code Descriptor for kernel */
1655 .ssd_limit = 0xfffff,
1656 .ssd_type = SDT_MEMERA,
1659 .ssd_xx = 0, .ssd_xx1 = 0,
1662 /* GDATA_SEL 5 Data Descriptor for kernel */
1664 .ssd_limit = 0xfffff,
1665 .ssd_type = SDT_MEMRWA,
1668 .ssd_xx = 0, .ssd_xx1 = 0,
1671 /* GUCODE_SEL 6 Code Descriptor for user */
1673 .ssd_limit = 0xfffff,
1674 .ssd_type = SDT_MEMERA,
1677 .ssd_xx = 0, .ssd_xx1 = 0,
1680 /* GUDATA_SEL 7 Data Descriptor for user */
1682 .ssd_limit = 0xfffff,
1683 .ssd_type = SDT_MEMRWA,
1686 .ssd_xx = 0, .ssd_xx1 = 0,
1689 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1690 { .ssd_base = 0x400,
1691 .ssd_limit = 0xfffff,
1692 .ssd_type = SDT_MEMRWA,
1695 .ssd_xx = 0, .ssd_xx1 = 0,
1699 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1702 .ssd_limit = sizeof(struct i386tss)-1,
1703 .ssd_type = SDT_SYS386TSS,
1706 .ssd_xx = 0, .ssd_xx1 = 0,
1709 /* GLDT_SEL 10 LDT Descriptor */
1710 { .ssd_base = (int) ldt,
1711 .ssd_limit = sizeof(ldt)-1,
1712 .ssd_type = SDT_SYSLDT,
1715 .ssd_xx = 0, .ssd_xx1 = 0,
1718 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1719 { .ssd_base = (int) ldt,
1720 .ssd_limit = (512 * sizeof(union descriptor)-1),
1721 .ssd_type = SDT_SYSLDT,
1724 .ssd_xx = 0, .ssd_xx1 = 0,
1727 /* GPANIC_SEL 12 Panic Tss Descriptor */
1728 { .ssd_base = (int) &dblfault_tss,
1729 .ssd_limit = sizeof(struct i386tss)-1,
1730 .ssd_type = SDT_SYS386TSS,
1733 .ssd_xx = 0, .ssd_xx1 = 0,
1736 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1738 .ssd_limit = 0xfffff,
1739 .ssd_type = SDT_MEMERA,
1742 .ssd_xx = 0, .ssd_xx1 = 0,
1745 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1747 .ssd_limit = 0xfffff,
1748 .ssd_type = SDT_MEMERA,
1751 .ssd_xx = 0, .ssd_xx1 = 0,
1754 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1756 .ssd_limit = 0xfffff,
1757 .ssd_type = SDT_MEMRWA,
1760 .ssd_xx = 0, .ssd_xx1 = 0,
1763 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1765 .ssd_limit = 0xfffff,
1766 .ssd_type = SDT_MEMRWA,
1769 .ssd_xx = 0, .ssd_xx1 = 0,
1772 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1774 .ssd_limit = 0xfffff,
1775 .ssd_type = SDT_MEMRWA,
1778 .ssd_xx = 0, .ssd_xx1 = 0,
1781 /* GNDIS_SEL 18 NDIS Descriptor */
1787 .ssd_xx = 0, .ssd_xx1 = 0,
1793 static struct soft_segment_descriptor ldt_segs[] = {
1794 /* Null Descriptor - overwritten by call gate */
1800 .ssd_xx = 0, .ssd_xx1 = 0,
1803 /* Null Descriptor - overwritten by call gate */
1809 .ssd_xx = 0, .ssd_xx1 = 0,
1812 /* Null Descriptor - overwritten by call gate */
1818 .ssd_xx = 0, .ssd_xx1 = 0,
1821 /* Code Descriptor for user */
1823 .ssd_limit = 0xfffff,
1824 .ssd_type = SDT_MEMERA,
1827 .ssd_xx = 0, .ssd_xx1 = 0,
1830 /* Null Descriptor - overwritten by call gate */
1836 .ssd_xx = 0, .ssd_xx1 = 0,
1839 /* Data Descriptor for user */
1841 .ssd_limit = 0xfffff,
1842 .ssd_type = SDT_MEMRWA,
1845 .ssd_xx = 0, .ssd_xx1 = 0,
1851 setidt(idx, func, typ, dpl, selec)
1858 struct gate_descriptor *ip;
1861 ip->gd_looffset = (int)func;
1862 ip->gd_selector = selec;
1868 ip->gd_hioffset = ((int)func)>>16 ;
1872 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1873 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1874 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1875 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1876 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1880 * Display the index and function name of any IDT entries that don't use
1881 * the default 'rsvd' entry point.
1883 DB_SHOW_COMMAND(idt, db_show_idt)
1885 struct gate_descriptor *ip;
1890 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
1891 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1892 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1893 db_printf("%3d\t", idx);
1894 db_printsym(func, DB_STGY_PROC);
1901 /* Show privileged registers. */
1902 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
1904 uint64_t idtr, gdtr;
1907 db_printf("idtr\t0x%08x/%04x\n",
1908 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
1910 db_printf("gdtr\t0x%08x/%04x\n",
1911 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
1912 db_printf("ldtr\t0x%04x\n", rldt());
1913 db_printf("tr\t0x%04x\n", rtr());
1914 db_printf("cr0\t0x%08x\n", rcr0());
1915 db_printf("cr2\t0x%08x\n", rcr2());
1916 db_printf("cr3\t0x%08x\n", rcr3());
1917 db_printf("cr4\t0x%08x\n", rcr4());
1923 struct segment_descriptor *sd;
1924 struct soft_segment_descriptor *ssd;
1926 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1927 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1928 ssd->ssd_type = sd->sd_type;
1929 ssd->ssd_dpl = sd->sd_dpl;
1930 ssd->ssd_p = sd->sd_p;
1931 ssd->ssd_def32 = sd->sd_def32;
1932 ssd->ssd_gran = sd->sd_gran;
1936 add_smap_entry(struct bios_smap *smap, vm_paddr_t *physmap, int *physmap_idxp)
1938 int i, insert_idx, physmap_idx;
1940 physmap_idx = *physmap_idxp;
1942 if (boothowto & RB_VERBOSE)
1943 printf("SMAP type=%02x base=%016llx len=%016llx\n",
1944 smap->type, smap->base, smap->length);
1946 if (smap->type != SMAP_TYPE_MEMORY)
1949 if (smap->length == 0)
1953 if (smap->base >= 0xffffffff) {
1954 printf("%uK of memory above 4GB ignored\n",
1955 (u_int)(smap->length / 1024));
1961 * Find insertion point while checking for overlap. Start off by
1962 * assuming the new entry will be added to the end.
1964 insert_idx = physmap_idx + 2;
1965 for (i = 0; i <= physmap_idx; i += 2) {
1966 if (smap->base < physmap[i + 1]) {
1967 if (smap->base + smap->length <= physmap[i]) {
1971 if (boothowto & RB_VERBOSE)
1973 "Overlapping memory regions, ignoring second region\n");
1978 /* See if we can prepend to the next entry. */
1979 if (insert_idx <= physmap_idx &&
1980 smap->base + smap->length == physmap[insert_idx]) {
1981 physmap[insert_idx] = smap->base;
1985 /* See if we can append to the previous entry. */
1986 if (insert_idx > 0 && smap->base == physmap[insert_idx - 1]) {
1987 physmap[insert_idx - 1] += smap->length;
1992 *physmap_idxp = physmap_idx;
1993 if (physmap_idx == PHYSMAP_SIZE) {
1995 "Too many segments in the physical address map, giving up\n");
2000 * Move the last 'N' entries down to make room for the new
2003 for (i = physmap_idx; i > insert_idx; i -= 2) {
2004 physmap[i] = physmap[i - 2];
2005 physmap[i + 1] = physmap[i - 1];
2008 /* Insert the new entry. */
2009 physmap[insert_idx] = smap->base;
2010 physmap[insert_idx + 1] = smap->base + smap->length;
2015 * Populate the (physmap) array with base/bound pairs describing the
2016 * available physical memory in the system, then test this memory and
2017 * build the phys_avail array describing the actually-available memory.
2019 * If we cannot accurately determine the physical memory map, then use
2020 * value from the 0xE801 call, and failing that, the RTC.
2022 * Total memory size may be set by the kernel environment variable
2023 * hw.physmem or the compile-time define MAXMEM.
2025 * XXX first should be vm_paddr_t.
2028 getmemsize(int first)
2030 int i, off, physmap_idx, pa_indx, da_indx;
2031 int hasbrokenint12, has_smap;
2032 u_long physmem_tunable;
2034 struct vm86frame vmf;
2035 struct vm86context vmc;
2036 vm_paddr_t pa, physmap[PHYSMAP_SIZE];
2038 struct bios_smap *smap, *smapbase, *smapend;
2040 quad_t dcons_addr, dcons_size;
2045 if (arch_i386_is_xbox) {
2047 * We queried the memory size before, so chop off 4MB for
2048 * the framebuffer and inform the OS of this.
2051 physmap[1] = (arch_i386_xbox_memsize * 1024 * 1024) - XBOX_FB_SIZE;
2058 Maxmem = xen_start_info->nr_pages - init_first;
2061 physmap[0] = init_first << PAGE_SHIFT;
2062 physmap[1] = ptoa(Maxmem) - round_page(MSGBUF_SIZE);
2067 TUNABLE_INT_FETCH("hw.hasbrokenint12", &hasbrokenint12);
2068 bzero(&vmf, sizeof(vmf));
2069 bzero(physmap, sizeof(physmap));
2073 * Some newer BIOSes has broken INT 12H implementation which cause
2074 * kernel panic immediately. In this case, we need to scan SMAP
2075 * with INT 15:E820 first, then determine base memory size.
2077 if (hasbrokenint12) {
2082 * Perform "base memory" related probes & setup
2084 vm86_intcall(0x12, &vmf);
2085 basemem = vmf.vmf_ax;
2086 if (basemem > 640) {
2087 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
2093 * XXX if biosbasemem is now < 640, there is a `hole'
2094 * between the end of base memory and the start of
2095 * ISA memory. The hole may be empty or it may
2096 * contain BIOS code or data. Map it read/write so
2097 * that the BIOS can write to it. (Memory from 0 to
2098 * the physical end of the kernel is mapped read-only
2099 * to begin with and then parts of it are remapped.
2100 * The parts that aren't remapped form holes that
2101 * remain read-only and are unused by the kernel.
2102 * The base memory area is below the physical end of
2103 * the kernel and right now forms a read-only hole.
2104 * The part of it from PAGE_SIZE to
2105 * (trunc_page(biosbasemem * 1024) - 1) will be
2106 * remapped and used by the kernel later.)
2108 * This code is similar to the code used in
2109 * pmap_mapdev, but since no memory needs to be
2110 * allocated we simply change the mapping.
2112 for (pa = trunc_page(basemem * 1024);
2113 pa < ISA_HOLE_START; pa += PAGE_SIZE)
2114 pmap_kenter(KERNBASE + pa, pa);
2117 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
2118 * the vm86 page table so that vm86 can scribble on them using
2119 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
2120 * page 0, at least as initialized here?
2122 pte = (pt_entry_t *)vm86paddr;
2123 for (i = basemem / 4; i < 160; i++)
2124 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
2128 * Fetch the memory map with INT 15:E820. First, check to see
2129 * if the loader supplied it and use that if so. Otherwise,
2130 * use vm86 to invoke the BIOS call directly.
2134 kmdp = preload_search_by_type("elf kernel");
2136 kmdp = preload_search_by_type("elf32 kernel");
2138 smapbase = (struct bios_smap *)preload_search_info(kmdp,
2139 MODINFO_METADATA | MODINFOMD_SMAP);
2140 if (smapbase != NULL) {
2141 /* subr_module.c says:
2142 * "Consumer may safely assume that size value precedes data."
2143 * ie: an int32_t immediately precedes smap.
2145 smapsize = *((u_int32_t *)smapbase - 1);
2146 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
2149 for (smap = smapbase; smap < smapend; smap++)
2150 if (!add_smap_entry(smap, physmap, &physmap_idx))
2154 * map page 1 R/W into the kernel page table so we can use it
2155 * as a buffer. The kernel will unmap this page later.
2157 pmap_kenter(KERNBASE + (1 << PAGE_SHIFT), 1 << PAGE_SHIFT);
2159 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE +
2161 vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
2165 vmf.vmf_eax = 0xE820;
2166 vmf.vmf_edx = SMAP_SIG;
2167 vmf.vmf_ecx = sizeof(struct bios_smap);
2168 i = vm86_datacall(0x15, &vmf, &vmc);
2169 if (i || vmf.vmf_eax != SMAP_SIG)
2172 if (!add_smap_entry(smap, physmap, &physmap_idx))
2174 } while (vmf.vmf_ebx != 0);
2178 * Perform "base memory" related probes & setup based on SMAP
2181 for (i = 0; i <= physmap_idx; i += 2) {
2182 if (physmap[i] == 0x00000000) {
2183 basemem = physmap[i + 1] / 1024;
2189 * XXX this function is horribly organized and has to the same
2190 * things that it does above here.
2194 if (basemem > 640) {
2196 "Preposterous BIOS basemem of %uK, truncating to 640K\n",
2202 * Let vm86 scribble on pages between basemem and
2203 * ISA_HOLE_START, as above.
2205 for (pa = trunc_page(basemem * 1024);
2206 pa < ISA_HOLE_START; pa += PAGE_SIZE)
2207 pmap_kenter(KERNBASE + pa, pa);
2208 pte = (pt_entry_t *)vm86paddr;
2209 for (i = basemem / 4; i < 160; i++)
2210 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
2213 if (physmap[1] != 0)
2217 * If we failed above, try memory map with INT 15:E801
2219 vmf.vmf_ax = 0xE801;
2220 if (vm86_intcall(0x15, &vmf) == 0) {
2221 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
2225 vm86_intcall(0x15, &vmf);
2226 extmem = vmf.vmf_ax;
2229 * Prefer the RTC value for extended memory.
2231 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
2236 * Special hack for chipsets that still remap the 384k hole when
2237 * there's 16MB of memory - this really confuses people that
2238 * are trying to use bus mastering ISA controllers with the
2239 * "16MB limit"; they only have 16MB, but the remapping puts
2240 * them beyond the limit.
2242 * If extended memory is between 15-16MB (16-17MB phys address range),
2245 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
2249 physmap[1] = basemem * 1024;
2251 physmap[physmap_idx] = 0x100000;
2252 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
2256 * Now, physmap contains a map of physical memory.
2260 /* make hole for AP bootstrap code */
2261 physmap[1] = mp_bootaddress(physmap[1]);
2265 * Maxmem isn't the "maximum memory", it's one larger than the
2266 * highest page of the physical address space. It should be
2267 * called something like "Maxphyspage". We may adjust this
2268 * based on ``hw.physmem'' and the results of the memory test.
2270 Maxmem = atop(physmap[physmap_idx + 1]);
2273 Maxmem = MAXMEM / 4;
2276 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
2277 Maxmem = atop(physmem_tunable);
2280 * If we have an SMAP, don't allow MAXMEM or hw.physmem to extend
2281 * the amount of memory in the system.
2283 if (has_smap && Maxmem > atop(physmap[physmap_idx + 1]))
2284 Maxmem = atop(physmap[physmap_idx + 1]);
2286 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
2287 (boothowto & RB_VERBOSE))
2288 printf("Physical memory use set to %ldK\n", Maxmem * 4);
2291 * If Maxmem has been increased beyond what the system has detected,
2292 * extend the last memory segment to the new limit.
2294 if (atop(physmap[physmap_idx + 1]) < Maxmem)
2295 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
2297 /* call pmap initialization to make new kernel address space */
2298 pmap_bootstrap(first);
2301 * Size up each available chunk of physical memory.
2303 physmap[0] = PAGE_SIZE; /* mask off page 0 */
2306 phys_avail[pa_indx++] = physmap[0];
2307 phys_avail[pa_indx] = physmap[0];
2308 dump_avail[da_indx] = physmap[0];
2312 * Get dcons buffer address
2314 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
2315 getenv_quad("dcons.size", &dcons_size) == 0)
2320 * physmap is in bytes, so when converting to page boundaries,
2321 * round up the start address and round down the end address.
2323 for (i = 0; i <= physmap_idx; i += 2) {
2326 end = ptoa((vm_paddr_t)Maxmem);
2327 if (physmap[i + 1] < end)
2328 end = trunc_page(physmap[i + 1]);
2329 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
2330 int tmp, page_bad, full;
2331 int *ptr = (int *)CADDR1;
2335 * block out kernel memory as not available.
2337 if (pa >= KERNLOAD && pa < first)
2341 * block out dcons buffer
2344 && pa >= trunc_page(dcons_addr)
2345 && pa < dcons_addr + dcons_size)
2351 * map page into kernel: valid, read/write,non-cacheable
2353 *pte = pa | PG_V | PG_RW | PG_N;
2358 * Test for alternating 1's and 0's
2360 *(volatile int *)ptr = 0xaaaaaaaa;
2361 if (*(volatile int *)ptr != 0xaaaaaaaa)
2364 * Test for alternating 0's and 1's
2366 *(volatile int *)ptr = 0x55555555;
2367 if (*(volatile int *)ptr != 0x55555555)
2372 *(volatile int *)ptr = 0xffffffff;
2373 if (*(volatile int *)ptr != 0xffffffff)
2378 *(volatile int *)ptr = 0x0;
2379 if (*(volatile int *)ptr != 0x0)
2382 * Restore original value.
2387 * Adjust array of valid/good pages.
2389 if (page_bad == TRUE)
2392 * If this good page is a continuation of the
2393 * previous set of good pages, then just increase
2394 * the end pointer. Otherwise start a new chunk.
2395 * Note that "end" points one higher than end,
2396 * making the range >= start and < end.
2397 * If we're also doing a speculative memory
2398 * test and we at or past the end, bump up Maxmem
2399 * so that we keep going. The first bad page
2400 * will terminate the loop.
2402 if (phys_avail[pa_indx] == pa) {
2403 phys_avail[pa_indx] += PAGE_SIZE;
2406 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
2408 "Too many holes in the physical address space, giving up\n");
2413 phys_avail[pa_indx++] = pa; /* start */
2414 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
2418 if (dump_avail[da_indx] == pa) {
2419 dump_avail[da_indx] += PAGE_SIZE;
2422 if (da_indx == DUMP_AVAIL_ARRAY_END) {
2426 dump_avail[da_indx++] = pa; /* start */
2427 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
2437 phys_avail[0] = physfree;
2438 phys_avail[1] = xen_start_info->nr_pages*PAGE_SIZE;
2443 * The last chunk must contain at least one page plus the message
2444 * buffer to avoid complicating other code (message buffer address
2445 * calculation, etc.).
2447 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
2448 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
2449 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
2450 phys_avail[pa_indx--] = 0;
2451 phys_avail[pa_indx--] = 0;
2454 Maxmem = atop(phys_avail[pa_indx]);
2456 /* Trim off space for the message buffer. */
2457 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
2459 /* Map the message buffer. */
2460 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
2461 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
2468 #define MTOPSIZE (1<<(14 + PAGE_SHIFT))
2474 unsigned long gdtmachpfn;
2475 int error, gsel_tss, metadata_missing, x, pa;
2477 struct callback_register event = {
2478 .type = CALLBACKTYPE_event,
2479 .address = {GSEL(GCODE_SEL, SEL_KPL), (unsigned long)Xhypervisor_callback },
2481 struct callback_register failsafe = {
2482 .type = CALLBACKTYPE_failsafe,
2483 .address = {GSEL(GCODE_SEL, SEL_KPL), (unsigned long)failsafe_callback },
2486 thread0.td_kstack = proc0kstack;
2487 thread0.td_pcb = (struct pcb *)
2488 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
2491 * This may be done better later if it gets more high level
2492 * components in it. If so just link td->td_proc here.
2494 proc_linkup0(&proc0, &thread0);
2496 metadata_missing = 0;
2497 if (xen_start_info->mod_start) {
2498 preload_metadata = (caddr_t)xen_start_info->mod_start;
2499 preload_bootstrap_relocate(KERNBASE);
2501 metadata_missing = 1;
2504 kern_envp = static_env;
2505 else if ((caddr_t)xen_start_info->cmd_line)
2506 kern_envp = xen_setbootenv((caddr_t)xen_start_info->cmd_line);
2508 boothowto |= xen_boothowto(kern_envp);
2510 /* Init basic tunables, hz etc */
2514 * XEN occupies a portion of the upper virtual address space
2515 * At its base it manages an array mapping machine page frames
2516 * to physical page frames - hence we need to be able to
2517 * access 4GB - (64MB - 4MB + 64k)
2519 gdt_segs[GPRIV_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2520 gdt_segs[GUFS_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2521 gdt_segs[GUGS_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2522 gdt_segs[GCODE_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2523 gdt_segs[GDATA_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2524 gdt_segs[GUCODE_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2525 gdt_segs[GUDATA_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2526 gdt_segs[GBIOSLOWMEM_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2529 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2530 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2532 PT_SET_MA(gdt, xpmap_ptom(VTOP(gdt)) | PG_V | PG_RW);
2533 bzero(gdt, PAGE_SIZE);
2534 for (x = 0; x < NGDT; x++)
2535 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2537 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2539 gdtmachpfn = vtomach(gdt) >> PAGE_SHIFT;
2540 PT_SET_MA(gdt, xpmap_ptom(VTOP(gdt)) | PG_V);
2541 PANIC_IF(HYPERVISOR_set_gdt(&gdtmachpfn, 512) != 0);
2545 if ((error = HYPERVISOR_set_trap_table(trap_table)) != 0) {
2546 panic("set_trap_table failed - error %d\n", error);
2549 error = HYPERVISOR_callback_op(CALLBACKOP_register, &event);
2551 error = HYPERVISOR_callback_op(CALLBACKOP_register, &failsafe);
2552 #if CONFIG_XEN_COMPAT <= 0x030002
2553 if (error == -ENOXENSYS)
2554 HYPERVISOR_set_callbacks(GSEL(GCODE_SEL, SEL_KPL),
2555 (unsigned long)Xhypervisor_callback,
2556 GSEL(GCODE_SEL, SEL_KPL), (unsigned long)failsafe_callback);
2558 pcpu_init(pc, 0, sizeof(struct pcpu));
2559 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2560 pmap_kenter(pa + KERNBASE, pa);
2561 dpcpu_init((void *)(first + KERNBASE), 0);
2562 first += DPCPU_SIZE;
2564 PCPU_SET(prvspace, pc);
2565 PCPU_SET(curthread, &thread0);
2566 PCPU_SET(curpcb, thread0.td_pcb);
2569 * Initialize mutexes.
2571 * icu_lock: in order to allow an interrupt to occur in a critical
2572 * section, to set pcpu->ipending (etc...) properly, we
2573 * must be able to get the icu lock, so it can't be
2577 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2579 /* make ldt memory segments */
2580 PT_SET_MA(ldt, xpmap_ptom(VTOP(ldt)) | PG_V | PG_RW);
2581 bzero(ldt, PAGE_SIZE);
2582 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2583 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2584 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2585 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2587 default_proc_ldt.ldt_base = (caddr_t)ldt;
2588 default_proc_ldt.ldt_len = 6;
2589 _default_ldt = (int)&default_proc_ldt;
2590 PCPU_SET(currentldt, _default_ldt)
2591 PT_SET_MA(ldt, *vtopte((unsigned long)ldt) & ~PG_RW);
2592 xen_set_ldt((unsigned long) ldt, (sizeof ldt_segs / sizeof ldt_segs[0]));
2594 #if defined(XEN_PRIVILEGED)
2596 * Initialize the i8254 before the console so that console
2597 * initialization can use DELAY().
2603 * Initialize the console before we print anything out.
2607 if (metadata_missing)
2608 printf("WARNING: loader(8) metadata is missing!\n");
2616 ksym_start = bootinfo.bi_symtab;
2617 ksym_end = bootinfo.bi_esymtab;
2623 if (boothowto & RB_KDB)
2624 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
2627 finishidentcpu(); /* Final stage of CPU initialization */
2628 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2629 GSEL(GCODE_SEL, SEL_KPL));
2630 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2631 GSEL(GCODE_SEL, SEL_KPL));
2632 initializecpu(); /* Initialize CPU registers */
2634 /* make an initial tss so cpu can get interrupt stack on syscall! */
2635 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2636 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2637 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
2638 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2639 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2640 HYPERVISOR_stack_switch(GSEL(GDATA_SEL, SEL_KPL),
2641 PCPU_GET(common_tss.tss_esp0));
2643 /* pointer to selector slot for %fs/%gs */
2644 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2646 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2647 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2648 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2649 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2651 dblfault_tss.tss_cr3 = (int)IdlePDPT;
2653 dblfault_tss.tss_cr3 = (int)IdlePTD;
2655 dblfault_tss.tss_eip = (int)dblfault_handler;
2656 dblfault_tss.tss_eflags = PSL_KERNEL;
2657 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2658 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2659 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2660 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2661 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2665 init_param2(physmem);
2667 /* now running on new page tables, configured,and u/iom is accessible */
2669 msgbufinit(msgbufp, MSGBUF_SIZE);
2670 /* transfer to user mode */
2672 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2673 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2675 /* setup proc 0's pcb */
2676 thread0.td_pcb->pcb_flags = 0;
2678 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
2680 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2682 thread0.td_pcb->pcb_ext = 0;
2683 thread0.td_frame = &proc0_tf;
2684 thread0.td_pcb->pcb_fsd = PCPU_GET(fsgs_gdt)[0];
2685 thread0.td_pcb->pcb_gsd = PCPU_GET(fsgs_gdt)[1];
2687 if (cpu_probe_amdc1e())
2688 cpu_idle_fn = cpu_idle_amdc1e;
2696 struct gate_descriptor *gdp;
2697 int gsel_tss, metadata_missing, x, pa;
2700 thread0.td_kstack = proc0kstack;
2701 thread0.td_pcb = (struct pcb *)
2702 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
2705 * This may be done better later if it gets more high level
2706 * components in it. If so just link td->td_proc here.
2708 proc_linkup0(&proc0, &thread0);
2710 metadata_missing = 0;
2711 if (bootinfo.bi_modulep) {
2712 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
2713 preload_bootstrap_relocate(KERNBASE);
2715 metadata_missing = 1;
2718 kern_envp = static_env;
2719 else if (bootinfo.bi_envp)
2720 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
2722 /* Init basic tunables, hz etc */
2726 * Make gdt memory segments. All segments cover the full 4GB
2727 * of address space and permissions are enforced at page level.
2729 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
2730 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
2731 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
2732 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
2733 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
2734 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
2737 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
2738 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2739 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2741 for (x = 0; x < NGDT; x++)
2742 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2744 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2745 r_gdt.rd_base = (int) gdt;
2746 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2749 pcpu_init(pc, 0, sizeof(struct pcpu));
2750 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2751 pmap_kenter(pa + KERNBASE, pa);
2752 dpcpu_init((void *)(first + KERNBASE), 0);
2753 first += DPCPU_SIZE;
2754 PCPU_SET(prvspace, pc);
2755 PCPU_SET(curthread, &thread0);
2756 PCPU_SET(curpcb, thread0.td_pcb);
2759 * Initialize mutexes.
2761 * icu_lock: in order to allow an interrupt to occur in a critical
2762 * section, to set pcpu->ipending (etc...) properly, we
2763 * must be able to get the icu lock, so it can't be
2767 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2769 /* make ldt memory segments */
2770 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2771 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2772 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2773 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2775 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2777 PCPU_SET(currentldt, _default_ldt);
2780 for (x = 0; x < NIDT; x++)
2781 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2782 GSEL(GCODE_SEL, SEL_KPL));
2783 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2784 GSEL(GCODE_SEL, SEL_KPL));
2785 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2786 GSEL(GCODE_SEL, SEL_KPL));
2787 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
2788 GSEL(GCODE_SEL, SEL_KPL));
2789 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2790 GSEL(GCODE_SEL, SEL_KPL));
2791 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2792 GSEL(GCODE_SEL, SEL_KPL));
2793 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2794 GSEL(GCODE_SEL, SEL_KPL));
2795 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2796 GSEL(GCODE_SEL, SEL_KPL));
2797 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2798 , GSEL(GCODE_SEL, SEL_KPL));
2799 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2800 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2801 GSEL(GCODE_SEL, SEL_KPL));
2802 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2803 GSEL(GCODE_SEL, SEL_KPL));
2804 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2805 GSEL(GCODE_SEL, SEL_KPL));
2806 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2807 GSEL(GCODE_SEL, SEL_KPL));
2808 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2809 GSEL(GCODE_SEL, SEL_KPL));
2810 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2811 GSEL(GCODE_SEL, SEL_KPL));
2812 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2813 GSEL(GCODE_SEL, SEL_KPL));
2814 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2815 GSEL(GCODE_SEL, SEL_KPL));
2816 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2817 GSEL(GCODE_SEL, SEL_KPL));
2818 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2819 GSEL(GCODE_SEL, SEL_KPL));
2820 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2821 GSEL(GCODE_SEL, SEL_KPL));
2823 r_idt.rd_limit = sizeof(idt0) - 1;
2824 r_idt.rd_base = (int) idt;
2829 * The following code queries the PCI ID of 0:0:0. For the XBOX,
2830 * This should be 0x10de / 0x02a5.
2832 * This is exactly what Linux does.
2834 outl(0xcf8, 0x80000000);
2835 if (inl(0xcfc) == 0x02a510de) {
2836 arch_i386_is_xbox = 1;
2837 pic16l_setled(XBOX_LED_GREEN);
2840 * We are an XBOX, but we may have either 64MB or 128MB of
2841 * memory. The PCI host bridge should be programmed for this,
2842 * so we just query it.
2844 outl(0xcf8, 0x80000084);
2845 arch_i386_xbox_memsize = (inl(0xcfc) == 0x7FFFFFF) ? 128 : 64;
2850 * Initialize the i8254 before the console so that console
2851 * initialization can use DELAY().
2856 * Initialize the console before we print anything out.
2860 if (metadata_missing)
2861 printf("WARNING: loader(8) metadata is missing!\n");
2869 ksym_start = bootinfo.bi_symtab;
2870 ksym_end = bootinfo.bi_esymtab;
2876 if (boothowto & RB_KDB)
2877 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
2880 finishidentcpu(); /* Final stage of CPU initialization */
2881 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2882 GSEL(GCODE_SEL, SEL_KPL));
2883 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2884 GSEL(GCODE_SEL, SEL_KPL));
2885 initializecpu(); /* Initialize CPU registers */
2887 /* make an initial tss so cpu can get interrupt stack on syscall! */
2888 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2889 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2890 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
2891 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2892 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2893 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2894 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2895 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2898 /* pointer to selector slot for %fs/%gs */
2899 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2901 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2902 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2903 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2904 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2906 dblfault_tss.tss_cr3 = (int)IdlePDPT;
2908 dblfault_tss.tss_cr3 = (int)IdlePTD;
2910 dblfault_tss.tss_eip = (int)dblfault_handler;
2911 dblfault_tss.tss_eflags = PSL_KERNEL;
2912 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2913 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2914 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2915 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2916 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2920 init_param2(physmem);
2922 /* now running on new page tables, configured,and u/iom is accessible */
2924 msgbufinit(msgbufp, MSGBUF_SIZE);
2926 /* make a call gate to reenter kernel with */
2927 gdp = &ldt[LSYS5CALLS_SEL].gd;
2929 x = (int) &IDTVEC(lcall_syscall);
2930 gdp->gd_looffset = x;
2931 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2933 gdp->gd_type = SDT_SYS386CGT;
2934 gdp->gd_dpl = SEL_UPL;
2936 gdp->gd_hioffset = x >> 16;
2938 /* XXX does this work? */
2940 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2941 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
2943 /* transfer to user mode */
2945 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2946 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2948 /* setup proc 0's pcb */
2949 thread0.td_pcb->pcb_flags = 0;
2951 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
2953 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2955 thread0.td_pcb->pcb_ext = 0;
2956 thread0.td_frame = &proc0_tf;
2958 if (cpu_probe_amdc1e())
2959 cpu_idle_fn = cpu_idle_amdc1e;
2964 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2967 pcpu->pc_acpi_id = 0xffffffff;
2971 spinlock_enter(void)
2976 if (td->td_md.md_spinlock_count == 0)
2977 td->td_md.md_saved_flags = intr_disable();
2978 td->td_md.md_spinlock_count++;
2989 td->td_md.md_spinlock_count--;
2990 if (td->td_md.md_spinlock_count == 0)
2991 intr_restore(td->td_md.md_saved_flags);
2994 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2995 static void f00f_hack(void *unused);
2996 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
2999 f00f_hack(void *unused)
3001 struct gate_descriptor *new_idt;
3009 printf("Intel Pentium detected, installing workaround for F00F bug\n");
3011 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
3013 panic("kmem_alloc returned 0");
3015 /* Put the problematic entry (#6) at the end of the lower page. */
3016 new_idt = (struct gate_descriptor*)
3017 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
3018 bcopy(idt, new_idt, sizeof(idt0));
3019 r_idt.rd_base = (u_int)new_idt;
3022 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
3023 VM_PROT_READ, FALSE) != KERN_SUCCESS)
3024 panic("vm_map_protect failed");
3026 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
3029 * Construct a PCB from a trapframe. This is called from kdb_trap() where
3030 * we want to start a backtrace from the function that caused us to enter
3031 * the debugger. We have the context in the trapframe, but base the trace
3032 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
3033 * enough for a backtrace.
3036 makectx(struct trapframe *tf, struct pcb *pcb)
3039 pcb->pcb_edi = tf->tf_edi;
3040 pcb->pcb_esi = tf->tf_esi;
3041 pcb->pcb_ebp = tf->tf_ebp;
3042 pcb->pcb_ebx = tf->tf_ebx;
3043 pcb->pcb_eip = tf->tf_eip;
3044 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
3048 ptrace_set_pc(struct thread *td, u_long addr)
3051 td->td_frame->tf_eip = addr;
3056 ptrace_single_step(struct thread *td)
3058 td->td_frame->tf_eflags |= PSL_T;
3063 ptrace_clear_single_step(struct thread *td)
3065 td->td_frame->tf_eflags &= ~PSL_T;
3070 fill_regs(struct thread *td, struct reg *regs)
3073 struct trapframe *tp;
3077 regs->r_fs = tp->tf_fs;
3078 regs->r_es = tp->tf_es;
3079 regs->r_ds = tp->tf_ds;
3080 regs->r_edi = tp->tf_edi;
3081 regs->r_esi = tp->tf_esi;
3082 regs->r_ebp = tp->tf_ebp;
3083 regs->r_ebx = tp->tf_ebx;
3084 regs->r_edx = tp->tf_edx;
3085 regs->r_ecx = tp->tf_ecx;
3086 regs->r_eax = tp->tf_eax;
3087 regs->r_eip = tp->tf_eip;
3088 regs->r_cs = tp->tf_cs;
3089 regs->r_eflags = tp->tf_eflags;
3090 regs->r_esp = tp->tf_esp;
3091 regs->r_ss = tp->tf_ss;
3092 regs->r_gs = pcb->pcb_gs;
3097 set_regs(struct thread *td, struct reg *regs)
3100 struct trapframe *tp;
3103 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
3104 !CS_SECURE(regs->r_cs))
3107 tp->tf_fs = regs->r_fs;
3108 tp->tf_es = regs->r_es;
3109 tp->tf_ds = regs->r_ds;
3110 tp->tf_edi = regs->r_edi;
3111 tp->tf_esi = regs->r_esi;
3112 tp->tf_ebp = regs->r_ebp;
3113 tp->tf_ebx = regs->r_ebx;
3114 tp->tf_edx = regs->r_edx;
3115 tp->tf_ecx = regs->r_ecx;
3116 tp->tf_eax = regs->r_eax;
3117 tp->tf_eip = regs->r_eip;
3118 tp->tf_cs = regs->r_cs;
3119 tp->tf_eflags = regs->r_eflags;
3120 tp->tf_esp = regs->r_esp;
3121 tp->tf_ss = regs->r_ss;
3122 pcb->pcb_gs = regs->r_gs;
3126 #ifdef CPU_ENABLE_SSE
3128 fill_fpregs_xmm(sv_xmm, sv_87)
3129 struct savexmm *sv_xmm;
3130 struct save87 *sv_87;
3132 register struct env87 *penv_87 = &sv_87->sv_env;
3133 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
3136 bzero(sv_87, sizeof(*sv_87));
3138 /* FPU control/status */
3139 penv_87->en_cw = penv_xmm->en_cw;
3140 penv_87->en_sw = penv_xmm->en_sw;
3141 penv_87->en_tw = penv_xmm->en_tw;
3142 penv_87->en_fip = penv_xmm->en_fip;
3143 penv_87->en_fcs = penv_xmm->en_fcs;
3144 penv_87->en_opcode = penv_xmm->en_opcode;
3145 penv_87->en_foo = penv_xmm->en_foo;
3146 penv_87->en_fos = penv_xmm->en_fos;
3149 for (i = 0; i < 8; ++i)
3150 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
3154 set_fpregs_xmm(sv_87, sv_xmm)
3155 struct save87 *sv_87;
3156 struct savexmm *sv_xmm;
3158 register struct env87 *penv_87 = &sv_87->sv_env;
3159 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
3162 /* FPU control/status */
3163 penv_xmm->en_cw = penv_87->en_cw;
3164 penv_xmm->en_sw = penv_87->en_sw;
3165 penv_xmm->en_tw = penv_87->en_tw;
3166 penv_xmm->en_fip = penv_87->en_fip;
3167 penv_xmm->en_fcs = penv_87->en_fcs;
3168 penv_xmm->en_opcode = penv_87->en_opcode;
3169 penv_xmm->en_foo = penv_87->en_foo;
3170 penv_xmm->en_fos = penv_87->en_fos;
3173 for (i = 0; i < 8; ++i)
3174 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
3176 #endif /* CPU_ENABLE_SSE */
3179 fill_fpregs(struct thread *td, struct fpreg *fpregs)
3181 #ifdef CPU_ENABLE_SSE
3183 fill_fpregs_xmm(&td->td_pcb->pcb_save.sv_xmm,
3184 (struct save87 *)fpregs);
3187 #endif /* CPU_ENABLE_SSE */
3188 bcopy(&td->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
3193 set_fpregs(struct thread *td, struct fpreg *fpregs)
3195 #ifdef CPU_ENABLE_SSE
3197 set_fpregs_xmm((struct save87 *)fpregs,
3198 &td->td_pcb->pcb_save.sv_xmm);
3201 #endif /* CPU_ENABLE_SSE */
3202 bcopy(fpregs, &td->td_pcb->pcb_save.sv_87, sizeof *fpregs);
3207 * Get machine context.
3210 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
3212 struct trapframe *tp;
3213 struct segment_descriptor *sdp;
3217 PROC_LOCK(curthread->td_proc);
3218 mcp->mc_onstack = sigonstack(tp->tf_esp);
3219 PROC_UNLOCK(curthread->td_proc);
3220 mcp->mc_gs = td->td_pcb->pcb_gs;
3221 mcp->mc_fs = tp->tf_fs;
3222 mcp->mc_es = tp->tf_es;
3223 mcp->mc_ds = tp->tf_ds;
3224 mcp->mc_edi = tp->tf_edi;
3225 mcp->mc_esi = tp->tf_esi;
3226 mcp->mc_ebp = tp->tf_ebp;
3227 mcp->mc_isp = tp->tf_isp;
3228 mcp->mc_eflags = tp->tf_eflags;
3229 if (flags & GET_MC_CLEAR_RET) {
3232 mcp->mc_eflags &= ~PSL_C;
3234 mcp->mc_eax = tp->tf_eax;
3235 mcp->mc_edx = tp->tf_edx;
3237 mcp->mc_ebx = tp->tf_ebx;
3238 mcp->mc_ecx = tp->tf_ecx;
3239 mcp->mc_eip = tp->tf_eip;
3240 mcp->mc_cs = tp->tf_cs;
3241 mcp->mc_esp = tp->tf_esp;
3242 mcp->mc_ss = tp->tf_ss;
3243 mcp->mc_len = sizeof(*mcp);
3244 get_fpcontext(td, mcp);
3245 sdp = &td->td_pcb->pcb_gsd;
3246 mcp->mc_fsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
3247 sdp = &td->td_pcb->pcb_fsd;
3248 mcp->mc_gsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
3254 * Set machine context.
3256 * However, we don't set any but the user modifiable flags, and we won't
3257 * touch the cs selector.
3260 set_mcontext(struct thread *td, const mcontext_t *mcp)
3262 struct trapframe *tp;
3266 if (mcp->mc_len != sizeof(*mcp))
3268 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
3269 (tp->tf_eflags & ~PSL_USERCHANGE);
3270 if ((ret = set_fpcontext(td, mcp)) == 0) {
3271 tp->tf_fs = mcp->mc_fs;
3272 tp->tf_es = mcp->mc_es;
3273 tp->tf_ds = mcp->mc_ds;
3274 tp->tf_edi = mcp->mc_edi;
3275 tp->tf_esi = mcp->mc_esi;
3276 tp->tf_ebp = mcp->mc_ebp;
3277 tp->tf_ebx = mcp->mc_ebx;
3278 tp->tf_edx = mcp->mc_edx;
3279 tp->tf_ecx = mcp->mc_ecx;
3280 tp->tf_eax = mcp->mc_eax;
3281 tp->tf_eip = mcp->mc_eip;
3282 tp->tf_eflags = eflags;
3283 tp->tf_esp = mcp->mc_esp;
3284 tp->tf_ss = mcp->mc_ss;
3285 td->td_pcb->pcb_gs = mcp->mc_gs;
3292 get_fpcontext(struct thread *td, mcontext_t *mcp)
3295 mcp->mc_fpformat = _MC_FPFMT_NODEV;
3296 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
3298 union savefpu *addr;
3301 * XXX mc_fpstate might be misaligned, since its declaration is not
3302 * unportabilized using __attribute__((aligned(16))) like the
3303 * declaration of struct savemm, and anyway, alignment doesn't work
3304 * for auto variables since we don't use gcc's pessimal stack
3305 * alignment. Work around this by abusing the spare fields after
3308 * XXX unpessimize most cases by only aligning when fxsave might be
3309 * called, although this requires knowing too much about
3310 * npxgetregs()'s internals.
3312 addr = (union savefpu *)&mcp->mc_fpstate;
3313 if (td == PCPU_GET(fpcurthread) &&
3314 #ifdef CPU_ENABLE_SSE
3317 ((uintptr_t)(void *)addr & 0xF)) {
3319 addr = (void *)((char *)addr + 4);
3320 while ((uintptr_t)(void *)addr & 0xF);
3322 mcp->mc_ownedfp = npxgetregs(td, addr);
3323 if (addr != (union savefpu *)&mcp->mc_fpstate) {
3324 bcopy(addr, &mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
3325 bzero(&mcp->mc_spare2, sizeof(mcp->mc_spare2));
3327 mcp->mc_fpformat = npxformat();
3332 set_fpcontext(struct thread *td, const mcontext_t *mcp)
3334 union savefpu *addr;
3336 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
3338 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
3339 mcp->mc_fpformat != _MC_FPFMT_XMM)
3341 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
3342 /* We don't care what state is left in the FPU or PCB. */
3344 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
3345 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
3346 /* XXX align as above. */
3347 addr = (union savefpu *)&mcp->mc_fpstate;
3348 if (td == PCPU_GET(fpcurthread) &&
3349 #ifdef CPU_ENABLE_SSE
3352 ((uintptr_t)(void *)addr & 0xF)) {
3354 addr = (void *)((char *)addr + 4);
3355 while ((uintptr_t)(void *)addr & 0xF);
3356 bcopy(&mcp->mc_fpstate, addr, sizeof(mcp->mc_fpstate));
3359 #ifdef CPU_ENABLE_SSE
3361 addr->sv_xmm.sv_env.en_mxcsr &= cpu_mxcsr_mask;
3364 * XXX we violate the dubious requirement that npxsetregs()
3365 * be called with interrupts disabled.
3367 npxsetregs(td, addr);
3370 * Don't bother putting things back where they were in the
3371 * misaligned case, since we know that the caller won't use
3380 fpstate_drop(struct thread *td)
3386 if (PCPU_GET(fpcurthread) == td)
3390 * XXX force a full drop of the npx. The above only drops it if we
3391 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
3393 * XXX I don't much like npxgetregs()'s semantics of doing a full
3394 * drop. Dropping only to the pcb matches fnsave's behaviour.
3395 * We only need to drop to !PCB_INITDONE in sendsig(). But
3396 * sendsig() is the only caller of npxgetregs()... perhaps we just
3397 * have too many layers.
3399 curthread->td_pcb->pcb_flags &= ~PCB_NPXINITDONE;
3404 fill_dbregs(struct thread *td, struct dbreg *dbregs)
3409 dbregs->dr[0] = rdr0();
3410 dbregs->dr[1] = rdr1();
3411 dbregs->dr[2] = rdr2();
3412 dbregs->dr[3] = rdr3();
3413 dbregs->dr[4] = rdr4();
3414 dbregs->dr[5] = rdr5();
3415 dbregs->dr[6] = rdr6();
3416 dbregs->dr[7] = rdr7();
3419 dbregs->dr[0] = pcb->pcb_dr0;
3420 dbregs->dr[1] = pcb->pcb_dr1;
3421 dbregs->dr[2] = pcb->pcb_dr2;
3422 dbregs->dr[3] = pcb->pcb_dr3;
3425 dbregs->dr[6] = pcb->pcb_dr6;
3426 dbregs->dr[7] = pcb->pcb_dr7;
3432 set_dbregs(struct thread *td, struct dbreg *dbregs)
3438 load_dr0(dbregs->dr[0]);
3439 load_dr1(dbregs->dr[1]);
3440 load_dr2(dbregs->dr[2]);
3441 load_dr3(dbregs->dr[3]);
3442 load_dr4(dbregs->dr[4]);
3443 load_dr5(dbregs->dr[5]);
3444 load_dr6(dbregs->dr[6]);
3445 load_dr7(dbregs->dr[7]);
3448 * Don't let an illegal value for dr7 get set. Specifically,
3449 * check for undefined settings. Setting these bit patterns
3450 * result in undefined behaviour and can lead to an unexpected
3453 for (i = 0; i < 4; i++) {
3454 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
3456 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
3463 * Don't let a process set a breakpoint that is not within the
3464 * process's address space. If a process could do this, it
3465 * could halt the system by setting a breakpoint in the kernel
3466 * (if ddb was enabled). Thus, we need to check to make sure
3467 * that no breakpoints are being enabled for addresses outside
3468 * process's address space.
3470 * XXX - what about when the watched area of the user's
3471 * address space is written into from within the kernel
3472 * ... wouldn't that still cause a breakpoint to be generated
3473 * from within kernel mode?
3476 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
3477 /* dr0 is enabled */
3478 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
3482 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
3483 /* dr1 is enabled */
3484 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
3488 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
3489 /* dr2 is enabled */
3490 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
3494 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
3495 /* dr3 is enabled */
3496 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
3500 pcb->pcb_dr0 = dbregs->dr[0];
3501 pcb->pcb_dr1 = dbregs->dr[1];
3502 pcb->pcb_dr2 = dbregs->dr[2];
3503 pcb->pcb_dr3 = dbregs->dr[3];
3504 pcb->pcb_dr6 = dbregs->dr[6];
3505 pcb->pcb_dr7 = dbregs->dr[7];
3507 pcb->pcb_flags |= PCB_DBREGS;
3514 * Return > 0 if a hardware breakpoint has been hit, and the
3515 * breakpoint was in user space. Return 0, otherwise.
3518 user_dbreg_trap(void)
3520 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
3521 u_int32_t bp; /* breakpoint bits extracted from dr6 */
3522 int nbp; /* number of breakpoints that triggered */
3523 caddr_t addr[4]; /* breakpoint addresses */
3527 if ((dr7 & 0x000000ff) == 0) {
3529 * all GE and LE bits in the dr7 register are zero,
3530 * thus the trap couldn't have been caused by the
3531 * hardware debug registers
3538 bp = dr6 & 0x0000000f;
3542 * None of the breakpoint bits are set meaning this
3543 * trap was not caused by any of the debug registers
3549 * at least one of the breakpoints were hit, check to see
3550 * which ones and if any of them are user space addresses
3554 addr[nbp++] = (caddr_t)rdr0();
3557 addr[nbp++] = (caddr_t)rdr1();
3560 addr[nbp++] = (caddr_t)rdr2();
3563 addr[nbp++] = (caddr_t)rdr3();
3566 for (i = 0; i < nbp; i++) {
3567 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
3569 * addr[i] is in user space
3576 * None of the breakpoints are in user space.
3582 #include <machine/apicvar.h>
3585 * Provide stub functions so that the MADT APIC enumerator in the acpi
3586 * kernel module will link against a kernel without 'device apic'.
3588 * XXX - This is a gross hack.
3591 apic_register_enumerator(struct apic_enumerator *enumerator)
3596 ioapic_create(vm_paddr_t addr, int32_t apic_id, int intbase)
3602 ioapic_disable_pin(void *cookie, u_int pin)
3608 ioapic_get_vector(void *cookie, u_int pin)
3614 ioapic_register(void *cookie)
3619 ioapic_remap_vector(void *cookie, u_int pin, int vector)
3625 ioapic_set_extint(void *cookie, u_int pin)
3631 ioapic_set_nmi(void *cookie, u_int pin)
3637 ioapic_set_polarity(void *cookie, u_int pin, enum intr_polarity pol)
3643 ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger)
3649 lapic_create(u_int apic_id, int boot_cpu)
3654 lapic_init(vm_paddr_t addr)
3659 lapic_set_lvt_mode(u_int apic_id, u_int lvt, u_int32_t mode)
3665 lapic_set_lvt_polarity(u_int apic_id, u_int lvt, enum intr_polarity pol)
3671 lapic_set_lvt_triggermode(u_int apic_id, u_int lvt, enum intr_trigger trigger)
3680 * Provide inb() and outb() as functions. They are normally only available as
3681 * inline functions, thus cannot be called from the debugger.
3684 /* silence compiler warnings */
3685 u_char inb_(u_short);
3686 void outb_(u_short, u_char);
3695 outb_(u_short port, u_char data)