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