Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * AMD Memory Encryption Support
4 *
5 * Copyright (C) 2016 Advanced Micro Devices, Inc.
6 *
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 */
9
10#define DISABLE_BRANCH_PROFILING
11
12#include <linux/linkage.h>
13#include <linux/init.h>
14#include <linux/mm.h>
15#include <linux/dma-direct.h>
16#include <linux/swiotlb.h>
17#include <linux/mem_encrypt.h>
18#include <linux/device.h>
19#include <linux/kernel.h>
20#include <linux/bitops.h>
21#include <linux/dma-mapping.h>
22
23#include <asm/tlbflush.h>
24#include <asm/fixmap.h>
25#include <asm/setup.h>
26#include <asm/bootparam.h>
27#include <asm/set_memory.h>
28#include <asm/cacheflush.h>
29#include <asm/processor-flags.h>
30#include <asm/msr.h>
31#include <asm/cmdline.h>
32
33#include "mm_internal.h"
34
35/*
36 * Since SME related variables are set early in the boot process they must
37 * reside in the .data section so as not to be zeroed out when the .bss
38 * section is later cleared.
39 */
40u64 sme_me_mask __section(.data) = 0;
41EXPORT_SYMBOL(sme_me_mask);
42DEFINE_STATIC_KEY_FALSE(sev_enable_key);
43EXPORT_SYMBOL_GPL(sev_enable_key);
44
45bool sev_enabled __section(.data);
46
47/* Buffer used for early in-place encryption by BSP, no locking needed */
48static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE);
49
50/*
51 * This routine does not change the underlying encryption setting of the
52 * page(s) that map this memory. It assumes that eventually the memory is
53 * meant to be accessed as either encrypted or decrypted but the contents
54 * are currently not in the desired state.
55 *
56 * This routine follows the steps outlined in the AMD64 Architecture
57 * Programmer's Manual Volume 2, Section 7.10.8 Encrypt-in-Place.
58 */
59static void __init __sme_early_enc_dec(resource_size_t paddr,
60 unsigned long size, bool enc)
61{
62 void *src, *dst;
63 size_t len;
64
65 if (!sme_me_mask)
66 return;
67
68 wbinvd();
69
70 /*
71 * There are limited number of early mapping slots, so map (at most)
72 * one page at time.
73 */
74 while (size) {
75 len = min_t(size_t, sizeof(sme_early_buffer), size);
76
77 /*
78 * Create mappings for the current and desired format of
79 * the memory. Use a write-protected mapping for the source.
80 */
81 src = enc ? early_memremap_decrypted_wp(paddr, len) :
82 early_memremap_encrypted_wp(paddr, len);
83
84 dst = enc ? early_memremap_encrypted(paddr, len) :
85 early_memremap_decrypted(paddr, len);
86
87 /*
88 * If a mapping can't be obtained to perform the operation,
89 * then eventual access of that area in the desired mode
90 * will cause a crash.
91 */
92 BUG_ON(!src || !dst);
93
94 /*
95 * Use a temporary buffer, of cache-line multiple size, to
96 * avoid data corruption as documented in the APM.
97 */
98 memcpy(sme_early_buffer, src, len);
99 memcpy(dst, sme_early_buffer, len);
100
101 early_memunmap(dst, len);
102 early_memunmap(src, len);
103
104 paddr += len;
105 size -= len;
106 }
107}
108
109void __init sme_early_encrypt(resource_size_t paddr, unsigned long size)
110{
111 __sme_early_enc_dec(paddr, size, true);
112}
113
114void __init sme_early_decrypt(resource_size_t paddr, unsigned long size)
115{
116 __sme_early_enc_dec(paddr, size, false);
117}
118
119static void __init __sme_early_map_unmap_mem(void *vaddr, unsigned long size,
120 bool map)
121{
122 unsigned long paddr = (unsigned long)vaddr - __PAGE_OFFSET;
123 pmdval_t pmd_flags, pmd;
124
125 /* Use early_pmd_flags but remove the encryption mask */
126 pmd_flags = __sme_clr(early_pmd_flags);
127
128 do {
129 pmd = map ? (paddr & PMD_MASK) + pmd_flags : 0;
130 __early_make_pgtable((unsigned long)vaddr, pmd);
131
132 vaddr += PMD_SIZE;
133 paddr += PMD_SIZE;
134 size = (size <= PMD_SIZE) ? 0 : size - PMD_SIZE;
135 } while (size);
136
137 flush_tlb_local();
138}
139
140void __init sme_unmap_bootdata(char *real_mode_data)
141{
142 struct boot_params *boot_data;
143 unsigned long cmdline_paddr;
144
145 if (!sme_active())
146 return;
147
148 /* Get the command line address before unmapping the real_mode_data */
149 boot_data = (struct boot_params *)real_mode_data;
150 cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
151
152 __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), false);
153
154 if (!cmdline_paddr)
155 return;
156
157 __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, false);
158}
159
160void __init sme_map_bootdata(char *real_mode_data)
161{
162 struct boot_params *boot_data;
163 unsigned long cmdline_paddr;
164
165 if (!sme_active())
166 return;
167
168 __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), true);
169
170 /* Get the command line address after mapping the real_mode_data */
171 boot_data = (struct boot_params *)real_mode_data;
172 cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
173
174 if (!cmdline_paddr)
175 return;
176
177 __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
178}
179
180void __init sme_early_init(void)
181{
182 unsigned int i;
183
184 if (!sme_me_mask)
185 return;
186
187 early_pmd_flags = __sme_set(early_pmd_flags);
188
189 __supported_pte_mask = __sme_set(__supported_pte_mask);
190
191 /* Update the protection map with memory encryption mask */
192 for (i = 0; i < ARRAY_SIZE(protection_map); i++)
193 protection_map[i] = pgprot_encrypted(protection_map[i]);
194
195 if (sev_active())
196 swiotlb_force = SWIOTLB_FORCE;
197}
198
199static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc)
200{
201 pgprot_t old_prot, new_prot;
202 unsigned long pfn, pa, size;
203 pte_t new_pte;
204
205 switch (level) {
206 case PG_LEVEL_4K:
207 pfn = pte_pfn(*kpte);
208 old_prot = pte_pgprot(*kpte);
209 break;
210 case PG_LEVEL_2M:
211 pfn = pmd_pfn(*(pmd_t *)kpte);
212 old_prot = pmd_pgprot(*(pmd_t *)kpte);
213 break;
214 case PG_LEVEL_1G:
215 pfn = pud_pfn(*(pud_t *)kpte);
216 old_prot = pud_pgprot(*(pud_t *)kpte);
217 break;
218 default:
219 return;
220 }
221
222 new_prot = old_prot;
223 if (enc)
224 pgprot_val(new_prot) |= _PAGE_ENC;
225 else
226 pgprot_val(new_prot) &= ~_PAGE_ENC;
227
228 /* If prot is same then do nothing. */
229 if (pgprot_val(old_prot) == pgprot_val(new_prot))
230 return;
231
232 pa = pfn << page_level_shift(level);
233 size = page_level_size(level);
234
235 /*
236 * We are going to perform in-place en-/decryption and change the
237 * physical page attribute from C=1 to C=0 or vice versa. Flush the
238 * caches to ensure that data gets accessed with the correct C-bit.
239 */
240 clflush_cache_range(__va(pa), size);
241
242 /* Encrypt/decrypt the contents in-place */
243 if (enc)
244 sme_early_encrypt(pa, size);
245 else
246 sme_early_decrypt(pa, size);
247
248 /* Change the page encryption mask. */
249 new_pte = pfn_pte(pfn, new_prot);
250 set_pte_atomic(kpte, new_pte);
251}
252
253static int __init early_set_memory_enc_dec(unsigned long vaddr,
254 unsigned long size, bool enc)
255{
256 unsigned long vaddr_end, vaddr_next;
257 unsigned long psize, pmask;
258 int split_page_size_mask;
259 int level, ret;
260 pte_t *kpte;
261
262 vaddr_next = vaddr;
263 vaddr_end = vaddr + size;
264
265 for (; vaddr < vaddr_end; vaddr = vaddr_next) {
266 kpte = lookup_address(vaddr, &level);
267 if (!kpte || pte_none(*kpte)) {
268 ret = 1;
269 goto out;
270 }
271
272 if (level == PG_LEVEL_4K) {
273 __set_clr_pte_enc(kpte, level, enc);
274 vaddr_next = (vaddr & PAGE_MASK) + PAGE_SIZE;
275 continue;
276 }
277
278 psize = page_level_size(level);
279 pmask = page_level_mask(level);
280
281 /*
282 * Check whether we can change the large page in one go.
283 * We request a split when the address is not aligned and
284 * the number of pages to set/clear encryption bit is smaller
285 * than the number of pages in the large page.
286 */
287 if (vaddr == (vaddr & pmask) &&
288 ((vaddr_end - vaddr) >= psize)) {
289 __set_clr_pte_enc(kpte, level, enc);
290 vaddr_next = (vaddr & pmask) + psize;
291 continue;
292 }
293
294 /*
295 * The virtual address is part of a larger page, create the next
296 * level page table mapping (4K or 2M). If it is part of a 2M
297 * page then we request a split of the large page into 4K
298 * chunks. A 1GB large page is split into 2M pages, resp.
299 */
300 if (level == PG_LEVEL_2M)
301 split_page_size_mask = 0;
302 else
303 split_page_size_mask = 1 << PG_LEVEL_2M;
304
305 /*
306 * kernel_physical_mapping_change() does not flush the TLBs, so
307 * a TLB flush is required after we exit from the for loop.
308 */
309 kernel_physical_mapping_change(__pa(vaddr & pmask),
310 __pa((vaddr_end & pmask) + psize),
311 split_page_size_mask);
312 }
313
314 ret = 0;
315
316out:
317 __flush_tlb_all();
318 return ret;
319}
320
321int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
322{
323 return early_set_memory_enc_dec(vaddr, size, false);
324}
325
326int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
327{
328 return early_set_memory_enc_dec(vaddr, size, true);
329}
330
331/*
332 * SME and SEV are very similar but they are not the same, so there are
333 * times that the kernel will need to distinguish between SME and SEV. The
334 * sme_active() and sev_active() functions are used for this. When a
335 * distinction isn't needed, the mem_encrypt_active() function can be used.
336 *
337 * The trampoline code is a good example for this requirement. Before
338 * paging is activated, SME will access all memory as decrypted, but SEV
339 * will access all memory as encrypted. So, when APs are being brought
340 * up under SME the trampoline area cannot be encrypted, whereas under SEV
341 * the trampoline area must be encrypted.
342 */
343bool sme_active(void)
344{
345 return sme_me_mask && !sev_enabled;
346}
347
348bool sev_active(void)
349{
350 return sme_me_mask && sev_enabled;
351}
352
353/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
354bool force_dma_unencrypted(struct device *dev)
355{
356 /*
357 * For SEV, all DMA must be to unencrypted addresses.
358 */
359 if (sev_active())
360 return true;
361
362 /*
363 * For SME, all DMA must be to unencrypted addresses if the
364 * device does not support DMA to addresses that include the
365 * encryption mask.
366 */
367 if (sme_active()) {
368 u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
369 u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
370 dev->bus_dma_limit);
371
372 if (dma_dev_mask <= dma_enc_mask)
373 return true;
374 }
375
376 return false;
377}
378
379void __init mem_encrypt_free_decrypted_mem(void)
380{
381 unsigned long vaddr, vaddr_end, npages;
382 int r;
383
384 vaddr = (unsigned long)__start_bss_decrypted_unused;
385 vaddr_end = (unsigned long)__end_bss_decrypted;
386 npages = (vaddr_end - vaddr) >> PAGE_SHIFT;
387
388 /*
389 * The unused memory range was mapped decrypted, change the encryption
390 * attribute from decrypted to encrypted before freeing it.
391 */
392 if (mem_encrypt_active()) {
393 r = set_memory_encrypted(vaddr, npages);
394 if (r) {
395 pr_warn("failed to free unused decrypted pages\n");
396 return;
397 }
398 }
399
400 free_init_pages("unused decrypted", vaddr, vaddr_end);
401}
402
403/* Architecture __weak replacement functions */
404void __init mem_encrypt_init(void)
405{
406 if (!sme_me_mask)
407 return;
408
409 /* Call into SWIOTLB to update the SWIOTLB DMA buffers */
410 swiotlb_update_mem_attributes();
411
412 /*
413 * With SEV, we need to unroll the rep string I/O instructions.
414 */
415 if (sev_active())
416 static_branch_enable(&sev_enable_key);
417
418 pr_info("AMD %s active\n",
419 sev_active() ? "Secure Encrypted Virtualization (SEV)"
420 : "Secure Memory Encryption (SME)");
421}
422
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * AMD Memory Encryption Support
4 *
5 * Copyright (C) 2016 Advanced Micro Devices, Inc.
6 *
7 * Author: Tom Lendacky <thomas.lendacky@amd.com>
8 */
9
10#define DISABLE_BRANCH_PROFILING
11
12#include <linux/linkage.h>
13#include <linux/init.h>
14#include <linux/mm.h>
15#include <linux/dma-direct.h>
16#include <linux/swiotlb.h>
17#include <linux/mem_encrypt.h>
18#include <linux/device.h>
19#include <linux/kernel.h>
20#include <linux/bitops.h>
21#include <linux/dma-mapping.h>
22#include <linux/virtio_config.h>
23
24#include <asm/tlbflush.h>
25#include <asm/fixmap.h>
26#include <asm/setup.h>
27#include <asm/bootparam.h>
28#include <asm/set_memory.h>
29#include <asm/cacheflush.h>
30#include <asm/processor-flags.h>
31#include <asm/msr.h>
32#include <asm/cmdline.h>
33
34#include "mm_internal.h"
35
36/*
37 * Since SME related variables are set early in the boot process they must
38 * reside in the .data section so as not to be zeroed out when the .bss
39 * section is later cleared.
40 */
41u64 sme_me_mask __section(".data") = 0;
42u64 sev_status __section(".data") = 0;
43u64 sev_check_data __section(".data") = 0;
44EXPORT_SYMBOL(sme_me_mask);
45DEFINE_STATIC_KEY_FALSE(sev_enable_key);
46EXPORT_SYMBOL_GPL(sev_enable_key);
47
48/* Buffer used for early in-place encryption by BSP, no locking needed */
49static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE);
50
51/*
52 * This routine does not change the underlying encryption setting of the
53 * page(s) that map this memory. It assumes that eventually the memory is
54 * meant to be accessed as either encrypted or decrypted but the contents
55 * are currently not in the desired state.
56 *
57 * This routine follows the steps outlined in the AMD64 Architecture
58 * Programmer's Manual Volume 2, Section 7.10.8 Encrypt-in-Place.
59 */
60static void __init __sme_early_enc_dec(resource_size_t paddr,
61 unsigned long size, bool enc)
62{
63 void *src, *dst;
64 size_t len;
65
66 if (!sme_me_mask)
67 return;
68
69 wbinvd();
70
71 /*
72 * There are limited number of early mapping slots, so map (at most)
73 * one page at time.
74 */
75 while (size) {
76 len = min_t(size_t, sizeof(sme_early_buffer), size);
77
78 /*
79 * Create mappings for the current and desired format of
80 * the memory. Use a write-protected mapping for the source.
81 */
82 src = enc ? early_memremap_decrypted_wp(paddr, len) :
83 early_memremap_encrypted_wp(paddr, len);
84
85 dst = enc ? early_memremap_encrypted(paddr, len) :
86 early_memremap_decrypted(paddr, len);
87
88 /*
89 * If a mapping can't be obtained to perform the operation,
90 * then eventual access of that area in the desired mode
91 * will cause a crash.
92 */
93 BUG_ON(!src || !dst);
94
95 /*
96 * Use a temporary buffer, of cache-line multiple size, to
97 * avoid data corruption as documented in the APM.
98 */
99 memcpy(sme_early_buffer, src, len);
100 memcpy(dst, sme_early_buffer, len);
101
102 early_memunmap(dst, len);
103 early_memunmap(src, len);
104
105 paddr += len;
106 size -= len;
107 }
108}
109
110void __init sme_early_encrypt(resource_size_t paddr, unsigned long size)
111{
112 __sme_early_enc_dec(paddr, size, true);
113}
114
115void __init sme_early_decrypt(resource_size_t paddr, unsigned long size)
116{
117 __sme_early_enc_dec(paddr, size, false);
118}
119
120static void __init __sme_early_map_unmap_mem(void *vaddr, unsigned long size,
121 bool map)
122{
123 unsigned long paddr = (unsigned long)vaddr - __PAGE_OFFSET;
124 pmdval_t pmd_flags, pmd;
125
126 /* Use early_pmd_flags but remove the encryption mask */
127 pmd_flags = __sme_clr(early_pmd_flags);
128
129 do {
130 pmd = map ? (paddr & PMD_MASK) + pmd_flags : 0;
131 __early_make_pgtable((unsigned long)vaddr, pmd);
132
133 vaddr += PMD_SIZE;
134 paddr += PMD_SIZE;
135 size = (size <= PMD_SIZE) ? 0 : size - PMD_SIZE;
136 } while (size);
137
138 flush_tlb_local();
139}
140
141void __init sme_unmap_bootdata(char *real_mode_data)
142{
143 struct boot_params *boot_data;
144 unsigned long cmdline_paddr;
145
146 if (!sme_active())
147 return;
148
149 /* Get the command line address before unmapping the real_mode_data */
150 boot_data = (struct boot_params *)real_mode_data;
151 cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
152
153 __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), false);
154
155 if (!cmdline_paddr)
156 return;
157
158 __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, false);
159}
160
161void __init sme_map_bootdata(char *real_mode_data)
162{
163 struct boot_params *boot_data;
164 unsigned long cmdline_paddr;
165
166 if (!sme_active())
167 return;
168
169 __sme_early_map_unmap_mem(real_mode_data, sizeof(boot_params), true);
170
171 /* Get the command line address after mapping the real_mode_data */
172 boot_data = (struct boot_params *)real_mode_data;
173 cmdline_paddr = boot_data->hdr.cmd_line_ptr | ((u64)boot_data->ext_cmd_line_ptr << 32);
174
175 if (!cmdline_paddr)
176 return;
177
178 __sme_early_map_unmap_mem(__va(cmdline_paddr), COMMAND_LINE_SIZE, true);
179}
180
181void __init sme_early_init(void)
182{
183 unsigned int i;
184
185 if (!sme_me_mask)
186 return;
187
188 early_pmd_flags = __sme_set(early_pmd_flags);
189
190 __supported_pte_mask = __sme_set(__supported_pte_mask);
191
192 /* Update the protection map with memory encryption mask */
193 for (i = 0; i < ARRAY_SIZE(protection_map); i++)
194 protection_map[i] = pgprot_encrypted(protection_map[i]);
195
196 if (sev_active())
197 swiotlb_force = SWIOTLB_FORCE;
198}
199
200void __init sev_setup_arch(void)
201{
202 phys_addr_t total_mem = memblock_phys_mem_size();
203 unsigned long size;
204
205 if (!sev_active())
206 return;
207
208 /*
209 * For SEV, all DMA has to occur via shared/unencrypted pages.
210 * SEV uses SWIOTLB to make this happen without changing device
211 * drivers. However, depending on the workload being run, the
212 * default 64MB of SWIOTLB may not be enough and SWIOTLB may
213 * run out of buffers for DMA, resulting in I/O errors and/or
214 * performance degradation especially with high I/O workloads.
215 *
216 * Adjust the default size of SWIOTLB for SEV guests using
217 * a percentage of guest memory for SWIOTLB buffers.
218 * Also, as the SWIOTLB bounce buffer memory is allocated
219 * from low memory, ensure that the adjusted size is within
220 * the limits of low available memory.
221 *
222 * The percentage of guest memory used here for SWIOTLB buffers
223 * is more of an approximation of the static adjustment which
224 * 64MB for <1G, and ~128M to 256M for 1G-to-4G, i.e., the 6%
225 */
226 size = total_mem * 6 / 100;
227 size = clamp_val(size, IO_TLB_DEFAULT_SIZE, SZ_1G);
228 swiotlb_adjust_size(size);
229}
230
231static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc)
232{
233 pgprot_t old_prot, new_prot;
234 unsigned long pfn, pa, size;
235 pte_t new_pte;
236
237 switch (level) {
238 case PG_LEVEL_4K:
239 pfn = pte_pfn(*kpte);
240 old_prot = pte_pgprot(*kpte);
241 break;
242 case PG_LEVEL_2M:
243 pfn = pmd_pfn(*(pmd_t *)kpte);
244 old_prot = pmd_pgprot(*(pmd_t *)kpte);
245 break;
246 case PG_LEVEL_1G:
247 pfn = pud_pfn(*(pud_t *)kpte);
248 old_prot = pud_pgprot(*(pud_t *)kpte);
249 break;
250 default:
251 return;
252 }
253
254 new_prot = old_prot;
255 if (enc)
256 pgprot_val(new_prot) |= _PAGE_ENC;
257 else
258 pgprot_val(new_prot) &= ~_PAGE_ENC;
259
260 /* If prot is same then do nothing. */
261 if (pgprot_val(old_prot) == pgprot_val(new_prot))
262 return;
263
264 pa = pfn << PAGE_SHIFT;
265 size = page_level_size(level);
266
267 /*
268 * We are going to perform in-place en-/decryption and change the
269 * physical page attribute from C=1 to C=0 or vice versa. Flush the
270 * caches to ensure that data gets accessed with the correct C-bit.
271 */
272 clflush_cache_range(__va(pa), size);
273
274 /* Encrypt/decrypt the contents in-place */
275 if (enc)
276 sme_early_encrypt(pa, size);
277 else
278 sme_early_decrypt(pa, size);
279
280 /* Change the page encryption mask. */
281 new_pte = pfn_pte(pfn, new_prot);
282 set_pte_atomic(kpte, new_pte);
283}
284
285static int __init early_set_memory_enc_dec(unsigned long vaddr,
286 unsigned long size, bool enc)
287{
288 unsigned long vaddr_end, vaddr_next;
289 unsigned long psize, pmask;
290 int split_page_size_mask;
291 int level, ret;
292 pte_t *kpte;
293
294 vaddr_next = vaddr;
295 vaddr_end = vaddr + size;
296
297 for (; vaddr < vaddr_end; vaddr = vaddr_next) {
298 kpte = lookup_address(vaddr, &level);
299 if (!kpte || pte_none(*kpte)) {
300 ret = 1;
301 goto out;
302 }
303
304 if (level == PG_LEVEL_4K) {
305 __set_clr_pte_enc(kpte, level, enc);
306 vaddr_next = (vaddr & PAGE_MASK) + PAGE_SIZE;
307 continue;
308 }
309
310 psize = page_level_size(level);
311 pmask = page_level_mask(level);
312
313 /*
314 * Check whether we can change the large page in one go.
315 * We request a split when the address is not aligned and
316 * the number of pages to set/clear encryption bit is smaller
317 * than the number of pages in the large page.
318 */
319 if (vaddr == (vaddr & pmask) &&
320 ((vaddr_end - vaddr) >= psize)) {
321 __set_clr_pte_enc(kpte, level, enc);
322 vaddr_next = (vaddr & pmask) + psize;
323 continue;
324 }
325
326 /*
327 * The virtual address is part of a larger page, create the next
328 * level page table mapping (4K or 2M). If it is part of a 2M
329 * page then we request a split of the large page into 4K
330 * chunks. A 1GB large page is split into 2M pages, resp.
331 */
332 if (level == PG_LEVEL_2M)
333 split_page_size_mask = 0;
334 else
335 split_page_size_mask = 1 << PG_LEVEL_2M;
336
337 /*
338 * kernel_physical_mapping_change() does not flush the TLBs, so
339 * a TLB flush is required after we exit from the for loop.
340 */
341 kernel_physical_mapping_change(__pa(vaddr & pmask),
342 __pa((vaddr_end & pmask) + psize),
343 split_page_size_mask);
344 }
345
346 ret = 0;
347
348out:
349 __flush_tlb_all();
350 return ret;
351}
352
353int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
354{
355 return early_set_memory_enc_dec(vaddr, size, false);
356}
357
358int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size)
359{
360 return early_set_memory_enc_dec(vaddr, size, true);
361}
362
363/*
364 * SME and SEV are very similar but they are not the same, so there are
365 * times that the kernel will need to distinguish between SME and SEV. The
366 * sme_active() and sev_active() functions are used for this. When a
367 * distinction isn't needed, the mem_encrypt_active() function can be used.
368 *
369 * The trampoline code is a good example for this requirement. Before
370 * paging is activated, SME will access all memory as decrypted, but SEV
371 * will access all memory as encrypted. So, when APs are being brought
372 * up under SME the trampoline area cannot be encrypted, whereas under SEV
373 * the trampoline area must be encrypted.
374 */
375bool sev_active(void)
376{
377 return sev_status & MSR_AMD64_SEV_ENABLED;
378}
379
380bool sme_active(void)
381{
382 return sme_me_mask && !sev_active();
383}
384EXPORT_SYMBOL_GPL(sev_active);
385
386/* Needs to be called from non-instrumentable code */
387bool noinstr sev_es_active(void)
388{
389 return sev_status & MSR_AMD64_SEV_ES_ENABLED;
390}
391
392/* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
393bool force_dma_unencrypted(struct device *dev)
394{
395 /*
396 * For SEV, all DMA must be to unencrypted addresses.
397 */
398 if (sev_active())
399 return true;
400
401 /*
402 * For SME, all DMA must be to unencrypted addresses if the
403 * device does not support DMA to addresses that include the
404 * encryption mask.
405 */
406 if (sme_active()) {
407 u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
408 u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
409 dev->bus_dma_limit);
410
411 if (dma_dev_mask <= dma_enc_mask)
412 return true;
413 }
414
415 return false;
416}
417
418void __init mem_encrypt_free_decrypted_mem(void)
419{
420 unsigned long vaddr, vaddr_end, npages;
421 int r;
422
423 vaddr = (unsigned long)__start_bss_decrypted_unused;
424 vaddr_end = (unsigned long)__end_bss_decrypted;
425 npages = (vaddr_end - vaddr) >> PAGE_SHIFT;
426
427 /*
428 * The unused memory range was mapped decrypted, change the encryption
429 * attribute from decrypted to encrypted before freeing it.
430 */
431 if (mem_encrypt_active()) {
432 r = set_memory_encrypted(vaddr, npages);
433 if (r) {
434 pr_warn("failed to free unused decrypted pages\n");
435 return;
436 }
437 }
438
439 free_init_pages("unused decrypted", vaddr, vaddr_end);
440}
441
442static void print_mem_encrypt_feature_info(void)
443{
444 pr_info("AMD Memory Encryption Features active:");
445
446 /* Secure Memory Encryption */
447 if (sme_active()) {
448 /*
449 * SME is mutually exclusive with any of the SEV
450 * features below.
451 */
452 pr_cont(" SME\n");
453 return;
454 }
455
456 /* Secure Encrypted Virtualization */
457 if (sev_active())
458 pr_cont(" SEV");
459
460 /* Encrypted Register State */
461 if (sev_es_active())
462 pr_cont(" SEV-ES");
463
464 pr_cont("\n");
465}
466
467/* Architecture __weak replacement functions */
468void __init mem_encrypt_init(void)
469{
470 if (!sme_me_mask)
471 return;
472
473 /* Call into SWIOTLB to update the SWIOTLB DMA buffers */
474 swiotlb_update_mem_attributes();
475
476 /*
477 * With SEV, we need to unroll the rep string I/O instructions,
478 * but SEV-ES supports them through the #VC handler.
479 */
480 if (sev_active() && !sev_es_active())
481 static_branch_enable(&sev_enable_key);
482
483 print_mem_encrypt_feature_info();
484}
485
486int arch_has_restricted_virtio_memory_access(void)
487{
488 return sev_active();
489}
490EXPORT_SYMBOL_GPL(arch_has_restricted_virtio_memory_access);