summaryrefslogtreecommitdiffstats
path: root/OvmfPkg/Library/BaseMemEncryptSevLib/X64/VirtualMemory.c
blob: 36aabcf556a7e935eec6b3e60ccdecb659c72607 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/** @file

  Virtual Memory Management Services to test an address range encryption state

  Copyright (c) 2020, AMD Incorporated. All rights reserved.<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/

#include <Library/CpuLib.h>
#include <Library/MemEncryptSevLib.h>

#include "VirtualMemory.h"

/**
  Returns the (updated) address range state based upon the page table
  entry.

  @param[in]  CurrentState            The current address range state
  @param[in]  PageDirectoryEntry      The page table entry to check
  @param[in]  AddressEncMask          The encryption mask

  @retval MemEncryptSevAddressRangeUnencrypted  Address range is mapped
                                                unencrypted
  @retval MemEncryptSevAddressRangeEncrypted    Address range is mapped
                                                encrypted
  @retval MemEncryptSevAddressRangeMixed        Address range is mapped mixed
**/
STATIC
MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE
UpdateAddressState (
  IN MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE  CurrentState,
  IN UINT64                               PageDirectoryEntry,
  IN UINT64                               AddressEncMask
  )
{
  if (CurrentState == MemEncryptSevAddressRangeEncrypted) {
    if ((PageDirectoryEntry & AddressEncMask) == 0) {
      CurrentState = MemEncryptSevAddressRangeMixed;
    }
  } else if (CurrentState == MemEncryptSevAddressRangeUnencrypted) {
    if ((PageDirectoryEntry & AddressEncMask) != 0) {
      CurrentState = MemEncryptSevAddressRangeMixed;
    }
  } else if (CurrentState == MemEncryptSevAddressRangeError) {
    //
    // First address check, set initial state
    //
    if ((PageDirectoryEntry & AddressEncMask) == 0) {
      CurrentState = MemEncryptSevAddressRangeUnencrypted;
    } else {
      CurrentState = MemEncryptSevAddressRangeEncrypted;
    }
  }

  return CurrentState;
}

/**
  Returns the encryption state of the specified virtual address range.

  @param[in]  Cr3BaseAddress          Cr3 Base Address (if zero then use
                                      current CR3)
  @param[in]  BaseAddress             Base address to check
  @param[in]  Length                  Length of virtual address range

  @retval MemEncryptSevAddressRangeUnencrypted  Address range is mapped
                                                unencrypted
  @retval MemEncryptSevAddressRangeEncrypted    Address range is mapped
                                                encrypted
  @retval MemEncryptSevAddressRangeMixed        Address range is mapped mixed
  @retval MemEncryptSevAddressRangeError        Address range is not mapped
**/
MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE
EFIAPI
InternalMemEncryptSevGetAddressRangeState (
  IN PHYSICAL_ADDRESS  Cr3BaseAddress,
  IN PHYSICAL_ADDRESS  BaseAddress,
  IN UINTN             Length
  )
{
  PAGE_MAP_AND_DIRECTORY_POINTER       *PageMapLevel4Entry;
  PAGE_MAP_AND_DIRECTORY_POINTER       *PageUpperDirectoryPointerEntry;
  PAGE_MAP_AND_DIRECTORY_POINTER       *PageDirectoryPointerEntry;
  PAGE_TABLE_1G_ENTRY                  *PageDirectory1GEntry;
  PAGE_TABLE_ENTRY                     *PageDirectory2MEntry;
  PAGE_TABLE_4K_ENTRY                  *PageTableEntry;
  UINT64                               AddressEncMask;
  UINT64                               PgTableMask;
  PHYSICAL_ADDRESS                     Address;
  PHYSICAL_ADDRESS                     AddressEnd;
  MEM_ENCRYPT_SEV_ADDRESS_RANGE_STATE  State;

  //
  // If Cr3BaseAddress is not specified then read the current CR3
  //
  if (Cr3BaseAddress == 0) {
    Cr3BaseAddress = AsmReadCr3();
  }

  AddressEncMask = MemEncryptSevGetEncryptionMask ();
  AddressEncMask &= PAGING_1G_ADDRESS_MASK_64;

  PgTableMask = AddressEncMask | EFI_PAGE_MASK;

  State = MemEncryptSevAddressRangeError;

  //
  // Encryption is on a page basis, so start at the beginning of the
  // virtual address page boundary and walk page-by-page.
  //
  Address = (PHYSICAL_ADDRESS) (UINTN) BaseAddress & ~EFI_PAGE_MASK;
  AddressEnd = (PHYSICAL_ADDRESS)
                 (UINTN) (BaseAddress + Length);

  while (Address < AddressEnd) {
    PageMapLevel4Entry = (VOID*) (Cr3BaseAddress & ~PgTableMask);
    PageMapLevel4Entry += PML4_OFFSET (Address);
    if (!PageMapLevel4Entry->Bits.Present) {
      return MemEncryptSevAddressRangeError;
    }

    PageDirectory1GEntry = (VOID *) (
                             (PageMapLevel4Entry->Bits.PageTableBaseAddress <<
                              12) & ~PgTableMask
                             );
    PageDirectory1GEntry += PDP_OFFSET (Address);
    if (!PageDirectory1GEntry->Bits.Present) {
      return MemEncryptSevAddressRangeError;
    }

    //
    // If the MustBe1 bit is not 1, it's not actually a 1GB entry
    //
    if (PageDirectory1GEntry->Bits.MustBe1) {
      //
      // Valid 1GB page
      //
      State = UpdateAddressState (
                State,
                PageDirectory1GEntry->Uint64,
                AddressEncMask
                );

      Address += BIT30;
      continue;
    }

    //
    // Actually a PDP
    //
    PageUpperDirectoryPointerEntry =
      (PAGE_MAP_AND_DIRECTORY_POINTER *) PageDirectory1GEntry;
    PageDirectory2MEntry =
      (VOID *) (
        (PageUpperDirectoryPointerEntry->Bits.PageTableBaseAddress <<
         12) & ~PgTableMask
        );
    PageDirectory2MEntry += PDE_OFFSET (Address);
    if (!PageDirectory2MEntry->Bits.Present) {
      return MemEncryptSevAddressRangeError;
    }

    //
    // If the MustBe1 bit is not a 1, it's not a 2MB entry
    //
    if (PageDirectory2MEntry->Bits.MustBe1) {
      //
      // Valid 2MB page
      //
      State = UpdateAddressState (
                State,
                PageDirectory2MEntry->Uint64,
                AddressEncMask
                );

      Address += BIT21;
      continue;
    }

    //
    // Actually a PMD
    //
    PageDirectoryPointerEntry =
      (PAGE_MAP_AND_DIRECTORY_POINTER *)PageDirectory2MEntry;
    PageTableEntry =
      (VOID *)(
        (PageDirectoryPointerEntry->Bits.PageTableBaseAddress <<
         12) & ~PgTableMask
        );
    PageTableEntry += PTE_OFFSET (Address);
    if (!PageTableEntry->Bits.Present) {
      return MemEncryptSevAddressRangeError;
    }

    State = UpdateAddressState (
              State,
              PageTableEntry->Uint64,
              AddressEncMask
              );

    Address += EFI_PAGE_SIZE;
  }

  return State;
}