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