2007-12-15 17:15:48 +00:00
|
|
|
/*
|
2009-12-31 23:51:26 +00:00
|
|
|
* PROJECT: ReactOS Hardware Abstraction Layer (HAL)
|
|
|
|
* LICENSE: BSD - See COPYING.ARM in the top level directory
|
2007-12-15 17:15:48 +00:00
|
|
|
* PURPOSE: BIOS Access Routines
|
2009-12-31 23:51:26 +00:00
|
|
|
* PROGRAMMERS: ReactOS Portable Systems Group
|
2010-01-07 19:08:11 +00:00
|
|
|
* Alex Ionescu (alex.ionescu@reactos.org)
|
2007-12-15 17:15:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES *******************************************************************/
|
|
|
|
|
|
|
|
#include <hal.h>
|
2020-07-25 13:31:02 +00:00
|
|
|
|
2007-12-15 17:15:48 +00:00
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
2020-07-25 13:31:02 +00:00
|
|
|
|
2010-02-10 23:24:59 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
|
2015-09-15 10:35:49 +00:00
|
|
|
void __cdecl HalpTrap0D();
|
2007-12-15 17:15:48 +00:00
|
|
|
|
|
|
|
/* GLOBALS ********************************************************************/
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// PTE Data
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
ULONG HalpSavedPfn;
|
|
|
|
HARDWARE_PTE HalpSavedPte;
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// IDT Data
|
|
|
|
//
|
2010-01-01 15:09:14 +00:00
|
|
|
PVOID HalpGpfHandler;
|
|
|
|
PVOID HalpBopHandler;
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// TSS Data
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
ULONG HalpSavedEsp0;
|
2010-01-01 00:03:52 +00:00
|
|
|
USHORT HalpSavedTss;
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// IOPM Data
|
|
|
|
//
|
2009-12-31 23:51:26 +00:00
|
|
|
USHORT HalpSavedIopmBase;
|
|
|
|
PUSHORT HalpSavedIoMap;
|
[HALX86] Fix the "ASSERT(j < 32);" problem in HalpStoreAndClearIopm() encountered from time to time.
CORE-11921 CORE-13715
(Regression introduced by commit 2e1b82cf, r44841.)
In some cases the number of valid (!= 0xFFFF) entries in the IOPM can be
larger than the assumed size (32) of the entries cache. The maximum
possible number of entries is equal to IOPM_SIZE / sizeof(USHORT).
A way to reproduce the problem is as follows: start ReactOS in debugging
mode using '/DEBUG /DEBUGPORT=SCREEN' . Then manage to break into the
debugger exactly during the execution of Ke386CallBios() triggered by
display initialization (for example in my case, while a video driver was
being initialized via the HwInitialize() call done by videoport inside
IntVideoPortDispatchOpen() ).
When this happens, a "concurrent" execution between Ke386CallBios() and
the HAL function HalpStoreAndClearIopm() takes place. This is due to the
fact that when entering the debugger in SCREEN mode, the following
call-chain holds:
InbvResetDisplay() -> VidResetDisplay() -> HalResetDisplay() ->
HalpBiosDisplayReset() -> HalpSetupRealModeIoPermissionsAndTask() ->
HalpStoreAndClearIopm().
However, the code of Ke386CallBios() has reset the IOPM contents with
all zeroes instead of 0xFFFF, and this triggers the caching of all the
entries of the IOPM by HalpStoreAndClearIopm(), whose number is greater
than the wrongly assumed number of '32'.
As Thomas explained to me, "Windows supports [the maximum number of IOPM entries],
it just makes a full copy of the table instead of this indexed partial copy."
And I agree that this overengineered so-called "optimization" committed
in 2e1b82cf contributed in introducing an unnecessary bug and making the
code less clear. Also it makes the IOPM cache larger than the necessary
size by twice as much. Finally, Ke386CallBios() also caches IOPM entries
before doing a 16-bit call, and obviously uses the more straightforward
way of doing a direct copy of the IOPM table (using RtlCopyMemory()).
I wonder what kind of "optimization" this tried to achieve, knowing that
we are not doing like thousands of 32->16bit BIOS interrupt calls per second
in ReactOS...
2019-11-24 21:58:12 +00:00
|
|
|
USHORT HalpSavedIoMapData[IOPM_SIZE / sizeof(USHORT)][2];
|
2009-12-31 23:51:26 +00:00
|
|
|
ULONG HalpSavedIoMapEntries;
|
2007-12-15 17:15:48 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Where the protected mode stack is */
|
|
|
|
ULONG_PTR HalpSavedEsp;
|
|
|
|
|
|
|
|
/* Where the real mode code ends */
|
|
|
|
extern PVOID HalpRealModeEnd;
|
|
|
|
|
2010-02-10 23:24:59 +00:00
|
|
|
/* Context saved for return from v86 mode */
|
|
|
|
jmp_buf HalpSavedContext;
|
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
|
|
|
|
/* V86 OPCODE HANDLERS ********************************************************/
|
|
|
|
|
|
|
|
BOOLEAN
|
|
|
|
FASTCALL
|
|
|
|
HalpOpcodeInvalid(IN PHAL_BIOS_FRAME BiosFrame)
|
|
|
|
{
|
2014-11-07 21:19:27 +00:00
|
|
|
PUCHAR Inst = (PUCHAR)(BiosFrame->CsBase + BiosFrame->Eip);
|
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Print error message */
|
2017-05-19 21:37:50 +00:00
|
|
|
DPRINT1("HAL: An invalid V86 opcode was encountered at address %X:%X\n"
|
|
|
|
"Opcode: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\n",
|
2014-11-07 21:19:27 +00:00
|
|
|
BiosFrame->SegCs, BiosFrame->Eip,
|
|
|
|
Inst[0], Inst[1], Inst[2], Inst[3], Inst[4],
|
|
|
|
Inst[5], Inst[6], Inst[7], Inst[8], Inst[9]);
|
2010-01-26 19:41:31 +00:00
|
|
|
|
|
|
|
/* Break */
|
|
|
|
DbgBreakPoint();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN
|
|
|
|
FASTCALL
|
|
|
|
HalpPushInt(IN PHAL_BIOS_FRAME BiosFrame,
|
|
|
|
IN ULONG Interrupt)
|
|
|
|
{
|
|
|
|
PUSHORT Stack;
|
|
|
|
ULONG Eip;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Calculate stack address (SP) */
|
|
|
|
Stack = (PUSHORT)(BiosFrame->SsBase + (BiosFrame->Esp & 0xFFFF));
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Push EFlags */
|
|
|
|
Stack--;
|
|
|
|
*Stack = BiosFrame->EFlags & 0xFFFF;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Push CS */
|
|
|
|
Stack--;
|
|
|
|
*Stack = BiosFrame->SegCs & 0xFFFF;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Push IP */
|
|
|
|
Stack--;
|
|
|
|
*Stack = BiosFrame->Eip & 0xFFFF;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Compute new CS:IP from the IVT address for this interrupt entry */
|
|
|
|
Eip = *(PULONG)(Interrupt * 4);
|
|
|
|
BiosFrame->Eip = Eip & 0xFFFF;
|
|
|
|
BiosFrame->SegCs = Eip >> 16;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Update stack address */
|
|
|
|
BiosFrame->Esp = (ULONG_PTR)Stack & 0xFFFF;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Update CS to linear */
|
|
|
|
BiosFrame->CsBase = BiosFrame->SegCs << 4;
|
|
|
|
BiosFrame->CsLimit = 0xFFFF;
|
|
|
|
BiosFrame->CsFlags = 0;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* We're done */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN
|
|
|
|
FASTCALL
|
|
|
|
HalpOpcodeINTnn(IN PHAL_BIOS_FRAME BiosFrame)
|
|
|
|
{
|
|
|
|
UCHAR Interrupt;
|
|
|
|
PKTRAP_FRAME TrapFrame;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Convert SS to linear */
|
|
|
|
BiosFrame->SsBase = BiosFrame->SegSs << 4;
|
|
|
|
BiosFrame->SsLimit = 0xFFFF;
|
|
|
|
BiosFrame->SsFlags = 0;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Increase EIP and validate */
|
|
|
|
BiosFrame->Eip++;
|
|
|
|
if (BiosFrame->Eip > BiosFrame->CsLimit) return FALSE;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Read interrupt number */
|
|
|
|
Interrupt = *(PUCHAR)(BiosFrame->CsBase + BiosFrame->Eip);
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Increase EIP and push the interrupt */
|
|
|
|
BiosFrame->Eip++;
|
|
|
|
if (HalpPushInt(BiosFrame, Interrupt))
|
|
|
|
{
|
|
|
|
/* Update the trap frame */
|
|
|
|
TrapFrame = BiosFrame->TrapFrame;
|
|
|
|
TrapFrame->HardwareSegSs = BiosFrame->SegSs;
|
|
|
|
TrapFrame->HardwareEsp = BiosFrame->Esp;
|
|
|
|
TrapFrame->SegCs = BiosFrame->SegCs;
|
|
|
|
TrapFrame->EFlags = BiosFrame->EFlags;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Success */
|
|
|
|
return TRUE;
|
|
|
|
}
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Failure */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN
|
|
|
|
FASTCALL
|
|
|
|
HalpDispatchV86Opcode(IN PKTRAP_FRAME TrapFrame)
|
|
|
|
{
|
|
|
|
UCHAR Instruction;
|
|
|
|
HAL_BIOS_FRAME BiosFrame;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Fill out the BIOS frame */
|
|
|
|
BiosFrame.TrapFrame = TrapFrame;
|
|
|
|
BiosFrame.SegSs = TrapFrame->HardwareSegSs;
|
|
|
|
BiosFrame.Esp = TrapFrame->HardwareEsp;
|
|
|
|
BiosFrame.EFlags = TrapFrame->EFlags;
|
|
|
|
BiosFrame.SegCs = TrapFrame->SegCs;
|
|
|
|
BiosFrame.Eip = TrapFrame->Eip;
|
|
|
|
BiosFrame.Prefix = 0;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Convert CS to linear */
|
|
|
|
BiosFrame.CsBase = BiosFrame.SegCs << 4;
|
|
|
|
BiosFrame.CsLimit = 0xFFFF;
|
|
|
|
BiosFrame.CsFlags = 0;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Validate IP */
|
|
|
|
if (BiosFrame.Eip > BiosFrame.CsLimit) return FALSE;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Read IP */
|
|
|
|
Instruction = *(PUCHAR)(BiosFrame.CsBase + BiosFrame.Eip);
|
|
|
|
if (Instruction != 0xCD)
|
|
|
|
{
|
|
|
|
/* We only support INT */
|
|
|
|
HalpOpcodeInvalid(&BiosFrame);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Handle the interrupt */
|
|
|
|
if (HalpOpcodeINTnn(&BiosFrame))
|
|
|
|
{
|
|
|
|
/* Update EIP */
|
|
|
|
TrapFrame->Eip = BiosFrame.Eip;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* We're done */
|
|
|
|
return TRUE;
|
|
|
|
}
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Failure */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* V86 TRAP HANDLERS **********************************************************/
|
|
|
|
|
2011-02-10 13:01:39 +00:00
|
|
|
DECLSPEC_NORETURN
|
2010-01-26 19:41:31 +00:00
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
HalpTrap0DHandler(IN PKTRAP_FRAME TrapFrame)
|
|
|
|
{
|
|
|
|
/* Enter the trap */
|
|
|
|
KiEnterTrap(TrapFrame);
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Check if this is a V86 trap */
|
|
|
|
if (TrapFrame->EFlags & EFLAGS_V86_MASK)
|
|
|
|
{
|
|
|
|
/* Dispatch the opcode and exit the trap */
|
|
|
|
HalpDispatchV86Opcode(TrapFrame);
|
|
|
|
KiEoiHelper(TrapFrame);
|
|
|
|
}
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Strange, it isn't! This can happen during NMI */
|
|
|
|
DPRINT1("HAL: Trap0D while not in V86 mode\n");
|
2010-01-30 16:40:57 +00:00
|
|
|
KiDumpTrapFrame(TrapFrame);
|
2013-01-04 11:47:19 +00:00
|
|
|
|
2013-01-10 01:45:22 +00:00
|
|
|
ERROR_FATAL();
|
|
|
|
while (TRUE); /* 'noreturn' function */
|
2010-01-26 19:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
DECLSPEC_NORETURN
|
2015-09-03 23:57:39 +00:00
|
|
|
HalpTrap06(VOID)
|
2010-01-26 19:41:31 +00:00
|
|
|
{
|
|
|
|
/* Restore ES/DS to known good values first */
|
|
|
|
Ke386SetEs(KGDT_R3_DATA | RPL_MASK);
|
|
|
|
Ke386SetDs(KGDT_R3_DATA | RPL_MASK);
|
2010-02-11 17:36:57 +00:00
|
|
|
Ke386SetFs(KGDT_R0_PCR);
|
2010-02-10 23:24:59 +00:00
|
|
|
|
2019-11-24 21:05:37 +00:00
|
|
|
/* Restore the stack */
|
2010-02-10 23:24:59 +00:00
|
|
|
KeGetPcr()->TSS->Esp0 = HalpSavedEsp0;
|
|
|
|
|
|
|
|
/* Return back to where we left */
|
|
|
|
longjmp(HalpSavedContext, 1);
|
|
|
|
UNREACHABLE;
|
2010-01-26 19:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* V8086 ENTER ****************************************************************/
|
|
|
|
|
|
|
|
VOID
|
2010-02-10 23:24:59 +00:00
|
|
|
NTAPI
|
2015-09-03 23:57:39 +00:00
|
|
|
HalpBiosCall(VOID)
|
2010-01-26 19:41:31 +00:00
|
|
|
{
|
|
|
|
/* Must be volatile so it doesn't get optimized away! */
|
|
|
|
volatile KTRAP_FRAME V86TrapFrame;
|
|
|
|
ULONG_PTR StackOffset, CodeOffset;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-02-10 23:24:59 +00:00
|
|
|
/* Save the context, check for return */
|
|
|
|
if (_setjmp(HalpSavedContext))
|
|
|
|
{
|
|
|
|
/* Returned from v86 */
|
|
|
|
return;
|
|
|
|
}
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Kill alignment faults */
|
|
|
|
__writecr0(__readcr0() & ~CR0_AM);
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Set new stack address */
|
2010-02-10 23:24:59 +00:00
|
|
|
KeGetPcr()->TSS->Esp0 = (ULONG)&V86TrapFrame - 0x20 - sizeof(FX_SAVE_AREA);
|
2010-01-26 19:41:31 +00:00
|
|
|
|
|
|
|
/* Compute segmented IP and SP offsets */
|
|
|
|
StackOffset = (ULONG_PTR)&HalpRealModeEnd - 4 - (ULONG_PTR)HalpRealModeStart;
|
|
|
|
CodeOffset = (ULONG_PTR)HalpRealModeStart & 0xFFF;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Now build the V86 trap frame */
|
|
|
|
V86TrapFrame.V86Es = 0;
|
|
|
|
V86TrapFrame.V86Ds = 0;
|
|
|
|
V86TrapFrame.V86Gs = 0;
|
|
|
|
V86TrapFrame.V86Fs = 0;
|
|
|
|
V86TrapFrame.HardwareSegSs = 0x2000;
|
|
|
|
V86TrapFrame.HardwareEsp = StackOffset + CodeOffset;
|
|
|
|
V86TrapFrame.EFlags = __readeflags() | EFLAGS_V86_MASK | EFLAGS_IOPL;
|
|
|
|
V86TrapFrame.SegCs = 0x2000;
|
|
|
|
V86TrapFrame.Eip = CodeOffset;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-26 19:41:31 +00:00
|
|
|
/* Exit to V86 mode */
|
2010-02-26 00:07:22 +00:00
|
|
|
HalpExitToV86((PKTRAP_FRAME)&V86TrapFrame);
|
2010-01-26 19:41:31 +00:00
|
|
|
}
|
|
|
|
|
2007-12-15 17:15:48 +00:00
|
|
|
/* FUNCTIONS ******************************************************************/
|
|
|
|
|
2010-01-01 00:03:52 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpBorrowTss(VOID)
|
|
|
|
{
|
|
|
|
USHORT Tss;
|
|
|
|
PKGDTENTRY TssGdt;
|
|
|
|
ULONG_PTR TssLimit;
|
|
|
|
PKTSS TssBase;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the current TSS and its GDT entry
|
|
|
|
//
|
|
|
|
Tss = Ke386GetTr();
|
2022-03-08 03:55:17 +00:00
|
|
|
TssGdt = &KeGetPcr()->GDT[Tss / sizeof(KGDTENTRY)];
|
2010-01-01 00:03:52 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Get the KTSS limit and check if it has IOPM space
|
|
|
|
//
|
|
|
|
TssLimit = TssGdt->LimitLow | TssGdt->HighWord.Bits.LimitHi << 16;
|
|
|
|
|
|
|
|
//
|
|
|
|
// If the KTSS doesn't have enough space this is probably an NMI or DF
|
|
|
|
//
|
|
|
|
if (TssLimit > IOPM_SIZE)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// We are good to go
|
|
|
|
//
|
|
|
|
HalpSavedTss = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the "real" TSS
|
|
|
|
//
|
2022-03-08 03:55:17 +00:00
|
|
|
TssGdt = &KeGetPcr()->GDT[KGDT_TSS / sizeof(KGDTENTRY)];
|
2010-01-01 00:03:52 +00:00
|
|
|
TssBase = (PKTSS)(ULONG_PTR)(TssGdt->BaseLow |
|
|
|
|
TssGdt->HighWord.Bytes.BaseMid << 16 |
|
|
|
|
TssGdt->HighWord.Bytes.BaseHi << 24);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Switch to it
|
|
|
|
//
|
|
|
|
KeGetPcr()->TSS = TssBase;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set it up
|
|
|
|
//
|
|
|
|
TssGdt->HighWord.Bits.Type = I386_TSS;
|
|
|
|
TssGdt->HighWord.Bits.Pres = 1;
|
|
|
|
TssGdt->HighWord.Bits.Dpl = 0;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-01 00:03:52 +00:00
|
|
|
//
|
|
|
|
// Load new TSS and return old one
|
|
|
|
//
|
|
|
|
Ke386SetTr(KGDT_TSS);
|
|
|
|
HalpSavedTss = Tss;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpReturnTss(VOID)
|
|
|
|
{
|
|
|
|
PKGDTENTRY TssGdt;
|
|
|
|
PKTSS TssBase;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-01 00:03:52 +00:00
|
|
|
//
|
|
|
|
// Get the original TSS
|
|
|
|
//
|
2022-03-08 03:55:17 +00:00
|
|
|
TssGdt = &KeGetPcr()->GDT[HalpSavedTss / sizeof(KGDTENTRY)];
|
2010-01-01 00:03:52 +00:00
|
|
|
TssBase = (PKTSS)(ULONG_PTR)(TssGdt->BaseLow |
|
|
|
|
TssGdt->HighWord.Bytes.BaseMid << 16 |
|
|
|
|
TssGdt->HighWord.Bytes.BaseHi << 24);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Switch to it
|
|
|
|
//
|
|
|
|
KeGetPcr()->TSS = TssBase;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set it up
|
|
|
|
//
|
|
|
|
TssGdt->HighWord.Bits.Type = I386_TSS;
|
|
|
|
TssGdt->HighWord.Bits.Pres = 1;
|
|
|
|
TssGdt->HighWord.Bits.Dpl = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Load old TSS
|
|
|
|
//
|
|
|
|
Ke386SetTr(HalpSavedTss);
|
|
|
|
}
|
|
|
|
|
2007-12-15 17:15:48 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
2009-12-31 23:51:26 +00:00
|
|
|
HalpStoreAndClearIopm(VOID)
|
2007-12-15 17:15:48 +00:00
|
|
|
{
|
2011-11-21 12:17:57 +00:00
|
|
|
USHORT i, j;
|
2009-12-31 23:51:26 +00:00
|
|
|
PUSHORT Entry = HalpSavedIoMap;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Loop the I/O Map
|
|
|
|
//
|
2017-05-22 12:20:31 +00:00
|
|
|
for (i = j = 0; i < IOPM_SIZE / sizeof(USHORT); i++)
|
2009-12-31 23:51:26 +00:00
|
|
|
{
|
|
|
|
//
|
|
|
|
// Check for non-FFFF entry
|
|
|
|
//
|
|
|
|
if (*Entry != 0xFFFF)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Save it
|
|
|
|
//
|
2011-11-21 12:17:57 +00:00
|
|
|
HalpSavedIoMapData[j][0] = i;
|
2009-12-31 23:51:26 +00:00
|
|
|
HalpSavedIoMapData[j][1] = *Entry;
|
2011-11-21 12:04:16 +00:00
|
|
|
j++;
|
2009-12-31 23:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Clear it
|
|
|
|
//
|
|
|
|
*Entry++ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Terminate it
|
|
|
|
//
|
2017-05-22 12:20:31 +00:00
|
|
|
while (i++ < IOPM_FULL_SIZE / sizeof(USHORT))
|
|
|
|
{
|
|
|
|
*Entry++ = 0xFFFF;
|
|
|
|
}
|
2007-12-15 17:15:48 +00:00
|
|
|
|
2009-12-31 23:51:26 +00:00
|
|
|
//
|
|
|
|
// Return the entries we saved
|
|
|
|
//
|
|
|
|
HalpSavedIoMapEntries = j;
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
2009-12-31 23:51:26 +00:00
|
|
|
HalpRestoreIopm(VOID)
|
2007-12-15 17:15:48 +00:00
|
|
|
{
|
2009-12-31 23:51:26 +00:00
|
|
|
ULONG i = HalpSavedIoMapEntries;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set default state
|
|
|
|
//
|
2011-09-01 12:38:56 +00:00
|
|
|
RtlFillMemory(HalpSavedIoMap, IOPM_FULL_SIZE, 0xFF);
|
2007-12-15 17:15:48 +00:00
|
|
|
|
2009-12-31 23:51:26 +00:00
|
|
|
//
|
|
|
|
// Restore the backed up copy, and initialize it
|
|
|
|
//
|
|
|
|
while (i--) HalpSavedIoMap[HalpSavedIoMapData[i][0]] = HalpSavedIoMapData[i][1];
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpMapRealModeMemory(VOID)
|
|
|
|
{
|
|
|
|
PHARDWARE_PTE Pte, V86Pte;
|
|
|
|
ULONG i;
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Get the page table directory for the lowest meg of memory
|
|
|
|
//
|
2010-01-01 15:09:14 +00:00
|
|
|
Pte = HalAddressToPde(0);
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpSavedPfn = Pte->PageFrameNumber;
|
|
|
|
HalpSavedPte = *Pte;
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Map it to the HAL reserved region and make it valid
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
Pte->Valid = 1;
|
|
|
|
Pte->Write = 1;
|
|
|
|
Pte->Owner = 1;
|
2010-01-01 15:09:14 +00:00
|
|
|
Pte->PageFrameNumber = (HalAddressToPde(0xFFC00000))->PageFrameNumber;
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Flush the TLB
|
|
|
|
//
|
|
|
|
HalpFlushTLB();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Now loop the first meg of memory
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
for (i = 0; i < 0x100000; i += PAGE_SIZE)
|
|
|
|
{
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Identity map it
|
|
|
|
//
|
2010-01-01 15:09:14 +00:00
|
|
|
Pte = HalAddressToPte(i);
|
2007-12-15 17:15:48 +00:00
|
|
|
Pte->PageFrameNumber = i >> PAGE_SHIFT;
|
|
|
|
Pte->Valid = 1;
|
|
|
|
Pte->Write = 1;
|
|
|
|
Pte->Owner = 1;
|
|
|
|
}
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Now get the entry for our real mode V86 code and the target
|
|
|
|
//
|
2010-01-01 15:09:14 +00:00
|
|
|
Pte = HalAddressToPte(0x20000);
|
|
|
|
V86Pte = HalAddressToPte(&HalpRealModeStart);
|
2007-12-15 17:15:48 +00:00
|
|
|
do
|
|
|
|
{
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Map the physical address into our real-mode region
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
Pte->PageFrameNumber = V86Pte->PageFrameNumber;
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Keep going until we've reached the end of our region
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
Pte++;
|
|
|
|
V86Pte++;
|
2010-01-01 15:09:14 +00:00
|
|
|
} while (V86Pte <= HalAddressToPte(&HalpRealModeEnd));
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Flush the TLB
|
|
|
|
//
|
|
|
|
HalpFlushTLB();
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpSwitchToRealModeTrapHandlers(VOID)
|
|
|
|
{
|
2010-01-01 15:09:14 +00:00
|
|
|
//
|
|
|
|
// Save the current Invalid Opcode and General Protection Fault Handlers
|
|
|
|
//
|
|
|
|
HalpGpfHandler = KeQueryInterruptHandler(13);
|
|
|
|
HalpBopHandler = KeQueryInterruptHandler(6);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Now set our own GPF handler to handle exceptions while in real mode
|
|
|
|
//
|
|
|
|
KeRegisterInterruptHandler(13, HalpTrap0D);
|
|
|
|
|
|
|
|
//
|
|
|
|
// And our own invalid opcode handler to detect the BOP to get us out
|
|
|
|
//
|
|
|
|
KeRegisterInterruptHandler(6, HalpTrap06);
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpSetupRealModeIoPermissionsAndTask(VOID)
|
|
|
|
{
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Switch to valid TSS
|
|
|
|
//
|
2010-01-01 00:03:52 +00:00
|
|
|
HalpBorrowTss();
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Save a copy of the I/O Map and delete it
|
|
|
|
//
|
2017-05-22 12:20:31 +00:00
|
|
|
HalpSavedIoMap = (PUSHORT)KeGetPcr()->TSS->IoMaps[0].IoMap;
|
2009-12-31 23:51:26 +00:00
|
|
|
HalpStoreAndClearIopm();
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Save the IOPM and switch to the real-mode one
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpSavedIopmBase = KeGetPcr()->TSS->IoMapBase;
|
|
|
|
KeGetPcr()->TSS->IoMapBase = KiComputeIopmOffset(1);
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Save our stack pointer
|
|
|
|
//
|
2019-11-24 21:05:37 +00:00
|
|
|
HalpSavedEsp0 = KeGetPcr()->TSS->Esp0;
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpRestoreTrapHandlers(VOID)
|
|
|
|
{
|
2010-01-01 15:09:14 +00:00
|
|
|
//
|
2010-01-01 21:05:41 +00:00
|
|
|
// Keep dummy GPF handler in case we get an NMI during V8086
|
|
|
|
//
|
|
|
|
if (!HalpNMIInProgress)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// Not an NMI -- put back the original handler
|
|
|
|
//
|
|
|
|
KeRegisterInterruptHandler(13, HalpGpfHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Restore invalid opcode handler
|
2010-01-01 15:09:14 +00:00
|
|
|
//
|
|
|
|
KeRegisterInterruptHandler(6, HalpBopHandler);
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpRestoreIoPermissionsAndTask(VOID)
|
|
|
|
{
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Restore the stack pointer
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
KeGetPcr()->TSS->Esp0 = HalpSavedEsp0;
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Restore the I/O Map
|
|
|
|
//
|
2009-12-31 23:51:26 +00:00
|
|
|
HalpRestoreIopm();
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Restore the IOPM
|
|
|
|
//
|
2010-01-01 00:03:52 +00:00
|
|
|
KeGetPcr()->TSS->IoMapBase = HalpSavedIopmBase;
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Restore the TSS
|
|
|
|
//
|
2010-01-01 00:03:52 +00:00
|
|
|
if (HalpSavedTss) HalpReturnTss();
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
HalpUnmapRealModeMemory(VOID)
|
|
|
|
{
|
|
|
|
ULONG i;
|
|
|
|
PHARDWARE_PTE Pte;
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Loop the first meg of memory
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
for (i = 0; i < 0x100000; i += PAGE_SIZE)
|
|
|
|
{
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Invalidate each PTE
|
|
|
|
//
|
2010-01-01 15:09:14 +00:00
|
|
|
Pte = HalAddressToPte(i);
|
2007-12-15 17:15:48 +00:00
|
|
|
Pte->Valid = 0;
|
|
|
|
Pte->Write = 0;
|
2009-11-04 21:57:32 +00:00
|
|
|
Pte->Owner = 0;
|
2007-12-15 17:15:48 +00:00
|
|
|
Pte->PageFrameNumber = 0;
|
|
|
|
}
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Restore the PDE for the lowest megabyte of memory
|
|
|
|
//
|
2010-01-01 15:09:14 +00:00
|
|
|
Pte = HalAddressToPde(0);
|
2007-12-15 17:15:48 +00:00
|
|
|
*Pte = HalpSavedPte;
|
|
|
|
Pte->PageFrameNumber = HalpSavedPfn;
|
2010-01-01 16:37:34 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Flush the TLB
|
|
|
|
//
|
|
|
|
HalpFlushTLB();
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN
|
|
|
|
NTAPI
|
|
|
|
HalpBiosDisplayReset(VOID)
|
|
|
|
{
|
2020-07-25 13:31:02 +00:00
|
|
|
#if defined(SARCH_XBOX) || defined(SARCH_PC98)
|
|
|
|
/* There is no VGA BIOS on these machine types */
|
2020-05-12 14:32:37 +00:00
|
|
|
return FALSE;
|
|
|
|
#else
|
2009-09-18 20:56:49 +00:00
|
|
|
ULONG Flags;
|
2009-11-08 21:42:28 +00:00
|
|
|
PHARDWARE_PTE IdtPte;
|
|
|
|
BOOLEAN RestoreWriteProtection = FALSE;
|
2007-12-15 17:15:48 +00:00
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Disable interrupts
|
|
|
|
//
|
2009-09-18 20:56:49 +00:00
|
|
|
Flags = __readeflags();
|
2007-12-15 17:15:48 +00:00
|
|
|
_disable();
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Map memory available to the V8086 real-mode code
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpMapRealModeMemory();
|
|
|
|
|
2019-11-24 21:05:37 +00:00
|
|
|
//
|
2010-01-01 16:37:34 +00:00
|
|
|
// On P5, the first 7 entries of the IDT are write protected to work around
|
|
|
|
// the cmpxchg8b lock errata. Unprotect them here so we can set our custom
|
|
|
|
// invalid op-code handler.
|
|
|
|
//
|
2022-03-08 03:55:17 +00:00
|
|
|
IdtPte = HalAddressToPte(KeGetPcr()->IDT);
|
2011-11-21 12:04:16 +00:00
|
|
|
RestoreWriteProtection = IdtPte->Write != 0;
|
2010-01-02 00:17:37 +00:00
|
|
|
IdtPte->Write = 1;
|
2009-11-08 21:42:28 +00:00
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Use special invalid opcode and GPF trap handlers
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpSwitchToRealModeTrapHandlers();
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Configure the IOPM and TSS
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpSetupRealModeIoPermissionsAndTask();
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Now jump to real mode
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpBiosCall();
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Restore kernel trap handlers
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpRestoreTrapHandlers();
|
2009-11-08 21:42:28 +00:00
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Restore write permission
|
|
|
|
//
|
2009-12-31 23:56:40 +00:00
|
|
|
IdtPte->Write = RestoreWriteProtection;
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Restore TSS and IOPM
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpRestoreIoPermissionsAndTask();
|
2019-11-24 21:05:37 +00:00
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Restore low memory mapping
|
|
|
|
//
|
2007-12-15 17:15:48 +00:00
|
|
|
HalpUnmapRealModeMemory();
|
|
|
|
|
2010-01-01 16:37:34 +00:00
|
|
|
//
|
|
|
|
// Restore interrupts if they were previously enabled
|
|
|
|
//
|
2009-09-18 20:56:49 +00:00
|
|
|
__writeeflags(Flags);
|
2007-12-15 17:15:48 +00:00
|
|
|
return TRUE;
|
2020-05-12 14:32:37 +00:00
|
|
|
#endif
|
2007-12-15 17:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|