]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Ia32/Thunk16.asm
Updated BaseLib for THUNK functions and some CPU functions
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / Thunk16.asm
1 ;------------------------------------------------------------------------------
2 ;
3 ; Copyright (c) 2006, 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 .model flat,C
24
25 EXTERNDEF C m16Start:BYTE
26 EXTERNDEF C m16Size:WORD
27 EXTERNDEF C mThunk16Attr:WORD
28 EXTERNDEF C m16Gdt:WORD
29 EXTERNDEF C m16GdtrBase:WORD
30 EXTERNDEF C mTransition:WORD
31
32 THUNK_ATTRIBUTE_BIG_REAL_MODE EQU 1
33 THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 EQU 2
34 THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL EQU 4
35
36 IA32_REGS STRUC 4t
37 _EDI DD ?
38 _ESI DD ?
39 _EBP DD ?
40 _ESP DD ?
41 _EBX DD ?
42 _EDX DD ?
43 _ECX DD ?
44 _EAX DD ?
45 _DS DW ?
46 _ES DW ?
47 _FS DW ?
48 _GS DW ?
49 _EFLAGS DD ?
50 _EIP DD ?
51 _CS DW ?
52 _SS DW ?
53 IA32_REGS ENDS
54
55 .const
56
57 m16Size DW offset InternalAsmThunk16 - offset m16Start
58 mThunk16Attr DW offset _ThunkAttr - offset m16Start
59 m16Gdt DW offset _NullSegDesc - offset m16Start
60 m16GdtrBase DW offset _16GdtrBase - offset m16Start
61 mTransition DW offset _EntryPoint - offset m16Start
62
63 .code
64
65 m16Start LABEL BYTE
66
67 SavedGdt LABEL FWORD
68 DW ?
69 DD ?
70
71 _BackFromUserCode PROC
72 push ss
73 push cs
74 DB 66h
75 call @Base ; push eip
76 @Base:
77 pushf ; pushfd actually
78 cli ; disable interrupts
79 push gs
80 push fs
81 push es
82 push ds
83 pushaw ; pushad actually
84 DB 66h, 0bah ; mov edx, imm32
85 _ThunkAttr DD ?
86 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
87 jz @1
88 mov eax, 15cd2401h ; mov ax, 2401h & int 15h
89 cli ; disable interrupts
90 jnc @2
91 @1:
92 test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
93 jz @2
94 in al, 92h
95 or al, 2
96 out 92h, al ; deactivate A20M#
97 @2:
98 mov eax, ss
99 DB 67h
100 lea bp, [esp + sizeof (IA32_REGS)]
101 mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp
102 mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP
103 shl ax, 4 ; shl eax, 4
104 add bp, ax ; add ebp, eax
105 DB 66h, 0b8h ; mov eax, imm32
106 SavedCr4 DD ?
107 mov cr4, eax
108 DB 66h
109 lgdt fword ptr cs:[edi + (offset SavedGdt - offset @Base)]
110 DB 66h, 0b8h ; mov eax, imm32
111 SavedCr0 DD ?
112 mov cr0, eax
113 DB 0b8h ; mov ax, imm16
114 SavedSs DW ?
115 mov ss, eax
116 DB 66h, 0bch ; mov esp, imm32
117 SavedEsp DD ?
118 DB 66h
119 retf ; return to protected mode
120 _BackFromUserCode ENDP
121
122 _EntryPoint DD offset _ToUserCode - offset m16Start
123 DW 8h
124 _16Idtr FWORD (1 SHL 10) - 1
125 _16Gdtr LABEL FWORD
126 DW offset GdtEnd - offset _NullSegDesc - 1
127 _16GdtrBase DD offset _NullSegDesc
128
129 _ToUserCode PROC
130 mov edx, ss
131 mov ss, ecx ; set new segment selectors
132 mov ds, ecx
133 mov es, ecx
134 mov fs, ecx
135 mov gs, ecx
136 mov cr0, eax
137 mov cr4, ebp ; real mode starts at next instruction
138 mov ss, esi ; set up 16-bit stack segment
139 xchg sp, bx ; set up 16-bit stack pointer
140 DB 66h
141 call @Base ; push eip
142 @Base:
143 pop bp ; ebp <- offset @Base
144 mov cs:[esi + (offset SavedSs - offset @Base)], edx
145 mov cs:[esi + (offset SavedEsp - offset @Base)], bx
146 DB 66h
147 lidt fword ptr cs:[esi + (offset _16Idtr - offset @Base)]
148 popaw ; popad actually
149 pop ds
150 pop es
151 pop fs
152 pop gs
153 popf ; popfd
154 DB 66h
155 retf ; transfer control to user code
156 _ToUserCode ENDP
157
158 _NullSegDesc DQ 0
159 _16CsDesc LABEL QWORD
160 DW -1
161 DW 0
162 DB 0
163 DB 9bh
164 DB 8fh ; 16-bit segment, 4GB limit
165 DB 0
166 _16DsDesc LABEL QWORD
167 DW -1
168 DW 0
169 DB 0
170 DB 93h
171 DB 8fh ; 16-bit segment, 4GB limit
172 DB 0
173 GdtEnd LABEL QWORD
174
175 ;
176 ; @param RegSet Pointer to a IA32_DWORD_REGS structure
177 ; @param Transition Pointer to the transition code
178 ; @return The address of the 16-bit stack after returning from user code
179 ;
180 InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs
181 mov esi, [esp + 36] ; esi <- RegSet
182 movzx edx, (IA32_REGS ptr [esi])._SS
183 mov edi, (IA32_REGS ptr [esi])._ESP
184 add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space
185 mov ebx, edi ; ebx <- stack offset
186 imul eax, edx, 16 ; eax <- edx * 16
187 push sizeof (IA32_REGS) / 4
188 add edi, eax ; edi <- linear address of 16-bit stack
189 pop ecx
190 rep movsd ; copy RegSet
191 mov eax, [esp + 40] ; eax <- address of transition code
192 mov esi, edx ; esi <- 16-bit stack segment
193 lea edx, [eax + (offset SavedCr0 - offset m16Start)]
194 mov ecx, eax
195 and ecx, 0fh
196 shl eax, 12
197 lea ecx, [ecx + (offset _BackFromUserCode - offset m16Start)]
198 mov ax, cx
199 stosd ; [edi] <- return address of user code
200 sgdt fword ptr [edx + (offset SavedGdt - offset SavedCr0)]
201 sidt fword ptr [esp + 36] ; save IDT stack in argument space
202 mov eax, cr0
203 mov [edx], eax ; save CR0 in SavedCr0
204 and eax, 7ffffffeh ; clear PE, PG bits
205 mov ebp, cr4
206 mov [edx + (offset SavedCr4 - offset SavedCr0)], ebp
207 and ebp, 300h ; clear all but PCE and OSFXSR bits
208 push 10h
209 pop ecx ; ecx <- selector for data segments
210 lgdt fword ptr [edx + (offset _16Gdtr - offset SavedCr0)]
211 call fword ptr [edx + (offset _EntryPoint - offset SavedCr0)]
212 lidt fword ptr [esp + 36] ; restore protected mode IDTR
213 lea eax, [ebp - sizeof (IA32_REGS)]
214 ret
215 InternalAsmThunk16 ENDP
216
217 END