Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * AMD Encrypted Register State Support
4 *
5 * Author: Joerg Roedel <jroedel@suse.de>
6 */
7
8/*
9 * misc.h needs to be first because it knows how to include the other kernel
10 * headers in the pre-decompression code in a way that does not break
11 * compilation.
12 */
13#include "misc.h"
14
15#include <asm/bootparam.h>
16#include <asm/pgtable_types.h>
17#include <asm/sev.h>
18#include <asm/trapnr.h>
19#include <asm/trap_pf.h>
20#include <asm/msr-index.h>
21#include <asm/fpu/xcr.h>
22#include <asm/ptrace.h>
23#include <asm/svm.h>
24#include <asm/cpuid.h>
25
26#include "error.h"
27#include "../msr.h"
28
29static struct ghcb boot_ghcb_page __aligned(PAGE_SIZE);
30struct ghcb *boot_ghcb;
31
32/*
33 * Copy a version of this function here - insn-eval.c can't be used in
34 * pre-decompression code.
35 */
36static bool insn_has_rep_prefix(struct insn *insn)
37{
38 insn_byte_t p;
39 int i;
40
41 insn_get_prefixes(insn);
42
43 for_each_insn_prefix(insn, i, p) {
44 if (p == 0xf2 || p == 0xf3)
45 return true;
46 }
47
48 return false;
49}
50
51/*
52 * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
53 * doesn't use segments.
54 */
55static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
56{
57 return 0UL;
58}
59
60static inline u64 sev_es_rd_ghcb_msr(void)
61{
62 struct msr m;
63
64 boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
65
66 return m.q;
67}
68
69static inline void sev_es_wr_ghcb_msr(u64 val)
70{
71 struct msr m;
72
73 m.q = val;
74 boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
75}
76
77static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
78{
79 char buffer[MAX_INSN_SIZE];
80 int ret;
81
82 memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
83
84 ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
85 if (ret < 0)
86 return ES_DECODE_FAILED;
87
88 return ES_OK;
89}
90
91static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
92 void *dst, char *buf, size_t size)
93{
94 memcpy(dst, buf, size);
95
96 return ES_OK;
97}
98
99static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
100 void *src, char *buf, size_t size)
101{
102 memcpy(buf, src, size);
103
104 return ES_OK;
105}
106
107static enum es_result vc_ioio_check(struct es_em_ctxt *ctxt, u16 port, size_t size)
108{
109 return ES_OK;
110}
111
112static bool fault_in_kernel_space(unsigned long address)
113{
114 return false;
115}
116
117#undef __init
118#define __init
119
120#undef __head
121#define __head
122
123#define __BOOT_COMPRESSED
124
125/* Basic instruction decoding support needed */
126#include "../../lib/inat.c"
127#include "../../lib/insn.c"
128
129/* Include code for early handlers */
130#include "../../coco/sev/shared.c"
131
132static struct svsm_ca *svsm_get_caa(void)
133{
134 return boot_svsm_caa;
135}
136
137static u64 svsm_get_caa_pa(void)
138{
139 return boot_svsm_caa_pa;
140}
141
142static int svsm_perform_call_protocol(struct svsm_call *call)
143{
144 struct ghcb *ghcb;
145 int ret;
146
147 if (boot_ghcb)
148 ghcb = boot_ghcb;
149 else
150 ghcb = NULL;
151
152 do {
153 ret = ghcb ? svsm_perform_ghcb_protocol(ghcb, call)
154 : svsm_perform_msr_protocol(call);
155 } while (ret == -EAGAIN);
156
157 return ret;
158}
159
160bool sev_snp_enabled(void)
161{
162 return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
163}
164
165static void __page_state_change(unsigned long paddr, enum psc_op op)
166{
167 u64 val;
168
169 if (!sev_snp_enabled())
170 return;
171
172 /*
173 * If private -> shared then invalidate the page before requesting the
174 * state change in the RMP table.
175 */
176 if (op == SNP_PAGE_STATE_SHARED)
177 pvalidate_4k_page(paddr, paddr, false);
178
179 /* Issue VMGEXIT to change the page state in RMP table. */
180 sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
181 VMGEXIT();
182
183 /* Read the response of the VMGEXIT. */
184 val = sev_es_rd_ghcb_msr();
185 if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))
186 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
187
188 /*
189 * Now that page state is changed in the RMP table, validate it so that it is
190 * consistent with the RMP entry.
191 */
192 if (op == SNP_PAGE_STATE_PRIVATE)
193 pvalidate_4k_page(paddr, paddr, true);
194}
195
196void snp_set_page_private(unsigned long paddr)
197{
198 __page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);
199}
200
201void snp_set_page_shared(unsigned long paddr)
202{
203 __page_state_change(paddr, SNP_PAGE_STATE_SHARED);
204}
205
206static bool early_setup_ghcb(void)
207{
208 if (set_page_decrypted((unsigned long)&boot_ghcb_page))
209 return false;
210
211 /* Page is now mapped decrypted, clear it */
212 memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));
213
214 boot_ghcb = &boot_ghcb_page;
215
216 /* Initialize lookup tables for the instruction decoder */
217 inat_init_tables();
218
219 /* SNP guest requires the GHCB GPA must be registered */
220 if (sev_snp_enabled())
221 snp_register_ghcb_early(__pa(&boot_ghcb_page));
222
223 return true;
224}
225
226static phys_addr_t __snp_accept_memory(struct snp_psc_desc *desc,
227 phys_addr_t pa, phys_addr_t pa_end)
228{
229 struct psc_hdr *hdr;
230 struct psc_entry *e;
231 unsigned int i;
232
233 hdr = &desc->hdr;
234 memset(hdr, 0, sizeof(*hdr));
235
236 e = desc->entries;
237
238 i = 0;
239 while (pa < pa_end && i < VMGEXIT_PSC_MAX_ENTRY) {
240 hdr->end_entry = i;
241
242 e->gfn = pa >> PAGE_SHIFT;
243 e->operation = SNP_PAGE_STATE_PRIVATE;
244 if (IS_ALIGNED(pa, PMD_SIZE) && (pa_end - pa) >= PMD_SIZE) {
245 e->pagesize = RMP_PG_SIZE_2M;
246 pa += PMD_SIZE;
247 } else {
248 e->pagesize = RMP_PG_SIZE_4K;
249 pa += PAGE_SIZE;
250 }
251
252 e++;
253 i++;
254 }
255
256 if (vmgexit_psc(boot_ghcb, desc))
257 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
258
259 pvalidate_pages(desc);
260
261 return pa;
262}
263
264void snp_accept_memory(phys_addr_t start, phys_addr_t end)
265{
266 struct snp_psc_desc desc = {};
267 unsigned int i;
268 phys_addr_t pa;
269
270 if (!boot_ghcb && !early_setup_ghcb())
271 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
272
273 pa = start;
274 while (pa < end)
275 pa = __snp_accept_memory(&desc, pa, end);
276}
277
278void sev_es_shutdown_ghcb(void)
279{
280 if (!boot_ghcb)
281 return;
282
283 if (!sev_es_check_cpu_features())
284 error("SEV-ES CPU Features missing.");
285
286 /*
287 * This denotes whether to use the GHCB MSR protocol or the GHCB
288 * shared page to perform a GHCB request. Since the GHCB page is
289 * being changed to encrypted, it can't be used to perform GHCB
290 * requests. Clear the boot_ghcb variable so that the GHCB MSR
291 * protocol is used to change the GHCB page over to an encrypted
292 * page.
293 */
294 boot_ghcb = NULL;
295
296 /*
297 * GHCB Page must be flushed from the cache and mapped encrypted again.
298 * Otherwise the running kernel will see strange cache effects when
299 * trying to use that page.
300 */
301 if (set_page_encrypted((unsigned long)&boot_ghcb_page))
302 error("Can't map GHCB page encrypted");
303
304 /*
305 * GHCB page is mapped encrypted again and flushed from the cache.
306 * Mark it non-present now to catch bugs when #VC exceptions trigger
307 * after this point.
308 */
309 if (set_page_non_present((unsigned long)&boot_ghcb_page))
310 error("Can't unmap GHCB page");
311}
312
313static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set,
314 unsigned int reason, u64 exit_info_2)
315{
316 u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason);
317
318 vc_ghcb_invalidate(ghcb);
319 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST);
320 ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
321 ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
322
323 sev_es_wr_ghcb_msr(__pa(ghcb));
324 VMGEXIT();
325
326 while (true)
327 asm volatile("hlt\n" : : : "memory");
328}
329
330bool sev_es_check_ghcb_fault(unsigned long address)
331{
332 /* Check whether the fault was on the GHCB page */
333 return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);
334}
335
336void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
337{
338 struct es_em_ctxt ctxt;
339 enum es_result result;
340
341 if (!boot_ghcb && !early_setup_ghcb())
342 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
343
344 vc_ghcb_invalidate(boot_ghcb);
345 result = vc_init_em_ctxt(&ctxt, regs, exit_code);
346 if (result != ES_OK)
347 goto finish;
348
349 result = vc_check_opcode_bytes(&ctxt, exit_code);
350 if (result != ES_OK)
351 goto finish;
352
353 switch (exit_code) {
354 case SVM_EXIT_RDTSC:
355 case SVM_EXIT_RDTSCP:
356 result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code);
357 break;
358 case SVM_EXIT_IOIO:
359 result = vc_handle_ioio(boot_ghcb, &ctxt);
360 break;
361 case SVM_EXIT_CPUID:
362 result = vc_handle_cpuid(boot_ghcb, &ctxt);
363 break;
364 default:
365 result = ES_UNSUPPORTED;
366 break;
367 }
368
369finish:
370 if (result == ES_OK)
371 vc_finish_insn(&ctxt);
372 else if (result != ES_RETRY)
373 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
374}
375
376/*
377 * SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need
378 * guest side implementation for proper functioning of the guest. If any
379 * of these features are enabled in the hypervisor but are lacking guest
380 * side implementation, the behavior of the guest will be undefined. The
381 * guest could fail in non-obvious way making it difficult to debug.
382 *
383 * As the behavior of reserved feature bits is unknown to be on the
384 * safe side add them to the required features mask.
385 */
386#define SNP_FEATURES_IMPL_REQ (MSR_AMD64_SNP_VTOM | \
387 MSR_AMD64_SNP_REFLECT_VC | \
388 MSR_AMD64_SNP_RESTRICTED_INJ | \
389 MSR_AMD64_SNP_ALT_INJ | \
390 MSR_AMD64_SNP_DEBUG_SWAP | \
391 MSR_AMD64_SNP_VMPL_SSS | \
392 MSR_AMD64_SNP_SECURE_TSC | \
393 MSR_AMD64_SNP_VMGEXIT_PARAM | \
394 MSR_AMD64_SNP_VMSA_REG_PROT | \
395 MSR_AMD64_SNP_RESERVED_BIT13 | \
396 MSR_AMD64_SNP_RESERVED_BIT15 | \
397 MSR_AMD64_SNP_RESERVED_MASK)
398
399/*
400 * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
401 * by the guest kernel. As and when a new feature is implemented in the
402 * guest kernel, a corresponding bit should be added to the mask.
403 */
404#define SNP_FEATURES_PRESENT MSR_AMD64_SNP_DEBUG_SWAP
405
406u64 snp_get_unsupported_features(u64 status)
407{
408 if (!(status & MSR_AMD64_SEV_SNP_ENABLED))
409 return 0;
410
411 return status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT;
412}
413
414void snp_check_features(void)
415{
416 u64 unsupported;
417
418 /*
419 * Terminate the boot if hypervisor has enabled any feature lacking
420 * guest side implementation. Pass on the unsupported features mask through
421 * EXIT_INFO_2 of the GHCB protocol so that those features can be reported
422 * as part of the guest boot failure.
423 */
424 unsupported = snp_get_unsupported_features(sev_status);
425 if (unsupported) {
426 if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb()))
427 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
428
429 sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN,
430 GHCB_SNP_UNSUPPORTED, unsupported);
431 }
432}
433
434/* Search for Confidential Computing blob in the EFI config table. */
435static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)
436{
437 unsigned long cfg_table_pa;
438 unsigned int cfg_table_len;
439 int ret;
440
441 ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);
442 if (ret)
443 return NULL;
444
445 return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,
446 cfg_table_len,
447 EFI_CC_BLOB_GUID);
448}
449
450/*
451 * Initial set up of SNP relies on information provided by the
452 * Confidential Computing blob, which can be passed to the boot kernel
453 * by firmware/bootloader in the following ways:
454 *
455 * - via an entry in the EFI config table
456 * - via a setup_data structure, as defined by the Linux Boot Protocol
457 *
458 * Scan for the blob in that order.
459 */
460static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
461{
462 struct cc_blob_sev_info *cc_info;
463
464 cc_info = find_cc_blob_efi(bp);
465 if (cc_info)
466 goto found_cc_info;
467
468 cc_info = find_cc_blob_setup_data(bp);
469 if (!cc_info)
470 return NULL;
471
472found_cc_info:
473 if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
474 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
475
476 return cc_info;
477}
478
479/*
480 * Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks
481 * will verify the SNP CPUID/MSR bits.
482 */
483static bool early_snp_init(struct boot_params *bp)
484{
485 struct cc_blob_sev_info *cc_info;
486
487 if (!bp)
488 return false;
489
490 cc_info = find_cc_blob(bp);
491 if (!cc_info)
492 return false;
493
494 /*
495 * If a SNP-specific Confidential Computing blob is present, then
496 * firmware/bootloader have indicated SNP support. Verifying this
497 * involves CPUID checks which will be more reliable if the SNP
498 * CPUID table is used. See comments over snp_setup_cpuid_table() for
499 * more details.
500 */
501 setup_cpuid_table(cc_info);
502
503 /*
504 * Record the SVSM Calling Area (CA) address if the guest is not
505 * running at VMPL0. The CA will be used to communicate with the
506 * SVSM and request its services.
507 */
508 svsm_setup_ca(cc_info);
509
510 /*
511 * Pass run-time kernel a pointer to CC info via boot_params so EFI
512 * config table doesn't need to be searched again during early startup
513 * phase.
514 */
515 bp->cc_blob_address = (u32)(unsigned long)cc_info;
516
517 return true;
518}
519
520/*
521 * sev_check_cpu_support - Check for SEV support in the CPU capabilities
522 *
523 * Returns < 0 if SEV is not supported, otherwise the position of the
524 * encryption bit in the page table descriptors.
525 */
526static int sev_check_cpu_support(void)
527{
528 unsigned int eax, ebx, ecx, edx;
529
530 /* Check for the SME/SEV support leaf */
531 eax = 0x80000000;
532 ecx = 0;
533 native_cpuid(&eax, &ebx, &ecx, &edx);
534 if (eax < 0x8000001f)
535 return -ENODEV;
536
537 /*
538 * Check for the SME/SEV feature:
539 * CPUID Fn8000_001F[EAX]
540 * - Bit 0 - Secure Memory Encryption support
541 * - Bit 1 - Secure Encrypted Virtualization support
542 * CPUID Fn8000_001F[EBX]
543 * - Bits 5:0 - Pagetable bit position used to indicate encryption
544 */
545 eax = 0x8000001f;
546 ecx = 0;
547 native_cpuid(&eax, &ebx, &ecx, &edx);
548 /* Check whether SEV is supported */
549 if (!(eax & BIT(1)))
550 return -ENODEV;
551
552 return ebx & 0x3f;
553}
554
555void sev_enable(struct boot_params *bp)
556{
557 struct msr m;
558 int bitpos;
559 bool snp;
560
561 /*
562 * bp->cc_blob_address should only be set by boot/compressed kernel.
563 * Initialize it to 0 to ensure that uninitialized values from
564 * buggy bootloaders aren't propagated.
565 */
566 if (bp)
567 bp->cc_blob_address = 0;
568
569 /*
570 * Do an initial SEV capability check before early_snp_init() which
571 * loads the CPUID page and the same checks afterwards are done
572 * without the hypervisor and are trustworthy.
573 *
574 * If the HV fakes SEV support, the guest will crash'n'burn
575 * which is good enough.
576 */
577
578 if (sev_check_cpu_support() < 0)
579 return;
580
581 /*
582 * Setup/preliminary detection of SNP. This will be sanity-checked
583 * against CPUID/MSR values later.
584 */
585 snp = early_snp_init(bp);
586
587 /* Now repeat the checks with the SNP CPUID table. */
588
589 bitpos = sev_check_cpu_support();
590 if (bitpos < 0) {
591 if (snp)
592 error("SEV-SNP support indicated by CC blob, but not CPUID.");
593 return;
594 }
595
596 /* Set the SME mask if this is an SEV guest. */
597 boot_rdmsr(MSR_AMD64_SEV, &m);
598 sev_status = m.q;
599 if (!(sev_status & MSR_AMD64_SEV_ENABLED))
600 return;
601
602 /* Negotiate the GHCB protocol version. */
603 if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {
604 if (!sev_es_negotiate_protocol())
605 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);
606 }
607
608 /*
609 * SNP is supported in v2 of the GHCB spec which mandates support for HV
610 * features.
611 */
612 if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
613 u64 hv_features;
614 int ret;
615
616 hv_features = get_hv_features();
617 if (!(hv_features & GHCB_HV_FT_SNP))
618 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
619
620 /*
621 * Enforce running at VMPL0 or with an SVSM.
622 *
623 * Use RMPADJUST (see the rmpadjust() function for a description of
624 * what the instruction does) to update the VMPL1 permissions of a
625 * page. If the guest is running at VMPL0, this will succeed. If the
626 * guest is running at any other VMPL, this will fail. Linux SNP guests
627 * only ever run at a single VMPL level so permission mask changes of a
628 * lesser-privileged VMPL are a don't-care.
629 */
630 ret = rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, 1);
631
632 /*
633 * Running at VMPL0 is not required if an SVSM is present and the hypervisor
634 * supports the required SVSM GHCB events.
635 */
636 if (ret &&
637 !(snp_vmpl && (hv_features & GHCB_HV_FT_SNP_MULTI_VMPL)))
638 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
639 }
640
641 if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
642 error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");
643
644 sme_me_mask = BIT_ULL(bitpos);
645}
646
647/*
648 * sev_get_status - Retrieve the SEV status mask
649 *
650 * Returns 0 if the CPU is not SEV capable, otherwise the value of the
651 * AMD64_SEV MSR.
652 */
653u64 sev_get_status(void)
654{
655 struct msr m;
656
657 if (sev_check_cpu_support() < 0)
658 return 0;
659
660 boot_rdmsr(MSR_AMD64_SEV, &m);
661 return m.q;
662}
663
664void sev_prep_identity_maps(unsigned long top_level_pgt)
665{
666 /*
667 * The Confidential Computing blob is used very early in uncompressed
668 * kernel to find the in-memory CPUID table to handle CPUID
669 * instructions. Make sure an identity-mapping exists so it can be
670 * accessed after switchover.
671 */
672 if (sev_snp_enabled()) {
673 unsigned long cc_info_pa = boot_params_ptr->cc_blob_address;
674 struct cc_blob_sev_info *cc_info;
675
676 kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));
677
678 cc_info = (struct cc_blob_sev_info *)cc_info_pa;
679 kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);
680 }
681
682 sev_verify_cbit(top_level_pgt);
683}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * AMD Encrypted Register State Support
4 *
5 * Author: Joerg Roedel <jroedel@suse.de>
6 */
7
8/*
9 * misc.h needs to be first because it knows how to include the other kernel
10 * headers in the pre-decompression code in a way that does not break
11 * compilation.
12 */
13#include "misc.h"
14
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.h>
24
25#include "error.h"
26#include "../msr.h"
27
28struct ghcb boot_ghcb_page __aligned(PAGE_SIZE);
29struct ghcb *boot_ghcb;
30
31/*
32 * Copy a version of this function here - insn-eval.c can't be used in
33 * pre-decompression code.
34 */
35static bool insn_has_rep_prefix(struct insn *insn)
36{
37 insn_byte_t p;
38 int i;
39
40 insn_get_prefixes(insn);
41
42 for_each_insn_prefix(insn, i, p) {
43 if (p == 0xf2 || p == 0xf3)
44 return true;
45 }
46
47 return false;
48}
49
50/*
51 * Only a dummy for insn_get_seg_base() - Early boot-code is 64bit only and
52 * doesn't use segments.
53 */
54static unsigned long insn_get_seg_base(struct pt_regs *regs, int seg_reg_idx)
55{
56 return 0UL;
57}
58
59static inline u64 sev_es_rd_ghcb_msr(void)
60{
61 struct msr m;
62
63 boot_rdmsr(MSR_AMD64_SEV_ES_GHCB, &m);
64
65 return m.q;
66}
67
68static inline void sev_es_wr_ghcb_msr(u64 val)
69{
70 struct msr m;
71
72 m.q = val;
73 boot_wrmsr(MSR_AMD64_SEV_ES_GHCB, &m);
74}
75
76static enum es_result vc_decode_insn(struct es_em_ctxt *ctxt)
77{
78 char buffer[MAX_INSN_SIZE];
79 int ret;
80
81 memcpy(buffer, (unsigned char *)ctxt->regs->ip, MAX_INSN_SIZE);
82
83 ret = insn_decode(&ctxt->insn, buffer, MAX_INSN_SIZE, INSN_MODE_64);
84 if (ret < 0)
85 return ES_DECODE_FAILED;
86
87 return ES_OK;
88}
89
90static enum es_result vc_write_mem(struct es_em_ctxt *ctxt,
91 void *dst, char *buf, size_t size)
92{
93 memcpy(dst, buf, size);
94
95 return ES_OK;
96}
97
98static enum es_result vc_read_mem(struct es_em_ctxt *ctxt,
99 void *src, char *buf, size_t size)
100{
101 memcpy(buf, src, size);
102
103 return ES_OK;
104}
105
106#undef __init
107#undef __pa
108#define __init
109#define __pa(x) ((unsigned long)(x))
110
111#define __BOOT_COMPRESSED
112
113/* Basic instruction decoding support needed */
114#include "../../lib/inat.c"
115#include "../../lib/insn.c"
116
117/* Include code for early handlers */
118#include "../../kernel/sev-shared.c"
119
120static inline bool sev_snp_enabled(void)
121{
122 return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
123}
124
125static void __page_state_change(unsigned long paddr, enum psc_op op)
126{
127 u64 val;
128
129 if (!sev_snp_enabled())
130 return;
131
132 /*
133 * If private -> shared then invalidate the page before requesting the
134 * state change in the RMP table.
135 */
136 if (op == SNP_PAGE_STATE_SHARED && pvalidate(paddr, RMP_PG_SIZE_4K, 0))
137 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
138
139 /* Issue VMGEXIT to change the page state in RMP table. */
140 sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op));
141 VMGEXIT();
142
143 /* Read the response of the VMGEXIT. */
144 val = sev_es_rd_ghcb_msr();
145 if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val))
146 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC);
147
148 /*
149 * Now that page state is changed in the RMP table, validate it so that it is
150 * consistent with the RMP entry.
151 */
152 if (op == SNP_PAGE_STATE_PRIVATE && pvalidate(paddr, RMP_PG_SIZE_4K, 1))
153 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE);
154}
155
156void snp_set_page_private(unsigned long paddr)
157{
158 __page_state_change(paddr, SNP_PAGE_STATE_PRIVATE);
159}
160
161void snp_set_page_shared(unsigned long paddr)
162{
163 __page_state_change(paddr, SNP_PAGE_STATE_SHARED);
164}
165
166static bool early_setup_ghcb(void)
167{
168 if (set_page_decrypted((unsigned long)&boot_ghcb_page))
169 return false;
170
171 /* Page is now mapped decrypted, clear it */
172 memset(&boot_ghcb_page, 0, sizeof(boot_ghcb_page));
173
174 boot_ghcb = &boot_ghcb_page;
175
176 /* Initialize lookup tables for the instruction decoder */
177 inat_init_tables();
178
179 /* SNP guest requires the GHCB GPA must be registered */
180 if (sev_snp_enabled())
181 snp_register_ghcb_early(__pa(&boot_ghcb_page));
182
183 return true;
184}
185
186void sev_es_shutdown_ghcb(void)
187{
188 if (!boot_ghcb)
189 return;
190
191 if (!sev_es_check_cpu_features())
192 error("SEV-ES CPU Features missing.");
193
194 /*
195 * GHCB Page must be flushed from the cache and mapped encrypted again.
196 * Otherwise the running kernel will see strange cache effects when
197 * trying to use that page.
198 */
199 if (set_page_encrypted((unsigned long)&boot_ghcb_page))
200 error("Can't map GHCB page encrypted");
201
202 /*
203 * GHCB page is mapped encrypted again and flushed from the cache.
204 * Mark it non-present now to catch bugs when #VC exceptions trigger
205 * after this point.
206 */
207 if (set_page_non_present((unsigned long)&boot_ghcb_page))
208 error("Can't unmap GHCB page");
209}
210
211static void __noreturn sev_es_ghcb_terminate(struct ghcb *ghcb, unsigned int set,
212 unsigned int reason, u64 exit_info_2)
213{
214 u64 exit_info_1 = SVM_VMGEXIT_TERM_REASON(set, reason);
215
216 vc_ghcb_invalidate(ghcb);
217 ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_TERM_REQUEST);
218 ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
219 ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
220
221 sev_es_wr_ghcb_msr(__pa(ghcb));
222 VMGEXIT();
223
224 while (true)
225 asm volatile("hlt\n" : : : "memory");
226}
227
228bool sev_es_check_ghcb_fault(unsigned long address)
229{
230 /* Check whether the fault was on the GHCB page */
231 return ((address & PAGE_MASK) == (unsigned long)&boot_ghcb_page);
232}
233
234void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
235{
236 struct es_em_ctxt ctxt;
237 enum es_result result;
238
239 if (!boot_ghcb && !early_setup_ghcb())
240 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
241
242 vc_ghcb_invalidate(boot_ghcb);
243 result = vc_init_em_ctxt(&ctxt, regs, exit_code);
244 if (result != ES_OK)
245 goto finish;
246
247 switch (exit_code) {
248 case SVM_EXIT_RDTSC:
249 case SVM_EXIT_RDTSCP:
250 result = vc_handle_rdtsc(boot_ghcb, &ctxt, exit_code);
251 break;
252 case SVM_EXIT_IOIO:
253 result = vc_handle_ioio(boot_ghcb, &ctxt);
254 break;
255 case SVM_EXIT_CPUID:
256 result = vc_handle_cpuid(boot_ghcb, &ctxt);
257 break;
258 default:
259 result = ES_UNSUPPORTED;
260 break;
261 }
262
263finish:
264 if (result == ES_OK)
265 vc_finish_insn(&ctxt);
266 else if (result != ES_RETRY)
267 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
268}
269
270static void enforce_vmpl0(void)
271{
272 u64 attrs;
273 int err;
274
275 /*
276 * RMPADJUST modifies RMP permissions of a lesser-privileged (numerically
277 * higher) privilege level. Here, clear the VMPL1 permission mask of the
278 * GHCB page. If the guest is not running at VMPL0, this will fail.
279 *
280 * If the guest is running at VMPL0, it will succeed. Even if that operation
281 * modifies permission bits, it is still ok to do so currently because Linux
282 * SNP guests are supported only on VMPL0 so VMPL1 or higher permission masks
283 * changing is a don't-care.
284 */
285 attrs = 1;
286 if (rmpadjust((unsigned long)&boot_ghcb_page, RMP_PG_SIZE_4K, attrs))
287 sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
288}
289
290/*
291 * SNP_FEATURES_IMPL_REQ is the mask of SNP features that will need
292 * guest side implementation for proper functioning of the guest. If any
293 * of these features are enabled in the hypervisor but are lacking guest
294 * side implementation, the behavior of the guest will be undefined. The
295 * guest could fail in non-obvious way making it difficult to debug.
296 *
297 * As the behavior of reserved feature bits is unknown to be on the
298 * safe side add them to the required features mask.
299 */
300#define SNP_FEATURES_IMPL_REQ (MSR_AMD64_SNP_VTOM | \
301 MSR_AMD64_SNP_REFLECT_VC | \
302 MSR_AMD64_SNP_RESTRICTED_INJ | \
303 MSR_AMD64_SNP_ALT_INJ | \
304 MSR_AMD64_SNP_DEBUG_SWAP | \
305 MSR_AMD64_SNP_VMPL_SSS | \
306 MSR_AMD64_SNP_SECURE_TSC | \
307 MSR_AMD64_SNP_VMGEXIT_PARAM | \
308 MSR_AMD64_SNP_VMSA_REG_PROTECTION | \
309 MSR_AMD64_SNP_RESERVED_BIT13 | \
310 MSR_AMD64_SNP_RESERVED_BIT15 | \
311 MSR_AMD64_SNP_RESERVED_MASK)
312
313/*
314 * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
315 * by the guest kernel. As and when a new feature is implemented in the
316 * guest kernel, a corresponding bit should be added to the mask.
317 */
318#define SNP_FEATURES_PRESENT (0)
319
320void snp_check_features(void)
321{
322 u64 unsupported;
323
324 if (!(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
325 return;
326
327 /*
328 * Terminate the boot if hypervisor has enabled any feature lacking
329 * guest side implementation. Pass on the unsupported features mask through
330 * EXIT_INFO_2 of the GHCB protocol so that those features can be reported
331 * as part of the guest boot failure.
332 */
333 unsupported = sev_status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT;
334 if (unsupported) {
335 if (ghcb_version < 2 || (!boot_ghcb && !early_setup_ghcb()))
336 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
337
338 sev_es_ghcb_terminate(boot_ghcb, SEV_TERM_SET_GEN,
339 GHCB_SNP_UNSUPPORTED, unsupported);
340 }
341}
342
343void sev_enable(struct boot_params *bp)
344{
345 unsigned int eax, ebx, ecx, edx;
346 struct msr m;
347 bool snp;
348
349 /*
350 * bp->cc_blob_address should only be set by boot/compressed kernel.
351 * Initialize it to 0 to ensure that uninitialized values from
352 * buggy bootloaders aren't propagated.
353 */
354 if (bp)
355 bp->cc_blob_address = 0;
356
357 /*
358 * Setup/preliminary detection of SNP. This will be sanity-checked
359 * against CPUID/MSR values later.
360 */
361 snp = snp_init(bp);
362
363 /* Check for the SME/SEV support leaf */
364 eax = 0x80000000;
365 ecx = 0;
366 native_cpuid(&eax, &ebx, &ecx, &edx);
367 if (eax < 0x8000001f)
368 return;
369
370 /*
371 * Check for the SME/SEV feature:
372 * CPUID Fn8000_001F[EAX]
373 * - Bit 0 - Secure Memory Encryption support
374 * - Bit 1 - Secure Encrypted Virtualization support
375 * CPUID Fn8000_001F[EBX]
376 * - Bits 5:0 - Pagetable bit position used to indicate encryption
377 */
378 eax = 0x8000001f;
379 ecx = 0;
380 native_cpuid(&eax, &ebx, &ecx, &edx);
381 /* Check whether SEV is supported */
382 if (!(eax & BIT(1))) {
383 if (snp)
384 error("SEV-SNP support indicated by CC blob, but not CPUID.");
385 return;
386 }
387
388 /* Set the SME mask if this is an SEV guest. */
389 boot_rdmsr(MSR_AMD64_SEV, &m);
390 sev_status = m.q;
391 if (!(sev_status & MSR_AMD64_SEV_ENABLED))
392 return;
393
394 /* Negotiate the GHCB protocol version. */
395 if (sev_status & MSR_AMD64_SEV_ES_ENABLED) {
396 if (!sev_es_negotiate_protocol())
397 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);
398 }
399
400 /*
401 * SNP is supported in v2 of the GHCB spec which mandates support for HV
402 * features.
403 */
404 if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
405 if (!(get_hv_features() & GHCB_HV_FT_SNP))
406 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
407
408 enforce_vmpl0();
409 }
410
411 if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
412 error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");
413
414 sme_me_mask = BIT_ULL(ebx & 0x3f);
415}
416
417/* Search for Confidential Computing blob in the EFI config table. */
418static struct cc_blob_sev_info *find_cc_blob_efi(struct boot_params *bp)
419{
420 unsigned long cfg_table_pa;
421 unsigned int cfg_table_len;
422 int ret;
423
424 ret = efi_get_conf_table(bp, &cfg_table_pa, &cfg_table_len);
425 if (ret)
426 return NULL;
427
428 return (struct cc_blob_sev_info *)efi_find_vendor_table(bp, cfg_table_pa,
429 cfg_table_len,
430 EFI_CC_BLOB_GUID);
431}
432
433/*
434 * Initial set up of SNP relies on information provided by the
435 * Confidential Computing blob, which can be passed to the boot kernel
436 * by firmware/bootloader in the following ways:
437 *
438 * - via an entry in the EFI config table
439 * - via a setup_data structure, as defined by the Linux Boot Protocol
440 *
441 * Scan for the blob in that order.
442 */
443static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
444{
445 struct cc_blob_sev_info *cc_info;
446
447 cc_info = find_cc_blob_efi(bp);
448 if (cc_info)
449 goto found_cc_info;
450
451 cc_info = find_cc_blob_setup_data(bp);
452 if (!cc_info)
453 return NULL;
454
455found_cc_info:
456 if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
457 sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
458
459 return cc_info;
460}
461
462/*
463 * Indicate SNP based on presence of SNP-specific CC blob. Subsequent checks
464 * will verify the SNP CPUID/MSR bits.
465 */
466bool snp_init(struct boot_params *bp)
467{
468 struct cc_blob_sev_info *cc_info;
469
470 if (!bp)
471 return false;
472
473 cc_info = find_cc_blob(bp);
474 if (!cc_info)
475 return false;
476
477 /*
478 * If a SNP-specific Confidential Computing blob is present, then
479 * firmware/bootloader have indicated SNP support. Verifying this
480 * involves CPUID checks which will be more reliable if the SNP
481 * CPUID table is used. See comments over snp_setup_cpuid_table() for
482 * more details.
483 */
484 setup_cpuid_table(cc_info);
485
486 /*
487 * Pass run-time kernel a pointer to CC info via boot_params so EFI
488 * config table doesn't need to be searched again during early startup
489 * phase.
490 */
491 bp->cc_blob_address = (u32)(unsigned long)cc_info;
492
493 return true;
494}
495
496void sev_prep_identity_maps(unsigned long top_level_pgt)
497{
498 /*
499 * The Confidential Computing blob is used very early in uncompressed
500 * kernel to find the in-memory CPUID table to handle CPUID
501 * instructions. Make sure an identity-mapping exists so it can be
502 * accessed after switchover.
503 */
504 if (sev_snp_enabled()) {
505 unsigned long cc_info_pa = boot_params->cc_blob_address;
506 struct cc_blob_sev_info *cc_info;
507
508 kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));
509
510 cc_info = (struct cc_blob_sev_info *)cc_info_pa;
511 kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);
512 }
513
514 sev_verify_cbit(top_level_pgt);
515}