UefiCpuPkg/CpuExceptionHandlerLib: Make runtime fixups XCODE-only

The CPU exception handler library code was rewritten at some point to
populate the vector code templates with absolute references at runtime,
given that the XCODE linker does not permit absolute references in
executable code when creating PIE executables.

This is rather unfortunate, as this prevents us from using strict
permissions on the memory mappings, given that the .text section needs
to be writable at runtime for this arrangement to work.

So let's make this hack XCODE-only, by setting a preprocessor #define
from the command line when using the XCODE toolchain, and only including
the runtime fixup code when the macro is defined.

While at it, rename the Xcode5ExceptionHandlerAsm.nasm source file and
drop the Xcode5 prefix: this code is used by other toolchains too.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Ray Ni <ray.ni@intel.com>
This commit is contained in:
Ard Biesheuvel 2023-04-03 22:29:18 +08:00 committed by mergify[bot]
parent 4764aa50ef
commit e5fcaeb133
3 changed files with 17 additions and 2 deletions

View File

@ -28,7 +28,7 @@
Ia32/ArchInterruptDefs.h Ia32/ArchInterruptDefs.h
[Sources.X64] [Sources.X64]
X64/Xcode5ExceptionHandlerAsm.nasm X64/ExceptionHandlerAsm.nasm
X64/ArchExceptionHandler.c X64/ArchExceptionHandler.c
X64/ArchInterruptDefs.h X64/ArchInterruptDefs.h
@ -61,3 +61,6 @@
MemoryAllocationLib MemoryAllocationLib
DebugLib DebugLib
CcExitLib CcExitLib
[BuildOptions]
XCODE:*_*_X64_NASM_FLAGS = -D NO_ABSOLUTE_RELOCS_IN_TEXT

View File

@ -28,7 +28,7 @@
Ia32/ArchInterruptDefs.h Ia32/ArchInterruptDefs.h
[Sources.X64] [Sources.X64]
X64/Xcode5ExceptionHandlerAsm.nasm X64/ExceptionHandlerAsm.nasm
X64/ArchExceptionHandler.c X64/ArchExceptionHandler.c
X64/ArchInterruptDefs.h X64/ArchInterruptDefs.h
@ -61,3 +61,5 @@
[FeaturePcd] [FeaturePcd]
gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmStackGuard ## CONSUMES gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmStackGuard ## CONSUMES
[BuildOptions]
XCODE:*_*_X64_NASM_FLAGS = -D NO_ABSOLUTE_RELOCS_IN_TEXT

View File

@ -59,7 +59,11 @@ AsmIdtVectorBegin:
%rep 256 %rep 256
push strict dword %[Vector] ; This instruction pushes sign-extended 8-byte value on stack push strict dword %[Vector] ; This instruction pushes sign-extended 8-byte value on stack
push rax push rax
%ifdef NO_ABSOLUTE_RELOCS_IN_TEXT
mov rax, strict qword 0 ; mov rax, ASM_PFX(CommonInterruptEntry) mov rax, strict qword 0 ; mov rax, ASM_PFX(CommonInterruptEntry)
%else
mov rax, ASM_PFX(CommonInterruptEntry)
%endif
jmp rax jmp rax
%assign Vector Vector+1 %assign Vector Vector+1
%endrep %endrep
@ -69,8 +73,12 @@ HookAfterStubHeaderBegin:
push strict dword 0 ; 0 will be fixed push strict dword 0 ; 0 will be fixed
VectorNum: VectorNum:
push rax push rax
%ifdef NO_ABSOLUTE_RELOCS_IN_TEXT
mov rax, strict qword 0 ; mov rax, HookAfterStubHeaderEnd mov rax, strict qword 0 ; mov rax, HookAfterStubHeaderEnd
JmpAbsoluteAddress: JmpAbsoluteAddress:
%else
mov rax, HookAfterStubHeaderEnd
%endif
jmp rax jmp rax
HookAfterStubHeaderEnd: HookAfterStubHeaderEnd:
mov rax, rsp mov rax, rsp
@ -457,6 +465,7 @@ ASM_PFX(AsmGetTemplateAddressMap):
lea rax, [HookAfterStubHeaderBegin] lea rax, [HookAfterStubHeaderBegin]
mov qword [rcx + 0x10], rax mov qword [rcx + 0x10], rax
%ifdef NO_ABSOLUTE_RELOCS_IN_TEXT
; Fix up CommonInterruptEntry address ; Fix up CommonInterruptEntry address
lea rax, [ASM_PFX(CommonInterruptEntry)] lea rax, [ASM_PFX(CommonInterruptEntry)]
lea rcx, [AsmIdtVectorBegin] lea rcx, [AsmIdtVectorBegin]
@ -468,6 +477,7 @@ ASM_PFX(AsmGetTemplateAddressMap):
lea rax, [HookAfterStubHeaderEnd] lea rax, [HookAfterStubHeaderEnd]
lea rcx, [JmpAbsoluteAddress] lea rcx, [JmpAbsoluteAddress]
mov qword [rcx - 8], rax mov qword [rcx - 8], rax
%endif
ret ret