]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/Thunk16/Ia32/Thunk16.asm
b59583effd175822b4d37bf2ae91abcb2954fc69
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Thunk16 / Ia32 / Thunk16.asm
1 ;*****************************************************************************
2 ;*
3 ;* Copyright (c) 2006 - 2007, Intel Corporation
4 ;* All rights reserved. This program and the accompanying materials
5 ;* are licensed and made available under the terms and conditions of the BSD License
6 ;* which accompanies this distribution. The full text of the license may be found at
7 ;* http://opensource.org/licenses/bsd-license.php
8 ;*
9 ;* THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 ;* WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 ;*
12 ;* Module Name:
13 ;*
14 ;* Thunk.asm
15 ;*
16 ;* Abstract:
17 ;*
18 ;* Real mode thunk
19 ;*
20 ;*****************************************************************************
21
22 .686p
23
24 EXTERNDEF C mCode16Size:DWORD
25
26 CONST SEGMENT FLAT "DATA" READONLY
27
28 mCode16Size DD _TEXT16SIZE
29
30 CONST ENDS
31
32 _DATA SEGMENT FLAT "DATA"
33
34 NullSegSel DQ 0
35 _16BitCsSel LABEL QWORD
36 DW -1
37 DW 0
38 DB 0
39 DB 9bh
40 DB 8fh ; 16-bit segment
41 DB 0
42 _16BitSsSel LABEL QWORD
43 DW -1
44 DW 0
45 DB 0
46 DB 93h
47 DB 8fh ; 16-bit segment
48 DB 0
49
50 _16Gdtr LABEL FWORD
51 DW $ - offset NullSegSel - 1
52 DD offset NullSegSel
53
54 _DATA ENDS
55
56 _TEXT SEGMENT FLAT "CODE" PARA
57
58 IA32_REGS STRUC 4t
59 _EDI DD ?
60 _ESI DD ?
61 _EBP DD ?
62 _ESP DD ?
63 _EBX DD ?
64 _EDX DD ?
65 _ECX DD ?
66 _EAX DD ?
67 _DS DW ?
68 _ES DW ?
69 _FS DW ?
70 _GS DW ?
71 _EFLAGS DD ?
72 _EIP DD ?
73 _CS DW ?
74 _SS DW ?
75 IA32_REGS ENDS
76
77 _STK16 STRUC 1t
78 RetEip DD ?
79 RetCs DW ?
80 ThunkFlags DW ?
81 SavedEsp DD ?
82 SavedSs DW ?
83 SavedGdtr FWORD ?
84 SavedCr0 DD ?
85 SavedCr4 DD ?
86 _STK16 ENDS
87
88 ASSUME ds:_DATA
89
90 __Thunk16 PROC USES ebp ebx esi edi ds es fs gs
91 ASSUME esi:PTR IA32_REGS
92 mov esi, [esp + 36]
93 movzx edx, [esi]._SS
94 mov edi, [esi]._ESP
95 add edi, - sizeof (_STK16) - sizeof (IA32_REGS)
96 push edi ; save stack offset
97 imul eax, edx, 16 ; eax <- edx*16
98 add edi, eax ; edi <- linear address of 16-bit stack
99 push sizeof (IA32_REGS) / 4
100 pop ecx
101 rep movsd ; copy context to 16-bit stack
102 pop ebx ; ebx <- 16-bit stack offset
103 mov eax, offset @F ; return offset
104 stosd
105 mov eax, cs ; return segment
106 stosw
107 mov eax, [esp + 40] ; THUNK flags
108 stosw
109 mov eax, esp
110 stosd ; save esp
111 mov eax, ss ; save ss
112 stosw
113 sgdt fword ptr [edi] ; save GDTR
114 sidt fword ptr [esp + 36] ; save IDTR
115 mov esi, cr0
116 mov [edi + 6], esi ; save CR0
117 and esi, NOT 80000001h ; esi <- CR0 to set
118 mov eax, cr4
119 mov [edi + 10], eax ; save CR4
120 and al, NOT 30h ; clear PAE & PSE
121 mov edi, edx ; edi <- 16-bit stack segment
122 mov edx, [esp + 44]
123 shl edx, 16
124 push edx
125 pop edx
126 mov dx, _16BitSsSel - NullSegSel
127 lgdt _16Gdtr ; load 16-bit GDTR
128 DB 0eah
129 DD offset @16Bit
130 DW _16BitCsSel - NullSegSel ; jmp far 8:@16Bit
131 @16Bit:
132 mov ss, dx
133 mov cr0, esi ; disable protected mode
134 mov cr4, eax ; disable PAE & PSE
135 db 67h, 0FFh, 06Ch, 024h, 0FCh ; jmp dword ptr [esp-4]
136 @@:
137 mov eax, ss
138 shl eax, 4
139 add eax, esp ; eax <- address of 16-bit stack
140 lss esp, fword ptr (_STK16 ptr [esp + sizeof (IA32_REGS)]).SavedEsp
141 lidt fword ptr [esp + 36] ; restore IDTR
142 ret
143 __Thunk16 ENDP
144
145 _TEXT ENDS
146
147 _TEXT16 SEGMENT USE16 "CODE" PARA
148
149 _Code16Addr PROC C
150 _Code16Addr ENDP
151
152 RealMode PROC
153 mov ss, di ; set up stack
154 mov esp, ebx
155 lidt fword ptr cs:[_16Idtr - _Code16Addr]
156 popad
157 pop ds
158 pop es
159 pop fs
160 pop gs
161 add sp, 4 ; skip EFlags
162 test (_STK16 ptr [esp + 8]).ThunkFlags, 1
163 jz @F
164 pushf
165 @@:
166 push cs
167 ; push @FarCallRet - _Code16Addr
168 DB 68h ; push /iw
169 DW @FarCallRet - _Code16Addr
170 jz @F
171 jmp fword ptr [esp + 6]
172 @@:
173 jmp fword ptr [esp + 4]
174 @FarCallRet:
175 pushfd
176 push gs
177 push fs
178 push es
179 push ds
180 pushad
181 cli
182 DB 66h
183 lgdt (_STK16 ptr [esp + sizeof (IA32_REGS)]).SavedGdtr
184 mov eax, (_STK16 ptr [esp + sizeof (IA32_REGS)]).SavedCr4
185 mov cr4, eax
186 mov eax, (_STK16 ptr [esp + sizeof (IA32_REGS)]).SavedCr0
187 mov cr0, eax ; restore CR0
188 jmp fword ptr (_STK16 ptr [esp + sizeof (IA32_REGS)]).RetEip
189 RealMode ENDP
190
191 _16Idtr FWORD (1 SHL 10) - 1
192
193 _TEXT16END:
194
195 _TEXT16SIZE = _TEXT16END - _Code16Addr
196
197 _TEXT16 ENDS
198
199 END