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
82 * Initialize page tables. This creates a PDE and a set of page
83 * tables, which are located immediately beyond _end. The variable
84 * init_pg_tables_end is set up to point to the first "safe" location.
85 * Mappings are created both at virtual address 0 (identity mapping)
86 * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
88 * Warning: don't use %esi or the stack in this code. However, %esp
89 * can be used as a GPR if you really need it...
91 page_pde_offset = (__PAGE_OFFSET >> 20);
93 movl $(pg0 - __PAGE_OFFSET), %edi
94 movl $(swapper_pg_dir - __PAGE_OFFSET), %edx
95 movl $0x007, %eax /* 0x007 = PRESENT+RW+USER */
97 leal 0x007(%edi),%ecx /* Create PDE entry */
98 movl %ecx,(%edx) /* Store identity PDE entry */
99 movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
106 /* End condition: we must map up to and including INIT_MAP_BEYOND_END */
107 /* bytes beyond the end of our own page tables; the +0x007 is the attribute bits */
108 leal (INIT_MAP_BEYOND_END+0x007)(%edi),%ebp
111 movl %edi,(init_pg_tables_end - __PAGE_OFFSET)
114 xorl %ebx,%ebx /* This is the boot CPU (BSP) */
118 * Non-boot CPU entry point; entered from trampoline.S
119 * We can't lgdt here, because lgdt itself uses a data segment, but
120 * we know the trampoline has already loaded the boot_gdt_table GDT
123 ENTRY(startup_32_smp)
125 movl $(__BOOT_DS),%eax
132 incl %ebx /* This is a secondary processor (AP) */
135 * New page tables may be in 4Mbyte page mode and may
136 * be using the global pages.
138 * NOTE! If we are on a 486 we may have no cr4 at all!
139 * So we do not try to touch it unless we really have
140 * some bits in it to set. This won't work if the BSP
141 * implements cr4 but this AP does not -- very unlikely
142 * but be warned! The same applies to the pse feature
143 * if not equally supported. --macro
145 * NOTE! We have to correct for the fact that we're
146 * not yet offset PAGE_OFFSET..
148 #define cr4_bits mmu_cr4_features-__PAGE_OFFSET
152 movl %cr4,%eax # Turn on paging options (PSE,PAE,..)
157 #endif /* CONFIG_SMP */
162 movl $swapper_pg_dir-__PAGE_OFFSET,%eax
163 movl %eax,%cr3 /* set the page table pointer.. */
166 movl %eax,%cr0 /* ..and set paging (PG) bit */
167 ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
169 /* Set up the stack pointer */
173 * Initialize eflags. Some BIOS's leave bits like NT set. This would
174 * confuse the debugger if this code is traced.
175 * XXX - best to initialize before switching to protected mode.
182 jz 1f /* Initial CPU cleans BSS */
185 #endif /* CONFIG_SMP */
188 * start system 32-bit setup. We need to re-do some of the things done
189 * in 16-bit mode for the "real" operations.
194 * Copy bootup parameters out of the way.
195 * Note: %esi still has the pointer to the real-mode data.
197 movl $boot_params,%edi
198 movl $(PARAM_SIZE/4),%ecx
202 movl boot_params+NEW_CL_POINTER,%esi
204 jnz 2f # New command line protocol
205 cmpw $(OLD_CL_MAGIC),OLD_CL_MAGIC_ADDR
207 movzwl OLD_CL_OFFSET,%esi
208 addl $(OLD_CL_BASE_ADDR),%esi
210 movl $saved_command_line,%edi
211 movl $(COMMAND_LINE_SIZE/4),%ecx
217 movl $-1,X86_CPUID # -1 for no CPUID initially
219 /* check if it is 486 or 386. */
221 * XXX - this does a lot of unnecessary setup. Alignment checks don't
222 * apply at our cpl of 0 and the stack ought to be aligned already, and
223 * we don't need to preserve eflags.
226 movb $3,X86 # at least 386
228 popl %eax # get EFLAGS
229 movl %eax,%ecx # save original EFLAGS
230 xorl $0x240000,%eax # flip AC and ID bits in EFLAGS
231 pushl %eax # copy to EFLAGS
233 pushfl # get new EFLAGS
234 popl %eax # put it in eax
235 xorl %ecx,%eax # change in flags
236 pushl %ecx # restore original EFLAGS
238 testl $0x40000,%eax # check if AC bit changed
241 movb $4,X86 # at least 486
242 testl $0x200000,%eax # check if ID bit changed
245 /* get vendor info */
246 xorl %eax,%eax # call CPUID with 0 -> return vendor ID
248 movl %eax,X86_CPUID # save CPUID level
249 movl %ebx,X86_VENDOR_ID # lo 4 chars
250 movl %edx,X86_VENDOR_ID+4 # next 4 chars
251 movl %ecx,X86_VENDOR_ID+8 # last 4 chars
253 orl %eax,%eax # do we have processor info as well?
256 movl $1,%eax # Use the CPUID instruction to get CPU type
258 movb %al,%cl # save reg for future use
259 andb $0x0f,%ah # mask processor family
261 andb $0xf0,%al # mask model
264 andb $0x0f,%cl # mask mask revision
266 movl %edx,X86_CAPABILITY
268 is486: movl $0x50022,%ecx # set AM, WP, NE and MP
271 is386: movl $2,%ecx # set MP
273 andl $0x80000011,%eax # Save PG,PE,ET
281 ljmp $(__KERNEL_CS),$1f
282 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
283 movl %eax,%ss # after changing gdt.
285 movl $(__USER_DS),%eax # DS/ES contains default USER segment
289 xorl %eax,%eax # Clear FS/GS and LDT
293 cld # gcc2 wants the direction flag cleared at all times
297 je 1f # the first CPU calls start_kernel
298 # all other CPUs call initialize_secondary
299 call initialize_secondary
302 #endif /* CONFIG_SMP */
305 jmp L6 # main should never return here, but
306 # just in case, we know what happens.
309 * We depend on ET to be correct. This checks for 287/387.
312 movb $0,X86_HARD_MATH
318 movl %cr0,%eax /* no coprocessor: have to set bits */
319 xorl $4,%eax /* set EM */
323 1: movb $1,X86_HARD_MATH
324 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
330 * sets up a idt with 256 entries pointing to
331 * ignore_int, interrupt gates. It doesn't actually load
332 * idt - that can be done only after paging has been enabled
333 * and the kernel moved to PAGE_OFFSET. Interrupts
334 * are enabled elsewhere, when we can be relatively
335 * sure everything is ok.
337 * Warning: %esi is live across this function.
341 movl $(__KERNEL_CS << 16),%eax
342 movw %dx,%ax /* selector = 0x0010 = cs */
343 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
355 /* This is the default interrupt "handler" :-) */
364 movl $(__KERNEL_DS),%eax
382 * Real beginning of normal "text" segment
390 .section ".bss.page_aligned","w"
391 ENTRY(swapper_pg_dir)
393 ENTRY(empty_zero_page)
397 * This starts the data section.
402 .long init_thread_union+THREAD_SIZE
408 .asciz "Unknown interrupt or fault at EIP %p %p %p\n"
411 * The IDT and GDT 'descriptors' are a strange 48-bit object
412 * only used by the lidt and lgdt instructions. They are not
413 * like usual segment descriptors - they consist of a 16-bit
414 * segment size, and 32-bit linear address value:
417 .globl boot_gdt_descr
422 # early boot GDT descriptor (must use 1:1 address mapping)
423 .word 0 # 32 bit align gdt_desc.address
426 .long boot_gdt_table - __PAGE_OFFSET
428 .word 0 # 32-bit align idt_desc.address
430 .word IDT_ENTRIES*8-1 # idt contains 256 entries
433 # boot GDT descriptor (later on used by CPU#0):
434 .word 0 # 32 bit align gdt_desc.address
436 .word GDT_ENTRIES*8-1
439 .fill NR_CPUS-1,8,0 # space for the other GDT descriptors
442 * The boot_gdt_table must mirror the equivalent in setup.S and is
443 * used only for booting.
445 .align L1_CACHE_BYTES
446 ENTRY(boot_gdt_table)
447 .fill GDT_ENTRY_BOOT_CS,8,0
448 .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
449 .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */
452 * The Global Descriptor Table contains 28 quadwords, per-CPU.
456 .quad 0x0000000000000000 /* NULL descriptor */
457 .quad 0x0000000000000000 /* 0x0b reserved */
458 .quad 0x0000000000000000 /* 0x13 reserved */
459 .quad 0x0000000000000000 /* 0x1b reserved */
460 .quad 0x0000000000000000 /* 0x20 unused */
461 .quad 0x0000000000000000 /* 0x28 unused */
462 .quad 0x0000000000000000 /* 0x33 TLS entry 1 */
463 .quad 0x0000000000000000 /* 0x3b TLS entry 2 */
464 .quad 0x0000000000000000 /* 0x43 TLS entry 3 */
465 .quad 0x0000000000000000 /* 0x4b reserved */
466 .quad 0x0000000000000000 /* 0x53 reserved */
467 .quad 0x0000000000000000 /* 0x5b reserved */
469 .quad 0x00cf9a000000ffff /* 0x60 kernel 4GB code at 0x00000000 */
470 .quad 0x00cf92000000ffff /* 0x68 kernel 4GB data at 0x00000000 */
471 .quad 0x00cffa000000ffff /* 0x73 user 4GB code at 0x00000000 */
472 .quad 0x00cff2000000ffff /* 0x7b user 4GB data at 0x00000000 */
474 .quad 0x0000000000000000 /* 0x80 TSS descriptor */
475 .quad 0x0000000000000000 /* 0x88 LDT descriptor */
477 /* Segments used for calling PnP BIOS */
478 .quad 0x00c09a0000000000 /* 0x90 32-bit code */
479 .quad 0x00809a0000000000 /* 0x98 16-bit code */
480 .quad 0x0080920000000000 /* 0xa0 16-bit data */
481 .quad 0x0080920000000000 /* 0xa8 16-bit data */
482 .quad 0x0080920000000000 /* 0xb0 16-bit data */
484 * The APM segments have byte granularity and their bases
485 * and limits are set at run time.
487 .quad 0x00409a0000000000 /* 0xb8 APM CS code */
488 .quad 0x00009a0000000000 /* 0xc0 APM CS 16 code (16 bit) */
489 .quad 0x0040920000000000 /* 0xc8 APM DS data */
491 .quad 0x0000000000000000 /* 0xd0 - unused */
492 .quad 0x0000000000000000 /* 0xd8 - unused */
493 .quad 0x0000000000000000 /* 0xe0 - unused */
494 .quad 0x0000000000000000 /* 0xe8 - unused */
495 .quad 0x0000000000000000 /* 0xf0 - unused */
496 .quad 0x0000000000000000 /* 0xf8 - GDT entry 31: double-fault TSS */
499 .fill (NR_CPUS-1)*GDT_ENTRIES,8,0 /* other CPU's GDT */