]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Ia32/Thunk16.S
Port thunk16 code from MSFT to GCC assembly.
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / Thunk16.S
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 # Thunk16.S
15 #
16 # Abstract:
17 #
18 # Real mode thunk
19 #
20 #------------------------------------------------------------------------------
21
22 .global _m16Start, _m16Size, _mThunk16Attr, _m16GdtrBase, _m16Gdt, _m16GdtrBase, _mTransition
23 .global _InternalAsmThunk16
24
25 #THUNK_ATTRIBUTE_BIG_REAL_MODE EQU 1
26 #THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 EQU 2
27 #THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL EQU 4
28
29 .code:
30
31 _m16Start:
32
33 SavedGdt: .space 6
34
35 _BackFromUserCode:
36 push %ss
37 push %cs
38 .byte 0x66
39 call @Base1 # push eip
40 @Base1:
41 pushfw # pushfd actually
42 cli # disable interrupts
43 push %gs
44 push %fs
45 push %es
46 push %ds
47 pushaw # pushad actually
48 .byte 0x66,0xba # mov edx, imm32
49 _ThunkAttr: .space 4
50 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15, %dl
51 jz @1
52 movl $0x15cd2401, %eax # mov ax, 2401h & int 15h
53 cli # disable interrupts
54 jnc @2
55 @1:
56 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL, %dl
57 jz @2
58 inb $0x92, %al
59 orb $2, %al
60 outb %al, $0x92 # deactivate A20M#
61 @2:
62 movl %ss, %eax
63 .byte 0x67, 0x66, 0x8d, 0x6c, 0x24, 0x34, 0x66
64 mov %ebp,0xffffffd8(%esi)
65 mov 0xfffffff8(%esi),%ebx
66 shlw $4, %ax # shl eax, 4
67 addw %ax, %bp # add ebp, eax
68 .byte 0x66,0xb8 # mov eax, imm32
69 SavedCr4: .space 4
70 movl %eax, %cr4
71 lgdtw %cs:0xfffffff2(%edi)
72 .byte 0x66,0xb8 # mov eax, imm32
73 SavedCr0: .space 4
74 movl %eax, %cr0
75 .byte 0xb8 # mov ax, imm16
76 SavedSs: .space 2
77 movl %eax, %ss
78 .byte 0x66,0xbc # mov esp, imm32
79 SavedEsp: .space 4
80 .byte 0x66
81 lret # return to protected mode
82
83 _EntryPoint: .long _ToUserCode - _m16Start
84 .word 0x8
85 _16Idtr: .word 0x3ff
86 .long 0
87 _16Gdtr: .word GdtEnd - _NullSegDesc - 1
88 _16GdtrBase: .long _NullSegDesc
89
90 _ToUserCode:
91 movl %ss, %edx
92 movl %ecx, %ss # set new segment selectors
93 movl %ecx, %ds
94 movl %ecx, %es
95 movl %ecx, %fs
96 movl %ecx, %gs
97 movl %eax, %cr0
98 movl %ebp, %cr4 # real mode starts at next instruction
99 movl %esi, %ss # set up 16-bit stack segment
100 xchgw %bx, %sp # set up 16-bit stack pointer
101 .byte 0x66
102 call @Base # push eip
103 @Base:
104 popw %bp # ebp <- offset @Base
105 addr16 pushl 36(%si)
106 .byte 0x36
107 lea 0xc(%esi),%eax
108 pushl %eax
109 lret
110
111 @RealMode:
112 mov %edx,%cs:0xffffffc5(%esi)
113 mov %bx,%cs:0xffffffcb(%esi)
114 lidtw %cs:0xffffffd7(%esi)
115 popaw # popad actually
116 popl %ds
117 popl %es
118 popl %fs
119 popl %gs
120 popfw # popfd
121 lretw # transfer control to user code
122
123 _NullSegDesc: .quad 0
124 _16CsDesc:
125 .word -1
126 .word 0
127 .byte 0
128 .byte 0x9b
129 .byte 0x8f # 16-bit segment, 4GB limit
130 .byte 0
131 _16DsDesc:
132 .word -1
133 .word 0
134 .byte 0
135 .byte 0x93
136 .byte 0x8f # 16-bit segment, 4GB limit
137 .byte 0
138 GdtEnd:
139
140 #
141 # @param RegSet Pointer to a IA32_DWORD_REGS structure
142 # @param Transition Pointer to the transition code
143 # @return The address of the 16-bit stack after returning from user code
144 #
145 _InternalAsmThunk16:
146 push %ebp
147 push %ebx
148 push %esi
149 push %edi
150 push %ds
151 push %es
152 push %fs
153 push %gs
154 movl 36(%esp), %esi # esi <- RegSet
155 movzwl 0x32(%esi),%edx
156 mov 0xc(%esi),%edi
157 add $0xffffffc8,%edi
158 movl %edi, %ebx # ebx <- stack offset
159 imul $0x10,%edx,%eax
160 push $0xd
161 addl %eax, %edi # edi <- linear address of 16-bit stack
162 popl %ecx
163 rep
164 movsl # copy RegSet
165 movl 40(%esp), %eax # eax <- address of transition code
166 movl %edx, %esi # esi <- 16-bit stack segment
167 lea 0x5e(%eax),%edx
168 movl %eax, %ecx
169 andl $0xf, %ecx
170 shll $12, %eax
171 lea 0x6(%ecx),%ecx
172 movw %cx, %ax
173 stosl # [edi] <- return address of user code
174 sgdtl 0xffffffa2(%edx)
175 sidtl 0x24(%esp)
176 movl %cr0, %eax
177 movl %eax, (%edx) # save CR0 in SavedCr0
178 andl $0x7ffffffe, %eax # clear PE, PG bits
179 movl %cr4, %ebp
180 mov %ebp,0xfffffff1(%edx)
181 andl $0x300, %ebp # clear all but PCE and OSFXSR bits
182 pushl $0x10
183 popl %ecx # ecx <- selector for data segments
184 lgdtl 0x20(%edx)
185 pushfl
186 lcall *0x14(%edx)
187 popfl
188 lidtl 0x24(%esp)
189 lea 0xffffffcc(%ebp),%eax
190 pop %gs
191 pop %fs
192 pop %es
193 pop %ds
194 pop %edi
195 pop %esi
196 pop %ebx
197 pop %ebp
198 ret
199
200 .const:
201
202 _m16Size: .word _InternalAsmThunk16 - _m16Start
203 _mThunk16Attr: .word _ThunkAttr - _m16Start
204 _m16Gdt: .word _NullSegDesc - _m16Start
205 _m16GdtrBase: .word _16GdtrBase - _m16Start
206 _mTransition: .word _EntryPoint - _m16Start
207