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