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;
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();
265 * Send an interrupt to process.
267 * Stack is set up to allow sigcode stored
268 * at top to call routine, followed by kcall
269 * to sigreturn routine below. After sigreturn
270 * resets the signal mask, the stack, and the
271 * frame pointer, it returns to the user
276 osendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
278 struct osigframe sf, *fp;
282 struct trapframe *regs;
288 PROC_LOCK_ASSERT(p, MA_OWNED);
289 sig = ksi->ksi_signo;
291 mtx_assert(&psp->ps_mtx, MA_OWNED);
293 oonstack = sigonstack(regs->tf_esp);
295 /* Allocate space for the signal handler context. */
296 if ((td->td_pflags & TDP_ALTSTACK) && !oonstack &&
297 SIGISMEMBER(psp->ps_sigonstack, sig)) {
298 fp = (struct osigframe *)(td->td_sigstk.ss_sp +
299 td->td_sigstk.ss_size - sizeof(struct osigframe));
300 #if defined(COMPAT_43)
301 td->td_sigstk.ss_flags |= SS_ONSTACK;
304 fp = (struct osigframe *)regs->tf_esp - 1;
306 /* Translate the signal if appropriate. */
307 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
308 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
310 /* Build the argument list for the signal handler. */
312 sf.sf_scp = (register_t)&fp->sf_siginfo.si_sc;
313 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
314 /* Signal handler installed with SA_SIGINFO. */
315 sf.sf_arg2 = (register_t)&fp->sf_siginfo;
316 sf.sf_siginfo.si_signo = sig;
317 sf.sf_siginfo.si_code = ksi->ksi_code;
318 sf.sf_ahu.sf_action = (__osiginfohandler_t *)catcher;
320 /* Old FreeBSD-style arguments. */
321 sf.sf_arg2 = ksi->ksi_code;
322 sf.sf_addr = (register_t)ksi->ksi_addr;
323 sf.sf_ahu.sf_handler = catcher;
325 mtx_unlock(&psp->ps_mtx);
328 /* Save most if not all of trap frame. */
329 sf.sf_siginfo.si_sc.sc_eax = regs->tf_eax;
330 sf.sf_siginfo.si_sc.sc_ebx = regs->tf_ebx;
331 sf.sf_siginfo.si_sc.sc_ecx = regs->tf_ecx;
332 sf.sf_siginfo.si_sc.sc_edx = regs->tf_edx;
333 sf.sf_siginfo.si_sc.sc_esi = regs->tf_esi;
334 sf.sf_siginfo.si_sc.sc_edi = regs->tf_edi;
335 sf.sf_siginfo.si_sc.sc_cs = regs->tf_cs;
336 sf.sf_siginfo.si_sc.sc_ds = regs->tf_ds;
337 sf.sf_siginfo.si_sc.sc_ss = regs->tf_ss;
338 sf.sf_siginfo.si_sc.sc_es = regs->tf_es;
339 sf.sf_siginfo.si_sc.sc_fs = regs->tf_fs;
340 sf.sf_siginfo.si_sc.sc_gs = rgs();
341 sf.sf_siginfo.si_sc.sc_isp = regs->tf_isp;
343 /* Build the signal context to be used by osigreturn(). */
344 sf.sf_siginfo.si_sc.sc_onstack = (oonstack) ? 1 : 0;
345 SIG2OSIG(*mask, sf.sf_siginfo.si_sc.sc_mask);
346 sf.sf_siginfo.si_sc.sc_sp = regs->tf_esp;
347 sf.sf_siginfo.si_sc.sc_fp = regs->tf_ebp;
348 sf.sf_siginfo.si_sc.sc_pc = regs->tf_eip;
349 sf.sf_siginfo.si_sc.sc_ps = regs->tf_eflags;
350 sf.sf_siginfo.si_sc.sc_trapno = regs->tf_trapno;
351 sf.sf_siginfo.si_sc.sc_err = regs->tf_err;
354 * If we're a vm86 process, we want to save the segment registers.
355 * We also change eflags to be our emulated eflags, not the actual
358 if (regs->tf_eflags & PSL_VM) {
359 /* XXX confusing names: `tf' isn't a trapframe; `regs' is. */
360 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
361 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
363 sf.sf_siginfo.si_sc.sc_gs = tf->tf_vm86_gs;
364 sf.sf_siginfo.si_sc.sc_fs = tf->tf_vm86_fs;
365 sf.sf_siginfo.si_sc.sc_es = tf->tf_vm86_es;
366 sf.sf_siginfo.si_sc.sc_ds = tf->tf_vm86_ds;
368 if (vm86->vm86_has_vme == 0)
369 sf.sf_siginfo.si_sc.sc_ps =
370 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
371 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
373 /* See sendsig() for comments. */
374 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
378 * Copy the sigframe out to the user's stack.
380 if (copyout(&sf, fp, sizeof(*fp)) != 0) {
382 printf("process %ld has trashed its stack\n", (long)p->p_pid);
388 regs->tf_esp = (int)fp;
389 regs->tf_eip = PS_STRINGS - szosigcode;
390 regs->tf_eflags &= ~(PSL_T | PSL_D);
391 regs->tf_cs = _ucodesel;
392 regs->tf_ds = _udatasel;
393 regs->tf_es = _udatasel;
394 regs->tf_fs = _udatasel;
396 regs->tf_ss = _udatasel;
398 mtx_lock(&psp->ps_mtx);
400 #endif /* COMPAT_43 */
402 #ifdef COMPAT_FREEBSD4
404 freebsd4_sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
406 struct sigframe4 sf, *sfp;
410 struct trapframe *regs;
416 PROC_LOCK_ASSERT(p, MA_OWNED);
417 sig = ksi->ksi_signo;
419 mtx_assert(&psp->ps_mtx, MA_OWNED);
421 oonstack = sigonstack(regs->tf_esp);
423 /* Save user context. */
424 bzero(&sf, sizeof(sf));
425 sf.sf_uc.uc_sigmask = *mask;
426 sf.sf_uc.uc_stack = td->td_sigstk;
427 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
428 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
429 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
430 sf.sf_uc.uc_mcontext.mc_gs = rgs();
431 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
433 /* Allocate space for the signal handler context. */
434 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
435 SIGISMEMBER(psp->ps_sigonstack, sig)) {
436 sfp = (struct sigframe4 *)(td->td_sigstk.ss_sp +
437 td->td_sigstk.ss_size - sizeof(struct sigframe4));
438 #if defined(COMPAT_43)
439 td->td_sigstk.ss_flags |= SS_ONSTACK;
442 sfp = (struct sigframe4 *)regs->tf_esp - 1;
444 /* Translate the signal if appropriate. */
445 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
446 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
448 /* Build the argument list for the signal handler. */
450 sf.sf_ucontext = (register_t)&sfp->sf_uc;
451 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
452 /* Signal handler installed with SA_SIGINFO. */
453 sf.sf_siginfo = (register_t)&sfp->sf_si;
454 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
456 /* Fill in POSIX parts */
457 sf.sf_si.si_signo = sig;
458 sf.sf_si.si_code = ksi->ksi_code;
459 sf.sf_si.si_addr = ksi->ksi_addr;
461 /* Old FreeBSD-style arguments. */
462 sf.sf_siginfo = ksi->ksi_code;
463 sf.sf_addr = (register_t)ksi->ksi_addr;
464 sf.sf_ahu.sf_handler = catcher;
466 mtx_unlock(&psp->ps_mtx);
470 * If we're a vm86 process, we want to save the segment registers.
471 * We also change eflags to be our emulated eflags, not the actual
474 if (regs->tf_eflags & PSL_VM) {
475 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
476 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
478 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
479 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
480 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
481 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
483 if (vm86->vm86_has_vme == 0)
484 sf.sf_uc.uc_mcontext.mc_eflags =
485 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
486 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
489 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
490 * syscalls made by the signal handler. This just avoids
491 * wasting time for our lazy fixup of such faults. PSL_NT
492 * does nothing in vm86 mode, but vm86 programs can set it
493 * almost legitimately in probes for old cpu types.
495 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
499 * Copy the sigframe out to the user's stack.
501 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
503 printf("process %ld has trashed its stack\n", (long)p->p_pid);
509 regs->tf_esp = (int)sfp;
510 regs->tf_eip = PS_STRINGS - szfreebsd4_sigcode;
511 regs->tf_eflags &= ~(PSL_T | PSL_D);
512 regs->tf_cs = _ucodesel;
513 regs->tf_ds = _udatasel;
514 regs->tf_es = _udatasel;
515 regs->tf_fs = _udatasel;
516 regs->tf_ss = _udatasel;
518 mtx_lock(&psp->ps_mtx);
520 #endif /* COMPAT_FREEBSD4 */
523 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
525 struct sigframe sf, *sfp;
530 struct trapframe *regs;
531 struct segment_descriptor *sdp;
537 PROC_LOCK_ASSERT(p, MA_OWNED);
538 sig = ksi->ksi_signo;
540 mtx_assert(&psp->ps_mtx, MA_OWNED);
541 #ifdef COMPAT_FREEBSD4
542 if (SIGISMEMBER(psp->ps_freebsd4, sig)) {
543 freebsd4_sendsig(catcher, ksi, mask);
548 if (SIGISMEMBER(psp->ps_osigset, sig)) {
549 osendsig(catcher, ksi, mask);
554 oonstack = sigonstack(regs->tf_esp);
556 /* Save user context. */
557 bzero(&sf, sizeof(sf));
558 sf.sf_uc.uc_sigmask = *mask;
559 sf.sf_uc.uc_stack = td->td_sigstk;
560 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
561 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
562 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
563 sf.sf_uc.uc_mcontext.mc_gs = rgs();
564 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_fs, sizeof(*regs));
565 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext); /* magic */
566 get_fpcontext(td, &sf.sf_uc.uc_mcontext);
569 * Unconditionally fill the fsbase and gsbase into the mcontext.
571 sdp = &td->td_pcb->pcb_gsd;
572 sf.sf_uc.uc_mcontext.mc_fsbase = sdp->sd_hibase << 24 |
574 sdp = &td->td_pcb->pcb_fsd;
575 sf.sf_uc.uc_mcontext.mc_gsbase = sdp->sd_hibase << 24 |
578 /* Allocate space for the signal handler context. */
579 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
580 SIGISMEMBER(psp->ps_sigonstack, sig)) {
581 sp = td->td_sigstk.ss_sp +
582 td->td_sigstk.ss_size - sizeof(struct sigframe);
583 #if defined(COMPAT_43)
584 td->td_sigstk.ss_flags |= SS_ONSTACK;
587 sp = (char *)regs->tf_esp - sizeof(struct sigframe);
588 /* Align to 16 bytes. */
589 sfp = (struct sigframe *)((unsigned int)sp & ~0xF);
591 /* Translate the signal if appropriate. */
592 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
593 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
595 /* Build the argument list for the signal handler. */
597 sf.sf_ucontext = (register_t)&sfp->sf_uc;
598 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
599 /* Signal handler installed with SA_SIGINFO. */
600 sf.sf_siginfo = (register_t)&sfp->sf_si;
601 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
603 /* Fill in POSIX parts */
604 sf.sf_si = ksi->ksi_info;
605 sf.sf_si.si_signo = sig; /* maybe a translated signal */
607 /* Old FreeBSD-style arguments. */
608 sf.sf_siginfo = ksi->ksi_code;
609 sf.sf_addr = (register_t)ksi->ksi_addr;
610 sf.sf_ahu.sf_handler = catcher;
612 mtx_unlock(&psp->ps_mtx);
616 * If we're a vm86 process, we want to save the segment registers.
617 * We also change eflags to be our emulated eflags, not the actual
620 if (regs->tf_eflags & PSL_VM) {
621 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
622 struct vm86_kernel *vm86 = &td->td_pcb->pcb_ext->ext_vm86;
624 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
625 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
626 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
627 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
629 if (vm86->vm86_has_vme == 0)
630 sf.sf_uc.uc_mcontext.mc_eflags =
631 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
632 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
635 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
636 * syscalls made by the signal handler. This just avoids
637 * wasting time for our lazy fixup of such faults. PSL_NT
638 * does nothing in vm86 mode, but vm86 programs can set it
639 * almost legitimately in probes for old cpu types.
641 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
645 * Copy the sigframe out to the user's stack.
647 if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
649 printf("process %ld has trashed its stack\n", (long)p->p_pid);
655 regs->tf_esp = (int)sfp;
656 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
657 regs->tf_eflags &= ~(PSL_T | PSL_D);
658 regs->tf_cs = _ucodesel;
659 regs->tf_ds = _udatasel;
660 regs->tf_es = _udatasel;
661 regs->tf_fs = _udatasel;
662 regs->tf_ss = _udatasel;
664 mtx_lock(&psp->ps_mtx);
668 * System call to cleanup state after a signal
669 * has been taken. Reset signal mask and
670 * stack state from context left by sendsig (above).
671 * Return to previous pc and psl as specified by
672 * context left by sendsig. Check carefully to
673 * make sure that the user has not modified the
674 * state to gain improper privileges.
682 struct osigreturn_args /* {
683 struct osigcontext *sigcntxp;
686 struct osigcontext sc;
687 struct trapframe *regs;
688 struct osigcontext *scp;
689 struct proc *p = td->td_proc;
694 error = copyin(uap->sigcntxp, &sc, sizeof(sc));
699 if (eflags & PSL_VM) {
700 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
701 struct vm86_kernel *vm86;
704 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
705 * set up the vm86 area, and we can't enter vm86 mode.
707 if (td->td_pcb->pcb_ext == 0)
709 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
710 if (vm86->vm86_inited == 0)
713 /* Go back to user mode if both flags are set. */
714 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
715 ksiginfo_init_trap(&ksi);
716 ksi.ksi_signo = SIGBUS;
717 ksi.ksi_code = BUS_OBJERR;
718 ksi.ksi_addr = (void *)regs->tf_eip;
719 trapsignal(td, &ksi);
722 if (vm86->vm86_has_vme) {
723 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
724 (eflags & VME_USERCHANGE) | PSL_VM;
726 vm86->vm86_eflags = eflags; /* save VIF, VIP */
727 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
728 (eflags & VM_USERCHANGE) | PSL_VM;
730 tf->tf_vm86_ds = scp->sc_ds;
731 tf->tf_vm86_es = scp->sc_es;
732 tf->tf_vm86_fs = scp->sc_fs;
733 tf->tf_vm86_gs = scp->sc_gs;
734 tf->tf_ds = _udatasel;
735 tf->tf_es = _udatasel;
736 tf->tf_fs = _udatasel;
739 * Don't allow users to change privileged or reserved flags.
742 * XXX do allow users to change the privileged flag PSL_RF.
743 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
744 * should sometimes set it there too. tf_eflags is kept in
745 * the signal context during signal handling and there is no
746 * other place to remember it, so the PSL_RF bit may be
747 * corrupted by the signal handler without us knowing.
748 * Corruption of the PSL_RF bit at worst causes one more or
749 * one less debugger trap, so allowing it is fairly harmless.
751 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
756 * Don't allow users to load a valid privileged %cs. Let the
757 * hardware check for invalid selectors, excess privilege in
758 * other selectors, invalid %eip's and invalid %esp's.
760 if (!CS_SECURE(scp->sc_cs)) {
761 ksiginfo_init_trap(&ksi);
762 ksi.ksi_signo = SIGBUS;
763 ksi.ksi_code = BUS_OBJERR;
764 ksi.ksi_trapno = T_PROTFLT;
765 ksi.ksi_addr = (void *)regs->tf_eip;
766 trapsignal(td, &ksi);
769 regs->tf_ds = scp->sc_ds;
770 regs->tf_es = scp->sc_es;
771 regs->tf_fs = scp->sc_fs;
774 /* Restore remaining registers. */
775 regs->tf_eax = scp->sc_eax;
776 regs->tf_ebx = scp->sc_ebx;
777 regs->tf_ecx = scp->sc_ecx;
778 regs->tf_edx = scp->sc_edx;
779 regs->tf_esi = scp->sc_esi;
780 regs->tf_edi = scp->sc_edi;
781 regs->tf_cs = scp->sc_cs;
782 regs->tf_ss = scp->sc_ss;
783 regs->tf_isp = scp->sc_isp;
784 regs->tf_ebp = scp->sc_fp;
785 regs->tf_esp = scp->sc_sp;
786 regs->tf_eip = scp->sc_pc;
787 regs->tf_eflags = eflags;
790 #if defined(COMPAT_43)
791 if (scp->sc_onstack & 1)
792 td->td_sigstk.ss_flags |= SS_ONSTACK;
794 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
796 SIGSETOLD(td->td_sigmask, scp->sc_mask);
797 SIG_CANTMASK(td->td_sigmask);
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 proc *p = td->td_proc;
817 struct trapframe *regs;
818 const struct ucontext4 *ucp;
819 int cs, eflags, error;
822 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
827 eflags = ucp->uc_mcontext.mc_eflags;
828 if (eflags & PSL_VM) {
829 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
830 struct vm86_kernel *vm86;
833 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
834 * set up the vm86 area, and we can't enter vm86 mode.
836 if (td->td_pcb->pcb_ext == 0)
838 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
839 if (vm86->vm86_inited == 0)
842 /* Go back to user mode if both flags are set. */
843 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
844 ksiginfo_init_trap(&ksi);
845 ksi.ksi_signo = SIGBUS;
846 ksi.ksi_code = BUS_OBJERR;
847 ksi.ksi_addr = (void *)regs->tf_eip;
848 trapsignal(td, &ksi);
850 if (vm86->vm86_has_vme) {
851 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
852 (eflags & VME_USERCHANGE) | PSL_VM;
854 vm86->vm86_eflags = eflags; /* save VIF, VIP */
855 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
856 (eflags & VM_USERCHANGE) | PSL_VM;
858 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
859 tf->tf_eflags = eflags;
860 tf->tf_vm86_ds = tf->tf_ds;
861 tf->tf_vm86_es = tf->tf_es;
862 tf->tf_vm86_fs = tf->tf_fs;
863 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
864 tf->tf_ds = _udatasel;
865 tf->tf_es = _udatasel;
866 tf->tf_fs = _udatasel;
869 * Don't allow users to change privileged or reserved flags.
872 * XXX do allow users to change the privileged flag PSL_RF.
873 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
874 * should sometimes set it there too. tf_eflags is kept in
875 * the signal context during signal handling and there is no
876 * other place to remember it, so the PSL_RF bit may be
877 * corrupted by the signal handler without us knowing.
878 * Corruption of the PSL_RF bit at worst causes one more or
879 * one less debugger trap, so allowing it is fairly harmless.
881 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
882 printf("freebsd4_sigreturn: eflags = 0x%x\n", 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 printf("freebsd4_sigreturn: cs = 0x%x\n", cs);
894 ksiginfo_init_trap(&ksi);
895 ksi.ksi_signo = SIGBUS;
896 ksi.ksi_code = BUS_OBJERR;
897 ksi.ksi_trapno = T_PROTFLT;
898 ksi.ksi_addr = (void *)regs->tf_eip;
899 trapsignal(td, &ksi);
903 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;
914 td->td_sigmask = ucp->uc_sigmask;
915 SIG_CANTMASK(td->td_sigmask);
918 return (EJUSTRETURN);
920 #endif /* COMPAT_FREEBSD4 */
928 struct sigreturn_args /* {
929 const struct __ucontext *sigcntxp;
933 struct proc *p = td->td_proc;
934 struct trapframe *regs;
935 const ucontext_t *ucp;
936 int cs, eflags, error, ret;
939 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
944 eflags = ucp->uc_mcontext.mc_eflags;
945 if (eflags & PSL_VM) {
946 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
947 struct vm86_kernel *vm86;
950 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
951 * set up the vm86 area, and we can't enter vm86 mode.
953 if (td->td_pcb->pcb_ext == 0)
955 vm86 = &td->td_pcb->pcb_ext->ext_vm86;
956 if (vm86->vm86_inited == 0)
959 /* Go back to user mode if both flags are set. */
960 if ((eflags & PSL_VIP) && (eflags & PSL_VIF)) {
961 ksiginfo_init_trap(&ksi);
962 ksi.ksi_signo = SIGBUS;
963 ksi.ksi_code = BUS_OBJERR;
964 ksi.ksi_addr = (void *)regs->tf_eip;
965 trapsignal(td, &ksi);
968 if (vm86->vm86_has_vme) {
969 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
970 (eflags & VME_USERCHANGE) | PSL_VM;
972 vm86->vm86_eflags = eflags; /* save VIF, VIP */
973 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
974 (eflags & VM_USERCHANGE) | PSL_VM;
976 bcopy(&ucp->uc_mcontext.mc_fs, tf, sizeof(struct trapframe));
977 tf->tf_eflags = eflags;
978 tf->tf_vm86_ds = tf->tf_ds;
979 tf->tf_vm86_es = tf->tf_es;
980 tf->tf_vm86_fs = tf->tf_fs;
981 tf->tf_vm86_gs = ucp->uc_mcontext.mc_gs;
982 tf->tf_ds = _udatasel;
983 tf->tf_es = _udatasel;
984 tf->tf_fs = _udatasel;
987 * Don't allow users to change privileged or reserved flags.
990 * XXX do allow users to change the privileged flag PSL_RF.
991 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
992 * should sometimes set it there too. tf_eflags is kept in
993 * the signal context during signal handling and there is no
994 * other place to remember it, so the PSL_RF bit may be
995 * corrupted by the signal handler without us knowing.
996 * Corruption of the PSL_RF bit at worst causes one more or
997 * one less debugger trap, so allowing it is fairly harmless.
999 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
1000 printf("sigreturn: eflags = 0x%x\n", eflags);
1005 * Don't allow users to load a valid privileged %cs. Let the
1006 * hardware check for invalid selectors, excess privilege in
1007 * other selectors, invalid %eip's and invalid %esp's.
1009 cs = ucp->uc_mcontext.mc_cs;
1010 if (!CS_SECURE(cs)) {
1011 printf("sigreturn: cs = 0x%x\n", cs);
1012 ksiginfo_init_trap(&ksi);
1013 ksi.ksi_signo = SIGBUS;
1014 ksi.ksi_code = BUS_OBJERR;
1015 ksi.ksi_trapno = T_PROTFLT;
1016 ksi.ksi_addr = (void *)regs->tf_eip;
1017 trapsignal(td, &ksi);
1021 ret = set_fpcontext(td, &ucp->uc_mcontext);
1024 bcopy(&ucp->uc_mcontext.mc_fs, regs, sizeof(*regs));
1028 #if defined(COMPAT_43)
1029 if (ucp->uc_mcontext.mc_onstack & 1)
1030 td->td_sigstk.ss_flags |= SS_ONSTACK;
1032 td->td_sigstk.ss_flags &= ~SS_ONSTACK;
1035 td->td_sigmask = ucp->uc_sigmask;
1036 SIG_CANTMASK(td->td_sigmask);
1039 return (EJUSTRETURN);
1043 * Machine dependent boot() routine
1045 * I haven't seen anything to put here yet
1046 * Possibly some stuff might be grafted back here from boot()
1054 * Flush the D-cache for non-DMA I/O so that the I-cache can
1055 * be made coherent later.
1058 cpu_flush_dcache(void *ptr, size_t len)
1060 /* Not applicable */
1063 /* Get current clock frequency for the given cpu id. */
1065 cpu_est_clockrate(int cpu_id, uint64_t *rate)
1068 uint64_t tsc1, tsc2;
1070 if (pcpu_find(cpu_id) == NULL || rate == NULL)
1073 return (EOPNOTSUPP);
1075 /* If we're booting, trust the rate calibrated moments ago. */
1082 /* Schedule ourselves on the indicated cpu. */
1083 thread_lock(curthread);
1084 sched_bind(curthread, cpu_id);
1085 thread_unlock(curthread);
1088 /* Calibrate by measuring a short delay. */
1089 reg = intr_disable();
1096 thread_lock(curthread);
1097 sched_unbind(curthread);
1098 thread_unlock(curthread);
1102 * Calculate the difference in readings, convert to Mhz, and
1103 * subtract 0.5% of the total. Empirical testing has shown that
1104 * overhead in DELAY() works out to approximately this value.
1107 *rate = tsc2 * 1000 - tsc2 * 5;
1112 * Shutdown the CPU as much as possible
1122 * Hook to idle the CPU when possible. In the SMP case we default to
1123 * off because a halted cpu will not currently pick up a new thread in the
1124 * run queue until the next timer tick. If turned on this will result in
1125 * approximately a 4.2% loss in real time performance in buildworld tests
1126 * (but improves user and sys times oddly enough), and saves approximately
1127 * 5% in power consumption on an idle machine (tests w/2xCPU 1.1GHz P3).
1129 * XXX we need to have a cpu mask of idle cpus and generate an IPI or
1130 * otherwise generate some sort of interrupt to wake up cpus sitting in HLT.
1131 * Then we can have our cake and eat it too.
1133 * XXX I'm turning it on for SMP as well by default for now. It seems to
1134 * help lock contention somewhat, and this is critical for HTT. -Peter
1136 static int cpu_idle_hlt = 1;
1137 TUNABLE_INT("machdep.cpu_idle_hlt", &cpu_idle_hlt);
1138 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
1139 &cpu_idle_hlt, 0, "Idle loop HLT enable");
1142 cpu_idle_default(void)
1145 * we must absolutely guarentee that hlt is the
1146 * absolute next instruction after sti or we
1147 * introduce a timing window.
1149 __asm __volatile("sti; hlt");
1153 * Note that we have to be careful here to avoid a race between checking
1154 * sched_runnable() and actually halting. If we don't do this, we may waste
1155 * the time between calling hlt and the next interrupt even though there
1156 * is a runnable process.
1163 if (mp_grab_cpu_hlt())
1169 if (sched_runnable())
1177 cpu_idle_wakeup(int cpu)
1183 /* Other subsystems (e.g., ACPI) can hook this later. */
1184 void (*cpu_idle_hook)(void) = cpu_idle_default;
1187 * Reset registers to default values on exec.
1190 exec_setregs(td, entry, stack, ps_strings)
1196 struct trapframe *regs = td->td_frame;
1197 struct pcb *pcb = td->td_pcb;
1199 /* Reset pc->pcb_gs and %gs before possibly invalidating it. */
1200 pcb->pcb_gs = _udatasel;
1203 mtx_lock_spin(&dt_lock);
1204 if (td->td_proc->p_md.md_ldt)
1207 mtx_unlock_spin(&dt_lock);
1209 bzero((char *)regs, sizeof(struct trapframe));
1210 regs->tf_eip = entry;
1211 regs->tf_esp = stack;
1212 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
1213 regs->tf_ss = _udatasel;
1214 regs->tf_ds = _udatasel;
1215 regs->tf_es = _udatasel;
1216 regs->tf_fs = _udatasel;
1217 regs->tf_cs = _ucodesel;
1219 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
1220 regs->tf_ebx = ps_strings;
1223 * Reset the hardware debug registers if they were in use.
1224 * They won't have any meaning for the newly exec'd process.
1226 if (pcb->pcb_flags & PCB_DBREGS) {
1233 if (pcb == PCPU_GET(curpcb)) {
1235 * Clear the debug registers on the running
1236 * CPU, otherwise they will end up affecting
1237 * the next process we switch to.
1241 pcb->pcb_flags &= ~PCB_DBREGS;
1245 * Initialize the math emulator (if any) for the current process.
1246 * Actually, just clear the bit that says that the emulator has
1247 * been initialized. Initialization is delayed until the process
1248 * traps to the emulator (if it is done at all) mainly because
1249 * emulators don't provide an entry point for initialization.
1251 td->td_pcb->pcb_flags &= ~FP_SOFTFP;
1252 pcb->pcb_initial_npxcw = __INITIAL_NPXCW__;
1255 * Drop the FP state if we hold it, so that the process gets a
1256 * clean FP state if it uses the FPU again.
1261 * XXX - Linux emulator
1262 * Make sure sure edx is 0x0 on entry. Linux binaries depend
1265 td->td_retval[1] = 0;
1276 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
1278 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
1279 * instructions. We must set the CR0_MP bit and use the CR0_TS
1280 * bit to control the trap, because setting the CR0_EM bit does
1281 * not cause WAIT instructions to trap. It's important to trap
1282 * WAIT instructions - otherwise the "wait" variants of no-wait
1283 * control instructions would degenerate to the "no-wait" variants
1284 * after FP context switches but work correctly otherwise. It's
1285 * particularly important to trap WAITs when there is no NPX -
1286 * otherwise the "wait" variants would always degenerate.
1288 * Try setting CR0_NE to get correct error reporting on 486DX's.
1289 * Setting it should fail or do nothing on lesser processors.
1291 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
1296 u_long bootdev; /* not a struct cdev *- encoding is different */
1297 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
1298 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
1301 * Initialize 386 and configure to run kernel
1305 * Initialize segments & interrupt table
1309 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
1310 static struct gate_descriptor idt0[NIDT];
1311 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
1312 union descriptor ldt[NLDT]; /* local descriptor table */
1313 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1314 struct mtx dt_lock; /* lock for GDT and LDT */
1316 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1317 extern int has_f00f_bug;
1320 static struct i386tss dblfault_tss;
1321 static char dblfault_stack[PAGE_SIZE];
1323 extern vm_offset_t proc0kstack;
1327 * software prototypes -- in more palatable form.
1329 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
1330 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
1332 struct soft_segment_descriptor gdt_segs[] = {
1333 /* GNULL_SEL 0 Null Descriptor */
1339 .ssd_xx = 0, .ssd_xx1 = 0,
1342 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
1344 .ssd_limit = 0xfffff,
1345 .ssd_type = SDT_MEMRWA,
1348 .ssd_xx = 0, .ssd_xx1 = 0,
1351 /* GUFS_SEL 2 %fs Descriptor for user */
1353 .ssd_limit = 0xfffff,
1354 .ssd_type = SDT_MEMRWA,
1357 .ssd_xx = 0, .ssd_xx1 = 0,
1360 /* GUGS_SEL 3 %gs Descriptor for user */
1362 .ssd_limit = 0xfffff,
1363 .ssd_type = SDT_MEMRWA,
1366 .ssd_xx = 0, .ssd_xx1 = 0,
1369 /* GCODE_SEL 4 Code Descriptor for kernel */
1371 .ssd_limit = 0xfffff,
1372 .ssd_type = SDT_MEMERA,
1375 .ssd_xx = 0, .ssd_xx1 = 0,
1378 /* GDATA_SEL 5 Data Descriptor for kernel */
1380 .ssd_limit = 0xfffff,
1381 .ssd_type = SDT_MEMRWA,
1384 .ssd_xx = 0, .ssd_xx1 = 0,
1387 /* GUCODE_SEL 6 Code Descriptor for user */
1389 .ssd_limit = 0xfffff,
1390 .ssd_type = SDT_MEMERA,
1393 .ssd_xx = 0, .ssd_xx1 = 0,
1396 /* GUDATA_SEL 7 Data Descriptor for user */
1398 .ssd_limit = 0xfffff,
1399 .ssd_type = SDT_MEMRWA,
1402 .ssd_xx = 0, .ssd_xx1 = 0,
1405 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1406 { .ssd_base = 0x400,
1407 .ssd_limit = 0xfffff,
1408 .ssd_type = SDT_MEMRWA,
1411 .ssd_xx = 0, .ssd_xx1 = 0,
1414 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
1417 .ssd_limit = sizeof(struct i386tss)-1,
1418 .ssd_type = SDT_SYS386TSS,
1421 .ssd_xx = 0, .ssd_xx1 = 0,
1424 /* GLDT_SEL 10 LDT Descriptor */
1425 { .ssd_base = (int) ldt,
1426 .ssd_limit = sizeof(ldt)-1,
1427 .ssd_type = SDT_SYSLDT,
1430 .ssd_xx = 0, .ssd_xx1 = 0,
1433 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
1434 { .ssd_base = (int) ldt,
1435 .ssd_limit = (512 * sizeof(union descriptor)-1),
1436 .ssd_type = SDT_SYSLDT,
1439 .ssd_xx = 0, .ssd_xx1 = 0,
1442 /* GPANIC_SEL 12 Panic Tss Descriptor */
1443 { .ssd_base = (int) &dblfault_tss,
1444 .ssd_limit = sizeof(struct i386tss)-1,
1445 .ssd_type = SDT_SYS386TSS,
1448 .ssd_xx = 0, .ssd_xx1 = 0,
1451 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
1453 .ssd_limit = 0xfffff,
1454 .ssd_type = SDT_MEMERA,
1457 .ssd_xx = 0, .ssd_xx1 = 0,
1460 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
1462 .ssd_limit = 0xfffff,
1463 .ssd_type = SDT_MEMERA,
1466 .ssd_xx = 0, .ssd_xx1 = 0,
1469 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
1471 .ssd_limit = 0xfffff,
1472 .ssd_type = SDT_MEMRWA,
1475 .ssd_xx = 0, .ssd_xx1 = 0,
1478 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
1480 .ssd_limit = 0xfffff,
1481 .ssd_type = SDT_MEMRWA,
1484 .ssd_xx = 0, .ssd_xx1 = 0,
1487 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
1489 .ssd_limit = 0xfffff,
1490 .ssd_type = SDT_MEMRWA,
1493 .ssd_xx = 0, .ssd_xx1 = 0,
1496 /* GNDIS_SEL 18 NDIS Descriptor */
1502 .ssd_xx = 0, .ssd_xx1 = 0,
1507 static struct soft_segment_descriptor ldt_segs[] = {
1508 /* Null Descriptor - overwritten by call gate */
1514 .ssd_xx = 0, .ssd_xx1 = 0,
1517 /* Null Descriptor - overwritten by call gate */
1523 .ssd_xx = 0, .ssd_xx1 = 0,
1526 /* Null Descriptor - overwritten by call gate */
1532 .ssd_xx = 0, .ssd_xx1 = 0,
1535 /* Code Descriptor for user */
1537 .ssd_limit = 0xfffff,
1538 .ssd_type = SDT_MEMERA,
1541 .ssd_xx = 0, .ssd_xx1 = 0,
1544 /* Null Descriptor - overwritten by call gate */
1550 .ssd_xx = 0, .ssd_xx1 = 0,
1553 /* Data Descriptor for user */
1555 .ssd_limit = 0xfffff,
1556 .ssd_type = SDT_MEMRWA,
1559 .ssd_xx = 0, .ssd_xx1 = 0,
1565 setidt(idx, func, typ, dpl, selec)
1572 struct gate_descriptor *ip;
1575 ip->gd_looffset = (int)func;
1576 ip->gd_selector = selec;
1582 ip->gd_hioffset = ((int)func)>>16 ;
1586 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1587 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1588 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1589 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
1590 IDTVEC(xmm), IDTVEC(lcall_syscall), IDTVEC(int0x80_syscall);
1594 * Display the index and function name of any IDT entries that don't use
1595 * the default 'rsvd' entry point.
1597 DB_SHOW_COMMAND(idt, db_show_idt)
1599 struct gate_descriptor *ip;
1604 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
1605 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
1606 if (func != (uintptr_t)&IDTVEC(rsvd)) {
1607 db_printf("%3d\t", idx);
1608 db_printsym(func, DB_STGY_PROC);
1615 /* Show privileged registers. */
1616 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
1618 uint64_t idtr, gdtr;
1621 db_printf("idtr\t0x%08x/%04x\n",
1622 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
1624 db_printf("gdtr\t0x%08x/%04x\n",
1625 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
1626 db_printf("ldtr\t0x%04x\n", rldt());
1627 db_printf("tr\t0x%04x\n", rtr());
1628 db_printf("cr0\t0x%08x\n", rcr0());
1629 db_printf("cr2\t0x%08x\n", rcr2());
1630 db_printf("cr3\t0x%08x\n", rcr3());
1631 db_printf("cr4\t0x%08x\n", rcr4());
1637 struct segment_descriptor *sd;
1638 struct soft_segment_descriptor *ssd;
1640 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1641 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1642 ssd->ssd_type = sd->sd_type;
1643 ssd->ssd_dpl = sd->sd_dpl;
1644 ssd->ssd_p = sd->sd_p;
1645 ssd->ssd_def32 = sd->sd_def32;
1646 ssd->ssd_gran = sd->sd_gran;
1650 * Populate the (physmap) array with base/bound pairs describing the
1651 * available physical memory in the system, then test this memory and
1652 * build the phys_avail array describing the actually-available memory.
1654 * If we cannot accurately determine the physical memory map, then use
1655 * value from the 0xE801 call, and failing that, the RTC.
1657 * Total memory size may be set by the kernel environment variable
1658 * hw.physmem or the compile-time define MAXMEM.
1660 * XXX first should be vm_paddr_t.
1663 getmemsize(int first)
1665 int i, off, physmap_idx, pa_indx, da_indx;
1667 u_long physmem_tunable;
1668 u_int extmem, under16;
1669 vm_paddr_t pa, physmap[PHYSMAP_SIZE];
1671 quad_t dcons_addr, dcons_size;
1673 bzero(physmap, sizeof(physmap));
1675 /* XXX - some of EPSON machines can't use PG_N */
1677 if (pc98_machine_type & M_EPSON_PC98) {
1678 switch (epson_machine_id) {
1682 case EPSON_PC486_HX:
1683 case EPSON_PC486_HG:
1684 case EPSON_PC486_HA:
1691 * Perform "base memory" related probes & setup
1693 under16 = pc98_getmemsize(&basemem, &extmem);
1694 if (basemem > 640) {
1695 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
1701 * XXX if biosbasemem is now < 640, there is a `hole'
1702 * between the end of base memory and the start of
1703 * ISA memory. The hole may be empty or it may
1704 * contain BIOS code or data. Map it read/write so
1705 * that the BIOS can write to it. (Memory from 0 to
1706 * the physical end of the kernel is mapped read-only
1707 * to begin with and then parts of it are remapped.
1708 * The parts that aren't remapped form holes that
1709 * remain read-only and are unused by the kernel.
1710 * The base memory area is below the physical end of
1711 * the kernel and right now forms a read-only hole.
1712 * The part of it from PAGE_SIZE to
1713 * (trunc_page(biosbasemem * 1024) - 1) will be
1714 * remapped and used by the kernel later.)
1716 * This code is similar to the code used in
1717 * pmap_mapdev, but since no memory needs to be
1718 * allocated we simply change the mapping.
1720 for (pa = trunc_page(basemem * 1024);
1721 pa < ISA_HOLE_START; pa += PAGE_SIZE)
1722 pmap_kenter(KERNBASE + pa, pa);
1725 * if basemem != 640, map pages r/w into vm86 page table so
1726 * that the bios can scribble on it.
1728 pte = (pt_entry_t *)vm86paddr;
1729 for (i = basemem / 4; i < 160; i++)
1730 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1733 physmap[1] = basemem * 1024;
1735 physmap[physmap_idx] = 0x100000;
1736 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1739 * Now, physmap contains a map of physical memory.
1743 /* make hole for AP bootstrap code */
1744 physmap[1] = mp_bootaddress(physmap[1]);
1748 * Maxmem isn't the "maximum memory", it's one larger than the
1749 * highest page of the physical address space. It should be
1750 * called something like "Maxphyspage". We may adjust this
1751 * based on ``hw.physmem'' and the results of the memory test.
1753 Maxmem = atop(physmap[physmap_idx + 1]);
1756 Maxmem = MAXMEM / 4;
1759 if (TUNABLE_ULONG_FETCH("hw.physmem", &physmem_tunable))
1760 Maxmem = atop(physmem_tunable);
1762 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1763 (boothowto & RB_VERBOSE))
1764 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1767 * If Maxmem has been increased beyond what the system has detected,
1768 * extend the last memory segment to the new limit.
1770 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1771 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1774 * We need to divide chunk if Maxmem is larger than 16MB and
1775 * under 16MB area is not full of memory.
1776 * (1) system area (15-16MB region) is cut off
1777 * (2) extended memory is only over 16MB area (ex. Melco "HYPERMEMORY")
1779 if ((under16 != 16 * 1024) && (extmem > 15 * 1024)) {
1780 /* 15M - 16M region is cut off, so need to divide chunk */
1781 physmap[physmap_idx + 1] = under16 * 1024;
1783 physmap[physmap_idx] = 0x1000000;
1784 physmap[physmap_idx + 1] = physmap[2] + extmem * 1024;
1787 /* call pmap initialization to make new kernel address space */
1788 pmap_bootstrap(first);
1791 * Size up each available chunk of physical memory.
1793 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1796 phys_avail[pa_indx++] = physmap[0];
1797 phys_avail[pa_indx] = physmap[0];
1798 dump_avail[da_indx] = physmap[0];
1802 * Get dcons buffer address
1804 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
1805 getenv_quad("dcons.size", &dcons_size) == 0)
1809 * physmap is in bytes, so when converting to page boundaries,
1810 * round up the start address and round down the end address.
1812 for (i = 0; i <= physmap_idx; i += 2) {
1815 end = ptoa((vm_paddr_t)Maxmem);
1816 if (physmap[i + 1] < end)
1817 end = trunc_page(physmap[i + 1]);
1818 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1819 int tmp, page_bad, full;
1820 int *ptr = (int *)CADDR1;
1824 * block out kernel memory as not available.
1826 if (pa >= KERNLOAD && pa < first)
1830 * block out dcons buffer
1833 && pa >= trunc_page(dcons_addr)
1834 && pa < dcons_addr + dcons_size)
1840 * map page into kernel: valid, read/write,non-cacheable
1842 *pte = pa | PG_V | PG_RW | pg_n;
1847 * Test for alternating 1's and 0's
1849 *(volatile int *)ptr = 0xaaaaaaaa;
1850 if (*(volatile int *)ptr != 0xaaaaaaaa)
1853 * Test for alternating 0's and 1's
1855 *(volatile int *)ptr = 0x55555555;
1856 if (*(volatile int *)ptr != 0x55555555)
1861 *(volatile int *)ptr = 0xffffffff;
1862 if (*(volatile int *)ptr != 0xffffffff)
1867 *(volatile int *)ptr = 0x0;
1868 if (*(volatile int *)ptr != 0x0)
1871 * Restore original value.
1876 * Adjust array of valid/good pages.
1878 if (page_bad == TRUE)
1881 * If this good page is a continuation of the
1882 * previous set of good pages, then just increase
1883 * the end pointer. Otherwise start a new chunk.
1884 * Note that "end" points one higher than end,
1885 * making the range >= start and < end.
1886 * If we're also doing a speculative memory
1887 * test and we at or past the end, bump up Maxmem
1888 * so that we keep going. The first bad page
1889 * will terminate the loop.
1891 if (phys_avail[pa_indx] == pa) {
1892 phys_avail[pa_indx] += PAGE_SIZE;
1895 if (pa_indx == PHYS_AVAIL_ARRAY_END) {
1897 "Too many holes in the physical address space, giving up\n");
1902 phys_avail[pa_indx++] = pa; /* start */
1903 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1907 if (dump_avail[da_indx] == pa) {
1908 dump_avail[da_indx] += PAGE_SIZE;
1911 if (da_indx == DUMP_AVAIL_ARRAY_END) {
1915 dump_avail[da_indx++] = pa; /* start */
1916 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
1928 * The last chunk must contain at least one page plus the message
1929 * buffer to avoid complicating other code (message buffer address
1930 * calculation, etc.).
1932 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1933 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1934 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1935 phys_avail[pa_indx--] = 0;
1936 phys_avail[pa_indx--] = 0;
1939 Maxmem = atop(phys_avail[pa_indx]);
1941 /* Trim off space for the message buffer. */
1942 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1944 /* Map the message buffer. */
1945 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1946 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
1954 struct gate_descriptor *gdp;
1955 int gsel_tss, metadata_missing, x;
1959 thread0.td_kstack = proc0kstack;
1960 thread0.td_pcb = (struct pcb *)
1961 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
1964 * This may be done better later if it gets more high level
1965 * components in it. If so just link td->td_proc here.
1967 proc_linkup0(&proc0, &thread0);
1974 metadata_missing = 0;
1975 if (bootinfo.bi_modulep) {
1976 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
1977 preload_bootstrap_relocate(KERNBASE);
1979 metadata_missing = 1;
1982 kern_envp = static_env;
1983 else if (bootinfo.bi_envp)
1984 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
1986 /* Init basic tunables, hz etc */
1990 * Make gdt memory segments. All segments cover the full 4GB
1991 * of address space and permissions are enforced at page level.
1993 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
1994 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
1995 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
1996 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
1997 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
1998 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
2001 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
2002 gdt_segs[GPRIV_SEL].ssd_base = (int) pc;
2003 gdt_segs[GPROC0_SEL].ssd_base = (int) &pc->pc_common_tss;
2005 for (x = 0; x < NGDT; x++)
2006 ssdtosd(&gdt_segs[x], &gdt[x].sd);
2008 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
2009 r_gdt.rd_base = (int) gdt;
2010 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
2013 pcpu_init(pc, 0, sizeof(struct pcpu));
2014 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
2015 pmap_kenter(pa + KERNBASE, pa);
2016 dpcpu_init((void *)(first + KERNBASE), 0);
2017 first += DPCPU_SIZE;
2019 PCPU_SET(prvspace, pc);
2020 PCPU_SET(curthread, &thread0);
2021 PCPU_SET(curpcb, thread0.td_pcb);
2024 * Initialize mutexes.
2026 * icu_lock: in order to allow an interrupt to occur in a critical
2027 * section, to set pcpu->ipending (etc...) properly, we
2028 * must be able to get the icu lock, so it can't be
2032 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
2034 /* make ldt memory segments */
2035 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
2036 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
2037 for (x = 0; x < sizeof ldt_segs / sizeof ldt_segs[0]; x++)
2038 ssdtosd(&ldt_segs[x], &ldt[x].sd);
2040 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
2042 PCPU_SET(currentldt, _default_ldt);
2045 for (x = 0; x < NIDT; x++)
2046 setidt(x, &IDTVEC(rsvd), SDT_SYS386TGT, SEL_KPL,
2047 GSEL(GCODE_SEL, SEL_KPL));
2048 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL,
2049 GSEL(GCODE_SEL, SEL_KPL));
2050 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
2051 GSEL(GCODE_SEL, SEL_KPL));
2052 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
2053 GSEL(GCODE_SEL, SEL_KPL));
2054 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
2055 GSEL(GCODE_SEL, SEL_KPL));
2056 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL,
2057 GSEL(GCODE_SEL, SEL_KPL));
2058 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL,
2059 GSEL(GCODE_SEL, SEL_KPL));
2060 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2061 GSEL(GCODE_SEL, SEL_KPL));
2062 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL
2063 , GSEL(GCODE_SEL, SEL_KPL));
2064 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
2065 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL,
2066 GSEL(GCODE_SEL, SEL_KPL));
2067 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL,
2068 GSEL(GCODE_SEL, SEL_KPL));
2069 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL,
2070 GSEL(GCODE_SEL, SEL_KPL));
2071 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL,
2072 GSEL(GCODE_SEL, SEL_KPL));
2073 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2074 GSEL(GCODE_SEL, SEL_KPL));
2075 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
2076 GSEL(GCODE_SEL, SEL_KPL));
2077 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL,
2078 GSEL(GCODE_SEL, SEL_KPL));
2079 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL,
2080 GSEL(GCODE_SEL, SEL_KPL));
2081 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL,
2082 GSEL(GCODE_SEL, SEL_KPL));
2083 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL,
2084 GSEL(GCODE_SEL, SEL_KPL));
2085 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall), SDT_SYS386TGT, SEL_UPL,
2086 GSEL(GCODE_SEL, SEL_KPL));
2088 r_idt.rd_limit = sizeof(idt0) - 1;
2089 r_idt.rd_base = (int) idt;
2093 * Initialize the i8254 before the console so that console
2094 * initialization can use DELAY().
2099 * Initialize the console before we print anything out.
2103 if (metadata_missing)
2104 printf("WARNING: loader(8) metadata is missing!\n");
2111 ksym_start = bootinfo.bi_symtab;
2112 ksym_end = bootinfo.bi_esymtab;
2118 if (boothowto & RB_KDB)
2119 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
2122 finishidentcpu(); /* Final stage of CPU initialization */
2123 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL,
2124 GSEL(GCODE_SEL, SEL_KPL));
2125 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL,
2126 GSEL(GCODE_SEL, SEL_KPL));
2127 initializecpu(); /* Initialize CPU registers */
2129 /* make an initial tss so cpu can get interrupt stack on syscall! */
2130 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
2131 PCPU_SET(common_tss.tss_esp0, thread0.td_kstack +
2132 KSTACK_PAGES * PAGE_SIZE - sizeof(struct pcb) - 16);
2133 PCPU_SET(common_tss.tss_ss0, GSEL(GDATA_SEL, SEL_KPL));
2134 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
2135 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
2136 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
2137 PCPU_SET(common_tss.tss_ioopt, (sizeof (struct i386tss)) << 16);
2140 /* pointer to selector slot for %fs/%gs */
2141 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
2143 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
2144 dblfault_tss.tss_esp2 = (int)&dblfault_stack[sizeof(dblfault_stack)];
2145 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
2146 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
2147 dblfault_tss.tss_cr3 = (int)IdlePTD;
2148 dblfault_tss.tss_eip = (int)dblfault_handler;
2149 dblfault_tss.tss_eflags = PSL_KERNEL;
2150 dblfault_tss.tss_ds = dblfault_tss.tss_es =
2151 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
2152 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
2153 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
2154 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
2158 init_param2(physmem);
2160 /* now running on new page tables, configured,and u/iom is accessible */
2162 msgbufinit(msgbufp, MSGBUF_SIZE);
2164 /* make a call gate to reenter kernel with */
2165 gdp = &ldt[LSYS5CALLS_SEL].gd;
2167 x = (int) &IDTVEC(lcall_syscall);
2168 gdp->gd_looffset = x;
2169 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
2171 gdp->gd_type = SDT_SYS386CGT;
2172 gdp->gd_dpl = SEL_UPL;
2174 gdp->gd_hioffset = x >> 16;
2176 /* XXX does this work? */
2178 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2179 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
2181 /* transfer to user mode */
2183 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
2184 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
2186 /* setup proc 0's pcb */
2187 thread0.td_pcb->pcb_flags = 0;
2188 thread0.td_pcb->pcb_cr3 = (int)IdlePTD;
2189 thread0.td_pcb->pcb_ext = 0;
2190 thread0.td_frame = &proc0_tf;
2194 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
2200 spinlock_enter(void)
2205 if (td->td_md.md_spinlock_count == 0)
2206 td->td_md.md_saved_flags = intr_disable();
2207 td->td_md.md_spinlock_count++;
2218 td->td_md.md_spinlock_count--;
2219 if (td->td_md.md_spinlock_count == 0)
2220 intr_restore(td->td_md.md_saved_flags);
2223 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2224 static void f00f_hack(void *unused);
2225 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
2228 f00f_hack(void *unused)
2230 struct gate_descriptor *new_idt;
2238 printf("Intel Pentium detected, installing workaround for F00F bug\n");
2240 tmp = kmem_alloc(kernel_map, PAGE_SIZE * 2);
2242 panic("kmem_alloc returned 0");
2244 /* Put the problematic entry (#6) at the end of the lower page. */
2245 new_idt = (struct gate_descriptor*)
2246 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
2247 bcopy(idt, new_idt, sizeof(idt0));
2248 r_idt.rd_base = (u_int)new_idt;
2251 if (vm_map_protect(kernel_map, tmp, tmp + PAGE_SIZE,
2252 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2253 panic("vm_map_protect failed");
2255 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2258 * Construct a PCB from a trapframe. This is called from kdb_trap() where
2259 * we want to start a backtrace from the function that caused us to enter
2260 * the debugger. We have the context in the trapframe, but base the trace
2261 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
2262 * enough for a backtrace.
2265 makectx(struct trapframe *tf, struct pcb *pcb)
2268 pcb->pcb_edi = tf->tf_edi;
2269 pcb->pcb_esi = tf->tf_esi;
2270 pcb->pcb_ebp = tf->tf_ebp;
2271 pcb->pcb_ebx = tf->tf_ebx;
2272 pcb->pcb_eip = tf->tf_eip;
2273 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
2277 ptrace_set_pc(struct thread *td, u_long addr)
2280 td->td_frame->tf_eip = addr;
2285 ptrace_single_step(struct thread *td)
2287 td->td_frame->tf_eflags |= PSL_T;
2292 ptrace_clear_single_step(struct thread *td)
2294 td->td_frame->tf_eflags &= ~PSL_T;
2299 fill_regs(struct thread *td, struct reg *regs)
2302 struct trapframe *tp;
2306 regs->r_fs = tp->tf_fs;
2307 regs->r_es = tp->tf_es;
2308 regs->r_ds = tp->tf_ds;
2309 regs->r_edi = tp->tf_edi;
2310 regs->r_esi = tp->tf_esi;
2311 regs->r_ebp = tp->tf_ebp;
2312 regs->r_ebx = tp->tf_ebx;
2313 regs->r_edx = tp->tf_edx;
2314 regs->r_ecx = tp->tf_ecx;
2315 regs->r_eax = tp->tf_eax;
2316 regs->r_eip = tp->tf_eip;
2317 regs->r_cs = tp->tf_cs;
2318 regs->r_eflags = tp->tf_eflags;
2319 regs->r_esp = tp->tf_esp;
2320 regs->r_ss = tp->tf_ss;
2321 regs->r_gs = pcb->pcb_gs;
2326 set_regs(struct thread *td, struct reg *regs)
2329 struct trapframe *tp;
2332 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2333 !CS_SECURE(regs->r_cs))
2336 tp->tf_fs = regs->r_fs;
2337 tp->tf_es = regs->r_es;
2338 tp->tf_ds = regs->r_ds;
2339 tp->tf_edi = regs->r_edi;
2340 tp->tf_esi = regs->r_esi;
2341 tp->tf_ebp = regs->r_ebp;
2342 tp->tf_ebx = regs->r_ebx;
2343 tp->tf_edx = regs->r_edx;
2344 tp->tf_ecx = regs->r_ecx;
2345 tp->tf_eax = regs->r_eax;
2346 tp->tf_eip = regs->r_eip;
2347 tp->tf_cs = regs->r_cs;
2348 tp->tf_eflags = regs->r_eflags;
2349 tp->tf_esp = regs->r_esp;
2350 tp->tf_ss = regs->r_ss;
2351 pcb->pcb_gs = regs->r_gs;
2355 #ifdef CPU_ENABLE_SSE
2357 fill_fpregs_xmm(sv_xmm, sv_87)
2358 struct savexmm *sv_xmm;
2359 struct save87 *sv_87;
2361 register struct env87 *penv_87 = &sv_87->sv_env;
2362 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2365 bzero(sv_87, sizeof(*sv_87));
2367 /* FPU control/status */
2368 penv_87->en_cw = penv_xmm->en_cw;
2369 penv_87->en_sw = penv_xmm->en_sw;
2370 penv_87->en_tw = penv_xmm->en_tw;
2371 penv_87->en_fip = penv_xmm->en_fip;
2372 penv_87->en_fcs = penv_xmm->en_fcs;
2373 penv_87->en_opcode = penv_xmm->en_opcode;
2374 penv_87->en_foo = penv_xmm->en_foo;
2375 penv_87->en_fos = penv_xmm->en_fos;
2378 for (i = 0; i < 8; ++i)
2379 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
2383 set_fpregs_xmm(sv_87, sv_xmm)
2384 struct save87 *sv_87;
2385 struct savexmm *sv_xmm;
2387 register struct env87 *penv_87 = &sv_87->sv_env;
2388 register struct envxmm *penv_xmm = &sv_xmm->sv_env;
2391 /* FPU control/status */
2392 penv_xmm->en_cw = penv_87->en_cw;
2393 penv_xmm->en_sw = penv_87->en_sw;
2394 penv_xmm->en_tw = penv_87->en_tw;
2395 penv_xmm->en_fip = penv_87->en_fip;
2396 penv_xmm->en_fcs = penv_87->en_fcs;
2397 penv_xmm->en_opcode = penv_87->en_opcode;
2398 penv_xmm->en_foo = penv_87->en_foo;
2399 penv_xmm->en_fos = penv_87->en_fos;
2402 for (i = 0; i < 8; ++i)
2403 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
2405 #endif /* CPU_ENABLE_SSE */
2408 fill_fpregs(struct thread *td, struct fpreg *fpregs)
2410 #ifdef CPU_ENABLE_SSE
2412 fill_fpregs_xmm(&td->td_pcb->pcb_save.sv_xmm,
2413 (struct save87 *)fpregs);
2416 #endif /* CPU_ENABLE_SSE */
2417 bcopy(&td->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
2422 set_fpregs(struct thread *td, struct fpreg *fpregs)
2424 #ifdef CPU_ENABLE_SSE
2426 set_fpregs_xmm((struct save87 *)fpregs,
2427 &td->td_pcb->pcb_save.sv_xmm);
2430 #endif /* CPU_ENABLE_SSE */
2431 bcopy(fpregs, &td->td_pcb->pcb_save.sv_87, sizeof *fpregs);
2436 * Get machine context.
2439 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
2441 struct trapframe *tp;
2442 struct segment_descriptor *sdp;
2446 PROC_LOCK(curthread->td_proc);
2447 mcp->mc_onstack = sigonstack(tp->tf_esp);
2448 PROC_UNLOCK(curthread->td_proc);
2449 mcp->mc_gs = td->td_pcb->pcb_gs;
2450 mcp->mc_fs = tp->tf_fs;
2451 mcp->mc_es = tp->tf_es;
2452 mcp->mc_ds = tp->tf_ds;
2453 mcp->mc_edi = tp->tf_edi;
2454 mcp->mc_esi = tp->tf_esi;
2455 mcp->mc_ebp = tp->tf_ebp;
2456 mcp->mc_isp = tp->tf_isp;
2457 mcp->mc_eflags = tp->tf_eflags;
2458 if (flags & GET_MC_CLEAR_RET) {
2461 mcp->mc_eflags &= ~PSL_C;
2463 mcp->mc_eax = tp->tf_eax;
2464 mcp->mc_edx = tp->tf_edx;
2466 mcp->mc_ebx = tp->tf_ebx;
2467 mcp->mc_ecx = tp->tf_ecx;
2468 mcp->mc_eip = tp->tf_eip;
2469 mcp->mc_cs = tp->tf_cs;
2470 mcp->mc_esp = tp->tf_esp;
2471 mcp->mc_ss = tp->tf_ss;
2472 mcp->mc_len = sizeof(*mcp);
2473 get_fpcontext(td, mcp);
2474 sdp = &td->td_pcb->pcb_gsd;
2475 mcp->mc_fsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2476 sdp = &td->td_pcb->pcb_fsd;
2477 mcp->mc_gsbase = sdp->sd_hibase << 24 | sdp->sd_lobase;
2483 * Set machine context.
2485 * However, we don't set any but the user modifiable flags, and we won't
2486 * touch the cs selector.
2489 set_mcontext(struct thread *td, const mcontext_t *mcp)
2491 struct trapframe *tp;
2495 if (mcp->mc_len != sizeof(*mcp))
2497 eflags = (mcp->mc_eflags & PSL_USERCHANGE) |
2498 (tp->tf_eflags & ~PSL_USERCHANGE);
2499 if ((ret = set_fpcontext(td, mcp)) == 0) {
2500 tp->tf_fs = mcp->mc_fs;
2501 tp->tf_es = mcp->mc_es;
2502 tp->tf_ds = mcp->mc_ds;
2503 tp->tf_edi = mcp->mc_edi;
2504 tp->tf_esi = mcp->mc_esi;
2505 tp->tf_ebp = mcp->mc_ebp;
2506 tp->tf_ebx = mcp->mc_ebx;
2507 tp->tf_edx = mcp->mc_edx;
2508 tp->tf_ecx = mcp->mc_ecx;
2509 tp->tf_eax = mcp->mc_eax;
2510 tp->tf_eip = mcp->mc_eip;
2511 tp->tf_eflags = eflags;
2512 tp->tf_esp = mcp->mc_esp;
2513 tp->tf_ss = mcp->mc_ss;
2514 td->td_pcb->pcb_gs = mcp->mc_gs;
2521 get_fpcontext(struct thread *td, mcontext_t *mcp)
2524 mcp->mc_fpformat = _MC_FPFMT_NODEV;
2525 mcp->mc_ownedfp = _MC_FPOWNED_NONE;
2527 union savefpu *addr;
2530 * XXX mc_fpstate might be misaligned, since its declaration is not
2531 * unportabilized using __attribute__((aligned(16))) like the
2532 * declaration of struct savemm, and anyway, alignment doesn't work
2533 * for auto variables since we don't use gcc's pessimal stack
2534 * alignment. Work around this by abusing the spare fields after
2537 * XXX unpessimize most cases by only aligning when fxsave might be
2538 * called, although this requires knowing too much about
2539 * npxgetregs()'s internals.
2541 addr = (union savefpu *)&mcp->mc_fpstate;
2542 if (td == PCPU_GET(fpcurthread) &&
2543 #ifdef CPU_ENABLE_SSE
2546 ((uintptr_t)(void *)addr & 0xF)) {
2548 addr = (void *)((char *)addr + 4);
2549 while ((uintptr_t)(void *)addr & 0xF);
2551 mcp->mc_ownedfp = npxgetregs(td, addr);
2552 if (addr != (union savefpu *)&mcp->mc_fpstate) {
2553 bcopy(addr, &mcp->mc_fpstate, sizeof(mcp->mc_fpstate));
2554 bzero(&mcp->mc_spare2, sizeof(mcp->mc_spare2));
2556 mcp->mc_fpformat = npxformat();
2561 set_fpcontext(struct thread *td, const mcontext_t *mcp)
2563 union savefpu *addr;
2565 if (mcp->mc_fpformat == _MC_FPFMT_NODEV)
2567 else if (mcp->mc_fpformat != _MC_FPFMT_387 &&
2568 mcp->mc_fpformat != _MC_FPFMT_XMM)
2570 else if (mcp->mc_ownedfp == _MC_FPOWNED_NONE)
2571 /* We don't care what state is left in the FPU or PCB. */
2573 else if (mcp->mc_ownedfp == _MC_FPOWNED_FPU ||
2574 mcp->mc_ownedfp == _MC_FPOWNED_PCB) {
2575 /* XXX align as above. */
2576 addr = (union savefpu *)&mcp->mc_fpstate;
2577 if (td == PCPU_GET(fpcurthread) &&
2578 #ifdef CPU_ENABLE_SSE
2581 ((uintptr_t)(void *)addr & 0xF)) {
2583 addr = (void *)((char *)addr + 4);
2584 while ((uintptr_t)(void *)addr & 0xF);
2585 bcopy(&mcp->mc_fpstate, addr, sizeof(mcp->mc_fpstate));
2588 #ifdef CPU_ENABLE_SSE
2590 addr->sv_xmm.sv_env.en_mxcsr &= cpu_mxcsr_mask;
2593 * XXX we violate the dubious requirement that npxsetregs()
2594 * be called with interrupts disabled.
2596 npxsetregs(td, addr);
2599 * Don't bother putting things back where they were in the
2600 * misaligned case, since we know that the caller won't use
2609 fpstate_drop(struct thread *td)
2615 if (PCPU_GET(fpcurthread) == td)
2619 * XXX force a full drop of the npx. The above only drops it if we
2620 * owned it. npxgetregs() has the same bug in the !cpu_fxsr case.
2622 * XXX I don't much like npxgetregs()'s semantics of doing a full
2623 * drop. Dropping only to the pcb matches fnsave's behaviour.
2624 * We only need to drop to !PCB_INITDONE in sendsig(). But
2625 * sendsig() is the only caller of npxgetregs()... perhaps we just
2626 * have too many layers.
2628 curthread->td_pcb->pcb_flags &= ~PCB_NPXINITDONE;
2633 fill_dbregs(struct thread *td, struct dbreg *dbregs)
2638 dbregs->dr[0] = rdr0();
2639 dbregs->dr[1] = rdr1();
2640 dbregs->dr[2] = rdr2();
2641 dbregs->dr[3] = rdr3();
2642 dbregs->dr[4] = rdr4();
2643 dbregs->dr[5] = rdr5();
2644 dbregs->dr[6] = rdr6();
2645 dbregs->dr[7] = rdr7();
2648 dbregs->dr[0] = pcb->pcb_dr0;
2649 dbregs->dr[1] = pcb->pcb_dr1;
2650 dbregs->dr[2] = pcb->pcb_dr2;
2651 dbregs->dr[3] = pcb->pcb_dr3;
2654 dbregs->dr[6] = pcb->pcb_dr6;
2655 dbregs->dr[7] = pcb->pcb_dr7;
2661 set_dbregs(struct thread *td, struct dbreg *dbregs)
2667 load_dr0(dbregs->dr[0]);
2668 load_dr1(dbregs->dr[1]);
2669 load_dr2(dbregs->dr[2]);
2670 load_dr3(dbregs->dr[3]);
2671 load_dr4(dbregs->dr[4]);
2672 load_dr5(dbregs->dr[5]);
2673 load_dr6(dbregs->dr[6]);
2674 load_dr7(dbregs->dr[7]);
2677 * Don't let an illegal value for dr7 get set. Specifically,
2678 * check for undefined settings. Setting these bit patterns
2679 * result in undefined behaviour and can lead to an unexpected
2682 for (i = 0; i < 4; i++) {
2683 if (DBREG_DR7_ACCESS(dbregs->dr[7], i) == 0x02)
2685 if (DBREG_DR7_LEN(dbregs->dr[7], i) == 0x02)
2692 * Don't let a process set a breakpoint that is not within the
2693 * process's address space. If a process could do this, it
2694 * could halt the system by setting a breakpoint in the kernel
2695 * (if ddb was enabled). Thus, we need to check to make sure
2696 * that no breakpoints are being enabled for addresses outside
2697 * process's address space.
2699 * XXX - what about when the watched area of the user's
2700 * address space is written into from within the kernel
2701 * ... wouldn't that still cause a breakpoint to be generated
2702 * from within kernel mode?
2705 if (DBREG_DR7_ENABLED(dbregs->dr[7], 0)) {
2706 /* dr0 is enabled */
2707 if (dbregs->dr[0] >= VM_MAXUSER_ADDRESS)
2711 if (DBREG_DR7_ENABLED(dbregs->dr[7], 1)) {
2712 /* dr1 is enabled */
2713 if (dbregs->dr[1] >= VM_MAXUSER_ADDRESS)
2717 if (DBREG_DR7_ENABLED(dbregs->dr[7], 2)) {
2718 /* dr2 is enabled */
2719 if (dbregs->dr[2] >= VM_MAXUSER_ADDRESS)
2723 if (DBREG_DR7_ENABLED(dbregs->dr[7], 3)) {
2724 /* dr3 is enabled */
2725 if (dbregs->dr[3] >= VM_MAXUSER_ADDRESS)
2729 pcb->pcb_dr0 = dbregs->dr[0];
2730 pcb->pcb_dr1 = dbregs->dr[1];
2731 pcb->pcb_dr2 = dbregs->dr[2];
2732 pcb->pcb_dr3 = dbregs->dr[3];
2733 pcb->pcb_dr6 = dbregs->dr[6];
2734 pcb->pcb_dr7 = dbregs->dr[7];
2736 pcb->pcb_flags |= PCB_DBREGS;
2743 * Return > 0 if a hardware breakpoint has been hit, and the
2744 * breakpoint was in user space. Return 0, otherwise.
2747 user_dbreg_trap(void)
2749 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2750 u_int32_t bp; /* breakpoint bits extracted from dr6 */
2751 int nbp; /* number of breakpoints that triggered */
2752 caddr_t addr[4]; /* breakpoint addresses */
2756 if ((dr7 & 0x000000ff) == 0) {
2758 * all GE and LE bits in the dr7 register are zero,
2759 * thus the trap couldn't have been caused by the
2760 * hardware debug registers
2767 bp = dr6 & 0x0000000f;
2771 * None of the breakpoint bits are set meaning this
2772 * trap was not caused by any of the debug registers
2778 * at least one of the breakpoints were hit, check to see
2779 * which ones and if any of them are user space addresses
2783 addr[nbp++] = (caddr_t)rdr0();
2786 addr[nbp++] = (caddr_t)rdr1();
2789 addr[nbp++] = (caddr_t)rdr2();
2792 addr[nbp++] = (caddr_t)rdr3();
2795 for (i = 0; i < nbp; i++) {
2796 if (addr[i] < (caddr_t)VM_MAXUSER_ADDRESS) {
2798 * addr[i] is in user space
2805 * None of the breakpoints are in user space.
2813 * Provide inb() and outb() as functions. They are normally only available as
2814 * inline functions, thus cannot be called from the debugger.
2817 /* silence compiler warnings */
2818 u_char inb_(u_short);
2819 void outb_(u_short, u_char);
2828 outb_(u_short port, u_char data)