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>
116 #include <pc98/pc98/pc98_machdep.h>
121 #include <net/netisr.h>
123 #include <machine/bootinfo.h>
124 #include <machine/clock.h>
125 #include <machine/cpu.h>
126 #include <machine/cputypes.h>
127 #include <machine/intr_machdep.h>
129 #include <machine/md_var.h>
130 #include <machine/metadata.h>
131 #include <machine/mp_watchdog.h>
132 #include <machine/pc/bios.h>
133 #include <machine/pcb.h>
134 #include <machine/pcb_ext.h>
135 #include <machine/proc.h>
136 #include <machine/reg.h>
137 #include <machine/sigframe.h>
138 #include <machine/specialreg.h>
139 #include <machine/vm86.h>
141 #include <machine/perfmon.h>
144 #include <machine/smp.h>
151 #include <machine/apicvar.h>
155 #include <x86/isa/icu.h>
159 #include <machine/xbox.h>
161 int arch_i386_is_xbox = 0;
162 uint32_t arch_i386_xbox_memsize = 0;
167 #include <xen/xen-os.h>
168 #include <xen/hypervisor.h>
169 #include <machine/xen/xenvar.h>
170 #include <machine/xen/xenfunc.h>
171 #include <xen/xen_intr.h>
173 void Xhypervisor_callback(void);
174 void failsafe_callback(void);
176 extern trap_info_t trap_table[];
177 struct proc_ldt default_proc_ldt;
178 extern int init_first;
180 extern unsigned long physfree;
183 /* Sanity check for __curthread() */
184 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
186 extern register_t init386(int first);
187 extern void dblfault_handler(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 char *xfpusave, size_t xfpusave_len);
200 static int set_fpcontext(struct thread *td, mcontext_t *mcp,
201 char *xfpustate, size_t xfpustate_len);
202 #ifdef CPU_ENABLE_SSE
203 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
204 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
205 #endif /* CPU_ENABLE_SSE */
206 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
209 extern vm_offset_t ksym_start, ksym_end;
212 /* Intel ICH registers */
213 #define ICH_PMBASE 0x400
214 #define ICH_SMI_EN ICH_PMBASE + 0x30
216 int _udatasel, _ucodesel;
220 int need_pre_dma_flush; /* If 1, use wbinvd befor DMA transfer. */
221 int need_post_dma_flush; /* If 1, use invd after DMA transfer. */
223 static int ispc98 = 1;
224 SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, "");
230 static void osendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
232 #ifdef COMPAT_FREEBSD4
233 static void freebsd4_sendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
240 FEATURE(pae, "Physical Address Extensions");
244 * The number of PHYSMAP entries must be one less than the number of
245 * PHYSSEG entries because the PHYSMAP entry that spans the largest
246 * physical address that is accessible by ISA DMA is split into two
249 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
251 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
252 vm_paddr_t dump_avail[PHYSMAP_SIZE + 2];
254 /* must be 2 less so 0 0 can signal end of chunks */
255 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(phys_avail[0])) - 2)
256 #define DUMP_AVAIL_ARRAY_END ((sizeof(dump_avail) / sizeof(dump_avail[0])) - 2)
258 struct kva_md_info kmi;
260 static struct trapframe proc0_tf;
261 struct pcpu __pcpu[MAXCPU];
265 struct mem_range_softc mem_range_softc;
276 * On MacBooks, we need to disallow the legacy USB circuit to
277 * generate an SMI# because this can cause several problems,
278 * namely: incorrect CPU frequency detection and failure to
280 * We do this by disabling a bit in the SMI_EN (SMI Control and
281 * Enable register) of the Intel ICH LPC Interface Bridge.
283 sysenv = getenv("smbios.system.product");
284 if (sysenv != NULL) {
285 if (strncmp(sysenv, "MacBook1,1", 10) == 0 ||
286 strncmp(sysenv, "MacBook3,1", 10) == 0 ||
287 strncmp(sysenv, "MacBook4,1", 10) == 0 ||
288 strncmp(sysenv, "MacBookPro1,1", 13) == 0 ||
289 strncmp(sysenv, "MacBookPro1,2", 13) == 0 ||
290 strncmp(sysenv, "MacBookPro3,1", 13) == 0 ||
291 strncmp(sysenv, "MacBookPro4,1", 13) == 0 ||
292 strncmp(sysenv, "Macmini1,1", 10) == 0) {
294 printf("Disabling LEGACY_USB_EN bit on "
296 outl(ICH_SMI_EN, inl(ICH_SMI_EN) & ~0x8);
303 * Good {morning,afternoon,evening,night}.
307 panicifcpuunsupported();
313 * Display physical memory if SMBIOS reports reasonable amount.
316 sysenv = getenv("smbios.memory.enabled");
317 if (sysenv != NULL) {
318 memsize = (uintmax_t)strtoul(sysenv, (char **)NULL, 10) << 10;
321 if (memsize < ptoa((uintmax_t)cnt.v_free_count))
322 memsize = ptoa((uintmax_t)Maxmem);
323 printf("real memory = %ju (%ju MB)\n", memsize, memsize >> 20);
324 realmem = atop(memsize);
327 * Display any holes after the first chunk of extended memory.
332 printf("Physical memory chunk(s):\n");
333 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
336 size = phys_avail[indx + 1] - phys_avail[indx];
338 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
339 (uintmax_t)phys_avail[indx],
340 (uintmax_t)phys_avail[indx + 1] - 1,
341 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
345 vm_ksubmap_init(&kmi);
347 printf("avail memory = %ju (%ju MB)\n",
348 ptoa((uintmax_t)cnt.v_free_count),
349 ptoa((uintmax_t)cnt.v_free_count) / 1048576);
352 * Set up buffers, so they can be used to read disk labels.
355 vm_pager_bufferinit();
362 * Send an interrupt to process.
364 * Stack is set up to allow sigcode stored
365 * at top to call routine, followed by call
366 * to sigreturn routine below. After sigreturn
367 * resets the signal mask, the stack, and the
368 * frame pointer, it returns to the user
373 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
375 struct osigframe sf, *fp;
379 struct trapframe *regs;
385 PROC_LOCK_ASSERT(p, MA_OWNED);
386 sig = ksi->ksi_signo;
388 mtx_assert(&psp->ps_mtx, MA_OWNED);
390 oonstack = sigonstack(regs->tf_esp);
392 /* Allocate space for the signal handler context. */
393 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
394 SIGISMEMBER(psp->ps_sigonstack, sig)) {
395 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
396 td->td_sigstk.ss_size - sizeof(struct osigframe));
397 #if defined(COMPAT_43)
398 td->td_sigstk.ss_flags |= SS_ONSTACK;
401 fp = (struct osigframe *)regs->tf_esp - 1;
403 /* Translate the signal if appropriate. */
404 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
405 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
407 /* Build the argument list for the signal handler. */
409 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
410 bzero(&sf.sf_siginfo, sizeof(sf.sf_siginfo));
411 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
412 /* Signal handler installed with SA_SIGINFO. */
413 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
414 sf.sf_siginfo.si_signo = sig;
415 sf.sf_siginfo.si_code = ksi->ksi_code;
416 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
419 /* Old FreeBSD-style arguments. */
420 sf.sf_arg2 = ksi->ksi_code;
421 sf.sf_addr = (register_t)ksi->ksi_addr;
422 sf.sf_ahu.sf_handler = catcher;
424 mtx_unlock(&psp->ps_mtx);
427 /* Save most if not all of trap frame. */
428 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
429 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
430 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
431 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
432 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
433 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
434 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
435 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
436 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
437 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
438 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
439 sf.sf_siginfo.si_sc.sc_gs = rgs();
440 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
442 /* Build the signal context to be used by osigreturn(). */
443 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
444 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
445 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
446 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
447 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
448 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
449 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
450 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
453 * If we're a vm86 process, we want to save the segment registers.
454 * We also change eflags to be our emulated eflags, not the actual
457 if (regs->tf_eflags & PSL_VM) {
458 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
459 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
460 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
462 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
463 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
464 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
465 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
467 if (vm86->vm86_has_vme == 0)
468 sf.sf_siginfo.si_sc.sc_ps =
469 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
470 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
472 /* See sendsig() for comments. */
473 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
477 * Copy the sigframe out to the user's stack.
479 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
481 printf("process %ld has trashed its stack\n", (long)p->p_pid);
487 regs->tf_esp = (int)fp;
488 if (p->p_sysent->sv_sigcode_base != 0) {
489 regs->tf_eip = p->p_sysent->sv_sigcode_base + szsigcode -
492 /* a.out sysentvec does not use shared page */
493 regs->tf_eip = p->p_sysent->sv_psstrings - szosigcode;
495 regs->tf_eflags &= ~(PSL_T | PSL_D);
496 regs->tf_cs = _ucodesel;
497 regs->tf_ds = _udatasel;
498 regs->tf_es = _udatasel;
499 regs->tf_fs = _udatasel;
501 regs->tf_ss = _udatasel;
503 mtx_lock(&psp->ps_mtx);
505 #endif /* COMPAT_43 */
507 #ifdef COMPAT_FREEBSD4
509 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
511 struct sigframe4 sf, *sfp;
515 struct trapframe *regs;
521 PROC_LOCK_ASSERT(p, MA_OWNED);
522 sig = ksi->ksi_signo;
524 mtx_assert(&psp->ps_mtx, MA_OWNED);
526 oonstack = sigonstack(regs->tf_esp);
528 /* Save user context. */
529 bzero(&sf, sizeof(sf));
530 sf.sf_uc.uc_sigmask = *mask;
531 sf.sf_uc.uc_stack = td->td_sigstk;
532 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
533 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
534 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
535 sf.sf_uc.uc_mcontext.mc_gs = rgs();
536 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
537 bzero(sf.sf_uc.uc_mcontext.mc_fpregs,
538 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
539 bzero(sf.sf_uc.uc_mcontext.__spare__,
540 sizeof(sf.sf_uc.uc_mcontext.__spare__));
541 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
543 /* Allocate space for the signal handler context. */
544 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
545 SIGISMEMBER(psp->ps_sigonstack, sig)) {
546 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
547 td->td_sigstk.ss_size - sizeof(struct sigframe4));
548 #if defined(COMPAT_43)
549 td->td_sigstk.ss_flags |= SS_ONSTACK;
552 sfp = (struct sigframe4 *)regs->tf_esp - 1;
554 /* Translate the signal if appropriate. */
555 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
556 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
558 /* Build the argument list for the signal handler. */
560 sf.sf_ucontext = (register_t)&sfp->sf_uc;
561 bzero(&sf.sf_si, sizeof(sf.sf_si));
562 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
563 /* Signal handler installed with SA_SIGINFO. */
564 sf.sf_siginfo = (register_t)&sfp->sf_si;
565 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
567 /* Fill in POSIX parts */
568 sf.sf_si.si_signo = sig;
569 sf.sf_si.si_code = ksi->ksi_code;
570 sf.sf_si.si_addr = ksi->ksi_addr;
572 /* Old FreeBSD-style arguments. */
573 sf.sf_siginfo = ksi->ksi_code;
574 sf.sf_addr = (register_t)ksi->ksi_addr;
575 sf.sf_ahu.sf_handler = catcher;
577 mtx_unlock(&psp->ps_mtx);
581 * If we're a vm86 process, we want to save the segment registers.
582 * We also change eflags to be our emulated eflags, not the actual
585 if (regs->tf_eflags & PSL_VM) {
586 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
587 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
589 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
590 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
591 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
592 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
594 if (vm86->vm86_has_vme == 0)
595 sf.sf_uc.uc_mcontext.mc_eflags =
596 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
597 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
600 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
601 * syscalls made by the signal handler. This just avoids
602 * wasting time for our lazy fixup of such faults. PSL_NT
603 * does nothing in vm86 mode, but vm86 programs can set it
604 * almost legitimately in probes for old cpu types.
606 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
610 * Copy the sigframe out to the user's stack.
612 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
614 printf("process %ld has trashed its stack\n", (long)p->p_pid);
620 regs->tf_esp = (int)sfp;
621 regs->tf_eip = p->p_sysent->sv_sigcode_base + szsigcode -
623 regs->tf_eflags &= ~(PSL_T | PSL_D);
624 regs->tf_cs = _ucodesel;
625 regs->tf_ds = _udatasel;
626 regs->tf_es = _udatasel;
627 regs->tf_fs = _udatasel;
628 regs->tf_ss = _udatasel;
630 mtx_lock(&psp->ps_mtx);
632 #endif /* COMPAT_FREEBSD4 */
635 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
637 struct sigframe sf, *sfp;
642 struct trapframe *regs;
643 struct segment_descriptor *sdp;
651 PROC_LOCK_ASSERT(p, MA_OWNED);
652 sig = ksi->ksi_signo;
654 mtx_assert(&psp->ps_mtx, MA_OWNED);
655 #ifdef COMPAT_FREEBSD4
656 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
657 freebsd4_sendsig(catcher, ksi, mask);
662 if (SIGISMEMBER(psp->ps_osigset, sig)) {
663 osendsig(catcher, ksi, mask);
668 oonstack = sigonstack(regs->tf_esp);
670 #ifdef CPU_ENABLE_SSE
671 if (cpu_max_ext_state_size > sizeof(union savefpu) && use_xsave) {
672 xfpusave_len = cpu_max_ext_state_size - sizeof(union savefpu);
673 xfpusave = __builtin_alloca(xfpusave_len);
682 /* Save user context. */
683 bzero(&sf, sizeof(sf));
684 sf.sf_uc.uc_sigmask = *mask;
685 sf.sf_uc.uc_stack = td->td_sigstk;
686 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
687 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
688 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
689 sf.sf_uc.uc_mcontext.mc_gs = rgs();
690 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
691 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
692 get_fpcontext(td, &sf.sf_uc.uc_mcontext, xfpusave, xfpusave_len);
695 * Unconditionally fill the fsbase and gsbase into the mcontext.
697 sdp = &td->td_pcb->pcb_fsd;
698 sf.sf_uc.uc_mcontext.mc_fsbase = sdp->sd_hibase << 24 |
700 sdp = &td->td_pcb->pcb_gsd;
701 sf.sf_uc.uc_mcontext.mc_gsbase = sdp->sd_hibase << 24 |
703 bzero(sf.sf_uc.uc_mcontext.mc_spare2,
704 sizeof(sf.sf_uc.uc_mcontext.mc_spare2));
705 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
707 /* Allocate space for the signal handler context. */
708 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
709 SIGISMEMBER(psp->ps_sigonstack, sig)) {
710 sp = td->td_sigstk.ss_sp + td->td_sigstk.ss_size;
711 #if defined(COMPAT_43)
712 td->td_sigstk.ss_flags |= SS_ONSTACK;
715 sp = (char *)regs->tf_esp - 128;
716 if (xfpusave != NULL) {
718 sp = (char *)((unsigned int)sp & ~0x3F);
719 sf.sf_uc.uc_mcontext.mc_xfpustate = (register_t)sp;
721 sp -= sizeof(struct sigframe);
723 /* Align to 16 bytes. */
724 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
726 /* Translate the signal if appropriate. */
727 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
728 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
730 /* Build the argument list for the signal handler. */
732 sf.sf_ucontext = (register_t)&sfp->sf_uc;
733 bzero(&sf.sf_si, sizeof(sf.sf_si));
734 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
735 /* Signal handler installed with SA_SIGINFO. */
736 sf.sf_siginfo = (register_t)&sfp->sf_si;
737 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
739 /* Fill in POSIX parts */
740 sf.sf_si = ksi->ksi_info;
741 sf.sf_si.si_signo = sig; /* maybe a translated signal */
743 /* Old FreeBSD-style arguments. */
744 sf.sf_siginfo = ksi->ksi_code;
745 sf.sf_addr = (register_t)ksi->ksi_addr;
746 sf.sf_ahu.sf_handler = catcher;
748 mtx_unlock(&psp->ps_mtx);
752 * If we're a vm86 process, we want to save the segment registers.
753 * We also change eflags to be our emulated eflags, not the actual
756 if (regs->tf_eflags & PSL_VM) {
757 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
758 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
760 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
761 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
762 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
763 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
765 if (vm86->vm86_has_vme == 0)
766 sf.sf_uc.uc_mcontext.mc_eflags =
767 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
768 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
771 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
772 * syscalls made by the signal handler. This just avoids
773 * wasting time for our lazy fixup of such faults. PSL_NT
774 * does nothing in vm86 mode, but vm86 programs can set it
775 * almost legitimately in probes for old cpu types.
777 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
781 * Copy the sigframe out to the user's stack.
783 if (copyout(&sf, sfp, sizeof(*sfp)) != 0 ||
784 (xfpusave != NULL && copyout(xfpusave,
785 (void *)sf.sf_uc.uc_mcontext.mc_xfpustate, xfpusave_len)
788 printf("process %ld has trashed its stack\n", (long)p->p_pid);
794 regs->tf_esp = (int)sfp;
795 regs->tf_eip = p->p_sysent->sv_sigcode_base;
796 if (regs->tf_eip == 0)
797 regs->tf_eip = p->p_sysent->sv_psstrings - szsigcode;
798 regs->tf_eflags &= ~(PSL_T | PSL_D);
799 regs->tf_cs = _ucodesel;
800 regs->tf_ds = _udatasel;
801 regs->tf_es = _udatasel;
802 regs->tf_fs = _udatasel;
803 regs->tf_ss = _udatasel;
805 mtx_lock(&psp->ps_mtx);
809 * System call to cleanup state after a signal
810 * has been taken. Reset signal mask and
811 * stack state from context left by sendsig (above).
812 * Return to previous pc and psl as specified by
813 * context left by sendsig. Check carefully to
814 * make sure that the user has not modified the
815 * state to gain improper privileges.
823 struct osigreturn_args /* {
824 struct osigcontext *sigcntxp;
827 struct osigcontext sc;
828 struct trapframe *regs;
829 struct osigcontext *scp;
834 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
839 if (eflags & PSL_VM) {
840 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
841 struct vm86_kernel *vm86;
844 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
845 * set up the vm86 area, and we can't enter vm86 mode.
847 if (td->td_pcb->pcb_ext == 0)
849 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
850 if (vm86->vm86_inited == 0)
853 /* Go back to user mode if both flags are set. */
854 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
855 ksiginfo_init_trap(&ksi);
856 ksi.ksi_signo = SIGBUS;
857 ksi.ksi_code = BUS_OBJERR;
858 ksi.ksi_addr = (void *)regs->tf_eip;
859 trapsignal(td, &ksi);
862 if (vm86->vm86_has_vme) {
863 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
864 (eflags & VME_USERCHANGE) | PSL_VM;
866 vm86->vm86_eflags = eflags; /* save VIF, VIP */
867 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
868 (eflags & VM_USERCHANGE) | PSL_VM;
870 tf->tf_vm86_ds = scp->sc_ds;
871 tf->tf_vm86_es = scp->sc_es;
872 tf->tf_vm86_fs = scp->sc_fs;
873 tf->tf_vm86_gs = scp->sc_gs;
874 tf->tf_ds = _udatasel;
875 tf->tf_es = _udatasel;
876 tf->tf_fs = _udatasel;
879 * Don't allow users to change privileged or reserved flags.
881 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
886 * Don't allow users to load a valid privileged %cs. Let the
887 * hardware check for invalid selectors, excess privilege in
888 * other selectors, invalid %eip's and invalid %esp's.
890 if (!CS_SECURE(scp->sc_cs)) {
891 ksiginfo_init_trap(&ksi);
892 ksi.ksi_signo = SIGBUS;
893 ksi.ksi_code = BUS_OBJERR;
894 ksi.ksi_trapno = T_PROTFLT;
895 ksi.ksi_addr = (void *)regs->tf_eip;
896 trapsignal(td, &ksi);
899 regs->tf_ds = scp->sc_ds;
900 regs->tf_es = scp->sc_es;
901 regs->tf_fs = scp->sc_fs;
904 /* Restore remaining registers. */
905 regs->tf_eax = scp->sc_eax;
906 regs->tf_ebx = scp->sc_ebx;
907 regs->tf_ecx = scp->sc_ecx;
908 regs->tf_edx = scp->sc_edx;
909 regs->tf_esi = scp->sc_esi;
910 regs->tf_edi = scp->sc_edi;
911 regs->tf_cs = scp->sc_cs;
912 regs->tf_ss = scp->sc_ss;
913 regs->tf_isp = scp->sc_isp;
914 regs->tf_ebp = scp->sc_fp;
915 regs->tf_esp = scp->sc_sp;
916 regs->tf_eip = scp->sc_pc;
917 regs->tf_eflags = eflags;
919 #if defined(COMPAT_43)
920 if (scp->sc_onstack & 1)
921 td->td_sigstk.ss_flags |= SS_ONSTACK;
923 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
925 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
927 return (EJUSTRETURN);
929 #endif /* COMPAT_43 */
931 #ifdef COMPAT_FREEBSD4
936 freebsd4_sigreturn(td, uap)
938 struct freebsd4_sigreturn_args /* {
939 const ucontext4 *sigcntxp;
943 struct trapframe *regs;
944 struct ucontext4 *ucp;
945 int cs, eflags, error;
948 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
953 eflags = ucp->uc_mcontext.mc_eflags;
954 if (eflags & PSL_VM) {
955 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
956 struct vm86_kernel *vm86;
959 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
960 * set up the vm86 area, and we can't enter vm86 mode.
962 if (td->td_pcb->pcb_ext == 0)
964 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
965 if (vm86->vm86_inited == 0)
968 /* Go back to user mode if both flags are set. */
969 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
970 ksiginfo_init_trap(&ksi);
971 ksi.ksi_signo = SIGBUS;
972 ksi.ksi_code = BUS_OBJERR;
973 ksi.ksi_addr = (void *)regs->tf_eip;
974 trapsignal(td, &ksi);
976 if (vm86->vm86_has_vme) {
977 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
978 (eflags & VME_USERCHANGE) | PSL_VM;
980 vm86->vm86_eflags = eflags; /* save VIF, VIP */
981 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
982 (eflags & VM_USERCHANGE) | PSL_VM;
984 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
985 tf->tf_eflags = eflags;
986 tf->tf_vm86_ds = tf->tf_ds;
987 tf->tf_vm86_es = tf->tf_es;
988 tf->tf_vm86_fs = tf->tf_fs;
989 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
990 tf->tf_ds = _udatasel;
991 tf->tf_es = _udatasel;
992 tf->tf_fs = _udatasel;
995 * Don't allow users to change privileged or reserved flags.
997 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
998 uprintf("pid %d (%s): freebsd4_sigreturn eflags = 0x%x\n",
999 td->td_proc->p_pid, td->td_name, eflags);
1004 * Don't allow users to load a valid privileged %cs. Let the
1005 * hardware check for invalid selectors, excess privilege in
1006 * other selectors, invalid %eip's and invalid %esp's.
1008 cs = ucp->uc_mcontext.mc_cs;
1009 if (!CS_SECURE(cs)) {
1010 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
1011 td->td_proc->p_pid, td->td_name, cs);
1012 ksiginfo_init_trap(&ksi);
1013 ksi.ksi_signo = SIGBUS;
1014 ksi.ksi_code = BUS_OBJERR;
1015 ksi.ksi_trapno = T_PROTFLT;
1016 ksi.ksi_addr = (void *)regs->tf_eip;
1017 trapsignal(td, &ksi);
1021 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1024 #if defined(COMPAT_43)
1025 if (ucp->uc_mcontext.mc_onstack & 1)
1026 td->td_sigstk.ss_flags |= SS_ONSTACK;
1028 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1030 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
1031 return (EJUSTRETURN);
1033 #endif /* COMPAT_FREEBSD4 */
1039 sys_sigreturn(td, uap)
1041 struct sigreturn_args /* {
1042 const struct __ucontext *sigcntxp;
1047 struct trapframe *regs;
1050 size_t xfpustate_len;
1051 int cs, eflags, error, ret;
1056 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
1060 if ((ucp->uc_mcontext.mc_flags & ~_MC_FLAG_MASK) != 0) {
1061 uprintf("pid %d (%s): sigreturn mc_flags %x\n", p->p_pid,
1062 td->td_name, ucp->uc_mcontext.mc_flags);
1065 regs = td->td_frame;
1066 eflags = ucp->uc_mcontext.mc_eflags;
1067 if (eflags & PSL_VM) {
1068 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
1069 struct vm86_kernel *vm86;
1072 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
1073 * set up the vm86 area, and we can't enter vm86 mode.
1075 if (td->td_pcb->pcb_ext == 0)
1077 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
1078 if (vm86->vm86_inited == 0)
1081 /* Go back to user mode if both flags are set. */
1082 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
1083 ksiginfo_init_trap(&ksi);
1084 ksi.ksi_signo = SIGBUS;
1085 ksi.ksi_code = BUS_OBJERR;
1086 ksi.ksi_addr = (void *)regs->tf_eip;
1087 trapsignal(td, &ksi);
1090 if (vm86->vm86_has_vme) {
1091 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
1092 (eflags & VME_USERCHANGE) | PSL_VM;
1094 vm86->vm86_eflags = eflags; /* save VIF, VIP */
1095 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
1096 (eflags & VM_USERCHANGE) | PSL_VM;
1098 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
1099 tf->tf_eflags = eflags;
1100 tf->tf_vm86_ds = tf->tf_ds;
1101 tf->tf_vm86_es = tf->tf_es;
1102 tf->tf_vm86_fs = tf->tf_fs;
1103 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
1104 tf->tf_ds = _udatasel;
1105 tf->tf_es = _udatasel;
1106 tf->tf_fs = _udatasel;
1109 * Don't allow users to change privileged or reserved flags.
1111 if (!EFL_SECURE(eflags, regs->tf_eflags)) {
1112 uprintf("pid %d (%s): sigreturn eflags = 0x%x\n",
1113 td->td_proc->p_pid, td->td_name, eflags);
1118 * Don't allow users to load a valid privileged %cs. Let the
1119 * hardware check for invalid selectors, excess privilege in
1120 * other selectors, invalid %eip's and invalid %esp's.
1122 cs = ucp->uc_mcontext.mc_cs;
1123 if (!CS_SECURE(cs)) {
1124 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
1125 td->td_proc->p_pid, td->td_name, cs);
1126 ksiginfo_init_trap(&ksi);
1127 ksi.ksi_signo = SIGBUS;
1128 ksi.ksi_code = BUS_OBJERR;
1129 ksi.ksi_trapno = T_PROTFLT;
1130 ksi.ksi_addr = (void *)regs->tf_eip;
1131 trapsignal(td, &ksi);
1135 if ((uc.uc_mcontext.mc_flags & _MC_HASFPXSTATE) != 0) {
1136 xfpustate_len = uc.uc_mcontext.mc_xfpustate_len;
1137 if (xfpustate_len > cpu_max_ext_state_size -
1138 sizeof(union savefpu)) {
1140 "pid %d (%s): sigreturn xfpusave_len = 0x%zx\n",
1141 p->p_pid, td->td_name, xfpustate_len);
1144 xfpustate = __builtin_alloca(xfpustate_len);
1145 error = copyin((const void *)uc.uc_mcontext.mc_xfpustate,
1146 xfpustate, xfpustate_len);
1149 "pid %d (%s): sigreturn copying xfpustate failed\n",
1150 p->p_pid, td->td_name);
1157 ret = set_fpcontext(td, &ucp->uc_mcontext, xfpustate,
1161 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1164 #if defined(COMPAT_43)
1165 if (ucp->uc_mcontext.mc_onstack & 1)
1166 td->td_sigstk.ss_flags |= SS_ONSTACK;
1168 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1171 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
1172 return (EJUSTRETURN);
1176 * Machine dependent boot() routine
1178 * I haven't seen anything to put here yet
1179 * Possibly some stuff might be grafted back here from boot()
1187 * Flush the D-cache for non-DMA I/O so that the I-cache can
1188 * be made coherent later.
1191 cpu_flush_dcache(void *ptr, size_t len)
1193 /* Not applicable */
1196 /* Get current clock frequency for the given cpu id. */
1198 cpu_est_clockrate(int cpu_id, uint64_t *rate)
1200 uint64_t tsc1, tsc2;
1201 uint64_t acnt, mcnt, perf;
1204 if (pcpu_find(cpu_id) == NULL || rate == NULL)
1206 if ((cpu_feature & CPUID_TSC) == 0)
1207 return (EOPNOTSUPP);
1210 * If TSC is P-state invariant and APERF/MPERF MSRs do not exist,
1211 * DELAY(9) based logic fails.
1213 if (tsc_is_invariant && !tsc_perf_stat)
1214 return (EOPNOTSUPP);
1218 /* Schedule ourselves on the indicated cpu. */
1219 thread_lock(curthread);
1220 sched_bind(curthread, cpu_id);
1221 thread_unlock(curthread);
1225 /* Calibrate by measuring a short delay. */
1226 reg = intr_disable();
1227 if (tsc_is_invariant) {
1228 wrmsr(MSR_MPERF, 0);
1229 wrmsr(MSR_APERF, 0);
1232 mcnt = rdmsr(MSR_MPERF);
1233 acnt = rdmsr(MSR_APERF);
1236 perf = 1000 * acnt / mcnt;
1237 *rate = (tsc2 - tsc1) * perf;
1243 *rate = (tsc2 - tsc1) * 1000;
1248 thread_lock(curthread);
1249 sched_unbind(curthread);
1250 thread_unlock(curthread);
1263 HYPERVISOR_sched_op(SCHEDOP_block, 0);
1269 HYPERVISOR_shutdown(SHUTDOWN_poweroff);
1272 int scheduler_running;
1275 cpu_idle_hlt(sbintime_t sbt)
1278 scheduler_running = 1;
1285 * Shutdown the CPU as much as possible
1296 void (*cpu_idle_hook)(sbintime_t) = NULL; /* ACPI idle hook. */
1297 static int cpu_ident_amdc1e = 0; /* AMD C1E supported. */
1298 static int idle_mwait = 1; /* Use MONITOR/MWAIT for short idle. */
1299 TUNABLE_INT("machdep.idle_mwait", &idle_mwait);
1300 SYSCTL_INT(_machdep, OID_AUTO, idle_mwait, CTLFLAG_RW, &idle_mwait,
1301 0, "Use MONITOR/MWAIT for short idle");
1303 #define STATE_RUNNING 0x0
1304 #define STATE_MWAIT 0x1
1305 #define STATE_SLEEPING 0x2
1309 cpu_idle_acpi(sbintime_t sbt)
1313 state = (int *)PCPU_PTR(monitorbuf);
1314 *state = STATE_SLEEPING;
1316 /* See comments in cpu_idle_hlt(). */
1318 if (sched_runnable())
1320 else if (cpu_idle_hook)
1323 __asm __volatile("sti; hlt");
1324 *state = STATE_RUNNING;
1330 cpu_idle_hlt(sbintime_t sbt)
1334 state = (int *)PCPU_PTR(monitorbuf);
1335 *state = STATE_SLEEPING;
1338 * Since we may be in a critical section from cpu_idle(), if
1339 * an interrupt fires during that critical section we may have
1340 * a pending preemption. If the CPU halts, then that thread
1341 * may not execute until a later interrupt awakens the CPU.
1342 * To handle this race, check for a runnable thread after
1343 * disabling interrupts and immediately return if one is
1344 * found. Also, we must absolutely guarentee that hlt is
1345 * the next instruction after sti. This ensures that any
1346 * interrupt that fires after the call to disable_intr() will
1347 * immediately awaken the CPU from hlt. Finally, please note
1348 * that on x86 this works fine because of interrupts enabled only
1349 * after the instruction following sti takes place, while IF is set
1350 * to 1 immediately, allowing hlt instruction to acknowledge the
1354 if (sched_runnable())
1357 __asm __volatile("sti; hlt");
1358 *state = STATE_RUNNING;
1363 cpu_idle_mwait(sbintime_t sbt)
1367 state = (int *)PCPU_PTR(monitorbuf);
1368 *state = STATE_MWAIT;
1370 /* See comments in cpu_idle_hlt(). */
1372 if (sched_runnable()) {
1374 *state = STATE_RUNNING;
1377 cpu_monitor(state, 0, 0);
1378 if (*state == STATE_MWAIT)
1379 __asm __volatile("sti; mwait" : : "a" (MWAIT_C1), "c" (0));
1382 *state = STATE_RUNNING;
1386 cpu_idle_spin(sbintime_t sbt)
1391 state = (int *)PCPU_PTR(monitorbuf);
1392 *state = STATE_RUNNING;
1395 * The sched_runnable() call is racy but as long as there is
1396 * a loop missing it one time will have just a little impact if any
1397 * (and it is much better than missing the check at all).
1399 for (i = 0; i < 1000; i++) {
1400 if (sched_runnable())
1407 * C1E renders the local APIC timer dead, so we disable it by
1408 * reading the Interrupt Pending Message register and clearing
1409 * both C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
1412 * "BIOS and Kernel Developer's Guide for AMD NPT Family 0Fh Processors"
1413 * #32559 revision 3.00+
1415 #define MSR_AMDK8_IPM 0xc0010055
1416 #define AMDK8_SMIONCMPHALT (1ULL << 27)
1417 #define AMDK8_C1EONCMPHALT (1ULL << 28)
1418 #define AMDK8_CMPHALT (AMDK8_SMIONCMPHALT | AMDK8_C1EONCMPHALT)
1421 cpu_probe_amdc1e(void)
1425 * Detect the presence of C1E capability mostly on latest
1426 * dual-cores (or future) k8 family.
1428 if (cpu_vendor_id == CPU_VENDOR_AMD &&
1429 (cpu_id & 0x00000f00) == 0x00000f00 &&
1430 (cpu_id & 0x0fff0000) >= 0x00040000) {
1431 cpu_ident_amdc1e = 1;
1435 #if defined(PC98) || defined(XEN)
1436 void (*cpu_idle_fn)(sbintime_t) = cpu_idle_hlt;
1438 void (*cpu_idle_fn)(sbintime_t) = cpu_idle_acpi;
1447 sbintime_t sbt = -1;
1449 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d",
1451 #if defined(MP_WATCHDOG) && !defined(XEN)
1452 ap_watchdog(PCPU_GET(cpuid));
1455 /* If we are busy - try to use fast methods. */
1457 if ((cpu_feature2 & CPUID2_MON) && idle_mwait) {
1458 cpu_idle_mwait(busy);
1464 /* If we have time - switch timers into idle mode. */
1467 sbt = cpu_idleclock();
1471 /* Apply AMD APIC timer C1E workaround. */
1472 if (cpu_ident_amdc1e && cpu_disable_c3_sleep) {
1473 msr = rdmsr(MSR_AMDK8_IPM);
1474 if (msr & AMDK8_CMPHALT)
1475 wrmsr(MSR_AMDK8_IPM, msr & ~AMDK8_CMPHALT);
1479 /* Call main idle method. */
1482 /* Switch timers mack into active mode. */
1490 CTR2(KTR_SPARE2, "cpu_idle(%d) at %d done",
1495 cpu_idle_wakeup(int cpu)
1500 pcpu = pcpu_find(cpu);
1501 state = (int *)pcpu->pc_monitorbuf;
1503 * This doesn't need to be atomic since missing the race will
1504 * simply result in unnecessary IPIs.
1506 if (*state == STATE_SLEEPING)
1508 if (*state == STATE_MWAIT)
1509 *state = STATE_RUNNING;
1514 * Ordered by speed/power consumption.
1520 { cpu_idle_spin, "spin" },
1521 { cpu_idle_mwait, "mwait" },
1522 { cpu_idle_hlt, "hlt" },
1524 { cpu_idle_acpi, "acpi" },
1530 idle_sysctl_available(SYSCTL_HANDLER_ARGS)
1536 avail = malloc(256, M_TEMP, M_WAITOK);
1538 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1539 if (strstr(idle_tbl[i].id_name, "mwait") &&
1540 (cpu_feature2 & CPUID2_MON) == 0)
1543 if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
1544 cpu_idle_hook == NULL)
1547 p += sprintf(p, "%s%s", p != avail ? ", " : "",
1548 idle_tbl[i].id_name);
1550 error = sysctl_handle_string(oidp, avail, 0, req);
1551 free(avail, M_TEMP);
1555 SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
1556 0, 0, idle_sysctl_available, "A", "list of available idle functions");
1559 idle_sysctl(SYSCTL_HANDLER_ARGS)
1567 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1568 if (idle_tbl[i].id_fn == cpu_idle_fn) {
1569 p = idle_tbl[i].id_name;
1573 strncpy(buf, p, sizeof(buf));
1574 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1575 if (error != 0 || req->newptr == NULL)
1577 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1578 if (strstr(idle_tbl[i].id_name, "mwait") &&
1579 (cpu_feature2 & CPUID2_MON) == 0)
1582 if (strcmp(idle_tbl[i].id_name, "acpi") == 0 &&
1583 cpu_idle_hook == NULL)
1586 if (strcmp(idle_tbl[i].id_name, buf))
1588 cpu_idle_fn = idle_tbl[i].id_fn;
1594 SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
1595 idle_sysctl, "A", "currently selected idle function");
1598 * Reset registers to default values on exec.
1601 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
1603 struct trapframe *regs = td->td_frame;
1604 struct pcb *pcb = td->td_pcb;
1606 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1607 pcb->pcb_gs = _udatasel;
1610 mtx_lock_spin(&dt_lock);
1611 if (td->td_proc->p_md.md_ldt)
1614 mtx_unlock_spin(&dt_lock);
1616 bzero((char *)regs, sizeof(struct trapframe));
1617 regs->tf_eip = imgp->entry_addr;
1618 regs->tf_esp = stack;
1619 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1620 regs->tf_ss = _udatasel;
1621 regs->tf_ds = _udatasel;
1622 regs->tf_es = _udatasel;
1623 regs->tf_fs = _udatasel;
1624 regs->tf_cs = _ucodesel;
1626 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1627 regs->tf_ebx = imgp->ps_strings;
1630 * Reset the hardware debug registers if they were in use.
1631 * They won't have any meaning for the newly exec'd process.
1633 if (pcb->pcb_flags & PCB_DBREGS) {
1640 if (pcb == curpcb) {
1642 * Clear the debug registers on the running
1643 * CPU, otherwise they will end up affecting
1644 * the next process we switch to.
1648 pcb->pcb_flags &= ~PCB_DBREGS;
1651 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1654 * Drop the FP state if we hold it, so that the process gets a
1655 * clean FP state if it uses the FPU again.
1660 * XXX - Linux emulator
1661 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1664 td->td_retval[1] = 0;
1675 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1677 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1678 * instructions. We must set the CR0_MP bit and use the CR0_TS
1679 * bit to control the trap, because setting the CR0_EM bit does
1680 * not cause WAIT instructions to trap. It's important to trap
1681 * WAIT instructions - otherwise the "wait" variants of no-wait
1682 * control instructions would degenerate to the "no-wait" variants
1683 * after FP context switches but work correctly otherwise. It's
1684 * particularly important to trap WAITs when there is no NPX -
1685 * otherwise the "wait" variants would always degenerate.
1687 * Try setting CR0_NE to get correct error reporting on 486DX's.
1688 * Setting it should fail or do nothing on lesser processors.
1690 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1695 u_long bootdev; /* not a struct cdev *- encoding is different */
1696 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1697 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1699 static char bootmethod[16] = "BIOS";
1700 SYSCTL_STRING(_machdep, OID_AUTO, bootmethod, CTLFLAG_RD, bootmethod, 0,
1701 "System firmware boot method");
1704 * Initialize 386 and configure to run kernel
1708 * Initialize segments & interrupt table
1714 union descriptor *gdt;
1715 union descriptor *ldt;
1717 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1718 union descriptor ldt[NLDT]; /* local descriptor table */
1720 static struct gate_descriptor idt0[NIDT];
1721 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1722 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1723 struct mtx dt_lock; /* lock for GDT and LDT */
1725 static struct i386tss dblfault_tss;
1726 static char dblfault_stack[PAGE_SIZE];
1728 extern vm_offset_t proc0kstack;
1732 * software prototypes -- in more palatable form.
1734 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1735 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1737 struct soft_segment_descriptor gdt_segs[] = {
1738 /* GNULL_SEL 0 Null Descriptor */
1744 .ssd_xx = 0, .ssd_xx1 = 0,
1747 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1749 .ssd_limit = 0xfffff,
1750 .ssd_type = SDT_MEMRWA,
1753 .ssd_xx = 0, .ssd_xx1 = 0,
1756 /* GUFS_SEL 2 %fs Descriptor for user */
1758 .ssd_limit = 0xfffff,
1759 .ssd_type = SDT_MEMRWA,
1762 .ssd_xx = 0, .ssd_xx1 = 0,
1765 /* GUGS_SEL 3 %gs Descriptor for user */
1767 .ssd_limit = 0xfffff,
1768 .ssd_type = SDT_MEMRWA,
1771 .ssd_xx = 0, .ssd_xx1 = 0,
1774 /* GCODE_SEL 4 Code Descriptor for kernel */
1776 .ssd_limit = 0xfffff,
1777 .ssd_type = SDT_MEMERA,
1780 .ssd_xx = 0, .ssd_xx1 = 0,
1783 /* GDATA_SEL 5 Data Descriptor for kernel */
1785 .ssd_limit = 0xfffff,
1786 .ssd_type = SDT_MEMRWA,
1789 .ssd_xx = 0, .ssd_xx1 = 0,
1792 /* GUCODE_SEL 6 Code Descriptor for user */
1794 .ssd_limit = 0xfffff,
1795 .ssd_type = SDT_MEMERA,
1798 .ssd_xx = 0, .ssd_xx1 = 0,
1801 /* GUDATA_SEL 7 Data Descriptor for user */
1803 .ssd_limit = 0xfffff,
1804 .ssd_type = SDT_MEMRWA,
1807 .ssd_xx = 0, .ssd_xx1 = 0,
1810 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1811 { .ssd_base = 0x400,
1812 .ssd_limit = 0xfffff,
1813 .ssd_type = SDT_MEMRWA,
1816 .ssd_xx = 0, .ssd_xx1 = 0,
1820 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1823 .ssd_limit = sizeof(struct i386tss)-1,
1824 .ssd_type = SDT_SYS386TSS,
1827 .ssd_xx = 0, .ssd_xx1 = 0,
1830 /* GLDT_SEL 10 LDT Descriptor */
1831 { .ssd_base = (int) ldt,
1832 .ssd_limit = sizeof(ldt)-1,
1833 .ssd_type = SDT_SYSLDT,
1836 .ssd_xx = 0, .ssd_xx1 = 0,
1839 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1840 { .ssd_base = (int) ldt,
1841 .ssd_limit = (512 * sizeof(union descriptor)-1),
1842 .ssd_type = SDT_SYSLDT,
1845 .ssd_xx = 0, .ssd_xx1 = 0,
1848 /* GPANIC_SEL 12 Panic Tss Descriptor */
1849 { .ssd_base = (int) &dblfault_tss,
1850 .ssd_limit = sizeof(struct i386tss)-1,
1851 .ssd_type = SDT_SYS386TSS,
1854 .ssd_xx = 0, .ssd_xx1 = 0,
1857 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1859 .ssd_limit = 0xfffff,
1860 .ssd_type = SDT_MEMERA,
1863 .ssd_xx = 0, .ssd_xx1 = 0,
1866 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1868 .ssd_limit = 0xfffff,
1869 .ssd_type = SDT_MEMERA,
1872 .ssd_xx = 0, .ssd_xx1 = 0,
1875 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1877 .ssd_limit = 0xfffff,
1878 .ssd_type = SDT_MEMRWA,
1881 .ssd_xx = 0, .ssd_xx1 = 0,
1884 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1886 .ssd_limit = 0xfffff,
1887 .ssd_type = SDT_MEMRWA,
1890 .ssd_xx = 0, .ssd_xx1 = 0,
1893 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1895 .ssd_limit = 0xfffff,
1896 .ssd_type = SDT_MEMRWA,
1899 .ssd_xx = 0, .ssd_xx1 = 0,
1902 /* GNDIS_SEL 18 NDIS Descriptor */
1908 .ssd_xx = 0, .ssd_xx1 = 0,
1914 static struct soft_segment_descriptor ldt_segs[] = {
1915 /* Null Descriptor - overwritten by call gate */
1921 .ssd_xx = 0, .ssd_xx1 = 0,
1924 /* Null Descriptor - overwritten by call gate */
1930 .ssd_xx = 0, .ssd_xx1 = 0,
1933 /* Null Descriptor - overwritten by call gate */
1939 .ssd_xx = 0, .ssd_xx1 = 0,
1942 /* Code Descriptor for user */
1944 .ssd_limit = 0xfffff,
1945 .ssd_type = SDT_MEMERA,
1948 .ssd_xx = 0, .ssd_xx1 = 0,
1951 /* Null Descriptor - overwritten by call gate */
1957 .ssd_xx = 0, .ssd_xx1 = 0,
1960 /* Data Descriptor for user */
1962 .ssd_limit = 0xfffff,
1963 .ssd_type = SDT_MEMRWA,
1966 .ssd_xx = 0, .ssd_xx1 = 0,
1972 setidt(idx, func, typ, dpl, selec)
1979 struct gate_descriptor *ip;
1982 ip->gd_looffset = (int)func;
1983 ip->gd_selector = selec;
1989 ip->gd_hioffset = ((int)func)>>16 ;
1993 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1994 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1995 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1996 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1998 #ifdef KDTRACE_HOOKS
2002 IDTVEC(xen_intr_upcall),
2004 IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
2008 * Display the index and function name of any IDT entries that don't use
2009 * the default 'rsvd' entry point.
2011 DB_SHOW_COMMAND(idt, db_show_idt)
2013 struct gate_descriptor *ip;
2018 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
2019 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
2020 if (func != (uintptr_t)&IDTVEC(rsvd)) {
2021 db_printf("%3d\t", idx);
2022 db_printsym(func, DB_STGY_PROC);
2029 /* Show privileged registers. */
2030 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
2032 uint64_t idtr, gdtr;
2035 db_printf("idtr\t0x%08x/%04x\n",
2036 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
2038 db_printf("gdtr\t0x%08x/%04x\n",
2039 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
2040 db_printf("ldtr\t0x%04x\n", rldt());
2041 db_printf("tr\t0x%04x\n", rtr());
2042 db_printf("cr0\t0x%08x\n", rcr0());
2043 db_printf("cr2\t0x%08x\n", rcr2());
2044 db_printf("cr3\t0x%08x\n", rcr3());
2045 db_printf("cr4\t0x%08x\n", rcr4());
2051 struct segment_descriptor *sd;
2052 struct soft_segment_descriptor *ssd;
2054 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
2055 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
2056 ssd->ssd_type = sd->sd_type;
2057 ssd->ssd_dpl = sd->sd_dpl;
2058 ssd->ssd_p = sd->sd_p;
2059 ssd->ssd_def32 = sd->sd_def32;
2060 ssd->ssd_gran = sd->sd_gran;
2063 #if !defined(PC98) && !defined(XEN)
2065 add_smap_entry(struct bios_smap *smap, vm_paddr_t *physmap, int *physmap_idxp)
2067 int i, insert_idx, physmap_idx;
2069 physmap_idx = *physmap_idxp;
2071 if (boothowto & RB_VERBOSE)
2072 printf("SMAP type=%02x base=%016llx len=%016llx\n",
2073 smap->type, smap->base, smap->length);
2075 if (smap->type != SMAP_TYPE_MEMORY)
2078 if (smap->length == 0)
2082 if (smap->base > 0xffffffff) {
2083 printf("%uK of memory above 4GB ignored\n",
2084 (u_int)(smap->length / 1024));
2090 * Find insertion point while checking for overlap. Start off by
2091 * assuming the new entry will be added to the end.
2093 insert_idx = physmap_idx + 2;
2094 for (i = 0; i <= physmap_idx; i += 2) {
2095 if (smap->base < physmap[i + 1]) {
2096 if (smap->base + smap->length <= physmap[i]) {
2100 if (boothowto & RB_VERBOSE)
2102 "Overlapping memory regions, ignoring second region\n");
2107 /* See if we can prepend to the next entry. */
2108 if (insert_idx <= physmap_idx &&
2109 smap->base + smap->length == physmap[insert_idx]) {
2110 physmap[insert_idx] = smap->base;
2114 /* See if we can append to the previous entry. */
2115 if (insert_idx > 0 && smap->base == physmap[insert_idx - 1]) {
2116 physmap[insert_idx - 1] += smap->length;
2121 *physmap_idxp = physmap_idx;
2122 if (physmap_idx == PHYSMAP_SIZE) {
2124 "Too many segments in the physical address map, giving up\n");
2129 * Move the last 'N' entries down to make room for the new
2132 for (i = physmap_idx; i > insert_idx; i -= 2) {
2133 physmap[i] = physmap[i - 2];
2134 physmap[i + 1] = physmap[i - 1];
2137 /* Insert the new entry. */
2138 physmap[insert_idx] = smap->base;
2139 physmap[insert_idx + 1] = smap->base + smap->length;
2142 #endif /* !PC98 && !XEN */
2152 if (basemem > 640) {
2153 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
2159 * XXX if biosbasemem is now < 640, there is a `hole'
2160 * between the end of base memory and the start of
2161 * ISA memory. The hole may be empty or it may
2162 * contain BIOS code or data. Map it read/write so
2163 * that the BIOS can write to it. (Memory from 0 to
2164 * the physical end of the kernel is mapped read-only
2165 * to begin with and then parts of it are remapped.
2166 * The parts that aren't remapped form holes that
2167 * remain read-only and are unused by the kernel.
2168 * The base memory area is below the physical end of
2169 * the kernel and right now forms a read-only hole.
2170 * The part of it from PAGE_SIZE to
2171 * (trunc_page(biosbasemem * 1024) - 1) will be
2172 * remapped and used by the kernel later.)
2174 * This code is similar to the code used in
2175 * pmap_mapdev, but since no memory needs to be
2176 * allocated we simply change the mapping.
2178 for (pa = trunc_page(basemem * 1024);
2179 pa < ISA_HOLE_START; pa += PAGE_SIZE)
2180 pmap_kenter(KERNBASE + pa, pa);
2183 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
2184 * the vm86 page table so that vm86 can scribble on them using
2185 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
2186 * page 0, at least as initialized here?
2188 pte = (pt_entry_t *)vm86paddr;
2189 for (i = basemem / 4; i < 160; i++)
2190 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
2195 * Populate the (physmap) array with base/bound pairs describing the
2196 * available physical memory in the system, then test this memory and
2197 * build the phys_avail array describing the actually-available memory.
2199 * If we cannot accurately determine the physical memory map, then use
2200 * value from the 0xE801 call, and failing that, the RTC.
2202 * Total memory size may be set by the kernel environment variable
2203 * hw.physmem or the compile-time define MAXMEM.
2205 * XXX first should be vm_paddr_t.
2209 getmemsize(int first)
2211 int off, physmap_idx, pa_indx, da_indx;
2212 u_long physmem_tunable, memtest;
2213 vm_paddr_t physmap[PHYSMAP_SIZE];
2215 quad_t dcons_addr, dcons_size;
2222 bzero(physmap, sizeof(physmap));
2224 /* XXX - some of EPSON machines can't use PG_N */
2226 if (pc98_machine_type & M_EPSON_PC98) {
2227 switch (epson_machine_id) {
2231 case EPSON_PC486_HX:
2232 case EPSON_PC486_HG:
2233 case EPSON_PC486_HA:
2239 under16 = pc98_getmemsize(&basemem, &extmem);
2243 physmap[1] = basemem * 1024;
2245 physmap[physmap_idx] = 0x100000;
2246 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
2249 * Now, physmap contains a map of physical memory.
2253 /* make hole for AP bootstrap code */
2254 physmap[1] = mp_bootaddress(physmap[1]);
2258 * Maxmem isn't the "maximum memory", it's one larger than the
2259 * highest page of the physical address space. It should be
2260 * called something like "Maxphyspage". We may adjust this
2261 * based on ``hw.physmem'' and the results of the memory test.
2263 Maxmem = atop(physmap[physmap_idx + 1]);
2266 Maxmem = MAXMEM / 4;
2269 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
2270 Maxmem = atop(physmem_tunable);
2273 * By default keep the memtest enabled. Use a general name so that
2274 * one could eventually do more with the code than just disable it.
2277 TUNABLE_ULONG_FETCH("hw.memtest.tests", &memtest);
2279 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
2280 (boothowto & RB_VERBOSE))
2281 printf("Physical memory use set to %ldK\n", Maxmem * 4);
2284 * If Maxmem has been increased beyond what the system has detected,
2285 * extend the last memory segment to the new limit.
2287 if (atop(physmap[physmap_idx + 1]) < Maxmem)
2288 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
2291 * We need to divide chunk if Maxmem is larger than 16MB and
2292 * under 16MB area is not full of memory.
2293 * (1) system area (15-16MB region) is cut off
2294 * (2) extended memory is only over 16MB area (ex. Melco "HYPERMEMORY")
2296 if ((under16 != 16 * 1024) && (extmem > 15 * 1024)) {
2297 /* 15M - 16M region is cut off, so need to divide chunk */
2298 physmap[physmap_idx + 1] = under16 * 1024;
2300 physmap[physmap_idx] = 0x1000000;
2301 physmap[physmap_idx + 1] = physmap[2] + extmem * 1024;
2304 /* call pmap initialization to make new kernel address space */
2305 pmap_bootstrap(first);
2308 * Size up each available chunk of physical memory.
2310 physmap[0] = PAGE_SIZE; /* mask off page 0 */
2313 phys_avail[pa_indx++] = physmap[0];
2314 phys_avail[pa_indx] = physmap[0];
2315 dump_avail[da_indx] = physmap[0];
2319 * Get dcons buffer address
2321 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
2322 getenv_quad("dcons.size", &dcons_size) == 0)
2326 * physmap is in bytes, so when converting to page boundaries,
2327 * round up the start address and round down the end address.
2329 for (i = 0; i <= physmap_idx; i += 2) {
2332 end = ptoa((vm_paddr_t)Maxmem);
2333 if (physmap[i + 1] < end)
2334 end = trunc_page(physmap[i + 1]);
2335 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
2336 int tmp, page_bad, full;
2337 int *ptr = (int *)CADDR3;
2341 * block out kernel memory as not available.
2343 if (pa >= KERNLOAD && pa < first)
2347 * block out dcons buffer
2350 && pa >= trunc_page(dcons_addr)
2351 && pa < dcons_addr + dcons_size)
2359 * map page into kernel: valid, read/write,non-cacheable
2361 *pte = pa | PG_V | PG_RW | pg_n;
2366 * Test for alternating 1's and 0's
2368 *(volatile int *)ptr = 0xaaaaaaaa;
2369 if (*(volatile int *)ptr != 0xaaaaaaaa)
2372 * Test for alternating 0's and 1's
2374 *(volatile int *)ptr = 0x55555555;
2375 if (*(volatile int *)ptr != 0x55555555)
2380 *(volatile int *)ptr = 0xffffffff;
2381 if (*(volatile int *)ptr != 0xffffffff)
2386 *(volatile int *)ptr = 0x0;
2387 if (*(volatile int *)ptr != 0x0)
2390 * Restore original value.
2396 * Adjust array of valid/good pages.
2398 if (page_bad == TRUE)
2401 * If this good page is a continuation of the
2402 * previous set of good pages, then just increase
2403 * the end pointer. Otherwise start a new chunk.
2404 * Note that "end" points one higher than end,
2405 * making the range >= start and < end.
2406 * If we're also doing a speculative memory
2407 * test and we at or past the end, bump up Maxmem
2408 * so that we keep going. The first bad page
2409 * will terminate the loop.
2411 if (phys_avail[pa_indx] == pa) {
2412 phys_avail[pa_indx] += PAGE_SIZE;
2415 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
2417 "Too many holes in the physical address space, giving up\n");
2422 phys_avail[pa_indx++] = pa; /* start */
2423 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
2427 if (dump_avail[da_indx] == pa) {
2428 dump_avail[da_indx] += PAGE_SIZE;
2431 if (da_indx == DUMP_AVAIL_ARRAY_END) {
2435 dump_avail[da_indx++] = pa; /* start */
2436 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
2448 * The last chunk must contain at least one page plus the message
2449 * buffer to avoid complicating other code (message buffer address
2450 * calculation, etc.).
2452 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
2453 round_page(msgbufsize) >= phys_avail[pa_indx]) {
2454 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
2455 phys_avail[pa_indx--] = 0;
2456 phys_avail[pa_indx--] = 0;
2459 Maxmem = atop(phys_avail[pa_indx]);
2461 /* Trim off space for the message buffer. */
2462 phys_avail[pa_indx] -= round_page(msgbufsize);
2464 /* Map the message buffer. */
2465 for (off = 0; off < round_page(msgbufsize); off += PAGE_SIZE)
2466 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
2473 getmemsize(int first)
2475 int has_smap, off, physmap_idx, pa_indx, da_indx;
2476 u_long physmem_tunable, memtest;
2477 vm_paddr_t physmap[PHYSMAP_SIZE];
2479 quad_t dcons_addr, dcons_size;
2481 int hasbrokenint12, i, res;
2483 struct vm86frame vmf;
2484 struct vm86context vmc;
2486 struct bios_smap *smap, *smapbase, *smapend;
2493 Maxmem = xen_start_info->nr_pages - init_first;
2496 physmap[0] = init_first << PAGE_SHIFT;
2497 physmap[1] = ptoa(Maxmem) - round_page(msgbufsize);
2501 if (arch_i386_is_xbox) {
2503 * We queried the memory size before, so chop off 4MB for
2504 * the framebuffer and inform the OS of this.
2507 physmap[1] = (arch_i386_xbox_memsize * 1024 * 1024) - XBOX_FB_SIZE;
2512 bzero(&vmf, sizeof(vmf));
2513 bzero(physmap, sizeof(physmap));
2517 * Check if the loader supplied an SMAP memory map. If so,
2518 * use that and do not make any VM86 calls.
2522 kmdp = preload_search_by_type("elf kernel");
2524 kmdp = preload_search_by_type("elf32 kernel");
2526 smapbase = (struct bios_smap *)preload_search_info(kmdp,
2527 MODINFO_METADATA | MODINFOMD_SMAP);
2528 if (smapbase != NULL) {
2530 * subr_module.c says:
2531 * "Consumer may safely assume that size value precedes data."
2532 * ie: an int32_t immediately precedes SMAP.
2534 smapsize = *((u_int32_t *)smapbase - 1);
2535 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
2538 for (smap = smapbase; smap < smapend; smap++)
2539 if (!add_smap_entry(smap, physmap, &physmap_idx))
2545 * Some newer BIOSes have a broken INT 12H implementation
2546 * which causes a kernel panic immediately. In this case, we
2547 * need use the SMAP to determine the base memory size.
2550 TUNABLE_INT_FETCH("hw.hasbrokenint12", &hasbrokenint12);
2551 if (hasbrokenint12 == 0) {
2552 /* Use INT12 to determine base memory size. */
2553 vm86_intcall(0x12, &vmf);
2554 basemem = vmf.vmf_ax;
2559 * Fetch the memory map with INT 15:E820. Map page 1 R/W into
2560 * the kernel page table so we can use it as a buffer. The
2561 * kernel will unmap this page later.
2563 pmap_kenter(KERNBASE + (1 << PAGE_SHIFT), 1 << PAGE_SHIFT);
2565 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
2566 res = vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
2567 KASSERT(res != 0, ("vm86_getptr() failed: address not found"));
2571 vmf.vmf_eax = 0xE820;
2572 vmf.vmf_edx = SMAP_SIG;
2573 vmf.vmf_ecx = sizeof(struct bios_smap);
2574 i = vm86_datacall(0x15, &vmf, &vmc);
2575 if (i || vmf.vmf_eax != SMAP_SIG)
2578 if (!add_smap_entry(smap, physmap, &physmap_idx))
2580 } while (vmf.vmf_ebx != 0);
2584 * If we didn't fetch the "base memory" size from INT12,
2585 * figure it out from the SMAP (or just guess).
2588 for (i = 0; i <= physmap_idx; i += 2) {
2589 if (physmap[i] == 0x00000000) {
2590 basemem = physmap[i + 1] / 1024;
2595 /* XXX: If we couldn't find basemem from SMAP, just guess. */
2601 if (physmap[1] != 0)
2605 * If we failed to find an SMAP, figure out the extended
2606 * memory size. We will then build a simple memory map with
2607 * two segments, one for "base memory" and the second for
2608 * "extended memory". Note that "extended memory" starts at a
2609 * physical address of 1MB and that both basemem and extmem
2610 * are in units of 1KB.
2612 * First, try to fetch the extended memory size via INT 15:E801.
2614 vmf.vmf_ax = 0xE801;
2615 if (vm86_intcall(0x15, &vmf) == 0) {
2616 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
2619 * If INT15:E801 fails, this is our last ditch effort
2620 * to determine the extended memory size. Currently
2621 * we prefer the RTC value over INT15:88.
2625 vm86_intcall(0x15, &vmf);
2626 extmem = vmf.vmf_ax;
2628 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
2633 * Special hack for chipsets that still remap the 384k hole when
2634 * there's 16MB of memory - this really confuses people that
2635 * are trying to use bus mastering ISA controllers with the
2636 * "16MB limit"; they only have 16MB, but the remapping puts
2637 * them beyond the limit.
2639 * If extended memory is between 15-16MB (16-17MB phys address range),
2642 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
2646 physmap[1] = basemem * 1024;
2648 physmap[physmap_idx] = 0x100000;
2649 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
2654 * Now, physmap contains a map of physical memory.
2658 /* make hole for AP bootstrap code */
2659 physmap[1] = mp_bootaddress(physmap[1]);
2663 * Maxmem isn't the "maximum memory", it's one larger than the
2664 * highest page of the physical address space. It should be
2665 * called something like "Maxphyspage". We may adjust this
2666 * based on ``hw.physmem'' and the results of the memory test.
2668 Maxmem = atop(physmap[physmap_idx + 1]);
2671 Maxmem = MAXMEM / 4;
2674 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
2675 Maxmem = atop(physmem_tunable);
2678 * If we have an SMAP, don't allow MAXMEM or hw.physmem to extend
2679 * the amount of memory in the system.
2681 if (has_smap && Maxmem > atop(physmap[physmap_idx + 1]))
2682 Maxmem = atop(physmap[physmap_idx + 1]);
2685 * By default enable the memory test on real hardware, and disable
2686 * it if we appear to be running in a VM. This avoids touching all
2687 * pages unnecessarily, which doesn't matter on real hardware but is
2688 * bad for shared VM hosts. Use a general name so that
2689 * one could eventually do more with the code than just disable it.
2691 memtest = (vm_guest > VM_GUEST_NO) ? 0 : 1;
2692 TUNABLE_ULONG_FETCH("hw.memtest.tests", &memtest);
2694 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
2695 (boothowto & RB_VERBOSE))
2696 printf("Physical memory use set to %ldK\n", Maxmem * 4);
2699 * If Maxmem has been increased beyond what the system has detected,
2700 * extend the last memory segment to the new limit.
2702 if (atop(physmap[physmap_idx + 1]) < Maxmem)
2703 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
2705 /* call pmap initialization to make new kernel address space */
2706 pmap_bootstrap(first);
2709 * Size up each available chunk of physical memory.
2711 physmap[0] = PAGE_SIZE; /* mask off page 0 */
2714 phys_avail[pa_indx++] = physmap[0];
2715 phys_avail[pa_indx] = physmap[0];
2716 dump_avail[da_indx] = physmap[0];
2720 * Get dcons buffer address
2722 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
2723 getenv_quad("dcons.size", &dcons_size) == 0)
2728 * physmap is in bytes, so when converting to page boundaries,
2729 * round up the start address and round down the end address.
2731 for (i = 0; i <= physmap_idx; i += 2) {
2734 end = ptoa((vm_paddr_t)Maxmem);
2735 if (physmap[i + 1] < end)
2736 end = trunc_page(physmap[i + 1]);
2737 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
2738 int tmp, page_bad, full;
2739 int *ptr = (int *)CADDR3;
2743 * block out kernel memory as not available.
2745 if (pa >= KERNLOAD && pa < first)
2749 * block out dcons buffer
2752 && pa >= trunc_page(dcons_addr)
2753 && pa < dcons_addr + dcons_size)
2761 * map page into kernel: valid, read/write,non-cacheable
2763 *pte = pa | PG_V | PG_RW | PG_N;
2768 * Test for alternating 1's and 0's
2770 *(volatile int *)ptr = 0xaaaaaaaa;
2771 if (*(volatile int *)ptr != 0xaaaaaaaa)
2774 * Test for alternating 0's and 1's
2776 *(volatile int *)ptr = 0x55555555;
2777 if (*(volatile int *)ptr != 0x55555555)
2782 *(volatile int *)ptr = 0xffffffff;
2783 if (*(volatile int *)ptr != 0xffffffff)
2788 *(volatile int *)ptr = 0x0;
2789 if (*(volatile int *)ptr != 0x0)
2792 * Restore original value.
2798 * Adjust array of valid/good pages.
2800 if (page_bad == TRUE)
2803 * If this good page is a continuation of the
2804 * previous set of good pages, then just increase
2805 * the end pointer. Otherwise start a new chunk.
2806 * Note that "end" points one higher than end,
2807 * making the range >= start and < end.
2808 * If we're also doing a speculative memory
2809 * test and we at or past the end, bump up Maxmem
2810 * so that we keep going. The first bad page
2811 * will terminate the loop.
2813 if (phys_avail[pa_indx] == pa) {
2814 phys_avail[pa_indx] += PAGE_SIZE;
2817 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
2819 "Too many holes in the physical address space, giving up\n");
2824 phys_avail[pa_indx++] = pa; /* start */
2825 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
2829 if (dump_avail[da_indx] == pa) {
2830 dump_avail[da_indx] += PAGE_SIZE;
2833 if (da_indx == DUMP_AVAIL_ARRAY_END) {
2837 dump_avail[da_indx++] = pa; /* start */
2838 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
2848 phys_avail[0] = physfree;
2849 phys_avail[1] = xen_start_info->nr_pages*PAGE_SIZE;
2851 dump_avail[1] = xen_start_info->nr_pages*PAGE_SIZE;
2857 * The last chunk must contain at least one page plus the message
2858 * buffer to avoid complicating other code (message buffer address
2859 * calculation, etc.).
2861 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
2862 round_page(msgbufsize) >= phys_avail[pa_indx]) {
2863 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
2864 phys_avail[pa_indx--] = 0;
2865 phys_avail[pa_indx--] = 0;
2868 Maxmem = atop(phys_avail[pa_indx]);
2870 /* Trim off space for the message buffer. */
2871 phys_avail[pa_indx] -= round_page(msgbufsize);
2873 /* Map the message buffer. */
2874 for (off = 0; off < round_page(msgbufsize); off += PAGE_SIZE)
2875 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
2883 #define MTOPSIZE (1<<(14 + PAGE_SHIFT))
2889 unsigned long gdtmachpfn;
2890 int error, gsel_tss, metadata_missing, x, pa;
2892 #ifdef CPU_ENABLE_SSE
2893 struct xstate_hdr *xhdr;
2895 struct callback_register event = {
2896 .type = CALLBACKTYPE_event,
2897 .address = {GSEL(GCODE_SEL, SEL_KPL), (unsigned long)Xhypervisor_callback },
2899 struct callback_register failsafe = {
2900 .type = CALLBACKTYPE_failsafe,
2901 .address = {GSEL(GCODE_SEL, SEL_KPL), (unsigned long)failsafe_callback },
2904 thread0.td_kstack = proc0kstack;
2905 thread0.td_kstack_pages = KSTACK_PAGES;
2908 * This may be done better later if it gets more high level
2909 * components in it. If so just link td->td_proc here.
2911 proc_linkup0(&proc0, &thread0);
2913 metadata_missing = 0;
2914 if (xen_start_info->mod_start) {
2915 preload_metadata = (caddr_t)xen_start_info->mod_start;
2916 preload_bootstrap_relocate(KERNBASE);
2918 metadata_missing = 1;
2921 kern_envp = static_env;
2922 else if ((caddr_t)xen_start_info->cmd_line)
2923 kern_envp = xen_setbootenv((caddr_t)xen_start_info->cmd_line);
2925 boothowto |= xen_boothowto(kern_envp);
2927 /* Init basic tunables, hz etc */
2931 * XEN occupies a portion of the upper virtual address space
2932 * At its base it manages an array mapping machine page frames
2933 * to physical page frames - hence we need to be able to
2934 * access 4GB - (64MB - 4MB + 64k)
2936 gdt_segs[GPRIV_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2937 gdt_segs[GUFS_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2938 gdt_segs[GUGS_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2939 gdt_segs[GCODE_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2940 gdt_segs[GDATA_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2941 gdt_segs[GUCODE_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2942 gdt_segs[GUDATA_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2943 gdt_segs[GBIOSLOWMEM_SEL].ssd_limit = atop(HYPERVISOR_VIRT_START + MTOPSIZE);
2946 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2947 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2949 PT_SET_MA(gdt, xpmap_ptom(VTOP(gdt)) | PG_V | PG_RW);
2950 bzero(gdt, PAGE_SIZE);
2951 for (x = 0; x < NGDT; x++)
2952 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2954 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2956 gdtmachpfn = vtomach(gdt) >> PAGE_SHIFT;
2957 PT_SET_MA(gdt, xpmap_ptom(VTOP(gdt)) | PG_V);
2958 PANIC_IF(HYPERVISOR_set_gdt(&gdtmachpfn, 512) != 0);
2962 if ((error = HYPERVISOR_set_trap_table(trap_table)) != 0) {
2963 panic("set_trap_table failed - error %d\n", error);
2966 error = HYPERVISOR_callback_op(CALLBACKOP_register, &event);
2968 error = HYPERVISOR_callback_op(CALLBACKOP_register, &failsafe);
2969 #if CONFIG_XEN_COMPAT <= 0x030002
2970 if (error == -ENOXENSYS)
2971 HYPERVISOR_set_callbacks(GSEL(GCODE_SEL, SEL_KPL),
2972 (unsigned long)Xhypervisor_callback,
2973 GSEL(GCODE_SEL, SEL_KPL), (unsigned long)failsafe_callback);
2975 pcpu_init(pc, 0, sizeof(struct pcpu));
2976 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2977 pmap_kenter(pa + KERNBASE, pa);
2978 dpcpu_init((void *)(first + KERNBASE), 0);
2979 first += DPCPU_SIZE;
2980 physfree += DPCPU_SIZE;
2981 init_first += DPCPU_SIZE / PAGE_SIZE;
2983 PCPU_SET(prvspace, pc);
2984 PCPU_SET(curthread, &thread0);
2987 * Initialize mutexes.
2989 * icu_lock: in order to allow an interrupt to occur in a critical
2990 * section, to set pcpu->ipending (etc...) properly, we
2991 * must be able to get the icu lock, so it can't be
2995 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2997 /* make ldt memory segments */
2998 PT_SET_MA(ldt, xpmap_ptom(VTOP(ldt)) | PG_V | PG_RW);
2999 bzero(ldt, PAGE_SIZE);
3000 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
3001 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
3002 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
3003 ssdtosd(&ldt_segs[x], &ldt[x].sd);
3005 default_proc_ldt.ldt_base = (caddr_t)ldt;
3006 default_proc_ldt.ldt_len = 6;
3007 _default_ldt = (int)&default_proc_ldt;
3008 PCPU_SET(currentldt, _default_ldt);
3009 PT_SET_MA(ldt, *vtopte((unsigned long)ldt) & ~PG_RW);
3010 xen_set_ldt((unsigned long) ldt, (sizeof ldt_segs / sizeof ldt_segs[0]));
3012 #if defined(XEN_PRIVILEGED)
3014 * Initialize the i8254 before the console so that console
3015 * initialization can use DELAY().
3021 * Initialize the console before we print anything out.
3025 if (metadata_missing)
3026 printf("WARNING: loader(8) metadata is missing!\n");
3033 /* Reset and mask the atpics and leave them shut down. */
3037 * Point the ICU spurious interrupt vectors at the APIC spurious
3038 * interrupt handler.
3040 setidt(IDT_IO_INTS + 7, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
3041 GSEL(GCODE_SEL, SEL_KPL));
3042 setidt(IDT_IO_INTS + 15, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
3043 GSEL(GCODE_SEL, SEL_KPL));
3048 ksym_start = bootinfo.bi_symtab;
3049 ksym_end = bootinfo.bi_esymtab;
3055 if (boothowto & RB_KDB)
3056 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
3059 finishidentcpu(); /* Final stage of CPU initialization */
3060 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
3061 GSEL(GCODE_SEL, SEL_KPL));
3062 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
3063 GSEL(GCODE_SEL, SEL_KPL));
3064 initializecpu(); /* Initialize CPU registers */
3065 initializecpucache();
3067 /* pointer to selector slot for %fs/%gs */
3068 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
3070 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
3071 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
3072 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
3073 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
3074 #if defined(PAE) || defined(PAE_TABLES)
3075 dblfault_tss.tss_cr3 = (int)IdlePDPT;
3077 dblfault_tss.tss_cr3 = (int)IdlePTD;
3079 dblfault_tss.tss_eip = (int)dblfault_handler;
3080 dblfault_tss.tss_eflags = PSL_KERNEL;
3081 dblfault_tss.tss_ds = dblfault_tss.tss_es =
3082 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
3083 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
3084 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
3085 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
3089 init_param2(physmem);
3091 /* now running on new page tables, configured,and u/iom is accessible */
3093 msgbufinit(msgbufp, msgbufsize);
3098 * Set up thread0 pcb after npxinit calculated pcb + fpu save
3099 * area size. Zero out the extended state header in fpu save
3102 thread0.td_pcb = get_pcb_td(&thread0);
3103 bzero(get_pcb_user_save_td(&thread0), cpu_max_ext_state_size);
3104 #ifdef CPU_ENABLE_SSE
3106 xhdr = (struct xstate_hdr *)(get_pcb_user_save_td(&thread0) +
3108 xhdr->xstate_bv = xsave_mask;
3111 PCPU_SET(curpcb, thread0.td_pcb);
3112 /* make an initial tss so cpu can get interrupt stack on syscall! */
3113 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
3114 PCPU_SET(common_tss.tss_esp0, (vm_offset_t)thread0.td_pcb - 16);
3115 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
3116 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
3117 HYPERVISOR_stack_switch(GSEL(GDATA_SEL, SEL_KPL),
3118 PCPU_GET(common_tss.tss_esp0));
3120 /* transfer to user mode */
3122 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
3123 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
3125 /* setup proc 0's pcb */
3126 thread0.td_pcb->pcb_flags = 0;
3127 #if defined(PAE) || defined(PAE_TABLES)
3128 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
3130 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
3132 thread0.td_pcb->pcb_ext = 0;
3133 thread0.td_frame = &proc0_tf;
3134 thread0.td_pcb->pcb_fsd = PCPU_GET(fsgs_gdt)[0];
3135 thread0.td_pcb->pcb_gsd = PCPU_GET(fsgs_gdt)[1];
3139 /* Location of kernel stack for locore */
3140 return ((register_t)thread0.td_pcb);
3148 struct gate_descriptor *gdp;
3149 int gsel_tss, metadata_missing, x, pa;
3151 #ifdef CPU_ENABLE_SSE
3152 struct xstate_hdr *xhdr;
3155 thread0.td_kstack = proc0kstack;
3156 thread0.td_kstack_pages = KSTACK_PAGES;
3159 * This may be done better later if it gets more high level
3160 * components in it. If so just link td->td_proc here.
3162 proc_linkup0(&proc0, &thread0);
3171 metadata_missing = 0;
3172 if (bootinfo.bi_modulep) {
3173 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
3174 preload_bootstrap_relocate(KERNBASE);
3176 metadata_missing = 1;
3179 kern_envp = static_env;
3180 else if (bootinfo.bi_envp)
3181 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
3183 /* Init basic tunables, hz etc */
3187 * Make gdt memory segments. All segments cover the full 4GB
3188 * of address space and permissions are enforced at page level.
3190 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
3191 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
3192 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
3193 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
3194 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
3195 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
3198 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
3199 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
3200 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
3202 for (x = 0; x < NGDT; x++)
3203 ssdtosd(&gdt_segs[x], &gdt[x].sd);
3205 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
3206 r_gdt.rd_base = (int) gdt;
3207 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
3210 pcpu_init(pc, 0, sizeof(struct pcpu));
3211 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
3212 pmap_kenter(pa + KERNBASE, pa);
3213 dpcpu_init((void *)(first + KERNBASE), 0);
3214 first += DPCPU_SIZE;
3215 PCPU_SET(prvspace, pc);
3216 PCPU_SET(curthread, &thread0);
3219 * Initialize mutexes.
3221 * icu_lock: in order to allow an interrupt to occur in a critical
3222 * section, to set pcpu->ipending (etc...) properly, we
3223 * must be able to get the icu lock, so it can't be
3227 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
3229 /* make ldt memory segments */
3230 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
3231 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
3232 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
3233 ssdtosd(&ldt_segs[x], &ldt[x].sd);
3235 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
3237 PCPU_SET(currentldt, _default_ldt);
3240 for (x = 0; x < NIDT; x++)
3241 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
3242 GSEL(GCODE_SEL, SEL_KPL));
3243 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
3244 GSEL(GCODE_SEL, SEL_KPL));
3245 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
3246 GSEL(GCODE_SEL, SEL_KPL));
3247 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
3248 GSEL(GCODE_SEL, SEL_KPL));
3249 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
3250 GSEL(GCODE_SEL, SEL_KPL));
3251 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
3252 GSEL(GCODE_SEL, SEL_KPL));
3253 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
3254 GSEL(GCODE_SEL, SEL_KPL));
3255 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
3256 GSEL(GCODE_SEL, SEL_KPL));
3257 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
3258 , GSEL(GCODE_SEL, SEL_KPL));
3259 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
3260 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
3261 GSEL(GCODE_SEL, SEL_KPL));
3262 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
3263 GSEL(GCODE_SEL, SEL_KPL));
3264 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
3265 GSEL(GCODE_SEL, SEL_KPL));
3266 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
3267 GSEL(GCODE_SEL, SEL_KPL));
3268 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
3269 GSEL(GCODE_SEL, SEL_KPL));
3270 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
3271 GSEL(GCODE_SEL, SEL_KPL));
3272 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
3273 GSEL(GCODE_SEL, SEL_KPL));
3274 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
3275 GSEL(GCODE_SEL, SEL_KPL));
3276 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
3277 GSEL(GCODE_SEL, SEL_KPL));
3278 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
3279 GSEL(GCODE_SEL, SEL_KPL));
3280 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
3281 GSEL(GCODE_SEL, SEL_KPL));
3282 #ifdef KDTRACE_HOOKS
3283 setidt(IDT_DTRACE_RET, &IDTVEC(dtrace_ret), SDT_SYS386TGT, SEL_UPL,
3284 GSEL(GCODE_SEL, SEL_KPL));
3287 setidt(IDT_EVTCHN, &IDTVEC(xen_intr_upcall), SDT_SYS386IGT, SEL_UPL,
3288 GSEL(GCODE_SEL, SEL_KPL));
3291 r_idt.rd_limit = sizeof(idt0) - 1;
3292 r_idt.rd_base = (int) idt;
3297 * The following code queries the PCI ID of 0:0:0. For the XBOX,
3298 * This should be 0x10de / 0x02a5.
3300 * This is exactly what Linux does.
3302 outl(0xcf8, 0x80000000);
3303 if (inl(0xcfc) == 0x02a510de) {
3304 arch_i386_is_xbox = 1;
3305 pic16l_setled(XBOX_LED_GREEN);
3308 * We are an XBOX, but we may have either 64MB or 128MB of
3309 * memory. The PCI host bridge should be programmed for this,
3310 * so we just query it.
3312 outl(0xcf8, 0x80000084);
3313 arch_i386_xbox_memsize = (inl(0xcfc) == 0x7FFFFFF) ? 128 : 64;
3318 * Initialize the i8254 before the console so that console
3319 * initialization can use DELAY().
3324 * Initialize the console before we print anything out.
3328 if (metadata_missing)
3329 printf("WARNING: loader(8) metadata is missing!\n");
3338 /* Reset and mask the atpics and leave them shut down. */
3342 * Point the ICU spurious interrupt vectors at the APIC spurious
3343 * interrupt handler.
3345 setidt(IDT_IO_INTS + 7, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
3346 GSEL(GCODE_SEL, SEL_KPL));
3347 setidt(IDT_IO_INTS + 15, IDTVEC(spuriousint), SDT_SYS386IGT, SEL_KPL,
3348 GSEL(GCODE_SEL, SEL_KPL));
3353 ksym_start = bootinfo.bi_symtab;
3354 ksym_end = bootinfo.bi_esymtab;
3360 if (boothowto & RB_KDB)
3361 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
3364 finishidentcpu(); /* Final stage of CPU initialization */
3365 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
3366 GSEL(GCODE_SEL, SEL_KPL));
3367 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
3368 GSEL(GCODE_SEL, SEL_KPL));
3369 initializecpu(); /* Initialize CPU registers */
3370 initializecpucache();
3372 /* pointer to selector slot for %fs/%gs */
3373 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
3375 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
3376 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
3377 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
3378 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
3379 #if defined(PAE) || defined(PAE_TABLES)
3380 dblfault_tss.tss_cr3 = (int)IdlePDPT;
3382 dblfault_tss.tss_cr3 = (int)IdlePTD;
3384 dblfault_tss.tss_eip = (int)dblfault_handler;
3385 dblfault_tss.tss_eflags = PSL_KERNEL;
3386 dblfault_tss.tss_ds = dblfault_tss.tss_es =
3387 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
3388 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
3389 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
3390 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
3394 init_param2(physmem);
3396 /* now running on new page tables, configured,and u/iom is accessible */
3398 msgbufinit(msgbufp, msgbufsize);
3403 * Set up thread0 pcb after npxinit calculated pcb + fpu save
3404 * area size. Zero out the extended state header in fpu save
3407 thread0.td_pcb = get_pcb_td(&thread0);
3408 bzero(get_pcb_user_save_td(&thread0), cpu_max_ext_state_size);
3409 #ifdef CPU_ENABLE_SSE
3411 xhdr = (struct xstate_hdr *)(get_pcb_user_save_td(&thread0) +
3413 xhdr->xstate_bv = xsave_mask;
3416 PCPU_SET(curpcb, thread0.td_pcb);
3417 /* make an initial tss so cpu can get interrupt stack on syscall! */
3418 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
3419 PCPU_SET(common_tss.tss_esp0, (vm_offset_t)thread0.td_pcb - 16);
3420 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
3421 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
3422 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
3423 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
3424 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
3427 /* make a call gate to reenter kernel with */
3428 gdp = &ldt[LSYS5CALLS_SEL].gd;
3430 x = (int) &IDTVEC(lcall_syscall);
3431 gdp->gd_looffset = x;
3432 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
3434 gdp->gd_type = SDT_SYS386CGT;
3435 gdp->gd_dpl = SEL_UPL;
3437 gdp->gd_hioffset = x >> 16;
3439 /* XXX does this work? */
3441 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
3442 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
3444 /* transfer to user mode */
3446 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
3447 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
3449 /* setup proc 0's pcb */
3450 thread0.td_pcb->pcb_flags = 0;
3451 #if defined(PAE) || defined(PAE_TABLES)
3452 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
3454 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
3456 thread0.td_pcb->pcb_ext = 0;
3457 thread0.td_frame = &proc0_tf;
3465 /* Location of kernel stack for locore */
3466 return ((register_t)thread0.td_pcb);
3471 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
3474 pcpu->pc_acpi_id = 0xffffffff;
3479 smap_sysctl_handler(SYSCTL_HANDLER_ARGS)
3481 struct bios_smap *smapbase;
3482 struct bios_smap_xattr smap;
3485 int count, error, i;
3487 /* Retrieve the system memory map from the loader. */
3488 kmdp = preload_search_by_type("elf kernel");
3490 kmdp = preload_search_by_type("elf32 kernel");
3493 smapbase = (struct bios_smap *)preload_search_info(kmdp,
3494 MODINFO_METADATA | MODINFOMD_SMAP);
3495 if (smapbase == NULL)
3497 smapattr = (uint32_t *)preload_search_info(kmdp,
3498 MODINFO_METADATA | MODINFOMD_SMAP_XATTR);
3499 count = *((u_int32_t *)smapbase - 1) / sizeof(*smapbase);
3501 for (i = 0; i < count; i++) {
3502 smap.base = smapbase[i].base;
3503 smap.length = smapbase[i].length;
3504 smap.type = smapbase[i].type;
3505 if (smapattr != NULL)
3506 smap.xattr = smapattr[i];
3509 error = SYSCTL_OUT(req, &smap, sizeof(smap));
3513 SYSCTL_PROC(_machdep, OID_AUTO, smap, CTLTYPE_OPAQUE|CTLFLAG_RD, NULL, 0,
3514 smap_sysctl_handler, "S,bios_smap_xattr", "Raw BIOS SMAP data");
3518 spinlock_enter(void)
3524 if (td->td_md.md_spinlock_count == 0) {
3525 flags = intr_disable();
3526 td->td_md.md_spinlock_count = 1;
3527 td->td_md.md_saved_flags = flags;
3529 td->td_md.md_spinlock_count++;
3541 flags = td->td_md.md_saved_flags;
3542 td->td_md.md_spinlock_count--;
3543 if (td->td_md.md_spinlock_count == 0)
3544 intr_restore(flags);
3547 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
3548 static void f00f_hack(void *unused);
3549 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
3552 f00f_hack(void *unused)
3554 struct gate_descriptor *new_idt;
3562 printf("Intel Pentium detected, installing workaround for F00F bug\n");
3564 tmp = kmem_malloc(kernel_arena, PAGE_SIZE * 2, M_WAITOK | M_ZERO);
3566 panic("kmem_malloc returned 0");
3568 /* Put the problematic entry (#6) at the end of the lower page. */
3569 new_idt = (struct gate_descriptor*)
3570 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
3571 bcopy(idt, new_idt, sizeof(idt0));
3572 r_idt.rd_base = (u_int)new_idt;
3575 pmap_protect(kernel_pmap, tmp, tmp + PAGE_SIZE, VM_PROT_READ);
3577 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
3580 * Construct a PCB from a trapframe. This is called from kdb_trap() where
3581 * we want to start a backtrace from the function that caused us to enter
3582 * the debugger. We have the context in the trapframe, but base the trace
3583 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
3584 * enough for a backtrace.
3587 makectx(struct trapframe *tf, struct pcb *pcb)
3590 pcb->pcb_edi = tf->tf_edi;
3591 pcb->pcb_esi = tf->tf_esi;
3592 pcb->pcb_ebp = tf->tf_ebp;
3593 pcb->pcb_ebx = tf->tf_ebx;
3594 pcb->pcb_eip = tf->tf_eip;
3595 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
3599 ptrace_set_pc(struct thread *td, u_long addr)
3602 td->td_frame->tf_eip = addr;
3607 ptrace_single_step(struct thread *td)
3609 td->td_frame->tf_eflags |= PSL_T;
3614 ptrace_clear_single_step(struct thread *td)
3616 td->td_frame->tf_eflags &= ~PSL_T;
3621 fill_regs(struct thread *td, struct reg *regs)
3624 struct trapframe *tp;
3628 regs->r_gs = pcb->pcb_gs;
3629 return (fill_frame_regs(tp, regs));
3633 fill_frame_regs(struct trapframe *tp, struct reg *regs)
3635 regs->r_fs = tp->tf_fs;
3636 regs->r_es = tp->tf_es;
3637 regs->r_ds = tp->tf_ds;
3638 regs->r_edi = tp->tf_edi;
3639 regs->r_esi = tp->tf_esi;
3640 regs->r_ebp = tp->tf_ebp;
3641 regs->r_ebx = tp->tf_ebx;
3642 regs->r_edx = tp->tf_edx;
3643 regs->r_ecx = tp->tf_ecx;
3644 regs->r_eax = tp->tf_eax;
3645 regs->r_eip = tp->tf_eip;
3646 regs->r_cs = tp->tf_cs;
3647 regs->r_eflags = tp->tf_eflags;
3648 regs->r_esp = tp->tf_esp;
3649 regs->r_ss = tp->tf_ss;
3654 set_regs(struct thread *td, struct reg *regs)
3657 struct trapframe *tp;
3660 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
3661 !CS_SECURE(regs->r_cs))
3664 tp->tf_fs = regs->r_fs;
3665 tp->tf_es = regs->r_es;
3666 tp->tf_ds = regs->r_ds;
3667 tp->tf_edi = regs->r_edi;
3668 tp->tf_esi = regs->r_esi;
3669 tp->tf_ebp = regs->r_ebp;
3670 tp->tf_ebx = regs->r_ebx;
3671 tp->tf_edx = regs->r_edx;
3672 tp->tf_ecx = regs->r_ecx;
3673 tp->tf_eax = regs->r_eax;
3674 tp->tf_eip = regs->r_eip;
3675 tp->tf_cs = regs->r_cs;
3676 tp->tf_eflags = regs->r_eflags;
3677 tp->tf_esp = regs->r_esp;
3678 tp->tf_ss = regs->r_ss;
3679 pcb->pcb_gs = regs->r_gs;
3683 #ifdef CPU_ENABLE_SSE
3685 fill_fpregs_xmm(sv_xmm, sv_87)
3686 struct savexmm *sv_xmm;
3687 struct save87 *sv_87;
3689 register struct env87 *penv_87 = &sv_87->sv_env;
3690 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
3693 bzero(sv_87, sizeof(*sv_87));
3695 /* FPU control/status */
3696 penv_87->en_cw = penv_xmm->en_cw;
3697 penv_87->en_sw = penv_xmm->en_sw;
3698 penv_87->en_tw = penv_xmm->en_tw;
3699 penv_87->en_fip = penv_xmm->en_fip;
3700 penv_87->en_fcs = penv_xmm->en_fcs;
3701 penv_87->en_opcode = penv_xmm->en_opcode;
3702 penv_87->en_foo = penv_xmm->en_foo;
3703 penv_87->en_fos = penv_xmm->en_fos;
3706 for (i = 0; i < 8; ++i)
3707 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
3711 set_fpregs_xmm(sv_87, sv_xmm)
3712 struct save87 *sv_87;
3713 struct savexmm *sv_xmm;
3715 register struct env87 *penv_87 = &sv_87->sv_env;
3716 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
3719 /* FPU control/status */
3720 penv_xmm->en_cw = penv_87->en_cw;
3721 penv_xmm->en_sw = penv_87->en_sw;
3722 penv_xmm->en_tw = penv_87->en_tw;
3723 penv_xmm->en_fip = penv_87->en_fip;
3724 penv_xmm->en_fcs = penv_87->en_fcs;
3725 penv_xmm->en_opcode = penv_87->en_opcode;
3726 penv_xmm->en_foo = penv_87->en_foo;
3727 penv_xmm->en_fos = penv_87->en_fos;
3730 for (i = 0; i < 8; ++i)
3731 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
3733 #endif /* CPU_ENABLE_SSE */
3736 fill_fpregs(struct thread *td, struct fpreg *fpregs)
3739 KASSERT(td == curthread || TD_IS_SUSPENDED(td) ||
3740 P_SHOULDSTOP(td->td_proc),
3741 ("not suspended thread %p", td));
3745 bzero(fpregs, sizeof(*fpregs));
3747 #ifdef CPU_ENABLE_SSE
3749 fill_fpregs_xmm(&get_pcb_user_save_td(td)->sv_xmm,
3750 (struct save87 *)fpregs);
3752 #endif /* CPU_ENABLE_SSE */
3753 bcopy(&get_pcb_user_save_td(td)->sv_87, fpregs,
3759 set_fpregs(struct thread *td, struct fpreg *fpregs)
3762 #ifdef CPU_ENABLE_SSE
3764 set_fpregs_xmm((struct save87 *)fpregs,
3765 &get_pcb_user_save_td(td)->sv_xmm);
3767 #endif /* CPU_ENABLE_SSE */
3768 bcopy(fpregs, &get_pcb_user_save_td(td)->sv_87,
3777 * Get machine context.
3780 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
3782 struct trapframe *tp;
3783 struct segment_descriptor *sdp;
3787 PROC_LOCK(curthread->td_proc);
3788 mcp->mc_onstack = sigonstack(tp->tf_esp);
3789 PROC_UNLOCK(curthread->td_proc);
3790 mcp->mc_gs = td->td_pcb->pcb_gs;
3791 mcp->mc_fs = tp->tf_fs;
3792 mcp->mc_es = tp->tf_es;
3793 mcp->mc_ds = tp->tf_ds;
3794 mcp->mc_edi = tp->tf_edi;
3795 mcp->mc_esi = tp->tf_esi;
3796 mcp->mc_ebp = tp->tf_ebp;
3797 mcp->mc_isp = tp->tf_isp;
3798 mcp->mc_eflags = tp->tf_eflags;
3799 if (flags & GET_MC_CLEAR_RET) {
3802 mcp->mc_eflags &= ~PSL_C;
3804 mcp->mc_eax = tp->tf_eax;
3805 mcp->mc_edx = tp->tf_edx;
3807 mcp->mc_ebx = tp->tf_ebx;
3808 mcp->mc_ecx = tp->tf_ecx;
3809 mcp->mc_eip = tp->tf_eip;
3810 mcp->mc_cs = tp->tf_cs;
3811 mcp->mc_esp = tp->tf_esp;
3812 mcp->mc_ss = tp->tf_ss;
3813 mcp->mc_len = sizeof(*mcp);
3814 get_fpcontext(td, mcp, NULL, 0);
3815 sdp = &td->td_pcb->pcb_fsd;
3816 mcp->mc_fsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
3817 sdp = &td->td_pcb->pcb_gsd;
3818 mcp->mc_gsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
3820 mcp->mc_xfpustate = 0;
3821 mcp->mc_xfpustate_len = 0;
3822 bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2));
3827 * Set machine context.
3829 * However, we don't set any but the user modifiable flags, and we won't
3830 * touch the cs selector.
3833 set_mcontext(struct thread *td, mcontext_t *mcp)
3835 struct trapframe *tp;
3840 if (mcp->mc_len != sizeof(*mcp) ||
3841 (mcp->mc_flags & ~_MC_FLAG_MASK) != 0)
3843 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
3844 (tp->tf_eflags & ~PSL_USERCHANGE);
3845 if (mcp->mc_flags & _MC_HASFPXSTATE) {
3846 if (mcp->mc_xfpustate_len > cpu_max_ext_state_size -
3847 sizeof(union savefpu))
3849 xfpustate = __builtin_alloca(mcp->mc_xfpustate_len);
3850 ret = copyin((void *)mcp->mc_xfpustate, xfpustate,
3851 mcp->mc_xfpustate_len);
3856 ret = set_fpcontext(td, mcp, xfpustate, mcp->mc_xfpustate_len);
3859 tp->tf_fs = mcp->mc_fs;
3860 tp->tf_es = mcp->mc_es;
3861 tp->tf_ds = mcp->mc_ds;
3862 tp->tf_edi = mcp->mc_edi;
3863 tp->tf_esi = mcp->mc_esi;
3864 tp->tf_ebp = mcp->mc_ebp;
3865 tp->tf_ebx = mcp->mc_ebx;
3866 tp->tf_edx = mcp->mc_edx;
3867 tp->tf_ecx = mcp->mc_ecx;
3868 tp->tf_eax = mcp->mc_eax;
3869 tp->tf_eip = mcp->mc_eip;
3870 tp->tf_eflags = eflags;
3871 tp->tf_esp = mcp->mc_esp;
3872 tp->tf_ss = mcp->mc_ss;
3873 td->td_pcb->pcb_gs = mcp->mc_gs;
3878 get_fpcontext(struct thread *td, mcontext_t *mcp, char *xfpusave,
3879 size_t xfpusave_len)
3881 #ifdef CPU_ENABLE_SSE
3882 size_t max_len, len;
3886 mcp->mc_fpformat = _MC_FPFMT_NODEV;
3887 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
3888 bzero(mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
3890 mcp->mc_ownedfp = npxgetregs(td);
3891 bcopy(get_pcb_user_save_td(td), &mcp->mc_fpstate[0],
3892 sizeof(mcp->mc_fpstate));
3893 mcp->mc_fpformat = npxformat();
3894 #ifdef CPU_ENABLE_SSE
3895 if (!use_xsave || xfpusave_len == 0)
3897 max_len = cpu_max_ext_state_size - sizeof(union savefpu);
3899 if (len > max_len) {
3901 bzero(xfpusave + max_len, len - max_len);
3903 mcp->mc_flags |= _MC_HASFPXSTATE;
3904 mcp->mc_xfpustate_len = len;
3905 bcopy(get_pcb_user_save_td(td) + 1, xfpusave, len);
3911 set_fpcontext(struct thread *td, mcontext_t *mcp, char *xfpustate,
3912 size_t xfpustate_len)
3914 union savefpu *fpstate;
3917 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
3919 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
3920 mcp->mc_fpformat != _MC_FPFMT_XMM)
3922 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE) {
3923 /* We don't care what state is left in the FPU or PCB. */
3926 } else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
3927 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
3929 fpstate = (union savefpu *)&mcp->mc_fpstate;
3930 #ifdef CPU_ENABLE_SSE
3932 fpstate->sv_xmm.sv_env.en_mxcsr &= cpu_mxcsr_mask;
3934 error = npxsetregs(td, fpstate, xfpustate, xfpustate_len);
3944 fpstate_drop(struct thread *td)
3947 KASSERT(PCB_USER_FPU(td->td_pcb), ("fpstate_drop: kernel-owned fpu"));
3950 if (PCPU_GET(fpcurthread) == td)
3954 * XXX force a full drop of the npx. The above only drops it if we
3955 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
3957 * XXX I don't much like npxgetregs()'s semantics of doing a full
3958 * drop. Dropping only to the pcb matches fnsave's behaviour.
3959 * We only need to drop to !PCB_INITDONE in sendsig(). But
3960 * sendsig() is the only caller of npxgetregs()... perhaps we just
3961 * have too many layers.
3963 curthread->td_pcb->pcb_flags &= ~(PCB_NPXINITDONE |
3964 PCB_NPXUSERINITDONE);
3969 fill_dbregs(struct thread *td, struct dbreg *dbregs)
3974 dbregs->dr[0] = rdr0();
3975 dbregs->dr[1] = rdr1();
3976 dbregs->dr[2] = rdr2();
3977 dbregs->dr[3] = rdr3();
3978 dbregs->dr[4] = rdr4();
3979 dbregs->dr[5] = rdr5();
3980 dbregs->dr[6] = rdr6();
3981 dbregs->dr[7] = rdr7();
3984 dbregs->dr[0] = pcb->pcb_dr0;
3985 dbregs->dr[1] = pcb->pcb_dr1;
3986 dbregs->dr[2] = pcb->pcb_dr2;
3987 dbregs->dr[3] = pcb->pcb_dr3;
3990 dbregs->dr[6] = pcb->pcb_dr6;
3991 dbregs->dr[7] = pcb->pcb_dr7;
3997 set_dbregs(struct thread *td, struct dbreg *dbregs)
4003 load_dr0(dbregs->dr[0]);
4004 load_dr1(dbregs->dr[1]);
4005 load_dr2(dbregs->dr[2]);
4006 load_dr3(dbregs->dr[3]);
4007 load_dr4(dbregs->dr[4]);
4008 load_dr5(dbregs->dr[5]);
4009 load_dr6(dbregs->dr[6]);
4010 load_dr7(dbregs->dr[7]);
4013 * Don't let an illegal value for dr7 get set. Specifically,
4014 * check for undefined settings. Setting these bit patterns
4015 * result in undefined behaviour and can lead to an unexpected
4018 for (i = 0; i < 4; i++) {
4019 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
4021 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
4028 * Don't let a process set a breakpoint that is not within the
4029 * process's address space. If a process could do this, it
4030 * could halt the system by setting a breakpoint in the kernel
4031 * (if ddb was enabled). Thus, we need to check to make sure
4032 * that no breakpoints are being enabled for addresses outside
4033 * process's address space.
4035 * XXX - what about when the watched area of the user's
4036 * address space is written into from within the kernel
4037 * ... wouldn't that still cause a breakpoint to be generated
4038 * from within kernel mode?
4041 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
4042 /* dr0 is enabled */
4043 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
4047 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
4048 /* dr1 is enabled */
4049 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
4053 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
4054 /* dr2 is enabled */
4055 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
4059 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
4060 /* dr3 is enabled */
4061 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
4065 pcb->pcb_dr0 = dbregs->dr[0];
4066 pcb->pcb_dr1 = dbregs->dr[1];
4067 pcb->pcb_dr2 = dbregs->dr[2];
4068 pcb->pcb_dr3 = dbregs->dr[3];
4069 pcb->pcb_dr6 = dbregs->dr[6];
4070 pcb->pcb_dr7 = dbregs->dr[7];
4072 pcb->pcb_flags |= PCB_DBREGS;
4079 * Return > 0 if a hardware breakpoint has been hit, and the
4080 * breakpoint was in user space. Return 0, otherwise.
4083 user_dbreg_trap(void)
4085 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
4086 u_int32_t bp; /* breakpoint bits extracted from dr6 */
4087 int nbp; /* number of breakpoints that triggered */
4088 caddr_t addr[4]; /* breakpoint addresses */
4092 if ((dr7 & 0x000000ff) == 0) {
4094 * all GE and LE bits in the dr7 register are zero,
4095 * thus the trap couldn't have been caused by the
4096 * hardware debug registers
4103 bp = dr6 & 0x0000000f;
4107 * None of the breakpoint bits are set meaning this
4108 * trap was not caused by any of the debug registers
4114 * at least one of the breakpoints were hit, check to see
4115 * which ones and if any of them are user space addresses
4119 addr[nbp++] = (caddr_t)rdr0();
4122 addr[nbp++] = (caddr_t)rdr1();
4125 addr[nbp++] = (caddr_t)rdr2();
4128 addr[nbp++] = (caddr_t)rdr3();
4131 for (i = 0; i < nbp; i++) {
4132 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
4134 * addr[i] is in user space
4141 * None of the breakpoints are in user space.
4149 * Provide inb() and outb() as functions. They are normally only available as
4150 * inline functions, thus cannot be called from the debugger.
4153 /* silence compiler warnings */
4154 u_char inb_(u_short);
4155 void outb_(u_short, u_char);
4164 outb_(u_short port, u_char data)