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$");
43 #include "opt_atalk.h"
44 #include "opt_compat.h"
50 #include "opt_kstack_pages.h"
51 #include "opt_maxmem.h"
53 #include "opt_perfmon.h"
55 #include <sys/param.h>
57 #include <sys/systm.h>
61 #include <sys/callout.h>
64 #include <sys/eventhandler.h>
66 #include <sys/imgact.h>
68 #include <sys/kernel.h>
70 #include <sys/linker.h>
72 #include <sys/malloc.h>
73 #include <sys/msgbuf.h>
74 #include <sys/mutex.h>
76 #include <sys/ptrace.h>
77 #include <sys/reboot.h>
78 #include <sys/sched.h>
79 #include <sys/signalvar.h>
80 #include <sys/sysctl.h>
81 #include <sys/sysent.h>
82 #include <sys/sysproto.h>
83 #include <sys/ucontext.h>
84 #include <sys/vmmeter.h>
87 #include <vm/vm_extern.h>
88 #include <vm/vm_kern.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_pager.h>
93 #include <vm/vm_param.h>
97 #error KDB must be enabled in order for DDB to work!
100 #include <ddb/db_sym.h>
103 #include <pc98/pc98/pc98_machdep.h>
105 #include <net/netisr.h>
107 #include <machine/bootinfo.h>
108 #include <machine/clock.h>
109 #include <machine/cpu.h>
110 #include <machine/cputypes.h>
111 #include <machine/intr_machdep.h>
112 #include <machine/mca.h>
113 #include <machine/md_var.h>
114 #include <machine/pc/bios.h>
115 #include <machine/pcb.h>
116 #include <machine/pcb_ext.h>
117 #include <machine/proc.h>
118 #include <machine/reg.h>
119 #include <machine/sigframe.h>
120 #include <machine/specialreg.h>
121 #include <machine/vm86.h>
123 #include <machine/perfmon.h>
126 #include <machine/smp.h>
130 #include <i386/isa/icu.h>
133 /* Sanity check for __curthread() */
134 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
136 extern void init386(int first);
137 extern void dblfault_handler(void);
139 extern void printcpuinfo(void); /* XXX header file */
140 extern void finishidentcpu(void);
141 extern void panicifcpuunsupported(void);
142 extern void initializecpu(void);
144 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
145 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
147 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
148 #define CPU_ENABLE_SSE
151 static void cpu_startup(void *);
152 static void fpstate_drop(struct thread *td);
153 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
154 static int set_fpcontext(struct thread *td, const mcontext_t *mcp);
155 #ifdef CPU_ENABLE_SSE
156 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
157 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
158 #endif /* CPU_ENABLE_SSE */
159 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
161 int need_pre_dma_flush; /* If 1, use wbinvd befor DMA transfer. */
162 int need_post_dma_flush; /* If 1, use invd after DMA transfer. */
165 extern vm_offset_t ksym_start, ksym_end;
168 int _udatasel, _ucodesel;
171 static int ispc98 = 1;
172 SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, "");
177 static void osendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
179 #ifdef COMPAT_FREEBSD4
180 static void freebsd4_sendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
187 * The number of PHYSMAP entries must be one less than the number of
188 * PHYSSEG entries because the PHYSMAP entry that spans the largest
189 * physical address that is accessible by ISA DMA is split into two
192 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
194 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
195 vm_paddr_t dump_avail[PHYSMAP_SIZE + 2];
197 /* must be 2 less so 0 0 can signal end of chunks */
198 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(phys_avail[0])) - 2)
199 #define DUMP_AVAIL_ARRAY_END ((sizeof(dump_avail) / sizeof(dump_avail[0])) - 2)
201 struct kva_md_info kmi;
203 static struct trapframe proc0_tf;
204 struct pcpu __pcpu[MAXCPU];
215 * Good {morning,afternoon,evening,night}.
219 panicifcpuunsupported();
226 * Display physical memory.
228 memsize = ptoa((uintmax_t)Maxmem);
229 printf("real memory = %ju (%ju MB)\n", memsize, memsize >> 20);
232 * Display any holes after the first chunk of extended memory.
237 printf("Physical memory chunk(s):\n");
238 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
241 size = phys_avail[indx + 1] - phys_avail[indx];
243 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
244 (uintmax_t)phys_avail[indx],
245 (uintmax_t)phys_avail[indx + 1] - 1,
246 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
250 vm_ksubmap_init(&kmi);
252 printf("avail memory = %ju (%ju MB)\n",
253 ptoa((uintmax_t)cnt.v_free_count),
254 ptoa((uintmax_t)cnt.v_free_count) / 1048576);
257 * Set up buffers, so they can be used to read disk labels.
260 vm_pager_bufferinit();
265 * Send an interrupt to process.
267 * Stack is set up to allow sigcode stored
268 * at top to call routine, followed by kcall
269 * to sigreturn routine below. After sigreturn
270 * resets the signal mask, the stack, and the
271 * frame pointer, it returns to the user
276 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
278 struct osigframe sf, *fp;
282 struct trapframe *regs;
288 PROC_LOCK_ASSERT(p, MA_OWNED);
289 sig = ksi->ksi_signo;
291 mtx_assert(&psp->ps_mtx, MA_OWNED);
293 oonstack = sigonstack(regs->tf_esp);
295 /* Allocate space for the signal handler context. */
296 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
297 SIGISMEMBER(psp->ps_sigonstack, sig)) {
298 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
299 td->td_sigstk.ss_size - sizeof(struct osigframe));
300 #if defined(COMPAT_43)
301 td->td_sigstk.ss_flags |= SS_ONSTACK;
304 fp = (struct osigframe *)regs->tf_esp - 1;
306 /* Translate the signal if appropriate. */
307 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
308 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
310 /* Build the argument list for the signal handler. */
312 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
313 bzero(&sf.sf_siginfo, sizeof(sf.sf_siginfo));
314 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
315 /* Signal handler installed with SA_SIGINFO. */
316 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
317 sf.sf_siginfo.si_signo = sig;
318 sf.sf_siginfo.si_code = ksi->ksi_code;
319 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
322 /* Old FreeBSD-style arguments. */
323 sf.sf_arg2 = ksi->ksi_code;
324 sf.sf_addr = (register_t)ksi->ksi_addr;
325 sf.sf_ahu.sf_handler = catcher;
327 mtx_unlock(&psp->ps_mtx);
330 /* Save most if not all of trap frame. */
331 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
332 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
333 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
334 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
335 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
336 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
337 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
338 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
339 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
340 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
341 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
342 sf.sf_siginfo.si_sc.sc_gs = rgs();
343 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
345 /* Build the signal context to be used by osigreturn(). */
346 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
347 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
348 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
349 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
350 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
351 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
352 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
353 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
356 * If we're a vm86 process, we want to save the segment registers.
357 * We also change eflags to be our emulated eflags, not the actual
360 if (regs->tf_eflags & PSL_VM) {
361 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
362 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
363 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
365 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
366 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
367 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
368 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
370 if (vm86->vm86_has_vme == 0)
371 sf.sf_siginfo.si_sc.sc_ps =
372 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
373 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
375 /* See sendsig() for comments. */
376 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
380 * Copy the sigframe out to the user's stack.
382 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
384 printf("process %ld has trashed its stack\n", (long)p->p_pid);
390 regs->tf_esp = (int)fp;
391 regs->tf_eip = PS_STRINGS - szosigcode;
392 regs->tf_eflags &= ~(PSL_T | PSL_D);
393 regs->tf_cs = _ucodesel;
394 regs->tf_ds = _udatasel;
395 regs->tf_es = _udatasel;
396 regs->tf_fs = _udatasel;
398 regs->tf_ss = _udatasel;
400 mtx_lock(&psp->ps_mtx);
402 #endif /* COMPAT_43 */
404 #ifdef COMPAT_FREEBSD4
406 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
408 struct sigframe4 sf, *sfp;
412 struct trapframe *regs;
418 PROC_LOCK_ASSERT(p, MA_OWNED);
419 sig = ksi->ksi_signo;
421 mtx_assert(&psp->ps_mtx, MA_OWNED);
423 oonstack = sigonstack(regs->tf_esp);
425 /* Save user context. */
426 bzero(&sf, sizeof(sf));
427 sf.sf_uc.uc_sigmask = *mask;
428 sf.sf_uc.uc_stack = td->td_sigstk;
429 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
430 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
431 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
432 sf.sf_uc.uc_mcontext.mc_gs = rgs();
433 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
434 bzero(sf.sf_uc.uc_mcontext.mc_fpregs,
435 sizeof(sf.sf_uc.uc_mcontext.mc_fpregs));
436 bzero(sf.sf_uc.uc_mcontext.__spare__,
437 sizeof(sf.sf_uc.uc_mcontext.__spare__));
438 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
440 /* Allocate space for the signal handler context. */
441 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
442 SIGISMEMBER(psp->ps_sigonstack, sig)) {
443 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
444 td->td_sigstk.ss_size - sizeof(struct sigframe4));
445 #if defined(COMPAT_43)
446 td->td_sigstk.ss_flags |= SS_ONSTACK;
449 sfp = (struct sigframe4 *)regs->tf_esp - 1;
451 /* Translate the signal if appropriate. */
452 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
453 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
455 /* Build the argument list for the signal handler. */
457 sf.sf_ucontext = (register_t)&sfp->sf_uc;
458 bzero(&sf.sf_si, sizeof(sf.sf_si));
459 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
460 /* Signal handler installed with SA_SIGINFO. */
461 sf.sf_siginfo = (register_t)&sfp->sf_si;
462 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
464 /* Fill in POSIX parts */
465 sf.sf_si.si_signo = sig;
466 sf.sf_si.si_code = ksi->ksi_code;
467 sf.sf_si.si_addr = ksi->ksi_addr;
469 /* Old FreeBSD-style arguments. */
470 sf.sf_siginfo = ksi->ksi_code;
471 sf.sf_addr = (register_t)ksi->ksi_addr;
472 sf.sf_ahu.sf_handler = catcher;
474 mtx_unlock(&psp->ps_mtx);
478 * If we're a vm86 process, we want to save the segment registers.
479 * We also change eflags to be our emulated eflags, not the actual
482 if (regs->tf_eflags & PSL_VM) {
483 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
484 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
486 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
487 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
488 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
489 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
491 if (vm86->vm86_has_vme == 0)
492 sf.sf_uc.uc_mcontext.mc_eflags =
493 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
494 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
497 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
498 * syscalls made by the signal handler. This just avoids
499 * wasting time for our lazy fixup of such faults. PSL_NT
500 * does nothing in vm86 mode, but vm86 programs can set it
501 * almost legitimately in probes for old cpu types.
503 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
507 * Copy the sigframe out to the user's stack.
509 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
511 printf("process %ld has trashed its stack\n", (long)p->p_pid);
517 regs->tf_esp = (int)sfp;
518 regs->tf_eip = PS_STRINGS - szfreebsd4_sigcode;
519 regs->tf_eflags &= ~(PSL_T | PSL_D);
520 regs->tf_cs = _ucodesel;
521 regs->tf_ds = _udatasel;
522 regs->tf_es = _udatasel;
523 regs->tf_fs = _udatasel;
524 regs->tf_ss = _udatasel;
526 mtx_lock(&psp->ps_mtx);
528 #endif /* COMPAT_FREEBSD4 */
531 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
533 struct sigframe sf, *sfp;
538 struct trapframe *regs;
539 struct segment_descriptor *sdp;
545 PROC_LOCK_ASSERT(p, MA_OWNED);
546 sig = ksi->ksi_signo;
548 mtx_assert(&psp->ps_mtx, MA_OWNED);
549 #ifdef COMPAT_FREEBSD4
550 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
551 freebsd4_sendsig(catcher, ksi, mask);
556 if (SIGISMEMBER(psp->ps_osigset, sig)) {
557 osendsig(catcher, ksi, mask);
562 oonstack = sigonstack(regs->tf_esp);
564 /* Save user context. */
565 bzero(&sf, sizeof(sf));
566 sf.sf_uc.uc_sigmask = *mask;
567 sf.sf_uc.uc_stack = td->td_sigstk;
568 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
569 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
570 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
571 sf.sf_uc.uc_mcontext.mc_gs = rgs();
572 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
573 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
574 get_fpcontext(td, &sf.sf_uc.uc_mcontext);
577 * Unconditionally fill the fsbase and gsbase into the mcontext.
579 sdp = &td->td_pcb->pcb_fsd;
580 sf.sf_uc.uc_mcontext.mc_fsbase = sdp->sd_hibase << 24 |
582 sdp = &td->td_pcb->pcb_gsd;
583 sf.sf_uc.uc_mcontext.mc_gsbase = sdp->sd_hibase << 24 |
585 bzero(sf.sf_uc.uc_mcontext.mc_spare1,
586 sizeof(sf.sf_uc.uc_mcontext.mc_spare1));
587 bzero(sf.sf_uc.uc_mcontext.mc_spare2,
588 sizeof(sf.sf_uc.uc_mcontext.mc_spare2));
589 bzero(sf.sf_uc.__spare__, sizeof(sf.sf_uc.__spare__));
591 /* Allocate space for the signal handler context. */
592 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
593 SIGISMEMBER(psp->ps_sigonstack, sig)) {
594 sp = td->td_sigstk.ss_sp +
595 td->td_sigstk.ss_size - sizeof(struct sigframe);
596 #if defined(COMPAT_43)
597 td->td_sigstk.ss_flags |= SS_ONSTACK;
600 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
601 /* Align to 16 bytes. */
602 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
604 /* Translate the signal if appropriate. */
605 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
606 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
608 /* Build the argument list for the signal handler. */
610 sf.sf_ucontext = (register_t)&sfp->sf_uc;
611 bzero(&sf.sf_si, sizeof(sf.sf_si));
612 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
613 /* Signal handler installed with SA_SIGINFO. */
614 sf.sf_siginfo = (register_t)&sfp->sf_si;
615 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
617 /* Fill in POSIX parts */
618 sf.sf_si = ksi->ksi_info;
619 sf.sf_si.si_signo = sig; /* maybe a translated signal */
621 /* Old FreeBSD-style arguments. */
622 sf.sf_siginfo = ksi->ksi_code;
623 sf.sf_addr = (register_t)ksi->ksi_addr;
624 sf.sf_ahu.sf_handler = catcher;
626 mtx_unlock(&psp->ps_mtx);
630 * If we're a vm86 process, we want to save the segment registers.
631 * We also change eflags to be our emulated eflags, not the actual
634 if (regs->tf_eflags & PSL_VM) {
635 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
636 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
638 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
639 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
640 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
641 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
643 if (vm86->vm86_has_vme == 0)
644 sf.sf_uc.uc_mcontext.mc_eflags =
645 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
646 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
649 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
650 * syscalls made by the signal handler. This just avoids
651 * wasting time for our lazy fixup of such faults. PSL_NT
652 * does nothing in vm86 mode, but vm86 programs can set it
653 * almost legitimately in probes for old cpu types.
655 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
659 * Copy the sigframe out to the user's stack.
661 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
663 printf("process %ld has trashed its stack\n", (long)p->p_pid);
669 regs->tf_esp = (int)sfp;
670 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
671 regs->tf_eflags &= ~(PSL_T | PSL_D);
672 regs->tf_cs = _ucodesel;
673 regs->tf_ds = _udatasel;
674 regs->tf_es = _udatasel;
675 regs->tf_fs = _udatasel;
676 regs->tf_ss = _udatasel;
678 mtx_lock(&psp->ps_mtx);
682 * System call to cleanup state after a signal
683 * has been taken. Reset signal mask and
684 * stack state from context left by sendsig (above).
685 * Return to previous pc and psl as specified by
686 * context left by sendsig. Check carefully to
687 * make sure that the user has not modified the
688 * state to gain improper privileges.
696 struct osigreturn_args /* {
697 struct osigcontext *sigcntxp;
700 struct osigcontext sc;
701 struct trapframe *regs;
702 struct osigcontext *scp;
707 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
712 if (eflags & PSL_VM) {
713 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
714 struct vm86_kernel *vm86;
717 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
718 * set up the vm86 area, and we can't enter vm86 mode.
720 if (td->td_pcb->pcb_ext == 0)
722 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
723 if (vm86->vm86_inited == 0)
726 /* Go back to user mode if both flags are set. */
727 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
728 ksiginfo_init_trap(&ksi);
729 ksi.ksi_signo = SIGBUS;
730 ksi.ksi_code = BUS_OBJERR;
731 ksi.ksi_addr = (void *)regs->tf_eip;
732 trapsignal(td, &ksi);
735 if (vm86->vm86_has_vme) {
736 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
737 (eflags & VME_USERCHANGE) | PSL_VM;
739 vm86->vm86_eflags = eflags; /* save VIF, VIP */
740 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
741 (eflags & VM_USERCHANGE) | PSL_VM;
743 tf->tf_vm86_ds = scp->sc_ds;
744 tf->tf_vm86_es = scp->sc_es;
745 tf->tf_vm86_fs = scp->sc_fs;
746 tf->tf_vm86_gs = scp->sc_gs;
747 tf->tf_ds = _udatasel;
748 tf->tf_es = _udatasel;
749 tf->tf_fs = _udatasel;
752 * Don't allow users to change privileged or reserved flags.
755 * XXX do allow users to change the privileged flag PSL_RF.
756 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
757 * should sometimes set it there too. tf_eflags is kept in
758 * the signal context during signal handling and there is no
759 * other place to remember it, so the PSL_RF bit may be
760 * corrupted by the signal handler without us knowing.
761 * Corruption of the PSL_RF bit at worst causes one more or
762 * one less debugger trap, so allowing it is fairly harmless.
764 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
769 * Don't allow users to load a valid privileged %cs. Let the
770 * hardware check for invalid selectors, excess privilege in
771 * other selectors, invalid %eip's and invalid %esp's.
773 if (!CS_SECURE(scp->sc_cs)) {
774 ksiginfo_init_trap(&ksi);
775 ksi.ksi_signo = SIGBUS;
776 ksi.ksi_code = BUS_OBJERR;
777 ksi.ksi_trapno = T_PROTFLT;
778 ksi.ksi_addr = (void *)regs->tf_eip;
779 trapsignal(td, &ksi);
782 regs->tf_ds = scp->sc_ds;
783 regs->tf_es = scp->sc_es;
784 regs->tf_fs = scp->sc_fs;
787 /* Restore remaining registers. */
788 regs->tf_eax = scp->sc_eax;
789 regs->tf_ebx = scp->sc_ebx;
790 regs->tf_ecx = scp->sc_ecx;
791 regs->tf_edx = scp->sc_edx;
792 regs->tf_esi = scp->sc_esi;
793 regs->tf_edi = scp->sc_edi;
794 regs->tf_cs = scp->sc_cs;
795 regs->tf_ss = scp->sc_ss;
796 regs->tf_isp = scp->sc_isp;
797 regs->tf_ebp = scp->sc_fp;
798 regs->tf_esp = scp->sc_sp;
799 regs->tf_eip = scp->sc_pc;
800 regs->tf_eflags = eflags;
802 #if defined(COMPAT_43)
803 if (scp->sc_onstack & 1)
804 td->td_sigstk.ss_flags |= SS_ONSTACK;
806 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
808 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
810 return (EJUSTRETURN);
812 #endif /* COMPAT_43 */
814 #ifdef COMPAT_FREEBSD4
819 freebsd4_sigreturn(td, uap)
821 struct freebsd4_sigreturn_args /* {
822 const ucontext4 *sigcntxp;
826 struct trapframe *regs;
827 struct ucontext4 *ucp;
828 int cs, eflags, error;
831 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
836 eflags = ucp->uc_mcontext.mc_eflags;
837 if (eflags & PSL_VM) {
838 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
839 struct vm86_kernel *vm86;
842 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
843 * set up the vm86 area, and we can't enter vm86 mode.
845 if (td->td_pcb->pcb_ext == 0)
847 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
848 if (vm86->vm86_inited == 0)
851 /* Go back to user mode if both flags are set. */
852 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
853 ksiginfo_init_trap(&ksi);
854 ksi.ksi_signo = SIGBUS;
855 ksi.ksi_code = BUS_OBJERR;
856 ksi.ksi_addr = (void *)regs->tf_eip;
857 trapsignal(td, &ksi);
859 if (vm86->vm86_has_vme) {
860 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
861 (eflags & VME_USERCHANGE) | PSL_VM;
863 vm86->vm86_eflags = eflags; /* save VIF, VIP */
864 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
865 (eflags & VM_USERCHANGE) | PSL_VM;
867 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
868 tf->tf_eflags = eflags;
869 tf->tf_vm86_ds = tf->tf_ds;
870 tf->tf_vm86_es = tf->tf_es;
871 tf->tf_vm86_fs = tf->tf_fs;
872 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
873 tf->tf_ds = _udatasel;
874 tf->tf_es = _udatasel;
875 tf->tf_fs = _udatasel;
878 * Don't allow users to change privileged or reserved flags.
881 * XXX do allow users to change the privileged flag PSL_RF.
882 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
883 * should sometimes set it there too. tf_eflags is kept in
884 * the signal context during signal handling and there is no
885 * other place to remember it, so the PSL_RF bit may be
886 * corrupted by the signal handler without us knowing.
887 * Corruption of the PSL_RF bit at worst causes one more or
888 * one less debugger trap, so allowing it is fairly harmless.
890 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
891 uprintf("pid %d (%s): freebsd4_sigreturn eflags = 0x%x\n",
892 td->td_proc->p_pid, td->td_name, eflags);
897 * Don't allow users to load a valid privileged %cs. Let the
898 * hardware check for invalid selectors, excess privilege in
899 * other selectors, invalid %eip's and invalid %esp's.
901 cs = ucp->uc_mcontext.mc_cs;
902 if (!CS_SECURE(cs)) {
903 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
904 td->td_proc->p_pid, td->td_name, cs);
905 ksiginfo_init_trap(&ksi);
906 ksi.ksi_signo = SIGBUS;
907 ksi.ksi_code = BUS_OBJERR;
908 ksi.ksi_trapno = T_PROTFLT;
909 ksi.ksi_addr = (void *)regs->tf_eip;
910 trapsignal(td, &ksi);
914 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
917 #if defined(COMPAT_43)
918 if (ucp->uc_mcontext.mc_onstack & 1)
919 td->td_sigstk.ss_flags |= SS_ONSTACK;
921 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
923 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
924 return (EJUSTRETURN);
926 #endif /* COMPAT_FREEBSD4 */
934 struct sigreturn_args /* {
935 const struct __ucontext *sigcntxp;
939 struct trapframe *regs;
941 int cs, eflags, error, ret;
944 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
949 eflags = ucp->uc_mcontext.mc_eflags;
950 if (eflags & PSL_VM) {
951 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
952 struct vm86_kernel *vm86;
955 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
956 * set up the vm86 area, and we can't enter vm86 mode.
958 if (td->td_pcb->pcb_ext == 0)
960 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
961 if (vm86->vm86_inited == 0)
964 /* Go back to user mode if both flags are set. */
965 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
966 ksiginfo_init_trap(&ksi);
967 ksi.ksi_signo = SIGBUS;
968 ksi.ksi_code = BUS_OBJERR;
969 ksi.ksi_addr = (void *)regs->tf_eip;
970 trapsignal(td, &ksi);
973 if (vm86->vm86_has_vme) {
974 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
975 (eflags & VME_USERCHANGE) | PSL_VM;
977 vm86->vm86_eflags = eflags; /* save VIF, VIP */
978 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
979 (eflags & VM_USERCHANGE) | PSL_VM;
981 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
982 tf->tf_eflags = eflags;
983 tf->tf_vm86_ds = tf->tf_ds;
984 tf->tf_vm86_es = tf->tf_es;
985 tf->tf_vm86_fs = tf->tf_fs;
986 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
987 tf->tf_ds = _udatasel;
988 tf->tf_es = _udatasel;
989 tf->tf_fs = _udatasel;
992 * Don't allow users to change privileged or reserved flags.
995 * XXX do allow users to change the privileged flag PSL_RF.
996 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
997 * should sometimes set it there too. tf_eflags is kept in
998 * the signal context during signal handling and there is no
999 * other place to remember it, so the PSL_RF bit may be
1000 * corrupted by the signal handler without us knowing.
1001 * Corruption of the PSL_RF bit at worst causes one more or
1002 * one less debugger trap, so allowing it is fairly harmless.
1004 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
1005 uprintf("pid %d (%s): sigreturn eflags = 0x%x\n",
1006 td->td_proc->p_pid, td->td_name, eflags);
1011 * Don't allow users to load a valid privileged %cs. Let the
1012 * hardware check for invalid selectors, excess privilege in
1013 * other selectors, invalid %eip's and invalid %esp's.
1015 cs = ucp->uc_mcontext.mc_cs;
1016 if (!CS_SECURE(cs)) {
1017 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
1018 td->td_proc->p_pid, td->td_name, cs);
1019 ksiginfo_init_trap(&ksi);
1020 ksi.ksi_signo = SIGBUS;
1021 ksi.ksi_code = BUS_OBJERR;
1022 ksi.ksi_trapno = T_PROTFLT;
1023 ksi.ksi_addr = (void *)regs->tf_eip;
1024 trapsignal(td, &ksi);
1028 ret = set_fpcontext(td, &ucp->uc_mcontext);
1031 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1034 #if defined(COMPAT_43)
1035 if (ucp->uc_mcontext.mc_onstack & 1)
1036 td->td_sigstk.ss_flags |= SS_ONSTACK;
1038 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1041 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
1042 return (EJUSTRETURN);
1046 * Machine dependent boot() routine
1048 * I haven't seen anything to put here yet
1049 * Possibly some stuff might be grafted back here from boot()
1057 * Flush the D-cache for non-DMA I/O so that the I-cache can
1058 * be made coherent later.
1061 cpu_flush_dcache(void *ptr, size_t len)
1063 /* Not applicable */
1066 /* Get current clock frequency for the given cpu id. */
1068 cpu_est_clockrate(int cpu_id, uint64_t *rate)
1071 uint64_t tsc1, tsc2;
1073 if (pcpu_find(cpu_id) == NULL || rate == NULL)
1076 return (EOPNOTSUPP);
1078 /* If we're booting, trust the rate calibrated moments ago. */
1085 /* Schedule ourselves on the indicated cpu. */
1086 thread_lock(curthread);
1087 sched_bind(curthread, cpu_id);
1088 thread_unlock(curthread);
1091 /* Calibrate by measuring a short delay. */
1092 reg = intr_disable();
1099 thread_lock(curthread);
1100 sched_unbind(curthread);
1101 thread_unlock(curthread);
1105 * Calculate the difference in readings, convert to Mhz, and
1106 * subtract 0.5% of the total. Empirical testing has shown that
1107 * overhead in DELAY() works out to approximately this value.
1110 *rate = tsc2 * 1000 - tsc2 * 5;
1116 * Shutdown the CPU as much as possible
1126 cpu_idle_hlt(int busy)
1129 * we must absolutely guarentee that hlt is the next instruction
1130 * after sti or we introduce a timing window.
1133 if (sched_runnable())
1136 __asm __volatile("sti; hlt");
1140 cpu_idle_spin(int busy)
1145 void (*cpu_idle_fn)(int) = cpu_idle_hlt;
1151 if (mp_grab_cpu_hlt())
1158 * mwait cpu power states. Lower 4 bits are sub-states.
1160 #define MWAIT_C0 0xf0
1161 #define MWAIT_C1 0x00
1162 #define MWAIT_C2 0x10
1163 #define MWAIT_C3 0x20
1164 #define MWAIT_C4 0x30
1166 #define MWAIT_DISABLED 0x0
1167 #define MWAIT_WOKEN 0x1
1168 #define MWAIT_WAITING 0x2
1171 cpu_idle_mwait(int busy)
1175 mwait = (int *)PCPU_PTR(monitorbuf);
1176 *mwait = MWAIT_WAITING;
1177 if (sched_runnable())
1179 cpu_monitor(mwait, 0, 0);
1180 if (*mwait == MWAIT_WAITING)
1181 cpu_mwait(0, MWAIT_C1);
1185 cpu_idle_mwait_hlt(int busy)
1189 mwait = (int *)PCPU_PTR(monitorbuf);
1191 *mwait = MWAIT_DISABLED;
1195 *mwait = MWAIT_WAITING;
1196 if (sched_runnable())
1198 cpu_monitor(mwait, 0, 0);
1199 if (*mwait == MWAIT_WAITING)
1200 cpu_mwait(0, MWAIT_C1);
1204 cpu_idle_wakeup(int cpu)
1209 if (cpu_idle_fn == cpu_idle_spin)
1211 if (cpu_idle_fn != cpu_idle_mwait && cpu_idle_fn != cpu_idle_mwait_hlt)
1213 pcpu = pcpu_find(cpu);
1214 mwait = (int *)pcpu->pc_monitorbuf;
1216 * This doesn't need to be atomic since missing the race will
1217 * simply result in unnecessary IPIs.
1219 if (cpu_idle_fn == cpu_idle_mwait_hlt && *mwait == MWAIT_DISABLED)
1221 *mwait = MWAIT_WOKEN;
1227 * Ordered by speed/power consumption.
1233 { cpu_idle_spin, "spin" },
1234 { cpu_idle_mwait, "mwait" },
1235 { cpu_idle_mwait_hlt, "mwait_hlt" },
1236 { cpu_idle_hlt, "hlt" },
1241 idle_sysctl_available(SYSCTL_HANDLER_ARGS)
1247 avail = malloc(256, M_TEMP, M_WAITOK);
1249 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1250 if (strstr(idle_tbl[i].id_name, "mwait") &&
1251 (cpu_feature2 & CPUID2_MON) == 0)
1253 p += sprintf(p, "%s, ", idle_tbl[i].id_name);
1255 error = sysctl_handle_string(oidp, avail, 0, req);
1256 free(avail, M_TEMP);
1261 idle_sysctl(SYSCTL_HANDLER_ARGS)
1269 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1270 if (idle_tbl[i].id_fn == cpu_idle_fn) {
1271 p = idle_tbl[i].id_name;
1275 strncpy(buf, p, sizeof(buf));
1276 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1277 if (error != 0 || req->newptr == NULL)
1279 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1280 if (strstr(idle_tbl[i].id_name, "mwait") &&
1281 (cpu_feature2 & CPUID2_MON) == 0)
1283 if (strcmp(idle_tbl[i].id_name, buf))
1285 cpu_idle_fn = idle_tbl[i].id_fn;
1291 SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
1292 0, 0, idle_sysctl_available, "A", "list of available idle functions");
1294 SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
1295 idle_sysctl, "A", "currently selected idle function");
1298 * Reset registers to default values on exec.
1301 exec_setregs(td, entry, stack, ps_strings)
1307 struct trapframe *regs = td->td_frame;
1308 struct pcb *pcb = td->td_pcb;
1310 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1311 pcb->pcb_gs = _udatasel;
1314 mtx_lock_spin(&dt_lock);
1315 if (td->td_proc->p_md.md_ldt)
1318 mtx_unlock_spin(&dt_lock);
1320 bzero((char *)regs, sizeof(struct trapframe));
1321 regs->tf_eip = entry;
1322 regs->tf_esp = stack;
1323 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1324 regs->tf_ss = _udatasel;
1325 regs->tf_ds = _udatasel;
1326 regs->tf_es = _udatasel;
1327 regs->tf_fs = _udatasel;
1328 regs->tf_cs = _ucodesel;
1330 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1331 regs->tf_ebx = ps_strings;
1334 * Reset the hardware debug registers if they were in use.
1335 * They won't have any meaning for the newly exec'd process.
1337 if (pcb->pcb_flags & PCB_DBREGS) {
1344 if (pcb == PCPU_GET(curpcb)) {
1346 * Clear the debug registers on the running
1347 * CPU, otherwise they will end up affecting
1348 * the next process we switch to.
1352 pcb->pcb_flags &= ~PCB_DBREGS;
1356 * Initialize the math emulator (if any) for the current process.
1357 * Actually, just clear the bit that says that the emulator has
1358 * been initialized. Initialization is delayed until the process
1359 * traps to the emulator (if it is done at all) mainly because
1360 * emulators don't provide an entry point for initialization.
1362 td->td_pcb->pcb_flags &= ~FP_SOFTFP;
1363 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1366 * Drop the FP state if we hold it, so that the process gets a
1367 * clean FP state if it uses the FPU again.
1372 * XXX - Linux emulator
1373 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1376 td->td_retval[1] = 0;
1387 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1389 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1390 * instructions. We must set the CR0_MP bit and use the CR0_TS
1391 * bit to control the trap, because setting the CR0_EM bit does
1392 * not cause WAIT instructions to trap. It's important to trap
1393 * WAIT instructions - otherwise the "wait" variants of no-wait
1394 * control instructions would degenerate to the "no-wait" variants
1395 * after FP context switches but work correctly otherwise. It's
1396 * particularly important to trap WAITs when there is no NPX -
1397 * otherwise the "wait" variants would always degenerate.
1399 * Try setting CR0_NE to get correct error reporting on 486DX's.
1400 * Setting it should fail or do nothing on lesser processors.
1402 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1407 u_long bootdev; /* not a struct cdev *- encoding is different */
1408 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1409 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1412 * Initialize 386 and configure to run kernel
1416 * Initialize segments & interrupt table
1421 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1422 union descriptor ldt[NLDT]; /* local descriptor table */
1423 static struct gate_descriptor idt0[NIDT];
1424 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1425 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1426 struct mtx dt_lock; /* lock for GDT and LDT */
1428 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1429 extern int has_f00f_bug;
1432 static struct i386tss dblfault_tss;
1433 static char dblfault_stack[PAGE_SIZE];
1435 extern vm_offset_t proc0kstack;
1439 * software prototypes -- in more palatable form.
1441 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1442 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1444 struct soft_segment_descriptor gdt_segs[] = {
1445 /* GNULL_SEL 0 Null Descriptor */
1451 .ssd_xx = 0, .ssd_xx1 = 0,
1454 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1456 .ssd_limit = 0xfffff,
1457 .ssd_type = SDT_MEMRWA,
1460 .ssd_xx = 0, .ssd_xx1 = 0,
1463 /* GUFS_SEL 2 %fs Descriptor for user */
1465 .ssd_limit = 0xfffff,
1466 .ssd_type = SDT_MEMRWA,
1469 .ssd_xx = 0, .ssd_xx1 = 0,
1472 /* GUGS_SEL 3 %gs Descriptor for user */
1474 .ssd_limit = 0xfffff,
1475 .ssd_type = SDT_MEMRWA,
1478 .ssd_xx = 0, .ssd_xx1 = 0,
1481 /* GCODE_SEL 4 Code Descriptor for kernel */
1483 .ssd_limit = 0xfffff,
1484 .ssd_type = SDT_MEMERA,
1487 .ssd_xx = 0, .ssd_xx1 = 0,
1490 /* GDATA_SEL 5 Data Descriptor for kernel */
1492 .ssd_limit = 0xfffff,
1493 .ssd_type = SDT_MEMRWA,
1496 .ssd_xx = 0, .ssd_xx1 = 0,
1499 /* GUCODE_SEL 6 Code Descriptor for user */
1501 .ssd_limit = 0xfffff,
1502 .ssd_type = SDT_MEMERA,
1505 .ssd_xx = 0, .ssd_xx1 = 0,
1508 /* GUDATA_SEL 7 Data Descriptor for user */
1510 .ssd_limit = 0xfffff,
1511 .ssd_type = SDT_MEMRWA,
1514 .ssd_xx = 0, .ssd_xx1 = 0,
1517 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1518 { .ssd_base = 0x400,
1519 .ssd_limit = 0xfffff,
1520 .ssd_type = SDT_MEMRWA,
1523 .ssd_xx = 0, .ssd_xx1 = 0,
1526 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1529 .ssd_limit = sizeof(struct i386tss)-1,
1530 .ssd_type = SDT_SYS386TSS,
1533 .ssd_xx = 0, .ssd_xx1 = 0,
1536 /* GLDT_SEL 10 LDT Descriptor */
1537 { .ssd_base = (int) ldt,
1538 .ssd_limit = sizeof(ldt)-1,
1539 .ssd_type = SDT_SYSLDT,
1542 .ssd_xx = 0, .ssd_xx1 = 0,
1545 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1546 { .ssd_base = (int) ldt,
1547 .ssd_limit = (512 * sizeof(union descriptor)-1),
1548 .ssd_type = SDT_SYSLDT,
1551 .ssd_xx = 0, .ssd_xx1 = 0,
1554 /* GPANIC_SEL 12 Panic Tss Descriptor */
1555 { .ssd_base = (int) &dblfault_tss,
1556 .ssd_limit = sizeof(struct i386tss)-1,
1557 .ssd_type = SDT_SYS386TSS,
1560 .ssd_xx = 0, .ssd_xx1 = 0,
1563 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1565 .ssd_limit = 0xfffff,
1566 .ssd_type = SDT_MEMERA,
1569 .ssd_xx = 0, .ssd_xx1 = 0,
1572 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1574 .ssd_limit = 0xfffff,
1575 .ssd_type = SDT_MEMERA,
1578 .ssd_xx = 0, .ssd_xx1 = 0,
1581 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1583 .ssd_limit = 0xfffff,
1584 .ssd_type = SDT_MEMRWA,
1587 .ssd_xx = 0, .ssd_xx1 = 0,
1590 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1592 .ssd_limit = 0xfffff,
1593 .ssd_type = SDT_MEMRWA,
1596 .ssd_xx = 0, .ssd_xx1 = 0,
1599 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1601 .ssd_limit = 0xfffff,
1602 .ssd_type = SDT_MEMRWA,
1605 .ssd_xx = 0, .ssd_xx1 = 0,
1608 /* GNDIS_SEL 18 NDIS Descriptor */
1614 .ssd_xx = 0, .ssd_xx1 = 0,
1619 static struct soft_segment_descriptor ldt_segs[] = {
1620 /* Null Descriptor - overwritten by call gate */
1626 .ssd_xx = 0, .ssd_xx1 = 0,
1629 /* Null Descriptor - overwritten by call gate */
1635 .ssd_xx = 0, .ssd_xx1 = 0,
1638 /* Null Descriptor - overwritten by call gate */
1644 .ssd_xx = 0, .ssd_xx1 = 0,
1647 /* Code Descriptor for user */
1649 .ssd_limit = 0xfffff,
1650 .ssd_type = SDT_MEMERA,
1653 .ssd_xx = 0, .ssd_xx1 = 0,
1656 /* Null Descriptor - overwritten by call gate */
1662 .ssd_xx = 0, .ssd_xx1 = 0,
1665 /* Data Descriptor for user */
1667 .ssd_limit = 0xfffff,
1668 .ssd_type = SDT_MEMRWA,
1671 .ssd_xx = 0, .ssd_xx1 = 0,
1677 setidt(idx, func, typ, dpl, selec)
1684 struct gate_descriptor *ip;
1687 ip->gd_looffset = (int)func;
1688 ip->gd_selector = selec;
1694 ip->gd_hioffset = ((int)func)>>16 ;
1698 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1699 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1700 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1701 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1702 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1706 * Display the index and function name of any IDT entries that don't use
1707 * the default 'rsvd' entry point.
1709 DB_SHOW_COMMAND(idt, db_show_idt)
1711 struct gate_descriptor *ip;
1716 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
1717 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1718 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1719 db_printf("%3d\t", idx);
1720 db_printsym(func, DB_STGY_PROC);
1727 /* Show privileged registers. */
1728 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
1730 uint64_t idtr, gdtr;
1733 db_printf("idtr\t0x%08x/%04x\n",
1734 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
1736 db_printf("gdtr\t0x%08x/%04x\n",
1737 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
1738 db_printf("ldtr\t0x%04x\n", rldt());
1739 db_printf("tr\t0x%04x\n", rtr());
1740 db_printf("cr0\t0x%08x\n", rcr0());
1741 db_printf("cr2\t0x%08x\n", rcr2());
1742 db_printf("cr3\t0x%08x\n", rcr3());
1743 db_printf("cr4\t0x%08x\n", rcr4());
1749 struct segment_descriptor *sd;
1750 struct soft_segment_descriptor *ssd;
1752 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1753 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1754 ssd->ssd_type = sd->sd_type;
1755 ssd->ssd_dpl = sd->sd_dpl;
1756 ssd->ssd_p = sd->sd_p;
1757 ssd->ssd_def32 = sd->sd_def32;
1758 ssd->ssd_gran = sd->sd_gran;
1768 if (basemem > 640) {
1769 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1775 * XXX if biosbasemem is now < 640, there is a `hole'
1776 * between the end of base memory and the start of
1777 * ISA memory. The hole may be empty or it may
1778 * contain BIOS code or data. Map it read/write so
1779 * that the BIOS can write to it. (Memory from 0 to
1780 * the physical end of the kernel is mapped read-only
1781 * to begin with and then parts of it are remapped.
1782 * The parts that aren't remapped form holes that
1783 * remain read-only and are unused by the kernel.
1784 * The base memory area is below the physical end of
1785 * the kernel and right now forms a read-only hole.
1786 * The part of it from PAGE_SIZE to
1787 * (trunc_page(biosbasemem * 1024) - 1) will be
1788 * remapped and used by the kernel later.)
1790 * This code is similar to the code used in
1791 * pmap_mapdev, but since no memory needs to be
1792 * allocated we simply change the mapping.
1794 for (pa = trunc_page(basemem * 1024);
1795 pa < ISA_HOLE_START; pa += PAGE_SIZE)
1796 pmap_kenter(KERNBASE + pa, pa);
1799 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
1800 * the vm86 page table so that vm86 can scribble on them using
1801 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
1802 * page 0, at least as initialized here?
1804 pte = (pt_entry_t *)vm86paddr;
1805 for (i = basemem / 4; i < 160; i++)
1806 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1810 * Populate the (physmap) array with base/bound pairs describing the
1811 * available physical memory in the system, then test this memory and
1812 * build the phys_avail array describing the actually-available memory.
1814 * If we cannot accurately determine the physical memory map, then use
1815 * value from the 0xE801 call, and failing that, the RTC.
1817 * Total memory size may be set by the kernel environment variable
1818 * hw.physmem or the compile-time define MAXMEM.
1820 * XXX first should be vm_paddr_t.
1823 getmemsize(int first)
1825 int off, physmap_idx, pa_indx, da_indx;
1826 u_long physmem_tunable;
1827 vm_paddr_t physmap[PHYSMAP_SIZE];
1829 quad_t dcons_addr, dcons_size;
1836 bzero(physmap, sizeof(physmap));
1838 /* XXX - some of EPSON machines can't use PG_N */
1840 if (pc98_machine_type & M_EPSON_PC98) {
1841 switch (epson_machine_id) {
1845 case EPSON_PC486_HX:
1846 case EPSON_PC486_HG:
1847 case EPSON_PC486_HA:
1853 under16 = pc98_getmemsize(&basemem, &extmem);
1857 physmap[1] = basemem * 1024;
1859 physmap[physmap_idx] = 0x100000;
1860 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1863 * Now, physmap contains a map of physical memory.
1867 /* make hole for AP bootstrap code */
1868 physmap[1] = mp_bootaddress(physmap[1]);
1872 * Maxmem isn't the "maximum memory", it's one larger than the
1873 * highest page of the physical address space. It should be
1874 * called something like "Maxphyspage". We may adjust this
1875 * based on ``hw.physmem'' and the results of the memory test.
1877 Maxmem = atop(physmap[physmap_idx + 1]);
1880 Maxmem = MAXMEM / 4;
1883 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
1884 Maxmem = atop(physmem_tunable);
1886 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1887 (boothowto & RB_VERBOSE))
1888 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1891 * If Maxmem has been increased beyond what the system has detected,
1892 * extend the last memory segment to the new limit.
1894 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1895 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1898 * We need to divide chunk if Maxmem is larger than 16MB and
1899 * under 16MB area is not full of memory.
1900 * (1) system area (15-16MB region) is cut off
1901 * (2) extended memory is only over 16MB area (ex. Melco "HYPERMEMORY")
1903 if ((under16 != 16 * 1024) && (extmem > 15 * 1024)) {
1904 /* 15M - 16M region is cut off, so need to divide chunk */
1905 physmap[physmap_idx + 1] = under16 * 1024;
1907 physmap[physmap_idx] = 0x1000000;
1908 physmap[physmap_idx + 1] = physmap[2] + extmem * 1024;
1911 /* call pmap initialization to make new kernel address space */
1912 pmap_bootstrap(first);
1915 * Size up each available chunk of physical memory.
1917 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1920 phys_avail[pa_indx++] = physmap[0];
1921 phys_avail[pa_indx] = physmap[0];
1922 dump_avail[da_indx] = physmap[0];
1926 * Get dcons buffer address
1928 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
1929 getenv_quad("dcons.size", &dcons_size) == 0)
1933 * physmap is in bytes, so when converting to page boundaries,
1934 * round up the start address and round down the end address.
1936 for (i = 0; i <= physmap_idx; i += 2) {
1939 end = ptoa((vm_paddr_t)Maxmem);
1940 if (physmap[i + 1] < end)
1941 end = trunc_page(physmap[i + 1]);
1942 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1943 int tmp, page_bad, full;
1944 int *ptr = (int *)CADDR1;
1948 * block out kernel memory as not available.
1950 if (pa >= KERNLOAD && pa < first)
1954 * block out dcons buffer
1957 && pa >= trunc_page(dcons_addr)
1958 && pa < dcons_addr + dcons_size)
1964 * map page into kernel: valid, read/write,non-cacheable
1966 *pte = pa | PG_V | PG_RW | pg_n;
1971 * Test for alternating 1's and 0's
1973 *(volatile int *)ptr = 0xaaaaaaaa;
1974 if (*(volatile int *)ptr != 0xaaaaaaaa)
1977 * Test for alternating 0's and 1's
1979 *(volatile int *)ptr = 0x55555555;
1980 if (*(volatile int *)ptr != 0x55555555)
1985 *(volatile int *)ptr = 0xffffffff;
1986 if (*(volatile int *)ptr != 0xffffffff)
1991 *(volatile int *)ptr = 0x0;
1992 if (*(volatile int *)ptr != 0x0)
1995 * Restore original value.
2000 * Adjust array of valid/good pages.
2002 if (page_bad == TRUE)
2005 * If this good page is a continuation of the
2006 * previous set of good pages, then just increase
2007 * the end pointer. Otherwise start a new chunk.
2008 * Note that "end" points one higher than end,
2009 * making the range >= start and < end.
2010 * If we're also doing a speculative memory
2011 * test and we at or past the end, bump up Maxmem
2012 * so that we keep going. The first bad page
2013 * will terminate the loop.
2015 if (phys_avail[pa_indx] == pa) {
2016 phys_avail[pa_indx] += PAGE_SIZE;
2019 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
2021 "Too many holes in the physical address space, giving up\n");
2026 phys_avail[pa_indx++] = pa; /* start */
2027 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
2031 if (dump_avail[da_indx] == pa) {
2032 dump_avail[da_indx] += PAGE_SIZE;
2035 if (da_indx == DUMP_AVAIL_ARRAY_END) {
2039 dump_avail[da_indx++] = pa; /* start */
2040 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
2052 * The last chunk must contain at least one page plus the message
2053 * buffer to avoid complicating other code (message buffer address
2054 * calculation, etc.).
2056 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
2057 round_page(msgbufsize) >= phys_avail[pa_indx]) {
2058 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
2059 phys_avail[pa_indx--] = 0;
2060 phys_avail[pa_indx--] = 0;
2063 Maxmem = atop(phys_avail[pa_indx]);
2065 /* Trim off space for the message buffer. */
2066 phys_avail[pa_indx] -= round_page(msgbufsize);
2068 /* Map the message buffer. */
2069 for (off = 0; off < round_page(msgbufsize); off += PAGE_SIZE)
2070 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
2078 struct gate_descriptor *gdp;
2079 int gsel_tss, metadata_missing, x, pa;
2082 thread0.td_kstack = proc0kstack;
2083 thread0.td_pcb = (struct pcb *)
2084 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
2087 * This may be done better later if it gets more high level
2088 * components in it. If so just link td->td_proc here.
2090 proc_linkup0(&proc0, &thread0);
2097 metadata_missing = 0;
2098 if (bootinfo.bi_modulep) {
2099 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
2100 preload_bootstrap_relocate(KERNBASE);
2102 metadata_missing = 1;
2105 kern_envp = static_env;
2106 else if (bootinfo.bi_envp)
2107 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
2109 /* Init basic tunables, hz etc */
2113 * Make gdt memory segments. All segments cover the full 4GB
2114 * of address space and permissions are enforced at page level.
2116 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
2117 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
2118 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
2119 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
2120 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
2121 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
2124 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
2125 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2126 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2128 for (x = 0; x < NGDT; x++)
2129 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2131 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2132 r_gdt.rd_base = (int) gdt;
2133 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2136 pcpu_init(pc, 0, sizeof(struct pcpu));
2137 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2138 pmap_kenter(pa + KERNBASE, pa);
2139 dpcpu_init((void *)(first + KERNBASE), 0);
2140 first += DPCPU_SIZE;
2141 PCPU_SET(prvspace, pc);
2142 PCPU_SET(curthread, &thread0);
2143 PCPU_SET(curpcb, thread0.td_pcb);
2146 * Initialize mutexes.
2148 * icu_lock: in order to allow an interrupt to occur in a critical
2149 * section, to set pcpu->ipending (etc...) properly, we
2150 * must be able to get the icu lock, so it can't be
2154 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2156 /* make ldt memory segments */
2157 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2158 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2159 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2160 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2162 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2164 PCPU_SET(currentldt, _default_ldt);
2167 for (x = 0; x < NIDT; x++)
2168 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2169 GSEL(GCODE_SEL, SEL_KPL));
2170 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2171 GSEL(GCODE_SEL, SEL_KPL));
2172 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2173 GSEL(GCODE_SEL, SEL_KPL));
2174 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
2175 GSEL(GCODE_SEL, SEL_KPL));
2176 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2177 GSEL(GCODE_SEL, SEL_KPL));
2178 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2179 GSEL(GCODE_SEL, SEL_KPL));
2180 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2181 GSEL(GCODE_SEL, SEL_KPL));
2182 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2183 GSEL(GCODE_SEL, SEL_KPL));
2184 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2185 , GSEL(GCODE_SEL, SEL_KPL));
2186 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2187 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2188 GSEL(GCODE_SEL, SEL_KPL));
2189 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2190 GSEL(GCODE_SEL, SEL_KPL));
2191 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2192 GSEL(GCODE_SEL, SEL_KPL));
2193 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2194 GSEL(GCODE_SEL, SEL_KPL));
2195 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2196 GSEL(GCODE_SEL, SEL_KPL));
2197 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2198 GSEL(GCODE_SEL, SEL_KPL));
2199 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2200 GSEL(GCODE_SEL, SEL_KPL));
2201 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2202 GSEL(GCODE_SEL, SEL_KPL));
2203 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2204 GSEL(GCODE_SEL, SEL_KPL));
2205 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2206 GSEL(GCODE_SEL, SEL_KPL));
2207 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2208 GSEL(GCODE_SEL, SEL_KPL));
2210 r_idt.rd_limit = sizeof(idt0) - 1;
2211 r_idt.rd_base = (int) idt;
2215 * Initialize the i8254 before the console so that console
2216 * initialization can use DELAY().
2221 * Initialize the console before we print anything out.
2225 if (metadata_missing)
2226 printf("WARNING: loader(8) metadata is missing!\n");
2233 ksym_start = bootinfo.bi_symtab;
2234 ksym_end = bootinfo.bi_esymtab;
2240 if (boothowto & RB_KDB)
2241 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
2244 finishidentcpu(); /* Final stage of CPU initialization */
2245 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2246 GSEL(GCODE_SEL, SEL_KPL));
2247 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2248 GSEL(GCODE_SEL, SEL_KPL));
2249 initializecpu(); /* Initialize CPU registers */
2251 /* make an initial tss so cpu can get interrupt stack on syscall! */
2252 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2253 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2254 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
2255 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2256 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2257 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2258 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2259 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2262 /* pointer to selector slot for %fs/%gs */
2263 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2265 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2266 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2267 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2268 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2269 dblfault_tss.tss_cr3 = (int)IdlePTD;
2270 dblfault_tss.tss_eip = (int)dblfault_handler;
2271 dblfault_tss.tss_eflags = PSL_KERNEL;
2272 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2273 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2274 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2275 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2276 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2280 init_param2(physmem);
2282 /* now running on new page tables, configured,and u/iom is accessible */
2284 msgbufinit(msgbufp, msgbufsize);
2286 /* make a call gate to reenter kernel with */
2287 gdp = &ldt[LSYS5CALLS_SEL].gd;
2289 x = (int) &IDTVEC(lcall_syscall);
2290 gdp->gd_looffset = x;
2291 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2293 gdp->gd_type = SDT_SYS386CGT;
2294 gdp->gd_dpl = SEL_UPL;
2296 gdp->gd_hioffset = x >> 16;
2298 /* XXX does this work? */
2300 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2301 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
2303 /* transfer to user mode */
2305 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2306 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2308 /* setup proc 0's pcb */
2309 thread0.td_pcb->pcb_flags = 0;
2310 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2311 thread0.td_pcb->pcb_ext = 0;
2312 thread0.td_frame = &proc0_tf;
2316 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2322 spinlock_enter(void)
2328 if (td->td_md.md_spinlock_count == 0) {
2329 flags = intr_disable();
2330 td->td_md.md_spinlock_count = 1;
2331 td->td_md.md_saved_flags = flags;
2333 td->td_md.md_spinlock_count++;
2345 flags = td->td_md.md_saved_flags;
2346 td->td_md.md_spinlock_count--;
2347 if (td->td_md.md_spinlock_count == 0)
2348 intr_restore(flags);
2351 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2352 static void f00f_hack(void *unused);
2353 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
2356 f00f_hack(void *unused)
2358 struct gate_descriptor *new_idt;
2366 printf("Intel Pentium detected, installing workaround for F00F bug\n");
2368 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
2370 panic("kmem_alloc returned 0");
2372 /* Put the problematic entry (#6) at the end of the lower page. */
2373 new_idt = (struct gate_descriptor*)
2374 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
2375 bcopy(idt, new_idt, sizeof(idt0));
2376 r_idt.rd_base = (u_int)new_idt;
2379 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2380 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2381 panic("vm_map_protect failed");
2383 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2386 * Construct a PCB from a trapframe. This is called from kdb_trap() where
2387 * we want to start a backtrace from the function that caused us to enter
2388 * the debugger. We have the context in the trapframe, but base the trace
2389 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
2390 * enough for a backtrace.
2393 makectx(struct trapframe *tf, struct pcb *pcb)
2396 pcb->pcb_edi = tf->tf_edi;
2397 pcb->pcb_esi = tf->tf_esi;
2398 pcb->pcb_ebp = tf->tf_ebp;
2399 pcb->pcb_ebx = tf->tf_ebx;
2400 pcb->pcb_eip = tf->tf_eip;
2401 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
2405 ptrace_set_pc(struct thread *td, u_long addr)
2408 td->td_frame->tf_eip = addr;
2413 ptrace_single_step(struct thread *td)
2415 td->td_frame->tf_eflags |= PSL_T;
2420 ptrace_clear_single_step(struct thread *td)
2422 td->td_frame->tf_eflags &= ~PSL_T;
2427 fill_regs(struct thread *td, struct reg *regs)
2430 struct trapframe *tp;
2434 regs->r_gs = pcb->pcb_gs;
2435 return (fill_frame_regs(tp, regs));
2439 fill_frame_regs(struct trapframe *tp, struct reg *regs)
2441 regs->r_fs = tp->tf_fs;
2442 regs->r_es = tp->tf_es;
2443 regs->r_ds = tp->tf_ds;
2444 regs->r_edi = tp->tf_edi;
2445 regs->r_esi = tp->tf_esi;
2446 regs->r_ebp = tp->tf_ebp;
2447 regs->r_ebx = tp->tf_ebx;
2448 regs->r_edx = tp->tf_edx;
2449 regs->r_ecx = tp->tf_ecx;
2450 regs->r_eax = tp->tf_eax;
2451 regs->r_eip = tp->tf_eip;
2452 regs->r_cs = tp->tf_cs;
2453 regs->r_eflags = tp->tf_eflags;
2454 regs->r_esp = tp->tf_esp;
2455 regs->r_ss = tp->tf_ss;
2460 set_regs(struct thread *td, struct reg *regs)
2463 struct trapframe *tp;
2466 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2467 !CS_SECURE(regs->r_cs))
2470 tp->tf_fs = regs->r_fs;
2471 tp->tf_es = regs->r_es;
2472 tp->tf_ds = regs->r_ds;
2473 tp->tf_edi = regs->r_edi;
2474 tp->tf_esi = regs->r_esi;
2475 tp->tf_ebp = regs->r_ebp;
2476 tp->tf_ebx = regs->r_ebx;
2477 tp->tf_edx = regs->r_edx;
2478 tp->tf_ecx = regs->r_ecx;
2479 tp->tf_eax = regs->r_eax;
2480 tp->tf_eip = regs->r_eip;
2481 tp->tf_cs = regs->r_cs;
2482 tp->tf_eflags = regs->r_eflags;
2483 tp->tf_esp = regs->r_esp;
2484 tp->tf_ss = regs->r_ss;
2485 pcb->pcb_gs = regs->r_gs;
2489 #ifdef CPU_ENABLE_SSE
2491 fill_fpregs_xmm(sv_xmm, sv_87)
2492 struct savexmm *sv_xmm;
2493 struct save87 *sv_87;
2495 register struct env87 *penv_87 = &sv_87->sv_env;
2496 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2499 bzero(sv_87, sizeof(*sv_87));
2501 /* FPU control/status */
2502 penv_87->en_cw = penv_xmm->en_cw;
2503 penv_87->en_sw = penv_xmm->en_sw;
2504 penv_87->en_tw = penv_xmm->en_tw;
2505 penv_87->en_fip = penv_xmm->en_fip;
2506 penv_87->en_fcs = penv_xmm->en_fcs;
2507 penv_87->en_opcode = penv_xmm->en_opcode;
2508 penv_87->en_foo = penv_xmm->en_foo;
2509 penv_87->en_fos = penv_xmm->en_fos;
2512 for (i = 0; i < 8; ++i)
2513 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
2517 set_fpregs_xmm(sv_87, sv_xmm)
2518 struct save87 *sv_87;
2519 struct savexmm *sv_xmm;
2521 register struct env87 *penv_87 = &sv_87->sv_env;
2522 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2525 /* FPU control/status */
2526 penv_xmm->en_cw = penv_87->en_cw;
2527 penv_xmm->en_sw = penv_87->en_sw;
2528 penv_xmm->en_tw = penv_87->en_tw;
2529 penv_xmm->en_fip = penv_87->en_fip;
2530 penv_xmm->en_fcs = penv_87->en_fcs;
2531 penv_xmm->en_opcode = penv_87->en_opcode;
2532 penv_xmm->en_foo = penv_87->en_foo;
2533 penv_xmm->en_fos = penv_87->en_fos;
2536 for (i = 0; i < 8; ++i)
2537 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
2539 #endif /* CPU_ENABLE_SSE */
2542 fill_fpregs(struct thread *td, struct fpreg *fpregs)
2545 KASSERT(td == curthread || TD_IS_SUSPENDED(td),
2546 ("not suspended thread %p", td));
2550 bzero(fpregs, sizeof(*fpregs));
2552 #ifdef CPU_ENABLE_SSE
2554 fill_fpregs_xmm(&td->td_pcb->pcb_user_save.sv_xmm,
2555 (struct save87 *)fpregs);
2557 #endif /* CPU_ENABLE_SSE */
2558 bcopy(&td->td_pcb->pcb_user_save.sv_87, fpregs,
2564 set_fpregs(struct thread *td, struct fpreg *fpregs)
2567 #ifdef CPU_ENABLE_SSE
2569 set_fpregs_xmm((struct save87 *)fpregs,
2570 &td->td_pcb->pcb_user_save.sv_xmm);
2572 #endif /* CPU_ENABLE_SSE */
2573 bcopy(fpregs, &td->td_pcb->pcb_user_save.sv_87,
2582 * Get machine context.
2585 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
2587 struct trapframe *tp;
2588 struct segment_descriptor *sdp;
2592 PROC_LOCK(curthread->td_proc);
2593 mcp->mc_onstack = sigonstack(tp->tf_esp);
2594 PROC_UNLOCK(curthread->td_proc);
2595 mcp->mc_gs = td->td_pcb->pcb_gs;
2596 mcp->mc_fs = tp->tf_fs;
2597 mcp->mc_es = tp->tf_es;
2598 mcp->mc_ds = tp->tf_ds;
2599 mcp->mc_edi = tp->tf_edi;
2600 mcp->mc_esi = tp->tf_esi;
2601 mcp->mc_ebp = tp->tf_ebp;
2602 mcp->mc_isp = tp->tf_isp;
2603 mcp->mc_eflags = tp->tf_eflags;
2604 if (flags & GET_MC_CLEAR_RET) {
2607 mcp->mc_eflags &= ~PSL_C;
2609 mcp->mc_eax = tp->tf_eax;
2610 mcp->mc_edx = tp->tf_edx;
2612 mcp->mc_ebx = tp->tf_ebx;
2613 mcp->mc_ecx = tp->tf_ecx;
2614 mcp->mc_eip = tp->tf_eip;
2615 mcp->mc_cs = tp->tf_cs;
2616 mcp->mc_esp = tp->tf_esp;
2617 mcp->mc_ss = tp->tf_ss;
2618 mcp->mc_len = sizeof(*mcp);
2619 get_fpcontext(td, mcp);
2620 sdp = &td->td_pcb->pcb_fsd;
2621 mcp->mc_fsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2622 sdp = &td->td_pcb->pcb_gsd;
2623 mcp->mc_gsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2624 bzero(mcp->mc_spare1, sizeof(mcp->mc_spare1));
2625 bzero(mcp->mc_spare2, sizeof(mcp->mc_spare2));
2630 * Set machine context.
2632 * However, we don't set any but the user modifiable flags, and we won't
2633 * touch the cs selector.
2636 set_mcontext(struct thread *td, const mcontext_t *mcp)
2638 struct trapframe *tp;
2642 if (mcp->mc_len != sizeof(*mcp))
2644 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
2645 (tp->tf_eflags & ~PSL_USERCHANGE);
2646 if ((ret = set_fpcontext(td, mcp)) == 0) {
2647 tp->tf_fs = mcp->mc_fs;
2648 tp->tf_es = mcp->mc_es;
2649 tp->tf_ds = mcp->mc_ds;
2650 tp->tf_edi = mcp->mc_edi;
2651 tp->tf_esi = mcp->mc_esi;
2652 tp->tf_ebp = mcp->mc_ebp;
2653 tp->tf_ebx = mcp->mc_ebx;
2654 tp->tf_edx = mcp->mc_edx;
2655 tp->tf_ecx = mcp->mc_ecx;
2656 tp->tf_eax = mcp->mc_eax;
2657 tp->tf_eip = mcp->mc_eip;
2658 tp->tf_eflags = eflags;
2659 tp->tf_esp = mcp->mc_esp;
2660 tp->tf_ss = mcp->mc_ss;
2661 td->td_pcb->pcb_gs = mcp->mc_gs;
2668 get_fpcontext(struct thread *td, mcontext_t *mcp)
2672 mcp->mc_fpformat = _MC_FPFMT_NODEV;
2673 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
2674 bzero(mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
2676 mcp->mc_ownedfp = npxgetregs(td);
2677 bcopy(&td->td_pcb->pcb_user_save, &mcp->mc_fpstate,
2678 sizeof(mcp->mc_fpstate));
2679 mcp->mc_fpformat = npxformat();
2684 set_fpcontext(struct thread *td, const mcontext_t *mcp)
2687 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2689 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2690 mcp->mc_fpformat != _MC_FPFMT_XMM)
2692 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
2693 /* We don't care what state is left in the FPU or PCB. */
2695 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2696 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2698 #ifdef CPU_ENABLE_SSE
2700 ((union savefpu *)&mcp->mc_fpstate)->sv_xmm.sv_env.
2701 en_mxcsr &= cpu_mxcsr_mask;
2703 npxsetregs(td, (union savefpu *)&mcp->mc_fpstate);
2711 fpstate_drop(struct thread *td)
2716 if (PCPU_GET(fpcurthread) == td)
2720 * XXX force a full drop of the npx. The above only drops it if we
2721 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
2723 * XXX I don't much like npxgetregs()'s semantics of doing a full
2724 * drop. Dropping only to the pcb matches fnsave's behaviour.
2725 * We only need to drop to !PCB_INITDONE in sendsig(). But
2726 * sendsig() is the only caller of npxgetregs()... perhaps we just
2727 * have too many layers.
2729 curthread->td_pcb->pcb_flags &= ~(PCB_NPXINITDONE |
2730 PCB_NPXUSERINITDONE);
2735 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2740 dbregs->dr[0] = rdr0();
2741 dbregs->dr[1] = rdr1();
2742 dbregs->dr[2] = rdr2();
2743 dbregs->dr[3] = rdr3();
2744 dbregs->dr[4] = rdr4();
2745 dbregs->dr[5] = rdr5();
2746 dbregs->dr[6] = rdr6();
2747 dbregs->dr[7] = rdr7();
2750 dbregs->dr[0] = pcb->pcb_dr0;
2751 dbregs->dr[1] = pcb->pcb_dr1;
2752 dbregs->dr[2] = pcb->pcb_dr2;
2753 dbregs->dr[3] = pcb->pcb_dr3;
2756 dbregs->dr[6] = pcb->pcb_dr6;
2757 dbregs->dr[7] = pcb->pcb_dr7;
2763 set_dbregs(struct thread *td, struct dbreg *dbregs)
2769 load_dr0(dbregs->dr[0]);
2770 load_dr1(dbregs->dr[1]);
2771 load_dr2(dbregs->dr[2]);
2772 load_dr3(dbregs->dr[3]);
2773 load_dr4(dbregs->dr[4]);
2774 load_dr5(dbregs->dr[5]);
2775 load_dr6(dbregs->dr[6]);
2776 load_dr7(dbregs->dr[7]);
2779 * Don't let an illegal value for dr7 get set. Specifically,
2780 * check for undefined settings. Setting these bit patterns
2781 * result in undefined behaviour and can lead to an unexpected
2784 for (i = 0; i < 4; i++) {
2785 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
2787 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
2794 * Don't let a process set a breakpoint that is not within the
2795 * process's address space. If a process could do this, it
2796 * could halt the system by setting a breakpoint in the kernel
2797 * (if ddb was enabled). Thus, we need to check to make sure
2798 * that no breakpoints are being enabled for addresses outside
2799 * process's address space.
2801 * XXX - what about when the watched area of the user's
2802 * address space is written into from within the kernel
2803 * ... wouldn't that still cause a breakpoint to be generated
2804 * from within kernel mode?
2807 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
2808 /* dr0 is enabled */
2809 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2813 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
2814 /* dr1 is enabled */
2815 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2819 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
2820 /* dr2 is enabled */
2821 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2825 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
2826 /* dr3 is enabled */
2827 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2831 pcb->pcb_dr0 = dbregs->dr[0];
2832 pcb->pcb_dr1 = dbregs->dr[1];
2833 pcb->pcb_dr2 = dbregs->dr[2];
2834 pcb->pcb_dr3 = dbregs->dr[3];
2835 pcb->pcb_dr6 = dbregs->dr[6];
2836 pcb->pcb_dr7 = dbregs->dr[7];
2838 pcb->pcb_flags |= PCB_DBREGS;
2845 * Return > 0 if a hardware breakpoint has been hit, and the
2846 * breakpoint was in user space. Return 0, otherwise.
2849 user_dbreg_trap(void)
2851 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2852 u_int32_t bp; /* breakpoint bits extracted from dr6 */
2853 int nbp; /* number of breakpoints that triggered */
2854 caddr_t addr[4]; /* breakpoint addresses */
2858 if ((dr7 & 0x000000ff) == 0) {
2860 * all GE and LE bits in the dr7 register are zero,
2861 * thus the trap couldn't have been caused by the
2862 * hardware debug registers
2869 bp = dr6 & 0x0000000f;
2873 * None of the breakpoint bits are set meaning this
2874 * trap was not caused by any of the debug registers
2880 * at least one of the breakpoints were hit, check to see
2881 * which ones and if any of them are user space addresses
2885 addr[nbp++] = (caddr_t)rdr0();
2888 addr[nbp++] = (caddr_t)rdr1();
2891 addr[nbp++] = (caddr_t)rdr2();
2894 addr[nbp++] = (caddr_t)rdr3();
2897 for (i = 0; i < nbp; i++) {
2898 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
2900 * addr[i] is in user space
2907 * None of the breakpoints are in user space.
2915 * Provide inb() and outb() as functions. They are normally only available as
2916 * inline functions, thus cannot be called from the debugger.
2919 /* silence compiler warnings */
2920 u_char inb_(u_short);
2921 void outb_(u_short, u_char);
2930 outb_(u_short port, u_char data)