Thunk.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /** @file
  2. Call into 16-bit BIOS code, Use AsmThunk16 function of BaseLib.
  3. Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
  4. SPDX-License-Identifier: BSD-2-Clause-Patent
  5. **/
  6. #include "LegacyBiosInterface.h"
  7. THUNK_CONTEXT mThunkContext;
  8. /**
  9. Sets the counter value for Timer #0 in a legacy 8254 timer.
  10. @param Count - The 16-bit counter value to program into Timer #0 of the legacy 8254 timer.
  11. **/
  12. VOID
  13. SetPitCount (
  14. IN UINT16 Count
  15. )
  16. {
  17. IoWrite8 (TIMER_CONTROL_PORT, TIMER0_CONTROL_WORD);
  18. IoWrite8 (TIMER0_COUNT_PORT, (UINT8)(Count & 0xFF));
  19. IoWrite8 (TIMER0_COUNT_PORT, (UINT8)((Count>>8) & 0xFF));
  20. }
  21. /**
  22. Thunk to 16-bit real mode and execute a software interrupt with a vector
  23. of BiosInt. Regs will contain the 16-bit register context on entry and
  24. exit.
  25. @param This Protocol instance pointer.
  26. @param BiosInt Processor interrupt vector to invoke
  27. @param Regs Register contexted passed into (and returned) from thunk to
  28. 16-bit mode
  29. @retval FALSE Thunk completed, and there were no BIOS errors in the target code.
  30. See Regs for status.
  31. @retval TRUE There was a BIOS erro in the target code.
  32. **/
  33. BOOLEAN
  34. EFIAPI
  35. LegacyBiosInt86 (
  36. IN EFI_LEGACY_BIOS_PROTOCOL *This,
  37. IN UINT8 BiosInt,
  38. IN EFI_IA32_REGISTER_SET *Regs
  39. )
  40. {
  41. UINT16 Segment;
  42. UINT16 Offset;
  43. Regs->X.Flags.Reserved1 = 1;
  44. Regs->X.Flags.Reserved2 = 0;
  45. Regs->X.Flags.Reserved3 = 0;
  46. Regs->X.Flags.Reserved4 = 0;
  47. Regs->X.Flags.IOPL = 3;
  48. Regs->X.Flags.NT = 0;
  49. Regs->X.Flags.IF = 0;
  50. Regs->X.Flags.TF = 0;
  51. Regs->X.Flags.CF = 0;
  52. //
  53. // The base address of legacy interrupt vector table is 0.
  54. // We use this base address to get the legacy interrupt handler.
  55. //
  56. ACCESS_PAGE0_CODE (
  57. Segment = (UINT16)(((UINT32 *)0)[BiosInt] >> 16);
  58. Offset = (UINT16)((UINT32 *)0)[BiosInt];
  59. );
  60. return InternalLegacyBiosFarCall (
  61. This,
  62. Segment,
  63. Offset,
  64. Regs,
  65. &Regs->X.Flags,
  66. sizeof (Regs->X.Flags)
  67. );
  68. }
  69. /**
  70. Thunk to 16-bit real mode and call Segment:Offset. Regs will contain the
  71. 16-bit register context on entry and exit. Arguments can be passed on
  72. the Stack argument
  73. @param This Protocol instance pointer.
  74. @param Segment Segemnt of 16-bit mode call
  75. @param Offset Offset of 16-bit mdoe call
  76. @param Regs Register contexted passed into (and returned) from
  77. thunk to 16-bit mode
  78. @param Stack Caller allocated stack used to pass arguments
  79. @param StackSize Size of Stack in bytes
  80. @retval FALSE Thunk completed, and there were no BIOS errors in
  81. the target code. See Regs for status.
  82. @retval TRUE There was a BIOS erro in the target code.
  83. **/
  84. BOOLEAN
  85. EFIAPI
  86. LegacyBiosFarCall86 (
  87. IN EFI_LEGACY_BIOS_PROTOCOL *This,
  88. IN UINT16 Segment,
  89. IN UINT16 Offset,
  90. IN EFI_IA32_REGISTER_SET *Regs,
  91. IN VOID *Stack,
  92. IN UINTN StackSize
  93. )
  94. {
  95. Regs->X.Flags.Reserved1 = 1;
  96. Regs->X.Flags.Reserved2 = 0;
  97. Regs->X.Flags.Reserved3 = 0;
  98. Regs->X.Flags.Reserved4 = 0;
  99. Regs->X.Flags.IOPL = 3;
  100. Regs->X.Flags.NT = 0;
  101. Regs->X.Flags.IF = 1;
  102. Regs->X.Flags.TF = 0;
  103. Regs->X.Flags.CF = 0;
  104. return InternalLegacyBiosFarCall (This, Segment, Offset, Regs, Stack, StackSize);
  105. }
  106. /**
  107. Provide NULL interrupt handler which is used to check
  108. if there is more than one HW interrupt registers with the CPU AP.
  109. @param InterruptType - The type of interrupt that occurred
  110. @param SystemContext - A pointer to the system context when the interrupt occurred
  111. **/
  112. VOID
  113. EFIAPI
  114. LegacyBiosNullInterruptHandler (
  115. IN EFI_EXCEPTION_TYPE InterruptType,
  116. IN EFI_SYSTEM_CONTEXT SystemContext
  117. )
  118. {
  119. }
  120. /**
  121. Thunk to 16-bit real mode and call Segment:Offset. Regs will contain the
  122. 16-bit register context on entry and exit. Arguments can be passed on
  123. the Stack argument
  124. @param This Protocol instance pointer.
  125. @param Segment Segemnt of 16-bit mode call
  126. @param Offset Offset of 16-bit mdoe call
  127. @param Regs Register contexted passed into (and returned) from thunk to
  128. 16-bit mode
  129. @param Stack Caller allocated stack used to pass arguments
  130. @param StackSize Size of Stack in bytes
  131. @retval FALSE Thunk completed, and there were no BIOS errors in the target code.
  132. See Regs for status.
  133. @retval TRUE There was a BIOS erro in the target code.
  134. **/
  135. BOOLEAN
  136. EFIAPI
  137. InternalLegacyBiosFarCall (
  138. IN EFI_LEGACY_BIOS_PROTOCOL *This,
  139. IN UINT16 Segment,
  140. IN UINT16 Offset,
  141. IN EFI_IA32_REGISTER_SET *Regs,
  142. IN VOID *Stack,
  143. IN UINTN StackSize
  144. )
  145. {
  146. UINTN Status;
  147. LEGACY_BIOS_INSTANCE *Private;
  148. UINT16 *Stack16;
  149. EFI_TPL OriginalTpl;
  150. IA32_REGISTER_SET ThunkRegSet;
  151. BOOLEAN InterruptState;
  152. UINT64 TimerPeriod;
  153. Private = LEGACY_BIOS_INSTANCE_FROM_THIS (This);
  154. ZeroMem (&ThunkRegSet, sizeof (ThunkRegSet));
  155. ThunkRegSet.X.DI = Regs->X.DI;
  156. ThunkRegSet.X.SI = Regs->X.SI;
  157. ThunkRegSet.X.BP = Regs->X.BP;
  158. ThunkRegSet.X.BX = Regs->X.BX;
  159. ThunkRegSet.X.DX = Regs->X.DX;
  160. //
  161. // Sometimes, ECX is used to pass in 32 bit data. For example, INT 1Ah, AX = B10Dh is
  162. // "PCI BIOS v2.0c + Write Configuration DWORD" and ECX has the dword to write.
  163. //
  164. ThunkRegSet.E.ECX = Regs->E.ECX;
  165. ThunkRegSet.X.AX = Regs->X.AX;
  166. ThunkRegSet.E.DS = Regs->X.DS;
  167. ThunkRegSet.E.ES = Regs->X.ES;
  168. CopyMem (&(ThunkRegSet.E.EFLAGS.UintN), &(Regs->X.Flags), sizeof (Regs->X.Flags));
  169. //
  170. // Clear the error flag; thunk code may set it. Stack16 should be the high address
  171. // Make Statk16 address the low 16 bit must be not zero.
  172. //
  173. Stack16 = (UINT16 *)((UINT8 *)mThunkContext.RealModeBuffer + mThunkContext.RealModeBufferSize - sizeof (UINT16));
  174. //
  175. // Save current rate of DXE Timer
  176. //
  177. Private->Timer->GetTimerPeriod (Private->Timer, &TimerPeriod);
  178. //
  179. // Disable DXE Timer while executing in real mode
  180. //
  181. Private->Timer->SetTimerPeriod (Private->Timer, 0);
  182. //
  183. // Save and disable interrupt of debug timer
  184. //
  185. InterruptState = SaveAndSetDebugTimerInterrupt (FALSE);
  186. //
  187. // The call to Legacy16 is a critical section to EFI
  188. //
  189. OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);
  190. //
  191. // Check to see if there is more than one HW interrupt registers with the CPU AP.
  192. // If there is, then ASSERT() since that is not compatible with the CSM because
  193. // interupts other than the Timer interrupt that was disabled above can not be
  194. // handled properly from real mode.
  195. //
  196. DEBUG_CODE_BEGIN ();
  197. UINTN Vector;
  198. UINTN Count;
  199. for (Vector = 0x20, Count = 0; Vector < 0x100; Vector++) {
  200. Status = Private->Cpu->RegisterInterruptHandler (Private->Cpu, Vector, LegacyBiosNullInterruptHandler);
  201. if (Status == EFI_ALREADY_STARTED) {
  202. Count++;
  203. }
  204. if (Status == EFI_SUCCESS) {
  205. Private->Cpu->RegisterInterruptHandler (Private->Cpu, Vector, NULL);
  206. }
  207. }
  208. if (Count >= 2) {
  209. DEBUG ((DEBUG_ERROR, "ERROR: More than one HW interrupt active with CSM enabled\n"));
  210. }
  211. ASSERT (Count < 2);
  212. DEBUG_CODE_END ();
  213. //
  214. // If the Timer AP has enabled the 8254 timer IRQ and the current 8254 timer
  215. // period is less than the CSM required rate of 54.9254, then force the 8254
  216. // PIT counter to 0, which is the CSM required rate of 54.9254 ms
  217. //
  218. if (Private->TimerUses8254 && (TimerPeriod < 549254)) {
  219. SetPitCount (0);
  220. }
  221. if ((Stack != NULL) && (StackSize != 0)) {
  222. //
  223. // Copy Stack to low memory stack
  224. //
  225. Stack16 -= StackSize / sizeof (UINT16);
  226. CopyMem (Stack16, Stack, StackSize);
  227. }
  228. ThunkRegSet.E.SS = (UINT16)(((UINTN)Stack16 >> 16) << 12);
  229. ThunkRegSet.E.ESP = (UINT16)(UINTN)Stack16;
  230. ThunkRegSet.E.CS = Segment;
  231. ThunkRegSet.E.Eip = Offset;
  232. mThunkContext.RealModeState = &ThunkRegSet;
  233. //
  234. // Set Legacy16 state. 0x08, 0x70 is legacy 8259 vector bases.
  235. //
  236. Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259LegacyMode, NULL, NULL);
  237. ASSERT_EFI_ERROR (Status);
  238. AsmThunk16 (&mThunkContext);
  239. if ((Stack != NULL) && (StackSize != 0)) {
  240. //
  241. // Copy low memory stack to Stack
  242. //
  243. CopyMem (Stack, Stack16, StackSize);
  244. }
  245. //
  246. // Restore protected mode interrupt state
  247. //
  248. Status = Private->Legacy8259->SetMode (Private->Legacy8259, Efi8259ProtectedMode, NULL, NULL);
  249. ASSERT_EFI_ERROR (Status);
  250. mThunkContext.RealModeState = NULL;
  251. //
  252. // Enable and restore rate of DXE Timer
  253. //
  254. Private->Timer->SetTimerPeriod (Private->Timer, TimerPeriod);
  255. //
  256. // End critical section
  257. //
  258. gBS->RestoreTPL (OriginalTpl);
  259. //
  260. // OPROM may allocate EBDA range by itself and change EBDA base and EBDA size.
  261. // Get the current EBDA base address, and compared with pre-allocate minimum
  262. // EBDA base address, if the current EBDA base address is smaller, it indicates
  263. // PcdEbdaReservedMemorySize should be adjusted to larger for more OPROMs.
  264. //
  265. DEBUG_CODE_BEGIN ();
  266. {
  267. UINTN EbdaBaseAddress;
  268. UINTN ReservedEbdaBaseAddress;
  269. ACCESS_PAGE0_CODE (
  270. EbdaBaseAddress = (*(UINT16 *)(UINTN)0x40E) << 4;
  271. ReservedEbdaBaseAddress = CONVENTIONAL_MEMORY_TOP
  272. - PcdGet32 (PcdEbdaReservedMemorySize);
  273. ASSERT (ReservedEbdaBaseAddress <= EbdaBaseAddress);
  274. );
  275. }
  276. DEBUG_CODE_END ();
  277. //
  278. // Restore interrupt of debug timer
  279. //
  280. SaveAndSetDebugTimerInterrupt (InterruptState);
  281. Regs->E.EDI = ThunkRegSet.E.EDI;
  282. Regs->E.ESI = ThunkRegSet.E.ESI;
  283. Regs->E.EBP = ThunkRegSet.E.EBP;
  284. Regs->E.EBX = ThunkRegSet.E.EBX;
  285. Regs->E.EDX = ThunkRegSet.E.EDX;
  286. Regs->E.ECX = ThunkRegSet.E.ECX;
  287. Regs->E.EAX = ThunkRegSet.E.EAX;
  288. Regs->X.SS = ThunkRegSet.E.SS;
  289. Regs->X.CS = ThunkRegSet.E.CS;
  290. Regs->X.DS = ThunkRegSet.E.DS;
  291. Regs->X.ES = ThunkRegSet.E.ES;
  292. CopyMem (&(Regs->X.Flags), &(ThunkRegSet.E.EFLAGS.UintN), sizeof (Regs->X.Flags));
  293. return (BOOLEAN)(Regs->X.Flags.CF == 1);
  294. }
  295. /**
  296. Allocate memory < 1 MB and copy the thunker code into low memory. Se up
  297. all the descriptors.
  298. @param Private Private context for Legacy BIOS
  299. @retval EFI_SUCCESS Should only pass.
  300. **/
  301. EFI_STATUS
  302. LegacyBiosInitializeThunk (
  303. IN LEGACY_BIOS_INSTANCE *Private
  304. )
  305. {
  306. EFI_STATUS Status;
  307. EFI_PHYSICAL_ADDRESS MemoryAddress;
  308. UINT8 TimerVector;
  309. MemoryAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)Private->IntThunk;
  310. mThunkContext.RealModeBuffer = (VOID *)(UINTN)(MemoryAddress + ((sizeof (LOW_MEMORY_THUNK) / EFI_PAGE_SIZE) + 1) * EFI_PAGE_SIZE);
  311. mThunkContext.RealModeBufferSize = EFI_PAGE_SIZE;
  312. mThunkContext.ThunkAttributes = THUNK_ATTRIBUTE_BIG_REAL_MODE | THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15;
  313. AsmPrepareThunk16 (&mThunkContext);
  314. //
  315. // Get the interrupt vector number corresponding to IRQ0 from the 8259 driver
  316. //
  317. TimerVector = 0;
  318. Status = Private->Legacy8259->GetVector (Private->Legacy8259, Efi8259Irq0, &TimerVector);
  319. ASSERT_EFI_ERROR (Status);
  320. //
  321. // Check to see if the Timer AP has hooked the IRQ0 from the 8254 PIT
  322. //
  323. Status = Private->Cpu->RegisterInterruptHandler (
  324. Private->Cpu,
  325. TimerVector,
  326. LegacyBiosNullInterruptHandler
  327. );
  328. if (Status == EFI_SUCCESS) {
  329. //
  330. // If the Timer AP has not enabled the 8254 timer IRQ, then force the 8254 PIT
  331. // counter to 0, which is the CSM required rate of 54.9254 ms
  332. //
  333. Private->Cpu->RegisterInterruptHandler (
  334. Private->Cpu,
  335. TimerVector,
  336. NULL
  337. );
  338. SetPitCount (0);
  339. //
  340. // Save status that the Timer AP is not using the 8254 PIT
  341. //
  342. Private->TimerUses8254 = FALSE;
  343. } else if (Status == EFI_ALREADY_STARTED) {
  344. //
  345. // Save status that the Timer AP is using the 8254 PIT
  346. //
  347. Private->TimerUses8254 = TRUE;
  348. } else {
  349. //
  350. // Unexpected status from CPU AP RegisterInterruptHandler()
  351. //
  352. ASSERT (FALSE);
  353. }
  354. return EFI_SUCCESS;
  355. }