TianoCore EDK2 master
Loading...
Searching...
No Matches
X86Thunk.c
Go to the documentation of this file.
1
9#include "BaseLibInternals.h"
10
11extern CONST UINT8 m16Start;
12extern CONST UINT16 m16Size;
13extern CONST UINT16 mThunk16Attr;
14extern CONST UINT16 m16Gdt;
15extern CONST UINT16 m16GdtrBase;
16extern CONST UINT16 mTransition;
17
32IA32_REGISTER_SET *
33EFIAPI
35 IN IA32_REGISTER_SET *RegisterSet,
36 IN OUT VOID *Transition
37 );
38
60VOID
61EFIAPI
63 OUT UINT32 *RealModeBufferSize,
64 OUT UINT32 *ExtraStackSize
65 )
66{
67 ASSERT (RealModeBufferSize != NULL);
68 ASSERT (ExtraStackSize != NULL);
69
70 *RealModeBufferSize = m16Size;
71
72 //
73 // Extra 4 bytes for return address, and another 4 bytes for mode transition
74 //
75 *ExtraStackSize = sizeof (IA32_DWORD_REGS) + 8;
76}
77
92VOID
93EFIAPI
95 IN OUT THUNK_CONTEXT *ThunkContext
96 )
97{
98 IA32_SEGMENT_DESCRIPTOR *RealModeGdt;
99
100 ASSERT (ThunkContext != NULL);
101 ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);
102 ASSERT (ThunkContext->RealModeBufferSize >= m16Size);
103 ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);
104
105 CopyMem (ThunkContext->RealModeBuffer, &m16Start, m16Size);
106
107 //
108 // Point RealModeGdt to the GDT to be used in transition
109 //
110 // RealModeGdt[0]: Reserved as NULL descriptor
111 // RealModeGdt[1]: Code Segment
112 // RealModeGdt[2]: Data Segment
113 // RealModeGdt[3]: Call Gate
114 //
115 RealModeGdt = (IA32_SEGMENT_DESCRIPTOR *)(
116 (UINTN)ThunkContext->RealModeBuffer + m16Gdt);
117
118 //
119 // Update Code & Data Segment Descriptor
120 //
121 RealModeGdt[1].Bits.BaseLow =
122 (UINT32)(UINTN)ThunkContext->RealModeBuffer & ~0xf;
123 RealModeGdt[1].Bits.BaseMid =
124 (UINT32)(UINTN)ThunkContext->RealModeBuffer >> 16;
125
126 //
127 // Update transition code entry point offset
128 //
129 *(UINT32 *)((UINTN)ThunkContext->RealModeBuffer + mTransition) +=
130 (UINT32)(UINTN)ThunkContext->RealModeBuffer & 0xf;
131
132 //
133 // Update Segment Limits for both Code and Data Segment Descriptors
134 //
135 if ((ThunkContext->ThunkAttributes & THUNK_ATTRIBUTE_BIG_REAL_MODE) == 0) {
136 //
137 // Set segment limits to 64KB
138 //
139 RealModeGdt[1].Bits.LimitHigh = 0;
140 RealModeGdt[1].Bits.G = 0;
141 RealModeGdt[2].Bits.LimitHigh = 0;
142 RealModeGdt[2].Bits.G = 0;
143 }
144
145 //
146 // Update GDTBASE for this thunk context
147 //
148 *(VOID **)((UINTN)ThunkContext->RealModeBuffer + m16GdtrBase) = RealModeGdt;
149
150 //
151 // Update Thunk Attributes
152 //
153 *(UINT32 *)((UINTN)ThunkContext->RealModeBuffer + mThunk16Attr) =
154 ThunkContext->ThunkAttributes;
155}
156
210VOID
211EFIAPI
213 IN OUT THUNK_CONTEXT *ThunkContext
214 )
215{
216 IA32_REGISTER_SET *UpdatedRegs;
217
218 ASSERT (ThunkContext != NULL);
219 ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);
220 ASSERT (ThunkContext->RealModeBufferSize >= m16Size);
221 ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);
222 ASSERT (
223 ((ThunkContext->ThunkAttributes & (THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 | THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL)) != \
224 (THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 | THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL))
225 );
226
227 UpdatedRegs = InternalAsmThunk16 (
228 ThunkContext->RealModeState,
229 ThunkContext->RealModeBuffer
230 );
231
232 CopyMem (ThunkContext->RealModeState, UpdatedRegs, sizeof (*UpdatedRegs));
233}
234
255VOID
256EFIAPI
258 IN OUT THUNK_CONTEXT *ThunkContext
259 )
260{
261 AsmPrepareThunk16 (ThunkContext);
262 AsmThunk16 (ThunkContext);
263}
UINT64 UINTN
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
VOID EFIAPI AsmGetThunk16Properties(OUT UINT32 *RealModeBufferSize, OUT UINT32 *ExtraStackSize)
Definition: X86Thunk.c:62
VOID EFIAPI AsmPrepareThunk16(IN OUT THUNK_CONTEXT *ThunkContext)
Definition: X86Thunk.c:94
VOID EFIAPI AsmPrepareAndThunk16(IN OUT THUNK_CONTEXT *ThunkContext)
Definition: X86Thunk.c:257
VOID EFIAPI AsmThunk16(IN OUT THUNK_CONTEXT *ThunkContext)
Definition: X86Thunk.c:212
IA32_REGISTER_SET *EFIAPI InternalAsmThunk16(IN IA32_REGISTER_SET *RegisterSet, IN OUT VOID *Transition)