Skip to content

Commit

Permalink
Ring3: Refactored exception handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Krichanov committed Mar 8, 2024
1 parent 0d1ff98 commit 96d43d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
7 changes: 6 additions & 1 deletion MdeModulePkg/Core/Dxe/Image/Image.c
Original file line number Diff line number Diff line change
Expand Up @@ -1691,13 +1691,18 @@ InitializeRing3 (
DEBUG ((DEBUG_ERROR, "Core: gRing3CallStackTop = %p\n", gRing3CallStackTop));

//
// Initialize MSR_IA32_STAR and MSR_IA32_LSTAR for SYSCALL and SYSRET.
// Initialize MSR_IA32_STAR, MSR_IA32_LSTAR and MSR_IA32_FMASK for SYSCALL and SYSRET.
//
Msr = (((((UINT64)RING3_CODE64_SEL - 16) | 3) << 16) | (UINT64)RING0_CODE64_SEL) << 32;
AsmWriteMsr64 (MSR_IA32_STAR, Msr);

Msr = (UINT64)(UINTN)CoreBootServices;
AsmWriteMsr64 (MSR_IA32_LSTAR, Msr);
//
// Disable maskable interrupts at SYSCALL.
//
Msr = (UINT64)BIT9;
AsmWriteMsr64 (MSR_IA32_FMASK, Msr);

return Status;
}
Expand Down
12 changes: 3 additions & 9 deletions MdeModulePkg/Core/Dxe/SysCall/X64/CoreBootServices.nasm
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ copy:
;------------------------------------------------------------------------------
global ASM_PFX(CoreBootServices)
ASM_PFX(CoreBootServices):
cli
; Switch from User to Core data segment selectors.
mov ax, ss
mov ds, ax
Expand Down Expand Up @@ -160,10 +159,8 @@ ASM_PFX(CoreBootServices):
mov r8, [rbp + 8*6]

sti

call ASM_PFX(CallBootService)
push rax

cli

SetRing3DataSegmentSelectors
Expand All @@ -181,8 +178,6 @@ ASM_PFX(CoreBootServices):
pop rbp
pop rsp

sti

; SYSCALL saves RFLAGS into R11 and the RIP of the next instruction into RCX.
o64 sysret
; SYSRET copies the value in RCX into RIP and loads RFLAGS from R11.
Expand All @@ -198,7 +193,10 @@ o64 sysret
;------------------------------------------------------------------------------
global ASM_PFX(CallRing3)
ASM_PFX(CallRing3):
pushfq
pop r11
cli

; Save input Arguments.
push rcx

Expand All @@ -207,17 +205,13 @@ ASM_PFX(CallRing3):
; Prepare SYSRET arguments.
mov rcx, [gRing3EntryPoint]
pop rdx
pushfq
pop r11

; Save Core Stack pointers and switch to User Stack.
mov [ASM_PFX(CoreRsp)], rsp
mov [ASM_PFX(CoreRbp)], rbp
mov rsp, [ASM_PFX(gRing3CallStackTop)]
mov rbp, rsp

sti

; Pass control to user image
o64 sysret

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ HasErrorCode:
mov rax, gs
push rax

; Check whether Ring3 process was interrupted.
and rax, 3
cmp rax, 3
jne SkipHook
mov rax, cr2
cmp rax, 0xFFFFFFFFFFFFFFF8
jne SkipHook
mov rcx, 32
SkipHook:
mov rax, ss
mov ds, rax
mov es, rax
Expand Down Expand Up @@ -433,14 +442,12 @@ CetDone:
pop r15

; Check whether Ring3 process was interrupted.
push rax
mov rax, ss
push rcx
mov rcx, ds
cmp rax, rcx
jne ReturnToRing3
and rcx, 3
cmp rcx, 3
pop rcx
pop rax
je ReturnToRing3

mov rsp, rbp
pop rbp
Expand Down Expand Up @@ -469,8 +476,6 @@ DoReturn:
DoIret:
iretq
ReturnToRing3:
pop rcx
pop rax
mov rsp, rbp
pop rbp
add rsp, 16
Expand Down

0 comments on commit 96d43d9

Please sign in to comment.