2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2018 The FreeBSD Foundation
5 * Copyright (c) 1992 Terrence R. Lambert.
6 * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
9 * This code is derived from software contributed to Berkeley by
12 * Portions of this software were developed by A. Joseph Koshy under
13 * sponsorship from the FreeBSD Foundation and Google, Inc.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)machdep.c 7.4 (Berkeley) 6/3/91
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
50 #include "opt_atpic.h"
55 #include "opt_kstack_pages.h"
56 #include "opt_maxmem.h"
57 #include "opt_mp_watchdog.h"
58 #include "opt_perfmon.h"
59 #include "opt_platform.h"
61 #include <sys/param.h>
63 #include <sys/systm.h>
67 #include <sys/callout.h>
70 #include <sys/eventhandler.h>
72 #include <sys/imgact.h>
74 #include <sys/kernel.h>
76 #include <sys/linker.h>
78 #include <sys/malloc.h>
79 #include <sys/memrange.h>
80 #include <sys/msgbuf.h>
81 #include <sys/mutex.h>
83 #include <sys/ptrace.h>
84 #include <sys/reboot.h>
86 #include <sys/rwlock.h>
87 #include <sys/sched.h>
88 #include <sys/signalvar.h>
90 #include <sys/syscallsubr.h>
91 #include <sys/sysctl.h>
92 #include <sys/sysent.h>
93 #include <sys/sysproto.h>
94 #include <sys/ucontext.h>
95 #include <sys/vmmeter.h>
98 #include <vm/vm_param.h>
99 #include <vm/vm_extern.h>
100 #include <vm/vm_kern.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_pager.h>
105 #include <vm/vm_phys.h>
106 #include <vm/vm_dumpset.h>
110 #error KDB must be enabled in order for DDB to work!
113 #include <ddb/db_sym.h>
118 #include <net/netisr.h>
120 #include <machine/bootinfo.h>
121 #include <machine/clock.h>
122 #include <machine/cpu.h>
123 #include <machine/cputypes.h>
124 #include <machine/intr_machdep.h>
126 #include <machine/md_var.h>
127 #include <machine/metadata.h>
128 #include <machine/mp_watchdog.h>
129 #include <machine/pc/bios.h>
130 #include <machine/pcb.h>
131 #include <machine/pcb_ext.h>
132 #include <machine/proc.h>
133 #include <machine/sigframe.h>
134 #include <machine/specialreg.h>
135 #include <machine/sysarch.h>
136 #include <machine/trap.h>
137 #include <x86/ucode.h>
138 #include <machine/vm86.h>
139 #include <x86/init.h>
141 #include <machine/perfmon.h>
144 #include <machine/smp.h>
151 #include <x86/apicvar.h>
155 #include <x86/isa/icu.h>
158 /* Sanity check for __curthread() */
159 CTASSERT(offsetof(struct pcpu, pc_curthread) == 0);
161 register_t init386(int first);
162 void dblfault_handler(void);
163 void identify_cpu(void);
165 static void cpu_startup(void *);
166 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
168 /* Intel ICH registers */
169 #define ICH_PMBASE 0x400
170 #define ICH_SMI_EN ICH_PMBASE + 0x30
172 int _udatasel, _ucodesel;
174 static int above4g_allow = 1;
175 static int above24g_allow = 0;
183 FEATURE(pae, "Physical Address Extensions");
186 struct kva_md_info kmi;
188 static struct trapframe proc0_tf;
189 struct pcpu __pcpu[MAXCPU];
191 static void i386_clock_source_init(void);
195 struct mem_range_softc mem_range_softc;
197 extern char start_exceptions[], end_exceptions[];
199 extern struct sysentvec elf32_freebsd_sysvec;
201 /* Default init_ops implementation. */
202 struct init_ops init_ops = {
203 .early_clock_source_init = i386_clock_source_init,
204 .early_delay = i8254_delay,
206 .msi_init = msi_init,
211 i386_clock_source_init(void)
225 * On MacBooks, we need to disallow the legacy USB circuit to
226 * generate an SMI# because this can cause several problems,
227 * namely: incorrect CPU frequency detection and failure to
229 * We do this by disabling a bit in the SMI_EN (SMI Control and
230 * Enable register) of the Intel ICH LPC Interface Bridge.
232 sysenv = kern_getenv("smbios.system.product");
233 if (sysenv != NULL) {
234 if (strncmp(sysenv, "MacBook1,1", 10) == 0 ||
235 strncmp(sysenv, "MacBook3,1", 10) == 0 ||
236 strncmp(sysenv, "MacBook4,1", 10) == 0 ||
237 strncmp(sysenv, "MacBookPro1,1", 13) == 0 ||
238 strncmp(sysenv, "MacBookPro1,2", 13) == 0 ||
239 strncmp(sysenv, "MacBookPro3,1", 13) == 0 ||
240 strncmp(sysenv, "MacBookPro4,1", 13) == 0 ||
241 strncmp(sysenv, "Macmini1,1", 10) == 0) {
243 printf("Disabling LEGACY_USB_EN bit on "
245 outl(ICH_SMI_EN, inl(ICH_SMI_EN) & ~0x8);
251 * Good {morning,afternoon,evening,night}.
255 panicifcpuunsupported();
261 * Display physical memory if SMBIOS reports reasonable amount.
264 sysenv = kern_getenv("smbios.memory.enabled");
265 if (sysenv != NULL) {
266 memsize = (uintmax_t)strtoul(sysenv, (char **)NULL, 10) << 10;
269 if (memsize < ptoa((uintmax_t)vm_free_count()))
270 memsize = ptoa((uintmax_t)Maxmem);
271 printf("real memory = %ju (%ju MB)\n", memsize, memsize >> 20);
272 realmem = atop(memsize);
275 * Display any holes after the first chunk of extended memory.
280 printf("Physical memory chunk(s):\n");
281 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
284 size = phys_avail[indx + 1] - phys_avail[indx];
286 "0x%016jx - 0x%016jx, %ju bytes (%ju pages)\n",
287 (uintmax_t)phys_avail[indx],
288 (uintmax_t)phys_avail[indx + 1] - 1,
289 (uintmax_t)size, (uintmax_t)size / PAGE_SIZE);
293 vm_ksubmap_init(&kmi);
295 printf("avail memory = %ju (%ju MB)\n",
296 ptoa((uintmax_t)vm_free_count()),
297 ptoa((uintmax_t)vm_free_count()) / 1048576);
300 * Set up buffers, so they can be used to read disk labels.
303 vm_pager_bufferinit();
315 * CR0_MP, CR0_NE and CR0_TS are set for NPX (FPU) support:
317 * Prepare to trap all ESC (i.e., NPX) instructions and all WAIT
318 * instructions. We must set the CR0_MP bit and use the CR0_TS
319 * bit to control the trap, because setting the CR0_EM bit does
320 * not cause WAIT instructions to trap. It's important to trap
321 * WAIT instructions - otherwise the "wait" variants of no-wait
322 * control instructions would degenerate to the "no-wait" variants
323 * after FP context switches but work correctly otherwise. It's
324 * particularly important to trap WAITs when there is no NPX -
325 * otherwise the "wait" variants would always degenerate.
327 * Try setting CR0_NE to get correct error reporting on 486DX's.
328 * Setting it should fail or do nothing on lesser processors.
330 cr0 |= CR0_MP | CR0_NE | CR0_TS | CR0_WP | CR0_AM;
335 u_long bootdev; /* not a struct cdev *- encoding is different */
336 SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev,
337 CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in struct cdev *format)");
339 static char bootmethod[16] = "BIOS";
340 SYSCTL_STRING(_machdep, OID_AUTO, bootmethod, CTLFLAG_RD, bootmethod, 0,
341 "System firmware boot method");
344 * Initialize 386 and configure to run kernel
348 * Initialize segments & interrupt table
353 struct mtx dt_lock; /* lock for GDT and LDT */
355 union descriptor gdt0[NGDT]; /* initial global descriptor table */
356 union descriptor *gdt = gdt0; /* global descriptor table */
358 union descriptor *ldt; /* local descriptor table */
360 static struct gate_descriptor idt0[NIDT];
361 struct gate_descriptor *idt = &idt0[0]; /* interrupt descriptor table */
363 static struct i386tss *dblfault_tss;
364 static char *dblfault_stack;
366 static struct i386tss common_tss0;
368 vm_offset_t proc0kstack;
371 * software prototypes -- in more palatable form.
373 * GCODE_SEL through GUDATA_SEL must be in this order for syscall/sysret
374 * GUFS_SEL and GUGS_SEL must be in this order (swtch.s knows it)
376 struct soft_segment_descriptor gdt_segs[] = {
377 /* GNULL_SEL 0 Null Descriptor */
383 .ssd_xx = 0, .ssd_xx1 = 0,
386 /* GPRIV_SEL 1 SMP Per-Processor Private Data Descriptor */
388 .ssd_limit = 0xfffff,
389 .ssd_type = SDT_MEMRWA,
392 .ssd_xx = 0, .ssd_xx1 = 0,
395 /* GUFS_SEL 2 %fs Descriptor for user */
397 .ssd_limit = 0xfffff,
398 .ssd_type = SDT_MEMRWA,
401 .ssd_xx = 0, .ssd_xx1 = 0,
404 /* GUGS_SEL 3 %gs Descriptor for user */
406 .ssd_limit = 0xfffff,
407 .ssd_type = SDT_MEMRWA,
410 .ssd_xx = 0, .ssd_xx1 = 0,
413 /* GCODE_SEL 4 Code Descriptor for kernel */
415 .ssd_limit = 0xfffff,
416 .ssd_type = SDT_MEMERA,
419 .ssd_xx = 0, .ssd_xx1 = 0,
422 /* GDATA_SEL 5 Data Descriptor for kernel */
424 .ssd_limit = 0xfffff,
425 .ssd_type = SDT_MEMRWA,
428 .ssd_xx = 0, .ssd_xx1 = 0,
431 /* GUCODE_SEL 6 Code Descriptor for user */
433 .ssd_limit = 0xfffff,
434 .ssd_type = SDT_MEMERA,
437 .ssd_xx = 0, .ssd_xx1 = 0,
440 /* GUDATA_SEL 7 Data Descriptor for user */
442 .ssd_limit = 0xfffff,
443 .ssd_type = SDT_MEMRWA,
446 .ssd_xx = 0, .ssd_xx1 = 0,
449 /* GBIOSLOWMEM_SEL 8 BIOS access to realmode segment 0x40, must be #8 in GDT */
451 .ssd_limit = 0xfffff,
452 .ssd_type = SDT_MEMRWA,
455 .ssd_xx = 0, .ssd_xx1 = 0,
458 /* GPROC0_SEL 9 Proc 0 Tss Descriptor */
461 .ssd_limit = sizeof(struct i386tss)-1,
462 .ssd_type = SDT_SYS386TSS,
465 .ssd_xx = 0, .ssd_xx1 = 0,
468 /* GLDT_SEL 10 LDT Descriptor */
470 .ssd_limit = sizeof(union descriptor) * NLDT - 1,
471 .ssd_type = SDT_SYSLDT,
474 .ssd_xx = 0, .ssd_xx1 = 0,
477 /* GUSERLDT_SEL 11 User LDT Descriptor per process */
479 .ssd_limit = (512 * sizeof(union descriptor)-1),
480 .ssd_type = SDT_SYSLDT,
483 .ssd_xx = 0, .ssd_xx1 = 0,
486 /* GPANIC_SEL 12 Panic Tss Descriptor */
488 .ssd_limit = sizeof(struct i386tss)-1,
489 .ssd_type = SDT_SYS386TSS,
492 .ssd_xx = 0, .ssd_xx1 = 0,
495 /* GBIOSCODE32_SEL 13 BIOS 32-bit interface (32bit Code) */
497 .ssd_limit = 0xfffff,
498 .ssd_type = SDT_MEMERA,
501 .ssd_xx = 0, .ssd_xx1 = 0,
504 /* GBIOSCODE16_SEL 14 BIOS 32-bit interface (16bit Code) */
506 .ssd_limit = 0xfffff,
507 .ssd_type = SDT_MEMERA,
510 .ssd_xx = 0, .ssd_xx1 = 0,
513 /* GBIOSDATA_SEL 15 BIOS 32-bit interface (Data) */
515 .ssd_limit = 0xfffff,
516 .ssd_type = SDT_MEMRWA,
519 .ssd_xx = 0, .ssd_xx1 = 0,
522 /* GBIOSUTIL_SEL 16 BIOS 16-bit interface (Utility) */
524 .ssd_limit = 0xfffff,
525 .ssd_type = SDT_MEMRWA,
528 .ssd_xx = 0, .ssd_xx1 = 0,
531 /* GBIOSARGS_SEL 17 BIOS 16-bit interface (Arguments) */
533 .ssd_limit = 0xfffff,
534 .ssd_type = SDT_MEMRWA,
537 .ssd_xx = 0, .ssd_xx1 = 0,
540 /* GNDIS_SEL 18 NDIS Descriptor */
546 .ssd_xx = 0, .ssd_xx1 = 0,
551 static struct soft_segment_descriptor ldt_segs[] = {
552 /* Null Descriptor - overwritten by call gate */
558 .ssd_xx = 0, .ssd_xx1 = 0,
561 /* Null Descriptor - overwritten by call gate */
567 .ssd_xx = 0, .ssd_xx1 = 0,
570 /* Null Descriptor - overwritten by call gate */
576 .ssd_xx = 0, .ssd_xx1 = 0,
579 /* Code Descriptor for user */
581 .ssd_limit = 0xfffff,
582 .ssd_type = SDT_MEMERA,
585 .ssd_xx = 0, .ssd_xx1 = 0,
588 /* Null Descriptor - overwritten by call gate */
594 .ssd_xx = 0, .ssd_xx1 = 0,
597 /* Data Descriptor for user */
599 .ssd_limit = 0xfffff,
600 .ssd_type = SDT_MEMRWA,
603 .ssd_xx = 0, .ssd_xx1 = 0,
611 setidt(int idx, inthand_t *func, int typ, int dpl, int selec)
615 off = func != NULL ? (uintptr_t)func + setidt_disp : 0;
616 setidt_nodisp(idx, off, typ, dpl, selec);
620 setidt_nodisp(int idx, uintptr_t off, int typ, int dpl, int selec)
622 struct gate_descriptor *ip;
625 ip->gd_looffset = off;
626 ip->gd_selector = selec;
632 ip->gd_hioffset = ((u_int)off) >> 16 ;
636 IDTVEC(div), IDTVEC(dbg), IDTVEC(nmi), IDTVEC(bpt), IDTVEC(ofl),
637 IDTVEC(bnd), IDTVEC(ill), IDTVEC(dna), IDTVEC(fpusegm),
638 IDTVEC(tss), IDTVEC(missing), IDTVEC(stk), IDTVEC(prot),
639 IDTVEC(page), IDTVEC(mchk), IDTVEC(rsvd), IDTVEC(fpu), IDTVEC(align),
645 IDTVEC(xen_intr_upcall),
647 IDTVEC(int0x80_syscall);
651 * Display the index and function name of any IDT entries that don't use
652 * the default 'rsvd' entry point.
654 DB_SHOW_COMMAND(idt, db_show_idt)
656 struct gate_descriptor *ip;
658 uintptr_t func, func_trm;
662 for (idx = 0; idx < NIDT && !db_pager_quit; idx++) {
663 if (ip->gd_type == SDT_SYSTASKGT) {
664 db_printf("%3d\t<TASK>\n", idx);
666 func = (ip->gd_hioffset << 16 | ip->gd_looffset);
667 if (func >= PMAP_TRM_MIN_ADDRESS) {
673 if (func != (uintptr_t)&IDTVEC(rsvd)) {
674 db_printf("%3d\t", idx);
675 db_printsym(func, DB_STGY_PROC);
677 db_printf(" (trampoline %#x)",
686 /* Show privileged registers. */
687 DB_SHOW_COMMAND(sysregs, db_show_sysregs)
692 db_printf("idtr\t0x%08x/%04x\n",
693 (u_int)(idtr >> 16), (u_int)idtr & 0xffff);
695 db_printf("gdtr\t0x%08x/%04x\n",
696 (u_int)(gdtr >> 16), (u_int)gdtr & 0xffff);
697 db_printf("ldtr\t0x%04x\n", rldt());
698 db_printf("tr\t0x%04x\n", rtr());
699 db_printf("cr0\t0x%08x\n", rcr0());
700 db_printf("cr2\t0x%08x\n", rcr2());
701 db_printf("cr3\t0x%08x\n", rcr3());
702 db_printf("cr4\t0x%08x\n", rcr4());
703 if (rcr4() & CR4_XSAVE)
704 db_printf("xcr0\t0x%016llx\n", rxcr(0));
705 if (amd_feature & (AMDID_NX | AMDID_LM))
706 db_printf("EFER\t0x%016llx\n", rdmsr(MSR_EFER));
707 if (cpu_feature2 & (CPUID2_VMX | CPUID2_SMX))
708 db_printf("FEATURES_CTL\t0x%016llx\n",
709 rdmsr(MSR_IA32_FEATURE_CONTROL));
710 if (((cpu_vendor_id == CPU_VENDOR_INTEL ||
711 cpu_vendor_id == CPU_VENDOR_AMD) && CPUID_TO_FAMILY(cpu_id) >= 6) ||
712 cpu_vendor_id == CPU_VENDOR_HYGON)
713 db_printf("DEBUG_CTL\t0x%016llx\n", rdmsr(MSR_DEBUGCTLMSR));
714 if (cpu_feature & CPUID_PAT)
715 db_printf("PAT\t0x%016llx\n", rdmsr(MSR_PAT));
718 DB_SHOW_COMMAND(dbregs, db_show_dbregs)
721 db_printf("dr0\t0x%08x\n", rdr0());
722 db_printf("dr1\t0x%08x\n", rdr1());
723 db_printf("dr2\t0x%08x\n", rdr2());
724 db_printf("dr3\t0x%08x\n", rdr3());
725 db_printf("dr6\t0x%08x\n", rdr6());
726 db_printf("dr7\t0x%08x\n", rdr7());
729 DB_SHOW_COMMAND(frame, db_show_frame)
731 struct trapframe *frame;
733 frame = have_addr ? (struct trapframe *)addr : curthread->td_frame;
734 printf("ss %#x esp %#x efl %#x cs %#x eip %#x\n",
735 frame->tf_ss, frame->tf_esp, frame->tf_eflags, frame->tf_cs,
737 printf("err %#x trapno %d\n", frame->tf_err, frame->tf_trapno);
738 printf("ds %#x es %#x fs %#x\n",
739 frame->tf_ds, frame->tf_es, frame->tf_fs);
740 printf("eax %#x ecx %#x edx %#x ebx %#x\n",
741 frame->tf_eax, frame->tf_ecx, frame->tf_edx, frame->tf_ebx);
742 printf("ebp %#x esi %#x edi %#x\n",
743 frame->tf_ebp, frame->tf_esi, frame->tf_edi);
750 struct segment_descriptor *sd;
751 struct soft_segment_descriptor *ssd;
753 ssd->ssd_base = (sd->sd_hibase << 24) | sd->sd_lobase;
754 ssd->ssd_limit = (sd->sd_hilimit << 16) | sd->sd_lolimit;
755 ssd->ssd_type = sd->sd_type;
756 ssd->ssd_dpl = sd->sd_dpl;
757 ssd->ssd_p = sd->sd_p;
758 ssd->ssd_def32 = sd->sd_def32;
759 ssd->ssd_gran = sd->sd_gran;
763 add_physmap_entry(uint64_t base, uint64_t length, vm_paddr_t *physmap,
767 int i, insert_idx, physmap_idx;
769 physmap_idx = *physmap_idxp;
774 lim = 0x100000000; /* 4G */
775 if (pae_mode && above4g_allow)
776 lim = above24g_allow ? -1ULL : 0x600000000; /* 24G */
778 printf("%uK of memory above %uGB ignored, pae %d "
779 "above4g_allow %d above24g_allow %d\n",
780 (u_int)(length / 1024), (u_int)(lim >> 30), pae_mode,
781 above4g_allow, above24g_allow);
784 if (base + length >= lim) {
785 ign = base + length - lim;
787 printf("%uK of memory above %uGB ignored, pae %d "
788 "above4g_allow %d above24g_allow %d\n",
789 (u_int)(ign / 1024), (u_int)(lim >> 30), pae_mode,
790 above4g_allow, above24g_allow);
794 * Find insertion point while checking for overlap. Start off by
795 * assuming the new entry will be added to the end.
797 insert_idx = physmap_idx + 2;
798 for (i = 0; i <= physmap_idx; i += 2) {
799 if (base < physmap[i + 1]) {
800 if (base + length <= physmap[i]) {
804 if (boothowto & RB_VERBOSE)
806 "Overlapping memory regions, ignoring second region\n");
811 /* See if we can prepend to the next entry. */
812 if (insert_idx <= physmap_idx && base + length == physmap[insert_idx]) {
813 physmap[insert_idx] = base;
817 /* See if we can append to the previous entry. */
818 if (insert_idx > 0 && base == physmap[insert_idx - 1]) {
819 physmap[insert_idx - 1] += length;
824 *physmap_idxp = physmap_idx;
825 if (physmap_idx == PHYS_AVAIL_ENTRIES) {
827 "Too many segments in the physical address map, giving up\n");
832 * Move the last 'N' entries down to make room for the new
835 for (i = physmap_idx; i > insert_idx; i -= 2) {
836 physmap[i] = physmap[i - 2];
837 physmap[i + 1] = physmap[i - 1];
840 /* Insert the new entry. */
841 physmap[insert_idx] = base;
842 physmap[insert_idx + 1] = base + length;
847 add_smap_entry(struct bios_smap *smap, vm_paddr_t *physmap, int *physmap_idxp)
849 if (boothowto & RB_VERBOSE)
850 printf("SMAP type=%02x base=%016llx len=%016llx\n",
851 smap->type, smap->base, smap->length);
853 if (smap->type != SMAP_TYPE_MEMORY)
856 return (add_physmap_entry(smap->base, smap->length, physmap,
861 add_smap_entries(struct bios_smap *smapbase, vm_paddr_t *physmap,
864 struct bios_smap *smap, *smapend;
867 * Memory map from INT 15:E820.
869 * subr_module.c says:
870 * "Consumer may safely assume that size value precedes data."
871 * ie: an int32_t immediately precedes SMAP.
873 smapsize = *((u_int32_t *)smapbase - 1);
874 smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
876 for (smap = smapbase; smap < smapend; smap++)
877 if (!add_smap_entry(smap, physmap, physmap_idxp))
886 printf("Preposterous BIOS basemem of %uK, truncating to 640K\n",
891 pmap_basemem_setup(basemem);
895 * Populate the (physmap) array with base/bound pairs describing the
896 * available physical memory in the system, then test this memory and
897 * build the phys_avail array describing the actually-available memory.
899 * If we cannot accurately determine the physical memory map, then use
900 * value from the 0xE801 call, and failing that, the RTC.
902 * Total memory size may be set by the kernel environment variable
903 * hw.physmem or the compile-time define MAXMEM.
905 * XXX first should be vm_paddr_t.
908 getmemsize(int first)
910 int has_smap, off, physmap_idx, pa_indx, da_indx;
912 vm_paddr_t physmap[PHYS_AVAIL_ENTRIES];
913 quad_t dcons_addr, dcons_size, physmem_tunable;
914 int hasbrokenint12, i, res;
916 struct vm86frame vmf;
917 struct vm86context vmc;
919 struct bios_smap *smap, *smapbase;
923 bzero(&vmf, sizeof(vmf));
924 bzero(physmap, sizeof(physmap));
928 * Tell the physical memory allocator about pages used to store
929 * the kernel and preloaded data. See kmem_bootstrap_free().
931 vm_phys_early_add_seg((vm_paddr_t)KERNLOAD, trunc_page(first));
933 TUNABLE_INT_FETCH("hw.above4g_allow", &above4g_allow);
934 TUNABLE_INT_FETCH("hw.above24g_allow", &above24g_allow);
937 * Check if the loader supplied an SMAP memory map. If so,
938 * use that and do not make any VM86 calls.
941 kmdp = preload_search_by_type("elf kernel");
943 kmdp = preload_search_by_type("elf32 kernel");
944 smapbase = (struct bios_smap *)preload_search_info(kmdp,
945 MODINFO_METADATA | MODINFOMD_SMAP);
946 if (smapbase != NULL) {
947 add_smap_entries(smapbase, physmap, &physmap_idx);
953 * Some newer BIOSes have a broken INT 12H implementation
954 * which causes a kernel panic immediately. In this case, we
955 * need use the SMAP to determine the base memory size.
958 TUNABLE_INT_FETCH("hw.hasbrokenint12", &hasbrokenint12);
959 if (hasbrokenint12 == 0) {
960 /* Use INT12 to determine base memory size. */
961 vm86_intcall(0x12, &vmf);
962 basemem = vmf.vmf_ax;
967 * Fetch the memory map with INT 15:E820. Map page 1 R/W into
968 * the kernel page table so we can use it as a buffer. The
969 * kernel will unmap this page later.
972 smap = (void *)vm86_addpage(&vmc, 1, PMAP_MAP_LOW + ptoa(1));
973 res = vm86_getptr(&vmc, (vm_offset_t)smap, &vmf.vmf_es, &vmf.vmf_di);
974 KASSERT(res != 0, ("vm86_getptr() failed: address not found"));
978 vmf.vmf_eax = 0xE820;
979 vmf.vmf_edx = SMAP_SIG;
980 vmf.vmf_ecx = sizeof(struct bios_smap);
981 i = vm86_datacall(0x15, &vmf, &vmc);
982 if (i || vmf.vmf_eax != SMAP_SIG)
985 if (!add_smap_entry(smap, physmap, &physmap_idx))
987 } while (vmf.vmf_ebx != 0);
991 * If we didn't fetch the "base memory" size from INT12,
992 * figure it out from the SMAP (or just guess).
995 for (i = 0; i <= physmap_idx; i += 2) {
996 if (physmap[i] == 0x00000000) {
997 basemem = physmap[i + 1] / 1024;
1002 /* XXX: If we couldn't find basemem from SMAP, just guess. */
1008 if (physmap[1] != 0)
1012 * If we failed to find an SMAP, figure out the extended
1013 * memory size. We will then build a simple memory map with
1014 * two segments, one for "base memory" and the second for
1015 * "extended memory". Note that "extended memory" starts at a
1016 * physical address of 1MB and that both basemem and extmem
1017 * are in units of 1KB.
1019 * First, try to fetch the extended memory size via INT 15:E801.
1021 vmf.vmf_ax = 0xE801;
1022 if (vm86_intcall(0x15, &vmf) == 0) {
1023 extmem = vmf.vmf_cx + vmf.vmf_dx * 64;
1026 * If INT15:E801 fails, this is our last ditch effort
1027 * to determine the extended memory size. Currently
1028 * we prefer the RTC value over INT15:88.
1032 vm86_intcall(0x15, &vmf);
1033 extmem = vmf.vmf_ax;
1035 extmem = rtcin(RTC_EXTLO) + (rtcin(RTC_EXTHI) << 8);
1040 * Special hack for chipsets that still remap the 384k hole when
1041 * there's 16MB of memory - this really confuses people that
1042 * are trying to use bus mastering ISA controllers with the
1043 * "16MB limit"; they only have 16MB, but the remapping puts
1044 * them beyond the limit.
1046 * If extended memory is between 15-16MB (16-17MB phys address range),
1049 if ((extmem > 15 * 1024) && (extmem < 16 * 1024))
1053 physmap[1] = basemem * 1024;
1055 physmap[physmap_idx] = 0x100000;
1056 physmap[physmap_idx + 1] = physmap[physmap_idx] + extmem * 1024;
1060 * Now, physmap contains a map of physical memory.
1064 /* make hole for AP bootstrap code */
1065 alloc_ap_trampoline(physmap, &physmap_idx);
1069 * Maxmem isn't the "maximum memory", it's one larger than the
1070 * highest page of the physical address space. It should be
1071 * called something like "Maxphyspage". We may adjust this
1072 * based on ``hw.physmem'' and the results of the memory test.
1074 * This is especially confusing when it is much larger than the
1075 * memory size and is displayed as "realmem".
1077 Maxmem = atop(physmap[physmap_idx + 1]);
1080 Maxmem = MAXMEM / 4;
1083 if (TUNABLE_QUAD_FETCH("hw.physmem", &physmem_tunable))
1084 Maxmem = atop(physmem_tunable);
1087 * If we have an SMAP, don't allow MAXMEM or hw.physmem to extend
1088 * the amount of memory in the system.
1090 if (has_smap && Maxmem > atop(physmap[physmap_idx + 1]))
1091 Maxmem = atop(physmap[physmap_idx + 1]);
1094 * The boot memory test is disabled by default, as it takes a
1095 * significant amount of time on large-memory systems, and is
1096 * unfriendly to virtual machines as it unnecessarily touches all
1099 * A general name is used as the code may be extended to support
1100 * additional tests beyond the current "page present" test.
1103 TUNABLE_ULONG_FETCH("hw.memtest.tests", &memtest);
1105 if (atop(physmap[physmap_idx + 1]) != Maxmem &&
1106 (boothowto & RB_VERBOSE))
1107 printf("Physical memory use set to %ldK\n", Maxmem * 4);
1110 * If Maxmem has been increased beyond what the system has detected,
1111 * extend the last memory segment to the new limit.
1113 if (atop(physmap[physmap_idx + 1]) < Maxmem)
1114 physmap[physmap_idx + 1] = ptoa((vm_paddr_t)Maxmem);
1116 /* call pmap initialization to make new kernel address space */
1117 pmap_bootstrap(first);
1120 * Size up each available chunk of physical memory.
1122 physmap[0] = PAGE_SIZE; /* mask off page 0 */
1125 phys_avail[pa_indx++] = physmap[0];
1126 phys_avail[pa_indx] = physmap[0];
1127 dump_avail[da_indx] = physmap[0];
1130 * Get dcons buffer address
1132 if (getenv_quad("dcons.addr", &dcons_addr) == 0 ||
1133 getenv_quad("dcons.size", &dcons_size) == 0)
1137 * physmap is in bytes, so when converting to page boundaries,
1138 * round up the start address and round down the end address.
1140 for (i = 0; i <= physmap_idx; i += 2) {
1143 end = ptoa((vm_paddr_t)Maxmem);
1144 if (physmap[i + 1] < end)
1145 end = trunc_page(physmap[i + 1]);
1146 for (pa = round_page(physmap[i]); pa < end; pa += PAGE_SIZE) {
1147 int tmp, page_bad, full;
1152 * block out kernel memory as not available.
1154 if (pa >= KERNLOAD && pa < first)
1158 * block out dcons buffer
1161 && pa >= trunc_page(dcons_addr)
1162 && pa < dcons_addr + dcons_size)
1170 * map page into kernel: valid, read/write,non-cacheable
1172 ptr = (int *)pmap_cmap3(pa, PG_V | PG_RW | PG_N);
1176 * Test for alternating 1's and 0's
1178 *(volatile int *)ptr = 0xaaaaaaaa;
1179 if (*(volatile int *)ptr != 0xaaaaaaaa)
1182 * Test for alternating 0's and 1's
1184 *(volatile int *)ptr = 0x55555555;
1185 if (*(volatile int *)ptr != 0x55555555)
1190 *(volatile int *)ptr = 0xffffffff;
1191 if (*(volatile int *)ptr != 0xffffffff)
1196 *(volatile int *)ptr = 0x0;
1197 if (*(volatile int *)ptr != 0x0)
1200 * Restore original value.
1206 * Adjust array of valid/good pages.
1208 if (page_bad == TRUE)
1211 * If this good page is a continuation of the
1212 * previous set of good pages, then just increase
1213 * the end pointer. Otherwise start a new chunk.
1214 * Note that "end" points one higher than end,
1215 * making the range >= start and < end.
1216 * If we're also doing a speculative memory
1217 * test and we at or past the end, bump up Maxmem
1218 * so that we keep going. The first bad page
1219 * will terminate the loop.
1221 if (phys_avail[pa_indx] == pa) {
1222 phys_avail[pa_indx] += PAGE_SIZE;
1225 if (pa_indx == PHYS_AVAIL_ENTRIES) {
1227 "Too many holes in the physical address space, giving up\n");
1232 phys_avail[pa_indx++] = pa; /* start */
1233 phys_avail[pa_indx] = pa + PAGE_SIZE; /* end */
1237 if (dump_avail[da_indx] == pa) {
1238 dump_avail[da_indx] += PAGE_SIZE;
1241 if (da_indx == PHYS_AVAIL_ENTRIES) {
1245 dump_avail[da_indx++] = pa; /* start */
1246 dump_avail[da_indx] = pa + PAGE_SIZE; /* end */
1257 * The last chunk must contain at least one page plus the message
1258 * buffer to avoid complicating other code (message buffer address
1259 * calculation, etc.).
1261 while (phys_avail[pa_indx - 1] + PAGE_SIZE +
1262 round_page(msgbufsize) >= phys_avail[pa_indx]) {
1263 physmem -= atop(phys_avail[pa_indx] - phys_avail[pa_indx - 1]);
1264 phys_avail[pa_indx--] = 0;
1265 phys_avail[pa_indx--] = 0;
1268 Maxmem = atop(phys_avail[pa_indx]);
1270 /* Trim off space for the message buffer. */
1271 phys_avail[pa_indx] -= round_page(msgbufsize);
1273 /* Map the message buffer. */
1274 for (off = 0; off < round_page(msgbufsize); off += PAGE_SIZE)
1275 pmap_kenter((vm_offset_t)msgbufp + off, phys_avail[pa_indx] +
1283 db_fetch_ksymtab(bootinfo.bi_symtab, bootinfo.bi_esymtab, 0);
1287 if (boothowto & RB_KDB)
1288 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
1295 struct gate_descriptor *ip;
1299 for (x = 0; x < NIDT; x++) {
1301 if (ip->gd_type != SDT_SYS386IGT &&
1302 ip->gd_type != SDT_SYS386TGT)
1304 off = ip->gd_looffset + (((u_int)ip->gd_hioffset) << 16);
1305 KASSERT(off >= (uintptr_t)start_exceptions &&
1306 off < (uintptr_t)end_exceptions,
1307 ("IDT[%d] type %d off %#x", x, ip->gd_type, off));
1309 MPASS(off >= PMAP_TRM_MIN_ADDRESS &&
1310 off < PMAP_TRM_MAX_ADDRESS);
1311 ip->gd_looffset = off;
1312 ip->gd_hioffset = off >> 16;
1322 for (x = 0; x < NIDT; x++)
1323 setidt(x, &IDTVEC(rsvd), SDT_SYS386IGT, SEL_KPL,
1324 GSEL(GCODE_SEL, SEL_KPL));
1325 setidt(IDT_DE, &IDTVEC(div), SDT_SYS386IGT, SEL_KPL,
1326 GSEL(GCODE_SEL, SEL_KPL));
1327 setidt(IDT_DB, &IDTVEC(dbg), SDT_SYS386IGT, SEL_KPL,
1328 GSEL(GCODE_SEL, SEL_KPL));
1329 setidt(IDT_NMI, &IDTVEC(nmi), SDT_SYS386IGT, SEL_KPL,
1330 GSEL(GCODE_SEL, SEL_KPL));
1331 setidt(IDT_BP, &IDTVEC(bpt), SDT_SYS386IGT, SEL_UPL,
1332 GSEL(GCODE_SEL, SEL_KPL));
1333 setidt(IDT_OF, &IDTVEC(ofl), SDT_SYS386IGT, SEL_UPL,
1334 GSEL(GCODE_SEL, SEL_KPL));
1335 setidt(IDT_BR, &IDTVEC(bnd), SDT_SYS386IGT, SEL_KPL,
1336 GSEL(GCODE_SEL, SEL_KPL));
1337 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386IGT, SEL_KPL,
1338 GSEL(GCODE_SEL, SEL_KPL));
1339 setidt(IDT_NM, &IDTVEC(dna), SDT_SYS386IGT, SEL_KPL,
1340 GSEL(GCODE_SEL, SEL_KPL));
1341 setidt(IDT_DF, 0, SDT_SYSTASKGT, SEL_KPL, GSEL(GPANIC_SEL,
1343 setidt(IDT_FPUGP, &IDTVEC(fpusegm), SDT_SYS386IGT,
1344 SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1345 setidt(IDT_TS, &IDTVEC(tss), SDT_SYS386IGT, SEL_KPL,
1346 GSEL(GCODE_SEL, SEL_KPL));
1347 setidt(IDT_NP, &IDTVEC(missing), SDT_SYS386IGT, SEL_KPL,
1348 GSEL(GCODE_SEL, SEL_KPL));
1349 setidt(IDT_SS, &IDTVEC(stk), SDT_SYS386IGT, SEL_KPL,
1350 GSEL(GCODE_SEL, SEL_KPL));
1351 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386IGT, SEL_KPL,
1352 GSEL(GCODE_SEL, SEL_KPL));
1353 setidt(IDT_PF, &IDTVEC(page), SDT_SYS386IGT, SEL_KPL,
1354 GSEL(GCODE_SEL, SEL_KPL));
1355 setidt(IDT_MF, &IDTVEC(fpu), SDT_SYS386IGT, SEL_KPL,
1356 GSEL(GCODE_SEL, SEL_KPL));
1357 setidt(IDT_AC, &IDTVEC(align), SDT_SYS386IGT, SEL_KPL,
1358 GSEL(GCODE_SEL, SEL_KPL));
1359 setidt(IDT_MC, &IDTVEC(mchk), SDT_SYS386IGT, SEL_KPL,
1360 GSEL(GCODE_SEL, SEL_KPL));
1361 setidt(IDT_XF, &IDTVEC(xmm), SDT_SYS386IGT, SEL_KPL,
1362 GSEL(GCODE_SEL, SEL_KPL));
1363 setidt(IDT_SYSCALL, &IDTVEC(int0x80_syscall),
1364 SDT_SYS386IGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1365 #ifdef KDTRACE_HOOKS
1366 setidt(IDT_DTRACE_RET, &IDTVEC(dtrace_ret),
1367 SDT_SYS386IGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
1370 setidt(IDT_EVTCHN, &IDTVEC(xen_intr_upcall),
1371 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1379 setidt(IDT_UD, &IDTVEC(ill), SDT_SYS386IGT, SEL_KPL,
1380 GSEL(GCODE_SEL, SEL_KPL));
1381 setidt(IDT_GP, &IDTVEC(prot), SDT_SYS386IGT, SEL_KPL,
1382 GSEL(GCODE_SEL, SEL_KPL));
1385 #if defined(DEV_ISA) && !defined(DEV_ATPIC)
1390 setidt(IDT_IO_INTS + 7, IDTVEC(spuriousint),
1391 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1392 setidt(IDT_IO_INTS + 15, IDTVEC(spuriousint),
1393 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
1400 struct region_descriptor r_gdt, r_idt; /* table descriptors */
1401 int gsel_tss, metadata_missing, x, pa;
1403 struct xstate_hdr *xhdr;
1409 thread0.td_kstack = proc0kstack;
1410 thread0.td_kstack_pages = TD0_KSTACK_PAGES;
1413 * This may be done better later if it gets more high level
1414 * components in it. If so just link td->td_proc here.
1416 proc_linkup0(&proc0, &thread0);
1418 if (bootinfo.bi_modulep) {
1419 metadata_missing = 0;
1420 addend = (vm_paddr_t)bootinfo.bi_modulep < KERNBASE ?
1422 preload_metadata = (caddr_t)bootinfo.bi_modulep + addend;
1423 preload_bootstrap_relocate(addend);
1425 metadata_missing = 1;
1428 if (bootinfo.bi_envp != 0) {
1429 addend = (vm_paddr_t)bootinfo.bi_envp < KERNBASE ?
1431 init_static_kenv((char *)bootinfo.bi_envp + addend, 0);
1433 init_static_kenv(NULL, 0);
1437 * Re-evaluate CPU features if we loaded a microcode update.
1439 ucode_len = ucode_load_bsp(first);
1440 if (ucode_len != 0) {
1442 first = roundup2(first + ucode_len, PAGE_SIZE);
1445 identify_hypervisor();
1447 /* Init basic tunables, hz etc */
1451 * Make gdt memory segments. All segments cover the full 4GB
1452 * of address space and permissions are enforced at page level.
1454 gdt_segs[GCODE_SEL].ssd_limit = atop(0 - 1);
1455 gdt_segs[GDATA_SEL].ssd_limit = atop(0 - 1);
1456 gdt_segs[GUCODE_SEL].ssd_limit = atop(0 - 1);
1457 gdt_segs[GUDATA_SEL].ssd_limit = atop(0 - 1);
1458 gdt_segs[GUFS_SEL].ssd_limit = atop(0 - 1);
1459 gdt_segs[GUGS_SEL].ssd_limit = atop(0 - 1);
1462 gdt_segs[GPRIV_SEL].ssd_limit = atop(0 - 1);
1463 gdt_segs[GPRIV_SEL].ssd_base = (int)pc;
1464 gdt_segs[GPROC0_SEL].ssd_base = (int)&common_tss0;
1466 for (x = 0; x < NGDT; x++)
1467 ssdtosd(&gdt_segs[x], &gdt0[x].sd);
1469 r_gdt.rd_limit = NGDT * sizeof(gdt0[0]) - 1;
1470 r_gdt.rd_base = (int)gdt0;
1471 mtx_init(&dt_lock, "descriptor tables", NULL, MTX_SPIN);
1474 pcpu_init(pc, 0, sizeof(struct pcpu));
1475 for (pa = first; pa < first + DPCPU_SIZE; pa += PAGE_SIZE)
1476 pmap_kenter(pa, pa);
1477 dpcpu_init((void *)first, 0);
1478 first += DPCPU_SIZE;
1479 PCPU_SET(prvspace, pc);
1480 PCPU_SET(curthread, &thread0);
1481 /* Non-late cninit() and printf() can be moved up to here. */
1484 * Initialize mutexes.
1486 * icu_lock: in order to allow an interrupt to occur in a critical
1487 * section, to set pcpu->ipending (etc...) properly, we
1488 * must be able to get the icu lock, so it can't be
1492 mtx_init(&icu_lock, "icu", NULL, MTX_SPIN | MTX_NOWITNESS | MTX_NOPROFILE);
1496 r_idt.rd_limit = sizeof(idt0) - 1;
1497 r_idt.rd_base = (int) idt;
1500 finishidentcpu(); /* Final stage of CPU initialization */
1503 * Initialize the clock before the console so that console
1504 * initialization can use DELAY().
1510 initializecpu(); /* Initialize CPU registers */
1511 initializecpucache();
1513 /* pointer to selector slot for %fs/%gs */
1514 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
1516 /* Initialize the tss (except for the final esp0) early for vm86. */
1517 common_tss0.tss_esp0 = thread0.td_kstack + thread0.td_kstack_pages *
1518 PAGE_SIZE - VM86_STACK_SPACE;
1519 common_tss0.tss_ss0 = GSEL(GDATA_SEL, SEL_KPL);
1520 common_tss0.tss_ioopt = sizeof(struct i386tss) << 16;
1521 gsel_tss = GSEL(GPROC0_SEL, SEL_KPL);
1522 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
1523 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
1526 /* Initialize the PIC early for vm86 calls. */
1532 /* Reset and mask the atpics and leave them shut down. */
1536 * Point the ICU spurious interrupt vectors at the APIC spurious
1537 * interrupt handler.
1544 * The console and kdb should be initialized even earlier than here,
1545 * but some console drivers don't work until after getmemsize().
1546 * Default to late console initialization to support these drivers.
1547 * This loses mainly printf()s in getmemsize() and early debugging.
1550 TUNABLE_INT_FETCH("debug.late_console", &late_console);
1551 if (!late_console) {
1556 kmdp = preload_search_by_type("elf kernel");
1557 link_elf_ireloc(kmdp);
1561 init_param2(physmem);
1563 /* now running on new page tables, configured,and u/iom is accessible */
1568 if (metadata_missing)
1569 printf("WARNING: loader(8) metadata is missing!\n");
1574 msgbufinit(msgbufp, msgbufsize);
1577 * Set up thread0 pcb after npxinit calculated pcb + fpu save
1578 * area size. Zero out the extended state header in fpu save
1581 thread0.td_pcb = get_pcb_td(&thread0);
1582 thread0.td_pcb->pcb_save = get_pcb_user_save_td(&thread0);
1583 bzero(get_pcb_user_save_td(&thread0), cpu_max_ext_state_size);
1585 xhdr = (struct xstate_hdr *)(get_pcb_user_save_td(&thread0) +
1587 xhdr->xstate_bv = xsave_mask;
1589 PCPU_SET(curpcb, thread0.td_pcb);
1590 /* Move esp0 in the tss to its final place. */
1591 /* Note: -16 is so we can grow the trapframe if we came from vm86 */
1592 common_tss0.tss_esp0 = (vm_offset_t)thread0.td_pcb - VM86_STACK_SPACE;
1593 PCPU_SET(kesp0, common_tss0.tss_esp0);
1594 gdt[GPROC0_SEL].sd.sd_type = SDT_SYS386TSS; /* clear busy bit */
1597 /* transfer to user mode */
1599 _ucodesel = GSEL(GUCODE_SEL, SEL_UPL);
1600 _udatasel = GSEL(GUDATA_SEL, SEL_UPL);
1602 /* setup proc 0's pcb */
1603 thread0.td_pcb->pcb_flags = 0;
1604 thread0.td_pcb->pcb_cr3 = pmap_get_kcr3();
1605 thread0.td_pcb->pcb_ext = 0;
1606 thread0.td_frame = &proc0_tf;
1612 /* Location of kernel stack for locore */
1613 return ((register_t)thread0.td_pcb);
1617 machdep_init_trampoline(void)
1619 struct region_descriptor r_gdt, r_idt;
1620 struct i386tss *tss;
1621 char *copyout_buf, *trampoline, *tramp_stack_base;
1624 gdt = pmap_trm_alloc(sizeof(union descriptor) * NGDT * mp_ncpus,
1626 bcopy(gdt0, gdt, sizeof(union descriptor) * NGDT);
1627 r_gdt.rd_limit = NGDT * sizeof(gdt[0]) - 1;
1628 r_gdt.rd_base = (int)gdt;
1631 tss = pmap_trm_alloc(sizeof(struct i386tss) * mp_ncpus,
1633 bcopy(&common_tss0, tss, sizeof(struct i386tss));
1634 gdt[GPROC0_SEL].sd.sd_lobase = (int)tss;
1635 gdt[GPROC0_SEL].sd.sd_hibase = (u_int)tss >> 24;
1636 gdt[GPROC0_SEL].sd.sd_type = SDT_SYS386TSS;
1638 PCPU_SET(fsgs_gdt, &gdt[GUFS_SEL].sd);
1639 PCPU_SET(tss_gdt, &gdt[GPROC0_SEL].sd);
1640 PCPU_SET(common_tssd, *PCPU_GET(tss_gdt));
1641 PCPU_SET(common_tssp, tss);
1642 ltr(GSEL(GPROC0_SEL, SEL_KPL));
1644 trampoline = pmap_trm_alloc(end_exceptions - start_exceptions,
1646 bcopy(start_exceptions, trampoline, end_exceptions - start_exceptions);
1647 tramp_stack_base = pmap_trm_alloc(TRAMP_STACK_SZ, M_NOWAIT);
1648 PCPU_SET(trampstk, (uintptr_t)tramp_stack_base + TRAMP_STACK_SZ -
1650 tss[0].tss_esp0 = PCPU_GET(trampstk);
1652 idt = pmap_trm_alloc(sizeof(idt0), M_NOWAIT | M_ZERO);
1653 bcopy(idt0, idt, sizeof(idt0));
1655 /* Re-initialize new IDT since the handlers were relocated */
1656 setidt_disp = trampoline - start_exceptions;
1659 r_idt.rd_limit = sizeof(struct gate_descriptor) * NIDT - 1;
1660 r_idt.rd_base = (int)idt;
1664 dblfault_tss = pmap_trm_alloc(sizeof(struct i386tss), M_NOWAIT | M_ZERO);
1665 dblfault_stack = pmap_trm_alloc(PAGE_SIZE, M_NOWAIT);
1666 dblfault_tss->tss_esp = dblfault_tss->tss_esp0 =
1667 dblfault_tss->tss_esp1 = dblfault_tss->tss_esp2 =
1668 (int)dblfault_stack + PAGE_SIZE;
1669 dblfault_tss->tss_ss = dblfault_tss->tss_ss0 = dblfault_tss->tss_ss1 =
1670 dblfault_tss->tss_ss2 = GSEL(GDATA_SEL, SEL_KPL);
1671 dblfault_tss->tss_cr3 = pmap_get_kcr3();
1672 dblfault_tss->tss_eip = (int)dblfault_handler;
1673 dblfault_tss->tss_eflags = PSL_KERNEL;
1674 dblfault_tss->tss_ds = dblfault_tss->tss_es =
1675 dblfault_tss->tss_gs = GSEL(GDATA_SEL, SEL_KPL);
1676 dblfault_tss->tss_fs = GSEL(GPRIV_SEL, SEL_KPL);
1677 dblfault_tss->tss_cs = GSEL(GCODE_SEL, SEL_KPL);
1678 dblfault_tss->tss_ldt = GSEL(GLDT_SEL, SEL_KPL);
1679 gdt[GPANIC_SEL].sd.sd_lobase = (int)dblfault_tss;
1680 gdt[GPANIC_SEL].sd.sd_hibase = (u_int)dblfault_tss >> 24;
1682 /* make ldt memory segments */
1683 ldt = pmap_trm_alloc(sizeof(union descriptor) * NLDT,
1685 gdt[GLDT_SEL].sd.sd_lobase = (int)ldt;
1686 gdt[GLDT_SEL].sd.sd_hibase = (u_int)ldt >> 24;
1687 ldt_segs[LUCODE_SEL].ssd_limit = atop(0 - 1);
1688 ldt_segs[LUDATA_SEL].ssd_limit = atop(0 - 1);
1689 for (x = 0; x < nitems(ldt_segs); x++)
1690 ssdtosd(&ldt_segs[x], &ldt[x].sd);
1692 _default_ldt = GSEL(GLDT_SEL, SEL_KPL);
1694 PCPU_SET(currentldt, _default_ldt);
1696 copyout_buf = pmap_trm_alloc(TRAMP_COPYOUT_SZ, M_NOWAIT);
1697 PCPU_SET(copyout_buf, copyout_buf);
1698 copyout_init_tramp();
1700 SYSINIT(vm_mem, SI_SUB_VM, SI_ORDER_SECOND, machdep_init_trampoline, NULL);
1704 i386_setup_lcall_gate(void)
1706 struct sysentvec *sv;
1707 struct user_segment_descriptor desc;
1710 sv = &elf32_freebsd_sysvec;
1711 lcall_addr = (uintptr_t)sv->sv_psstrings - sz_lcall_tramp;
1713 bzero(&desc, sizeof(desc));
1714 desc.sd_type = SDT_MEMERA;
1715 desc.sd_dpl = SEL_UPL;
1719 desc.sd_lolimit = 0xffff;
1720 desc.sd_hilimit = 0xf;
1721 desc.sd_lobase = lcall_addr;
1722 desc.sd_hibase = lcall_addr >> 24;
1723 bcopy(&desc, &ldt[LSYS5CALLS_SEL], sizeof(desc));
1725 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_ANY, i386_setup_lcall_gate, NULL);
1729 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t size)
1732 pcpu->pc_acpi_id = 0xffffffff;
1736 smap_sysctl_handler(SYSCTL_HANDLER_ARGS)
1738 struct bios_smap *smapbase;
1739 struct bios_smap_xattr smap;
1742 int count, error, i;
1744 /* Retrieve the system memory map from the loader. */
1745 kmdp = preload_search_by_type("elf kernel");
1747 kmdp = preload_search_by_type("elf32 kernel");
1748 smapbase = (struct bios_smap *)preload_search_info(kmdp,
1749 MODINFO_METADATA | MODINFOMD_SMAP);
1750 if (smapbase == NULL)
1752 smapattr = (uint32_t *)preload_search_info(kmdp,
1753 MODINFO_METADATA | MODINFOMD_SMAP_XATTR);
1754 count = *((u_int32_t *)smapbase - 1) / sizeof(*smapbase);
1756 for (i = 0; i < count; i++) {
1757 smap.base = smapbase[i].base;
1758 smap.length = smapbase[i].length;
1759 smap.type = smapbase[i].type;
1760 if (smapattr != NULL)
1761 smap.xattr = smapattr[i];
1764 error = SYSCTL_OUT(req, &smap, sizeof(smap));
1768 SYSCTL_PROC(_machdep, OID_AUTO, smap,
1769 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1770 smap_sysctl_handler, "S,bios_smap_xattr",
1771 "Raw BIOS SMAP data");
1774 spinlock_enter(void)
1780 if (td->td_md.md_spinlock_count == 0) {
1781 flags = intr_disable();
1782 td->td_md.md_spinlock_count = 1;
1783 td->td_md.md_saved_flags = flags;
1786 td->td_md.md_spinlock_count++;
1796 flags = td->td_md.md_saved_flags;
1797 td->td_md.md_spinlock_count--;
1798 if (td->td_md.md_spinlock_count == 0) {
1800 intr_restore(flags);
1804 #if defined(I586_CPU) && !defined(NO_F00F_HACK)
1805 static void f00f_hack(void *unused);
1806 SYSINIT(f00f_hack, SI_SUB_INTRINSIC, SI_ORDER_FIRST, f00f_hack, NULL);
1809 f00f_hack(void *unused)
1811 struct region_descriptor r_idt;
1812 struct gate_descriptor *new_idt;
1820 printf("Intel Pentium detected, installing workaround for F00F bug\n");
1822 tmp = (vm_offset_t)pmap_trm_alloc(PAGE_SIZE * 3, M_NOWAIT | M_ZERO);
1824 panic("kmem_malloc returned 0");
1825 tmp = round_page(tmp);
1827 /* Put the problematic entry (#6) at the end of the lower page. */
1828 new_idt = (struct gate_descriptor *)
1829 (tmp + PAGE_SIZE - 7 * sizeof(struct gate_descriptor));
1830 bcopy(idt, new_idt, sizeof(idt0));
1831 r_idt.rd_base = (u_int)new_idt;
1832 r_idt.rd_limit = sizeof(idt0) - 1;
1834 /* SMP machines do not need the F00F hack. */
1836 pmap_protect(kernel_pmap, tmp, tmp + PAGE_SIZE, VM_PROT_READ);
1838 #endif /* defined(I586_CPU) && !NO_F00F_HACK */
1841 * Construct a PCB from a trapframe. This is called from kdb_trap() where
1842 * we want to start a backtrace from the function that caused us to enter
1843 * the debugger. We have the context in the trapframe, but base the trace
1844 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
1845 * enough for a backtrace.
1848 makectx(struct trapframe *tf, struct pcb *pcb)
1851 pcb->pcb_edi = tf->tf_edi;
1852 pcb->pcb_esi = tf->tf_esi;
1853 pcb->pcb_ebp = tf->tf_ebp;
1854 pcb->pcb_ebx = tf->tf_ebx;
1855 pcb->pcb_eip = tf->tf_eip;
1856 pcb->pcb_esp = (ISPL(tf->tf_cs)) ? tf->tf_esp : (int)(tf + 1) - 8;
1857 pcb->pcb_gs = rgs();
1863 * Provide inb() and outb() as functions. They are normally only available as
1864 * inline functions, thus cannot be called from the debugger.
1867 /* silence compiler warnings */
1868 u_char inb_(u_short);
1869 void outb_(u_short, u_char);
1878 outb_(u_short port, u_char data)