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/config.h>
12 #include <linux/threads.h>
13 #include <linux/linkage.h>
14 #include <asm/segment.h>
16 #include <asm/pgtable.h>
18 #include <asm/cache.h>
19 #include <asm/thread_info.h>
20 #include <asm/asm-offsets.h>
21 #include <asm/setup.h>
24 * References to members of the new_cpu_data structure.
27 #define X86 new_cpu_data+CPUINFO_x86
28 #define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor
29 #define X86_MODEL new_cpu_data+CPUINFO_x86_model
30 #define X86_MASK new_cpu_data+CPUINFO_x86_mask
31 #define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math
32 #define X86_CPUID new_cpu_data+CPUINFO_cpuid_level
33 #define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability
34 #define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id
37 * This is how much memory *in addition to the memory covered up to
38 * and including _end* we need mapped initially. We need one bit for
39 * each possible page, but only in low memory, which means
40 * 2^32/4096/8 = 128K worst case (4G/4G split.)
42 * Modulo rounding, each megabyte assigned here requires a kilobyte of
43 * memory, which is currently unreclaimed.
45 * This should be a multiple of a page.
47 #define INIT_MAP_BEYOND_END (128*1024)
51 * 32-bit kernel entrypoint; only used by the boot CPU. On entry,
52 * %esi points to the real-mode code as a 32-bit pointer.
53 * CS and DS must be 4 GB flat segments, but we don't depend on
54 * any particular GDT layout, because we load our own as soon as we
60 * Set segments to known values.
63 lgdt boot_gdt_descr - __PAGE_OFFSET
64 movl $(__BOOT_DS),%eax
71 * Clear BSS first so that there are no surprises...
72 * No need to cld as DF is already clear from cld above...
75 movl $__bss_start - __PAGE_OFFSET,%edi
76 movl $__bss_stop - __PAGE_OFFSET,%ecx
81 * Copy bootup parameters out of the way.
82 * Note: %esi still has the pointer to the real-mode data.
83 * With the kexec as boot loader, parameter segment might be loaded beyond
84 * kernel image and might not even be addressable by early boot page tables.
85 * (kexec on panic case). Hence copy out the parameters before initializing
88 movl $(boot_params - __PAGE_OFFSET),%edi
89 movl $(PARAM_SIZE/4),%ecx
93 movl boot_params - __PAGE_OFFSET + NEW_CL_POINTER,%esi
95 jnz 2f # New command line protocol
96 cmpw $(OLD_CL_MAGIC),OLD_CL_MAGIC_ADDR
98 movzwl OLD_CL_OFFSET,%esi
99 addl $(OLD_CL_BASE_ADDR),%esi
101 movl $(saved_command_line - __PAGE_OFFSET),%edi
102 movl $(COMMAND_LINE_SIZE/4),%ecx
108 * Initialize page tables. This creates a PDE and a set of page
109 * tables, which are located immediately beyond _end. The variable
110 * init_pg_tables_end is set up to point to the first "safe" location.
111 * Mappings are created both at virtual address 0 (identity mapping)
112 * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
114 * Warning: don't use %esi or the stack in this code. However, %esp
115 * can be used as a GPR if you really need it...
117 page_pde_offset = (__PAGE_OFFSET >> 20);
119 movl $(pg0 - __PAGE_OFFSET), %edi
120 movl $(swapper_pg_dir - __PAGE_OFFSET), %edx
121 movl $0x007, %eax /* 0x007 = PRESENT+RW+USER */
123 leal 0x007(%edi),%ecx /* Create PDE entry */
124 movl %ecx,(%edx) /* Store identity PDE entry */
125 movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
132 /* End condition: we must map up to and including INIT_MAP_BEYOND_END */
133 /* bytes beyond the end of our own page tables; the +0x007 is the attribute bits */
134 leal (INIT_MAP_BEYOND_END+0x007)(%edi),%ebp
137 movl %edi,(init_pg_tables_end - __PAGE_OFFSET)
140 xorl %ebx,%ebx /* This is the boot CPU (BSP) */
144 * Non-boot CPU entry point; entered from trampoline.S
145 * We can't lgdt here, because lgdt itself uses a data segment, but
146 * we know the trampoline has already loaded the boot_gdt_table GDT
149 ENTRY(startup_32_smp)
151 movl $(__BOOT_DS),%eax
158 * New page tables may be in 4Mbyte page mode and may
159 * be using the global pages.
161 * NOTE! If we are on a 486 we may have no cr4 at all!
162 * So we do not try to touch it unless we really have
163 * some bits in it to set. This won't work if the BSP
164 * implements cr4 but this AP does not -- very unlikely
165 * but be warned! The same applies to the pse feature
166 * if not equally supported. --macro
168 * NOTE! We have to correct for the fact that we're
169 * not yet offset PAGE_OFFSET..
171 #define cr4_bits mmu_cr4_features-__PAGE_OFFSET
175 movl %cr4,%eax # Turn on paging options (PSE,PAE,..)
179 btl $5, %eax # check if PAE is enabled
182 /* Check if extended functions are implemented */
183 movl $0x80000000, %eax
185 cmpl $0x80000000, %eax
187 mov $0x80000001, %eax
189 /* Execute Disable bit supported? */
193 /* Setup EFER (Extended Feature Enable Register) */
194 movl $0xc0000080, %ecx
198 /* Make changes effective */
202 /* This is a secondary processor (AP) */
207 #endif /* CONFIG_SMP */
212 movl $swapper_pg_dir-__PAGE_OFFSET,%eax
213 movl %eax,%cr3 /* set the page table pointer.. */
216 movl %eax,%cr0 /* ..and set paging (PG) bit */
217 ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
219 /* Set up the stack pointer */
223 * Initialize eflags. Some BIOS's leave bits like NT set. This would
224 * confuse the debugger if this code is traced.
225 * XXX - best to initialize before switching to protected mode.
232 jz 1f /* Initial CPU cleans BSS */
235 #endif /* CONFIG_SMP */
238 * start system 32-bit setup. We need to re-do some of the things done
239 * in 16-bit mode for the "real" operations.
245 movl $-1,X86_CPUID # -1 for no CPUID initially
247 /* check if it is 486 or 386. */
249 * XXX - this does a lot of unnecessary setup. Alignment checks don't
250 * apply at our cpl of 0 and the stack ought to be aligned already, and
251 * we don't need to preserve eflags.
254 movb $3,X86 # at least 386
256 popl %eax # get EFLAGS
257 movl %eax,%ecx # save original EFLAGS
258 xorl $0x240000,%eax # flip AC and ID bits in EFLAGS
259 pushl %eax # copy to EFLAGS
261 pushfl # get new EFLAGS
262 popl %eax # put it in eax
263 xorl %ecx,%eax # change in flags
264 pushl %ecx # restore original EFLAGS
266 testl $0x40000,%eax # check if AC bit changed
269 movb $4,X86 # at least 486
270 testl $0x200000,%eax # check if ID bit changed
273 /* get vendor info */
274 xorl %eax,%eax # call CPUID with 0 -> return vendor ID
276 movl %eax,X86_CPUID # save CPUID level
277 movl %ebx,X86_VENDOR_ID # lo 4 chars
278 movl %edx,X86_VENDOR_ID+4 # next 4 chars
279 movl %ecx,X86_VENDOR_ID+8 # last 4 chars
281 orl %eax,%eax # do we have processor info as well?
284 movl $1,%eax # Use the CPUID instruction to get CPU type
286 movb %al,%cl # save reg for future use
287 andb $0x0f,%ah # mask processor family
289 andb $0xf0,%al # mask model
292 andb $0x0f,%cl # mask mask revision
294 movl %edx,X86_CAPABILITY
296 is486: movl $0x50022,%ecx # set AM, WP, NE and MP
299 is386: movl $2,%ecx # set MP
301 andl $0x80000011,%eax # Save PG,PE,ET
308 ljmp $(__KERNEL_CS),$1f
309 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
310 movl %eax,%ss # after changing gdt.
312 movl $(__USER_DS),%eax # DS/ES contains default USER segment
316 xorl %eax,%eax # Clear FS/GS and LDT
320 cld # gcc2 wants the direction flag cleared at all times
325 je 1f # the first CPU calls start_kernel
326 # all other CPUs call initialize_secondary
327 call initialize_secondary
330 #endif /* CONFIG_SMP */
333 jmp L6 # main should never return here, but
334 # just in case, we know what happens.
337 * We depend on ET to be correct. This checks for 287/387.
340 movb $0,X86_HARD_MATH
346 movl %cr0,%eax /* no coprocessor: have to set bits */
347 xorl $4,%eax /* set EM */
351 1: movb $1,X86_HARD_MATH
352 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
358 * sets up a idt with 256 entries pointing to
359 * ignore_int, interrupt gates. It doesn't actually load
360 * idt - that can be done only after paging has been enabled
361 * and the kernel moved to PAGE_OFFSET. Interrupts
362 * are enabled elsewhere, when we can be relatively
363 * sure everything is ok.
365 * Warning: %esi is live across this function.
369 movl $(__KERNEL_CS << 16),%eax
370 movw %dx,%ax /* selector = 0x0010 = cs */
371 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
383 /* This is the default interrupt "handler" :-) */
393 movl $(__KERNEL_DS),%eax
412 * Real beginning of normal "text" segment
420 .section ".bss.page_aligned","w"
421 ENTRY(swapper_pg_dir)
423 ENTRY(empty_zero_page)
427 * This starts the data section.
432 .long init_thread_union+THREAD_SIZE
438 .asciz "Unknown interrupt or fault at EIP %p %p %p\n"
441 * The IDT and GDT 'descriptors' are a strange 48-bit object
442 * only used by the lidt and lgdt instructions. They are not
443 * like usual segment descriptors - they consist of a 16-bit
444 * segment size, and 32-bit linear address value:
447 .globl boot_gdt_descr
452 # early boot GDT descriptor (must use 1:1 address mapping)
453 .word 0 # 32 bit align gdt_desc.address
456 .long boot_gdt_table - __PAGE_OFFSET
458 .word 0 # 32-bit align idt_desc.address
460 .word IDT_ENTRIES*8-1 # idt contains 256 entries
463 # boot GDT descriptor (later on used by CPU#0):
464 .word 0 # 32 bit align gdt_desc.address
466 .word GDT_ENTRIES*8-1
469 .fill NR_CPUS-1,8,0 # space for the other GDT descriptors
472 * The boot_gdt_table must mirror the equivalent in setup.S and is
473 * used only for booting.
475 .align L1_CACHE_BYTES
476 ENTRY(boot_gdt_table)
477 .fill GDT_ENTRY_BOOT_CS,8,0
478 .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
479 .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */
482 * The Global Descriptor Table contains 28 quadwords, per-CPU.
486 .quad 0x0000000000000000 /* NULL descriptor */
487 .quad 0x0000000000000000 /* 0x0b reserved */
488 .quad 0x0000000000000000 /* 0x13 reserved */
489 .quad 0x0000000000000000 /* 0x1b reserved */
490 .quad 0x0000000000000000 /* 0x20 unused */
491 .quad 0x0000000000000000 /* 0x28 unused */
492 .quad 0x0000000000000000 /* 0x33 TLS entry 1 */
493 .quad 0x0000000000000000 /* 0x3b TLS entry 2 */
494 .quad 0x0000000000000000 /* 0x43 TLS entry 3 */
495 .quad 0x0000000000000000 /* 0x4b reserved */
496 .quad 0x0000000000000000 /* 0x53 reserved */
497 .quad 0x0000000000000000 /* 0x5b reserved */
499 .quad 0x00cf9a000000ffff /* 0x60 kernel 4GB code at 0x00000000 */
500 .quad 0x00cf92000000ffff /* 0x68 kernel 4GB data at 0x00000000 */
501 .quad 0x00cffa000000ffff /* 0x73 user 4GB code at 0x00000000 */
502 .quad 0x00cff2000000ffff /* 0x7b user 4GB data at 0x00000000 */
504 .quad 0x0000000000000000 /* 0x80 TSS descriptor */
505 .quad 0x0000000000000000 /* 0x88 LDT descriptor */
507 /* Segments used for calling PnP BIOS */
508 .quad 0x00c09a0000000000 /* 0x90 32-bit code */
509 .quad 0x00809a0000000000 /* 0x98 16-bit code */
510 .quad 0x0080920000000000 /* 0xa0 16-bit data */
511 .quad 0x0080920000000000 /* 0xa8 16-bit data */
512 .quad 0x0080920000000000 /* 0xb0 16-bit data */
514 * The APM segments have byte granularity and their bases
515 * and limits are set at run time.
517 .quad 0x00409a0000000000 /* 0xb8 APM CS code */
518 .quad 0x00009a0000000000 /* 0xc0 APM CS 16 code (16 bit) */
519 .quad 0x0040920000000000 /* 0xc8 APM DS data */
521 .quad 0x0000920000000000 /* 0xd0 - ESPFIX 16-bit SS */
522 .quad 0x0000000000000000 /* 0xd8 - unused */
523 .quad 0x0000000000000000 /* 0xe0 - unused */
524 .quad 0x0000000000000000 /* 0xe8 - unused */
525 .quad 0x0000000000000000 /* 0xf0 - unused */
526 .quad 0x0000000000000000 /* 0xf8 - GDT entry 31: double-fault TSS */