]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/ia32/Thunk16.asm
Initial import.
[mirror_edk2.git] / MdePkg / Library / BaseLib / ia32 / Thunk16.asm
CommitLineData
878ddf1f 1;------------------------------------------------------------------------------\r
2;\r
3; Copyright (c) 2006, Intel Corporation\r
4; All rights reserved. This program and the accompanying materials\r
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
7; http://opensource.org/licenses/bsd-license.php\r
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; Thunk.asm\r
15;\r
16; Abstract:\r
17;\r
18; Real mode thunk\r
19;\r
20;------------------------------------------------------------------------------\r
21\r
22 .686p\r
23 .model flat,C\r
24\r
25 .data\r
26\r
27NullSegSel DQ 0\r
28_16BitCsSel LABEL QWORD\r
29 DW -1\r
30 DW 0\r
31 DB 0\r
32 DB 9bh\r
33 DB 8fh ; 16-bit segment\r
34 DB 0\r
35_16BitDsSel LABEL QWORD\r
36 DW -1\r
37 DW 0\r
38 DB 0\r
39 DB 93h\r
40 DB 8fh ; 16-bit segment\r
41 DB 0\r
42GdtEnd LABEL QWORD\r
43\r
44 .const\r
45\r
46_16Gdtr LABEL FWORD\r
47 DW offset GdtEnd - offset NullSegSel - 1\r
48 DD offset NullSegSel\r
49\r
50_16Idtr FWORD (1 SHL 10) - 1\r
51\r
52 .code\r
53\r
54IA32_REGS STRUC 4t\r
55_EDI DD ?\r
56_ESI DD ?\r
57_EBP DD ?\r
58_ESP DD ?\r
59_EBX DD ?\r
60_EDX DD ?\r
61_ECX DD ?\r
62_EAX DD ?\r
63_DS DW ?\r
64_ES DW ?\r
65_FS DW ?\r
66_GS DW ?\r
67_EFLAGS DD ?\r
68_EIP DD ?\r
69_CS DW ?\r
70_SS DW ?\r
71IA32_REGS ENDS\r
72\r
73InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs\r
74 mov esi, [esp + 36] ; esi <- RegSet\r
75 push sizeof (IA32_REGS)\r
76 pop ecx\r
77 movzx edx, (IA32_REGS ptr [esi])._SS\r
78 mov edi, (IA32_REGS ptr [esi])._ESP\r
79 sub edi, ecx ; reserve space on realmode stack\r
80 push edi ; save stack offset\r
81 imul eax, edx, 16 ; eax <- edx * 16\r
82 add edi, eax ; edi <- linear address of 16-bit stack\r
83 rep movsb ; copy RegSet\r
84 mov esi, edx ; esi <- 16-bit stack segment\r
85 pop ebx ; ebx <- 16-bit stack offset\r
86 mov edi, [esp + 40] ; edi <- realmode patch\r
87 push cs ; save CS segment selector\r
88 push offset @BackToThunk ; offset to back from real mode\r
89 mov eax, offset @16Return\r
90 stosd\r
91 xor eax, eax\r
92 stosw ; set CS base to 0\r
93 mov eax, esp\r
94 stosd\r
95 mov eax, ss\r
96 stosd\r
97 mov eax, cr0\r
98 mov ecx, eax ; ecx <- CR0\r
99 and ecx, 7ffffffeh ; clear PE, PG bits\r
100 stosd\r
101 mov eax, cr4\r
102 mov ebp, eax\r
103 and ebp, 300h ; clear all but PCE and OSFXSR bits\r
104 stosd\r
105 sidt fword ptr [esp + 44] ; use parameter space to save IDTR\r
106 sgdt fword ptr [edi]\r
107 lidt _16Idtr\r
108 push 10h\r
109 pop eax\r
110 push 8\r
111 push offset @16Start\r
112 lgdt _16Gdtr\r
113 retf\r
114@16Start: ; 16-bit starts here\r
115 mov ss, eax ; set SS to be a 16-bit segment\r
116 mov cr0, ecx\r
117 mov cr4, ebp\r
118 mov ss, esi ; set up 16-bit stack\r
119 mov sp, bx ; mov esp, ebx actually\r
120 popaw ; popad actually\r
121 pop ds\r
122 pop es\r
123 pop fs\r
124 pop gs\r
125 add sp, 4 ; skip _EFLAGS\r
126 DB 66h\r
127 retf ; transfer control to 16-bit code\r
128@16Return:\r
129 pushf ; pushfd actually\r
130 push gs\r
131 push fs\r
132 push es\r
133 push ds\r
134 pushaw ; pushad actually\r
135 DB 67h, 66h\r
136 lds esi, fword ptr (IA32_REGS ptr [esp])._EIP\r
137 DB 67h, 66h\r
138 mov eax, [esi + 12]\r
139 mov cr4, eax ; restore CR4\r
140 DB 67h, 66h\r
141 lgdt fword ptr [esi + 16]\r
142 DB 67h, 66h\r
143 mov eax, [esi + 8]\r
144 mov cr0, eax ; restore CR0\r
145 xor ax, ax ; xor eax, eax actually\r
146 mov eax, ss\r
147 DB 67h\r
148 mov dword ptr (IA32_REGS ptr [esp])._SS, eax\r
149 shl ax, 4 ; shl eax, 4 actually\r
150 add ax, sp ; add eax, esp actually\r
151 add sp, sizeof (IA32_REGS) ; add esp, sizeof (IA32_REGS)\r
152 DB 67h, 66h\r
153 mov dword ptr (IA32_REGS ptr [esp - sizeof (IA32_REGS)])._ESP, esp\r
154 DB 67h, 66h\r
155 lss esp, fword ptr [esi] ; restore protected mode stack\r
156 DB 66h\r
157 retf ; go back to protected mode\r
158@BackToThunk:\r
159 lidt fword ptr [esp + 36] ; restore protected mode IDTR\r
160 ret\r
161InternalAsmThunk16 ENDP\r
162\r
163 END\r