DispatchExecute.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /** @file
  2. Execute 64-bit code in Long Mode.
  3. Provide a thunk function to transition from long mode to compatibility mode to execute 32-bit code and then transit
  4. back to long mode.
  5. Copyright (c) 2014 - 2022, Intel Corporation. All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include <Uefi.h>
  9. #include <Library/BaseLib.h>
  10. #include <FspEas.h>
  11. /**
  12. FSP API functions.
  13. @param[in] Param1 The first parameter to pass to 64bit code.
  14. @param[in] Param2 The second parameter to pass to 64bit code.
  15. @return EFI_STATUS.
  16. **/
  17. typedef
  18. EFI_STATUS
  19. (EFIAPI *FSP_FUNCTION)(
  20. IN VOID *Param1,
  21. IN VOID *Param2
  22. );
  23. #pragma pack(1)
  24. typedef union {
  25. struct {
  26. UINT32 LimitLow : 16;
  27. UINT32 BaseLow : 16;
  28. UINT32 BaseMid : 8;
  29. UINT32 Type : 4;
  30. UINT32 System : 1;
  31. UINT32 Dpl : 2;
  32. UINT32 Present : 1;
  33. UINT32 LimitHigh : 4;
  34. UINT32 Software : 1;
  35. UINT32 Reserved : 1;
  36. UINT32 DefaultSize : 1;
  37. UINT32 Granularity : 1;
  38. UINT32 BaseHigh : 8;
  39. } Bits;
  40. UINT64 Uint64;
  41. } IA32_GDT;
  42. #pragma pack()
  43. GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT mGdtEntries[] = {
  44. {
  45. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  46. }, /* 0x0: reserve */
  47. {
  48. { 0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 0, 1, 1, 0 }
  49. }, /* 0x8: compatibility mode */
  50. {
  51. { 0xFFFF, 0, 0, 0xB, 1, 0, 1, 0xF, 0, 1, 0, 1, 0 }
  52. }, /* 0x10: for long mode */
  53. {
  54. { 0xFFFF, 0, 0, 0x3, 1, 0, 1, 0xF, 0, 0, 1, 1, 0 }
  55. }, /* 0x18: data */
  56. {
  57. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  58. }, /* 0x20: reserve */
  59. };
  60. //
  61. // IA32 Gdt register
  62. //
  63. GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR mGdt = {
  64. sizeof (mGdtEntries) - 1,
  65. (UINTN)mGdtEntries
  66. };
  67. /**
  68. Assembly function to transition from long mode to compatibility mode to execute 32-bit code and then transit back to
  69. long mode.
  70. @param[in] Function The 32bit code entry to be executed.
  71. @param[in] Param1 The first parameter to pass to 32bit code
  72. @param[in] Param2 The second parameter to pass to 32bit code
  73. @param[in] InternalGdtr The GDT and GDT descriptor used by this library
  74. @return status.
  75. **/
  76. UINT32
  77. EFIAPI
  78. AsmExecute32BitCode (
  79. IN UINT64 Function,
  80. IN UINT64 Param1,
  81. IN UINT64 Param2,
  82. IN IA32_DESCRIPTOR *InternalGdtr
  83. );
  84. /**
  85. Wrapper for a thunk to transition from long mode to compatibility mode to execute 32-bit code and then transit back to
  86. long mode.
  87. @param[in] Function The 32bit code entry to be executed.
  88. @param[in] Param1 The first parameter to pass to 32bit code.
  89. @param[in] Param2 The second parameter to pass to 32bit code.
  90. @return EFI_STATUS.
  91. **/
  92. EFI_STATUS
  93. Execute32BitCode (
  94. IN UINT64 Function,
  95. IN UINT64 Param1,
  96. IN UINT64 Param2
  97. )
  98. {
  99. EFI_STATUS Status;
  100. IA32_DESCRIPTOR Idtr;
  101. //
  102. // Idtr might be changed inside of FSP. 32bit FSP only knows the <4G address.
  103. // If IDTR.Base is >4G, FSP can not handle. So we need save/restore IDTR here for X64 only.
  104. // Interrupt is already disabled here, so it is safety to update IDTR.
  105. //
  106. AsmReadIdtr (&Idtr);
  107. Status = AsmExecute32BitCode (Function, Param1, Param2, &mGdt);
  108. //
  109. // Convert FSP Status code from 32bit to 64bit to match caller expectation.
  110. //
  111. Status = (Status & ~(BIT31 + BIT30)) | LShiftU64 (Status & (BIT31 + BIT30), 32);
  112. AsmWriteIdtr (&Idtr);
  113. return Status;
  114. }
  115. /**
  116. Wrapper to execute 64-bit code directly from long mode.
  117. @param[in] Function The 64bit code entry to be executed.
  118. @param[in] Param1 The first parameter to pass to 64bit code.
  119. @param[in] Param2 The second parameter to pass to 64bit code.
  120. @return EFI_STATUS.
  121. **/
  122. EFI_STATUS
  123. Execute64BitCode (
  124. IN UINT64 Function,
  125. IN UINT64 Param1,
  126. IN UINT64 Param2
  127. )
  128. {
  129. FSP_FUNCTION EntryFunc;
  130. EFI_STATUS Status;
  131. EntryFunc = (FSP_FUNCTION)(UINTN)(Function);
  132. Status = EntryFunc ((VOID *)(UINTN)Param1, (VOID *)(UINTN)Param2);
  133. return Status;
  134. }