amd-memory-encryption.rst 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. .. SPDX-License-Identifier: GPL-2.0
  2. =====================
  3. AMD Memory Encryption
  4. =====================
  5. Secure Memory Encryption (SME) and Secure Encrypted Virtualization (SEV) are
  6. features found on AMD processors.
  7. SME provides the ability to mark individual pages of memory as encrypted using
  8. the standard x86 page tables. A page that is marked encrypted will be
  9. automatically decrypted when read from DRAM and encrypted when written to
  10. DRAM. SME can therefore be used to protect the contents of DRAM from physical
  11. attacks on the system.
  12. SEV enables running encrypted virtual machines (VMs) in which the code and data
  13. of the guest VM are secured so that a decrypted version is available only
  14. within the VM itself. SEV guest VMs have the concept of private and shared
  15. memory. Private memory is encrypted with the guest-specific key, while shared
  16. memory may be encrypted with hypervisor key. When SME is enabled, the hypervisor
  17. key is the same key which is used in SME.
  18. A page is encrypted when a page table entry has the encryption bit set (see
  19. below on how to determine its position). The encryption bit can also be
  20. specified in the cr3 register, allowing the PGD table to be encrypted. Each
  21. successive level of page tables can also be encrypted by setting the encryption
  22. bit in the page table entry that points to the next table. This allows the full
  23. page table hierarchy to be encrypted. Note, this means that just because the
  24. encryption bit is set in cr3, doesn't imply the full hierarchy is encrypted.
  25. Each page table entry in the hierarchy needs to have the encryption bit set to
  26. achieve that. So, theoretically, you could have the encryption bit set in cr3
  27. so that the PGD is encrypted, but not set the encryption bit in the PGD entry
  28. for a PUD which results in the PUD pointed to by that entry to not be
  29. encrypted.
  30. When SEV is enabled, instruction pages and guest page tables are always treated
  31. as private. All the DMA operations inside the guest must be performed on shared
  32. memory. Since the memory encryption bit is controlled by the guest OS when it
  33. is operating in 64-bit or 32-bit PAE mode, in all other modes the SEV hardware
  34. forces the memory encryption bit to 1.
  35. Support for SME and SEV can be determined through the CPUID instruction. The
  36. CPUID function 0x8000001f reports information related to SME::
  37. 0x8000001f[eax]:
  38. Bit[0] indicates support for SME
  39. Bit[1] indicates support for SEV
  40. 0x8000001f[ebx]:
  41. Bits[5:0] pagetable bit number used to activate memory
  42. encryption
  43. Bits[11:6] reduction in physical address space, in bits, when
  44. memory encryption is enabled (this only affects
  45. system physical addresses, not guest physical
  46. addresses)
  47. If support for SME is present, MSR 0xc00100010 (MSR_K8_SYSCFG) can be used to
  48. determine if SME is enabled and/or to enable memory encryption::
  49. 0xc0010010:
  50. Bit[23] 0 = memory encryption features are disabled
  51. 1 = memory encryption features are enabled
  52. If SEV is supported, MSR 0xc0010131 (MSR_AMD64_SEV) can be used to determine if
  53. SEV is active::
  54. 0xc0010131:
  55. Bit[0] 0 = memory encryption is not active
  56. 1 = memory encryption is active
  57. Linux relies on BIOS to set this bit if BIOS has determined that the reduction
  58. in the physical address space as a result of enabling memory encryption (see
  59. CPUID information above) will not conflict with the address space resource
  60. requirements for the system. If this bit is not set upon Linux startup then
  61. Linux itself will not set it and memory encryption will not be possible.
  62. The state of SME in the Linux kernel can be documented as follows:
  63. - Supported:
  64. The CPU supports SME (determined through CPUID instruction).
  65. - Enabled:
  66. Supported and bit 23 of MSR_K8_SYSCFG is set.
  67. - Active:
  68. Supported, Enabled and the Linux kernel is actively applying
  69. the encryption bit to page table entries (the SME mask in the
  70. kernel is non-zero).
  71. SME can also be enabled and activated in the BIOS. If SME is enabled and
  72. activated in the BIOS, then all memory accesses will be encrypted and it will
  73. not be necessary to activate the Linux memory encryption support. If the BIOS
  74. merely enables SME (sets bit 23 of the MSR_K8_SYSCFG), then Linux can activate
  75. memory encryption by default (CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT=y) or
  76. by supplying mem_encrypt=on on the kernel command line. However, if BIOS does
  77. not enable SME, then Linux will not be able to activate memory encryption, even
  78. if configured to do so by default or the mem_encrypt=on command line parameter
  79. is specified.