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