head_32.S 14 KB

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