]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/x86Thunk.c
Updated to support compiler intrinsics properly. I had to comment out some of the...
[mirror_edk2.git] / MdePkg / Library / BaseLib / x86Thunk.c
CommitLineData
e1f414b6 1/** @file\r
2 Real Mode Thunk Functions for IA32 and X64.\r
3\r
4 Copyright (c) 2006, Intel Corporation<BR>\r
5 All rights reserved. This program and the accompanying materials\r
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
13 Module Name: x86Thunk.c\r
14\r
15**/\r
16\r
17//\r
18// Include common header file for this module.\r
19//\r
20#include "CommonHeader.h"\r
21\r
22//\r
23// Byte packed structure for a segment descriptor in a GDT/LDT\r
24//\r
25typedef union {\r
26 struct {\r
27 UINT32 LimitLow:16;\r
28 UINT32 BaseLow:16;\r
29 UINT32 BaseMid:8;\r
30 UINT32 Type:4;\r
31 UINT32 S:1;\r
32 UINT32 DPL:2;\r
33 UINT32 P:1;\r
34 UINT32 LimitHigh:4;\r
35 UINT32 AVL:1;\r
36 UINT32 L:1;\r
37 UINT32 DB:1;\r
38 UINT32 G:1;\r
39 UINT32 BaseHigh:8;\r
40 } Bits;\r
41 UINT64 Uint64;\r
42} IA32_SEGMENT_DESCRIPTOR;\r
43\r
44extern CONST UINT8 m16Start;\r
45extern CONST UINT16 m16Size;\r
46extern CONST UINT16 mThunk16Attr;\r
47extern CONST UINT16 m16Gdt;\r
48extern CONST UINT16 m16GdtrBase;\r
49extern CONST UINT16 mTransition;\r
50\r
51/**\r
52 Invokes 16-bit code in big real mode and returns the updated register set.\r
53\r
54 This function transfers control to the 16-bit code specified by CS:EIP using\r
55 the stack specified by SS:ESP in RegisterSet. The updated registers are saved\r
56 on the real mode stack and the starting address of the save area is returned.\r
57\r
58 @param RegisterSet Values of registers before invocation of 16-bit code.\r
59 @param Transition Pointer to the transition code under 1MB.\r
60\r
61 @return The pointer to a IA32_REGISTER_SET structure containing the updated\r
62 register values.\r
63\r
64**/\r
65IA32_REGISTER_SET *\r
66EFIAPI\r
67InternalAsmThunk16 (\r
68 IN IA32_REGISTER_SET *RegisterSet,\r
69 IN OUT VOID *Transition\r
70 );\r
71\r
72/**\r
73 Retrieves the properties for 16-bit thunk functions.\r
74\r
75 Computes the size of the buffer and stack below 1MB required to use the\r
76 AsmPrepareThunk16(), AsmThunk16() and AsmPrepareAndThunk16() functions. This\r
77 buffer size is returned in RealModeBufferSize, and the stack size is returned\r
78 in ExtraStackSize. If parameters are passed to the 16-bit real mode code,\r
79 then the actual minimum stack size is ExtraStackSize plus the maximum number\r
80 of bytes that need to be passed to the 16-bit real mode code.\r
81\r
82 If RealModeBufferSize is NULL, then ASSERT().\r
83 If ExtraStackSize is NULL, then ASSERT().\r
84\r
85 @param RealModeBufferSize A pointer to the size of the buffer below 1MB\r
86 required to use the 16-bit thunk functions.\r
87 @param ExtraStackSize A pointer to the extra size of stack below 1MB\r
88 that the 16-bit thunk functions require for\r
89 temporary storage in the transition to and from\r
90 16-bit real mode.\r
91\r
92**/\r
93VOID\r
94EFIAPI\r
95AsmGetThunk16Properties (\r
96 OUT UINT32 *RealModeBufferSize,\r
97 OUT UINT32 *ExtraStackSize\r
98 )\r
99{\r
100 ASSERT (RealModeBufferSize != NULL);\r
101 ASSERT (ExtraStackSize != NULL);\r
102\r
103 *RealModeBufferSize = m16Size;\r
104\r
105 //\r
106 // Extra 4 bytes for return address, and another 4 bytes for mode transition\r
107 //\r
108 *ExtraStackSize = sizeof (IA32_DWORD_REGS) + 8;\r
109}\r
110\r
111/**\r
112 Prepares all structures a code required to use AsmThunk16().\r
113\r
114 Prepares all structures and code required to use AsmThunk16().\r
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
191 AsmPrepareThunk16() must be called with ThunkContext before this function is\r
192 used. This function must be called with interrupts disabled.\r
193\r
194 If ThunkContext is NULL, then ASSERT().\r
195 If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().\r
196\r
197 @param ThunkContext A pointer to the context structure that describes the\r
198 16-bit real mode code to call.\r
199\r
200**/\r
201VOID\r
202EFIAPI\r
203AsmThunk16 (\r
204 IN OUT THUNK_CONTEXT *ThunkContext\r
205 )\r
206{\r
207 IA32_REGISTER_SET *UpdatedRegs;\r
208\r
209 ASSERT (ThunkContext != NULL);\r
210 ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);\r
211 ASSERT (ThunkContext->RealModeBufferSize >= m16Size);\r
212 ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);\r
213\r
214 UpdatedRegs = InternalAsmThunk16 (\r
215 ThunkContext->RealModeState,\r
216 ThunkContext->RealModeBuffer\r
217 );\r
218\r
219 CopyMem (ThunkContext->RealModeState, UpdatedRegs, sizeof (*UpdatedRegs));\r
220}\r
221\r
222/**\r
223 Prepares all structures and code for a 16-bit real mode thunk, transfers\r
224 control to a 16-bit real mode entry point, and returns the results.\r
225\r
226 Prepares all structures and code for a 16-bit real mode thunk, transfers\r
227 control to a 16-bit real mode entry point, and returns the results. If the\r
228 caller only need to perform a single 16-bit real mode thunk, then this\r
229 service should be used. If the caller intends to make more than one 16-bit\r
230 real mode thunk, then it is more efficient if AsmPrepareThunk16() is called\r
231 once and AsmThunk16() can be called for each 16-bit real mode thunk. This\r
232 function must be called with interrupts disabled.\r
233\r
234 If ThunkContext is NULL, then ASSERT().\r
235\r
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
242AsmPrepareAndThunk16 (\r
243 IN OUT THUNK_CONTEXT *ThunkContext\r
244 )\r
245{\r
246 AsmPrepareThunk16 (ThunkContext);\r
247 AsmThunk16 (ThunkContext);\r
248}\r