head.S 13 KB

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