head.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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/config.h>
  11. #include <linux/threads.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. We need one bit for
  35. * each possible page, but only in low memory, which means
  36. * 2^32/4096/8 = 128K worst case (4G/4G split.)
  37. *
  38. * Modulo rounding, each megabyte assigned here requires a kilobyte of
  39. * memory, which is currently unreclaimed.
  40. *
  41. * This should be a multiple of a page.
  42. */
  43. #define INIT_MAP_BEYOND_END (128*1024)
  44. /*
  45. * 32-bit kernel entrypoint; only used by the boot CPU. On entry,
  46. * %esi points to the real-mode code as a 32-bit pointer.
  47. * CS and DS must be 4 GB flat segments, but we don't depend on
  48. * any particular GDT layout, because we load our own as soon as we
  49. * can.
  50. */
  51. ENTRY(startup_32)
  52. /*
  53. * Set segments to known values.
  54. */
  55. cld
  56. lgdt boot_gdt_descr - __PAGE_OFFSET
  57. movl $(__BOOT_DS),%eax
  58. movl %eax,%ds
  59. movl %eax,%es
  60. movl %eax,%fs
  61. movl %eax,%gs
  62. /*
  63. * Clear BSS first so that there are no surprises...
  64. * No need to cld as DF is already clear from cld above...
  65. */
  66. xorl %eax,%eax
  67. movl $__bss_start - __PAGE_OFFSET,%edi
  68. movl $__bss_stop - __PAGE_OFFSET,%ecx
  69. subl %edi,%ecx
  70. shrl $2,%ecx
  71. rep ; stosl
  72. /*
  73. * Initialize page tables. This creates a PDE and a set of page
  74. * tables, which are located immediately beyond _end. The variable
  75. * init_pg_tables_end is set up to point to the first "safe" location.
  76. * Mappings are created both at virtual address 0 (identity mapping)
  77. * and PAGE_OFFSET for up to _end+sizeof(page tables)+INIT_MAP_BEYOND_END.
  78. *
  79. * Warning: don't use %esi or the stack in this code. However, %esp
  80. * can be used as a GPR if you really need it...
  81. */
  82. page_pde_offset = (__PAGE_OFFSET >> 20);
  83. movl $(pg0 - __PAGE_OFFSET), %edi
  84. movl $(swapper_pg_dir - __PAGE_OFFSET), %edx
  85. movl $0x007, %eax /* 0x007 = PRESENT+RW+USER */
  86. 10:
  87. leal 0x007(%edi),%ecx /* Create PDE entry */
  88. movl %ecx,(%edx) /* Store identity PDE entry */
  89. movl %ecx,page_pde_offset(%edx) /* Store kernel PDE entry */
  90. addl $4,%edx
  91. movl $1024, %ecx
  92. 11:
  93. stosl
  94. addl $0x1000,%eax
  95. loop 11b
  96. /* End condition: we must map up to and including INIT_MAP_BEYOND_END */
  97. /* bytes beyond the end of our own page tables; the +0x007 is the attribute bits */
  98. leal (INIT_MAP_BEYOND_END+0x007)(%edi),%ebp
  99. cmpl %ebp,%eax
  100. jb 10b
  101. movl %edi,(init_pg_tables_end - __PAGE_OFFSET)
  102. #ifdef CONFIG_SMP
  103. xorl %ebx,%ebx /* This is the boot CPU (BSP) */
  104. jmp 3f
  105. /*
  106. * Non-boot CPU entry point; entered from trampoline.S
  107. * We can't lgdt here, because lgdt itself uses a data segment, but
  108. * we know the trampoline has already loaded the boot_gdt_table GDT
  109. * for us.
  110. */
  111. ENTRY(startup_32_smp)
  112. cld
  113. movl $(__BOOT_DS),%eax
  114. movl %eax,%ds
  115. movl %eax,%es
  116. movl %eax,%fs
  117. movl %eax,%gs
  118. /*
  119. * New page tables may be in 4Mbyte page mode and may
  120. * be using the global pages.
  121. *
  122. * NOTE! If we are on a 486 we may have no cr4 at all!
  123. * So we do not try to touch it unless we really have
  124. * some bits in it to set. This won't work if the BSP
  125. * implements cr4 but this AP does not -- very unlikely
  126. * but be warned! The same applies to the pse feature
  127. * if not equally supported. --macro
  128. *
  129. * NOTE! We have to correct for the fact that we're
  130. * not yet offset PAGE_OFFSET..
  131. */
  132. #define cr4_bits mmu_cr4_features-__PAGE_OFFSET
  133. movl cr4_bits,%edx
  134. andl %edx,%edx
  135. jz 6f
  136. movl %cr4,%eax # Turn on paging options (PSE,PAE,..)
  137. orl %edx,%eax
  138. movl %eax,%cr4
  139. btl $5, %eax # check if PAE is enabled
  140. jnc 6f
  141. /* Check if extended functions are implemented */
  142. movl $0x80000000, %eax
  143. cpuid
  144. cmpl $0x80000000, %eax
  145. jbe 6f
  146. mov $0x80000001, %eax
  147. cpuid
  148. /* Execute Disable bit supported? */
  149. btl $20, %edx
  150. jnc 6f
  151. /* Setup EFER (Extended Feature Enable Register) */
  152. movl $0xc0000080, %ecx
  153. rdmsr
  154. btsl $11, %eax
  155. /* Make changes effective */
  156. wrmsr
  157. 6:
  158. /* This is a secondary processor (AP) */
  159. xorl %ebx,%ebx
  160. incl %ebx
  161. 3:
  162. #endif /* CONFIG_SMP */
  163. /*
  164. * Enable paging
  165. */
  166. movl $swapper_pg_dir-__PAGE_OFFSET,%eax
  167. movl %eax,%cr3 /* set the page table pointer.. */
  168. movl %cr0,%eax
  169. orl $0x80000000,%eax
  170. movl %eax,%cr0 /* ..and set paging (PG) bit */
  171. ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
  172. 1:
  173. /* Set up the stack pointer */
  174. lss stack_start,%esp
  175. /*
  176. * Initialize eflags. Some BIOS's leave bits like NT set. This would
  177. * confuse the debugger if this code is traced.
  178. * XXX - best to initialize before switching to protected mode.
  179. */
  180. pushl $0
  181. popfl
  182. #ifdef CONFIG_SMP
  183. andl %ebx,%ebx
  184. jz 1f /* Initial CPU cleans BSS */
  185. jmp checkCPUtype
  186. 1:
  187. #endif /* CONFIG_SMP */
  188. /*
  189. * start system 32-bit setup. We need to re-do some of the things done
  190. * in 16-bit mode for the "real" operations.
  191. */
  192. call setup_idt
  193. /*
  194. * Copy bootup parameters out of the way.
  195. * Note: %esi still has the pointer to the real-mode data.
  196. */
  197. movl $boot_params,%edi
  198. movl $(PARAM_SIZE/4),%ecx
  199. cld
  200. rep
  201. movsl
  202. movl boot_params+NEW_CL_POINTER,%esi
  203. andl %esi,%esi
  204. jnz 2f # New command line protocol
  205. cmpw $(OLD_CL_MAGIC),OLD_CL_MAGIC_ADDR
  206. jne 1f
  207. movzwl OLD_CL_OFFSET,%esi
  208. addl $(OLD_CL_BASE_ADDR),%esi
  209. 2:
  210. movl $saved_command_line,%edi
  211. movl $(COMMAND_LINE_SIZE/4),%ecx
  212. rep
  213. movsl
  214. 1:
  215. checkCPUtype:
  216. movl $-1,X86_CPUID # -1 for no CPUID initially
  217. /* check if it is 486 or 386. */
  218. /*
  219. * XXX - this does a lot of unnecessary setup. Alignment checks don't
  220. * apply at our cpl of 0 and the stack ought to be aligned already, and
  221. * we don't need to preserve eflags.
  222. */
  223. movb $3,X86 # at least 386
  224. pushfl # push EFLAGS
  225. popl %eax # get EFLAGS
  226. movl %eax,%ecx # save original EFLAGS
  227. xorl $0x240000,%eax # flip AC and ID bits in EFLAGS
  228. pushl %eax # copy to EFLAGS
  229. popfl # set EFLAGS
  230. pushfl # get new EFLAGS
  231. popl %eax # put it in eax
  232. xorl %ecx,%eax # change in flags
  233. pushl %ecx # restore original EFLAGS
  234. popfl
  235. testl $0x40000,%eax # check if AC bit changed
  236. je is386
  237. movb $4,X86 # at least 486
  238. testl $0x200000,%eax # check if ID bit changed
  239. je is486
  240. /* get vendor info */
  241. xorl %eax,%eax # call CPUID with 0 -> return vendor ID
  242. cpuid
  243. movl %eax,X86_CPUID # save CPUID level
  244. movl %ebx,X86_VENDOR_ID # lo 4 chars
  245. movl %edx,X86_VENDOR_ID+4 # next 4 chars
  246. movl %ecx,X86_VENDOR_ID+8 # last 4 chars
  247. orl %eax,%eax # do we have processor info as well?
  248. je is486
  249. movl $1,%eax # Use the CPUID instruction to get CPU type
  250. cpuid
  251. movb %al,%cl # save reg for future use
  252. andb $0x0f,%ah # mask processor family
  253. movb %ah,X86
  254. andb $0xf0,%al # mask model
  255. shrb $4,%al
  256. movb %al,X86_MODEL
  257. andb $0x0f,%cl # mask mask revision
  258. movb %cl,X86_MASK
  259. movl %edx,X86_CAPABILITY
  260. is486: movl $0x50022,%ecx # set AM, WP, NE and MP
  261. jmp 2f
  262. is386: movl $2,%ecx # set MP
  263. 2: movl %cr0,%eax
  264. andl $0x80000011,%eax # Save PG,PE,ET
  265. orl %ecx,%eax
  266. movl %eax,%cr0
  267. call check_x87
  268. lgdt cpu_gdt_descr
  269. lidt idt_descr
  270. ljmp $(__KERNEL_CS),$1f
  271. 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
  272. movl %eax,%ss # after changing gdt.
  273. movl $(__USER_DS),%eax # DS/ES contains default USER segment
  274. movl %eax,%ds
  275. movl %eax,%es
  276. xorl %eax,%eax # Clear FS/GS and LDT
  277. movl %eax,%fs
  278. movl %eax,%gs
  279. lldt %ax
  280. cld # gcc2 wants the direction flag cleared at all times
  281. #ifdef CONFIG_SMP
  282. movb ready, %cl
  283. movb $1, ready
  284. cmpb $0,%cl
  285. je 1f # the first CPU calls start_kernel
  286. # all other CPUs call initialize_secondary
  287. call initialize_secondary
  288. jmp L6
  289. 1:
  290. #endif /* CONFIG_SMP */
  291. call start_kernel
  292. L6:
  293. jmp L6 # main should never return here, but
  294. # just in case, we know what happens.
  295. /*
  296. * We depend on ET to be correct. This checks for 287/387.
  297. */
  298. check_x87:
  299. movb $0,X86_HARD_MATH
  300. clts
  301. fninit
  302. fstsw %ax
  303. cmpb $0,%al
  304. je 1f
  305. movl %cr0,%eax /* no coprocessor: have to set bits */
  306. xorl $4,%eax /* set EM */
  307. movl %eax,%cr0
  308. ret
  309. ALIGN
  310. 1: movb $1,X86_HARD_MATH
  311. .byte 0xDB,0xE4 /* fsetpm for 287, ignored by 387 */
  312. ret
  313. /*
  314. * setup_idt
  315. *
  316. * sets up a idt with 256 entries pointing to
  317. * ignore_int, interrupt gates. It doesn't actually load
  318. * idt - that can be done only after paging has been enabled
  319. * and the kernel moved to PAGE_OFFSET. Interrupts
  320. * are enabled elsewhere, when we can be relatively
  321. * sure everything is ok.
  322. *
  323. * Warning: %esi is live across this function.
  324. */
  325. setup_idt:
  326. lea ignore_int,%edx
  327. movl $(__KERNEL_CS << 16),%eax
  328. movw %dx,%ax /* selector = 0x0010 = cs */
  329. movw $0x8E00,%dx /* interrupt gate - dpl=0, present */
  330. lea idt_table,%edi
  331. mov $256,%ecx
  332. rp_sidt:
  333. movl %eax,(%edi)
  334. movl %edx,4(%edi)
  335. addl $8,%edi
  336. dec %ecx
  337. jne rp_sidt
  338. ret
  339. /* This is the default interrupt "handler" :-) */
  340. ALIGN
  341. ignore_int:
  342. cld
  343. #ifdef CONFIG_PRINTK
  344. pushl %eax
  345. pushl %ecx
  346. pushl %edx
  347. pushl %es
  348. pushl %ds
  349. movl $(__KERNEL_DS),%eax
  350. movl %eax,%ds
  351. movl %eax,%es
  352. pushl 16(%esp)
  353. pushl 24(%esp)
  354. pushl 32(%esp)
  355. pushl 40(%esp)
  356. pushl $int_msg
  357. call printk
  358. addl $(5*4),%esp
  359. popl %ds
  360. popl %es
  361. popl %edx
  362. popl %ecx
  363. popl %eax
  364. #endif
  365. iret
  366. /*
  367. * Real beginning of normal "text" segment
  368. */
  369. ENTRY(stext)
  370. ENTRY(_stext)
  371. /*
  372. * BSS section
  373. */
  374. .section ".bss.page_aligned","w"
  375. ENTRY(swapper_pg_dir)
  376. .fill 1024,4,0
  377. ENTRY(empty_zero_page)
  378. .fill 4096,1,0
  379. /*
  380. * This starts the data section.
  381. */
  382. .data
  383. ENTRY(stack_start)
  384. .long init_thread_union+THREAD_SIZE
  385. .long __BOOT_DS
  386. ready: .byte 0
  387. int_msg:
  388. .asciz "Unknown interrupt or fault at EIP %p %p %p\n"
  389. /*
  390. * The IDT and GDT 'descriptors' are a strange 48-bit object
  391. * only used by the lidt and lgdt instructions. They are not
  392. * like usual segment descriptors - they consist of a 16-bit
  393. * segment size, and 32-bit linear address value:
  394. */
  395. .globl boot_gdt_descr
  396. .globl idt_descr
  397. .globl cpu_gdt_descr
  398. ALIGN
  399. # early boot GDT descriptor (must use 1:1 address mapping)
  400. .word 0 # 32 bit align gdt_desc.address
  401. boot_gdt_descr:
  402. .word __BOOT_DS+7
  403. .long boot_gdt_table - __PAGE_OFFSET
  404. .word 0 # 32-bit align idt_desc.address
  405. idt_descr:
  406. .word IDT_ENTRIES*8-1 # idt contains 256 entries
  407. .long idt_table
  408. # boot GDT descriptor (later on used by CPU#0):
  409. .word 0 # 32 bit align gdt_desc.address
  410. cpu_gdt_descr:
  411. .word GDT_ENTRIES*8-1
  412. .long cpu_gdt_table
  413. .fill NR_CPUS-1,8,0 # space for the other GDT descriptors
  414. /*
  415. * The boot_gdt_table must mirror the equivalent in setup.S and is
  416. * used only for booting.
  417. */
  418. .align L1_CACHE_BYTES
  419. ENTRY(boot_gdt_table)
  420. .fill GDT_ENTRY_BOOT_CS,8,0
  421. .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
  422. .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */
  423. /*
  424. * The Global Descriptor Table contains 28 quadwords, per-CPU.
  425. */
  426. .align PAGE_SIZE_asm
  427. ENTRY(cpu_gdt_table)
  428. .quad 0x0000000000000000 /* NULL descriptor */
  429. .quad 0x0000000000000000 /* 0x0b reserved */
  430. .quad 0x0000000000000000 /* 0x13 reserved */
  431. .quad 0x0000000000000000 /* 0x1b reserved */
  432. .quad 0x0000000000000000 /* 0x20 unused */
  433. .quad 0x0000000000000000 /* 0x28 unused */
  434. .quad 0x0000000000000000 /* 0x33 TLS entry 1 */
  435. .quad 0x0000000000000000 /* 0x3b TLS entry 2 */
  436. .quad 0x0000000000000000 /* 0x43 TLS entry 3 */
  437. .quad 0x0000000000000000 /* 0x4b reserved */
  438. .quad 0x0000000000000000 /* 0x53 reserved */
  439. .quad 0x0000000000000000 /* 0x5b reserved */
  440. .quad 0x00cf9a000000ffff /* 0x60 kernel 4GB code at 0x00000000 */
  441. .quad 0x00cf92000000ffff /* 0x68 kernel 4GB data at 0x00000000 */
  442. .quad 0x00cffa000000ffff /* 0x73 user 4GB code at 0x00000000 */
  443. .quad 0x00cff2000000ffff /* 0x7b user 4GB data at 0x00000000 */
  444. .quad 0x0000000000000000 /* 0x80 TSS descriptor */
  445. .quad 0x0000000000000000 /* 0x88 LDT descriptor */
  446. /* Segments used for calling PnP BIOS */
  447. .quad 0x00c09a0000000000 /* 0x90 32-bit code */
  448. .quad 0x00809a0000000000 /* 0x98 16-bit code */
  449. .quad 0x0080920000000000 /* 0xa0 16-bit data */
  450. .quad 0x0080920000000000 /* 0xa8 16-bit data */
  451. .quad 0x0080920000000000 /* 0xb0 16-bit data */
  452. /*
  453. * The APM segments have byte granularity and their bases
  454. * and limits are set at run time.
  455. */
  456. .quad 0x00409a0000000000 /* 0xb8 APM CS code */
  457. .quad 0x00009a0000000000 /* 0xc0 APM CS 16 code (16 bit) */
  458. .quad 0x0040920000000000 /* 0xc8 APM DS data */
  459. .quad 0x0000920000000000 /* 0xd0 - ESPFIX 16-bit SS */
  460. .quad 0x0000000000000000 /* 0xd8 - unused */
  461. .quad 0x0000000000000000 /* 0xe0 - unused */
  462. .quad 0x0000000000000000 /* 0xe8 - unused */
  463. .quad 0x0000000000000000 /* 0xf0 - unused */
  464. .quad 0x0000000000000000 /* 0xf8 - GDT entry 31: double-fault TSS */