/* SPDX-License-Identifier: GPL-2.0 */1/*2*3* Copyright (C) 1991, 1992 Linus Torvalds4*5* Enhanced CPU detection and feature setting code by Mike Jagdis6* and Martin Mares, November 1997.7*/89.text10#include <linux/export.h>11#include <linux/threads.h>12#include <linux/init.h>13#include <linux/linkage.h>14#include <asm/segment.h>15#include <asm/page_types.h>16#include <asm/pgtable_types.h>17#include <asm/cache.h>18#include <asm/thread_info.h>19#include <asm/asm-offsets.h>20#include <asm/setup.h>21#include <asm/processor-flags.h>22#include <asm/msr-index.h>23#include <asm/cpufeatures.h>24#include <asm/percpu.h>25#include <asm/nops.h>26#include <asm/nospec-branch.h>27#include <asm/bootparam.h>28#include <asm/pgtable_32.h>2930/* Physical address */31#define pa(X) ((X) - __PAGE_OFFSET)3233/*34* References to members of the new_cpu_data structure.35*/3637#define X86 new_cpu_data+CPUINFO_x8638#define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor39#define X86_MODEL new_cpu_data+CPUINFO_x86_model40#define X86_STEPPING new_cpu_data+CPUINFO_x86_stepping41#define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math42#define X86_CPUID new_cpu_data+CPUINFO_cpuid_level43#define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability44#define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id4546/*47* Worst-case size of the kernel mapping we need to make:48* a relocatable kernel can live anywhere in lowmem, so we need to be able49* to map all of lowmem.50*/51KERNEL_PAGES = LOWMEM_PAGES5253INIT_MAP_SIZE = PAGE_TABLE_SIZE(KERNEL_PAGES) * PAGE_SIZE54RESERVE_BRK(pagetables, INIT_MAP_SIZE)5556/*57* 32-bit kernel entrypoint; only used by the boot CPU. On entry,58* %esi points to the real-mode code as a 32-bit pointer.59* CS and DS must be 4 GB flat segments, but we don't depend on60* any particular GDT layout, because we load our own as soon as we61* can.62*/63__INIT64SYM_CODE_START(startup_32)65movl pa(initial_stack),%ecx6667/*68* Set segments to known values.69*/70lgdt pa(boot_gdt_descr)71movl $(__BOOT_DS),%eax72movl %eax,%ds73movl %eax,%es74movl %eax,%fs75movl %eax,%gs76movl %eax,%ss77leal -__PAGE_OFFSET(%ecx),%esp7879/*80* Clear BSS first so that there are no surprises...81*/82cld83xorl %eax,%eax84movl $pa(__bss_start),%edi85movl $pa(__bss_stop),%ecx86subl %edi,%ecx87shrl $2,%ecx88rep stosl89/*90* Copy bootup parameters out of the way.91* Note: %esi still has the pointer to the real-mode data.92* With the kexec as boot loader, parameter segment might be loaded beyond93* kernel image and might not even be addressable by early boot page tables.94* (kexec on panic case). Hence copy out the parameters before initializing95* page tables.96*/97movl $pa(boot_params),%edi98movl $(PARAM_SIZE/4),%ecx99cld100rep movsl101movl pa(boot_params) + NEW_CL_POINTER,%esi102andl %esi,%esi103jz 1f # No command line104movl $pa(boot_command_line),%edi105movl $(COMMAND_LINE_SIZE/4),%ecx106rep movsl1071:108109#ifdef CONFIG_OLPC110/* save OFW's pgdir table for later use when calling into OFW */111movl %cr3, %eax112movl %eax, pa(olpc_ofw_pgd)113#endif114115/* Create early pagetables. */116call mk_early_pgtbl_32117118/* Do early initialization of the fixmap area */119movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax120#ifdef CONFIG_X86_PAE121#define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */122movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)123#else124movl %eax,pa(initial_page_table+0xffc)125#endif126127jmp .Ldefault_entry128SYM_CODE_END(startup_32)129130/*131* Non-boot CPU entry point; entered from trampoline.S132* We can't lgdt here, because lgdt itself uses a data segment, but133* we know the trampoline has already loaded the boot_gdt for us.134*135* If cpu hotplug is not supported then this code can go in init section136* which will be freed later137*/138#ifdef CONFIG_HOTPLUG_CPU139.text140#endif141SYM_FUNC_START(startup_32_smp)142cld143movl $(__BOOT_DS),%eax144movl %eax,%ds145movl %eax,%es146movl %eax,%fs147movl %eax,%gs148movl pa(initial_stack),%ecx149movl %eax,%ss150leal -__PAGE_OFFSET(%ecx),%esp151152.Ldefault_entry:153movl $(CR0_STATE & ~X86_CR0_PG),%eax154movl %eax,%cr0155156/*157* We want to start out with EFLAGS unambiguously cleared. Some BIOSes leave158* bits like NT set. This would confuse the debugger if this code is traced. So159* initialize them properly now before switching to protected mode. That means160* DF in particular (even though we have cleared it earlier after copying the161* command line) because GCC expects it.162*/163pushl $0164popfl165166/*167* New page tables may be in 4Mbyte page mode and may be using the global pages.168*169* NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists170* if and only if CPUID exists and has flags other than the FPU flag set.171*/172movl $-1,pa(X86_CPUID) # preset CPUID level173movl $X86_EFLAGS_ID,%ecx174pushl %ecx175popfl # set EFLAGS=ID176pushfl177popl %eax # get EFLAGS178testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set?179jz .Lenable_paging # hw disallowed setting of ID bit180# which means no CPUID and no CR4181182xorl %eax,%eax183cpuid184movl %eax,pa(X86_CPUID) # save largest std CPUID function185186movl $1,%eax187cpuid188andl $~1,%edx # Ignore CPUID.FPU189jz .Lenable_paging # No flags or only CPUID.FPU = no CR4190191movl pa(mmu_cr4_features),%eax192movl %eax,%cr4193194testb $X86_CR4_PAE, %al # check if PAE is enabled195jz .Lenable_paging196197/* Check if extended functions are implemented */198movl $0x80000000, %eax199cpuid200/* Value must be in the range 0x80000001 to 0x8000ffff */201subl $0x80000001, %eax202cmpl $(0x8000ffff-0x80000001), %eax203ja .Lenable_paging204205/* Clear bogus XD_DISABLE bits */206call verify_cpu207208mov $0x80000001, %eax209cpuid210/* Execute Disable bit supported? */211btl $(X86_FEATURE_NX & 31), %edx212jnc .Lenable_paging213214/* Setup EFER (Extended Feature Enable Register) */215movl $MSR_EFER, %ecx216rdmsr217218btsl $_EFER_NX, %eax219/* Make changes effective */220wrmsr221222.Lenable_paging:223224/*225* Enable paging226*/227movl $pa(initial_page_table), %eax228movl %eax,%cr3 /* set the page table pointer.. */229movl $CR0_STATE,%eax230movl %eax,%cr0 /* ..and set paging (PG) bit */231ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */2321:233/* Shift the stack pointer to a virtual address */234addl $__PAGE_OFFSET, %esp235236/*237* Check if it is 486238*/239movb $4,X86 # at least 486240cmpl $-1,X86_CPUID241je .Lis486242243/* get vendor info */244xorl %eax,%eax # call CPUID with 0 -> return vendor ID245cpuid246movl %eax,X86_CPUID # save CPUID level247movl %ebx,X86_VENDOR_ID # lo 4 chars248movl %edx,X86_VENDOR_ID+4 # next 4 chars249movl %ecx,X86_VENDOR_ID+8 # last 4 chars250251orl %eax,%eax # do we have processor info as well?252je .Lis486253254movl $1,%eax # Use the CPUID instruction to get CPU type255cpuid256movb %al,%cl # save reg for future use257andb $0x0f,%ah # mask processor family258movb %ah,X86259andb $0xf0,%al # mask model260shrb $4,%al261movb %al,X86_MODEL262andb $0x0f,%cl # mask mask revision263movb %cl,X86_STEPPING264movl %edx,X86_CAPABILITY265266.Lis486:267movl $0x50022,%ecx # set AM, WP, NE and MP268movl %cr0,%eax269andl $0x80000011,%eax # Save PG,PE,ET270orl %ecx,%eax271movl %eax,%cr0272273lgdt early_gdt_descr274ljmp $(__KERNEL_CS),$1f2751: movl $(__KERNEL_DS),%eax # reload all the segment registers276movl %eax,%ss # after changing gdt.277278movl $(__USER_DS),%eax # DS/ES contains default USER segment279movl %eax,%ds280movl %eax,%es281282movl $(__KERNEL_PERCPU), %eax283movl %eax,%fs # set this cpu's percpu284285xorl %eax,%eax286movl %eax,%gs # clear possible garbage in %gs287288xorl %eax,%eax # Clear LDT289lldt %ax290291call *(initial_code)2921: jmp 1b293SYM_FUNC_END(startup_32_smp)294295#include "verify_cpu.S"296297__INIT298SYM_FUNC_START(early_idt_handler_array)299# 36(%esp) %eflags300# 32(%esp) %cs301# 28(%esp) %eip302# 24(%rsp) error code303i = 0304.rept NUM_EXCEPTION_VECTORS305.if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0306pushl $0 # Dummy error code, to make stack frame uniform307.endif308pushl $i # 20(%esp) Vector number309jmp early_idt_handler_common310i = i + 1311.fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc312.endr313SYM_FUNC_END(early_idt_handler_array)314315SYM_CODE_START_LOCAL(early_idt_handler_common)316/*317* The stack is the hardware frame, an error code or zero, and the318* vector number.319*/320cld321322incl %ss:early_recursion_flag323324/* The vector number is in pt_regs->gs */325326cld327pushl %fs /* pt_regs->fs (__fsh varies by model) */328pushl %es /* pt_regs->es (__esh varies by model) */329pushl %ds /* pt_regs->ds (__dsh varies by model) */330pushl %eax /* pt_regs->ax */331pushl %ebp /* pt_regs->bp */332pushl %edi /* pt_regs->di */333pushl %esi /* pt_regs->si */334pushl %edx /* pt_regs->dx */335pushl %ecx /* pt_regs->cx */336pushl %ebx /* pt_regs->bx */337338/* Fix up DS and ES */339movl $(__KERNEL_DS), %ecx340movl %ecx, %ds341movl %ecx, %es342343/* Load the vector number into EDX */344movl PT_GS(%esp), %edx345346/* Load GS into pt_regs->gs (and maybe clobber __gsh) */347movw %gs, PT_GS(%esp)348349movl %esp, %eax /* args are pt_regs (EAX), trapnr (EDX) */350call early_fixup_exception351352popl %ebx /* pt_regs->bx */353popl %ecx /* pt_regs->cx */354popl %edx /* pt_regs->dx */355popl %esi /* pt_regs->si */356popl %edi /* pt_regs->di */357popl %ebp /* pt_regs->bp */358popl %eax /* pt_regs->ax */359popl %ds /* pt_regs->ds (always ignores __dsh) */360popl %es /* pt_regs->es (always ignores __esh) */361popl %fs /* pt_regs->fs (always ignores __fsh) */362popl %gs /* pt_regs->gs (always ignores __gsh) */363decl %ss:early_recursion_flag364addl $4, %esp /* pop pt_regs->orig_ax */365iret366SYM_CODE_END(early_idt_handler_common)367368/* This is the default interrupt "handler" :-) */369SYM_FUNC_START(early_ignore_irq)370cld371#ifdef CONFIG_PRINTK372pushl %eax373pushl %ecx374pushl %edx375pushl %es376pushl %ds377movl $(__KERNEL_DS),%eax378movl %eax,%ds379movl %eax,%es380cmpl $2,early_recursion_flag381je hlt_loop382incl early_recursion_flag383pushl 16(%esp)384pushl 24(%esp)385pushl 32(%esp)386pushl 40(%esp)387pushl $int_msg388call _printk389390call dump_stack391392addl $(5*4),%esp393popl %ds394popl %es395popl %edx396popl %ecx397popl %eax398#endif399iret400401hlt_loop:402hlt403jmp hlt_loop404SYM_FUNC_END(early_ignore_irq)405406__INITDATA407.align 4408SYM_DATA(early_recursion_flag, .long 0)409410__REFDATA411.align 4412SYM_DATA(initial_code, .long i386_start_kernel)413414#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION415#define PGD_ALIGN (2 * PAGE_SIZE)416#define PTI_USER_PGD_FILL 1024417#else418#define PGD_ALIGN (PAGE_SIZE)419#define PTI_USER_PGD_FILL 0420#endif421/*422* BSS section423*/424__PAGE_ALIGNED_BSS425.align PGD_ALIGN426#ifdef CONFIG_X86_PAE427.globl initial_pg_pmd428initial_pg_pmd:429.fill 1024*KPMDS,4,0430#else431.globl initial_page_table432initial_page_table:433.fill 1024,4,0434#endif435.align PGD_ALIGN436initial_pg_fixmap:437.fill 1024,4,0438.globl swapper_pg_dir439.align PGD_ALIGN440swapper_pg_dir:441.fill 1024,4,0442.fill PTI_USER_PGD_FILL,4,0443.globl empty_zero_page444empty_zero_page:445.fill 4096,1,0446EXPORT_SYMBOL(empty_zero_page)447448/*449* This starts the data section.450*/451#ifdef CONFIG_X86_PAE452__PAGE_ALIGNED_DATA453/* Page-aligned for the benefit of paravirt? */454.align PGD_ALIGN455SYM_DATA_START(initial_page_table)456.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */457# if KPMDS == 3458.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0459.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0460.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x2000),0461# elif KPMDS == 2462.long 0,0463.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0464.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0465# elif KPMDS == 1466.long 0,0467.long 0,0468.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0469# else470# error "Kernel PMDs should be 1, 2 or 3"471# endif472.align PAGE_SIZE /* needs to be page-sized too */473474#ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION475/*476* PTI needs another page so sync_initial_pagetable() works correctly477* and does not scribble over the data which is placed behind the478* actual initial_page_table. See clone_pgd_range().479*/480.fill 1024, 4, 0481#endif482483SYM_DATA_END(initial_page_table)484#endif485486.data487.balign 4488SYM_DATA(initial_stack, .long __top_init_kernel_stack)489490__INITRODATA491int_msg:492.asciz "Unknown interrupt or fault at: %p %p %p\n"493494#include "../xen/xen-head.S"495496/*497* The IDT and GDT 'descriptors' are a strange 48-bit object498* only used by the lidt and lgdt instructions. They are not499* like usual segment descriptors - they consist of a 16-bit500* segment size, and 32-bit linear address value:501*/502503.data504ALIGN505# early boot GDT descriptor (must use 1:1 address mapping)506.word 0 # 32 bit align gdt_desc.address507SYM_DATA_START_LOCAL(boot_gdt_descr)508.word __BOOT_DS+7509.long boot_gdt - __PAGE_OFFSET510SYM_DATA_END(boot_gdt_descr)511512# boot GDT descriptor (later on used by CPU#0):513.word 0 # 32 bit align gdt_desc.address514SYM_DATA_START(early_gdt_descr)515.word GDT_ENTRIES*8-1516.long gdt_page /* Overwritten for secondary CPUs */517SYM_DATA_END(early_gdt_descr)518519/*520* The boot_gdt must mirror the equivalent in setup.S and is521* used only for booting.522*/523.align L1_CACHE_BYTES524SYM_DATA_START(boot_gdt)525.fill GDT_ENTRY_BOOT_CS,8,0526.quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */527.quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */528SYM_DATA_END(boot_gdt)529530531