]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Library/BaseMemEncryptSevLib/X64/SecSnpSystemRamValidate.c
OvmfPkg/MemEncryptSevLib: add function to check the VMPL0
[mirror_edk2.git] / OvmfPkg / Library / BaseMemEncryptSevLib / X64 / SecSnpSystemRamValidate.c
1 /** @file
2
3 SEV-SNP Page Validation functions.
4
5 Copyright (c) 2021 AMD Incorporated. All rights reserved.<BR>
6
7 SPDX-License-Identifier: BSD-2-Clause-Patent
8
9 **/
10
11 #include <Uefi/UefiBaseType.h>
12 #include <Library/BaseLib.h>
13 #include <Library/MemEncryptSevLib.h>
14
15 #include "SnpPageStateChange.h"
16
17 //
18 // The variable used for the VMPL check.
19 //
20 STATIC UINT8 gVmpl0Data[4096];
21
22 /**
23 The function checks whether SEV-SNP guest is booted under VMPL0.
24
25 @retval TRUE The guest is booted under VMPL0
26 @retval FALSE The guest is not booted under VMPL0
27 **/
28 STATIC
29 BOOLEAN
30 SevSnpIsVmpl0 (
31 VOID
32 )
33 {
34 UINT64 Rdx;
35 EFI_STATUS Status;
36
37 //
38 // There is no straightforward way to query the current VMPL level.
39 // The simplest method is to use the RMPADJUST instruction to change
40 // a page permission to a VMPL level-1, and if the guest kernel is
41 // launched at a level <= 1, then RMPADJUST instruction will return
42 // an error.
43 //
44 Rdx = 1;
45
46 Status = AsmRmpAdjust ((UINT64)gVmpl0Data, 0, Rdx);
47 if (EFI_ERROR (Status)) {
48 return FALSE;
49 }
50
51 return TRUE;
52 }
53
54 /**
55 Pre-validate the system RAM when SEV-SNP is enabled in the guest VM.
56
57 @param[in] BaseAddress Base address
58 @param[in] NumPages Number of pages starting from the base address
59
60 **/
61 VOID
62 EFIAPI
63 MemEncryptSevSnpPreValidateSystemRam (
64 IN PHYSICAL_ADDRESS BaseAddress,
65 IN UINTN NumPages
66 )
67 {
68 if (!MemEncryptSevSnpIsEnabled ()) {
69 return;
70 }
71
72 //
73 // The page state change uses the PVALIDATE instruction. The instruction
74 // can be run on VMPL-0 only. If its not VMPL-0 guest then terminate
75 // the boot.
76 //
77 if (!SevSnpIsVmpl0 ()) {
78 SnpPageStateFailureTerminate ();
79 }
80
81 InternalSetPageState (BaseAddress, NumPages, SevSnpPagePrivate, TRUE);
82 }