amd-memory-encryption.txt 4.3 KB

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