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