2 Real Mode Thunk Functions for IA32 and X64.
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
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.
16 // Include common header file for this module.
18 #include <BaseLibInternals.h>
22 // Byte packed structure for a segment descriptor in a GDT/LDT
41 } IA32_SEGMENT_DESCRIPTOR
;
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
;
51 Invokes 16-bit code in big real mode and returns the updated register set.
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.
57 @param RegisterSet Values of registers before invocation of 16-bit code.
58 @param Transition Pointer to the transition code under 1MB.
60 @return The pointer to a IA32_REGISTER_SET structure containing the updated
67 IN IA32_REGISTER_SET
*RegisterSet
,
68 IN OUT VOID
*Transition
72 Retrieves the properties for 16-bit thunk functions.
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.
81 If RealModeBufferSize is NULL, then ASSERT().
82 If ExtraStackSize is NULL, then ASSERT().
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
94 AsmGetThunk16Properties (
95 OUT UINT32
*RealModeBufferSize
,
96 OUT UINT32
*ExtraStackSize
99 ASSERT (RealModeBufferSize
!= NULL
);
100 ASSERT (ExtraStackSize
!= NULL
);
102 *RealModeBufferSize
= m16Size
;
105 // Extra 4 bytes for return address, and another 4 bytes for mode transition
107 *ExtraStackSize
= sizeof (IA32_DWORD_REGS
) + 8;
111 Prepares all structures a code required to use AsmThunk16().
113 Prepares all structures and code required to use AsmThunk16().
115 If ThunkContext is NULL, then ASSERT().
117 @param ThunkContext A pointer to the context structure that describes the
118 16-bit real mode code to call.
124 OUT THUNK_CONTEXT
*ThunkContext
127 IA32_SEGMENT_DESCRIPTOR
*RealModeGdt
;
129 ASSERT (ThunkContext
!= NULL
);
130 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
< 0x100000);
131 ASSERT (ThunkContext
->RealModeBufferSize
>= m16Size
);
132 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
+ m16Size
<= 0x100000);
134 CopyMem (ThunkContext
->RealModeBuffer
, &m16Start
, m16Size
);
137 // Point RealModeGdt to the GDT to be used in transition
139 // RealModeGdt[0]: Reserved as NULL descriptor
140 // RealModeGdt[1]: Code Segment
141 // RealModeGdt[2]: Data Segment
142 // RealModeGdt[3]: Call Gate
144 RealModeGdt
= (IA32_SEGMENT_DESCRIPTOR
*)(
145 (UINTN
)ThunkContext
->RealModeBuffer
+ m16Gdt
);
148 // Update Code & Data Segment Descriptor
150 RealModeGdt
[1].Bits
.BaseLow
=
151 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
& ~0xf;
152 RealModeGdt
[1].Bits
.BaseMid
=
153 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
>> 16;
156 // Update transition code entry point offset
158 *(UINT32
*)((UINTN
)ThunkContext
->RealModeBuffer
+ mTransition
) +=
159 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
& 0xf;
162 // Update Segment Limits for both Code and Data Segment Descriptors
164 if ((ThunkContext
->ThunkAttributes
& THUNK_ATTRIBUTE_BIG_REAL_MODE
) == 0) {
166 // Set segment limits to 64KB
168 RealModeGdt
[1].Bits
.LimitHigh
= 0;
169 RealModeGdt
[1].Bits
.G
= 0;
170 RealModeGdt
[2].Bits
.LimitHigh
= 0;
171 RealModeGdt
[2].Bits
.G
= 0;
175 // Update GDTBASE for this thunk context
177 *(VOID
**)((UINTN
)ThunkContext
->RealModeBuffer
+ m16GdtrBase
) = RealModeGdt
;
180 // Update Thunk Attributes
182 *(UINT32
*)((UINTN
)ThunkContext
->RealModeBuffer
+ mThunk16Attr
) =
183 ThunkContext
->ThunkAttributes
;
187 Transfers control to a 16-bit real mode entry point and returns the results.
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.
193 If ThunkContext is NULL, then ASSERT().
194 If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().
196 @param ThunkContext A pointer to the context structure that describes the
197 16-bit real mode code to call.
203 IN OUT THUNK_CONTEXT
*ThunkContext
206 IA32_REGISTER_SET
*UpdatedRegs
;
208 ASSERT (ThunkContext
!= NULL
);
209 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
< 0x100000);
210 ASSERT (ThunkContext
->RealModeBufferSize
>= m16Size
);
211 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
+ m16Size
<= 0x100000);
213 UpdatedRegs
= InternalAsmThunk16 (
214 ThunkContext
->RealModeState
,
215 ThunkContext
->RealModeBuffer
218 CopyMem (ThunkContext
->RealModeState
, UpdatedRegs
, sizeof (*UpdatedRegs
));
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.
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.
233 If ThunkContext is NULL, then ASSERT().
235 @param ThunkContext A pointer to the context structure that describes the
236 16-bit real mode code to call.
241 AsmPrepareAndThunk16 (
242 IN OUT THUNK_CONTEXT
*ThunkContext
245 AsmPrepareThunk16 (ThunkContext
);
246 AsmThunk16 (ThunkContext
);