Updated BaseLib for THUNK functions and some CPU functions

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@158 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
bxing
2006-05-15 11:14:20 +00:00
parent 7c28d0e095
commit 97d92bdaf0
9 changed files with 664 additions and 256 deletions

View File

@@ -22,34 +22,16 @@
.686p
.model flat,C
.data
EXTERNDEF C m16Start:BYTE
EXTERNDEF C m16Size:WORD
EXTERNDEF C mThunk16Attr:WORD
EXTERNDEF C m16Gdt:WORD
EXTERNDEF C m16GdtrBase:WORD
EXTERNDEF C mTransition:WORD
NullSegSel DQ 0
_16BitCsSel LABEL QWORD
DW -1
DW 0
DB 0
DB 9bh
DB 8fh ; 16-bit segment
DB 0
_16BitDsSel LABEL QWORD
DW -1
DW 0
DB 0
DB 93h
DB 8fh ; 16-bit segment
DB 0
GdtEnd LABEL QWORD
.const
_16Gdtr LABEL FWORD
DW offset GdtEnd - offset NullSegSel - 1
DD offset NullSegSel
_16Idtr FWORD (1 SHL 10) - 1
.code
THUNK_ATTRIBUTE_BIG_REAL_MODE EQU 1
THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15 EQU 2
THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL EQU 4
IA32_REGS STRUC 4t
_EDI DD ?
@@ -70,93 +52,165 @@ _CS DW ?
_SS DW ?
IA32_REGS ENDS
InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs
mov esi, [esp + 36] ; esi <- RegSet
push sizeof (IA32_REGS)
pop ecx
movzx edx, (IA32_REGS ptr [esi])._SS
mov edi, (IA32_REGS ptr [esi])._ESP
sub edi, ecx ; reserve space on realmode stack
push edi ; save stack offset
imul eax, edx, 16 ; eax <- edx * 16
add edi, eax ; edi <- linear address of 16-bit stack
rep movsb ; copy RegSet
mov esi, edx ; esi <- 16-bit stack segment
pop ebx ; ebx <- 16-bit stack offset
mov edi, [esp + 40] ; edi <- realmode patch
push cs ; save CS segment selector
push offset @BackToThunk ; offset to back from real mode
mov eax, offset @16Return
stosd
xor eax, eax
stosw ; set CS base to 0
mov eax, esp
stosd
mov eax, ss
stosd
mov eax, cr0
mov ecx, eax ; ecx <- CR0
and ecx, 7ffffffeh ; clear PE, PG bits
stosd
mov eax, cr4
mov ebp, eax
and ebp, 300h ; clear all but PCE and OSFXSR bits
stosd
sidt fword ptr [esp + 44] ; use parameter space to save IDTR
sgdt fword ptr [edi]
lidt _16Idtr
push 10h
pop eax
push 8
push offset @16Start
lgdt _16Gdtr
retf
@16Start: ; 16-bit starts here
mov ss, eax ; set SS to be a 16-bit segment
mov cr0, ecx
mov cr4, ebp
mov ss, esi ; set up 16-bit stack
mov sp, bx ; mov esp, ebx actually
popaw ; popad actually
pop ds
pop es
pop fs
pop gs
add sp, 4 ; skip _EFLAGS
.const
m16Size DW offset InternalAsmThunk16 - offset m16Start
mThunk16Attr DW offset _ThunkAttr - offset m16Start
m16Gdt DW offset _NullSegDesc - offset m16Start
m16GdtrBase DW offset _16GdtrBase - offset m16Start
mTransition DW offset _EntryPoint - offset m16Start
.code
m16Start LABEL BYTE
SavedGdt LABEL FWORD
DW ?
DD ?
_BackFromUserCode PROC
push ss
push cs
DB 66h
retf ; transfer control to 16-bit code
@16Return:
call @Base ; push eip
@Base:
pushf ; pushfd actually
cli ; disable interrupts
push gs
push fs
push es
push ds
pushaw ; pushad actually
DB 67h, 66h
lds esi, fword ptr (IA32_REGS ptr [esp])._EIP
DB 67h, 66h
mov eax, [esi + 12]
mov cr4, eax ; restore CR4
DB 67h, 66h
lgdt fword ptr [esi + 16]
DB 67h, 66h
mov eax, [esi + 8]
mov cr0, eax ; restore CR0
xor ax, ax ; xor eax, eax actually
DB 66h, 0bah ; mov edx, imm32
_ThunkAttr DD ?
test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_INT_15
jz @1
mov eax, 15cd2401h ; mov ax, 2401h & int 15h
cli ; disable interrupts
jnc @2
@1:
test dl, THUNK_ATTRIBUTE_DISABLE_A20_MASK_KBD_CTRL
jz @2
in al, 92h
or al, 2
out 92h, al ; deactivate A20M#
@2:
mov eax, ss
DB 67h
mov dword ptr (IA32_REGS ptr [esp])._SS, eax
shl ax, 4 ; shl eax, 4 actually
add ax, sp ; add eax, esp actually
add sp, sizeof (IA32_REGS) ; add esp, sizeof (IA32_REGS)
DB 67h, 66h
mov dword ptr (IA32_REGS ptr [esp - sizeof (IA32_REGS)])._ESP, esp
DB 67h, 66h
lss esp, fword ptr [esi] ; restore protected mode stack
lea bp, [esp + sizeof (IA32_REGS)]
mov word ptr (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._ESP, bp
mov ebx, (IA32_REGS ptr [esi - sizeof (IA32_REGS)])._EIP
shl ax, 4 ; shl eax, 4
add bp, ax ; add ebp, eax
DB 66h, 0b8h ; mov eax, imm32
SavedCr4 DD ?
mov cr4, eax
DB 66h
retf ; go back to protected mode
@BackToThunk:
lgdt fword ptr cs:[edi + (offset SavedGdt - offset @Base)]
DB 66h, 0b8h ; mov eax, imm32
SavedCr0 DD ?
mov cr0, eax
DB 0b8h ; mov ax, imm16
SavedSs DW ?
mov ss, eax
DB 66h, 0bch ; mov esp, imm32
SavedEsp DD ?
DB 66h
retf ; return to protected mode
_BackFromUserCode ENDP
_EntryPoint DD offset _ToUserCode - offset m16Start
DW 8h
_16Idtr FWORD (1 SHL 10) - 1
_16Gdtr LABEL FWORD
DW offset GdtEnd - offset _NullSegDesc - 1
_16GdtrBase DD offset _NullSegDesc
_ToUserCode PROC
mov edx, ss
mov ss, ecx ; set new segment selectors
mov ds, ecx
mov es, ecx
mov fs, ecx
mov gs, ecx
mov cr0, eax
mov cr4, ebp ; real mode starts at next instruction
mov ss, esi ; set up 16-bit stack segment
xchg sp, bx ; set up 16-bit stack pointer
DB 66h
call @Base ; push eip
@Base:
pop bp ; ebp <- offset @Base
mov cs:[esi + (offset SavedSs - offset @Base)], edx
mov cs:[esi + (offset SavedEsp - offset @Base)], bx
DB 66h
lidt fword ptr cs:[esi + (offset _16Idtr - offset @Base)]
popaw ; popad actually
pop ds
pop es
pop fs
pop gs
popf ; popfd
DB 66h
retf ; transfer control to user code
_ToUserCode ENDP
_NullSegDesc DQ 0
_16CsDesc LABEL QWORD
DW -1
DW 0
DB 0
DB 9bh
DB 8fh ; 16-bit segment, 4GB limit
DB 0
_16DsDesc LABEL QWORD
DW -1
DW 0
DB 0
DB 93h
DB 8fh ; 16-bit segment, 4GB limit
DB 0
GdtEnd LABEL QWORD
;
; @param RegSet Pointer to a IA32_DWORD_REGS structure
; @param Transition Pointer to the transition code
; @return The address of the 16-bit stack after returning from user code
;
InternalAsmThunk16 PROC USES ebp ebx esi edi ds es fs gs
mov esi, [esp + 36] ; esi <- RegSet
movzx edx, (IA32_REGS ptr [esi])._SS
mov edi, (IA32_REGS ptr [esi])._ESP
add edi, - (sizeof (IA32_REGS) + 4) ; reserve stack space
mov ebx, edi ; ebx <- stack offset
imul eax, edx, 16 ; eax <- edx * 16
push sizeof (IA32_REGS) / 4
add edi, eax ; edi <- linear address of 16-bit stack
pop ecx
rep movsd ; copy RegSet
mov eax, [esp + 40] ; eax <- address of transition code
mov esi, edx ; esi <- 16-bit stack segment
lea edx, [eax + (offset SavedCr0 - offset m16Start)]
mov ecx, eax
and ecx, 0fh
shl eax, 12
lea ecx, [ecx + (offset _BackFromUserCode - offset m16Start)]
mov ax, cx
stosd ; [edi] <- return address of user code
sgdt fword ptr [edx + (offset SavedGdt - offset SavedCr0)]
sidt fword ptr [esp + 36] ; save IDT stack in argument space
mov eax, cr0
mov [edx], eax ; save CR0 in SavedCr0
and eax, 7ffffffeh ; clear PE, PG bits
mov ebp, cr4
mov [edx + (offset SavedCr4 - offset SavedCr0)], ebp
and ebp, 300h ; clear all but PCE and OSFXSR bits
push 10h
pop ecx ; ecx <- selector for data segments
lgdt fword ptr [edx + (offset _16Gdtr - offset SavedCr0)]
call fword ptr [edx + (offset _EntryPoint - offset SavedCr0)]
lidt fword ptr [esp + 36] ; restore protected mode IDTR
lea eax, [ebp - sizeof (IA32_REGS)]
ret
InternalAsmThunk16 ENDP