head_32.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  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/init.h>
  12. #include <linux/linkage.h>
  13. #include <asm/segment.h>
  14. #include <asm/page_types.h>
  15. #include <asm/pgtable_types.h>
  16. #include <asm/cache.h>
  17. #include <asm/thread_info.h>
  18. #include <asm/asm-offsets.h>
  19. #include <asm/setup.h>
  20. #include <asm/processor-flags.h>
  21. #include <asm/msr-index.h>
  22. #include <asm/cpufeatures.h>
  23. #include <asm/percpu.h>
  24. #include <asm/nops.h>
  25. #include <asm/bootparam.h>
  26. #include <asm/export.h>
  27. #include <asm/pgtable_32.h>
  28. /* Physical address */
  29. #define pa(X) ((X) - __PAGE_OFFSET)
  30. /*
  31. * References to members of the new_cpu_data structure.
  32. */
  33. #define X86 new_cpu_data+CPUINFO_x86
  34. #define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor
  35. #define X86_MODEL new_cpu_data+CPUINFO_x86_model
  36. #define X86_STEPPING new_cpu_data+CPUINFO_x86_stepping
  37. #define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math
  38. #define X86_CPUID new_cpu_data+CPUINFO_cpuid_level
  39. #define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability
  40. #define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id
  41. #define SIZEOF_PTREGS 17*4
  42. /*
  43. * Worst-case size of the kernel mapping we need to make:
  44. * a relocatable kernel can live anywhere in lowmem, so we need to be able
  45. * to map all of lowmem.
  46. */
  47. KERNEL_PAGES = LOWMEM_PAGES
  48. INIT_MAP_SIZE = PAGE_TABLE_SIZE(KERNEL_PAGES) * PAGE_SIZE
  49. RESERVE_BRK(pagetables, INIT_MAP_SIZE)
  50. /*
  51. * 32-bit kernel entrypoint; only used by the boot CPU. On entry,
  52. * %esi points to the real-mode code as a 32-bit pointer.
  53. * CS and DS must be 4 GB flat segments, but we don't depend on
  54. * any particular GDT layout, because we load our own as soon as we
  55. * can.
  56. */
  57. __HEAD
  58. ENTRY(startup_32)
  59. movl pa(initial_stack),%ecx
  60. /* test KEEP_SEGMENTS flag to see if the bootloader is asking
  61. us to not reload segments */
  62. testb $KEEP_SEGMENTS, BP_loadflags(%esi)
  63. jnz 2f
  64. /*
  65. * Set segments to known values.
  66. */
  67. lgdt pa(boot_gdt_descr)
  68. movl $(__BOOT_DS),%eax
  69. movl %eax,%ds
  70. movl %eax,%es
  71. movl %eax,%fs
  72. movl %eax,%gs
  73. movl %eax,%ss
  74. 2:
  75. leal -__PAGE_OFFSET(%ecx),%esp
  76. /*
  77. * Clear BSS first so that there are no surprises...
  78. */
  79. cld
  80. xorl %eax,%eax
  81. movl $pa(__bss_start),%edi
  82. movl $pa(__bss_stop),%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 $pa(boot_params),%edi
  95. movl $(PARAM_SIZE/4),%ecx
  96. cld
  97. rep
  98. movsl
  99. movl pa(boot_params) + NEW_CL_POINTER,%esi
  100. andl %esi,%esi
  101. jz 1f # No command line
  102. movl $pa(boot_command_line),%edi
  103. movl $(COMMAND_LINE_SIZE/4),%ecx
  104. rep
  105. movsl
  106. 1:
  107. #ifdef CONFIG_OLPC
  108. /* save OFW's pgdir table for later use when calling into OFW */
  109. movl %cr3, %eax
  110. movl %eax, pa(olpc_ofw_pgd)
  111. #endif
  112. #ifdef CONFIG_MICROCODE
  113. /* Early load ucode on BSP. */
  114. call load_ucode_bsp
  115. #endif
  116. /* Create early pagetables. */
  117. call mk_early_pgtbl_32
  118. /* Do early initialization of the fixmap area */
  119. movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
  120. #ifdef CONFIG_X86_PAE
  121. #define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
  122. movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)
  123. #else
  124. movl %eax,pa(initial_page_table+0xffc)
  125. #endif
  126. #ifdef CONFIG_PARAVIRT
  127. /* This is can only trip for a broken bootloader... */
  128. cmpw $0x207, pa(boot_params + BP_version)
  129. jb .Ldefault_entry
  130. /* Paravirt-compatible boot parameters. Look to see what architecture
  131. we're booting under. */
  132. movl pa(boot_params + BP_hardware_subarch), %eax
  133. cmpl $num_subarch_entries, %eax
  134. jae .Lbad_subarch
  135. movl pa(subarch_entries)(,%eax,4), %eax
  136. subl $__PAGE_OFFSET, %eax
  137. jmp *%eax
  138. .Lbad_subarch:
  139. WEAK(xen_entry)
  140. /* Unknown implementation; there's really
  141. nothing we can do at this point. */
  142. ud2a
  143. __INITDATA
  144. subarch_entries:
  145. .long .Ldefault_entry /* normal x86/PC */
  146. .long xen_entry /* Xen hypervisor */
  147. .long .Ldefault_entry /* Moorestown MID */
  148. num_subarch_entries = (. - subarch_entries) / 4
  149. .previous
  150. #else
  151. jmp .Ldefault_entry
  152. #endif /* CONFIG_PARAVIRT */
  153. #ifdef CONFIG_HOTPLUG_CPU
  154. /*
  155. * Boot CPU0 entry point. It's called from play_dead(). Everything has been set
  156. * up already except stack. We just set up stack here. Then call
  157. * start_secondary().
  158. */
  159. ENTRY(start_cpu0)
  160. movl initial_stack, %ecx
  161. movl %ecx, %esp
  162. call *(initial_code)
  163. 1: jmp 1b
  164. ENDPROC(start_cpu0)
  165. #endif
  166. /*
  167. * Non-boot CPU entry point; entered from trampoline.S
  168. * We can't lgdt here, because lgdt itself uses a data segment, but
  169. * we know the trampoline has already loaded the boot_gdt for us.
  170. *
  171. * If cpu hotplug is not supported then this code can go in init section
  172. * which will be freed later
  173. */
  174. ENTRY(startup_32_smp)
  175. cld
  176. movl $(__BOOT_DS),%eax
  177. movl %eax,%ds
  178. movl %eax,%es
  179. movl %eax,%fs
  180. movl %eax,%gs
  181. movl pa(initial_stack),%ecx
  182. movl %eax,%ss
  183. leal -__PAGE_OFFSET(%ecx),%esp
  184. #ifdef CONFIG_MICROCODE
  185. /* Early load ucode on AP. */
  186. call load_ucode_ap
  187. #endif
  188. .Ldefault_entry:
  189. movl $(CR0_STATE & ~X86_CR0_PG),%eax
  190. movl %eax,%cr0
  191. /*
  192. * We want to start out with EFLAGS unambiguously cleared. Some BIOSes leave
  193. * bits like NT set. This would confuse the debugger if this code is traced. So
  194. * initialize them properly now before switching to protected mode. That means
  195. * DF in particular (even though we have cleared it earlier after copying the
  196. * command line) because GCC expects it.
  197. */
  198. pushl $0
  199. popfl
  200. /*
  201. * New page tables may be in 4Mbyte page mode and may be using the global pages.
  202. *
  203. * NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists
  204. * if and only if CPUID exists and has flags other than the FPU flag set.
  205. */
  206. movl $-1,pa(X86_CPUID) # preset CPUID level
  207. movl $X86_EFLAGS_ID,%ecx
  208. pushl %ecx
  209. popfl # set EFLAGS=ID
  210. pushfl
  211. popl %eax # get EFLAGS
  212. testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set?
  213. jz .Lenable_paging # hw disallowed setting of ID bit
  214. # which means no CPUID and no CR4
  215. xorl %eax,%eax
  216. cpuid
  217. movl %eax,pa(X86_CPUID) # save largest std CPUID function
  218. movl $1,%eax
  219. cpuid
  220. andl $~1,%edx # Ignore CPUID.FPU
  221. jz .Lenable_paging # No flags or only CPUID.FPU = no CR4
  222. movl pa(mmu_cr4_features),%eax
  223. movl %eax,%cr4
  224. testb $X86_CR4_PAE, %al # check if PAE is enabled
  225. jz .Lenable_paging
  226. /* Check if extended functions are implemented */
  227. movl $0x80000000, %eax
  228. cpuid
  229. /* Value must be in the range 0x80000001 to 0x8000ffff */
  230. subl $0x80000001, %eax
  231. cmpl $(0x8000ffff-0x80000001), %eax
  232. ja .Lenable_paging
  233. /* Clear bogus XD_DISABLE bits */
  234. call verify_cpu
  235. mov $0x80000001, %eax
  236. cpuid
  237. /* Execute Disable bit supported? */
  238. btl $(X86_FEATURE_NX & 31), %edx
  239. jnc .Lenable_paging
  240. /* Setup EFER (Extended Feature Enable Register) */
  241. movl $MSR_EFER, %ecx
  242. rdmsr
  243. btsl $_EFER_NX, %eax
  244. /* Make changes effective */
  245. wrmsr
  246. .Lenable_paging:
  247. /*
  248. * Enable paging
  249. */
  250. movl $pa(initial_page_table), %eax
  251. movl %eax,%cr3 /* set the page table pointer.. */
  252. movl $CR0_STATE,%eax
  253. movl %eax,%cr0 /* ..and set paging (PG) bit */
  254. ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
  255. 1:
  256. /* Shift the stack pointer to a virtual address */
  257. addl $__PAGE_OFFSET, %esp
  258. /*
  259. * start system 32-bit setup. We need to re-do some of the things done
  260. * in 16-bit mode for the "real" operations.
  261. */
  262. movl setup_once_ref,%eax
  263. andl %eax,%eax
  264. jz 1f # Did we do this already?
  265. call *%eax
  266. 1:
  267. /*
  268. * Check if it is 486
  269. */
  270. movb $4,X86 # at least 486
  271. cmpl $-1,X86_CPUID
  272. je .Lis486
  273. /* get vendor info */
  274. xorl %eax,%eax # call CPUID with 0 -> return vendor ID
  275. cpuid
  276. movl %eax,X86_CPUID # save CPUID level
  277. movl %ebx,X86_VENDOR_ID # lo 4 chars
  278. movl %edx,X86_VENDOR_ID+4 # next 4 chars
  279. movl %ecx,X86_VENDOR_ID+8 # last 4 chars
  280. orl %eax,%eax # do we have processor info as well?
  281. je .Lis486
  282. movl $1,%eax # Use the CPUID instruction to get CPU type
  283. cpuid
  284. movb %al,%cl # save reg for future use
  285. andb $0x0f,%ah # mask processor family
  286. movb %ah,X86
  287. andb $0xf0,%al # mask model
  288. shrb $4,%al
  289. movb %al,X86_MODEL
  290. andb $0x0f,%cl # mask mask revision
  291. movb %cl,X86_STEPPING
  292. movl %edx,X86_CAPABILITY
  293. .Lis486:
  294. movl $0x50022,%ecx # set AM, WP, NE and MP
  295. movl %cr0,%eax
  296. andl $0x80000011,%eax # Save PG,PE,ET
  297. orl %ecx,%eax
  298. movl %eax,%cr0
  299. lgdt early_gdt_descr
  300. ljmp $(__KERNEL_CS),$1f
  301. 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
  302. movl %eax,%ss # after changing gdt.
  303. movl $(__USER_DS),%eax # DS/ES contains default USER segment
  304. movl %eax,%ds
  305. movl %eax,%es
  306. movl $(__KERNEL_PERCPU), %eax
  307. movl %eax,%fs # set this cpu's percpu
  308. movl $(__KERNEL_STACK_CANARY),%eax
  309. movl %eax,%gs
  310. xorl %eax,%eax # Clear LDT
  311. lldt %ax
  312. call *(initial_code)
  313. 1: jmp 1b
  314. ENDPROC(startup_32_smp)
  315. #include "verify_cpu.S"
  316. /*
  317. * setup_once
  318. *
  319. * The setup work we only want to run on the BSP.
  320. *
  321. * Warning: %esi is live across this function.
  322. */
  323. __INIT
  324. setup_once:
  325. #ifdef CONFIG_CC_STACKPROTECTOR
  326. /*
  327. * Configure the stack canary. The linker can't handle this by
  328. * relocation. Manually set base address in stack canary
  329. * segment descriptor.
  330. */
  331. movl $gdt_page,%eax
  332. movl $stack_canary,%ecx
  333. movw %cx, 8 * GDT_ENTRY_STACK_CANARY + 2(%eax)
  334. shrl $16, %ecx
  335. movb %cl, 8 * GDT_ENTRY_STACK_CANARY + 4(%eax)
  336. movb %ch, 8 * GDT_ENTRY_STACK_CANARY + 7(%eax)
  337. #endif
  338. andl $0,setup_once_ref /* Once is enough, thanks */
  339. ret
  340. ENTRY(early_idt_handler_array)
  341. # 36(%esp) %eflags
  342. # 32(%esp) %cs
  343. # 28(%esp) %eip
  344. # 24(%rsp) error code
  345. i = 0
  346. .rept NUM_EXCEPTION_VECTORS
  347. .if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
  348. pushl $0 # Dummy error code, to make stack frame uniform
  349. .endif
  350. pushl $i # 20(%esp) Vector number
  351. jmp early_idt_handler_common
  352. i = i + 1
  353. .fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
  354. .endr
  355. ENDPROC(early_idt_handler_array)
  356. early_idt_handler_common:
  357. /*
  358. * The stack is the hardware frame, an error code or zero, and the
  359. * vector number.
  360. */
  361. cld
  362. incl %ss:early_recursion_flag
  363. /* The vector number is in pt_regs->gs */
  364. cld
  365. pushl %fs /* pt_regs->fs (__fsh varies by model) */
  366. pushl %es /* pt_regs->es (__esh varies by model) */
  367. pushl %ds /* pt_regs->ds (__dsh varies by model) */
  368. pushl %eax /* pt_regs->ax */
  369. pushl %ebp /* pt_regs->bp */
  370. pushl %edi /* pt_regs->di */
  371. pushl %esi /* pt_regs->si */
  372. pushl %edx /* pt_regs->dx */
  373. pushl %ecx /* pt_regs->cx */
  374. pushl %ebx /* pt_regs->bx */
  375. /* Fix up DS and ES */
  376. movl $(__KERNEL_DS), %ecx
  377. movl %ecx, %ds
  378. movl %ecx, %es
  379. /* Load the vector number into EDX */
  380. movl PT_GS(%esp), %edx
  381. /* Load GS into pt_regs->gs (and maybe clobber __gsh) */
  382. movw %gs, PT_GS(%esp)
  383. movl %esp, %eax /* args are pt_regs (EAX), trapnr (EDX) */
  384. call early_fixup_exception
  385. popl %ebx /* pt_regs->bx */
  386. popl %ecx /* pt_regs->cx */
  387. popl %edx /* pt_regs->dx */
  388. popl %esi /* pt_regs->si */
  389. popl %edi /* pt_regs->di */
  390. popl %ebp /* pt_regs->bp */
  391. popl %eax /* pt_regs->ax */
  392. popl %ds /* pt_regs->ds (always ignores __dsh) */
  393. popl %es /* pt_regs->es (always ignores __esh) */
  394. popl %fs /* pt_regs->fs (always ignores __fsh) */
  395. popl %gs /* pt_regs->gs (always ignores __gsh) */
  396. decl %ss:early_recursion_flag
  397. addl $4, %esp /* pop pt_regs->orig_ax */
  398. iret
  399. ENDPROC(early_idt_handler_common)
  400. /* This is the default interrupt "handler" :-) */
  401. ENTRY(early_ignore_irq)
  402. cld
  403. #ifdef CONFIG_PRINTK
  404. pushl %eax
  405. pushl %ecx
  406. pushl %edx
  407. pushl %es
  408. pushl %ds
  409. movl $(__KERNEL_DS),%eax
  410. movl %eax,%ds
  411. movl %eax,%es
  412. cmpl $2,early_recursion_flag
  413. je hlt_loop
  414. incl early_recursion_flag
  415. pushl 16(%esp)
  416. pushl 24(%esp)
  417. pushl 32(%esp)
  418. pushl 40(%esp)
  419. pushl $int_msg
  420. call printk
  421. call dump_stack
  422. addl $(5*4),%esp
  423. popl %ds
  424. popl %es
  425. popl %edx
  426. popl %ecx
  427. popl %eax
  428. #endif
  429. iret
  430. hlt_loop:
  431. hlt
  432. jmp hlt_loop
  433. ENDPROC(early_ignore_irq)
  434. __INITDATA
  435. .align 4
  436. GLOBAL(early_recursion_flag)
  437. .long 0
  438. __REFDATA
  439. .align 4
  440. ENTRY(initial_code)
  441. .long i386_start_kernel
  442. ENTRY(setup_once_ref)
  443. .long setup_once
  444. /*
  445. * BSS section
  446. */
  447. __PAGE_ALIGNED_BSS
  448. .align PAGE_SIZE
  449. #ifdef CONFIG_X86_PAE
  450. .globl initial_pg_pmd
  451. initial_pg_pmd:
  452. .fill 1024*KPMDS,4,0
  453. #else
  454. .globl initial_page_table
  455. initial_page_table:
  456. .fill 1024,4,0
  457. #endif
  458. initial_pg_fixmap:
  459. .fill 1024,4,0
  460. .globl empty_zero_page
  461. empty_zero_page:
  462. .fill 4096,1,0
  463. .globl swapper_pg_dir
  464. swapper_pg_dir:
  465. .fill 1024,4,0
  466. EXPORT_SYMBOL(empty_zero_page)
  467. /*
  468. * This starts the data section.
  469. */
  470. #ifdef CONFIG_X86_PAE
  471. __PAGE_ALIGNED_DATA
  472. /* Page-aligned for the benefit of paravirt? */
  473. .align PAGE_SIZE
  474. ENTRY(initial_page_table)
  475. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */
  476. # if KPMDS == 3
  477. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  478. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
  479. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x2000),0
  480. # elif KPMDS == 2
  481. .long 0,0
  482. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  483. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
  484. # elif KPMDS == 1
  485. .long 0,0
  486. .long 0,0
  487. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  488. # else
  489. # error "Kernel PMDs should be 1, 2 or 3"
  490. # endif
  491. .align PAGE_SIZE /* needs to be page-sized too */
  492. #endif
  493. .data
  494. .balign 4
  495. ENTRY(initial_stack)
  496. /*
  497. * The SIZEOF_PTREGS gap is a convention which helps the in-kernel
  498. * unwinder reliably detect the end of the stack.
  499. */
  500. .long init_thread_union + THREAD_SIZE - SIZEOF_PTREGS - \
  501. TOP_OF_KERNEL_STACK_PADDING;
  502. __INITRODATA
  503. int_msg:
  504. .asciz "Unknown interrupt or fault at: %p %p %p\n"
  505. #include "../../x86/xen/xen-head.S"
  506. /*
  507. * The IDT and GDT 'descriptors' are a strange 48-bit object
  508. * only used by the lidt and lgdt instructions. They are not
  509. * like usual segment descriptors - they consist of a 16-bit
  510. * segment size, and 32-bit linear address value:
  511. */
  512. .data
  513. .globl boot_gdt_descr
  514. ALIGN
  515. # early boot GDT descriptor (must use 1:1 address mapping)
  516. .word 0 # 32 bit align gdt_desc.address
  517. boot_gdt_descr:
  518. .word __BOOT_DS+7
  519. .long boot_gdt - __PAGE_OFFSET
  520. # boot GDT descriptor (later on used by CPU#0):
  521. .word 0 # 32 bit align gdt_desc.address
  522. ENTRY(early_gdt_descr)
  523. .word GDT_ENTRIES*8-1
  524. .long gdt_page /* Overwritten for secondary CPUs */
  525. /*
  526. * The boot_gdt must mirror the equivalent in setup.S and is
  527. * used only for booting.
  528. */
  529. .align L1_CACHE_BYTES
  530. ENTRY(boot_gdt)
  531. .fill GDT_ENTRY_BOOT_CS,8,0
  532. .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
  533. .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */