head_32.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. 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. #ifdef CONFIG_MICROCODE
  108. /* Early load ucode on BSP. */
  109. call load_ucode_bsp
  110. #endif
  111. /* Create early pagetables. */
  112. call mk_early_pgtbl_32
  113. /* Do early initialization of the fixmap area */
  114. movl $pa(initial_pg_fixmap)+PDE_IDENT_ATTR,%eax
  115. #ifdef CONFIG_X86_PAE
  116. #define KPMDS (((-__PAGE_OFFSET) >> 30) & 3) /* Number of kernel PMDs */
  117. movl %eax,pa(initial_pg_pmd+0x1000*KPMDS-8)
  118. #else
  119. movl %eax,pa(initial_page_table+0xffc)
  120. #endif
  121. jmp .Ldefault_entry
  122. SYM_CODE_END(startup_32)
  123. #ifdef CONFIG_HOTPLUG_CPU
  124. /*
  125. * Boot CPU0 entry point. It's called from play_dead(). Everything has been set
  126. * up already except stack. We just set up stack here. Then call
  127. * start_secondary().
  128. */
  129. SYM_FUNC_START(start_cpu0)
  130. movl initial_stack, %ecx
  131. movl %ecx, %esp
  132. call *(initial_code)
  133. 1: jmp 1b
  134. SYM_FUNC_END(start_cpu0)
  135. #endif
  136. /*
  137. * Non-boot CPU entry point; entered from trampoline.S
  138. * We can't lgdt here, because lgdt itself uses a data segment, but
  139. * we know the trampoline has already loaded the boot_gdt for us.
  140. *
  141. * If cpu hotplug is not supported then this code can go in init section
  142. * which will be freed later
  143. */
  144. SYM_FUNC_START(startup_32_smp)
  145. cld
  146. movl $(__BOOT_DS),%eax
  147. movl %eax,%ds
  148. movl %eax,%es
  149. movl %eax,%fs
  150. movl %eax,%gs
  151. movl pa(initial_stack),%ecx
  152. movl %eax,%ss
  153. leal -__PAGE_OFFSET(%ecx),%esp
  154. #ifdef CONFIG_MICROCODE
  155. /* Early load ucode on AP. */
  156. call load_ucode_ap
  157. #endif
  158. .Ldefault_entry:
  159. movl $(CR0_STATE & ~X86_CR0_PG),%eax
  160. movl %eax,%cr0
  161. /*
  162. * We want to start out with EFLAGS unambiguously cleared. Some BIOSes leave
  163. * bits like NT set. This would confuse the debugger if this code is traced. So
  164. * initialize them properly now before switching to protected mode. That means
  165. * DF in particular (even though we have cleared it earlier after copying the
  166. * command line) because GCC expects it.
  167. */
  168. pushl $0
  169. popfl
  170. /*
  171. * New page tables may be in 4Mbyte page mode and may be using the global pages.
  172. *
  173. * NOTE! If we are on a 486 we may have no cr4 at all! Specifically, cr4 exists
  174. * if and only if CPUID exists and has flags other than the FPU flag set.
  175. */
  176. movl $-1,pa(X86_CPUID) # preset CPUID level
  177. movl $X86_EFLAGS_ID,%ecx
  178. pushl %ecx
  179. popfl # set EFLAGS=ID
  180. pushfl
  181. popl %eax # get EFLAGS
  182. testl $X86_EFLAGS_ID,%eax # did EFLAGS.ID remained set?
  183. jz .Lenable_paging # hw disallowed setting of ID bit
  184. # which means no CPUID and no CR4
  185. xorl %eax,%eax
  186. cpuid
  187. movl %eax,pa(X86_CPUID) # save largest std CPUID function
  188. movl $1,%eax
  189. cpuid
  190. andl $~1,%edx # Ignore CPUID.FPU
  191. jz .Lenable_paging # No flags or only CPUID.FPU = no CR4
  192. movl pa(mmu_cr4_features),%eax
  193. movl %eax,%cr4
  194. testb $X86_CR4_PAE, %al # check if PAE is enabled
  195. jz .Lenable_paging
  196. /* Check if extended functions are implemented */
  197. movl $0x80000000, %eax
  198. cpuid
  199. /* Value must be in the range 0x80000001 to 0x8000ffff */
  200. subl $0x80000001, %eax
  201. cmpl $(0x8000ffff-0x80000001), %eax
  202. ja .Lenable_paging
  203. /* Clear bogus XD_DISABLE bits */
  204. call verify_cpu
  205. mov $0x80000001, %eax
  206. cpuid
  207. /* Execute Disable bit supported? */
  208. btl $(X86_FEATURE_NX & 31), %edx
  209. jnc .Lenable_paging
  210. /* Setup EFER (Extended Feature Enable Register) */
  211. movl $MSR_EFER, %ecx
  212. rdmsr
  213. btsl $_EFER_NX, %eax
  214. /* Make changes effective */
  215. wrmsr
  216. .Lenable_paging:
  217. /*
  218. * Enable paging
  219. */
  220. movl $pa(initial_page_table), %eax
  221. movl %eax,%cr3 /* set the page table pointer.. */
  222. movl $CR0_STATE,%eax
  223. movl %eax,%cr0 /* ..and set paging (PG) bit */
  224. ljmp $__BOOT_CS,$1f /* Clear prefetch and normalize %eip */
  225. 1:
  226. /* Shift the stack pointer to a virtual address */
  227. addl $__PAGE_OFFSET, %esp
  228. /*
  229. * start system 32-bit setup. We need to re-do some of the things done
  230. * in 16-bit mode for the "real" operations.
  231. */
  232. movl setup_once_ref,%eax
  233. andl %eax,%eax
  234. jz 1f # Did we do this already?
  235. call *%eax
  236. 1:
  237. /*
  238. * Check if it is 486
  239. */
  240. movb $4,X86 # at least 486
  241. cmpl $-1,X86_CPUID
  242. je .Lis486
  243. /* get vendor info */
  244. xorl %eax,%eax # call CPUID with 0 -> return vendor ID
  245. cpuid
  246. movl %eax,X86_CPUID # save CPUID level
  247. movl %ebx,X86_VENDOR_ID # lo 4 chars
  248. movl %edx,X86_VENDOR_ID+4 # next 4 chars
  249. movl %ecx,X86_VENDOR_ID+8 # last 4 chars
  250. orl %eax,%eax # do we have processor info as well?
  251. je .Lis486
  252. movl $1,%eax # Use the CPUID instruction to get CPU type
  253. cpuid
  254. movb %al,%cl # save reg for future use
  255. andb $0x0f,%ah # mask processor family
  256. movb %ah,X86
  257. andb $0xf0,%al # mask model
  258. shrb $4,%al
  259. movb %al,X86_MODEL
  260. andb $0x0f,%cl # mask mask revision
  261. movb %cl,X86_STEPPING
  262. movl %edx,X86_CAPABILITY
  263. .Lis486:
  264. movl $0x50022,%ecx # set AM, WP, NE and MP
  265. movl %cr0,%eax
  266. andl $0x80000011,%eax # Save PG,PE,ET
  267. orl %ecx,%eax
  268. movl %eax,%cr0
  269. lgdt early_gdt_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. movl $(__KERNEL_PERCPU), %eax
  277. movl %eax,%fs # set this cpu's percpu
  278. movl $(__KERNEL_STACK_CANARY),%eax
  279. movl %eax,%gs
  280. xorl %eax,%eax # Clear LDT
  281. lldt %ax
  282. call *(initial_code)
  283. 1: jmp 1b
  284. SYM_FUNC_END(startup_32_smp)
  285. #include "verify_cpu.S"
  286. /*
  287. * setup_once
  288. *
  289. * The setup work we only want to run on the BSP.
  290. *
  291. * Warning: %esi is live across this function.
  292. */
  293. __INIT
  294. setup_once:
  295. #ifdef CONFIG_STACKPROTECTOR
  296. /*
  297. * Configure the stack canary. The linker can't handle this by
  298. * relocation. Manually set base address in stack canary
  299. * segment descriptor.
  300. */
  301. movl $gdt_page,%eax
  302. movl $stack_canary,%ecx
  303. movw %cx, 8 * GDT_ENTRY_STACK_CANARY + 2(%eax)
  304. shrl $16, %ecx
  305. movb %cl, 8 * GDT_ENTRY_STACK_CANARY + 4(%eax)
  306. movb %ch, 8 * GDT_ENTRY_STACK_CANARY + 7(%eax)
  307. #endif
  308. andl $0,setup_once_ref /* Once is enough, thanks */
  309. ret
  310. SYM_FUNC_START(early_idt_handler_array)
  311. # 36(%esp) %eflags
  312. # 32(%esp) %cs
  313. # 28(%esp) %eip
  314. # 24(%rsp) error code
  315. i = 0
  316. .rept NUM_EXCEPTION_VECTORS
  317. .if ((EXCEPTION_ERRCODE_MASK >> i) & 1) == 0
  318. pushl $0 # Dummy error code, to make stack frame uniform
  319. .endif
  320. pushl $i # 20(%esp) Vector number
  321. jmp early_idt_handler_common
  322. i = i + 1
  323. .fill early_idt_handler_array + i*EARLY_IDT_HANDLER_SIZE - ., 1, 0xcc
  324. .endr
  325. SYM_FUNC_END(early_idt_handler_array)
  326. SYM_CODE_START_LOCAL(early_idt_handler_common)
  327. /*
  328. * The stack is the hardware frame, an error code or zero, and the
  329. * vector number.
  330. */
  331. cld
  332. incl %ss:early_recursion_flag
  333. /* The vector number is in pt_regs->gs */
  334. cld
  335. pushl %fs /* pt_regs->fs (__fsh varies by model) */
  336. pushl %es /* pt_regs->es (__esh varies by model) */
  337. pushl %ds /* pt_regs->ds (__dsh varies by model) */
  338. pushl %eax /* pt_regs->ax */
  339. pushl %ebp /* pt_regs->bp */
  340. pushl %edi /* pt_regs->di */
  341. pushl %esi /* pt_regs->si */
  342. pushl %edx /* pt_regs->dx */
  343. pushl %ecx /* pt_regs->cx */
  344. pushl %ebx /* pt_regs->bx */
  345. /* Fix up DS and ES */
  346. movl $(__KERNEL_DS), %ecx
  347. movl %ecx, %ds
  348. movl %ecx, %es
  349. /* Load the vector number into EDX */
  350. movl PT_GS(%esp), %edx
  351. /* Load GS into pt_regs->gs (and maybe clobber __gsh) */
  352. movw %gs, PT_GS(%esp)
  353. movl %esp, %eax /* args are pt_regs (EAX), trapnr (EDX) */
  354. call early_fixup_exception
  355. popl %ebx /* pt_regs->bx */
  356. popl %ecx /* pt_regs->cx */
  357. popl %edx /* pt_regs->dx */
  358. popl %esi /* pt_regs->si */
  359. popl %edi /* pt_regs->di */
  360. popl %ebp /* pt_regs->bp */
  361. popl %eax /* pt_regs->ax */
  362. popl %ds /* pt_regs->ds (always ignores __dsh) */
  363. popl %es /* pt_regs->es (always ignores __esh) */
  364. popl %fs /* pt_regs->fs (always ignores __fsh) */
  365. popl %gs /* pt_regs->gs (always ignores __gsh) */
  366. decl %ss:early_recursion_flag
  367. addl $4, %esp /* pop pt_regs->orig_ax */
  368. iret
  369. SYM_CODE_END(early_idt_handler_common)
  370. /* This is the default interrupt "handler" :-) */
  371. SYM_FUNC_START(early_ignore_irq)
  372. cld
  373. #ifdef CONFIG_PRINTK
  374. pushl %eax
  375. pushl %ecx
  376. pushl %edx
  377. pushl %es
  378. pushl %ds
  379. movl $(__KERNEL_DS),%eax
  380. movl %eax,%ds
  381. movl %eax,%es
  382. cmpl $2,early_recursion_flag
  383. je hlt_loop
  384. incl early_recursion_flag
  385. pushl 16(%esp)
  386. pushl 24(%esp)
  387. pushl 32(%esp)
  388. pushl 40(%esp)
  389. pushl $int_msg
  390. call printk
  391. call dump_stack
  392. addl $(5*4),%esp
  393. popl %ds
  394. popl %es
  395. popl %edx
  396. popl %ecx
  397. popl %eax
  398. #endif
  399. iret
  400. hlt_loop:
  401. hlt
  402. jmp hlt_loop
  403. SYM_FUNC_END(early_ignore_irq)
  404. __INITDATA
  405. .align 4
  406. SYM_DATA(early_recursion_flag, .long 0)
  407. __REFDATA
  408. .align 4
  409. SYM_DATA(initial_code, .long i386_start_kernel)
  410. SYM_DATA(setup_once_ref, .long setup_once)
  411. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  412. #define PGD_ALIGN (2 * PAGE_SIZE)
  413. #define PTI_USER_PGD_FILL 1024
  414. #else
  415. #define PGD_ALIGN (PAGE_SIZE)
  416. #define PTI_USER_PGD_FILL 0
  417. #endif
  418. /*
  419. * BSS section
  420. */
  421. __PAGE_ALIGNED_BSS
  422. .align PGD_ALIGN
  423. #ifdef CONFIG_X86_PAE
  424. .globl initial_pg_pmd
  425. initial_pg_pmd:
  426. .fill 1024*KPMDS,4,0
  427. #else
  428. .globl initial_page_table
  429. initial_page_table:
  430. .fill 1024,4,0
  431. #endif
  432. .align PGD_ALIGN
  433. initial_pg_fixmap:
  434. .fill 1024,4,0
  435. .globl swapper_pg_dir
  436. .align PGD_ALIGN
  437. swapper_pg_dir:
  438. .fill 1024,4,0
  439. .fill PTI_USER_PGD_FILL,4,0
  440. .globl empty_zero_page
  441. empty_zero_page:
  442. .fill 4096,1,0
  443. EXPORT_SYMBOL(empty_zero_page)
  444. /*
  445. * This starts the data section.
  446. */
  447. #ifdef CONFIG_X86_PAE
  448. __PAGE_ALIGNED_DATA
  449. /* Page-aligned for the benefit of paravirt? */
  450. .align PGD_ALIGN
  451. SYM_DATA_START(initial_page_table)
  452. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0 /* low identity map */
  453. # if KPMDS == 3
  454. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  455. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
  456. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x2000),0
  457. # elif KPMDS == 2
  458. .long 0,0
  459. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  460. .long pa(initial_pg_pmd+PGD_IDENT_ATTR+0x1000),0
  461. # elif KPMDS == 1
  462. .long 0,0
  463. .long 0,0
  464. .long pa(initial_pg_pmd+PGD_IDENT_ATTR),0
  465. # else
  466. # error "Kernel PMDs should be 1, 2 or 3"
  467. # endif
  468. .align PAGE_SIZE /* needs to be page-sized too */
  469. #ifdef CONFIG_PAGE_TABLE_ISOLATION
  470. /*
  471. * PTI needs another page so sync_initial_pagetable() works correctly
  472. * and does not scribble over the data which is placed behind the
  473. * actual initial_page_table. See clone_pgd_range().
  474. */
  475. .fill 1024, 4, 0
  476. #endif
  477. SYM_DATA_END(initial_page_table)
  478. #endif
  479. .data
  480. .balign 4
  481. /*
  482. * The SIZEOF_PTREGS gap is a convention which helps the in-kernel unwinder
  483. * reliably detect the end of the stack.
  484. */
  485. SYM_DATA(initial_stack,
  486. .long init_thread_union + THREAD_SIZE -
  487. SIZEOF_PTREGS - TOP_OF_KERNEL_STACK_PADDING)
  488. __INITRODATA
  489. int_msg:
  490. .asciz "Unknown interrupt or fault at: %p %p %p\n"
  491. #include "../../x86/xen/xen-head.S"
  492. /*
  493. * The IDT and GDT 'descriptors' are a strange 48-bit object
  494. * only used by the lidt and lgdt instructions. They are not
  495. * like usual segment descriptors - they consist of a 16-bit
  496. * segment size, and 32-bit linear address value:
  497. */
  498. .data
  499. ALIGN
  500. # early boot GDT descriptor (must use 1:1 address mapping)
  501. .word 0 # 32 bit align gdt_desc.address
  502. SYM_DATA_START_LOCAL(boot_gdt_descr)
  503. .word __BOOT_DS+7
  504. .long boot_gdt - __PAGE_OFFSET
  505. SYM_DATA_END(boot_gdt_descr)
  506. # boot GDT descriptor (later on used by CPU#0):
  507. .word 0 # 32 bit align gdt_desc.address
  508. SYM_DATA_START(early_gdt_descr)
  509. .word GDT_ENTRIES*8-1
  510. .long gdt_page /* Overwritten for secondary CPUs */
  511. SYM_DATA_END(early_gdt_descr)
  512. /*
  513. * The boot_gdt must mirror the equivalent in setup.S and is
  514. * used only for booting.
  515. */
  516. .align L1_CACHE_BYTES
  517. SYM_DATA_START(boot_gdt)
  518. .fill GDT_ENTRY_BOOT_CS,8,0
  519. .quad 0x00cf9a000000ffff /* kernel 4GB code at 0x00000000 */
  520. .quad 0x00cf92000000ffff /* kernel 4GB data at 0x00000000 */
  521. SYM_DATA_END(boot_gdt)