2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2014 Aleksandar Andrejevic <theflash AT sdf DOT lonestar DOT org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /* INCLUDES *******************************************************************/
32 /* PUBLIC FUNCTIONS ***********************************************************/
35 Fast486ReadMemory(PFAST486_STATE State
,
36 FAST486_SEG_REGS SegmentReg
,
43 PFAST486_SEG_REG CachedDescriptor
;
45 ASSERT(SegmentReg
< FAST486_NUM_SEG_REGS
);
47 /* Get the cached descriptor */
48 CachedDescriptor
= &State
->SegmentRegs
[SegmentReg
];
50 if ((Offset
+ Size
- 1) > CachedDescriptor
->Limit
)
52 /* Read beyond limit */
53 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
57 /* Check for protected mode */
58 if (State
->ControlRegisters
[0] & FAST486_CR0_PE
)
60 /* Privilege checks */
62 if (!CachedDescriptor
->Present
)
64 Fast486Exception(State
, FAST486_EXCEPTION_NP
);
68 if ((!InstFetch
&& (CachedDescriptor
->Rpl
> CachedDescriptor
->Dpl
))
69 || (Fast486GetCurrentPrivLevel(State
) > CachedDescriptor
->Dpl
))
71 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
77 if (!CachedDescriptor
->Executable
)
79 /* Data segment not executable */
80 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
86 if (CachedDescriptor
->Executable
&& (!CachedDescriptor
->ReadWrite
))
88 /* Code segment not readable */
89 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
95 /* Find the linear address */
96 LinearAddress
= CachedDescriptor
->Base
+ Offset
;
98 /* Read from the linear address */
99 return Fast486ReadLinearMemory(State
, LinearAddress
, Buffer
, Size
);
103 Fast486WriteMemory(PFAST486_STATE State
,
104 FAST486_SEG_REGS SegmentReg
,
110 PFAST486_SEG_REG CachedDescriptor
;
112 ASSERT(SegmentReg
< FAST486_NUM_SEG_REGS
);
114 /* Get the cached descriptor */
115 CachedDescriptor
= &State
->SegmentRegs
[SegmentReg
];
117 if ((Offset
+ Size
- 1) > CachedDescriptor
->Limit
)
119 /* Write beyond limit */
120 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
124 /* Check for protected mode */
125 if (State
->ControlRegisters
[0] & FAST486_CR0_PE
)
127 /* Privilege checks */
129 if (!CachedDescriptor
->Present
)
131 Fast486Exception(State
, FAST486_EXCEPTION_NP
);
135 if ((CachedDescriptor
->Rpl
> CachedDescriptor
->Dpl
)
136 || (Fast486GetCurrentPrivLevel(State
) > CachedDescriptor
->Dpl
))
138 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
142 if (CachedDescriptor
->Executable
)
144 /* Code segment not writable */
145 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
148 else if (!CachedDescriptor
->ReadWrite
)
150 /* Data segment not writeable */
151 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
156 /* Find the linear address */
157 LinearAddress
= CachedDescriptor
->Base
+ Offset
;
159 /* Write to the linear address */
160 return Fast486WriteLinearMemory(State
, LinearAddress
, Buffer
, Size
);
163 static inline BOOLEAN
165 Fast486GetIntVector(PFAST486_STATE State
,
167 PFAST486_IDT_ENTRY IdtEntry
)
169 /* Check for protected mode */
170 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
172 /* Read from the IDT */
173 if (!Fast486ReadLinearMemory(State
,
175 + Number
* sizeof(*IdtEntry
),
179 /* Exception occurred */
185 /* Read from the real-mode IVT */
188 /* Paging is always disabled in real mode */
189 State
->MemReadCallback(State
,
191 + Number
* sizeof(FarPointer
),
195 /* Fill a fake IDT entry */
196 IdtEntry
->Offset
= LOWORD(FarPointer
);
197 IdtEntry
->Selector
= HIWORD(FarPointer
);
199 IdtEntry
->Type
= FAST486_IDT_INT_GATE
;
200 IdtEntry
->Storage
= FALSE
;
202 IdtEntry
->Present
= TRUE
;
203 IdtEntry
->OffsetHigh
= 0;
209 static inline BOOLEAN
211 Fast486InterruptInternal(PFAST486_STATE State
,
212 PFAST486_IDT_ENTRY IdtEntry
)
214 USHORT SegmentSelector
= IdtEntry
->Selector
;
215 ULONG Offset
= MAKELONG(IdtEntry
->Offset
, IdtEntry
->OffsetHigh
);
216 ULONG GateType
= IdtEntry
->Type
;
217 BOOLEAN GateSize
= (GateType
== FAST486_IDT_INT_GATE_32
) ||
218 (GateType
== FAST486_IDT_TRAP_GATE_32
);
220 BOOLEAN Success
= FALSE
;
221 ULONG OldPrefixFlags
= State
->PrefixFlags
;
223 /* Check for protected mode */
224 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
227 USHORT OldSs
= State
->SegmentRegs
[FAST486_REG_SS
].Selector
;
228 ULONG OldEsp
= State
->GeneralRegs
[FAST486_REG_ESP
].Long
;
230 if (GateSize
!= (State
->SegmentRegs
[FAST486_REG_CS
].Size
))
233 * The gate size doesn't match the current operand size, so toggle
236 State
->PrefixFlags
^= FAST486_PREFIX_OPSIZE
;
239 /* Check if the interrupt handler is more privileged */
240 if (Fast486GetCurrentPrivLevel(State
) > GET_SEGMENT_RPL(SegmentSelector
))
243 if (!Fast486ReadLinearMemory(State
,
248 /* Exception occurred */
252 /* Check the new (higher) privilege level */
253 switch (GET_SEGMENT_RPL(SegmentSelector
))
257 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss0
))
259 /* Exception occurred */
262 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp0
;
269 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss1
))
271 /* Exception occurred */
274 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp1
;
281 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss2
))
283 /* Exception occurred */
286 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp2
;
293 /* Should never reach here! */
298 /* Push SS selector */
299 if (!Fast486StackPush(State
, OldSs
)) goto Cleanup
;
301 /* Push stack pointer */
302 if (!Fast486StackPush(State
, OldEsp
)) goto Cleanup
;
307 if (State
->SegmentRegs
[FAST486_REG_CS
].Size
)
309 /* Set OPSIZE, because INT always pushes 16-bit values in real mode */
310 State
->PrefixFlags
|= FAST486_PREFIX_OPSIZE
;
315 if (!Fast486StackPush(State
, State
->Flags
.Long
)) goto Cleanup
;
317 /* Push CS selector */
318 if (!Fast486StackPush(State
, State
->SegmentRegs
[FAST486_REG_CS
].Selector
)) goto Cleanup
;
320 /* Push the instruction pointer */
321 if (!Fast486StackPush(State
, State
->InstPtr
.Long
)) goto Cleanup
;
323 if ((GateType
== FAST486_IDT_INT_GATE
) || (GateType
== FAST486_IDT_INT_GATE_32
))
325 /* Disable interrupts after a jump to an interrupt gate handler */
326 State
->Flags
.If
= FALSE
;
330 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, SegmentSelector
))
332 /* An exception occurred during the jump */
338 /* 32-bit code segment, use EIP */
339 State
->InstPtr
.Long
= Offset
;
343 /* 16-bit code segment, use IP */
344 State
->InstPtr
.LowWord
= LOWORD(Offset
);
350 /* Restore the prefix flags */
351 State
->PrefixFlags
= OldPrefixFlags
;
358 Fast486PerformInterrupt(PFAST486_STATE State
,
361 FAST486_IDT_ENTRY IdtEntry
;
363 /* Get the interrupt vector */
364 if (!Fast486GetIntVector(State
, Number
, &IdtEntry
))
366 /* Exception occurred */
370 /* Perform the interrupt */
371 if (!Fast486InterruptInternal(State
, &IdtEntry
))
373 /* Exception occurred */
382 Fast486ExceptionWithErrorCode(PFAST486_STATE State
,
383 FAST486_EXCEPTIONS ExceptionCode
,
386 /* Increment the exception count */
387 State
->ExceptionCount
++;
389 /* Check if the exception occurred more than once */
390 if (State
->ExceptionCount
> 1)
392 /* Then this is a double fault */
393 ExceptionCode
= FAST486_EXCEPTION_DF
;
396 /* Check if this is a triple fault */
397 if (State
->ExceptionCount
== 3)
399 DPRINT("Fast486ExceptionWithErrorCode(%04X:%08X) -- Triple fault\n",
400 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
401 State
->InstPtr
.Long
);
408 /* Restore the IP to the saved IP */
409 State
->InstPtr
= State
->SavedInstPtr
;
411 /* Perform the interrupt */
412 if (!Fast486PerformInterrupt(State
, ExceptionCode
))
415 * If this function failed, that means Fast486Exception
416 * was called again, so just return in this case.
421 if (EXCEPTION_HAS_ERROR_CODE(ExceptionCode
)
422 && (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
))
424 /* Push the error code */
425 if (!Fast486StackPush(State
, ErrorCode
))
428 * If this function failed, that means Fast486Exception
429 * was called again, so just return in this case.
435 /* Reset the exception count */
436 State
->ExceptionCount
= 0;