ap_tramp.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. .equ AP_INFO_CR0, AP_INFO + 16 /* written back: CR0 as the AP woke up */
  21. .globl ap_tramp_start
  22. ap_tramp_start:
  23. cli
  24. cld
  25. xorw %ax, %ax
  26. movw %ax, %ds
  27. movw %ax, %es
  28. movw %ax, %fs
  29. movw %ax, %gs
  30. movw %ax, %ss
  31. movw $0x6C00, %sp
  32. lgdt ap_gdt_desc
  33. movl %cr0, %eax
  34. orl $1, %eax
  35. movl %eax, %cr0
  36. /* 32-bit far jump into the protected-mode part. */
  37. .byte 0x66, 0xea
  38. .long ap_pm32
  39. .word 0x08
  40. .code32
  41. ap_pm32:
  42. movw $0x10, %ax
  43. movw %ax, %ds
  44. movw %ax, %es
  45. movw %ax, %fs
  46. movw %ax, %gs
  47. movw %ax, %ss
  48. movl AP_INFO_STACK, %esp
  49. movl AP_INFO_CR3, %eax
  50. movl %eax, %cr3
  51. /* Clear stale translation controls left by the firmware (PAE/PGE/
  52. * LA57/PCIDE/SMEP/SMAP) and enable PSE. An AP that starts with
  53. * CR4.PAE set would otherwise walk the kernel page directory as a PAE
  54. * PDPT the moment CR0.PG is set. */
  55. movl %cr4, %eax
  56. andl $0xFFCDEF5F, %eax
  57. orl $0x10, %eax /* CR4.PSE */
  58. movl %eax, %cr4
  59. /* Clear EFER.LME too when long mode exists and the firmware left it
  60. * set; otherwise setting CR0.PG would enter long mode. */
  61. movl $0x80000000, %eax
  62. cpuid
  63. cmpl $0x80000001, %eax
  64. jb 3f
  65. movl $0x80000001, %eax
  66. cpuid
  67. testl $(1 << 29), %edx
  68. jz 3f
  69. movl $0xC0000080, %ecx
  70. rdmsr
  71. testl $(1 << 8), %eax
  72. jz 3f
  73. andl $~0x100, %eax
  74. wrmsr
  75. 3:
  76. /* INIT leaves an AP with CR0 = 0x60000010: CD and NW set, so its
  77. * caches are off until the OS clears them. Nothing did, and every
  78. * ring-3 process the scheduler put on an AP (the browser, its TLS
  79. * helpers) ran from uncached memory, about 100x slower; QEMU does
  80. * not model caches, so only real hardware showed it. The original
  81. * value goes back to the BSP for the boot log. */
  82. movl %cr0, %eax
  83. movl %eax, AP_INFO_CR0
  84. andl $~0x60000000, %eax /* clear CR0.CD | CR0.NW */
  85. orl $0x80010000, %eax /* CR0.PG | CR0.WP */
  86. movl %eax, %cr0
  87. jmp 1f
  88. 1:
  89. movl AP_INFO_CPU, %eax
  90. pushl %eax
  91. movl AP_INFO_ENTRY, %eax
  92. call *%eax
  93. 2: cli
  94. hlt
  95. jmp 2b
  96. .align 8
  97. ap_gdt:
  98. .quad 0
  99. .word 0xFFFF, 0x0000
  100. .byte 0x00, 0x9A, 0xCF, 0x00
  101. .word 0xFFFF, 0x0000
  102. .byte 0x00, 0x92, 0xCF, 0x00
  103. ap_gdt_end:
  104. ap_gdt_desc:
  105. .word ap_gdt_end - ap_gdt - 1
  106. .long ap_gdt