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
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.
19 Real Mode Thunk Functions for IA32 and X64.
23 #include "BaseLibInternals.h"
25 // Byte packed structure for a segment descriptor in a GDT/LDT
44 } IA32_SEGMENT_DESCRIPTOR
;
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
;
54 Invokes 16-bit code in big real mode and returns the updated register set.
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.
60 @param RegisterSet Values of registers before invocation of 16-bit code.
61 @param Transition Pointer to the transition code under 1MB.
63 @return The pointer to a IA32_REGISTER_SET structure containing the updated
70 IN IA32_REGISTER_SET
*RegisterSet
,
71 IN OUT VOID
*Transition
75 Retrieves the properties for 16-bit thunk functions.
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.
84 If RealModeBufferSize is NULL, then ASSERT().
85 If ExtraStackSize is NULL, then ASSERT().
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
97 AsmGetThunk16Properties (
98 OUT UINT32
*RealModeBufferSize
,
99 OUT UINT32
*ExtraStackSize
102 ASSERT (RealModeBufferSize
!= NULL
);
103 ASSERT (ExtraStackSize
!= NULL
);
105 *RealModeBufferSize
= m16Size
;
108 // Extra 4 bytes for return address, and another 4 bytes for mode transition
110 *ExtraStackSize
= sizeof (IA32_DWORD_REGS
) + 8;
114 Prepares all structures a code required to use AsmThunk16().
116 Prepares all structures and code required to use AsmThunk16().
118 If ThunkContext is NULL, then ASSERT().
120 @param ThunkContext A pointer to the context structure that describes the
121 16-bit real mode code to call.
127 OUT THUNK_CONTEXT
*ThunkContext
130 IA32_SEGMENT_DESCRIPTOR
*RealModeGdt
;
132 ASSERT (ThunkContext
!= NULL
);
133 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
< 0x100000);
134 ASSERT (ThunkContext
->RealModeBufferSize
>= m16Size
);
135 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
+ m16Size
<= 0x100000);
137 CopyMem (ThunkContext
->RealModeBuffer
, &m16Start
, m16Size
);
140 // Point RealModeGdt to the GDT to be used in transition
142 // RealModeGdt[0]: Reserved as NULL descriptor
143 // RealModeGdt[1]: Code Segment
144 // RealModeGdt[2]: Data Segment
145 // RealModeGdt[3]: Call Gate
147 RealModeGdt
= (IA32_SEGMENT_DESCRIPTOR
*)(
148 (UINTN
)ThunkContext
->RealModeBuffer
+ m16Gdt
);
151 // Update Code & Data Segment Descriptor
153 RealModeGdt
[1].Bits
.BaseLow
=
154 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
& ~0xf;
155 RealModeGdt
[1].Bits
.BaseMid
=
156 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
>> 16;
159 // Update transition code entry point offset
161 *(UINT32
*)((UINTN
)ThunkContext
->RealModeBuffer
+ mTransition
) +=
162 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
& 0xf;
165 // Update Segment Limits for both Code and Data Segment Descriptors
167 if ((ThunkContext
->ThunkAttributes
& THUNK_ATTRIBUTE_BIG_REAL_MODE
) == 0) {
169 // Set segment limits to 64KB
171 RealModeGdt
[1].Bits
.LimitHigh
= 0;
172 RealModeGdt
[1].Bits
.G
= 0;
173 RealModeGdt
[2].Bits
.LimitHigh
= 0;
174 RealModeGdt
[2].Bits
.G
= 0;
178 // Update GDTBASE for this thunk context
180 *(VOID
**)((UINTN
)ThunkContext
->RealModeBuffer
+ m16GdtrBase
) = RealModeGdt
;
183 // Update Thunk Attributes
185 *(UINT32
*)((UINTN
)ThunkContext
->RealModeBuffer
+ mThunk16Attr
) =
186 ThunkContext
->ThunkAttributes
;
190 Transfers control to a 16-bit real mode entry point and returns the results.
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.
196 If ThunkContext is NULL, then ASSERT().
197 If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().
199 @param ThunkContext A pointer to the context structure that describes the
200 16-bit real mode code to call.
206 IN OUT THUNK_CONTEXT
*ThunkContext
209 IA32_REGISTER_SET
*UpdatedRegs
;
211 ASSERT (ThunkContext
!= NULL
);
212 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
< 0x100000);
213 ASSERT (ThunkContext
->RealModeBufferSize
>= m16Size
);
214 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
+ m16Size
<= 0x100000);
216 UpdatedRegs
= InternalAsmThunk16 (
217 ThunkContext
->RealModeState
,
218 ThunkContext
->RealModeBuffer
221 CopyMem (ThunkContext
->RealModeState
, UpdatedRegs
, sizeof (*UpdatedRegs
));
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.
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.
236 If ThunkContext is NULL, then ASSERT().
238 @param ThunkContext A pointer to the context structure that describes the
239 16-bit real mode code to call.
244 AsmPrepareAndThunk16 (
245 IN OUT THUNK_CONTEXT
*ThunkContext
248 AsmPrepareThunk16 (ThunkContext
);
249 AsmThunk16 (ThunkContext
);