2020-09-07 15:15:20 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* AMD Encrypted Register State Support
|
|
|
|
*
|
|
|
|
* Author: Joerg Roedel <jroedel@suse.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* misc.h needs to be first because it knows how to include the other kernel
|
|
|
|
* headers in the pre-decompression code in a way that does not break
|
|
|
|
* compilation.
|
|
|
|
*/
|
|
|
|
#include "misc.h"
|
|
|
|
|
2024-01-12 10:44:39 +01:00
|
|
|
#include <asm/bootparam.h>
|
2020-09-07 15:16:12 +02:00
|
|
|
#include <asm/pgtable_types.h>
|
2021-04-27 06:16:34 -05:00
|
|
|
#include <asm/sev.h>
|
2020-09-07 15:15:24 +02:00
|
|
|
#include <asm/trapnr.h>
|
|
|
|
#include <asm/trap_pf.h>
|
2020-09-07 15:15:20 +02:00
|
|
|
#include <asm/msr-index.h>
|
2020-09-07 15:15:28 +02:00
|
|
|
#include <asm/fpu/xcr.h>
|
2020-09-07 15:15:20 +02:00
|
|
|
#include <asm/ptrace.h>
|
|
|
|
#include <asm/svm.h>
|
2022-02-24 10:56:11 -06:00
|
|
|
#include <asm/cpuid.h>
|
2020-09-07 15:15:20 +02:00
|
|
|
|
2020-09-07 15:15:24 +02:00
|
|
|
#include "error.h"
|
2022-02-09 12:10:00 -06:00
|
|
|
#include "../msr.h"
|
2020-09-07 15:15:24 +02:00
|
|
|
|
2023-08-02 10:14:36 +08:00
|
|
|
static struct ghcb boot_ghcb_page __aligned(PAGE_SIZE);
|
2020-09-07 15:15:24 +02:00
|
|
|
struct ghcb *boot_ghcb;
|
|
|
|
|
2020-09-07 15:15:26 +02:00
|
|
|
/*
|
|
|
|
* Copy a version of this function here - insn-eval.c can't be used in
|
|
|
|
* pre-decompression code.
|
|
|
|
*/
|
|
|
|
static bool insn_has_rep_prefix(struct insn *insn)
|
|
|
|
{
|
2020-12-03 13:51:01 +09:00
|
|
|
insn_byte_t p;
|
2020-09-07 15:15:26 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
insn_get_prefixes(insn);
|
|
|
|
|
2020-12-03 13:51:01 +09:00
|
|
|
for_each_insn_prefix(insn, i, p) {
|
2020-09-07 15:15:26 +02:00
|
|
|
if (p == 0xf2 || p == 0xf3)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
|
|
|
|
* doesn't use segments.
|
|
|
|
*/
|
|
|
|
static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
|
|
|
|
{
|
|
|
|
return 0UL;
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:15:20 +02:00
|
|
|
static inline u64 sev_es_rd_ghcb_msr(void)
|
|
|
|
{
|
2022-02-09 12:10:00 -06:00
|
|
|
struct msr m;
|
2020-09-07 15:15:20 +02:00
|
|
|
|
2022-02-09 12:10:00 -06:00
|
|
|
boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
|
2020-09-07 15:15:20 +02:00
|
|
|
|
2022-02-09 12:10:00 -06:00
|
|
|
return m.q;
|
2020-09-07 15:15:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void sev_es_wr_ghcb_msr(u64 val)
|
|
|
|
{
|
2022-02-09 12:10:00 -06:00
|
|
|
struct msr m;
|
2020-09-07 15:15:20 +02:00
|
|
|
|
2022-02-09 12:10:00 -06:00
|
|
|
m.q = val;
|
|
|
|
boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
|
2020-09-07 15:15:20 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 15:15:24 +02:00
|
|
|
static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
|
|
|
|
{
|
|
|
|
char buffer[MAX_INSN_SIZE];
|
2020-11-05 17:53:20 +01:00
|
|
|
int ret;
|
2020-09-07 15:15:24 +02:00
|
|
|
|
|
|
|
memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
|
|
|
|
|
2020-11-05 17:53:20 +01:00
|
|
|
ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
|
|
|
|
if (ret < 0)
|
|
|
|
return ES_DECODE_FAILED;
|
2020-09-07 15:15:24 +02:00
|
|
|
|
2020-11-05 17:53:20 +01:00
|
|
|
return ES_OK;
|
2020-09-07 15:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
|
|
|
|
void *dst, char *buf, size_t size)
|
|
|
|
{
|
|
|
|
memcpy(dst, buf, size);
|
|
|
|
|
|
|
|
return ES_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
|
|
|
|
void *src, char *buf, size_t size)
|
|
|
|
{
|
|
|
|
memcpy(buf, src, size);
|
|
|
|
|
|
|
|
return ES_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-21 17:42:42 +02:00
|
|
|
static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
|
|
|
|
{
|
|
|
|
return ES_OK;
|
|
|
|
}
|
|
|
|
|
2023-10-16 14:42:50 +02:00
|
|
|
static bool fault_in_kernel_space(unsigned long address)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:15:20 +02:00
|
|
|
#undef __init
|
|
|
|
#define __init
|
2020-09-07 15:15:24 +02:00
|
|
|
|
2024-02-27 16:19:16 +01:00
|
|
|
#undef __head
|
|
|
|
#define __head
|
|
|
|
|
2020-09-07 15:15:24 +02:00
|
|
|
#define __BOOT_COMPRESSED
|
|
|
|
|
|
|
|
/* Basic instruction decoding support needed */
|
|
|
|
#include "../../lib/inat.c"
|
|
|
|
#include "../../lib/insn.c"
|
2020-09-07 15:15:20 +02:00
|
|
|
|
|
|
|
/* Include code for early handlers */
|
2021-04-27 06:16:34 -05:00
|
|
|
#include "../../kernel/sev-shared.c"
|
2020-09-07 15:15:24 +02:00
|
|
|
|
2023-06-06 09:51:26 -05:00
|
|
|
bool sev_snp_enabled(void)
|
2022-02-09 12:10:09 -06:00
|
|
|
{
|
|
|
|
return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __page_state_change(unsigned long paddr, enum psc_op op)
|
|
|
|
{
|
|
|
|
u64 val;
|
|
|
|
|
|
|
|
if (!sev_snp_enabled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If private -> shared then invalidate the page before requesting the
|
|
|
|
* state change in the RMP table.
|
|
|
|
*/
|
|
|
|
if (op == SNP_PAGE_STATE_SHARED && pvalidate(paddr, RMP_PG_SIZE_4K, 0))
|
|
|
|
sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
|
|
|
|
|
|
|
|
/* Issue VMGEXIT to change the page state in RMP table. */
|
|
|
|
sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
|
|
|
|
VMGEXIT();
|
|
|
|
|
|
|
|
/* Read the response of the VMGEXIT. */
|
|
|
|
val = sev_es_rd_ghcb_msr();
|
|
|
|
if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))
|
|
|
|
sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that page state is changed in the RMP table, validate it so that it is
|
|
|
|
* consistent with the RMP entry.
|
|
|
|
*/
|
|
|
|
if (op == SNP_PAGE_STATE_PRIVATE && pvalidate(paddr, RMP_PG_SIZE_4K, 1))
|
|
|
|
sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void snp_set_page_private(unsigned long paddr)
|
|
|
|
{
|
|
|
|
__page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void snp_set_page_shared(unsigned long paddr)
|
|
|
|
{
|
|
|
|
__page_state_change(paddr, SNP_PAGE_STATE_SHARED);
|
|
|
|
}
|
|
|
|
|
2022-02-09 12:10:06 -06:00
|
|
|
static bool early_setup_ghcb(void)
|
2020-09-07 15:15:24 +02:00
|
|
|
{
|
|
|
|
if (set_page_decrypted((unsigned long)&boot_ghcb_page))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Page is now mapped decrypted, clear it */
|
|
|
|
memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));
|
|
|
|
|
|
|
|
boot_ghcb = &boot_ghcb_page;
|
|
|
|
|
|
|
|
/* Initialize lookup tables for the instruction decoder */
|
|
|
|
inat_init_tables();
|
|
|
|
|
2022-02-09 12:10:10 -06:00
|
|
|
/* SNP guest requires the GHCB GPA must be registered */
|
|
|
|
if (sev_snp_enabled())
|
|
|
|
snp_register_ghcb_early(__pa(&boot_ghcb_page));
|
|
|
|
|
2020-09-07 15:15:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-06 09:51:26 -05:00
|
|
|
static phys_addr_t __snp_accept_memory(struct snp_psc_desc *desc,
|
|
|
|
phys_addr_t pa, phys_addr_t pa_end)
|
|
|
|
{
|
|
|
|
struct psc_hdr *hdr;
|
|
|
|
struct psc_entry *e;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
hdr = &desc->hdr;
|
|
|
|
memset(hdr, 0, sizeof(*hdr));
|
|
|
|
|
|
|
|
e = desc->entries;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (pa < pa_end && i < VMGEXIT_PSC_MAX_ENTRY) {
|
|
|
|
hdr->end_entry = i;
|
|
|
|
|
|
|
|
e->gfn = pa >> PAGE_SHIFT;
|
|
|
|
e->operation = SNP_PAGE_STATE_PRIVATE;
|
|
|
|
if (IS_ALIGNED(pa, PMD_SIZE) && (pa_end - pa) >= PMD_SIZE) {
|
|
|
|
e->pagesize = RMP_PG_SIZE_2M;
|
|
|
|
pa += PMD_SIZE;
|
|
|
|
} else {
|
|
|
|
e->pagesize = RMP_PG_SIZE_4K;
|
|
|
|
pa += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
e++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vmgexit_psc(boot_ghcb, desc))
|
|
|
|
sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
|
|
|
|
|
|
|
|
pvalidate_pages(desc);
|
|
|
|
|
|
|
|
return pa;
|
|
|
|
}
|
|
|
|
|
|
|
|
void snp_accept_memory(phys_addr_t start, phys_addr_t end)
|
|
|
|
{
|
|
|
|
struct snp_psc_desc desc = {};
|
|
|
|
unsigned int i;
|
|
|
|
phys_addr_t pa;
|
|
|
|
|
|
|
|
if (!boot_ghcb && !early_setup_ghcb())
|
|
|
|
sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
|
|
|
|
|
|
|
|
pa = start;
|
|
|
|
while (pa < end)
|
|
|
|
pa = __snp_accept_memory(&desc, pa, end);
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:15:24 +02:00
|
|
|
void sev_es_shutdown_ghcb(void)
|
|
|
|
{
|
|
|
|
if (!boot_ghcb)
|
|
|
|
return;
|
|
|
|
|
2020-09-07 15:16:13 +02:00
|
|
|
if (!sev_es_check_cpu_features())
|
|
|
|
error("SEV-ES CPU Features missing.");
|
|
|
|
|
2020-09-07 15:15:24 +02:00
|
|
|
/*
|
|
|
|
* GHCB Page must be flushed from the cache and mapped encrypted again.
|
|
|
|
* Otherwise the running kernel will see strange cache effects when
|
|
|
|
* trying to use that page.
|
|
|
|
*/
|
|
|
|
if (set_page_encrypted((unsigned long)&boot_ghcb_page))
|
|
|
|
error("Can't map GHCB page encrypted");
|
2020-09-07 15:15:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* GHCB page is mapped encrypted again and flushed from the cache.
|
|
|
|
* Mark it non-present now to catch bugs when #VC exceptions trigger
|
|
|
|
* after this point.
|
|
|
|
*/
|
|
|
|
if (set_page_non_present((unsigned long)&boot_ghcb_page))
|
|
|
|
error("Can't unmap GHCB page");
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:49:43 +05:30
|
|
|
static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set,
|
|
|
|
unsigned int reason, u64 exit_info_2)
|
|
|
|
{
|
|
|
|
u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason);
|
|
|
|
|
|
|
|
vc_ghcb_invalidate(ghcb);
|
|
|
|
ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST);
|
|
|
|
ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
|
|
|
|
ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
|
|
|
|
|
|
|
|
sev_es_wr_ghcb_msr(__pa(ghcb));
|
|
|
|
VMGEXIT();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
asm volatile("hlt\n" : : : "memory");
|
|
|
|
}
|
|
|
|
|
2020-09-07 15:15:25 +02:00
|
|
|
bool sev_es_check_ghcb_fault(unsigned long address)
|
|
|
|
{
|
|
|
|
/* Check whether the fault was on the GHCB page */
|
|
|
|
return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);
|
2020-09-07 15:15:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
|
|
|
|
{
|
|
|
|
struct es_em_ctxt ctxt;
|
|
|
|
enum es_result result;
|
|
|
|
|
2022-02-09 12:10:06 -06:00
|
|
|
if (!boot_ghcb && !early_setup_ghcb())
|
2022-02-09 12:10:04 -06:00
|
|
|
sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
|
2020-09-07 15:15:24 +02:00
|
|
|
|
|
|
|
vc_ghcb_invalidate(boot_ghcb);
|
|
|
|
result = vc_init_em_ctxt(&ctxt, regs, exit_code);
|
|
|
|
if (result != ES_OK)
|
|
|
|
goto finish;
|
|
|
|
|
2024-01-05 11:14:07 +01:00
|
|
|
result = vc_check_opcode_bytes(&ctxt, exit_code);
|
|
|
|
if (result != ES_OK)
|
|
|
|
goto finish;
|
|
|
|
|
2020-09-07 15:15:24 +02:00
|
|
|
switch (exit_code) {
|
2020-09-07 15:15:55 +02:00
|
|
|
case SVM_EXIT_RDTSC:
|
|
|
|
case SVM_EXIT_RDTSCP:
|
|
|
|
result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code);
|
|
|
|
break;
|
2020-09-07 15:15:26 +02:00
|
|
|
case SVM_EXIT_IOIO:
|
|
|
|
result = vc_handle_ioio(boot_ghcb, &ctxt);
|
|
|
|
break;
|
2020-09-07 15:15:28 +02:00
|
|
|
case SVM_EXIT_CPUID:
|
|
|
|
result = vc_handle_cpuid(boot_ghcb, &ctxt);
|
|
|
|
break;
|
2020-09-07 15:15:24 +02:00
|
|
|
default:
|
|
|
|
result = ES_UNSUPPORTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
2021-03-12 13:38:24 +01:00
|
|
|
if (result == ES_OK)
|
2020-09-07 15:15:24 +02:00
|
|
|
vc_finish_insn(&ctxt);
|
2021-03-12 13:38:24 +01:00
|
|
|
else if (result != ES_RETRY)
|
2022-02-09 12:10:04 -06:00
|
|
|
sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
|
2020-09-07 15:15:24 +02:00
|
|
|
}
|
2022-02-09 12:10:01 -06:00
|
|
|
|
2023-01-18 11:49:43 +05:30
|
|
|
/*
|
|
|
|
* SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need
|
|
|
|
* guest side implementation for proper functioning of the guest. If any
|
|
|
|
* of these features are enabled in the hypervisor but are lacking guest
|
|
|
|
* side implementation, the behavior of the guest will be undefined. The
|
|
|
|
* guest could fail in non-obvious way making it difficult to debug.
|
|
|
|
*
|
|
|
|
* As the behavior of reserved feature bits is unknown to be on the
|
|
|
|
* safe side add them to the required features mask.
|
|
|
|
*/
|
|
|
|
#define SNP_FEATURES_IMPL_REQ (MSR_AMD64_SNP_VTOM | \
|
|
|
|
MSR_AMD64_SNP_REFLECT_VC | \
|
|
|
|
MSR_AMD64_SNP_RESTRICTED_INJ | \
|
|
|
|
MSR_AMD64_SNP_ALT_INJ | \
|
|
|
|
MSR_AMD64_SNP_DEBUG_SWAP | \
|
|
|
|
MSR_AMD64_SNP_VMPL_SSS | \
|
|
|
|
MSR_AMD64_SNP_SECURE_TSC | \
|
|
|
|
MSR_AMD64_SNP_VMGEXIT_PARAM | \
|
2024-02-19 10:42:16 +01:00
|
|
|
MSR_AMD64_SNP_VMSA_REG_PROT | \
|
2023-01-18 11:49:43 +05:30
|
|
|
MSR_AMD64_SNP_RESERVED_BIT13 | \
|
|
|
|
MSR_AMD64_SNP_RESERVED_BIT15 | \
|
|
|
|
MSR_AMD64_SNP_RESERVED_MASK)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
|
|
|
|
* by the guest kernel. As and when a new feature is implemented in the
|
|
|
|
* guest kernel, a corresponding bit should be added to the mask.
|
|
|
|
*/
|
2023-08-16 12:21:22 +10:00
|
|
|
#define SNP_FEATURES_PRESENT MSR_AMD64_SNP_DEBUG_SWAP
|
2023-01-18 11:49:43 +05:30
|
|
|
|
2023-08-07 18:27:19 +02:00
|
|
|
u64 snp_get_unsupported_features(u64 status)
|
|
|
|
{
|
|
|
|
if (!(status & MSR_AMD64_SEV_SNP_ENABLED))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT;
|
|
|
|
}
|
|
|
|
|
2023-01-18 11:49:43 +05:30
|
|
|
void snp_check_features(void)
|
|
|
|
{
|
|
|
|
u64 unsupported;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Terminate the boot if hypervisor has enabled any feature lacking
|
|
|
|
* guest side implementation. Pass on the unsupported features mask through
|
|
|
|
* EXIT_INFO_2 of the GHCB protocol so that those features can be reported
|
|
|
|
* as part of the guest boot failure.
|
|
|
|
*/
|
2023-08-07 18:27:19 +02:00
|
|
|
unsupported = snp_get_unsupported_features(sev_status);
|
2023-01-18 11:49:43 +05:30
|
|
|
if (unsupported) {
|
|
|
|
if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb()))
|
|
|
|
sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
|
|
|
|
|
|
|
|
sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN,
|
|
|
|
GHCB_SNP_UNSUPPORTED, unsupported);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-24 10:57:58 -05:00
|
|
|
/* Search for Confidential Computing blob in the EFI config table. */
|
|
|
|
static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)
|
|
|
|
{
|
|
|
|
unsigned long cfg_table_pa;
|
|
|
|
unsigned int cfg_table_len;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,
|
|
|
|
cfg_table_len,
|
|
|
|
EFI_CC_BLOB_GUID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initial set up of SNP relies on information provided by the
|
|
|
|
* Confidential Computing blob, which can be passed to the boot kernel
|
|
|
|
* by firmware/bootloader in the following ways:
|
|
|
|
*
|
|
|
|
* - via an entry in the EFI config table
|
|
|
|
* - via a setup_data structure, as defined by the Linux Boot Protocol
|
|
|
|
*
|
|
|
|
* Scan for the blob in that order.
|
|
|
|
*/
|
|
|
|
static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
|
|
|
|
{
|
|
|
|
struct cc_blob_sev_info *cc_info;
|
|
|
|
|
|
|
|
cc_info = find_cc_blob_efi(bp);
|
|
|
|
if (cc_info)
|
|
|
|
goto found_cc_info;
|
|
|
|
|
|
|
|
cc_info = find_cc_blob_setup_data(bp);
|
|
|
|
if (!cc_info)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
found_cc_info:
|
|
|
|
if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
|
|
|
|
sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
|
|
|
|
|
|
|
|
return cc_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks
|
|
|
|
* will verify the SNP CPUID/MSR bits.
|
|
|
|
*/
|
|
|
|
static bool early_snp_init(struct boot_params *bp)
|
|
|
|
{
|
|
|
|
struct cc_blob_sev_info *cc_info;
|
|
|
|
|
|
|
|
if (!bp)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
cc_info = find_cc_blob(bp);
|
|
|
|
if (!cc_info)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a SNP-specific Confidential Computing blob is present, then
|
|
|
|
* firmware/bootloader have indicated SNP support. Verifying this
|
|
|
|
* involves CPUID checks which will be more reliable if the SNP
|
|
|
|
* CPUID table is used. See comments over snp_setup_cpuid_table() for
|
|
|
|
* more details.
|
|
|
|
*/
|
|
|
|
setup_cpuid_table(cc_info);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pass run-time kernel a pointer to CC info via boot_params so EFI
|
|
|
|
* config table doesn't need to be searched again during early startup
|
|
|
|
* phase.
|
|
|
|
*/
|
|
|
|
bp->cc_blob_address = (u32)(unsigned long)cc_info;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-07 18:27:19 +02:00
|
|
|
/*
|
|
|
|
* sev_check_cpu_support - Check for SEV support in the CPU capabilities
|
|
|
|
*
|
|
|
|
* Returns < 0 if SEV is not supported, otherwise the position of the
|
|
|
|
* encryption bit in the page table descriptors.
|
|
|
|
*/
|
|
|
|
static int sev_check_cpu_support(void)
|
2022-02-09 12:10:01 -06:00
|
|
|
{
|
|
|
|
unsigned int eax, ebx, ecx, edx;
|
2023-07-16 20:22:20 +02:00
|
|
|
|
|
|
|
/* Check for the SME/SEV support leaf */
|
|
|
|
eax = 0x80000000;
|
|
|
|
ecx = 0;
|
|
|
|
native_cpuid(&eax, &ebx, &ecx, &edx);
|
|
|
|
if (eax < 0x8000001f)
|
2023-08-07 18:27:19 +02:00
|
|
|
return -ENODEV;
|
2023-07-16 20:22:20 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for the SME/SEV feature:
|
|
|
|
* CPUID Fn8000_001F[EAX]
|
|
|
|
* - Bit 0 - Secure Memory Encryption support
|
|
|
|
* - Bit 1 - Secure Encrypted Virtualization support
|
|
|
|
* CPUID Fn8000_001F[EBX]
|
|
|
|
* - Bits 5:0 - Pagetable bit position used to indicate encryption
|
|
|
|
*/
|
|
|
|
eax = 0x8000001f;
|
|
|
|
ecx = 0;
|
|
|
|
native_cpuid(&eax, &ebx, &ecx, &edx);
|
|
|
|
/* Check whether SEV is supported */
|
|
|
|
if (!(eax & BIT(1)))
|
2023-08-07 18:27:19 +02:00
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
return ebx & 0x3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sev_enable(struct boot_params *bp)
|
|
|
|
{
|
|
|
|
struct msr m;
|
|
|
|
int bitpos;
|
|
|
|
bool snp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bp->cc_blob_address should only be set by boot/compressed kernel.
|
|
|
|
* Initialize it to 0 to ensure that uninitialized values from
|
|
|
|
* buggy bootloaders aren't propagated.
|
|
|
|
*/
|
|
|
|
if (bp)
|
|
|
|
bp->cc_blob_address = 0;
|
|
|
|
|
|
|
|
/*
|
2024-04-24 10:57:58 -05:00
|
|
|
* Do an initial SEV capability check before early_snp_init() which
|
2023-08-07 18:27:19 +02:00
|
|
|
* loads the CPUID page and the same checks afterwards are done
|
|
|
|
* without the hypervisor and are trustworthy.
|
|
|
|
*
|
|
|
|
* If the HV fakes SEV support, the guest will crash'n'burn
|
|
|
|
* which is good enough.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sev_check_cpu_support() < 0)
|
2023-07-16 20:22:20 +02:00
|
|
|
return;
|
|
|
|
|
2022-02-24 10:56:14 -06:00
|
|
|
/*
|
|
|
|
* Setup/preliminary detection of SNP. This will be sanity-checked
|
|
|
|
* against CPUID/MSR values later.
|
|
|
|
*/
|
2024-04-24 10:57:58 -05:00
|
|
|
snp = early_snp_init(bp);
|
2022-02-09 12:10:01 -06:00
|
|
|
|
2023-07-16 20:22:20 +02:00
|
|
|
/* Now repeat the checks with the SNP CPUID table. */
|
|
|
|
|
2023-08-07 18:27:19 +02:00
|
|
|
bitpos = sev_check_cpu_support();
|
|
|
|
if (bitpos < 0) {
|
2022-02-24 10:56:14 -06:00
|
|
|
if (snp)
|
|
|
|
error("SEV-SNP support indicated by CC blob, but not CPUID.");
|
2022-02-09 12:10:01 -06:00
|
|
|
return;
|
2022-02-24 10:56:14 -06:00
|
|
|
}
|
2022-02-09 12:10:01 -06:00
|
|
|
|
|
|
|
/* Set the SME mask if this is an SEV guest. */
|
|
|
|
boot_rdmsr(MSR_AMD64_SEV, &m);
|
|
|
|
sev_status = m.q;
|
|
|
|
if (!(sev_status & MSR_AMD64_SEV_ENABLED))
|
|
|
|
return;
|
|
|
|
|
2022-02-09 12:10:06 -06:00
|
|
|
/* Negotiate the GHCB protocol version. */
|
|
|
|
if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {
|
|
|
|
if (!sev_es_negotiate_protocol())
|
|
|
|
sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SNP is supported in v2 of the GHCB spec which mandates support for HV
|
|
|
|
* features.
|
|
|
|
*/
|
2022-02-09 12:10:08 -06:00
|
|
|
if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
|
|
|
|
if (!(get_hv_features() & GHCB_HV_FT_SNP))
|
|
|
|
sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
|
|
|
|
|
2024-04-24 10:57:59 -05:00
|
|
|
/*
|
|
|
|
* Enforce running at VMPL0.
|
|
|
|
*
|
|
|
|
* RMPADJUST modifies RMP permissions of a lesser-privileged (numerically
|
|
|
|
* higher) privilege level. Here, clear the VMPL1 permission mask of the
|
|
|
|
* GHCB page. If the guest is not running at VMPL0, this will fail.
|
|
|
|
*
|
|
|
|
* If the guest is running at VMPL0, it will succeed. Even if that operation
|
|
|
|
* modifies permission bits, it is still ok to do so currently because Linux
|
|
|
|
* SNP guests running at VMPL0 only run at VMPL0, so VMPL1 or higher
|
|
|
|
* permission mask changes are a don't-care.
|
|
|
|
*/
|
|
|
|
if (rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, 1))
|
|
|
|
sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
|
2022-02-09 12:10:08 -06:00
|
|
|
}
|
2022-02-09 12:10:06 -06:00
|
|
|
|
2022-02-24 10:56:14 -06:00
|
|
|
if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
|
|
|
|
error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");
|
|
|
|
|
2023-08-07 18:27:19 +02:00
|
|
|
sme_me_mask = BIT_ULL(bitpos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sev_get_status - Retrieve the SEV status mask
|
|
|
|
*
|
|
|
|
* Returns 0 if the CPU is not SEV capable, otherwise the value of the
|
|
|
|
* AMD64_SEV MSR.
|
|
|
|
*/
|
|
|
|
u64 sev_get_status(void)
|
|
|
|
{
|
|
|
|
struct msr m;
|
|
|
|
|
|
|
|
if (sev_check_cpu_support() < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
boot_rdmsr(MSR_AMD64_SEV, &m);
|
|
|
|
return m.q;
|
2022-02-09 12:10:01 -06:00
|
|
|
}
|
2022-02-24 10:56:14 -06:00
|
|
|
|
2022-02-24 10:56:17 -06:00
|
|
|
void sev_prep_identity_maps(unsigned long top_level_pgt)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The Confidential Computing blob is used very early in uncompressed
|
|
|
|
* kernel to find the in-memory CPUID table to handle CPUID
|
|
|
|
* instructions. Make sure an identity-mapping exists so it can be
|
|
|
|
* accessed after switchover.
|
|
|
|
*/
|
|
|
|
if (sev_snp_enabled()) {
|
x86/boot: Rename conflicting 'boot_params' pointer to 'boot_params_ptr'
The x86 decompressor is built and linked as a separate executable, but
it shares components with the kernel proper, which are either #include'd
as C files, or linked into the decompresor as a static library (e.g, the
EFI stub)
Both the kernel itself and the decompressor define a global symbol
'boot_params' to refer to the boot_params struct, but in the former
case, it refers to the struct directly, whereas in the decompressor, it
refers to a global pointer variable referring to the struct boot_params
passed by the bootloader or constructed from scratch.
This ambiguity is unfortunate, and makes it impossible to assign this
decompressor variable from the x86 EFI stub, given that declaring it as
extern results in a clash. So rename the decompressor version (whose
scope is limited) to boot_params_ptr.
[ mingo: Renamed 'boot_params_p' to 'boot_params_ptr' for clarity ]
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org
2023-10-17 15:25:12 +02:00
|
|
|
unsigned long cc_info_pa = boot_params_ptr->cc_blob_address;
|
2022-02-24 10:56:17 -06:00
|
|
|
struct cc_blob_sev_info *cc_info;
|
|
|
|
|
|
|
|
kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));
|
|
|
|
|
|
|
|
cc_info = (struct cc_blob_sev_info *)cc_info_pa;
|
|
|
|
kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
sev_verify_cbit(top_level_pgt);
|
|
|
|
}
|