18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * AMD Memory Encryption Support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 Advanced Micro Devices, Inc.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Tom Lendacky <thomas.lendacky@amd.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define DISABLE_BRANCH_PROFILING
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * Since we're dealing with identity mappings, physical and virtual
148c2ecf20Sopenharmony_ci * addresses are the same, so override these defines which are ultimately
158c2ecf20Sopenharmony_ci * used by the headers in misc.h.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci#define __pa(x)  ((unsigned long)(x))
188c2ecf20Sopenharmony_ci#define __va(x)  ((void *)((unsigned long)(x)))
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * Special hack: we have to be careful, because no indirections are
228c2ecf20Sopenharmony_ci * allowed here, and paravirt_ops is a kind of one. As it will only run in
238c2ecf20Sopenharmony_ci * baremetal anyway, we just keep it from happening. (This list needs to
248c2ecf20Sopenharmony_ci * be extended when new paravirt and debugging variants are added.)
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci#undef CONFIG_PARAVIRT
278c2ecf20Sopenharmony_ci#undef CONFIG_PARAVIRT_XXL
288c2ecf20Sopenharmony_ci#undef CONFIG_PARAVIRT_SPINLOCKS
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * This code runs before CPU feature bits are set. By default, the
328c2ecf20Sopenharmony_ci * pgtable_l5_enabled() function uses bit X86_FEATURE_LA57 to determine if
338c2ecf20Sopenharmony_ci * 5-level paging is active, so that won't work here. USE_EARLY_PGTABLE_L5
348c2ecf20Sopenharmony_ci * is provided to handle this situation and, instead, use a variable that
358c2ecf20Sopenharmony_ci * has been set by the early boot code.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci#define USE_EARLY_PGTABLE_L5
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include <linux/kernel.h>
408c2ecf20Sopenharmony_ci#include <linux/mm.h>
418c2ecf20Sopenharmony_ci#include <linux/mem_encrypt.h>
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#include <asm/setup.h>
448c2ecf20Sopenharmony_ci#include <asm/sections.h>
458c2ecf20Sopenharmony_ci#include <asm/cmdline.h>
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#include "mm_internal.h"
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define PGD_FLAGS		_KERNPG_TABLE_NOENC
508c2ecf20Sopenharmony_ci#define P4D_FLAGS		_KERNPG_TABLE_NOENC
518c2ecf20Sopenharmony_ci#define PUD_FLAGS		_KERNPG_TABLE_NOENC
528c2ecf20Sopenharmony_ci#define PMD_FLAGS		_KERNPG_TABLE_NOENC
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define PMD_FLAGS_LARGE		(__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define PMD_FLAGS_DEC		PMD_FLAGS_LARGE
578c2ecf20Sopenharmony_ci#define PMD_FLAGS_DEC_WP	((PMD_FLAGS_DEC & ~_PAGE_LARGE_CACHE_MASK) | \
588c2ecf20Sopenharmony_ci				 (_PAGE_PAT_LARGE | _PAGE_PWT))
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define PMD_FLAGS_ENC		(PMD_FLAGS_LARGE | _PAGE_ENC)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define PTE_FLAGS		(__PAGE_KERNEL_EXEC & ~_PAGE_GLOBAL)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define PTE_FLAGS_DEC		PTE_FLAGS
658c2ecf20Sopenharmony_ci#define PTE_FLAGS_DEC_WP	((PTE_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \
668c2ecf20Sopenharmony_ci				 (_PAGE_PAT | _PAGE_PWT))
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define PTE_FLAGS_ENC		(PTE_FLAGS | _PAGE_ENC)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistruct sme_populate_pgd_data {
718c2ecf20Sopenharmony_ci	void    *pgtable_area;
728c2ecf20Sopenharmony_ci	pgd_t   *pgd;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	pmdval_t pmd_flags;
758c2ecf20Sopenharmony_ci	pteval_t pte_flags;
768c2ecf20Sopenharmony_ci	unsigned long paddr;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	unsigned long vaddr;
798c2ecf20Sopenharmony_ci	unsigned long vaddr_end;
808c2ecf20Sopenharmony_ci};
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/*
838c2ecf20Sopenharmony_ci * This work area lives in the .init.scratch section, which lives outside of
848c2ecf20Sopenharmony_ci * the kernel proper. It is sized to hold the intermediate copy buffer and
858c2ecf20Sopenharmony_ci * more than enough pagetable pages.
868c2ecf20Sopenharmony_ci *
878c2ecf20Sopenharmony_ci * By using this section, the kernel can be encrypted in place and it
888c2ecf20Sopenharmony_ci * avoids any possibility of boot parameters or initramfs images being
898c2ecf20Sopenharmony_ci * placed such that the in-place encryption logic overwrites them.  This
908c2ecf20Sopenharmony_ci * section is 2MB aligned to allow for simple pagetable setup using only
918c2ecf20Sopenharmony_ci * PMD entries (see vmlinux.lds.S).
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_cistatic char sme_workarea[2 * PMD_PAGE_SIZE] __section(".init.scratch");
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic char sme_cmdline_arg[] __initdata = "mem_encrypt";
968c2ecf20Sopenharmony_cistatic char sme_cmdline_on[]  __initdata = "on";
978c2ecf20Sopenharmony_cistatic char sme_cmdline_off[] __initdata = "off";
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic void __init sme_clear_pgd(struct sme_populate_pgd_data *ppd)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	unsigned long pgd_start, pgd_end, pgd_size;
1028c2ecf20Sopenharmony_ci	pgd_t *pgd_p;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	pgd_start = ppd->vaddr & PGDIR_MASK;
1058c2ecf20Sopenharmony_ci	pgd_end = ppd->vaddr_end & PGDIR_MASK;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	pgd_p = ppd->pgd + pgd_index(ppd->vaddr);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	memset(pgd_p, 0, pgd_size);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic pud_t __init *sme_prepare_pgd(struct sme_populate_pgd_data *ppd)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	pgd_t *pgd;
1178c2ecf20Sopenharmony_ci	p4d_t *p4d;
1188c2ecf20Sopenharmony_ci	pud_t *pud;
1198c2ecf20Sopenharmony_ci	pmd_t *pmd;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	pgd = ppd->pgd + pgd_index(ppd->vaddr);
1228c2ecf20Sopenharmony_ci	if (pgd_none(*pgd)) {
1238c2ecf20Sopenharmony_ci		p4d = ppd->pgtable_area;
1248c2ecf20Sopenharmony_ci		memset(p4d, 0, sizeof(*p4d) * PTRS_PER_P4D);
1258c2ecf20Sopenharmony_ci		ppd->pgtable_area += sizeof(*p4d) * PTRS_PER_P4D;
1268c2ecf20Sopenharmony_ci		set_pgd(pgd, __pgd(PGD_FLAGS | __pa(p4d)));
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	p4d = p4d_offset(pgd, ppd->vaddr);
1308c2ecf20Sopenharmony_ci	if (p4d_none(*p4d)) {
1318c2ecf20Sopenharmony_ci		pud = ppd->pgtable_area;
1328c2ecf20Sopenharmony_ci		memset(pud, 0, sizeof(*pud) * PTRS_PER_PUD);
1338c2ecf20Sopenharmony_ci		ppd->pgtable_area += sizeof(*pud) * PTRS_PER_PUD;
1348c2ecf20Sopenharmony_ci		set_p4d(p4d, __p4d(P4D_FLAGS | __pa(pud)));
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	pud = pud_offset(p4d, ppd->vaddr);
1388c2ecf20Sopenharmony_ci	if (pud_none(*pud)) {
1398c2ecf20Sopenharmony_ci		pmd = ppd->pgtable_area;
1408c2ecf20Sopenharmony_ci		memset(pmd, 0, sizeof(*pmd) * PTRS_PER_PMD);
1418c2ecf20Sopenharmony_ci		ppd->pgtable_area += sizeof(*pmd) * PTRS_PER_PMD;
1428c2ecf20Sopenharmony_ci		set_pud(pud, __pud(PUD_FLAGS | __pa(pmd)));
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (pud_large(*pud))
1468c2ecf20Sopenharmony_ci		return NULL;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	return pud;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic void __init sme_populate_pgd_large(struct sme_populate_pgd_data *ppd)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	pud_t *pud;
1548c2ecf20Sopenharmony_ci	pmd_t *pmd;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	pud = sme_prepare_pgd(ppd);
1578c2ecf20Sopenharmony_ci	if (!pud)
1588c2ecf20Sopenharmony_ci		return;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	pmd = pmd_offset(pud, ppd->vaddr);
1618c2ecf20Sopenharmony_ci	if (pmd_large(*pmd))
1628c2ecf20Sopenharmony_ci		return;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	set_pmd(pmd, __pmd(ppd->paddr | ppd->pmd_flags));
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic void __init sme_populate_pgd(struct sme_populate_pgd_data *ppd)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	pud_t *pud;
1708c2ecf20Sopenharmony_ci	pmd_t *pmd;
1718c2ecf20Sopenharmony_ci	pte_t *pte;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	pud = sme_prepare_pgd(ppd);
1748c2ecf20Sopenharmony_ci	if (!pud)
1758c2ecf20Sopenharmony_ci		return;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	pmd = pmd_offset(pud, ppd->vaddr);
1788c2ecf20Sopenharmony_ci	if (pmd_none(*pmd)) {
1798c2ecf20Sopenharmony_ci		pte = ppd->pgtable_area;
1808c2ecf20Sopenharmony_ci		memset(pte, 0, sizeof(*pte) * PTRS_PER_PTE);
1818c2ecf20Sopenharmony_ci		ppd->pgtable_area += sizeof(*pte) * PTRS_PER_PTE;
1828c2ecf20Sopenharmony_ci		set_pmd(pmd, __pmd(PMD_FLAGS | __pa(pte)));
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (pmd_large(*pmd))
1868c2ecf20Sopenharmony_ci		return;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	pte = pte_offset_map(pmd, ppd->vaddr);
1898c2ecf20Sopenharmony_ci	if (pte_none(*pte))
1908c2ecf20Sopenharmony_ci		set_pte(pte, __pte(ppd->paddr | ppd->pte_flags));
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic void __init __sme_map_range_pmd(struct sme_populate_pgd_data *ppd)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	while (ppd->vaddr < ppd->vaddr_end) {
1968c2ecf20Sopenharmony_ci		sme_populate_pgd_large(ppd);
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci		ppd->vaddr += PMD_PAGE_SIZE;
1998c2ecf20Sopenharmony_ci		ppd->paddr += PMD_PAGE_SIZE;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic void __init __sme_map_range_pte(struct sme_populate_pgd_data *ppd)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	while (ppd->vaddr < ppd->vaddr_end) {
2068c2ecf20Sopenharmony_ci		sme_populate_pgd(ppd);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci		ppd->vaddr += PAGE_SIZE;
2098c2ecf20Sopenharmony_ci		ppd->paddr += PAGE_SIZE;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic void __init __sme_map_range(struct sme_populate_pgd_data *ppd,
2148c2ecf20Sopenharmony_ci				   pmdval_t pmd_flags, pteval_t pte_flags)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	unsigned long vaddr_end;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	ppd->pmd_flags = pmd_flags;
2198c2ecf20Sopenharmony_ci	ppd->pte_flags = pte_flags;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* Save original end value since we modify the struct value */
2228c2ecf20Sopenharmony_ci	vaddr_end = ppd->vaddr_end;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/* If start is not 2MB aligned, create PTE entries */
2258c2ecf20Sopenharmony_ci	ppd->vaddr_end = ALIGN(ppd->vaddr, PMD_PAGE_SIZE);
2268c2ecf20Sopenharmony_ci	__sme_map_range_pte(ppd);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* Create PMD entries */
2298c2ecf20Sopenharmony_ci	ppd->vaddr_end = vaddr_end & PMD_PAGE_MASK;
2308c2ecf20Sopenharmony_ci	__sme_map_range_pmd(ppd);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* If end is not 2MB aligned, create PTE entries */
2338c2ecf20Sopenharmony_ci	ppd->vaddr_end = vaddr_end;
2348c2ecf20Sopenharmony_ci	__sme_map_range_pte(ppd);
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic void __init sme_map_range_encrypted(struct sme_populate_pgd_data *ppd)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	__sme_map_range(ppd, PMD_FLAGS_ENC, PTE_FLAGS_ENC);
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic void __init sme_map_range_decrypted(struct sme_populate_pgd_data *ppd)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	__sme_map_range(ppd, PMD_FLAGS_DEC, PTE_FLAGS_DEC);
2458c2ecf20Sopenharmony_ci}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic void __init sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	__sme_map_range(ppd, PMD_FLAGS_DEC_WP, PTE_FLAGS_DEC_WP);
2508c2ecf20Sopenharmony_ci}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_cistatic unsigned long __init sme_pgtable_calc(unsigned long len)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	unsigned long entries = 0, tables = 0;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	/*
2578c2ecf20Sopenharmony_ci	 * Perform a relatively simplistic calculation of the pagetable
2588c2ecf20Sopenharmony_ci	 * entries that are needed. Those mappings will be covered mostly
2598c2ecf20Sopenharmony_ci	 * by 2MB PMD entries so we can conservatively calculate the required
2608c2ecf20Sopenharmony_ci	 * number of P4D, PUD and PMD structures needed to perform the
2618c2ecf20Sopenharmony_ci	 * mappings.  For mappings that are not 2MB aligned, PTE mappings
2628c2ecf20Sopenharmony_ci	 * would be needed for the start and end portion of the address range
2638c2ecf20Sopenharmony_ci	 * that fall outside of the 2MB alignment.  This results in, at most,
2648c2ecf20Sopenharmony_ci	 * two extra pages to hold PTE entries for each range that is mapped.
2658c2ecf20Sopenharmony_ci	 * Incrementing the count for each covers the case where the addresses
2668c2ecf20Sopenharmony_ci	 * cross entries.
2678c2ecf20Sopenharmony_ci	 */
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	/* PGDIR_SIZE is equal to P4D_SIZE on 4-level machine. */
2708c2ecf20Sopenharmony_ci	if (PTRS_PER_P4D > 1)
2718c2ecf20Sopenharmony_ci		entries += (DIV_ROUND_UP(len, PGDIR_SIZE) + 1) * sizeof(p4d_t) * PTRS_PER_P4D;
2728c2ecf20Sopenharmony_ci	entries += (DIV_ROUND_UP(len, P4D_SIZE) + 1) * sizeof(pud_t) * PTRS_PER_PUD;
2738c2ecf20Sopenharmony_ci	entries += (DIV_ROUND_UP(len, PUD_SIZE) + 1) * sizeof(pmd_t) * PTRS_PER_PMD;
2748c2ecf20Sopenharmony_ci	entries += 2 * sizeof(pte_t) * PTRS_PER_PTE;
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	/*
2778c2ecf20Sopenharmony_ci	 * Now calculate the added pagetable structures needed to populate
2788c2ecf20Sopenharmony_ci	 * the new pagetables.
2798c2ecf20Sopenharmony_ci	 */
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (PTRS_PER_P4D > 1)
2828c2ecf20Sopenharmony_ci		tables += DIV_ROUND_UP(entries, PGDIR_SIZE) * sizeof(p4d_t) * PTRS_PER_P4D;
2838c2ecf20Sopenharmony_ci	tables += DIV_ROUND_UP(entries, P4D_SIZE) * sizeof(pud_t) * PTRS_PER_PUD;
2848c2ecf20Sopenharmony_ci	tables += DIV_ROUND_UP(entries, PUD_SIZE) * sizeof(pmd_t) * PTRS_PER_PMD;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return entries + tables;
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_civoid __init sme_encrypt_kernel(struct boot_params *bp)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	unsigned long workarea_start, workarea_end, workarea_len;
2928c2ecf20Sopenharmony_ci	unsigned long execute_start, execute_end, execute_len;
2938c2ecf20Sopenharmony_ci	unsigned long kernel_start, kernel_end, kernel_len;
2948c2ecf20Sopenharmony_ci	unsigned long initrd_start, initrd_end, initrd_len;
2958c2ecf20Sopenharmony_ci	struct sme_populate_pgd_data ppd;
2968c2ecf20Sopenharmony_ci	unsigned long pgtable_area_len;
2978c2ecf20Sopenharmony_ci	unsigned long decrypted_base;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if (!sme_active())
3008c2ecf20Sopenharmony_ci		return;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	/*
3038c2ecf20Sopenharmony_ci	 * Prepare for encrypting the kernel and initrd by building new
3048c2ecf20Sopenharmony_ci	 * pagetables with the necessary attributes needed to encrypt the
3058c2ecf20Sopenharmony_ci	 * kernel in place.
3068c2ecf20Sopenharmony_ci	 *
3078c2ecf20Sopenharmony_ci	 *   One range of virtual addresses will map the memory occupied
3088c2ecf20Sopenharmony_ci	 *   by the kernel and initrd as encrypted.
3098c2ecf20Sopenharmony_ci	 *
3108c2ecf20Sopenharmony_ci	 *   Another range of virtual addresses will map the memory occupied
3118c2ecf20Sopenharmony_ci	 *   by the kernel and initrd as decrypted and write-protected.
3128c2ecf20Sopenharmony_ci	 *
3138c2ecf20Sopenharmony_ci	 *     The use of write-protect attribute will prevent any of the
3148c2ecf20Sopenharmony_ci	 *     memory from being cached.
3158c2ecf20Sopenharmony_ci	 */
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* Physical addresses gives us the identity mapped virtual addresses */
3188c2ecf20Sopenharmony_ci	kernel_start = __pa_symbol(_text);
3198c2ecf20Sopenharmony_ci	kernel_end = ALIGN(__pa_symbol(_end), PMD_PAGE_SIZE);
3208c2ecf20Sopenharmony_ci	kernel_len = kernel_end - kernel_start;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	initrd_start = 0;
3238c2ecf20Sopenharmony_ci	initrd_end = 0;
3248c2ecf20Sopenharmony_ci	initrd_len = 0;
3258c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
3268c2ecf20Sopenharmony_ci	initrd_len = (unsigned long)bp->hdr.ramdisk_size |
3278c2ecf20Sopenharmony_ci		     ((unsigned long)bp->ext_ramdisk_size << 32);
3288c2ecf20Sopenharmony_ci	if (initrd_len) {
3298c2ecf20Sopenharmony_ci		initrd_start = (unsigned long)bp->hdr.ramdisk_image |
3308c2ecf20Sopenharmony_ci			       ((unsigned long)bp->ext_ramdisk_image << 32);
3318c2ecf20Sopenharmony_ci		initrd_end = PAGE_ALIGN(initrd_start + initrd_len);
3328c2ecf20Sopenharmony_ci		initrd_len = initrd_end - initrd_start;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci#endif
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/*
3378c2ecf20Sopenharmony_ci	 * We're running identity mapped, so we must obtain the address to the
3388c2ecf20Sopenharmony_ci	 * SME encryption workarea using rip-relative addressing.
3398c2ecf20Sopenharmony_ci	 */
3408c2ecf20Sopenharmony_ci	asm ("lea sme_workarea(%%rip), %0"
3418c2ecf20Sopenharmony_ci	     : "=r" (workarea_start)
3428c2ecf20Sopenharmony_ci	     : "p" (sme_workarea));
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/*
3458c2ecf20Sopenharmony_ci	 * Calculate required number of workarea bytes needed:
3468c2ecf20Sopenharmony_ci	 *   executable encryption area size:
3478c2ecf20Sopenharmony_ci	 *     stack page (PAGE_SIZE)
3488c2ecf20Sopenharmony_ci	 *     encryption routine page (PAGE_SIZE)
3498c2ecf20Sopenharmony_ci	 *     intermediate copy buffer (PMD_PAGE_SIZE)
3508c2ecf20Sopenharmony_ci	 *   pagetable structures for the encryption of the kernel
3518c2ecf20Sopenharmony_ci	 *   pagetable structures for workarea (in case not currently mapped)
3528c2ecf20Sopenharmony_ci	 */
3538c2ecf20Sopenharmony_ci	execute_start = workarea_start;
3548c2ecf20Sopenharmony_ci	execute_end = execute_start + (PAGE_SIZE * 2) + PMD_PAGE_SIZE;
3558c2ecf20Sopenharmony_ci	execute_len = execute_end - execute_start;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/*
3588c2ecf20Sopenharmony_ci	 * One PGD for both encrypted and decrypted mappings and a set of
3598c2ecf20Sopenharmony_ci	 * PUDs and PMDs for each of the encrypted and decrypted mappings.
3608c2ecf20Sopenharmony_ci	 */
3618c2ecf20Sopenharmony_ci	pgtable_area_len = sizeof(pgd_t) * PTRS_PER_PGD;
3628c2ecf20Sopenharmony_ci	pgtable_area_len += sme_pgtable_calc(execute_end - kernel_start) * 2;
3638c2ecf20Sopenharmony_ci	if (initrd_len)
3648c2ecf20Sopenharmony_ci		pgtable_area_len += sme_pgtable_calc(initrd_len) * 2;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	/* PUDs and PMDs needed in the current pagetables for the workarea */
3678c2ecf20Sopenharmony_ci	pgtable_area_len += sme_pgtable_calc(execute_len + pgtable_area_len);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	/*
3708c2ecf20Sopenharmony_ci	 * The total workarea includes the executable encryption area and
3718c2ecf20Sopenharmony_ci	 * the pagetable area. The start of the workarea is already 2MB
3728c2ecf20Sopenharmony_ci	 * aligned, align the end of the workarea on a 2MB boundary so that
3738c2ecf20Sopenharmony_ci	 * we don't try to create/allocate PTE entries from the workarea
3748c2ecf20Sopenharmony_ci	 * before it is mapped.
3758c2ecf20Sopenharmony_ci	 */
3768c2ecf20Sopenharmony_ci	workarea_len = execute_len + pgtable_area_len;
3778c2ecf20Sopenharmony_ci	workarea_end = ALIGN(workarea_start + workarea_len, PMD_PAGE_SIZE);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/*
3808c2ecf20Sopenharmony_ci	 * Set the address to the start of where newly created pagetable
3818c2ecf20Sopenharmony_ci	 * structures (PGDs, PUDs and PMDs) will be allocated. New pagetable
3828c2ecf20Sopenharmony_ci	 * structures are created when the workarea is added to the current
3838c2ecf20Sopenharmony_ci	 * pagetables and when the new encrypted and decrypted kernel
3848c2ecf20Sopenharmony_ci	 * mappings are populated.
3858c2ecf20Sopenharmony_ci	 */
3868c2ecf20Sopenharmony_ci	ppd.pgtable_area = (void *)execute_end;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/*
3898c2ecf20Sopenharmony_ci	 * Make sure the current pagetable structure has entries for
3908c2ecf20Sopenharmony_ci	 * addressing the workarea.
3918c2ecf20Sopenharmony_ci	 */
3928c2ecf20Sopenharmony_ci	ppd.pgd = (pgd_t *)native_read_cr3_pa();
3938c2ecf20Sopenharmony_ci	ppd.paddr = workarea_start;
3948c2ecf20Sopenharmony_ci	ppd.vaddr = workarea_start;
3958c2ecf20Sopenharmony_ci	ppd.vaddr_end = workarea_end;
3968c2ecf20Sopenharmony_ci	sme_map_range_decrypted(&ppd);
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/* Flush the TLB - no globals so cr3 is enough */
3998c2ecf20Sopenharmony_ci	native_write_cr3(__native_read_cr3());
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	/*
4028c2ecf20Sopenharmony_ci	 * A new pagetable structure is being built to allow for the kernel
4038c2ecf20Sopenharmony_ci	 * and initrd to be encrypted. It starts with an empty PGD that will
4048c2ecf20Sopenharmony_ci	 * then be populated with new PUDs and PMDs as the encrypted and
4058c2ecf20Sopenharmony_ci	 * decrypted kernel mappings are created.
4068c2ecf20Sopenharmony_ci	 */
4078c2ecf20Sopenharmony_ci	ppd.pgd = ppd.pgtable_area;
4088c2ecf20Sopenharmony_ci	memset(ppd.pgd, 0, sizeof(pgd_t) * PTRS_PER_PGD);
4098c2ecf20Sopenharmony_ci	ppd.pgtable_area += sizeof(pgd_t) * PTRS_PER_PGD;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	/*
4128c2ecf20Sopenharmony_ci	 * A different PGD index/entry must be used to get different
4138c2ecf20Sopenharmony_ci	 * pagetable entries for the decrypted mapping. Choose the next
4148c2ecf20Sopenharmony_ci	 * PGD index and convert it to a virtual address to be used as
4158c2ecf20Sopenharmony_ci	 * the base of the mapping.
4168c2ecf20Sopenharmony_ci	 */
4178c2ecf20Sopenharmony_ci	decrypted_base = (pgd_index(workarea_end) + 1) & (PTRS_PER_PGD - 1);
4188c2ecf20Sopenharmony_ci	if (initrd_len) {
4198c2ecf20Sopenharmony_ci		unsigned long check_base;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci		check_base = (pgd_index(initrd_end) + 1) & (PTRS_PER_PGD - 1);
4228c2ecf20Sopenharmony_ci		decrypted_base = max(decrypted_base, check_base);
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci	decrypted_base <<= PGDIR_SHIFT;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	/* Add encrypted kernel (identity) mappings */
4278c2ecf20Sopenharmony_ci	ppd.paddr = kernel_start;
4288c2ecf20Sopenharmony_ci	ppd.vaddr = kernel_start;
4298c2ecf20Sopenharmony_ci	ppd.vaddr_end = kernel_end;
4308c2ecf20Sopenharmony_ci	sme_map_range_encrypted(&ppd);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	/* Add decrypted, write-protected kernel (non-identity) mappings */
4338c2ecf20Sopenharmony_ci	ppd.paddr = kernel_start;
4348c2ecf20Sopenharmony_ci	ppd.vaddr = kernel_start + decrypted_base;
4358c2ecf20Sopenharmony_ci	ppd.vaddr_end = kernel_end + decrypted_base;
4368c2ecf20Sopenharmony_ci	sme_map_range_decrypted_wp(&ppd);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (initrd_len) {
4398c2ecf20Sopenharmony_ci		/* Add encrypted initrd (identity) mappings */
4408c2ecf20Sopenharmony_ci		ppd.paddr = initrd_start;
4418c2ecf20Sopenharmony_ci		ppd.vaddr = initrd_start;
4428c2ecf20Sopenharmony_ci		ppd.vaddr_end = initrd_end;
4438c2ecf20Sopenharmony_ci		sme_map_range_encrypted(&ppd);
4448c2ecf20Sopenharmony_ci		/*
4458c2ecf20Sopenharmony_ci		 * Add decrypted, write-protected initrd (non-identity) mappings
4468c2ecf20Sopenharmony_ci		 */
4478c2ecf20Sopenharmony_ci		ppd.paddr = initrd_start;
4488c2ecf20Sopenharmony_ci		ppd.vaddr = initrd_start + decrypted_base;
4498c2ecf20Sopenharmony_ci		ppd.vaddr_end = initrd_end + decrypted_base;
4508c2ecf20Sopenharmony_ci		sme_map_range_decrypted_wp(&ppd);
4518c2ecf20Sopenharmony_ci	}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	/* Add decrypted workarea mappings to both kernel mappings */
4548c2ecf20Sopenharmony_ci	ppd.paddr = workarea_start;
4558c2ecf20Sopenharmony_ci	ppd.vaddr = workarea_start;
4568c2ecf20Sopenharmony_ci	ppd.vaddr_end = workarea_end;
4578c2ecf20Sopenharmony_ci	sme_map_range_decrypted(&ppd);
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	ppd.paddr = workarea_start;
4608c2ecf20Sopenharmony_ci	ppd.vaddr = workarea_start + decrypted_base;
4618c2ecf20Sopenharmony_ci	ppd.vaddr_end = workarea_end + decrypted_base;
4628c2ecf20Sopenharmony_ci	sme_map_range_decrypted(&ppd);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	/* Perform the encryption */
4658c2ecf20Sopenharmony_ci	sme_encrypt_execute(kernel_start, kernel_start + decrypted_base,
4668c2ecf20Sopenharmony_ci			    kernel_len, workarea_start, (unsigned long)ppd.pgd);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	if (initrd_len)
4698c2ecf20Sopenharmony_ci		sme_encrypt_execute(initrd_start, initrd_start + decrypted_base,
4708c2ecf20Sopenharmony_ci				    initrd_len, workarea_start,
4718c2ecf20Sopenharmony_ci				    (unsigned long)ppd.pgd);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	/*
4748c2ecf20Sopenharmony_ci	 * At this point we are running encrypted.  Remove the mappings for
4758c2ecf20Sopenharmony_ci	 * the decrypted areas - all that is needed for this is to remove
4768c2ecf20Sopenharmony_ci	 * the PGD entry/entries.
4778c2ecf20Sopenharmony_ci	 */
4788c2ecf20Sopenharmony_ci	ppd.vaddr = kernel_start + decrypted_base;
4798c2ecf20Sopenharmony_ci	ppd.vaddr_end = kernel_end + decrypted_base;
4808c2ecf20Sopenharmony_ci	sme_clear_pgd(&ppd);
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	if (initrd_len) {
4838c2ecf20Sopenharmony_ci		ppd.vaddr = initrd_start + decrypted_base;
4848c2ecf20Sopenharmony_ci		ppd.vaddr_end = initrd_end + decrypted_base;
4858c2ecf20Sopenharmony_ci		sme_clear_pgd(&ppd);
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	ppd.vaddr = workarea_start + decrypted_base;
4898c2ecf20Sopenharmony_ci	ppd.vaddr_end = workarea_end + decrypted_base;
4908c2ecf20Sopenharmony_ci	sme_clear_pgd(&ppd);
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	/* Flush the TLB - no globals so cr3 is enough */
4938c2ecf20Sopenharmony_ci	native_write_cr3(__native_read_cr3());
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_civoid __init sme_enable(struct boot_params *bp)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	const char *cmdline_ptr, *cmdline_arg, *cmdline_on, *cmdline_off;
4998c2ecf20Sopenharmony_ci	unsigned int eax, ebx, ecx, edx;
5008c2ecf20Sopenharmony_ci	unsigned long feature_mask;
5018c2ecf20Sopenharmony_ci	bool active_by_default;
5028c2ecf20Sopenharmony_ci	unsigned long me_mask;
5038c2ecf20Sopenharmony_ci	char buffer[16];
5048c2ecf20Sopenharmony_ci	u64 msr;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* Check for the SME/SEV support leaf */
5078c2ecf20Sopenharmony_ci	eax = 0x80000000;
5088c2ecf20Sopenharmony_ci	ecx = 0;
5098c2ecf20Sopenharmony_ci	native_cpuid(&eax, &ebx, &ecx, &edx);
5108c2ecf20Sopenharmony_ci	if (eax < 0x8000001f)
5118c2ecf20Sopenharmony_ci		return;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci#define AMD_SME_BIT	BIT(0)
5148c2ecf20Sopenharmony_ci#define AMD_SEV_BIT	BIT(1)
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	/*
5178c2ecf20Sopenharmony_ci	 * Check for the SME/SEV feature:
5188c2ecf20Sopenharmony_ci	 *   CPUID Fn8000_001F[EAX]
5198c2ecf20Sopenharmony_ci	 *   - Bit 0 - Secure Memory Encryption support
5208c2ecf20Sopenharmony_ci	 *   - Bit 1 - Secure Encrypted Virtualization support
5218c2ecf20Sopenharmony_ci	 *   CPUID Fn8000_001F[EBX]
5228c2ecf20Sopenharmony_ci	 *   - Bits 5:0 - Pagetable bit position used to indicate encryption
5238c2ecf20Sopenharmony_ci	 */
5248c2ecf20Sopenharmony_ci	eax = 0x8000001f;
5258c2ecf20Sopenharmony_ci	ecx = 0;
5268c2ecf20Sopenharmony_ci	native_cpuid(&eax, &ebx, &ecx, &edx);
5278c2ecf20Sopenharmony_ci	/* Check whether SEV or SME is supported */
5288c2ecf20Sopenharmony_ci	if (!(eax & (AMD_SEV_BIT | AMD_SME_BIT)))
5298c2ecf20Sopenharmony_ci		return;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	me_mask = 1UL << (ebx & 0x3f);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	/* Check the SEV MSR whether SEV or SME is enabled */
5348c2ecf20Sopenharmony_ci	sev_status   = __rdmsr(MSR_AMD64_SEV);
5358c2ecf20Sopenharmony_ci	feature_mask = (sev_status & MSR_AMD64_SEV_ENABLED) ? AMD_SEV_BIT : AMD_SME_BIT;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	/* Check if memory encryption is enabled */
5388c2ecf20Sopenharmony_ci	if (feature_mask == AMD_SME_BIT) {
5398c2ecf20Sopenharmony_ci		/*
5408c2ecf20Sopenharmony_ci		 * No SME if Hypervisor bit is set. This check is here to
5418c2ecf20Sopenharmony_ci		 * prevent a guest from trying to enable SME. For running as a
5428c2ecf20Sopenharmony_ci		 * KVM guest the MSR_K8_SYSCFG will be sufficient, but there
5438c2ecf20Sopenharmony_ci		 * might be other hypervisors which emulate that MSR as non-zero
5448c2ecf20Sopenharmony_ci		 * or even pass it through to the guest.
5458c2ecf20Sopenharmony_ci		 * A malicious hypervisor can still trick a guest into this
5468c2ecf20Sopenharmony_ci		 * path, but there is no way to protect against that.
5478c2ecf20Sopenharmony_ci		 */
5488c2ecf20Sopenharmony_ci		eax = 1;
5498c2ecf20Sopenharmony_ci		ecx = 0;
5508c2ecf20Sopenharmony_ci		native_cpuid(&eax, &ebx, &ecx, &edx);
5518c2ecf20Sopenharmony_ci		if (ecx & BIT(31))
5528c2ecf20Sopenharmony_ci			return;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci		/* For SME, check the SYSCFG MSR */
5558c2ecf20Sopenharmony_ci		msr = __rdmsr(MSR_K8_SYSCFG);
5568c2ecf20Sopenharmony_ci		if (!(msr & MSR_K8_SYSCFG_MEM_ENCRYPT))
5578c2ecf20Sopenharmony_ci			return;
5588c2ecf20Sopenharmony_ci	} else {
5598c2ecf20Sopenharmony_ci		/* SEV state cannot be controlled by a command line option */
5608c2ecf20Sopenharmony_ci		sme_me_mask = me_mask;
5618c2ecf20Sopenharmony_ci		sev_enabled = true;
5628c2ecf20Sopenharmony_ci		physical_mask &= ~sme_me_mask;
5638c2ecf20Sopenharmony_ci		return;
5648c2ecf20Sopenharmony_ci	}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	/*
5678c2ecf20Sopenharmony_ci	 * Fixups have not been applied to phys_base yet and we're running
5688c2ecf20Sopenharmony_ci	 * identity mapped, so we must obtain the address to the SME command
5698c2ecf20Sopenharmony_ci	 * line argument data using rip-relative addressing.
5708c2ecf20Sopenharmony_ci	 */
5718c2ecf20Sopenharmony_ci	asm ("lea sme_cmdline_arg(%%rip), %0"
5728c2ecf20Sopenharmony_ci	     : "=r" (cmdline_arg)
5738c2ecf20Sopenharmony_ci	     : "p" (sme_cmdline_arg));
5748c2ecf20Sopenharmony_ci	asm ("lea sme_cmdline_on(%%rip), %0"
5758c2ecf20Sopenharmony_ci	     : "=r" (cmdline_on)
5768c2ecf20Sopenharmony_ci	     : "p" (sme_cmdline_on));
5778c2ecf20Sopenharmony_ci	asm ("lea sme_cmdline_off(%%rip), %0"
5788c2ecf20Sopenharmony_ci	     : "=r" (cmdline_off)
5798c2ecf20Sopenharmony_ci	     : "p" (sme_cmdline_off));
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT_ACTIVE_BY_DEFAULT))
5828c2ecf20Sopenharmony_ci		active_by_default = true;
5838c2ecf20Sopenharmony_ci	else
5848c2ecf20Sopenharmony_ci		active_by_default = false;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	cmdline_ptr = (const char *)((u64)bp->hdr.cmd_line_ptr |
5878c2ecf20Sopenharmony_ci				     ((u64)bp->ext_cmd_line_ptr << 32));
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	if (cmdline_find_option(cmdline_ptr, cmdline_arg, buffer, sizeof(buffer)) < 0)
5908c2ecf20Sopenharmony_ci		return;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	if (!strncmp(buffer, cmdline_on, sizeof(buffer)))
5938c2ecf20Sopenharmony_ci		sme_me_mask = me_mask;
5948c2ecf20Sopenharmony_ci	else if (!strncmp(buffer, cmdline_off, sizeof(buffer)))
5958c2ecf20Sopenharmony_ci		sme_me_mask = 0;
5968c2ecf20Sopenharmony_ci	else
5978c2ecf20Sopenharmony_ci		sme_me_mask = active_by_default ? me_mask : 0;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	physical_mask &= ~sme_me_mask;
6008c2ecf20Sopenharmony_ci}
601