2 * Copyright (c) 1992 Terrence R. Lambert.
3 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 #include "opt_atalk.h"
45 #include "opt_compat.h"
51 #include "opt_kstack_pages.h"
52 #include "opt_maxmem.h"
53 #include "opt_msgbuf.h"
55 #include "opt_perfmon.h"
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/sysproto.h>
60 #include <sys/signalvar.h>
61 #include <sys/imgact.h>
63 #include <sys/kernel.h>
65 #include <sys/linker.h>
67 #include <sys/malloc.h>
68 #include <sys/memrange.h>
69 #include <sys/mutex.h>
74 #include <sys/reboot.h>
75 #include <sys/callout.h>
76 #include <sys/msgbuf.h>
77 #include <sys/sched.h>
78 #include <sys/sysent.h>
79 #include <sys/sysctl.h>
80 #include <sys/ucontext.h>
81 #include <sys/vmmeter.h>
83 #include <sys/eventhandler.h>
86 #include <vm/vm_param.h>
87 #include <vm/vm_kern.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_pager.h>
92 #include <vm/vm_extern.h>
100 #error KDB must be enabled in order for DDB to work!
103 #include <ddb/db_sym.h>
106 #include <net/netisr.h>
108 #include <machine/cpu.h>
109 #include <machine/cputypes.h>
110 #include <machine/reg.h>
111 #include <machine/clock.h>
112 #include <machine/specialreg.h>
113 #include <machine/bootinfo.h>
114 #include <machine/intr_machdep.h>
115 #include <machine/md_var.h>
116 #include <machine/pc/bios.h>
117 #include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
118 #include <machine/proc.h>
120 #include <machine/perfmon.h>
123 #include <machine/privatespace.h>
124 #include <machine/smp.h>
128 #include <i386/isa/icu.h>
132 #include <machine/vm86.h>
133 #include <sys/ptrace.h>
134 #include <machine/sigframe.h>
136 /* Sanity check for __curthread() */
137 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
139 extern void init386(int first);
140 extern void dblfault_handler(void);
142 extern void printcpuinfo(void); /* XXX header file */
143 extern void finishidentcpu(void);
144 extern void panicifcpuunsupported(void);
145 extern void initializecpu(void);
147 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
148 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
150 #if !defined(CPU_ENABLE_SSE) && defined(I686_CPU)
151 #define CPU_ENABLE_SSE
153 #if defined(CPU_DISABLE_SSE)
154 #undef CPU_ENABLE_SSE
157 static void cpu_startup(void *);
158 static void fpstate_drop(struct thread *td);
159 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
160 static int set_fpcontext(struct thread *td, const mcontext_t *mcp);
161 #ifdef CPU_ENABLE_SSE
162 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
163 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
164 #endif /* CPU_ENABLE_SSE */
165 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
168 extern vm_offset_t ksym_start, ksym_end;
171 int _udatasel, _ucodesel;
177 static void osendsig(sig_t catcher, int sig, sigset_t *mask, u_long code);
179 #ifdef COMPAT_FREEBSD4
180 static void freebsd4_sendsig(sig_t catcher, int sig, sigset_t *mask,
186 vm_paddr_t phys_avail[10];
188 /* must be 2 less so 0 0 can signal end of chunks */
189 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(vm_offset_t)) - 2)
191 struct kva_md_info kmi;
193 static struct trapframe proc0_tf;
195 static struct pcpu __pcpu;
200 struct mem_range_softc mem_range_softc;
207 * Good {morning,afternoon,evening,night}.
211 panicifcpuunsupported();
215 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)Maxmem),
216 ptoa((uintmax_t)Maxmem) / 1048576);
218 * Display any holes after the first chunk of extended memory.
223 printf("Physical memory chunk(s):\n");
224 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
227 size = phys_avail[indx + 1] - phys_avail[indx];
229 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
230 (uintmax_t)phys_avail[indx],
231 (uintmax_t)phys_avail[indx + 1] - 1,
232 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
236 vm_ksubmap_init(&kmi);
238 printf("avail memory = %ju (%ju MB)\n",
239 ptoa((uintmax_t)cnt.v_free_count),
240 ptoa((uintmax_t)cnt.v_free_count) / 1048576);
243 * Set up buffers, so they can be used to read disk labels.
246 vm_pager_bufferinit();
252 * Send an interrupt to process.
254 * Stack is set up to allow sigcode stored
255 * at top to call routine, followed by kcall
256 * to sigreturn routine below. After sigreturn
257 * resets the signal mask, the stack, and the
258 * frame pointer, it returns to the user
263 osendsig(catcher, sig, mask, code)
269 struct osigframe sf, *fp;
273 struct trapframe *regs;
278 PROC_LOCK_ASSERT(p, MA_OWNED);
280 mtx_assert(&psp->ps_mtx, MA_OWNED);
282 oonstack = sigonstack(regs->tf_esp);
284 /* Allocate space for the signal handler context. */
285 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
286 SIGISMEMBER(psp->ps_sigonstack, sig)) {
287 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
288 td->td_sigstk.ss_size - sizeof(struct osigframe));
289 #if defined(COMPAT_43)
290 td->td_sigstk.ss_flags |= SS_ONSTACK;
293 fp = (struct osigframe *)regs->tf_esp - 1;
295 /* Translate the signal if appropriate. */
296 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
297 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
299 /* Build the argument list for the signal handler. */
301 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
302 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
303 /* Signal handler installed with SA_SIGINFO. */
304 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
305 sf.sf_siginfo.si_signo = sig;
306 sf.sf_siginfo.si_code = code;
307 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
309 /* Old FreeBSD-style arguments. */
311 sf.sf_addr = regs->tf_err;
312 sf.sf_ahu.sf_handler = catcher;
314 mtx_unlock(&psp->ps_mtx);
317 /* Save most if not all of trap frame. */
318 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
319 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
320 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
321 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
322 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
323 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
324 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
325 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
326 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
327 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
328 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
329 sf.sf_siginfo.si_sc.sc_gs = rgs();
330 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
332 /* Build the signal context to be used by osigreturn(). */
333 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
334 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
335 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
336 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
337 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
338 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
339 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
340 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
343 * If we're a vm86 process, we want to save the segment registers.
344 * We also change eflags to be our emulated eflags, not the actual
347 if (regs->tf_eflags & PSL_VM) {
348 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
349 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
350 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
352 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
353 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
354 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
355 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
357 if (vm86->vm86_has_vme == 0)
358 sf.sf_siginfo.si_sc.sc_ps =
359 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
360 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
362 /* See sendsig() for comments. */
363 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
367 * Copy the sigframe out to the user's stack.
369 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
371 printf("process %ld has trashed its stack\n", (long)p->p_pid);
377 regs->tf_esp = (int)fp;
378 regs->tf_eip = PS_STRINGS - szosigcode;
379 regs->tf_eflags &= ~PSL_T;
380 regs->tf_cs = _ucodesel;
381 regs->tf_ds = _udatasel;
382 regs->tf_es = _udatasel;
383 regs->tf_fs = _udatasel;
385 regs->tf_ss = _udatasel;
387 mtx_lock(&psp->ps_mtx);
389 #endif /* COMPAT_43 */
391 #ifdef COMPAT_FREEBSD4
393 freebsd4_sendsig(catcher, sig, mask, code)
399 struct sigframe4 sf, *sfp;
403 struct trapframe *regs;
408 PROC_LOCK_ASSERT(p, MA_OWNED);
410 mtx_assert(&psp->ps_mtx, MA_OWNED);
412 oonstack = sigonstack(regs->tf_esp);
414 /* Save user context. */
415 bzero(&sf, sizeof(sf));
416 sf.sf_uc.uc_sigmask = *mask;
417 sf.sf_uc.uc_stack = td->td_sigstk;
418 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
419 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
420 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
421 sf.sf_uc.uc_mcontext.mc_gs = rgs();
422 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
424 /* Allocate space for the signal handler context. */
425 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
426 SIGISMEMBER(psp->ps_sigonstack, sig)) {
427 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
428 td->td_sigstk.ss_size - sizeof(struct sigframe4));
429 #if defined(COMPAT_43)
430 td->td_sigstk.ss_flags |= SS_ONSTACK;
433 sfp = (struct sigframe4 *)regs->tf_esp - 1;
435 /* Translate the signal if appropriate. */
436 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
437 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
439 /* Build the argument list for the signal handler. */
441 sf.sf_ucontext = (register_t)&sfp->sf_uc;
442 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
443 /* Signal handler installed with SA_SIGINFO. */
444 sf.sf_siginfo = (register_t)&sfp->sf_si;
445 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
447 /* Fill in POSIX parts */
448 sf.sf_si.si_signo = sig;
449 sf.sf_si.si_code = code;
450 sf.sf_si.si_addr = (void *)regs->tf_err;
452 /* Old FreeBSD-style arguments. */
453 sf.sf_siginfo = code;
454 sf.sf_addr = regs->tf_err;
455 sf.sf_ahu.sf_handler = catcher;
457 mtx_unlock(&psp->ps_mtx);
461 * If we're a vm86 process, we want to save the segment registers.
462 * We also change eflags to be our emulated eflags, not the actual
465 if (regs->tf_eflags & PSL_VM) {
466 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
467 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
469 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
470 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
471 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
472 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
474 if (vm86->vm86_has_vme == 0)
475 sf.sf_uc.uc_mcontext.mc_eflags =
476 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
477 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
480 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
481 * syscalls made by the signal handler. This just avoids
482 * wasting time for our lazy fixup of such faults. PSL_NT
483 * does nothing in vm86 mode, but vm86 programs can set it
484 * almost legitimately in probes for old cpu types.
486 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
490 * Copy the sigframe out to the user's stack.
492 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
494 printf("process %ld has trashed its stack\n", (long)p->p_pid);
500 regs->tf_esp = (int)sfp;
501 regs->tf_eip = PS_STRINGS - szfreebsd4_sigcode;
502 regs->tf_eflags &= ~PSL_T;
503 regs->tf_cs = _ucodesel;
504 regs->tf_ds = _udatasel;
505 regs->tf_es = _udatasel;
506 regs->tf_fs = _udatasel;
507 regs->tf_ss = _udatasel;
509 mtx_lock(&psp->ps_mtx);
511 #endif /* COMPAT_FREEBSD4 */
514 sendsig(catcher, sig, mask, code)
520 struct sigframe sf, *sfp;
525 struct trapframe *regs;
530 PROC_LOCK_ASSERT(p, MA_OWNED);
532 mtx_assert(&psp->ps_mtx, MA_OWNED);
533 #ifdef COMPAT_FREEBSD4
534 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
535 freebsd4_sendsig(catcher, sig, mask, code);
540 if (SIGISMEMBER(psp->ps_osigset, sig)) {
541 osendsig(catcher, sig, mask, code);
546 oonstack = sigonstack(regs->tf_esp);
548 /* Save user context. */
549 bzero(&sf, sizeof(sf));
550 sf.sf_uc.uc_sigmask = *mask;
551 sf.sf_uc.uc_stack = td->td_sigstk;
552 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
553 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
554 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
555 sf.sf_uc.uc_mcontext.mc_gs = rgs();
556 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
557 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
558 get_fpcontext(td, &sf.sf_uc.uc_mcontext);
561 /* Allocate space for the signal handler context. */
562 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
563 SIGISMEMBER(psp->ps_sigonstack, sig)) {
564 sp = td->td_sigstk.ss_sp +
565 td->td_sigstk.ss_size - sizeof(struct sigframe);
566 #if defined(COMPAT_43)
567 td->td_sigstk.ss_flags |= SS_ONSTACK;
570 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
571 /* Align to 16 bytes. */
572 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
574 /* Translate the signal if appropriate. */
575 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
576 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
578 /* Build the argument list for the signal handler. */
580 sf.sf_ucontext = (register_t)&sfp->sf_uc;
581 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
582 /* Signal handler installed with SA_SIGINFO. */
583 sf.sf_siginfo = (register_t)&sfp->sf_si;
584 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
586 /* Fill in POSIX parts */
587 sf.sf_si.si_signo = sig;
588 sf.sf_si.si_code = code;
589 sf.sf_si.si_addr = (void *)regs->tf_err;
591 /* Old FreeBSD-style arguments. */
592 sf.sf_siginfo = code;
593 sf.sf_addr = regs->tf_err;
594 sf.sf_ahu.sf_handler = catcher;
596 mtx_unlock(&psp->ps_mtx);
600 * If we're a vm86 process, we want to save the segment registers.
601 * We also change eflags to be our emulated eflags, not the actual
604 if (regs->tf_eflags & PSL_VM) {
605 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
606 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
608 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
609 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
610 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
611 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
613 if (vm86->vm86_has_vme == 0)
614 sf.sf_uc.uc_mcontext.mc_eflags =
615 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
616 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
619 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
620 * syscalls made by the signal handler. This just avoids
621 * wasting time for our lazy fixup of such faults. PSL_NT
622 * does nothing in vm86 mode, but vm86 programs can set it
623 * almost legitimately in probes for old cpu types.
625 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
629 * Copy the sigframe out to the user's stack.
631 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
633 printf("process %ld has trashed its stack\n", (long)p->p_pid);
639 regs->tf_esp = (int)sfp;
640 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
641 regs->tf_eflags &= ~PSL_T;
642 regs->tf_cs = _ucodesel;
643 regs->tf_ds = _udatasel;
644 regs->tf_es = _udatasel;
645 regs->tf_fs = _udatasel;
646 regs->tf_ss = _udatasel;
648 mtx_lock(&psp->ps_mtx);
652 * Build siginfo_t for SA thread
655 cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
662 PROC_LOCK_ASSERT(p, MA_OWNED);
664 bzero(si, sizeof(*si));
667 si->si_addr = (void *)td->td_frame->tf_err;
668 /* XXXKSE fill other fields */
672 * System call to cleanup state after a signal
673 * has been taken. Reset signal mask and
674 * stack state from context left by sendsig (above).
675 * Return to previous pc and psl as specified by
676 * context left by sendsig. Check carefully to
677 * make sure that the user has not modified the
678 * state to gain improper privileges.
686 struct osigreturn_args /* {
687 struct osigcontext *sigcntxp;
690 struct osigcontext sc;
691 struct trapframe *regs;
692 struct osigcontext *scp;
693 struct proc *p = td->td_proc;
697 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
702 if (eflags & PSL_VM) {
703 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
704 struct vm86_kernel *vm86;
707 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
708 * set up the vm86 area, and we can't enter vm86 mode.
710 if (td->td_pcb->pcb_ext == 0)
712 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
713 if (vm86->vm86_inited == 0)
716 /* Go back to user mode if both flags are set. */
717 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
718 trapsignal(td, SIGBUS, 0);
720 if (vm86->vm86_has_vme) {
721 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
722 (eflags & VME_USERCHANGE) | PSL_VM;
724 vm86->vm86_eflags = eflags; /* save VIF, VIP */
725 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
726 (eflags & VM_USERCHANGE) | PSL_VM;
728 tf->tf_vm86_ds = scp->sc_ds;
729 tf->tf_vm86_es = scp->sc_es;
730 tf->tf_vm86_fs = scp->sc_fs;
731 tf->tf_vm86_gs = scp->sc_gs;
732 tf->tf_ds = _udatasel;
733 tf->tf_es = _udatasel;
734 tf->tf_fs = _udatasel;
737 * Don't allow users to change privileged or reserved flags.
740 * XXX do allow users to change the privileged flag PSL_RF.
741 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
742 * should sometimes set it there too. tf_eflags is kept in
743 * the signal context during signal handling and there is no
744 * other place to remember it, so the PSL_RF bit may be
745 * corrupted by the signal handler without us knowing.
746 * Corruption of the PSL_RF bit at worst causes one more or
747 * one less debugger trap, so allowing it is fairly harmless.
749 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
754 * Don't allow users to load a valid privileged %cs. Let the
755 * hardware check for invalid selectors, excess privilege in
756 * other selectors, invalid %eip's and invalid %esp's.
758 if (!CS_SECURE(scp->sc_cs)) {
759 trapsignal(td, SIGBUS, T_PROTFLT);
762 regs->tf_ds = scp->sc_ds;
763 regs->tf_es = scp->sc_es;
764 regs->tf_fs = scp->sc_fs;
767 /* Restore remaining registers. */
768 regs->tf_eax = scp->sc_eax;
769 regs->tf_ebx = scp->sc_ebx;
770 regs->tf_ecx = scp->sc_ecx;
771 regs->tf_edx = scp->sc_edx;
772 regs->tf_esi = scp->sc_esi;
773 regs->tf_edi = scp->sc_edi;
774 regs->tf_cs = scp->sc_cs;
775 regs->tf_ss = scp->sc_ss;
776 regs->tf_isp = scp->sc_isp;
777 regs->tf_ebp = scp->sc_fp;
778 regs->tf_esp = scp->sc_sp;
779 regs->tf_eip = scp->sc_pc;
780 regs->tf_eflags = eflags;
783 #if defined(COMPAT_43)
784 if (scp->sc_onstack & 1)
785 td->td_sigstk.ss_flags |= SS_ONSTACK;
787 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
789 SIGSETOLD(td->td_sigmask, scp->sc_mask);
790 SIG_CANTMASK(td->td_sigmask);
793 return (EJUSTRETURN);
795 #endif /* COMPAT_43 */
797 #ifdef COMPAT_FREEBSD4
802 freebsd4_sigreturn(td, uap)
804 struct freebsd4_sigreturn_args /* {
805 const ucontext4 *sigcntxp;
809 struct proc *p = td->td_proc;
810 struct trapframe *regs;
811 const struct ucontext4 *ucp;
812 int cs, eflags, error;
814 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
819 eflags = ucp->uc_mcontext.mc_eflags;
820 if (eflags & PSL_VM) {
821 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
822 struct vm86_kernel *vm86;
825 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
826 * set up the vm86 area, and we can't enter vm86 mode.
828 if (td->td_pcb->pcb_ext == 0)
830 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
831 if (vm86->vm86_inited == 0)
834 /* Go back to user mode if both flags are set. */
835 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
836 trapsignal(td, SIGBUS, 0);
838 if (vm86->vm86_has_vme) {
839 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
840 (eflags & VME_USERCHANGE) | PSL_VM;
842 vm86->vm86_eflags = eflags; /* save VIF, VIP */
843 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
844 (eflags & VM_USERCHANGE) | PSL_VM;
846 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
847 tf->tf_eflags = eflags;
848 tf->tf_vm86_ds = tf->tf_ds;
849 tf->tf_vm86_es = tf->tf_es;
850 tf->tf_vm86_fs = tf->tf_fs;
851 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
852 tf->tf_ds = _udatasel;
853 tf->tf_es = _udatasel;
854 tf->tf_fs = _udatasel;
857 * Don't allow users to change privileged or reserved flags.
860 * XXX do allow users to change the privileged flag PSL_RF.
861 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
862 * should sometimes set it there too. tf_eflags is kept in
863 * the signal context during signal handling and there is no
864 * other place to remember it, so the PSL_RF bit may be
865 * corrupted by the signal handler without us knowing.
866 * Corruption of the PSL_RF bit at worst causes one more or
867 * one less debugger trap, so allowing it is fairly harmless.
869 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
870 printf("freebsd4_sigreturn: eflags = 0x%x\n", eflags);
875 * Don't allow users to load a valid privileged %cs. Let the
876 * hardware check for invalid selectors, excess privilege in
877 * other selectors, invalid %eip's and invalid %esp's.
879 cs = ucp->uc_mcontext.mc_cs;
880 if (!CS_SECURE(cs)) {
881 printf("freebsd4_sigreturn: cs = 0x%x\n", cs);
882 trapsignal(td, SIGBUS, T_PROTFLT);
886 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
890 #if defined(COMPAT_43)
891 if (ucp->uc_mcontext.mc_onstack & 1)
892 td->td_sigstk.ss_flags |= SS_ONSTACK;
894 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
897 td->td_sigmask = ucp->uc_sigmask;
898 SIG_CANTMASK(td->td_sigmask);
901 return (EJUSTRETURN);
903 #endif /* COMPAT_FREEBSD4 */
911 struct sigreturn_args /* {
912 const __ucontext *sigcntxp;
916 struct proc *p = td->td_proc;
917 struct trapframe *regs;
918 const ucontext_t *ucp;
919 int cs, eflags, error, ret;
921 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
926 eflags = ucp->uc_mcontext.mc_eflags;
927 if (eflags & PSL_VM) {
928 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
929 struct vm86_kernel *vm86;
932 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
933 * set up the vm86 area, and we can't enter vm86 mode.
935 if (td->td_pcb->pcb_ext == 0)
937 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
938 if (vm86->vm86_inited == 0)
941 /* Go back to user mode if both flags are set. */
942 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
943 trapsignal(td, SIGBUS, 0);
945 if (vm86->vm86_has_vme) {
946 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
947 (eflags & VME_USERCHANGE) | PSL_VM;
949 vm86->vm86_eflags = eflags; /* save VIF, VIP */
950 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
951 (eflags & VM_USERCHANGE) | PSL_VM;
953 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
954 tf->tf_eflags = eflags;
955 tf->tf_vm86_ds = tf->tf_ds;
956 tf->tf_vm86_es = tf->tf_es;
957 tf->tf_vm86_fs = tf->tf_fs;
958 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
959 tf->tf_ds = _udatasel;
960 tf->tf_es = _udatasel;
961 tf->tf_fs = _udatasel;
964 * Don't allow users to change privileged or reserved flags.
967 * XXX do allow users to change the privileged flag PSL_RF.
968 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
969 * should sometimes set it there too. tf_eflags is kept in
970 * the signal context during signal handling and there is no
971 * other place to remember it, so the PSL_RF bit may be
972 * corrupted by the signal handler without us knowing.
973 * Corruption of the PSL_RF bit at worst causes one more or
974 * one less debugger trap, so allowing it is fairly harmless.
976 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
977 printf("sigreturn: eflags = 0x%x\n", eflags);
982 * Don't allow users to load a valid privileged %cs. Let the
983 * hardware check for invalid selectors, excess privilege in
984 * other selectors, invalid %eip's and invalid %esp's.
986 cs = ucp->uc_mcontext.mc_cs;
987 if (!CS_SECURE(cs)) {
988 printf("sigreturn: cs = 0x%x\n", cs);
989 trapsignal(td, SIGBUS, T_PROTFLT);
993 ret = set_fpcontext(td, &ucp->uc_mcontext);
996 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1000 #if defined(COMPAT_43)
1001 if (ucp->uc_mcontext.mc_onstack & 1)
1002 td->td_sigstk.ss_flags |= SS_ONSTACK;
1004 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1007 td->td_sigmask = ucp->uc_sigmask;
1008 SIG_CANTMASK(td->td_sigmask);
1011 return (EJUSTRETURN);
1015 * Machine dependent boot() routine
1017 * I haven't seen anything to put here yet
1018 * Possibly some stuff might be grafted back here from boot()
1026 * Shutdown the CPU as much as possible
1036 * Hook to idle the CPU when possible. In the SMP case we default to
1037 * off because a halted cpu will not currently pick up a new thread in the
1038 * run queue until the next timer tick. If turned on this will result in
1039 * approximately a 4.2% loss in real time performance in buildworld tests
1040 * (but improves user and sys times oddly enough), and saves approximately
1041 * 5% in power consumption on an idle machine (tests w/2xCPU 1.1GHz P3).
1043 * XXX we need to have a cpu mask of idle cpus and generate an IPI or
1044 * otherwise generate some sort of interrupt to wake up cpus sitting in HLT.
1045 * Then we can have our cake and eat it too.
1047 * XXX I'm turning it on for SMP as well by default for now. It seems to
1048 * help lock contention somewhat, and this is critical for HTT. -Peter
1050 static int cpu_idle_hlt = 1;
1051 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
1052 &cpu_idle_hlt, 0, "Idle loop HLT enable");
1055 cpu_idle_default(void)
1058 * we must absolutely guarentee that hlt is the
1059 * absolute next instruction after sti or we
1060 * introduce a timing window.
1062 __asm __volatile("sti; hlt");
1066 * Note that we have to be careful here to avoid a race between checking
1067 * sched_runnable() and actually halting. If we don't do this, we may waste
1068 * the time between calling hlt and the next interrupt even though there
1069 * is a runnable process.
1076 if (mp_grab_cpu_hlt())
1082 if (sched_runnable())
1089 /* Other subsystems (e.g., ACPI) can hook this later. */
1090 void (*cpu_idle_hook)(void) = cpu_idle_default;
1093 * Clear registers on exec
1096 exec_setregs(td, entry, stack, ps_strings)
1102 struct trapframe *regs = td->td_frame;
1103 struct pcb *pcb = td->td_pcb;
1105 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1106 pcb->pcb_gs = _udatasel;
1109 if (td->td_proc->p_md.md_ldt)
1112 bzero((char *)regs, sizeof(struct trapframe));
1113 regs->tf_eip = entry;
1114 regs->tf_esp = stack;
1115 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1116 regs->tf_ss = _udatasel;
1117 regs->tf_ds = _udatasel;
1118 regs->tf_es = _udatasel;
1119 regs->tf_fs = _udatasel;
1120 regs->tf_cs = _ucodesel;
1122 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1123 regs->tf_ebx = ps_strings;
1126 * Reset the hardware debug registers if they were in use.
1127 * They won't have any meaning for the newly exec'd process.
1129 if (pcb->pcb_flags & PCB_DBREGS) {
1136 if (pcb == PCPU_GET(curpcb)) {
1138 * Clear the debug registers on the running
1139 * CPU, otherwise they will end up affecting
1140 * the next process we switch to.
1144 pcb->pcb_flags &= ~PCB_DBREGS;
1148 * Initialize the math emulator (if any) for the current process.
1149 * Actually, just clear the bit that says that the emulator has
1150 * been initialized. Initialization is delayed until the process
1151 * traps to the emulator (if it is done at all) mainly because
1152 * emulators don't provide an entry point for initialization.
1154 td->td_pcb->pcb_flags &= ~FP_SOFTFP;
1157 * Drop the FP state if we hold it, so that the process gets a
1158 * clean FP state if it uses the FPU again.
1163 * XXX - Linux emulator
1164 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1167 td->td_retval[1] = 0;
1177 * CR0_MP, CR0_NE and CR0_TS are also set by npx_probe() for the
1178 * BSP. See the comments there about why we set them.
1180 cr0 |= CR0_MP | CR0_NE | CR0_TS;
1182 cr0 |= CR0_WP | CR0_AM;
1189 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
1192 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
1194 if (!error && req->newptr)
1199 SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
1200 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
1202 SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
1203 CTLFLAG_RW, &disable_rtc_set, 0, "");
1205 SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
1206 CTLFLAG_RD, &bootinfo, bootinfo, "");
1208 SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
1209 CTLFLAG_RW, &wall_cmos_clock, 0, "");
1211 u_long bootdev; /* not a struct cdev *- encoding is different */
1212 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1213 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1216 * Initialize 386 and configure to run kernel
1220 * Initialize segments & interrupt table
1224 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1225 static struct gate_descriptor idt0[NIDT];
1226 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1227 union descriptor ldt[NLDT]; /* local descriptor table */
1228 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1230 int private_tss; /* flag indicating private tss */
1232 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1233 extern int has_f00f_bug;
1236 static struct i386tss dblfault_tss;
1237 static char dblfault_stack[PAGE_SIZE];
1239 extern struct user *proc0uarea;
1240 extern vm_offset_t proc0kstack;
1243 /* software prototypes -- in more palatable form */
1244 struct soft_segment_descriptor gdt_segs[] = {
1245 /* GNULL_SEL 0 Null Descriptor */
1246 { 0x0, /* segment base address */
1248 0, /* segment type */
1249 0, /* segment descriptor priority level */
1250 0, /* segment descriptor present */
1252 0, /* default 32 vs 16 bit size */
1253 0 /* limit granularity (byte/page units)*/ },
1254 /* GCODE_SEL 1 Code Descriptor for kernel */
1255 { 0x0, /* segment base address */
1256 0xfffff, /* length - all address space */
1257 SDT_MEMERA, /* segment type */
1258 0, /* segment descriptor priority level */
1259 1, /* segment descriptor present */
1261 1, /* default 32 vs 16 bit size */
1262 1 /* limit granularity (byte/page units)*/ },
1263 /* GDATA_SEL 2 Data Descriptor for kernel */
1264 { 0x0, /* segment base address */
1265 0xfffff, /* length - all address space */
1266 SDT_MEMRWA, /* segment type */
1267 0, /* segment descriptor priority level */
1268 1, /* segment descriptor present */
1270 1, /* default 32 vs 16 bit size */
1271 1 /* limit granularity (byte/page units)*/ },
1272 /* GPRIV_SEL 3 SMP Per-Processor Private Data Descriptor */
1273 { 0x0, /* segment base address */
1274 0xfffff, /* length - all address space */
1275 SDT_MEMRWA, /* segment type */
1276 0, /* segment descriptor priority level */
1277 1, /* segment descriptor present */
1279 1, /* default 32 vs 16 bit size */
1280 1 /* limit granularity (byte/page units)*/ },
1281 /* GPROC0_SEL 4 Proc 0 Tss Descriptor */
1283 0x0, /* segment base address */
1284 sizeof(struct i386tss)-1,/* length */
1285 SDT_SYS386TSS, /* segment type */
1286 0, /* segment descriptor priority level */
1287 1, /* segment descriptor present */
1289 0, /* unused - default 32 vs 16 bit size */
1290 0 /* limit granularity (byte/page units)*/ },
1291 /* GLDT_SEL 5 LDT Descriptor */
1292 { (int) ldt, /* segment base address */
1293 sizeof(ldt)-1, /* length - all address space */
1294 SDT_SYSLDT, /* segment type */
1295 SEL_UPL, /* segment descriptor priority level */
1296 1, /* segment descriptor present */
1298 0, /* unused - default 32 vs 16 bit size */
1299 0 /* limit granularity (byte/page units)*/ },
1300 /* GUSERLDT_SEL 6 User LDT Descriptor per process */
1301 { (int) ldt, /* segment base address */
1302 (512 * sizeof(union descriptor)-1), /* length */
1303 SDT_SYSLDT, /* segment type */
1304 0, /* segment descriptor priority level */
1305 1, /* segment descriptor present */
1307 0, /* unused - default 32 vs 16 bit size */
1308 0 /* limit granularity (byte/page units)*/ },
1309 /* GTGATE_SEL 7 Null Descriptor - Placeholder */
1310 { 0x0, /* segment base address */
1311 0x0, /* length - all address space */
1312 0, /* segment type */
1313 0, /* segment descriptor priority level */
1314 0, /* segment descriptor present */
1316 0, /* default 32 vs 16 bit size */
1317 0 /* limit granularity (byte/page units)*/ },
1318 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1319 { 0x400, /* segment base address */
1320 0xfffff, /* length */
1321 SDT_MEMRWA, /* segment type */
1322 0, /* segment descriptor priority level */
1323 1, /* segment descriptor present */
1325 1, /* default 32 vs 16 bit size */
1326 1 /* limit granularity (byte/page units)*/ },
1327 /* GPANIC_SEL 9 Panic Tss Descriptor */
1328 { (int) &dblfault_tss, /* segment base address */
1329 sizeof(struct i386tss)-1,/* length - all address space */
1330 SDT_SYS386TSS, /* segment type */
1331 0, /* segment descriptor priority level */
1332 1, /* segment descriptor present */
1334 0, /* unused - default 32 vs 16 bit size */
1335 0 /* limit granularity (byte/page units)*/ },
1336 /* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */
1337 { 0, /* segment base address (overwritten) */
1338 0xfffff, /* length */
1339 SDT_MEMERA, /* segment type */
1340 0, /* segment descriptor priority level */
1341 1, /* segment descriptor present */
1343 0, /* default 32 vs 16 bit size */
1344 1 /* limit granularity (byte/page units)*/ },
1345 /* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit Code) */
1346 { 0, /* segment base address (overwritten) */
1347 0xfffff, /* length */
1348 SDT_MEMERA, /* segment type */
1349 0, /* segment descriptor priority level */
1350 1, /* segment descriptor present */
1352 0, /* default 32 vs 16 bit size */
1353 1 /* limit granularity (byte/page units)*/ },
1354 /* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */
1355 { 0, /* segment base address (overwritten) */
1356 0xfffff, /* length */
1357 SDT_MEMRWA, /* segment type */
1358 0, /* segment descriptor priority level */
1359 1, /* segment descriptor present */
1361 1, /* default 32 vs 16 bit size */
1362 1 /* limit granularity (byte/page units)*/ },
1363 /* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */
1364 { 0, /* segment base address (overwritten) */
1365 0xfffff, /* length */
1366 SDT_MEMRWA, /* segment type */
1367 0, /* segment descriptor priority level */
1368 1, /* segment descriptor present */
1370 0, /* default 32 vs 16 bit size */
1371 1 /* limit granularity (byte/page units)*/ },
1372 /* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */
1373 { 0, /* segment base address (overwritten) */
1374 0xfffff, /* length */
1375 SDT_MEMRWA, /* segment type */
1376 0, /* segment descriptor priority level */
1377 1, /* segment descriptor present */
1379 0, /* default 32 vs 16 bit size */
1380 1 /* limit granularity (byte/page units)*/ },
1383 static struct soft_segment_descriptor ldt_segs[] = {
1384 /* Null Descriptor - overwritten by call gate */
1385 { 0x0, /* segment base address */
1386 0x0, /* length - all address space */
1387 0, /* segment type */
1388 0, /* segment descriptor priority level */
1389 0, /* segment descriptor present */
1391 0, /* default 32 vs 16 bit size */
1392 0 /* limit granularity (byte/page units)*/ },
1393 /* Null Descriptor - overwritten by call gate */
1394 { 0x0, /* segment base address */
1395 0x0, /* length - all address space */
1396 0, /* segment type */
1397 0, /* segment descriptor priority level */
1398 0, /* segment descriptor present */
1400 0, /* default 32 vs 16 bit size */
1401 0 /* limit granularity (byte/page units)*/ },
1402 /* Null Descriptor - overwritten by call gate */
1403 { 0x0, /* segment base address */
1404 0x0, /* length - all address space */
1405 0, /* segment type */
1406 0, /* segment descriptor priority level */
1407 0, /* segment descriptor present */
1409 0, /* default 32 vs 16 bit size */
1410 0 /* limit granularity (byte/page units)*/ },
1411 /* Code Descriptor for user */
1412 { 0x0, /* segment base address */
1413 0xfffff, /* length - all address space */
1414 SDT_MEMERA, /* segment type */
1415 SEL_UPL, /* segment descriptor priority level */
1416 1, /* segment descriptor present */
1418 1, /* default 32 vs 16 bit size */
1419 1 /* limit granularity (byte/page units)*/ },
1420 /* Null Descriptor - overwritten by call gate */
1421 { 0x0, /* segment base address */
1422 0x0, /* length - all address space */
1423 0, /* segment type */
1424 0, /* segment descriptor priority level */
1425 0, /* segment descriptor present */
1427 0, /* default 32 vs 16 bit size */
1428 0 /* limit granularity (byte/page units)*/ },
1429 /* Data Descriptor for user */
1430 { 0x0, /* segment base address */
1431 0xfffff, /* length - all address space */
1432 SDT_MEMRWA, /* segment type */
1433 SEL_UPL, /* segment descriptor priority level */
1434 1, /* segment descriptor present */
1436 1, /* default 32 vs 16 bit size */
1437 1 /* limit granularity (byte/page units)*/ },
1441 setidt(idx, func, typ, dpl, selec)
1448 struct gate_descriptor *ip;
1451 ip->gd_looffset = (int)func;
1452 ip->gd_selector = selec;
1458 ip->gd_hioffset = ((int)func)>>16 ;
1461 #define IDTVEC(name) __CONCAT(X,name)
1464 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1465 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1466 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1467 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1468 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1472 * Display the index and function name of any IDT entries that don't use
1473 * the default 'rsvd' entry point.
1475 DB_SHOW_COMMAND(idt, db_show_idt)
1477 struct gate_descriptor *ip;
1482 db_setup_paging(db_simple_pager, &quit, DB_LINES_PER_PAGE);
1483 for (idx = 0, quit = 0; idx < NIDT; idx++) {
1484 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1485 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1486 db_printf("%3d\t", idx);
1487 db_printsym(func, DB_STGY_PROC);
1497 struct segment_descriptor *sd;
1498 struct soft_segment_descriptor *ssd;
1500 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1501 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1502 ssd->ssd_type = sd->sd_type;
1503 ssd->ssd_dpl = sd->sd_dpl;
1504 ssd->ssd_p = sd->sd_p;
1505 ssd->ssd_def32 = sd->sd_def32;
1506 ssd->ssd_gran = sd->sd_gran;
1509 #define PHYSMAP_SIZE (2 * 8)
1512 * Populate the (physmap) array with base/bound pairs describing the
1513 * available physical memory in the system, then test this memory and
1514 * build the phys_avail array describing the actually-available memory.
1516 * If we cannot accurately determine the physical memory map, then use
1517 * value from the 0xE801 call, and failing that, the RTC.
1519 * Total memory size may be set by the kernel environment variable
1520 * hw.physmem or the compile-time define MAXMEM.
1522 * XXX first should be vm_paddr_t.
1525 getmemsize(int first)
1527 int i, physmap_idx, pa_indx;
1530 struct vm86frame vmf;
1531 struct vm86context vmc;
1532 vm_paddr_t pa, physmap[PHYSMAP_SIZE];
1535 struct bios_smap *smap;
1536 quad_t dcons_addr, dcons_size;
1539 TUNABLE_INT_FETCH("hw.hasbrokenint12", &hasbrokenint12);
1540 bzero(&vmf, sizeof(vmf));
1541 bzero(physmap, sizeof(physmap));
1545 * Some newer BIOSes has broken INT 12H implementation which cause
1546 * kernel panic immediately. In this case, we need to scan SMAP
1547 * with INT 15:E820 first, then determine base memory size.
1549 if (hasbrokenint12) {
1554 * Perform "base memory" related probes & setup
1556 vm86_intcall(0x12, &vmf);
1557 basemem = vmf.vmf_ax;
1558 if (basemem > 640) {
1559 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1565 * XXX if biosbasemem is now < 640, there is a `hole'
1566 * between the end of base memory and the start of
1567 * ISA memory. The hole may be empty or it may
1568 * contain BIOS code or data. Map it read/write so
1569 * that the BIOS can write to it. (Memory from 0 to
1570 * the physical end of the kernel is mapped read-only
1571 * to begin with and then parts of it are remapped.
1572 * The parts that aren't remapped form holes that
1573 * remain read-only and are unused by the kernel.
1574 * The base memory area is below the physical end of
1575 * the kernel and right now forms a read-only hole.
1576 * The part of it from PAGE_SIZE to
1577 * (trunc_page(biosbasemem * 1024) - 1) will be
1578 * remapped and used by the kernel later.)
1580 * This code is similar to the code used in
1581 * pmap_mapdev, but since no memory needs to be
1582 * allocated we simply change the mapping.
1584 for (pa = trunc_page(basemem * 1024);
1585 pa < ISA_HOLE_START; pa += PAGE_SIZE)
1586 pmap_kenter(KERNBASE + pa, pa);
1589 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
1590 * the vm86 page table so that vm86 can scribble on them using
1591 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
1592 * page 0, at least as initialized here?
1594 pte = (pt_entry_t *)vm86paddr;
1595 for (i = basemem / 4; i < 160; i++)
1596 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1600 * map page 1 R/W into the kernel page table so we can use it
1601 * as a buffer. The kernel will unmap this page later.
1603 pmap_kenter(KERNBASE + (1 << PAGE_SHIFT), 1 << PAGE_SHIFT);
1606 * get memory map with INT 15:E820
1609 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1610 vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1615 vmf.vmf_eax = 0xE820;
1616 vmf.vmf_edx = SMAP_SIG;
1617 vmf.vmf_ecx = sizeof(struct bios_smap);
1618 i = vm86_datacall(0x15, &vmf, &vmc);
1619 if (i || vmf.vmf_eax != SMAP_SIG)
1621 if (boothowto & RB_VERBOSE)
1622 printf("SMAP type=%02x base=%016llx len=%016llx\n",
1623 smap->type, smap->base, smap->length);
1625 if (smap->type != 0x01)
1628 if (smap->length == 0)
1632 if (smap->base >= 0xffffffff) {
1633 printf("%uK of memory above 4GB ignored\n",
1634 (u_int)(smap->length / 1024));
1639 for (i = 0; i <= physmap_idx; i += 2) {
1640 if (smap->base < physmap[i + 1]) {
1641 if (boothowto & RB_VERBOSE)
1643 "Overlapping or non-montonic memory region, ignoring second region\n");
1648 if (smap->base == physmap[physmap_idx + 1]) {
1649 physmap[physmap_idx + 1] += smap->length;
1654 if (physmap_idx == PHYSMAP_SIZE) {
1656 "Too many segments in the physical address map, giving up\n");
1659 physmap[physmap_idx] = smap->base;
1660 physmap[physmap_idx + 1] = smap->base + smap->length;
1662 } while (vmf.vmf_ebx != 0);
1665 * Perform "base memory" related probes & setup based on SMAP
1668 for (i = 0; i <= physmap_idx; i += 2) {
1669 if (physmap[i] == 0x00000000) {
1670 basemem = physmap[i + 1] / 1024;
1676 * XXX this function is horribly organized and has to the same
1677 * things that it does above here.
1681 if (basemem > 640) {
1683 "Preposterous BIOS basemem of %uK, truncating to 640K\n",
1689 * Let vm86 scribble on pages between basemem and
1690 * ISA_HOLE_START, as above.
1692 for (pa = trunc_page(basemem * 1024);
1693 pa < ISA_HOLE_START; pa += PAGE_SIZE)
1694 pmap_kenter(KERNBASE + pa, pa);
1695 pte = (pt_entry_t *)vm86paddr;
1696 for (i = basemem / 4; i < 160; i++)
1697 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1700 if (physmap[1] != 0)
1704 * If we failed above, try memory map with INT 15:E801
1706 vmf.vmf_ax = 0xE801;
1707 if (vm86_intcall(0x15, &vmf) == 0) {
1708 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1712 vm86_intcall(0x15, &vmf);
1713 extmem = vmf.vmf_ax;
1716 * Prefer the RTC value for extended memory.
1718 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1723 * Special hack for chipsets that still remap the 384k hole when
1724 * there's 16MB of memory - this really confuses people that
1725 * are trying to use bus mastering ISA controllers with the
1726 * "16MB limit"; they only have 16MB, but the remapping puts
1727 * them beyond the limit.
1729 * If extended memory is between 15-16MB (16-17MB phys address range),
1732 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1736 physmap[1] = basemem * 1024;
1738 physmap[physmap_idx] = 0x100000;
1739 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1743 * Now, physmap contains a map of physical memory.
1747 /* make hole for AP bootstrap code */
1748 physmap[1] = mp_bootaddress(physmap[1]);
1752 * Maxmem isn't the "maximum memory", it's one larger than the
1753 * highest page of the physical address space. It should be
1754 * called something like "Maxphyspage". We may adjust this
1755 * based on ``hw.physmem'' and the results of the memory test.
1757 Maxmem = atop(physmap[physmap_idx + 1]);
1760 Maxmem = MAXMEM / 4;
1764 * hw.physmem is a size in bytes; we also allow k, m, and g suffixes
1765 * for the appropriate modifiers. This overrides MAXMEM.
1767 if ((cp = getenv("hw.physmem")) != NULL) {
1768 u_int64_t AllowMem, sanity;
1771 sanity = AllowMem = strtouq(cp, &ep, 0);
1772 if ((ep != cp) && (*ep != 0)) {
1785 AllowMem = sanity = 0;
1787 if (AllowMem < sanity)
1791 printf("Ignoring invalid memory size of '%s'\n", cp);
1793 Maxmem = atop(AllowMem);
1797 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1798 (boothowto & RB_VERBOSE))
1799 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1802 * If Maxmem has been increased beyond what the system has detected,
1803 * extend the last memory segment to the new limit.
1805 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1806 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1808 /* call pmap initialization to make new kernel address space */
1809 pmap_bootstrap(first, 0);
1812 * Size up each available chunk of physical memory.
1814 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1816 phys_avail[pa_indx++] = physmap[0];
1817 phys_avail[pa_indx] = physmap[0];
1821 * Get dcons buffer address
1823 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
1824 getenv_quad("dcons.size", &dcons_size) == 0)
1828 * physmap is in bytes, so when converting to page boundaries,
1829 * round up the start address and round down the end address.
1831 for (i = 0; i <= physmap_idx; i += 2) {
1834 end = ptoa((vm_paddr_t)Maxmem);
1835 if (physmap[i + 1] < end)
1836 end = trunc_page(physmap[i + 1]);
1837 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1839 int *ptr = (int *)CADDR1;
1842 * block out kernel memory as not available.
1844 if (pa >= KERNLOAD && pa < first)
1848 * block out dcons buffer
1851 && pa >= trunc_page(dcons_addr)
1852 && pa < dcons_addr + dcons_size)
1858 * map page into kernel: valid, read/write,non-cacheable
1860 *pte = pa | PG_V | PG_RW | PG_N;
1865 * Test for alternating 1's and 0's
1867 *(volatile int *)ptr = 0xaaaaaaaa;
1868 if (*(volatile int *)ptr != 0xaaaaaaaa) {
1872 * Test for alternating 0's and 1's
1874 *(volatile int *)ptr = 0x55555555;
1875 if (*(volatile int *)ptr != 0x55555555) {
1881 *(volatile int *)ptr = 0xffffffff;
1882 if (*(volatile int *)ptr != 0xffffffff) {
1888 *(volatile int *)ptr = 0x0;
1889 if (*(volatile int *)ptr != 0x0) {
1893 * Restore original value.
1898 * Adjust array of valid/good pages.
1900 if (page_bad == TRUE) {
1904 * If this good page is a continuation of the
1905 * previous set of good pages, then just increase
1906 * the end pointer. Otherwise start a new chunk.
1907 * Note that "end" points one higher than end,
1908 * making the range >= start and < end.
1909 * If we're also doing a speculative memory
1910 * test and we at or past the end, bump up Maxmem
1911 * so that we keep going. The first bad page
1912 * will terminate the loop.
1914 if (phys_avail[pa_indx] == pa) {
1915 phys_avail[pa_indx] += PAGE_SIZE;
1918 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1920 "Too many holes in the physical address space, giving up\n");
1924 phys_avail[pa_indx++] = pa; /* start */
1925 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1935 * The last chunk must contain at least one page plus the message
1936 * buffer to avoid complicating other code (message buffer address
1937 * calculation, etc.).
1939 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1940 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1941 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1942 phys_avail[pa_indx--] = 0;
1943 phys_avail[pa_indx--] = 0;
1946 Maxmem = atop(phys_avail[pa_indx]);
1948 /* Trim off space for the message buffer. */
1949 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1951 avail_end = phys_avail[pa_indx];
1958 struct gate_descriptor *gdp;
1959 int gsel_tss, metadata_missing, off, x;
1962 proc0.p_uarea = proc0uarea;
1963 thread0.td_kstack = proc0kstack;
1964 thread0.td_pcb = (struct pcb *)
1965 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
1968 * This may be done better later if it gets more high level
1969 * components in it. If so just link td->td_proc here.
1971 proc_linkup(&proc0, &ksegrp0, &thread0);
1973 metadata_missing = 0;
1974 if (bootinfo.bi_modulep) {
1975 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
1976 preload_bootstrap_relocate(KERNBASE);
1978 metadata_missing = 1;
1981 kern_envp = static_env;
1982 else if (bootinfo.bi_envp)
1983 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
1985 /* Init basic tunables, hz etc */
1989 * make gdt memory segments, the code segment goes up to end of the
1990 * page with etext in it, the data segment goes to the end of
1994 * XXX text protection is temporarily (?) disabled. The limit was
1995 * i386_btop(round_page(etext)) - 1.
1997 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
1998 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
2000 pc = &SMP_prvspace[0].pcpu;
2001 gdt_segs[GPRIV_SEL].ssd_limit =
2002 atop(sizeof(struct privatespace) - 1);
2005 gdt_segs[GPRIV_SEL].ssd_limit =
2006 atop(sizeof(struct pcpu) - 1);
2008 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2009 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2011 for (x = 0; x < NGDT; x++)
2012 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2014 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2015 r_gdt.rd_base = (int) gdt;
2018 pcpu_init(pc, 0, sizeof(struct pcpu));
2019 PCPU_SET(prvspace, pc);
2020 PCPU_SET(curthread, &thread0);
2021 PCPU_SET(curpcb, thread0.td_pcb);
2024 * Initialize mutexes.
2026 * icu_lock: in order to allow an interrupt to occur in a critical
2027 * section, to set pcpu->ipending (etc...) properly, we
2028 * must be able to get the icu lock, so it can't be
2032 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN);
2033 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS);
2035 /* make ldt memory segments */
2037 * XXX - VM_MAXUSER_ADDRESS is an end address, not a max. And it
2038 * should be spelled ...MAX_USER...
2040 ldt_segs[LUCODE_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1);
2041 ldt_segs[LUDATA_SEL].ssd_limit = atop(VM_MAXUSER_ADDRESS - 1);
2042 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2043 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2045 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2047 PCPU_SET(currentldt, _default_ldt);
2050 for (x = 0; x < NIDT; x++)
2051 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2052 GSEL(GCODE_SEL, SEL_KPL));
2053 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2054 GSEL(GCODE_SEL, SEL_KPL));
2055 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2056 GSEL(GCODE_SEL, SEL_KPL));
2057 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL,
2058 GSEL(GCODE_SEL, SEL_KPL));
2059 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2060 GSEL(GCODE_SEL, SEL_KPL));
2061 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2062 GSEL(GCODE_SEL, SEL_KPL));
2063 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2064 GSEL(GCODE_SEL, SEL_KPL));
2065 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2066 GSEL(GCODE_SEL, SEL_KPL));
2067 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2068 , GSEL(GCODE_SEL, SEL_KPL));
2069 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2070 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2071 GSEL(GCODE_SEL, SEL_KPL));
2072 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2073 GSEL(GCODE_SEL, SEL_KPL));
2074 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2075 GSEL(GCODE_SEL, SEL_KPL));
2076 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2077 GSEL(GCODE_SEL, SEL_KPL));
2078 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2079 GSEL(GCODE_SEL, SEL_KPL));
2080 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2081 GSEL(GCODE_SEL, SEL_KPL));
2082 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2083 GSEL(GCODE_SEL, SEL_KPL));
2084 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2085 GSEL(GCODE_SEL, SEL_KPL));
2086 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2087 GSEL(GCODE_SEL, SEL_KPL));
2088 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2089 GSEL(GCODE_SEL, SEL_KPL));
2090 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2091 GSEL(GCODE_SEL, SEL_KPL));
2093 r_idt.rd_limit = sizeof(idt0) - 1;
2094 r_idt.rd_base = (int) idt;
2098 * Initialize the console before we print anything out.
2102 if (metadata_missing)
2103 printf("WARNING: loader(8) metadata is missing!\n");
2110 ksym_start = bootinfo.bi_symtab;
2111 ksym_end = bootinfo.bi_esymtab;
2117 if (boothowto & RB_KDB)
2118 kdb_enter("Boot flags requested debugger");
2121 finishidentcpu(); /* Final stage of CPU initialization */
2122 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2123 GSEL(GCODE_SEL, SEL_KPL));
2124 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2125 GSEL(GCODE_SEL, SEL_KPL));
2126 initializecpu(); /* Initialize CPU registers */
2128 /* make an initial tss so cpu can get interrupt stack on syscall! */
2129 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2130 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2131 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
2132 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2133 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2135 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2136 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2137 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2140 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2141 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2142 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2143 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2145 dblfault_tss.tss_cr3 = (int)IdlePDPT;
2147 dblfault_tss.tss_cr3 = (int)IdlePTD;
2149 dblfault_tss.tss_eip = (int)dblfault_handler;
2150 dblfault_tss.tss_eflags = PSL_KERNEL;
2151 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2152 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2153 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2154 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2155 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2159 init_param2(physmem);
2161 /* now running on new page tables, configured,and u/iom is accessible */
2163 /* Map the message buffer. */
2164 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
2165 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
2167 msgbufinit(msgbufp, MSGBUF_SIZE);
2169 /* make a call gate to reenter kernel with */
2170 gdp = &ldt[LSYS5CALLS_SEL].gd;
2172 x = (int) &IDTVEC(lcall_syscall);
2173 gdp->gd_looffset = x;
2174 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2176 gdp->gd_type = SDT_SYS386CGT;
2177 gdp->gd_dpl = SEL_UPL;
2179 gdp->gd_hioffset = x >> 16;
2181 /* XXX does this work? */
2182 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2183 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
2185 /* transfer to user mode */
2187 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
2188 _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
2190 /* setup proc 0's pcb */
2191 thread0.td_pcb->pcb_flags = 0; /* XXXKSE */
2193 thread0.td_pcb->pcb_cr3 = (int)IdlePDPT;
2195 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2197 thread0.td_pcb->pcb_ext = 0;
2198 thread0.td_frame = &proc0_tf;
2202 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2205 pcpu->pc_acpi_id = 0xffffffff;
2208 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2209 static void f00f_hack(void *unused);
2210 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL)
2213 f00f_hack(void *unused)
2215 struct gate_descriptor *new_idt;
2223 printf("Intel Pentium detected, installing workaround for F00F bug\n");
2225 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
2227 panic("kmem_alloc returned 0");
2229 /* Put the problematic entry (#6) at the end of the lower page. */
2230 new_idt = (struct gate_descriptor*)
2231 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
2232 bcopy(idt, new_idt, sizeof(idt0));
2233 r_idt.rd_base = (u_int)new_idt;
2236 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2237 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2238 panic("vm_map_protect failed");
2240 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2243 * Construct a PCB from a trapframe. This is called from kdb_trap() where
2244 * we want to start a backtrace from the function that caused us to enter
2245 * the debugger. We have the context in the trapframe, but base the trace
2246 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
2247 * enough for a backtrace.
2250 makectx(struct trapframe *tf, struct pcb *pcb)
2253 pcb->pcb_edi = tf->tf_edi;
2254 pcb->pcb_esi = tf->tf_esi;
2255 pcb->pcb_ebp = tf->tf_ebp;
2256 pcb->pcb_ebx = tf->tf_ebx;
2257 pcb->pcb_eip = tf->tf_eip;
2258 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
2262 ptrace_set_pc(struct thread *td, u_long addr)
2265 td->td_frame->tf_eip = addr;
2270 ptrace_single_step(struct thread *td)
2272 td->td_frame->tf_eflags |= PSL_T;
2277 ptrace_clear_single_step(struct thread *td)
2279 td->td_frame->tf_eflags &= ~PSL_T;
2284 fill_regs(struct thread *td, struct reg *regs)
2287 struct trapframe *tp;
2290 regs->r_fs = tp->tf_fs;
2291 regs->r_es = tp->tf_es;
2292 regs->r_ds = tp->tf_ds;
2293 regs->r_edi = tp->tf_edi;
2294 regs->r_esi = tp->tf_esi;
2295 regs->r_ebp = tp->tf_ebp;
2296 regs->r_ebx = tp->tf_ebx;
2297 regs->r_edx = tp->tf_edx;
2298 regs->r_ecx = tp->tf_ecx;
2299 regs->r_eax = tp->tf_eax;
2300 regs->r_eip = tp->tf_eip;
2301 regs->r_cs = tp->tf_cs;
2302 regs->r_eflags = tp->tf_eflags;
2303 regs->r_esp = tp->tf_esp;
2304 regs->r_ss = tp->tf_ss;
2306 regs->r_gs = pcb->pcb_gs;
2311 set_regs(struct thread *td, struct reg *regs)
2314 struct trapframe *tp;
2317 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2318 !CS_SECURE(regs->r_cs))
2320 tp->tf_fs = regs->r_fs;
2321 tp->tf_es = regs->r_es;
2322 tp->tf_ds = regs->r_ds;
2323 tp->tf_edi = regs->r_edi;
2324 tp->tf_esi = regs->r_esi;
2325 tp->tf_ebp = regs->r_ebp;
2326 tp->tf_ebx = regs->r_ebx;
2327 tp->tf_edx = regs->r_edx;
2328 tp->tf_ecx = regs->r_ecx;
2329 tp->tf_eax = regs->r_eax;
2330 tp->tf_eip = regs->r_eip;
2331 tp->tf_cs = regs->r_cs;
2332 tp->tf_eflags = regs->r_eflags;
2333 tp->tf_esp = regs->r_esp;
2334 tp->tf_ss = regs->r_ss;
2336 pcb->pcb_gs = regs->r_gs;
2340 #ifdef CPU_ENABLE_SSE
2342 fill_fpregs_xmm(sv_xmm, sv_87)
2343 struct savexmm *sv_xmm;
2344 struct save87 *sv_87;
2346 register struct env87 *penv_87 = &sv_87->sv_env;
2347 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2350 bzero(sv_87, sizeof(*sv_87));
2352 /* FPU control/status */
2353 penv_87->en_cw = penv_xmm->en_cw;
2354 penv_87->en_sw = penv_xmm->en_sw;
2355 penv_87->en_tw = penv_xmm->en_tw;
2356 penv_87->en_fip = penv_xmm->en_fip;
2357 penv_87->en_fcs = penv_xmm->en_fcs;
2358 penv_87->en_opcode = penv_xmm->en_opcode;
2359 penv_87->en_foo = penv_xmm->en_foo;
2360 penv_87->en_fos = penv_xmm->en_fos;
2363 for (i = 0; i < 8; ++i)
2364 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
2368 set_fpregs_xmm(sv_87, sv_xmm)
2369 struct save87 *sv_87;
2370 struct savexmm *sv_xmm;
2372 register struct env87 *penv_87 = &sv_87->sv_env;
2373 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2376 /* FPU control/status */
2377 penv_xmm->en_cw = penv_87->en_cw;
2378 penv_xmm->en_sw = penv_87->en_sw;
2379 penv_xmm->en_tw = penv_87->en_tw;
2380 penv_xmm->en_fip = penv_87->en_fip;
2381 penv_xmm->en_fcs = penv_87->en_fcs;
2382 penv_xmm->en_opcode = penv_87->en_opcode;
2383 penv_xmm->en_foo = penv_87->en_foo;
2384 penv_xmm->en_fos = penv_87->en_fos;
2387 for (i = 0; i < 8; ++i)
2388 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
2390 #endif /* CPU_ENABLE_SSE */
2393 fill_fpregs(struct thread *td, struct fpreg *fpregs)
2395 #ifdef CPU_ENABLE_SSE
2397 fill_fpregs_xmm(&td->td_pcb->pcb_save.sv_xmm,
2398 (struct save87 *)fpregs);
2401 #endif /* CPU_ENABLE_SSE */
2402 bcopy(&td->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
2407 set_fpregs(struct thread *td, struct fpreg *fpregs)
2409 #ifdef CPU_ENABLE_SSE
2411 set_fpregs_xmm((struct save87 *)fpregs,
2412 &td->td_pcb->pcb_save.sv_xmm);
2415 #endif /* CPU_ENABLE_SSE */
2416 bcopy(fpregs, &td->td_pcb->pcb_save.sv_87, sizeof *fpregs);
2421 * Get machine context.
2424 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
2426 struct trapframe *tp;
2430 PROC_LOCK(curthread->td_proc);
2431 mcp->mc_onstack = sigonstack(tp->tf_esp);
2432 PROC_UNLOCK(curthread->td_proc);
2433 mcp->mc_gs = td->td_pcb->pcb_gs;
2434 mcp->mc_fs = tp->tf_fs;
2435 mcp->mc_es = tp->tf_es;
2436 mcp->mc_ds = tp->tf_ds;
2437 mcp->mc_edi = tp->tf_edi;
2438 mcp->mc_esi = tp->tf_esi;
2439 mcp->mc_ebp = tp->tf_ebp;
2440 mcp->mc_isp = tp->tf_isp;
2441 if (flags & GET_MC_CLEAR_RET) {
2445 mcp->mc_eax = tp->tf_eax;
2446 mcp->mc_edx = tp->tf_edx;
2448 mcp->mc_ebx = tp->tf_ebx;
2449 mcp->mc_ecx = tp->tf_ecx;
2450 mcp->mc_eip = tp->tf_eip;
2451 mcp->mc_cs = tp->tf_cs;
2452 mcp->mc_eflags = tp->tf_eflags;
2453 mcp->mc_esp = tp->tf_esp;
2454 mcp->mc_ss = tp->tf_ss;
2455 mcp->mc_len = sizeof(*mcp);
2456 get_fpcontext(td, mcp);
2461 * Set machine context.
2463 * However, we don't set any but the user modifiable flags, and we won't
2464 * touch the cs selector.
2467 set_mcontext(struct thread *td, const mcontext_t *mcp)
2469 struct trapframe *tp;
2473 if (mcp->mc_len != sizeof(*mcp))
2475 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
2476 (tp->tf_eflags & ~PSL_USERCHANGE);
2477 if ((ret = set_fpcontext(td, mcp)) == 0) {
2478 tp->tf_fs = mcp->mc_fs;
2479 tp->tf_es = mcp->mc_es;
2480 tp->tf_ds = mcp->mc_ds;
2481 tp->tf_edi = mcp->mc_edi;
2482 tp->tf_esi = mcp->mc_esi;
2483 tp->tf_ebp = mcp->mc_ebp;
2484 tp->tf_ebx = mcp->mc_ebx;
2485 tp->tf_edx = mcp->mc_edx;
2486 tp->tf_ecx = mcp->mc_ecx;
2487 tp->tf_eax = mcp->mc_eax;
2488 tp->tf_eip = mcp->mc_eip;
2489 tp->tf_eflags = eflags;
2490 tp->tf_esp = mcp->mc_esp;
2491 tp->tf_ss = mcp->mc_ss;
2492 td->td_pcb->pcb_gs = mcp->mc_gs;
2499 get_fpcontext(struct thread *td, mcontext_t *mcp)
2502 mcp->mc_fpformat = _MC_FPFMT_NODEV;
2503 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
2505 union savefpu *addr;
2508 * XXX mc_fpstate might be misaligned, since its declaration is not
2509 * unportabilized using __attribute__((aligned(16))) like the
2510 * declaration of struct savemm, and anyway, alignment doesn't work
2511 * for auto variables since we don't use gcc's pessimal stack
2512 * alignment. Work around this by abusing the spare fields after
2515 * XXX unpessimize most cases by only aligning when fxsave might be
2516 * called, although this requires knowing too much about
2517 * npxgetregs()'s internals.
2519 addr = (union savefpu *)&mcp->mc_fpstate;
2520 if (td == PCPU_GET(fpcurthread) &&
2521 #ifdef CPU_ENABLE_SSE
2524 ((uintptr_t)(void *)addr & 0xF)) {
2526 addr = (void *)((char *)addr + 4);
2527 while ((uintptr_t)(void *)addr & 0xF);
2529 mcp->mc_ownedfp = npxgetregs(td, addr);
2530 if (addr != (union savefpu *)&mcp->mc_fpstate) {
2531 bcopy(addr, &mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
2532 bzero(&mcp->mc_spare2, sizeof(mcp->mc_spare2));
2534 mcp->mc_fpformat = npxformat();
2539 set_fpcontext(struct thread *td, const mcontext_t *mcp)
2541 union savefpu *addr;
2543 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2545 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2546 mcp->mc_fpformat != _MC_FPFMT_XMM)
2548 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
2549 /* We don't care what state is left in the FPU or PCB. */
2551 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2552 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2553 /* XXX align as above. */
2554 addr = (union savefpu *)&mcp->mc_fpstate;
2555 if (td == PCPU_GET(fpcurthread) &&
2556 #ifdef CPU_ENABLE_SSE
2559 ((uintptr_t)(void *)addr & 0xF)) {
2561 addr = (void *)((char *)addr + 4);
2562 while ((uintptr_t)(void *)addr & 0xF);
2563 bcopy(&mcp->mc_fpstate, addr, sizeof(mcp->mc_fpstate));
2567 * XXX we violate the dubious requirement that npxsetregs()
2568 * be called with interrupts disabled.
2570 npxsetregs(td, addr);
2573 * Don't bother putting things back where they were in the
2574 * misaligned case, since we know that the caller won't use
2583 fpstate_drop(struct thread *td)
2589 if (PCPU_GET(fpcurthread) == td)
2593 * XXX force a full drop of the npx. The above only drops it if we
2594 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
2596 * XXX I don't much like npxgetregs()'s semantics of doing a full
2597 * drop. Dropping only to the pcb matches fnsave's behaviour.
2598 * We only need to drop to !PCB_INITDONE in sendsig(). But
2599 * sendsig() is the only caller of npxgetregs()... perhaps we just
2600 * have too many layers.
2602 curthread->td_pcb->pcb_flags &= ~PCB_NPXINITDONE;
2607 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2612 dbregs->dr[0] = rdr0();
2613 dbregs->dr[1] = rdr1();
2614 dbregs->dr[2] = rdr2();
2615 dbregs->dr[3] = rdr3();
2616 dbregs->dr[4] = rdr4();
2617 dbregs->dr[5] = rdr5();
2618 dbregs->dr[6] = rdr6();
2619 dbregs->dr[7] = rdr7();
2622 dbregs->dr[0] = pcb->pcb_dr0;
2623 dbregs->dr[1] = pcb->pcb_dr1;
2624 dbregs->dr[2] = pcb->pcb_dr2;
2625 dbregs->dr[3] = pcb->pcb_dr3;
2628 dbregs->dr[6] = pcb->pcb_dr6;
2629 dbregs->dr[7] = pcb->pcb_dr7;
2635 set_dbregs(struct thread *td, struct dbreg *dbregs)
2639 u_int32_t mask1, mask2;
2642 load_dr0(dbregs->dr[0]);
2643 load_dr1(dbregs->dr[1]);
2644 load_dr2(dbregs->dr[2]);
2645 load_dr3(dbregs->dr[3]);
2646 load_dr4(dbregs->dr[4]);
2647 load_dr5(dbregs->dr[5]);
2648 load_dr6(dbregs->dr[6]);
2649 load_dr7(dbregs->dr[7]);
2652 * Don't let an illegal value for dr7 get set. Specifically,
2653 * check for undefined settings. Setting these bit patterns
2654 * result in undefined behaviour and can lead to an unexpected
2657 for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8;
2658 i++, mask1 <<= 2, mask2 <<= 2)
2659 if ((dbregs->dr[7] & mask1) == mask2)
2665 * Don't let a process set a breakpoint that is not within the
2666 * process's address space. If a process could do this, it
2667 * could halt the system by setting a breakpoint in the kernel
2668 * (if ddb was enabled). Thus, we need to check to make sure
2669 * that no breakpoints are being enabled for addresses outside
2670 * process's address space, unless, perhaps, we were called by
2673 * XXX - what about when the watched area of the user's
2674 * address space is written into from within the kernel
2675 * ... wouldn't that still cause a breakpoint to be generated
2676 * from within kernel mode?
2679 if (suser(td) != 0) {
2680 if (dbregs->dr[7] & 0x3) {
2681 /* dr0 is enabled */
2682 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2686 if (dbregs->dr[7] & (0x3<<2)) {
2687 /* dr1 is enabled */
2688 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2692 if (dbregs->dr[7] & (0x3<<4)) {
2693 /* dr2 is enabled */
2694 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2698 if (dbregs->dr[7] & (0x3<<6)) {
2699 /* dr3 is enabled */
2700 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2705 pcb->pcb_dr0 = dbregs->dr[0];
2706 pcb->pcb_dr1 = dbregs->dr[1];
2707 pcb->pcb_dr2 = dbregs->dr[2];
2708 pcb->pcb_dr3 = dbregs->dr[3];
2709 pcb->pcb_dr6 = dbregs->dr[6];
2710 pcb->pcb_dr7 = dbregs->dr[7];
2712 pcb->pcb_flags |= PCB_DBREGS;
2719 * Return > 0 if a hardware breakpoint has been hit, and the
2720 * breakpoint was in user space. Return 0, otherwise.
2723 user_dbreg_trap(void)
2725 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2726 u_int32_t bp; /* breakpoint bits extracted from dr6 */
2727 int nbp; /* number of breakpoints that triggered */
2728 caddr_t addr[4]; /* breakpoint addresses */
2732 if ((dr7 & 0x000000ff) == 0) {
2734 * all GE and LE bits in the dr7 register are zero,
2735 * thus the trap couldn't have been caused by the
2736 * hardware debug registers
2743 bp = dr6 & 0x0000000f;
2747 * None of the breakpoint bits are set meaning this
2748 * trap was not caused by any of the debug registers
2754 * at least one of the breakpoints were hit, check to see
2755 * which ones and if any of them are user space addresses
2759 addr[nbp++] = (caddr_t)rdr0();
2762 addr[nbp++] = (caddr_t)rdr1();
2765 addr[nbp++] = (caddr_t)rdr2();
2768 addr[nbp++] = (caddr_t)rdr3();
2771 for (i=0; i<nbp; i++) {
2773 (caddr_t)VM_MAXUSER_ADDRESS) {
2775 * addr[i] is in user space
2782 * None of the breakpoints are in user space.
2788 #include <machine/apicvar.h>
2791 * Provide stub functions so that the MADT APIC enumerator in the acpi
2792 * kernel module will link against a kernel without 'device apic'.
2794 * XXX - This is a gross hack.
2797 apic_register_enumerator(struct apic_enumerator *enumerator)
2802 ioapic_create(uintptr_t addr, int32_t id, int intbase)
2808 ioapic_disable_pin(void *cookie, u_int pin)
2814 ioapic_enable_mixed_mode(void)
2819 ioapic_get_vector(void *cookie, u_int pin)
2825 ioapic_register(void *cookie)
2830 ioapic_remap_vector(void *cookie, u_int pin, int vector)
2836 ioapic_set_extint(void *cookie, u_int pin)
2842 ioapic_set_nmi(void *cookie, u_int pin)
2848 ioapic_set_polarity(void *cookie, u_int pin, enum intr_polarity pol)
2854 ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger)
2860 lapic_create(u_int apic_id, int boot_cpu)
2865 lapic_init(uintptr_t addr)
2870 lapic_set_lvt_mode(u_int apic_id, u_int lvt, u_int32_t mode)
2876 lapic_set_lvt_polarity(u_int apic_id, u_int lvt, enum intr_polarity pol)
2882 lapic_set_lvt_triggermode(u_int apic_id, u_int lvt, enum intr_trigger trigger)
2891 * Provide inb() and outb() as functions. They are normally only
2892 * available as macros calling inlined functions, thus cannot be
2893 * called from the debugger.
2895 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2901 /* silence compiler warnings */
2903 void outb(u_int, u_char);
2910 * We use %%dx and not %1 here because i/o is done at %dx and not at
2911 * %edx, while gcc generates inferior code (movw instead of movl)
2912 * if we tell it to load (u_short) port.
2914 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2919 outb(u_int port, u_char data)
2923 * Use an unnecessary assignment to help gcc's register allocator.
2924 * This make a large difference for gcc-1.40 and a tiny difference
2925 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
2926 * best results. gcc-2.6.0 can't handle this.
2929 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));