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>
63 #include <sys/clock.h>
66 #include <sys/eventhandler.h>
68 #include <sys/imgact.h>
70 #include <sys/kernel.h>
72 #include <sys/linker.h>
74 #include <sys/malloc.h>
75 #include <sys/memrange.h>
76 #include <sys/msgbuf.h>
77 #include <sys/mutex.h>
79 #include <sys/ptrace.h>
80 #include <sys/reboot.h>
81 #include <sys/sched.h>
82 #include <sys/signalvar.h>
83 #include <sys/sysctl.h>
84 #include <sys/sysent.h>
85 #include <sys/sysproto.h>
86 #include <sys/ucontext.h>
87 #include <sys/vmmeter.h>
90 #include <vm/vm_extern.h>
91 #include <vm/vm_kern.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_pager.h>
96 #include <vm/vm_param.h>
100 #error KDB must be enabled in order for DDB to work!
103 #include <ddb/db_sym.h>
106 #include <pc98/pc98/pc98_machdep.h>
108 #include <net/netisr.h>
110 #include <machine/bootinfo.h>
111 #include <machine/clock.h>
112 #include <machine/cpu.h>
113 #include <machine/cputypes.h>
114 #include <machine/intr_machdep.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;
217 * Good {morning,afternoon,evening,night}.
221 panicifcpuunsupported();
225 printf("real memory = %ju (%ju MB)\n", ptoa((uintmax_t)Maxmem),
226 ptoa((uintmax_t)Maxmem) / 1048576);
229 * Display any holes after the first chunk of extended memory.
234 printf("Physical memory chunk(s):\n");
235 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
238 size = phys_avail[indx + 1] - phys_avail[indx];
240 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
241 (uintmax_t)phys_avail[indx],
242 (uintmax_t)phys_avail[indx + 1] - 1,
243 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
247 vm_ksubmap_init(&kmi);
249 printf("avail memory = %ju (%ju MB)\n",
250 ptoa((uintmax_t)cnt.v_free_count),
251 ptoa((uintmax_t)cnt.v_free_count) / 1048576);
254 * Set up buffers, so they can be used to read disk labels.
257 vm_pager_bufferinit();
263 * Send an interrupt to process.
265 * Stack is set up to allow sigcode stored
266 * at top to call routine, followed by kcall
267 * to sigreturn routine below. After sigreturn
268 * resets the signal mask, the stack, and the
269 * frame pointer, it returns to the user
274 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
276 struct osigframe sf, *fp;
280 struct trapframe *regs;
286 PROC_LOCK_ASSERT(p, MA_OWNED);
287 sig = ksi->ksi_signo;
289 mtx_assert(&psp->ps_mtx, MA_OWNED);
291 oonstack = sigonstack(regs->tf_esp);
293 /* Allocate space for the signal handler context. */
294 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
295 SIGISMEMBER(psp->ps_sigonstack, sig)) {
296 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
297 td->td_sigstk.ss_size - sizeof(struct osigframe));
298 #if defined(COMPAT_43)
299 td->td_sigstk.ss_flags |= SS_ONSTACK;
302 fp = (struct osigframe *)regs->tf_esp - 1;
304 /* Translate the signal if appropriate. */
305 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
306 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
308 /* Build the argument list for the signal handler. */
310 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
311 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
312 /* Signal handler installed with SA_SIGINFO. */
313 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
314 sf.sf_siginfo.si_signo = sig;
315 sf.sf_siginfo.si_code = ksi->ksi_code;
316 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
318 /* Old FreeBSD-style arguments. */
319 sf.sf_arg2 = ksi->ksi_code;
320 sf.sf_addr = (register_t)ksi->ksi_addr;
321 sf.sf_ahu.sf_handler = catcher;
323 mtx_unlock(&psp->ps_mtx);
326 /* Save most if not all of trap frame. */
327 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
328 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
329 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
330 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
331 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
332 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
333 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
334 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
335 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
336 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
337 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
338 sf.sf_siginfo.si_sc.sc_gs = rgs();
339 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
341 /* Build the signal context to be used by osigreturn(). */
342 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
343 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
344 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
345 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
346 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
347 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
348 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
349 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
352 * If we're a vm86 process, we want to save the segment registers.
353 * We also change eflags to be our emulated eflags, not the actual
356 if (regs->tf_eflags & PSL_VM) {
357 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
358 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
359 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
361 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
362 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
363 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
364 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
366 if (vm86->vm86_has_vme == 0)
367 sf.sf_siginfo.si_sc.sc_ps =
368 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
369 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
371 /* See sendsig() for comments. */
372 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
376 * Copy the sigframe out to the user's stack.
378 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
380 printf("process %ld has trashed its stack\n", (long)p->p_pid);
386 regs->tf_esp = (int)fp;
387 regs->tf_eip = PS_STRINGS - szosigcode;
388 regs->tf_eflags &= ~(PSL_T | PSL_D);
389 regs->tf_cs = _ucodesel;
390 regs->tf_ds = _udatasel;
391 regs->tf_es = _udatasel;
392 regs->tf_fs = _udatasel;
394 regs->tf_ss = _udatasel;
396 mtx_lock(&psp->ps_mtx);
398 #endif /* COMPAT_43 */
400 #ifdef COMPAT_FREEBSD4
402 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
404 struct sigframe4 sf, *sfp;
408 struct trapframe *regs;
414 PROC_LOCK_ASSERT(p, MA_OWNED);
415 sig = ksi->ksi_signo;
417 mtx_assert(&psp->ps_mtx, MA_OWNED);
419 oonstack = sigonstack(regs->tf_esp);
421 /* Save user context. */
422 bzero(&sf, sizeof(sf));
423 sf.sf_uc.uc_sigmask = *mask;
424 sf.sf_uc.uc_stack = td->td_sigstk;
425 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
426 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
427 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
428 sf.sf_uc.uc_mcontext.mc_gs = rgs();
429 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
431 /* Allocate space for the signal handler context. */
432 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
433 SIGISMEMBER(psp->ps_sigonstack, sig)) {
434 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
435 td->td_sigstk.ss_size - sizeof(struct sigframe4));
436 #if defined(COMPAT_43)
437 td->td_sigstk.ss_flags |= SS_ONSTACK;
440 sfp = (struct sigframe4 *)regs->tf_esp - 1;
442 /* Translate the signal if appropriate. */
443 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
444 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
446 /* Build the argument list for the signal handler. */
448 sf.sf_ucontext = (register_t)&sfp->sf_uc;
449 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
450 /* Signal handler installed with SA_SIGINFO. */
451 sf.sf_siginfo = (register_t)&sfp->sf_si;
452 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
454 /* Fill in POSIX parts */
455 sf.sf_si.si_signo = sig;
456 sf.sf_si.si_code = ksi->ksi_code;
457 sf.sf_si.si_addr = ksi->ksi_addr;
459 /* Old FreeBSD-style arguments. */
460 sf.sf_siginfo = ksi->ksi_code;
461 sf.sf_addr = (register_t)ksi->ksi_addr;
462 sf.sf_ahu.sf_handler = catcher;
464 mtx_unlock(&psp->ps_mtx);
468 * If we're a vm86 process, we want to save the segment registers.
469 * We also change eflags to be our emulated eflags, not the actual
472 if (regs->tf_eflags & PSL_VM) {
473 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
474 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
476 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
477 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
478 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
479 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
481 if (vm86->vm86_has_vme == 0)
482 sf.sf_uc.uc_mcontext.mc_eflags =
483 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
484 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
487 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
488 * syscalls made by the signal handler. This just avoids
489 * wasting time for our lazy fixup of such faults. PSL_NT
490 * does nothing in vm86 mode, but vm86 programs can set it
491 * almost legitimately in probes for old cpu types.
493 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
497 * Copy the sigframe out to the user's stack.
499 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
501 printf("process %ld has trashed its stack\n", (long)p->p_pid);
507 regs->tf_esp = (int)sfp;
508 regs->tf_eip = PS_STRINGS - szfreebsd4_sigcode;
509 regs->tf_eflags &= ~(PSL_T | PSL_D);
510 regs->tf_cs = _ucodesel;
511 regs->tf_ds = _udatasel;
512 regs->tf_es = _udatasel;
513 regs->tf_fs = _udatasel;
514 regs->tf_ss = _udatasel;
516 mtx_lock(&psp->ps_mtx);
518 #endif /* COMPAT_FREEBSD4 */
521 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
523 struct sigframe sf, *sfp;
528 struct trapframe *regs;
534 PROC_LOCK_ASSERT(p, MA_OWNED);
535 sig = ksi->ksi_signo;
537 mtx_assert(&psp->ps_mtx, MA_OWNED);
538 #ifdef COMPAT_FREEBSD4
539 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
540 freebsd4_sendsig(catcher, ksi, mask);
545 if (SIGISMEMBER(psp->ps_osigset, sig)) {
546 osendsig(catcher, ksi, mask);
551 oonstack = sigonstack(regs->tf_esp);
553 /* Save user context. */
554 bzero(&sf, sizeof(sf));
555 sf.sf_uc.uc_sigmask = *mask;
556 sf.sf_uc.uc_stack = td->td_sigstk;
557 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
558 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
559 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
560 sf.sf_uc.uc_mcontext.mc_gs = rgs();
561 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
562 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
563 get_fpcontext(td, &sf.sf_uc.uc_mcontext);
566 /* Allocate space for the signal handler context. */
567 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
568 SIGISMEMBER(psp->ps_sigonstack, sig)) {
569 sp = td->td_sigstk.ss_sp +
570 td->td_sigstk.ss_size - sizeof(struct sigframe);
571 #if defined(COMPAT_43)
572 td->td_sigstk.ss_flags |= SS_ONSTACK;
575 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
576 /* Align to 16 bytes. */
577 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
579 /* Translate the signal if appropriate. */
580 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
581 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
583 /* Build the argument list for the signal handler. */
585 sf.sf_ucontext = (register_t)&sfp->sf_uc;
586 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
587 /* Signal handler installed with SA_SIGINFO. */
588 sf.sf_siginfo = (register_t)&sfp->sf_si;
589 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
591 /* Fill in POSIX parts */
592 sf.sf_si = ksi->ksi_info;
593 sf.sf_si.si_signo = sig; /* maybe a translated signal */
595 /* Old FreeBSD-style arguments. */
596 sf.sf_siginfo = ksi->ksi_code;
597 sf.sf_addr = (register_t)ksi->ksi_addr;
598 sf.sf_ahu.sf_handler = catcher;
600 mtx_unlock(&psp->ps_mtx);
604 * If we're a vm86 process, we want to save the segment registers.
605 * We also change eflags to be our emulated eflags, not the actual
608 if (regs->tf_eflags & PSL_VM) {
609 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
610 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
612 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
613 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
614 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
615 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
617 if (vm86->vm86_has_vme == 0)
618 sf.sf_uc.uc_mcontext.mc_eflags =
619 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
620 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
623 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
624 * syscalls made by the signal handler. This just avoids
625 * wasting time for our lazy fixup of such faults. PSL_NT
626 * does nothing in vm86 mode, but vm86 programs can set it
627 * almost legitimately in probes for old cpu types.
629 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
633 * Copy the sigframe out to the user's stack.
635 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
637 printf("process %ld has trashed its stack\n", (long)p->p_pid);
643 regs->tf_esp = (int)sfp;
644 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
645 regs->tf_eflags &= ~(PSL_T | PSL_D);
646 regs->tf_cs = _ucodesel;
647 regs->tf_ds = _udatasel;
648 regs->tf_es = _udatasel;
649 regs->tf_fs = _udatasel;
650 regs->tf_ss = _udatasel;
652 mtx_lock(&psp->ps_mtx);
656 * System call to cleanup state after a signal
657 * has been taken. Reset signal mask and
658 * stack state from context left by sendsig (above).
659 * Return to previous pc and psl as specified by
660 * context left by sendsig. Check carefully to
661 * make sure that the user has not modified the
662 * state to gain improper privileges.
670 struct osigreturn_args /* {
671 struct osigcontext *sigcntxp;
674 struct osigcontext sc;
675 struct trapframe *regs;
676 struct osigcontext *scp;
677 struct proc *p = td->td_proc;
682 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
687 if (eflags & PSL_VM) {
688 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
689 struct vm86_kernel *vm86;
692 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
693 * set up the vm86 area, and we can't enter vm86 mode.
695 if (td->td_pcb->pcb_ext == 0)
697 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
698 if (vm86->vm86_inited == 0)
701 /* Go back to user mode if both flags are set. */
702 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
703 ksiginfo_init_trap(&ksi);
704 ksi.ksi_signo = SIGBUS;
705 ksi.ksi_code = BUS_OBJERR;
706 ksi.ksi_addr = (void *)regs->tf_eip;
707 trapsignal(td, &ksi);
710 if (vm86->vm86_has_vme) {
711 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
712 (eflags & VME_USERCHANGE) | PSL_VM;
714 vm86->vm86_eflags = eflags; /* save VIF, VIP */
715 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
716 (eflags & VM_USERCHANGE) | PSL_VM;
718 tf->tf_vm86_ds = scp->sc_ds;
719 tf->tf_vm86_es = scp->sc_es;
720 tf->tf_vm86_fs = scp->sc_fs;
721 tf->tf_vm86_gs = scp->sc_gs;
722 tf->tf_ds = _udatasel;
723 tf->tf_es = _udatasel;
724 tf->tf_fs = _udatasel;
727 * Don't allow users to change privileged or reserved flags.
730 * XXX do allow users to change the privileged flag PSL_RF.
731 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
732 * should sometimes set it there too. tf_eflags is kept in
733 * the signal context during signal handling and there is no
734 * other place to remember it, so the PSL_RF bit may be
735 * corrupted by the signal handler without us knowing.
736 * Corruption of the PSL_RF bit at worst causes one more or
737 * one less debugger trap, so allowing it is fairly harmless.
739 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
744 * Don't allow users to load a valid privileged %cs. Let the
745 * hardware check for invalid selectors, excess privilege in
746 * other selectors, invalid %eip's and invalid %esp's.
748 if (!CS_SECURE(scp->sc_cs)) {
749 ksiginfo_init_trap(&ksi);
750 ksi.ksi_signo = SIGBUS;
751 ksi.ksi_code = BUS_OBJERR;
752 ksi.ksi_trapno = T_PROTFLT;
753 ksi.ksi_addr = (void *)regs->tf_eip;
754 trapsignal(td, &ksi);
757 regs->tf_ds = scp->sc_ds;
758 regs->tf_es = scp->sc_es;
759 regs->tf_fs = scp->sc_fs;
762 /* Restore remaining registers. */
763 regs->tf_eax = scp->sc_eax;
764 regs->tf_ebx = scp->sc_ebx;
765 regs->tf_ecx = scp->sc_ecx;
766 regs->tf_edx = scp->sc_edx;
767 regs->tf_esi = scp->sc_esi;
768 regs->tf_edi = scp->sc_edi;
769 regs->tf_cs = scp->sc_cs;
770 regs->tf_ss = scp->sc_ss;
771 regs->tf_isp = scp->sc_isp;
772 regs->tf_ebp = scp->sc_fp;
773 regs->tf_esp = scp->sc_sp;
774 regs->tf_eip = scp->sc_pc;
775 regs->tf_eflags = eflags;
778 #if defined(COMPAT_43)
779 if (scp->sc_onstack & 1)
780 td->td_sigstk.ss_flags |= SS_ONSTACK;
782 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
784 SIGSETOLD(td->td_sigmask, scp->sc_mask);
785 SIG_CANTMASK(td->td_sigmask);
788 return (EJUSTRETURN);
790 #endif /* COMPAT_43 */
792 #ifdef COMPAT_FREEBSD4
797 freebsd4_sigreturn(td, uap)
799 struct freebsd4_sigreturn_args /* {
800 const ucontext4 *sigcntxp;
804 struct proc *p = td->td_proc;
805 struct trapframe *regs;
806 const struct ucontext4 *ucp;
807 int cs, eflags, error;
810 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
815 eflags = ucp->uc_mcontext.mc_eflags;
816 if (eflags & PSL_VM) {
817 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
818 struct vm86_kernel *vm86;
821 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
822 * set up the vm86 area, and we can't enter vm86 mode.
824 if (td->td_pcb->pcb_ext == 0)
826 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
827 if (vm86->vm86_inited == 0)
830 /* Go back to user mode if both flags are set. */
831 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
832 ksiginfo_init_trap(&ksi);
833 ksi.ksi_signo = SIGBUS;
834 ksi.ksi_code = BUS_OBJERR;
835 ksi.ksi_addr = (void *)regs->tf_eip;
836 trapsignal(td, &ksi);
838 if (vm86->vm86_has_vme) {
839 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
840 (eflags & VME_USERCHANGE) | PSL_VM;
842 vm86->vm86_eflags = eflags; /* save VIF, VIP */
843 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
844 (eflags & VM_USERCHANGE) | PSL_VM;
846 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
847 tf->tf_eflags = eflags;
848 tf->tf_vm86_ds = tf->tf_ds;
849 tf->tf_vm86_es = tf->tf_es;
850 tf->tf_vm86_fs = tf->tf_fs;
851 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
852 tf->tf_ds = _udatasel;
853 tf->tf_es = _udatasel;
854 tf->tf_fs = _udatasel;
857 * Don't allow users to change privileged or reserved flags.
860 * XXX do allow users to change the privileged flag PSL_RF.
861 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
862 * should sometimes set it there too. tf_eflags is kept in
863 * the signal context during signal handling and there is no
864 * other place to remember it, so the PSL_RF bit may be
865 * corrupted by the signal handler without us knowing.
866 * Corruption of the PSL_RF bit at worst causes one more or
867 * one less debugger trap, so allowing it is fairly harmless.
869 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
870 printf("freebsd4_sigreturn: eflags = 0x%x\n", eflags);
875 * Don't allow users to load a valid privileged %cs. Let the
876 * hardware check for invalid selectors, excess privilege in
877 * other selectors, invalid %eip's and invalid %esp's.
879 cs = ucp->uc_mcontext.mc_cs;
880 if (!CS_SECURE(cs)) {
881 printf("freebsd4_sigreturn: cs = 0x%x\n", cs);
882 ksiginfo_init_trap(&ksi);
883 ksi.ksi_signo = SIGBUS;
884 ksi.ksi_code = BUS_OBJERR;
885 ksi.ksi_trapno = T_PROTFLT;
886 ksi.ksi_addr = (void *)regs->tf_eip;
887 trapsignal(td, &ksi);
891 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
895 #if defined(COMPAT_43)
896 if (ucp->uc_mcontext.mc_onstack & 1)
897 td->td_sigstk.ss_flags |= SS_ONSTACK;
899 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
902 td->td_sigmask = ucp->uc_sigmask;
903 SIG_CANTMASK(td->td_sigmask);
906 return (EJUSTRETURN);
908 #endif /* COMPAT_FREEBSD4 */
916 struct sigreturn_args /* {
917 const struct __ucontext *sigcntxp;
921 struct proc *p = td->td_proc;
922 struct trapframe *regs;
923 const ucontext_t *ucp;
924 int cs, eflags, error, ret;
927 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
932 eflags = ucp->uc_mcontext.mc_eflags;
933 if (eflags & PSL_VM) {
934 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
935 struct vm86_kernel *vm86;
938 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
939 * set up the vm86 area, and we can't enter vm86 mode.
941 if (td->td_pcb->pcb_ext == 0)
943 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
944 if (vm86->vm86_inited == 0)
947 /* Go back to user mode if both flags are set. */
948 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
949 ksiginfo_init_trap(&ksi);
950 ksi.ksi_signo = SIGBUS;
951 ksi.ksi_code = BUS_OBJERR;
952 ksi.ksi_addr = (void *)regs->tf_eip;
953 trapsignal(td, &ksi);
956 if (vm86->vm86_has_vme) {
957 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
958 (eflags & VME_USERCHANGE) | PSL_VM;
960 vm86->vm86_eflags = eflags; /* save VIF, VIP */
961 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
962 (eflags & VM_USERCHANGE) | PSL_VM;
964 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
965 tf->tf_eflags = eflags;
966 tf->tf_vm86_ds = tf->tf_ds;
967 tf->tf_vm86_es = tf->tf_es;
968 tf->tf_vm86_fs = tf->tf_fs;
969 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
970 tf->tf_ds = _udatasel;
971 tf->tf_es = _udatasel;
972 tf->tf_fs = _udatasel;
975 * Don't allow users to change privileged or reserved flags.
978 * XXX do allow users to change the privileged flag PSL_RF.
979 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
980 * should sometimes set it there too. tf_eflags is kept in
981 * the signal context during signal handling and there is no
982 * other place to remember it, so the PSL_RF bit may be
983 * corrupted by the signal handler without us knowing.
984 * Corruption of the PSL_RF bit at worst causes one more or
985 * one less debugger trap, so allowing it is fairly harmless.
987 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
988 printf("sigreturn: eflags = 0x%x\n", eflags);
993 * Don't allow users to load a valid privileged %cs. Let the
994 * hardware check for invalid selectors, excess privilege in
995 * other selectors, invalid %eip's and invalid %esp's.
997 cs = ucp->uc_mcontext.mc_cs;
998 if (!CS_SECURE(cs)) {
999 printf("sigreturn: cs = 0x%x\n", cs);
1000 ksiginfo_init_trap(&ksi);
1001 ksi.ksi_signo = SIGBUS;
1002 ksi.ksi_code = BUS_OBJERR;
1003 ksi.ksi_trapno = T_PROTFLT;
1004 ksi.ksi_addr = (void *)regs->tf_eip;
1005 trapsignal(td, &ksi);
1009 ret = set_fpcontext(td, &ucp->uc_mcontext);
1012 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1016 #if defined(COMPAT_43)
1017 if (ucp->uc_mcontext.mc_onstack & 1)
1018 td->td_sigstk.ss_flags |= SS_ONSTACK;
1020 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1023 td->td_sigmask = ucp->uc_sigmask;
1024 SIG_CANTMASK(td->td_sigmask);
1027 return (EJUSTRETURN);
1031 * Machine dependent boot() routine
1033 * I haven't seen anything to put here yet
1034 * Possibly some stuff might be grafted back here from boot()
1041 /* Get current clock frequency for the given cpu id. */
1043 cpu_est_clockrate(int cpu_id, uint64_t *rate)
1046 uint64_t tsc1, tsc2;
1048 if (pcpu_find(cpu_id) == NULL || rate == NULL)
1051 return (EOPNOTSUPP);
1053 /* If we're booting, trust the rate calibrated moments ago. */
1060 /* Schedule ourselves on the indicated cpu. */
1061 thread_lock(curthread);
1062 sched_bind(curthread, cpu_id);
1063 thread_unlock(curthread);
1066 /* Calibrate by measuring a short delay. */
1067 reg = intr_disable();
1074 thread_lock(curthread);
1075 sched_unbind(curthread);
1076 thread_unlock(curthread);
1080 * Calculate the difference in readings, convert to Mhz, and
1081 * subtract 0.5% of the total. Empirical testing has shown that
1082 * overhead in DELAY() works out to approximately this value.
1085 *rate = tsc2 * 1000 - tsc2 * 5;
1090 * Shutdown the CPU as much as possible
1100 * Hook to idle the CPU when possible. In the SMP case we default to
1101 * off because a halted cpu will not currently pick up a new thread in the
1102 * run queue until the next timer tick. If turned on this will result in
1103 * approximately a 4.2% loss in real time performance in buildworld tests
1104 * (but improves user and sys times oddly enough), and saves approximately
1105 * 5% in power consumption on an idle machine (tests w/2xCPU 1.1GHz P3).
1107 * XXX we need to have a cpu mask of idle cpus and generate an IPI or
1108 * otherwise generate some sort of interrupt to wake up cpus sitting in HLT.
1109 * Then we can have our cake and eat it too.
1111 * XXX I'm turning it on for SMP as well by default for now. It seems to
1112 * help lock contention somewhat, and this is critical for HTT. -Peter
1114 static int cpu_idle_hlt = 1;
1115 TUNABLE_INT("machdep.cpu_idle_hlt", &cpu_idle_hlt);
1116 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
1117 &cpu_idle_hlt, 0, "Idle loop HLT enable");
1120 cpu_idle_default(void)
1123 * we must absolutely guarentee that hlt is the
1124 * absolute next instruction after sti or we
1125 * introduce a timing window.
1127 __asm __volatile("sti; hlt");
1131 * Note that we have to be careful here to avoid a race between checking
1132 * sched_runnable() and actually halting. If we don't do this, we may waste
1133 * the time between calling hlt and the next interrupt even though there
1134 * is a runnable process.
1141 if (mp_grab_cpu_hlt())
1147 if (sched_runnable())
1154 /* Other subsystems (e.g., ACPI) can hook this later. */
1155 void (*cpu_idle_hook)(void) = cpu_idle_default;
1158 * Reset registers to default values on exec.
1161 exec_setregs(td, entry, stack, ps_strings)
1167 struct trapframe *regs = td->td_frame;
1168 struct pcb *pcb = td->td_pcb;
1170 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1171 pcb->pcb_gs = _udatasel;
1174 mtx_lock_spin(&dt_lock);
1175 if (td->td_proc->p_md.md_ldt)
1178 mtx_unlock_spin(&dt_lock);
1180 bzero((char *)regs, sizeof(struct trapframe));
1181 regs->tf_eip = entry;
1182 regs->tf_esp = stack;
1183 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1184 regs->tf_ss = _udatasel;
1185 regs->tf_ds = _udatasel;
1186 regs->tf_es = _udatasel;
1187 regs->tf_fs = _udatasel;
1188 regs->tf_cs = _ucodesel;
1190 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1191 regs->tf_ebx = ps_strings;
1194 * Reset the hardware debug registers if they were in use.
1195 * They won't have any meaning for the newly exec'd process.
1197 if (pcb->pcb_flags & PCB_DBREGS) {
1204 if (pcb == PCPU_GET(curpcb)) {
1206 * Clear the debug registers on the running
1207 * CPU, otherwise they will end up affecting
1208 * the next process we switch to.
1212 pcb->pcb_flags &= ~PCB_DBREGS;
1216 * Initialize the math emulator (if any) for the current process.
1217 * Actually, just clear the bit that says that the emulator has
1218 * been initialized. Initialization is delayed until the process
1219 * traps to the emulator (if it is done at all) mainly because
1220 * emulators don't provide an entry point for initialization.
1222 td->td_pcb->pcb_flags &= ~FP_SOFTFP;
1223 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1226 * Drop the FP state if we hold it, so that the process gets a
1227 * clean FP state if it uses the FPU again.
1232 * XXX - Linux emulator
1233 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1236 td->td_retval[1] = 0;
1247 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1249 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1250 * instructions. We must set the CR0_MP bit and use the CR0_TS
1251 * bit to control the trap, because setting the CR0_EM bit does
1252 * not cause WAIT instructions to trap. It's important to trap
1253 * WAIT instructions - otherwise the "wait" variants of no-wait
1254 * control instructions would degenerate to the "no-wait" variants
1255 * after FP context switches but work correctly otherwise. It's
1256 * particularly important to trap WAITs when there is no NPX -
1257 * otherwise the "wait" variants would always degenerate.
1259 * Try setting CR0_NE to get correct error reporting on 486DX's.
1260 * Setting it should fail or do nothing on lesser processors.
1262 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1267 u_long bootdev; /* not a struct cdev *- encoding is different */
1268 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1269 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1272 * Initialize 386 and configure to run kernel
1276 * Initialize segments & interrupt table
1280 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1281 static struct gate_descriptor idt0[NIDT];
1282 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1283 union descriptor ldt[NLDT]; /* local descriptor table */
1284 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1285 struct mtx dt_lock; /* lock for GDT and LDT */
1287 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1288 extern int has_f00f_bug;
1291 static struct i386tss dblfault_tss;
1292 static char dblfault_stack[PAGE_SIZE];
1294 extern vm_offset_t proc0kstack;
1298 * software prototypes -- in more palatable form.
1300 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1301 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1303 struct soft_segment_descriptor gdt_segs[] = {
1304 /* GNULL_SEL 0 Null Descriptor */
1305 { 0x0, /* segment base address */
1307 0, /* segment type */
1308 0, /* segment descriptor priority level */
1309 0, /* segment descriptor present */
1311 0, /* default 32 vs 16 bit size */
1312 0 /* limit granularity (byte/page units)*/ },
1313 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1314 { 0x0, /* segment base address */
1315 0xfffff, /* length - all address space */
1316 SDT_MEMRWA, /* segment type */
1317 0, /* segment descriptor priority level */
1318 1, /* segment descriptor present */
1320 1, /* default 32 vs 16 bit size */
1321 1 /* limit granularity (byte/page units)*/ },
1322 /* GUFS_SEL 2 %fs Descriptor for user */
1323 { 0x0, /* segment base address */
1324 0xfffff, /* length - all address space */
1325 SDT_MEMRWA, /* segment type */
1326 SEL_UPL, /* segment descriptor priority level */
1327 1, /* segment descriptor present */
1329 1, /* default 32 vs 16 bit size */
1330 1 /* limit granularity (byte/page units)*/ },
1331 /* GUGS_SEL 3 %gs Descriptor for user */
1332 { 0x0, /* segment base address */
1333 0xfffff, /* length - all address space */
1334 SDT_MEMRWA, /* segment type */
1335 SEL_UPL, /* segment descriptor priority level */
1336 1, /* segment descriptor present */
1338 1, /* default 32 vs 16 bit size */
1339 1 /* limit granularity (byte/page units)*/ },
1340 /* GCODE_SEL 4 Code Descriptor for kernel */
1341 { 0x0, /* segment base address */
1342 0xfffff, /* length - all address space */
1343 SDT_MEMERA, /* segment type */
1344 0, /* segment descriptor priority level */
1345 1, /* segment descriptor present */
1347 1, /* default 32 vs 16 bit size */
1348 1 /* limit granularity (byte/page units)*/ },
1349 /* GDATA_SEL 5 Data Descriptor for kernel */
1350 { 0x0, /* segment base address */
1351 0xfffff, /* length - all address space */
1352 SDT_MEMRWA, /* segment type */
1353 0, /* segment descriptor priority level */
1354 1, /* segment descriptor present */
1356 1, /* default 32 vs 16 bit size */
1357 1 /* limit granularity (byte/page units)*/ },
1358 /* GUCODE_SEL 6 Code Descriptor for user */
1359 { 0x0, /* segment base address */
1360 0xfffff, /* length - all address space */
1361 SDT_MEMERA, /* segment type */
1362 SEL_UPL, /* segment descriptor priority level */
1363 1, /* segment descriptor present */
1365 1, /* default 32 vs 16 bit size */
1366 1 /* limit granularity (byte/page units)*/ },
1367 /* GUDATA_SEL 7 Data Descriptor for user */
1368 { 0x0, /* segment base address */
1369 0xfffff, /* length - all address space */
1370 SDT_MEMRWA, /* segment type */
1371 SEL_UPL, /* segment descriptor priority level */
1372 1, /* segment descriptor present */
1374 1, /* default 32 vs 16 bit size */
1375 1 /* limit granularity (byte/page units)*/ },
1376 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1377 { 0x400, /* segment base address */
1378 0xfffff, /* length */
1379 SDT_MEMRWA, /* segment type */
1380 0, /* segment descriptor priority level */
1381 1, /* segment descriptor present */
1383 1, /* default 32 vs 16 bit size */
1384 1 /* limit granularity (byte/page units)*/ },
1385 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1387 0x0, /* segment base address */
1388 sizeof(struct i386tss)-1,/* length */
1389 SDT_SYS386TSS, /* segment type */
1390 0, /* segment descriptor priority level */
1391 1, /* segment descriptor present */
1393 0, /* unused - default 32 vs 16 bit size */
1394 0 /* limit granularity (byte/page units)*/ },
1395 /* GLDT_SEL 10 LDT Descriptor */
1396 { (int) ldt, /* segment base address */
1397 sizeof(ldt)-1, /* length - all address space */
1398 SDT_SYSLDT, /* segment type */
1399 SEL_UPL, /* segment descriptor priority level */
1400 1, /* segment descriptor present */
1402 0, /* unused - default 32 vs 16 bit size */
1403 0 /* limit granularity (byte/page units)*/ },
1404 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1405 { (int) ldt, /* segment base address */
1406 (512 * sizeof(union descriptor)-1), /* length */
1407 SDT_SYSLDT, /* segment type */
1408 0, /* segment descriptor priority level */
1409 1, /* segment descriptor present */
1411 0, /* unused - default 32 vs 16 bit size */
1412 0 /* limit granularity (byte/page units)*/ },
1413 /* GPANIC_SEL 12 Panic Tss Descriptor */
1414 { (int) &dblfault_tss, /* segment base address */
1415 sizeof(struct i386tss)-1,/* length - all address space */
1416 SDT_SYS386TSS, /* segment type */
1417 0, /* segment descriptor priority level */
1418 1, /* segment descriptor present */
1420 0, /* unused - default 32 vs 16 bit size */
1421 0 /* limit granularity (byte/page units)*/ },
1422 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1423 { 0, /* segment base address (overwritten) */
1424 0xfffff, /* length */
1425 SDT_MEMERA, /* segment type */
1426 0, /* segment descriptor priority level */
1427 1, /* segment descriptor present */
1429 0, /* default 32 vs 16 bit size */
1430 1 /* limit granularity (byte/page units)*/ },
1431 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1432 { 0, /* segment base address (overwritten) */
1433 0xfffff, /* length */
1434 SDT_MEMERA, /* segment type */
1435 0, /* segment descriptor priority level */
1436 1, /* segment descriptor present */
1438 0, /* default 32 vs 16 bit size */
1439 1 /* limit granularity (byte/page units)*/ },
1440 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1441 { 0, /* segment base address (overwritten) */
1442 0xfffff, /* length */
1443 SDT_MEMRWA, /* segment type */
1444 0, /* segment descriptor priority level */
1445 1, /* segment descriptor present */
1447 1, /* default 32 vs 16 bit size */
1448 1 /* limit granularity (byte/page units)*/ },
1449 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1450 { 0, /* segment base address (overwritten) */
1451 0xfffff, /* length */
1452 SDT_MEMRWA, /* segment type */
1453 0, /* segment descriptor priority level */
1454 1, /* segment descriptor present */
1456 0, /* default 32 vs 16 bit size */
1457 1 /* limit granularity (byte/page units)*/ },
1458 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1459 { 0, /* segment base address (overwritten) */
1460 0xfffff, /* length */
1461 SDT_MEMRWA, /* segment type */
1462 0, /* segment descriptor priority level */
1463 1, /* segment descriptor present */
1465 0, /* default 32 vs 16 bit size */
1466 1 /* limit granularity (byte/page units)*/ },
1467 /* GNDIS_SEL 18 NDIS Descriptor */
1468 { 0x0, /* segment base address */
1470 0, /* segment type */
1471 0, /* segment descriptor priority level */
1472 0, /* segment descriptor present */
1474 0, /* default 32 vs 16 bit size */
1475 0 /* limit granularity (byte/page units)*/ },
1478 static struct soft_segment_descriptor ldt_segs[] = {
1479 /* Null Descriptor - overwritten by call gate */
1480 { 0x0, /* segment base address */
1481 0x0, /* length - all address space */
1482 0, /* segment type */
1483 0, /* segment descriptor priority level */
1484 0, /* segment descriptor present */
1486 0, /* default 32 vs 16 bit size */
1487 0 /* limit granularity (byte/page units)*/ },
1488 /* Null Descriptor - overwritten by call gate */
1489 { 0x0, /* segment base address */
1490 0x0, /* length - all address space */
1491 0, /* segment type */
1492 0, /* segment descriptor priority level */
1493 0, /* segment descriptor present */
1495 0, /* default 32 vs 16 bit size */
1496 0 /* limit granularity (byte/page units)*/ },
1497 /* Null Descriptor - overwritten by call gate */
1498 { 0x0, /* segment base address */
1499 0x0, /* length - all address space */
1500 0, /* segment type */
1501 0, /* segment descriptor priority level */
1502 0, /* segment descriptor present */
1504 0, /* default 32 vs 16 bit size */
1505 0 /* limit granularity (byte/page units)*/ },
1506 /* Code Descriptor for user */
1507 { 0x0, /* segment base address */
1508 0xfffff, /* length - all address space */
1509 SDT_MEMERA, /* segment type */
1510 SEL_UPL, /* segment descriptor priority level */
1511 1, /* segment descriptor present */
1513 1, /* default 32 vs 16 bit size */
1514 1 /* limit granularity (byte/page units)*/ },
1515 /* Null Descriptor - overwritten by call gate */
1516 { 0x0, /* segment base address */
1517 0x0, /* length - all address space */
1518 0, /* segment type */
1519 0, /* segment descriptor priority level */
1520 0, /* segment descriptor present */
1522 0, /* default 32 vs 16 bit size */
1523 0 /* limit granularity (byte/page units)*/ },
1524 /* Data Descriptor for user */
1525 { 0x0, /* segment base address */
1526 0xfffff, /* length - all address space */
1527 SDT_MEMRWA, /* segment type */
1528 SEL_UPL, /* segment descriptor priority level */
1529 1, /* segment descriptor present */
1531 1, /* default 32 vs 16 bit size */
1532 1 /* limit granularity (byte/page units)*/ },
1536 setidt(idx, func, typ, dpl, selec)
1543 struct gate_descriptor *ip;
1546 ip->gd_looffset = (int)func;
1547 ip->gd_selector = selec;
1553 ip->gd_hioffset = ((int)func)>>16 ;
1557 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1558 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1559 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1560 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1561 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1565 * Display the index and function name of any IDT entries that don't use
1566 * the default 'rsvd' entry point.
1568 DB_SHOW_COMMAND(idt, db_show_idt)
1570 struct gate_descriptor *ip;
1575 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
1576 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1577 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1578 db_printf("%3d\t", idx);
1579 db_printsym(func, DB_STGY_PROC);
1586 /* Show privileged registers. */
1587 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
1589 uint64_t idtr, gdtr;
1592 db_printf("idtr\t0x%08x/%04x\n",
1593 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
1595 db_printf("gdtr\t0x%08x/%04x\n",
1596 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
1597 db_printf("ldtr\t0x%04x\n", rldt());
1598 db_printf("tr\t0x%04x\n", rtr());
1599 db_printf("cr0\t0x%08x\n", rcr0());
1600 db_printf("cr2\t0x%08x\n", rcr2());
1601 db_printf("cr3\t0x%08x\n", rcr3());
1602 db_printf("cr4\t0x%08x\n", rcr4());
1608 struct segment_descriptor *sd;
1609 struct soft_segment_descriptor *ssd;
1611 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1612 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1613 ssd->ssd_type = sd->sd_type;
1614 ssd->ssd_dpl = sd->sd_dpl;
1615 ssd->ssd_p = sd->sd_p;
1616 ssd->ssd_def32 = sd->sd_def32;
1617 ssd->ssd_gran = sd->sd_gran;
1621 * Populate the (physmap) array with base/bound pairs describing the
1622 * available physical memory in the system, then test this memory and
1623 * build the phys_avail array describing the actually-available memory.
1625 * If we cannot accurately determine the physical memory map, then use
1626 * value from the 0xE801 call, and failing that, the RTC.
1628 * Total memory size may be set by the kernel environment variable
1629 * hw.physmem or the compile-time define MAXMEM.
1631 * XXX first should be vm_paddr_t.
1634 getmemsize(int first)
1636 int i, off, physmap_idx, pa_indx, da_indx;
1638 u_long physmem_tunable;
1639 u_int extmem, under16;
1640 vm_paddr_t pa, physmap[PHYSMAP_SIZE];
1642 quad_t dcons_addr, dcons_size;
1644 bzero(physmap, sizeof(physmap));
1646 /* XXX - some of EPSON machines can't use PG_N */
1648 if (pc98_machine_type & M_EPSON_PC98) {
1649 switch (epson_machine_id) {
1653 case EPSON_PC486_HX:
1654 case EPSON_PC486_HG:
1655 case EPSON_PC486_HA:
1662 * Perform "base memory" related probes & setup
1664 under16 = pc98_getmemsize(&basemem, &extmem);
1665 if (basemem > 640) {
1666 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1672 * XXX if biosbasemem is now < 640, there is a `hole'
1673 * between the end of base memory and the start of
1674 * ISA memory. The hole may be empty or it may
1675 * contain BIOS code or data. Map it read/write so
1676 * that the BIOS can write to it. (Memory from 0 to
1677 * the physical end of the kernel is mapped read-only
1678 * to begin with and then parts of it are remapped.
1679 * The parts that aren't remapped form holes that
1680 * remain read-only and are unused by the kernel.
1681 * The base memory area is below the physical end of
1682 * the kernel and right now forms a read-only hole.
1683 * The part of it from PAGE_SIZE to
1684 * (trunc_page(biosbasemem * 1024) - 1) will be
1685 * remapped and used by the kernel later.)
1687 * This code is similar to the code used in
1688 * pmap_mapdev, but since no memory needs to be
1689 * allocated we simply change the mapping.
1691 for (pa = trunc_page(basemem * 1024);
1692 pa < ISA_HOLE_START; pa += PAGE_SIZE)
1693 pmap_kenter(KERNBASE + pa, pa);
1696 * if basemem != 640, map pages r/w into vm86 page table so
1697 * that the bios can scribble on it.
1699 pte = (pt_entry_t *)vm86paddr;
1700 for (i = basemem / 4; i < 160; i++)
1701 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1704 physmap[1] = basemem * 1024;
1706 physmap[physmap_idx] = 0x100000;
1707 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1710 * Now, physmap contains a map of physical memory.
1714 /* make hole for AP bootstrap code */
1715 physmap[1] = mp_bootaddress(physmap[1]);
1719 * Maxmem isn't the "maximum memory", it's one larger than the
1720 * highest page of the physical address space. It should be
1721 * called something like "Maxphyspage". We may adjust this
1722 * based on ``hw.physmem'' and the results of the memory test.
1724 Maxmem = atop(physmap[physmap_idx + 1]);
1727 Maxmem = MAXMEM / 4;
1730 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
1731 Maxmem = atop(physmem_tunable);
1733 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1734 (boothowto & RB_VERBOSE))
1735 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1738 * If Maxmem has been increased beyond what the system has detected,
1739 * extend the last memory segment to the new limit.
1741 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1742 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1745 * We need to divide chunk if Maxmem is larger than 16MB and
1746 * under 16MB area is not full of memory.
1747 * (1) system area (15-16MB region) is cut off
1748 * (2) extended memory is only over 16MB area (ex. Melco "HYPERMEMORY")
1750 if ((under16 != 16 * 1024) && (extmem > 15 * 1024)) {
1751 /* 15M - 16M region is cut off, so need to divide chunk */
1752 physmap[physmap_idx + 1] = under16 * 1024;
1754 physmap[physmap_idx] = 0x1000000;
1755 physmap[physmap_idx + 1] = physmap[2] + extmem * 1024;
1758 /* call pmap initialization to make new kernel address space */
1759 pmap_bootstrap(first);
1762 * Size up each available chunk of physical memory.
1764 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1767 phys_avail[pa_indx++] = physmap[0];
1768 phys_avail[pa_indx] = physmap[0];
1769 dump_avail[da_indx] = physmap[0];
1773 * Get dcons buffer address
1775 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
1776 getenv_quad("dcons.size", &dcons_size) == 0)
1780 * physmap is in bytes, so when converting to page boundaries,
1781 * round up the start address and round down the end address.
1783 for (i = 0; i <= physmap_idx; i += 2) {
1786 end = ptoa((vm_paddr_t)Maxmem);
1787 if (physmap[i + 1] < end)
1788 end = trunc_page(physmap[i + 1]);
1789 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1790 int tmp, page_bad, full;
1791 int *ptr = (int *)CADDR1;
1795 * block out kernel memory as not available.
1797 if (pa >= KERNLOAD && pa < first)
1801 * block out dcons buffer
1804 && pa >= trunc_page(dcons_addr)
1805 && pa < dcons_addr + dcons_size)
1811 * map page into kernel: valid, read/write,non-cacheable
1813 *pte = pa | PG_V | PG_RW | pg_n;
1818 * Test for alternating 1's and 0's
1820 *(volatile int *)ptr = 0xaaaaaaaa;
1821 if (*(volatile int *)ptr != 0xaaaaaaaa)
1824 * Test for alternating 0's and 1's
1826 *(volatile int *)ptr = 0x55555555;
1827 if (*(volatile int *)ptr != 0x55555555)
1832 *(volatile int *)ptr = 0xffffffff;
1833 if (*(volatile int *)ptr != 0xffffffff)
1838 *(volatile int *)ptr = 0x0;
1839 if (*(volatile int *)ptr != 0x0)
1842 * Restore original value.
1847 * Adjust array of valid/good pages.
1849 if (page_bad == TRUE)
1852 * If this good page is a continuation of the
1853 * previous set of good pages, then just increase
1854 * the end pointer. Otherwise start a new chunk.
1855 * Note that "end" points one higher than end,
1856 * making the range >= start and < end.
1857 * If we're also doing a speculative memory
1858 * test and we at or past the end, bump up Maxmem
1859 * so that we keep going. The first bad page
1860 * will terminate the loop.
1862 if (phys_avail[pa_indx] == pa) {
1863 phys_avail[pa_indx] += PAGE_SIZE;
1866 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1868 "Too many holes in the physical address space, giving up\n");
1873 phys_avail[pa_indx++] = pa; /* start */
1874 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1878 if (dump_avail[da_indx] == pa) {
1879 dump_avail[da_indx] += PAGE_SIZE;
1882 if (da_indx == DUMP_AVAIL_ARRAY_END) {
1886 dump_avail[da_indx++] = pa; /* start */
1887 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
1899 * The last chunk must contain at least one page plus the message
1900 * buffer to avoid complicating other code (message buffer address
1901 * calculation, etc.).
1903 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1904 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1905 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1906 phys_avail[pa_indx--] = 0;
1907 phys_avail[pa_indx--] = 0;
1910 Maxmem = atop(phys_avail[pa_indx]);
1912 /* Trim off space for the message buffer. */
1913 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1915 /* Map the message buffer. */
1916 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1917 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
1925 struct gate_descriptor *gdp;
1926 int gsel_tss, metadata_missing, x;
1929 thread0.td_kstack = proc0kstack;
1930 thread0.td_pcb = (struct pcb *)
1931 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
1934 * This may be done better later if it gets more high level
1935 * components in it. If so just link td->td_proc here.
1937 proc_linkup0(&proc0, &thread0);
1944 metadata_missing = 0;
1945 if (bootinfo.bi_modulep) {
1946 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
1947 preload_bootstrap_relocate(KERNBASE);
1949 metadata_missing = 1;
1952 kern_envp = static_env;
1953 else if (bootinfo.bi_envp)
1954 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
1956 /* Init basic tunables, hz etc */
1960 * Make gdt memory segments. All segments cover the full 4GB
1961 * of address space and permissions are enforced at page level.
1963 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
1964 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
1965 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
1966 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
1967 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
1968 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
1971 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
1972 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
1973 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
1975 for (x = 0; x < NGDT; x++)
1976 ssdtosd(&gdt_segs[x], &gdt[x].sd);
1978 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
1979 r_gdt.rd_base = (int) gdt;
1980 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
1983 pcpu_init(pc, 0, sizeof(struct pcpu));
1984 PCPU_SET(prvspace, pc);
1985 PCPU_SET(curthread, &thread0);
1986 PCPU_SET(curpcb, thread0.td_pcb);
1989 * Initialize mutexes.
1991 * icu_lock: in order to allow an interrupt to occur in a critical
1992 * section, to set pcpu->ipending (etc...) properly, we
1993 * must be able to get the icu lock, so it can't be
1997 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
1999 /* make ldt memory segments */
2000 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2001 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2002 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2003 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2005 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2007 PCPU_SET(currentldt, _default_ldt);
2010 for (x = 0; x < NIDT; x++)
2011 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2012 GSEL(GCODE_SEL, SEL_KPL));
2013 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2014 GSEL(GCODE_SEL, SEL_KPL));
2015 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2016 GSEL(GCODE_SEL, SEL_KPL));
2017 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
2018 GSEL(GCODE_SEL, SEL_KPL));
2019 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2020 GSEL(GCODE_SEL, SEL_KPL));
2021 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2022 GSEL(GCODE_SEL, SEL_KPL));
2023 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2024 GSEL(GCODE_SEL, SEL_KPL));
2025 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2026 GSEL(GCODE_SEL, SEL_KPL));
2027 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2028 , GSEL(GCODE_SEL, SEL_KPL));
2029 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2030 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2031 GSEL(GCODE_SEL, SEL_KPL));
2032 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2033 GSEL(GCODE_SEL, SEL_KPL));
2034 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2035 GSEL(GCODE_SEL, SEL_KPL));
2036 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2037 GSEL(GCODE_SEL, SEL_KPL));
2038 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2039 GSEL(GCODE_SEL, SEL_KPL));
2040 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2041 GSEL(GCODE_SEL, SEL_KPL));
2042 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2043 GSEL(GCODE_SEL, SEL_KPL));
2044 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2045 GSEL(GCODE_SEL, SEL_KPL));
2046 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2047 GSEL(GCODE_SEL, SEL_KPL));
2048 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2049 GSEL(GCODE_SEL, SEL_KPL));
2050 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2051 GSEL(GCODE_SEL, SEL_KPL));
2053 r_idt.rd_limit = sizeof(idt0) - 1;
2054 r_idt.rd_base = (int) idt;
2058 * Initialize the i8254 before the console so that console
2059 * initialization can use DELAY().
2064 * Initialize the console before we print anything out.
2068 if (metadata_missing)
2069 printf("WARNING: loader(8) metadata is missing!\n");
2076 ksym_start = bootinfo.bi_symtab;
2077 ksym_end = bootinfo.bi_esymtab;
2083 if (boothowto & RB_KDB)
2084 kdb_enter_why(KDB_WHY_BOOTFLAGS,
2085 "Boot flags requested debugger");
2088 finishidentcpu(); /* Final stage of CPU initialization */
2089 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2090 GSEL(GCODE_SEL, SEL_KPL));
2091 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2092 GSEL(GCODE_SEL, SEL_KPL));
2093 initializecpu(); /* Initialize CPU registers */
2095 /* make an initial tss so cpu can get interrupt stack on syscall! */
2096 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2097 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2098 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
2099 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2100 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2101 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2102 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2103 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2106 /* pointer to selector slot for %fs/%gs */
2107 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2109 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2110 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2111 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2112 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2113 dblfault_tss.tss_cr3 = (int)IdlePTD;
2114 dblfault_tss.tss_eip = (int)dblfault_handler;
2115 dblfault_tss.tss_eflags = PSL_KERNEL;
2116 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2117 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2118 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2119 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2120 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2124 init_param2(physmem);
2126 /* now running on new page tables, configured,and u/iom is accessible */
2128 msgbufinit(msgbufp, MSGBUF_SIZE);
2130 /* make a call gate to reenter kernel with */
2131 gdp = &ldt[LSYS5CALLS_SEL].gd;
2133 x = (int) &IDTVEC(lcall_syscall);
2134 gdp->gd_looffset = x;
2135 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2137 gdp->gd_type = SDT_SYS386CGT;
2138 gdp->gd_dpl = SEL_UPL;
2140 gdp->gd_hioffset = x >> 16;
2142 /* XXX does this work? */
2144 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2145 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
2147 /* transfer to user mode */
2149 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2150 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2152 /* setup proc 0's pcb */
2153 thread0.td_pcb->pcb_flags = 0; /* XXXKSE */
2154 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2155 thread0.td_pcb->pcb_ext = 0;
2156 thread0.td_frame = &proc0_tf;
2160 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2166 spinlock_enter(void)
2171 if (td->td_md.md_spinlock_count == 0)
2172 td->td_md.md_saved_flags = intr_disable();
2173 td->td_md.md_spinlock_count++;
2184 td->td_md.md_spinlock_count--;
2185 if (td->td_md.md_spinlock_count == 0)
2186 intr_restore(td->td_md.md_saved_flags);
2189 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2190 static void f00f_hack(void *unused);
2191 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
2194 f00f_hack(void *unused)
2196 struct gate_descriptor *new_idt;
2204 printf("Intel Pentium detected, installing workaround for F00F bug\n");
2206 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
2208 panic("kmem_alloc returned 0");
2210 /* Put the problematic entry (#6) at the end of the lower page. */
2211 new_idt = (struct gate_descriptor*)
2212 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
2213 bcopy(idt, new_idt, sizeof(idt0));
2214 r_idt.rd_base = (u_int)new_idt;
2217 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2218 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2219 panic("vm_map_protect failed");
2221 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2224 * Construct a PCB from a trapframe. This is called from kdb_trap() where
2225 * we want to start a backtrace from the function that caused us to enter
2226 * the debugger. We have the context in the trapframe, but base the trace
2227 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
2228 * enough for a backtrace.
2231 makectx(struct trapframe *tf, struct pcb *pcb)
2234 pcb->pcb_edi = tf->tf_edi;
2235 pcb->pcb_esi = tf->tf_esi;
2236 pcb->pcb_ebp = tf->tf_ebp;
2237 pcb->pcb_ebx = tf->tf_ebx;
2238 pcb->pcb_eip = tf->tf_eip;
2239 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
2243 ptrace_set_pc(struct thread *td, u_long addr)
2246 td->td_frame->tf_eip = addr;
2251 ptrace_single_step(struct thread *td)
2253 td->td_frame->tf_eflags |= PSL_T;
2258 ptrace_clear_single_step(struct thread *td)
2260 td->td_frame->tf_eflags &= ~PSL_T;
2265 fill_regs(struct thread *td, struct reg *regs)
2268 struct trapframe *tp;
2272 regs->r_fs = tp->tf_fs;
2273 regs->r_es = tp->tf_es;
2274 regs->r_ds = tp->tf_ds;
2275 regs->r_edi = tp->tf_edi;
2276 regs->r_esi = tp->tf_esi;
2277 regs->r_ebp = tp->tf_ebp;
2278 regs->r_ebx = tp->tf_ebx;
2279 regs->r_edx = tp->tf_edx;
2280 regs->r_ecx = tp->tf_ecx;
2281 regs->r_eax = tp->tf_eax;
2282 regs->r_eip = tp->tf_eip;
2283 regs->r_cs = tp->tf_cs;
2284 regs->r_eflags = tp->tf_eflags;
2285 regs->r_esp = tp->tf_esp;
2286 regs->r_ss = tp->tf_ss;
2287 regs->r_gs = pcb->pcb_gs;
2292 set_regs(struct thread *td, struct reg *regs)
2295 struct trapframe *tp;
2298 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2299 !CS_SECURE(regs->r_cs))
2302 tp->tf_fs = regs->r_fs;
2303 tp->tf_es = regs->r_es;
2304 tp->tf_ds = regs->r_ds;
2305 tp->tf_edi = regs->r_edi;
2306 tp->tf_esi = regs->r_esi;
2307 tp->tf_ebp = regs->r_ebp;
2308 tp->tf_ebx = regs->r_ebx;
2309 tp->tf_edx = regs->r_edx;
2310 tp->tf_ecx = regs->r_ecx;
2311 tp->tf_eax = regs->r_eax;
2312 tp->tf_eip = regs->r_eip;
2313 tp->tf_cs = regs->r_cs;
2314 tp->tf_eflags = regs->r_eflags;
2315 tp->tf_esp = regs->r_esp;
2316 tp->tf_ss = regs->r_ss;
2317 pcb->pcb_gs = regs->r_gs;
2321 #ifdef CPU_ENABLE_SSE
2323 fill_fpregs_xmm(sv_xmm, sv_87)
2324 struct savexmm *sv_xmm;
2325 struct save87 *sv_87;
2327 register struct env87 *penv_87 = &sv_87->sv_env;
2328 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2331 bzero(sv_87, sizeof(*sv_87));
2333 /* FPU control/status */
2334 penv_87->en_cw = penv_xmm->en_cw;
2335 penv_87->en_sw = penv_xmm->en_sw;
2336 penv_87->en_tw = penv_xmm->en_tw;
2337 penv_87->en_fip = penv_xmm->en_fip;
2338 penv_87->en_fcs = penv_xmm->en_fcs;
2339 penv_87->en_opcode = penv_xmm->en_opcode;
2340 penv_87->en_foo = penv_xmm->en_foo;
2341 penv_87->en_fos = penv_xmm->en_fos;
2344 for (i = 0; i < 8; ++i)
2345 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
2349 set_fpregs_xmm(sv_87, sv_xmm)
2350 struct save87 *sv_87;
2351 struct savexmm *sv_xmm;
2353 register struct env87 *penv_87 = &sv_87->sv_env;
2354 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2357 /* FPU control/status */
2358 penv_xmm->en_cw = penv_87->en_cw;
2359 penv_xmm->en_sw = penv_87->en_sw;
2360 penv_xmm->en_tw = penv_87->en_tw;
2361 penv_xmm->en_fip = penv_87->en_fip;
2362 penv_xmm->en_fcs = penv_87->en_fcs;
2363 penv_xmm->en_opcode = penv_87->en_opcode;
2364 penv_xmm->en_foo = penv_87->en_foo;
2365 penv_xmm->en_fos = penv_87->en_fos;
2368 for (i = 0; i < 8; ++i)
2369 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
2371 #endif /* CPU_ENABLE_SSE */
2374 fill_fpregs(struct thread *td, struct fpreg *fpregs)
2376 #ifdef CPU_ENABLE_SSE
2378 fill_fpregs_xmm(&td->td_pcb->pcb_save.sv_xmm,
2379 (struct save87 *)fpregs);
2382 #endif /* CPU_ENABLE_SSE */
2383 bcopy(&td->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
2388 set_fpregs(struct thread *td, struct fpreg *fpregs)
2390 #ifdef CPU_ENABLE_SSE
2392 set_fpregs_xmm((struct save87 *)fpregs,
2393 &td->td_pcb->pcb_save.sv_xmm);
2396 #endif /* CPU_ENABLE_SSE */
2397 bcopy(fpregs, &td->td_pcb->pcb_save.sv_87, sizeof *fpregs);
2402 * Get machine context.
2405 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
2407 struct trapframe *tp;
2411 PROC_LOCK(curthread->td_proc);
2412 mcp->mc_onstack = sigonstack(tp->tf_esp);
2413 PROC_UNLOCK(curthread->td_proc);
2414 mcp->mc_gs = td->td_pcb->pcb_gs;
2415 mcp->mc_fs = tp->tf_fs;
2416 mcp->mc_es = tp->tf_es;
2417 mcp->mc_ds = tp->tf_ds;
2418 mcp->mc_edi = tp->tf_edi;
2419 mcp->mc_esi = tp->tf_esi;
2420 mcp->mc_ebp = tp->tf_ebp;
2421 mcp->mc_isp = tp->tf_isp;
2422 mcp->mc_eflags = tp->tf_eflags;
2423 if (flags & GET_MC_CLEAR_RET) {
2426 mcp->mc_eflags &= ~PSL_C;
2428 mcp->mc_eax = tp->tf_eax;
2429 mcp->mc_edx = tp->tf_edx;
2431 mcp->mc_ebx = tp->tf_ebx;
2432 mcp->mc_ecx = tp->tf_ecx;
2433 mcp->mc_eip = tp->tf_eip;
2434 mcp->mc_cs = tp->tf_cs;
2435 mcp->mc_esp = tp->tf_esp;
2436 mcp->mc_ss = tp->tf_ss;
2437 mcp->mc_len = sizeof(*mcp);
2438 get_fpcontext(td, mcp);
2443 * Set machine context.
2445 * However, we don't set any but the user modifiable flags, and we won't
2446 * touch the cs selector.
2449 set_mcontext(struct thread *td, const mcontext_t *mcp)
2451 struct trapframe *tp;
2455 if (mcp->mc_len != sizeof(*mcp))
2457 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
2458 (tp->tf_eflags & ~PSL_USERCHANGE);
2459 if ((ret = set_fpcontext(td, mcp)) == 0) {
2460 tp->tf_fs = mcp->mc_fs;
2461 tp->tf_es = mcp->mc_es;
2462 tp->tf_ds = mcp->mc_ds;
2463 tp->tf_edi = mcp->mc_edi;
2464 tp->tf_esi = mcp->mc_esi;
2465 tp->tf_ebp = mcp->mc_ebp;
2466 tp->tf_ebx = mcp->mc_ebx;
2467 tp->tf_edx = mcp->mc_edx;
2468 tp->tf_ecx = mcp->mc_ecx;
2469 tp->tf_eax = mcp->mc_eax;
2470 tp->tf_eip = mcp->mc_eip;
2471 tp->tf_eflags = eflags;
2472 tp->tf_esp = mcp->mc_esp;
2473 tp->tf_ss = mcp->mc_ss;
2474 td->td_pcb->pcb_gs = mcp->mc_gs;
2481 get_fpcontext(struct thread *td, mcontext_t *mcp)
2484 mcp->mc_fpformat = _MC_FPFMT_NODEV;
2485 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
2487 union savefpu *addr;
2490 * XXX mc_fpstate might be misaligned, since its declaration is not
2491 * unportabilized using __attribute__((aligned(16))) like the
2492 * declaration of struct savemm, and anyway, alignment doesn't work
2493 * for auto variables since we don't use gcc's pessimal stack
2494 * alignment. Work around this by abusing the spare fields after
2497 * XXX unpessimize most cases by only aligning when fxsave might be
2498 * called, although this requires knowing too much about
2499 * npxgetregs()'s internals.
2501 addr = (union savefpu *)&mcp->mc_fpstate;
2502 if (td == PCPU_GET(fpcurthread) &&
2503 #ifdef CPU_ENABLE_SSE
2506 ((uintptr_t)(void *)addr & 0xF)) {
2508 addr = (void *)((char *)addr + 4);
2509 while ((uintptr_t)(void *)addr & 0xF);
2511 mcp->mc_ownedfp = npxgetregs(td, addr);
2512 if (addr != (union savefpu *)&mcp->mc_fpstate) {
2513 bcopy(addr, &mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
2514 bzero(&mcp->mc_spare2, sizeof(mcp->mc_spare2));
2516 mcp->mc_fpformat = npxformat();
2521 set_fpcontext(struct thread *td, const mcontext_t *mcp)
2523 union savefpu *addr;
2525 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2527 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2528 mcp->mc_fpformat != _MC_FPFMT_XMM)
2530 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
2531 /* We don't care what state is left in the FPU or PCB. */
2533 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2534 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2535 /* XXX align as above. */
2536 addr = (union savefpu *)&mcp->mc_fpstate;
2537 if (td == PCPU_GET(fpcurthread) &&
2538 #ifdef CPU_ENABLE_SSE
2541 ((uintptr_t)(void *)addr & 0xF)) {
2543 addr = (void *)((char *)addr + 4);
2544 while ((uintptr_t)(void *)addr & 0xF);
2545 bcopy(&mcp->mc_fpstate, addr, sizeof(mcp->mc_fpstate));
2548 #ifdef CPU_ENABLE_SSE
2550 addr->sv_xmm.sv_env.en_mxcsr &= cpu_mxcsr_mask;
2553 * XXX we violate the dubious requirement that npxsetregs()
2554 * be called with interrupts disabled.
2556 npxsetregs(td, addr);
2559 * Don't bother putting things back where they were in the
2560 * misaligned case, since we know that the caller won't use
2569 fpstate_drop(struct thread *td)
2575 if (PCPU_GET(fpcurthread) == td)
2579 * XXX force a full drop of the npx. The above only drops it if we
2580 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
2582 * XXX I don't much like npxgetregs()'s semantics of doing a full
2583 * drop. Dropping only to the pcb matches fnsave's behaviour.
2584 * We only need to drop to !PCB_INITDONE in sendsig(). But
2585 * sendsig() is the only caller of npxgetregs()... perhaps we just
2586 * have too many layers.
2588 curthread->td_pcb->pcb_flags &= ~PCB_NPXINITDONE;
2593 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2598 dbregs->dr[0] = rdr0();
2599 dbregs->dr[1] = rdr1();
2600 dbregs->dr[2] = rdr2();
2601 dbregs->dr[3] = rdr3();
2602 dbregs->dr[4] = rdr4();
2603 dbregs->dr[5] = rdr5();
2604 dbregs->dr[6] = rdr6();
2605 dbregs->dr[7] = rdr7();
2608 dbregs->dr[0] = pcb->pcb_dr0;
2609 dbregs->dr[1] = pcb->pcb_dr1;
2610 dbregs->dr[2] = pcb->pcb_dr2;
2611 dbregs->dr[3] = pcb->pcb_dr3;
2614 dbregs->dr[6] = pcb->pcb_dr6;
2615 dbregs->dr[7] = pcb->pcb_dr7;
2621 set_dbregs(struct thread *td, struct dbreg *dbregs)
2627 load_dr0(dbregs->dr[0]);
2628 load_dr1(dbregs->dr[1]);
2629 load_dr2(dbregs->dr[2]);
2630 load_dr3(dbregs->dr[3]);
2631 load_dr4(dbregs->dr[4]);
2632 load_dr5(dbregs->dr[5]);
2633 load_dr6(dbregs->dr[6]);
2634 load_dr7(dbregs->dr[7]);
2637 * Don't let an illegal value for dr7 get set. Specifically,
2638 * check for undefined settings. Setting these bit patterns
2639 * result in undefined behaviour and can lead to an unexpected
2642 for (i = 0; i < 4; i++) {
2643 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
2645 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
2652 * Don't let a process set a breakpoint that is not within the
2653 * process's address space. If a process could do this, it
2654 * could halt the system by setting a breakpoint in the kernel
2655 * (if ddb was enabled). Thus, we need to check to make sure
2656 * that no breakpoints are being enabled for addresses outside
2657 * process's address space.
2659 * XXX - what about when the watched area of the user's
2660 * address space is written into from within the kernel
2661 * ... wouldn't that still cause a breakpoint to be generated
2662 * from within kernel mode?
2665 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
2666 /* dr0 is enabled */
2667 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2671 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
2672 /* dr1 is enabled */
2673 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2677 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
2678 /* dr2 is enabled */
2679 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2683 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
2684 /* dr3 is enabled */
2685 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2689 pcb->pcb_dr0 = dbregs->dr[0];
2690 pcb->pcb_dr1 = dbregs->dr[1];
2691 pcb->pcb_dr2 = dbregs->dr[2];
2692 pcb->pcb_dr3 = dbregs->dr[3];
2693 pcb->pcb_dr6 = dbregs->dr[6];
2694 pcb->pcb_dr7 = dbregs->dr[7];
2696 pcb->pcb_flags |= PCB_DBREGS;
2703 * Return > 0 if a hardware breakpoint has been hit, and the
2704 * breakpoint was in user space. Return 0, otherwise.
2707 user_dbreg_trap(void)
2709 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2710 u_int32_t bp; /* breakpoint bits extracted from dr6 */
2711 int nbp; /* number of breakpoints that triggered */
2712 caddr_t addr[4]; /* breakpoint addresses */
2716 if ((dr7 & 0x000000ff) == 0) {
2718 * all GE and LE bits in the dr7 register are zero,
2719 * thus the trap couldn't have been caused by the
2720 * hardware debug registers
2727 bp = dr6 & 0x0000000f;
2731 * None of the breakpoint bits are set meaning this
2732 * trap was not caused by any of the debug registers
2738 * at least one of the breakpoints were hit, check to see
2739 * which ones and if any of them are user space addresses
2743 addr[nbp++] = (caddr_t)rdr0();
2746 addr[nbp++] = (caddr_t)rdr1();
2749 addr[nbp++] = (caddr_t)rdr2();
2752 addr[nbp++] = (caddr_t)rdr3();
2755 for (i = 0; i < nbp; i++) {
2756 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
2758 * addr[i] is in user space
2765 * None of the breakpoints are in user space.
2773 * Provide inb() and outb() as functions. They are normally only
2774 * available as macros calling inlined functions, thus cannot be
2775 * called from the debugger.
2777 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2783 /* silence compiler warnings */
2785 void outb(u_int, u_char);
2792 * We use %%dx and not %1 here because i/o is done at %dx and not at
2793 * %edx, while gcc generates inferior code (movw instead of movl)
2794 * if we tell it to load (u_short) port.
2796 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2801 outb(u_int port, u_char data)
2805 * Use an unnecessary assignment to help gcc's register allocator.
2806 * This make a large difference for gcc-1.40 and a tiny difference
2807 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
2808 * best results. gcc-2.6.0 can't handle this.
2811 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));