// 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#undef __head35#define __head3637#define __BOOT_COMPRESSED3839extern struct svsm_ca *boot_svsm_caa;40extern u64 boot_svsm_caa_pa;4142struct svsm_ca *svsm_get_caa(void)43{44return boot_svsm_caa;45}4647u64 svsm_get_caa_pa(void)48{49return boot_svsm_caa_pa;50}5152int svsm_perform_call_protocol(struct svsm_call *call);5354u8 snp_vmpl;5556/* Include code for early handlers */57#include "../../boot/startup/sev-shared.c"5859int svsm_perform_call_protocol(struct svsm_call *call)60{61struct ghcb *ghcb;62int ret;6364if (boot_ghcb)65ghcb = boot_ghcb;66else67ghcb = NULL;6869do {70ret = ghcb ? svsm_perform_ghcb_protocol(ghcb, call)71: svsm_perform_msr_protocol(call);72} while (ret == -EAGAIN);7374return ret;75}7677static bool sev_snp_enabled(void)78{79return sev_status & MSR_AMD64_SEV_SNP_ENABLED;80}8182static void __page_state_change(unsigned long paddr, enum psc_op op)83{84u64 val, msr;8586/*87* If private -> shared then invalidate the page before requesting the88* state change in the RMP table.89*/90if (op == SNP_PAGE_STATE_SHARED)91pvalidate_4k_page(paddr, paddr, false);9293/* Save the current GHCB MSR value */94msr = sev_es_rd_ghcb_msr();9596/* Issue VMGEXIT to change the page state in RMP table. */97sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));98VMGEXIT();99100/* Read the response of the VMGEXIT. */101val = sev_es_rd_ghcb_msr();102if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))103sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);104105/* Restore the GHCB MSR value */106sev_es_wr_ghcb_msr(msr);107108/*109* Now that page state is changed in the RMP table, validate it so that it is110* consistent with the RMP entry.111*/112if (op == SNP_PAGE_STATE_PRIVATE)113pvalidate_4k_page(paddr, paddr, true);114}115116void snp_set_page_private(unsigned long paddr)117{118if (!sev_snp_enabled())119return;120121__page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);122}123124void snp_set_page_shared(unsigned long paddr)125{126if (!sev_snp_enabled())127return;128129__page_state_change(paddr, SNP_PAGE_STATE_SHARED);130}131132bool early_setup_ghcb(void)133{134if (set_page_decrypted((unsigned long)&boot_ghcb_page))135return false;136137/* Page is now mapped decrypted, clear it */138memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));139140boot_ghcb = &boot_ghcb_page;141142/* Initialize lookup tables for the instruction decoder */143sev_insn_decode_init();144145/* SNP guest requires the GHCB GPA must be registered */146if (sev_snp_enabled())147snp_register_ghcb_early(__pa(&boot_ghcb_page));148149return true;150}151152void snp_accept_memory(phys_addr_t start, phys_addr_t end)153{154for (phys_addr_t pa = start; pa < end; pa += PAGE_SIZE)155__page_state_change(pa, SNP_PAGE_STATE_PRIVATE);156}157158void sev_es_shutdown_ghcb(void)159{160if (!boot_ghcb)161return;162163if (!sev_es_check_cpu_features())164error("SEV-ES CPU Features missing.");165166/*167* This denotes whether to use the GHCB MSR protocol or the GHCB168* shared page to perform a GHCB request. Since the GHCB page is169* being changed to encrypted, it can't be used to perform GHCB170* requests. Clear the boot_ghcb variable so that the GHCB MSR171* protocol is used to change the GHCB page over to an encrypted172* page.173*/174boot_ghcb = NULL;175176/*177* GHCB Page must be flushed from the cache and mapped encrypted again.178* Otherwise the running kernel will see strange cache effects when179* trying to use that page.180*/181if (set_page_encrypted((unsigned long)&boot_ghcb_page))182error("Can't map GHCB page encrypted");183184/*185* GHCB page is mapped encrypted again and flushed from the cache.186* Mark it non-present now to catch bugs when #VC exceptions trigger187* after this point.188*/189if (set_page_non_present((unsigned long)&boot_ghcb_page))190error("Can't unmap GHCB page");191}192193static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set,194unsigned int reason, u64 exit_info_2)195{196u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason);197198vc_ghcb_invalidate(ghcb);199ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST);200ghcb_set_sw_exit_info_1(ghcb, exit_info_1);201ghcb_set_sw_exit_info_2(ghcb, exit_info_2);202203sev_es_wr_ghcb_msr(__pa(ghcb));204VMGEXIT();205206while (true)207asm volatile("hlt\n" : : : "memory");208}209210bool sev_es_check_ghcb_fault(unsigned long address)211{212/* Check whether the fault was on the GHCB page */213return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);214}215216/*217* SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need218* guest side implementation for proper functioning of the guest. If any219* of these features are enabled in the hypervisor but are lacking guest220* side implementation, the behavior of the guest will be undefined. The221* guest could fail in non-obvious way making it difficult to debug.222*223* As the behavior of reserved feature bits is unknown to be on the224* safe side add them to the required features mask.225*/226#define SNP_FEATURES_IMPL_REQ (MSR_AMD64_SNP_VTOM | \227MSR_AMD64_SNP_REFLECT_VC | \228MSR_AMD64_SNP_RESTRICTED_INJ | \229MSR_AMD64_SNP_ALT_INJ | \230MSR_AMD64_SNP_DEBUG_SWAP | \231MSR_AMD64_SNP_VMPL_SSS | \232MSR_AMD64_SNP_SECURE_TSC | \233MSR_AMD64_SNP_VMGEXIT_PARAM | \234MSR_AMD64_SNP_VMSA_REG_PROT | \235MSR_AMD64_SNP_RESERVED_BIT13 | \236MSR_AMD64_SNP_RESERVED_BIT15 | \237MSR_AMD64_SNP_RESERVED_MASK)238239/*240* SNP_FEATURES_PRESENT is the mask of SNP features that are implemented241* by the guest kernel. As and when a new feature is implemented in the242* guest kernel, a corresponding bit should be added to the mask.243*/244#define SNP_FEATURES_PRESENT (MSR_AMD64_SNP_DEBUG_SWAP | \245MSR_AMD64_SNP_SECURE_TSC)246247u64 snp_get_unsupported_features(u64 status)248{249if (!(status & MSR_AMD64_SEV_SNP_ENABLED))250return 0;251252return status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT;253}254255void snp_check_features(void)256{257u64 unsupported;258259/*260* Terminate the boot if hypervisor has enabled any feature lacking261* guest side implementation. Pass on the unsupported features mask through262* EXIT_INFO_2 of the GHCB protocol so that those features can be reported263* as part of the guest boot failure.264*/265unsupported = snp_get_unsupported_features(sev_status);266if (unsupported) {267if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb()))268sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);269270sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN,271GHCB_SNP_UNSUPPORTED, unsupported);272}273}274275/* Search for Confidential Computing blob in the EFI config table. */276static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)277{278unsigned long cfg_table_pa;279unsigned int cfg_table_len;280int ret;281282ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);283if (ret)284return NULL;285286return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,287cfg_table_len,288EFI_CC_BLOB_GUID);289}290291/*292* Initial set up of SNP relies on information provided by the293* Confidential Computing blob, which can be passed to the boot kernel294* by firmware/bootloader in the following ways:295*296* - via an entry in the EFI config table297* - via a setup_data structure, as defined by the Linux Boot Protocol298*299* Scan for the blob in that order.300*/301static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)302{303struct cc_blob_sev_info *cc_info;304305cc_info = find_cc_blob_efi(bp);306if (cc_info)307goto found_cc_info;308309cc_info = find_cc_blob_setup_data(bp);310if (!cc_info)311return NULL;312313found_cc_info:314if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)315sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);316317return cc_info;318}319320/*321* Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks322* will verify the SNP CPUID/MSR bits.323*/324static bool early_snp_init(struct boot_params *bp)325{326struct cc_blob_sev_info *cc_info;327328if (!bp)329return false;330331cc_info = find_cc_blob(bp);332if (!cc_info)333return false;334335/*336* If a SNP-specific Confidential Computing blob is present, then337* firmware/bootloader have indicated SNP support. Verifying this338* involves CPUID checks which will be more reliable if the SNP339* CPUID table is used. See comments over snp_setup_cpuid_table() for340* more details.341*/342setup_cpuid_table(cc_info);343344/*345* Record the SVSM Calling Area (CA) address if the guest is not346* running at VMPL0. The CA will be used to communicate with the347* SVSM and request its services.348*/349svsm_setup_ca(cc_info);350351/*352* Pass run-time kernel a pointer to CC info via boot_params so EFI353* config table doesn't need to be searched again during early startup354* phase.355*/356bp->cc_blob_address = (u32)(unsigned long)cc_info;357358return true;359}360361/*362* sev_check_cpu_support - Check for SEV support in the CPU capabilities363*364* Returns < 0 if SEV is not supported, otherwise the position of the365* encryption bit in the page table descriptors.366*/367static int sev_check_cpu_support(void)368{369unsigned int eax, ebx, ecx, edx;370371/* Check for the SME/SEV support leaf */372eax = 0x80000000;373ecx = 0;374native_cpuid(&eax, &ebx, &ecx, &edx);375if (eax < 0x8000001f)376return -ENODEV;377378/*379* Check for the SME/SEV feature:380* CPUID Fn8000_001F[EAX]381* - Bit 0 - Secure Memory Encryption support382* - Bit 1 - Secure Encrypted Virtualization support383* CPUID Fn8000_001F[EBX]384* - Bits 5:0 - Pagetable bit position used to indicate encryption385*/386eax = 0x8000001f;387ecx = 0;388native_cpuid(&eax, &ebx, &ecx, &edx);389/* Check whether SEV is supported */390if (!(eax & BIT(1)))391return -ENODEV;392393return ebx & 0x3f;394}395396void sev_enable(struct boot_params *bp)397{398struct msr m;399int bitpos;400bool snp;401402/*403* bp->cc_blob_address should only be set by boot/compressed kernel.404* Initialize it to 0 to ensure that uninitialized values from405* buggy bootloaders aren't propagated.406*/407if (bp)408bp->cc_blob_address = 0;409410/*411* Do an initial SEV capability check before early_snp_init() which412* loads the CPUID page and the same checks afterwards are done413* without the hypervisor and are trustworthy.414*415* If the HV fakes SEV support, the guest will crash'n'burn416* which is good enough.417*/418419if (sev_check_cpu_support() < 0)420return;421422/*423* Setup/preliminary detection of SNP. This will be sanity-checked424* against CPUID/MSR values later.425*/426snp = early_snp_init(bp);427428/* Now repeat the checks with the SNP CPUID table. */429430bitpos = sev_check_cpu_support();431if (bitpos < 0) {432if (snp)433error("SEV-SNP support indicated by CC blob, but not CPUID.");434return;435}436437/* Set the SME mask if this is an SEV guest. */438boot_rdmsr(MSR_AMD64_SEV, &m);439sev_status = m.q;440if (!(sev_status & MSR_AMD64_SEV_ENABLED))441return;442443/* Negotiate the GHCB protocol version. */444if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {445if (!sev_es_negotiate_protocol())446sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);447}448449/*450* SNP is supported in v2 of the GHCB spec which mandates support for HV451* features.452*/453if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {454u64 hv_features;455int ret;456457hv_features = get_hv_features();458if (!(hv_features & GHCB_HV_FT_SNP))459sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);460461/*462* Enforce running at VMPL0 or with an SVSM.463*464* Use RMPADJUST (see the rmpadjust() function for a description of465* what the instruction does) to update the VMPL1 permissions of a466* page. If the guest is running at VMPL0, this will succeed. If the467* guest is running at any other VMPL, this will fail. Linux SNP guests468* only ever run at a single VMPL level so permission mask changes of a469* lesser-privileged VMPL are a don't-care.470*/471ret = rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, 1);472473/*474* Running at VMPL0 is not required if an SVSM is present and the hypervisor475* supports the required SVSM GHCB events.476*/477if (ret &&478!(snp_vmpl && (hv_features & GHCB_HV_FT_SNP_MULTI_VMPL)))479sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);480}481482if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))483error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");484485sme_me_mask = BIT_ULL(bitpos);486}487488/*489* sev_get_status - Retrieve the SEV status mask490*491* Returns 0 if the CPU is not SEV capable, otherwise the value of the492* AMD64_SEV MSR.493*/494u64 sev_get_status(void)495{496struct msr m;497498if (sev_check_cpu_support() < 0)499return 0;500501boot_rdmsr(MSR_AMD64_SEV, &m);502return m.q;503}504505void sev_prep_identity_maps(unsigned long top_level_pgt)506{507/*508* The Confidential Computing blob is used very early in uncompressed509* kernel to find the in-memory CPUID table to handle CPUID510* instructions. Make sure an identity-mapping exists so it can be511* accessed after switchover.512*/513if (sev_snp_enabled()) {514unsigned long cc_info_pa = boot_params_ptr->cc_blob_address;515struct cc_blob_sev_info *cc_info;516517kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));518519cc_info = (struct cc_blob_sev_info *)cc_info_pa;520kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);521}522523sev_verify_cbit(top_level_pgt);524}525526bool early_is_sevsnp_guest(void)527{528static bool sevsnp;529530if (sevsnp)531return true;532533if (!(sev_get_status() & MSR_AMD64_SEV_SNP_ENABLED))534return false;535536sevsnp = true;537538if (!snp_vmpl) {539unsigned int eax, ebx, ecx, edx;540541/*542* CPUID Fn8000_001F_EAX[28] - SVSM support543*/544eax = 0x8000001f;545ecx = 0;546native_cpuid(&eax, &ebx, &ecx, &edx);547if (eax & BIT(28)) {548struct msr m;549550/* Obtain the address of the calling area to use */551boot_rdmsr(MSR_SVSM_CAA, &m);552boot_svsm_caa = (void *)m.q;553boot_svsm_caa_pa = m.q;554555/*556* The real VMPL level cannot be discovered, but the557* memory acceptance routines make no use of that so558* any non-zero value suffices here.559*/560snp_vmpl = U8_MAX;561}562}563return true;564}565566567