head_32.S 14 KB

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