amd-memory-encryption.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. .. SPDX-License-Identifier: GPL-2.0
  2. ======================================
  3. Secure Encrypted Virtualization (SEV)
  4. ======================================
  5. Overview
  6. ========
  7. Secure Encrypted Virtualization (SEV) is a feature found on AMD processors.
  8. SEV is an extension to the AMD-V architecture which supports running
  9. virtual machines (VMs) under the control of a hypervisor. When enabled,
  10. the memory contents of a VM will be transparently encrypted with a key
  11. unique to that VM.
  12. The hypervisor can determine the SEV support through the CPUID
  13. instruction. The CPUID function 0x8000001f reports information related
  14. to SEV::
  15. 0x8000001f[eax]:
  16. Bit[1] indicates support for SEV
  17. ...
  18. [ecx]:
  19. Bits[31:0] Number of encrypted guests supported simultaneously
  20. If support for SEV is present, MSR 0xc001_0010 (MSR_AMD64_SYSCFG) and MSR 0xc001_0015
  21. (MSR_K7_HWCR) can be used to determine if it can be enabled::
  22. 0xc001_0010:
  23. Bit[23] 1 = memory encryption can be enabled
  24. 0 = memory encryption can not be enabled
  25. 0xc001_0015:
  26. Bit[0] 1 = memory encryption can be enabled
  27. 0 = memory encryption can not be enabled
  28. When SEV support is available, it can be enabled in a specific VM by
  29. setting the SEV bit before executing VMRUN.::
  30. VMCB[0x90]:
  31. Bit[1] 1 = SEV is enabled
  32. 0 = SEV is disabled
  33. SEV hardware uses ASIDs to associate a memory encryption key with a VM.
  34. Hence, the ASID for the SEV-enabled guests must be from 1 to a maximum value
  35. defined in the CPUID 0x8000001f[ecx] field.
  36. SEV Key Management
  37. ==================
  38. The SEV guest key management is handled by a separate processor called the AMD
  39. Secure Processor (AMD-SP). Firmware running inside the AMD-SP provides a secure
  40. key management interface to perform common hypervisor activities such as
  41. encrypting bootstrap code, snapshot, migrating and debugging the guest. For more
  42. information, see the SEV Key Management spec [api-spec]_
  43. The main ioctl to access SEV is KVM_MEMORY_ENCRYPT_OP. If the argument
  44. to KVM_MEMORY_ENCRYPT_OP is NULL, the ioctl returns 0 if SEV is enabled
  45. and ``ENOTTY` if it is disabled (on some older versions of Linux,
  46. the ioctl runs normally even with a NULL argument, and therefore will
  47. likely return ``EFAULT``). If non-NULL, the argument to KVM_MEMORY_ENCRYPT_OP
  48. must be a struct kvm_sev_cmd::
  49. struct kvm_sev_cmd {
  50. __u32 id;
  51. __u64 data;
  52. __u32 error;
  53. __u32 sev_fd;
  54. };
  55. The ``id`` field contains the subcommand, and the ``data`` field points to
  56. another struct containing arguments specific to command. The ``sev_fd``
  57. should point to a file descriptor that is opened on the ``/dev/sev``
  58. device, if needed (see individual commands).
  59. On output, ``error`` is zero on success, or an error code. Error codes
  60. are defined in ``<linux/psp-dev.h>``.
  61. KVM implements the following commands to support common lifecycle events of SEV
  62. guests, such as launching, running, snapshotting, migrating and decommissioning.
  63. 1. KVM_SEV_INIT
  64. ---------------
  65. The KVM_SEV_INIT command is used by the hypervisor to initialize the SEV platform
  66. context. In a typical workflow, this command should be the first command issued.
  67. The firmware can be initialized either by using its own non-volatile storage or
  68. the OS can manage the NV storage for the firmware using the module parameter
  69. ``init_ex_path``. If the file specified by ``init_ex_path`` does not exist or
  70. is invalid, the OS will create or override the file with output from PSP.
  71. Returns: 0 on success, -negative on error
  72. 2. KVM_SEV_LAUNCH_START
  73. -----------------------
  74. The KVM_SEV_LAUNCH_START command is used for creating the memory encryption
  75. context. To create the encryption context, user must provide a guest policy,
  76. the owner's public Diffie-Hellman (PDH) key and session information.
  77. Parameters: struct kvm_sev_launch_start (in/out)
  78. Returns: 0 on success, -negative on error
  79. ::
  80. struct kvm_sev_launch_start {
  81. __u32 handle; /* if zero then firmware creates a new handle */
  82. __u32 policy; /* guest's policy */
  83. __u64 dh_uaddr; /* userspace address pointing to the guest owner's PDH key */
  84. __u32 dh_len;
  85. __u64 session_addr; /* userspace address which points to the guest session information */
  86. __u32 session_len;
  87. };
  88. On success, the 'handle' field contains a new handle and on error, a negative value.
  89. KVM_SEV_LAUNCH_START requires the ``sev_fd`` field to be valid.
  90. For more details, see SEV spec Section 6.2.
  91. 3. KVM_SEV_LAUNCH_UPDATE_DATA
  92. -----------------------------
  93. The KVM_SEV_LAUNCH_UPDATE_DATA is used for encrypting a memory region. It also
  94. calculates a measurement of the memory contents. The measurement is a signature
  95. of the memory contents that can be sent to the guest owner as an attestation
  96. that the memory was encrypted correctly by the firmware.
  97. Parameters (in): struct kvm_sev_launch_update_data
  98. Returns: 0 on success, -negative on error
  99. ::
  100. struct kvm_sev_launch_update {
  101. __u64 uaddr; /* userspace address to be encrypted (must be 16-byte aligned) */
  102. __u32 len; /* length of the data to be encrypted (must be 16-byte aligned) */
  103. };
  104. For more details, see SEV spec Section 6.3.
  105. 4. KVM_SEV_LAUNCH_MEASURE
  106. -------------------------
  107. The KVM_SEV_LAUNCH_MEASURE command is used to retrieve the measurement of the
  108. data encrypted by the KVM_SEV_LAUNCH_UPDATE_DATA command. The guest owner may
  109. wait to provide the guest with confidential information until it can verify the
  110. measurement. Since the guest owner knows the initial contents of the guest at
  111. boot, the measurement can be verified by comparing it to what the guest owner
  112. expects.
  113. If len is zero on entry, the measurement blob length is written to len and
  114. uaddr is unused.
  115. Parameters (in): struct kvm_sev_launch_measure
  116. Returns: 0 on success, -negative on error
  117. ::
  118. struct kvm_sev_launch_measure {
  119. __u64 uaddr; /* where to copy the measurement */
  120. __u32 len; /* length of measurement blob */
  121. };
  122. For more details on the measurement verification flow, see SEV spec Section 6.4.
  123. 5. KVM_SEV_LAUNCH_FINISH
  124. ------------------------
  125. After completion of the launch flow, the KVM_SEV_LAUNCH_FINISH command can be
  126. issued to make the guest ready for the execution.
  127. Returns: 0 on success, -negative on error
  128. 6. KVM_SEV_GUEST_STATUS
  129. -----------------------
  130. The KVM_SEV_GUEST_STATUS command is used to retrieve status information about a
  131. SEV-enabled guest.
  132. Parameters (out): struct kvm_sev_guest_status
  133. Returns: 0 on success, -negative on error
  134. ::
  135. struct kvm_sev_guest_status {
  136. __u32 handle; /* guest handle */
  137. __u32 policy; /* guest policy */
  138. __u8 state; /* guest state (see enum below) */
  139. };
  140. SEV guest state:
  141. ::
  142. enum {
  143. SEV_STATE_INVALID = 0;
  144. SEV_STATE_LAUNCHING, /* guest is currently being launched */
  145. SEV_STATE_SECRET, /* guest is being launched and ready to accept the ciphertext data */
  146. SEV_STATE_RUNNING, /* guest is fully launched and running */
  147. SEV_STATE_RECEIVING, /* guest is being migrated in from another SEV machine */
  148. SEV_STATE_SENDING /* guest is getting migrated out to another SEV machine */
  149. };
  150. 7. KVM_SEV_DBG_DECRYPT
  151. ----------------------
  152. The KVM_SEV_DEBUG_DECRYPT command can be used by the hypervisor to request the
  153. firmware to decrypt the data at the given memory region.
  154. Parameters (in): struct kvm_sev_dbg
  155. Returns: 0 on success, -negative on error
  156. ::
  157. struct kvm_sev_dbg {
  158. __u64 src_uaddr; /* userspace address of data to decrypt */
  159. __u64 dst_uaddr; /* userspace address of destination */
  160. __u32 len; /* length of memory region to decrypt */
  161. };
  162. The command returns an error if the guest policy does not allow debugging.
  163. 8. KVM_SEV_DBG_ENCRYPT
  164. ----------------------
  165. The KVM_SEV_DEBUG_ENCRYPT command can be used by the hypervisor to request the
  166. firmware to encrypt the data at the given memory region.
  167. Parameters (in): struct kvm_sev_dbg
  168. Returns: 0 on success, -negative on error
  169. ::
  170. struct kvm_sev_dbg {
  171. __u64 src_uaddr; /* userspace address of data to encrypt */
  172. __u64 dst_uaddr; /* userspace address of destination */
  173. __u32 len; /* length of memory region to encrypt */
  174. };
  175. The command returns an error if the guest policy does not allow debugging.
  176. 9. KVM_SEV_LAUNCH_SECRET
  177. ------------------------
  178. The KVM_SEV_LAUNCH_SECRET command can be used by the hypervisor to inject secret
  179. data after the measurement has been validated by the guest owner.
  180. Parameters (in): struct kvm_sev_launch_secret
  181. Returns: 0 on success, -negative on error
  182. ::
  183. struct kvm_sev_launch_secret {
  184. __u64 hdr_uaddr; /* userspace address containing the packet header */
  185. __u32 hdr_len;
  186. __u64 guest_uaddr; /* the guest memory region where the secret should be injected */
  187. __u32 guest_len;
  188. __u64 trans_uaddr; /* the hypervisor memory region which contains the secret */
  189. __u32 trans_len;
  190. };
  191. 10. KVM_SEV_GET_ATTESTATION_REPORT
  192. ----------------------------------
  193. The KVM_SEV_GET_ATTESTATION_REPORT command can be used by the hypervisor to query the attestation
  194. report containing the SHA-256 digest of the guest memory and VMSA passed through the KVM_SEV_LAUNCH
  195. commands and signed with the PEK. The digest returned by the command should match the digest
  196. used by the guest owner with the KVM_SEV_LAUNCH_MEASURE.
  197. If len is zero on entry, the measurement blob length is written to len and
  198. uaddr is unused.
  199. Parameters (in): struct kvm_sev_attestation
  200. Returns: 0 on success, -negative on error
  201. ::
  202. struct kvm_sev_attestation_report {
  203. __u8 mnonce[16]; /* A random mnonce that will be placed in the report */
  204. __u64 uaddr; /* userspace address where the report should be copied */
  205. __u32 len;
  206. };
  207. 11. KVM_SEV_SEND_START
  208. ----------------------
  209. The KVM_SEV_SEND_START command can be used by the hypervisor to create an
  210. outgoing guest encryption context.
  211. If session_len is zero on entry, the length of the guest session information is
  212. written to session_len and all other fields are not used.
  213. Parameters (in): struct kvm_sev_send_start
  214. Returns: 0 on success, -negative on error
  215. ::
  216. struct kvm_sev_send_start {
  217. __u32 policy; /* guest policy */
  218. __u64 pdh_cert_uaddr; /* platform Diffie-Hellman certificate */
  219. __u32 pdh_cert_len;
  220. __u64 plat_certs_uaddr; /* platform certificate chain */
  221. __u32 plat_certs_len;
  222. __u64 amd_certs_uaddr; /* AMD certificate */
  223. __u32 amd_certs_len;
  224. __u64 session_uaddr; /* Guest session information */
  225. __u32 session_len;
  226. };
  227. 12. KVM_SEV_SEND_UPDATE_DATA
  228. ----------------------------
  229. The KVM_SEV_SEND_UPDATE_DATA command can be used by the hypervisor to encrypt the
  230. outgoing guest memory region with the encryption context creating using
  231. KVM_SEV_SEND_START.
  232. If hdr_len or trans_len are zero on entry, the length of the packet header and
  233. transport region are written to hdr_len and trans_len respectively, and all
  234. other fields are not used.
  235. Parameters (in): struct kvm_sev_send_update_data
  236. Returns: 0 on success, -negative on error
  237. ::
  238. struct kvm_sev_launch_send_update_data {
  239. __u64 hdr_uaddr; /* userspace address containing the packet header */
  240. __u32 hdr_len;
  241. __u64 guest_uaddr; /* the source memory region to be encrypted */
  242. __u32 guest_len;
  243. __u64 trans_uaddr; /* the destination memory region */
  244. __u32 trans_len;
  245. };
  246. 13. KVM_SEV_SEND_FINISH
  247. ------------------------
  248. After completion of the migration flow, the KVM_SEV_SEND_FINISH command can be
  249. issued by the hypervisor to delete the encryption context.
  250. Returns: 0 on success, -negative on error
  251. 14. KVM_SEV_SEND_CANCEL
  252. ------------------------
  253. After completion of SEND_START, but before SEND_FINISH, the source VMM can issue the
  254. SEND_CANCEL command to stop a migration. This is necessary so that a cancelled
  255. migration can restart with a new target later.
  256. Returns: 0 on success, -negative on error
  257. 15. KVM_SEV_RECEIVE_START
  258. -------------------------
  259. The KVM_SEV_RECEIVE_START command is used for creating the memory encryption
  260. context for an incoming SEV guest. To create the encryption context, the user must
  261. provide a guest policy, the platform public Diffie-Hellman (PDH) key and session
  262. information.
  263. Parameters: struct kvm_sev_receive_start (in/out)
  264. Returns: 0 on success, -negative on error
  265. ::
  266. struct kvm_sev_receive_start {
  267. __u32 handle; /* if zero then firmware creates a new handle */
  268. __u32 policy; /* guest's policy */
  269. __u64 pdh_uaddr; /* userspace address pointing to the PDH key */
  270. __u32 pdh_len;
  271. __u64 session_uaddr; /* userspace address which points to the guest session information */
  272. __u32 session_len;
  273. };
  274. On success, the 'handle' field contains a new handle and on error, a negative value.
  275. For more details, see SEV spec Section 6.12.
  276. 16. KVM_SEV_RECEIVE_UPDATE_DATA
  277. -------------------------------
  278. The KVM_SEV_RECEIVE_UPDATE_DATA command can be used by the hypervisor to copy
  279. the incoming buffers into the guest memory region with encryption context
  280. created during the KVM_SEV_RECEIVE_START.
  281. Parameters (in): struct kvm_sev_receive_update_data
  282. Returns: 0 on success, -negative on error
  283. ::
  284. struct kvm_sev_launch_receive_update_data {
  285. __u64 hdr_uaddr; /* userspace address containing the packet header */
  286. __u32 hdr_len;
  287. __u64 guest_uaddr; /* the destination guest memory region */
  288. __u32 guest_len;
  289. __u64 trans_uaddr; /* the incoming buffer memory region */
  290. __u32 trans_len;
  291. };
  292. 17. KVM_SEV_RECEIVE_FINISH
  293. --------------------------
  294. After completion of the migration flow, the KVM_SEV_RECEIVE_FINISH command can be
  295. issued by the hypervisor to make the guest ready for execution.
  296. Returns: 0 on success, -negative on error
  297. References
  298. ==========
  299. See [white-paper]_, [api-spec]_, [amd-apm]_ and [kvm-forum]_ for more info.
  300. .. [white-paper] http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf
  301. .. [api-spec] https://support.amd.com/TechDocs/55766_SEV-KM_API_Specification.pdf
  302. .. [amd-apm] https://support.amd.com/TechDocs/24593.pdf (section 15.34)
  303. .. [kvm-forum] https://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf