1e759959fSBrijesh Singh // SPDX-License-Identifier: GPL-2.0 2e759959fSBrijesh Singh /* 3e759959fSBrijesh Singh * AMD Encrypted Register State Support 4e759959fSBrijesh Singh * 5e759959fSBrijesh Singh * Author: Joerg Roedel <jroedel@suse.de> 6e759959fSBrijesh Singh */ 7e759959fSBrijesh Singh 8e759959fSBrijesh Singh /* 9e759959fSBrijesh Singh * misc.h needs to be first because it knows how to include the other kernel 10e759959fSBrijesh Singh * headers in the pre-decompression code in a way that does not break 11e759959fSBrijesh Singh * compilation. 12e759959fSBrijesh Singh */ 13e759959fSBrijesh Singh #include "misc.h" 14e759959fSBrijesh Singh 15e759959fSBrijesh Singh #include <asm/pgtable_types.h> 16e759959fSBrijesh Singh #include <asm/sev.h> 17e759959fSBrijesh Singh #include <asm/trapnr.h> 18e759959fSBrijesh Singh #include <asm/trap_pf.h> 19e759959fSBrijesh Singh #include <asm/msr-index.h> 20e759959fSBrijesh Singh #include <asm/fpu/xcr.h> 21e759959fSBrijesh Singh #include <asm/ptrace.h> 22e759959fSBrijesh Singh #include <asm/svm.h> 23801baa69SMichael Roth #include <asm/cpuid.h> 24e759959fSBrijesh Singh 25e759959fSBrijesh Singh #include "error.h" 26950d0055SMichael Roth #include "../msr.h" 27e759959fSBrijesh Singh 28e759959fSBrijesh Singh struct ghcb boot_ghcb_page __aligned(PAGE_SIZE); 29e759959fSBrijesh Singh struct ghcb *boot_ghcb; 30e759959fSBrijesh Singh 31e759959fSBrijesh Singh /* 32e759959fSBrijesh Singh * Copy a version of this function here - insn-eval.c can't be used in 33e759959fSBrijesh Singh * pre-decompression code. 34e759959fSBrijesh Singh */ 35e759959fSBrijesh Singh static bool insn_has_rep_prefix(struct insn *insn) 36e759959fSBrijesh Singh { 37e759959fSBrijesh Singh insn_byte_t p; 38e759959fSBrijesh Singh int i; 39e759959fSBrijesh Singh 40e759959fSBrijesh Singh insn_get_prefixes(insn); 41e759959fSBrijesh Singh 42e759959fSBrijesh Singh for_each_insn_prefix(insn, i, p) { 43e759959fSBrijesh Singh if (p == 0xf2 || p == 0xf3) 44e759959fSBrijesh Singh return true; 45e759959fSBrijesh Singh } 46e759959fSBrijesh Singh 47e759959fSBrijesh Singh return false; 48e759959fSBrijesh Singh } 49e759959fSBrijesh Singh 50e759959fSBrijesh Singh /* 51e759959fSBrijesh Singh * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and 52e759959fSBrijesh Singh * doesn't use segments. 53e759959fSBrijesh Singh */ 54e759959fSBrijesh Singh static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx) 55e759959fSBrijesh Singh { 56e759959fSBrijesh Singh return 0UL; 57e759959fSBrijesh Singh } 58e759959fSBrijesh Singh 59e759959fSBrijesh Singh static inline u64 sev_es_rd_ghcb_msr(void) 60e759959fSBrijesh Singh { 61950d0055SMichael Roth struct msr m; 62e759959fSBrijesh Singh 63950d0055SMichael Roth boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m); 64e759959fSBrijesh Singh 65950d0055SMichael Roth return m.q; 66e759959fSBrijesh Singh } 67e759959fSBrijesh Singh 68e759959fSBrijesh Singh static inline void sev_es_wr_ghcb_msr(u64 val) 69e759959fSBrijesh Singh { 70950d0055SMichael Roth struct msr m; 71e759959fSBrijesh Singh 72950d0055SMichael Roth m.q = val; 73950d0055SMichael Roth boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m); 74e759959fSBrijesh Singh } 75e759959fSBrijesh Singh 76e759959fSBrijesh Singh static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) 77e759959fSBrijesh Singh { 78e759959fSBrijesh Singh char buffer[MAX_INSN_SIZE]; 79e759959fSBrijesh Singh int ret; 80e759959fSBrijesh Singh 81e759959fSBrijesh Singh memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE); 82e759959fSBrijesh Singh 83e759959fSBrijesh Singh ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64); 84e759959fSBrijesh Singh if (ret < 0) 85e759959fSBrijesh Singh return ES_DECODE_FAILED; 86e759959fSBrijesh Singh 87e759959fSBrijesh Singh return ES_OK; 88e759959fSBrijesh Singh } 89e759959fSBrijesh Singh 90e759959fSBrijesh Singh static enum es_result vc_write_mem(struct es_em_ctxt *ctxt, 91e759959fSBrijesh Singh void *dst, char *buf, size_t size) 92e759959fSBrijesh Singh { 93e759959fSBrijesh Singh memcpy(dst, buf, size); 94e759959fSBrijesh Singh 95e759959fSBrijesh Singh return ES_OK; 96e759959fSBrijesh Singh } 97e759959fSBrijesh Singh 98e759959fSBrijesh Singh static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, 99e759959fSBrijesh Singh void *src, char *buf, size_t size) 100e759959fSBrijesh Singh { 101e759959fSBrijesh Singh memcpy(buf, src, size); 102e759959fSBrijesh Singh 103e759959fSBrijesh Singh return ES_OK; 104e759959fSBrijesh Singh } 105e759959fSBrijesh Singh 106b9cb9c45SJoerg Roedel static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size) 107b9cb9c45SJoerg Roedel { 108b9cb9c45SJoerg Roedel return ES_OK; 109b9cb9c45SJoerg Roedel } 110b9cb9c45SJoerg Roedel 111*63e44bc5SJoerg Roedel static bool fault_in_kernel_space(unsigned long address) 112*63e44bc5SJoerg Roedel { 113*63e44bc5SJoerg Roedel return false; 114*63e44bc5SJoerg Roedel } 115*63e44bc5SJoerg Roedel 116e759959fSBrijesh Singh #undef __init 117e759959fSBrijesh Singh #define __init 118e759959fSBrijesh Singh 119e759959fSBrijesh Singh #define __BOOT_COMPRESSED 120e759959fSBrijesh Singh 121e759959fSBrijesh Singh /* Basic instruction decoding support needed */ 122e759959fSBrijesh Singh #include "../../lib/inat.c" 123e759959fSBrijesh Singh #include "../../lib/insn.c" 124e759959fSBrijesh Singh 125e759959fSBrijesh Singh /* Include code for early handlers */ 126e759959fSBrijesh Singh #include "../../kernel/sev-shared.c" 127e759959fSBrijesh Singh 1286c321179STom Lendacky bool sev_snp_enabled(void) 1294f9c403eSBrijesh Singh { 1304f9c403eSBrijesh Singh return sev_status & MSR_AMD64_SEV_SNP_ENABLED; 1314f9c403eSBrijesh Singh } 1324f9c403eSBrijesh Singh 1334f9c403eSBrijesh Singh static void __page_state_change(unsigned long paddr, enum psc_op op) 1344f9c403eSBrijesh Singh { 1354f9c403eSBrijesh Singh u64 val; 1364f9c403eSBrijesh Singh 1374f9c403eSBrijesh Singh if (!sev_snp_enabled()) 1384f9c403eSBrijesh Singh return; 1394f9c403eSBrijesh Singh 1404f9c403eSBrijesh Singh /* 1414f9c403eSBrijesh Singh * If private -> shared then invalidate the page before requesting the 1424f9c403eSBrijesh Singh * state change in the RMP table. 1434f9c403eSBrijesh Singh */ 1444f9c403eSBrijesh Singh if (op == SNP_PAGE_STATE_SHARED && pvalidate(paddr, RMP_PG_SIZE_4K, 0)) 1454f9c403eSBrijesh Singh sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); 1464f9c403eSBrijesh Singh 1474f9c403eSBrijesh Singh /* Issue VMGEXIT to change the page state in RMP table. */ 1484f9c403eSBrijesh Singh sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op)); 1494f9c403eSBrijesh Singh VMGEXIT(); 1504f9c403eSBrijesh Singh 1514f9c403eSBrijesh Singh /* Read the response of the VMGEXIT. */ 1524f9c403eSBrijesh Singh val = sev_es_rd_ghcb_msr(); 1534f9c403eSBrijesh Singh if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val)) 1544f9c403eSBrijesh Singh sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); 1554f9c403eSBrijesh Singh 1564f9c403eSBrijesh Singh /* 1574f9c403eSBrijesh Singh * Now that page state is changed in the RMP table, validate it so that it is 1584f9c403eSBrijesh Singh * consistent with the RMP entry. 1594f9c403eSBrijesh Singh */ 1604f9c403eSBrijesh Singh if (op == SNP_PAGE_STATE_PRIVATE && pvalidate(paddr, RMP_PG_SIZE_4K, 1)) 1614f9c403eSBrijesh Singh sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); 1624f9c403eSBrijesh Singh } 1634f9c403eSBrijesh Singh 1644f9c403eSBrijesh Singh void snp_set_page_private(unsigned long paddr) 1654f9c403eSBrijesh Singh { 1664f9c403eSBrijesh Singh __page_state_change(paddr, SNP_PAGE_STATE_PRIVATE); 1674f9c403eSBrijesh Singh } 1684f9c403eSBrijesh Singh 1694f9c403eSBrijesh Singh void snp_set_page_shared(unsigned long paddr) 1704f9c403eSBrijesh Singh { 1714f9c403eSBrijesh Singh __page_state_change(paddr, SNP_PAGE_STATE_SHARED); 1724f9c403eSBrijesh Singh } 1734f9c403eSBrijesh Singh 174cbd3d4f7SBrijesh Singh static bool early_setup_ghcb(void) 175e759959fSBrijesh Singh { 176e759959fSBrijesh Singh if (set_page_decrypted((unsigned long)&boot_ghcb_page)) 177e759959fSBrijesh Singh return false; 178e759959fSBrijesh Singh 179e759959fSBrijesh Singh /* Page is now mapped decrypted, clear it */ 180e759959fSBrijesh Singh memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page)); 181e759959fSBrijesh Singh 182e759959fSBrijesh Singh boot_ghcb = &boot_ghcb_page; 183e759959fSBrijesh Singh 184e759959fSBrijesh Singh /* Initialize lookup tables for the instruction decoder */ 185e759959fSBrijesh Singh inat_init_tables(); 186e759959fSBrijesh Singh 18787294bdbSBrijesh Singh /* SNP guest requires the GHCB GPA must be registered */ 18887294bdbSBrijesh Singh if (sev_snp_enabled()) 18987294bdbSBrijesh Singh snp_register_ghcb_early(__pa(&boot_ghcb_page)); 19087294bdbSBrijesh Singh 191e759959fSBrijesh Singh return true; 192e759959fSBrijesh Singh } 193e759959fSBrijesh Singh 1946c321179STom Lendacky static phys_addr_t __snp_accept_memory(struct snp_psc_desc *desc, 1956c321179STom Lendacky phys_addr_t pa, phys_addr_t pa_end) 1966c321179STom Lendacky { 1976c321179STom Lendacky struct psc_hdr *hdr; 1986c321179STom Lendacky struct psc_entry *e; 1996c321179STom Lendacky unsigned int i; 2006c321179STom Lendacky 2016c321179STom Lendacky hdr = &desc->hdr; 2026c321179STom Lendacky memset(hdr, 0, sizeof(*hdr)); 2036c321179STom Lendacky 2046c321179STom Lendacky e = desc->entries; 2056c321179STom Lendacky 2066c321179STom Lendacky i = 0; 2076c321179STom Lendacky while (pa < pa_end && i < VMGEXIT_PSC_MAX_ENTRY) { 2086c321179STom Lendacky hdr->end_entry = i; 2096c321179STom Lendacky 2106c321179STom Lendacky e->gfn = pa >> PAGE_SHIFT; 2116c321179STom Lendacky e->operation = SNP_PAGE_STATE_PRIVATE; 2126c321179STom Lendacky if (IS_ALIGNED(pa, PMD_SIZE) && (pa_end - pa) >= PMD_SIZE) { 2136c321179STom Lendacky e->pagesize = RMP_PG_SIZE_2M; 2146c321179STom Lendacky pa += PMD_SIZE; 2156c321179STom Lendacky } else { 2166c321179STom Lendacky e->pagesize = RMP_PG_SIZE_4K; 2176c321179STom Lendacky pa += PAGE_SIZE; 2186c321179STom Lendacky } 2196c321179STom Lendacky 2206c321179STom Lendacky e++; 2216c321179STom Lendacky i++; 2226c321179STom Lendacky } 2236c321179STom Lendacky 2246c321179STom Lendacky if (vmgexit_psc(boot_ghcb, desc)) 2256c321179STom Lendacky sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); 2266c321179STom Lendacky 2276c321179STom Lendacky pvalidate_pages(desc); 2286c321179STom Lendacky 2296c321179STom Lendacky return pa; 2306c321179STom Lendacky } 2316c321179STom Lendacky 2326c321179STom Lendacky void snp_accept_memory(phys_addr_t start, phys_addr_t end) 2336c321179STom Lendacky { 2346c321179STom Lendacky struct snp_psc_desc desc = {}; 2356c321179STom Lendacky unsigned int i; 2366c321179STom Lendacky phys_addr_t pa; 2376c321179STom Lendacky 2386c321179STom Lendacky if (!boot_ghcb && !early_setup_ghcb()) 2396c321179STom Lendacky sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); 2406c321179STom Lendacky 2416c321179STom Lendacky pa = start; 2426c321179STom Lendacky while (pa < end) 2436c321179STom Lendacky pa = __snp_accept_memory(&desc, pa, end); 2446c321179STom Lendacky } 2456c321179STom Lendacky 246e759959fSBrijesh Singh void sev_es_shutdown_ghcb(void) 247e759959fSBrijesh Singh { 248e759959fSBrijesh Singh if (!boot_ghcb) 249e759959fSBrijesh Singh return; 250e759959fSBrijesh Singh 251e759959fSBrijesh Singh if (!sev_es_check_cpu_features()) 252e759959fSBrijesh Singh error("SEV-ES CPU Features missing."); 253e759959fSBrijesh Singh 254e759959fSBrijesh Singh /* 255e759959fSBrijesh Singh * GHCB Page must be flushed from the cache and mapped encrypted again. 256e759959fSBrijesh Singh * Otherwise the running kernel will see strange cache effects when 257e759959fSBrijesh Singh * trying to use that page. 258e759959fSBrijesh Singh */ 259e759959fSBrijesh Singh if (set_page_encrypted((unsigned long)&boot_ghcb_page)) 260e759959fSBrijesh Singh error("Can't map GHCB page encrypted"); 261e759959fSBrijesh Singh 262e759959fSBrijesh Singh /* 263e759959fSBrijesh Singh * GHCB page is mapped encrypted again and flushed from the cache. 264e759959fSBrijesh Singh * Mark it non-present now to catch bugs when #VC exceptions trigger 265e759959fSBrijesh Singh * after this point. 266e759959fSBrijesh Singh */ 267e759959fSBrijesh Singh if (set_page_non_present((unsigned long)&boot_ghcb_page)) 268e759959fSBrijesh Singh error("Can't unmap GHCB page"); 269e759959fSBrijesh Singh } 270e759959fSBrijesh Singh 2718c29f016SNikunj A Dadhania static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set, 2728c29f016SNikunj A Dadhania unsigned int reason, u64 exit_info_2) 2738c29f016SNikunj A Dadhania { 2748c29f016SNikunj A Dadhania u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason); 2758c29f016SNikunj A Dadhania 2768c29f016SNikunj A Dadhania vc_ghcb_invalidate(ghcb); 2778c29f016SNikunj A Dadhania ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST); 2788c29f016SNikunj A Dadhania ghcb_set_sw_exit_info_1(ghcb, exit_info_1); 2798c29f016SNikunj A Dadhania ghcb_set_sw_exit_info_2(ghcb, exit_info_2); 2808c29f016SNikunj A Dadhania 2818c29f016SNikunj A Dadhania sev_es_wr_ghcb_msr(__pa(ghcb)); 2828c29f016SNikunj A Dadhania VMGEXIT(); 2838c29f016SNikunj A Dadhania 2848c29f016SNikunj A Dadhania while (true) 2858c29f016SNikunj A Dadhania asm volatile("hlt\n" : : : "memory"); 2868c29f016SNikunj A Dadhania } 2878c29f016SNikunj A Dadhania 288e759959fSBrijesh Singh bool sev_es_check_ghcb_fault(unsigned long address) 289e759959fSBrijesh Singh { 290e759959fSBrijesh Singh /* Check whether the fault was on the GHCB page */ 291e759959fSBrijesh Singh return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page); 292e759959fSBrijesh Singh } 293e759959fSBrijesh Singh 294e759959fSBrijesh Singh void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) 295e759959fSBrijesh Singh { 296e759959fSBrijesh Singh struct es_em_ctxt ctxt; 297e759959fSBrijesh Singh enum es_result result; 298e759959fSBrijesh Singh 299cbd3d4f7SBrijesh Singh if (!boot_ghcb && !early_setup_ghcb()) 3006c0f74d6SBrijesh Singh sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); 301e759959fSBrijesh Singh 302e759959fSBrijesh Singh vc_ghcb_invalidate(boot_ghcb); 303e759959fSBrijesh Singh result = vc_init_em_ctxt(&ctxt, regs, exit_code); 304e759959fSBrijesh Singh if (result != ES_OK) 305e759959fSBrijesh Singh goto finish; 306e759959fSBrijesh Singh 307e759959fSBrijesh Singh switch (exit_code) { 308e759959fSBrijesh Singh case SVM_EXIT_RDTSC: 309e759959fSBrijesh Singh case SVM_EXIT_RDTSCP: 310e759959fSBrijesh Singh result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code); 311e759959fSBrijesh Singh break; 312e759959fSBrijesh Singh case SVM_EXIT_IOIO: 313e759959fSBrijesh Singh result = vc_handle_ioio(boot_ghcb, &ctxt); 314e759959fSBrijesh Singh break; 315e759959fSBrijesh Singh case SVM_EXIT_CPUID: 316e759959fSBrijesh Singh result = vc_handle_cpuid(boot_ghcb, &ctxt); 317e759959fSBrijesh Singh break; 318e759959fSBrijesh Singh default: 319e759959fSBrijesh Singh result = ES_UNSUPPORTED; 320e759959fSBrijesh Singh break; 321e759959fSBrijesh Singh } 322e759959fSBrijesh Singh 323e759959fSBrijesh Singh finish: 324e759959fSBrijesh Singh if (result == ES_OK) 325e759959fSBrijesh Singh vc_finish_insn(&ctxt); 326e759959fSBrijesh Singh else if (result != ES_RETRY) 3276c0f74d6SBrijesh Singh sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); 328e759959fSBrijesh Singh } 329ec1c66afSMichael Roth 33081cc3df9SBrijesh Singh static void enforce_vmpl0(void) 33181cc3df9SBrijesh Singh { 33281cc3df9SBrijesh Singh u64 attrs; 33381cc3df9SBrijesh Singh int err; 33481cc3df9SBrijesh Singh 33581cc3df9SBrijesh Singh /* 33681cc3df9SBrijesh Singh * RMPADJUST modifies RMP permissions of a lesser-privileged (numerically 33781cc3df9SBrijesh Singh * higher) privilege level. Here, clear the VMPL1 permission mask of the 33881cc3df9SBrijesh Singh * GHCB page. If the guest is not running at VMPL0, this will fail. 33981cc3df9SBrijesh Singh * 34081cc3df9SBrijesh Singh * If the guest is running at VMPL0, it will succeed. Even if that operation 34181cc3df9SBrijesh Singh * modifies permission bits, it is still ok to do so currently because Linux 34281cc3df9SBrijesh Singh * SNP guests are supported only on VMPL0 so VMPL1 or higher permission masks 34381cc3df9SBrijesh Singh * changing is a don't-care. 34481cc3df9SBrijesh Singh */ 34581cc3df9SBrijesh Singh attrs = 1; 34681cc3df9SBrijesh Singh if (rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, attrs)) 34781cc3df9SBrijesh Singh sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0); 34881cc3df9SBrijesh Singh } 34981cc3df9SBrijesh Singh 3508c29f016SNikunj A Dadhania /* 3518c29f016SNikunj A Dadhania * SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need 3528c29f016SNikunj A Dadhania * guest side implementation for proper functioning of the guest. If any 3538c29f016SNikunj A Dadhania * of these features are enabled in the hypervisor but are lacking guest 3548c29f016SNikunj A Dadhania * side implementation, the behavior of the guest will be undefined. The 3558c29f016SNikunj A Dadhania * guest could fail in non-obvious way making it difficult to debug. 3568c29f016SNikunj A Dadhania * 3578c29f016SNikunj A Dadhania * As the behavior of reserved feature bits is unknown to be on the 3588c29f016SNikunj A Dadhania * safe side add them to the required features mask. 3598c29f016SNikunj A Dadhania */ 3608c29f016SNikunj A Dadhania #define SNP_FEATURES_IMPL_REQ (MSR_AMD64_SNP_VTOM | \ 3618c29f016SNikunj A Dadhania MSR_AMD64_SNP_REFLECT_VC | \ 3628c29f016SNikunj A Dadhania MSR_AMD64_SNP_RESTRICTED_INJ | \ 3638c29f016SNikunj A Dadhania MSR_AMD64_SNP_ALT_INJ | \ 3648c29f016SNikunj A Dadhania MSR_AMD64_SNP_DEBUG_SWAP | \ 3658c29f016SNikunj A Dadhania MSR_AMD64_SNP_VMPL_SSS | \ 3668c29f016SNikunj A Dadhania MSR_AMD64_SNP_SECURE_TSC | \ 3678c29f016SNikunj A Dadhania MSR_AMD64_SNP_VMGEXIT_PARAM | \ 3688c29f016SNikunj A Dadhania MSR_AMD64_SNP_VMSA_REG_PROTECTION | \ 3698c29f016SNikunj A Dadhania MSR_AMD64_SNP_RESERVED_BIT13 | \ 3708c29f016SNikunj A Dadhania MSR_AMD64_SNP_RESERVED_BIT15 | \ 3718c29f016SNikunj A Dadhania MSR_AMD64_SNP_RESERVED_MASK) 3728c29f016SNikunj A Dadhania 3738c29f016SNikunj A Dadhania /* 3748c29f016SNikunj A Dadhania * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented 3758c29f016SNikunj A Dadhania * by the guest kernel. As and when a new feature is implemented in the 3768c29f016SNikunj A Dadhania * guest kernel, a corresponding bit should be added to the mask. 3778c29f016SNikunj A Dadhania */ 378e221804dSAlexey Kardashevskiy #define SNP_FEATURES_PRESENT MSR_AMD64_SNP_DEBUG_SWAP 3798c29f016SNikunj A Dadhania 38031c77a50SArd Biesheuvel u64 snp_get_unsupported_features(u64 status) 38131c77a50SArd Biesheuvel { 38231c77a50SArd Biesheuvel if (!(status & MSR_AMD64_SEV_SNP_ENABLED)) 38331c77a50SArd Biesheuvel return 0; 38431c77a50SArd Biesheuvel 38531c77a50SArd Biesheuvel return status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT; 38631c77a50SArd Biesheuvel } 38731c77a50SArd Biesheuvel 3888c29f016SNikunj A Dadhania void snp_check_features(void) 3898c29f016SNikunj A Dadhania { 3908c29f016SNikunj A Dadhania u64 unsupported; 3918c29f016SNikunj A Dadhania 3928c29f016SNikunj A Dadhania /* 3938c29f016SNikunj A Dadhania * Terminate the boot if hypervisor has enabled any feature lacking 3948c29f016SNikunj A Dadhania * guest side implementation. Pass on the unsupported features mask through 3958c29f016SNikunj A Dadhania * EXIT_INFO_2 of the GHCB protocol so that those features can be reported 3968c29f016SNikunj A Dadhania * as part of the guest boot failure. 3978c29f016SNikunj A Dadhania */ 39831c77a50SArd Biesheuvel unsupported = snp_get_unsupported_features(sev_status); 3998c29f016SNikunj A Dadhania if (unsupported) { 4008c29f016SNikunj A Dadhania if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb())) 4018c29f016SNikunj A Dadhania sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); 4028c29f016SNikunj A Dadhania 4038c29f016SNikunj A Dadhania sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN, 4048c29f016SNikunj A Dadhania GHCB_SNP_UNSUPPORTED, unsupported); 4058c29f016SNikunj A Dadhania } 4068c29f016SNikunj A Dadhania } 4078c29f016SNikunj A Dadhania 40831c77a50SArd Biesheuvel /* 40931c77a50SArd Biesheuvel * sev_check_cpu_support - Check for SEV support in the CPU capabilities 41031c77a50SArd Biesheuvel * 41131c77a50SArd Biesheuvel * Returns < 0 if SEV is not supported, otherwise the position of the 41231c77a50SArd Biesheuvel * encryption bit in the page table descriptors. 41331c77a50SArd Biesheuvel */ 41431c77a50SArd Biesheuvel static int sev_check_cpu_support(void) 415ec1c66afSMichael Roth { 416ec1c66afSMichael Roth unsigned int eax, ebx, ecx, edx; 41731c77a50SArd Biesheuvel 41831c77a50SArd Biesheuvel /* Check for the SME/SEV support leaf */ 41931c77a50SArd Biesheuvel eax = 0x80000000; 42031c77a50SArd Biesheuvel ecx = 0; 42131c77a50SArd Biesheuvel native_cpuid(&eax, &ebx, &ecx, &edx); 42231c77a50SArd Biesheuvel if (eax < 0x8000001f) 42331c77a50SArd Biesheuvel return -ENODEV; 42431c77a50SArd Biesheuvel 42531c77a50SArd Biesheuvel /* 42631c77a50SArd Biesheuvel * Check for the SME/SEV feature: 42731c77a50SArd Biesheuvel * CPUID Fn8000_001F[EAX] 42831c77a50SArd Biesheuvel * - Bit 0 - Secure Memory Encryption support 42931c77a50SArd Biesheuvel * - Bit 1 - Secure Encrypted Virtualization support 43031c77a50SArd Biesheuvel * CPUID Fn8000_001F[EBX] 43131c77a50SArd Biesheuvel * - Bits 5:0 - Pagetable bit position used to indicate encryption 43231c77a50SArd Biesheuvel */ 43331c77a50SArd Biesheuvel eax = 0x8000001f; 43431c77a50SArd Biesheuvel ecx = 0; 43531c77a50SArd Biesheuvel native_cpuid(&eax, &ebx, &ecx, &edx); 43631c77a50SArd Biesheuvel /* Check whether SEV is supported */ 43731c77a50SArd Biesheuvel if (!(eax & BIT(1))) 43831c77a50SArd Biesheuvel return -ENODEV; 43931c77a50SArd Biesheuvel 44031c77a50SArd Biesheuvel return ebx & 0x3f; 44131c77a50SArd Biesheuvel } 44231c77a50SArd Biesheuvel 44331c77a50SArd Biesheuvel void sev_enable(struct boot_params *bp) 44431c77a50SArd Biesheuvel { 445ec1c66afSMichael Roth struct msr m; 44631c77a50SArd Biesheuvel int bitpos; 447c01fce9cSMichael Roth bool snp; 448c01fce9cSMichael Roth 449c01fce9cSMichael Roth /* 4504b1c7424SMichael Roth * bp->cc_blob_address should only be set by boot/compressed kernel. 4514b1c7424SMichael Roth * Initialize it to 0 to ensure that uninitialized values from 4524b1c7424SMichael Roth * buggy bootloaders aren't propagated. 4534b1c7424SMichael Roth */ 4544b1c7424SMichael Roth if (bp) 4554b1c7424SMichael Roth bp->cc_blob_address = 0; 4564b1c7424SMichael Roth 4574b1c7424SMichael Roth /* 458bee6cf1aSBorislav Petkov (AMD) * Do an initial SEV capability check before snp_init() which 459bee6cf1aSBorislav Petkov (AMD) * loads the CPUID page and the same checks afterwards are done 460bee6cf1aSBorislav Petkov (AMD) * without the hypervisor and are trustworthy. 461bee6cf1aSBorislav Petkov (AMD) * 462bee6cf1aSBorislav Petkov (AMD) * If the HV fakes SEV support, the guest will crash'n'burn 463bee6cf1aSBorislav Petkov (AMD) * which is good enough. 464c01fce9cSMichael Roth */ 465ec1c66afSMichael Roth 46631c77a50SArd Biesheuvel if (sev_check_cpu_support() < 0) 467bee6cf1aSBorislav Petkov (AMD) return; 468bee6cf1aSBorislav Petkov (AMD) 469bee6cf1aSBorislav Petkov (AMD) /* 470bee6cf1aSBorislav Petkov (AMD) * Setup/preliminary detection of SNP. This will be sanity-checked 471bee6cf1aSBorislav Petkov (AMD) * against CPUID/MSR values later. 472bee6cf1aSBorislav Petkov (AMD) */ 473bee6cf1aSBorislav Petkov (AMD) snp = snp_init(bp); 474bee6cf1aSBorislav Petkov (AMD) 475bee6cf1aSBorislav Petkov (AMD) /* Now repeat the checks with the SNP CPUID table. */ 476bee6cf1aSBorislav Petkov (AMD) 47731c77a50SArd Biesheuvel bitpos = sev_check_cpu_support(); 47831c77a50SArd Biesheuvel if (bitpos < 0) { 479c01fce9cSMichael Roth if (snp) 480c01fce9cSMichael Roth error("SEV-SNP support indicated by CC blob, but not CPUID."); 481ec1c66afSMichael Roth return; 482c01fce9cSMichael Roth } 483ec1c66afSMichael Roth 484ec1c66afSMichael Roth /* Set the SME mask if this is an SEV guest. */ 485ec1c66afSMichael Roth boot_rdmsr(MSR_AMD64_SEV, &m); 486ec1c66afSMichael Roth sev_status = m.q; 487ec1c66afSMichael Roth if (!(sev_status & MSR_AMD64_SEV_ENABLED)) 488ec1c66afSMichael Roth return; 489ec1c66afSMichael Roth 490cbd3d4f7SBrijesh Singh /* Negotiate the GHCB protocol version. */ 491cbd3d4f7SBrijesh Singh if (sev_status & MSR_AMD64_SEV_ES_ENABLED) { 492cbd3d4f7SBrijesh Singh if (!sev_es_negotiate_protocol()) 493cbd3d4f7SBrijesh Singh sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED); 494cbd3d4f7SBrijesh Singh } 495cbd3d4f7SBrijesh Singh 496cbd3d4f7SBrijesh Singh /* 497cbd3d4f7SBrijesh Singh * SNP is supported in v2 of the GHCB spec which mandates support for HV 498cbd3d4f7SBrijesh Singh * features. 499cbd3d4f7SBrijesh Singh */ 50081cc3df9SBrijesh Singh if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) { 50181cc3df9SBrijesh Singh if (!(get_hv_features() & GHCB_HV_FT_SNP)) 502cbd3d4f7SBrijesh Singh sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); 503cbd3d4f7SBrijesh Singh 50481cc3df9SBrijesh Singh enforce_vmpl0(); 50581cc3df9SBrijesh Singh } 50681cc3df9SBrijesh Singh 507c01fce9cSMichael Roth if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED)) 508c01fce9cSMichael Roth error("SEV-SNP supported indicated by CC blob, but not SEV status MSR."); 509c01fce9cSMichael Roth 51031c77a50SArd Biesheuvel sme_me_mask = BIT_ULL(bitpos); 51131c77a50SArd Biesheuvel } 51231c77a50SArd Biesheuvel 51331c77a50SArd Biesheuvel /* 51431c77a50SArd Biesheuvel * sev_get_status - Retrieve the SEV status mask 51531c77a50SArd Biesheuvel * 51631c77a50SArd Biesheuvel * Returns 0 if the CPU is not SEV capable, otherwise the value of the 51731c77a50SArd Biesheuvel * AMD64_SEV MSR. 51831c77a50SArd Biesheuvel */ 51931c77a50SArd Biesheuvel u64 sev_get_status(void) 52031c77a50SArd Biesheuvel { 52131c77a50SArd Biesheuvel struct msr m; 52231c77a50SArd Biesheuvel 52331c77a50SArd Biesheuvel if (sev_check_cpu_support() < 0) 52431c77a50SArd Biesheuvel return 0; 52531c77a50SArd Biesheuvel 52631c77a50SArd Biesheuvel boot_rdmsr(MSR_AMD64_SEV, &m); 52731c77a50SArd Biesheuvel return m.q; 528ec1c66afSMichael Roth } 529c01fce9cSMichael Roth 530c01fce9cSMichael Roth /* Search for Confidential Computing blob in the EFI config table. */ 531c01fce9cSMichael Roth static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp) 532c01fce9cSMichael Roth { 533c01fce9cSMichael Roth unsigned long cfg_table_pa; 534c01fce9cSMichael Roth unsigned int cfg_table_len; 535c01fce9cSMichael Roth int ret; 536c01fce9cSMichael Roth 537c01fce9cSMichael Roth ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len); 538c01fce9cSMichael Roth if (ret) 539c01fce9cSMichael Roth return NULL; 540c01fce9cSMichael Roth 541c01fce9cSMichael Roth return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa, 542c01fce9cSMichael Roth cfg_table_len, 543c01fce9cSMichael Roth EFI_CC_BLOB_GUID); 544c01fce9cSMichael Roth } 545c01fce9cSMichael Roth 546c01fce9cSMichael Roth /* 547c01fce9cSMichael Roth * Initial set up of SNP relies on information provided by the 548c01fce9cSMichael Roth * Confidential Computing blob, which can be passed to the boot kernel 549c01fce9cSMichael Roth * by firmware/bootloader in the following ways: 550c01fce9cSMichael Roth * 551c01fce9cSMichael Roth * - via an entry in the EFI config table 552c01fce9cSMichael Roth * - via a setup_data structure, as defined by the Linux Boot Protocol 553c01fce9cSMichael Roth * 554c01fce9cSMichael Roth * Scan for the blob in that order. 555c01fce9cSMichael Roth */ 556c01fce9cSMichael Roth static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp) 557c01fce9cSMichael Roth { 558c01fce9cSMichael Roth struct cc_blob_sev_info *cc_info; 559c01fce9cSMichael Roth 560c01fce9cSMichael Roth cc_info = find_cc_blob_efi(bp); 561c01fce9cSMichael Roth if (cc_info) 562c01fce9cSMichael Roth goto found_cc_info; 563c01fce9cSMichael Roth 564c01fce9cSMichael Roth cc_info = find_cc_blob_setup_data(bp); 565c01fce9cSMichael Roth if (!cc_info) 566c01fce9cSMichael Roth return NULL; 567c01fce9cSMichael Roth 568c01fce9cSMichael Roth found_cc_info: 569c01fce9cSMichael Roth if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC) 570c01fce9cSMichael Roth sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); 571c01fce9cSMichael Roth 572c01fce9cSMichael Roth return cc_info; 573c01fce9cSMichael Roth } 574c01fce9cSMichael Roth 575c01fce9cSMichael Roth /* 576c01fce9cSMichael Roth * Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks 577c01fce9cSMichael Roth * will verify the SNP CPUID/MSR bits. 578c01fce9cSMichael Roth */ 579c01fce9cSMichael Roth bool snp_init(struct boot_params *bp) 580c01fce9cSMichael Roth { 581c01fce9cSMichael Roth struct cc_blob_sev_info *cc_info; 582c01fce9cSMichael Roth 583c01fce9cSMichael Roth if (!bp) 584c01fce9cSMichael Roth return false; 585c01fce9cSMichael Roth 586c01fce9cSMichael Roth cc_info = find_cc_blob(bp); 587c01fce9cSMichael Roth if (!cc_info) 588c01fce9cSMichael Roth return false; 589c01fce9cSMichael Roth 590c01fce9cSMichael Roth /* 5915f211f4fSMichael Roth * If a SNP-specific Confidential Computing blob is present, then 5925f211f4fSMichael Roth * firmware/bootloader have indicated SNP support. Verifying this 5935f211f4fSMichael Roth * involves CPUID checks which will be more reliable if the SNP 5945f211f4fSMichael Roth * CPUID table is used. See comments over snp_setup_cpuid_table() for 5955f211f4fSMichael Roth * more details. 5965f211f4fSMichael Roth */ 5975f211f4fSMichael Roth setup_cpuid_table(cc_info); 5985f211f4fSMichael Roth 5995f211f4fSMichael Roth /* 600c01fce9cSMichael Roth * Pass run-time kernel a pointer to CC info via boot_params so EFI 601c01fce9cSMichael Roth * config table doesn't need to be searched again during early startup 602c01fce9cSMichael Roth * phase. 603c01fce9cSMichael Roth */ 604c01fce9cSMichael Roth bp->cc_blob_address = (u32)(unsigned long)cc_info; 605c01fce9cSMichael Roth 606c01fce9cSMichael Roth return true; 607c01fce9cSMichael Roth } 60876f61e1eSMichael Roth 60976f61e1eSMichael Roth void sev_prep_identity_maps(unsigned long top_level_pgt) 61076f61e1eSMichael Roth { 61176f61e1eSMichael Roth /* 61276f61e1eSMichael Roth * The Confidential Computing blob is used very early in uncompressed 61376f61e1eSMichael Roth * kernel to find the in-memory CPUID table to handle CPUID 61476f61e1eSMichael Roth * instructions. Make sure an identity-mapping exists so it can be 61576f61e1eSMichael Roth * accessed after switchover. 61676f61e1eSMichael Roth */ 61776f61e1eSMichael Roth if (sev_snp_enabled()) { 61876f61e1eSMichael Roth unsigned long cc_info_pa = boot_params->cc_blob_address; 61976f61e1eSMichael Roth struct cc_blob_sev_info *cc_info; 62076f61e1eSMichael Roth 62176f61e1eSMichael Roth kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info)); 62276f61e1eSMichael Roth 62376f61e1eSMichael Roth cc_info = (struct cc_blob_sev_info *)cc_info_pa; 62476f61e1eSMichael Roth kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len); 62576f61e1eSMichael Roth } 62676f61e1eSMichael Roth 62776f61e1eSMichael Roth sev_verify_cbit(top_level_pgt); 62876f61e1eSMichael Roth } 629