2 * linux/arch/i386/kernel/head.S -- the 32-bit startup code.
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Enhanced CPU detection and feature setting code by Mike Jagdis
7 * and Martin Mares, November 1997.
11 #include <linux/threads.h>
12 #include <linux/linkage.h>
13 #include <asm/segment.h>
15 #include <asm/pgtable.h>
17 #include <asm/cache.h>
18 #include <asm/thread_info.h>
19 #include <asm/asm-offsets.h>
20 #include <asm/setup.h>
23 * References to members of the new_cpu_data structure.
26 #define X86 new_cpu_data+CPUINFO_x86
27 #define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor
28 #define X86_MODEL new_cpu_data+CPUINFO_x86_model
29 #define X86_MASK new_cpu_data+CPUINFO_x86_mask
30 #define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math
31 #define X86_CPUID new_cpu_data+CPUINFO_cpuid_level
32 #define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability
33 #define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id
36 * This is how much memory *in addition to the memory covered up to
37 * and including _end* we need mapped initially. We need one bit for
38 * each possible page, but only in low memory, which means
39 * 2^32/4096/8 = 128K worst case (4G/4G split.)
41 * Modulo rounding, each megabyte assigned here requires a kilobyte of
42 * memory, which is currently unreclaimed.
44 * This should be a multiple of a page.
46 #define INIT_MAP_BEYOND_END (128*1024)
50 * 32-bit kernel entrypoint; only used by the boot CPU. On entry,
51 * %esi points to the real-mode code as a 32-bit pointer.
52 * CS and DS must be 4 GB flat segments, but we don't depend on
53 * any particular GDT layout, because we load our own as soon as we
56 .section .text.head,"ax",@progbits
59 #ifdef CONFIG_PARAVIRT
66 * Set segments to known values.
69 lgdt boot_gdt_descr - __PAGE_OFFSET
70 movl $(__BOOT_DS),%eax
77 * Clear BSS first so that there are no surprises...
78 * No need to cld as DF is already clear from cld above...
81 movl $__bss_start - __PAGE_OFFSET,%edi
82 movl $__bss_stop - __PAGE_OFFSET,%ecx
87 * Copy bootup parameters out of the way.
88 * Note: %esi still has the pointer to the real-mode data.
89 * With the kexec as boot loader, parameter segment might be loaded beyond
90 * kernel image and might not even be addressable by early boot page tables.
91 * (kexec on panic case). Hence copy out the parameters before initializing
94 movl $(boot_params - __PAGE_OFFSET),%edi
95 movl $(PARAM_SIZE/4),%ecx
99 movl boot_params - __PAGE_OFFSET + NEW_CL_POINTER,%esi
101 jnz 2f # New command line protocol
102 cmpw $(OLD_CL_MAGIC),OLD_CL_MAGIC_ADDR
104 movzwl OLD_CL_OFFSET,%esi
105 addl $(OLD_CL_BASE_ADDR),%esi
107 movl $(boot_command_line - __PAGE_OFFSET),%edi
108 movl $(COMMAND_LINE_SIZE/4),%ecx
114 * Initialize page tables. This creates a PDE and a set of page
115 * tables, which are located immediately beyond _end. The variable
116 * init_pg_tables_end is set up to point to the first "safe" location.
117 * Mappings are created both at virtual address 0 (identity mapping)
118 * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
120 * Warning: don't use %esi or the stack in this code. However, %esp
121 * can be used as a GPR if you really need it...
123 page_pde_offset = (__PAGE_OFFSET >> 20);
125 movl $(pg0 - __PAGE_OFFSET), %edi
126 movl $(swapper_pg_dir - __PAGE_OFFSET), %edx
127 movl $0x007, %eax /* 0x007 = PRESENT+RW+USER */
129 leal 0x007(%edi),%ecx /* Create PDE entry */
130 movl %ecx,(%edx) /* Store identity PDE entry */
131 movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
138 /* End condition: we must map up to and including INIT_MAP_BEYOND_END */
139 /* bytes beyond the end of our own page tables; the +0x007 is the attribute bits */
140 leal (INIT_MAP_BEYOND_END+0x007)(%edi),%ebp
143 movl %edi,(init_pg_tables_end - __PAGE_OFFSET)
145 xorl %ebx,%ebx /* This is the boot CPU (BSP) */
148 * Non-boot CPU entry point; entered from trampoline.S
149 * We can't lgdt here, because lgdt itself uses a data segment, but
150 * we know the trampoline has already loaded the boot_gdt for us.
152 * If cpu hotplug is not supported then this code can go in init section
153 * which will be freed later
156 #ifdef CONFIG_HOTPLUG_CPU
157 .section .text,"ax",@progbits
159 .section .init.text,"ax",@progbits
163 ENTRY(startup_32_smp)
165 movl $(__BOOT_DS),%eax
172 * New page tables may be in 4Mbyte page mode and may
173 * be using the global pages.
175 * NOTE! If we are on a 486 we may have no cr4 at all!
176 * So we do not try to touch it unless we really have
177 * some bits in it to set. This won't work if the BSP
178 * implements cr4 but this AP does not -- very unlikely
179 * but be warned! The same applies to the pse feature
180 * if not equally supported. --macro
182 * NOTE! We have to correct for the fact that we're
183 * not yet offset PAGE_OFFSET..
185 #define cr4_bits mmu_cr4_features-__PAGE_OFFSET
189 movl %cr4,%eax # Turn on paging options (PSE,PAE,..)
193 btl $5, %eax # check if PAE is enabled
196 /* Check if extended functions are implemented */
197 movl $0x80000000, %eax
199 cmpl $0x80000000, %eax
201 mov $0x80000001, %eax
203 /* Execute Disable bit supported? */
207 /* Setup EFER (Extended Feature Enable Register) */
208 movl $0xc0000080, %ecx
212 /* Make changes effective */
216 /* This is a secondary processor (AP) */
220 #endif /* CONFIG_SMP */
226 movl $swapper_pg_dir-__PAGE_OFFSET,%eax
227 movl %eax,%cr3 /* set the page table pointer.. */
230 movl %eax,%cr0 /* ..and set paging (PG) bit */
231 ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
233 /* Set up the stack pointer */
237 * Initialize eflags. Some BIOS's leave bits like NT set. This would
238 * confuse the debugger if this code is traced.
239 * XXX - best to initialize before switching to protected mode.
246 jz 1f /* Initial CPU cleans BSS */
249 #endif /* CONFIG_SMP */
252 * start system 32-bit setup. We need to re-do some of the things done
253 * in 16-bit mode for the "real" operations.
259 movl $-1,X86_CPUID # -1 for no CPUID initially
261 /* check if it is 486 or 386. */
263 * XXX - this does a lot of unnecessary setup. Alignment checks don't
264 * apply at our cpl of 0 and the stack ought to be aligned already, and
265 * we don't need to preserve eflags.
268 movb $3,X86 # at least 386
270 popl %eax # get EFLAGS
271 movl %eax,%ecx # save original EFLAGS
272 xorl $0x240000,%eax # flip AC and ID bits in EFLAGS
273 pushl %eax # copy to EFLAGS
275 pushfl # get new EFLAGS
276 popl %eax # put it in eax
277 xorl %ecx,%eax # change in flags
278 pushl %ecx # restore original EFLAGS
280 testl $0x40000,%eax # check if AC bit changed
283 movb $4,X86 # at least 486
284 testl $0x200000,%eax # check if ID bit changed
287 /* get vendor info */
288 xorl %eax,%eax # call CPUID with 0 -> return vendor ID
290 movl %eax,X86_CPUID # save CPUID level
291 movl %ebx,X86_VENDOR_ID # lo 4 chars
292 movl %edx,X86_VENDOR_ID+4 # next 4 chars
293 movl %ecx,X86_VENDOR_ID+8 # last 4 chars
295 orl %eax,%eax # do we have processor info as well?
298 movl $1,%eax # Use the CPUID instruction to get CPU type
300 movb %al,%cl # save reg for future use
301 andb $0x0f,%ah # mask processor family
303 andb $0xf0,%al # mask model
306 andb $0x0f,%cl # mask mask revision
308 movl %edx,X86_CAPABILITY
310 is486: movl $0x50022,%ecx # set AM, WP, NE and MP
313 is386: movl $2,%ecx # set MP
315 andl $0x80000011,%eax # Save PG,PE,ET
323 ljmp $(__KERNEL_CS),$1f
324 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
325 movl %eax,%ss # after changing gdt.
327 movl $(__USER_DS),%eax # DS/ES contains default USER segment
331 xorl %eax,%eax # Clear GS and LDT
335 movl $(__KERNEL_PDA),%eax
338 cld # gcc2 wants the direction flag cleared at all times
339 pushl $0 # fake return address for unwinder
343 cmpb $0,%cl # the first CPU calls start_kernel
344 jne initialize_secondary # all other CPUs call initialize_secondary
345 #endif /* CONFIG_SMP */
349 * We depend on ET to be correct. This checks for 287/387.
352 movb $0,X86_HARD_MATH
358 movl %cr0,%eax /* no coprocessor: have to set bits */
359 xorl $4,%eax /* set EM */
363 1: movb $1,X86_HARD_MATH
364 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
368 * Point the GDT at this CPU's PDA. On boot this will be
369 * cpu_gdt_table and boot_pda; for secondary CPUs, these will be
370 * that CPU's GDT and PDA.
373 /* get the PDA pointer */
376 /* slot the PDA address into the GDT */
377 mov early_gdt_descr+2, %ecx
378 mov %ax, (__KERNEL_PDA+0+2)(%ecx) /* base & 0x0000ffff */
380 mov %al, (__KERNEL_PDA+4+0)(%ecx) /* base & 0x00ff0000 */
381 mov %ah, (__KERNEL_PDA+4+3)(%ecx) /* base & 0xff000000 */
387 * sets up a idt with 256 entries pointing to
388 * ignore_int, interrupt gates. It doesn't actually load
389 * idt - that can be done only after paging has been enabled
390 * and the kernel moved to PAGE_OFFSET. Interrupts
391 * are enabled elsewhere, when we can be relatively
392 * sure everything is ok.
394 * Warning: %esi is live across this function.
398 movl $(__KERNEL_CS << 16),%eax
399 movw %dx,%ax /* selector = 0x0010 = cs */
400 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
411 .macro set_early_handler handler,trapno
413 movl $(__KERNEL_CS << 16),%eax
415 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
417 movl %eax,8*\trapno(%edi)
418 movl %edx,8*\trapno+4(%edi)
421 set_early_handler handler=early_divide_err,trapno=0
422 set_early_handler handler=early_illegal_opcode,trapno=6
423 set_early_handler handler=early_protection_fault,trapno=13
424 set_early_handler handler=early_page_fault,trapno=14
430 pushl $0 /* fake errcode */
433 early_illegal_opcode:
435 pushl $0 /* fake errcode */
438 early_protection_fault:
449 movl $(__KERNEL_DS),%eax
452 cmpl $2,early_recursion_flag
454 incl early_recursion_flag
457 pushl %edx /* trapno */
459 #ifdef CONFIG_EARLY_PRINTK
469 /* This is the default interrupt "handler" :-) */
479 movl $(__KERNEL_DS),%eax
482 cmpl $2,early_recursion_flag
484 incl early_recursion_flag
490 #ifdef CONFIG_EARLY_PRINTK
505 #ifdef CONFIG_PARAVIRT
508 movl $(init_thread_union+THREAD_SIZE),%esp
510 /* We take pains to preserve all the regs. */
515 pushl $__start_paravirtprobe
518 cmpl $__stop_paravirtprobe, %eax
519 je unhandled_paravirt
533 /* Nothing wanted us: we're screwed. */
538 * Real beginning of normal "text" segment
546 .section ".bss.page_aligned","w"
547 ENTRY(swapper_pg_dir)
549 ENTRY(empty_zero_page)
553 * This starts the data section.
560 .long init_thread_union+THREAD_SIZE
565 early_recursion_flag:
569 .asciz "Unknown interrupt or fault at EIP %p %p %p\n"
572 .ascii "Int %d: CR2 %p err %p EIP %p CS %p flags %p\n"
573 .asciz "Stack: %p %p %p %p %p %p %p %p\n"
576 * The IDT and GDT 'descriptors' are a strange 48-bit object
577 * only used by the lidt and lgdt instructions. They are not
578 * like usual segment descriptors - they consist of a 16-bit
579 * segment size, and 32-bit linear address value:
582 .globl boot_gdt_descr
586 # early boot GDT descriptor (must use 1:1 address mapping)
587 .word 0 # 32 bit align gdt_desc.address
590 .long boot_gdt - __PAGE_OFFSET
592 .word 0 # 32-bit align idt_desc.address
594 .word IDT_ENTRIES*8-1 # idt contains 256 entries
597 # boot GDT descriptor (later on used by CPU#0):
598 .word 0 # 32 bit align gdt_desc.address
599 ENTRY(early_gdt_descr)
600 .word GDT_ENTRIES*8-1
601 .long per_cpu__gdt_page /* Overwritten for secondary CPUs */
604 * The boot_gdt must mirror the equivalent in setup.S and is
605 * used only for booting.
607 .align L1_CACHE_BYTES
609 .fill GDT_ENTRY_BOOT_CS,8,0
610 .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
611 .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */