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
38 * $FreeBSD: src/sys/i386/i386/machdep.c,v 1.385.2.30 2003/05/31 08:48:05 alc Exp $
43 #include "opt_compat.h"
46 #include "opt_directio.h"
49 #include "opt_maxmem.h"
50 #include "opt_msgbuf.h"
51 #include "opt_perfmon.h"
53 #include "opt_userconfig.h"
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/sysproto.h>
58 #include <sys/signalvar.h>
59 #include <sys/kernel.h>
60 #include <sys/linker.h>
61 #include <sys/malloc.h>
65 #include <sys/reboot.h>
67 #include <sys/msgbuf.h>
68 #include <sys/sysent.h>
69 #include <sys/sysctl.h>
70 #include <sys/vmmeter.h>
72 #include <sys/usched.h>
76 #include <vm/vm_param.h>
78 #include <vm/vm_kern.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_pager.h>
83 #include <vm/vm_extern.h>
85 #include <sys/thread2.h>
86 #include <sys/mplock2.h>
87 #include <sys/mutex2.h>
95 #include <machine/cpu.h>
96 #include <machine/clock.h>
97 #include <machine/specialreg.h>
98 #include <machine/bootinfo.h>
99 #include <machine/md_var.h>
100 #include <machine/pcb_ext.h> /* pcb.h included via sys/user.h */
101 #include <machine/globaldata.h> /* CPU_prvspace */
102 #include <machine/smp.h>
104 #include <machine/perfmon.h>
106 #include <machine/cputypes.h>
107 #include <machine/intr_machdep.h>
110 #include <bus/isa/isa_device.h>
112 #include <machine_base/isa/isa_intr.h>
113 #include <bus/isa/rtc.h>
114 #include <machine/vm86.h>
115 #include <sys/random.h>
116 #include <sys/ptrace.h>
117 #include <machine/sigframe.h>
119 #include <sys/machintr.h>
120 #include <machine_base/icu/icu_abi.h>
121 #include <machine_base/icu/elcr_var.h>
122 #include <machine_base/apic/lapic.h>
123 #include <machine_base/apic/ioapic.h>
124 #include <machine_base/apic/ioapic_abi.h>
125 #include <machine/mptable.h>
127 #define PHYSMAP_ENTRIES 10
129 extern void init386(int first);
130 extern void dblfault_handler(void);
132 extern void printcpuinfo(void); /* XXX header file */
133 extern void finishidentcpu(void);
134 extern void panicifcpuunsupported(void);
135 extern void initializecpu(void);
137 static void cpu_startup(void *);
138 static void pic_finish(void *);
139 static void cpu_finish(void *);
140 #ifndef CPU_DISABLE_SSE
141 static void set_fpregs_xmm(struct save87 *, struct savexmm *);
142 static void fill_fpregs_xmm(struct savexmm *, struct save87 *);
143 #endif /* CPU_DISABLE_SSE */
145 extern void ffs_rawread_setup(void);
146 #endif /* DIRECTIO */
147 static void init_locks(void);
149 SYSINIT(cpu, SI_BOOT2_START_CPU, SI_ORDER_FIRST, cpu_startup, NULL)
150 SYSINIT(pic_finish, SI_BOOT2_FINISH_PIC, SI_ORDER_FIRST, pic_finish, NULL)
151 SYSINIT(cpu_finish, SI_BOOT2_FINISH_CPU, SI_ORDER_FIRST, cpu_finish, NULL)
153 int _udatasel, _ucodesel;
155 int64_t tsc_offsets[MAXCPU];
157 #if defined(SWTCH_OPTIM_STATS)
158 extern int swtch_optim_stats;
159 SYSCTL_INT(_debug, OID_AUTO, swtch_optim_stats,
160 CTLFLAG_RD, &swtch_optim_stats, 0, "");
161 SYSCTL_INT(_debug, OID_AUTO, tlb_flush_count,
162 CTLFLAG_RD, &tlb_flush_count, 0, "");
167 u_long ebda_addr = 0;
169 int imcr_present = 0;
171 int naps = 0; /* # of Applications processors */
172 struct mtx dt_lock; /* lock for GDT and LDT */
177 sysctl_hw_physmem(SYSCTL_HANDLER_ARGS)
179 u_long pmem = ctob(physmem);
181 int error = sysctl_handle_long(oidp, &pmem, 0, req);
185 SYSCTL_PROC(_hw, HW_PHYSMEM, physmem, CTLTYPE_ULONG|CTLFLAG_RD,
186 0, 0, sysctl_hw_physmem, "LU", "Total system memory in bytes (number of pages * page size)");
189 sysctl_hw_usermem(SYSCTL_HANDLER_ARGS)
191 int error = sysctl_handle_int(oidp, 0,
192 ctob(physmem - vmstats.v_wire_count), req);
196 SYSCTL_PROC(_hw, HW_USERMEM, usermem, CTLTYPE_INT|CTLFLAG_RD,
197 0, 0, sysctl_hw_usermem, "IU", "");
200 sysctl_hw_availpages(SYSCTL_HANDLER_ARGS)
202 int error = sysctl_handle_int(oidp, 0,
203 i386_btop(avail_end - avail_start), req);
207 SYSCTL_PROC(_hw, OID_AUTO, availpages, CTLTYPE_INT|CTLFLAG_RD,
208 0, 0, sysctl_hw_availpages, "I", "");
213 vm_paddr_t phys_avail[PHYSMAP_ENTRIES*2+2];
214 vm_paddr_t dump_avail[PHYSMAP_ENTRIES*2+2];
217 static vm_offset_t buffer_sva, buffer_eva;
218 vm_offset_t clean_sva, clean_eva;
219 static vm_offset_t pager_sva, pager_eva;
220 static struct trapframe proc0_tf;
223 cpu_startup(void *dummy)
227 vm_offset_t firstaddr;
230 * Good {morning,afternoon,evening,night}.
232 kprintf("%s", version);
235 panicifcpuunsupported();
239 kprintf("real memory = %ju (%ju MB)\n",
241 (intmax_t)Realmem / 1024 / 1024);
243 * Display any holes after the first chunk of extended memory.
248 kprintf("Physical memory chunk(s):\n");
249 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
250 vm_paddr_t size1 = phys_avail[indx + 1] - phys_avail[indx];
252 kprintf("0x%08llx - 0x%08llx, %llu bytes (%llu pages)\n",
253 phys_avail[indx], phys_avail[indx + 1] - 1, size1,
259 * Allocate space for system data structures.
260 * The first available kernel virtual address is in "v".
261 * As pages of kernel virtual memory are allocated, "v" is incremented.
262 * As pages of memory are allocated and cleared,
263 * "firstaddr" is incremented.
264 * An index into the kernel page table corresponding to the
265 * virtual memory address maintained in "v" is kept in "mapaddr".
269 * Make two passes. The first pass calculates how much memory is
270 * needed and allocates it. The second pass assigns virtual
271 * addresses to the various data structures.
275 v = (caddr_t)firstaddr;
277 #define valloc(name, type, num) \
278 (name) = (type *)v; v = (caddr_t)((name)+(num))
279 #define valloclim(name, type, num, lim) \
280 (name) = (type *)v; v = (caddr_t)((lim) = ((name)+(num)))
283 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
284 * For the first 64MB of ram nominally allocate sufficient buffers to
285 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
286 * buffers to cover 1/20 of our ram over 64MB. When auto-sizing
287 * the buffer cache we limit the eventual kva reservation to
290 * factor represents the 1/4 x ram conversion.
293 int factor = 4 * BKVASIZE / 1024;
294 int kbytes = physmem * (PAGE_SIZE / 1024);
298 nbuf += min((kbytes - 4096) / factor, 65536 / factor);
300 nbuf += (kbytes - 65536) * 2 / (factor * 5);
301 if (maxbcache && nbuf > maxbcache / BKVASIZE)
302 nbuf = maxbcache / BKVASIZE;
306 * Do not allow the buffer_map to be more then 1/2 the size of the
309 if (nbuf > (virtual_end - virtual_start) / (BKVASIZE * 2)) {
310 nbuf = (virtual_end - virtual_start) / (BKVASIZE * 2);
311 kprintf("Warning: nbufs capped at %ld\n", nbuf);
314 /* limit to 128 on i386 */
315 nswbuf = lmax(lmin(nbuf / 4, 128), 16);
317 if (nswbuf < NSWBUF_MIN)
324 valloc(swbuf, struct buf, nswbuf);
325 valloc(buf, struct buf, nbuf);
328 * End of first pass, size has been calculated so allocate memory
330 if (firstaddr == 0) {
331 size = (vm_size_t)(v - firstaddr);
332 firstaddr = kmem_alloc(&kernel_map, round_page(size));
334 panic("startup: no room for tables");
339 * End of second pass, addresses have been assigned
341 if ((vm_size_t)(v - firstaddr) != size)
342 panic("startup: table size inconsistency");
344 kmem_suballoc(&kernel_map, &clean_map, &clean_sva, &clean_eva,
345 (nbuf*BKVASIZE) + (nswbuf*MAXPHYS) + pager_map_size);
346 kmem_suballoc(&clean_map, &buffer_map, &buffer_sva, &buffer_eva,
348 buffer_map.system_map = 1;
349 kmem_suballoc(&clean_map, &pager_map, &pager_sva, &pager_eva,
350 (nswbuf*MAXPHYS) + pager_map_size);
351 pager_map.system_map = 1;
353 #if defined(USERCONFIG)
355 cninit(); /* the preferred console may have changed */
358 kprintf("avail memory = %ju (%ju MB)\n",
359 (intmax_t)ptoa(vmstats.v_free_count + vmstats.v_dma_pages),
360 (intmax_t)ptoa(vmstats.v_free_count + vmstats.v_dma_pages) /
364 * Set up buffers, so they can be used to read disk labels.
367 vm_pager_bufferinit();
371 cpu_finish(void *dummy __unused)
377 pic_finish(void *dummy __unused)
379 /* Log ELCR information */
382 /* Log MPTABLE information */
383 mptable_pci_int_dump();
386 MachIntrABI.finalize();
390 * Send an interrupt to process.
392 * Stack is set up to allow sigcode stored
393 * at top to call routine, followed by kcall
394 * to sigreturn routine below. After sigreturn
395 * resets the signal mask, the stack, and the
396 * frame pointer, it returns to the user
400 sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
402 struct lwp *lp = curthread->td_lwp;
403 struct proc *p = lp->lwp_proc;
404 struct trapframe *regs;
405 struct sigacts *psp = p->p_sigacts;
406 struct sigframe sf, *sfp;
409 regs = lp->lwp_md.md_regs;
410 oonstack = (lp->lwp_sigstk.ss_flags & SS_ONSTACK) ? 1 : 0;
412 /* save user context */
413 bzero(&sf, sizeof(struct sigframe));
414 sf.sf_uc.uc_sigmask = *mask;
415 sf.sf_uc.uc_stack = lp->lwp_sigstk;
416 sf.sf_uc.uc_mcontext.mc_onstack = oonstack;
417 bcopy(regs, &sf.sf_uc.uc_mcontext.mc_gs, sizeof(struct trapframe));
419 /* make the size of the saved context visible to userland */
420 sf.sf_uc.uc_mcontext.mc_len = sizeof(sf.sf_uc.uc_mcontext);
422 /* Allocate and validate space for the signal handler context. */
423 if ((lp->lwp_flags & LWP_ALTSTACK) != 0 && !oonstack &&
424 SIGISMEMBER(psp->ps_sigonstack, sig)) {
425 sfp = (struct sigframe *)(lp->lwp_sigstk.ss_sp +
426 lp->lwp_sigstk.ss_size - sizeof(struct sigframe));
427 lp->lwp_sigstk.ss_flags |= SS_ONSTACK;
429 sfp = (struct sigframe *)regs->tf_esp - 1;
432 /* Translate the signal is appropriate */
433 if (p->p_sysent->sv_sigtbl) {
434 if (sig <= p->p_sysent->sv_sigsize)
435 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
438 /* Build the argument list for the signal handler. */
440 sf.sf_ucontext = (register_t)&sfp->sf_uc;
441 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
442 /* Signal handler installed with SA_SIGINFO. */
443 sf.sf_siginfo = (register_t)&sfp->sf_si;
444 sf.sf_ahu.sf_action = (__siginfohandler_t *)catcher;
446 /* fill siginfo structure */
447 sf.sf_si.si_signo = sig;
448 sf.sf_si.si_code = code;
449 sf.sf_si.si_addr = (void*)regs->tf_err;
452 /* Old FreeBSD-style arguments. */
453 sf.sf_siginfo = code;
454 sf.sf_addr = regs->tf_err;
455 sf.sf_ahu.sf_handler = catcher;
459 * If we're a vm86 process, we want to save the segment registers.
460 * We also change eflags to be our emulated eflags, not the actual
463 if (regs->tf_eflags & PSL_VM) {
464 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
465 struct vm86_kernel *vm86 = &lp->lwp_thread->td_pcb->pcb_ext->ext_vm86;
467 sf.sf_uc.uc_mcontext.mc_gs = tf->tf_vm86_gs;
468 sf.sf_uc.uc_mcontext.mc_fs = tf->tf_vm86_fs;
469 sf.sf_uc.uc_mcontext.mc_es = tf->tf_vm86_es;
470 sf.sf_uc.uc_mcontext.mc_ds = tf->tf_vm86_ds;
472 if (vm86->vm86_has_vme == 0)
473 sf.sf_uc.uc_mcontext.mc_eflags =
474 (tf->tf_eflags & ~(PSL_VIF | PSL_VIP)) |
475 (vm86->vm86_eflags & (PSL_VIF | PSL_VIP));
478 * Clear PSL_NT to inhibit T_TSSFLT faults on return from
479 * syscalls made by the signal handler. This just avoids
480 * wasting time for our lazy fixup of such faults. PSL_NT
481 * does nothing in vm86 mode, but vm86 programs can set it
482 * almost legitimately in probes for old cpu types.
484 tf->tf_eflags &= ~(PSL_VM | PSL_NT | PSL_VIF | PSL_VIP);
488 * Save the FPU state and reinit the FP unit
490 npxpush(&sf.sf_uc.uc_mcontext);
493 * Copy the sigframe out to the user's stack.
495 if (copyout(&sf, sfp, sizeof(struct sigframe)) != 0) {
497 * Something is wrong with the stack pointer.
498 * ...Kill the process.
503 regs->tf_esp = (int)sfp;
504 regs->tf_eip = PS_STRINGS - *(p->p_sysent->sv_szsigcode);
507 * i386 abi specifies that the direction flag must be cleared
510 regs->tf_eflags &= ~(PSL_T|PSL_D);
512 regs->tf_cs = _ucodesel;
513 regs->tf_ds = _udatasel;
514 regs->tf_es = _udatasel;
517 * Allow the signal handler to inherit %fs in addition to %gs as
518 * the userland program might be using both.
520 * However, if a T_PROTFLT occured the segment registers could be
521 * totally broken. They must be reset in order to be able to
522 * return to userland.
524 if (regs->tf_trapno == T_PROTFLT) {
525 regs->tf_fs = _udatasel;
526 regs->tf_gs = _udatasel;
528 regs->tf_ss = _udatasel;
532 * Sanitize the trapframe for a virtual kernel passing control to a custom
533 * VM context. Remove any items that would otherwise create a privilage
536 * XXX at the moment we allow userland to set the resume flag. Is this a
540 cpu_sanitize_frame(struct trapframe *frame)
542 frame->tf_cs = _ucodesel;
543 frame->tf_ds = _udatasel;
544 frame->tf_es = _udatasel; /* XXX allow userland this one too? */
546 frame->tf_fs = _udatasel;
547 frame->tf_gs = _udatasel;
549 frame->tf_ss = _udatasel;
550 frame->tf_eflags &= (PSL_RF | PSL_USERCHANGE);
551 frame->tf_eflags |= PSL_RESERVED_DEFAULT | PSL_I;
556 cpu_sanitize_tls(struct savetls *tls)
558 struct segment_descriptor *desc;
561 for (i = 0; i < NGTLS; ++i) {
563 if (desc->sd_dpl == 0 && desc->sd_type == 0)
565 if (desc->sd_def32 == 0)
567 if (desc->sd_type != SDT_MEMRWA)
569 if (desc->sd_dpl != SEL_UPL)
571 if (desc->sd_xx != 0 || desc->sd_p != 1)
578 * sigreturn(ucontext_t *sigcntxp)
580 * System call to cleanup state after a signal
581 * has been taken. Reset signal mask and
582 * stack state from context left by sendsig (above).
583 * Return to previous pc and psl as specified by
584 * context left by sendsig. Check carefully to
585 * make sure that the user has not modified the
586 * state to gain improper privileges.
590 #define EFL_SECURE(ef, oef) ((((ef) ^ (oef)) & ~PSL_USERCHANGE) == 0)
591 #define CS_SECURE(cs) (ISPL(cs) == SEL_UPL)
594 sys_sigreturn(struct sigreturn_args *uap)
596 struct lwp *lp = curthread->td_lwp;
597 struct trapframe *regs;
605 * We have to copy the information into kernel space so userland
606 * can't modify it while we are sniffing it.
608 regs = lp->lwp_md.md_regs;
609 error = copyin(uap->sigcntxp, &uc, sizeof(uc));
613 eflags = ucp->uc_mcontext.mc_eflags;
615 if (eflags & PSL_VM) {
616 struct trapframe_vm86 *tf = (struct trapframe_vm86 *)regs;
617 struct vm86_kernel *vm86;
620 * if pcb_ext == 0 or vm86_inited == 0, the user hasn't
621 * set up the vm86 area, and we can't enter vm86 mode.
623 if (lp->lwp_thread->td_pcb->pcb_ext == 0)
625 vm86 = &lp->lwp_thread->td_pcb->pcb_ext->ext_vm86;
626 if (vm86->vm86_inited == 0)
629 /* go back to user mode if both flags are set */
630 if ((eflags & PSL_VIP) && (eflags & PSL_VIF))
631 trapsignal(lp, SIGBUS, 0);
633 if (vm86->vm86_has_vme) {
634 eflags = (tf->tf_eflags & ~VME_USERCHANGE) |
635 (eflags & VME_USERCHANGE) | PSL_VM;
637 vm86->vm86_eflags = eflags; /* save VIF, VIP */
638 eflags = (tf->tf_eflags & ~VM_USERCHANGE) |
639 (eflags & VM_USERCHANGE) | PSL_VM;
641 bcopy(&ucp->uc_mcontext.mc_gs, tf, sizeof(struct trapframe));
642 tf->tf_eflags = eflags;
643 tf->tf_vm86_ds = tf->tf_ds;
644 tf->tf_vm86_es = tf->tf_es;
645 tf->tf_vm86_fs = tf->tf_fs;
646 tf->tf_vm86_gs = tf->tf_gs;
647 tf->tf_ds = _udatasel;
648 tf->tf_es = _udatasel;
650 tf->tf_fs = _udatasel;
651 tf->tf_gs = _udatasel;
655 * Don't allow users to change privileged or reserved flags.
658 * XXX do allow users to change the privileged flag PSL_RF.
659 * The cpu sets PSL_RF in tf_eflags for faults. Debuggers
660 * should sometimes set it there too. tf_eflags is kept in
661 * the signal context during signal handling and there is no
662 * other place to remember it, so the PSL_RF bit may be
663 * corrupted by the signal handler without us knowing.
664 * Corruption of the PSL_RF bit at worst causes one more or
665 * one less debugger trap, so allowing it is fairly harmless.
667 if (!EFL_SECURE(eflags & ~PSL_RF, regs->tf_eflags & ~PSL_RF)) {
668 kprintf("sigreturn: eflags = 0x%x\n", eflags);
673 * Don't allow users to load a valid privileged %cs. Let the
674 * hardware check for invalid selectors, excess privilege in
675 * other selectors, invalid %eip's and invalid %esp's.
677 cs = ucp->uc_mcontext.mc_cs;
678 if (!CS_SECURE(cs)) {
679 kprintf("sigreturn: cs = 0x%x\n", cs);
680 trapsignal(lp, SIGBUS, T_PROTFLT);
683 bcopy(&ucp->uc_mcontext.mc_gs, regs, sizeof(struct trapframe));
687 * Restore the FPU state from the frame
690 npxpop(&ucp->uc_mcontext);
692 if (ucp->uc_mcontext.mc_onstack & 1)
693 lp->lwp_sigstk.ss_flags |= SS_ONSTACK;
695 lp->lwp_sigstk.ss_flags &= ~SS_ONSTACK;
697 lp->lwp_sigmask = ucp->uc_sigmask;
698 SIG_CANTMASK(lp->lwp_sigmask);
704 * Machine dependent boot() routine
706 * I haven't seen anything to put here yet
707 * Possibly some stuff might be grafted back here from boot()
715 * Shutdown the CPU as much as possible
721 __asm__ __volatile("hlt");
725 * cpu_idle() represents the idle LWKT. You cannot return from this function
726 * (unless you want to blow things up!). Instead we look for runnable threads
727 * and loop or halt as appropriate. Giant is not held on entry to the thread.
729 * The main loop is entered with a critical section held, we must release
730 * the critical section before doing anything else. lwkt_switch() will
731 * check for pending interrupts due to entering and exiting its own
734 * NOTE: On an SMP system we rely on a scheduler IPI to wake a HLTed cpu up.
736 * NOTE: cpu_idle_hlt again defaults to 2 (use ACPI sleep states). Set to
737 * 1 to just use hlt and for debugging purposes.
739 static int cpu_idle_hlt = 2;
740 static int cpu_idle_hltcnt;
741 static int cpu_idle_spincnt;
742 static u_int cpu_idle_repeat = 750;
743 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hlt, CTLFLAG_RW,
744 &cpu_idle_hlt, 0, "Idle loop HLT enable");
745 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_hltcnt, CTLFLAG_RW,
746 &cpu_idle_hltcnt, 0, "Idle loop entry halts");
747 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_spincnt, CTLFLAG_RW,
748 &cpu_idle_spincnt, 0, "Idle loop entry spins");
749 SYSCTL_INT(_machdep, OID_AUTO, cpu_idle_repeat, CTLFLAG_RW,
750 &cpu_idle_repeat, 0, "Idle entries before acpi hlt");
753 cpu_idle_default_hook(void)
756 * We must guarentee that hlt is exactly the instruction
759 __asm __volatile("sti; hlt");
762 /* Other subsystems (e.g., ACPI) can hook this later. */
763 void (*cpu_idle_hook)(void) = cpu_idle_default_hook;
768 globaldata_t gd = mycpu;
769 struct thread *td __debugvar = gd->gd_curthread;
774 KKASSERT(td->td_critcount == 0);
777 * See if there are any LWKTs ready to go.
782 * When halting inside a cli we must check for reqflags
783 * races, particularly [re]schedule requests. Running
784 * splz() does the job.
787 * 0 Never halt, just spin
789 * 1 Always use HLT (or MONITOR/MWAIT if avail).
790 * This typically eats more power than the
793 * 2 Use HLT/MONITOR/MWAIT up to a point and then
794 * use the ACPI halt (default). This is a hybrid
795 * approach. See machdep.cpu_idle_repeat.
797 * 3 Always use the ACPI halt. This typically
798 * eats the least amount of power but the cpu
799 * will be slow waking up. Slows down e.g.
800 * compiles and other pipe/event oriented stuff.
803 * NOTE: Interrupts are enabled and we are not in a critical
806 * NOTE: Preemptions do not reset gd_idle_repeat. Also we
807 * don't bother capping gd_idle_repeat, it is ok if
810 ++gd->gd_idle_repeat;
811 reqflags = gd->gd_reqflags;
812 quick = (cpu_idle_hlt == 1) ||
814 gd->gd_idle_repeat < cpu_idle_repeat);
816 if (quick && (cpu_mi_feature & CPU_MI_MONITOR) &&
817 (reqflags & RQF_IDLECHECK_WK_MASK) == 0) {
818 cpu_mmw_pause_int(&gd->gd_reqflags, reqflags);
820 } else if (cpu_idle_hlt) {
821 __asm __volatile("cli");
823 if ((gd->gd_reqflags & RQF_IDLECHECK_WK_MASK) == 0) {
825 cpu_idle_default_hook();
829 __asm __volatile("sti");
833 __asm __volatile("sti");
840 * This routine is called if a spinlock has been held through the
841 * exponential backoff period and is seriously contested. On a real cpu
845 cpu_spinlock_contested(void)
851 * Clear registers on exec
854 exec_setregs(u_long entry, u_long stack, u_long ps_strings)
856 struct thread *td = curthread;
857 struct lwp *lp = td->td_lwp;
858 struct pcb *pcb = td->td_pcb;
859 struct trapframe *regs = lp->lwp_md.md_regs;
861 /* was i386_user_cleanup() in NetBSD */
864 bzero((char *)regs, sizeof(struct trapframe));
865 regs->tf_eip = entry;
866 regs->tf_esp = stack;
867 regs->tf_eflags = PSL_USER | (regs->tf_eflags & PSL_T);
868 regs->tf_ss = _udatasel;
869 regs->tf_ds = _udatasel;
870 regs->tf_es = _udatasel;
871 regs->tf_fs = _udatasel;
872 regs->tf_gs = _udatasel;
873 regs->tf_cs = _ucodesel;
875 /* PS_STRINGS value for BSD/OS binaries. It is 0 for non-BSD/OS. */
876 regs->tf_ebx = ps_strings;
879 * Reset the hardware debug registers if they were in use.
880 * They won't have any meaning for the newly exec'd process.
882 if (pcb->pcb_flags & PCB_DBREGS) {
889 if (pcb == td->td_pcb) {
891 * Clear the debug registers on the running
892 * CPU, otherwise they will end up affecting
893 * the next process we switch to.
897 pcb->pcb_flags &= ~PCB_DBREGS;
901 * Initialize the math emulator (if any) for the current process.
902 * Actually, just clear the bit that says that the emulator has
903 * been initialized. Initialization is delayed until the process
904 * traps to the emulator (if it is done at all) mainly because
905 * emulators don't provide an entry point for initialization.
907 pcb->pcb_flags &= ~FP_SOFTFP;
910 * note: do not set CR0_TS here. npxinit() must do it after clearing
911 * gd_npxthread. Otherwise a preemptive interrupt thread may panic
915 load_cr0(rcr0() | CR0_MP);
918 /* Initialize the npx (if any) for the current process. */
919 npxinit(__INITIAL_NPXCW__);
924 * note: linux emulator needs edx to be 0x0 on entry, which is
925 * handled in execve simply by setting the 64 bit syscall
936 cr0 |= CR0_NE; /* Done by npxinit() */
937 cr0 |= CR0_MP | CR0_TS; /* Done at every execve() too. */
938 cr0 |= CR0_WP | CR0_AM;
944 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
947 error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2,
949 if (!error && req->newptr)
954 SYSCTL_PROC(_machdep, CPU_ADJKERNTZ, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
955 &adjkerntz, 0, sysctl_machdep_adjkerntz, "I", "");
957 SYSCTL_INT(_machdep, CPU_DISRTCSET, disable_rtc_set,
958 CTLFLAG_RW, &disable_rtc_set, 0, "");
960 SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo,
961 CTLFLAG_RD, &bootinfo, bootinfo, "");
963 SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock,
964 CTLFLAG_RW, &wall_cmos_clock, 0, "");
966 extern u_long bootdev; /* not a cdev_t - encoding is different */
967 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
968 CTLFLAG_RD, &bootdev, 0, "Boot device (not in cdev_t format)");
971 * Initialize 386 and configure to run kernel
975 * Initialize segments & interrupt table
979 union descriptor gdt[NGDT * MAXCPU]; /* global descriptor table */
980 static struct gate_descriptor idt0[NIDT];
981 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
982 union descriptor ldt[NLDT]; /* local descriptor table */
984 /* table descriptors - used to load tables by cpu */
985 struct region_descriptor r_gdt, r_idt;
987 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
988 extern int has_f00f_bug;
991 static struct i386tss dblfault_tss;
992 static char dblfault_stack[PAGE_SIZE];
994 extern struct user *proc0paddr;
997 /* software prototypes -- in more palatable form */
998 struct soft_segment_descriptor gdt_segs[] = {
999 /* GNULL_SEL 0 Null Descriptor */
1000 { 0x0, /* segment base address */
1002 0, /* segment type */
1003 0, /* segment descriptor priority level */
1004 0, /* segment descriptor present */
1006 0, /* default 32 vs 16 bit size */
1007 0 /* limit granularity (byte/page units)*/ },
1008 /* GCODE_SEL 1 Code Descriptor for kernel */
1009 { 0x0, /* segment base address */
1010 0xfffff, /* length - all address space */
1011 SDT_MEMERA, /* segment type */
1012 0, /* segment descriptor priority level */
1013 1, /* segment descriptor present */
1015 1, /* default 32 vs 16 bit size */
1016 1 /* limit granularity (byte/page units)*/ },
1017 /* GDATA_SEL 2 Data Descriptor for kernel */
1018 { 0x0, /* segment base address */
1019 0xfffff, /* length - all address space */
1020 SDT_MEMRWA, /* segment type */
1021 0, /* segment descriptor priority level */
1022 1, /* segment descriptor present */
1024 1, /* default 32 vs 16 bit size */
1025 1 /* limit granularity (byte/page units)*/ },
1026 /* GPRIV_SEL 3 SMP Per-Processor Private Data Descriptor */
1027 { 0x0, /* segment base address */
1028 0xfffff, /* length - all address space */
1029 SDT_MEMRWA, /* segment type */
1030 0, /* segment descriptor priority level */
1031 1, /* segment descriptor present */
1033 1, /* default 32 vs 16 bit size */
1034 1 /* limit granularity (byte/page units)*/ },
1035 /* GPROC0_SEL 4 Proc 0 Tss Descriptor */
1037 0x0, /* segment base address */
1038 sizeof(struct i386tss)-1,/* length - all address space */
1039 SDT_SYS386TSS, /* segment type */
1040 0, /* segment descriptor priority level */
1041 1, /* segment descriptor present */
1043 0, /* unused - default 32 vs 16 bit size */
1044 0 /* limit granularity (byte/page units)*/ },
1045 /* GLDT_SEL 5 LDT Descriptor */
1046 { (int) ldt, /* segment base address */
1047 sizeof(ldt)-1, /* length - all address space */
1048 SDT_SYSLDT, /* segment type */
1049 SEL_UPL, /* segment descriptor priority level */
1050 1, /* segment descriptor present */
1052 0, /* unused - default 32 vs 16 bit size */
1053 0 /* limit granularity (byte/page units)*/ },
1054 /* GUSERLDT_SEL 6 User LDT Descriptor per process */
1055 { (int) ldt, /* segment base address */
1056 (512 * sizeof(union descriptor)-1), /* length */
1057 SDT_SYSLDT, /* segment type */
1058 0, /* segment descriptor priority level */
1059 1, /* segment descriptor present */
1061 0, /* unused - default 32 vs 16 bit size */
1062 0 /* limit granularity (byte/page units)*/ },
1063 /* GTGATE_SEL 7 Null Descriptor - Placeholder */
1064 { 0x0, /* segment base address */
1065 0x0, /* length - all address space */
1066 0, /* segment type */
1067 0, /* segment descriptor priority level */
1068 0, /* segment descriptor present */
1070 0, /* default 32 vs 16 bit size */
1071 0 /* limit granularity (byte/page units)*/ },
1072 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
1073 { 0x400, /* segment base address */
1074 0xfffff, /* length */
1075 SDT_MEMRWA, /* segment type */
1076 0, /* segment descriptor priority level */
1077 1, /* segment descriptor present */
1079 1, /* default 32 vs 16 bit size */
1080 1 /* limit granularity (byte/page units)*/ },
1081 /* GPANIC_SEL 9 Panic Tss Descriptor */
1082 { (int) &dblfault_tss, /* segment base address */
1083 sizeof(struct i386tss)-1,/* length - all address space */
1084 SDT_SYS386TSS, /* segment type */
1085 0, /* segment descriptor priority level */
1086 1, /* segment descriptor present */
1088 0, /* unused - default 32 vs 16 bit size */
1089 0 /* limit granularity (byte/page units)*/ },
1090 /* GBIOSCODE32_SEL 10 BIOS 32-bit interface (32bit Code) */
1091 { 0, /* segment base address (overwritten) */
1092 0xfffff, /* length */
1093 SDT_MEMERA, /* segment type */
1094 0, /* segment descriptor priority level */
1095 1, /* segment descriptor present */
1097 0, /* default 32 vs 16 bit size */
1098 1 /* limit granularity (byte/page units)*/ },
1099 /* GBIOSCODE16_SEL 11 BIOS 32-bit interface (16bit Code) */
1100 { 0, /* segment base address (overwritten) */
1101 0xfffff, /* length */
1102 SDT_MEMERA, /* segment type */
1103 0, /* segment descriptor priority level */
1104 1, /* segment descriptor present */
1106 0, /* default 32 vs 16 bit size */
1107 1 /* limit granularity (byte/page units)*/ },
1108 /* GBIOSDATA_SEL 12 BIOS 32-bit interface (Data) */
1109 { 0, /* segment base address (overwritten) */
1110 0xfffff, /* length */
1111 SDT_MEMRWA, /* segment type */
1112 0, /* segment descriptor priority level */
1113 1, /* segment descriptor present */
1115 1, /* default 32 vs 16 bit size */
1116 1 /* limit granularity (byte/page units)*/ },
1117 /* GBIOSUTIL_SEL 13 BIOS 16-bit interface (Utility) */
1118 { 0, /* segment base address (overwritten) */
1119 0xfffff, /* length */
1120 SDT_MEMRWA, /* segment type */
1121 0, /* segment descriptor priority level */
1122 1, /* segment descriptor present */
1124 0, /* default 32 vs 16 bit size */
1125 1 /* limit granularity (byte/page units)*/ },
1126 /* GBIOSARGS_SEL 14 BIOS 16-bit interface (Arguments) */
1127 { 0, /* segment base address (overwritten) */
1128 0xfffff, /* length */
1129 SDT_MEMRWA, /* segment type */
1130 0, /* segment descriptor priority level */
1131 1, /* segment descriptor present */
1133 0, /* default 32 vs 16 bit size */
1134 1 /* limit granularity (byte/page units)*/ },
1135 /* GTLS_START 15 TLS */
1136 { 0x0, /* segment base address */
1138 0, /* segment type */
1139 0, /* segment descriptor priority level */
1140 0, /* segment descriptor present */
1142 0, /* default 32 vs 16 bit size */
1143 0 /* limit granularity (byte/page units)*/ },
1144 /* GTLS_START+1 16 TLS */
1145 { 0x0, /* segment base address */
1147 0, /* segment type */
1148 0, /* segment descriptor priority level */
1149 0, /* segment descriptor present */
1151 0, /* default 32 vs 16 bit size */
1152 0 /* limit granularity (byte/page units)*/ },
1153 /* GTLS_END 17 TLS */
1154 { 0x0, /* segment base address */
1156 0, /* segment type */
1157 0, /* segment descriptor priority level */
1158 0, /* segment descriptor present */
1160 0, /* default 32 vs 16 bit size */
1161 0 /* limit granularity (byte/page units)*/ },
1162 /* GNDIS_SEL 18 NDIS Descriptor */
1163 { 0x0, /* segment base address */
1165 0, /* segment type */
1166 0, /* segment descriptor priority level */
1167 0, /* segment descriptor present */
1169 0, /* default 32 vs 16 bit size */
1170 0 /* limit granularity (byte/page units)*/ },
1173 static struct soft_segment_descriptor ldt_segs[] = {
1174 /* Null Descriptor - overwritten by call gate */
1175 { 0x0, /* segment base address */
1176 0x0, /* length - all address space */
1177 0, /* segment type */
1178 0, /* segment descriptor priority level */
1179 0, /* segment descriptor present */
1181 0, /* default 32 vs 16 bit size */
1182 0 /* limit granularity (byte/page units)*/ },
1183 /* Null Descriptor - overwritten by call gate */
1184 { 0x0, /* segment base address */
1185 0x0, /* length - all address space */
1186 0, /* segment type */
1187 0, /* segment descriptor priority level */
1188 0, /* segment descriptor present */
1190 0, /* default 32 vs 16 bit size */
1191 0 /* limit granularity (byte/page units)*/ },
1192 /* Null Descriptor - overwritten by call gate */
1193 { 0x0, /* segment base address */
1194 0x0, /* length - all address space */
1195 0, /* segment type */
1196 0, /* segment descriptor priority level */
1197 0, /* segment descriptor present */
1199 0, /* default 32 vs 16 bit size */
1200 0 /* limit granularity (byte/page units)*/ },
1201 /* Code Descriptor for user */
1202 { 0x0, /* segment base address */
1203 0xfffff, /* length - all address space */
1204 SDT_MEMERA, /* segment type */
1205 SEL_UPL, /* segment descriptor priority level */
1206 1, /* segment descriptor present */
1208 1, /* default 32 vs 16 bit size */
1209 1 /* limit granularity (byte/page units)*/ },
1210 /* Null Descriptor - overwritten by call gate */
1211 { 0x0, /* segment base address */
1212 0x0, /* length - all address space */
1213 0, /* segment type */
1214 0, /* segment descriptor priority level */
1215 0, /* segment descriptor present */
1217 0, /* default 32 vs 16 bit size */
1218 0 /* limit granularity (byte/page units)*/ },
1219 /* Data Descriptor for user */
1220 { 0x0, /* segment base address */
1221 0xfffff, /* length - all address space */
1222 SDT_MEMRWA, /* segment type */
1223 SEL_UPL, /* segment descriptor priority level */
1224 1, /* segment descriptor present */
1226 1, /* default 32 vs 16 bit size */
1227 1 /* limit granularity (byte/page units)*/ },
1231 setidt(int idx, inthand_t *func, int typ, int dpl, int selec)
1233 struct gate_descriptor *ip;
1236 ip->gd_looffset = (int)func;
1237 ip->gd_selector = selec;
1243 ip->gd_hioffset = ((int)func)>>16 ;
1246 #define IDTVEC(name) __CONCAT(X,name)
1249 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
1250 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
1251 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
1252 IDTVEC(page), IDTVEC(mchk), IDTVEC(fpu), IDTVEC(align),
1253 IDTVEC(xmm), IDTVEC(syscall),
1256 IDTVEC(int0x80_syscall);
1258 #ifdef DEBUG_INTERRUPTS
1259 extern inthand_t *Xrsvdary[256];
1263 sdtossd(struct segment_descriptor *sd, struct soft_segment_descriptor *ssd)
1265 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
1266 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
1267 ssd->ssd_type = sd->sd_type;
1268 ssd->ssd_dpl = sd->sd_dpl;
1269 ssd->ssd_p = sd->sd_p;
1270 ssd->ssd_def32 = sd->sd_def32;
1271 ssd->ssd_gran = sd->sd_gran;
1275 * Populate the (physmap) array with base/bound pairs describing the
1276 * available physical memory in the system, then test this memory and
1277 * build the phys_avail array describing the actually-available memory.
1279 * If we cannot accurately determine the physical memory map, then use
1280 * value from the 0xE801 call, and failing that, the RTC.
1282 * Total memory size may be set by the kernel environment variable
1283 * hw.physmem or the compile-time define MAXMEM.
1286 getmemsize(int first)
1288 int i, physmap_idx, pa_indx, da_indx;
1290 u_int basemem, extmem;
1291 struct vm86frame vmf;
1292 struct vm86context vmc;
1294 vm_offset_t physmap[PHYSMAP_ENTRIES*2];
1302 quad_t dcons_addr, dcons_size;
1304 bzero(&vmf, sizeof(struct vm86frame));
1305 bzero(physmap, sizeof(physmap));
1309 * Some newer BIOSes has broken INT 12H implementation which cause
1310 * kernel panic immediately. In this case, we need to scan SMAP
1311 * with INT 15:E820 first, then determine base memory size.
1314 TUNABLE_INT_FETCH("hw.hasbrokenint12", &hasbrokenint12);
1315 if (hasbrokenint12) {
1320 * Perform "base memory" related probes & setup. If we get a crazy
1321 * value give the bios some scribble space just in case.
1323 vm86_intcall(0x12, &vmf);
1324 basemem = vmf.vmf_ax;
1325 if (basemem > 640) {
1326 kprintf("Preposterous BIOS basemem of %uK, "
1327 "truncating to < 640K\n", basemem);
1332 * XXX if biosbasemem is now < 640, there is a `hole'
1333 * between the end of base memory and the start of
1334 * ISA memory. The hole may be empty or it may
1335 * contain BIOS code or data. Map it read/write so
1336 * that the BIOS can write to it. (Memory from 0 to
1337 * the physical end of the kernel is mapped read-only
1338 * to begin with and then parts of it are remapped.
1339 * The parts that aren't remapped form holes that
1340 * remain read-only and are unused by the kernel.
1341 * The base memory area is below the physical end of
1342 * the kernel and right now forms a read-only hole.
1343 * The part of it from PAGE_SIZE to
1344 * (trunc_page(biosbasemem * 1024) - 1) will be
1345 * remapped and used by the kernel later.)
1347 * This code is similar to the code used in
1348 * pmap_mapdev, but since no memory needs to be
1349 * allocated we simply change the mapping.
1351 for (pa = trunc_page(basemem * 1024);
1352 pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1353 pte = vtopte(pa + KERNBASE);
1354 *pte = pa | PG_RW | PG_V;
1358 * if basemem != 640, map pages r/w into vm86 page table so
1359 * that the bios can scribble on it.
1362 for (i = basemem / 4; i < 160; i++)
1363 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1367 * map page 1 R/W into the kernel page table so we can use it
1368 * as a buffer. The kernel will unmap this page later.
1370 pte = vtopte(KERNBASE + (1 << PAGE_SHIFT));
1371 *pte = (1 << PAGE_SHIFT) | PG_RW | PG_V;
1374 * get memory map with INT 15:E820
1376 #define SMAPSIZ sizeof(*smap)
1377 #define SMAP_SIG 0x534D4150 /* 'SMAP' */
1380 smap = (void *)vm86_addpage(&vmc, 1, KERNBASE + (1 << PAGE_SHIFT));
1381 vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
1386 vmf.vmf_eax = 0xE820;
1387 vmf.vmf_edx = SMAP_SIG;
1388 vmf.vmf_ecx = SMAPSIZ;
1389 i = vm86_datacall(0x15, &vmf, &vmc);
1390 if (i || vmf.vmf_eax != SMAP_SIG)
1392 if (boothowto & RB_VERBOSE)
1393 kprintf("SMAP type=%02x base=%08x %08x len=%08x %08x\n",
1395 *(u_int32_t *)((char *)&smap->base + 4),
1396 (u_int32_t)smap->base,
1397 *(u_int32_t *)((char *)&smap->length + 4),
1398 (u_int32_t)smap->length);
1400 if (smap->type != 0x01)
1403 if (smap->length == 0)
1406 Realmem += smap->length;
1408 if (smap->base >= 0xffffffffLLU) {
1409 kprintf("%ju MB of memory above 4GB ignored\n",
1410 (uintmax_t)(smap->length / 1024 / 1024));
1414 for (i = 0; i <= physmap_idx; i += 2) {
1415 if (smap->base < physmap[i + 1]) {
1416 if (boothowto & RB_VERBOSE) {
1417 kprintf("Overlapping or non-montonic "
1418 "memory region, ignoring "
1421 Realmem -= smap->length;
1426 if (smap->base == physmap[physmap_idx + 1]) {
1427 physmap[physmap_idx + 1] += smap->length;
1432 if (physmap_idx == PHYSMAP_ENTRIES*2) {
1433 kprintf("Too many segments in the physical "
1434 "address map, giving up\n");
1437 physmap[physmap_idx] = smap->base;
1438 physmap[physmap_idx + 1] = smap->base + smap->length;
1440 ; /* fix GCC3.x warning */
1441 } while (vmf.vmf_ebx != 0);
1444 * Perform "base memory" related probes & setup based on SMAP
1447 for (i = 0; i <= physmap_idx; i += 2) {
1448 if (physmap[i] == 0x00000000) {
1449 basemem = physmap[i + 1] / 1024;
1458 if (basemem > 640) {
1459 kprintf("Preposterous BIOS basemem of %uK, "
1460 "truncating to 640K\n", basemem);
1464 for (pa = trunc_page(basemem * 1024);
1465 pa < ISA_HOLE_START; pa += PAGE_SIZE) {
1466 pte = vtopte(pa + KERNBASE);
1467 *pte = pa | PG_RW | PG_V;
1471 for (i = basemem / 4; i < 160; i++)
1472 pte[i] = (i << PAGE_SHIFT) | PG_V | PG_RW | PG_U;
1475 if (physmap[1] != 0)
1479 * If we failed above, try memory map with INT 15:E801
1481 vmf.vmf_ax = 0xE801;
1482 if (vm86_intcall(0x15, &vmf) == 0) {
1483 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1487 vm86_intcall(0x15, &vmf);
1488 extmem = vmf.vmf_ax;
1491 * Prefer the RTC value for extended memory.
1493 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1498 * Special hack for chipsets that still remap the 384k hole when
1499 * there's 16MB of memory - this really confuses people that
1500 * are trying to use bus mastering ISA controllers with the
1501 * "16MB limit"; they only have 16MB, but the remapping puts
1502 * them beyond the limit.
1504 * If extended memory is between 15-16MB (16-17MB phys address range),
1507 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1511 physmap[1] = basemem * 1024;
1513 physmap[physmap_idx] = 0x100000;
1514 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1518 * Now, physmap contains a map of physical memory.
1521 base_memory = physmap[1];
1522 /* make hole for AP bootstrap code YYY */
1523 physmap[1] = mp_bootaddress(base_memory);
1525 /* Save EBDA address, if any */
1526 ebda_addr = (u_long)(*(u_short *)(KERNBASE + 0x40e));
1530 * Maxmem isn't the "maximum memory", it's one larger than the
1531 * highest page of the physical address space. It should be
1532 * called something like "Maxphyspage". We may adjust this
1533 * based on ``hw.physmem'' and the results of the memory test.
1535 Maxmem = atop(physmap[physmap_idx + 1]);
1538 Maxmem = MAXMEM / 4;
1541 if (kgetenv_quad("hw.physmem", &maxmem))
1542 Maxmem = atop(maxmem);
1544 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1545 (boothowto & RB_VERBOSE))
1546 kprintf("Physical memory use set to %lluK\n", Maxmem * 4);
1549 * If Maxmem has been increased beyond what the system has detected,
1550 * extend the last memory segment to the new limit.
1552 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1553 physmap[physmap_idx + 1] = ptoa(Maxmem);
1555 /* call pmap initialization to make new kernel address space */
1556 pmap_bootstrap(first, 0);
1559 * Size up each available chunk of physical memory.
1561 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1564 phys_avail[pa_indx++] = physmap[0];
1565 phys_avail[pa_indx] = physmap[0];
1566 dump_avail[da_indx] = physmap[0];
1571 * Get dcons buffer address
1573 if (kgetenv_quad("dcons.addr", &dcons_addr) == 0 ||
1574 kgetenv_quad("dcons.size", &dcons_size) == 0)
1578 * physmap is in bytes, so when converting to page boundaries,
1579 * round up the start address and round down the end address.
1581 for (i = 0; i <= physmap_idx; i += 2) {
1585 if (physmap[i + 1] < end)
1586 end = trunc_page(physmap[i + 1]);
1587 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1588 int tmp, page_bad, full;
1592 int *ptr = (int *)CADDR1;
1597 * block out kernel memory as not available.
1599 if (pa >= 0x100000 && pa < first)
1603 * block out dcons buffer
1606 && pa >= trunc_page(dcons_addr)
1607 && pa < dcons_addr + dcons_size)
1613 * map page into kernel: valid, read/write,non-cacheable
1615 *pte = pa | PG_V | PG_RW | PG_N;
1620 * Test for alternating 1's and 0's
1622 *(volatile int *)ptr = 0xaaaaaaaa;
1623 if (*(volatile int *)ptr != 0xaaaaaaaa) {
1627 * Test for alternating 0's and 1's
1629 *(volatile int *)ptr = 0x55555555;
1630 if (*(volatile int *)ptr != 0x55555555) {
1636 *(volatile int *)ptr = 0xffffffff;
1637 if (*(volatile int *)ptr != 0xffffffff) {
1643 *(volatile int *)ptr = 0x0;
1644 if (*(volatile int *)ptr != 0x0) {
1648 * Restore original value.
1653 * Adjust array of valid/good pages.
1655 if (page_bad == TRUE) {
1659 * If this good page is a continuation of the
1660 * previous set of good pages, then just increase
1661 * the end pointer. Otherwise start a new chunk.
1662 * Note that "end" points one higher than end,
1663 * making the range >= start and < end.
1664 * If we're also doing a speculative memory
1665 * test and we at or past the end, bump up Maxmem
1666 * so that we keep going. The first bad page
1667 * will terminate the loop.
1669 if (phys_avail[pa_indx] == pa) {
1670 phys_avail[pa_indx] += PAGE_SIZE;
1673 if (pa_indx >= PHYSMAP_ENTRIES*2) {
1674 kprintf("Too many holes in the physical address space, giving up\n");
1679 phys_avail[pa_indx++] = pa; /* start */
1680 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1684 if (dump_avail[da_indx] == pa) {
1685 dump_avail[da_indx] += PAGE_SIZE;
1688 if (da_indx >= PHYSMAP_ENTRIES*2) {
1692 dump_avail[da_indx++] = pa; /* start */
1693 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
1706 * The last chunk must contain at least one page plus the message
1707 * buffer to avoid complicating other code (message buffer address
1708 * calculation, etc.).
1710 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1711 round_page(MSGBUF_SIZE) >= phys_avail[pa_indx]) {
1712 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1713 phys_avail[pa_indx--] = 0;
1714 phys_avail[pa_indx--] = 0;
1717 Maxmem = atop(phys_avail[pa_indx]);
1719 /* Trim off space for the message buffer. */
1720 phys_avail[pa_indx] -= round_page(MSGBUF_SIZE);
1722 avail_end = phys_avail[pa_indx];
1725 struct machintr_abi MachIntrABI;
1736 * 7 Device Not Available (x87)
1738 * 9 Coprocessor Segment overrun (unsupported, reserved)
1740 * 11 Segment not present
1742 * 13 General Protection
1745 * 16 x87 FP Exception pending
1746 * 17 Alignment Check
1748 * 19 SIMD floating point
1750 * 32-255 INTn/external sources
1755 struct gate_descriptor *gdp;
1756 int gsel_tss, metadata_missing, off, x;
1757 struct mdglobaldata *gd;
1760 * Prevent lowering of the ipl if we call tsleep() early.
1762 gd = &CPU_prvspace[0].mdglobaldata;
1763 bzero(gd, sizeof(*gd));
1765 gd->mi.gd_curthread = &thread0;
1766 thread0.td_gd = &gd->mi;
1768 atdevbase = ISA_HOLE_START + KERNBASE;
1770 metadata_missing = 0;
1771 if (bootinfo.bi_modulep) {
1772 preload_metadata = (caddr_t)bootinfo.bi_modulep + KERNBASE;
1773 preload_bootstrap_relocate(KERNBASE);
1775 metadata_missing = 1;
1777 if (bootinfo.bi_envp)
1778 kern_envp = (caddr_t)bootinfo.bi_envp + KERNBASE;
1780 if (boothowto & RB_VERBOSE)
1784 * Default MachIntrABI to ICU
1786 MachIntrABI = MachIntrABI_ICU;
1788 TUNABLE_INT_FETCH("hw.apic_io_enable", &ioapic_enable); /* for compat */
1789 TUNABLE_INT_FETCH("hw.ioapic_enable", &ioapic_enable);
1790 TUNABLE_INT_FETCH("hw.lapic_enable", &lapic_enable);
1793 * Some of the virtual machines do not work w/ I/O APIC
1794 * enabled. If the user does not explicitly enable or
1795 * disable the I/O APIC (ioapic_enable < 0), then we
1796 * disable I/O APIC on all virtual machines.
1798 if (ioapic_enable < 0) {
1799 if (cpu_feature2 & CPUID2_VMM)
1806 * start with one cpu. Note: with one cpu, ncpus2_shift, ncpus2_mask,
1807 * and ncpus_fit_mask remain 0.
1812 /* Init basic tunables, hz etc */
1816 * make gdt memory segments, the code segment goes up to end of the
1817 * page with etext in it, the data segment goes to the end of
1821 * XXX text protection is temporarily (?) disabled. The limit was
1822 * i386_btop(round_page(etext)) - 1.
1824 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
1825 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
1827 gdt_segs[GPRIV_SEL].ssd_limit =
1828 atop(sizeof(struct privatespace) - 1);
1829 gdt_segs[GPRIV_SEL].ssd_base = (int) &CPU_prvspace[0];
1830 gdt_segs[GPROC0_SEL].ssd_base =
1831 (int) &CPU_prvspace[0].mdglobaldata.gd_common_tss;
1833 gd->mi.gd_prvspace = &CPU_prvspace[0];
1836 * Note: on both UP and SMP curthread must be set non-NULL
1837 * early in the boot sequence because the system assumes
1838 * that 'curthread' is never NULL.
1841 for (x = 0; x < NGDT; x++) {
1843 /* avoid overwriting db entries with APM ones */
1844 if (x >= GAPMCODE32_SEL && x <= GAPMDATA_SEL)
1847 ssdtosd(&gdt_segs[x], &gdt[x].sd);
1850 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
1851 r_gdt.rd_base = (int) gdt;
1854 mi_gdinit(&gd->mi, 0);
1856 mi_proc0init(&gd->mi, proc0paddr);
1857 safepri = TDPRI_MAX;
1859 /* make ldt memory segments */
1861 * XXX - VM_MAX_USER_ADDRESS is an end address, not a max. And it
1862 * should be spelled ...MAX_USER...
1864 ldt_segs[LUCODE_SEL].ssd_limit = atop(VM_MAX_USER_ADDRESS - 1);
1865 ldt_segs[LUDATA_SEL].ssd_limit = atop(VM_MAX_USER_ADDRESS - 1);
1866 for (x = 0; x < NELEM(ldt_segs); x++)
1867 ssdtosd(&ldt_segs[x], &ldt[x].sd);
1869 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1871 gd->gd_currentldt = _default_ldt;
1872 /* spinlocks and the BGL */
1876 * Setup the hardware exception table. Most exceptions use
1877 * SDT_SYS386TGT, known as a 'trap gate'. Trap gates leave
1878 * interrupts enabled. VM page faults use SDT_SYS386IGT, known as
1879 * an 'interrupt trap gate', which disables interrupts on entry,
1880 * in order to be able to poll the appropriate CRn register to
1881 * determine the fault address.
1883 for (x = 0; x < NIDT; x++) {
1884 #ifdef DEBUG_INTERRUPTS
1885 setidt(x, Xrsvdary[x], SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1887 setidt(x, &IDTVEC(rsvd0), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1890 setidt(0, &IDTVEC(div), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1891 setidt(1, &IDTVEC(dbg), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1892 setidt(2, &IDTVEC(nmi), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1893 setidt(3, &IDTVEC(bpt), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1894 setidt(4, &IDTVEC(ofl), SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1895 setidt(5, &IDTVEC(bnd), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1896 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1897 setidt(7, &IDTVEC(dna), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1898 setidt(8, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL, SEL_KPL));
1899 setidt(9, &IDTVEC(fpusegm), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1900 setidt(10, &IDTVEC(tss), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1901 setidt(11, &IDTVEC(missing), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1902 setidt(12, &IDTVEC(stk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1903 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1904 setidt(14, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1905 setidt(15, &IDTVEC(rsvd0), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1906 setidt(16, &IDTVEC(fpu), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1907 setidt(17, &IDTVEC(align), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1908 setidt(18, &IDTVEC(mchk), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1909 setidt(19, &IDTVEC(xmm), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1910 setidt(0x80, &IDTVEC(int0x80_syscall),
1911 SDT_SYS386TGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1913 r_idt.rd_limit = sizeof(idt0) - 1;
1914 r_idt.rd_base = (int) idt;
1918 * Initialize the console before we print anything out.
1922 if (metadata_missing)
1923 kprintf("WARNING: loader(8) metadata is missing!\n");
1932 * Initialize IRQ mapping
1935 * SHOULD be after elcr_probe()
1937 MachIntrABI_ICU.initmap();
1938 MachIntrABI_IOAPIC.initmap();
1942 if (boothowto & RB_KDB)
1943 Debugger("Boot flags requested debugger");
1946 finishidentcpu(); /* Final stage of CPU initialization */
1947 setidt(6, &IDTVEC(ill), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1948 setidt(13, &IDTVEC(prot), SDT_SYS386TGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1949 initializecpu(); /* Initialize CPU registers */
1952 * make an initial tss so cpu can get interrupt stack on syscall!
1953 * The 16 bytes is to save room for a VM86 context.
1955 gd->gd_common_tss.tss_esp0 = (int) thread0.td_pcb - 16;
1956 gd->gd_common_tss.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL) ;
1957 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1958 gd->gd_tss_gdt = &gdt[GPROC0_SEL].sd;
1959 gd->gd_common_tssd = *gd->gd_tss_gdt;
1960 gd->gd_common_tss.tss_ioopt = (sizeof gd->gd_common_tss) << 16;
1963 dblfault_tss.tss_esp = dblfault_tss.tss_esp0 = dblfault_tss.tss_esp1 =
1964 dblfault_tss.tss_esp2 = (int) &dblfault_stack[sizeof(dblfault_stack)];
1965 dblfault_tss.tss_ss = dblfault_tss.tss_ss0 = dblfault_tss.tss_ss1 =
1966 dblfault_tss.tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1967 dblfault_tss.tss_cr3 = (int)IdlePTD;
1968 dblfault_tss.tss_eip = (int) dblfault_handler;
1969 dblfault_tss.tss_eflags = PSL_KERNEL;
1970 dblfault_tss.tss_ds = dblfault_tss.tss_es =
1971 dblfault_tss.tss_gs = GSEL(GDATA_SEL, SEL_KPL);
1972 dblfault_tss.tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
1973 dblfault_tss.tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1974 dblfault_tss.tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1978 init_param2(physmem);
1980 /* now running on new page tables, configured,and u/iom is accessible */
1982 /* Map the message buffer. */
1983 for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
1984 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
1986 msgbufinit(msgbufp, MSGBUF_SIZE);
1988 /* make a call gate to reenter kernel with */
1989 gdp = &ldt[LSYS5CALLS_SEL].gd;
1991 x = (int) &IDTVEC(syscall);
1992 gdp->gd_looffset = x++;
1993 gdp->gd_selector = GSEL(GCODE_SEL,SEL_KPL);
1995 gdp->gd_type = SDT_SYS386CGT;
1996 gdp->gd_dpl = SEL_UPL;
1998 gdp->gd_hioffset = ((int) &IDTVEC(syscall)) >>16;
2000 /* XXX does this work? */
2001 ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
2002 ldt[LSOL26CALLS_SEL] = ldt[LSYS5CALLS_SEL];
2004 /* transfer to user mode */
2006 _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
2007 _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
2009 /* setup proc 0's pcb */
2010 thread0.td_pcb->pcb_flags = 0;
2011 thread0.td_pcb->pcb_cr3 = (int)IdlePTD; /* should already be setup */
2012 thread0.td_pcb->pcb_ext = NULL;
2013 lwp0.lwp_md.md_regs = &proc0_tf;
2017 * Initialize machine-dependant portions of the global data structure.
2018 * Note that the global data area and cpu0's idlestack in the private
2019 * data space were allocated in locore.
2021 * Note: the idlethread's cpl is 0
2023 * WARNING! Called from early boot, 'mycpu' may not work yet.
2026 cpu_gdinit(struct mdglobaldata *gd, int cpu)
2029 gd->mi.gd_curthread = &gd->mi.gd_idlethread;
2031 lwkt_init_thread(&gd->mi.gd_idlethread,
2032 gd->mi.gd_prvspace->idlestack,
2033 sizeof(gd->mi.gd_prvspace->idlestack),
2035 lwkt_set_comm(&gd->mi.gd_idlethread, "idle_%d", cpu);
2036 gd->mi.gd_idlethread.td_switch = cpu_lwkt_switch;
2037 gd->mi.gd_idlethread.td_sp -= sizeof(void *);
2038 *(void **)gd->mi.gd_idlethread.td_sp = cpu_idle_restore;
2042 is_globaldata_space(vm_offset_t saddr, vm_offset_t eaddr)
2044 if (saddr >= (vm_offset_t)&CPU_prvspace[0] &&
2045 eaddr <= (vm_offset_t)&CPU_prvspace[MAXCPU]) {
2052 globaldata_find(int cpu)
2054 KKASSERT(cpu >= 0 && cpu < ncpus);
2055 return(&CPU_prvspace[cpu].mdglobaldata.mi);
2058 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
2059 static void f00f_hack(void *unused);
2060 SYSINIT(f00f_hack, SI_BOOT2_BIOS, SI_ORDER_ANY, f00f_hack, NULL);
2063 f00f_hack(void *unused)
2065 struct gate_descriptor *new_idt;
2071 kprintf("Intel Pentium detected, installing workaround for F00F bug\n");
2073 r_idt.rd_limit = sizeof(idt0) - 1;
2075 tmp = kmem_alloc(&kernel_map, PAGE_SIZE * 2);
2077 panic("kmem_alloc returned 0");
2078 if (((unsigned int)tmp & (PAGE_SIZE-1)) != 0)
2079 panic("kmem_alloc returned non-page-aligned memory");
2080 /* Put the first seven entries in the lower page */
2081 new_idt = (struct gate_descriptor*)(tmp + PAGE_SIZE - (7*8));
2082 bcopy(idt, new_idt, sizeof(idt0));
2083 r_idt.rd_base = (int)new_idt;
2086 if (vm_map_protect(&kernel_map, tmp, tmp + PAGE_SIZE,
2087 VM_PROT_READ, FALSE) != KERN_SUCCESS)
2088 panic("vm_map_protect failed");
2091 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
2094 ptrace_set_pc(struct lwp *lp, unsigned long addr)
2096 lp->lwp_md.md_regs->tf_eip = addr;
2101 ptrace_single_step(struct lwp *lp)
2103 lp->lwp_md.md_regs->tf_eflags |= PSL_T;
2108 fill_regs(struct lwp *lp, struct reg *regs)
2110 struct trapframe *tp;
2112 if ((tp = lp->lwp_md.md_regs) == NULL)
2114 regs->r_gs = tp->tf_gs;
2115 regs->r_fs = tp->tf_fs;
2116 regs->r_es = tp->tf_es;
2117 regs->r_ds = tp->tf_ds;
2118 regs->r_edi = tp->tf_edi;
2119 regs->r_esi = tp->tf_esi;
2120 regs->r_ebp = tp->tf_ebp;
2121 regs->r_ebx = tp->tf_ebx;
2122 regs->r_edx = tp->tf_edx;
2123 regs->r_ecx = tp->tf_ecx;
2124 regs->r_eax = tp->tf_eax;
2125 regs->r_eip = tp->tf_eip;
2126 regs->r_cs = tp->tf_cs;
2127 regs->r_eflags = tp->tf_eflags;
2128 regs->r_esp = tp->tf_esp;
2129 regs->r_ss = tp->tf_ss;
2134 set_regs(struct lwp *lp, struct reg *regs)
2136 struct trapframe *tp;
2138 tp = lp->lwp_md.md_regs;
2139 if (!EFL_SECURE(regs->r_eflags, tp->tf_eflags) ||
2140 !CS_SECURE(regs->r_cs))
2142 tp->tf_gs = regs->r_gs;
2143 tp->tf_fs = regs->r_fs;
2144 tp->tf_es = regs->r_es;
2145 tp->tf_ds = regs->r_ds;
2146 tp->tf_edi = regs->r_edi;
2147 tp->tf_esi = regs->r_esi;
2148 tp->tf_ebp = regs->r_ebp;
2149 tp->tf_ebx = regs->r_ebx;
2150 tp->tf_edx = regs->r_edx;
2151 tp->tf_ecx = regs->r_ecx;
2152 tp->tf_eax = regs->r_eax;
2153 tp->tf_eip = regs->r_eip;
2154 tp->tf_cs = regs->r_cs;
2155 tp->tf_eflags = regs->r_eflags;
2156 tp->tf_esp = regs->r_esp;
2157 tp->tf_ss = regs->r_ss;
2161 #ifndef CPU_DISABLE_SSE
2163 fill_fpregs_xmm(struct savexmm *sv_xmm, struct save87 *sv_87)
2165 struct env87 *penv_87 = &sv_87->sv_env;
2166 struct envxmm *penv_xmm = &sv_xmm->sv_env;
2169 /* FPU control/status */
2170 penv_87->en_cw = penv_xmm->en_cw;
2171 penv_87->en_sw = penv_xmm->en_sw;
2172 penv_87->en_tw = penv_xmm->en_tw;
2173 penv_87->en_fip = penv_xmm->en_fip;
2174 penv_87->en_fcs = penv_xmm->en_fcs;
2175 penv_87->en_opcode = penv_xmm->en_opcode;
2176 penv_87->en_foo = penv_xmm->en_foo;
2177 penv_87->en_fos = penv_xmm->en_fos;
2180 for (i = 0; i < 8; ++i)
2181 sv_87->sv_ac[i] = sv_xmm->sv_fp[i].fp_acc;
2185 set_fpregs_xmm(struct save87 *sv_87, struct savexmm *sv_xmm)
2187 struct env87 *penv_87 = &sv_87->sv_env;
2188 struct envxmm *penv_xmm = &sv_xmm->sv_env;
2191 /* FPU control/status */
2192 penv_xmm->en_cw = penv_87->en_cw;
2193 penv_xmm->en_sw = penv_87->en_sw;
2194 penv_xmm->en_tw = penv_87->en_tw;
2195 penv_xmm->en_fip = penv_87->en_fip;
2196 penv_xmm->en_fcs = penv_87->en_fcs;
2197 penv_xmm->en_opcode = penv_87->en_opcode;
2198 penv_xmm->en_foo = penv_87->en_foo;
2199 penv_xmm->en_fos = penv_87->en_fos;
2202 for (i = 0; i < 8; ++i)
2203 sv_xmm->sv_fp[i].fp_acc = sv_87->sv_ac[i];
2205 #endif /* CPU_DISABLE_SSE */
2208 fill_fpregs(struct lwp *lp, struct fpreg *fpregs)
2210 if (lp->lwp_thread == NULL || lp->lwp_thread->td_pcb == NULL)
2212 #ifndef CPU_DISABLE_SSE
2214 fill_fpregs_xmm(&lp->lwp_thread->td_pcb->pcb_save.sv_xmm,
2215 (struct save87 *)fpregs);
2218 #endif /* CPU_DISABLE_SSE */
2219 bcopy(&lp->lwp_thread->td_pcb->pcb_save.sv_87, fpregs, sizeof *fpregs);
2224 set_fpregs(struct lwp *lp, struct fpreg *fpregs)
2226 #ifndef CPU_DISABLE_SSE
2228 set_fpregs_xmm((struct save87 *)fpregs,
2229 &lp->lwp_thread->td_pcb->pcb_save.sv_xmm);
2232 #endif /* CPU_DISABLE_SSE */
2233 bcopy(fpregs, &lp->lwp_thread->td_pcb->pcb_save.sv_87, sizeof *fpregs);
2238 fill_dbregs(struct lwp *lp, struct dbreg *dbregs)
2243 dbregs->dr0 = rdr0();
2244 dbregs->dr1 = rdr1();
2245 dbregs->dr2 = rdr2();
2246 dbregs->dr3 = rdr3();
2247 dbregs->dr4 = rdr4();
2248 dbregs->dr5 = rdr5();
2249 dbregs->dr6 = rdr6();
2250 dbregs->dr7 = rdr7();
2253 if (lp->lwp_thread == NULL || (pcb = lp->lwp_thread->td_pcb) == NULL)
2255 dbregs->dr0 = pcb->pcb_dr0;
2256 dbregs->dr1 = pcb->pcb_dr1;
2257 dbregs->dr2 = pcb->pcb_dr2;
2258 dbregs->dr3 = pcb->pcb_dr3;
2261 dbregs->dr6 = pcb->pcb_dr6;
2262 dbregs->dr7 = pcb->pcb_dr7;
2267 set_dbregs(struct lwp *lp, struct dbreg *dbregs)
2270 load_dr0(dbregs->dr0);
2271 load_dr1(dbregs->dr1);
2272 load_dr2(dbregs->dr2);
2273 load_dr3(dbregs->dr3);
2274 load_dr4(dbregs->dr4);
2275 load_dr5(dbregs->dr5);
2276 load_dr6(dbregs->dr6);
2277 load_dr7(dbregs->dr7);
2280 struct ucred *ucred;
2282 uint32_t mask1, mask2;
2285 * Don't let an illegal value for dr7 get set. Specifically,
2286 * check for undefined settings. Setting these bit patterns
2287 * result in undefined behaviour and can lead to an unexpected
2290 for (i = 0, mask1 = 0x3<<16, mask2 = 0x2<<16; i < 8;
2291 i++, mask1 <<= 2, mask2 <<= 2)
2292 if ((dbregs->dr7 & mask1) == mask2)
2295 pcb = lp->lwp_thread->td_pcb;
2296 ucred = lp->lwp_proc->p_ucred;
2299 * Don't let a process set a breakpoint that is not within the
2300 * process's address space. If a process could do this, it
2301 * could halt the system by setting a breakpoint in the kernel
2302 * (if ddb was enabled). Thus, we need to check to make sure
2303 * that no breakpoints are being enabled for addresses outside
2304 * process's address space, unless, perhaps, we were called by
2307 * XXX - what about when the watched area of the user's
2308 * address space is written into from within the kernel
2309 * ... wouldn't that still cause a breakpoint to be generated
2310 * from within kernel mode?
2313 if (priv_check_cred(ucred, PRIV_ROOT, 0) != 0) {
2314 if (dbregs->dr7 & 0x3) {
2315 /* dr0 is enabled */
2316 if (dbregs->dr0 >= VM_MAX_USER_ADDRESS)
2320 if (dbregs->dr7 & (0x3<<2)) {
2321 /* dr1 is enabled */
2322 if (dbregs->dr1 >= VM_MAX_USER_ADDRESS)
2326 if (dbregs->dr7 & (0x3<<4)) {
2327 /* dr2 is enabled */
2328 if (dbregs->dr2 >= VM_MAX_USER_ADDRESS)
2332 if (dbregs->dr7 & (0x3<<6)) {
2333 /* dr3 is enabled */
2334 if (dbregs->dr3 >= VM_MAX_USER_ADDRESS)
2339 pcb->pcb_dr0 = dbregs->dr0;
2340 pcb->pcb_dr1 = dbregs->dr1;
2341 pcb->pcb_dr2 = dbregs->dr2;
2342 pcb->pcb_dr3 = dbregs->dr3;
2343 pcb->pcb_dr6 = dbregs->dr6;
2344 pcb->pcb_dr7 = dbregs->dr7;
2346 pcb->pcb_flags |= PCB_DBREGS;
2353 * Return > 0 if a hardware breakpoint has been hit, and the
2354 * breakpoint was in user space. Return 0, otherwise.
2357 user_dbreg_trap(void)
2359 u_int32_t dr7, dr6; /* debug registers dr6 and dr7 */
2360 u_int32_t bp; /* breakpoint bits extracted from dr6 */
2361 int nbp; /* number of breakpoints that triggered */
2362 caddr_t addr[4]; /* breakpoint addresses */
2366 if ((dr7 & 0x000000ff) == 0) {
2368 * all GE and LE bits in the dr7 register are zero,
2369 * thus the trap couldn't have been caused by the
2370 * hardware debug registers
2377 bp = dr6 & 0x0000000f;
2381 * None of the breakpoint bits are set meaning this
2382 * trap was not caused by any of the debug registers
2388 * at least one of the breakpoints were hit, check to see
2389 * which ones and if any of them are user space addresses
2393 addr[nbp++] = (caddr_t)rdr0();
2396 addr[nbp++] = (caddr_t)rdr1();
2399 addr[nbp++] = (caddr_t)rdr2();
2402 addr[nbp++] = (caddr_t)rdr3();
2405 for (i=0; i<nbp; i++) {
2407 (caddr_t)VM_MAX_USER_ADDRESS) {
2409 * addr[i] is in user space
2416 * None of the breakpoints are in user space.
2424 Debugger(const char *msg)
2426 kprintf("Debugger(\"%s\") called.\n", msg);
2433 * Provide inb() and outb() as functions. They are normally only
2434 * available as macros calling inlined functions, thus cannot be
2435 * called inside DDB.
2437 * The actual code is stolen from <machine/cpufunc.h>, and de-inlined.
2443 /* silence compiler warnings */
2445 void outb(u_int, u_char);
2452 * We use %%dx and not %1 here because i/o is done at %dx and not at
2453 * %edx, while gcc generates inferior code (movw instead of movl)
2454 * if we tell it to load (u_short) port.
2456 __asm __volatile("inb %%dx,%0" : "=a" (data) : "d" (port));
2461 outb(u_int port, u_char data)
2465 * Use an unnecessary assignment to help gcc's register allocator.
2466 * This make a large difference for gcc-1.40 and a tiny difference
2467 * for gcc-2.6.0. For gcc-1.40, al had to be ``asm("ax")'' for
2468 * best results. gcc-2.6.0 can't handle this.
2471 __asm __volatile("outb %0,%%dx" : : "a" (al), "d" (port));
2479 * initialize all the SMP locks
2482 /* critical region when masking or unmasking interupts */
2483 struct spinlock_deprecated imen_spinlock;
2485 /* critical region for old style disable_intr/enable_intr */
2486 struct spinlock_deprecated mpintr_spinlock;
2488 /* critical region around INTR() routines */
2489 struct spinlock_deprecated intr_spinlock;
2491 /* lock region used by kernel profiling */
2492 struct spinlock_deprecated mcount_spinlock;
2494 /* locks com (tty) data/hardware accesses: a FASTINTR() */
2495 struct spinlock_deprecated com_spinlock;
2497 /* lock regions around the clock hardware */
2498 struct spinlock_deprecated clock_spinlock;
2500 /* lock around the MP rendezvous */
2501 struct spinlock_deprecated smp_rv_spinlock;
2507 * Get the initial mplock with a count of 1 for the BSP.
2508 * This uses a LOGICAL cpu ID, ie BSP == 0.
2510 cpu_get_initial_mplock();
2512 spin_lock_init(&mcount_spinlock);
2513 spin_lock_init(&intr_spinlock);
2514 spin_lock_init(&mpintr_spinlock);
2515 spin_lock_init(&imen_spinlock);
2516 spin_lock_init(&smp_rv_spinlock);
2517 spin_lock_init(&com_spinlock);
2518 spin_lock_init(&clock_spinlock);
2520 /* our token pool needs to work early */
2521 lwkt_token_pool_init();