ap_tramp.S 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * AP startup trampoline, linked at 0x6000 and copied there by the BSP.
  3. *
  4. * The BSP fills the fixed info block at 0x6F00 before sending SIPI:
  5. * 0x6F00 cr3 (physical kernel page directory)
  6. * 0x6F04 stack_top (AP idle stack top, identity mapped)
  7. * 0x6F08 entry (C entry point, protected-mode address)
  8. * 0x6F0C cpu_index
  9. *
  10. * The AP starts in real mode at 0x6000 (SIPI vector 0x06), builds a tiny flat
  11. * GDT, enters protected mode, enables PSE + paging and jumps to the C entry.
  12. */
  13. .code16
  14. .section .text.ap, "ax"
  15. .equ AP_INFO, 0x6F00
  16. .equ AP_INFO_CR3, AP_INFO + 0
  17. .equ AP_INFO_STACK, AP_INFO + 4
  18. .equ AP_INFO_ENTRY, AP_INFO + 8
  19. .equ AP_INFO_CPU, AP_INFO + 12
  20. .globl ap_tramp_start
  21. ap_tramp_start:
  22. cli
  23. cld
  24. xorw %ax, %ax
  25. movw %ax, %ds
  26. movw %ax, %es
  27. movw %ax, %fs
  28. movw %ax, %gs
  29. movw %ax, %ss
  30. movw $0x6C00, %sp
  31. lgdt ap_gdt_desc
  32. movl %cr0, %eax
  33. orl $1, %eax
  34. movl %eax, %cr0
  35. /* 32-bit far jump into the protected-mode part. */
  36. .byte 0x66, 0xea
  37. .long ap_pm32
  38. .word 0x08
  39. .code32
  40. ap_pm32:
  41. movw $0x10, %ax
  42. movw %ax, %ds
  43. movw %ax, %es
  44. movw %ax, %fs
  45. movw %ax, %gs
  46. movw %ax, %ss
  47. movl AP_INFO_STACK, %esp
  48. movl AP_INFO_CR3, %eax
  49. movl %eax, %cr3
  50. /* Clear stale translation controls left by the firmware (PAE/PGE/
  51. * LA57/PCIDE/SMEP/SMAP) and enable PSE. An AP that starts with
  52. * CR4.PAE set would otherwise walk the kernel page directory as a PAE
  53. * PDPT the moment CR0.PG is set. */
  54. movl %cr4, %eax
  55. andl $0xFFCDEF5F, %eax
  56. orl $0x10, %eax /* CR4.PSE */
  57. movl %eax, %cr4
  58. /* Clear EFER.LME too when long mode exists and the firmware left it
  59. * set; otherwise setting CR0.PG would enter long mode. */
  60. movl $0x80000000, %eax
  61. cpuid
  62. cmpl $0x80000001, %eax
  63. jb 3f
  64. movl $0x80000001, %eax
  65. cpuid
  66. testl $(1 << 29), %edx
  67. jz 3f
  68. movl $0xC0000080, %ecx
  69. rdmsr
  70. testl $(1 << 8), %eax
  71. jz 3f
  72. andl $~0x100, %eax
  73. wrmsr
  74. 3:
  75. movl %cr0, %eax
  76. orl $0x80010000, %eax /* CR0.PG | CR0.WP */
  77. movl %eax, %cr0
  78. jmp 1f
  79. 1:
  80. movl AP_INFO_CPU, %eax
  81. pushl %eax
  82. movl AP_INFO_ENTRY, %eax
  83. call *%eax
  84. 2: cli
  85. hlt
  86. jmp 2b
  87. .align 8
  88. ap_gdt:
  89. .quad 0
  90. .word 0xFFFF, 0x0000
  91. .byte 0x00, 0x9A, 0xCF, 0x00
  92. .word 0xFFFF, 0x0000
  93. .byte 0x00, 0x92, 0xCF, 0x00
  94. ap_gdt_end:
  95. ap_gdt_desc:
  96. .word ap_gdt_end - ap_gdt - 1
  97. .long ap_gdt