]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/Thunk16/X64/Thunk16.asm
Update the copyright notice format
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / Thunk16 / X64 / Thunk16.asm
1 ;*****************************************************************************
2 ;*
3 ;* Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
4 ;* 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 EXTERNDEF mCode16Size:QWORD
23
24 .const
25
26 mCode16Size DQ _Code16End - _Code16Addr
27
28 .data
29
30 NullSegSel DQ 0
31 _16CsSegSel LABEL QWORD
32 DW -1
33 DW 0
34 DB 0
35 DB 9bh
36 DB 8fh ; 16-bit segment
37 DB 0
38 _16DsSegSel LABEL QWORD
39 DW -1
40 DW 0
41 DB 0
42 DB 93h
43 DB 8fh ; 16-bit segment
44 DB 0
45
46 _16Gdtr LABEL FWORD
47 DW $ - offset NullSegSel - 1
48 DQ offset NullSegSel
49
50 .code
51
52 IA32_REGS STRUC 4t
53 _EDI DD ?
54 _ESI DD ?
55 _EBP DD ?
56 _ESP DD ?
57 _EBX DD ?
58 _EDX DD ?
59 _ECX DD ?
60 _EAX DD ?
61 _DS DW ?
62 _ES DW ?
63 _FS DW ?
64 _GS DW ?
65 _RFLAGS DQ ?
66 _EIP DD ?
67 _CS DW ?
68 _SS DW ?
69 IA32_REGS ENDS
70
71 _STK16 STRUC 1t
72 RetEip DD ?
73 RetCs DW ?
74 ThunkFlags DW ?
75 SavedGdtr FWORD ?
76 Resvd1 DW ?
77 SavedCr0 DD ?
78 SavedCr4 DD ?
79 _STK16 ENDS
80
81 _Thunk16 PROC USES rbp rbx rsi rdi r12 r13 r14 r15
82
83 push fs
84 push gs
85
86 mov r12d, ds
87 mov r13d, es
88 mov r14d, ss
89 mov r15, rsp
90 mov rsi, rcx
91 movzx r10, (IA32_REGS ptr [rsi])._SS
92 xor rdi, rdi
93 mov edi, (IA32_REGS ptr [rsi])._ESP
94 add rdi, - sizeof (IA32_REGS) - sizeof (_STK16)
95 push rdi
96 imul rax, r10, 16
97 add rdi, rax
98 push sizeof (IA32_REGS) / 4
99 pop rcx
100 rep movsd
101 pop rbx ; rbx <- 16-bit stack offset
102 lea eax, @F ; return offset
103 stosd
104 mov eax, cs ; return segment
105 stosw
106 mov eax, edx ; THUNK Flags
107 stosw
108 sgdt fword ptr [rsp + 58h] ; save GDTR
109 mov rax, [rsp + 58h]
110 stosq
111 mov rax, cr0 ; save CR0
112 mov esi, eax ; esi <- CR0 to set
113 stosd
114 mov rax, cr4 ; save CR4
115 stosd
116 sidt fword ptr [rsp + 58h] ; save IDTR
117 and esi, 07ffffffeh ; clear PE & PG bits
118 mov rdi, r10 ; rdi <- 16-bit stack segment
119
120 shl r8, 16
121 push r8 ; far jmp address
122 lea eax, @16Bit
123 push rax
124 mov word ptr [rsp + 4], 8
125 lgdt _16Gdtr
126 retf
127 @16Bit:
128 DB 66h
129 mov ecx, 0c0000080h
130 mov cr0, rsi ; disable PE & PG
131 rdmsr
132 and ah, NOT 1
133 wrmsr ; clear LME bit
134 mov rax, cr4
135 and al, NOT 30h ; clear PAE & PSE
136 mov cr4, rax
137 retf
138 @@:
139 xor rax, rax
140 mov eax, ss
141 shl eax, 4
142 add eax, esp ; rax <- address of 16-bit stack
143 mov rsp, r15
144 lidt fword ptr [rsp + 58h] ; restore IDTR
145 mov ds, r12d
146 mov es, r13d
147 mov ss, r14d
148 pop gs
149 pop fs
150 ret
151 _Thunk16 ENDP
152
153 ALIGN 10h
154
155 _Code16Addr PROC
156 _Code16Addr ENDP
157
158 RealMode PROC
159 mov ss, edi
160 mov sp, bx ; set up 16-bit stack
161 DB 2eh, 0fh, 1, 1eh
162 DW _16Idtr - _Code16Addr ; lidt _16Idtr
163 DB 66h, 61h ; popad
164 DB 1fh ; pop ds
165 DB 7 ; pop es
166 pop fs
167 pop gs
168
169 add esp, 8 ; skip RFLAGS
170 DB 67h, 0f7h, 44h, 24h, 0eh, 1, 0 ; test [esp + 0eh], 1
171 jz @F
172 pushfq ; pushf, actually
173 @@:
174 DB 0eh ; push cs
175 DB 68h ; push /iw
176 DW @FarCallRet - _Code16Addr
177 jz @F
178 DB 66h
179 jmp fword ptr [esp + 6]
180 @@:
181 DB 66h
182 jmp fword ptr [esp + 4]
183 @FarCallRet:
184 DB 66h
185 push 0 ; push a dword of zero
186 pushf ; pushfd, actually
187 push gs
188 push fs
189 DB 6 ; push es
190 DB 1eh ; push ds
191 DB 66h, 60h ; pushad
192 cli
193
194 DB 66h
195 lgdt (_STK16 ptr [esp + sizeof(IA32_REGS)]).SavedGdtr
196 DB 66h
197 mov eax, (_STK16 ptr [esp + sizeof(IA32_REGS)]).SavedCr4
198 mov cr4, rax
199 DB 66h
200 mov ecx, 0c0000080h
201 rdmsr
202 or ah, 1
203 wrmsr ; set LME
204 DB 66h
205 mov eax, (_STK16 ptr [esp + sizeof(IA32_REGS)]).SavedCr0
206 mov cr0, rax
207 DB 66h
208 jmp fword ptr (_STK16 ptr [esp + sizeof(IA32_REGS)]).RetEip
209
210 RealMode ENDP
211
212 _16Idtr FWORD (1 SHL 10) - 1
213
214 _Code16End:
215
216 END