1 | /** @file
|
---|
2 | Real Mode Thunk Functions for IA32 and x64.
|
---|
3 |
|
---|
4 | Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 |
|
---|
7 | **/
|
---|
8 |
|
---|
9 |
|
---|
10 | #include "BaseLibInternals.h"
|
---|
11 |
|
---|
12 | extern CONST UINT8 m16Start;
|
---|
13 | extern CONST UINT16 m16Size;
|
---|
14 | extern CONST UINT16 mThunk16Attr;
|
---|
15 | extern CONST UINT16 m16Gdt;
|
---|
16 | extern CONST UINT16 m16GdtrBase;
|
---|
17 | extern CONST UINT16 mTransition;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | Invokes 16-bit code in big real mode and returns the updated register set.
|
---|
21 |
|
---|
22 | This function transfers control to the 16-bit code specified by CS:EIP using
|
---|
23 | the stack specified by SS:ESP in RegisterSet. The updated registers are saved
|
---|
24 | on the real mode stack and the starting address of the save area is returned.
|
---|
25 |
|
---|
26 | @param RegisterSet Values of registers before invocation of 16-bit code.
|
---|
27 | @param Transition The pointer to the transition code under 1MB.
|
---|
28 |
|
---|
29 | @return The pointer to a IA32_REGISTER_SET structure containing the updated
|
---|
30 | register values.
|
---|
31 |
|
---|
32 | **/
|
---|
33 | IA32_REGISTER_SET *
|
---|
34 | EFIAPI
|
---|
35 | InternalAsmThunk16 (
|
---|
36 | IN IA32_REGISTER_SET *RegisterSet,
|
---|
37 | IN OUT VOID *Transition
|
---|
38 | );
|
---|
39 |
|
---|
40 | /**
|
---|
41 | Retrieves the properties for 16-bit thunk functions.
|
---|
42 |
|
---|
43 | Computes the size of the buffer and stack below 1MB required to use the
|
---|
44 | AsmPrepareThunk16(), AsmThunk16() and AsmPrepareAndThunk16() functions. This
|
---|
45 | buffer size is returned in RealModeBufferSize, and the stack size is returned
|
---|
46 | in ExtraStackSize. If parameters are passed to the 16-bit real mode code,
|
---|
47 | then the actual minimum stack size is ExtraStackSize plus the maximum number
|
---|
48 | of bytes that need to be passed to the 16-bit real mode code.
|
---|
49 |
|
---|
50 | If RealModeBufferSize is NULL, then ASSERT().
|
---|
51 | If ExtraStackSize is NULL, then ASSERT().
|
---|
52 |
|
---|
53 | @param RealModeBufferSize A pointer to the size of the buffer below 1MB
|
---|
54 | required to use the 16-bit thunk functions.
|
---|
55 | @param ExtraStackSize A pointer to the extra size of stack below 1MB
|
---|
56 | that the 16-bit thunk functions require for
|
---|
57 | temporary storage in the transition to and from
|
---|
58 | 16-bit real mode.
|
---|
59 |
|
---|
60 | **/
|
---|
61 | VOID
|
---|
62 | EFIAPI
|
---|
63 | AsmGetThunk16Properties (
|
---|
64 | OUT UINT32 *RealModeBufferSize,
|
---|
65 | OUT UINT32 *ExtraStackSize
|
---|
66 | )
|
---|
67 | {
|
---|
68 | ASSERT (RealModeBufferSize != NULL);
|
---|
69 | ASSERT (ExtraStackSize != NULL);
|
---|
70 |
|
---|
71 | *RealModeBufferSize = m16Size;
|
---|
72 |
|
---|
73 | //
|
---|
74 | // Extra 4 bytes for return address, and another 4 bytes for mode transition
|
---|
75 | //
|
---|
76 | *ExtraStackSize = sizeof (IA32_DWORD_REGS) + 8;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | Prepares all structures a code required to use AsmThunk16().
|
---|
81 |
|
---|
82 | Prepares all structures and code required to use AsmThunk16().
|
---|
83 |
|
---|
84 | This interface is limited to be used in either physical mode or virtual modes with paging enabled where the
|
---|
85 | virtual to physical mappings for ThunkContext.RealModeBuffer is mapped 1:1.
|
---|
86 |
|
---|
87 | If ThunkContext is NULL, then ASSERT().
|
---|
88 |
|
---|
89 | @param ThunkContext A pointer to the context structure that describes the
|
---|
90 | 16-bit real mode code to call.
|
---|
91 |
|
---|
92 | **/
|
---|
93 | VOID
|
---|
94 | EFIAPI
|
---|
95 | AsmPrepareThunk16 (
|
---|
96 | IN OUT THUNK_CONTEXT *ThunkContext
|
---|
97 | )
|
---|
98 | {
|
---|
99 | IA32_SEGMENT_DESCRIPTOR *RealModeGdt;
|
---|
100 |
|
---|
101 | ASSERT (ThunkContext != NULL);
|
---|
102 | ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);
|
---|
103 | ASSERT (ThunkContext->RealModeBufferSize >= m16Size);
|
---|
104 | ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);
|
---|
105 |
|
---|
106 | CopyMem (ThunkContext->RealModeBuffer, &m16Start, m16Size);
|
---|
107 |
|
---|
108 | //
|
---|
109 | // Point RealModeGdt to the GDT to be used in transition
|
---|
110 | //
|
---|
111 | // RealModeGdt[0]: Reserved as NULL descriptor
|
---|
112 | // RealModeGdt[1]: Code Segment
|
---|
113 | // RealModeGdt[2]: Data Segment
|
---|
114 | // RealModeGdt[3]: Call Gate
|
---|
115 | //
|
---|
116 | RealModeGdt = (IA32_SEGMENT_DESCRIPTOR*)(
|
---|
117 | (UINTN)ThunkContext->RealModeBuffer + m16Gdt);
|
---|
118 |
|
---|
119 | //
|
---|
120 | // Update Code & Data Segment Descriptor
|
---|
121 | //
|
---|
122 | RealModeGdt[1].Bits.BaseLow =
|
---|
123 | (UINT32)(UINTN)ThunkContext->RealModeBuffer & ~0xf;
|
---|
124 | RealModeGdt[1].Bits.BaseMid =
|
---|
125 | (UINT32)(UINTN)ThunkContext->RealModeBuffer >> 16;
|
---|
126 |
|
---|
127 | //
|
---|
128 | // Update transition code entry point offset
|
---|
129 | //
|
---|
130 | *(UINT32*)((UINTN)ThunkContext->RealModeBuffer + mTransition) +=
|
---|
131 | (UINT32)(UINTN)ThunkContext->RealModeBuffer & 0xf;
|
---|
132 |
|
---|
133 | //
|
---|
134 | // Update Segment Limits for both Code and Data Segment Descriptors
|
---|
135 | //
|
---|
136 | if ((ThunkContext->ThunkAttributes & THUNK_ATTRIBUTE_BIG_REAL_MODE) == 0) {
|
---|
137 | //
|
---|
138 | // Set segment limits to 64KB
|
---|
139 | //
|
---|
140 | RealModeGdt[1].Bits.LimitHigh = 0;
|
---|
141 | RealModeGdt[1].Bits.G = 0;
|
---|
142 | RealModeGdt[2].Bits.LimitHigh = 0;
|
---|
143 | RealModeGdt[2].Bits.G = 0;
|
---|
144 | }
|
---|
145 |
|
---|
146 | //
|
---|
147 | // Update GDTBASE for this thunk context
|
---|
148 | //
|
---|
149 | *(VOID**)((UINTN)ThunkContext->RealModeBuffer + m16GdtrBase) = RealModeGdt;
|
---|
150 |
|
---|
151 | //
|
---|
152 | // Update Thunk Attributes
|
---|
153 | //
|
---|
154 | *(UINT32*)((UINTN)ThunkContext->RealModeBuffer + mThunk16Attr) =
|
---|
155 | ThunkContext->ThunkAttributes;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | Transfers control to a 16-bit real mode entry point and returns the results.
|
---|
160 |
|
---|
161 | Transfers control to a 16-bit real mode entry point and returns the results.
|
---|
162 | AsmPrepareThunk16() must be called with ThunkContext before this function is used.
|
---|
163 | This function must be called with interrupts disabled.
|
---|
164 |
|
---|
165 | The register state from the RealModeState field of ThunkContext is restored just prior
|
---|
166 | to calling the 16-bit real mode entry point. This includes the EFLAGS field of RealModeState,
|
---|
167 | which is used to set the interrupt state when a 16-bit real mode entry point is called.
|
---|
168 | Control is transferred to the 16-bit real mode entry point specified by the CS and Eip fields of RealModeState.
|
---|
169 | The stack is initialized to the SS and ESP fields of RealModeState. Any parameters passed to
|
---|
170 | the 16-bit real mode code must be populated by the caller at SS:ESP prior to calling this function.
|
---|
171 | The 16-bit real mode entry point is invoked with a 16-bit CALL FAR instruction,
|
---|
172 | so when accessing stack contents, the 16-bit real mode code must account for the 16-bit segment
|
---|
173 | and 16-bit offset of the return address that were pushed onto the stack. The 16-bit real mode entry
|
---|
174 | point must exit with a RETF instruction. The register state is captured into RealModeState immediately
|
---|
175 | after the RETF instruction is executed.
|
---|
176 |
|
---|
177 | If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts,
|
---|
178 | or any of the 16-bit real mode code makes a SW interrupt, then the caller is responsible for making sure
|
---|
179 | the IDT at address 0 is initialized to handle any HW or SW interrupts that may occur while in 16-bit real mode.
|
---|
180 |
|
---|
181 | If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts,
|
---|
182 | then the caller is responsible for making sure the 8259 PIC is in a state compatible with 16-bit real mode.
|
---|
183 | This includes the base vectors, the interrupt masks, and the edge/level trigger mode.
|
---|
184 |
|
---|
185 | If THUNK_ATTRIBUTE_BIG_REAL_MODE is set in the ThunkAttributes field of ThunkContext, then the user code
|
---|
186 | is invoked in big real mode. Otherwise, the user code is invoked in 16-bit real mode with 64KB segment limits.
|
---|
187 |
|
---|
188 | If neither THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 nor THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in
|
---|
189 | ThunkAttributes, then it is assumed that the user code did not enable the A20 mask, and no attempt is made to
|
---|
190 | disable the A20 mask.
|
---|
191 |
|
---|
192 | If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is set and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is clear in
|
---|
193 | ThunkAttributes, then attempt to use the INT 15 service to disable the A20 mask. If this INT 15 call fails,
|
---|
194 | then attempt to disable the A20 mask by directly accessing the 8042 keyboard controller I/O ports.
|
---|
195 |
|
---|
196 | If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is clear and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is set in
|
---|
197 | ThunkAttributes, then attempt to disable the A20 mask by directly accessing the 8042 keyboard controller I/O ports.
|
---|
198 |
|
---|
199 | If ThunkContext is NULL, then ASSERT().
|
---|
200 | If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().
|
---|
201 | If both THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in
|
---|
202 | ThunkAttributes, then ASSERT().
|
---|
203 |
|
---|
204 | This interface is limited to be used in either physical mode or virtual modes with paging enabled where the
|
---|
205 | virtual to physical mappings for ThunkContext.RealModeBuffer is mapped 1:1.
|
---|
206 |
|
---|
207 | @param ThunkContext A pointer to the context structure that describes the
|
---|
208 | 16-bit real mode code to call.
|
---|
209 |
|
---|
210 | **/
|
---|
211 | VOID
|
---|
212 | EFIAPI
|
---|
213 | AsmThunk16 (
|
---|
214 | IN OUT THUNK_CONTEXT *ThunkContext
|
---|
215 | )
|
---|
216 | {
|
---|
217 | IA32_REGISTER_SET *UpdatedRegs;
|
---|
218 |
|
---|
219 | ASSERT (ThunkContext != NULL);
|
---|
220 | ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);
|
---|
221 | ASSERT (ThunkContext->RealModeBufferSize >= m16Size);
|
---|
222 | ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);
|
---|
223 | ASSERT (((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 | UpdatedRegs = InternalAsmThunk16 (
|
---|
227 | ThunkContext->RealModeState,
|
---|
228 | ThunkContext->RealModeBuffer
|
---|
229 | );
|
---|
230 |
|
---|
231 | CopyMem (ThunkContext->RealModeState, UpdatedRegs, sizeof (*UpdatedRegs));
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | Prepares all structures and code for a 16-bit real mode thunk, transfers
|
---|
236 | control to a 16-bit real mode entry point, and returns the results.
|
---|
237 |
|
---|
238 | Prepares all structures and code for a 16-bit real mode thunk, transfers
|
---|
239 | control to a 16-bit real mode entry point, and returns the results. If the
|
---|
240 | caller only need to perform a single 16-bit real mode thunk, then this
|
---|
241 | service should be used. If the caller intends to make more than one 16-bit
|
---|
242 | real mode thunk, then it is more efficient if AsmPrepareThunk16() is called
|
---|
243 | once and AsmThunk16() can be called for each 16-bit real mode thunk.
|
---|
244 |
|
---|
245 | This interface is limited to be used in either physical mode or virtual modes with paging enabled where the
|
---|
246 | virtual to physical mappings for ThunkContext.RealModeBuffer is mapped 1:1.
|
---|
247 |
|
---|
248 | See AsmPrepareThunk16() and AsmThunk16() for the detailed description and ASSERT() conditions.
|
---|
249 |
|
---|
250 | @param ThunkContext A pointer to the context structure that describes the
|
---|
251 | 16-bit real mode code to call.
|
---|
252 |
|
---|
253 | **/
|
---|
254 | VOID
|
---|
255 | EFIAPI
|
---|
256 | AsmPrepareAndThunk16 (
|
---|
257 | IN OUT THUNK_CONTEXT *ThunkContext
|
---|
258 | )
|
---|
259 | {
|
---|
260 | AsmPrepareThunk16 (ThunkContext);
|
---|
261 | AsmThunk16 (ThunkContext);
|
---|
262 | }
|
---|