2 Call into 16-bit BIOS code, Use AsmThunk16 function of BaseLib.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions
8 of the BSD License which accompanies this distribution. The
9 full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 #include "LegacyBiosInterface.h"
19 THUNK_CONTEXT mThunkContext
;
22 Sets the counter value for Timer #0 in a legacy 8254 timer.
24 @param Count - The 16-bit counter value to program into Timer #0 of the legacy 8254 timer.
32 IoWrite8 (TIMER_CONTROL_PORT
, TIMER0_CONTROL_WORD
);
33 IoWrite8 (TIMER0_COUNT_PORT
, (UINT8
) (Count
& 0xFF));
34 IoWrite8 (TIMER0_COUNT_PORT
, (UINT8
) ((Count
>>8) & 0xFF));
38 Thunk to 16-bit real mode and execute a software interrupt with a vector
39 of BiosInt. Regs will contain the 16-bit register context on entry and
42 @param This Protocol instance pointer.
43 @param BiosInt Processor interrupt vector to invoke
44 @param Regs Register contexted passed into (and returned) from thunk to
47 @retval FALSE Thunk completed, and there were no BIOS errors in the target code.
49 @retval TRUE There was a BIOS erro in the target code.
55 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
57 IN EFI_IA32_REGISTER_SET
*Regs
63 Regs
->X
.Flags
.Reserved1
= 1;
64 Regs
->X
.Flags
.Reserved2
= 0;
65 Regs
->X
.Flags
.Reserved3
= 0;
66 Regs
->X
.Flags
.Reserved4
= 0;
67 Regs
->X
.Flags
.IOPL
= 3;
73 // The base address of legacy interrupt vector table is 0.
74 // We use this base address to get the legacy interrupt handler.
77 Segment
= (UINT16
)(((UINT32
*)0)[BiosInt
] >> 16);
78 Offset
= (UINT16
)((UINT32
*)0)[BiosInt
];
81 return InternalLegacyBiosFarCall (
87 sizeof (Regs
->X
.Flags
)
92 Thunk to 16-bit real mode and call Segment:Offset. Regs will contain the
93 16-bit register context on entry and exit. Arguments can be passed on
96 @param This Protocol instance pointer.
97 @param Segment Segemnt of 16-bit mode call
98 @param Offset Offset of 16-bit mdoe call
99 @param Regs Register contexted passed into (and returned) from
101 @param Stack Caller allocated stack used to pass arguments
102 @param StackSize Size of Stack in bytes
104 @retval FALSE Thunk completed, and there were no BIOS errors in
105 the target code. See Regs for status.
106 @retval TRUE There was a BIOS erro in the target code.
111 LegacyBiosFarCall86 (
112 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
115 IN EFI_IA32_REGISTER_SET
*Regs
,
120 Regs
->X
.Flags
.Reserved1
= 1;
121 Regs
->X
.Flags
.Reserved2
= 0;
122 Regs
->X
.Flags
.Reserved3
= 0;
123 Regs
->X
.Flags
.Reserved4
= 0;
124 Regs
->X
.Flags
.IOPL
= 3;
125 Regs
->X
.Flags
.NT
= 0;
126 Regs
->X
.Flags
.IF
= 1;
127 Regs
->X
.Flags
.TF
= 0;
128 Regs
->X
.Flags
.CF
= 0;
130 return InternalLegacyBiosFarCall (This
, Segment
, Offset
, Regs
, Stack
, StackSize
);
134 Provide NULL interrupt handler which is used to check
135 if there is more than one HW interrupt registers with the CPU AP.
137 @param InterruptType - The type of interrupt that occured
138 @param SystemContext - A pointer to the system context when the interrupt occured
143 LegacyBiosNullInterruptHandler (
144 IN EFI_EXCEPTION_TYPE InterruptType
,
145 IN EFI_SYSTEM_CONTEXT SystemContext
151 Thunk to 16-bit real mode and call Segment:Offset. Regs will contain the
152 16-bit register context on entry and exit. Arguments can be passed on
155 @param This Protocol instance pointer.
156 @param Segment Segemnt of 16-bit mode call
157 @param Offset Offset of 16-bit mdoe call
158 @param Regs Register contexted passed into (and returned) from thunk to
160 @param Stack Caller allocated stack used to pass arguments
161 @param StackSize Size of Stack in bytes
163 @retval FALSE Thunk completed, and there were no BIOS errors in the target code.
165 @retval TRUE There was a BIOS erro in the target code.
170 InternalLegacyBiosFarCall (
171 IN EFI_LEGACY_BIOS_PROTOCOL
*This
,
174 IN EFI_IA32_REGISTER_SET
*Regs
,
180 LEGACY_BIOS_INSTANCE
*Private
;
183 IA32_REGISTER_SET ThunkRegSet
;
184 BOOLEAN InterruptState
;
187 Private
= LEGACY_BIOS_INSTANCE_FROM_THIS (This
);
189 ZeroMem (&ThunkRegSet
, sizeof (ThunkRegSet
));
190 ThunkRegSet
.X
.DI
= Regs
->X
.DI
;
191 ThunkRegSet
.X
.SI
= Regs
->X
.SI
;
192 ThunkRegSet
.X
.BP
= Regs
->X
.BP
;
193 ThunkRegSet
.X
.BX
= Regs
->X
.BX
;
194 ThunkRegSet
.X
.DX
= Regs
->X
.DX
;
196 // Sometimes, ECX is used to pass in 32 bit data. For example, INT 1Ah, AX = B10Dh is
197 // "PCI BIOS v2.0c + Write Configuration DWORD" and ECX has the dword to write.
199 ThunkRegSet
.E
.ECX
= Regs
->E
.ECX
;
200 ThunkRegSet
.X
.AX
= Regs
->X
.AX
;
201 ThunkRegSet
.E
.DS
= Regs
->X
.DS
;
202 ThunkRegSet
.E
.ES
= Regs
->X
.ES
;
204 CopyMem (&(ThunkRegSet
.E
.EFLAGS
.UintN
), &(Regs
->X
.Flags
), sizeof (Regs
->X
.Flags
));
207 // Clear the error flag; thunk code may set it. Stack16 should be the high address
208 // Make Statk16 address the low 16 bit must be not zero.
210 Stack16
= (UINT16
*)((UINT8
*) mThunkContext
.RealModeBuffer
+ mThunkContext
.RealModeBufferSize
- sizeof (UINT16
));
213 // Save current rate of DXE Timer
215 Private
->Timer
->GetTimerPeriod (Private
->Timer
, &TimerPeriod
);
218 // Disable DXE Timer while executing in real mode
220 Private
->Timer
->SetTimerPeriod (Private
->Timer
, 0);
223 // Save and disable interrupt of debug timer
225 InterruptState
= SaveAndSetDebugTimerInterrupt (FALSE
);
228 // The call to Legacy16 is a critical section to EFI
230 OriginalTpl
= gBS
->RaiseTPL (TPL_HIGH_LEVEL
);
233 // Check to see if there is more than one HW interrupt registers with the CPU AP.
234 // If there is, then ASSERT() since that is not compatible with the CSM because
235 // interupts other than the Timer interrupt that was disabled above can not be
236 // handled properly from real mode.
242 for (Vector
= 0x20, Count
= 0; Vector
< 0x100; Vector
++) {
243 Status
= Private
->Cpu
->RegisterInterruptHandler (Private
->Cpu
, Vector
, LegacyBiosNullInterruptHandler
);
244 if (Status
== EFI_ALREADY_STARTED
) {
247 if (Status
== EFI_SUCCESS
) {
248 Private
->Cpu
->RegisterInterruptHandler (Private
->Cpu
, Vector
, NULL
);
252 DEBUG ((EFI_D_ERROR
, "ERROR: More than one HW interrupt active with CSM enabled\n"));
258 // If the Timer AP has enabled the 8254 timer IRQ and the current 8254 timer
259 // period is less than the CSM required rate of 54.9254, then force the 8254
260 // PIT counter to 0, which is the CSM required rate of 54.9254 ms
262 if (Private
->TimerUses8254
&& TimerPeriod
< 549254) {
266 if (Stack
!= NULL
&& StackSize
!= 0) {
268 // Copy Stack to low memory stack
270 Stack16
-= StackSize
/ sizeof (UINT16
);
271 CopyMem (Stack16
, Stack
, StackSize
);
274 ThunkRegSet
.E
.SS
= (UINT16
) (((UINTN
) Stack16
>> 16) << 12);
275 ThunkRegSet
.E
.ESP
= (UINT16
) (UINTN
) Stack16
;
276 ThunkRegSet
.E
.CS
= Segment
;
277 ThunkRegSet
.E
.Eip
= Offset
;
279 mThunkContext
.RealModeState
= &ThunkRegSet
;
282 // Set Legacy16 state. 0x08, 0x70 is legacy 8259 vector bases.
284 Status
= Private
->Legacy8259
->SetMode (Private
->Legacy8259
, Efi8259LegacyMode
, NULL
, NULL
);
285 ASSERT_EFI_ERROR (Status
);
287 AsmThunk16 (&mThunkContext
);
289 if (Stack
!= NULL
&& StackSize
!= 0) {
291 // Copy low memory stack to Stack
293 CopyMem (Stack
, Stack16
, StackSize
);
297 // Restore protected mode interrupt state
299 Status
= Private
->Legacy8259
->SetMode (Private
->Legacy8259
, Efi8259ProtectedMode
, NULL
, NULL
);
300 ASSERT_EFI_ERROR (Status
);
302 mThunkContext
.RealModeState
= NULL
;
305 // Enable and restore rate of DXE Timer
307 Private
->Timer
->SetTimerPeriod (Private
->Timer
, TimerPeriod
);
310 // End critical section
312 gBS
->RestoreTPL (OriginalTpl
);
315 // OPROM may allocate EBDA range by itself and change EBDA base and EBDA size.
316 // Get the current EBDA base address, and compared with pre-allocate minimum
317 // EBDA base address, if the current EBDA base address is smaller, it indicates
318 // PcdEbdaReservedMemorySize should be adjusted to larger for more OPROMs.
322 UINTN EbdaBaseAddress
;
323 UINTN ReservedEbdaBaseAddress
;
326 EbdaBaseAddress
= (*(UINT16
*) (UINTN
) 0x40E) << 4;
327 ReservedEbdaBaseAddress
= CONVENTIONAL_MEMORY_TOP
328 - PcdGet32 (PcdEbdaReservedMemorySize
);
329 ASSERT (ReservedEbdaBaseAddress
<= EbdaBaseAddress
);
335 // Restore interrupt of debug timer
337 SaveAndSetDebugTimerInterrupt (InterruptState
);
339 Regs
->E
.EDI
= ThunkRegSet
.E
.EDI
;
340 Regs
->E
.ESI
= ThunkRegSet
.E
.ESI
;
341 Regs
->E
.EBP
= ThunkRegSet
.E
.EBP
;
342 Regs
->E
.EBX
= ThunkRegSet
.E
.EBX
;
343 Regs
->E
.EDX
= ThunkRegSet
.E
.EDX
;
344 Regs
->E
.ECX
= ThunkRegSet
.E
.ECX
;
345 Regs
->E
.EAX
= ThunkRegSet
.E
.EAX
;
346 Regs
->X
.SS
= ThunkRegSet
.E
.SS
;
347 Regs
->X
.CS
= ThunkRegSet
.E
.CS
;
348 Regs
->X
.DS
= ThunkRegSet
.E
.DS
;
349 Regs
->X
.ES
= ThunkRegSet
.E
.ES
;
351 CopyMem (&(Regs
->X
.Flags
), &(ThunkRegSet
.E
.EFLAGS
.UintN
), sizeof (Regs
->X
.Flags
));
353 return (BOOLEAN
) (Regs
->X
.Flags
.CF
== 1);
357 Allocate memory < 1 MB and copy the thunker code into low memory. Se up
360 @param Private Private context for Legacy BIOS
362 @retval EFI_SUCCESS Should only pass.
366 LegacyBiosInitializeThunk (
367 IN LEGACY_BIOS_INSTANCE
*Private
371 EFI_PHYSICAL_ADDRESS MemoryAddress
;
374 MemoryAddress
= (EFI_PHYSICAL_ADDRESS
) (UINTN
) Private
->IntThunk
;
376 mThunkContext
.RealModeBuffer
= (VOID
*) (UINTN
) (MemoryAddress
+ ((sizeof (LOW_MEMORY_THUNK
) / EFI_PAGE_SIZE
) + 1) * EFI_PAGE_SIZE
);
377 mThunkContext
.RealModeBufferSize
= EFI_PAGE_SIZE
;
378 mThunkContext
.ThunkAttributes
= THUNK_ATTRIBUTE_BIG_REAL_MODE
| THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
;
380 AsmPrepareThunk16 (&mThunkContext
);
383 // Get the interrupt vector number corresponding to IRQ0 from the 8259 driver
386 Status
= Private
->Legacy8259
->GetVector (Private
->Legacy8259
, Efi8259Irq0
, &TimerVector
);
387 ASSERT_EFI_ERROR (Status
);
390 // Check to see if the Timer AP has hooked the IRQ0 from the 8254 PIT
392 Status
= Private
->Cpu
->RegisterInterruptHandler (
395 LegacyBiosNullInterruptHandler
397 if (Status
== EFI_SUCCESS
) {
399 // If the Timer AP has not enabled the 8254 timer IRQ, then force the 8254 PIT
400 // counter to 0, which is the CSM required rate of 54.9254 ms
402 Private
->Cpu
->RegisterInterruptHandler (
410 // Save status that the Timer AP is not using the 8254 PIT
412 Private
->TimerUses8254
= FALSE
;
413 } else if (Status
== EFI_ALREADY_STARTED
) {
415 // Save status that the Timer AP is using the 8254 PIT
417 Private
->TimerUses8254
= TRUE
;
420 // Unexpected status from CPU AP RegisterInterruptHandler()