This source file includes following definitions.
- __sme_early_enc_dec
- sme_early_encrypt
- sme_early_decrypt
- __sme_early_map_unmap_mem
- sme_unmap_bootdata
- sme_map_bootdata
- sme_early_init
- __set_clr_pte_enc
- early_set_memory_enc_dec
- early_set_memory_decrypted
- early_set_memory_encrypted
- sme_active
- sev_active
- force_dma_unencrypted
- mem_encrypt_free_decrypted_mem
- mem_encrypt_init
1
2
3
4
5
6
7
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
37
38
39
40 u64 sme_me_mask __section(.data) = 0;
41 EXPORT_SYMBOL(sme_me_mask);
42 DEFINE_STATIC_KEY_FALSE(sev_enable_key);
43 EXPORT_SYMBOL_GPL(sev_enable_key);
44
45 bool sev_enabled __section(.data);
46
47
48 static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE);
49
50
51
52
53
54
55
56
57
58
59 static 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
72
73
74 while (size) {
75 len = min_t(size_t, sizeof(sme_early_buffer), size);
76
77
78
79
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
89
90
91
92 BUG_ON(!src || !dst);
93
94
95
96
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
109 void __init sme_early_encrypt(resource_size_t paddr, unsigned long size)
110 {
111 __sme_early_enc_dec(paddr, size, true);
112 }
113
114 void __init sme_early_decrypt(resource_size_t paddr, unsigned long size)
115 {
116 __sme_early_enc_dec(paddr, size, false);
117 }
118
119 static 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
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 __native_flush_tlb();
138 }
139
140 void __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
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
160 void __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
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
180 void __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
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
199 static 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
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
237
238
239
240 clflush_cache_range(__va(pa), size);
241
242
243 if (enc)
244 sme_early_encrypt(pa, size);
245 else
246 sme_early_decrypt(pa, size);
247
248
249 new_pte = pfn_pte(pfn, new_prot);
250 set_pte_atomic(kpte, new_pte);
251 }
252
253 static 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
283
284
285
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
296
297
298
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
307
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
316 out:
317 __flush_tlb_all();
318 return ret;
319 }
320
321 int __init early_set_memory_decrypted(unsigned long vaddr, unsigned long size)
322 {
323 return early_set_memory_enc_dec(vaddr, size, false);
324 }
325
326 int __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
333
334
335
336
337
338
339
340
341
342
343 bool sme_active(void)
344 {
345 return sme_me_mask && !sev_enabled;
346 }
347
348 bool sev_active(void)
349 {
350 return sme_me_mask && sev_enabled;
351 }
352
353
354 bool force_dma_unencrypted(struct device *dev)
355 {
356
357
358
359 if (sev_active())
360 return true;
361
362
363
364
365
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_mask);
371
372 if (dma_dev_mask <= dma_enc_mask)
373 return true;
374 }
375
376 return false;
377 }
378
379
380 void __init mem_encrypt_free_decrypted_mem(void)
381 {
382 unsigned long vaddr, vaddr_end, npages;
383 int r;
384
385 vaddr = (unsigned long)__start_bss_decrypted_unused;
386 vaddr_end = (unsigned long)__end_bss_decrypted;
387 npages = (vaddr_end - vaddr) >> PAGE_SHIFT;
388
389
390
391
392
393 if (mem_encrypt_active()) {
394 r = set_memory_encrypted(vaddr, npages);
395 if (r) {
396 pr_warn("failed to free unused decrypted pages\n");
397 return;
398 }
399 }
400
401 free_init_pages("unused decrypted", vaddr, vaddr_end);
402 }
403
404 void __init mem_encrypt_init(void)
405 {
406 if (!sme_me_mask)
407 return;
408
409
410 swiotlb_update_mem_attributes();
411
412
413
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