8cb5f4f8bdfce04b3dcecd41f0c2dbfb506a5b00
[mirror_edk2.git] / MdePkg / Library / BaseLib / x86Thunk.c
1 /** @file
2 Real Mode Thunk Functions for IA32 and X64.
3
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
9
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.
12
13 Module Name: x86Thunk.c
14
15 **/
16
17 /**
18 Invokes 16-bit code in big real mode and returns the updated register set.
19
20 This function transfers control to the 16-bit code specified by CS:EIP using
21 the stack specified by SS:ESP in RegisterSet. The updated registers are saved
22 on the real mode stack and the starting address of the save area is returned.
23
24 @param RegisterSet Values of registers before invocation of 16-bit code.
25 @param Patch Pointer to the area following the 16-bit code.
26
27 @return The pointer to a IA32_REGISTER_SET structure containing the updated
28 register values.
29
30 **/
31 IA32_REGISTER_SET *
32 InternalAsmThunk16 (
33 IN IA32_REGISTER_SET *RegisterSet,
34 IN OUT VOID *Patch
35 );
36
37 /**
38 Prepares all structures a code required to use AsmThunk16().
39
40 Prepares all structures and code required to use AsmThunk16().
41
42 If ThunkContext is NULL, then ASSERT().
43
44 @param ThunkContext A pointer to the context structure that describes the
45 16-bit real mode code to call.
46
47 **/
48 VOID
49 EFIAPI
50 AsmPrepareThunk16 (
51 OUT THUNK_CONTEXT *ThunkContext
52 )
53 {
54 ASSERT (ThunkContext != NULL);
55 }
56
57 /**
58 Transfers control to a 16-bit real mode entry point and returns the results.
59
60 Transfers control to a 16-bit real mode entry point and returns the results.
61 AsmPrepareThunk16() must be called with ThunkContext before this function is
62 used. This function must be called with interrupts disabled.
63
64 If ThunkContext is NULL, then ASSERT().
65 If AsmPrepareThunk16() was not previously called with ThunkContext, then ASSERT().
66
67 @param ThunkContext A pointer to the context structure that describes the
68 16-bit real mode code to call.
69
70 **/
71 VOID
72 EFIAPI
73 AsmThunk16 (
74 IN OUT THUNK_CONTEXT *ThunkContext
75 )
76 {
77 UINT16 *Patch;
78
79 ASSERT (ThunkContext != NULL);
80
81 Patch = (UINT16*)(
82 (UINTN)ThunkContext->RealModeCode +
83 ThunkContext->RealModeCodeSize
84 );
85
86 //
87 // 0x9a66 is the OpCode of far call with an operand size override.
88 //
89 *Patch = 0x9a66;
90
91 //
92 // CopyMem() here copies the updated register values back to RealModeState
93 //
94 CopyMem (
95 &ThunkContext->RealModeState,
96 InternalAsmThunk16 (&ThunkContext->RealModeState, Patch + 1),
97 sizeof (ThunkContext->RealModeState)
98 );
99 }
100
101 /**
102 Prepares all structures and code for a 16-bit real mode thunk, transfers
103 control to a 16-bit real mode entry point, and returns the results.
104
105 Prepares all structures and code for a 16-bit real mode thunk, transfers
106 control to a 16-bit real mode entry point, and returns the results. If the
107 caller only need to perform a single 16-bit real mode thunk, then this
108 service should be used. If the caller intends to make more than one 16-bit
109 real mode thunk, then it is more efficient if AsmPrepareThunk16() is called
110 once and AsmThunk16() can be called for each 16-bit real mode thunk. This
111 function must be called with interrupts disabled.
112
113 If ThunkContext is NULL, then ASSERT().
114
115 @param ThunkContext A pointer to the context structure that describes the
116 16-bit real mode code to call.
117
118 **/
119 VOID
120 EFIAPI
121 AsmPrepareAndThunk16 (
122 IN OUT THUNK_CONTEXT *ThunkContext
123 )
124 {
125 AsmPrepareThunk16 (ThunkContext);
126 AsmThunk16 (ThunkContext);
127 }