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 |
20 | typedef 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 |
39 | extern CONST UINT8 m16Start;\r |
40 | extern CONST UINT16 m16Size;\r |
41 | extern CONST UINT16 mThunk16Attr;\r |
42 | extern CONST UINT16 m16Gdt;\r |
43 | extern CONST UINT16 m16GdtrBase;\r |
44 | extern 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 |
60 | IA32_REGISTER_SET *\r |
3f566587 |
61 | EFIAPI\r |
878ddf1f |
62 | InternalAsmThunk16 (\r |
63 | IN IA32_REGISTER_SET *RegisterSet,\r |
97d92bda |
64 | IN OUT VOID *Transition\r |
878ddf1f |
65 | );\r |
66 | \r |
97d92bda |
67 | /**\r |
68 | Retrieves the properties for 16-bit thunk functions.\r |
69 | \r |
70 | Computes the size of the buffer and stack below 1MB required to use the\r |
71 | AsmPrepareThunk16(), AsmThunk16() and AsmPrepareAndThunk16() functions. This\r |
72 | buffer size is returned in RealModeBufferSize, and the stack size is returned\r |
73 | in ExtraStackSize. If parameters are passed to the 16-bit real mode code,\r |
74 | then the actual minimum stack size is ExtraStackSize plus the maximum number\r |
75 | of bytes that need to be passed to the 16-bit real mode code.\r |
76 | \r |
77 | If RealModeBufferSize is NULL, then ASSERT().\r |
78 | If ExtraStackSize is NULL, then ASSERT().\r |
79 | \r |
80 | @param RealModeBufferSize A pointer to the size of the buffer below 1MB\r |
81 | required to use the 16-bit thunk functions.\r |
82 | @param ExtraStackSize A pointer to the extra size of stack below 1MB\r |
83 | that the 16-bit thunk functions require for\r |
84 | temporary storage in the transition to and from\r |
85 | 16-bit real mode.\r |
86 | \r |
87 | **/\r |
88 | VOID\r |
89 | EFIAPI\r |
90 | AsmGetThunk16Properties (\r |
91 | OUT UINT32 *RealModeBufferSize,\r |
92 | OUT UINT32 *ExtraStackSize\r |
93 | )\r |
94 | {\r |
95 | ASSERT (RealModeBufferSize != NULL);\r |
96 | ASSERT (ExtraStackSize != NULL);\r |
97 | \r |
98 | *RealModeBufferSize = m16Size;\r |
18c319ae |
99 | \r |
100 | //\r |
101 | // Extra 4 bytes for return address, and another 4 bytes for mode transition\r |
102 | //\r |
97d92bda |
103 | *ExtraStackSize = sizeof (IA32_DWORD_REGS) + 8;\r |
104 | }\r |
105 | \r |
878ddf1f |
106 | /**\r |
107 | Prepares all structures a code required to use AsmThunk16().\r |
108 | \r |
109 | Prepares all structures and code required to use AsmThunk16().\r |
110 | \r |
111 | If ThunkContext is NULL, then ASSERT().\r |
112 | \r |
113 | @param ThunkContext A pointer to the context structure that describes the\r |
114 | 16-bit real mode code to call.\r |
115 | \r |
116 | **/\r |
117 | VOID\r |
118 | EFIAPI\r |
119 | AsmPrepareThunk16 (\r |
120 | OUT THUNK_CONTEXT *ThunkContext\r |
121 | )\r |
122 | {\r |
97d92bda |
123 | IA32_SEGMENT_DESCRIPTOR *RealModeGdt;\r |
124 | \r |
878ddf1f |
125 | ASSERT (ThunkContext != NULL);\r |
97d92bda |
126 | ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);\r |
127 | ASSERT (ThunkContext->RealModeBufferSize >= m16Size);\r |
128 | ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);\r |
97d92bda |
129 | \r |
130 | CopyMem (ThunkContext->RealModeBuffer, &m16Start, m16Size);\r |
131 | \r |
132 | //\r |
133 | // Point RealModeGdt to the GDT to be used in transition\r |
134 | //\r |
135 | // RealModeGdt[0]: Reserved as NULL descriptor\r |
136 | // RealModeGdt[1]: Code Segment\r |
137 | // RealModeGdt[2]: Data Segment\r |
138 | // RealModeGdt[3]: Call Gate\r |
139 | //\r |
140 | RealModeGdt = (IA32_SEGMENT_DESCRIPTOR*)(\r |
141 | (UINTN)ThunkContext->RealModeBuffer + m16Gdt);\r |
142 | \r |
143 | //\r |
144 | // Update Code & Data Segment Descriptor\r |
145 | //\r |
146 | RealModeGdt[1].Bits.BaseLow =\r |
147 | (UINT32)(UINTN)ThunkContext->RealModeBuffer & ~0xf;\r |
148 | RealModeGdt[1].Bits.BaseMid =\r |
149 | (UINT32)(UINTN)ThunkContext->RealModeBuffer >> 16;\r |
150 | \r |
151 | //\r |
152 | // Update transition code entry point offset\r |
153 | //\r |
154 | *(UINT32*)((UINTN)ThunkContext->RealModeBuffer + mTransition) +=\r |
155 | (UINT32)(UINTN)ThunkContext->RealModeBuffer & 0xf;\r |
156 | \r |
157 | //\r |
158 | // Update Segment Limits for both Code and Data Segment Descriptors\r |
159 | //\r |
160 | if ((ThunkContext->ThunkAttributes & THUNK_ATTRIBUTE_BIG_REAL_MODE) == 0) {\r |
161 | //\r |
162 | // Set segment limits to 64KB\r |
163 | //\r |
164 | RealModeGdt[1].Bits.LimitHigh = 0;\r |
165 | RealModeGdt[1].Bits.G = 0;\r |
166 | RealModeGdt[2].Bits.LimitHigh = 0;\r |
167 | RealModeGdt[2].Bits.G = 0;\r |
168 | }\r |
169 | \r |
170 | //\r |
171 | // Update GDTBASE for this thunk context\r |
172 | //\r |
173 | *(VOID**)((UINTN)ThunkContext->RealModeBuffer + m16GdtrBase) = RealModeGdt;\r |
174 | \r |
175 | //\r |
176 | // Update Thunk Attributes\r |
177 | //\r |
178 | *(UINT32*)((UINTN)ThunkContext->RealModeBuffer + mThunk16Attr) =\r |
179 | ThunkContext->ThunkAttributes;\r |
878ddf1f |
180 | }\r |
181 | \r |
182 | /**\r |
183 | Transfers control to a 16-bit real mode entry point and returns the results.\r |
184 | \r |
185 | Transfers control to a 16-bit real mode entry point and returns the results.\r |
186 | AsmPrepareThunk16() must be called with ThunkContext before this function is\r |
187 | used. This function must be called with interrupts disabled.\r |
188 | \r |
189 | If ThunkContext is NULL, then ASSERT().\r |
190 | If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().\r |
191 | \r |
192 | @param ThunkContext A pointer to the context structure that describes the\r |
193 | 16-bit real mode code to call.\r |
194 | \r |
195 | **/\r |
196 | VOID\r |
197 | EFIAPI\r |
198 | AsmThunk16 (\r |
199 | IN OUT THUNK_CONTEXT *ThunkContext\r |
200 | )\r |
201 | {\r |
97d92bda |
202 | IA32_REGISTER_SET *UpdatedRegs;\r |
878ddf1f |
203 | \r |
204 | ASSERT (ThunkContext != NULL);\r |
97d92bda |
205 | ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);\r |
206 | ASSERT (ThunkContext->RealModeBufferSize >= m16Size);\r |
207 | ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);\r |
878ddf1f |
208 | \r |
97d92bda |
209 | UpdatedRegs = InternalAsmThunk16 (\r |
210 | ThunkContext->RealModeState,\r |
211 | ThunkContext->RealModeBuffer\r |
212 | );\r |
878ddf1f |
213 | \r |
97d92bda |
214 | CopyMem (ThunkContext->RealModeState, UpdatedRegs, sizeof (*UpdatedRegs));\r |
878ddf1f |
215 | }\r |
216 | \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.\r |
220 | \r |
221 | Prepares all structures and code for a 16-bit real mode thunk, transfers\r |
222 | control to a 16-bit real mode entry point, and returns the results. If the\r |
223 | caller only need to perform a single 16-bit real mode thunk, then this\r |
224 | service should be used. If the caller intends to make more than one 16-bit\r |
225 | real mode thunk, then it is more efficient if AsmPrepareThunk16() is called\r |
226 | once and AsmThunk16() can be called for each 16-bit real mode thunk. This\r |
227 | function must be called with interrupts disabled.\r |
228 | \r |
229 | If ThunkContext is NULL, then ASSERT().\r |
230 | \r |
231 | @param ThunkContext A pointer to the context structure that describes the\r |
232 | 16-bit real mode code to call.\r |
233 | \r |
234 | **/\r |
235 | VOID\r |
236 | EFIAPI\r |
237 | AsmPrepareAndThunk16 (\r |
238 | IN OUT THUNK_CONTEXT *ThunkContext\r |
239 | )\r |
240 | {\r |
241 | AsmPrepareThunk16 (ThunkContext);\r |
242 | AsmThunk16 (ThunkContext);\r |
243 | }\r |