head.S 14 KB

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