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.
39 * - one bit for each possible page, but only in low memory, which means
40 * 2^32/4096/8 = 128K worst case (4G/4G split.)
41 * - enough space to map all low memory, which means
42 * (2^32/4096) / 1024 pages (worst case, non PAE)
43 * (2^32/4096) / 512 + 4 pages (worst case for PAE)
44 * - a few pages for allocator use before the kernel pagetable has
47 * Modulo rounding, each megabyte assigned here requires a kilobyte of
48 * memory, which is currently unreclaimed.
50 * This should be a multiple of a page.
52 LOW_PAGES = 1<<(32-PAGE_SHIFT_asm)
55 PAGE_TABLE_SIZE = (LOW_PAGES / PTRS_PER_PMD) + PTRS_PER_PGD
57 PAGE_TABLE_SIZE = (LOW_PAGES / PTRS_PER_PGD)
59 BOOTBITMAP_SIZE = LOW_PAGES / 8
62 INIT_MAP_BEYOND_END = BOOTBITMAP_SIZE + (PAGE_TABLE_SIZE + ALLOCATOR_SLOP)*PAGE_SIZE_asm
65 * 32-bit kernel entrypoint; only used by the boot CPU. On entry,
66 * %esi points to the real-mode code as a 32-bit pointer.
67 * CS and DS must be 4 GB flat segments, but we don't depend on
68 * any particular GDT layout, because we load our own as soon as we
71 .section .text.head,"ax",@progbits
75 * Set segments to known values.
78 lgdt boot_gdt_descr - __PAGE_OFFSET
79 movl $(__BOOT_DS),%eax
86 * Clear BSS first so that there are no surprises...
87 * No need to cld as DF is already clear from cld above...
90 movl $__bss_start - __PAGE_OFFSET,%edi
91 movl $__bss_stop - __PAGE_OFFSET,%ecx
96 * Copy bootup parameters out of the way.
97 * Note: %esi still has the pointer to the real-mode data.
98 * With the kexec as boot loader, parameter segment might be loaded beyond
99 * kernel image and might not even be addressable by early boot page tables.
100 * (kexec on panic case). Hence copy out the parameters before initializing
103 movl $(boot_params - __PAGE_OFFSET),%edi
104 movl $(PARAM_SIZE/4),%ecx
108 movl boot_params - __PAGE_OFFSET + NEW_CL_POINTER,%esi
110 jnz 2f # New command line protocol
111 cmpw $(OLD_CL_MAGIC),OLD_CL_MAGIC_ADDR
113 movzwl OLD_CL_OFFSET,%esi
114 addl $(OLD_CL_BASE_ADDR),%esi
116 movl $(boot_command_line - __PAGE_OFFSET),%edi
117 movl $(COMMAND_LINE_SIZE/4),%ecx
123 * Initialize page tables. This creates a PDE and a set of page
124 * tables, which are located immediately beyond _end. The variable
125 * init_pg_tables_end is set up to point to the first "safe" location.
126 * Mappings are created both at virtual address 0 (identity mapping)
127 * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
129 * Warning: don't use %esi or the stack in this code. However, %esp
130 * can be used as a GPR if you really need it...
132 page_pde_offset = (__PAGE_OFFSET >> 20);
134 movl $(pg0 - __PAGE_OFFSET), %edi
135 movl $(swapper_pg_dir - __PAGE_OFFSET), %edx
136 movl $0x007, %eax /* 0x007 = PRESENT+RW+USER */
138 leal 0x007(%edi),%ecx /* Create PDE entry */
139 movl %ecx,(%edx) /* Store identity PDE entry */
140 movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
147 /* End condition: we must map up to and including INIT_MAP_BEYOND_END */
148 /* bytes beyond the end of our own page tables; the +0x007 is the attribute bits */
149 leal (INIT_MAP_BEYOND_END+0x007)(%edi),%ebp
152 movl %edi,(init_pg_tables_end - __PAGE_OFFSET)
154 xorl %ebx,%ebx /* This is the boot CPU (BSP) */
157 * Non-boot CPU entry point; entered from trampoline.S
158 * We can't lgdt here, because lgdt itself uses a data segment, but
159 * we know the trampoline has already loaded the boot_gdt for us.
161 * If cpu hotplug is not supported then this code can go in init section
162 * which will be freed later
165 #ifdef CONFIG_HOTPLUG_CPU
166 .section .text,"ax",@progbits
168 .section .init.text,"ax",@progbits
172 ENTRY(startup_32_smp)
174 movl $(__BOOT_DS),%eax
181 * New page tables may be in 4Mbyte page mode and may
182 * be using the global pages.
184 * NOTE! If we are on a 486 we may have no cr4 at all!
185 * So we do not try to touch it unless we really have
186 * some bits in it to set. This won't work if the BSP
187 * implements cr4 but this AP does not -- very unlikely
188 * but be warned! The same applies to the pse feature
189 * if not equally supported. --macro
191 * NOTE! We have to correct for the fact that we're
192 * not yet offset PAGE_OFFSET..
194 #define cr4_bits mmu_cr4_features-__PAGE_OFFSET
198 movl %cr4,%eax # Turn on paging options (PSE,PAE,..)
202 btl $5, %eax # check if PAE is enabled
205 /* Check if extended functions are implemented */
206 movl $0x80000000, %eax
208 cmpl $0x80000000, %eax
210 mov $0x80000001, %eax
212 /* Execute Disable bit supported? */
216 /* Setup EFER (Extended Feature Enable Register) */
217 movl $0xc0000080, %ecx
221 /* Make changes effective */
225 /* This is a secondary processor (AP) */
229 #endif /* CONFIG_SMP */
235 movl $swapper_pg_dir-__PAGE_OFFSET,%eax
236 movl %eax,%cr3 /* set the page table pointer.. */
239 movl %eax,%cr0 /* ..and set paging (PG) bit */
240 ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
242 /* Set up the stack pointer */
246 * Initialize eflags. Some BIOS's leave bits like NT set. This would
247 * confuse the debugger if this code is traced.
248 * XXX - best to initialize before switching to protected mode.
255 jz 1f /* Initial CPU cleans BSS */
258 #endif /* CONFIG_SMP */
261 * start system 32-bit setup. We need to re-do some of the things done
262 * in 16-bit mode for the "real" operations.
268 movl $-1,X86_CPUID # -1 for no CPUID initially
270 /* check if it is 486 or 386. */
272 * XXX - this does a lot of unnecessary setup. Alignment checks don't
273 * apply at our cpl of 0 and the stack ought to be aligned already, and
274 * we don't need to preserve eflags.
277 movb $3,X86 # at least 386
279 popl %eax # get EFLAGS
280 movl %eax,%ecx # save original EFLAGS
281 xorl $0x240000,%eax # flip AC and ID bits in EFLAGS
282 pushl %eax # copy to EFLAGS
284 pushfl # get new EFLAGS
285 popl %eax # put it in eax
286 xorl %ecx,%eax # change in flags
287 pushl %ecx # restore original EFLAGS
289 testl $0x40000,%eax # check if AC bit changed
292 movb $4,X86 # at least 486
293 testl $0x200000,%eax # check if ID bit changed
296 /* get vendor info */
297 xorl %eax,%eax # call CPUID with 0 -> return vendor ID
299 movl %eax,X86_CPUID # save CPUID level
300 movl %ebx,X86_VENDOR_ID # lo 4 chars
301 movl %edx,X86_VENDOR_ID+4 # next 4 chars
302 movl %ecx,X86_VENDOR_ID+8 # last 4 chars
304 orl %eax,%eax # do we have processor info as well?
307 movl $1,%eax # Use the CPUID instruction to get CPU type
309 movb %al,%cl # save reg for future use
310 andb $0x0f,%ah # mask processor family
312 andb $0xf0,%al # mask model
315 andb $0x0f,%cl # mask mask revision
317 movl %edx,X86_CAPABILITY
319 is486: movl $0x50022,%ecx # set AM, WP, NE and MP
322 is386: movl $2,%ecx # set MP
324 andl $0x80000011,%eax # Save PG,PE,ET
331 ljmp $(__KERNEL_CS),$1f
332 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
333 movl %eax,%ss # after changing gdt.
334 movl %eax,%fs # gets reset once there's real percpu
336 movl $(__USER_DS),%eax # DS/ES contains default USER segment
340 xorl %eax,%eax # Clear GS and LDT
344 cld # gcc2 wants the direction flag cleared at all times
345 pushl $0 # fake return address for unwinder
349 cmpb $0,%cl # the first CPU calls start_kernel
351 movl $(__KERNEL_PERCPU), %eax
352 movl %eax,%fs # set this cpu's percpu
353 jmp initialize_secondary # all other CPUs call initialize_secondary
355 #endif /* CONFIG_SMP */
359 * We depend on ET to be correct. This checks for 287/387.
362 movb $0,X86_HARD_MATH
368 movl %cr0,%eax /* no coprocessor: have to set bits */
369 xorl $4,%eax /* set EM */
373 1: movb $1,X86_HARD_MATH
374 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
380 * sets up a idt with 256 entries pointing to
381 * ignore_int, interrupt gates. It doesn't actually load
382 * idt - that can be done only after paging has been enabled
383 * and the kernel moved to PAGE_OFFSET. Interrupts
384 * are enabled elsewhere, when we can be relatively
385 * sure everything is ok.
387 * Warning: %esi is live across this function.
391 movl $(__KERNEL_CS << 16),%eax
392 movw %dx,%ax /* selector = 0x0010 = cs */
393 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
404 .macro set_early_handler handler,trapno
406 movl $(__KERNEL_CS << 16),%eax
408 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
410 movl %eax,8*\trapno(%edi)
411 movl %edx,8*\trapno+4(%edi)
414 set_early_handler handler=early_divide_err,trapno=0
415 set_early_handler handler=early_illegal_opcode,trapno=6
416 set_early_handler handler=early_protection_fault,trapno=13
417 set_early_handler handler=early_page_fault,trapno=14
423 pushl $0 /* fake errcode */
426 early_illegal_opcode:
428 pushl $0 /* fake errcode */
431 early_protection_fault:
442 movl $(__KERNEL_DS),%eax
445 cmpl $2,early_recursion_flag
447 incl early_recursion_flag
450 pushl %edx /* trapno */
452 #ifdef CONFIG_EARLY_PRINTK
462 /* This is the default interrupt "handler" :-) */
472 movl $(__KERNEL_DS),%eax
475 cmpl $2,early_recursion_flag
477 incl early_recursion_flag
483 #ifdef CONFIG_EARLY_PRINTK
499 * Real beginning of normal "text" segment
507 .section ".bss.page_aligned","w"
508 ENTRY(swapper_pg_dir)
510 ENTRY(empty_zero_page)
514 * This starts the data section.
518 .long init_thread_union+THREAD_SIZE
523 early_recursion_flag:
527 .asciz "Unknown interrupt or fault at EIP %p %p %p\n"
530 .ascii "Int %d: CR2 %p err %p EIP %p CS %p flags %p\n"
531 .asciz "Stack: %p %p %p %p %p %p %p %p\n"
534 * The IDT and GDT 'descriptors' are a strange 48-bit object
535 * only used by the lidt and lgdt instructions. They are not
536 * like usual segment descriptors - they consist of a 16-bit
537 * segment size, and 32-bit linear address value:
540 .globl boot_gdt_descr
544 # early boot GDT descriptor (must use 1:1 address mapping)
545 .word 0 # 32 bit align gdt_desc.address
548 .long boot_gdt - __PAGE_OFFSET
550 .word 0 # 32-bit align idt_desc.address
552 .word IDT_ENTRIES*8-1 # idt contains 256 entries
555 # boot GDT descriptor (later on used by CPU#0):
556 .word 0 # 32 bit align gdt_desc.address
557 ENTRY(early_gdt_descr)
558 .word GDT_ENTRIES*8-1
559 .long per_cpu__gdt_page /* Overwritten for secondary CPUs */
562 * The boot_gdt must mirror the equivalent in setup.S and is
563 * used only for booting.
565 .align L1_CACHE_BYTES
567 .fill GDT_ENTRY_BOOT_CS,8,0
568 .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
569 .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */