2 * Fast486 386/486 CPU Emulation Library
5 * Copyright (C) 2015 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 (InstFetch
|| !CachedDescriptor
->DirConf
)
52 if ((Offset
+ Size
- 1) > CachedDescriptor
->Limit
)
54 /* Read beyond limit */
55 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
61 if (Offset
< CachedDescriptor
->Limit
)
63 /* Read beyond limit */
64 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
69 /* Check for protected mode */
70 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
72 /* Privilege checks */
74 if (!CachedDescriptor
->Present
)
76 Fast486Exception(State
, FAST486_EXCEPTION_NP
);
80 if ((!InstFetch
&& (CachedDescriptor
->Rpl
> CachedDescriptor
->Dpl
))
81 || (Fast486GetCurrentPrivLevel(State
) > CachedDescriptor
->Dpl
))
83 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
89 if (!CachedDescriptor
->Executable
)
91 /* Data segment not executable */
92 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
98 if (CachedDescriptor
->Executable
&& (!CachedDescriptor
->ReadWrite
))
100 /* Code segment not readable */
101 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
107 /* Find the linear address */
108 LinearAddress
= CachedDescriptor
->Base
+ Offset
;
110 #ifndef FAST486_NO_PREFETCH
111 if (InstFetch
&& ((Offset
+ FAST486_CACHE_SIZE
- 1) <= CachedDescriptor
->Limit
))
113 State
->PrefetchAddress
= LinearAddress
;
115 if ((State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PG
)
116 && (PAGE_OFFSET(State
->PrefetchAddress
) > (FAST486_PAGE_SIZE
- FAST486_CACHE_SIZE
)))
118 /* We mustn't prefetch across a page boundary */
119 State
->PrefetchAddress
= PAGE_ALIGN(State
->PrefetchAddress
)
120 | (FAST486_PAGE_SIZE
- FAST486_CACHE_SIZE
);
122 if ((LinearAddress
- State
->PrefetchAddress
+ Size
) >= FAST486_CACHE_SIZE
)
124 /* We can't prefetch without possibly violating page permissions */
125 State
->PrefetchValid
= FALSE
;
126 return Fast486ReadLinearMemory(State
, LinearAddress
, Buffer
, Size
);
131 if (Fast486ReadLinearMemory(State
,
132 State
->PrefetchAddress
,
133 State
->PrefetchCache
,
136 State
->PrefetchValid
= TRUE
;
138 RtlMoveMemory(Buffer
,
139 &State
->PrefetchCache
[LinearAddress
- State
->PrefetchAddress
],
145 State
->PrefetchValid
= FALSE
;
152 /* Read from the linear address */
153 return Fast486ReadLinearMemory(State
, LinearAddress
, Buffer
, Size
);
158 Fast486WriteMemory(PFAST486_STATE State
,
159 FAST486_SEG_REGS SegmentReg
,
165 PFAST486_SEG_REG CachedDescriptor
;
167 ASSERT(SegmentReg
< FAST486_NUM_SEG_REGS
);
169 /* Get the cached descriptor */
170 CachedDescriptor
= &State
->SegmentRegs
[SegmentReg
];
172 if (!CachedDescriptor
->DirConf
)
174 if ((Offset
+ Size
- 1) > CachedDescriptor
->Limit
)
176 /* Write beyond limit */
177 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
183 if (Offset
< CachedDescriptor
->Limit
)
185 /* Read beyond limit */
186 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
191 /* Check for protected mode */
192 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
194 /* Privilege checks */
196 if (!CachedDescriptor
->Present
)
198 Fast486Exception(State
, FAST486_EXCEPTION_NP
);
202 if ((CachedDescriptor
->Rpl
> CachedDescriptor
->Dpl
)
203 || (Fast486GetCurrentPrivLevel(State
) > CachedDescriptor
->Dpl
))
205 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
209 if (CachedDescriptor
->Executable
)
211 /* Code segment not writable */
212 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
215 else if (!CachedDescriptor
->ReadWrite
)
217 /* Data segment not writeable */
218 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
223 /* Find the linear address */
224 LinearAddress
= CachedDescriptor
->Base
+ Offset
;
226 #ifndef FAST486_NO_PREFETCH
227 if (State
->PrefetchValid
228 && (LinearAddress
>= State
->PrefetchAddress
)
229 && ((LinearAddress
+ Size
) <= (State
->PrefetchAddress
+ FAST486_CACHE_SIZE
)))
231 /* Update the prefetch */
232 RtlMoveMemory(&State
->PrefetchCache
[LinearAddress
- State
->PrefetchAddress
],
234 min(Size
, FAST486_CACHE_SIZE
+ State
->PrefetchAddress
- LinearAddress
));
238 /* Write to the linear address */
239 return Fast486WriteLinearMemory(State
, LinearAddress
, Buffer
, Size
);
242 static inline BOOLEAN
244 Fast486GetIntVector(PFAST486_STATE State
,
246 PFAST486_IDT_ENTRY IdtEntry
)
248 /* Check for protected mode */
249 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
251 /* Read from the IDT */
252 if (!Fast486ReadLinearMemory(State
,
254 + Number
* sizeof(*IdtEntry
),
258 /* Exception occurred */
264 /* Read from the real-mode IVT */
267 /* Paging is always disabled in real mode */
268 State
->MemReadCallback(State
,
270 + Number
* sizeof(FarPointer
),
274 /* Fill a fake IDT entry */
275 IdtEntry
->Offset
= LOWORD(FarPointer
);
276 IdtEntry
->Selector
= HIWORD(FarPointer
);
278 IdtEntry
->Type
= FAST486_IDT_INT_GATE
;
279 IdtEntry
->Storage
= FALSE
;
281 IdtEntry
->Present
= TRUE
;
282 IdtEntry
->OffsetHigh
= 0;
288 static inline BOOLEAN
290 Fast486InterruptInternal(PFAST486_STATE State
,
291 PFAST486_IDT_ENTRY IdtEntry
,
292 BOOLEAN PushErrorCode
,
295 BOOLEAN GateSize
= (IdtEntry
->Type
== FAST486_IDT_INT_GATE_32
) ||
296 (IdtEntry
->Type
== FAST486_IDT_TRAP_GATE_32
);
297 USHORT OldCs
= State
->SegmentRegs
[FAST486_REG_CS
].Selector
;
298 ULONG OldEip
= State
->InstPtr
.Long
;
299 ULONG OldFlags
= State
->Flags
.Long
;
300 UCHAR OldCpl
= State
->Cpl
;
302 /* Check for protected mode */
303 if (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
)
305 USHORT OldSs
= State
->SegmentRegs
[FAST486_REG_SS
].Selector
;
306 ULONG OldEsp
= State
->GeneralRegs
[FAST486_REG_ESP
].Long
;
308 if (IdtEntry
->Type
== FAST486_TASK_GATE_SIGNATURE
)
311 return Fast486TaskSwitch(State
, FAST486_TASK_CALL
, IdtEntry
->Selector
);
314 /* Check if the interrupt handler is more privileged or if we're in V86 mode */
315 if ((OldCpl
> GET_SEGMENT_RPL(IdtEntry
->Selector
)) || State
->Flags
.Vm
)
320 if (!Fast486ReadLinearMemory(State
,
325 /* Exception occurred */
329 /* Switch to the new privilege level */
330 State
->Cpl
= GET_SEGMENT_RPL(IdtEntry
->Selector
);
332 /* Check the new (higher) privilege level */
337 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss0
))
339 /* Exception occurred */
342 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp0
;
349 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss1
))
351 /* Exception occurred */
354 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp1
;
361 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss2
))
363 /* Exception occurred */
366 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp2
;
373 /* Should never reach here! */
380 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, IdtEntry
->Selector
))
382 /* An exception occurred during the jump */
388 /* 32-bit code segment, use EIP */
389 State
->InstPtr
.Long
= MAKELONG(IdtEntry
->Offset
, IdtEntry
->OffsetHigh
);
393 /* 16-bit code segment, use IP */
394 State
->InstPtr
.LowWord
= IdtEntry
->Offset
;
397 /* Check if the interrupt handler is more privileged or we're in VM86 mode (again) */
398 if ((OldCpl
> GET_SEGMENT_RPL(IdtEntry
->Selector
)) || State
->Flags
.Vm
)
402 /* Clear the VM flag */
403 State
->Flags
.Vm
= FALSE
;
405 /* Push GS, FS, DS and ES */
406 if (!Fast486StackPush(State
, State
->SegmentRegs
[FAST486_REG_GS
].Selector
)) return FALSE
;
407 if (!Fast486StackPush(State
, State
->SegmentRegs
[FAST486_REG_FS
].Selector
)) return FALSE
;
408 if (!Fast486StackPush(State
, State
->SegmentRegs
[FAST486_REG_DS
].Selector
)) return FALSE
;
409 if (!Fast486StackPush(State
, State
->SegmentRegs
[FAST486_REG_ES
].Selector
)) return FALSE
;
411 /* Now load them with NULL selectors, since they are useless in protected mode */
412 if (!Fast486LoadSegment(State
, FAST486_REG_GS
, 0)) return FALSE
;
413 if (!Fast486LoadSegment(State
, FAST486_REG_FS
, 0)) return FALSE
;
414 if (!Fast486LoadSegment(State
, FAST486_REG_DS
, 0)) return FALSE
;
415 if (!Fast486LoadSegment(State
, FAST486_REG_ES
, 0)) return FALSE
;
418 /* Push SS selector */
419 if (!Fast486StackPushInternal(State
, GateSize
, OldSs
)) return FALSE
;
421 /* Push the stack pointer */
422 if (!Fast486StackPushInternal(State
, GateSize
, OldEsp
)) return FALSE
;
428 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, IdtEntry
->Selector
))
430 /* An exception occurred during the jump */
435 State
->InstPtr
.LowWord
= IdtEntry
->Offset
;
439 if (!Fast486StackPushInternal(State
, GateSize
, OldFlags
)) return FALSE
;
441 /* Push CS selector */
442 if (!Fast486StackPushInternal(State
, GateSize
, OldCs
)) return FALSE
;
444 /* Push the instruction pointer */
445 if (!Fast486StackPushInternal(State
, GateSize
, OldEip
)) return FALSE
;
449 /* Push the error code */
450 if (!Fast486StackPushInternal(State
, GateSize
, ErrorCode
)) return FALSE
;
453 if ((IdtEntry
->Type
== FAST486_IDT_INT_GATE
)
454 || (IdtEntry
->Type
== FAST486_IDT_INT_GATE_32
))
456 /* Disable interrupts after a jump to an interrupt gate handler */
457 State
->Flags
.If
= FALSE
;
465 Fast486PerformInterrupt(PFAST486_STATE State
,
468 FAST486_IDT_ENTRY IdtEntry
;
470 /* Get the interrupt vector */
471 if (!Fast486GetIntVector(State
, Number
, &IdtEntry
))
473 /* Exception occurred */
477 /* Perform the interrupt */
478 if (!Fast486InterruptInternal(State
, &IdtEntry
, FALSE
, 0))
480 /* Exception occurred */
489 Fast486ExceptionWithErrorCode(PFAST486_STATE State
,
490 FAST486_EXCEPTIONS ExceptionCode
,
493 FAST486_IDT_ENTRY IdtEntry
;
495 /* Increment the exception count */
496 State
->ExceptionCount
++;
498 /* Check if the exception occurred more than once */
499 if (State
->ExceptionCount
> 1)
501 /* Then this is a double fault */
502 ExceptionCode
= FAST486_EXCEPTION_DF
;
505 /* Check if this is a triple fault */
506 if (State
->ExceptionCount
== 3)
508 DPRINT("Fast486ExceptionWithErrorCode(%04X:%08X) -- Triple fault\n",
509 State
->SegmentRegs
[FAST486_REG_CS
].Selector
,
510 State
->InstPtr
.Long
);
517 /* Clear the prefix flags */
518 State
->PrefixFlags
= 0;
520 /* Restore the IP to the saved IP */
521 State
->InstPtr
= State
->SavedInstPtr
;
523 /* Get the interrupt vector */
524 if (!Fast486GetIntVector(State
, ExceptionCode
, &IdtEntry
))
527 * If this function failed, that means Fast486Exception
528 * was called again, so just return in this case.
533 /* Perform the interrupt */
534 if (!Fast486InterruptInternal(State
,
536 EXCEPTION_HAS_ERROR_CODE(ExceptionCode
)
537 && (State
->ControlRegisters
[FAST486_REG_CR0
] & FAST486_CR0_PE
),
541 * If this function failed, that means Fast486Exception
542 * was called again, so just return in this case.
547 /* Reset the exception count */
548 State
->ExceptionCount
= 0;
553 Fast486TaskSwitch(PFAST486_STATE State
, FAST486_TASK_SWITCH_TYPE Type
, USHORT Selector
)
559 FAST486_SYSTEM_DESCRIPTOR NewTssDescriptor
;
561 /* Read the old TSS */
562 if (!Fast486ReadLinearMemory(State
,
567 /* Exception occurred */
571 /* If this is a task return, use the linked previous selector */
572 if (Type
== FAST486_TASK_RETURN
) Selector
= LOWORD(OldTss
.Link
);
574 /* Make sure the entry exists in the GDT (not LDT!) */
575 if ((GET_SEGMENT_INDEX(Selector
) == 0)
576 || (Selector
& SEGMENT_TABLE_INDICATOR
)
577 || GET_SEGMENT_INDEX(Selector
) >= (State
->Gdtr
.Size
+ 1u))
579 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, Selector
);
583 /* Get the TSS descriptor from the GDT */
584 if (!Fast486ReadLinearMemory(State
,
585 State
->Gdtr
.Address
+ GET_SEGMENT_INDEX(Selector
),
587 sizeof(NewTssDescriptor
)))
589 /* Exception occurred */
593 if (!NewTssDescriptor
.Present
)
595 /* Incoming task TSS not present */
596 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_NP
, Selector
);
600 /* Calculate the linear address of the new TSS */
601 NewTssAddress
= NewTssDescriptor
.Base
;
602 NewTssAddress
|= NewTssDescriptor
.BaseMid
<< 16;
603 NewTssAddress
|= NewTssDescriptor
.BaseHigh
<< 24;
605 /* Calculate the limit of the new TSS */
606 NewTssLimit
= NewTssDescriptor
.Limit
| (NewTssDescriptor
.LimitHigh
<< 16);
608 if (NewTssDescriptor
.Granularity
)
611 NewTssLimit
|= 0x00000FFF;
614 if (NewTssLimit
< sizeof(FAST486_TSS
))
616 /* TSS limit too small */
617 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, Selector
);
622 * The incoming task shouldn't be busy if we're executing it as a
623 * new task, and it should be busy if we're returning to it.
625 if (((NewTssDescriptor
.Signature
!= FAST486_TSS_SIGNATURE
)
626 || (Type
== FAST486_TASK_RETURN
))
627 && ((NewTssDescriptor
.Signature
!= FAST486_BUSY_TSS_SIGNATURE
)
628 || (Type
!= FAST486_TASK_RETURN
)))
630 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_GP
, Selector
);
634 /* Read the new TSS */
635 if (!Fast486ReadLinearMemory(State
,
640 /* Exception occurred */
644 if (Type
!= FAST486_TASK_CALL
)
646 /* Clear the busy bit of the outgoing task */
647 FAST486_SYSTEM_DESCRIPTOR OldTssDescriptor
;
649 if (!Fast486ReadLinearMemory(State
,
651 + GET_SEGMENT_INDEX(State
->TaskReg
.Selector
),
653 sizeof(OldTssDescriptor
)))
655 /* Exception occurred */
659 OldTssDescriptor
.Signature
= FAST486_TSS_SIGNATURE
;
661 if (!Fast486WriteLinearMemory(State
,
663 + GET_SEGMENT_INDEX(State
->TaskReg
.Selector
),
665 sizeof(OldTssDescriptor
)))
667 /* Exception occurred */
674 NewTss
.Link
= State
->TaskReg
.Selector
;
677 /* Save the current task into the TSS */
678 OldTss
.Cr3
= State
->ControlRegisters
[FAST486_REG_CR3
];
679 OldTss
.Eip
= State
->InstPtr
.Long
;
680 OldTss
.Eflags
= State
->Flags
.Long
;
681 OldTss
.Eax
= State
->GeneralRegs
[FAST486_REG_EAX
].Long
;
682 OldTss
.Ecx
= State
->GeneralRegs
[FAST486_REG_ECX
].Long
;
683 OldTss
.Edx
= State
->GeneralRegs
[FAST486_REG_EDX
].Long
;
684 OldTss
.Ebx
= State
->GeneralRegs
[FAST486_REG_EBX
].Long
;
685 OldTss
.Esp
= State
->GeneralRegs
[FAST486_REG_ESP
].Long
;
686 OldTss
.Ebp
= State
->GeneralRegs
[FAST486_REG_EBP
].Long
;
687 OldTss
.Esi
= State
->GeneralRegs
[FAST486_REG_ESI
].Long
;
688 OldTss
.Edi
= State
->GeneralRegs
[FAST486_REG_EDI
].Long
;
689 OldTss
.Es
= State
->SegmentRegs
[FAST486_REG_ES
].Selector
;
690 OldTss
.Cs
= State
->SegmentRegs
[FAST486_REG_CS
].Selector
;
691 OldTss
.Ss
= State
->SegmentRegs
[FAST486_REG_SS
].Selector
;
692 OldTss
.Ds
= State
->SegmentRegs
[FAST486_REG_DS
].Selector
;
693 OldTss
.Fs
= State
->SegmentRegs
[FAST486_REG_FS
].Selector
;
694 OldTss
.Gs
= State
->SegmentRegs
[FAST486_REG_GS
].Selector
;
695 OldTss
.Ldtr
= State
->Ldtr
.Selector
;
697 /* Write back the old TSS */
698 if (!Fast486WriteLinearMemory(State
,
703 /* Exception occurred */
707 /* Mark the new task as busy */
708 NewTssDescriptor
.Signature
= FAST486_BUSY_TSS_SIGNATURE
;
710 /* Write back the new TSS descriptor */
711 if (!Fast486WriteLinearMemory(State
,
712 State
->Gdtr
.Address
+ GET_SEGMENT_INDEX(Selector
),
714 sizeof(NewTssDescriptor
)))
716 /* Exception occurred */
720 /* Set the task switch bit */
721 State
->ControlRegisters
[FAST486_REG_CR0
] |= FAST486_CR0_TS
;
723 /* Load the task register with the new values */
724 State
->TaskReg
.Selector
= Selector
;
725 State
->TaskReg
.Base
= NewTssAddress
;
726 State
->TaskReg
.Limit
= NewTssLimit
;
728 /* Change the page directory */
729 State
->ControlRegisters
[FAST486_REG_CR3
] = NewTss
.Cr3
;
732 if (State
->Tlb
) RtlZeroMemory(State
->Tlb
, NUM_TLB_ENTRIES
* sizeof(ULONG
));
735 State
->Cpl
= GET_SEGMENT_RPL(NewTss
.Cs
);
737 #ifndef FAST486_NO_PREFETCH
738 /* Context switching invalidates the prefetch */
739 State
->PrefetchValid
= FALSE
;
743 State
->Cpl
= GET_SEGMENT_RPL(NewTss
.Cs
);
745 /* Load the registers */
746 State
->InstPtr
.Long
= State
->SavedInstPtr
.Long
= NewTss
.Eip
;
747 State
->Flags
.Long
= NewTss
.Eflags
;
748 State
->GeneralRegs
[FAST486_REG_EAX
].Long
= NewTss
.Eax
;
749 State
->GeneralRegs
[FAST486_REG_ECX
].Long
= NewTss
.Ecx
;
750 State
->GeneralRegs
[FAST486_REG_EDX
].Long
= NewTss
.Edx
;
751 State
->GeneralRegs
[FAST486_REG_EBX
].Long
= NewTss
.Ebx
;
752 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= NewTss
.Esp
;
753 State
->GeneralRegs
[FAST486_REG_EBP
].Long
= NewTss
.Ebp
;
754 State
->GeneralRegs
[FAST486_REG_ESI
].Long
= NewTss
.Esi
;
755 State
->GeneralRegs
[FAST486_REG_EDI
].Long
= NewTss
.Edi
;
757 /* Set the NT flag if nesting */
758 if (Type
== FAST486_TASK_CALL
) State
->Flags
.Nt
= TRUE
;
760 if (GET_SEGMENT_INDEX(NewTss
.Ldtr
) != 0)
763 FAST486_SYSTEM_DESCRIPTOR GdtEntry
;
765 if (NewTss
.Ldtr
& SEGMENT_TABLE_INDICATOR
)
767 /* This selector doesn't point to the GDT */
768 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
772 if (!Fast486ReadDescriptorEntry(State
,
775 (PFAST486_GDT_ENTRY
)&GdtEntry
))
777 /* Exception occurred */
783 /* Invalid selector */
784 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
788 if (GdtEntry
.Signature
!= FAST486_LDT_SIGNATURE
)
790 /* This is not an LDT descriptor */
791 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
795 if (!GdtEntry
.Present
)
797 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_TS
, NewTss
.Ldtr
);
801 /* Update the LDTR */
802 State
->Ldtr
.Selector
= NewTss
.Ldtr
;
803 State
->Ldtr
.Base
= GdtEntry
.Base
| (GdtEntry
.BaseMid
<< 16) | (GdtEntry
.BaseHigh
<< 24);
804 State
->Ldtr
.Limit
= GdtEntry
.Limit
| (GdtEntry
.LimitHigh
<< 16);
806 if (GdtEntry
.Granularity
)
808 State
->Ldtr
.Limit
<<= 12;
809 State
->Ldtr
.Limit
|= 0x00000FFF;
814 /* The LDT of this task is empty */
815 RtlZeroMemory(&State
->Ldtr
, sizeof(State
->Ldtr
));
818 /* Load the new segments */
819 if (!Fast486LoadSegmentInternal(State
,
822 FAST486_EXCEPTION_TS
))
827 if (!Fast486LoadSegmentInternal(State
,
830 FAST486_EXCEPTION_TS
))
835 if (!Fast486LoadSegmentInternal(State
,
838 FAST486_EXCEPTION_TS
))
843 if (!Fast486LoadSegmentInternal(State
,
846 FAST486_EXCEPTION_TS
))
851 if (!Fast486LoadSegmentInternal(State
,
854 FAST486_EXCEPTION_TS
))
859 if (!Fast486LoadSegmentInternal(State
,
862 FAST486_EXCEPTION_TS
))
872 Fast486CallGate(PFAST486_STATE State
,
873 PFAST486_CALL_GATE Gate
,
877 FAST486_GDT_ENTRY NewCodeSegment
;
878 BOOLEAN GateSize
= (Gate
->Type
== FAST486_CALL_GATE_SIGNATURE
);
880 USHORT OldCs
= State
->SegmentRegs
[FAST486_REG_CS
].Selector
;
881 ULONG OldEip
= State
->InstPtr
.Long
;
882 USHORT OldCpl
= State
->Cpl
;
883 USHORT OldSs
= State
->SegmentRegs
[FAST486_REG_SS
].Selector
;
884 ULONG OldEsp
= State
->GeneralRegs
[FAST486_REG_ESP
].Long
;
885 ULONG ParamBuffer
[32]; /* Maximum possible size - 32 DWORDs */
886 PULONG LongParams
= (PULONG
)ParamBuffer
;
887 PUSHORT ShortParams
= (PUSHORT
)ParamBuffer
;
891 /* The code segment is NULL */
892 Fast486Exception(State
, FAST486_EXCEPTION_GP
);
896 if (!Fast486ReadDescriptorEntry(State
, Gate
->Selector
, &Valid
, &NewCodeSegment
))
898 /* Exception occurred */
902 if (!Valid
|| (NewCodeSegment
.Dpl
> Fast486GetCurrentPrivLevel(State
)))
904 /* Code segment invalid */
905 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_GP
, Gate
->Selector
);
909 if (Call
&& Gate
->ParamCount
)
911 /* Read the parameters */
912 if (!Fast486ReadMemory(State
,
917 Gate
->ParamCount
* sizeof(ULONG
)))
919 /* Exception occurred */
924 /* Check if the new code segment is more privileged */
925 if (NewCodeSegment
.Dpl
< OldCpl
)
930 if (!Fast486ReadLinearMemory(State
,
935 /* Exception occurred */
939 /* Switch to the new privilege level */
940 State
->Cpl
= NewCodeSegment
.Dpl
;
942 /* Check the new (higher) privilege level */
947 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss0
))
949 /* Exception occurred */
952 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp0
;
959 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss1
))
961 /* Exception occurred */
964 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp1
;
971 if (!Fast486LoadSegment(State
, FAST486_REG_SS
, Tss
.Ss2
))
973 /* Exception occurred */
976 State
->GeneralRegs
[FAST486_REG_ESP
].Long
= Tss
.Esp2
;
983 /* Should never reach here! */
988 else if (!NewCodeSegment
.DirConf
)
990 /* This is not allowed for jumps */
991 Fast486ExceptionWithErrorCode(State
, FAST486_EXCEPTION_GP
, Gate
->Selector
);
997 if (!Fast486LoadSegment(State
, FAST486_REG_CS
, Gate
->Selector
))
999 /* An exception occurred during the jump */
1003 /* Set the instruction pointer */
1004 if (GateSize
) State
->InstPtr
.Long
= MAKELONG(Gate
->Offset
, Gate
->OffsetHigh
);
1005 else State
->InstPtr
.Long
= Gate
->Offset
;
1011 /* Check if the new code segment is more privileged (again) */
1012 if (NewCodeSegment
.Dpl
< OldCpl
)
1014 /* Push SS selector */
1015 if (!Fast486StackPushInternal(State
, GateSize
, OldSs
)) return FALSE
;
1017 /* Push stack pointer */
1018 if (!Fast486StackPushInternal(State
, GateSize
, OldEsp
)) return FALSE
;
1021 /* Push the parameters in reverse order */
1022 for (i
= Gate
->ParamCount
- 1; i
>= 0; i
--)
1024 if (!Fast486StackPushInternal(State
,
1026 GateSize
? LongParams
[i
] : ShortParams
[i
]))
1028 /* Exception occurred */
1033 /* Push the parameter count */
1034 if (!Fast486StackPushInternal(State
, GateSize
, Gate
->ParamCount
)) return FALSE
;
1036 /* Push CS selector */
1037 if (!Fast486StackPushInternal(State
, GateSize
, OldCs
)) return FALSE
;
1039 /* Push the instruction pointer */
1040 if (!Fast486StackPushInternal(State
, GateSize
, OldEip
)) return FALSE
;