]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/Ia32/SetJump.c
4a284904183c2cb043a67d565d3e51d8b9b86c97
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / SetJump.c
1 /** @file
2 Implementation of SetJump() on IA-32.
3
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9
10 #include "BaseLibInternals.h"
11
12 /**
13 Worker function that checks ASSERT condition for JumpBuffer
14
15 Checks ASSERT condition for JumpBuffer.
16
17 If JumpBuffer is NULL, then ASSERT().
18 For IPF CPUs, if JumpBuffer is not aligned on a 16-byte boundary, then ASSERT().
19
20 @param JumpBuffer A pointer to CPU context buffer.
21
22 **/
23 VOID
24 EFIAPI
25 InternalAssertJumpBuffer (
26 IN BASE_LIBRARY_JUMP_BUFFER *JumpBuffer
27 );
28
29 /**
30 Saves the current CPU context that can be restored with a call to LongJump()
31 and returns 0.
32
33 Saves the current CPU context in the buffer specified by JumpBuffer and
34 returns 0. The initial call to SetJump() must always return 0. Subsequent
35 calls to LongJump() cause a non-zero value to be returned by SetJump().
36
37 If JumpBuffer is NULL, then ASSERT().
38 For IPF CPUs, if JumpBuffer is not aligned on a 16-byte boundary, then ASSERT().
39
40 @param JumpBuffer A pointer to CPU context buffer.
41
42 @retval 0 Indicates a return from SetJump().
43
44 **/
45 _declspec (naked)
46 RETURNS_TWICE
47 UINTN
48 EFIAPI
49 SetJump (
50 OUT BASE_LIBRARY_JUMP_BUFFER *JumpBuffer
51 )
52 {
53 _asm {
54 push [esp + 4]
55 call InternalAssertJumpBuffer
56 pop ecx
57 pop ecx
58 mov edx, [esp]
59
60 xor eax, eax
61 mov [edx + 24], eax ; save 0 to SSP
62
63 mov eax, [PcdGet32 (PcdControlFlowEnforcementPropertyMask)]
64 test eax, eax
65 jz CetDone
66 _emit 0x0F
67 _emit 0x20
68 _emit 0xE0 ; mov eax, cr4
69 bt eax, 23 ; check if CET is enabled
70 jnc CetDone
71
72 mov eax, 1
73 _emit 0xF3
74 _emit 0x0F
75 _emit 0xAE
76 _emit 0xE8 ; INCSSP EAX to read original SSP
77 _emit 0xF3
78 _emit 0x0F
79 _emit 0x1E
80 _emit 0xC8 ; READSSP EAX
81 mov [edx + 0x24], eax ; save SSP
82
83 CetDone:
84
85 mov [edx], ebx
86 mov [edx + 4], esi
87 mov [edx + 8], edi
88 mov [edx + 12], ebp
89 mov [edx + 16], esp
90 mov [edx + 20], ecx
91 xor eax, eax
92 jmp ecx
93 }
94 }
95