]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/x86Thunk.c
Updated BaseLib for THUNK functions and some CPU functions
[mirror_edk2.git] / MdePkg / Library / BaseLib / x86Thunk.c
CommitLineData
878ddf1f 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
97d92bda 17//\r
18// Byte packed structure for a segment descriptor in a GDT/LDT\r
19//\r
20typedef union {\r
21 struct {\r
22 UINT32 LimitLow:16;\r
23 UINT32 BaseLow:16;\r
24 UINT32 BaseMid:8;\r
25 UINT32 Type:4;\r
26 UINT32 S:1;\r
27 UINT32 DPL:2;\r
28 UINT32 P:1;\r
29 UINT32 LimitHigh:4;\r
30 UINT32 AVL:1;\r
31 UINT32 L:1;\r
32 UINT32 DB:1;\r
33 UINT32 G:1;\r
34 UINT32 BaseHigh:8;\r
35 } Bits;\r
36 UINT64 Uint64;\r
37} IA32_SEGMENT_DESCRIPTOR;\r
38\r
39extern CONST UINT8 m16Start;\r
40extern CONST UINT16 m16Size;\r
41extern CONST UINT16 mThunk16Attr;\r
42extern CONST UINT16 m16Gdt;\r
43extern CONST UINT16 m16GdtrBase;\r
44extern CONST UINT16 mTransition;\r
45\r
878ddf1f 46/**\r
47 Invokes 16-bit code in big real mode and returns the updated register set.\r
48\r
49 This function transfers control to the 16-bit code specified by CS:EIP using\r
50 the stack specified by SS:ESP in RegisterSet. The updated registers are saved\r
51 on the real mode stack and the starting address of the save area is returned.\r
52\r
53 @param RegisterSet Values of registers before invocation of 16-bit code.\r
97d92bda 54 @param Transition Pointer to the transition code under 1MB.\r
878ddf1f 55\r
56 @return The pointer to a IA32_REGISTER_SET structure containing the updated\r
57 register values.\r
58\r
59**/\r
60IA32_REGISTER_SET *\r
61InternalAsmThunk16 (\r
62 IN IA32_REGISTER_SET *RegisterSet,\r
97d92bda 63 IN OUT VOID *Transition\r
878ddf1f 64 );\r
65\r
97d92bda 66/**\r
67 Retrieves the properties for 16-bit thunk functions.\r
68\r
69 Computes the size of the buffer and stack below 1MB required to use the\r
70 AsmPrepareThunk16(), AsmThunk16() and AsmPrepareAndThunk16() functions. This\r
71 buffer size is returned in RealModeBufferSize, and the stack size is returned\r
72 in ExtraStackSize. If parameters are passed to the 16-bit real mode code,\r
73 then the actual minimum stack size is ExtraStackSize plus the maximum number\r
74 of bytes that need to be passed to the 16-bit real mode code.\r
75\r
76 If RealModeBufferSize is NULL, then ASSERT().\r
77 If ExtraStackSize is NULL, then ASSERT().\r
78\r
79 @param RealModeBufferSize A pointer to the size of the buffer below 1MB\r
80 required to use the 16-bit thunk functions.\r
81 @param ExtraStackSize A pointer to the extra size of stack below 1MB\r
82 that the 16-bit thunk functions require for\r
83 temporary storage in the transition to and from\r
84 16-bit real mode.\r
85\r
86**/\r
87VOID\r
88EFIAPI\r
89AsmGetThunk16Properties (\r
90 OUT UINT32 *RealModeBufferSize,\r
91 OUT UINT32 *ExtraStackSize\r
92 )\r
93{\r
94 ASSERT (RealModeBufferSize != NULL);\r
95 ASSERT (ExtraStackSize != NULL);\r
96\r
97 *RealModeBufferSize = m16Size;\r
98 *ExtraStackSize = sizeof (IA32_DWORD_REGS) + 8;\r
99}\r
100\r
878ddf1f 101/**\r
102 Prepares all structures a code required to use AsmThunk16().\r
103\r
104 Prepares all structures and code required to use AsmThunk16().\r
105\r
106 If ThunkContext is NULL, then ASSERT().\r
107\r
108 @param ThunkContext A pointer to the context structure that describes the\r
109 16-bit real mode code to call.\r
110\r
111**/\r
112VOID\r
113EFIAPI\r
114AsmPrepareThunk16 (\r
115 OUT THUNK_CONTEXT *ThunkContext\r
116 )\r
117{\r
97d92bda 118 IA32_SEGMENT_DESCRIPTOR *RealModeGdt;\r
119\r
878ddf1f 120 ASSERT (ThunkContext != NULL);\r
97d92bda 121 ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);\r
122 ASSERT (ThunkContext->RealModeBufferSize >= m16Size);\r
123 ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);\r
124 ASSERT (((UINTN)ThunkContext->RealModeBuffer & 0x0f) == 0);\r
125\r
126 CopyMem (ThunkContext->RealModeBuffer, &m16Start, m16Size);\r
127\r
128 //\r
129 // Point RealModeGdt to the GDT to be used in transition\r
130 //\r
131 // RealModeGdt[0]: Reserved as NULL descriptor\r
132 // RealModeGdt[1]: Code Segment\r
133 // RealModeGdt[2]: Data Segment\r
134 // RealModeGdt[3]: Call Gate\r
135 //\r
136 RealModeGdt = (IA32_SEGMENT_DESCRIPTOR*)(\r
137 (UINTN)ThunkContext->RealModeBuffer + m16Gdt);\r
138\r
139 //\r
140 // Update Code & Data Segment Descriptor\r
141 //\r
142 RealModeGdt[1].Bits.BaseLow =\r
143 (UINT32)(UINTN)ThunkContext->RealModeBuffer & ~0xf;\r
144 RealModeGdt[1].Bits.BaseMid =\r
145 (UINT32)(UINTN)ThunkContext->RealModeBuffer >> 16;\r
146\r
147 //\r
148 // Update transition code entry point offset\r
149 //\r
150 *(UINT32*)((UINTN)ThunkContext->RealModeBuffer + mTransition) +=\r
151 (UINT32)(UINTN)ThunkContext->RealModeBuffer & 0xf;\r
152\r
153 //\r
154 // Update Segment Limits for both Code and Data Segment Descriptors\r
155 //\r
156 if ((ThunkContext->ThunkAttributes & THUNK_ATTRIBUTE_BIG_REAL_MODE) == 0) {\r
157 //\r
158 // Set segment limits to 64KB\r
159 //\r
160 RealModeGdt[1].Bits.LimitHigh = 0;\r
161 RealModeGdt[1].Bits.G = 0;\r
162 RealModeGdt[2].Bits.LimitHigh = 0;\r
163 RealModeGdt[2].Bits.G = 0;\r
164 }\r
165\r
166 //\r
167 // Update GDTBASE for this thunk context\r
168 //\r
169 *(VOID**)((UINTN)ThunkContext->RealModeBuffer + m16GdtrBase) = RealModeGdt;\r
170\r
171 //\r
172 // Update Thunk Attributes\r
173 //\r
174 *(UINT32*)((UINTN)ThunkContext->RealModeBuffer + mThunk16Attr) =\r
175 ThunkContext->ThunkAttributes;\r
878ddf1f 176}\r
177\r
178/**\r
179 Transfers control to a 16-bit real mode entry point and returns the results.\r
180\r
181 Transfers control to a 16-bit real mode entry point and returns the results.\r
182 AsmPrepareThunk16() must be called with ThunkContext before this function is\r
183 used. This function must be called with interrupts disabled.\r
184\r
185 If ThunkContext is NULL, then ASSERT().\r
186 If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().\r
187\r
188 @param ThunkContext A pointer to the context structure that describes the\r
189 16-bit real mode code to call.\r
190\r
191**/\r
192VOID\r
193EFIAPI\r
194AsmThunk16 (\r
195 IN OUT THUNK_CONTEXT *ThunkContext\r
196 )\r
197{\r
97d92bda 198 IA32_REGISTER_SET *UpdatedRegs;\r
878ddf1f 199\r
200 ASSERT (ThunkContext != NULL);\r
97d92bda 201 ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);\r
202 ASSERT (ThunkContext->RealModeBufferSize >= m16Size);\r
203 ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);\r
204 ASSERT (((UINTN)ThunkContext->RealModeBuffer & 0x0f) == 0);\r
878ddf1f 205\r
97d92bda 206 UpdatedRegs = InternalAsmThunk16 (\r
207 ThunkContext->RealModeState,\r
208 ThunkContext->RealModeBuffer\r
209 );\r
878ddf1f 210\r
97d92bda 211 CopyMem (ThunkContext->RealModeState, UpdatedRegs, sizeof (*UpdatedRegs));\r
878ddf1f 212}\r
213\r
214/**\r
215 Prepares all structures and code for a 16-bit real mode thunk, transfers\r
216 control to a 16-bit real mode entry point, and returns the results.\r
217\r
218 Prepares all structures and code for a 16-bit real mode thunk, transfers\r
219 control to a 16-bit real mode entry point, and returns the results. If the\r
220 caller only need to perform a single 16-bit real mode thunk, then this\r
221 service should be used. If the caller intends to make more than one 16-bit\r
222 real mode thunk, then it is more efficient if AsmPrepareThunk16() is called\r
223 once and AsmThunk16() can be called for each 16-bit real mode thunk. This\r
224 function must be called with interrupts disabled.\r
225\r
226 If ThunkContext is NULL, then ASSERT().\r
227\r
228 @param ThunkContext A pointer to the context structure that describes the\r
229 16-bit real mode code to call.\r
230\r
231**/\r
232VOID\r
233EFIAPI\r
234AsmPrepareAndThunk16 (\r
235 IN OUT THUNK_CONTEXT *ThunkContext\r
236 )\r
237{\r
238 AsmPrepareThunk16 (ThunkContext);\r
239 AsmThunk16 (ThunkContext);\r
240}\r