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