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.
13 Module Name: x86Thunk.c
18 // Include common header file for this module.
20 #include "CommonHeader.h"
23 // Byte packed structure for a segment descriptor in a GDT/LDT
42 } IA32_SEGMENT_DESCRIPTOR
;
44 extern CONST UINT8 m16Start
;
45 extern CONST UINT16 m16Size
;
46 extern CONST UINT16 mThunk16Attr
;
47 extern CONST UINT16 m16Gdt
;
48 extern CONST UINT16 m16GdtrBase
;
49 extern CONST UINT16 mTransition
;
52 Invokes 16-bit code in big real mode and returns the updated register set.
54 This function transfers control to the 16-bit code specified by CS:EIP using
55 the stack specified by SS:ESP in RegisterSet. The updated registers are saved
56 on the real mode stack and the starting address of the save area is returned.
58 @param RegisterSet Values of registers before invocation of 16-bit code.
59 @param Transition Pointer to the transition code under 1MB.
61 @return The pointer to a IA32_REGISTER_SET structure containing the updated
68 IN IA32_REGISTER_SET
*RegisterSet
,
69 IN OUT VOID
*Transition
73 Retrieves the properties for 16-bit thunk functions.
75 Computes the size of the buffer and stack below 1MB required to use the
76 AsmPrepareThunk16(), AsmThunk16() and AsmPrepareAndThunk16() functions. This
77 buffer size is returned in RealModeBufferSize, and the stack size is returned
78 in ExtraStackSize. If parameters are passed to the 16-bit real mode code,
79 then the actual minimum stack size is ExtraStackSize plus the maximum number
80 of bytes that need to be passed to the 16-bit real mode code.
82 If RealModeBufferSize is NULL, then ASSERT().
83 If ExtraStackSize is NULL, then ASSERT().
85 @param RealModeBufferSize A pointer to the size of the buffer below 1MB
86 required to use the 16-bit thunk functions.
87 @param ExtraStackSize A pointer to the extra size of stack below 1MB
88 that the 16-bit thunk functions require for
89 temporary storage in the transition to and from
95 AsmGetThunk16Properties (
96 OUT UINT32
*RealModeBufferSize
,
97 OUT UINT32
*ExtraStackSize
100 ASSERT (RealModeBufferSize
!= NULL
);
101 ASSERT (ExtraStackSize
!= NULL
);
103 *RealModeBufferSize
= m16Size
;
106 // Extra 4 bytes for return address, and another 4 bytes for mode transition
108 *ExtraStackSize
= sizeof (IA32_DWORD_REGS
) + 8;
112 Prepares all structures a code required to use AsmThunk16().
114 Prepares all structures and code required to use AsmThunk16().
116 If ThunkContext is NULL, then ASSERT().
118 @param ThunkContext A pointer to the context structure that describes the
119 16-bit real mode code to call.
125 OUT THUNK_CONTEXT
*ThunkContext
128 IA32_SEGMENT_DESCRIPTOR
*RealModeGdt
;
130 ASSERT (ThunkContext
!= NULL
);
131 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
< 0x100000);
132 ASSERT (ThunkContext
->RealModeBufferSize
>= m16Size
);
133 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
+ m16Size
<= 0x100000);
135 CopyMem (ThunkContext
->RealModeBuffer
, &m16Start
, m16Size
);
138 // Point RealModeGdt to the GDT to be used in transition
140 // RealModeGdt[0]: Reserved as NULL descriptor
141 // RealModeGdt[1]: Code Segment
142 // RealModeGdt[2]: Data Segment
143 // RealModeGdt[3]: Call Gate
145 RealModeGdt
= (IA32_SEGMENT_DESCRIPTOR
*)(
146 (UINTN
)ThunkContext
->RealModeBuffer
+ m16Gdt
);
149 // Update Code & Data Segment Descriptor
151 RealModeGdt
[1].Bits
.BaseLow
=
152 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
& ~0xf;
153 RealModeGdt
[1].Bits
.BaseMid
=
154 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
>> 16;
157 // Update transition code entry point offset
159 *(UINT32
*)((UINTN
)ThunkContext
->RealModeBuffer
+ mTransition
) +=
160 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
& 0xf;
163 // Update Segment Limits for both Code and Data Segment Descriptors
165 if ((ThunkContext
->ThunkAttributes
& THUNK_ATTRIBUTE_BIG_REAL_MODE
) == 0) {
167 // Set segment limits to 64KB
169 RealModeGdt
[1].Bits
.LimitHigh
= 0;
170 RealModeGdt
[1].Bits
.G
= 0;
171 RealModeGdt
[2].Bits
.LimitHigh
= 0;
172 RealModeGdt
[2].Bits
.G
= 0;
176 // Update GDTBASE for this thunk context
178 *(VOID
**)((UINTN
)ThunkContext
->RealModeBuffer
+ m16GdtrBase
) = RealModeGdt
;
181 // Update Thunk Attributes
183 *(UINT32
*)((UINTN
)ThunkContext
->RealModeBuffer
+ mThunk16Attr
) =
184 ThunkContext
->ThunkAttributes
;
188 Transfers control to a 16-bit real mode entry point and returns the results.
190 Transfers control to a 16-bit real mode entry point and returns the results.
191 AsmPrepareThunk16() must be called with ThunkContext before this function is
192 used. This function must be called with interrupts disabled.
194 If ThunkContext is NULL, then ASSERT().
195 If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().
197 @param ThunkContext A pointer to the context structure that describes the
198 16-bit real mode code to call.
204 IN OUT THUNK_CONTEXT
*ThunkContext
207 IA32_REGISTER_SET
*UpdatedRegs
;
209 ASSERT (ThunkContext
!= NULL
);
210 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
< 0x100000);
211 ASSERT (ThunkContext
->RealModeBufferSize
>= m16Size
);
212 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
+ m16Size
<= 0x100000);
214 UpdatedRegs
= InternalAsmThunk16 (
215 ThunkContext
->RealModeState
,
216 ThunkContext
->RealModeBuffer
219 CopyMem (ThunkContext
->RealModeState
, UpdatedRegs
, sizeof (*UpdatedRegs
));
223 Prepares all structures and code for a 16-bit real mode thunk, transfers
224 control to a 16-bit real mode entry point, and returns the results.
226 Prepares all structures and code for a 16-bit real mode thunk, transfers
227 control to a 16-bit real mode entry point, and returns the results. If the
228 caller only need to perform a single 16-bit real mode thunk, then this
229 service should be used. If the caller intends to make more than one 16-bit
230 real mode thunk, then it is more efficient if AsmPrepareThunk16() is called
231 once and AsmThunk16() can be called for each 16-bit real mode thunk. This
232 function must be called with interrupts disabled.
234 If ThunkContext is NULL, then ASSERT().
236 @param ThunkContext A pointer to the context structure that describes the
237 16-bit real mode code to call.
242 AsmPrepareAndThunk16 (
243 IN OUT THUNK_CONTEXT
*ThunkContext
246 AsmPrepareThunk16 (ThunkContext
);
247 AsmThunk16 (ThunkContext
);