1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/** @file
Real Mode Thunk Functions for IA32 and x64.
Copyright (c) 2006 - 2008, Intel Corporation<BR>
All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "BaseLibInternals.h"
//
// Byte packed structure for a segment descriptor in a GDT/LDT
//
typedef union {
struct {
UINT32 LimitLow:16;
UINT32 BaseLow:16;
UINT32 BaseMid:8;
UINT32 Type:4;
UINT32 S:1;
UINT32 DPL:2;
UINT32 P:1;
UINT32 LimitHigh:4;
UINT32 AVL:1;
UINT32 L:1;
UINT32 DB:1;
UINT32 G:1;
UINT32 BaseHigh:8;
} Bits;
UINT64 Uint64;
} IA32_SEGMENT_DESCRIPTOR;
extern CONST UINT8 m16Start;
extern CONST UINT16 m16Size;
extern CONST UINT16 mThunk16Attr;
extern CONST UINT16 m16Gdt;
extern CONST UINT16 m16GdtrBase;
extern CONST UINT16 mTransition;
/**
Invokes 16-bit code in big real mode and returns the updated register set.
This function transfers control to the 16-bit code specified by CS:EIP using
the stack specified by SS:ESP in RegisterSet. The updated registers are saved
on the real mode stack and the starting address of the save area is returned.
@param RegisterSet Values of registers before invocation of 16-bit code.
@param Transition Pointer to the transition code under 1MB.
@return The pointer to a IA32_REGISTER_SET structure containing the updated
register values.
**/
IA32_REGISTER_SET *
EFIAPI
InternalAsmThunk16 (
IN IA32_REGISTER_SET *RegisterSet,
IN OUT VOID *Transition
);
/**
Retrieves the properties for 16-bit thunk functions.
Computes the size of the buffer and stack below 1MB required to use the
AsmPrepareThunk16(), AsmThunk16() and AsmPrepareAndThunk16() functions. This
buffer size is returned in RealModeBufferSize, and the stack size is returned
in ExtraStackSize. If parameters are passed to the 16-bit real mode code,
then the actual minimum stack size is ExtraStackSize plus the maximum number
of bytes that need to be passed to the 16-bit real mode code.
If RealModeBufferSize is NULL, then ASSERT().
If ExtraStackSize is NULL, then ASSERT().
@param RealModeBufferSize A pointer to the size of the buffer below 1MB
required to use the 16-bit thunk functions.
@param ExtraStackSize A pointer to the extra size of stack below 1MB
that the 16-bit thunk functions require for
temporary storage in the transition to and from
16-bit real mode.
**/
VOID
EFIAPI
AsmGetThunk16Properties (
OUT UINT32 *RealModeBufferSize,
OUT UINT32 *ExtraStackSize
)
{
ASSERT (RealModeBufferSize != NULL);
ASSERT (ExtraStackSize != NULL);
*RealModeBufferSize = m16Size;
//
// Extra 4 bytes for return address, and another 4 bytes for mode transition
//
*ExtraStackSize = sizeof (IA32_DWORD_REGS) + 8;
}
/**
Prepares all structures a code required to use AsmThunk16().
Prepares all structures and code required to use AsmThunk16().
This interface is limited to be used in physical mode but can not be used in
virtual mode with paging.
If ThunkContext is NULL, then ASSERT().
@param ThunkContext A pointer to the context structure that describes the
16-bit real mode code to call.
**/
VOID
EFIAPI
AsmPrepareThunk16 (
OUT THUNK_CONTEXT *ThunkContext
)
{
IA32_SEGMENT_DESCRIPTOR *RealModeGdt;
ASSERT (ThunkContext != NULL);
ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);
ASSERT (ThunkContext->RealModeBufferSize >= m16Size);
ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);
CopyMem (ThunkContext->RealModeBuffer, &m16Start, m16Size);
//
// Point RealModeGdt to the GDT to be used in transition
//
// RealModeGdt[0]: Reserved as NULL descriptor
// RealModeGdt[1]: Code Segment
// RealModeGdt[2]: Data Segment
// RealModeGdt[3]: Call Gate
//
RealModeGdt = (IA32_SEGMENT_DESCRIPTOR*)(
(UINTN)ThunkContext->RealModeBuffer + m16Gdt);
//
// Update Code & Data Segment Descriptor
//
RealModeGdt[1].Bits.BaseLow =
(UINT32)(UINTN)ThunkContext->RealModeBuffer & ~0xf;
RealModeGdt[1].Bits.BaseMid =
(UINT32)(UINTN)ThunkContext->RealModeBuffer >> 16;
//
// Update transition code entry point offset
//
*(UINT32*)((UINTN)ThunkContext->RealModeBuffer + mTransition) +=
(UINT32)(UINTN)ThunkContext->RealModeBuffer & 0xf;
//
// Update Segment Limits for both Code and Data Segment Descriptors
//
if ((ThunkContext->ThunkAttributes & THUNK_ATTRIBUTE_BIG_REAL_MODE) == 0) {
//
// Set segment limits to 64KB
//
RealModeGdt[1].Bits.LimitHigh = 0;
RealModeGdt[1].Bits.G = 0;
RealModeGdt[2].Bits.LimitHigh = 0;
RealModeGdt[2].Bits.G = 0;
}
//
// Update GDTBASE for this thunk context
//
*(VOID**)((UINTN)ThunkContext->RealModeBuffer + m16GdtrBase) = RealModeGdt;
//
// Update Thunk Attributes
//
*(UINT32*)((UINTN)ThunkContext->RealModeBuffer + mThunk16Attr) =
ThunkContext->ThunkAttributes;
}
/**
Transfers control to a 16-bit real mode entry point and returns the results.
Transfers control to a 16-bit real mode entry point and returns the results.
AsmPrepareThunk16() must be called with ThunkContext before this function is used.
This function must be called with interrupts disabled.
The register state from the RealModeState field of ThunkContext is restored just prior
to calling the 16-bit real mode entry point. This includes the EFLAGS field of RealModeState,
which is used to set the interrupt state when a 16-bit real mode entry point is called.
Control is transferred to the 16-bit real mode entry point specified by the CS and Eip fields of RealModeState.
The stack is initialized to the SS and ESP fields of RealModeState. Any parameters passed to
the 16-bit real mode code must be populated by the caller at SS:ESP prior to calling this function.
The 16-bit real mode entry point is invoked with a 16-bit CALL FAR instruction,
so when accessing stack contents, the 16-bit real mode code must account for the 16-bit segment
and 16-bit offset of the return address that were pushed onto the stack. The 16-bit real mode entry
point must exit with a RETF instruction. The register state is captured into RealModeState immediately
after the RETF instruction is executed.
If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts,
or any of the 16-bit real mode code makes a SW interrupt, then the caller is responsible for making sure
the IDT at address 0 is initialized to handle any HW or SW interrupts that may occur while in 16-bit real mode.
If EFLAGS specifies interrupts enabled, or any of the 16-bit real mode code enables interrupts,
then the caller is responsible for making sure the 8259 PIC is in a state compatible with 16-bit real mode.
This includes the base vectors, the interrupt masks, and the edge/level trigger mode.
If THUNK_ATTRIBUTE_BIG_REAL_MODE is set in the ThunkAttributes field of ThunkContext, then the user code
is invoked in big real mode. Otherwise, the user code is invoked in 16-bit real mode with 64KB segment limits.
If neither THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 nor THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in
ThunkAttributes, then it is assumed that the user code did not enable the A20 mask, and no attempt is made to
disable the A20 mask.
If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is set and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is clear in
ThunkAttributes, then attempt to use the INT 15 service to disable the A20 mask. If this INT 15 call fails,
then attempt to disable the A20 mask by directly accessing the 8042 keyboard controller I/O ports.
If THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 is clear and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL is set in
ThunkAttributes, then attempt to disable the A20 mask by directly accessing the 8042 keyboard controller I/O ports.
If ThunkContext is NULL, then ASSERT().
If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().
If both THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 and THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL are set in
ThunkAttributes, then ASSERT().
This interface is limited to be used in physical mode but can not be used in
virtual mode with paging.
@param ThunkContext A pointer to the context structure that describes the
16-bit real mode code to call.
**/
VOID
EFIAPI
AsmThunk16 (
IN OUT THUNK_CONTEXT *ThunkContext
)
{
IA32_REGISTER_SET *UpdatedRegs;
ASSERT (ThunkContext != NULL);
ASSERT ((UINTN)ThunkContext->RealModeBuffer < 0x100000);
ASSERT (ThunkContext->RealModeBufferSize >= m16Size);
ASSERT ((UINTN)ThunkContext->RealModeBuffer + m16Size <= 0x100000);
ASSERT (((ThunkContext->ThunkAttributes & (THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 | THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL)) != \
(THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 | THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL)));
UpdatedRegs = InternalAsmThunk16 (
ThunkContext->RealModeState,
ThunkContext->RealModeBuffer
);
CopyMem (ThunkContext->RealModeState, UpdatedRegs, sizeof (*UpdatedRegs));
}
/**
Prepares all structures and code for a 16-bit real mode thunk, transfers
control to a 16-bit real mode entry point, and returns the results.
Prepares all structures and code for a 16-bit real mode thunk, transfers
control to a 16-bit real mode entry point, and returns the results. If the
caller only need to perform a single 16-bit real mode thunk, then this
service should be used. If the caller intends to make more than one 16-bit
real mode thunk, then it is more efficient if AsmPrepareThunk16() is called
once and AsmThunk16() can be called for each 16-bit real mode thunk.
This interface is limited to be used in physical mode but can not be used in
virtual mode with paging.
See AsmPrepareThunk16() and AsmThunk16() for the detailed description and ASSERT() conditions.
@param ThunkContext A pointer to the context structure that describes the
16-bit real mode code to call.
**/
VOID
EFIAPI
AsmPrepareAndThunk16 (
IN OUT THUNK_CONTEXT *ThunkContext
)
{
AsmPrepareThunk16 (ThunkContext);
AsmThunk16 (ThunkContext);
}
|