sev.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * AMD Encrypted Register State Support
  4. *
  5. * Author: Joerg Roedel <[email protected]>
  6. */
  7. #ifndef __ASM_ENCRYPTED_STATE_H
  8. #define __ASM_ENCRYPTED_STATE_H
  9. #include <linux/types.h>
  10. #include <linux/sev-guest.h>
  11. #include <asm/insn.h>
  12. #include <asm/sev-common.h>
  13. #include <asm/bootparam.h>
  14. #define GHCB_PROTOCOL_MIN 1ULL
  15. #define GHCB_PROTOCOL_MAX 2ULL
  16. #define GHCB_DEFAULT_USAGE 0ULL
  17. #define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); }
  18. enum es_result {
  19. ES_OK, /* All good */
  20. ES_UNSUPPORTED, /* Requested operation not supported */
  21. ES_VMM_ERROR, /* Unexpected state from the VMM */
  22. ES_DECODE_FAILED, /* Instruction decoding failed */
  23. ES_EXCEPTION, /* Instruction caused exception */
  24. ES_RETRY, /* Retry instruction emulation */
  25. };
  26. struct es_fault_info {
  27. unsigned long vector;
  28. unsigned long error_code;
  29. unsigned long cr2;
  30. };
  31. struct pt_regs;
  32. /* ES instruction emulation context */
  33. struct es_em_ctxt {
  34. struct pt_regs *regs;
  35. struct insn insn;
  36. struct es_fault_info fi;
  37. };
  38. /*
  39. * AMD SEV Confidential computing blob structure. The structure is
  40. * defined in OVMF UEFI firmware header:
  41. * https://github.com/tianocore/edk2/blob/master/OvmfPkg/Include/Guid/ConfidentialComputingSevSnpBlob.h
  42. */
  43. #define CC_BLOB_SEV_HDR_MAGIC 0x45444d41
  44. struct cc_blob_sev_info {
  45. u32 magic;
  46. u16 version;
  47. u16 reserved;
  48. u64 secrets_phys;
  49. u32 secrets_len;
  50. u32 rsvd1;
  51. u64 cpuid_phys;
  52. u32 cpuid_len;
  53. u32 rsvd2;
  54. } __packed;
  55. void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code);
  56. static inline u64 lower_bits(u64 val, unsigned int bits)
  57. {
  58. u64 mask = (1ULL << bits) - 1;
  59. return (val & mask);
  60. }
  61. struct real_mode_header;
  62. enum stack_type;
  63. /* Early IDT entry points for #VC handler */
  64. extern void vc_no_ghcb(void);
  65. extern void vc_boot_ghcb(void);
  66. extern bool handle_vc_boot_ghcb(struct pt_regs *regs);
  67. /* Software defined (when rFlags.CF = 1) */
  68. #define PVALIDATE_FAIL_NOUPDATE 255
  69. /* RMP page size */
  70. #define RMP_PG_SIZE_4K 0
  71. #define RMPADJUST_VMSA_PAGE_BIT BIT(16)
  72. /* SNP Guest message request */
  73. struct snp_req_data {
  74. unsigned long req_gpa;
  75. unsigned long resp_gpa;
  76. unsigned long data_gpa;
  77. unsigned int data_npages;
  78. };
  79. struct sev_guest_platform_data {
  80. u64 secrets_gpa;
  81. };
  82. /*
  83. * The secrets page contains 96-bytes of reserved field that can be used by
  84. * the guest OS. The guest OS uses the area to save the message sequence
  85. * number for each VMPCK.
  86. *
  87. * See the GHCB spec section Secret page layout for the format for this area.
  88. */
  89. struct secrets_os_area {
  90. u32 msg_seqno_0;
  91. u32 msg_seqno_1;
  92. u32 msg_seqno_2;
  93. u32 msg_seqno_3;
  94. u64 ap_jump_table_pa;
  95. u8 rsvd[40];
  96. u8 guest_usage[32];
  97. } __packed;
  98. #define VMPCK_KEY_LEN 32
  99. /* See the SNP spec version 0.9 for secrets page format */
  100. struct snp_secrets_page_layout {
  101. u32 version;
  102. u32 imien : 1,
  103. rsvd1 : 31;
  104. u32 fms;
  105. u32 rsvd2;
  106. u8 gosvw[16];
  107. u8 vmpck0[VMPCK_KEY_LEN];
  108. u8 vmpck1[VMPCK_KEY_LEN];
  109. u8 vmpck2[VMPCK_KEY_LEN];
  110. u8 vmpck3[VMPCK_KEY_LEN];
  111. struct secrets_os_area os_area;
  112. u8 rsvd3[3840];
  113. } __packed;
  114. #ifdef CONFIG_AMD_MEM_ENCRYPT
  115. extern struct static_key_false sev_es_enable_key;
  116. extern void __sev_es_ist_enter(struct pt_regs *regs);
  117. extern void __sev_es_ist_exit(void);
  118. static __always_inline void sev_es_ist_enter(struct pt_regs *regs)
  119. {
  120. if (static_branch_unlikely(&sev_es_enable_key))
  121. __sev_es_ist_enter(regs);
  122. }
  123. static __always_inline void sev_es_ist_exit(void)
  124. {
  125. if (static_branch_unlikely(&sev_es_enable_key))
  126. __sev_es_ist_exit();
  127. }
  128. extern int sev_es_setup_ap_jump_table(struct real_mode_header *rmh);
  129. extern void __sev_es_nmi_complete(void);
  130. static __always_inline void sev_es_nmi_complete(void)
  131. {
  132. if (static_branch_unlikely(&sev_es_enable_key))
  133. __sev_es_nmi_complete();
  134. }
  135. extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
  136. static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs)
  137. {
  138. int rc;
  139. /* "rmpadjust" mnemonic support in binutils 2.36 and newer */
  140. asm volatile(".byte 0xF3,0x0F,0x01,0xFE\n\t"
  141. : "=a"(rc)
  142. : "a"(vaddr), "c"(rmp_psize), "d"(attrs)
  143. : "memory", "cc");
  144. return rc;
  145. }
  146. static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate)
  147. {
  148. bool no_rmpupdate;
  149. int rc;
  150. /* "pvalidate" mnemonic support in binutils 2.36 and newer */
  151. asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t"
  152. CC_SET(c)
  153. : CC_OUT(c) (no_rmpupdate), "=a"(rc)
  154. : "a"(vaddr), "c"(rmp_psize), "d"(validate)
  155. : "memory", "cc");
  156. if (no_rmpupdate)
  157. return PVALIDATE_FAIL_NOUPDATE;
  158. return rc;
  159. }
  160. struct snp_guest_request_ioctl;
  161. void setup_ghcb(void);
  162. void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr,
  163. unsigned long npages);
  164. void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr,
  165. unsigned long npages);
  166. void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op);
  167. void snp_set_memory_shared(unsigned long vaddr, unsigned long npages);
  168. void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
  169. void snp_set_wakeup_secondary_cpu(void);
  170. bool snp_init(struct boot_params *bp);
  171. void __init __noreturn snp_abort(void);
  172. int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio);
  173. #else
  174. static inline void sev_es_ist_enter(struct pt_regs *regs) { }
  175. static inline void sev_es_ist_exit(void) { }
  176. static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
  177. static inline void sev_es_nmi_complete(void) { }
  178. static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
  179. static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; }
  180. static inline int rmpadjust(unsigned long vaddr, bool rmp_psize, unsigned long attrs) { return 0; }
  181. static inline void setup_ghcb(void) { }
  182. static inline void __init
  183. early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
  184. static inline void __init
  185. early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned long npages) { }
  186. static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, enum psc_op op) { }
  187. static inline void snp_set_memory_shared(unsigned long vaddr, unsigned long npages) { }
  188. static inline void snp_set_memory_private(unsigned long vaddr, unsigned long npages) { }
  189. static inline void snp_set_wakeup_secondary_cpu(void) { }
  190. static inline bool snp_init(struct boot_params *bp) { return false; }
  191. static inline void snp_abort(void) { }
  192. static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, struct snp_guest_request_ioctl *rio)
  193. {
  194. return -ENOTTY;
  195. }
  196. #endif
  197. #endif