]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/Thunk16.S
Fix the issue to clear the high 16-bit of EAX before use it Thunk code.
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / Thunk16.S
CommitLineData
e1f414b6 1#------------------------------------------------------------------------------\r
2#\r
c577049a 3# Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
bb817c56 4# This program and the accompanying materials\r
e1f414b6 5# are licensed and made available under the terms and conditions of the BSD License\r
6# which accompanies this distribution. The full text of the license may be found at\r
e4a34497 7# http://opensource.org/licenses/bsd-license.php.\r
e1f414b6 8#\r
9# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11#\r
12# Module Name:\r
13#\r
14# Thunk16.S\r
15#\r
16# Abstract:\r
17#\r
18# Real mode thunk\r
19#\r
20#------------------------------------------------------------------------------\r
21\r
bff2467a 22#include <Library/BaseLib.h>\r
23\r
132f41f0 24ASM_GLOBAL ASM_PFX(m16Start), ASM_PFX(m16Size), ASM_PFX(mThunk16Attr), ASM_PFX(m16Gdt), ASM_PFX(m16GdtrBase), ASM_PFX(mTransition)\r
25ASM_GLOBAL ASM_PFX(InternalAsmThunk16)\r
e1f414b6 26\r
27ASM_PFX(m16Start):\r
28\r
29SavedGdt: .space 6\r
30\r
31ASM_PFX(BackFromUserCode):\r
32 push %ss\r
33 push %cs\r
34 .byte 0x66\r
35 call L_Base1 # push eip\r
36L_Base1:\r
37 pushfw # pushfd actually\r
38 cli # disable interrupts\r
39 push %gs\r
40 push %fs\r
41 push %es\r
42 push %ds\r
43 pushaw # pushad actually\r
44 .byte 0x66, 0xba # mov edx, imm32\r
45ASM_PFX(ThunkAttr): .space 4\r
46 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15, %dl\r
47 jz 1f\r
48 movl $0x15cd2401, %eax # mov ax, 2401h & int 15h\r
49 cli # disable interrupts\r
50 jnc 2f\r
511:\r
52 testb $THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL, %dl\r
53 jz 2f\r
54 inb $0x92, %al\r
55 orb $2, %al\r
56 outb %al, $0x92 # deactivate A20M#\r
572:\r
c577049a
LG
58 xorw %ax, %ax # xor eax, eax\r
59 movl %ss, %eax # mov ax, ss\r
e1f414b6 60 .byte 0x67, 0x66, 0x8d, 0x6c, 0x24, 0x34, 0x66\r
61 mov %ebp, 0xffffffd8(%esi)\r
62 mov 0xfffffff8(%esi), %ebx\r
63 shlw $4, %ax # shl eax, 4\r
64 addw %ax, %bp # add ebp, eax\r
65 .byte 0x66, 0xb8 # mov eax, imm32\r
66SavedCr4: .space 4\r
67 movl %eax, %cr4\r
68 lgdtw %cs:0xfffffff2(%edi)\r
69 .byte 0x66, 0xb8 # mov eax, imm32\r
70SavedCr0: .space 4\r
71 movl %eax, %cr0\r
72 .byte 0xb8 # mov ax, imm16\r
73SavedSs: .space 2\r
74 movl %eax, %ss\r
75 .byte 0x66, 0xbc # mov esp, imm32\r
76SavedEsp: .space 4\r
77 .byte 0x66\r
78 lret # return to protected mode\r
79\r
80_EntryPoint: .long ASM_PFX(ToUserCode) - ASM_PFX(m16Start)\r
81 .word 0x8\r
82_16Idtr: .word 0x3ff\r
83 .long 0\r
84_16Gdtr: .word GdtEnd - _NullSegDesc - 1\r
85_16GdtrBase: .long _NullSegDesc\r
86\r
87ASM_PFX(ToUserCode):\r
88 movl %ss, %edx\r
89 movl %ecx, %ss # set new segment selectors\r
90 movl %ecx, %ds\r
91 movl %ecx, %es\r
92 movl %ecx, %fs\r
93 movl %ecx, %gs\r
94 movl %eax, %cr0\r
95 movl %ebp, %cr4 # real mode starts at next instruction\r
96 movl %esi, %ss # set up 16-bit stack segment\r
97 xchgw %bx, %sp # set up 16-bit stack pointer\r
98 .byte 0x66\r
99 call L_Base # push eip\r
100L_Base:\r
101 popw %bp # ebp <- offset L_Base\r
9e77c2c3 102 .byte 0x67; # address size override\r
103 push 54(%esp)\r
e1f414b6 104 lea 0xc(%esi), %eax\r
105 push %eax\r
106 lret\r
107\r
108L_RealMode:\r
109 mov %edx, %cs:0xffffffc5(%esi)\r
110 mov %bx, %cs:0xffffffcb(%esi)\r
111 lidtw %cs:0xffffffd7(%esi)\r
112 popaw # popad actually\r
113 pop %ds\r
114 pop %es\r
115 pop %fs\r
116 pop %gs\r
117 popfw # popfd\r
118 lretw # transfer control to user code\r
119\r
120_NullSegDesc: .quad 0\r
121_16CsDesc:\r
122 .word -1\r
123 .word 0\r
124 .byte 0\r
125 .byte 0x9b\r
126 .byte 0x8f # 16-bit segment, 4GB limit\r
127 .byte 0\r
128_16DsDesc:\r
129 .word -1\r
130 .word 0\r
131 .byte 0\r
132 .byte 0x93\r
133 .byte 0x8f # 16-bit segment, 4GB limit\r
134 .byte 0\r
135GdtEnd:\r
136\r
137#\r
2fc59a00 138# @param RegSet The pointer to a IA32_DWORD_REGS structure\r
139# @param Transition The pointer to the transition code\r
e1f414b6 140# @return The address of the 16-bit stack after returning from user code\r
141#\r
142ASM_PFX(InternalAsmThunk16):\r
143 push %ebp\r
144 push %ebx\r
145 push %esi\r
146 push %edi\r
147 push %ds\r
148 push %es\r
149 push %fs\r
150 push %gs\r
151 movl 36(%esp), %esi # esi <- RegSet\r
152 movzwl 0x32(%esi), %edx\r
153 mov 0xc(%esi), %edi\r
154 add $0xffffffc8, %edi\r
155 movl %edi, %ebx # ebx <- stack offset\r
156 imul $0x10, %edx, %eax\r
157 push $0xd\r
158 addl %eax, %edi # edi <- linear address of 16-bit stack\r
159 pop %ecx\r
160 rep\r
161 movsl # copy RegSet\r
162 movl 40(%esp), %eax # eax <- address of transition code\r
163 movl %edx, %esi # esi <- 16-bit stack segment\r
dccf78f0 164 lea 0x5f(%eax), %edx\r
e1f414b6 165 movl %eax, %ecx\r
166 andl $0xf, %ecx\r
167 shll $12, %eax\r
168 lea 0x6(%ecx), %ecx\r
169 movw %cx, %ax\r
170 stosl # [edi] <- return address of user code\r
dccf78f0 171 sgdtl 0xffffffa1(%edx)\r
e1f414b6 172 sidtl 0x24(%esp)\r
173 movl %cr0, %eax\r
174 movl %eax, (%edx) # save CR0 in SavedCr0\r
175 andl $0x7ffffffe, %eax # clear PE, PG bits\r
176 movl %cr4, %ebp\r
177 mov %ebp, 0xfffffff1(%edx)\r
178 andl $0x300, %ebp # clear all but PCE and OSFXSR bits\r
179 pushl $0x10\r
180 pop %ecx # ecx <- selector for data segments\r
181 lgdtl 0x20(%edx)\r
182 pushfl\r
183 lcall *0x14(%edx)\r
184 popfl\r
185 lidtl 0x24(%esp)\r
186 lea 0xffffffcc(%ebp), %eax\r
6f890d5b 187 pop %gs\r
188 pop %fs\r
189 pop %es\r
190 pop %ds\r
191 pop %edi\r
192 pop %esi\r
193 pop %ebx\r
194 pop %ebp\r
e1f414b6 195 ret\r
196\r
197 .const:\r
198\r
6a8e44d9 199ASM_PFX(m16Size): .word ASM_PFX(InternalAsmThunk16) - ASM_PFX(m16Start)\r
200ASM_PFX(mThunk16Attr): .word ASM_PFX(ThunkAttr) - ASM_PFX(m16Start)\r
e1f414b6 201ASM_PFX(m16Gdt): .word _NullSegDesc - ASM_PFX(m16Start)\r
202ASM_PFX(m16GdtrBase): .word _16GdtrBase - ASM_PFX(m16Start)\r
203ASM_PFX(mTransition): .word _EntryPoint - ASM_PFX(m16Start)\r