sev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * AMD Encrypted Register State Support
  4. *
  5. * Author: Joerg Roedel <[email protected]>
  6. */
  7. /*
  8. * misc.h needs to be first because it knows how to include the other kernel
  9. * headers in the pre-decompression code in a way that does not break
  10. * compilation.
  11. */
  12. #include "misc.h"
  13. #include <asm/pgtable_types.h>
  14. #include <asm/sev.h>
  15. #include <asm/trapnr.h>
  16. #include <asm/trap_pf.h>
  17. #include <asm/msr-index.h>
  18. #include <asm/fpu/xcr.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/svm.h>
  21. #include <asm/cpuid.h>
  22. #include "error.h"
  23. #include "../msr.h"
  24. struct ghcb boot_ghcb_page __aligned(PAGE_SIZE);
  25. struct ghcb *boot_ghcb;
  26. /*
  27. * Copy a version of this function here - insn-eval.c can't be used in
  28. * pre-decompression code.
  29. */
  30. static bool insn_has_rep_prefix(struct insn *insn)
  31. {
  32. insn_byte_t p;
  33. int i;
  34. insn_get_prefixes(insn);
  35. for_each_insn_prefix(insn, i, p) {
  36. if (p == 0xf2 || p == 0xf3)
  37. return true;
  38. }
  39. return false;
  40. }
  41. /*
  42. * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
  43. * doesn't use segments.
  44. */
  45. static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
  46. {
  47. return 0UL;
  48. }
  49. static inline u64 sev_es_rd_ghcb_msr(void)
  50. {
  51. struct msr m;
  52. boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
  53. return m.q;
  54. }
  55. static inline void sev_es_wr_ghcb_msr(u64 val)
  56. {
  57. struct msr m;
  58. m.q = val;
  59. boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
  60. }
  61. static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
  62. {
  63. char buffer[MAX_INSN_SIZE];
  64. int ret;
  65. memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
  66. ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
  67. if (ret < 0)
  68. return ES_DECODE_FAILED;
  69. return ES_OK;
  70. }
  71. static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
  72. void *dst, char *buf, size_t size)
  73. {
  74. memcpy(dst, buf, size);
  75. return ES_OK;
  76. }
  77. static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
  78. void *src, char *buf, size_t size)
  79. {
  80. memcpy(buf, src, size);
  81. return ES_OK;
  82. }
  83. static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
  84. {
  85. return ES_OK;
  86. }
  87. static bool fault_in_kernel_space(unsigned long address)
  88. {
  89. return false;
  90. }
  91. #undef __init
  92. #undef __pa
  93. #define __init
  94. #define __pa(x) ((unsigned long)(x))
  95. #define __BOOT_COMPRESSED
  96. /* Basic instruction decoding support needed */
  97. #include "../../lib/inat.c"
  98. #include "../../lib/insn.c"
  99. /* Include code for early handlers */
  100. #include "../../kernel/sev-shared.c"
  101. static inline bool sev_snp_enabled(void)
  102. {
  103. return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
  104. }
  105. static void __page_state_change(unsigned long paddr, enum psc_op op)
  106. {
  107. u64 val;
  108. if (!sev_snp_enabled())
  109. return;
  110. /*
  111. * If private -> shared then invalidate the page before requesting the
  112. * state change in the RMP table.
  113. */
  114. if (op == SNP_PAGE_STATE_SHARED && pvalidate(paddr, RMP_PG_SIZE_4K, 0))
  115. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
  116. /* Issue VMGEXIT to change the page state in RMP table. */
  117. sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
  118. VMGEXIT();
  119. /* Read the response of the VMGEXIT. */
  120. val = sev_es_rd_ghcb_msr();
  121. if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))
  122. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
  123. /*
  124. * Now that page state is changed in the RMP table, validate it so that it is
  125. * consistent with the RMP entry.
  126. */
  127. if (op == SNP_PAGE_STATE_PRIVATE && pvalidate(paddr, RMP_PG_SIZE_4K, 1))
  128. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
  129. }
  130. void snp_set_page_private(unsigned long paddr)
  131. {
  132. __page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);
  133. }
  134. void snp_set_page_shared(unsigned long paddr)
  135. {
  136. __page_state_change(paddr, SNP_PAGE_STATE_SHARED);
  137. }
  138. static bool early_setup_ghcb(void)
  139. {
  140. if (set_page_decrypted((unsigned long)&boot_ghcb_page))
  141. return false;
  142. /* Page is now mapped decrypted, clear it */
  143. memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));
  144. boot_ghcb = &boot_ghcb_page;
  145. /* Initialize lookup tables for the instruction decoder */
  146. inat_init_tables();
  147. /* SNP guest requires the GHCB GPA must be registered */
  148. if (sev_snp_enabled())
  149. snp_register_ghcb_early(__pa(&boot_ghcb_page));
  150. return true;
  151. }
  152. void sev_es_shutdown_ghcb(void)
  153. {
  154. if (!boot_ghcb)
  155. return;
  156. if (!sev_es_check_cpu_features())
  157. error("SEV-ES CPU Features missing.");
  158. /*
  159. * GHCB Page must be flushed from the cache and mapped encrypted again.
  160. * Otherwise the running kernel will see strange cache effects when
  161. * trying to use that page.
  162. */
  163. if (set_page_encrypted((unsigned long)&boot_ghcb_page))
  164. error("Can't map GHCB page encrypted");
  165. /*
  166. * GHCB page is mapped encrypted again and flushed from the cache.
  167. * Mark it non-present now to catch bugs when #VC exceptions trigger
  168. * after this point.
  169. */
  170. if (set_page_non_present((unsigned long)&boot_ghcb_page))
  171. error("Can't unmap GHCB page");
  172. }
  173. static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set,
  174. unsigned int reason, u64 exit_info_2)
  175. {
  176. u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason);
  177. vc_ghcb_invalidate(ghcb);
  178. ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST);
  179. ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
  180. ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
  181. sev_es_wr_ghcb_msr(__pa(ghcb));
  182. VMGEXIT();
  183. while (true)
  184. asm volatile("hlt\n" : : : "memory");
  185. }
  186. bool sev_es_check_ghcb_fault(unsigned long address)
  187. {
  188. /* Check whether the fault was on the GHCB page */
  189. return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);
  190. }
  191. void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
  192. {
  193. struct es_em_ctxt ctxt;
  194. enum es_result result;
  195. if (!boot_ghcb && !early_setup_ghcb())
  196. sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
  197. vc_ghcb_invalidate(boot_ghcb);
  198. result = vc_init_em_ctxt(&ctxt, regs, exit_code);
  199. if (result != ES_OK)
  200. goto finish;
  201. switch (exit_code) {
  202. case SVM_EXIT_RDTSC:
  203. case SVM_EXIT_RDTSCP:
  204. result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code);
  205. break;
  206. case SVM_EXIT_IOIO:
  207. result = vc_handle_ioio(boot_ghcb, &ctxt);
  208. break;
  209. case SVM_EXIT_CPUID:
  210. result = vc_handle_cpuid(boot_ghcb, &ctxt);
  211. break;
  212. default:
  213. result = ES_UNSUPPORTED;
  214. break;
  215. }
  216. finish:
  217. if (result == ES_OK)
  218. vc_finish_insn(&ctxt);
  219. else if (result != ES_RETRY)
  220. sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
  221. }
  222. static void enforce_vmpl0(void)
  223. {
  224. u64 attrs;
  225. int err;
  226. /*
  227. * RMPADJUST modifies RMP permissions of a lesser-privileged (numerically
  228. * higher) privilege level. Here, clear the VMPL1 permission mask of the
  229. * GHCB page. If the guest is not running at VMPL0, this will fail.
  230. *
  231. * If the guest is running at VMPL0, it will succeed. Even if that operation
  232. * modifies permission bits, it is still ok to do so currently because Linux
  233. * SNP guests are supported only on VMPL0 so VMPL1 or higher permission masks
  234. * changing is a don't-care.
  235. */
  236. attrs = 1;
  237. if (rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, attrs))
  238. sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
  239. }
  240. /*
  241. * SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need
  242. * guest side implementation for proper functioning of the guest. If any
  243. * of these features are enabled in the hypervisor but are lacking guest
  244. * side implementation, the behavior of the guest will be undefined. The
  245. * guest could fail in non-obvious way making it difficult to debug.
  246. *
  247. * As the behavior of reserved feature bits is unknown to be on the
  248. * safe side add them to the required features mask.
  249. */
  250. #define SNP_FEATURES_IMPL_REQ (MSR_AMD64_SNP_VTOM | \
  251. MSR_AMD64_SNP_REFLECT_VC | \
  252. MSR_AMD64_SNP_RESTRICTED_INJ | \
  253. MSR_AMD64_SNP_ALT_INJ | \
  254. MSR_AMD64_SNP_DEBUG_SWAP | \
  255. MSR_AMD64_SNP_VMPL_SSS | \
  256. MSR_AMD64_SNP_SECURE_TSC | \
  257. MSR_AMD64_SNP_VMGEXIT_PARAM | \
  258. MSR_AMD64_SNP_VMSA_REG_PROTECTION | \
  259. MSR_AMD64_SNP_RESERVED_BIT13 | \
  260. MSR_AMD64_SNP_RESERVED_BIT15 | \
  261. MSR_AMD64_SNP_RESERVED_MASK)
  262. /*
  263. * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
  264. * by the guest kernel. As and when a new feature is implemented in the
  265. * guest kernel, a corresponding bit should be added to the mask.
  266. */
  267. #define SNP_FEATURES_PRESENT (0)
  268. void snp_check_features(void)
  269. {
  270. u64 unsupported;
  271. if (!(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
  272. return;
  273. /*
  274. * Terminate the boot if hypervisor has enabled any feature lacking
  275. * guest side implementation. Pass on the unsupported features mask through
  276. * EXIT_INFO_2 of the GHCB protocol so that those features can be reported
  277. * as part of the guest boot failure.
  278. */
  279. unsupported = sev_status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT;
  280. if (unsupported) {
  281. if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb()))
  282. sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
  283. sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN,
  284. GHCB_SNP_UNSUPPORTED, unsupported);
  285. }
  286. }
  287. void sev_enable(struct boot_params *bp)
  288. {
  289. unsigned int eax, ebx, ecx, edx;
  290. struct msr m;
  291. bool snp;
  292. /*
  293. * bp->cc_blob_address should only be set by boot/compressed kernel.
  294. * Initialize it to 0 to ensure that uninitialized values from
  295. * buggy bootloaders aren't propagated.
  296. */
  297. if (bp)
  298. bp->cc_blob_address = 0;
  299. /*
  300. * Do an initial SEV capability check before snp_init() which
  301. * loads the CPUID page and the same checks afterwards are done
  302. * without the hypervisor and are trustworthy.
  303. *
  304. * If the HV fakes SEV support, the guest will crash'n'burn
  305. * which is good enough.
  306. */
  307. /* Check for the SME/SEV support leaf */
  308. eax = 0x80000000;
  309. ecx = 0;
  310. native_cpuid(&eax, &ebx, &ecx, &edx);
  311. if (eax < 0x8000001f)
  312. return;
  313. /*
  314. * Check for the SME/SEV feature:
  315. * CPUID Fn8000_001F[EAX]
  316. * - Bit 0 - Secure Memory Encryption support
  317. * - Bit 1 - Secure Encrypted Virtualization support
  318. * CPUID Fn8000_001F[EBX]
  319. * - Bits 5:0 - Pagetable bit position used to indicate encryption
  320. */
  321. eax = 0x8000001f;
  322. ecx = 0;
  323. native_cpuid(&eax, &ebx, &ecx, &edx);
  324. /* Check whether SEV is supported */
  325. if (!(eax & BIT(1)))
  326. return;
  327. /*
  328. * Setup/preliminary detection of SNP. This will be sanity-checked
  329. * against CPUID/MSR values later.
  330. */
  331. snp = snp_init(bp);
  332. /* Now repeat the checks with the SNP CPUID table. */
  333. /* Recheck the SME/SEV support leaf */
  334. eax = 0x80000000;
  335. ecx = 0;
  336. native_cpuid(&eax, &ebx, &ecx, &edx);
  337. if (eax < 0x8000001f)
  338. return;
  339. /*
  340. * Recheck for the SME/SEV feature:
  341. * CPUID Fn8000_001F[EAX]
  342. * - Bit 0 - Secure Memory Encryption support
  343. * - Bit 1 - Secure Encrypted Virtualization support
  344. * CPUID Fn8000_001F[EBX]
  345. * - Bits 5:0 - Pagetable bit position used to indicate encryption
  346. */
  347. eax = 0x8000001f;
  348. ecx = 0;
  349. native_cpuid(&eax, &ebx, &ecx, &edx);
  350. /* Check whether SEV is supported */
  351. if (!(eax & BIT(1))) {
  352. if (snp)
  353. error("SEV-SNP support indicated by CC blob, but not CPUID.");
  354. return;
  355. }
  356. /* Set the SME mask if this is an SEV guest. */
  357. boot_rdmsr(MSR_AMD64_SEV, &m);
  358. sev_status = m.q;
  359. if (!(sev_status & MSR_AMD64_SEV_ENABLED))
  360. return;
  361. /* Negotiate the GHCB protocol version. */
  362. if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {
  363. if (!sev_es_negotiate_protocol())
  364. sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);
  365. }
  366. /*
  367. * SNP is supported in v2 of the GHCB spec which mandates support for HV
  368. * features.
  369. */
  370. if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
  371. if (!(get_hv_features() & GHCB_HV_FT_SNP))
  372. sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
  373. enforce_vmpl0();
  374. }
  375. if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
  376. error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");
  377. sme_me_mask = BIT_ULL(ebx & 0x3f);
  378. }
  379. /* Search for Confidential Computing blob in the EFI config table. */
  380. static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)
  381. {
  382. unsigned long cfg_table_pa;
  383. unsigned int cfg_table_len;
  384. int ret;
  385. ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);
  386. if (ret)
  387. return NULL;
  388. return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,
  389. cfg_table_len,
  390. EFI_CC_BLOB_GUID);
  391. }
  392. /*
  393. * Initial set up of SNP relies on information provided by the
  394. * Confidential Computing blob, which can be passed to the boot kernel
  395. * by firmware/bootloader in the following ways:
  396. *
  397. * - via an entry in the EFI config table
  398. * - via a setup_data structure, as defined by the Linux Boot Protocol
  399. *
  400. * Scan for the blob in that order.
  401. */
  402. static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
  403. {
  404. struct cc_blob_sev_info *cc_info;
  405. cc_info = find_cc_blob_efi(bp);
  406. if (cc_info)
  407. goto found_cc_info;
  408. cc_info = find_cc_blob_setup_data(bp);
  409. if (!cc_info)
  410. return NULL;
  411. found_cc_info:
  412. if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
  413. sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
  414. return cc_info;
  415. }
  416. /*
  417. * Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks
  418. * will verify the SNP CPUID/MSR bits.
  419. */
  420. bool snp_init(struct boot_params *bp)
  421. {
  422. struct cc_blob_sev_info *cc_info;
  423. if (!bp)
  424. return false;
  425. cc_info = find_cc_blob(bp);
  426. if (!cc_info)
  427. return false;
  428. /*
  429. * If a SNP-specific Confidential Computing blob is present, then
  430. * firmware/bootloader have indicated SNP support. Verifying this
  431. * involves CPUID checks which will be more reliable if the SNP
  432. * CPUID table is used. See comments over snp_setup_cpuid_table() for
  433. * more details.
  434. */
  435. setup_cpuid_table(cc_info);
  436. /*
  437. * Pass run-time kernel a pointer to CC info via boot_params so EFI
  438. * config table doesn't need to be searched again during early startup
  439. * phase.
  440. */
  441. bp->cc_blob_address = (u32)(unsigned long)cc_info;
  442. return true;
  443. }
  444. void sev_prep_identity_maps(unsigned long top_level_pgt)
  445. {
  446. /*
  447. * The Confidential Computing blob is used very early in uncompressed
  448. * kernel to find the in-memory CPUID table to handle CPUID
  449. * instructions. Make sure an identity-mapping exists so it can be
  450. * accessed after switchover.
  451. */
  452. if (sev_snp_enabled()) {
  453. unsigned long cc_info_pa = boot_params->cc_blob_address;
  454. struct cc_blob_sev_info *cc_info;
  455. kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));
  456. cc_info = (struct cc_blob_sev_info *)cc_info_pa;
  457. kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);
  458. }
  459. sev_verify_cbit(top_level_pgt);
  460. }