+\r
+#include "BaseLibInternals.h"\r
+\r
;------------------------------------------------------------------------------\r
;\r
-; Copyright (c) 2006, Intel Corporation\r
+; Copyright (c) 2006 - 2008, Intel Corporation\r
; All rights reserved. This program and the accompanying materials\r
; are licensed and made available under the terms and conditions of the BSD License\r
; which accompanies this distribution. The full text of the license may be found at\r
.686p\r
.model flat,C\r
\r
- .data\r
-\r
-NullSegSel DQ 0\r
-_16BitCsSel LABEL QWORD\r
- DW -1\r
- DW 0\r
- DB 0\r
- DB 9bh\r
- DB 8fh ; 16-bit segment\r
- DB 0\r
-_16BitDsSel LABEL QWORD\r
- DW -1\r
- DW 0\r
- DB 0\r
- DB 93h\r
- DB 8fh ; 16-bit segment\r
- DB 0\r
-GdtEnd LABEL QWORD\r
-\r
- .const\r
-\r
-_16Gdtr LABEL FWORD\r
- DW offset GdtEnd - offset NullSegSel - 1\r
- DD offset NullSegSel\r
-\r
-_16Idtr FWORD (1 SHL 10) - 1\r
-\r
- .code\r
+EXTERNDEF C m16Start:BYTE\r
+EXTERNDEF C m16Size:WORD\r
+EXTERNDEF C mThunk16Attr:WORD\r
+EXTERNDEF C m16Gdt:WORD\r
+EXTERNDEF C m16GdtrBase:WORD\r
+EXTERNDEF C mTransition:WORD\r
\r
+;\r
+; Here is the layout of the real mode stack. _ToUserCode() is responsible for\r
+; loading all these registers from real mode stack.\r
+;\r
IA32_REGS STRUC 4t\r
_EDI DD ?\r
_ESI DD ?\r
_SS DW ?\r
IA32_REGS ENDS\r
\r
-InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs\r
- mov esi, [esp + 36] ; esi <- RegSet\r
- push sizeof (IA32_REGS)\r
- pop ecx\r
- movzx edx, (IA32_REGS ptr [esi])._SS\r
- mov edi, (IA32_REGS ptr [esi])._ESP\r
- sub edi, ecx ; reserve space on realmode stack\r
- push edi ; save stack offset\r
- imul eax, edx, 16 ; eax <- edx * 16\r
- add edi, eax ; edi <- linear address of 16-bit stack\r
- rep movsb ; copy RegSet\r
- mov esi, edx ; esi <- 16-bit stack segment\r
- pop ebx ; ebx <- 16-bit stack offset\r
- mov edi, [esp + 40] ; edi <- realmode patch\r
- push cs ; save CS segment selector\r
- push offset @BackToThunk ; offset to back from real mode\r
- mov eax, offset @16Return\r
- stosd\r
- xor eax, eax\r
- stosw ; set CS base to 0\r
- mov eax, esp\r
- stosd\r
- mov eax, ss\r
- stosd\r
- mov eax, cr0\r
- mov ecx, eax ; ecx <- CR0\r
- and ecx, 7ffffffeh ; clear PE, PG bits\r
- stosd\r
- mov eax, cr4\r
- mov ebp, eax\r
- and ebp, 300h ; clear all but PCE and OSFXSR bits\r
- stosd\r
- sidt fword ptr [esp + 44] ; use parameter space to save IDTR\r
- sgdt fword ptr [edi]\r
- lidt _16Idtr\r
- push 10h\r
- pop eax\r
- push 8\r
- push offset @16Start\r
- lgdt _16Gdtr\r
- retf\r
-@16Start: ; 16-bit starts here\r
- mov ss, eax ; set SS to be a 16-bit segment\r
- mov cr0, ecx\r
- mov cr4, ebp\r
- mov ss, esi ; set up 16-bit stack\r
- mov sp, bx ; mov esp, ebx actually\r
- popaw ; popad actually\r
- pop ds\r
- pop es\r
- pop fs\r
- pop gs\r
- add sp, 4 ; skip _EFLAGS\r
+ .const\r
+\r
+;\r
+; These are global constant to convey information to C code.\r
+;\r
+m16Size DW InternalAsmThunk16 - m16Start\r
+mThunk16Attr DW _ThunkAttr - m16Start\r
+m16Gdt DW _NullSegDesc - m16Start\r
+m16GdtrBase DW _16GdtrBase - m16Start\r
+mTransition DW _EntryPoint - m16Start\r
+\r
+ .code\r
+\r
+m16Start LABEL BYTE\r
+\r
+SavedGdt LABEL FWORD\r
+ DW ?\r
+ DD ?\r
+;------------------------------------------------------------------------------\r
+; _BackFromUserCode() takes control in real mode after 'retf' has been executed\r
+; by user code. It will be shadowed to somewhere in memory below 1MB.\r
+;------------------------------------------------------------------------------\r
+_BackFromUserCode PROC\r
+ ;\r
+ ; The order of saved registers on the stack matches the order they appears\r
+ ; in IA32_REGS structure. This facilitates wrapper function to extract them\r
+ ; into that structure.\r
+ ;\r
+ push ss\r
+ push cs\r
DB 66h\r
- retf ; transfer control to 16-bit code\r
-@16Return:\r
+ call @Base ; push eip\r
+@Base:\r
pushf ; pushfd actually\r
+ cli ; disable interrupts\r
push gs\r
push fs\r
push es\r
push ds\r
pushaw ; pushad actually\r
- DB 67h, 66h\r
- lds esi, fword ptr (IA32_REGS ptr [esp])._EIP\r
- DB 67h, 66h\r
- mov eax, [esi + 12]\r
- mov cr4, eax ; restore CR4\r
- DB 67h, 66h\r
- lgdt fword ptr [esi + 16]\r
- DB 67h, 66h\r
- mov eax, [esi + 8]\r
- mov cr0, eax ; restore CR0\r
- xor ax, ax ; xor eax, eax actually\r
+ DB 66h, 0bah ; mov edx, imm32\r
+_ThunkAttr DD ?\r
+ test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15\r
+ jz @1\r
+ mov eax, 15cd2401h ; mov ax, 2401h & int 15h\r
+ cli ; disable interrupts\r
+ jnc @2\r
+@1:\r
+ test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL\r
+ jz @2\r
+ in al, 92h\r
+ or al, 2\r
+ out 92h, al ; deactivate A20M#\r
+@2:\r
mov eax, ss\r
DB 67h\r
- mov dword ptr (IA32_REGS ptr [esp])._SS, eax\r
- shl ax, 4 ; shl eax, 4 actually\r
- add ax, sp ; add eax, esp actually\r
- add sp, sizeof (IA32_REGS) ; add esp, sizeof (IA32_REGS)\r
- DB 67h, 66h\r
- mov dword ptr (IA32_REGS ptr [esp - sizeof (IA32_REGS)])._ESP, esp\r
- DB 67h, 66h\r
- lss esp, fword ptr [esi] ; restore protected mode stack\r
+ lea bp, [esp + sizeof (IA32_REGS)]\r
+ ;\r
+ ; esi's in the following 2 instructions are indeed bp in 16-bit code. Fact\r
+ ; is "esi" in 32-bit addressing mode has the same encoding of "bp" in 16-\r
+ ; bit addressing mode.\r
+ ;\r
+ mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp\r
+ mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP\r
+ shl ax, 4 ; shl eax, 4\r
+ add bp, ax ; add ebp, eax\r
+ DB 66h, 0b8h ; mov eax, imm32\r
+SavedCr4 DD ?\r
+ mov cr4, eax\r
+ DB 66h\r
+ lgdt fword ptr cs:[edi + (SavedGdt - @Base)]\r
+ DB 66h, 0b8h ; mov eax, imm32\r
+SavedCr0 DD ?\r
+ mov cr0, eax\r
+ DB 0b8h ; mov ax, imm16\r
+SavedSs DW ?\r
+ mov ss, eax\r
+ DB 66h, 0bch ; mov esp, imm32\r
+SavedEsp DD ?\r
+ DB 66h\r
+ retf ; return to protected mode\r
+_BackFromUserCode ENDP\r
+\r
+_EntryPoint DD _ToUserCode - m16Start\r
+ DW 8h\r
+_16Idtr FWORD (1 SHL 10) - 1\r
+_16Gdtr LABEL FWORD\r
+ DW GdtEnd - _NullSegDesc - 1\r
+_16GdtrBase DD _NullSegDesc\r
+\r
+;------------------------------------------------------------------------------\r
+; _ToUserCode() takes control in real mode before passing control to user code.\r
+; It will be shadowed to somewhere in memory below 1MB.\r
+;------------------------------------------------------------------------------\r
+_ToUserCode PROC\r
+ mov edx, ss\r
+ mov ss, ecx ; set new segment selectors\r
+ mov ds, ecx\r
+ mov es, ecx\r
+ mov fs, ecx\r
+ mov gs, ecx\r
+ mov cr0, eax\r
+ mov cr4, ebp ; real mode starts at next instruction\r
+ mov ss, esi ; set up 16-bit stack segment\r
+ xchg sp, bx ; set up 16-bit stack pointer\r
DB 66h\r
- retf ; go back to protected mode\r
-@BackToThunk:\r
+ call @Base ; push eip\r
+@Base:\r
+ pop bp ; ebp <- address of @Base\r
+ DB 67h ; address size override\r
+ push [esp + sizeof (IA32_REGS) + 2]\r
+ lea eax, [esi + (@RealMode - @Base)]\r
+ push eax\r
+ retf\r
+@RealMode:\r
+ mov cs:[esi + (SavedSs - @Base)], edx\r
+ mov cs:[esi + (SavedEsp - @Base)], bx\r
+ DB 66h\r
+ lidt fword ptr cs:[esi + (_16Idtr - @Base)]\r
+ popaw ; popad actually\r
+ pop ds\r
+ pop es\r
+ pop fs\r
+ pop gs\r
+ popf ; popfd\r
+ DB 66h ; Use 32-bit addressing for "retf" below\r
+ retf ; transfer control to user code\r
+_ToUserCode ENDP\r
+\r
+_NullSegDesc DQ 0\r
+_16CsDesc LABEL QWORD\r
+ DW -1\r
+ DW 0\r
+ DB 0\r
+ DB 9bh\r
+ DB 8fh ; 16-bit segment, 4GB limit\r
+ DB 0\r
+_16DsDesc LABEL QWORD\r
+ DW -1\r
+ DW 0\r
+ DB 0\r
+ DB 93h\r
+ DB 8fh ; 16-bit segment, 4GB limit\r
+ DB 0\r
+GdtEnd LABEL QWORD\r
+\r
+;------------------------------------------------------------------------------\r
+; IA32_REGISTER_SET *\r
+; EFIAPI\r
+; InternalAsmThunk16 (\r
+; IN IA32_REGISTER_SET *RegisterSet,\r
+; IN OUT VOID *Transition\r
+; );\r
+;------------------------------------------------------------------------------\r
+InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs\r
+ mov esi, [esp + 36] ; esi <- RegSet, the 1st parameter\r
+ movzx edx, (IA32_REGS ptr [esi])._SS\r
+ mov edi, (IA32_REGS ptr [esi])._ESP\r
+ add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space\r
+ mov ebx, edi ; ebx <- stack offset\r
+ imul eax, edx, 16 ; eax <- edx * 16\r
+ push sizeof (IA32_REGS) / 4\r
+ add edi, eax ; edi <- linear address of 16-bit stack\r
+ pop ecx\r
+ rep movsd ; copy RegSet\r
+ mov eax, [esp + 40] ; eax <- address of transition code\r
+ mov esi, edx ; esi <- 16-bit stack segment\r
+ lea edx, [eax + (SavedCr0 - m16Start)]\r
+ mov ecx, eax\r
+ and ecx, 0fh\r
+ shl eax, 12\r
+ lea ecx, [ecx + (_BackFromUserCode - m16Start)]\r
+ mov ax, cx\r
+ stosd ; [edi] <- return address of user code\r
+ sgdt fword ptr [edx + (SavedGdt - SavedCr0)]\r
+ sidt fword ptr [esp + 36] ; save IDT stack in argument space\r
+ mov eax, cr0\r
+ mov [edx], eax ; save CR0 in SavedCr0\r
+ and eax, 7ffffffeh ; clear PE, PG bits\r
+ mov ebp, cr4\r
+ mov [edx + (SavedCr4 - SavedCr0)], ebp\r
+ and ebp, 300h ; clear all but PCE and OSFXSR bits\r
+ push 10h\r
+ pop ecx ; ecx <- selector for data segments\r
+ lgdt fword ptr [edx + (_16Gdtr - SavedCr0)]\r
+ pushfd ; Save df/if indeed\r
+ call fword ptr [edx + (_EntryPoint - SavedCr0)]\r
+ popfd\r
lidt fword ptr [esp + 36] ; restore protected mode IDTR\r
+ lea eax, [ebp - sizeof (IA32_REGS)] ; eax <- the address of IA32_REGS\r
ret\r
InternalAsmThunk16 ENDP\r
\r