efi_thunk_64.S 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2014 Intel Corporation; author Matt Fleming
  3. *
  4. * Support for invoking 32-bit EFI runtime services from a 64-bit
  5. * kernel.
  6. *
  7. * The below thunking functions are only used after ExitBootServices()
  8. * has been called. This simplifies things considerably as compared with
  9. * the early EFI thunking because we can leave all the kernel state
  10. * intact (GDT, IDT, etc) and simply invoke the the 32-bit EFI runtime
  11. * services from __KERNEL32_CS. This means we can continue to service
  12. * interrupts across an EFI mixed mode call.
  13. *
  14. * We do however, need to handle the fact that we're running in a full
  15. * 64-bit virtual address space. Things like the stack and instruction
  16. * addresses need to be accessible by the 32-bit firmware, so we rely on
  17. * using the identity mappings in the EFI page table to access the stack
  18. * and kernel text (see efi_setup_page_tables()).
  19. */
  20. #include <linux/linkage.h>
  21. #include <asm/page_types.h>
  22. #include <asm/segment.h>
  23. .text
  24. .code64
  25. ENTRY(efi64_thunk)
  26. push %rbp
  27. push %rbx
  28. /*
  29. * Switch to 1:1 mapped 32-bit stack pointer.
  30. */
  31. movq %rsp, efi_saved_sp(%rip)
  32. movq efi_scratch+25(%rip), %rsp
  33. /*
  34. * Calculate the physical address of the kernel text.
  35. */
  36. movq $__START_KERNEL_map, %rax
  37. subq phys_base(%rip), %rax
  38. /*
  39. * Push some physical addresses onto the stack. This is easier
  40. * to do now in a code64 section while the assembler can address
  41. * 64-bit values. Note that all the addresses on the stack are
  42. * 32-bit.
  43. */
  44. subq $16, %rsp
  45. leaq efi_exit32(%rip), %rbx
  46. subq %rax, %rbx
  47. movl %ebx, 8(%rsp)
  48. leaq __efi64_thunk(%rip), %rbx
  49. subq %rax, %rbx
  50. call *%rbx
  51. movq efi_saved_sp(%rip), %rsp
  52. pop %rbx
  53. pop %rbp
  54. retq
  55. ENDPROC(efi64_thunk)
  56. /*
  57. * We run this function from the 1:1 mapping.
  58. *
  59. * This function must be invoked with a 1:1 mapped stack.
  60. */
  61. ENTRY(__efi64_thunk)
  62. movl %ds, %eax
  63. push %rax
  64. movl %es, %eax
  65. push %rax
  66. movl %ss, %eax
  67. push %rax
  68. subq $32, %rsp
  69. movl %esi, 0x0(%rsp)
  70. movl %edx, 0x4(%rsp)
  71. movl %ecx, 0x8(%rsp)
  72. movq %r8, %rsi
  73. movl %esi, 0xc(%rsp)
  74. movq %r9, %rsi
  75. movl %esi, 0x10(%rsp)
  76. leaq 1f(%rip), %rbx
  77. movq %rbx, func_rt_ptr(%rip)
  78. /* Switch to 32-bit descriptor */
  79. pushq $__KERNEL32_CS
  80. leaq efi_enter32(%rip), %rax
  81. pushq %rax
  82. lretq
  83. 1: addq $32, %rsp
  84. pop %rbx
  85. movl %ebx, %ss
  86. pop %rbx
  87. movl %ebx, %es
  88. pop %rbx
  89. movl %ebx, %ds
  90. /*
  91. * Convert 32-bit status code into 64-bit.
  92. */
  93. test %rax, %rax
  94. jz 1f
  95. movl %eax, %ecx
  96. andl $0x0fffffff, %ecx
  97. andl $0xf0000000, %eax
  98. shl $32, %rax
  99. or %rcx, %rax
  100. 1:
  101. ret
  102. ENDPROC(__efi64_thunk)
  103. ENTRY(efi_exit32)
  104. movq func_rt_ptr(%rip), %rax
  105. push %rax
  106. mov %rdi, %rax
  107. ret
  108. ENDPROC(efi_exit32)
  109. .code32
  110. /*
  111. * EFI service pointer must be in %edi.
  112. *
  113. * The stack should represent the 32-bit calling convention.
  114. */
  115. ENTRY(efi_enter32)
  116. movl $__KERNEL_DS, %eax
  117. movl %eax, %ds
  118. movl %eax, %es
  119. movl %eax, %ss
  120. call *%edi
  121. /* We must preserve return value */
  122. movl %eax, %edi
  123. movl 72(%esp), %eax
  124. pushl $__KERNEL_CS
  125. pushl %eax
  126. lret
  127. ENDPROC(efi_enter32)
  128. .data
  129. .balign 8
  130. func_rt_ptr: .quad 0
  131. efi_saved_sp: .quad 0