blob: c32ca19d591a5de88057d2d9534168332fa7bf77 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 *
5 * Enhanced CPU detection and feature setting code by Mike Jagdis
6 * and Martin Mares, November 1997.
7 */
8
9.text
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/threads.h>
Sam Ravnborg8b2f7ff2008-01-30 13:33:28 +010011#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/linkage.h>
13#include <asm/segment.h>
Jeremy Fitzhardinge0341c142009-02-13 11:14:01 -080014#include <asm/page_types.h>
15#include <asm/pgtable_types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/desc.h>
17#include <asm/cache.h>
18#include <asm/thread_info.h>
Sam Ravnborg86feeaa2005-09-09 19:28:28 +020019#include <asm/asm-offsets.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/setup.h>
Ian Campbell551889a62008-02-09 23:24:09 +010021#include <asm/processor-flags.h>
Tejun Heo60a53172009-02-09 22:17:40 +090022#include <asm/percpu.h>
Ian Campbell551889a62008-02-09 23:24:09 +010023
24/* Physical address */
25#define pa(X) ((X) - __PAGE_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
28 * References to members of the new_cpu_data structure.
29 */
30
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
39
40/*
41 * This is how much memory *in addition to the memory covered up to
Jeremy Fitzhardinge9ce8c2e2007-05-02 19:27:16 +020042 * and including _end* we need mapped initially.
43 * We need:
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
50 * been set up
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 *
52 * Modulo rounding, each megabyte assigned here requires a kilobyte of
53 * memory, which is currently unreclaimed.
54 *
55 * This should be a multiple of a page.
56 */
Jeremy Fitzhardinge9ce8c2e2007-05-02 19:27:16 +020057LOW_PAGES = 1<<(32-PAGE_SHIFT_asm)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Ingo Molnar1e3e1972007-10-17 18:04:34 +020059/*
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):
63 */
64#ifdef CONFIG_DEBUG_PAGEALLOC
65LOW_PAGES = LOW_PAGES + 0x1000000
66#endif
67
Jeremy Fitzhardinge9ce8c2e2007-05-02 19:27:16 +020068#if PTRS_PER_PMD > 1
69PAGE_TABLE_SIZE = (LOW_PAGES / PTRS_PER_PMD) + PTRS_PER_PGD
70#else
71PAGE_TABLE_SIZE = (LOW_PAGES / PTRS_PER_PGD)
72#endif
73BOOTBITMAP_SIZE = LOW_PAGES / 8
74ALLOCATOR_SLOP = 4
75
76INIT_MAP_BEYOND_END = BOOTBITMAP_SIZE + (PAGE_TABLE_SIZE + ALLOCATOR_SLOP)*PAGE_SIZE_asm
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
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
83 * can.
84 */
Vivek Goyalf8657e12007-02-13 13:26:22 +010085.section .text.head,"ax",@progbits
Linus Torvalds1da177e2005-04-16 15:20:36 -070086ENTRY(startup_32)
Rusty Russella24e7852007-10-21 16:41:35 -070087 /* test KEEP_SEGMENTS flag to see if the bootloader is asking
88 us to not reload segments */
89 testb $(1<<6), BP_loadflags(%esi)
90 jnz 2f
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/*
93 * Set segments to known values.
94 */
Ian Campbell551889a62008-02-09 23:24:09 +010095 lgdt pa(boot_gdt_descr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 movl $(__BOOT_DS),%eax
97 movl %eax,%ds
98 movl %eax,%es
99 movl %eax,%fs
100 movl %eax,%gs
Rusty Russella24e7852007-10-21 16:41:35 -07001012:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/*
104 * Clear BSS first so that there are no surprises...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 */
Rusty Russella24e7852007-10-21 16:41:35 -0700106 cld
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 xorl %eax,%eax
Ian Campbell551889a62008-02-09 23:24:09 +0100108 movl $pa(__bss_start),%edi
109 movl $pa(__bss_stop),%ecx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 subl %edi,%ecx
111 shrl $2,%ecx
112 rep ; stosl
Vivek Goyal484b90c2005-09-03 15:56:31 -0700113/*
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
119 * page tables.
120 */
Ian Campbell551889a62008-02-09 23:24:09 +0100121 movl $pa(boot_params),%edi
Vivek Goyal484b90c2005-09-03 15:56:31 -0700122 movl $(PARAM_SIZE/4),%ecx
123 cld
124 rep
125 movsl
Ian Campbell551889a62008-02-09 23:24:09 +0100126 movl pa(boot_params) + NEW_CL_POINTER,%esi
Vivek Goyal484b90c2005-09-03 15:56:31 -0700127 andl %esi,%esi
H. Peter Anvinfa76dab2007-10-23 22:37:25 +0200128 jz 1f # No comand line
Ian Campbell551889a62008-02-09 23:24:09 +0100129 movl $pa(boot_command_line),%edi
Vivek Goyal484b90c2005-09-03 15:56:31 -0700130 movl $(COMMAND_LINE_SIZE/4),%ecx
131 rep
132 movsl
1331:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Rusty Russella24e7852007-10-21 16:41:35 -0700135#ifdef CONFIG_PARAVIRT
Ian Campbell551889a62008-02-09 23:24:09 +0100136 /* This is can only trip for a broken bootloader... */
137 cmpw $0x207, pa(boot_params + BP_version)
Rusty Russella24e7852007-10-21 16:41:35 -0700138 jb default_entry
139
140 /* Paravirt-compatible boot parameters. Look to see what architecture
141 we're booting under. */
Ian Campbell551889a62008-02-09 23:24:09 +0100142 movl pa(boot_params + BP_hardware_subarch), %eax
Rusty Russella24e7852007-10-21 16:41:35 -0700143 cmpl $num_subarch_entries, %eax
144 jae bad_subarch
145
Ian Campbell551889a62008-02-09 23:24:09 +0100146 movl pa(subarch_entries)(,%eax,4), %eax
Rusty Russella24e7852007-10-21 16:41:35 -0700147 subl $__PAGE_OFFSET, %eax
148 jmp *%eax
149
150bad_subarch:
151WEAK(lguest_entry)
152WEAK(xen_entry)
153 /* Unknown implementation; there's really
154 nothing we can do at this point. */
155 ud2a
Sam Ravnborg8b2f7ff2008-01-30 13:33:28 +0100156
157 __INITDATA
158
Rusty Russella24e7852007-10-21 16:41:35 -0700159subarch_entries:
160 .long default_entry /* normal x86/PC */
161 .long lguest_entry /* lguest hypervisor */
162 .long xen_entry /* Xen hypervisor */
163num_subarch_entries = (. - subarch_entries) / 4
164.previous
165#endif /* CONFIG_PARAVIRT */
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
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.
173 *
Ian Campbell551889a62008-02-09 23:24:09 +0100174 * Note that the stack is not yet set up!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
Rusty Russella24e7852007-10-21 16:41:35 -0700176default_entry:
Ian Campbell551889a62008-02-09 23:24:09 +0100177#ifdef CONFIG_X86_PAE
178
179 /*
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.
184 *
185 * Note the upper half of each PMD or PTE are always zero at
186 * this stage.
187 */
188
Joe Korty86b2b702008-06-02 17:21:06 -0400189#define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
Ian Campbell551889a62008-02-09 23:24:09 +0100190
191 xorl %ebx,%ebx /* %ebx is kept at zero */
192
193 movl $pa(pg0), %edi
Yinghai Luf0d43102008-05-29 12:56:36 -0700194 movl %edi, pa(init_pg_tables_start)
Ian Campbell551889a62008-02-09 23:24:09 +0100195 movl $pa(swapper_pg_pmd), %edx
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700196 movl $PTE_IDENT_ATTR, %eax
Linus Torvalds1da177e2005-04-16 15:20:36 -070019710:
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700198 leal PDE_IDENT_ATTR(%edi),%ecx /* Create PMD entry */
Ian Campbell551889a62008-02-09 23:24:09 +0100199 movl %ecx,(%edx) /* Store PMD entry */
200 /* Upper half already zero */
201 addl $8,%edx
202 movl $512,%ecx
20311:
204 stosl
205 xchgl %eax,%ebx
206 stosl
207 xchgl %eax,%ebx
208 addl $0x1000,%eax
209 loop 11b
210
211 /*
212 * End condition: we must map up to and including INIT_MAP_BEYOND_END
213 * bytes beyond the end of our own page tables.
214 */
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700215 leal (INIT_MAP_BEYOND_END+PTE_IDENT_ATTR)(%edi),%ebp
Ian Campbell551889a62008-02-09 23:24:09 +0100216 cmpl %ebp,%eax
217 jb 10b
2181:
219 movl %edi,pa(init_pg_tables_end)
Yinghai Lu6af61a72008-06-01 23:53:50 -0700220 shrl $12, %eax
221 movl %eax, pa(max_pfn_mapped)
Ian Campbell551889a62008-02-09 23:24:09 +0100222
223 /* Do early initialization of the fixmap area */
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700224 movl $pa(swapper_pg_fixmap)+PDE_IDENT_ATTR,%eax
Ian Campbell551889a62008-02-09 23:24:09 +0100225 movl %eax,pa(swapper_pg_pmd+0x1000*KPMDS-8)
226#else /* Not PAE */
227
228page_pde_offset = (__PAGE_OFFSET >> 20);
229
230 movl $pa(pg0), %edi
Yinghai Luf0d43102008-05-29 12:56:36 -0700231 movl %edi, pa(init_pg_tables_start)
Ian Campbell551889a62008-02-09 23:24:09 +0100232 movl $pa(swapper_pg_dir), %edx
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700233 movl $PTE_IDENT_ATTR, %eax
Ian Campbell551889a62008-02-09 23:24:09 +010023410:
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700235 leal PDE_IDENT_ATTR(%edi),%ecx /* Create PDE entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 movl %ecx,(%edx) /* Store identity PDE entry */
237 movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
238 addl $4,%edx
239 movl $1024, %ecx
24011:
241 stosl
242 addl $0x1000,%eax
243 loop 11b
Ian Campbell551889a62008-02-09 23:24:09 +0100244 /*
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
247 * the attribute bits
248 */
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700249 leal (INIT_MAP_BEYOND_END+PTE_IDENT_ATTR)(%edi),%ebp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 cmpl %ebp,%eax
251 jb 10b
Ian Campbell551889a62008-02-09 23:24:09 +0100252 movl %edi,pa(init_pg_tables_end)
Yinghai Lu6af61a72008-06-01 23:53:50 -0700253 shrl $12, %eax
254 movl %eax, pa(max_pfn_mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Ian Campbell551889a62008-02-09 23:24:09 +0100256 /* Do early initialization of the fixmap area */
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700257 movl $pa(swapper_pg_fixmap)+PDE_IDENT_ATTR,%eax
Ian Campbell551889a62008-02-09 23:24:09 +0100258 movl %eax,pa(swapper_pg_dir+0xffc)
259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 jmp 3f
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*
262 * Non-boot CPU entry point; entered from trampoline.S
263 * We can't lgdt here, because lgdt itself uses a data segment, but
Sebastien Dugue52de74d2007-05-02 19:27:10 +0200264 * we know the trampoline has already loaded the boot_gdt for us.
Vivek Goyalf8657e12007-02-13 13:26:22 +0100265 *
266 * If cpu hotplug is not supported then this code can go in init section
267 * which will be freed later
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
Vivek Goyalf8657e12007-02-13 13:26:22 +0100269
Andi Kleen5fe44862007-08-10 22:31:10 +0200270#ifndef CONFIG_HOTPLUG_CPU
Vivek Goyalf8657e12007-02-13 13:26:22 +0100271.section .init.text,"ax",@progbits
272#endif
273
274#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275ENTRY(startup_32_smp)
276 cld
277 movl $(__BOOT_DS),%eax
278 movl %eax,%ds
279 movl %eax,%es
280 movl %eax,%fs
281 movl %eax,%gs
Ian Campbell5756dd52008-01-30 13:33:27 +0100282#endif /* CONFIG_SMP */
2833:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285/*
286 * New page tables may be in 4Mbyte page mode and may
287 * be using the global pages.
288 *
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
295 *
296 * NOTE! We have to correct for the fact that we're
297 * not yet offset PAGE_OFFSET..
298 */
Ian Campbell551889a62008-02-09 23:24:09 +0100299#define cr4_bits pa(mmu_cr4_features)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 movl cr4_bits,%edx
301 andl %edx,%edx
302 jz 6f
303 movl %cr4,%eax # Turn on paging options (PSE,PAE,..)
304 orl %edx,%eax
305 movl %eax,%cr4
306
307 btl $5, %eax # check if PAE is enabled
308 jnc 6f
309
310 /* Check if extended functions are implemented */
311 movl $0x80000000, %eax
312 cpuid
313 cmpl $0x80000000, %eax
314 jbe 6f
315 mov $0x80000001, %eax
316 cpuid
317 /* Execute Disable bit supported? */
318 btl $20, %edx
319 jnc 6f
320
321 /* Setup EFER (Extended Feature Enable Register) */
322 movl $0xc0000080, %ecx
323 rdmsr
324
325 btsl $11, %eax
326 /* Make changes effective */
327 wrmsr
328
3296:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331/*
332 * Enable paging
333 */
Ian Campbell551889a62008-02-09 23:24:09 +0100334 movl $pa(swapper_pg_dir),%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 movl %eax,%cr3 /* set the page table pointer.. */
336 movl %cr0,%eax
Ian Campbell551889a62008-02-09 23:24:09 +0100337 orl $X86_CR0_PG,%eax
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 movl %eax,%cr0 /* ..and set paging (PG) bit */
339 ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
3401:
341 /* Set up the stack pointer */
342 lss stack_start,%esp
343
344/*
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.
348 */
349 pushl $0
350 popfl
351
352#ifdef CONFIG_SMP
Ian Campbell50359502008-01-30 13:33:27 +0100353 cmpb $0, ready
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 jz 1f /* Initial CPU cleans BSS */
355 jmp checkCPUtype
3561:
357#endif /* CONFIG_SMP */
358
359/*
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.
362 */
363 call setup_idt
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365checkCPUtype:
366
367 movl $-1,X86_CPUID # -1 for no CPUID initially
368
369/* check if it is 486 or 386. */
370/*
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.
374 */
375
376 movb $3,X86 # at least 386
377 pushfl # push EFLAGS
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
382 popfl # set 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
387 popfl
388 testl $0x40000,%eax # check if AC bit changed
389 je is386
390
391 movb $4,X86 # at least 486
392 testl $0x200000,%eax # check if ID bit changed
393 je is486
394
395 /* get vendor info */
396 xorl %eax,%eax # call CPUID with 0 -> return vendor ID
397 cpuid
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
402
403 orl %eax,%eax # do we have processor info as well?
404 je is486
405
406 movl $1,%eax # Use the CPUID instruction to get CPU type
407 cpuid
408 movb %al,%cl # save reg for future use
409 andb $0x0f,%ah # mask processor family
410 movb %ah,X86
411 andb $0xf0,%al # mask model
412 shrb $4,%al
413 movb %al,X86_MODEL
414 andb $0x0f,%cl # mask mask revision
415 movb %cl,X86_MASK
416 movl %edx,X86_CAPABILITY
417
418is486: movl $0x50022,%ecx # set AM, WP, NE and MP
419 jmp 2f
420
421is386: movl $2,%ecx # set MP
4222: movl %cr0,%eax
423 andl $0x80000011,%eax # Save PG,PE,ET
424 orl %ecx,%eax
425 movl %eax,%cr0
426
427 call check_x87
Rusty Russell2a57ff12007-02-13 13:26:26 +0100428 lgdt early_gdt_descr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 lidt idt_descr
430 ljmp $(__KERNEL_CS),$1f
4311: movl $(__KERNEL_DS),%eax # reload all the segment registers
432 movl %eax,%ss # after changing gdt.
433
434 movl $(__USER_DS),%eax # DS/ES contains default USER segment
435 movl %eax,%ds
436 movl %eax,%es
437
Brian Gerst0dd76d72009-01-21 17:26:05 +0900438 movl $(__KERNEL_PERCPU), %eax
439 movl %eax,%fs # set this cpu's percpu
440
Tejun Heo60a53172009-02-09 22:17:40 +0900441#ifdef CONFIG_CC_STACKPROTECTOR
442 /*
443 * The linker can't handle this by relocation. Manually set
444 * base address in stack canary segment descriptor.
445 */
446 cmpb $0,ready
447 jne 1f
448 movl $per_cpu__gdt_page,%eax
449 movl $per_cpu__stack_canary,%ecx
Tejun Heo5c79d2a2009-02-11 16:31:00 +0900450 subl $20, %ecx
Tejun Heo60a53172009-02-09 22:17:40 +0900451 movw %cx, 8 * GDT_ENTRY_STACK_CANARY + 2(%eax)
452 shrl $16, %ecx
453 movb %cl, 8 * GDT_ENTRY_STACK_CANARY + 4(%eax)
454 movb %ch, 8 * GDT_ENTRY_STACK_CANARY + 7(%eax)
4551:
456#endif
457 movl $(__KERNEL_STACK_CANARY),%eax
Jeremy Fitzhardinge464d1a72007-02-13 13:26:20 +0100458 movl %eax,%gs
Tejun Heo60a53172009-02-09 22:17:40 +0900459
460 xorl %eax,%eax # Clear LDT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 lldt %ax
Jeremy Fitzhardingef95d47c2006-12-07 02:14:02 +0100462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 cld # gcc2 wants the direction flag cleared at all times
Jeremy Fitzhardinge26fd5e02006-10-21 18:37:02 +0200464 pushl $0 # fake return address for unwinder
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465#ifdef CONFIG_SMP
Shaohua Lid92de652005-06-25 14:54:49 -0700466 movb ready, %cl
467 movb $1, ready
Andi Kleen29fe5f32006-08-30 19:37:09 +0200468 cmpb $0,%cl # the first CPU calls start_kernel
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +0200469 je 1f
Glauber Costa3e970472008-05-28 13:01:54 -0300470 movl (stack_start), %esp
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +02004711:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#endif /* CONFIG_SMP */
Glauber Costae3f77ed2008-05-28 12:57:02 -0300473 jmp *(initial_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475/*
476 * We depend on ET to be correct. This checks for 287/387.
477 */
478check_x87:
479 movb $0,X86_HARD_MATH
480 clts
481 fninit
482 fstsw %ax
483 cmpb $0,%al
484 je 1f
485 movl %cr0,%eax /* no coprocessor: have to set bits */
486 xorl $4,%eax /* set EM */
487 movl %eax,%cr0
488 ret
489 ALIGN
4901: movb $1,X86_HARD_MATH
491 .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
492 ret
493
494/*
495 * setup_idt
496 *
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.
503 *
504 * Warning: %esi is live across this function.
505 */
506setup_idt:
507 lea ignore_int,%edx
508 movl $(__KERNEL_CS << 16),%eax
509 movw %dx,%ax /* selector = 0x0010 = cs */
510 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
511
512 lea idt_table,%edi
513 mov $256,%ecx
514rp_sidt:
515 movl %eax,(%edi)
516 movl %edx,4(%edi)
517 addl $8,%edi
518 dec %ecx
519 jne rp_sidt
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200520
521.macro set_early_handler handler,trapno
522 lea \handler,%edx
523 movl $(__KERNEL_CS << 16),%eax
524 movw %dx,%ax
525 movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
526 lea idt_table,%edi
527 movl %eax,8*\trapno(%edi)
528 movl %edx,8*\trapno+4(%edi)
529.endm
530
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
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 ret
537
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200538early_divide_err:
539 xor %edx,%edx
540 pushl $0 /* fake errcode */
541 jmp early_fault
542
543early_illegal_opcode:
544 movl $6,%edx
545 pushl $0 /* fake errcode */
546 jmp early_fault
547
548early_protection_fault:
549 movl $13,%edx
550 jmp early_fault
551
552early_page_fault:
553 movl $14,%edx
554 jmp early_fault
555
556early_fault:
557 cld
558#ifdef CONFIG_PRINTK
Ingo Molnar382f64a2007-10-17 18:04:41 +0200559 pusha
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200560 movl $(__KERNEL_DS),%eax
561 movl %eax,%ds
562 movl %eax,%es
563 cmpl $2,early_recursion_flag
564 je hlt_loop
565 incl early_recursion_flag
566 movl %cr2,%eax
567 pushl %eax
568 pushl %edx /* trapno */
569 pushl $fault_msg
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200570 call printk
571#endif
Ingo Molnar94878ef2008-01-30 13:33:09 +0100572 call dump_stack
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200573hlt_loop:
574 hlt
575 jmp hlt_loop
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/* This is the default interrupt "handler" :-) */
578 ALIGN
579ignore_int:
580 cld
Matt Mackalld59745c2005-05-01 08:59:02 -0700581#ifdef CONFIG_PRINTK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 pushl %eax
583 pushl %ecx
584 pushl %edx
585 pushl %es
586 pushl %ds
587 movl $(__KERNEL_DS),%eax
588 movl %eax,%ds
589 movl %eax,%es
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200590 cmpl $2,early_recursion_flag
591 je hlt_loop
592 incl early_recursion_flag
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 pushl 16(%esp)
594 pushl 24(%esp)
595 pushl 32(%esp)
596 pushl 40(%esp)
597 pushl $int_msg
598 call printk
Ingo Molnard5e397c2009-01-26 06:09:00 +0100599
600 call dump_stack
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 addl $(5*4),%esp
603 popl %ds
604 popl %es
605 popl %edx
606 popl %ecx
607 popl %eax
Matt Mackalld59745c2005-05-01 08:59:02 -0700608#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 iret
610
Thomas Gleixner583323b2008-07-27 21:43:11 +0200611.section .cpuinit.data,"wa"
612.align 4
613ENTRY(initial_code)
614 .long i386_start_kernel
615
Vivek Goyalf8657e12007-02-13 13:26:22 +0100616.section .text
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/*
618 * Real beginning of normal "text" segment
619 */
620ENTRY(stext)
621ENTRY(_stext)
622
623/*
624 * BSS section
625 */
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700626.section ".bss.page_aligned","wa"
627 .align PAGE_SIZE_asm
Ian Campbell551889a62008-02-09 23:24:09 +0100628#ifdef CONFIG_X86_PAE
Adrian Bunked2b7e22008-02-22 21:58:37 +0200629swapper_pg_pmd:
Ian Campbell551889a62008-02-09 23:24:09 +0100630 .fill 1024*KPMDS,4,0
631#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632ENTRY(swapper_pg_dir)
633 .fill 1024,4,0
Ian Campbell551889a62008-02-09 23:24:09 +0100634#endif
Adrian Bunkaa65af3f2008-02-13 23:29:55 +0200635swapper_pg_fixmap:
Eric W. Bidermanb1c931e2007-07-15 23:37:28 -0700636 .fill 1024,4,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637ENTRY(empty_zero_page)
638 .fill 4096,1,0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/*
640 * This starts the data section.
641 */
Ian Campbell551889a62008-02-09 23:24:09 +0100642#ifdef CONFIG_X86_PAE
643.section ".data.page_aligned","wa"
644 /* Page-aligned for the benefit of paravirt? */
645 .align PAGE_SIZE_asm
646ENTRY(swapper_pg_dir)
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700647 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */
Ian Campbell551889a62008-02-09 23:24:09 +0100648# if KPMDS == 3
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700649 .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
Ian Campbell551889a62008-02-09 23:24:09 +0100652# elif KPMDS == 2
653 .long 0,0
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700654 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR),0
655 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR+0x1000),0
Ian Campbell551889a62008-02-09 23:24:09 +0100656# elif KPMDS == 1
657 .long 0,0
658 .long 0,0
Suresh Siddhab2bc2732008-09-23 14:00:36 -0700659 .long pa(swapper_pg_pmd+PGD_IDENT_ATTR),0
Ian Campbell551889a62008-02-09 23:24:09 +0100660# else
661# error "Kernel PMDs should be 1, 2 or 3"
662# endif
663 .align PAGE_SIZE_asm /* needs to be page-sized too */
664#endif
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666.data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667ENTRY(stack_start)
668 .long init_thread_union+THREAD_SIZE
669 .long __BOOT_DS
670
671ready: .byte 0
672
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200673early_recursion_flag:
674 .long 0
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676int_msg:
Ingo Molnard5e397c2009-01-26 06:09:00 +0100677 .asciz "Unknown interrupt or fault at: %p %p %p\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200679fault_msg:
Vegard Nossum575ca732008-04-25 21:02:34 +0200680/* fault info: */
681 .ascii "BUG: Int %d: CR2 %p\n"
682/* pusha regs: */
683 .ascii " EDI %p ESI %p EBP %p ESP %p\n"
684 .ascii " EBX %p EDX %p ECX %p EAX %p\n"
685/* fault frame: */
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"
Chuck Ebbertec5c0922006-09-26 10:52:39 +0200690
Thomas Gleixner97027852007-10-11 11:16:51 +0200691#include "../../x86/xen/xen-head.S"
Jeremy Fitzhardinge5ead97c2007-07-17 18:37:04 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/*
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:
698 */
699
700.globl boot_gdt_descr
701.globl idt_descr
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 ALIGN
704# early boot GDT descriptor (must use 1:1 address mapping)
705 .word 0 # 32 bit align gdt_desc.address
706boot_gdt_descr:
707 .word __BOOT_DS+7
Sebastien Dugue52de74d2007-05-02 19:27:10 +0200708 .long boot_gdt - __PAGE_OFFSET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 .word 0 # 32-bit align idt_desc.address
711idt_descr:
712 .word IDT_ENTRIES*8-1 # idt contains 256 entries
713 .long idt_table
714
715# boot GDT descriptor (later on used by CPU#0):
716 .word 0 # 32 bit align gdt_desc.address
Rusty Russell2a57ff12007-02-13 13:26:26 +0100717ENTRY(early_gdt_descr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .word GDT_ENTRIES*8-1
Jeremy Fitzhardinge7a61d352007-05-02 19:27:15 +0200719 .long per_cpu__gdt_page /* Overwritten for secondary CPUs */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
Sebastien Dugue52de74d2007-05-02 19:27:10 +0200722 * The boot_gdt must mirror the equivalent in setup.S and is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 * used only for booting.
724 */
725 .align L1_CACHE_BYTES
Sebastien Dugue52de74d2007-05-02 19:27:10 +0200726ENTRY(boot_gdt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 .fill GDT_ENTRY_BOOT_CS,8,0
728 .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
729 .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */