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
[FAST486_REG_CR0
] & 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 #ifndef FAST486_NO_PREFETCH
99 if (InstFetch
&& ((Offset
+ FAST486_CACHE_SIZE
- 1) <= CachedDescriptor
->Limit
))
101 State
->PrefetchAddress
= LinearAddress
;
103 if ((State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PG
)
104 && (PAGE_OFFSET(State
->PrefetchAddress
) > (FAST486_PAGE_SIZE
- FAST486_CACHE_SIZE
)))
106 /* We mustn't prefetch across a page boundary */
107 State
->PrefetchAddress
= PAGE_ALIGN(State
->PrefetchAddress
)
108 | (FAST486_PAGE_SIZE
- FAST486_CACHE_SIZE
);
110 if ((LinearAddress
- State
->PrefetchAddress
+ Size
) >= FAST486_CACHE_SIZE
)
112 /* We can't prefetch without possibly violating page permissions */
113 State
->PrefetchValid
= FALSE
;
114 return Fast486ReadLinearMemory(State
, LinearAddress
, Buffer
, Size
);
119 if (Fast486ReadLinearMemory(State
,
120 State
->PrefetchAddress
,
121 State
->PrefetchCache
,
124 State
->PrefetchValid
= TRUE
;
126 RtlMoveMemory(Buffer
,
127 &State
->PrefetchCache
[LinearAddress
- State
->PrefetchAddress
],
133 State
->PrefetchValid
= FALSE
;
140 /* Read from the linear address */
141 return Fast486ReadLinearMemory(State
, LinearAddress
, Buffer
, Size
);
146 Fast486WriteMemory(PFAST486_STATE State
,
147 FAST486_SEG_REGS SegmentReg
,
153 PFAST486_SEG_REG CachedDescriptor
;
155 ASSERT(SegmentReg
< FAST486_NUM_SEG_REGS
);
157 /* Get the cached descriptor */
158 CachedDescriptor
= &State
->SegmentRegs
[SegmentReg
];
160 if ((Offset
+ Size
- 1) > CachedDescriptor
->Limit
)
162 /* Write beyond limit */
163 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
167 /* Check for protected mode */
168 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
170 /* Privilege checks */
172 if (!CachedDescriptor
->Present
)
174 Fast486Exception(State
, FAST486_EXCEPTION_NP
);
178 if ((CachedDescriptor
->Rpl
> CachedDescriptor
->Dpl
)
179 || (Fast486GetCurrentPrivLevel(State
) > CachedDescriptor
->Dpl
))
181 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
185 if (CachedDescriptor
->Executable
)
187 /* Code segment not writable */
188 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
191 else if (!CachedDescriptor
->ReadWrite
)
193 /* Data segment not writeable */
194 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
199 /* Find the linear address */
200 LinearAddress
= CachedDescriptor
->Base
+ Offset
;
202 #ifndef FAST486_NO_PREFETCH
203 if (State
->PrefetchValid
204 && (LinearAddress
>= State
->PrefetchAddress
)
205 && ((LinearAddress
+ Size
) <= (State
->PrefetchAddress
+ FAST486_CACHE_SIZE
)))
207 /* Update the prefetch */
208 RtlMoveMemory(&State
->PrefetchCache
[LinearAddress
- State
->PrefetchAddress
],
210 min(Size
, FAST486_CACHE_SIZE
+ State
->PrefetchAddress
- LinearAddress
));
214 /* Write to the linear address */
215 return Fast486WriteLinearMemory(State
, LinearAddress
, Buffer
, Size
);
218 static inline BOOLEAN
220 Fast486GetIntVector(PFAST486_STATE State
,
222 PFAST486_IDT_ENTRY IdtEntry
)
224 /* Check for protected mode */
225 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
227 /* Read from the IDT */
228 if (!Fast486ReadLinearMemory(State
,
230 + Number
* sizeof(*IdtEntry
),
234 /* Exception occurred */
240 /* Read from the real-mode IVT */
243 /* Paging is always disabled in real mode */
244 State
->MemReadCallback(State
,
246 + Number
* sizeof(FarPointer
),
250 /* Fill a fake IDT entry */
251 IdtEntry
->Offset
= LOWORD(FarPointer
);
252 IdtEntry
->Selector
= HIWORD(FarPointer
);
254 IdtEntry
->Type
= FAST486_IDT_INT_GATE
;
255 IdtEntry
->Storage
= FALSE
;
257 IdtEntry
->Present
= TRUE
;
258 IdtEntry
->OffsetHigh
= 0;
264 static inline BOOLEAN
266 Fast486InterruptInternal(PFAST486_STATE State
,
267 PFAST486_IDT_ENTRY IdtEntry
)
269 USHORT SegmentSelector
= IdtEntry
->Selector
;
270 ULONG Offset
= MAKELONG(IdtEntry
->Offset
, IdtEntry
->OffsetHigh
);
271 ULONG GateType
= IdtEntry
->Type
;
272 BOOLEAN GateSize
= (GateType
== FAST486_IDT_INT_GATE_32
) ||
273 (GateType
== FAST486_IDT_TRAP_GATE_32
);
275 BOOLEAN Success
= FALSE
;
276 ULONG OldPrefixFlags
= State
->PrefixFlags
;
278 /* Check for protected mode */
279 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
282 USHORT OldSs
= State
->SegmentRegs
[FAST486_REG_SS
].Selector
;
283 ULONG OldEsp
= State
->GeneralRegs
[FAST486_REG_ESP
].Long
;
285 if (GateType
== FAST486_TASK_GATE_SIGNATURE
)
288 return Fast486TaskSwitch(State
, FAST486_TASK_CALL
, IdtEntry
->Selector
);
291 if (GateSize
!= (State
->SegmentRegs
[FAST486_REG_CS
].Size
))
294 * The gate size doesn't match the current operand size, so toggle
297 State
->PrefixFlags
^= FAST486_PREFIX_OPSIZE
;
300 /* Check if the interrupt handler is more privileged */
301 if (Fast486GetCurrentPrivLevel(State
) > GET_SEGMENT_RPL(SegmentSelector
))
304 if (!Fast486ReadLinearMemory(State
,
309 /* Exception occurred */
313 /* Check the new (higher) privilege level */
314 switch (GET_SEGMENT_RPL(SegmentSelector
))
318 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss0
))
320 /* Exception occurred */
323 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp0
;
330 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss1
))
332 /* Exception occurred */
335 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp1
;
342 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss2
))
344 /* Exception occurred */
347 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp2
;
354 /* Should never reach here! */
359 /* Push SS selector */
360 if (!Fast486StackPush(State
, OldSs
)) goto Cleanup
;
362 /* Push stack pointer */
363 if (!Fast486StackPush(State
, OldEsp
)) goto Cleanup
;
368 if (State
->SegmentRegs
[FAST486_REG_CS
].Size
)
370 /* Set OPSIZE, because INT always pushes 16-bit values in real mode */
371 State
->PrefixFlags
|= FAST486_PREFIX_OPSIZE
;
376 if (!Fast486StackPush(State
, State
->Flags
.Long
)) goto Cleanup
;
378 /* Push CS selector */
379 if (!Fast486StackPush(State
, State
->SegmentRegs
[FAST486_REG_CS
].Selector
)) goto Cleanup
;
381 /* Push the instruction pointer */
382 if (!Fast486StackPush(State
, State
->InstPtr
.Long
)) goto Cleanup
;
384 if ((GateType
== FAST486_IDT_INT_GATE
) || (GateType
== FAST486_IDT_INT_GATE_32
))
386 /* Disable interrupts after a jump to an interrupt gate handler */
387 State
->Flags
.If
= FALSE
;
391 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, SegmentSelector
))
393 /* An exception occurred during the jump */
399 /* 32-bit code segment, use EIP */
400 State
->InstPtr
.Long
= Offset
;
404 /* 16-bit code segment, use IP */
405 State
->InstPtr
.LowWord
= LOWORD(Offset
);
411 /* Restore the prefix flags */
412 State
->PrefixFlags
= OldPrefixFlags
;
419 Fast486PerformInterrupt(PFAST486_STATE State
,
422 FAST486_IDT_ENTRY IdtEntry
;
424 /* Get the interrupt vector */
425 if (!Fast486GetIntVector(State
, Number
, &IdtEntry
))
427 /* Exception occurred */
431 /* Perform the interrupt */
432 if (!Fast486InterruptInternal(State
, &IdtEntry
))
434 /* Exception occurred */
443 Fast486ExceptionWithErrorCode(PFAST486_STATE State
,
444 FAST486_EXCEPTIONS ExceptionCode
,
447 /* Increment the exception count */
448 State
->ExceptionCount
++;
450 /* Check if the exception occurred more than once */
451 if (State
->ExceptionCount
> 1)
453 /* Then this is a double fault */
454 ExceptionCode
= FAST486_EXCEPTION_DF
;
457 /* Check if this is a triple fault */
458 if (State
->ExceptionCount
== 3)
460 DPRINT("Fast486ExceptionWithErrorCode(%04X:%08X) -- Triple fault\n",
461 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
462 State
->InstPtr
.Long
);
469 /* Restore the IP to the saved IP */
470 State
->InstPtr
= State
->SavedInstPtr
;
472 /* Perform the interrupt */
473 if (!Fast486PerformInterrupt(State
, ExceptionCode
))
476 * If this function failed, that means Fast486Exception
477 * was called again, so just return in this case.
482 if (EXCEPTION_HAS_ERROR_CODE(ExceptionCode
)
483 && (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
))
485 /* Push the error code */
486 if (!Fast486StackPush(State
, ErrorCode
))
489 * If this function failed, that means Fast486Exception
490 * was called again, so just return in this case.
496 /* Reset the exception count */
497 State
->ExceptionCount
= 0;
502 Fast486TaskSwitch(PFAST486_STATE State
, FAST486_TASK_SWITCH_TYPE Type
, USHORT Selector
)
508 FAST486_SYSTEM_DESCRIPTOR NewTssDescriptor
;
510 /* Read the old TSS */
511 if (!Fast486ReadLinearMemory(State
,
516 /* Exception occurred */
520 /* If this is a task return, use the linked previous selector */
521 if (Type
== FAST486_TASK_RETURN
) Selector
= LOWORD(OldTss
.Link
);
523 /* Make sure the entry exists in the GDT (not LDT!) */
524 if ((GET_SEGMENT_INDEX(Selector
) == 0)
525 || (Selector
& SEGMENT_TABLE_INDICATOR
)
526 || GET_SEGMENT_INDEX(Selector
) >= (State
->Gdtr
.Size
+ 1))
528 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, Selector
);
532 /* Get the TSS descriptor from the GDT */
533 if (!Fast486ReadLinearMemory(State
,
534 State
->Gdtr
.Address
+ GET_SEGMENT_INDEX(Selector
),
536 sizeof(NewTssDescriptor
)))
538 /* Exception occurred */
542 if (!NewTssDescriptor
.Present
)
544 /* Incoming task TSS not present */
545 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_NP
, Selector
);
549 /* Calculate the linear address of the new TSS */
550 NewTssAddress
= NewTssDescriptor
.Base
;
551 NewTssAddress
|= NewTssDescriptor
.BaseMid
<< 16;
552 NewTssAddress
|= NewTssDescriptor
.BaseHigh
<< 24;
554 /* Calculate the limit of the new TSS */
555 NewTssLimit
= NewTssDescriptor
.Limit
| (NewTssDescriptor
.LimitHigh
<< 16);
557 if (NewTssDescriptor
.Granularity
)
560 NewTssLimit
|= 0x00000FFF;
563 if (NewTssLimit
< sizeof(FAST486_TSS
))
565 /* TSS limit too small */
566 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, Selector
);
570 * The incoming task shouldn't be busy if we're executing it as a
571 * new task, and it should be busy if we're returning to it.
573 if (((NewTssDescriptor
.Signature
!= FAST486_TSS_SIGNATURE
)
574 || (Type
== FAST486_TASK_RETURN
))
575 && ((NewTssDescriptor
.Signature
!= FAST486_BUSY_TSS_SIGNATURE
)
576 || (Type
!= FAST486_TASK_RETURN
)))
578 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_GP
, Selector
);
582 /* Read the new TSS */
583 if (!Fast486ReadLinearMemory(State
,
588 /* Exception occurred */
592 if (Type
!= FAST486_TASK_CALL
)
594 /* Clear the busy bit of the outgoing task */
595 FAST486_SYSTEM_DESCRIPTOR OldTssDescriptor
;
597 if (!Fast486ReadLinearMemory(State
,
599 + GET_SEGMENT_INDEX(State
->TaskReg
.Selector
),
601 sizeof(OldTssDescriptor
)))
603 /* Exception occurred */
607 OldTssDescriptor
.Signature
= FAST486_TSS_SIGNATURE
;
609 if (!Fast486WriteLinearMemory(State
,
611 + GET_SEGMENT_INDEX(State
->TaskReg
.Selector
),
613 sizeof(OldTssDescriptor
)))
615 /* Exception occurred */
622 NewTss
.Link
= State
->TaskReg
.Selector
;
625 /* Save the current task into the TSS */
626 OldTss
.Cr3
= State
->ControlRegisters
[FAST486_REG_CR3
];
627 OldTss
.Eip
= State
->InstPtr
.Long
;
628 OldTss
.Eflags
= State
->Flags
.Long
;
629 OldTss
.Eax
= State
->GeneralRegs
[FAST486_REG_EAX
].Long
;
630 OldTss
.Ecx
= State
->GeneralRegs
[FAST486_REG_ECX
].Long
;
631 OldTss
.Edx
= State
->GeneralRegs
[FAST486_REG_EDX
].Long
;
632 OldTss
.Ebx
= State
->GeneralRegs
[FAST486_REG_EBX
].Long
;
633 OldTss
.Esp
= State
->GeneralRegs
[FAST486_REG_ESP
].Long
;
634 OldTss
.Ebp
= State
->GeneralRegs
[FAST486_REG_EBP
].Long
;
635 OldTss
.Esi
= State
->GeneralRegs
[FAST486_REG_ESI
].Long
;
636 OldTss
.Edi
= State
->GeneralRegs
[FAST486_REG_EDI
].Long
;
637 OldTss
.Es
= State
->SegmentRegs
[FAST486_REG_ES
].Selector
;
638 OldTss
.Cs
= State
->SegmentRegs
[FAST486_REG_CS
].Selector
;
639 OldTss
.Ss
= State
->SegmentRegs
[FAST486_REG_SS
].Selector
;
640 OldTss
.Ds
= State
->SegmentRegs
[FAST486_REG_DS
].Selector
;
641 OldTss
.Fs
= State
->SegmentRegs
[FAST486_REG_FS
].Selector
;
642 OldTss
.Gs
= State
->SegmentRegs
[FAST486_REG_GS
].Selector
;
643 OldTss
.Ldtr
= State
->Ldtr
.Selector
;
645 /* Write back the old TSS */
646 if (!Fast486WriteLinearMemory(State
,
651 /* Exception occurred */
655 /* Mark the new task as busy */
656 NewTssDescriptor
.Signature
= FAST486_BUSY_TSS_SIGNATURE
;
658 /* Write back the new TSS descriptor */
659 if (!Fast486WriteLinearMemory(State
,
660 State
->Gdtr
.Address
+ GET_SEGMENT_INDEX(Selector
),
662 sizeof(NewTssDescriptor
)))
664 /* Exception occurred */
668 /* Set the task switch bit */
669 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_TS
;
671 /* Load the task register with the new values */
672 State
->TaskReg
.Selector
= Selector
;
673 State
->TaskReg
.Base
= NewTssAddress
;
674 State
->TaskReg
.Limit
= NewTssLimit
;
676 /* Change the page directory */
677 State
->ControlRegisters
[FAST486_REG_CR3
] = NewTss
.Cr3
;
680 if (State
->Tlb
) RtlZeroMemory(State
->Tlb
, NUM_TLB_ENTRIES
* sizeof(ULONG
));
682 #ifndef FAST486_NO_PREFETCH
683 /* Context switching invalidates the prefetch */
684 State
->PrefetchValid
= FALSE
;
687 /* Load the registers */
688 State
->InstPtr
.Long
= State
->SavedInstPtr
.Long
= NewTss
.Eip
;
689 State
->Flags
.Long
= NewTss
.Eflags
;
690 State
->GeneralRegs
[FAST486_REG_EAX
].Long
= NewTss
.Eax
;
691 State
->GeneralRegs
[FAST486_REG_ECX
].Long
= NewTss
.Ecx
;
692 State
->GeneralRegs
[FAST486_REG_EDX
].Long
= NewTss
.Edx
;
693 State
->GeneralRegs
[FAST486_REG_EBX
].Long
= NewTss
.Ebx
;
694 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= NewTss
.Esp
;
695 State
->GeneralRegs
[FAST486_REG_EBP
].Long
= NewTss
.Ebp
;
696 State
->GeneralRegs
[FAST486_REG_ESI
].Long
= NewTss
.Esi
;
697 State
->GeneralRegs
[FAST486_REG_EDI
].Long
= NewTss
.Edi
;
699 /* Set the NT flag if nesting */
700 if (Type
== FAST486_TASK_CALL
) State
->Flags
.Nt
= TRUE
;
702 if (GET_SEGMENT_INDEX(NewTss
.Ldtr
) != 0)
705 FAST486_SYSTEM_DESCRIPTOR GdtEntry
;
707 if (NewTss
.Ldtr
& SEGMENT_TABLE_INDICATOR
)
709 /* This selector doesn't point to the GDT */
710 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
714 if (!Fast486ReadDescriptorEntry(State
,
717 (PFAST486_GDT_ENTRY
)&GdtEntry
))
719 /* Exception occurred */
725 /* Invalid selector */
726 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
730 if (GdtEntry
.Signature
!= FAST486_LDT_SIGNATURE
)
732 /* This is not an LDT descriptor */
733 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
737 if (!GdtEntry
.Present
)
739 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
743 /* Update the LDTR */
744 State
->Ldtr
.Selector
= NewTss
.Ldtr
;
745 State
->Ldtr
.Base
= GdtEntry
.Base
| (GdtEntry
.BaseMid
<< 16) | (GdtEntry
.BaseHigh
<< 24);
746 State
->Ldtr
.Limit
= GdtEntry
.Limit
| (GdtEntry
.LimitHigh
<< 16);
748 if (GdtEntry
.Granularity
)
750 State
->Ldtr
.Limit
<<= 12;
751 State
->Ldtr
.Limit
|= 0x00000FFF;
756 /* The LDT of this task is empty */
757 RtlZeroMemory(&State
->Ldtr
, sizeof(State
->Ldtr
));
760 /* Load the new segments */
761 if (!Fast486LoadSegmentInternal(State
,
764 FAST486_EXCEPTION_TS
))
769 if (!Fast486LoadSegmentInternal(State
,
772 FAST486_EXCEPTION_TS
))
777 if (!Fast486LoadSegmentInternal(State
,
780 FAST486_EXCEPTION_TS
))
785 if (!Fast486LoadSegmentInternal(State
,
788 FAST486_EXCEPTION_TS
))
793 if (!Fast486LoadSegmentInternal(State
,
796 FAST486_EXCEPTION_TS
))
801 if (!Fast486LoadSegmentInternal(State
,
804 FAST486_EXCEPTION_TS
))