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 // Byte packed structure for a segment descriptor in a GDT/LDT
37 } IA32_SEGMENT_DESCRIPTOR
;
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
;
47 Invokes 16-bit code in big real mode and returns the updated register set.
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.
53 @param RegisterSet Values of registers before invocation of 16-bit code.
54 @param Transition Pointer to the transition code under 1MB.
56 @return The pointer to a IA32_REGISTER_SET structure containing the updated
63 IN IA32_REGISTER_SET
*RegisterSet
,
64 IN OUT VOID
*Transition
68 Retrieves the properties for 16-bit thunk functions.
70 Computes the size of the buffer and stack below 1MB required to use the
71 AsmPrepareThunk16(), AsmThunk16() and AsmPrepareAndThunk16() functions. This
72 buffer size is returned in RealModeBufferSize, and the stack size is returned
73 in ExtraStackSize. If parameters are passed to the 16-bit real mode code,
74 then the actual minimum stack size is ExtraStackSize plus the maximum number
75 of bytes that need to be passed to the 16-bit real mode code.
77 If RealModeBufferSize is NULL, then ASSERT().
78 If ExtraStackSize is NULL, then ASSERT().
80 @param RealModeBufferSize A pointer to the size of the buffer below 1MB
81 required to use the 16-bit thunk functions.
82 @param ExtraStackSize A pointer to the extra size of stack below 1MB
83 that the 16-bit thunk functions require for
84 temporary storage in the transition to and from
90 AsmGetThunk16Properties (
91 OUT UINT32
*RealModeBufferSize
,
92 OUT UINT32
*ExtraStackSize
95 ASSERT (RealModeBufferSize
!= NULL
);
96 ASSERT (ExtraStackSize
!= NULL
);
98 *RealModeBufferSize
= m16Size
;
101 // Extra 4 bytes for return address, and another 4 bytes for mode transition
103 *ExtraStackSize
= sizeof (IA32_DWORD_REGS
) + 8;
107 Prepares all structures a code required to use AsmThunk16().
109 Prepares all structures and code required to use AsmThunk16().
111 If ThunkContext is NULL, then ASSERT().
113 @param ThunkContext A pointer to the context structure that describes the
114 16-bit real mode code to call.
120 OUT THUNK_CONTEXT
*ThunkContext
123 IA32_SEGMENT_DESCRIPTOR
*RealModeGdt
;
125 ASSERT (ThunkContext
!= NULL
);
126 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
< 0x100000);
127 ASSERT (ThunkContext
->RealModeBufferSize
>= m16Size
);
128 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
+ m16Size
<= 0x100000);
130 CopyMem (ThunkContext
->RealModeBuffer
, &m16Start
, m16Size
);
133 // Point RealModeGdt to the GDT to be used in transition
135 // RealModeGdt[0]: Reserved as NULL descriptor
136 // RealModeGdt[1]: Code Segment
137 // RealModeGdt[2]: Data Segment
138 // RealModeGdt[3]: Call Gate
140 RealModeGdt
= (IA32_SEGMENT_DESCRIPTOR
*)(
141 (UINTN
)ThunkContext
->RealModeBuffer
+ m16Gdt
);
144 // Update Code & Data Segment Descriptor
146 RealModeGdt
[1].Bits
.BaseLow
=
147 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
& ~0xf;
148 RealModeGdt
[1].Bits
.BaseMid
=
149 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
>> 16;
152 // Update transition code entry point offset
154 *(UINT32
*)((UINTN
)ThunkContext
->RealModeBuffer
+ mTransition
) +=
155 (UINT32
)(UINTN
)ThunkContext
->RealModeBuffer
& 0xf;
158 // Update Segment Limits for both Code and Data Segment Descriptors
160 if ((ThunkContext
->ThunkAttributes
& THUNK_ATTRIBUTE_BIG_REAL_MODE
) == 0) {
162 // Set segment limits to 64KB
164 RealModeGdt
[1].Bits
.LimitHigh
= 0;
165 RealModeGdt
[1].Bits
.G
= 0;
166 RealModeGdt
[2].Bits
.LimitHigh
= 0;
167 RealModeGdt
[2].Bits
.G
= 0;
171 // Update GDTBASE for this thunk context
173 *(VOID
**)((UINTN
)ThunkContext
->RealModeBuffer
+ m16GdtrBase
) = RealModeGdt
;
176 // Update Thunk Attributes
178 *(UINT32
*)((UINTN
)ThunkContext
->RealModeBuffer
+ mThunk16Attr
) =
179 ThunkContext
->ThunkAttributes
;
183 Transfers control to a 16-bit real mode entry point and returns the results.
185 Transfers control to a 16-bit real mode entry point and returns the results.
186 AsmPrepareThunk16() must be called with ThunkContext before this function is
187 used. This function must be called with interrupts disabled.
189 If ThunkContext is NULL, then ASSERT().
190 If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().
192 @param ThunkContext A pointer to the context structure that describes the
193 16-bit real mode code to call.
199 IN OUT THUNK_CONTEXT
*ThunkContext
202 IA32_REGISTER_SET
*UpdatedRegs
;
204 ASSERT (ThunkContext
!= NULL
);
205 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
< 0x100000);
206 ASSERT (ThunkContext
->RealModeBufferSize
>= m16Size
);
207 ASSERT ((UINTN
)ThunkContext
->RealModeBuffer
+ m16Size
<= 0x100000);
209 UpdatedRegs
= InternalAsmThunk16 (
210 ThunkContext
->RealModeState
,
211 ThunkContext
->RealModeBuffer
214 CopyMem (ThunkContext
->RealModeState
, UpdatedRegs
, sizeof (*UpdatedRegs
));
218 Prepares all structures and code for a 16-bit real mode thunk, transfers
219 control to a 16-bit real mode entry point, and returns the results.
221 Prepares all structures and code for a 16-bit real mode thunk, transfers
222 control to a 16-bit real mode entry point, and returns the results. If the
223 caller only need to perform a single 16-bit real mode thunk, then this
224 service should be used. If the caller intends to make more than one 16-bit
225 real mode thunk, then it is more efficient if AsmPrepareThunk16() is called
226 once and AsmThunk16() can be called for each 16-bit real mode thunk. This
227 function must be called with interrupts disabled.
229 If ThunkContext is NULL, then ASSERT().
231 @param ThunkContext A pointer to the context structure that describes the
232 16-bit real mode code to call.
237 AsmPrepareAndThunk16 (
238 IN OUT THUNK_CONTEXT
*ThunkContext
241 AsmPrepareThunk16 (ThunkContext
);
242 AsmThunk16 (ThunkContext
);