blob: 4b6a2e3098c5a5382384b13e924028f9c9df792e [file] [log] [blame]
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +03001/*
2 * AMD Memory Encryption Support
3 *
4 * Copyright (C) 2016 Advanced Micro Devices, Inc.
5 *
6 * Author: Tom Lendacky <thomas.lendacky@amd.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#define DISABLE_BRANCH_PROFILING
14
Kirill A. Shutemovaad98392018-01-31 16:54:03 +030015/*
16 * Since we're dealing with identity mappings, physical and virtual
17 * addresses are the same, so override these defines which are ultimately
18 * used by the headers in misc.h.
19 */
20#define __pa(x) ((unsigned long)(x))
21#define __va(x) ((void *)((unsigned long)(x)))
22
23/*
24 * Special hack: we have to be careful, because no indirections are
25 * allowed here, and paravirt_ops is a kind of one. As it will only run in
26 * baremetal anyway, we just keep it from happening. (This list needs to
27 * be extended when new paravirt and debugging variants are added.)
28 */
29#undef CONFIG_PARAVIRT
30#undef CONFIG_PARAVIRT_SPINLOCKS
31
32#include <linux/kernel.h>
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +030033#include <linux/mm.h>
34#include <linux/mem_encrypt.h>
35
36#include <asm/setup.h>
37#include <asm/sections.h>
38#include <asm/cmdline.h>
39
40#include "mm_internal.h"
41
42#define PGD_FLAGS _KERNPG_TABLE_NOENC
43#define P4D_FLAGS _KERNPG_TABLE_NOENC
44#define PUD_FLAGS _KERNPG_TABLE_NOENC
45#define PMD_FLAGS _KERNPG_TABLE_NOENC
46
47#define PMD_FLAGS_LARGE (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL)
48
49#define PMD_FLAGS_DEC PMD_FLAGS_LARGE
50#define PMD_FLAGS_DEC_WP ((PMD_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \
51 (_PAGE_PAT | _PAGE_PWT))
52
53#define PMD_FLAGS_ENC (PMD_FLAGS_LARGE | _PAGE_ENC)
54
55#define PTE_FLAGS (__PAGE_KERNEL_EXEC & ~_PAGE_GLOBAL)
56
57#define PTE_FLAGS_DEC PTE_FLAGS
58#define PTE_FLAGS_DEC_WP ((PTE_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \
59 (_PAGE_PAT | _PAGE_PWT))
60
61#define PTE_FLAGS_ENC (PTE_FLAGS | _PAGE_ENC)
62
63struct sme_populate_pgd_data {
64 void *pgtable_area;
65 pgd_t *pgd;
66
67 pmdval_t pmd_flags;
68 pteval_t pte_flags;
69 unsigned long paddr;
70
71 unsigned long vaddr;
72 unsigned long vaddr_end;
73};
74
75static char sme_cmdline_arg[] __initdata = "mem_encrypt";
76static char sme_cmdline_on[] __initdata = "on";
77static char sme_cmdline_off[] __initdata = "off";
78
79static void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd)
80{
81 unsigned long pgd_start, pgd_end, pgd_size;
82 pgd_t *pgd_p;
83
84 pgd_start = ppd->vaddr & PGDIR_MASK;
85 pgd_end = ppd->vaddr_end & PGDIR_MASK;
86
87 pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
88
89 pgd_p = ppd->pgd + pgd_index(ppd->vaddr);
90
91 memset(pgd_p, 0, pgd_size);
92}
93
Kirill A. Shutemovaad98392018-01-31 16:54:03 +030094static pud_t __init *sme_prepare_pgd(struct sme_populate_pgd_data *ppd)
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +030095{
Kirill A. Shutemovaad98392018-01-31 16:54:03 +030096 pgd_t *pgd;
97 p4d_t *p4d;
98 pud_t *pud;
99 pmd_t *pmd;
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300100
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300101 pgd = ppd->pgd + pgd_index(ppd->vaddr);
102 if (pgd_none(*pgd)) {
103 p4d = ppd->pgtable_area;
104 memset(p4d, 0, sizeof(*p4d) * PTRS_PER_P4D);
105 ppd->pgtable_area += sizeof(*p4d) * PTRS_PER_P4D;
106 set_pgd(pgd, __pgd(PGD_FLAGS | __pa(p4d)));
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300107 }
108
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300109 p4d = p4d_offset(pgd, ppd->vaddr);
110 if (p4d_none(*p4d)) {
111 pud = ppd->pgtable_area;
112 memset(pud, 0, sizeof(*pud) * PTRS_PER_PUD);
113 ppd->pgtable_area += sizeof(*pud) * PTRS_PER_PUD;
114 set_p4d(p4d, __p4d(P4D_FLAGS | __pa(pud)));
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300115 }
116
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300117 pud = pud_offset(p4d, ppd->vaddr);
118 if (pud_none(*pud)) {
119 pmd = ppd->pgtable_area;
120 memset(pmd, 0, sizeof(*pmd) * PTRS_PER_PMD);
121 ppd->pgtable_area += sizeof(*pmd) * PTRS_PER_PMD;
122 set_pud(pud, __pud(PUD_FLAGS | __pa(pmd)));
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300123 }
124
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300125 if (pud_large(*pud))
126 return NULL;
127
128 return pud;
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300129}
130
131static void __init sme_populate_pgd_large(struct sme_populate_pgd_data *ppd)
132{
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300133 pud_t *pud;
134 pmd_t *pmd;
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300135
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300136 pud = sme_prepare_pgd(ppd);
137 if (!pud)
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300138 return;
139
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300140 pmd = pmd_offset(pud, ppd->vaddr);
141 if (pmd_large(*pmd))
142 return;
143
144 set_pmd(pmd, __pmd(ppd->paddr | ppd->pmd_flags));
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300145}
146
147static void __init sme_populate_pgd(struct sme_populate_pgd_data *ppd)
148{
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300149 pud_t *pud;
150 pmd_t *pmd;
151 pte_t *pte;
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300152
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300153 pud = sme_prepare_pgd(ppd);
154 if (!pud)
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300155 return;
156
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300157 pmd = pmd_offset(pud, ppd->vaddr);
158 if (pmd_none(*pmd)) {
159 pte = ppd->pgtable_area;
160 memset(pte, 0, sizeof(pte) * PTRS_PER_PTE);
161 ppd->pgtable_area += sizeof(pte) * PTRS_PER_PTE;
162 set_pmd(pmd, __pmd(PMD_FLAGS | __pa(pte)));
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300163 }
164
Kirill A. Shutemovaad98392018-01-31 16:54:03 +0300165 if (pmd_large(*pmd))
166 return;
167
168 pte = pte_offset_map(pmd, ppd->vaddr);
169 if (pte_none(*pte))
170 set_pte(pte, __pte(ppd->paddr | ppd->pte_flags));
Kirill A. Shutemov1cd9c222018-01-31 16:54:02 +0300171}
172
173static void __init __sme_map_range_pmd(struct sme_populate_pgd_data *ppd)
174{
175 while (ppd->vaddr < ppd->vaddr_end) {
176 sme_populate_pgd_large(ppd);
177
178 ppd->vaddr += PMD_PAGE_SIZE;
179 ppd->paddr += PMD_PAGE_SIZE;
180 }
181}
182
183static void __init __sme_map_range_pte(struct sme_populate_pgd_data *ppd)
184{
185 while (ppd->vaddr < ppd->vaddr_end) {
186 sme_populate_pgd(ppd);
187
188 ppd->vaddr += PAGE_SIZE;
189 ppd->paddr += PAGE_SIZE;
190 }
191}
192
193static void __init __sme_map_range(struct sme_populate_pgd_data *ppd,
194 pmdval_t pmd_flags, pteval_t pte_flags)
195{
196 unsigned long vaddr_end;
197
198 ppd->pmd_flags = pmd_flags;
199 ppd->pte_flags = pte_flags;
200
201 /* Save original end value since we modify the struct value */
202 vaddr_end = ppd->vaddr_end;
203
204 /* If start is not 2MB aligned, create PTE entries */
205 ppd->vaddr_end = ALIGN(ppd->vaddr, PMD_PAGE_SIZE);
206 __sme_map_range_pte(ppd);
207
208 /* Create PMD entries */
209 ppd->vaddr_end = vaddr_end & PMD_PAGE_MASK;
210 __sme_map_range_pmd(ppd);
211
212 /* If end is not 2MB aligned, create PTE entries */
213 ppd->vaddr_end = vaddr_end;
214 __sme_map_range_pte(ppd);
215}
216
217static void __init sme_map_range_encrypted(struct sme_populate_pgd_data *ppd)
218{
219 __sme_map_range(ppd, PMD_FLAGS_ENC, PTE_FLAGS_ENC);
220}
221
222static void __init sme_map_range_decrypted(struct sme_populate_pgd_data *ppd)
223{
224 __sme_map_range(ppd, PMD_FLAGS_DEC, PTE_FLAGS_DEC);
225}
226
227static void __init sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd)
228{
229 __sme_map_range(ppd, PMD_FLAGS_DEC_WP, PTE_FLAGS_DEC_WP);
230}
231
232static unsigned long __init sme_pgtable_calc(unsigned long len)
233{
234 unsigned long p4d_size, pud_size, pmd_size, pte_size;
235 unsigned long total;
236
237 /*
238 * Perform a relatively simplistic calculation of the pagetable
239 * entries that are needed. Those mappings will be covered mostly
240 * by 2MB PMD entries so we can conservatively calculate the required
241 * number of P4D, PUD and PMD structures needed to perform the
242 * mappings. For mappings that are not 2MB aligned, PTE mappings
243 * would be needed for the start and end portion of the address range
244 * that fall outside of the 2MB alignment. This results in, at most,
245 * two extra pages to hold PTE entries for each range that is mapped.
246 * Incrementing the count for each covers the case where the addresses
247 * cross entries.
248 */
249 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
250 p4d_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1;
251 p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D;
252 pud_size = (ALIGN(len, P4D_SIZE) / P4D_SIZE) + 1;
253 pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
254 } else {
255 p4d_size = 0;
256 pud_size = (ALIGN(len, PGDIR_SIZE) / PGDIR_SIZE) + 1;
257 pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
258 }
259 pmd_size = (ALIGN(len, PUD_SIZE) / PUD_SIZE) + 1;
260 pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD;
261 pte_size = 2 * sizeof(pte_t) * PTRS_PER_PTE;
262
263 total = p4d_size + pud_size + pmd_size + pte_size;
264
265 /*
266 * Now calculate the added pagetable structures needed to populate
267 * the new pagetables.
268 */
269 if (IS_ENABLED(CONFIG_X86_5LEVEL)) {
270 p4d_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE;
271 p4d_size *= sizeof(p4d_t) * PTRS_PER_P4D;
272 pud_size = ALIGN(total, P4D_SIZE) / P4D_SIZE;
273 pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
274 } else {
275 p4d_size = 0;
276 pud_size = ALIGN(total, PGDIR_SIZE) / PGDIR_SIZE;
277 pud_size *= sizeof(pud_t) * PTRS_PER_PUD;
278 }
279 pmd_size = ALIGN(total, PUD_SIZE) / PUD_SIZE;
280 pmd_size *= sizeof(pmd_t) * PTRS_PER_PMD;
281
282 total += p4d_size + pud_size + pmd_size;
283
284 return total;
285}
286
287void __init __nostackprotector sme_encrypt_kernel(struct boot_params *bp)
288{
289 unsigned long workarea_start, workarea_end, workarea_len;
290 unsigned long execute_start, execute_end, execute_len;
291 unsigned long kernel_start, kernel_end, kernel_len;
292 unsigned long initrd_start, initrd_end, initrd_len;
293 struct sme_populate_pgd_data ppd;
294 unsigned long pgtable_area_len;
295 unsigned long decrypted_base;
296
297 if (!sme_active())
298 return;
299
300 /*
301 * Prepare for encrypting the kernel and initrd by building new
302 * pagetables with the necessary attributes needed to encrypt the
303 * kernel in place.
304 *
305 * One range of virtual addresses will map the memory occupied
306 * by the kernel and initrd as encrypted.
307 *
308 * Another range of virtual addresses will map the memory occupied
309 * by the kernel and initrd as decrypted and write-protected.
310 *
311 * The use of write-protect attribute will prevent any of the
312 * memory from being cached.
313 */
314
315 /* Physical addresses gives us the identity mapped virtual addresses */
316 kernel_start = __pa_symbol(_text);
317 kernel_end = ALIGN(__pa_symbol(_end), PMD_PAGE_SIZE);
318 kernel_len = kernel_end - kernel_start;
319
320 initrd_start = 0;
321 initrd_end = 0;
322 initrd_len = 0;
323#ifdef CONFIG_BLK_DEV_INITRD
324 initrd_len = (unsigned long)bp->hdr.ramdisk_size |
325 ((unsigned long)bp->ext_ramdisk_size << 32);
326 if (initrd_len) {
327 initrd_start = (unsigned long)bp->hdr.ramdisk_image |
328 ((unsigned long)bp->ext_ramdisk_image << 32);
329 initrd_end = PAGE_ALIGN(initrd_start + initrd_len);
330 initrd_len = initrd_end - initrd_start;
331 }
332#endif
333
334 /* Set the encryption workarea to be immediately after the kernel */
335 workarea_start = kernel_end;
336
337 /*
338 * Calculate required number of workarea bytes needed:
339 * executable encryption area size:
340 * stack page (PAGE_SIZE)
341 * encryption routine page (PAGE_SIZE)
342 * intermediate copy buffer (PMD_PAGE_SIZE)
343 * pagetable structures for the encryption of the kernel
344 * pagetable structures for workarea (in case not currently mapped)
345 */
346 execute_start = workarea_start;
347 execute_end = execute_start + (PAGE_SIZE * 2) + PMD_PAGE_SIZE;
348 execute_len = execute_end - execute_start;
349
350 /*
351 * One PGD for both encrypted and decrypted mappings and a set of
352 * PUDs and PMDs for each of the encrypted and decrypted mappings.
353 */
354 pgtable_area_len = sizeof(pgd_t) * PTRS_PER_PGD;
355 pgtable_area_len += sme_pgtable_calc(execute_end - kernel_start) * 2;
356 if (initrd_len)
357 pgtable_area_len += sme_pgtable_calc(initrd_len) * 2;
358
359 /* PUDs and PMDs needed in the current pagetables for the workarea */
360 pgtable_area_len += sme_pgtable_calc(execute_len + pgtable_area_len);
361
362 /*
363 * The total workarea includes the executable encryption area and
364 * the pagetable area. The start of the workarea is already 2MB
365 * aligned, align the end of the workarea on a 2MB boundary so that
366 * we don't try to create/allocate PTE entries from the workarea
367 * before it is mapped.
368 */
369 workarea_len = execute_len + pgtable_area_len;
370 workarea_end = ALIGN(workarea_start + workarea_len, PMD_PAGE_SIZE);
371
372 /*
373 * Set the address to the start of where newly created pagetable
374 * structures (PGDs, PUDs and PMDs) will be allocated. New pagetable
375 * structures are created when the workarea is added to the current
376 * pagetables and when the new encrypted and decrypted kernel
377 * mappings are populated.
378 */
379 ppd.pgtable_area = (void *)execute_end;
380
381 /*
382 * Make sure the current pagetable structure has entries for
383 * addressing the workarea.
384 */
385 ppd.pgd = (pgd_t *)native_read_cr3_pa();
386 ppd.paddr = workarea_start;
387 ppd.vaddr = workarea_start;
388 ppd.vaddr_end = workarea_end;
389 sme_map_range_decrypted(&ppd);
390
391 /* Flush the TLB - no globals so cr3 is enough */
392 native_write_cr3(__native_read_cr3());
393
394 /*
395 * A new pagetable structure is being built to allow for the kernel
396 * and initrd to be encrypted. It starts with an empty PGD that will
397 * then be populated with new PUDs and PMDs as the encrypted and
398 * decrypted kernel mappings are created.
399 */
400 ppd.pgd = ppd.pgtable_area;
401 memset(ppd.pgd, 0, sizeof(pgd_t) * PTRS_PER_PGD);
402 ppd.pgtable_area += sizeof(pgd_t) * PTRS_PER_PGD;
403
404 /*
405 * A different PGD index/entry must be used to get different
406 * pagetable entries for the decrypted mapping. Choose the next
407 * PGD index and convert it to a virtual address to be used as
408 * the base of the mapping.
409 */
410 decrypted_base = (pgd_index(workarea_end) + 1) & (PTRS_PER_PGD - 1);
411 if (initrd_len) {
412 unsigned long check_base;
413
414 check_base = (pgd_index(initrd_end) + 1) & (PTRS_PER_PGD - 1);
415 decrypted_base = max(decrypted_base, check_base);
416 }
417 decrypted_base <<= PGDIR_SHIFT;
418
419 /* Add encrypted kernel (identity) mappings */
420 ppd.paddr = kernel_start;
421 ppd.vaddr = kernel_start;
422 ppd.vaddr_end = kernel_end;
423 sme_map_range_encrypted(&ppd);
424
425 /* Add decrypted, write-protected kernel (non-identity) mappings */
426 ppd.paddr = kernel_start;
427 ppd.vaddr = kernel_start + decrypted_base;
428 ppd.vaddr_end = kernel_end + decrypted_base;
429 sme_map_range_decrypted_wp(&ppd);
430
431 if (initrd_len) {
432 /* Add encrypted initrd (identity) mappings */
433 ppd.paddr = initrd_start;
434 ppd.vaddr = initrd_start;
435 ppd.vaddr_end = initrd_end;
436 sme_map_range_encrypted(&ppd);
437 /*
438 * Add decrypted, write-protected initrd (non-identity) mappings
439 */
440 ppd.paddr = initrd_start;
441 ppd.vaddr = initrd_start + decrypted_base;
442 ppd.vaddr_end = initrd_end + decrypted_base;
443 sme_map_range_decrypted_wp(&ppd);
444 }
445
446 /* Add decrypted workarea mappings to both kernel mappings */
447 ppd.paddr = workarea_start;
448 ppd.vaddr = workarea_start;
449 ppd.vaddr_end = workarea_end;
450 sme_map_range_decrypted(&ppd);
451
452 ppd.paddr = workarea_start;
453 ppd.vaddr = workarea_start + decrypted_base;
454 ppd.vaddr_end = workarea_end + decrypted_base;
455 sme_map_range_decrypted(&ppd);
456
457 /* Perform the encryption */
458 sme_encrypt_execute(kernel_start, kernel_start + decrypted_base,
459 kernel_len, workarea_start, (unsigned long)ppd.pgd);
460
461 if (initrd_len)
462 sme_encrypt_execute(initrd_start, initrd_start + decrypted_base,
463 initrd_len, workarea_start,
464 (unsigned long)ppd.pgd);
465
466 /*
467 * At this point we are running encrypted. Remove the mappings for
468 * the decrypted areas - all that is needed for this is to remove
469 * the PGD entry/entries.
470 */
471 ppd.vaddr = kernel_start + decrypted_base;
472 ppd.vaddr_end = kernel_end + decrypted_base;
473 sme_clear_pgd(&ppd);
474
475 if (initrd_len) {
476 ppd.vaddr = initrd_start + decrypted_base;
477 ppd.vaddr_end = initrd_end + decrypted_base;
478 sme_clear_pgd(&ppd);
479 }
480
481 ppd.vaddr = workarea_start + decrypted_base;
482 ppd.vaddr_end = workarea_end + decrypted_base;
483 sme_clear_pgd(&ppd);
484
485 /* Flush the TLB - no globals so cr3 is enough */
486 native_write_cr3(__native_read_cr3());
487}
488
489void __init __nostackprotector sme_enable(struct boot_params *bp)
490{
491 const char *cmdline_ptr, *cmdline_arg, *cmdline_on, *cmdline_off;
492 unsigned int eax, ebx, ecx, edx;
493 unsigned long feature_mask;
494 bool active_by_default;
495 unsigned long me_mask;
496 char buffer[16];
497 u64 msr;
498
499 /* Check for the SME/SEV support leaf */
500 eax = 0x80000000;
501 ecx = 0;
502 native_cpuid(&eax, &ebx, &ecx, &edx);
503 if (eax < 0x8000001f)
504 return;
505
506#define AMD_SME_BIT BIT(0)
507#define AMD_SEV_BIT BIT(1)
508 /*
509 * Set the feature mask (SME or SEV) based on whether we are
510 * running under a hypervisor.
511 */
512 eax = 1;
513 ecx = 0;
514 native_cpuid(&eax, &ebx, &ecx, &edx);
515 feature_mask = (ecx & BIT(31)) ? AMD_SEV_BIT : AMD_SME_BIT;
516
517 /*
518 * Check for the SME/SEV feature:
519 * CPUID Fn8000_001F[EAX]
520 * - Bit 0 - Secure Memory Encryption support
521 * - Bit 1 - Secure Encrypted Virtualization support
522 * CPUID Fn8000_001F[EBX]
523 * - Bits 5:0 - Pagetable bit position used to indicate encryption
524 */
525 eax = 0x8000001f;
526 ecx = 0;
527 native_cpuid(&eax, &ebx, &ecx, &edx);
528 if (!(eax & feature_mask))
529 return;
530
531 me_mask = 1UL << (ebx & 0x3f);
532
533 /* Check if memory encryption is enabled */
534 if (feature_mask == AMD_SME_BIT) {
535 /* For SME, check the SYSCFG MSR */
536 msr = __rdmsr(MSR_K8_SYSCFG);
537 if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
538 return;
539 } else {
540 /* For SEV, check the SEV MSR */
541 msr = __rdmsr(MSR_AMD64_SEV);
542 if (!(msr & MSR_AMD64_SEV_ENABLED))
543 return;
544
545 /* SEV state cannot be controlled by a command line option */
546 sme_me_mask = me_mask;
547 sev_enabled = true;
548 return;
549 }
550
551 /*
552 * Fixups have not been applied to phys_base yet and we're running
553 * identity mapped, so we must obtain the address to the SME command
554 * line argument data using rip-relative addressing.
555 */
556 asm ("lea sme_cmdline_arg(%%rip), %0"
557 : "=r" (cmdline_arg)
558 : "p" (sme_cmdline_arg));
559 asm ("lea sme_cmdline_on(%%rip), %0"
560 : "=r" (cmdline_on)
561 : "p" (sme_cmdline_on));
562 asm ("lea sme_cmdline_off(%%rip), %0"
563 : "=r" (cmdline_off)
564 : "p" (sme_cmdline_off));
565
566 if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT))
567 active_by_default = true;
568 else
569 active_by_default = false;
570
571 cmdline_ptr = (const char *)((u64)bp->hdr.cmd_line_ptr |
572 ((u64)bp->ext_cmd_line_ptr << 32));
573
574 cmdline_find_option(cmdline_ptr, cmdline_arg, buffer, sizeof(buffer));
575
576 if (!strncmp(buffer, cmdline_on, sizeof(buffer)))
577 sme_me_mask = me_mask;
578 else if (!strncmp(buffer, cmdline_off, sizeof(buffer)))
579 sme_me_mask = 0;
580 else
581 sme_me_mask = active_by_default ? me_mask : 0;
582}