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"
52 #include "opt_msgbuf.h"
54 #include "opt_perfmon.h"
56 #include <sys/param.h>
58 #include <sys/systm.h>
62 #include <sys/callout.h>
65 #include <sys/eventhandler.h>
67 #include <sys/imgact.h>
69 #include <sys/kernel.h>
71 #include <sys/linker.h>
73 #include <sys/malloc.h>
74 #include <sys/memrange.h>
75 #include <sys/msgbuf.h>
76 #include <sys/mutex.h>
78 #include <sys/ptrace.h>
79 #include <sys/reboot.h>
80 #include <sys/sched.h>
81 #include <sys/signalvar.h>
82 #include <sys/sysctl.h>
83 #include <sys/sysent.h>
84 #include <sys/sysproto.h>
85 #include <sys/ucontext.h>
86 #include <sys/vmmeter.h>
89 #include <vm/vm_extern.h>
90 #include <vm/vm_kern.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_map.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_pager.h>
95 #include <vm/vm_param.h>
99 #error KDB must be enabled in order for DDB to work!
102 #include <ddb/db_sym.h>
105 #include <pc98/pc98/pc98_machdep.h>
107 #include <net/netisr.h>
109 #include <machine/bootinfo.h>
110 #include <machine/clock.h>
111 #include <machine/cpu.h>
112 #include <machine/cputypes.h>
113 #include <machine/intr_machdep.h>
114 #include <machine/mca.h>
115 #include <machine/md_var.h>
116 #include <machine/pc/bios.h>
117 #include <machine/pcb.h>
118 #include <machine/pcb_ext.h>
119 #include <machine/proc.h>
120 #include <machine/reg.h>
121 #include <machine/sigframe.h>
122 #include <machine/specialreg.h>
123 #include <machine/vm86.h>
125 #include <machine/perfmon.h>
128 #include <machine/smp.h>
132 #include <i386/isa/icu.h>
135 /* Sanity check for __curthread() */
136 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
138 extern void init386(int first);
139 extern void dblfault_handler(void);
141 extern void printcpuinfo(void); /* XXX header file */
142 extern void finishidentcpu(void);
143 extern void panicifcpuunsupported(void);
144 extern void initializecpu(void);
146 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
147 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
149 #if !defined(CPU_DISABLE_SSE) && defined(I686_CPU)
150 #define CPU_ENABLE_SSE
153 static void cpu_startup(void *);
154 static void fpstate_drop(struct thread *td);
155 static void get_fpcontext(struct thread *td, mcontext_t *mcp);
156 static int set_fpcontext(struct thread *td, const mcontext_t *mcp);
157 #ifdef CPU_ENABLE_SSE
158 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
159 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
160 #endif /* CPU_ENABLE_SSE */
161 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
163 int need_pre_dma_flush; /* If 1, use wbinvd befor DMA transfer. */
164 int need_post_dma_flush; /* If 1, use invd after DMA transfer. */
167 extern vm_offset_t ksym_start, ksym_end;
170 int _udatasel, _ucodesel;
173 static int ispc98 = 1;
174 SYSCTL_INT(_machdep, OID_AUTO, ispc98, CTLFLAG_RD, &ispc98, 0, "");
179 static void osendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
181 #ifdef COMPAT_FREEBSD4
182 static void freebsd4_sendsig(sig_t catcher, ksiginfo_t *, sigset_t *mask);
189 * The number of PHYSMAP entries must be one less than the number of
190 * PHYSSEG entries because the PHYSMAP entry that spans the largest
191 * physical address that is accessible by ISA DMA is split into two
194 #define PHYSMAP_SIZE (2 * (VM_PHYSSEG_MAX - 1))
196 vm_paddr_t phys_avail[PHYSMAP_SIZE + 2];
197 vm_paddr_t dump_avail[PHYSMAP_SIZE + 2];
199 /* must be 2 less so 0 0 can signal end of chunks */
200 #define PHYS_AVAIL_ARRAY_END ((sizeof(phys_avail) / sizeof(phys_avail[0])) - 2)
201 #define DUMP_AVAIL_ARRAY_END ((sizeof(dump_avail) / sizeof(dump_avail[0])) - 2)
203 struct kva_md_info kmi;
205 static struct trapframe proc0_tf;
206 struct pcpu __pcpu[MAXCPU];
210 struct mem_range_softc mem_range_softc;
219 * Good {morning,afternoon,evening,night}.
223 panicifcpuunsupported();
230 * Display physical memory.
232 memsize = ptoa((uintmax_t)Maxmem);
233 printf("real memory = %ju (%ju MB)\n", memsize, memsize >> 20);
236 * Display any holes after the first chunk of extended memory.
241 printf("Physical memory chunk(s):\n");
242 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
245 size = phys_avail[indx + 1] - phys_avail[indx];
247 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
248 (uintmax_t)phys_avail[indx],
249 (uintmax_t)phys_avail[indx + 1] - 1,
250 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
254 vm_ksubmap_init(&kmi);
256 printf("avail memory = %ju (%ju MB)\n",
257 ptoa((uintmax_t)cnt.v_free_count),
258 ptoa((uintmax_t)cnt.v_free_count) / 1048576);
261 * Set up buffers, so they can be used to read disk labels.
264 vm_pager_bufferinit();
269 * Send an interrupt to process.
271 * Stack is set up to allow sigcode stored
272 * at top to call routine, followed by kcall
273 * to sigreturn routine below. After sigreturn
274 * resets the signal mask, the stack, and the
275 * frame pointer, it returns to the user
280 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
282 struct osigframe sf, *fp;
286 struct trapframe *regs;
292 PROC_LOCK_ASSERT(p, MA_OWNED);
293 sig = ksi->ksi_signo;
295 mtx_assert(&psp->ps_mtx, MA_OWNED);
297 oonstack = sigonstack(regs->tf_esp);
299 /* Allocate space for the signal handler context. */
300 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
301 SIGISMEMBER(psp->ps_sigonstack, sig)) {
302 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
303 td->td_sigstk.ss_size - sizeof(struct osigframe));
304 #if defined(COMPAT_43)
305 td->td_sigstk.ss_flags |= SS_ONSTACK;
308 fp = (struct osigframe *)regs->tf_esp - 1;
310 /* Translate the signal if appropriate. */
311 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
312 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
314 /* Build the argument list for the signal handler. */
316 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
317 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
318 /* Signal handler installed with SA_SIGINFO. */
319 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
320 sf.sf_siginfo.si_signo = sig;
321 sf.sf_siginfo.si_code = ksi->ksi_code;
322 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
324 /* Old FreeBSD-style arguments. */
325 sf.sf_arg2 = ksi->ksi_code;
326 sf.sf_addr = (register_t)ksi->ksi_addr;
327 sf.sf_ahu.sf_handler = catcher;
329 mtx_unlock(&psp->ps_mtx);
332 /* Save most if not all of trap frame. */
333 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
334 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
335 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
336 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
337 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
338 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
339 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
340 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
341 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
342 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
343 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
344 sf.sf_siginfo.si_sc.sc_gs = rgs();
345 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
347 /* Build the signal context to be used by osigreturn(). */
348 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
349 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
350 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
351 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
352 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
353 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
354 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
355 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
358 * If we're a vm86 process, we want to save the segment registers.
359 * We also change eflags to be our emulated eflags, not the actual
362 if (regs->tf_eflags & PSL_VM) {
363 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
364 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
365 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
367 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
368 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
369 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
370 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
372 if (vm86->vm86_has_vme == 0)
373 sf.sf_siginfo.si_sc.sc_ps =
374 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
375 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
377 /* See sendsig() for comments. */
378 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
382 * Copy the sigframe out to the user's stack.
384 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
386 printf("process %ld has trashed its stack\n", (long)p->p_pid);
392 regs->tf_esp = (int)fp;
393 regs->tf_eip = PS_STRINGS - szosigcode;
394 regs->tf_eflags &= ~(PSL_T | PSL_D);
395 regs->tf_cs = _ucodesel;
396 regs->tf_ds = _udatasel;
397 regs->tf_es = _udatasel;
398 regs->tf_fs = _udatasel;
400 regs->tf_ss = _udatasel;
402 mtx_lock(&psp->ps_mtx);
404 #endif /* COMPAT_43 */
406 #ifdef COMPAT_FREEBSD4
408 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
410 struct sigframe4 sf, *sfp;
414 struct trapframe *regs;
420 PROC_LOCK_ASSERT(p, MA_OWNED);
421 sig = ksi->ksi_signo;
423 mtx_assert(&psp->ps_mtx, MA_OWNED);
425 oonstack = sigonstack(regs->tf_esp);
427 /* Save user context. */
428 bzero(&sf, sizeof(sf));
429 sf.sf_uc.uc_sigmask = *mask;
430 sf.sf_uc.uc_stack = td->td_sigstk;
431 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
432 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
433 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
434 sf.sf_uc.uc_mcontext.mc_gs = rgs();
435 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
437 /* Allocate space for the signal handler context. */
438 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
439 SIGISMEMBER(psp->ps_sigonstack, sig)) {
440 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
441 td->td_sigstk.ss_size - sizeof(struct sigframe4));
442 #if defined(COMPAT_43)
443 td->td_sigstk.ss_flags |= SS_ONSTACK;
446 sfp = (struct sigframe4 *)regs->tf_esp - 1;
448 /* Translate the signal if appropriate. */
449 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
450 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
452 /* Build the argument list for the signal handler. */
454 sf.sf_ucontext = (register_t)&sfp->sf_uc;
455 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
456 /* Signal handler installed with SA_SIGINFO. */
457 sf.sf_siginfo = (register_t)&sfp->sf_si;
458 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
460 /* Fill in POSIX parts */
461 sf.sf_si.si_signo = sig;
462 sf.sf_si.si_code = ksi->ksi_code;
463 sf.sf_si.si_addr = ksi->ksi_addr;
465 /* Old FreeBSD-style arguments. */
466 sf.sf_siginfo = ksi->ksi_code;
467 sf.sf_addr = (register_t)ksi->ksi_addr;
468 sf.sf_ahu.sf_handler = catcher;
470 mtx_unlock(&psp->ps_mtx);
474 * If we're a vm86 process, we want to save the segment registers.
475 * We also change eflags to be our emulated eflags, not the actual
478 if (regs->tf_eflags & PSL_VM) {
479 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
480 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
482 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
483 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
484 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
485 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
487 if (vm86->vm86_has_vme == 0)
488 sf.sf_uc.uc_mcontext.mc_eflags =
489 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
490 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
493 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
494 * syscalls made by the signal handler. This just avoids
495 * wasting time for our lazy fixup of such faults. PSL_NT
496 * does nothing in vm86 mode, but vm86 programs can set it
497 * almost legitimately in probes for old cpu types.
499 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
503 * Copy the sigframe out to the user's stack.
505 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
507 printf("process %ld has trashed its stack\n", (long)p->p_pid);
513 regs->tf_esp = (int)sfp;
514 regs->tf_eip = PS_STRINGS - szfreebsd4_sigcode;
515 regs->tf_eflags &= ~(PSL_T | PSL_D);
516 regs->tf_cs = _ucodesel;
517 regs->tf_ds = _udatasel;
518 regs->tf_es = _udatasel;
519 regs->tf_fs = _udatasel;
520 regs->tf_ss = _udatasel;
522 mtx_lock(&psp->ps_mtx);
524 #endif /* COMPAT_FREEBSD4 */
527 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
529 struct sigframe sf, *sfp;
534 struct trapframe *regs;
535 struct segment_descriptor *sdp;
541 PROC_LOCK_ASSERT(p, MA_OWNED);
542 sig = ksi->ksi_signo;
544 mtx_assert(&psp->ps_mtx, MA_OWNED);
545 #ifdef COMPAT_FREEBSD4
546 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
547 freebsd4_sendsig(catcher, ksi, mask);
552 if (SIGISMEMBER(psp->ps_osigset, sig)) {
553 osendsig(catcher, ksi, mask);
558 oonstack = sigonstack(regs->tf_esp);
560 /* Save user context. */
561 bzero(&sf, sizeof(sf));
562 sf.sf_uc.uc_sigmask = *mask;
563 sf.sf_uc.uc_stack = td->td_sigstk;
564 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
565 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
566 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
567 sf.sf_uc.uc_mcontext.mc_gs = rgs();
568 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
569 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
570 get_fpcontext(td, &sf.sf_uc.uc_mcontext);
573 * Unconditionally fill the fsbase and gsbase into the mcontext.
575 sdp = &td->td_pcb->pcb_fsd;
576 sf.sf_uc.uc_mcontext.mc_fsbase = sdp->sd_hibase << 24 |
578 sdp = &td->td_pcb->pcb_gsd;
579 sf.sf_uc.uc_mcontext.mc_gsbase = sdp->sd_hibase << 24 |
582 /* Allocate space for the signal handler context. */
583 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
584 SIGISMEMBER(psp->ps_sigonstack, sig)) {
585 sp = td->td_sigstk.ss_sp +
586 td->td_sigstk.ss_size - sizeof(struct sigframe);
587 #if defined(COMPAT_43)
588 td->td_sigstk.ss_flags |= SS_ONSTACK;
591 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
592 /* Align to 16 bytes. */
593 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
595 /* Translate the signal if appropriate. */
596 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
597 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
599 /* Build the argument list for the signal handler. */
601 sf.sf_ucontext = (register_t)&sfp->sf_uc;
602 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
603 /* Signal handler installed with SA_SIGINFO. */
604 sf.sf_siginfo = (register_t)&sfp->sf_si;
605 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
607 /* Fill in POSIX parts */
608 sf.sf_si = ksi->ksi_info;
609 sf.sf_si.si_signo = sig; /* maybe a translated signal */
611 /* Old FreeBSD-style arguments. */
612 sf.sf_siginfo = ksi->ksi_code;
613 sf.sf_addr = (register_t)ksi->ksi_addr;
614 sf.sf_ahu.sf_handler = catcher;
616 mtx_unlock(&psp->ps_mtx);
620 * If we're a vm86 process, we want to save the segment registers.
621 * We also change eflags to be our emulated eflags, not the actual
624 if (regs->tf_eflags & PSL_VM) {
625 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
626 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
628 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
629 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
630 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
631 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
633 if (vm86->vm86_has_vme == 0)
634 sf.sf_uc.uc_mcontext.mc_eflags =
635 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
636 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
639 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
640 * syscalls made by the signal handler. This just avoids
641 * wasting time for our lazy fixup of such faults. PSL_NT
642 * does nothing in vm86 mode, but vm86 programs can set it
643 * almost legitimately in probes for old cpu types.
645 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
649 * Copy the sigframe out to the user's stack.
651 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
653 printf("process %ld has trashed its stack\n", (long)p->p_pid);
659 regs->tf_esp = (int)sfp;
660 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
661 regs->tf_eflags &= ~(PSL_T | PSL_D);
662 regs->tf_cs = _ucodesel;
663 regs->tf_ds = _udatasel;
664 regs->tf_es = _udatasel;
665 regs->tf_fs = _udatasel;
666 regs->tf_ss = _udatasel;
668 mtx_lock(&psp->ps_mtx);
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;
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 ksiginfo_init_trap(&ksi);
719 ksi.ksi_signo = SIGBUS;
720 ksi.ksi_code = BUS_OBJERR;
721 ksi.ksi_addr = (void *)regs->tf_eip;
722 trapsignal(td, &ksi);
725 if (vm86->vm86_has_vme) {
726 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
727 (eflags & VME_USERCHANGE) | PSL_VM;
729 vm86->vm86_eflags = eflags; /* save VIF, VIP */
730 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
731 (eflags & VM_USERCHANGE) | PSL_VM;
733 tf->tf_vm86_ds = scp->sc_ds;
734 tf->tf_vm86_es = scp->sc_es;
735 tf->tf_vm86_fs = scp->sc_fs;
736 tf->tf_vm86_gs = scp->sc_gs;
737 tf->tf_ds = _udatasel;
738 tf->tf_es = _udatasel;
739 tf->tf_fs = _udatasel;
742 * Don't allow users to change privileged or reserved flags.
745 * XXX do allow users to change the privileged flag PSL_RF.
746 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
747 * should sometimes set it there too. tf_eflags is kept in
748 * the signal context during signal handling and there is no
749 * other place to remember it, so the PSL_RF bit may be
750 * corrupted by the signal handler without us knowing.
751 * Corruption of the PSL_RF bit at worst causes one more or
752 * one less debugger trap, so allowing it is fairly harmless.
754 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
759 * Don't allow users to load a valid privileged %cs. Let the
760 * hardware check for invalid selectors, excess privilege in
761 * other selectors, invalid %eip's and invalid %esp's.
763 if (!CS_SECURE(scp->sc_cs)) {
764 ksiginfo_init_trap(&ksi);
765 ksi.ksi_signo = SIGBUS;
766 ksi.ksi_code = BUS_OBJERR;
767 ksi.ksi_trapno = T_PROTFLT;
768 ksi.ksi_addr = (void *)regs->tf_eip;
769 trapsignal(td, &ksi);
772 regs->tf_ds = scp->sc_ds;
773 regs->tf_es = scp->sc_es;
774 regs->tf_fs = scp->sc_fs;
777 /* Restore remaining registers. */
778 regs->tf_eax = scp->sc_eax;
779 regs->tf_ebx = scp->sc_ebx;
780 regs->tf_ecx = scp->sc_ecx;
781 regs->tf_edx = scp->sc_edx;
782 regs->tf_esi = scp->sc_esi;
783 regs->tf_edi = scp->sc_edi;
784 regs->tf_cs = scp->sc_cs;
785 regs->tf_ss = scp->sc_ss;
786 regs->tf_isp = scp->sc_isp;
787 regs->tf_ebp = scp->sc_fp;
788 regs->tf_esp = scp->sc_sp;
789 regs->tf_eip = scp->sc_pc;
790 regs->tf_eflags = eflags;
792 #if defined(COMPAT_43)
793 if (scp->sc_onstack & 1)
794 td->td_sigstk.ss_flags |= SS_ONSTACK;
796 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
798 kern_sigprocmask(td, SIG_SETMASK, (sigset_t *)&scp->sc_mask, NULL,
800 return (EJUSTRETURN);
802 #endif /* COMPAT_43 */
804 #ifdef COMPAT_FREEBSD4
809 freebsd4_sigreturn(td, uap)
811 struct freebsd4_sigreturn_args /* {
812 const ucontext4 *sigcntxp;
816 struct trapframe *regs;
817 struct ucontext4 *ucp;
818 int cs, eflags, error;
821 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
826 eflags = ucp->uc_mcontext.mc_eflags;
827 if (eflags & PSL_VM) {
828 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
829 struct vm86_kernel *vm86;
832 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
833 * set up the vm86 area, and we can't enter vm86 mode.
835 if (td->td_pcb->pcb_ext == 0)
837 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
838 if (vm86->vm86_inited == 0)
841 /* Go back to user mode if both flags are set. */
842 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
843 ksiginfo_init_trap(&ksi);
844 ksi.ksi_signo = SIGBUS;
845 ksi.ksi_code = BUS_OBJERR;
846 ksi.ksi_addr = (void *)regs->tf_eip;
847 trapsignal(td, &ksi);
849 if (vm86->vm86_has_vme) {
850 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
851 (eflags & VME_USERCHANGE) | PSL_VM;
853 vm86->vm86_eflags = eflags; /* save VIF, VIP */
854 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
855 (eflags & VM_USERCHANGE) | PSL_VM;
857 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
858 tf->tf_eflags = eflags;
859 tf->tf_vm86_ds = tf->tf_ds;
860 tf->tf_vm86_es = tf->tf_es;
861 tf->tf_vm86_fs = tf->tf_fs;
862 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
863 tf->tf_ds = _udatasel;
864 tf->tf_es = _udatasel;
865 tf->tf_fs = _udatasel;
868 * Don't allow users to change privileged or reserved flags.
871 * XXX do allow users to change the privileged flag PSL_RF.
872 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
873 * should sometimes set it there too. tf_eflags is kept in
874 * the signal context during signal handling and there is no
875 * other place to remember it, so the PSL_RF bit may be
876 * corrupted by the signal handler without us knowing.
877 * Corruption of the PSL_RF bit at worst causes one more or
878 * one less debugger trap, so allowing it is fairly harmless.
880 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
881 uprintf("pid %d (%s): freebsd4_sigreturn eflags = 0x%x\n",
882 td->td_proc->p_pid, td->td_name, eflags);
887 * Don't allow users to load a valid privileged %cs. Let the
888 * hardware check for invalid selectors, excess privilege in
889 * other selectors, invalid %eip's and invalid %esp's.
891 cs = ucp->uc_mcontext.mc_cs;
892 if (!CS_SECURE(cs)) {
893 uprintf("pid %d (%s): freebsd4_sigreturn cs = 0x%x\n",
894 td->td_proc->p_pid, td->td_name, cs);
895 ksiginfo_init_trap(&ksi);
896 ksi.ksi_signo = SIGBUS;
897 ksi.ksi_code = BUS_OBJERR;
898 ksi.ksi_trapno = T_PROTFLT;
899 ksi.ksi_addr = (void *)regs->tf_eip;
900 trapsignal(td, &ksi);
904 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
907 #if defined(COMPAT_43)
908 if (ucp->uc_mcontext.mc_onstack & 1)
909 td->td_sigstk.ss_flags |= SS_ONSTACK;
911 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
913 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
914 return (EJUSTRETURN);
916 #endif /* COMPAT_FREEBSD4 */
924 struct sigreturn_args /* {
925 const struct __ucontext *sigcntxp;
929 struct trapframe *regs;
931 int cs, eflags, error, ret;
934 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
939 eflags = ucp->uc_mcontext.mc_eflags;
940 if (eflags & PSL_VM) {
941 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
942 struct vm86_kernel *vm86;
945 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
946 * set up the vm86 area, and we can't enter vm86 mode.
948 if (td->td_pcb->pcb_ext == 0)
950 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
951 if (vm86->vm86_inited == 0)
954 /* Go back to user mode if both flags are set. */
955 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
956 ksiginfo_init_trap(&ksi);
957 ksi.ksi_signo = SIGBUS;
958 ksi.ksi_code = BUS_OBJERR;
959 ksi.ksi_addr = (void *)regs->tf_eip;
960 trapsignal(td, &ksi);
963 if (vm86->vm86_has_vme) {
964 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
965 (eflags & VME_USERCHANGE) | PSL_VM;
967 vm86->vm86_eflags = eflags; /* save VIF, VIP */
968 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
969 (eflags & VM_USERCHANGE) | PSL_VM;
971 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
972 tf->tf_eflags = eflags;
973 tf->tf_vm86_ds = tf->tf_ds;
974 tf->tf_vm86_es = tf->tf_es;
975 tf->tf_vm86_fs = tf->tf_fs;
976 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
977 tf->tf_ds = _udatasel;
978 tf->tf_es = _udatasel;
979 tf->tf_fs = _udatasel;
982 * Don't allow users to change privileged or reserved flags.
985 * XXX do allow users to change the privileged flag PSL_RF.
986 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
987 * should sometimes set it there too. tf_eflags is kept in
988 * the signal context during signal handling and there is no
989 * other place to remember it, so the PSL_RF bit may be
990 * corrupted by the signal handler without us knowing.
991 * Corruption of the PSL_RF bit at worst causes one more or
992 * one less debugger trap, so allowing it is fairly harmless.
994 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
995 uprintf("pid %d (%s): sigreturn eflags = 0x%x\n",
996 td->td_proc->p_pid, td->td_name, eflags);
1001 * Don't allow users to load a valid privileged %cs. Let the
1002 * hardware check for invalid selectors, excess privilege in
1003 * other selectors, invalid %eip's and invalid %esp's.
1005 cs = ucp->uc_mcontext.mc_cs;
1006 if (!CS_SECURE(cs)) {
1007 uprintf("pid %d (%s): sigreturn cs = 0x%x\n",
1008 td->td_proc->p_pid, td->td_name, cs);
1009 ksiginfo_init_trap(&ksi);
1010 ksi.ksi_signo = SIGBUS;
1011 ksi.ksi_code = BUS_OBJERR;
1012 ksi.ksi_trapno = T_PROTFLT;
1013 ksi.ksi_addr = (void *)regs->tf_eip;
1014 trapsignal(td, &ksi);
1018 ret = set_fpcontext(td, &ucp->uc_mcontext);
1021 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1024 #if defined(COMPAT_43)
1025 if (ucp->uc_mcontext.mc_onstack & 1)
1026 td->td_sigstk.ss_flags |= SS_ONSTACK;
1028 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1031 kern_sigprocmask(td, SIG_SETMASK, &ucp->uc_sigmask, NULL, 0);
1032 return (EJUSTRETURN);
1036 * Machine dependent boot() routine
1038 * I haven't seen anything to put here yet
1039 * Possibly some stuff might be grafted back here from boot()
1047 * Flush the D-cache for non-DMA I/O so that the I-cache can
1048 * be made coherent later.
1051 cpu_flush_dcache(void *ptr, size_t len)
1053 /* Not applicable */
1056 /* Get current clock frequency for the given cpu id. */
1058 cpu_est_clockrate(int cpu_id, uint64_t *rate)
1061 uint64_t tsc1, tsc2;
1063 if (pcpu_find(cpu_id) == NULL || rate == NULL)
1066 return (EOPNOTSUPP);
1068 /* If we're booting, trust the rate calibrated moments ago. */
1075 /* Schedule ourselves on the indicated cpu. */
1076 thread_lock(curthread);
1077 sched_bind(curthread, cpu_id);
1078 thread_unlock(curthread);
1081 /* Calibrate by measuring a short delay. */
1082 reg = intr_disable();
1089 thread_lock(curthread);
1090 sched_unbind(curthread);
1091 thread_unlock(curthread);
1095 * Calculate the difference in readings, convert to Mhz, and
1096 * subtract 0.5% of the total. Empirical testing has shown that
1097 * overhead in DELAY() works out to approximately this value.
1100 *rate = tsc2 * 1000 - tsc2 * 5;
1106 * Shutdown the CPU as much as possible
1116 cpu_idle_hlt(int busy)
1119 * we must absolutely guarentee that hlt is the next instruction
1120 * after sti or we introduce a timing window.
1123 if (sched_runnable())
1126 __asm __volatile("sti; hlt");
1130 cpu_idle_spin(int busy)
1135 void (*cpu_idle_fn)(int) = cpu_idle_hlt;
1141 if (mp_grab_cpu_hlt())
1148 * mwait cpu power states. Lower 4 bits are sub-states.
1150 #define MWAIT_C0 0xf0
1151 #define MWAIT_C1 0x00
1152 #define MWAIT_C2 0x10
1153 #define MWAIT_C3 0x20
1154 #define MWAIT_C4 0x30
1156 #define MWAIT_DISABLED 0x0
1157 #define MWAIT_WOKEN 0x1
1158 #define MWAIT_WAITING 0x2
1161 cpu_idle_mwait(int busy)
1165 mwait = (int *)PCPU_PTR(monitorbuf);
1166 *mwait = MWAIT_WAITING;
1167 if (sched_runnable())
1169 cpu_monitor(mwait, 0, 0);
1170 if (*mwait == MWAIT_WAITING)
1171 cpu_mwait(0, MWAIT_C1);
1175 cpu_idle_mwait_hlt(int busy)
1179 mwait = (int *)PCPU_PTR(monitorbuf);
1181 *mwait = MWAIT_DISABLED;
1185 *mwait = MWAIT_WAITING;
1186 if (sched_runnable())
1188 cpu_monitor(mwait, 0, 0);
1189 if (*mwait == MWAIT_WAITING)
1190 cpu_mwait(0, MWAIT_C1);
1194 cpu_idle_wakeup(int cpu)
1199 if (cpu_idle_fn == cpu_idle_spin)
1201 if (cpu_idle_fn != cpu_idle_mwait && cpu_idle_fn != cpu_idle_mwait_hlt)
1203 pcpu = pcpu_find(cpu);
1204 mwait = (int *)pcpu->pc_monitorbuf;
1206 * This doesn't need to be atomic since missing the race will
1207 * simply result in unnecessary IPIs.
1209 if (cpu_idle_fn == cpu_idle_mwait_hlt && *mwait == MWAIT_DISABLED)
1211 *mwait = MWAIT_WOKEN;
1217 * Ordered by speed/power consumption.
1223 { cpu_idle_spin, "spin" },
1224 { cpu_idle_mwait, "mwait" },
1225 { cpu_idle_mwait_hlt, "mwait_hlt" },
1226 { cpu_idle_hlt, "hlt" },
1231 idle_sysctl_available(SYSCTL_HANDLER_ARGS)
1237 avail = malloc(256, M_TEMP, M_WAITOK);
1239 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1240 if (strstr(idle_tbl[i].id_name, "mwait") &&
1241 (cpu_feature2 & CPUID2_MON) == 0)
1243 p += sprintf(p, "%s, ", idle_tbl[i].id_name);
1245 error = sysctl_handle_string(oidp, avail, 0, req);
1246 free(avail, M_TEMP);
1251 idle_sysctl(SYSCTL_HANDLER_ARGS)
1259 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1260 if (idle_tbl[i].id_fn == cpu_idle_fn) {
1261 p = idle_tbl[i].id_name;
1265 strncpy(buf, p, sizeof(buf));
1266 error = sysctl_handle_string(oidp, buf, sizeof(buf), req);
1267 if (error != 0 || req->newptr == NULL)
1269 for (i = 0; idle_tbl[i].id_name != NULL; i++) {
1270 if (strstr(idle_tbl[i].id_name, "mwait") &&
1271 (cpu_feature2 & CPUID2_MON) == 0)
1273 if (strcmp(idle_tbl[i].id_name, buf))
1275 cpu_idle_fn = idle_tbl[i].id_fn;
1281 SYSCTL_PROC(_machdep, OID_AUTO, idle_available, CTLTYPE_STRING | CTLFLAG_RD,
1282 0, 0, idle_sysctl_available, "A", "list of available idle functions");
1284 SYSCTL_PROC(_machdep, OID_AUTO, idle, CTLTYPE_STRING | CTLFLAG_RW, 0, 0,
1285 idle_sysctl, "A", "currently selected idle function");
1288 * Reset registers to default values on exec.
1291 exec_setregs(td, entry, stack, ps_strings)
1297 struct trapframe *regs = td->td_frame;
1298 struct pcb *pcb = td->td_pcb;
1300 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1301 pcb->pcb_gs = _udatasel;
1304 mtx_lock_spin(&dt_lock);
1305 if (td->td_proc->p_md.md_ldt)
1308 mtx_unlock_spin(&dt_lock);
1310 bzero((char *)regs, sizeof(struct trapframe));
1311 regs->tf_eip = entry;
1312 regs->tf_esp = stack;
1313 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1314 regs->tf_ss = _udatasel;
1315 regs->tf_ds = _udatasel;
1316 regs->tf_es = _udatasel;
1317 regs->tf_fs = _udatasel;
1318 regs->tf_cs = _ucodesel;
1320 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1321 regs->tf_ebx = ps_strings;
1324 * Reset the hardware debug registers if they were in use.
1325 * They won't have any meaning for the newly exec'd process.
1327 if (pcb->pcb_flags & PCB_DBREGS) {
1334 if (pcb == PCPU_GET(curpcb)) {
1336 * Clear the debug registers on the running
1337 * CPU, otherwise they will end up affecting
1338 * the next process we switch to.
1342 pcb->pcb_flags &= ~PCB_DBREGS;
1346 * Initialize the math emulator (if any) for the current process.
1347 * Actually, just clear the bit that says that the emulator has
1348 * been initialized. Initialization is delayed until the process
1349 * traps to the emulator (if it is done at all) mainly because
1350 * emulators don't provide an entry point for initialization.
1352 td->td_pcb->pcb_flags &= ~FP_SOFTFP;
1353 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1356 * Drop the FP state if we hold it, so that the process gets a
1357 * clean FP state if it uses the FPU again.
1362 * XXX - Linux emulator
1363 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1366 td->td_retval[1] = 0;
1377 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1379 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1380 * instructions. We must set the CR0_MP bit and use the CR0_TS
1381 * bit to control the trap, because setting the CR0_EM bit does
1382 * not cause WAIT instructions to trap. It's important to trap
1383 * WAIT instructions - otherwise the "wait" variants of no-wait
1384 * control instructions would degenerate to the "no-wait" variants
1385 * after FP context switches but work correctly otherwise. It's
1386 * particularly important to trap WAITs when there is no NPX -
1387 * otherwise the "wait" variants would always degenerate.
1389 * Try setting CR0_NE to get correct error reporting on 486DX's.
1390 * Setting it should fail or do nothing on lesser processors.
1392 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1397 u_long bootdev; /* not a struct cdev *- encoding is different */
1398 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1399 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1402 * Initialize 386 and configure to run kernel
1406 * Initialize segments & interrupt table
1411 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1412 union descriptor ldt[NLDT]; /* local descriptor table */
1413 static struct gate_descriptor idt0[NIDT];
1414 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1415 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1416 struct mtx dt_lock; /* lock for GDT and LDT */
1418 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1419 extern int has_f00f_bug;
1422 static struct i386tss dblfault_tss;
1423 static char dblfault_stack[PAGE_SIZE];
1425 extern vm_offset_t proc0kstack;
1429 * software prototypes -- in more palatable form.
1431 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1432 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1434 struct soft_segment_descriptor gdt_segs[] = {
1435 /* GNULL_SEL 0 Null Descriptor */
1441 .ssd_xx = 0, .ssd_xx1 = 0,
1444 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1446 .ssd_limit = 0xfffff,
1447 .ssd_type = SDT_MEMRWA,
1450 .ssd_xx = 0, .ssd_xx1 = 0,
1453 /* GUFS_SEL 2 %fs Descriptor for user */
1455 .ssd_limit = 0xfffff,
1456 .ssd_type = SDT_MEMRWA,
1459 .ssd_xx = 0, .ssd_xx1 = 0,
1462 /* GUGS_SEL 3 %gs Descriptor for user */
1464 .ssd_limit = 0xfffff,
1465 .ssd_type = SDT_MEMRWA,
1468 .ssd_xx = 0, .ssd_xx1 = 0,
1471 /* GCODE_SEL 4 Code Descriptor for kernel */
1473 .ssd_limit = 0xfffff,
1474 .ssd_type = SDT_MEMERA,
1477 .ssd_xx = 0, .ssd_xx1 = 0,
1480 /* GDATA_SEL 5 Data Descriptor for kernel */
1482 .ssd_limit = 0xfffff,
1483 .ssd_type = SDT_MEMRWA,
1486 .ssd_xx = 0, .ssd_xx1 = 0,
1489 /* GUCODE_SEL 6 Code Descriptor for user */
1491 .ssd_limit = 0xfffff,
1492 .ssd_type = SDT_MEMERA,
1495 .ssd_xx = 0, .ssd_xx1 = 0,
1498 /* GUDATA_SEL 7 Data Descriptor for user */
1500 .ssd_limit = 0xfffff,
1501 .ssd_type = SDT_MEMRWA,
1504 .ssd_xx = 0, .ssd_xx1 = 0,
1507 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1508 { .ssd_base = 0x400,
1509 .ssd_limit = 0xfffff,
1510 .ssd_type = SDT_MEMRWA,
1513 .ssd_xx = 0, .ssd_xx1 = 0,
1516 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1519 .ssd_limit = sizeof(struct i386tss)-1,
1520 .ssd_type = SDT_SYS386TSS,
1523 .ssd_xx = 0, .ssd_xx1 = 0,
1526 /* GLDT_SEL 10 LDT Descriptor */
1527 { .ssd_base = (int) ldt,
1528 .ssd_limit = sizeof(ldt)-1,
1529 .ssd_type = SDT_SYSLDT,
1532 .ssd_xx = 0, .ssd_xx1 = 0,
1535 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1536 { .ssd_base = (int) ldt,
1537 .ssd_limit = (512 * sizeof(union descriptor)-1),
1538 .ssd_type = SDT_SYSLDT,
1541 .ssd_xx = 0, .ssd_xx1 = 0,
1544 /* GPANIC_SEL 12 Panic Tss Descriptor */
1545 { .ssd_base = (int) &dblfault_tss,
1546 .ssd_limit = sizeof(struct i386tss)-1,
1547 .ssd_type = SDT_SYS386TSS,
1550 .ssd_xx = 0, .ssd_xx1 = 0,
1553 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1555 .ssd_limit = 0xfffff,
1556 .ssd_type = SDT_MEMERA,
1559 .ssd_xx = 0, .ssd_xx1 = 0,
1562 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1564 .ssd_limit = 0xfffff,
1565 .ssd_type = SDT_MEMERA,
1568 .ssd_xx = 0, .ssd_xx1 = 0,
1571 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1573 .ssd_limit = 0xfffff,
1574 .ssd_type = SDT_MEMRWA,
1577 .ssd_xx = 0, .ssd_xx1 = 0,
1580 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1582 .ssd_limit = 0xfffff,
1583 .ssd_type = SDT_MEMRWA,
1586 .ssd_xx = 0, .ssd_xx1 = 0,
1589 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1591 .ssd_limit = 0xfffff,
1592 .ssd_type = SDT_MEMRWA,
1595 .ssd_xx = 0, .ssd_xx1 = 0,
1598 /* GNDIS_SEL 18 NDIS Descriptor */
1604 .ssd_xx = 0, .ssd_xx1 = 0,
1609 static struct soft_segment_descriptor ldt_segs[] = {
1610 /* Null Descriptor - overwritten by call gate */
1616 .ssd_xx = 0, .ssd_xx1 = 0,
1619 /* Null Descriptor - overwritten by call gate */
1625 .ssd_xx = 0, .ssd_xx1 = 0,
1628 /* Null Descriptor - overwritten by call gate */
1634 .ssd_xx = 0, .ssd_xx1 = 0,
1637 /* Code Descriptor for user */
1639 .ssd_limit = 0xfffff,
1640 .ssd_type = SDT_MEMERA,
1643 .ssd_xx = 0, .ssd_xx1 = 0,
1646 /* Null Descriptor - overwritten by call gate */
1652 .ssd_xx = 0, .ssd_xx1 = 0,
1655 /* Data Descriptor for user */
1657 .ssd_limit = 0xfffff,
1658 .ssd_type = SDT_MEMRWA,
1661 .ssd_xx = 0, .ssd_xx1 = 0,
1667 setidt(idx, func, typ, dpl, selec)
1674 struct gate_descriptor *ip;
1677 ip->gd_looffset = (int)func;
1678 ip->gd_selector = selec;
1684 ip->gd_hioffset = ((int)func)>>16 ;
1688 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1689 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1690 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1691 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1692 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1696 * Display the index and function name of any IDT entries that don't use
1697 * the default 'rsvd' entry point.
1699 DB_SHOW_COMMAND(idt, db_show_idt)
1701 struct gate_descriptor *ip;
1706 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
1707 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1708 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1709 db_printf("%3d\t", idx);
1710 db_printsym(func, DB_STGY_PROC);
1717 /* Show privileged registers. */
1718 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
1720 uint64_t idtr, gdtr;
1723 db_printf("idtr\t0x%08x/%04x\n",
1724 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
1726 db_printf("gdtr\t0x%08x/%04x\n",
1727 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
1728 db_printf("ldtr\t0x%04x\n", rldt());
1729 db_printf("tr\t0x%04x\n", rtr());
1730 db_printf("cr0\t0x%08x\n", rcr0());
1731 db_printf("cr2\t0x%08x\n", rcr2());
1732 db_printf("cr3\t0x%08x\n", rcr3());
1733 db_printf("cr4\t0x%08x\n", rcr4());
1739 struct segment_descriptor *sd;
1740 struct soft_segment_descriptor *ssd;
1742 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1743 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1744 ssd->ssd_type = sd->sd_type;
1745 ssd->ssd_dpl = sd->sd_dpl;
1746 ssd->ssd_p = sd->sd_p;
1747 ssd->ssd_def32 = sd->sd_def32;
1748 ssd->ssd_gran = sd->sd_gran;
1758 if (basemem > 640) {
1759 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1765 * XXX if biosbasemem is now < 640, there is a `hole'
1766 * between the end of base memory and the start of
1767 * ISA memory. The hole may be empty or it may
1768 * contain BIOS code or data. Map it read/write so
1769 * that the BIOS can write to it. (Memory from 0 to
1770 * the physical end of the kernel is mapped read-only
1771 * to begin with and then parts of it are remapped.
1772 * The parts that aren't remapped form holes that
1773 * remain read-only and are unused by the kernel.
1774 * The base memory area is below the physical end of
1775 * the kernel and right now forms a read-only hole.
1776 * The part of it from PAGE_SIZE to
1777 * (trunc_page(biosbasemem * 1024) - 1) will be
1778 * remapped and used by the kernel later.)
1780 * This code is similar to the code used in
1781 * pmap_mapdev, but since no memory needs to be
1782 * allocated we simply change the mapping.
1784 for (pa = trunc_page(basemem * 1024);
1785 pa < ISA_HOLE_START; pa += PAGE_SIZE)
1786 pmap_kenter(KERNBASE + pa, pa);
1789 * Map pages between basemem and ISA_HOLE_START, if any, r/w into
1790 * the vm86 page table so that vm86 can scribble on them using
1791 * the vm86 map too. XXX: why 2 ways for this and only 1 way for
1792 * page 0, at least as initialized here?
1794 pte = (pt_entry_t *)vm86paddr;
1795 for (i = basemem / 4; i < 160; i++)
1796 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1800 * Populate the (physmap) array with base/bound pairs describing the
1801 * available physical memory in the system, then test this memory and
1802 * build the phys_avail array describing the actually-available memory.
1804 * If we cannot accurately determine the physical memory map, then use
1805 * value from the 0xE801 call, and failing that, the RTC.
1807 * Total memory size may be set by the kernel environment variable
1808 * hw.physmem or the compile-time define MAXMEM.
1810 * XXX first should be vm_paddr_t.
1813 getmemsize(int first)
1815 int off, physmap_idx, pa_indx, da_indx;
1816 u_long physmem_tunable;
1817 vm_paddr_t physmap[PHYSMAP_SIZE];
1819 quad_t dcons_addr, dcons_size;
1826 bzero(physmap, sizeof(physmap));
1828 /* XXX - some of EPSON machines can't use PG_N */
1830 if (pc98_machine_type & M_EPSON_PC98) {
1831 switch (epson_machine_id) {
1835 case EPSON_PC486_HX:
1836 case EPSON_PC486_HG:
1837 case EPSON_PC486_HA:
1843 under16 = pc98_getmemsize(&basemem, &extmem);
1847 physmap[1] = basemem * 1024;
1849 physmap[physmap_idx] = 0x100000;
1850 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1853 * Now, physmap contains a map of physical memory.
1857 /* make hole for AP bootstrap code */
1858 physmap[1] = mp_bootaddress(physmap[1]);
1862 * Maxmem isn't the "maximum memory", it's one larger than the
1863 * highest page of the physical address space. It should be
1864 * called something like "Maxphyspage". We may adjust this
1865 * based on ``hw.physmem'' and the results of the memory test.
1867 Maxmem = atop(physmap[physmap_idx + 1]);
1870 Maxmem = MAXMEM / 4;
1873 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
1874 Maxmem = atop(physmem_tunable);
1876 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1877 (boothowto & RB_VERBOSE))
1878 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1881 * If Maxmem has been increased beyond what the system has detected,
1882 * extend the last memory segment to the new limit.
1884 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1885 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1888 * We need to divide chunk if Maxmem is larger than 16MB and
1889 * under 16MB area is not full of memory.
1890 * (1) system area (15-16MB region) is cut off
1891 * (2) extended memory is only over 16MB area (ex. Melco "HYPERMEMORY")
1893 if ((under16 != 16 * 1024) && (extmem > 15 * 1024)) {
1894 /* 15M - 16M region is cut off, so need to divide chunk */
1895 physmap[physmap_idx + 1] = under16 * 1024;
1897 physmap[physmap_idx] = 0x1000000;
1898 physmap[physmap_idx + 1] = physmap[2] + extmem * 1024;
1901 /* call pmap initialization to make new kernel address space */
1902 pmap_bootstrap(first);
1905 * Size up each available chunk of physical memory.
1907 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1910 phys_avail[pa_indx++] = physmap[0];
1911 phys_avail[pa_indx] = physmap[0];
1912 dump_avail[da_indx] = physmap[0];
1916 * Get dcons buffer address
1918 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
1919 getenv_quad("dcons.size", &dcons_size) == 0)
1923 * physmap is in bytes, so when converting to page boundaries,
1924 * round up the start address and round down the end address.
1926 for (i = 0; i <= physmap_idx; i += 2) {
1929 end = ptoa((vm_paddr_t)Maxmem);
1930 if (physmap[i + 1] < end)
1931 end = trunc_page(physmap[i + 1]);
1932 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1933 int tmp, page_bad, full;
1934 int *ptr = (int *)CADDR1;
1938 * block out kernel memory as not available.
1940 if (pa >= KERNLOAD && pa < first)
1944 * block out dcons buffer
1947 && pa >= trunc_page(dcons_addr)
1948 && pa < dcons_addr + dcons_size)
1954 * map page into kernel: valid, read/write,non-cacheable
1956 *pte = pa | PG_V | PG_RW | pg_n;
1961 * Test for alternating 1's and 0's
1963 *(volatile int *)ptr = 0xaaaaaaaa;
1964 if (*(volatile int *)ptr != 0xaaaaaaaa)
1967 * Test for alternating 0's and 1's
1969 *(volatile int *)ptr = 0x55555555;
1970 if (*(volatile int *)ptr != 0x55555555)
1975 *(volatile int *)ptr = 0xffffffff;
1976 if (*(volatile int *)ptr != 0xffffffff)
1981 *(volatile int *)ptr = 0x0;
1982 if (*(volatile int *)ptr != 0x0)
1985 * Restore original value.
1990 * Adjust array of valid/good pages.
1992 if (page_bad == TRUE)
1995 * If this good page is a continuation of the
1996 * previous set of good pages, then just increase
1997 * the end pointer. Otherwise start a new chunk.
1998 * Note that "end" points one higher than end,
1999 * making the range >= start and < end.
2000 * If we're also doing a speculative memory
2001 * test and we at or past the end, bump up Maxmem
2002 * so that we keep going. The first bad page
2003 * will terminate the loop.
2005 if (phys_avail[pa_indx] == pa) {
2006 phys_avail[pa_indx] += PAGE_SIZE;
2009 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
2011 "Too many holes in the physical address space, giving up\n");
2016 phys_avail[pa_indx++] = pa; /* start */
2017 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
2021 if (dump_avail[da_indx] == pa) {
2022 dump_avail[da_indx] += PAGE_SIZE;
2025 if (da_indx == DUMP_AVAIL_ARRAY_END) {
2029 dump_avail[da_indx++] = pa; /* start */
2030 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
2042 * The last chunk must contain at least one page plus the message
2043 * buffer to avoid complicating other code (message buffer address
2044 * calculation, etc.).
2046 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
2047 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
2048 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
2049 phys_avail[pa_indx--] = 0;
2050 phys_avail[pa_indx--] = 0;
2053 Maxmem = atop(phys_avail[pa_indx]);
2055 /* Trim off space for the message buffer. */
2056 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
2058 /* Map the message buffer. */
2059 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
2060 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
2068 struct gate_descriptor *gdp;
2069 int gsel_tss, metadata_missing, x, pa;
2072 thread0.td_kstack = proc0kstack;
2073 thread0.td_pcb = (struct pcb *)
2074 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
2077 * This may be done better later if it gets more high level
2078 * components in it. If so just link td->td_proc here.
2080 proc_linkup0(&proc0, &thread0);
2087 metadata_missing = 0;
2088 if (bootinfo.bi_modulep) {
2089 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
2090 preload_bootstrap_relocate(KERNBASE);
2092 metadata_missing = 1;
2095 kern_envp = static_env;
2096 else if (bootinfo.bi_envp)
2097 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
2099 /* Init basic tunables, hz etc */
2103 * Make gdt memory segments. All segments cover the full 4GB
2104 * of address space and permissions are enforced at page level.
2106 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
2107 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
2108 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
2109 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
2110 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
2111 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
2114 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
2115 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2116 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2118 for (x = 0; x < NGDT; x++)
2119 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2121 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2122 r_gdt.rd_base = (int) gdt;
2123 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2126 pcpu_init(pc, 0, sizeof(struct pcpu));
2127 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2128 pmap_kenter(pa + KERNBASE, pa);
2129 dpcpu_init((void *)(first + KERNBASE), 0);
2130 first += DPCPU_SIZE;
2131 PCPU_SET(prvspace, pc);
2132 PCPU_SET(curthread, &thread0);
2133 PCPU_SET(curpcb, thread0.td_pcb);
2136 * Initialize mutexes.
2138 * icu_lock: in order to allow an interrupt to occur in a critical
2139 * section, to set pcpu->ipending (etc...) properly, we
2140 * must be able to get the icu lock, so it can't be
2144 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2146 /* make ldt memory segments */
2147 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2148 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2149 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2150 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2152 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2154 PCPU_SET(currentldt, _default_ldt);
2157 for (x = 0; x < NIDT; x++)
2158 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2159 GSEL(GCODE_SEL, SEL_KPL));
2160 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2161 GSEL(GCODE_SEL, SEL_KPL));
2162 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2163 GSEL(GCODE_SEL, SEL_KPL));
2164 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
2165 GSEL(GCODE_SEL, SEL_KPL));
2166 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2167 GSEL(GCODE_SEL, SEL_KPL));
2168 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2169 GSEL(GCODE_SEL, SEL_KPL));
2170 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2171 GSEL(GCODE_SEL, SEL_KPL));
2172 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2173 GSEL(GCODE_SEL, SEL_KPL));
2174 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2175 , GSEL(GCODE_SEL, SEL_KPL));
2176 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2177 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2178 GSEL(GCODE_SEL, SEL_KPL));
2179 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2180 GSEL(GCODE_SEL, SEL_KPL));
2181 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2182 GSEL(GCODE_SEL, SEL_KPL));
2183 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2184 GSEL(GCODE_SEL, SEL_KPL));
2185 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2186 GSEL(GCODE_SEL, SEL_KPL));
2187 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2188 GSEL(GCODE_SEL, SEL_KPL));
2189 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2190 GSEL(GCODE_SEL, SEL_KPL));
2191 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2192 GSEL(GCODE_SEL, SEL_KPL));
2193 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2194 GSEL(GCODE_SEL, SEL_KPL));
2195 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2196 GSEL(GCODE_SEL, SEL_KPL));
2197 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2198 GSEL(GCODE_SEL, SEL_KPL));
2200 r_idt.rd_limit = sizeof(idt0) - 1;
2201 r_idt.rd_base = (int) idt;
2205 * Initialize the i8254 before the console so that console
2206 * initialization can use DELAY().
2211 * Initialize the console before we print anything out.
2215 if (metadata_missing)
2216 printf("WARNING: loader(8) metadata is missing!\n");
2223 ksym_start = bootinfo.bi_symtab;
2224 ksym_end = bootinfo.bi_esymtab;
2230 if (boothowto & RB_KDB)
2231 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
2234 finishidentcpu(); /* Final stage of CPU initialization */
2235 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2236 GSEL(GCODE_SEL, SEL_KPL));
2237 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2238 GSEL(GCODE_SEL, SEL_KPL));
2239 initializecpu(); /* Initialize CPU registers */
2241 /* make an initial tss so cpu can get interrupt stack on syscall! */
2242 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2243 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2244 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
2245 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2246 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2247 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2248 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2249 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2252 /* pointer to selector slot for %fs/%gs */
2253 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2255 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2256 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2257 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2258 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2259 dblfault_tss.tss_cr3 = (int)IdlePTD;
2260 dblfault_tss.tss_eip = (int)dblfault_handler;
2261 dblfault_tss.tss_eflags = PSL_KERNEL;
2262 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2263 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2264 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2265 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2266 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2270 init_param2(physmem);
2272 /* now running on new page tables, configured,and u/iom is accessible */
2274 msgbufinit(msgbufp, MSGBUF_SIZE);
2276 /* make a call gate to reenter kernel with */
2277 gdp = &ldt[LSYS5CALLS_SEL].gd;
2279 x = (int) &IDTVEC(lcall_syscall);
2280 gdp->gd_looffset = x;
2281 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2283 gdp->gd_type = SDT_SYS386CGT;
2284 gdp->gd_dpl = SEL_UPL;
2286 gdp->gd_hioffset = x >> 16;
2288 /* XXX does this work? */
2290 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2291 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
2293 /* transfer to user mode */
2295 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2296 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2298 /* setup proc 0's pcb */
2299 thread0.td_pcb->pcb_flags = 0;
2300 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2301 thread0.td_pcb->pcb_ext = 0;
2302 thread0.td_frame = &proc0_tf;
2306 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2312 spinlock_enter(void)
2317 if (td->td_md.md_spinlock_count == 0)
2318 td->td_md.md_saved_flags = intr_disable();
2319 td->td_md.md_spinlock_count++;
2330 td->td_md.md_spinlock_count--;
2331 if (td->td_md.md_spinlock_count == 0)
2332 intr_restore(td->td_md.md_saved_flags);
2335 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2336 static void f00f_hack(void *unused);
2337 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
2340 f00f_hack(void *unused)
2342 struct gate_descriptor *new_idt;
2350 printf("Intel Pentium detected, installing workaround for F00F bug\n");
2352 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
2354 panic("kmem_alloc returned 0");
2356 /* Put the problematic entry (#6) at the end of the lower page. */
2357 new_idt = (struct gate_descriptor*)
2358 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
2359 bcopy(idt, new_idt, sizeof(idt0));
2360 r_idt.rd_base = (u_int)new_idt;
2363 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2364 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2365 panic("vm_map_protect failed");
2367 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2370 * Construct a PCB from a trapframe. This is called from kdb_trap() where
2371 * we want to start a backtrace from the function that caused us to enter
2372 * the debugger. We have the context in the trapframe, but base the trace
2373 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
2374 * enough for a backtrace.
2377 makectx(struct trapframe *tf, struct pcb *pcb)
2380 pcb->pcb_edi = tf->tf_edi;
2381 pcb->pcb_esi = tf->tf_esi;
2382 pcb->pcb_ebp = tf->tf_ebp;
2383 pcb->pcb_ebx = tf->tf_ebx;
2384 pcb->pcb_eip = tf->tf_eip;
2385 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
2389 ptrace_set_pc(struct thread *td, u_long addr)
2392 td->td_frame->tf_eip = addr;
2397 ptrace_single_step(struct thread *td)
2399 td->td_frame->tf_eflags |= PSL_T;
2404 ptrace_clear_single_step(struct thread *td)
2406 td->td_frame->tf_eflags &= ~PSL_T;
2411 fill_regs(struct thread *td, struct reg *regs)
2414 struct trapframe *tp;
2418 regs->r_fs = tp->tf_fs;
2419 regs->r_es = tp->tf_es;
2420 regs->r_ds = tp->tf_ds;
2421 regs->r_edi = tp->tf_edi;
2422 regs->r_esi = tp->tf_esi;
2423 regs->r_ebp = tp->tf_ebp;
2424 regs->r_ebx = tp->tf_ebx;
2425 regs->r_edx = tp->tf_edx;
2426 regs->r_ecx = tp->tf_ecx;
2427 regs->r_eax = tp->tf_eax;
2428 regs->r_eip = tp->tf_eip;
2429 regs->r_cs = tp->tf_cs;
2430 regs->r_eflags = tp->tf_eflags;
2431 regs->r_esp = tp->tf_esp;
2432 regs->r_ss = tp->tf_ss;
2433 regs->r_gs = pcb->pcb_gs;
2438 set_regs(struct thread *td, struct reg *regs)
2441 struct trapframe *tp;
2444 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2445 !CS_SECURE(regs->r_cs))
2448 tp->tf_fs = regs->r_fs;
2449 tp->tf_es = regs->r_es;
2450 tp->tf_ds = regs->r_ds;
2451 tp->tf_edi = regs->r_edi;
2452 tp->tf_esi = regs->r_esi;
2453 tp->tf_ebp = regs->r_ebp;
2454 tp->tf_ebx = regs->r_ebx;
2455 tp->tf_edx = regs->r_edx;
2456 tp->tf_ecx = regs->r_ecx;
2457 tp->tf_eax = regs->r_eax;
2458 tp->tf_eip = regs->r_eip;
2459 tp->tf_cs = regs->r_cs;
2460 tp->tf_eflags = regs->r_eflags;
2461 tp->tf_esp = regs->r_esp;
2462 tp->tf_ss = regs->r_ss;
2463 pcb->pcb_gs = regs->r_gs;
2467 #ifdef CPU_ENABLE_SSE
2469 fill_fpregs_xmm(sv_xmm, sv_87)
2470 struct savexmm *sv_xmm;
2471 struct save87 *sv_87;
2473 register struct env87 *penv_87 = &sv_87->sv_env;
2474 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2477 bzero(sv_87, sizeof(*sv_87));
2479 /* FPU control/status */
2480 penv_87->en_cw = penv_xmm->en_cw;
2481 penv_87->en_sw = penv_xmm->en_sw;
2482 penv_87->en_tw = penv_xmm->en_tw;
2483 penv_87->en_fip = penv_xmm->en_fip;
2484 penv_87->en_fcs = penv_xmm->en_fcs;
2485 penv_87->en_opcode = penv_xmm->en_opcode;
2486 penv_87->en_foo = penv_xmm->en_foo;
2487 penv_87->en_fos = penv_xmm->en_fos;
2490 for (i = 0; i < 8; ++i)
2491 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
2495 set_fpregs_xmm(sv_87, sv_xmm)
2496 struct save87 *sv_87;
2497 struct savexmm *sv_xmm;
2499 register struct env87 *penv_87 = &sv_87->sv_env;
2500 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2503 /* FPU control/status */
2504 penv_xmm->en_cw = penv_87->en_cw;
2505 penv_xmm->en_sw = penv_87->en_sw;
2506 penv_xmm->en_tw = penv_87->en_tw;
2507 penv_xmm->en_fip = penv_87->en_fip;
2508 penv_xmm->en_fcs = penv_87->en_fcs;
2509 penv_xmm->en_opcode = penv_87->en_opcode;
2510 penv_xmm->en_foo = penv_87->en_foo;
2511 penv_xmm->en_fos = penv_87->en_fos;
2514 for (i = 0; i < 8; ++i)
2515 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
2517 #endif /* CPU_ENABLE_SSE */
2520 fill_fpregs(struct thread *td, struct fpreg *fpregs)
2523 KASSERT(TD_IS_SUSPENDED(td), ("not suspended thread %p", td));
2525 #ifdef CPU_ENABLE_SSE
2527 fill_fpregs_xmm(&td->td_pcb->pcb_user_save.sv_xmm,
2528 (struct save87 *)fpregs);
2530 #endif /* CPU_ENABLE_SSE */
2531 bcopy(&td->td_pcb->pcb_user_save.sv_87, fpregs,
2537 set_fpregs(struct thread *td, struct fpreg *fpregs)
2540 #ifdef CPU_ENABLE_SSE
2542 set_fpregs_xmm((struct save87 *)fpregs,
2543 &td->td_pcb->pcb_user_save.sv_xmm);
2545 #endif /* CPU_ENABLE_SSE */
2546 bcopy(fpregs, &td->td_pcb->pcb_user_save.sv_87,
2553 * Get machine context.
2556 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
2558 struct trapframe *tp;
2559 struct segment_descriptor *sdp;
2563 PROC_LOCK(curthread->td_proc);
2564 mcp->mc_onstack = sigonstack(tp->tf_esp);
2565 PROC_UNLOCK(curthread->td_proc);
2566 mcp->mc_gs = td->td_pcb->pcb_gs;
2567 mcp->mc_fs = tp->tf_fs;
2568 mcp->mc_es = tp->tf_es;
2569 mcp->mc_ds = tp->tf_ds;
2570 mcp->mc_edi = tp->tf_edi;
2571 mcp->mc_esi = tp->tf_esi;
2572 mcp->mc_ebp = tp->tf_ebp;
2573 mcp->mc_isp = tp->tf_isp;
2574 mcp->mc_eflags = tp->tf_eflags;
2575 if (flags & GET_MC_CLEAR_RET) {
2578 mcp->mc_eflags &= ~PSL_C;
2580 mcp->mc_eax = tp->tf_eax;
2581 mcp->mc_edx = tp->tf_edx;
2583 mcp->mc_ebx = tp->tf_ebx;
2584 mcp->mc_ecx = tp->tf_ecx;
2585 mcp->mc_eip = tp->tf_eip;
2586 mcp->mc_cs = tp->tf_cs;
2587 mcp->mc_esp = tp->tf_esp;
2588 mcp->mc_ss = tp->tf_ss;
2589 mcp->mc_len = sizeof(*mcp);
2590 get_fpcontext(td, mcp);
2591 sdp = &td->td_pcb->pcb_fsd;
2592 mcp->mc_fsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2593 sdp = &td->td_pcb->pcb_gsd;
2594 mcp->mc_gsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2600 * Set machine context.
2602 * However, we don't set any but the user modifiable flags, and we won't
2603 * touch the cs selector.
2606 set_mcontext(struct thread *td, const mcontext_t *mcp)
2608 struct trapframe *tp;
2612 if (mcp->mc_len != sizeof(*mcp))
2614 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
2615 (tp->tf_eflags & ~PSL_USERCHANGE);
2616 if ((ret = set_fpcontext(td, mcp)) == 0) {
2617 tp->tf_fs = mcp->mc_fs;
2618 tp->tf_es = mcp->mc_es;
2619 tp->tf_ds = mcp->mc_ds;
2620 tp->tf_edi = mcp->mc_edi;
2621 tp->tf_esi = mcp->mc_esi;
2622 tp->tf_ebp = mcp->mc_ebp;
2623 tp->tf_ebx = mcp->mc_ebx;
2624 tp->tf_edx = mcp->mc_edx;
2625 tp->tf_ecx = mcp->mc_ecx;
2626 tp->tf_eax = mcp->mc_eax;
2627 tp->tf_eip = mcp->mc_eip;
2628 tp->tf_eflags = eflags;
2629 tp->tf_esp = mcp->mc_esp;
2630 tp->tf_ss = mcp->mc_ss;
2631 td->td_pcb->pcb_gs = mcp->mc_gs;
2638 get_fpcontext(struct thread *td, mcontext_t *mcp)
2642 mcp->mc_fpformat = _MC_FPFMT_NODEV;
2643 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
2645 mcp->mc_ownedfp = npxgetregs(td);
2646 bcopy(&td->td_pcb->pcb_user_save, &mcp->mc_fpstate,
2647 sizeof(mcp->mc_fpstate));
2648 mcp->mc_fpformat = npxformat();
2653 set_fpcontext(struct thread *td, const mcontext_t *mcp)
2656 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2658 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2659 mcp->mc_fpformat != _MC_FPFMT_XMM)
2661 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
2662 /* We don't care what state is left in the FPU or PCB. */
2664 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2665 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2667 #ifdef CPU_ENABLE_SSE
2669 ((union savefpu *)&mcp->mc_fpstate)->sv_xmm.sv_env.
2670 en_mxcsr &= cpu_mxcsr_mask;
2672 npxsetregs(td, (union savefpu *)&mcp->mc_fpstate);
2680 fpstate_drop(struct thread *td)
2685 if (PCPU_GET(fpcurthread) == td)
2689 * XXX force a full drop of the npx. The above only drops it if we
2690 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
2692 * XXX I don't much like npxgetregs()'s semantics of doing a full
2693 * drop. Dropping only to the pcb matches fnsave's behaviour.
2694 * We only need to drop to !PCB_INITDONE in sendsig(). But
2695 * sendsig() is the only caller of npxgetregs()... perhaps we just
2696 * have too many layers.
2698 curthread->td_pcb->pcb_flags &= ~(PCB_NPXINITDONE |
2699 PCB_NPXUSERINITDONE);
2704 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2709 dbregs->dr[0] = rdr0();
2710 dbregs->dr[1] = rdr1();
2711 dbregs->dr[2] = rdr2();
2712 dbregs->dr[3] = rdr3();
2713 dbregs->dr[4] = rdr4();
2714 dbregs->dr[5] = rdr5();
2715 dbregs->dr[6] = rdr6();
2716 dbregs->dr[7] = rdr7();
2719 dbregs->dr[0] = pcb->pcb_dr0;
2720 dbregs->dr[1] = pcb->pcb_dr1;
2721 dbregs->dr[2] = pcb->pcb_dr2;
2722 dbregs->dr[3] = pcb->pcb_dr3;
2725 dbregs->dr[6] = pcb->pcb_dr6;
2726 dbregs->dr[7] = pcb->pcb_dr7;
2732 set_dbregs(struct thread *td, struct dbreg *dbregs)
2738 load_dr0(dbregs->dr[0]);
2739 load_dr1(dbregs->dr[1]);
2740 load_dr2(dbregs->dr[2]);
2741 load_dr3(dbregs->dr[3]);
2742 load_dr4(dbregs->dr[4]);
2743 load_dr5(dbregs->dr[5]);
2744 load_dr6(dbregs->dr[6]);
2745 load_dr7(dbregs->dr[7]);
2748 * Don't let an illegal value for dr7 get set. Specifically,
2749 * check for undefined settings. Setting these bit patterns
2750 * result in undefined behaviour and can lead to an unexpected
2753 for (i = 0; i < 4; i++) {
2754 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
2756 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
2763 * Don't let a process set a breakpoint that is not within the
2764 * process's address space. If a process could do this, it
2765 * could halt the system by setting a breakpoint in the kernel
2766 * (if ddb was enabled). Thus, we need to check to make sure
2767 * that no breakpoints are being enabled for addresses outside
2768 * process's address space.
2770 * XXX - what about when the watched area of the user's
2771 * address space is written into from within the kernel
2772 * ... wouldn't that still cause a breakpoint to be generated
2773 * from within kernel mode?
2776 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
2777 /* dr0 is enabled */
2778 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2782 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
2783 /* dr1 is enabled */
2784 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2788 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
2789 /* dr2 is enabled */
2790 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2794 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
2795 /* dr3 is enabled */
2796 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2800 pcb->pcb_dr0 = dbregs->dr[0];
2801 pcb->pcb_dr1 = dbregs->dr[1];
2802 pcb->pcb_dr2 = dbregs->dr[2];
2803 pcb->pcb_dr3 = dbregs->dr[3];
2804 pcb->pcb_dr6 = dbregs->dr[6];
2805 pcb->pcb_dr7 = dbregs->dr[7];
2807 pcb->pcb_flags |= PCB_DBREGS;
2814 * Return > 0 if a hardware breakpoint has been hit, and the
2815 * breakpoint was in user space. Return 0, otherwise.
2818 user_dbreg_trap(void)
2820 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2821 u_int32_t bp; /* breakpoint bits extracted from dr6 */
2822 int nbp; /* number of breakpoints that triggered */
2823 caddr_t addr[4]; /* breakpoint addresses */
2827 if ((dr7 & 0x000000ff) == 0) {
2829 * all GE and LE bits in the dr7 register are zero,
2830 * thus the trap couldn't have been caused by the
2831 * hardware debug registers
2838 bp = dr6 & 0x0000000f;
2842 * None of the breakpoint bits are set meaning this
2843 * trap was not caused by any of the debug registers
2849 * at least one of the breakpoints were hit, check to see
2850 * which ones and if any of them are user space addresses
2854 addr[nbp++] = (caddr_t)rdr0();
2857 addr[nbp++] = (caddr_t)rdr1();
2860 addr[nbp++] = (caddr_t)rdr2();
2863 addr[nbp++] = (caddr_t)rdr3();
2866 for (i = 0; i < nbp; i++) {
2867 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
2869 * addr[i] is in user space
2876 * None of the breakpoints are in user space.
2884 * Provide inb() and outb() as functions. They are normally only available as
2885 * inline functions, thus cannot be called from the debugger.
2888 /* silence compiler warnings */
2889 u_char inb_(u_short);
2890 void outb_(u_short, u_char);
2899 outb_(u_short port, u_char data)