]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Ia32/LongJump.c
MdePkg/BaseLib: Add Shadow Stack Support for X86.
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / LongJump.c
1 /** @file
2 Implementation of _LongJump() on IA-32.
3
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "BaseLibInternals.h"
17
18
19 /**
20 Restores the CPU context that was saved with SetJump().
21
22 Restores the CPU context from the buffer specified by JumpBuffer.
23 This function never returns to the caller.
24 Instead is resumes execution based on the state of JumpBuffer.
25
26 @param JumpBuffer A pointer to CPU context buffer.
27 @param Value The value to return when the SetJump() context is restored.
28
29 **/
30 __declspec (naked)
31 VOID
32 EFIAPI
33 InternalLongJump (
34 IN BASE_LIBRARY_JUMP_BUFFER *JumpBuffer,
35 IN UINTN Value
36 )
37 {
38 _asm {
39 mov eax, [PcdGet32 (PcdControlFlowEnforcementPropertyMask)]
40 test eax, eax
41 jz CetDone
42 _emit 0x0F
43 _emit 0x20
44 _emit 0xE0 ; mov eax, cr4
45 bt eax, 23 ; check if CET is enabled
46 jnc CetDone
47
48 mov edx, [esp + 4] ; edx = JumpBuffer
49 mov edx, [edx + 24] ; edx = target SSP
50 _emit 0xF3
51 _emit 0x0F
52 _emit 0x1E
53 _emit 0xC8 ; READSSP EAX
54 sub edx, eax ; edx = delta
55 mov eax, edx ; eax = delta
56
57 shr eax, 2 ; eax = delta/sizeof(UINT32)
58 _emit 0xF3
59 _emit 0x0F
60 _emit 0xAE
61 _emit 0xE8 ; INCSSP EAX
62
63 CetDone:
64
65 pop eax ; skip return address
66 pop edx ; edx <- JumpBuffer
67 pop eax ; eax <- Value
68 mov ebx, [edx]
69 mov esi, [edx + 4]
70 mov edi, [edx + 8]
71 mov ebp, [edx + 12]
72 mov esp, [edx + 16]
73 jmp dword ptr [edx + 20]
74 }
75 }
76