tramp.S 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * UEFI -> 32-bit kernel handoff trampoline.
  3. *
  4. * Linked at 0x800 and copied there by the loader after ExitBootServices.
  5. * Everything internal uses 0x800-based absolute addresses, so the blob is
  6. * position-independent until it is copied to its linked address.
  7. *
  8. * Entered in 64-bit mode with the Microsoft x64 ABI:
  9. * ecx = staging physical address (< 4 GB)
  10. * edx = byte count
  11. * r8d = kernel_entry address
  12. *
  13. * Leaves long mode (compatibility mode, then protected mode), copies the
  14. * staged image to 0x7E00, and jumps to the kernel with paging off.
  15. */
  16. .code64
  17. .section .text
  18. .globl _start
  19. _start:
  20. cli
  21. movl %ecx, param_staging(%rip)
  22. movl %edx, param_size(%rip)
  23. movl %r8d, param_entry(%rip)
  24. lgdt gdt_desc
  25. movl $0x08, %eax
  26. pushq %rax
  27. movl $compat, %edx
  28. pushq %rdx
  29. lretq
  30. .code32
  31. compat:
  32. /* Compatibility mode: 32-bit code, still paging in the firmware's
  33. * 64-bit page tables. Leave IA-32e mode in the documented order. */
  34. movl %cr0, %eax
  35. andl $0x7FFFFFFF, %eax /* CR0.PG = 0 */
  36. movl %eax, %cr0
  37. movl $0xC0000080, %ecx /* IA32_EFER */
  38. rdmsr
  39. andl $0xFFFFFEFF, %eax /* LME = 0 */
  40. wrmsr
  41. movl %cr4, %eax
  42. /* Clear PAE(5), PGE(7), LA57(12), PCIDE(17), SMEP(20), SMAP(21).
  43. * The kernel's page tables are non-PAE and its ring-0 code accesses
  44. * user pages; the firmware's global TLB entries must not survive. */
  45. andl $0xFFCDEF5F, %eax
  46. movl %eax, %cr4
  47. movl %cr0, %eax
  48. andl $0xFFFFFFF3, %eax /* TS = 0, EM = 0 */
  49. movl %eax, %cr0
  50. ljmp $0x08, $copy32
  51. copy32:
  52. movw $0x10, %ax
  53. movw %ax, %ds
  54. movw %ax, %es
  55. movw %ax, %fs
  56. movw %ax, %gs
  57. movw %ax, %ss
  58. movl $0x7C00, %esp
  59. movl param_staging, %esi
  60. movl $0x7E00, %edi
  61. movl param_size, %ecx
  62. cld
  63. rep movsb
  64. wbinvd
  65. movl param_entry, %edx
  66. jmp *%edx
  67. .align 8
  68. gdt:
  69. .quad 0
  70. .word 0xFFFF, 0x0000
  71. .byte 0x00, 0x9A, 0xCF, 0x00 /* 0x08: 32-bit code */
  72. .word 0xFFFF, 0x0000
  73. .byte 0x00, 0x92, 0xCF, 0x00 /* 0x10: data */
  74. gdt_end:
  75. .align 2
  76. gdt_desc:
  77. .word gdt_end - gdt - 1
  78. .quad gdt
  79. .align 4
  80. param_staging:
  81. .long 0
  82. param_size:
  83. .long 0
  84. param_entry:
  85. .long 0