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