// SPDX-License-Identifier: GPL-2.01/*2* AMD Encrypted Register State Support3*4* Author: Joerg Roedel <[email protected]>5*/67/*8* misc.h needs to be first because it knows how to include the other kernel9* headers in the pre-decompression code in a way that does not break10* compilation.11*/12#include "misc.h"1314#include <asm/bootparam.h>15#include <asm/pgtable_types.h>16#include <asm/sev.h>17#include <asm/trapnr.h>18#include <asm/trap_pf.h>19#include <asm/msr-index.h>20#include <asm/fpu/xcr.h>21#include <asm/ptrace.h>22#include <asm/svm.h>23#include <asm/cpuid/api.h>2425#include "error.h"26#include "sev.h"2728static struct ghcb boot_ghcb_page __aligned(PAGE_SIZE);29struct ghcb *boot_ghcb;3031#undef __init32#define __init3334#define __BOOT_COMPRESSED3536u8 snp_vmpl;37u16 ghcb_version;3839u64 boot_svsm_caa_pa;4041/* Include code for early handlers */42#include "../../boot/startup/sev-shared.c"4344static bool sev_snp_enabled(void)45{46return sev_status & MSR_AMD64_SEV_SNP_ENABLED;47}4849void snp_set_page_private(unsigned long paddr)50{51struct psc_desc d = {52SNP_PAGE_STATE_PRIVATE,53(struct svsm_ca *)boot_svsm_caa_pa,54boot_svsm_caa_pa55};5657if (!sev_snp_enabled())58return;5960__page_state_change(paddr, paddr, &d);61}6263void snp_set_page_shared(unsigned long paddr)64{65struct psc_desc d = {66SNP_PAGE_STATE_SHARED,67(struct svsm_ca *)boot_svsm_caa_pa,68boot_svsm_caa_pa69};7071if (!sev_snp_enabled())72return;7374__page_state_change(paddr, paddr, &d);75}7677bool early_setup_ghcb(void)78{79if (set_page_decrypted((unsigned long)&boot_ghcb_page))80return false;8182/* Page is now mapped decrypted, clear it */83memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));8485boot_ghcb = &boot_ghcb_page;8687/* Initialize lookup tables for the instruction decoder */88sev_insn_decode_init();8990/* SNP guest requires the GHCB GPA must be registered */91if (sev_snp_enabled())92snp_register_ghcb_early(__pa(&boot_ghcb_page));9394return true;95}9697void snp_accept_memory(phys_addr_t start, phys_addr_t end)98{99struct psc_desc d = {100SNP_PAGE_STATE_PRIVATE,101(struct svsm_ca *)boot_svsm_caa_pa,102boot_svsm_caa_pa103};104105for (phys_addr_t pa = start; pa < end; pa += PAGE_SIZE)106__page_state_change(pa, pa, &d);107}108109void sev_es_shutdown_ghcb(void)110{111if (!boot_ghcb)112return;113114if (!sev_es_check_cpu_features())115error("SEV-ES CPU Features missing.");116117/*118* This denotes whether to use the GHCB MSR protocol or the GHCB119* shared page to perform a GHCB request. Since the GHCB page is120* being changed to encrypted, it can't be used to perform GHCB121* requests. Clear the boot_ghcb variable so that the GHCB MSR122* protocol is used to change the GHCB page over to an encrypted123* page.124*/125boot_ghcb = NULL;126127/*128* GHCB Page must be flushed from the cache and mapped encrypted again.129* Otherwise the running kernel will see strange cache effects when130* trying to use that page.131*/132if (set_page_encrypted((unsigned long)&boot_ghcb_page))133error("Can't map GHCB page encrypted");134135/*136* GHCB page is mapped encrypted again and flushed from the cache.137* Mark it non-present now to catch bugs when #VC exceptions trigger138* after this point.139*/140if (set_page_non_present((unsigned long)&boot_ghcb_page))141error("Can't unmap GHCB page");142}143144static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set,145unsigned int reason, u64 exit_info_2)146{147u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason);148149vc_ghcb_invalidate(ghcb);150ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST);151ghcb_set_sw_exit_info_1(ghcb, exit_info_1);152ghcb_set_sw_exit_info_2(ghcb, exit_info_2);153154sev_es_wr_ghcb_msr(__pa(ghcb));155VMGEXIT();156157while (true)158asm volatile("hlt\n" : : : "memory");159}160161bool sev_es_check_ghcb_fault(unsigned long address)162{163/* Check whether the fault was on the GHCB page */164return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);165}166167/*168* SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need169* guest side implementation for proper functioning of the guest. If any170* of these features are enabled in the hypervisor but are lacking guest171* side implementation, the behavior of the guest will be undefined. The172* guest could fail in non-obvious way making it difficult to debug.173*174* As the behavior of reserved feature bits is unknown to be on the175* safe side add them to the required features mask.176*/177#define SNP_FEATURES_IMPL_REQ (MSR_AMD64_SNP_VTOM | \178MSR_AMD64_SNP_REFLECT_VC | \179MSR_AMD64_SNP_RESTRICTED_INJ | \180MSR_AMD64_SNP_ALT_INJ | \181MSR_AMD64_SNP_DEBUG_SWAP | \182MSR_AMD64_SNP_VMPL_SSS | \183MSR_AMD64_SNP_SECURE_TSC | \184MSR_AMD64_SNP_VMGEXIT_PARAM | \185MSR_AMD64_SNP_VMSA_REG_PROT | \186MSR_AMD64_SNP_RESERVED_BIT13 | \187MSR_AMD64_SNP_RESERVED_BIT15 | \188MSR_AMD64_SNP_SECURE_AVIC | \189MSR_AMD64_SNP_RESERVED_MASK)190191#ifdef CONFIG_AMD_SECURE_AVIC192#define SNP_FEATURE_SECURE_AVIC MSR_AMD64_SNP_SECURE_AVIC193#else194#define SNP_FEATURE_SECURE_AVIC 0195#endif196197/*198* SNP_FEATURES_PRESENT is the mask of SNP features that are implemented199* by the guest kernel. As and when a new feature is implemented in the200* guest kernel, a corresponding bit should be added to the mask.201*/202#define SNP_FEATURES_PRESENT (MSR_AMD64_SNP_DEBUG_SWAP | \203MSR_AMD64_SNP_SECURE_TSC | \204SNP_FEATURE_SECURE_AVIC)205206u64 snp_get_unsupported_features(u64 status)207{208if (!(status & MSR_AMD64_SEV_SNP_ENABLED))209return 0;210211return status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT;212}213214void snp_check_features(void)215{216u64 unsupported;217218/*219* Terminate the boot if hypervisor has enabled any feature lacking220* guest side implementation. Pass on the unsupported features mask through221* EXIT_INFO_2 of the GHCB protocol so that those features can be reported222* as part of the guest boot failure.223*/224unsupported = snp_get_unsupported_features(sev_status);225if (unsupported) {226if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb()))227sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);228229sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN,230GHCB_SNP_UNSUPPORTED, unsupported);231}232}233234/* Search for Confidential Computing blob in the EFI config table. */235static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)236{237unsigned long cfg_table_pa;238unsigned int cfg_table_len;239int ret;240241ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);242if (ret)243return NULL;244245return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,246cfg_table_len,247EFI_CC_BLOB_GUID);248}249250/*251* Initial set up of SNP relies on information provided by the252* Confidential Computing blob, which can be passed to the boot kernel253* by firmware/bootloader in the following ways:254*255* - via an entry in the EFI config table256* - via a setup_data structure, as defined by the Linux Boot Protocol257*258* Scan for the blob in that order.259*/260static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)261{262struct cc_blob_sev_info *cc_info;263264cc_info = find_cc_blob_efi(bp);265if (cc_info)266goto found_cc_info;267268cc_info = find_cc_blob_setup_data(bp);269if (!cc_info)270return NULL;271272found_cc_info:273if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)274sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);275276return cc_info;277}278279/*280* Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks281* will verify the SNP CPUID/MSR bits.282*/283static bool early_snp_init(struct boot_params *bp)284{285struct cc_blob_sev_info *cc_info;286287if (!bp)288return false;289290cc_info = find_cc_blob(bp);291if (!cc_info)292return false;293294/*295* If a SNP-specific Confidential Computing blob is present, then296* firmware/bootloader have indicated SNP support. Verifying this297* involves CPUID checks which will be more reliable if the SNP298* CPUID table is used. See comments over snp_setup_cpuid_table() for299* more details.300*/301setup_cpuid_table(cc_info);302303/*304* Record the SVSM Calling Area (CA) address if the guest is not305* running at VMPL0. The CA will be used to communicate with the306* SVSM and request its services.307*/308svsm_setup_ca(cc_info, rip_rel_ptr(&boot_ghcb_page));309310/*311* Pass run-time kernel a pointer to CC info via boot_params so EFI312* config table doesn't need to be searched again during early startup313* phase.314*/315bp->cc_blob_address = (u32)(unsigned long)cc_info;316317return true;318}319320/*321* sev_check_cpu_support - Check for SEV support in the CPU capabilities322*323* Returns < 0 if SEV is not supported, otherwise the position of the324* encryption bit in the page table descriptors.325*/326static int sev_check_cpu_support(void)327{328unsigned int eax, ebx, ecx, edx;329330/* Check for the SME/SEV support leaf */331eax = 0x80000000;332ecx = 0;333native_cpuid(&eax, &ebx, &ecx, &edx);334if (eax < 0x8000001f)335return -ENODEV;336337/*338* Check for the SME/SEV feature:339* CPUID Fn8000_001F[EAX]340* - Bit 0 - Secure Memory Encryption support341* - Bit 1 - Secure Encrypted Virtualization support342* CPUID Fn8000_001F[EBX]343* - Bits 5:0 - Pagetable bit position used to indicate encryption344*/345eax = 0x8000001f;346ecx = 0;347native_cpuid(&eax, &ebx, &ecx, &edx);348/* Check whether SEV is supported */349if (!(eax & BIT(1)))350return -ENODEV;351352sev_snp_needs_sfw = !(ebx & BIT(31));353354return ebx & 0x3f;355}356357void sev_enable(struct boot_params *bp)358{359struct msr m;360int bitpos;361bool snp;362363/*364* bp->cc_blob_address should only be set by boot/compressed kernel.365* Initialize it to 0 to ensure that uninitialized values from366* buggy bootloaders aren't propagated.367*/368if (bp)369bp->cc_blob_address = 0;370371/*372* Do an initial SEV capability check before early_snp_init() which373* loads the CPUID page and the same checks afterwards are done374* without the hypervisor and are trustworthy.375*376* If the HV fakes SEV support, the guest will crash'n'burn377* which is good enough.378*/379380if (sev_check_cpu_support() < 0)381return;382383/*384* Setup/preliminary detection of SNP. This will be sanity-checked385* against CPUID/MSR values later.386*/387snp = early_snp_init(bp);388389/* Now repeat the checks with the SNP CPUID table. */390391bitpos = sev_check_cpu_support();392if (bitpos < 0) {393if (snp)394error("SEV-SNP support indicated by CC blob, but not CPUID.");395return;396}397398/* Set the SME mask if this is an SEV guest. */399boot_rdmsr(MSR_AMD64_SEV, &m);400sev_status = m.q;401if (!(sev_status & MSR_AMD64_SEV_ENABLED))402return;403404/* Negotiate the GHCB protocol version. */405if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {406if (!sev_es_negotiate_protocol())407sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);408}409410/*411* SNP is supported in v2 of the GHCB spec which mandates support for HV412* features.413*/414if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {415u64 hv_features;416417hv_features = get_hv_features();418if (!(hv_features & GHCB_HV_FT_SNP))419sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);420421/*422* Running at VMPL0 is required unless an SVSM is present and423* the hypervisor supports the required SVSM GHCB events.424*/425if (snp_vmpl && !(hv_features & GHCB_HV_FT_SNP_MULTI_VMPL))426sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);427}428429if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))430error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");431432sme_me_mask = BIT_ULL(bitpos);433}434435/*436* sev_get_status - Retrieve the SEV status mask437*438* Returns 0 if the CPU is not SEV capable, otherwise the value of the439* AMD64_SEV MSR.440*/441u64 sev_get_status(void)442{443struct msr m;444445if (sev_check_cpu_support() < 0)446return 0;447448boot_rdmsr(MSR_AMD64_SEV, &m);449return m.q;450}451452void sev_prep_identity_maps(unsigned long top_level_pgt)453{454/*455* The Confidential Computing blob is used very early in uncompressed456* kernel to find the in-memory CPUID table to handle CPUID457* instructions. Make sure an identity-mapping exists so it can be458* accessed after switchover.459*/460if (sev_snp_enabled()) {461unsigned long cc_info_pa = boot_params_ptr->cc_blob_address;462struct cc_blob_sev_info *cc_info;463464kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));465466cc_info = (struct cc_blob_sev_info *)cc_info_pa;467kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);468}469470sev_verify_cbit(top_level_pgt);471}472473bool early_is_sevsnp_guest(void)474{475static bool sevsnp;476477if (sevsnp)478return true;479480if (!(sev_get_status() & MSR_AMD64_SEV_SNP_ENABLED))481return false;482483sevsnp = true;484485if (!snp_vmpl) {486unsigned int eax, ebx, ecx, edx;487488/*489* CPUID Fn8000_001F_EAX[28] - SVSM support490*/491eax = 0x8000001f;492ecx = 0;493native_cpuid(&eax, &ebx, &ecx, &edx);494if (eax & BIT(28)) {495struct msr m;496497/* Obtain the address of the calling area to use */498boot_rdmsr(MSR_SVSM_CAA, &m);499boot_svsm_caa_pa = m.q;500501/*502* The real VMPL level cannot be discovered, but the503* memory acceptance routines make no use of that so504* any non-zero value suffices here.505*/506snp_vmpl = U8_MAX;507}508}509return true;510}511512513