1dbca5e1aSKuppuswamy Sathyanarayanan // SPDX-License-Identifier: GPL-2.0-only
2dbca5e1aSKuppuswamy Sathyanarayanan /*
3dbca5e1aSKuppuswamy Sathyanarayanan * AMD Memory Encryption Support
4dbca5e1aSKuppuswamy Sathyanarayanan *
534ff6590STom Lendacky * Copyright (C) 2016-2024 Advanced Micro Devices, Inc.
6dbca5e1aSKuppuswamy Sathyanarayanan *
7dbca5e1aSKuppuswamy Sathyanarayanan * Author: Tom Lendacky <thomas.lendacky@amd.com>
8dbca5e1aSKuppuswamy Sathyanarayanan */
9dbca5e1aSKuppuswamy Sathyanarayanan
10dbca5e1aSKuppuswamy Sathyanarayanan #define DISABLE_BRANCH_PROFILING
11dbca5e1aSKuppuswamy Sathyanarayanan
12dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/linkage.h>
13dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/init.h>
14dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/mm.h>
15dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/dma-direct.h>
16dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/swiotlb.h>
17dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/mem_encrypt.h>
18dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/device.h>
19dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/kernel.h>
20dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/bitops.h>
21dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/dma-mapping.h>
22dbca5e1aSKuppuswamy Sathyanarayanan #include <linux/cc_platform.h>
23dbca5e1aSKuppuswamy Sathyanarayanan
24dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/tlbflush.h>
25dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/fixmap.h>
26dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/setup.h>
274867fbbdSAnshuman Khandual #include <asm/mem_encrypt.h>
28dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/bootparam.h>
29dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/set_memory.h>
30dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/cacheflush.h>
31dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/processor-flags.h>
32dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/msr.h>
33dbca5e1aSKuppuswamy Sathyanarayanan #include <asm/cmdline.h>
345e5ccff6SBrijesh Singh #include <asm/sev.h>
35b82a8dbdSKirill A. Shutemov #include <asm/ia32.h>
36dbca5e1aSKuppuswamy Sathyanarayanan
37dbca5e1aSKuppuswamy Sathyanarayanan #include "mm_internal.h"
38dbca5e1aSKuppuswamy Sathyanarayanan
39dbca5e1aSKuppuswamy Sathyanarayanan /*
40dbca5e1aSKuppuswamy Sathyanarayanan * Since SME related variables are set early in the boot process they must
41dbca5e1aSKuppuswamy Sathyanarayanan * reside in the .data section so as not to be zeroed out when the .bss
42dbca5e1aSKuppuswamy Sathyanarayanan * section is later cleared.
43dbca5e1aSKuppuswamy Sathyanarayanan */
44dbca5e1aSKuppuswamy Sathyanarayanan u64 sme_me_mask __section(".data") = 0;
45dbca5e1aSKuppuswamy Sathyanarayanan u64 sev_status __section(".data") = 0;
46dbca5e1aSKuppuswamy Sathyanarayanan u64 sev_check_data __section(".data") = 0;
47dbca5e1aSKuppuswamy Sathyanarayanan EXPORT_SYMBOL(sme_me_mask);
48dbca5e1aSKuppuswamy Sathyanarayanan
49dbca5e1aSKuppuswamy Sathyanarayanan /* Buffer used for early in-place encryption by BSP, no locking needed */
50dbca5e1aSKuppuswamy Sathyanarayanan static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE);
51dbca5e1aSKuppuswamy Sathyanarayanan
52dbca5e1aSKuppuswamy Sathyanarayanan /*
535e5ccff6SBrijesh Singh * SNP-specific routine which needs to additionally change the page state from
545e5ccff6SBrijesh Singh * private to shared before copying the data from the source to destination and
555e5ccff6SBrijesh Singh * restore after the copy.
565e5ccff6SBrijesh Singh */
snp_memcpy(void * dst,void * src,size_t sz,unsigned long paddr,bool decrypt)575e5ccff6SBrijesh Singh static inline void __init snp_memcpy(void *dst, void *src, size_t sz,
585e5ccff6SBrijesh Singh unsigned long paddr, bool decrypt)
595e5ccff6SBrijesh Singh {
605e5ccff6SBrijesh Singh unsigned long npages = PAGE_ALIGN(sz) >> PAGE_SHIFT;
615e5ccff6SBrijesh Singh
625e5ccff6SBrijesh Singh if (decrypt) {
635e5ccff6SBrijesh Singh /*
645e5ccff6SBrijesh Singh * @paddr needs to be accessed decrypted, mark the page shared in
655e5ccff6SBrijesh Singh * the RMP table before copying it.
665e5ccff6SBrijesh Singh */
675e5ccff6SBrijesh Singh early_snp_set_memory_shared((unsigned long)__va(paddr), paddr, npages);
685e5ccff6SBrijesh Singh
695e5ccff6SBrijesh Singh memcpy(dst, src, sz);
705e5ccff6SBrijesh Singh
715e5ccff6SBrijesh Singh /* Restore the page state after the memcpy. */
725e5ccff6SBrijesh Singh early_snp_set_memory_private((unsigned long)__va(paddr), paddr, npages);
735e5ccff6SBrijesh Singh } else {
745e5ccff6SBrijesh Singh /*
755e5ccff6SBrijesh Singh * @paddr need to be accessed encrypted, no need for the page state
765e5ccff6SBrijesh Singh * change.
775e5ccff6SBrijesh Singh */
785e5ccff6SBrijesh Singh memcpy(dst, src, sz);
795e5ccff6SBrijesh Singh }
805e5ccff6SBrijesh Singh }
815e5ccff6SBrijesh Singh
825e5ccff6SBrijesh Singh /*
83dbca5e1aSKuppuswamy Sathyanarayanan * This routine does not change the underlying encryption setting of the
84dbca5e1aSKuppuswamy Sathyanarayanan * page(s) that map this memory. It assumes that eventually the memory is
85dbca5e1aSKuppuswamy Sathyanarayanan * meant to be accessed as either encrypted or decrypted but the contents
86dbca5e1aSKuppuswamy Sathyanarayanan * are currently not in the desired state.
87dbca5e1aSKuppuswamy Sathyanarayanan *
88dbca5e1aSKuppuswamy Sathyanarayanan * This routine follows the steps outlined in the AMD64 Architecture
89dbca5e1aSKuppuswamy Sathyanarayanan * Programmer's Manual Volume 2, Section 7.10.8 Encrypt-in-Place.
90dbca5e1aSKuppuswamy Sathyanarayanan */
__sme_early_enc_dec(resource_size_t paddr,unsigned long size,bool enc)91dbca5e1aSKuppuswamy Sathyanarayanan static void __init __sme_early_enc_dec(resource_size_t paddr,
92dbca5e1aSKuppuswamy Sathyanarayanan unsigned long size, bool enc)
93dbca5e1aSKuppuswamy Sathyanarayanan {
94dbca5e1aSKuppuswamy Sathyanarayanan void *src, *dst;
95dbca5e1aSKuppuswamy Sathyanarayanan size_t len;
96dbca5e1aSKuppuswamy Sathyanarayanan
97dbca5e1aSKuppuswamy Sathyanarayanan if (!sme_me_mask)
98dbca5e1aSKuppuswamy Sathyanarayanan return;
99dbca5e1aSKuppuswamy Sathyanarayanan
100dbca5e1aSKuppuswamy Sathyanarayanan wbinvd();
101dbca5e1aSKuppuswamy Sathyanarayanan
102dbca5e1aSKuppuswamy Sathyanarayanan /*
103dbca5e1aSKuppuswamy Sathyanarayanan * There are limited number of early mapping slots, so map (at most)
104dbca5e1aSKuppuswamy Sathyanarayanan * one page at time.
105dbca5e1aSKuppuswamy Sathyanarayanan */
106dbca5e1aSKuppuswamy Sathyanarayanan while (size) {
107dbca5e1aSKuppuswamy Sathyanarayanan len = min_t(size_t, sizeof(sme_early_buffer), size);
108dbca5e1aSKuppuswamy Sathyanarayanan
109dbca5e1aSKuppuswamy Sathyanarayanan /*
110dbca5e1aSKuppuswamy Sathyanarayanan * Create mappings for the current and desired format of
111dbca5e1aSKuppuswamy Sathyanarayanan * the memory. Use a write-protected mapping for the source.
112dbca5e1aSKuppuswamy Sathyanarayanan */
113dbca5e1aSKuppuswamy Sathyanarayanan src = enc ? early_memremap_decrypted_wp(paddr, len) :
114dbca5e1aSKuppuswamy Sathyanarayanan early_memremap_encrypted_wp(paddr, len);
115dbca5e1aSKuppuswamy Sathyanarayanan
116dbca5e1aSKuppuswamy Sathyanarayanan dst = enc ? early_memremap_encrypted(paddr, len) :
117dbca5e1aSKuppuswamy Sathyanarayanan early_memremap_decrypted(paddr, len);
118dbca5e1aSKuppuswamy Sathyanarayanan
119dbca5e1aSKuppuswamy Sathyanarayanan /*
120dbca5e1aSKuppuswamy Sathyanarayanan * If a mapping can't be obtained to perform the operation,
121dbca5e1aSKuppuswamy Sathyanarayanan * then eventual access of that area in the desired mode
122dbca5e1aSKuppuswamy Sathyanarayanan * will cause a crash.
123dbca5e1aSKuppuswamy Sathyanarayanan */
124dbca5e1aSKuppuswamy Sathyanarayanan BUG_ON(!src || !dst);
125dbca5e1aSKuppuswamy Sathyanarayanan
126dbca5e1aSKuppuswamy Sathyanarayanan /*
127dbca5e1aSKuppuswamy Sathyanarayanan * Use a temporary buffer, of cache-line multiple size, to
128dbca5e1aSKuppuswamy Sathyanarayanan * avoid data corruption as documented in the APM.
129dbca5e1aSKuppuswamy Sathyanarayanan */
1305e5ccff6SBrijesh Singh if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
1315e5ccff6SBrijesh Singh snp_memcpy(sme_early_buffer, src, len, paddr, enc);
1325e5ccff6SBrijesh Singh snp_memcpy(dst, sme_early_buffer, len, paddr, !enc);
1335e5ccff6SBrijesh Singh } else {
134dbca5e1aSKuppuswamy Sathyanarayanan memcpy(sme_early_buffer, src, len);
135dbca5e1aSKuppuswamy Sathyanarayanan memcpy(dst, sme_early_buffer, len);
1365e5ccff6SBrijesh Singh }
137dbca5e1aSKuppuswamy Sathyanarayanan
138dbca5e1aSKuppuswamy Sathyanarayanan early_memunmap(dst, len);
139dbca5e1aSKuppuswamy Sathyanarayanan early_memunmap(src, len);
140dbca5e1aSKuppuswamy Sathyanarayanan
141dbca5e1aSKuppuswamy Sathyanarayanan paddr += len;
142dbca5e1aSKuppuswamy Sathyanarayanan size -= len;
143dbca5e1aSKuppuswamy Sathyanarayanan }
144dbca5e1aSKuppuswamy Sathyanarayanan }
145dbca5e1aSKuppuswamy Sathyanarayanan
sme_early_encrypt(resource_size_t paddr,unsigned long size)146dbca5e1aSKuppuswamy Sathyanarayanan void __init sme_early_encrypt(resource_size_t paddr, unsigned long size)
147dbca5e1aSKuppuswamy Sathyanarayanan {
148dbca5e1aSKuppuswamy Sathyanarayanan __sme_early_enc_dec(paddr, size, true);
149dbca5e1aSKuppuswamy Sathyanarayanan }
150dbca5e1aSKuppuswamy Sathyanarayanan
sme_early_decrypt(resource_size_t paddr,unsigned long size)151dbca5e1aSKuppuswamy Sathyanarayanan void __init sme_early_decrypt(resource_size_t paddr, unsigned long size)
152dbca5e1aSKuppuswamy Sathyanarayanan {
153dbca5e1aSKuppuswamy Sathyanarayanan __sme_early_enc_dec(paddr, size, false);
154dbca5e1aSKuppuswamy Sathyanarayanan }
155dbca5e1aSKuppuswamy Sathyanarayanan
__sme_early_map_unmap_mem(void * vaddr,unsigned long size,bool map)156dbca5e1aSKuppuswamy Sathyanarayanan static void __init __sme_early_map_unmap_mem(void *vaddr, unsigned long size,
157dbca5e1aSKuppuswamy Sathyanarayanan bool map)
158dbca5e1aSKuppuswamy Sathyanarayanan {
159dbca5e1aSKuppuswamy Sathyanarayanan unsigned long paddr = (unsigned long)vaddr - __PAGE_OFFSET;
160dbca5e1aSKuppuswamy Sathyanarayanan pmdval_t pmd_flags, pmd;
161dbca5e1aSKuppuswamy Sathyanarayanan
162dbca5e1aSKuppuswamy Sathyanarayanan /* Use early_pmd_flags but remove the encryption mask */
163dbca5e1aSKuppuswamy Sathyanarayanan pmd_flags = __sme_clr(early_pmd_flags);
164dbca5e1aSKuppuswamy Sathyanarayanan
165dbca5e1aSKuppuswamy Sathyanarayanan do {
166dbca5e1aSKuppuswamy Sathyanarayanan pmd = map ? (paddr & PMD_MASK) + pmd_flags : 0;
167dbca5e1aSKuppuswamy Sathyanarayanan __early_make_pgtable((unsigned long)vaddr, pmd);
168dbca5e1aSKuppuswamy Sathyanarayanan
169dbca5e1aSKuppuswamy Sathyanarayanan vaddr += PMD_SIZE;
170dbca5e1aSKuppuswamy Sathyanarayanan paddr += PMD_SIZE;
171dbca5e1aSKuppuswamy Sathyanarayanan size = (size <= PMD_SIZE) ? 0 : size - PMD_SIZE;
172dbca5e1aSKuppuswamy Sathyanarayanan } while (size);
173dbca5e1aSKuppuswamy Sathyanarayanan
174dbca5e1aSKuppuswamy Sathyanarayanan flush_tlb_local();
175dbca5e1aSKuppuswamy Sathyanarayanan }
176dbca5e1aSKuppuswamy Sathyanarayanan
sme_unmap_bootdata(char * real_mode_data)177dbca5e1aSKuppuswamy Sathyanarayanan void __init sme_unmap_bootdata(char *real_mode_data)
178dbca5e1aSKuppuswamy Sathyanarayanan {
179dbca5e1aSKuppuswamy Sathyanarayanan struct boot_params *boot_data;
180dbca5e1aSKuppuswamy Sathyanarayanan unsigned long cmdline_paddr;
181dbca5e1aSKuppuswamy Sathyanarayanan
182dbca5e1aSKuppuswamy Sathyanarayanan if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
183dbca5e1aSKuppuswamy Sathyanarayanan return;
184dbca5e1aSKuppuswamy Sathyanarayanan
185dbca5e1aSKuppuswamy Sathyanarayanan /* Get the command line address before unmapping the real_mode_data */
186dbca5e1aSKuppuswamy Sathyanarayanan boot_data = (struct boot_params *)real_mode_data;
187dbca5e1aSKuppuswamy Sathyanarayanan cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
188dbca5e1aSKuppuswamy Sathyanarayanan
189dbca5e1aSKuppuswamy Sathyanarayanan __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), false);
190dbca5e1aSKuppuswamy Sathyanarayanan
191dbca5e1aSKuppuswamy Sathyanarayanan if (!cmdline_paddr)
192dbca5e1aSKuppuswamy Sathyanarayanan return;
193dbca5e1aSKuppuswamy Sathyanarayanan
194dbca5e1aSKuppuswamy Sathyanarayanan __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, false);
195dbca5e1aSKuppuswamy Sathyanarayanan }
196dbca5e1aSKuppuswamy Sathyanarayanan
sme_map_bootdata(char * real_mode_data)197dbca5e1aSKuppuswamy Sathyanarayanan void __init sme_map_bootdata(char *real_mode_data)
198dbca5e1aSKuppuswamy Sathyanarayanan {
199dbca5e1aSKuppuswamy Sathyanarayanan struct boot_params *boot_data;
200dbca5e1aSKuppuswamy Sathyanarayanan unsigned long cmdline_paddr;
201dbca5e1aSKuppuswamy Sathyanarayanan
202dbca5e1aSKuppuswamy Sathyanarayanan if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
203dbca5e1aSKuppuswamy Sathyanarayanan return;
204dbca5e1aSKuppuswamy Sathyanarayanan
205dbca5e1aSKuppuswamy Sathyanarayanan __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), true);
206dbca5e1aSKuppuswamy Sathyanarayanan
207dbca5e1aSKuppuswamy Sathyanarayanan /* Get the command line address after mapping the real_mode_data */
208dbca5e1aSKuppuswamy Sathyanarayanan boot_data = (struct boot_params *)real_mode_data;
209dbca5e1aSKuppuswamy Sathyanarayanan cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
210dbca5e1aSKuppuswamy Sathyanarayanan
211dbca5e1aSKuppuswamy Sathyanarayanan if (!cmdline_paddr)
212dbca5e1aSKuppuswamy Sathyanarayanan return;
213dbca5e1aSKuppuswamy Sathyanarayanan
214dbca5e1aSKuppuswamy Sathyanarayanan __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
215dbca5e1aSKuppuswamy Sathyanarayanan }
216dbca5e1aSKuppuswamy Sathyanarayanan
pg_level_to_pfn(int level,pte_t * kpte,pgprot_t * ret_prot)217dbca5e1aSKuppuswamy Sathyanarayanan static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot)
218dbca5e1aSKuppuswamy Sathyanarayanan {
219dbca5e1aSKuppuswamy Sathyanarayanan unsigned long pfn = 0;
220dbca5e1aSKuppuswamy Sathyanarayanan pgprot_t prot;
221dbca5e1aSKuppuswamy Sathyanarayanan
222dbca5e1aSKuppuswamy Sathyanarayanan switch (level) {
223dbca5e1aSKuppuswamy Sathyanarayanan case PG_LEVEL_4K:
224dbca5e1aSKuppuswamy Sathyanarayanan pfn = pte_pfn(*kpte);
225dbca5e1aSKuppuswamy Sathyanarayanan prot = pte_pgprot(*kpte);
226dbca5e1aSKuppuswamy Sathyanarayanan break;
227dbca5e1aSKuppuswamy Sathyanarayanan case PG_LEVEL_2M:
228dbca5e1aSKuppuswamy Sathyanarayanan pfn = pmd_pfn(*(pmd_t *)kpte);
229dbca5e1aSKuppuswamy Sathyanarayanan prot = pmd_pgprot(*(pmd_t *)kpte);
230dbca5e1aSKuppuswamy Sathyanarayanan break;
231dbca5e1aSKuppuswamy Sathyanarayanan case PG_LEVEL_1G:
232dbca5e1aSKuppuswamy Sathyanarayanan pfn = pud_pfn(*(pud_t *)kpte);
233dbca5e1aSKuppuswamy Sathyanarayanan prot = pud_pgprot(*(pud_t *)kpte);
234dbca5e1aSKuppuswamy Sathyanarayanan break;
235dbca5e1aSKuppuswamy Sathyanarayanan default:
236dbca5e1aSKuppuswamy Sathyanarayanan WARN_ONCE(1, "Invalid level for kpte\n");
237dbca5e1aSKuppuswamy Sathyanarayanan return 0;
238dbca5e1aSKuppuswamy Sathyanarayanan }
239dbca5e1aSKuppuswamy Sathyanarayanan
240dbca5e1aSKuppuswamy Sathyanarayanan if (ret_prot)
241dbca5e1aSKuppuswamy Sathyanarayanan *ret_prot = prot;
242dbca5e1aSKuppuswamy Sathyanarayanan
243dbca5e1aSKuppuswamy Sathyanarayanan return pfn;
244dbca5e1aSKuppuswamy Sathyanarayanan }
245dbca5e1aSKuppuswamy Sathyanarayanan
amd_enc_tlb_flush_required(bool enc)2461e8c5971SBrijesh Singh static bool amd_enc_tlb_flush_required(bool enc)
2471e8c5971SBrijesh Singh {
2481e8c5971SBrijesh Singh return true;
2491e8c5971SBrijesh Singh }
2501e8c5971SBrijesh Singh
amd_enc_cache_flush_required(void)2511e8c5971SBrijesh Singh static bool amd_enc_cache_flush_required(void)
2521e8c5971SBrijesh Singh {
2531e8c5971SBrijesh Singh return !cpu_feature_enabled(X86_FEATURE_SME_COHERENT);
2541e8c5971SBrijesh Singh }
2551e8c5971SBrijesh Singh
enc_dec_hypercall(unsigned long vaddr,unsigned long size,bool enc)256ac3f9c9fSSteve Rutherford static void enc_dec_hypercall(unsigned long vaddr, unsigned long size, bool enc)
257dbca5e1aSKuppuswamy Sathyanarayanan {
258dbca5e1aSKuppuswamy Sathyanarayanan #ifdef CONFIG_PARAVIRT
259ac3f9c9fSSteve Rutherford unsigned long vaddr_end = vaddr + size;
260dbca5e1aSKuppuswamy Sathyanarayanan
261dbca5e1aSKuppuswamy Sathyanarayanan while (vaddr < vaddr_end) {
262dbca5e1aSKuppuswamy Sathyanarayanan int psize, pmask, level;
263dbca5e1aSKuppuswamy Sathyanarayanan unsigned long pfn;
264dbca5e1aSKuppuswamy Sathyanarayanan pte_t *kpte;
265dbca5e1aSKuppuswamy Sathyanarayanan
266dbca5e1aSKuppuswamy Sathyanarayanan kpte = lookup_address(vaddr, &level);
267dbca5e1aSKuppuswamy Sathyanarayanan if (!kpte || pte_none(*kpte)) {
268dbca5e1aSKuppuswamy Sathyanarayanan WARN_ONCE(1, "kpte lookup for vaddr\n");
269dbca5e1aSKuppuswamy Sathyanarayanan return;
270dbca5e1aSKuppuswamy Sathyanarayanan }
271dbca5e1aSKuppuswamy Sathyanarayanan
272dbca5e1aSKuppuswamy Sathyanarayanan pfn = pg_level_to_pfn(level, kpte, NULL);
273dbca5e1aSKuppuswamy Sathyanarayanan if (!pfn)
274dbca5e1aSKuppuswamy Sathyanarayanan continue;
275dbca5e1aSKuppuswamy Sathyanarayanan
276dbca5e1aSKuppuswamy Sathyanarayanan psize = page_level_size(level);
277dbca5e1aSKuppuswamy Sathyanarayanan pmask = page_level_mask(level);
278dbca5e1aSKuppuswamy Sathyanarayanan
279dbca5e1aSKuppuswamy Sathyanarayanan notify_page_enc_status_changed(pfn, psize >> PAGE_SHIFT, enc);
280dbca5e1aSKuppuswamy Sathyanarayanan
281dbca5e1aSKuppuswamy Sathyanarayanan vaddr = (vaddr & pmask) + psize;
282dbca5e1aSKuppuswamy Sathyanarayanan }
283dbca5e1aSKuppuswamy Sathyanarayanan #endif
284dbca5e1aSKuppuswamy Sathyanarayanan }
285dbca5e1aSKuppuswamy Sathyanarayanan
amd_enc_status_change_prepare(unsigned long vaddr,int npages,bool enc)286*99c5c4c6SKirill A. Shutemov static int amd_enc_status_change_prepare(unsigned long vaddr, int npages, bool enc)
2871e8c5971SBrijesh Singh {
288dc3f3d24SBrijesh Singh /*
289dc3f3d24SBrijesh Singh * To maintain the security guarantees of SEV-SNP guests, make sure
290dc3f3d24SBrijesh Singh * to invalidate the memory before encryption attribute is cleared.
291dc3f3d24SBrijesh Singh */
292dc3f3d24SBrijesh Singh if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && !enc)
293dc3f3d24SBrijesh Singh snp_set_memory_shared(vaddr, npages);
2943f6819ddSKirill A. Shutemov
295*99c5c4c6SKirill A. Shutemov return 0;
2961e8c5971SBrijesh Singh }
2971e8c5971SBrijesh Singh
2981e8c5971SBrijesh Singh /* Return true unconditionally: return value doesn't matter for the SEV side */
amd_enc_status_change_finish(unsigned long vaddr,int npages,bool enc)299*99c5c4c6SKirill A. Shutemov static int amd_enc_status_change_finish(unsigned long vaddr, int npages, bool enc)
3001e8c5971SBrijesh Singh {
301dc3f3d24SBrijesh Singh /*
302dc3f3d24SBrijesh Singh * After memory is mapped encrypted in the page table, validate it
303dc3f3d24SBrijesh Singh * so that it is consistent with the page table updates.
304dc3f3d24SBrijesh Singh */
305dc3f3d24SBrijesh Singh if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP) && enc)
306dc3f3d24SBrijesh Singh snp_set_memory_private(vaddr, npages);
307dc3f3d24SBrijesh Singh
3081e8c5971SBrijesh Singh if (!cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT))
309ac3f9c9fSSteve Rutherford enc_dec_hypercall(vaddr, npages << PAGE_SHIFT, enc);
3101e8c5971SBrijesh Singh
311*99c5c4c6SKirill A. Shutemov return 0;
3121e8c5971SBrijesh Singh }
3131e8c5971SBrijesh Singh
__set_clr_pte_enc(pte_t * kpte,int level,bool enc)314dbca5e1aSKuppuswamy Sathyanarayanan static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc)
315dbca5e1aSKuppuswamy Sathyanarayanan {
316dbca5e1aSKuppuswamy Sathyanarayanan pgprot_t old_prot, new_prot;
317dbca5e1aSKuppuswamy Sathyanarayanan unsigned long pfn, pa, size;
318dbca5e1aSKuppuswamy Sathyanarayanan pte_t new_pte;
319dbca5e1aSKuppuswamy Sathyanarayanan
320dbca5e1aSKuppuswamy Sathyanarayanan pfn = pg_level_to_pfn(level, kpte, &old_prot);
321dbca5e1aSKuppuswamy Sathyanarayanan if (!pfn)
322dbca5e1aSKuppuswamy Sathyanarayanan return;
323dbca5e1aSKuppuswamy Sathyanarayanan
324dbca5e1aSKuppuswamy Sathyanarayanan new_prot = old_prot;
325dbca5e1aSKuppuswamy Sathyanarayanan if (enc)
326dbca5e1aSKuppuswamy Sathyanarayanan pgprot_val(new_prot) |= _PAGE_ENC;
327dbca5e1aSKuppuswamy Sathyanarayanan else
328dbca5e1aSKuppuswamy Sathyanarayanan pgprot_val(new_prot) &= ~_PAGE_ENC;
329dbca5e1aSKuppuswamy Sathyanarayanan
330dbca5e1aSKuppuswamy Sathyanarayanan /* If prot is same then do nothing. */
331dbca5e1aSKuppuswamy Sathyanarayanan if (pgprot_val(old_prot) == pgprot_val(new_prot))
332dbca5e1aSKuppuswamy Sathyanarayanan return;
333dbca5e1aSKuppuswamy Sathyanarayanan
334dbca5e1aSKuppuswamy Sathyanarayanan pa = pfn << PAGE_SHIFT;
335dbca5e1aSKuppuswamy Sathyanarayanan size = page_level_size(level);
336dbca5e1aSKuppuswamy Sathyanarayanan
337dbca5e1aSKuppuswamy Sathyanarayanan /*
338dbca5e1aSKuppuswamy Sathyanarayanan * We are going to perform in-place en-/decryption and change the
339dbca5e1aSKuppuswamy Sathyanarayanan * physical page attribute from C=1 to C=0 or vice versa. Flush the
340dbca5e1aSKuppuswamy Sathyanarayanan * caches to ensure that data gets accessed with the correct C-bit.
341dbca5e1aSKuppuswamy Sathyanarayanan */
342dbca5e1aSKuppuswamy Sathyanarayanan clflush_cache_range(__va(pa), size);
343dbca5e1aSKuppuswamy Sathyanarayanan
344dbca5e1aSKuppuswamy Sathyanarayanan /* Encrypt/decrypt the contents in-place */
3455e5ccff6SBrijesh Singh if (enc) {
346dbca5e1aSKuppuswamy Sathyanarayanan sme_early_encrypt(pa, size);
3475e5ccff6SBrijesh Singh } else {
348dbca5e1aSKuppuswamy Sathyanarayanan sme_early_decrypt(pa, size);
349dbca5e1aSKuppuswamy Sathyanarayanan
3505e5ccff6SBrijesh Singh /*
3515e5ccff6SBrijesh Singh * ON SNP, the page state in the RMP table must happen
3525e5ccff6SBrijesh Singh * before the page table updates.
3535e5ccff6SBrijesh Singh */
3545e5ccff6SBrijesh Singh early_snp_set_memory_shared((unsigned long)__va(pa), pa, 1);
3555e5ccff6SBrijesh Singh }
3565e5ccff6SBrijesh Singh
357dbca5e1aSKuppuswamy Sathyanarayanan /* Change the page encryption mask. */
358dbca5e1aSKuppuswamy Sathyanarayanan new_pte = pfn_pte(pfn, new_prot);
359dbca5e1aSKuppuswamy Sathyanarayanan set_pte_atomic(kpte, new_pte);
3605e5ccff6SBrijesh Singh
3615e5ccff6SBrijesh Singh /*
3625e5ccff6SBrijesh Singh * If page is set encrypted in the page table, then update the RMP table to
3635e5ccff6SBrijesh Singh * add this page as private.
3645e5ccff6SBrijesh Singh */
3655e5ccff6SBrijesh Singh if (enc)
3665e5ccff6SBrijesh Singh early_snp_set_memory_private((unsigned long)__va(pa), pa, 1);
367dbca5e1aSKuppuswamy Sathyanarayanan }
368dbca5e1aSKuppuswamy Sathyanarayanan
early_set_memory_enc_dec(unsigned long vaddr,unsigned long size,bool enc)369dbca5e1aSKuppuswamy Sathyanarayanan static int __init early_set_memory_enc_dec(unsigned long vaddr,
370dbca5e1aSKuppuswamy Sathyanarayanan unsigned long size, bool enc)
371dbca5e1aSKuppuswamy Sathyanarayanan {
372dbca5e1aSKuppuswamy Sathyanarayanan unsigned long vaddr_end, vaddr_next, start;
373dbca5e1aSKuppuswamy Sathyanarayanan unsigned long psize, pmask;
374dbca5e1aSKuppuswamy Sathyanarayanan int split_page_size_mask;
375dbca5e1aSKuppuswamy Sathyanarayanan int level, ret;
376dbca5e1aSKuppuswamy Sathyanarayanan pte_t *kpte;
377dbca5e1aSKuppuswamy Sathyanarayanan
378dbca5e1aSKuppuswamy Sathyanarayanan start = vaddr;
379dbca5e1aSKuppuswamy Sathyanarayanan vaddr_next = vaddr;
380dbca5e1aSKuppuswamy Sathyanarayanan vaddr_end = vaddr + size;
381dbca5e1aSKuppuswamy Sathyanarayanan
382dbca5e1aSKuppuswamy Sathyanarayanan for (; vaddr < vaddr_end; vaddr = vaddr_next) {
383dbca5e1aSKuppuswamy Sathyanarayanan kpte = lookup_address(vaddr, &level);
384dbca5e1aSKuppuswamy Sathyanarayanan if (!kpte || pte_none(*kpte)) {
385dbca5e1aSKuppuswamy Sathyanarayanan ret = 1;
386dbca5e1aSKuppuswamy Sathyanarayanan goto out;
387dbca5e1aSKuppuswamy Sathyanarayanan }
388dbca5e1aSKuppuswamy Sathyanarayanan
389dbca5e1aSKuppuswamy Sathyanarayanan if (level == PG_LEVEL_4K) {
390dbca5e1aSKuppuswamy Sathyanarayanan __set_clr_pte_enc(kpte, level, enc);
391dbca5e1aSKuppuswamy Sathyanarayanan vaddr_next = (vaddr & PAGE_MASK) + PAGE_SIZE;
392dbca5e1aSKuppuswamy Sathyanarayanan continue;
393dbca5e1aSKuppuswamy Sathyanarayanan }
394dbca5e1aSKuppuswamy Sathyanarayanan
395dbca5e1aSKuppuswamy Sathyanarayanan psize = page_level_size(level);
396dbca5e1aSKuppuswamy Sathyanarayanan pmask = page_level_mask(level);
397dbca5e1aSKuppuswamy Sathyanarayanan
398dbca5e1aSKuppuswamy Sathyanarayanan /*
399dbca5e1aSKuppuswamy Sathyanarayanan * Check whether we can change the large page in one go.
400dbca5e1aSKuppuswamy Sathyanarayanan * We request a split when the address is not aligned and
401dbca5e1aSKuppuswamy Sathyanarayanan * the number of pages to set/clear encryption bit is smaller
402dbca5e1aSKuppuswamy Sathyanarayanan * than the number of pages in the large page.
403dbca5e1aSKuppuswamy Sathyanarayanan */
404dbca5e1aSKuppuswamy Sathyanarayanan if (vaddr == (vaddr & pmask) &&
405dbca5e1aSKuppuswamy Sathyanarayanan ((vaddr_end - vaddr) >= psize)) {
406dbca5e1aSKuppuswamy Sathyanarayanan __set_clr_pte_enc(kpte, level, enc);
407dbca5e1aSKuppuswamy Sathyanarayanan vaddr_next = (vaddr & pmask) + psize;
408dbca5e1aSKuppuswamy Sathyanarayanan continue;
409dbca5e1aSKuppuswamy Sathyanarayanan }
410dbca5e1aSKuppuswamy Sathyanarayanan
411dbca5e1aSKuppuswamy Sathyanarayanan /*
412dbca5e1aSKuppuswamy Sathyanarayanan * The virtual address is part of a larger page, create the next
413dbca5e1aSKuppuswamy Sathyanarayanan * level page table mapping (4K or 2M). If it is part of a 2M
414dbca5e1aSKuppuswamy Sathyanarayanan * page then we request a split of the large page into 4K
415dbca5e1aSKuppuswamy Sathyanarayanan * chunks. A 1GB large page is split into 2M pages, resp.
416dbca5e1aSKuppuswamy Sathyanarayanan */
417dbca5e1aSKuppuswamy Sathyanarayanan if (level == PG_LEVEL_2M)
418dbca5e1aSKuppuswamy Sathyanarayanan split_page_size_mask = 0;
419dbca5e1aSKuppuswamy Sathyanarayanan else
420dbca5e1aSKuppuswamy Sathyanarayanan split_page_size_mask = 1 << PG_LEVEL_2M;
421dbca5e1aSKuppuswamy Sathyanarayanan
422dbca5e1aSKuppuswamy Sathyanarayanan /*
423dbca5e1aSKuppuswamy Sathyanarayanan * kernel_physical_mapping_change() does not flush the TLBs, so
424dbca5e1aSKuppuswamy Sathyanarayanan * a TLB flush is required after we exit from the for loop.
425dbca5e1aSKuppuswamy Sathyanarayanan */
426dbca5e1aSKuppuswamy Sathyanarayanan kernel_physical_mapping_change(__pa(vaddr & pmask),
427dbca5e1aSKuppuswamy Sathyanarayanan __pa((vaddr_end & pmask) + psize),
428dbca5e1aSKuppuswamy Sathyanarayanan split_page_size_mask);
429dbca5e1aSKuppuswamy Sathyanarayanan }
430dbca5e1aSKuppuswamy Sathyanarayanan
431dbca5e1aSKuppuswamy Sathyanarayanan ret = 0;
432dbca5e1aSKuppuswamy Sathyanarayanan
433ac3f9c9fSSteve Rutherford early_set_mem_enc_dec_hypercall(start, size, enc);
434dbca5e1aSKuppuswamy Sathyanarayanan out:
435dbca5e1aSKuppuswamy Sathyanarayanan __flush_tlb_all();
436dbca5e1aSKuppuswamy Sathyanarayanan return ret;
437dbca5e1aSKuppuswamy Sathyanarayanan }
438dbca5e1aSKuppuswamy Sathyanarayanan
early_set_memory_decrypted(unsigned long vaddr,unsigned long size)439dbca5e1aSKuppuswamy Sathyanarayanan int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
440dbca5e1aSKuppuswamy Sathyanarayanan {
441dbca5e1aSKuppuswamy Sathyanarayanan return early_set_memory_enc_dec(vaddr, size, false);
442dbca5e1aSKuppuswamy Sathyanarayanan }
443dbca5e1aSKuppuswamy Sathyanarayanan
early_set_memory_encrypted(unsigned long vaddr,unsigned long size)444dbca5e1aSKuppuswamy Sathyanarayanan int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
445dbca5e1aSKuppuswamy Sathyanarayanan {
446dbca5e1aSKuppuswamy Sathyanarayanan return early_set_memory_enc_dec(vaddr, size, true);
447dbca5e1aSKuppuswamy Sathyanarayanan }
448dbca5e1aSKuppuswamy Sathyanarayanan
early_set_mem_enc_dec_hypercall(unsigned long vaddr,unsigned long size,bool enc)449ac3f9c9fSSteve Rutherford void __init early_set_mem_enc_dec_hypercall(unsigned long vaddr, unsigned long size, bool enc)
450dbca5e1aSKuppuswamy Sathyanarayanan {
451ac3f9c9fSSteve Rutherford enc_dec_hypercall(vaddr, size, enc);
4521e8c5971SBrijesh Singh }
4531e8c5971SBrijesh Singh
sme_early_init(void)4541e8c5971SBrijesh Singh void __init sme_early_init(void)
4551e8c5971SBrijesh Singh {
4561e8c5971SBrijesh Singh if (!sme_me_mask)
4571e8c5971SBrijesh Singh return;
4581e8c5971SBrijesh Singh
4591e8c5971SBrijesh Singh early_pmd_flags = __sme_set(early_pmd_flags);
4601e8c5971SBrijesh Singh
4611e8c5971SBrijesh Singh __supported_pte_mask = __sme_set(__supported_pte_mask);
4621e8c5971SBrijesh Singh
4631e8c5971SBrijesh Singh /* Update the protection map with memory encryption mask */
4644867fbbdSAnshuman Khandual add_encrypt_protection_map();
4651e8c5971SBrijesh Singh
4661e8c5971SBrijesh Singh x86_platform.guest.enc_status_change_prepare = amd_enc_status_change_prepare;
4671e8c5971SBrijesh Singh x86_platform.guest.enc_status_change_finish = amd_enc_status_change_finish;
4681e8c5971SBrijesh Singh x86_platform.guest.enc_tlb_flush_required = amd_enc_tlb_flush_required;
4691e8c5971SBrijesh Singh x86_platform.guest.enc_cache_flush_required = amd_enc_cache_flush_required;
470ff3cfcb0SThomas Gleixner
471ff3cfcb0SThomas Gleixner /*
472ff3cfcb0SThomas Gleixner * AMD-SEV-ES intercepts the RDMSR to read the X2APIC ID in the
473ff3cfcb0SThomas Gleixner * parallel bringup low level code. That raises #VC which cannot be
474ff3cfcb0SThomas Gleixner * handled there.
475ff3cfcb0SThomas Gleixner * It does not provide a RDMSR GHCB protocol so the early startup
476ff3cfcb0SThomas Gleixner * code cannot directly communicate with the secure firmware. The
477ff3cfcb0SThomas Gleixner * alternative solution to retrieve the APIC ID via CPUID(0xb),
478ff3cfcb0SThomas Gleixner * which is covered by the GHCB protocol, is not viable either
479ff3cfcb0SThomas Gleixner * because there is no enforcement of the CPUID(0xb) provided
480ff3cfcb0SThomas Gleixner * "initial" APIC ID to be the same as the real APIC ID.
481ff3cfcb0SThomas Gleixner * Disable parallel bootup.
482ff3cfcb0SThomas Gleixner */
483ff3cfcb0SThomas Gleixner if (sev_status & MSR_AMD64_SEV_ES_ENABLED)
484ff3cfcb0SThomas Gleixner x86_cpuinit.parallel_bringup = false;
485b82a8dbdSKirill A. Shutemov
486b82a8dbdSKirill A. Shutemov /*
487b82a8dbdSKirill A. Shutemov * The VMM is capable of injecting interrupt 0x80 and triggering the
488b82a8dbdSKirill A. Shutemov * compatibility syscall path.
489b82a8dbdSKirill A. Shutemov *
490b82a8dbdSKirill A. Shutemov * By default, the 32-bit emulation is disabled in order to ensure
491b82a8dbdSKirill A. Shutemov * the safety of the VM.
492b82a8dbdSKirill A. Shutemov */
493b82a8dbdSKirill A. Shutemov if (sev_status & MSR_AMD64_SEV_ENABLED)
494b82a8dbdSKirill A. Shutemov ia32_disable();
4950f4a1e80SKevin Loughlin
4960f4a1e80SKevin Loughlin /*
4970f4a1e80SKevin Loughlin * Override init functions that scan the ROM region in SEV-SNP guests,
4980f4a1e80SKevin Loughlin * as this memory is not pre-validated and would thus cause a crash.
4990f4a1e80SKevin Loughlin */
5000f4a1e80SKevin Loughlin if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
5010f4a1e80SKevin Loughlin x86_init.mpparse.find_mptable = x86_init_noop;
5020f4a1e80SKevin Loughlin x86_init.pci.init_irq = x86_init_noop;
5030f4a1e80SKevin Loughlin x86_init.resources.probe_roms = x86_init_noop;
5040f4a1e80SKevin Loughlin
5050f4a1e80SKevin Loughlin /*
5060f4a1e80SKevin Loughlin * DMI setup behavior for SEV-SNP guests depends on
5070f4a1e80SKevin Loughlin * efi_enabled(EFI_CONFIG_TABLES), which hasn't been
5080f4a1e80SKevin Loughlin * parsed yet. snp_dmi_setup() will run after that
5090f4a1e80SKevin Loughlin * parsing has happened.
5100f4a1e80SKevin Loughlin */
5110f4a1e80SKevin Loughlin x86_init.resources.dmi_setup = snp_dmi_setup;
5120f4a1e80SKevin Loughlin }
51334ff6590STom Lendacky
51434ff6590STom Lendacky /*
51534ff6590STom Lendacky * Switch the SVSM CA mapping (if active) from identity mapped to
51634ff6590STom Lendacky * kernel mapped.
51734ff6590STom Lendacky */
51834ff6590STom Lendacky snp_update_svsm_ca();
519dbca5e1aSKuppuswamy Sathyanarayanan }
520dbca5e1aSKuppuswamy Sathyanarayanan
mem_encrypt_free_decrypted_mem(void)521dbca5e1aSKuppuswamy Sathyanarayanan void __init mem_encrypt_free_decrypted_mem(void)
522dbca5e1aSKuppuswamy Sathyanarayanan {
523dbca5e1aSKuppuswamy Sathyanarayanan unsigned long vaddr, vaddr_end, npages;
524dbca5e1aSKuppuswamy Sathyanarayanan int r;
525dbca5e1aSKuppuswamy Sathyanarayanan
526dbca5e1aSKuppuswamy Sathyanarayanan vaddr = (unsigned long)__start_bss_decrypted_unused;
527dbca5e1aSKuppuswamy Sathyanarayanan vaddr_end = (unsigned long)__end_bss_decrypted;
528dbca5e1aSKuppuswamy Sathyanarayanan npages = (vaddr_end - vaddr) >> PAGE_SHIFT;
529dbca5e1aSKuppuswamy Sathyanarayanan
530dbca5e1aSKuppuswamy Sathyanarayanan /*
531c7b5254bSMichael Kelley * If the unused memory range was mapped decrypted, change the encryption
532c7b5254bSMichael Kelley * attribute from decrypted to encrypted before freeing it. Base the
533c7b5254bSMichael Kelley * re-encryption on the same condition used for the decryption in
534c7b5254bSMichael Kelley * sme_postprocess_startup(). Higher level abstractions, such as
535c7b5254bSMichael Kelley * CC_ATTR_MEM_ENCRYPT, aren't necessarily equivalent in a Hyper-V VM
536c7b5254bSMichael Kelley * using vTOM, where sme_me_mask is always zero.
537dbca5e1aSKuppuswamy Sathyanarayanan */
538c7b5254bSMichael Kelley if (sme_me_mask) {
539dbca5e1aSKuppuswamy Sathyanarayanan r = set_memory_encrypted(vaddr, npages);
540dbca5e1aSKuppuswamy Sathyanarayanan if (r) {
541dbca5e1aSKuppuswamy Sathyanarayanan pr_warn("failed to free unused decrypted pages\n");
542dbca5e1aSKuppuswamy Sathyanarayanan return;
543dbca5e1aSKuppuswamy Sathyanarayanan }
544dbca5e1aSKuppuswamy Sathyanarayanan }
545dbca5e1aSKuppuswamy Sathyanarayanan
546dbca5e1aSKuppuswamy Sathyanarayanan free_init_pages("unused decrypted", vaddr, vaddr_end);
547dbca5e1aSKuppuswamy Sathyanarayanan }
548