head_32.S 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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/export.h>
  11. #include <linux/threads.h>
  12. #include <linux/init.h>
  13. #include <linux/linkage.h>
  14. #include <asm/segment.h>
  15. #include <asm/page_types.h>
  16. #include <asm/pgtable_types.h>
  17. #include <asm/cache.h>
  18. #include <asm/thread_info.h>
  19. #include <asm/asm-offsets.h>
  20. #include <asm/setup.h>
  21. #include <asm/processor-flags.h>
  22. #include <asm/msr-index.h>
  23. #include <asm/cpufeatures.h>
  24. #include <asm/percpu.h>
  25. #include <asm/nops.h>
  26. #include <asm/nospec-branch.h>
  27. #include <asm/bootparam.h>
  28. #include <asm/pgtable_32.h>
  29. /* Physical address */
  30. #define pa(X) ((X) - __PAGE_OFFSET)
  31. /*
  32. * References to members of the new_cpu_data structure.
  33. */
  34. #define X86 new_cpu_data+CPUINFO_x86
  35. #define X86_VENDOR new_cpu_data+CPUINFO_x86_vendor
  36. #define X86_MODEL new_cpu_data+CPUINFO_x86_model
  37. #define X86_STEPPING new_cpu_data+CPUINFO_x86_stepping
  38. #define X86_HARD_MATH new_cpu_data+CPUINFO_hard_math
  39. #define X86_CPUID new_cpu_data+CPUINFO_cpuid_level
  40. #define X86_CAPABILITY new_cpu_data+CPUINFO_x86_capability
  41. #define X86_VENDOR_ID new_cpu_data+CPUINFO_x86_vendor_id
  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. SYM_CODE_START(startup_32)
  59. movl pa(initial_stack),%ecx
  60. /*
  61. * Set segments to known values.
  62. */
  63. lgdt pa(boot_gdt_descr)
  64. movl $(__BOOT_DS),%eax
  65. movl %eax,%ds
  66. movl %eax,%es
  67. movl %eax,%fs
  68. movl %eax,%gs
  69. movl %eax,%ss
  70. leal -__PAGE_OFFSET(%ecx),%esp
  71. /*
  72. * Clear BSS first so that there are no surprises...
  73. */
  74. cld
  75. xorl %eax,%eax
  76. movl $pa(__bss_start),%edi
  77. movl $pa(__bss_stop),%ecx
  78. subl %edi,%ecx
  79. shrl $2,%ecx
  80. rep ; stosl
  81. /*
  82. * Copy bootup parameters out of the way.
  83. * Note: %esi still has the pointer to the real-mode data.
  84. * With the kexec as boot loader, parameter segment might be loaded beyond
  85. * kernel image and might not even be addressable by early boot page tables.
  86. * (kexec on panic case). Hence copy out the parameters before initializing
  87. * page tables.
  88. */
  89. movl $pa(boot_params),%edi
  90. movl $(PARAM_SIZE/4),%ecx
  91. cld
  92. rep
  93. movsl
  94. movl pa(boot_params) + NEW_CL_POINTER,%esi
  95. andl %esi,%esi
  96. jz 1f # No command line
  97. movl $pa(boot_command_line),%edi
  98. movl $(COMMAND_LINE_SIZE/4),%ecx
  99. rep
  100. movsl
  101. 1:
  102. #ifdef CONFIG_OLPC
  103. /* save OFW's pgdir table for later use when calling into OFW */
  104. movl %cr3, %eax
  105. movl %eax, pa(olpc_ofw_pgd)
  106. #endif
  107. /* Create early pagetables. */
  108. call mk_early_pgtbl_32
  109. /* Do early initialization of the fixmap area */
  110. movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
  111. #ifdef CONFIG_X86_PAE
  112. #define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
  113. movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)
  114. #else
  115. movl %eax,pa(initial_page_table+0xffc)
  116. #endif
  117. jmp .Ldefault_entry
  118. SYM_CODE_END(startup_32)
  119. /*
  120. * Non-boot CPU entry point; entered from trampoline.S
  121. * We can't lgdt here, because lgdt itself uses a data segment, but
  122. * we know the trampoline has already loaded the boot_gdt for us.
  123. *
  124. * If cpu hotplug is not supported then this code can go in init section
  125. * which will be freed later
  126. */
  127. SYM_FUNC_START(startup_32_smp)
  128. cld
  129. movl $(__BOOT_DS),%eax
  130. movl %eax,%ds
  131. movl %eax,%es
  132. movl %eax,%fs
  133. movl %eax,%gs
  134. movl pa(initial_stack),%ecx
  135. movl %eax,%ss
  136. leal -__PAGE_OFFSET(%ecx),%esp
  137. .Ldefault_entry:
  138. movl $(CR0_STATE & ~X86_CR0_PG),%eax
  139. movl %eax,%cr0
  140. /*
  141. * We want to start out with EFLAGS unambiguously cleared. Some BIOSes leave
  142. * bits like NT set. This would confuse the debugger if this code is traced. So
  143. * initialize them properly now before switching to protected mode. That means
  144. * DF in particular (even though we have cleared it earlier after copying the
  145. * command line) because GCC expects it.
  146. */
  147. pushl $0
  148. popfl
  149. /*
  150. * New page tables may be in 4Mbyte page mode and may be using the global pages.
  151. *
  152. * NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists
  153. * if and only if CPUID exists and has flags other than the FPU flag set.
  154. */
  155. movl $-1,pa(X86_CPUID) # preset CPUID level
  156. movl $X86_EFLAGS_ID,%ecx
  157. pushl %ecx
  158. popfl # set EFLAGS=ID
  159. pushfl
  160. popl %eax # get EFLAGS
  161. testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set?
  162. jz .Lenable_paging # hw disallowed setting of ID bit
  163. # which means no CPUID and no CR4
  164. xorl %eax,%eax
  165. cpuid
  166. movl %eax,pa(X86_CPUID) # save largest std CPUID function
  167. movl $1,%eax
  168. cpuid
  169. andl $~1,%edx # Ignore CPUID.FPU
  170. jz .Lenable_paging # No flags or only CPUID.FPU = no CR4
  171. movl pa(mmu_cr4_features),%eax
  172. movl %eax,%cr4
  173. testb $X86_CR4_PAE, %al # check if PAE is enabled
  174. jz .Lenable_paging
  175. /* Check if extended functions are implemented */
  176. movl $0x80000000, %eax
  177. cpuid
  178. /* Value must be in the range 0x80000001 to 0x8000ffff */
  179. subl $0x80000001, %eax
  180. cmpl $(0x8000ffff-0x80000001), %eax
  181. ja .Lenable_paging
  182. /* Clear bogus XD_DISABLE bits */
  183. call verify_cpu
  184. mov $0x80000001, %eax
  185. cpuid
  186. /* Execute Disable bit supported? */
  187. btl $(X86_FEATURE_NX & 31), %edx
  188. jnc .Lenable_paging
  189. /* Setup EFER (Extended Feature Enable Register) */
  190. movl $MSR_EFER, %ecx
  191. rdmsr
  192. btsl $_EFER_NX, %eax
  193. /* Make changes effective */
  194. wrmsr
  195. .Lenable_paging:
  196. /*
  197. * Enable paging
  198. */
  199. movl $pa(initial_page_table), %eax
  200. movl %eax,%cr3 /* set the page table pointer.. */
  201. movl $CR0_STATE,%eax
  202. movl %eax,%cr0 /* ..and set paging (PG) bit */
  203. ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
  204. 1:
  205. /* Shift the stack pointer to a virtual address */
  206. addl $__PAGE_OFFSET, %esp
  207. /*
  208. * Check if it is 486
  209. */
  210. movb $4,X86 # at least 486
  211. cmpl $-1,X86_CPUID
  212. je .Lis486
  213. /* get vendor info */
  214. xorl %eax,%eax # call CPUID with 0 -> return vendor ID
  215. cpuid
  216. movl %eax,X86_CPUID # save CPUID level
  217. movl %ebx,X86_VENDOR_ID # lo 4 chars
  218. movl %edx,X86_VENDOR_ID+4 # next 4 chars
  219. movl %ecx,X86_VENDOR_ID+8 # last 4 chars
  220. orl %eax,%eax # do we have processor info as well?
  221. je .Lis486
  222. movl $1,%eax # Use the CPUID instruction to get CPU type
  223. cpuid
  224. movb %al,%cl # save reg for future use
  225. andb $0x0f,%ah # mask processor family
  226. movb %ah,X86
  227. andb $0xf0,%al # mask model
  228. shrb $4,%al
  229. movb %al,X86_MODEL
  230. andb $0x0f,%cl # mask mask revision
  231. movb %cl,X86_STEPPING
  232. movl %edx,X86_CAPABILITY
  233. .Lis486:
  234. movl $0x50022,%ecx # set AM, WP, NE and MP
  235. movl %cr0,%eax
  236. andl $0x80000011,%eax # Save PG,PE,ET
  237. orl %ecx,%eax
  238. movl %eax,%cr0
  239. lgdt early_gdt_descr
  240. ljmp $(__KERNEL_CS),$1f
  241. 1: movl $(__KERNEL_DS),%eax # reload all the segment registers
  242. movl %eax,%ss # after changing gdt.
  243. movl $(__USER_DS),%eax # DS/ES contains default USER segment
  244. movl %eax,%ds
  245. movl %eax,%es
  246. movl $(__KERNEL_PERCPU), %eax
  247. movl %eax,%fs # set this cpu's percpu
  248. xorl %eax,%eax
  249. movl %eax,%gs # clear possible garbage in %gs
  250. xorl %eax,%eax # Clear LDT
  251. lldt %ax
  252. call *(initial_code)
  253. 1: jmp 1b
  254. SYM_FUNC_END(startup_32_smp)
  255. #include "verify_cpu.S"
  256. __INIT
  257. SYM_FUNC_START(early_idt_handler_array)
  258. # 36(%esp) %eflags
  259. # 32(%esp) %cs
  260. # 28(%esp) %eip
  261. # 24(%rsp) error code
  262. i = 0
  263. .rept NUM_EXCEPTION_VECTORS
  264. .if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
  265. pushl $0 # Dummy error code, to make stack frame uniform
  266. .endif
  267. pushl $i # 20(%esp) Vector number
  268. jmp early_idt_handler_common
  269. i = i + 1
  270. .fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
  271. .endr
  272. SYM_FUNC_END(early_idt_handler_array)
  273. SYM_CODE_START_LOCAL(early_idt_handler_common)
  274. /*
  275. * The stack is the hardware frame, an error code or zero, and the
  276. * vector number.
  277. */
  278. cld
  279. incl %ss:early_recursion_flag
  280. /* The vector number is in pt_regs->gs */
  281. cld
  282. pushl %fs /* pt_regs->fs (__fsh varies by model) */
  283. pushl %es /* pt_regs->es (__esh varies by model) */
  284. pushl %ds /* pt_regs->ds (__dsh varies by model) */
  285. pushl %eax /* pt_regs->ax */
  286. pushl %ebp /* pt_regs->bp */
  287. pushl %edi /* pt_regs->di */
  288. pushl %esi /* pt_regs->si */
  289. pushl %edx /* pt_regs->dx */
  290. pushl %ecx /* pt_regs->cx */
  291. pushl %ebx /* pt_regs->bx */
  292. /* Fix up DS and ES */
  293. movl $(__KERNEL_DS), %ecx
  294. movl %ecx, %ds
  295. movl %ecx, %es
  296. /* Load the vector number into EDX */
  297. movl PT_GS(%esp), %edx
  298. /* Load GS into pt_regs->gs (and maybe clobber __gsh) */
  299. movw %gs, PT_GS(%esp)
  300. movl %esp, %eax /* args are pt_regs (EAX), trapnr (EDX) */
  301. call early_fixup_exception
  302. popl %ebx /* pt_regs->bx */
  303. popl %ecx /* pt_regs->cx */
  304. popl %edx /* pt_regs->dx */
  305. popl %esi /* pt_regs->si */
  306. popl %edi /* pt_regs->di */
  307. popl %ebp /* pt_regs->bp */
  308. popl %eax /* pt_regs->ax */
  309. popl %ds /* pt_regs->ds (always ignores __dsh) */
  310. popl %es /* pt_regs->es (always ignores __esh) */
  311. popl %fs /* pt_regs->fs (always ignores __fsh) */
  312. popl %gs /* pt_regs->gs (always ignores __gsh) */
  313. decl %ss:early_recursion_flag
  314. addl $4, %esp /* pop pt_regs->orig_ax */
  315. iret
  316. SYM_CODE_END(early_idt_handler_common)
  317. /* This is the default interrupt "handler" :-) */
  318. SYM_FUNC_START(early_ignore_irq)
  319. cld
  320. #ifdef CONFIG_PRINTK
  321. pushl %eax
  322. pushl %ecx
  323. pushl %edx
  324. pushl %es
  325. pushl %ds
  326. movl $(__KERNEL_DS),%eax
  327. movl %eax,%ds
  328. movl %eax,%es
  329. cmpl $2,early_recursion_flag
  330. je hlt_loop
  331. incl early_recursion_flag
  332. pushl 16(%esp)
  333. pushl 24(%esp)
  334. pushl 32(%esp)
  335. pushl 40(%esp)
  336. pushl $int_msg
  337. call _printk
  338. call dump_stack
  339. addl $(5*4),%esp
  340. popl %ds
  341. popl %es
  342. popl %edx
  343. popl %ecx
  344. popl %eax
  345. #endif
  346. iret
  347. hlt_loop:
  348. hlt
  349. jmp hlt_loop
  350. SYM_FUNC_END(early_ignore_irq)
  351. __INITDATA
  352. .align 4
  353. SYM_DATA(early_recursion_flag, .long 0)
  354. __REFDATA
  355. .align 4
  356. SYM_DATA(initial_code, .long i386_start_kernel)
  357. #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
  358. #define PGD_ALIGN (2 * PAGE_SIZE)
  359. #define PTI_USER_PGD_FILL 1024
  360. #else
  361. #define PGD_ALIGN (PAGE_SIZE)
  362. #define PTI_USER_PGD_FILL 0
  363. #endif
  364. /*
  365. * BSS section
  366. */
  367. __PAGE_ALIGNED_BSS
  368. .align PGD_ALIGN
  369. #ifdef CONFIG_X86_PAE
  370. .globl initial_pg_pmd
  371. initial_pg_pmd:
  372. .fill 1024*KPMDS,4,0
  373. #else
  374. .globl initial_page_table
  375. initial_page_table:
  376. .fill 1024,4,0
  377. #endif
  378. .align PGD_ALIGN
  379. initial_pg_fixmap:
  380. .fill 1024,4,0
  381. .globl swapper_pg_dir
  382. .align PGD_ALIGN
  383. swapper_pg_dir:
  384. .fill 1024,4,0
  385. .fill PTI_USER_PGD_FILL,4,0
  386. .globl empty_zero_page
  387. empty_zero_page:
  388. .fill 4096,1,0
  389. EXPORT_SYMBOL(empty_zero_page)
  390. /*
  391. * This starts the data section.
  392. */
  393. #ifdef CONFIG_X86_PAE
  394. __PAGE_ALIGNED_DATA
  395. /* Page-aligned for the benefit of paravirt? */
  396. .align PGD_ALIGN
  397. SYM_DATA_START(initial_page_table)
  398. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */
  399. # if KPMDS == 3
  400. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  401. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
  402. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x2000),0
  403. # elif KPMDS == 2
  404. .long 0,0
  405. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  406. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
  407. # elif KPMDS == 1
  408. .long 0,0
  409. .long 0,0
  410. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  411. # else
  412. # error "Kernel PMDs should be 1, 2 or 3"
  413. # endif
  414. .align PAGE_SIZE /* needs to be page-sized too */
  415. #ifdef CONFIG_MITIGATION_PAGE_TABLE_ISOLATION
  416. /*
  417. * PTI needs another page so sync_initial_pagetable() works correctly
  418. * and does not scribble over the data which is placed behind the
  419. * actual initial_page_table. See clone_pgd_range().
  420. */
  421. .fill 1024, 4, 0
  422. #endif
  423. SYM_DATA_END(initial_page_table)
  424. #endif
  425. .data
  426. .balign 4
  427. SYM_DATA(initial_stack, .long __top_init_kernel_stack)
  428. __INITRODATA
  429. int_msg:
  430. .asciz "Unknown interrupt or fault at: %p %p %p\n"
  431. #include "../xen/xen-head.S"
  432. /*
  433. * The IDT and GDT 'descriptors' are a strange 48-bit object
  434. * only used by the lidt and lgdt instructions. They are not
  435. * like usual segment descriptors - they consist of a 16-bit
  436. * segment size, and 32-bit linear address value:
  437. */
  438. .data
  439. ALIGN
  440. # early boot GDT descriptor (must use 1:1 address mapping)
  441. .word 0 # 32 bit align gdt_desc.address
  442. SYM_DATA_START_LOCAL(boot_gdt_descr)
  443. .word __BOOT_DS+7
  444. .long boot_gdt - __PAGE_OFFSET
  445. SYM_DATA_END(boot_gdt_descr)
  446. # boot GDT descriptor (later on used by CPU#0):
  447. .word 0 # 32 bit align gdt_desc.address
  448. SYM_DATA_START(early_gdt_descr)
  449. .word GDT_ENTRIES*8-1
  450. .long gdt_page /* Overwritten for secondary CPUs */
  451. SYM_DATA_END(early_gdt_descr)
  452. /*
  453. * The boot_gdt must mirror the equivalent in setup.S and is
  454. * used only for booting.
  455. */
  456. .align L1_CACHE_BYTES
  457. SYM_DATA_START(boot_gdt)
  458. .fill GDT_ENTRY_BOOT_CS,8,0
  459. .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
  460. .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */
  461. SYM_DATA_END(boot_gdt)