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