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