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