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