/*1*2* Copyright (C) 1991, 1992 Linus Torvalds3*4* Enhanced CPU detection and feature setting code by Mike Jagdis5* and Martin Mares, November 1997.6*/78.text9#include <linux/threads.h>10#include <linux/init.h>11#include <linux/linkage.h>12#include <asm/segment.h>13#include <asm/page_types.h>14#include <asm/pgtable_types.h>15#include <asm/cache.h>16#include <asm/thread_info.h>17#include <asm/asm-offsets.h>18#include <asm/setup.h>19#include <asm/processor-flags.h>20#include <asm/msr-index.h>21#include <asm/cpufeature.h>22#include <asm/percpu.h>2324/* Physical address */25#define pa(X) ((X) - __PAGE_OFFSET)2627/*28* References to members of the new_cpu_data structure.29*/3031#define X86 new_cpu_data+CPUINFO_x8632#define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor33#define X86_MODEL new_cpu_data+CPUINFO_x86_model34#define X86_MASK new_cpu_data+CPUINFO_x86_mask35#define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math36#define X86_CPUID new_cpu_data+CPUINFO_cpuid_level37#define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability38#define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id3940/*41* This is how much memory in addition to the memory covered up to42* and including _end we need mapped initially.43* We need:44* (KERNEL_IMAGE_SIZE/4096) / 1024 pages (worst case, non PAE)45* (KERNEL_IMAGE_SIZE/4096) / 512 + 4 pages (worst case for PAE)46*47* Modulo rounding, each megabyte assigned here requires a kilobyte of48* memory, which is currently unreclaimed.49*50* This should be a multiple of a page.51*52* KERNEL_IMAGE_SIZE should be greater than pa(_end)53* and small than max_low_pfn, otherwise will waste some page table entries54*/5556#if PTRS_PER_PMD > 157#define PAGE_TABLE_SIZE(pages) (((pages) / PTRS_PER_PMD) + PTRS_PER_PGD)58#else59#define PAGE_TABLE_SIZE(pages) ((pages) / PTRS_PER_PGD)60#endif6162/* Number of possible pages in the lowmem region */63LOWMEM_PAGES = (((1<<32) - __PAGE_OFFSET) >> PAGE_SHIFT)6465/* Enough space to fit pagetables for the low memory linear map */66MAPPING_BEYOND_END = PAGE_TABLE_SIZE(LOWMEM_PAGES) << PAGE_SHIFT6768/*69* Worst-case size of the kernel mapping we need to make:70* a relocatable kernel can live anywhere in lowmem, so we need to be able71* to map all of lowmem.72*/73KERNEL_PAGES = LOWMEM_PAGES7475INIT_MAP_SIZE = PAGE_TABLE_SIZE(KERNEL_PAGES) * PAGE_SIZE76RESERVE_BRK(pagetables, INIT_MAP_SIZE)7778/*79* 32-bit kernel entrypoint; only used by the boot CPU. On entry,80* %esi points to the real-mode code as a 32-bit pointer.81* CS and DS must be 4 GB flat segments, but we don't depend on82* any particular GDT layout, because we load our own as soon as we83* can.84*/85__HEAD86ENTRY(startup_32)87movl pa(stack_start),%ecx8889/* test KEEP_SEGMENTS flag to see if the bootloader is asking90us to not reload segments */91testb $(1<<6), BP_loadflags(%esi)92jnz 2f9394/*95* Set segments to known values.96*/97lgdt pa(boot_gdt_descr)98movl $(__BOOT_DS),%eax99movl %eax,%ds100movl %eax,%es101movl %eax,%fs102movl %eax,%gs103movl %eax,%ss1042:105leal -__PAGE_OFFSET(%ecx),%esp106107/*108* Clear BSS first so that there are no surprises...109*/110cld111xorl %eax,%eax112movl $pa(__bss_start),%edi113movl $pa(__bss_stop),%ecx114subl %edi,%ecx115shrl $2,%ecx116rep ; stosl117/*118* Copy bootup parameters out of the way.119* Note: %esi still has the pointer to the real-mode data.120* With the kexec as boot loader, parameter segment might be loaded beyond121* kernel image and might not even be addressable by early boot page tables.122* (kexec on panic case). Hence copy out the parameters before initializing123* page tables.124*/125movl $pa(boot_params),%edi126movl $(PARAM_SIZE/4),%ecx127cld128rep129movsl130movl pa(boot_params) + NEW_CL_POINTER,%esi131andl %esi,%esi132jz 1f # No command line133movl $pa(boot_command_line),%edi134movl $(COMMAND_LINE_SIZE/4),%ecx135rep136movsl1371:138139#ifdef CONFIG_OLPC140/* save OFW's pgdir table for later use when calling into OFW */141movl %cr3, %eax142movl %eax, pa(olpc_ofw_pgd)143#endif144145/*146* Initialize page tables. This creates a PDE and a set of page147* tables, which are located immediately beyond __brk_base. The variable148* _brk_end is set up to point to the first "safe" location.149* Mappings are created both at virtual address 0 (identity mapping)150* and PAGE_OFFSET for up to _end.151*/152#ifdef CONFIG_X86_PAE153154/*155* In PAE mode initial_page_table is statically defined to contain156* enough entries to cover the VMSPLIT option (that is the top 1, 2 or 3157* entries). The identity mapping is handled by pointing two PGD entries158* to the first kernel PMD.159*160* Note the upper half of each PMD or PTE are always zero at this stage.161*/162163#define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */164165xorl %ebx,%ebx /* %ebx is kept at zero */166167movl $pa(__brk_base), %edi168movl $pa(initial_pg_pmd), %edx169movl $PTE_IDENT_ATTR, %eax17010:171leal PDE_IDENT_ATTR(%edi),%ecx /* Create PMD entry */172movl %ecx,(%edx) /* Store PMD entry */173/* Upper half already zero */174addl $8,%edx175movl $512,%ecx17611:177stosl178xchgl %eax,%ebx179stosl180xchgl %eax,%ebx181addl $0x1000,%eax182loop 11b183184/*185* End condition: we must map up to the end + MAPPING_BEYOND_END.186*/187movl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %ebp188cmpl %ebp,%eax189jb 10b1901:191addl $__PAGE_OFFSET, %edi192movl %edi, pa(_brk_end)193shrl $12, %eax194movl %eax, pa(max_pfn_mapped)195196/* Do early initialization of the fixmap area */197movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax198movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)199#else /* Not PAE */200201page_pde_offset = (__PAGE_OFFSET >> 20);202203movl $pa(__brk_base), %edi204movl $pa(initial_page_table), %edx205movl $PTE_IDENT_ATTR, %eax20610:207leal PDE_IDENT_ATTR(%edi),%ecx /* Create PDE entry */208movl %ecx,(%edx) /* Store identity PDE entry */209movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */210addl $4,%edx211movl $1024, %ecx21211:213stosl214addl $0x1000,%eax215loop 11b216/*217* End condition: we must map up to the end + MAPPING_BEYOND_END.218*/219movl $pa(_end) + MAPPING_BEYOND_END + PTE_IDENT_ATTR, %ebp220cmpl %ebp,%eax221jb 10b222addl $__PAGE_OFFSET, %edi223movl %edi, pa(_brk_end)224shrl $12, %eax225movl %eax, pa(max_pfn_mapped)226227/* Do early initialization of the fixmap area */228movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax229movl %eax,pa(initial_page_table+0xffc)230#endif231232#ifdef CONFIG_PARAVIRT233/* This is can only trip for a broken bootloader... */234cmpw $0x207, pa(boot_params + BP_version)235jb default_entry236237/* Paravirt-compatible boot parameters. Look to see what architecture238we're booting under. */239movl pa(boot_params + BP_hardware_subarch), %eax240cmpl $num_subarch_entries, %eax241jae bad_subarch242243movl pa(subarch_entries)(,%eax,4), %eax244subl $__PAGE_OFFSET, %eax245jmp *%eax246247bad_subarch:248WEAK(lguest_entry)249WEAK(xen_entry)250/* Unknown implementation; there's really251nothing we can do at this point. */252ud2a253254__INITDATA255256subarch_entries:257.long default_entry /* normal x86/PC */258.long lguest_entry /* lguest hypervisor */259.long xen_entry /* Xen hypervisor */260.long default_entry /* Moorestown MID */261num_subarch_entries = (. - subarch_entries) / 4262.previous263#else264jmp default_entry265#endif /* CONFIG_PARAVIRT */266267/*268* Non-boot CPU entry point; entered from trampoline.S269* We can't lgdt here, because lgdt itself uses a data segment, but270* we know the trampoline has already loaded the boot_gdt for us.271*272* If cpu hotplug is not supported then this code can go in init section273* which will be freed later274*/275276__CPUINIT277278#ifdef CONFIG_SMP279ENTRY(startup_32_smp)280cld281movl $(__BOOT_DS),%eax282movl %eax,%ds283movl %eax,%es284movl %eax,%fs285movl %eax,%gs286movl pa(stack_start),%ecx287movl %eax,%ss288leal -__PAGE_OFFSET(%ecx),%esp289#endif /* CONFIG_SMP */290default_entry:291292/*293* New page tables may be in 4Mbyte page mode and may294* be using the global pages.295*296* NOTE! If we are on a 486 we may have no cr4 at all!297* So we do not try to touch it unless we really have298* some bits in it to set. This won't work if the BSP299* implements cr4 but this AP does not -- very unlikely300* but be warned! The same applies to the pse feature301* if not equally supported. --macro302*303* NOTE! We have to correct for the fact that we're304* not yet offset PAGE_OFFSET..305*/306#define cr4_bits pa(mmu_cr4_features)307movl cr4_bits,%edx308andl %edx,%edx309jz 6f310movl %cr4,%eax # Turn on paging options (PSE,PAE,..)311orl %edx,%eax312movl %eax,%cr4313314testb $X86_CR4_PAE, %al # check if PAE is enabled315jz 6f316317/* Check if extended functions are implemented */318movl $0x80000000, %eax319cpuid320/* Value must be in the range 0x80000001 to 0x8000ffff */321subl $0x80000001, %eax322cmpl $(0x8000ffff-0x80000001), %eax323ja 6f324325/* Clear bogus XD_DISABLE bits */326call verify_cpu327328mov $0x80000001, %eax329cpuid330/* Execute Disable bit supported? */331btl $(X86_FEATURE_NX & 31), %edx332jnc 6f333334/* Setup EFER (Extended Feature Enable Register) */335movl $MSR_EFER, %ecx336rdmsr337338btsl $_EFER_NX, %eax339/* Make changes effective */340wrmsr3413426:343344/*345* Enable paging346*/347movl $pa(initial_page_table), %eax348movl %eax,%cr3 /* set the page table pointer.. */349movl %cr0,%eax350orl $X86_CR0_PG,%eax351movl %eax,%cr0 /* ..and set paging (PG) bit */352ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */3531:354/* Shift the stack pointer to a virtual address */355addl $__PAGE_OFFSET, %esp356357/*358* Initialize eflags. Some BIOS's leave bits like NT set. This would359* confuse the debugger if this code is traced.360* XXX - best to initialize before switching to protected mode.361*/362pushl $0363popfl364365#ifdef CONFIG_SMP366cmpb $0, ready367jnz checkCPUtype368#endif /* CONFIG_SMP */369370/*371* start system 32-bit setup. We need to re-do some of the things done372* in 16-bit mode for the "real" operations.373*/374call setup_idt375376checkCPUtype:377378movl $-1,X86_CPUID # -1 for no CPUID initially379380/* check if it is 486 or 386. */381/*382* XXX - this does a lot of unnecessary setup. Alignment checks don't383* apply at our cpl of 0 and the stack ought to be aligned already, and384* we don't need to preserve eflags.385*/386387movb $3,X86 # at least 386388pushfl # push EFLAGS389popl %eax # get EFLAGS390movl %eax,%ecx # save original EFLAGS391xorl $0x240000,%eax # flip AC and ID bits in EFLAGS392pushl %eax # copy to EFLAGS393popfl # set EFLAGS394pushfl # get new EFLAGS395popl %eax # put it in eax396xorl %ecx,%eax # change in flags397pushl %ecx # restore original EFLAGS398popfl399testl $0x40000,%eax # check if AC bit changed400je is386401402movb $4,X86 # at least 486403testl $0x200000,%eax # check if ID bit changed404je is486405406/* get vendor info */407xorl %eax,%eax # call CPUID with 0 -> return vendor ID408cpuid409movl %eax,X86_CPUID # save CPUID level410movl %ebx,X86_VENDOR_ID # lo 4 chars411movl %edx,X86_VENDOR_ID+4 # next 4 chars412movl %ecx,X86_VENDOR_ID+8 # last 4 chars413414orl %eax,%eax # do we have processor info as well?415je is486416417movl $1,%eax # Use the CPUID instruction to get CPU type418cpuid419movb %al,%cl # save reg for future use420andb $0x0f,%ah # mask processor family421movb %ah,X86422andb $0xf0,%al # mask model423shrb $4,%al424movb %al,X86_MODEL425andb $0x0f,%cl # mask mask revision426movb %cl,X86_MASK427movl %edx,X86_CAPABILITY428429is486: movl $0x50022,%ecx # set AM, WP, NE and MP430jmp 2f431432is386: movl $2,%ecx # set MP4332: movl %cr0,%eax434andl $0x80000011,%eax # Save PG,PE,ET435orl %ecx,%eax436movl %eax,%cr0437438call check_x87439lgdt early_gdt_descr440lidt idt_descr441ljmp $(__KERNEL_CS),$1f4421: movl $(__KERNEL_DS),%eax # reload all the segment registers443movl %eax,%ss # after changing gdt.444445movl $(__USER_DS),%eax # DS/ES contains default USER segment446movl %eax,%ds447movl %eax,%es448449movl $(__KERNEL_PERCPU), %eax450movl %eax,%fs # set this cpu's percpu451452#ifdef CONFIG_CC_STACKPROTECTOR453/*454* The linker can't handle this by relocation. Manually set455* base address in stack canary segment descriptor.456*/457cmpb $0,ready458jne 1f459movl $gdt_page,%eax460movl $stack_canary,%ecx461movw %cx, 8 * GDT_ENTRY_STACK_CANARY + 2(%eax)462shrl $16, %ecx463movb %cl, 8 * GDT_ENTRY_STACK_CANARY + 4(%eax)464movb %ch, 8 * GDT_ENTRY_STACK_CANARY + 7(%eax)4651:466#endif467movl $(__KERNEL_STACK_CANARY),%eax468movl %eax,%gs469470xorl %eax,%eax # Clear LDT471lldt %ax472473cld # gcc2 wants the direction flag cleared at all times474pushl $0 # fake return address for unwinder475movb $1, ready476jmp *(initial_code)477478/*479* We depend on ET to be correct. This checks for 287/387.480*/481check_x87:482movb $0,X86_HARD_MATH483clts484fninit485fstsw %ax486cmpb $0,%al487je 1f488movl %cr0,%eax /* no coprocessor: have to set bits */489xorl $4,%eax /* set EM */490movl %eax,%cr0491ret492ALIGN4931: movb $1,X86_HARD_MATH494.byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */495ret496497/*498* setup_idt499*500* sets up a idt with 256 entries pointing to501* ignore_int, interrupt gates. It doesn't actually load502* idt - that can be done only after paging has been enabled503* and the kernel moved to PAGE_OFFSET. Interrupts504* are enabled elsewhere, when we can be relatively505* sure everything is ok.506*507* Warning: %esi is live across this function.508*/509setup_idt:510lea ignore_int,%edx511movl $(__KERNEL_CS << 16),%eax512movw %dx,%ax /* selector = 0x0010 = cs */513movw $0x8E00,%dx /* interrupt gate - dpl=0, present */514515lea idt_table,%edi516mov $256,%ecx517rp_sidt:518movl %eax,(%edi)519movl %edx,4(%edi)520addl $8,%edi521dec %ecx522jne rp_sidt523524.macro set_early_handler handler,trapno525lea \handler,%edx526movl $(__KERNEL_CS << 16),%eax527movw %dx,%ax528movw $0x8E00,%dx /* interrupt gate - dpl=0, present */529lea idt_table,%edi530movl %eax,8*\trapno(%edi)531movl %edx,8*\trapno+4(%edi)532.endm533534set_early_handler handler=early_divide_err,trapno=0535set_early_handler handler=early_illegal_opcode,trapno=6536set_early_handler handler=early_protection_fault,trapno=13537set_early_handler handler=early_page_fault,trapno=14538539ret540541early_divide_err:542xor %edx,%edx543pushl $0 /* fake errcode */544jmp early_fault545546early_illegal_opcode:547movl $6,%edx548pushl $0 /* fake errcode */549jmp early_fault550551early_protection_fault:552movl $13,%edx553jmp early_fault554555early_page_fault:556movl $14,%edx557jmp early_fault558559early_fault:560cld561#ifdef CONFIG_PRINTK562pusha563movl $(__KERNEL_DS),%eax564movl %eax,%ds565movl %eax,%es566cmpl $2,early_recursion_flag567je hlt_loop568incl early_recursion_flag569movl %cr2,%eax570pushl %eax571pushl %edx /* trapno */572pushl $fault_msg573call printk574#endif575call dump_stack576hlt_loop:577hlt578jmp hlt_loop579580/* This is the default interrupt "handler" :-) */581ALIGN582ignore_int:583cld584#ifdef CONFIG_PRINTK585pushl %eax586pushl %ecx587pushl %edx588pushl %es589pushl %ds590movl $(__KERNEL_DS),%eax591movl %eax,%ds592movl %eax,%es593cmpl $2,early_recursion_flag594je hlt_loop595incl early_recursion_flag596pushl 16(%esp)597pushl 24(%esp)598pushl 32(%esp)599pushl 40(%esp)600pushl $int_msg601call printk602603call dump_stack604605addl $(5*4),%esp606popl %ds607popl %es608popl %edx609popl %ecx610popl %eax611#endif612iret613614#include "verify_cpu.S"615616__REFDATA617.align 4618ENTRY(initial_code)619.long i386_start_kernel620621/*622* BSS section623*/624__PAGE_ALIGNED_BSS625.align PAGE_SIZE626#ifdef CONFIG_X86_PAE627initial_pg_pmd:628.fill 1024*KPMDS,4,0629#else630ENTRY(initial_page_table)631.fill 1024,4,0632#endif633initial_pg_fixmap:634.fill 1024,4,0635ENTRY(empty_zero_page)636.fill 4096,1,0637ENTRY(swapper_pg_dir)638.fill 1024,4,0639640/*641* This starts the data section.642*/643#ifdef CONFIG_X86_PAE644__PAGE_ALIGNED_DATA645/* Page-aligned for the benefit of paravirt? */646.align PAGE_SIZE647ENTRY(initial_page_table)648.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */649# if KPMDS == 3650.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0651.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0652.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x2000),0653# elif KPMDS == 2654.long 0,0655.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0656.long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0657# elif KPMDS == 1658.long 0,0659.long 0,0660.long pa(initial_pg_pmd+PGD_IDENT_ATTR),0661# else662# error "Kernel PMDs should be 1, 2 or 3"663# endif664.align PAGE_SIZE /* needs to be page-sized too */665#endif666667.data668.balign 4669ENTRY(stack_start)670.long init_thread_union+THREAD_SIZE671672early_recursion_flag:673.long 0674675ready: .byte 0676677int_msg:678.asciz "Unknown interrupt or fault at: %p %p %p\n"679680fault_msg:681/* fault info: */682.ascii "BUG: Int %d: CR2 %p\n"683/* pusha regs: */684.ascii " EDI %p ESI %p EBP %p ESP %p\n"685.ascii " EBX %p EDX %p ECX %p EAX %p\n"686/* fault frame: */687.ascii " err %p EIP %p CS %p flg %p\n"688.ascii "Stack: %p %p %p %p %p %p %p %p\n"689.ascii " %p %p %p %p %p %p %p %p\n"690.asciz " %p %p %p %p %p %p %p %p\n"691692#include "../../x86/xen/xen-head.S"693694/*695* The IDT and GDT 'descriptors' are a strange 48-bit object696* only used by the lidt and lgdt instructions. They are not697* like usual segment descriptors - they consist of a 16-bit698* segment size, and 32-bit linear address value:699*/700701.globl boot_gdt_descr702.globl idt_descr703704ALIGN705# early boot GDT descriptor (must use 1:1 address mapping)706.word 0 # 32 bit align gdt_desc.address707boot_gdt_descr:708.word __BOOT_DS+7709.long boot_gdt - __PAGE_OFFSET710711.word 0 # 32-bit align idt_desc.address712idt_descr:713.word IDT_ENTRIES*8-1 # idt contains 256 entries714.long idt_table715716# boot GDT descriptor (later on used by CPU#0):717.word 0 # 32 bit align gdt_desc.address718ENTRY(early_gdt_descr)719.word GDT_ENTRIES*8-1720.long gdt_page /* Overwritten for secondary CPUs */721722/*723* The boot_gdt must mirror the equivalent in setup.S and is724* used only for booting.725*/726.align L1_CACHE_BYTES727ENTRY(boot_gdt)728.fill GDT_ENTRY_BOOT_CS,8,0729.quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */730.quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */731732733