1/*++
2
3Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
4This program and the accompanying materials
5are licensed and made available under the terms and conditions of the BSD License
6which accompanies this distribution.  The full text of the license may be found at
7http://opensource.org/licenses/bsd-license.php
8
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12Module Name:
13
14  Thunk16Lib.h
15
16Abstract:
17
18  Real Mode Thunk Header file
19
20--*/
21
22#ifndef __THUNK_16_LIB_H__
23#define __THUNK_16_LIB_H__
24#include "Tiano.h"
25
26//
27// Thunk Flags
28//
29#define THUNK_SAVE_FP_STATE         0x1
30#define THUNK_USER_STACK            0x2
31#define THUNK_INTERRUPT             0x10000
32
33//
34// Byte packed structure for 16-bit Real Mode FLAGS
35//
36typedef union {
37  struct {
38    UINT32  CF:1;           // Carry Flag
39    UINT32  Reserved_0:1;   // Reserved
40    UINT32  PF:1;           // Parity Flag
41    UINT32  Reserved_1:1;   // Reserved
42    UINT32  AF:1;           // Auxiliary Carry Flag
43    UINT32  Reserved_2:1;   // Reserved
44    UINT32  ZF:1;           // Zero Flag
45    UINT32  SF:1;           // Sign Flag
46    UINT32  TF:1;           // Trap Flag
47    UINT32  IF:1;           // Interrupt Enable Flag
48    UINT32  DF:1;           // Direction Flag
49    UINT32  OF:1;           // Overflow Flag
50    UINT32  IOPL:2;         // I/O Privilege Level
51    UINT32  NT:1;           // Nested Task
52    UINT32  Reserved_3:1;   // Reserved
53  } Bits;
54  UINTN     UintN;
55} IA32_FLAGS16;
56
57//
58// Byte packed structure for EFLAGS
59// 32-bits on IA32
60// 64-bits on X64
61//
62
63typedef union {
64  struct {
65    UINT32  CF:1;           // Carry Flag
66    UINT32  Reserved_0:1;   // Reserved
67    UINT32  PF:1;           // Parity Flag
68    UINT32  Reserved_1:1;   // Reserved
69    UINT32  AF:1;           // Auxiliary Carry Flag
70    UINT32  Reserved_2:1;   // Reserved
71    UINT32  ZF:1;           // Zero Flag
72    UINT32  SF:1;           // Sign Flag
73    UINT32  TF:1;           // Trap Flag
74    UINT32  IF:1;           // Interrupt Enable Flag
75    UINT32  DF:1;           // Direction Flag
76    UINT32  OF:1;           // Overflow Flag
77    UINT32  IOPL:2;         // I/O Privilege Level
78    UINT32  NT:1;           // Nested Task
79    UINT32  Reserved_3:1;   // Reserved
80    UINT32  RF:1;           // Resume Flag
81    UINT32  VM:1;           // Virtual 8086 Mode
82    UINT32  AC:1;           // Alignment Check
83    UINT32  VIF:1;          // Virtual Interrupt Flag
84    UINT32  VIP:1;          // Virtual Interrupt Pending
85    UINT32  ID:1;           // ID Flag
86    UINT32  Reserved_4:10;  // Reserved
87  } Bits;
88  UINTN     UintN;
89} IA32_EFLAGS32;
90
91//
92// Byte packed structure for an FP/SSE/SSE2 context
93//
94typedef struct {
95  UINT8  Buffer[512];
96} IA32_FX_BUFFER;
97
98//
99// Structures for the 16-bit real mode thunks
100//
101typedef struct {
102  UINT32                            Reserved1;
103  UINT32                            Reserved2;
104  UINT32                            Reserved3;
105  UINT32                            Reserved4;
106  UINT8                             BL;
107  UINT8                             BH;
108  UINT16                            Reserved5;
109  UINT8                             DL;
110  UINT8                             DH;
111  UINT16                            Reserved6;
112  UINT8                             CL;
113  UINT8                             CH;
114  UINT16                            Reserved7;
115  UINT8                             AL;
116  UINT8                             AH;
117  UINT16                            Reserved8;
118} IA32_BYTE_REGS;
119
120typedef struct {
121  UINT16                            DI;
122  UINT16                            Reserved1;
123  UINT16                            SI;
124  UINT16                            Reserved2;
125  UINT16                            BP;
126  UINT16                            Reserved3;
127  UINT16                            SP;
128  UINT16                            Reserved4;
129  UINT16                            BX;
130  UINT16                            Reserved5;
131  UINT16                            DX;
132  UINT16                            Reserved6;
133  UINT16                            CX;
134  UINT16                            Reserved7;
135  UINT16                            AX;
136  UINT16                            Reserved8;
137  UINT16                            DS;
138  UINT16                            ES;
139  UINT16                            FS;
140  UINT16                            GS;
141  IA32_FLAGS16                      Flags;
142  UINT16                            IP;
143  UINT16                            Reserved10;
144  UINT16                            CS;
145  UINT16                            SS;
146} IA32_WORD_REGS;
147
148typedef struct {
149  UINT32                            EDI;
150  UINT32                            ESI;
151  UINT32                            EBP;
152  UINT32                            ESP;
153  UINT32                            EBX;
154  UINT32                            EDX;
155  UINT32                            ECX;
156  UINT32                            EAX;
157  UINT16                            DS;
158  UINT16                            ES;
159  UINT16                            FS;
160  UINT16                            GS;
161  IA32_EFLAGS32                     EFLAGS;
162  UINT32                            EIP;
163  UINT16                            CS;
164  UINT16                            SS;
165} IA32_DWORD_REGS;
166
167typedef union {
168  IA32_DWORD_REGS                   E;
169  IA32_WORD_REGS                    X;
170  IA32_BYTE_REGS                    H;
171} IA32_REGISTER_SET;
172
173//
174// Byte packed structure for an 16-bit real mode thunks
175//
176typedef struct {
177  UINT32                            RealModeBuffer;
178  UINT32                            DefaultStack;
179} THUNK_CONTEXT;
180
181//
182// 16-bit thunking services
183//
184
185UINTN
186EFIAPI
187AsmThunk16GetProperties (
188  OUT     UINTN                     *MinimumStackSize OPTIONAL
189  )
190/*++
191
192Routine Description:
193
194  Returns the properties of this real mode thunk implementation. Currently
195  there are 2 properties has been defined, the minimum real mode buffer size
196  and the minimum stack size.
197
198Arguments:
199
200  MinimumStackSize  - The minimum size required for a 16-bit stack.
201
202Returns:
203
204  The minimum size of the real mode buffer needed by this thunk implementation
205  is returned.
206
207--*/
208;
209
210THUNK_CONTEXT *
211EFIAPI
212AsmThunk16SetProperties (
213  OUT     THUNK_CONTEXT             *ThunkContext,
214  IN      VOID                      *RealModeBuffer,
215  IN      UINTN                     BufferSize
216  )
217/*++
218
219Routine Description:
220
221  Tell this real mode thunk implementation the address and size of the real
222  mode buffer needed.
223
224Arguments:
225
226  ThunkContext    - The thunk context whose properties to set.
227  RealModeBuffer  - The address of the buffer allocated by caller. It should be
228                    aligned on a 16-byte boundary.
229                    This buffer must be in identity mapped pages.
230  BufferSize      - The size of RealModeBuffer. Must be larger than the minimum
231                    size required as returned by AsmThunk16GetProperties().
232
233Returns:
234
235  None
236
237--*/
238;
239
240BOOLEAN
241AsmThunk16SetUserStack (
242  IN THUNK_CONTEXT             *ThunkContext,
243  IN VOID                      *Stack,
244  IN UINTN                     StackSize
245  );
246
247VOID
248EFIAPI
249AsmThunk16Destroy (
250  IN OUT  THUNK_CONTEXT             *ThunkContext
251  )
252/*++
253
254Routine Description:
255
256  Reset all internal states to their initial values. The caller should not
257  release the real mode buffer until after a call to this function.
258
259Arguments:
260
261  ThunkContext  - The thunk context to destroy.
262
263Returns:
264
265  None
266
267--*/
268;
269
270IA32_REGISTER_SET *
271EFIAPI
272AsmThunk16FarCall86 (
273  IN      THUNK_CONTEXT             *ThunkContext,
274  IN OUT  IA32_REGISTER_SET         *RegisterSet,
275  IN      UINT32                    ThunkFlags
276  )
277/*++
278
279Routine Description:
280
281  Make a far call to 16-bit code.
282
283  NOTE: This function must be called on TPL_HIGH_LEVEL or with interrupts
284        disabled because of GDTR and IDTR manipulations.
285        This function must be placed in identity mapped pages.
286
287Arguments:
288
289  ThunkContext  - Thunk context to use.
290  RegisterSet   - CPU registers would be set to the values contained in this
291                  structure before making the far call. Then CPU registers are
292                  copied back to this structure.
293                  CS:EIP points to the real mode code being called on input.
294                  SS:ESP points to the real mode stack if THUNK_USER_STACK is
295                  set on input, otherwise ignored.
296                  EFlages is ignored on input.
297                  On output, values of CS, EIP, SS and ESP should be ignored.
298  ThunkFlags    - 2 flags have currently been defined, THUNK_SAVE_FP_STATE and
299                  THUNK_USER_STACK.
300                  THUNK_SAVE_FP_STATE - FPU state would be saved/restored
301                                        before/after calling real mode code.
302                  THUNK_USER_STACK    - The stack specified by SS:ESP would be
303                                        used instead of the default stack.
304
305Returns:
306
307  RegisterSet is returned.
308
309--*/
310;
311
312IA32_REGISTER_SET *
313EFIAPI
314AsmThunk16Int86 (
315  IN      THUNK_CONTEXT             *ThunkContext,
316  IN      UINT8                     IntNumber,
317  IN OUT  IA32_REGISTER_SET         *RegisterSet,
318  IN      UINT32                    ThunkFlags
319  )
320/*++
321
322Routine Description:
323
324  Invoke a 16-bit interrupt handler.
325
326  NOTE: This function must be called on TPL_HIGH_LEVEL or with interrupts
327        disabled because of GDTR and IDTR manipulations.
328        This function must be placed in identity mapped pages.
329
330Arguments:
331
332  ThunkContext  - Thunk context to use.
333  IntNumber     - The ordinal of the interrupt handler ranging from 0 to 255.
334  RegisterSet   - CPU registers would be set to the values contained in this
335                  structure before making the far call. Then CPU registers are
336                  copied back to this structure.
337                  SS:ESP points to the real mode stack if THUNK_USER_STACK is
338                  set on input, otherwise ignored.
339                  EFlages is ignored on input.
340                  On output, values of CS, EIP, SS and ESP should be ignored.
341  ThunkFlags    - 2 flags have currently been defined, THUNK_SAVE_FP_STATE and
342                  THUNK_USER_STACK.
343                  THUNK_SAVE_FP_STATE - FPU state would be saved/restored
344                                        before/after calling real mode code.
345                  THUNK_USER_STACK    - The stack specified by SS:ESP would be
346                                        used instead of the default stack.
347
348Returns:
349
350  RegisterSet is returned.
351
352--*/
353;
354
355#endif
356