head_32.S 15 KB

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