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
);
112 if (Fast486ReadLinearMemory(State
,
113 State
->PrefetchAddress
,
114 State
->PrefetchCache
,
117 State
->PrefetchValid
= TRUE
;
119 RtlMoveMemory(Buffer
,
120 &State
->PrefetchCache
[LinearAddress
- State
->PrefetchAddress
],
126 State
->PrefetchValid
= FALSE
;
133 /* Read from the linear address */
134 return Fast486ReadLinearMemory(State
, LinearAddress
, Buffer
, Size
);
139 Fast486WriteMemory(PFAST486_STATE State
,
140 FAST486_SEG_REGS SegmentReg
,
146 PFAST486_SEG_REG CachedDescriptor
;
148 ASSERT(SegmentReg
< FAST486_NUM_SEG_REGS
);
150 /* Get the cached descriptor */
151 CachedDescriptor
= &State
->SegmentRegs
[SegmentReg
];
153 if ((Offset
+ Size
- 1) > CachedDescriptor
->Limit
)
155 /* Write beyond limit */
156 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
160 /* Check for protected mode */
161 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
163 /* Privilege checks */
165 if (!CachedDescriptor
->Present
)
167 Fast486Exception(State
, FAST486_EXCEPTION_NP
);
171 if ((CachedDescriptor
->Rpl
> CachedDescriptor
->Dpl
)
172 || (Fast486GetCurrentPrivLevel(State
) > CachedDescriptor
->Dpl
))
174 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
178 if (CachedDescriptor
->Executable
)
180 /* Code segment not writable */
181 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
184 else if (!CachedDescriptor
->ReadWrite
)
186 /* Data segment not writeable */
187 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
192 /* Find the linear address */
193 LinearAddress
= CachedDescriptor
->Base
+ Offset
;
195 #ifndef FAST486_NO_PREFETCH
196 if (State
->PrefetchValid
197 && (LinearAddress
>= State
->PrefetchAddress
)
198 && ((LinearAddress
+ Size
) <= (State
->PrefetchAddress
+ FAST486_CACHE_SIZE
)))
200 /* Update the prefetch */
201 RtlMoveMemory(&State
->PrefetchCache
[LinearAddress
- State
->PrefetchAddress
],
203 min(Size
, FAST486_CACHE_SIZE
+ State
->PrefetchAddress
- LinearAddress
));
207 /* Write to the linear address */
208 return Fast486WriteLinearMemory(State
, LinearAddress
, Buffer
, Size
);
211 static inline BOOLEAN
213 Fast486GetIntVector(PFAST486_STATE State
,
215 PFAST486_IDT_ENTRY IdtEntry
)
217 /* Check for protected mode */
218 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
220 /* Read from the IDT */
221 if (!Fast486ReadLinearMemory(State
,
223 + Number
* sizeof(*IdtEntry
),
227 /* Exception occurred */
233 /* Read from the real-mode IVT */
236 /* Paging is always disabled in real mode */
237 State
->MemReadCallback(State
,
239 + Number
* sizeof(FarPointer
),
243 /* Fill a fake IDT entry */
244 IdtEntry
->Offset
= LOWORD(FarPointer
);
245 IdtEntry
->Selector
= HIWORD(FarPointer
);
247 IdtEntry
->Type
= FAST486_IDT_INT_GATE
;
248 IdtEntry
->Storage
= FALSE
;
250 IdtEntry
->Present
= TRUE
;
251 IdtEntry
->OffsetHigh
= 0;
257 static inline BOOLEAN
259 Fast486InterruptInternal(PFAST486_STATE State
,
260 PFAST486_IDT_ENTRY IdtEntry
)
262 USHORT SegmentSelector
= IdtEntry
->Selector
;
263 ULONG Offset
= MAKELONG(IdtEntry
->Offset
, IdtEntry
->OffsetHigh
);
264 ULONG GateType
= IdtEntry
->Type
;
265 BOOLEAN GateSize
= (GateType
== FAST486_IDT_INT_GATE_32
) ||
266 (GateType
== FAST486_IDT_TRAP_GATE_32
);
268 BOOLEAN Success
= FALSE
;
269 ULONG OldPrefixFlags
= State
->PrefixFlags
;
271 /* Check for protected mode */
272 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
275 USHORT OldSs
= State
->SegmentRegs
[FAST486_REG_SS
].Selector
;
276 ULONG OldEsp
= State
->GeneralRegs
[FAST486_REG_ESP
].Long
;
278 if (GateType
== FAST486_TASK_GATE_SIGNATURE
)
281 return Fast486TaskSwitch(State
, FAST486_TASK_CALL
, IdtEntry
->Selector
);
284 if (GateSize
!= (State
->SegmentRegs
[FAST486_REG_CS
].Size
))
287 * The gate size doesn't match the current operand size, so toggle
290 State
->PrefixFlags
^= FAST486_PREFIX_OPSIZE
;
293 /* Check if the interrupt handler is more privileged */
294 if (Fast486GetCurrentPrivLevel(State
) > GET_SEGMENT_RPL(SegmentSelector
))
297 if (!Fast486ReadLinearMemory(State
,
302 /* Exception occurred */
306 /* Check the new (higher) privilege level */
307 switch (GET_SEGMENT_RPL(SegmentSelector
))
311 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss0
))
313 /* Exception occurred */
316 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp0
;
323 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss1
))
325 /* Exception occurred */
328 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp1
;
335 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss2
))
337 /* Exception occurred */
340 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp2
;
347 /* Should never reach here! */
352 /* Push SS selector */
353 if (!Fast486StackPush(State
, OldSs
)) goto Cleanup
;
355 /* Push stack pointer */
356 if (!Fast486StackPush(State
, OldEsp
)) goto Cleanup
;
361 if (State
->SegmentRegs
[FAST486_REG_CS
].Size
)
363 /* Set OPSIZE, because INT always pushes 16-bit values in real mode */
364 State
->PrefixFlags
|= FAST486_PREFIX_OPSIZE
;
369 if (!Fast486StackPush(State
, State
->Flags
.Long
)) goto Cleanup
;
371 /* Push CS selector */
372 if (!Fast486StackPush(State
, State
->SegmentRegs
[FAST486_REG_CS
].Selector
)) goto Cleanup
;
374 /* Push the instruction pointer */
375 if (!Fast486StackPush(State
, State
->InstPtr
.Long
)) goto Cleanup
;
377 if ((GateType
== FAST486_IDT_INT_GATE
) || (GateType
== FAST486_IDT_INT_GATE_32
))
379 /* Disable interrupts after a jump to an interrupt gate handler */
380 State
->Flags
.If
= FALSE
;
384 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, SegmentSelector
))
386 /* An exception occurred during the jump */
392 /* 32-bit code segment, use EIP */
393 State
->InstPtr
.Long
= Offset
;
397 /* 16-bit code segment, use IP */
398 State
->InstPtr
.LowWord
= LOWORD(Offset
);
404 /* Restore the prefix flags */
405 State
->PrefixFlags
= OldPrefixFlags
;
412 Fast486PerformInterrupt(PFAST486_STATE State
,
415 FAST486_IDT_ENTRY IdtEntry
;
417 /* Get the interrupt vector */
418 if (!Fast486GetIntVector(State
, Number
, &IdtEntry
))
420 /* Exception occurred */
424 /* Perform the interrupt */
425 if (!Fast486InterruptInternal(State
, &IdtEntry
))
427 /* Exception occurred */
436 Fast486ExceptionWithErrorCode(PFAST486_STATE State
,
437 FAST486_EXCEPTIONS ExceptionCode
,
440 /* Increment the exception count */
441 State
->ExceptionCount
++;
443 /* Check if the exception occurred more than once */
444 if (State
->ExceptionCount
> 1)
446 /* Then this is a double fault */
447 ExceptionCode
= FAST486_EXCEPTION_DF
;
450 /* Check if this is a triple fault */
451 if (State
->ExceptionCount
== 3)
453 DPRINT("Fast486ExceptionWithErrorCode(%04X:%08X) -- Triple fault\n",
454 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
455 State
->InstPtr
.Long
);
462 /* Restore the IP to the saved IP */
463 State
->InstPtr
= State
->SavedInstPtr
;
465 /* Perform the interrupt */
466 if (!Fast486PerformInterrupt(State
, ExceptionCode
))
469 * If this function failed, that means Fast486Exception
470 * was called again, so just return in this case.
475 if (EXCEPTION_HAS_ERROR_CODE(ExceptionCode
)
476 && (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
))
478 /* Push the error code */
479 if (!Fast486StackPush(State
, ErrorCode
))
482 * If this function failed, that means Fast486Exception
483 * was called again, so just return in this case.
489 /* Reset the exception count */
490 State
->ExceptionCount
= 0;
495 Fast486TaskSwitch(PFAST486_STATE State
, FAST486_TASK_SWITCH_TYPE Type
, USHORT Selector
)
501 FAST486_SYSTEM_DESCRIPTOR NewTssDescriptor
;
503 /* Read the old TSS */
504 if (!Fast486ReadLinearMemory(State
,
509 /* Exception occurred */
513 /* If this is a task return, use the linked previous selector */
514 if (Type
== FAST486_TASK_RETURN
) Selector
= LOWORD(OldTss
.Link
);
516 /* Make sure the entry exists in the GDT (not LDT!) */
517 if ((GET_SEGMENT_INDEX(Selector
) == 0)
518 || (Selector
& SEGMENT_TABLE_INDICATOR
)
519 || GET_SEGMENT_INDEX(Selector
) >= (State
->Gdtr
.Size
+ 1))
521 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, Selector
);
525 /* Get the TSS descriptor from the GDT */
526 if (!Fast486ReadLinearMemory(State
,
527 State
->Gdtr
.Address
+ GET_SEGMENT_INDEX(Selector
),
529 sizeof(NewTssDescriptor
)))
531 /* Exception occurred */
535 if (!NewTssDescriptor
.Present
)
537 /* Incoming task TSS not present */
538 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_NP
, Selector
);
542 /* Calculate the linear address of the new TSS */
543 NewTssAddress
= NewTssDescriptor
.Base
;
544 NewTssAddress
|= NewTssDescriptor
.BaseMid
<< 16;
545 NewTssAddress
|= NewTssDescriptor
.BaseHigh
<< 24;
547 /* Calculate the limit of the new TSS */
548 NewTssLimit
= NewTssDescriptor
.Limit
| (NewTssDescriptor
.LimitHigh
<< 16);
549 if (NewTssDescriptor
.Granularity
) NewTssLimit
<<= 12;
551 if (NewTssLimit
< sizeof(FAST486_TSS
))
553 /* TSS limit too small */
554 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, Selector
);
558 * The incoming task shouldn't be busy if we're executing it as a
559 * new task, and it should be busy if we're returning to it.
561 if (((NewTssDescriptor
.Signature
!= FAST486_TSS_SIGNATURE
)
562 || (Type
== FAST486_TASK_RETURN
))
563 && ((NewTssDescriptor
.Signature
!= FAST486_BUSY_TSS_SIGNATURE
)
564 || (Type
!= FAST486_TASK_RETURN
)))
566 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_GP
, Selector
);
570 /* Read the new TSS */
571 if (!Fast486ReadLinearMemory(State
,
576 /* Exception occurred */
580 if (Type
!= FAST486_TASK_CALL
)
582 /* Clear the busy bit of the outgoing task */
583 FAST486_SYSTEM_DESCRIPTOR OldTssDescriptor
;
585 if (!Fast486ReadLinearMemory(State
,
587 + GET_SEGMENT_INDEX(State
->TaskReg
.Selector
),
589 sizeof(OldTssDescriptor
)))
591 /* Exception occurred */
595 OldTssDescriptor
.Signature
= FAST486_TSS_SIGNATURE
;
597 if (!Fast486WriteLinearMemory(State
,
599 + GET_SEGMENT_INDEX(State
->TaskReg
.Selector
),
601 sizeof(OldTssDescriptor
)))
603 /* Exception occurred */
610 NewTss
.Link
= State
->TaskReg
.Selector
;
613 /* Save the current task into the TSS */
614 OldTss
.Cr3
= State
->ControlRegisters
[FAST486_REG_CR3
];
615 OldTss
.Eip
= State
->InstPtr
.Long
;
616 OldTss
.Eflags
= State
->Flags
.Long
;
617 OldTss
.Eax
= State
->GeneralRegs
[FAST486_REG_EAX
].Long
;
618 OldTss
.Ecx
= State
->GeneralRegs
[FAST486_REG_ECX
].Long
;
619 OldTss
.Edx
= State
->GeneralRegs
[FAST486_REG_EDX
].Long
;
620 OldTss
.Ebx
= State
->GeneralRegs
[FAST486_REG_EBX
].Long
;
621 OldTss
.Esp
= State
->GeneralRegs
[FAST486_REG_ESP
].Long
;
622 OldTss
.Ebp
= State
->GeneralRegs
[FAST486_REG_EBP
].Long
;
623 OldTss
.Esi
= State
->GeneralRegs
[FAST486_REG_ESI
].Long
;
624 OldTss
.Edi
= State
->GeneralRegs
[FAST486_REG_EDI
].Long
;
625 OldTss
.Es
= State
->SegmentRegs
[FAST486_REG_ES
].Selector
;
626 OldTss
.Cs
= State
->SegmentRegs
[FAST486_REG_CS
].Selector
;
627 OldTss
.Ss
= State
->SegmentRegs
[FAST486_REG_SS
].Selector
;
628 OldTss
.Ds
= State
->SegmentRegs
[FAST486_REG_DS
].Selector
;
629 OldTss
.Fs
= State
->SegmentRegs
[FAST486_REG_FS
].Selector
;
630 OldTss
.Gs
= State
->SegmentRegs
[FAST486_REG_GS
].Selector
;
631 OldTss
.Ldtr
= State
->Ldtr
.Selector
;
633 /* Write back the old TSS */
634 if (!Fast486WriteLinearMemory(State
,
639 /* Exception occurred */
643 /* Mark the new task as busy */
644 NewTssDescriptor
.Signature
= FAST486_BUSY_TSS_SIGNATURE
;
646 /* Write back the new TSS descriptor */
647 if (!Fast486WriteLinearMemory(State
,
648 State
->Gdtr
.Address
+ GET_SEGMENT_INDEX(Selector
),
650 sizeof(NewTssDescriptor
)))
652 /* Exception occurred */
656 /* Set the task switch bit */
657 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_TS
;
659 /* Load the task register with the new values */
660 State
->TaskReg
.Selector
= Selector
;
661 State
->TaskReg
.Base
= NewTssAddress
;
662 State
->TaskReg
.Limit
= NewTssLimit
;
664 /* Change the page directory */
665 State
->ControlRegisters
[FAST486_REG_CR3
] = NewTss
.Cr3
;
668 if (State
->Tlb
) RtlZeroMemory(State
->Tlb
, NUM_TLB_ENTRIES
* sizeof(ULONG
));
670 #ifndef FAST486_NO_PREFETCH
671 /* Context switching invalidates the prefetch */
672 State
->PrefetchValid
= FALSE
;
675 /* Load the registers */
676 State
->InstPtr
.Long
= State
->SavedInstPtr
.Long
= NewTss
.Eip
;
677 State
->Flags
.Long
= NewTss
.Eflags
;
678 State
->GeneralRegs
[FAST486_REG_EAX
].Long
= NewTss
.Eax
;
679 State
->GeneralRegs
[FAST486_REG_ECX
].Long
= NewTss
.Ecx
;
680 State
->GeneralRegs
[FAST486_REG_EDX
].Long
= NewTss
.Edx
;
681 State
->GeneralRegs
[FAST486_REG_EBX
].Long
= NewTss
.Ebx
;
682 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= NewTss
.Esp
;
683 State
->GeneralRegs
[FAST486_REG_EBP
].Long
= NewTss
.Ebp
;
684 State
->GeneralRegs
[FAST486_REG_ESI
].Long
= NewTss
.Esi
;
685 State
->GeneralRegs
[FAST486_REG_EDI
].Long
= NewTss
.Edi
;
687 /* Set the NT flag if nesting */
688 if (Type
== FAST486_TASK_CALL
) State
->Flags
.Nt
= TRUE
;
690 if (GET_SEGMENT_INDEX(NewTss
.Ldtr
) != 0)
693 FAST486_SYSTEM_DESCRIPTOR GdtEntry
;
695 if (NewTss
.Ldtr
& SEGMENT_TABLE_INDICATOR
)
697 /* This selector doesn't point to the GDT */
698 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
702 if (!Fast486ReadDescriptorEntry(State
,
705 (PFAST486_GDT_ENTRY
)&GdtEntry
))
707 /* Exception occurred */
713 /* Invalid selector */
714 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
718 if (GdtEntry
.Signature
!= FAST486_LDT_SIGNATURE
)
720 /* This is not an LDT descriptor */
721 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
725 if (!GdtEntry
.Present
)
727 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
731 /* Update the LDTR */
732 State
->Ldtr
.Selector
= NewTss
.Ldtr
;
733 State
->Ldtr
.Base
= GdtEntry
.Base
| (GdtEntry
.BaseMid
<< 16) | (GdtEntry
.BaseHigh
<< 24);
734 State
->Ldtr
.Limit
= GdtEntry
.Limit
| (GdtEntry
.LimitHigh
<< 16);
735 if (GdtEntry
.Granularity
) State
->Ldtr
.Limit
<<= 12;
739 /* The LDT of this task is empty */
740 RtlZeroMemory(&State
->Ldtr
, sizeof(State
->Ldtr
));
743 /* Load the new segments */
744 if (!Fast486LoadSegmentInternal(State
,
747 FAST486_EXCEPTION_TS
))
752 if (!Fast486LoadSegmentInternal(State
,
755 FAST486_EXCEPTION_TS
))
760 if (!Fast486LoadSegmentInternal(State
,
763 FAST486_EXCEPTION_TS
))
768 if (!Fast486LoadSegmentInternal(State
,
771 FAST486_EXCEPTION_TS
))
776 if (!Fast486LoadSegmentInternal(State
,
779 FAST486_EXCEPTION_TS
))
784 if (!Fast486LoadSegmentInternal(State
,
787 FAST486_EXCEPTION_TS
))