head_32.S 15 KB

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