Merge branch 'bug-fixes' of git://farnsworth.org/dale/linux-2.6-mv643xx_eth into...
[sfrench/cifs-2.6.git] / arch / x86 / kernel / head_32.S
1 /*
2  *  linux/arch/i386/kernel/head.S -- the 32-bit startup code.
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *
6  *  Enhanced CPU detection and feature setting code by Mike Jagdis
7  *  and Martin Mares, November 1997.
8  */
9
10 .text
11 #include <linux/threads.h>
12 #include <linux/linkage.h>
13 #include <asm/segment.h>
14 #include <asm/page.h>
15 #include <asm/pgtable.h>
16 #include <asm/desc.h>
17 #include <asm/cache.h>
18 #include <asm/thread_info.h>
19 #include <asm/asm-offsets.h>
20 #include <asm/setup.h>
21
22 /*
23  * References to members of the new_cpu_data structure.
24  */
25
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
34
35 /*
36  * This is how much memory *in addition to the memory covered up to
37  * and including _end* we need mapped initially.
38  * We need:
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
45  *     been set up
46  *
47  * Modulo rounding, each megabyte assigned here requires a kilobyte of
48  * memory, which is currently unreclaimed.
49  *
50  * This should be a multiple of a page.
51  */
52 LOW_PAGES = 1<<(32-PAGE_SHIFT_asm)
53
54 /*
55  * To preserve the DMA pool in PAGEALLOC kernels, we'll allocate
56  * pagetables from above the 16MB DMA limit, so we'll have to set
57  * up pagetables 16MB more (worst-case):
58  */
59 #ifdef CONFIG_DEBUG_PAGEALLOC
60 LOW_PAGES = LOW_PAGES + 0x1000000
61 #endif
62
63 #if PTRS_PER_PMD > 1
64 PAGE_TABLE_SIZE = (LOW_PAGES / PTRS_PER_PMD) + PTRS_PER_PGD
65 #else
66 PAGE_TABLE_SIZE = (LOW_PAGES / PTRS_PER_PGD)
67 #endif
68 BOOTBITMAP_SIZE = LOW_PAGES / 8
69 ALLOCATOR_SLOP = 4
70
71 INIT_MAP_BEYOND_END = BOOTBITMAP_SIZE + (PAGE_TABLE_SIZE + ALLOCATOR_SLOP)*PAGE_SIZE_asm
72
73 /*
74  * 32-bit kernel entrypoint; only used by the boot CPU.  On entry,
75  * %esi points to the real-mode code as a 32-bit pointer.
76  * CS and DS must be 4 GB flat segments, but we don't depend on
77  * any particular GDT layout, because we load our own as soon as we
78  * can.
79  */
80 .section .text.head,"ax",@progbits
81 ENTRY(startup_32)
82         /* check to see if KEEP_SEGMENTS flag is meaningful */
83         cmpw $0x207, BP_version(%esi)
84         jb 1f
85
86         /* test KEEP_SEGMENTS flag to see if the bootloader is asking
87                 us to not reload segments */
88         testb $(1<<6), BP_loadflags(%esi)
89         jnz 2f
90
91 /*
92  * Set segments to known values.
93  */
94 1:      lgdt boot_gdt_descr - __PAGE_OFFSET
95         movl $(__BOOT_DS),%eax
96         movl %eax,%ds
97         movl %eax,%es
98         movl %eax,%fs
99         movl %eax,%gs
100 2:
101
102 /*
103  * Clear BSS first so that there are no surprises...
104  */
105         cld
106         xorl %eax,%eax
107         movl $__bss_start - __PAGE_OFFSET,%edi
108         movl $__bss_stop - __PAGE_OFFSET,%ecx
109         subl %edi,%ecx
110         shrl $2,%ecx
111         rep ; stosl
112 /*
113  * Copy bootup parameters out of the way.
114  * Note: %esi still has the pointer to the real-mode data.
115  * With the kexec as boot loader, parameter segment might be loaded beyond
116  * kernel image and might not even be addressable by early boot page tables.
117  * (kexec on panic case). Hence copy out the parameters before initializing
118  * page tables.
119  */
120         movl $(boot_params - __PAGE_OFFSET),%edi
121         movl $(PARAM_SIZE/4),%ecx
122         cld
123         rep
124         movsl
125         movl boot_params - __PAGE_OFFSET + NEW_CL_POINTER,%esi
126         andl %esi,%esi
127         jnz 2f                  # New command line protocol
128         cmpw $(OLD_CL_MAGIC),OLD_CL_MAGIC_ADDR
129         jne 1f
130         movzwl OLD_CL_OFFSET,%esi
131         addl $(OLD_CL_BASE_ADDR),%esi
132 2:
133         movl $(boot_command_line - __PAGE_OFFSET),%edi
134         movl $(COMMAND_LINE_SIZE/4),%ecx
135         rep
136         movsl
137 1:
138
139 #ifdef CONFIG_PARAVIRT
140         cmpw $0x207, (boot_params + BP_version - __PAGE_OFFSET)
141         jb default_entry
142
143         /* Paravirt-compatible boot parameters.  Look to see what architecture
144                 we're booting under. */
145         movl (boot_params + BP_hardware_subarch - __PAGE_OFFSET), %eax
146         cmpl $num_subarch_entries, %eax
147         jae bad_subarch
148
149         movl subarch_entries - __PAGE_OFFSET(,%eax,4), %eax
150         subl $__PAGE_OFFSET, %eax
151         jmp *%eax
152
153 bad_subarch:
154 WEAK(lguest_entry)
155 WEAK(xen_entry)
156         /* Unknown implementation; there's really
157            nothing we can do at this point. */
158         ud2a
159 .data
160 subarch_entries:
161         .long default_entry             /* normal x86/PC */
162         .long lguest_entry              /* lguest hypervisor */
163         .long xen_entry                 /* Xen hypervisor */
164 num_subarch_entries = (. - subarch_entries) / 4
165 .previous
166 #endif /* CONFIG_PARAVIRT */
167
168 /*
169  * Initialize page tables.  This creates a PDE and a set of page
170  * tables, which are located immediately beyond _end.  The variable
171  * init_pg_tables_end is set up to point to the first "safe" location.
172  * Mappings are created both at virtual address 0 (identity mapping)
173  * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
174  *
175  * Warning: don't use %esi or the stack in this code.  However, %esp
176  * can be used as a GPR if you really need it...
177  */
178 page_pde_offset = (__PAGE_OFFSET >> 20);
179
180 default_entry:
181         movl $(pg0 - __PAGE_OFFSET), %edi
182         movl $(swapper_pg_dir - __PAGE_OFFSET), %edx
183         movl $0x007, %eax                       /* 0x007 = PRESENT+RW+USER */
184 10:
185         leal 0x007(%edi),%ecx                   /* Create PDE entry */
186         movl %ecx,(%edx)                        /* Store identity PDE entry */
187         movl %ecx,page_pde_offset(%edx)         /* Store kernel PDE entry */
188         addl $4,%edx
189         movl $1024, %ecx
190 11:
191         stosl
192         addl $0x1000,%eax
193         loop 11b
194         /* End condition: we must map up to and including INIT_MAP_BEYOND_END */
195         /* bytes beyond the end of our own page tables; the +0x007 is the attribute bits */
196         leal (INIT_MAP_BEYOND_END+0x007)(%edi),%ebp
197         cmpl %ebp,%eax
198         jb 10b
199         movl %edi,(init_pg_tables_end - __PAGE_OFFSET)
200
201         xorl %ebx,%ebx                          /* This is the boot CPU (BSP) */
202         jmp 3f
203 /*
204  * Non-boot CPU entry point; entered from trampoline.S
205  * We can't lgdt here, because lgdt itself uses a data segment, but
206  * we know the trampoline has already loaded the boot_gdt for us.
207  *
208  * If cpu hotplug is not supported then this code can go in init section
209  * which will be freed later
210  */
211
212 #ifndef CONFIG_HOTPLUG_CPU
213 .section .init.text,"ax",@progbits
214 #endif
215
216         /* Do an early initialization of the fixmap area */
217         movl $(swapper_pg_dir - __PAGE_OFFSET), %edx
218         movl $(swapper_pg_pmd - __PAGE_OFFSET), %eax
219         addl $0x007, %eax                       /* 0x007 = PRESENT+RW+USER */
220         movl %eax, 4092(%edx)
221
222 #ifdef CONFIG_SMP
223 ENTRY(startup_32_smp)
224         cld
225         movl $(__BOOT_DS),%eax
226         movl %eax,%ds
227         movl %eax,%es
228         movl %eax,%fs
229         movl %eax,%gs
230
231 /*
232  *      New page tables may be in 4Mbyte page mode and may
233  *      be using the global pages. 
234  *
235  *      NOTE! If we are on a 486 we may have no cr4 at all!
236  *      So we do not try to touch it unless we really have
237  *      some bits in it to set.  This won't work if the BSP
238  *      implements cr4 but this AP does not -- very unlikely
239  *      but be warned!  The same applies to the pse feature
240  *      if not equally supported. --macro
241  *
242  *      NOTE! We have to correct for the fact that we're
243  *      not yet offset PAGE_OFFSET..
244  */
245 #define cr4_bits mmu_cr4_features-__PAGE_OFFSET
246         movl cr4_bits,%edx
247         andl %edx,%edx
248         jz 6f
249         movl %cr4,%eax          # Turn on paging options (PSE,PAE,..)
250         orl %edx,%eax
251         movl %eax,%cr4
252
253         btl $5, %eax            # check if PAE is enabled
254         jnc 6f
255
256         /* Check if extended functions are implemented */
257         movl $0x80000000, %eax
258         cpuid
259         cmpl $0x80000000, %eax
260         jbe 6f
261         mov $0x80000001, %eax
262         cpuid
263         /* Execute Disable bit supported? */
264         btl $20, %edx
265         jnc 6f
266
267         /* Setup EFER (Extended Feature Enable Register) */
268         movl $0xc0000080, %ecx
269         rdmsr
270
271         btsl $11, %eax
272         /* Make changes effective */
273         wrmsr
274
275 6:
276         /* This is a secondary processor (AP) */
277         xorl %ebx,%ebx
278         incl %ebx
279
280 #endif /* CONFIG_SMP */
281 3:
282
283 /*
284  * Enable paging
285  */
286         movl $swapper_pg_dir-__PAGE_OFFSET,%eax
287         movl %eax,%cr3          /* set the page table pointer.. */
288         movl %cr0,%eax
289         orl $0x80000000,%eax
290         movl %eax,%cr0          /* ..and set paging (PG) bit */
291         ljmp $__BOOT_CS,$1f     /* Clear prefetch and normalize %eip */
292 1:
293         /* Set up the stack pointer */
294         lss stack_start,%esp
295
296 /*
297  * Initialize eflags.  Some BIOS's leave bits like NT set.  This would
298  * confuse the debugger if this code is traced.
299  * XXX - best to initialize before switching to protected mode.
300  */
301         pushl $0
302         popfl
303
304 #ifdef CONFIG_SMP
305         andl %ebx,%ebx
306         jz  1f                          /* Initial CPU cleans BSS */
307         jmp checkCPUtype
308 1:
309 #endif /* CONFIG_SMP */
310
311 /*
312  * start system 32-bit setup. We need to re-do some of the things done
313  * in 16-bit mode for the "real" operations.
314  */
315         call setup_idt
316
317 checkCPUtype:
318
319         movl $-1,X86_CPUID              #  -1 for no CPUID initially
320
321 /* check if it is 486 or 386. */
322 /*
323  * XXX - this does a lot of unnecessary setup.  Alignment checks don't
324  * apply at our cpl of 0 and the stack ought to be aligned already, and
325  * we don't need to preserve eflags.
326  */
327
328         movb $3,X86             # at least 386
329         pushfl                  # push EFLAGS
330         popl %eax               # get EFLAGS
331         movl %eax,%ecx          # save original EFLAGS
332         xorl $0x240000,%eax     # flip AC and ID bits in EFLAGS
333         pushl %eax              # copy to EFLAGS
334         popfl                   # set EFLAGS
335         pushfl                  # get new EFLAGS
336         popl %eax               # put it in eax
337         xorl %ecx,%eax          # change in flags
338         pushl %ecx              # restore original EFLAGS
339         popfl
340         testl $0x40000,%eax     # check if AC bit changed
341         je is386
342
343         movb $4,X86             # at least 486
344         testl $0x200000,%eax    # check if ID bit changed
345         je is486
346
347         /* get vendor info */
348         xorl %eax,%eax                  # call CPUID with 0 -> return vendor ID
349         cpuid
350         movl %eax,X86_CPUID             # save CPUID level
351         movl %ebx,X86_VENDOR_ID         # lo 4 chars
352         movl %edx,X86_VENDOR_ID+4       # next 4 chars
353         movl %ecx,X86_VENDOR_ID+8       # last 4 chars
354
355         orl %eax,%eax                   # do we have processor info as well?
356         je is486
357
358         movl $1,%eax            # Use the CPUID instruction to get CPU type
359         cpuid
360         movb %al,%cl            # save reg for future use
361         andb $0x0f,%ah          # mask processor family
362         movb %ah,X86
363         andb $0xf0,%al          # mask model
364         shrb $4,%al
365         movb %al,X86_MODEL
366         andb $0x0f,%cl          # mask mask revision
367         movb %cl,X86_MASK
368         movl %edx,X86_CAPABILITY
369
370 is486:  movl $0x50022,%ecx      # set AM, WP, NE and MP
371         jmp 2f
372
373 is386:  movl $2,%ecx            # set MP
374 2:      movl %cr0,%eax
375         andl $0x80000011,%eax   # Save PG,PE,ET
376         orl %ecx,%eax
377         movl %eax,%cr0
378
379         call check_x87
380         lgdt early_gdt_descr
381         lidt idt_descr
382         ljmp $(__KERNEL_CS),$1f
383 1:      movl $(__KERNEL_DS),%eax        # reload all the segment registers
384         movl %eax,%ss                   # after changing gdt.
385         movl %eax,%fs                   # gets reset once there's real percpu
386
387         movl $(__USER_DS),%eax          # DS/ES contains default USER segment
388         movl %eax,%ds
389         movl %eax,%es
390
391         xorl %eax,%eax                  # Clear GS and LDT
392         movl %eax,%gs
393         lldt %ax
394
395         cld                     # gcc2 wants the direction flag cleared at all times
396         pushl $0                # fake return address for unwinder
397 #ifdef CONFIG_SMP
398         movb ready, %cl
399         movb $1, ready
400         cmpb $0,%cl             # the first CPU calls start_kernel
401         je   1f
402         movl $(__KERNEL_PERCPU), %eax
403         movl %eax,%fs           # set this cpu's percpu
404         jmp initialize_secondary # all other CPUs call initialize_secondary
405 1:
406 #endif /* CONFIG_SMP */
407         jmp start_kernel
408
409 /*
410  * We depend on ET to be correct. This checks for 287/387.
411  */
412 check_x87:
413         movb $0,X86_HARD_MATH
414         clts
415         fninit
416         fstsw %ax
417         cmpb $0,%al
418         je 1f
419         movl %cr0,%eax          /* no coprocessor: have to set bits */
420         xorl $4,%eax            /* set EM */
421         movl %eax,%cr0
422         ret
423         ALIGN
424 1:      movb $1,X86_HARD_MATH
425         .byte 0xDB,0xE4         /* fsetpm for 287, ignored by 387 */
426         ret
427
428 /*
429  *  setup_idt
430  *
431  *  sets up a idt with 256 entries pointing to
432  *  ignore_int, interrupt gates. It doesn't actually load
433  *  idt - that can be done only after paging has been enabled
434  *  and the kernel moved to PAGE_OFFSET. Interrupts
435  *  are enabled elsewhere, when we can be relatively
436  *  sure everything is ok.
437  *
438  *  Warning: %esi is live across this function.
439  */
440 setup_idt:
441         lea ignore_int,%edx
442         movl $(__KERNEL_CS << 16),%eax
443         movw %dx,%ax            /* selector = 0x0010 = cs */
444         movw $0x8E00,%dx        /* interrupt gate - dpl=0, present */
445
446         lea idt_table,%edi
447         mov $256,%ecx
448 rp_sidt:
449         movl %eax,(%edi)
450         movl %edx,4(%edi)
451         addl $8,%edi
452         dec %ecx
453         jne rp_sidt
454
455 .macro  set_early_handler handler,trapno
456         lea \handler,%edx
457         movl $(__KERNEL_CS << 16),%eax
458         movw %dx,%ax
459         movw $0x8E00,%dx        /* interrupt gate - dpl=0, present */
460         lea idt_table,%edi
461         movl %eax,8*\trapno(%edi)
462         movl %edx,8*\trapno+4(%edi)
463 .endm
464
465         set_early_handler handler=early_divide_err,trapno=0
466         set_early_handler handler=early_illegal_opcode,trapno=6
467         set_early_handler handler=early_protection_fault,trapno=13
468         set_early_handler handler=early_page_fault,trapno=14
469
470         ret
471
472 early_divide_err:
473         xor %edx,%edx
474         pushl $0        /* fake errcode */
475         jmp early_fault
476
477 early_illegal_opcode:
478         movl $6,%edx
479         pushl $0        /* fake errcode */
480         jmp early_fault
481
482 early_protection_fault:
483         movl $13,%edx
484         jmp early_fault
485
486 early_page_fault:
487         movl $14,%edx
488         jmp early_fault
489
490 early_fault:
491         cld
492 #ifdef CONFIG_PRINTK
493         pusha
494         movl $(__KERNEL_DS),%eax
495         movl %eax,%ds
496         movl %eax,%es
497         cmpl $2,early_recursion_flag
498         je hlt_loop
499         incl early_recursion_flag
500         movl %cr2,%eax
501         pushl %eax
502         pushl %edx              /* trapno */
503         pushl $fault_msg
504 #ifdef CONFIG_EARLY_PRINTK
505         call early_printk
506 #else
507         call printk
508 #endif
509 #endif
510 hlt_loop:
511         hlt
512         jmp hlt_loop
513
514 /* This is the default interrupt "handler" :-) */
515         ALIGN
516 ignore_int:
517         cld
518 #ifdef CONFIG_PRINTK
519         pushl %eax
520         pushl %ecx
521         pushl %edx
522         pushl %es
523         pushl %ds
524         movl $(__KERNEL_DS),%eax
525         movl %eax,%ds
526         movl %eax,%es
527         cmpl $2,early_recursion_flag
528         je hlt_loop
529         incl early_recursion_flag
530         pushl 16(%esp)
531         pushl 24(%esp)
532         pushl 32(%esp)
533         pushl 40(%esp)
534         pushl $int_msg
535 #ifdef CONFIG_EARLY_PRINTK
536         call early_printk
537 #else
538         call printk
539 #endif
540         addl $(5*4),%esp
541         popl %ds
542         popl %es
543         popl %edx
544         popl %ecx
545         popl %eax
546 #endif
547         iret
548
549 .section .text
550 /*
551  * Real beginning of normal "text" segment
552  */
553 ENTRY(stext)
554 ENTRY(_stext)
555
556 /*
557  * BSS section
558  */
559 .section ".bss.page_aligned","wa"
560         .align PAGE_SIZE_asm
561 ENTRY(swapper_pg_dir)
562         .fill 1024,4,0
563 ENTRY(swapper_pg_pmd)
564         .fill 1024,4,0
565 ENTRY(empty_zero_page)
566         .fill 4096,1,0
567
568 /*
569  * This starts the data section.
570  */
571 .data
572 ENTRY(stack_start)
573         .long init_thread_union+THREAD_SIZE
574         .long __BOOT_DS
575
576 ready:  .byte 0
577
578 early_recursion_flag:
579         .long 0
580
581 int_msg:
582         .asciz "Unknown interrupt or fault at EIP %p %p %p\n"
583
584 fault_msg:
585         .ascii                                                          \
586 /* fault info: */       "BUG: Int %d: CR2 %p\n"                         \
587 /* pusha regs: */       "     EDI %p  ESI %p  EBP %p  ESP %p\n"         \
588                         "     EBX %p  EDX %p  ECX %p  EAX %p\n"         \
589 /* fault frame: */      "     err %p  EIP %p   CS %p  flg %p\n"         \
590                                                                         \
591                         "Stack: %p %p %p %p %p %p %p %p\n"              \
592                         "       %p %p %p %p %p %p %p %p\n"              \
593                         "       %p %p %p %p %p %p %p %p\n"
594
595 #include "../../x86/xen/xen-head.S"
596
597 /*
598  * The IDT and GDT 'descriptors' are a strange 48-bit object
599  * only used by the lidt and lgdt instructions. They are not
600  * like usual segment descriptors - they consist of a 16-bit
601  * segment size, and 32-bit linear address value:
602  */
603
604 .globl boot_gdt_descr
605 .globl idt_descr
606
607         ALIGN
608 # early boot GDT descriptor (must use 1:1 address mapping)
609         .word 0                         # 32 bit align gdt_desc.address
610 boot_gdt_descr:
611         .word __BOOT_DS+7
612         .long boot_gdt - __PAGE_OFFSET
613
614         .word 0                         # 32-bit align idt_desc.address
615 idt_descr:
616         .word IDT_ENTRIES*8-1           # idt contains 256 entries
617         .long idt_table
618
619 # boot GDT descriptor (later on used by CPU#0):
620         .word 0                         # 32 bit align gdt_desc.address
621 ENTRY(early_gdt_descr)
622         .word GDT_ENTRIES*8-1
623         .long per_cpu__gdt_page         /* Overwritten for secondary CPUs */
624
625 /*
626  * The boot_gdt must mirror the equivalent in setup.S and is
627  * used only for booting.
628  */
629         .align L1_CACHE_BYTES
630 ENTRY(boot_gdt)
631         .fill GDT_ENTRY_BOOT_CS,8,0
632         .quad 0x00cf9a000000ffff        /* kernel 4GB code at 0x00000000 */
633         .quad 0x00cf92000000ffff        /* kernel 4GB data at 0x00000000 */