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