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> 23e759959fSBrijesh Singh 24e759959fSBrijesh Singh #include "error.h" 25950d0055SMichael Roth #include "../msr.h" 26e759959fSBrijesh Singh 27e759959fSBrijesh Singh struct ghcb boot_ghcb_page __aligned(PAGE_SIZE); 28e759959fSBrijesh Singh struct ghcb *boot_ghcb; 29e759959fSBrijesh Singh 30e759959fSBrijesh Singh /* 31e759959fSBrijesh Singh * Copy a version of this function here - insn-eval.c can't be used in 32e759959fSBrijesh Singh * pre-decompression code. 33e759959fSBrijesh Singh */ 34e759959fSBrijesh Singh static bool insn_has_rep_prefix(struct insn *insn) 35e759959fSBrijesh Singh { 36e759959fSBrijesh Singh insn_byte_t p; 37e759959fSBrijesh Singh int i; 38e759959fSBrijesh Singh 39e759959fSBrijesh Singh insn_get_prefixes(insn); 40e759959fSBrijesh Singh 41e759959fSBrijesh Singh for_each_insn_prefix(insn, i, p) { 42e759959fSBrijesh Singh if (p == 0xf2 || p == 0xf3) 43e759959fSBrijesh Singh return true; 44e759959fSBrijesh Singh } 45e759959fSBrijesh Singh 46e759959fSBrijesh Singh return false; 47e759959fSBrijesh Singh } 48e759959fSBrijesh Singh 49e759959fSBrijesh Singh /* 50e759959fSBrijesh Singh * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and 51e759959fSBrijesh Singh * doesn't use segments. 52e759959fSBrijesh Singh */ 53e759959fSBrijesh Singh static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx) 54e759959fSBrijesh Singh { 55e759959fSBrijesh Singh return 0UL; 56e759959fSBrijesh Singh } 57e759959fSBrijesh Singh 58e759959fSBrijesh Singh static inline u64 sev_es_rd_ghcb_msr(void) 59e759959fSBrijesh Singh { 60950d0055SMichael Roth struct msr m; 61e759959fSBrijesh Singh 62950d0055SMichael Roth boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m); 63e759959fSBrijesh Singh 64950d0055SMichael Roth return m.q; 65e759959fSBrijesh Singh } 66e759959fSBrijesh Singh 67e759959fSBrijesh Singh static inline void sev_es_wr_ghcb_msr(u64 val) 68e759959fSBrijesh Singh { 69950d0055SMichael Roth struct msr m; 70e759959fSBrijesh Singh 71950d0055SMichael Roth m.q = val; 72950d0055SMichael Roth boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m); 73e759959fSBrijesh Singh } 74e759959fSBrijesh Singh 75e759959fSBrijesh Singh static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt) 76e759959fSBrijesh Singh { 77e759959fSBrijesh Singh char buffer[MAX_INSN_SIZE]; 78e759959fSBrijesh Singh int ret; 79e759959fSBrijesh Singh 80e759959fSBrijesh Singh memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE); 81e759959fSBrijesh Singh 82e759959fSBrijesh Singh ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64); 83e759959fSBrijesh Singh if (ret < 0) 84e759959fSBrijesh Singh return ES_DECODE_FAILED; 85e759959fSBrijesh Singh 86e759959fSBrijesh Singh return ES_OK; 87e759959fSBrijesh Singh } 88e759959fSBrijesh Singh 89e759959fSBrijesh Singh static enum es_result vc_write_mem(struct es_em_ctxt *ctxt, 90e759959fSBrijesh Singh void *dst, char *buf, size_t size) 91e759959fSBrijesh Singh { 92e759959fSBrijesh Singh memcpy(dst, buf, size); 93e759959fSBrijesh Singh 94e759959fSBrijesh Singh return ES_OK; 95e759959fSBrijesh Singh } 96e759959fSBrijesh Singh 97e759959fSBrijesh Singh static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, 98e759959fSBrijesh Singh void *src, char *buf, size_t size) 99e759959fSBrijesh Singh { 100e759959fSBrijesh Singh memcpy(buf, src, size); 101e759959fSBrijesh Singh 102e759959fSBrijesh Singh return ES_OK; 103e759959fSBrijesh Singh } 104e759959fSBrijesh Singh 105e759959fSBrijesh Singh #undef __init 106e759959fSBrijesh Singh #undef __pa 107e759959fSBrijesh Singh #define __init 108e759959fSBrijesh Singh #define __pa(x) ((unsigned long)(x)) 109e759959fSBrijesh Singh 110e759959fSBrijesh Singh #define __BOOT_COMPRESSED 111e759959fSBrijesh Singh 112e759959fSBrijesh Singh /* Basic instruction decoding support needed */ 113e759959fSBrijesh Singh #include "../../lib/inat.c" 114e759959fSBrijesh Singh #include "../../lib/insn.c" 115e759959fSBrijesh Singh 116e759959fSBrijesh Singh /* Include code for early handlers */ 117e759959fSBrijesh Singh #include "../../kernel/sev-shared.c" 118e759959fSBrijesh Singh 119*cbd3d4f7SBrijesh Singh static bool early_setup_ghcb(void) 120e759959fSBrijesh Singh { 121e759959fSBrijesh Singh if (set_page_decrypted((unsigned long)&boot_ghcb_page)) 122e759959fSBrijesh Singh return false; 123e759959fSBrijesh Singh 124e759959fSBrijesh Singh /* Page is now mapped decrypted, clear it */ 125e759959fSBrijesh Singh memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page)); 126e759959fSBrijesh Singh 127e759959fSBrijesh Singh boot_ghcb = &boot_ghcb_page; 128e759959fSBrijesh Singh 129e759959fSBrijesh Singh /* Initialize lookup tables for the instruction decoder */ 130e759959fSBrijesh Singh inat_init_tables(); 131e759959fSBrijesh Singh 132e759959fSBrijesh Singh return true; 133e759959fSBrijesh Singh } 134e759959fSBrijesh Singh 135e759959fSBrijesh Singh void sev_es_shutdown_ghcb(void) 136e759959fSBrijesh Singh { 137e759959fSBrijesh Singh if (!boot_ghcb) 138e759959fSBrijesh Singh return; 139e759959fSBrijesh Singh 140e759959fSBrijesh Singh if (!sev_es_check_cpu_features()) 141e759959fSBrijesh Singh error("SEV-ES CPU Features missing."); 142e759959fSBrijesh Singh 143e759959fSBrijesh Singh /* 144e759959fSBrijesh Singh * GHCB Page must be flushed from the cache and mapped encrypted again. 145e759959fSBrijesh Singh * Otherwise the running kernel will see strange cache effects when 146e759959fSBrijesh Singh * trying to use that page. 147e759959fSBrijesh Singh */ 148e759959fSBrijesh Singh if (set_page_encrypted((unsigned long)&boot_ghcb_page)) 149e759959fSBrijesh Singh error("Can't map GHCB page encrypted"); 150e759959fSBrijesh Singh 151e759959fSBrijesh Singh /* 152e759959fSBrijesh Singh * GHCB page is mapped encrypted again and flushed from the cache. 153e759959fSBrijesh Singh * Mark it non-present now to catch bugs when #VC exceptions trigger 154e759959fSBrijesh Singh * after this point. 155e759959fSBrijesh Singh */ 156e759959fSBrijesh Singh if (set_page_non_present((unsigned long)&boot_ghcb_page)) 157e759959fSBrijesh Singh error("Can't unmap GHCB page"); 158e759959fSBrijesh Singh } 159e759959fSBrijesh Singh 160e759959fSBrijesh Singh bool sev_es_check_ghcb_fault(unsigned long address) 161e759959fSBrijesh Singh { 162e759959fSBrijesh Singh /* Check whether the fault was on the GHCB page */ 163e759959fSBrijesh Singh return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page); 164e759959fSBrijesh Singh } 165e759959fSBrijesh Singh 166e759959fSBrijesh Singh void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) 167e759959fSBrijesh Singh { 168e759959fSBrijesh Singh struct es_em_ctxt ctxt; 169e759959fSBrijesh Singh enum es_result result; 170e759959fSBrijesh Singh 171*cbd3d4f7SBrijesh Singh if (!boot_ghcb && !early_setup_ghcb()) 1726c0f74d6SBrijesh Singh sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); 173e759959fSBrijesh Singh 174e759959fSBrijesh Singh vc_ghcb_invalidate(boot_ghcb); 175e759959fSBrijesh Singh result = vc_init_em_ctxt(&ctxt, regs, exit_code); 176e759959fSBrijesh Singh if (result != ES_OK) 177e759959fSBrijesh Singh goto finish; 178e759959fSBrijesh Singh 179e759959fSBrijesh Singh switch (exit_code) { 180e759959fSBrijesh Singh case SVM_EXIT_RDTSC: 181e759959fSBrijesh Singh case SVM_EXIT_RDTSCP: 182e759959fSBrijesh Singh result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code); 183e759959fSBrijesh Singh break; 184e759959fSBrijesh Singh case SVM_EXIT_IOIO: 185e759959fSBrijesh Singh result = vc_handle_ioio(boot_ghcb, &ctxt); 186e759959fSBrijesh Singh break; 187e759959fSBrijesh Singh case SVM_EXIT_CPUID: 188e759959fSBrijesh Singh result = vc_handle_cpuid(boot_ghcb, &ctxt); 189e759959fSBrijesh Singh break; 190e759959fSBrijesh Singh default: 191e759959fSBrijesh Singh result = ES_UNSUPPORTED; 192e759959fSBrijesh Singh break; 193e759959fSBrijesh Singh } 194e759959fSBrijesh Singh 195e759959fSBrijesh Singh finish: 196e759959fSBrijesh Singh if (result == ES_OK) 197e759959fSBrijesh Singh vc_finish_insn(&ctxt); 198e759959fSBrijesh Singh else if (result != ES_RETRY) 1996c0f74d6SBrijesh Singh sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); 200e759959fSBrijesh Singh } 201ec1c66afSMichael Roth 202ec1c66afSMichael Roth void sev_enable(struct boot_params *bp) 203ec1c66afSMichael Roth { 204ec1c66afSMichael Roth unsigned int eax, ebx, ecx, edx; 205ec1c66afSMichael Roth struct msr m; 206ec1c66afSMichael Roth 207ec1c66afSMichael Roth /* Check for the SME/SEV support leaf */ 208ec1c66afSMichael Roth eax = 0x80000000; 209ec1c66afSMichael Roth ecx = 0; 210ec1c66afSMichael Roth native_cpuid(&eax, &ebx, &ecx, &edx); 211ec1c66afSMichael Roth if (eax < 0x8000001f) 212ec1c66afSMichael Roth return; 213ec1c66afSMichael Roth 214ec1c66afSMichael Roth /* 215ec1c66afSMichael Roth * Check for the SME/SEV feature: 216ec1c66afSMichael Roth * CPUID Fn8000_001F[EAX] 217ec1c66afSMichael Roth * - Bit 0 - Secure Memory Encryption support 218ec1c66afSMichael Roth * - Bit 1 - Secure Encrypted Virtualization support 219ec1c66afSMichael Roth * CPUID Fn8000_001F[EBX] 220ec1c66afSMichael Roth * - Bits 5:0 - Pagetable bit position used to indicate encryption 221ec1c66afSMichael Roth */ 222ec1c66afSMichael Roth eax = 0x8000001f; 223ec1c66afSMichael Roth ecx = 0; 224ec1c66afSMichael Roth native_cpuid(&eax, &ebx, &ecx, &edx); 225ec1c66afSMichael Roth /* Check whether SEV is supported */ 226ec1c66afSMichael Roth if (!(eax & BIT(1))) 227ec1c66afSMichael Roth return; 228ec1c66afSMichael Roth 229ec1c66afSMichael Roth /* Set the SME mask if this is an SEV guest. */ 230ec1c66afSMichael Roth boot_rdmsr(MSR_AMD64_SEV, &m); 231ec1c66afSMichael Roth sev_status = m.q; 232ec1c66afSMichael Roth if (!(sev_status & MSR_AMD64_SEV_ENABLED)) 233ec1c66afSMichael Roth return; 234ec1c66afSMichael Roth 235*cbd3d4f7SBrijesh Singh /* Negotiate the GHCB protocol version. */ 236*cbd3d4f7SBrijesh Singh if (sev_status & MSR_AMD64_SEV_ES_ENABLED) { 237*cbd3d4f7SBrijesh Singh if (!sev_es_negotiate_protocol()) 238*cbd3d4f7SBrijesh Singh sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED); 239*cbd3d4f7SBrijesh Singh } 240*cbd3d4f7SBrijesh Singh 241*cbd3d4f7SBrijesh Singh /* 242*cbd3d4f7SBrijesh Singh * SNP is supported in v2 of the GHCB spec which mandates support for HV 243*cbd3d4f7SBrijesh Singh * features. 244*cbd3d4f7SBrijesh Singh */ 245*cbd3d4f7SBrijesh Singh if (sev_status & MSR_AMD64_SEV_SNP_ENABLED && !(get_hv_features() & GHCB_HV_FT_SNP)) 246*cbd3d4f7SBrijesh Singh sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); 247*cbd3d4f7SBrijesh Singh 248ec1c66afSMichael Roth sme_me_mask = BIT_ULL(ebx & 0x3f); 249ec1c66afSMichael Roth } 250