3 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Enhanced CPU detection and feature setting code by Mike Jagdis
6 * and Martin Mares, November 1997.
10 #include <linux/threads.h>
11 #include <linux/init.h>
12 #include <linux/linkage.h>
13 #include <asm/segment.h>
14 #include <asm/page_types.h>
15 #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/percpu.h>
24 /* Physical address */
25 #define pa(X) ((X) - __PAGE_OFFSET)
28 * References to members of the new_cpu_data structure.
31 #define X86 new_cpu_data+CPUINFO_x86
32 #define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor
33 #define X86_MODEL new_cpu_data+CPUINFO_x86_model
34 #define X86_MASK new_cpu_data+CPUINFO_x86_mask
35 #define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math
36 #define X86_CPUID new_cpu_data+CPUINFO_cpuid_level
37 #define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability
38 #define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id
41 * This is how much memory *in addition to the memory covered up to
42 * and including _end* we need mapped initially.
44 * - one bit for each possible page, but only in low memory, which means
45 * 2^32/4096/8 = 128K worst case (4G/4G split.)
46 * - enough space to map all low memory, which means
47 * (2^32/4096) / 1024 pages (worst case, non PAE)
48 * (2^32/4096) / 512 + 4 pages (worst case for PAE)
49 * - a few pages for allocator use before the kernel pagetable has
52 * Modulo rounding, each megabyte assigned here requires a kilobyte of
53 * memory, which is currently unreclaimed.
55 * This should be a multiple of a page.
57 LOW_PAGES = 1<<(32-PAGE_SHIFT_asm)
60 * To preserve the DMA pool in PAGEALLOC kernels, we'll allocate
61 * pagetables from above the 16MB DMA limit, so we'll have to set
62 * up pagetables 16MB more (worst-case):
64 #ifdef CONFIG_DEBUG_PAGEALLOC
65 LOW_PAGES = LOW_PAGES + 0x1000000
69 PAGE_TABLE_SIZE = (LOW_PAGES / PTRS_PER_PMD) + PTRS_PER_PGD
71 PAGE_TABLE_SIZE = (LOW_PAGES / PTRS_PER_PGD)
73 BOOTBITMAP_SIZE = LOW_PAGES / 8
76 INIT_MAP_BEYOND_END = BOOTBITMAP_SIZE + (PAGE_TABLE_SIZE + ALLOCATOR_SLOP)*PAGE_SIZE_asm
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 on
82 * any particular GDT layout, because we load our own as soon as we
85 .section .text.head,"ax",@progbits
87 /* test KEEP_SEGMENTS flag to see if the bootloader is asking
88 us to not reload segments */
89 testb $(1<<6), BP_loadflags(%esi)
93 * Set segments to known values.
95 lgdt pa(boot_gdt_descr)
96 movl $(__BOOT_DS),%eax
104 * Clear BSS first so that there are no surprises...
108 movl $pa(__bss_start),%edi
109 movl $pa(__bss_stop),%ecx
114 * Copy bootup parameters out of the way.
115 * Note: %esi still has the pointer to the real-mode data.
116 * With the kexec as boot loader, parameter segment might be loaded beyond
117 * kernel image and might not even be addressable by early boot page tables.
118 * (kexec on panic case). Hence copy out the parameters before initializing
121 movl $pa(boot_params),%edi
122 movl $(PARAM_SIZE/4),%ecx
126 movl pa(boot_params) + NEW_CL_POINTER,%esi
128 jz 1f # No comand line
129 movl $pa(boot_command_line),%edi
130 movl $(COMMAND_LINE_SIZE/4),%ecx
135 #ifdef CONFIG_PARAVIRT
136 /* This is can only trip for a broken bootloader... */
137 cmpw $0x207, pa(boot_params + BP_version)
140 /* Paravirt-compatible boot parameters. Look to see what architecture
141 we're booting under. */
142 movl pa(boot_params + BP_hardware_subarch), %eax
143 cmpl $num_subarch_entries, %eax
146 movl pa(subarch_entries)(,%eax,4), %eax
147 subl $__PAGE_OFFSET, %eax
153 /* Unknown implementation; there's really
154 nothing we can do at this point. */
160 .long default_entry /* normal x86/PC */
161 .long lguest_entry /* lguest hypervisor */
162 .long xen_entry /* Xen hypervisor */
163 num_subarch_entries = (. - subarch_entries) / 4
165 #endif /* CONFIG_PARAVIRT */
168 * Initialize page tables. This creates a PDE and a set of page
169 * tables, which are located immediately beyond _end. The variable
170 * init_pg_tables_end is set up to point to the first "safe" location.
171 * Mappings are created both at virtual address 0 (identity mapping)
172 * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
174 * Note that the stack is not yet set up!
177 #ifdef CONFIG_X86_PAE
180 * In PAE mode swapper_pg_dir is statically defined to contain enough
181 * entries to cover the VMSPLIT option (that is the top 1, 2 or 3
182 * entries). The identity mapping is handled by pointing two PGD
183 * entries to the first kernel PMD.
185 * Note the upper half of each PMD or PTE are always zero at
189 #define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
191 xorl %ebx,%ebx /* %ebx is kept at zero */
194 movl %edi, pa(init_pg_tables_start)
195 movl $pa(swapper_pg_pmd), %edx
196 movl $PTE_IDENT_ATTR, %eax
198 leal PDE_IDENT_ATTR(%edi),%ecx /* Create PMD entry */
199 movl %ecx,(%edx) /* Store PMD entry */
200 /* Upper half already zero */
212 * End condition: we must map up to and including INIT_MAP_BEYOND_END
213 * bytes beyond the end of our own page tables.
215 leal (INIT_MAP_BEYOND_END+PTE_IDENT_ATTR)(%edi),%ebp
219 movl %edi,pa(init_pg_tables_end)
221 movl %eax, pa(max_pfn_mapped)
223 /* Do early initialization of the fixmap area */
224 movl $pa(swapper_pg_fixmap)+PDE_IDENT_ATTR,%eax
225 movl %eax,pa(swapper_pg_pmd+0x1000*KPMDS-8)
228 page_pde_offset = (__PAGE_OFFSET >> 20);
231 movl %edi, pa(init_pg_tables_start)
232 movl $pa(swapper_pg_dir), %edx
233 movl $PTE_IDENT_ATTR, %eax
235 leal PDE_IDENT_ATTR(%edi),%ecx /* Create PDE entry */
236 movl %ecx,(%edx) /* Store identity PDE entry */
237 movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
245 * End condition: we must map up to and including INIT_MAP_BEYOND_END
246 * bytes beyond the end of our own page tables; the +0x007 is
249 leal (INIT_MAP_BEYOND_END+PTE_IDENT_ATTR)(%edi),%ebp
252 movl %edi,pa(init_pg_tables_end)
254 movl %eax, pa(max_pfn_mapped)
256 /* Do early initialization of the fixmap area */
257 movl $pa(swapper_pg_fixmap)+PDE_IDENT_ATTR,%eax
258 movl %eax,pa(swapper_pg_dir+0xffc)
262 * Non-boot CPU entry point; entered from trampoline.S
263 * We can't lgdt here, because lgdt itself uses a data segment, but
264 * we know the trampoline has already loaded the boot_gdt for us.
266 * If cpu hotplug is not supported then this code can go in init section
267 * which will be freed later
270 #ifndef CONFIG_HOTPLUG_CPU
271 .section .init.text,"ax",@progbits
275 ENTRY(startup_32_smp)
277 movl $(__BOOT_DS),%eax
282 #endif /* CONFIG_SMP */
286 * New page tables may be in 4Mbyte page mode and may
287 * be using the global pages.
289 * NOTE! If we are on a 486 we may have no cr4 at all!
290 * So we do not try to touch it unless we really have
291 * some bits in it to set. This won't work if the BSP
292 * implements cr4 but this AP does not -- very unlikely
293 * but be warned! The same applies to the pse feature
294 * if not equally supported. --macro
296 * NOTE! We have to correct for the fact that we're
297 * not yet offset PAGE_OFFSET..
299 #define cr4_bits pa(mmu_cr4_features)
303 movl %cr4,%eax # Turn on paging options (PSE,PAE,..)
307 btl $5, %eax # check if PAE is enabled
310 /* Check if extended functions are implemented */
311 movl $0x80000000, %eax
313 cmpl $0x80000000, %eax
315 mov $0x80000001, %eax
317 /* Execute Disable bit supported? */
321 /* Setup EFER (Extended Feature Enable Register) */
322 movl $0xc0000080, %ecx
326 /* Make changes effective */
334 movl $pa(swapper_pg_dir),%eax
335 movl %eax,%cr3 /* set the page table pointer.. */
338 movl %eax,%cr0 /* ..and set paging (PG) bit */
339 ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
341 /* Set up the stack pointer */
345 * Initialize eflags. Some BIOS's leave bits like NT set. This would
346 * confuse the debugger if this code is traced.
347 * XXX - best to initialize before switching to protected mode.
354 jz 1f /* Initial CPU cleans BSS */
357 #endif /* CONFIG_SMP */
360 * start system 32-bit setup. We need to re-do some of the things done
361 * in 16-bit mode for the "real" operations.
367 movl $-1,X86_CPUID # -1 for no CPUID initially
369 /* check if it is 486 or 386. */
371 * XXX - this does a lot of unnecessary setup. Alignment checks don't
372 * apply at our cpl of 0 and the stack ought to be aligned already, and
373 * we don't need to preserve eflags.
376 movb $3,X86 # at least 386
378 popl %eax # get EFLAGS
379 movl %eax,%ecx # save original EFLAGS
380 xorl $0x240000,%eax # flip AC and ID bits in EFLAGS
381 pushl %eax # copy to EFLAGS
383 pushfl # get new EFLAGS
384 popl %eax # put it in eax
385 xorl %ecx,%eax # change in flags
386 pushl %ecx # restore original EFLAGS
388 testl $0x40000,%eax # check if AC bit changed
391 movb $4,X86 # at least 486
392 testl $0x200000,%eax # check if ID bit changed
395 /* get vendor info */
396 xorl %eax,%eax # call CPUID with 0 -> return vendor ID
398 movl %eax,X86_CPUID # save CPUID level
399 movl %ebx,X86_VENDOR_ID # lo 4 chars
400 movl %edx,X86_VENDOR_ID+4 # next 4 chars
401 movl %ecx,X86_VENDOR_ID+8 # last 4 chars
403 orl %eax,%eax # do we have processor info as well?
406 movl $1,%eax # Use the CPUID instruction to get CPU type
408 movb %al,%cl # save reg for future use
409 andb $0x0f,%ah # mask processor family
411 andb $0xf0,%al # mask model
414 andb $0x0f,%cl # mask mask revision
416 movl %edx,X86_CAPABILITY
418 is486: movl $0x50022,%ecx # set AM, WP, NE and MP
421 is386: movl $2,%ecx # set MP
423 andl $0x80000011,%eax # Save PG,PE,ET
430 ljmp $(__KERNEL_CS),$1f
431 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
432 movl %eax,%ss # after changing gdt.
434 movl $(__USER_DS),%eax # DS/ES contains default USER segment
438 movl $(__KERNEL_PERCPU), %eax
439 movl %eax,%fs # set this cpu's percpu
441 #ifdef CONFIG_CC_STACKPROTECTOR
443 * The linker can't handle this by relocation. Manually set
444 * base address in stack canary segment descriptor.
448 movl $per_cpu__gdt_page,%eax
449 movl $per_cpu__stack_canary,%ecx
451 movw %cx, 8 * GDT_ENTRY_STACK_CANARY + 2(%eax)
453 movb %cl, 8 * GDT_ENTRY_STACK_CANARY + 4(%eax)
454 movb %ch, 8 * GDT_ENTRY_STACK_CANARY + 7(%eax)
457 movl $(__KERNEL_STACK_CANARY),%eax
460 xorl %eax,%eax # Clear LDT
463 cld # gcc2 wants the direction flag cleared at all times
464 pushl $0 # fake return address for unwinder
468 cmpb $0,%cl # the first CPU calls start_kernel
470 movl (stack_start), %esp
472 #endif /* CONFIG_SMP */
476 * We depend on ET to be correct. This checks for 287/387.
479 movb $0,X86_HARD_MATH
485 movl %cr0,%eax /* no coprocessor: have to set bits */
486 xorl $4,%eax /* set EM */
490 1: movb $1,X86_HARD_MATH
491 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
497 * sets up a idt with 256 entries pointing to
498 * ignore_int, interrupt gates. It doesn't actually load
499 * idt - that can be done only after paging has been enabled
500 * and the kernel moved to PAGE_OFFSET. Interrupts
501 * are enabled elsewhere, when we can be relatively
502 * sure everything is ok.
504 * Warning: %esi is live across this function.
508 movl $(__KERNEL_CS << 16),%eax
509 movw %dx,%ax /* selector = 0x0010 = cs */
510 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
521 .macro set_early_handler handler,trapno
523 movl $(__KERNEL_CS << 16),%eax
525 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
527 movl %eax,8*\trapno(%edi)
528 movl %edx,8*\trapno+4(%edi)
531 set_early_handler handler=early_divide_err,trapno=0
532 set_early_handler handler=early_illegal_opcode,trapno=6
533 set_early_handler handler=early_protection_fault,trapno=13
534 set_early_handler handler=early_page_fault,trapno=14
540 pushl $0 /* fake errcode */
543 early_illegal_opcode:
545 pushl $0 /* fake errcode */
548 early_protection_fault:
560 movl $(__KERNEL_DS),%eax
563 cmpl $2,early_recursion_flag
565 incl early_recursion_flag
568 pushl %edx /* trapno */
577 /* This is the default interrupt "handler" :-) */
587 movl $(__KERNEL_DS),%eax
590 cmpl $2,early_recursion_flag
592 incl early_recursion_flag
611 .section .cpuinit.data,"wa"
614 .long i386_start_kernel
618 * Real beginning of normal "text" segment
626 .section ".bss.page_aligned","wa"
628 #ifdef CONFIG_X86_PAE
632 ENTRY(swapper_pg_dir)
637 ENTRY(empty_zero_page)
640 * This starts the data section.
642 #ifdef CONFIG_X86_PAE
643 .section ".data.page_aligned","wa"
644 /* Page-aligned for the benefit of paravirt? */
646 ENTRY(swapper_pg_dir)
647 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */
649 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR),0
650 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR+0x1000),0
651 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR+0x2000),0
654 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR),0
655 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR+0x1000),0
659 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR),0
661 # error "Kernel PMDs should be 1, 2 or 3"
663 .align PAGE_SIZE_asm /* needs to be page-sized too */
668 .long init_thread_union+THREAD_SIZE
673 early_recursion_flag:
677 .asciz "Unknown interrupt or fault at: %p %p %p\n"
681 .ascii "BUG: Int %d: CR2 %p\n"
683 .ascii " EDI %p ESI %p EBP %p ESP %p\n"
684 .ascii " EBX %p EDX %p ECX %p EAX %p\n"
686 .ascii " err %p EIP %p CS %p flg %p\n"
687 .ascii "Stack: %p %p %p %p %p %p %p %p\n"
688 .ascii " %p %p %p %p %p %p %p %p\n"
689 .asciz " %p %p %p %p %p %p %p %p\n"
691 #include "../../x86/xen/xen-head.S"
694 * The IDT and GDT 'descriptors' are a strange 48-bit object
695 * only used by the lidt and lgdt instructions. They are not
696 * like usual segment descriptors - they consist of a 16-bit
697 * segment size, and 32-bit linear address value:
700 .globl boot_gdt_descr
704 # early boot GDT descriptor (must use 1:1 address mapping)
705 .word 0 # 32 bit align gdt_desc.address
708 .long boot_gdt - __PAGE_OFFSET
710 .word 0 # 32-bit align idt_desc.address
712 .word IDT_ENTRIES*8-1 # idt contains 256 entries
715 # boot GDT descriptor (later on used by CPU#0):
716 .word 0 # 32 bit align gdt_desc.address
717 ENTRY(early_gdt_descr)
718 .word GDT_ENTRIES*8-1
719 .long per_cpu__gdt_page /* Overwritten for secondary CPUs */
722 * The boot_gdt must mirror the equivalent in setup.S and is
723 * used only for booting.
725 .align L1_CACHE_BYTES
727 .fill GDT_ENTRY_BOOT_CS,8,0
728 .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
729 .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */