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