head_32.S 14 KB

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