902861e34c
from hotplugged memory rather than only from main memory. Series "implement "memmap on memory" feature on s390". - More folio conversions from Matthew Wilcox in the series "Convert memcontrol charge moving to use folios" "mm: convert mm counter to take a folio" - Chengming Zhou has optimized zswap's rbtree locking, providing significant reductions in system time and modest but measurable reductions in overall runtimes. The series is "mm/zswap: optimize the scalability of zswap rb-tree". - Chengming Zhou has also provided the series "mm/zswap: optimize zswap lru list" which provides measurable runtime benefits in some swap-intensive situations. - And Chengming Zhou further optimizes zswap in the series "mm/zswap: optimize for dynamic zswap_pools". Measured improvements are modest. - zswap cleanups and simplifications from Yosry Ahmed in the series "mm: zswap: simplify zswap_swapoff()". - In the series "Add DAX ABI for memmap_on_memory", Vishal Verma has contributed several DAX cleanups as well as adding a sysfs tunable to control the memmap_on_memory setting when the dax device is hotplugged as system memory. - Johannes Weiner has added the large series "mm: zswap: cleanups", which does that. - More DAMON work from SeongJae Park in the series "mm/damon: make DAMON debugfs interface deprecation unignorable" "selftests/damon: add more tests for core functionalities and corner cases" "Docs/mm/damon: misc readability improvements" "mm/damon: let DAMOS feeds and tame/auto-tune itself" - In the series "mm/mempolicy: weighted interleave mempolicy and sysfs extension" Rakie Kim has developed a new mempolicy interleaving policy wherein we allocate memory across nodes in a weighted fashion rather than uniformly. This is beneficial in heterogeneous memory environments appearing with CXL. - Christophe Leroy has contributed some cleanup and consolidation work against the ARM pagetable dumping code in the series "mm: ptdump: Refactor CONFIG_DEBUG_WX and check_wx_pages debugfs attribute". - Luis Chamberlain has added some additional xarray selftesting in the series "test_xarray: advanced API multi-index tests". - Muhammad Usama Anjum has reworked the selftest code to make its human-readable output conform to the TAP ("Test Anything Protocol") format. Amongst other things, this opens up the use of third-party tools to parse and process out selftesting results. - Ryan Roberts has added fork()-time PTE batching of THP ptes in the series "mm/memory: optimize fork() with PTE-mapped THP". Mainly targeted at arm64, this significantly speeds up fork() when the process has a large number of pte-mapped folios. - David Hildenbrand also gets in on the THP pte batching game in his series "mm/memory: optimize unmap/zap with PTE-mapped THP". It implements batching during munmap() and other pte teardown situations. The microbenchmark improvements are nice. - And in the series "Transparent Contiguous PTEs for User Mappings" Ryan Roberts further utilizes arm's pte's contiguous bit ("contpte mappings"). Kernel build times on arm64 improved nicely. Ryan's series "Address some contpte nits" provides some followup work. - In the series "mm/hugetlb: Restore the reservation" Breno Leitao has fixed an obscure hugetlb race which was causing unnecessary page faults. He has also added a reproducer under the selftest code. - In the series "selftests/mm: Output cleanups for the compaction test", Mark Brown did what the title claims. - Kinsey Ho has added the series "mm/mglru: code cleanup and refactoring". - Even more zswap material from Nhat Pham. The series "fix and extend zswap kselftests" does as claimed. - In the series "Introduce cpu_dcache_is_aliasing() to fix DAX regression" Mathieu Desnoyers has cleaned up and fixed rather a mess in our handling of DAX on archiecctures which have virtually aliasing data caches. The arm architecture is the main beneficiary. - Lokesh Gidra's series "per-vma locks in userfaultfd" provides dramatic improvements in worst-case mmap_lock hold times during certain userfaultfd operations. - Some page_owner enhancements and maintenance work from Oscar Salvador in his series "page_owner: print stacks and their outstanding allocations" "page_owner: Fixup and cleanup" - Uladzislau Rezki has contributed some vmalloc scalability improvements in his series "Mitigate a vmap lock contention". It realizes a 12x improvement for a certain microbenchmark. - Some kexec/crash cleanup work from Baoquan He in the series "Split crash out from kexec and clean up related config items". - Some zsmalloc maintenance work from Chengming Zhou in the series "mm/zsmalloc: fix and optimize objects/page migration" "mm/zsmalloc: some cleanup for get/set_zspage_mapping()" - Zi Yan has taught the MM to perform compaction on folios larger than order=0. This a step along the path to implementaton of the merging of large anonymous folios. The series is named "Enable >0 order folio memory compaction". - Christoph Hellwig has done quite a lot of cleanup work in the pagecache writeback code in his series "convert write_cache_pages() to an iterator". - Some modest hugetlb cleanups and speedups in Vishal Moola's series "Handle hugetlb faults under the VMA lock". - Zi Yan has changed the page splitting code so we can split huge pages into sizes other than order-0 to better utilize large folios. The series is named "Split a folio to any lower order folios". - David Hildenbrand has contributed the series "mm: remove total_mapcount()", a cleanup. - Matthew Wilcox has sought to improve the performance of bulk memory freeing in his series "Rearrange batched folio freeing". - Gang Li's series "hugetlb: parallelize hugetlb page init on boot" provides large improvements in bootup times on large machines which are configured to use large numbers of hugetlb pages. - Matthew Wilcox's series "PageFlags cleanups" does that. - Qi Zheng's series "minor fixes and supplement for ptdesc" does that also. S390 is affected. - Cleanups to our pagemap utility functions from Peter Xu in his series "mm/treewide: Replace pXd_large() with pXd_leaf()". - Nico Pache has fixed a few things with our hugepage selftests in his series "selftests/mm: Improve Hugepage Test Handling in MM Selftests". - Also, of course, many singleton patches to many things. Please see the individual changelogs for details. -----BEGIN PGP SIGNATURE----- iHUEABYIAB0WIQTTMBEPP41GrTpTJgfdBJ7gKXxAjgUCZfJpPQAKCRDdBJ7gKXxA joxeAP9TrcMEuHnLmBlhIXkWbIR4+ki+pA3v+gNTlJiBhnfVSgD9G55t1aBaRplx TMNhHfyiHYDTx/GAV9NXW84tasJSDgA= =TG55 -----END PGP SIGNATURE----- Merge tag 'mm-stable-2024-03-13-20-04' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Pull MM updates from Andrew Morton: - Sumanth Korikkar has taught s390 to allocate hotplug-time page frames from hotplugged memory rather than only from main memory. Series "implement "memmap on memory" feature on s390". - More folio conversions from Matthew Wilcox in the series "Convert memcontrol charge moving to use folios" "mm: convert mm counter to take a folio" - Chengming Zhou has optimized zswap's rbtree locking, providing significant reductions in system time and modest but measurable reductions in overall runtimes. The series is "mm/zswap: optimize the scalability of zswap rb-tree". - Chengming Zhou has also provided the series "mm/zswap: optimize zswap lru list" which provides measurable runtime benefits in some swap-intensive situations. - And Chengming Zhou further optimizes zswap in the series "mm/zswap: optimize for dynamic zswap_pools". Measured improvements are modest. - zswap cleanups and simplifications from Yosry Ahmed in the series "mm: zswap: simplify zswap_swapoff()". - In the series "Add DAX ABI for memmap_on_memory", Vishal Verma has contributed several DAX cleanups as well as adding a sysfs tunable to control the memmap_on_memory setting when the dax device is hotplugged as system memory. - Johannes Weiner has added the large series "mm: zswap: cleanups", which does that. - More DAMON work from SeongJae Park in the series "mm/damon: make DAMON debugfs interface deprecation unignorable" "selftests/damon: add more tests for core functionalities and corner cases" "Docs/mm/damon: misc readability improvements" "mm/damon: let DAMOS feeds and tame/auto-tune itself" - In the series "mm/mempolicy: weighted interleave mempolicy and sysfs extension" Rakie Kim has developed a new mempolicy interleaving policy wherein we allocate memory across nodes in a weighted fashion rather than uniformly. This is beneficial in heterogeneous memory environments appearing with CXL. - Christophe Leroy has contributed some cleanup and consolidation work against the ARM pagetable dumping code in the series "mm: ptdump: Refactor CONFIG_DEBUG_WX and check_wx_pages debugfs attribute". - Luis Chamberlain has added some additional xarray selftesting in the series "test_xarray: advanced API multi-index tests". - Muhammad Usama Anjum has reworked the selftest code to make its human-readable output conform to the TAP ("Test Anything Protocol") format. Amongst other things, this opens up the use of third-party tools to parse and process out selftesting results. - Ryan Roberts has added fork()-time PTE batching of THP ptes in the series "mm/memory: optimize fork() with PTE-mapped THP". Mainly targeted at arm64, this significantly speeds up fork() when the process has a large number of pte-mapped folios. - David Hildenbrand also gets in on the THP pte batching game in his series "mm/memory: optimize unmap/zap with PTE-mapped THP". It implements batching during munmap() and other pte teardown situations. The microbenchmark improvements are nice. - And in the series "Transparent Contiguous PTEs for User Mappings" Ryan Roberts further utilizes arm's pte's contiguous bit ("contpte mappings"). Kernel build times on arm64 improved nicely. Ryan's series "Address some contpte nits" provides some followup work. - In the series "mm/hugetlb: Restore the reservation" Breno Leitao has fixed an obscure hugetlb race which was causing unnecessary page faults. He has also added a reproducer under the selftest code. - In the series "selftests/mm: Output cleanups for the compaction test", Mark Brown did what the title claims. - Kinsey Ho has added the series "mm/mglru: code cleanup and refactoring". - Even more zswap material from Nhat Pham. The series "fix and extend zswap kselftests" does as claimed. - In the series "Introduce cpu_dcache_is_aliasing() to fix DAX regression" Mathieu Desnoyers has cleaned up and fixed rather a mess in our handling of DAX on archiecctures which have virtually aliasing data caches. The arm architecture is the main beneficiary. - Lokesh Gidra's series "per-vma locks in userfaultfd" provides dramatic improvements in worst-case mmap_lock hold times during certain userfaultfd operations. - Some page_owner enhancements and maintenance work from Oscar Salvador in his series "page_owner: print stacks and their outstanding allocations" "page_owner: Fixup and cleanup" - Uladzislau Rezki has contributed some vmalloc scalability improvements in his series "Mitigate a vmap lock contention". It realizes a 12x improvement for a certain microbenchmark. - Some kexec/crash cleanup work from Baoquan He in the series "Split crash out from kexec and clean up related config items". - Some zsmalloc maintenance work from Chengming Zhou in the series "mm/zsmalloc: fix and optimize objects/page migration" "mm/zsmalloc: some cleanup for get/set_zspage_mapping()" - Zi Yan has taught the MM to perform compaction on folios larger than order=0. This a step along the path to implementaton of the merging of large anonymous folios. The series is named "Enable >0 order folio memory compaction". - Christoph Hellwig has done quite a lot of cleanup work in the pagecache writeback code in his series "convert write_cache_pages() to an iterator". - Some modest hugetlb cleanups and speedups in Vishal Moola's series "Handle hugetlb faults under the VMA lock". - Zi Yan has changed the page splitting code so we can split huge pages into sizes other than order-0 to better utilize large folios. The series is named "Split a folio to any lower order folios". - David Hildenbrand has contributed the series "mm: remove total_mapcount()", a cleanup. - Matthew Wilcox has sought to improve the performance of bulk memory freeing in his series "Rearrange batched folio freeing". - Gang Li's series "hugetlb: parallelize hugetlb page init on boot" provides large improvements in bootup times on large machines which are configured to use large numbers of hugetlb pages. - Matthew Wilcox's series "PageFlags cleanups" does that. - Qi Zheng's series "minor fixes and supplement for ptdesc" does that also. S390 is affected. - Cleanups to our pagemap utility functions from Peter Xu in his series "mm/treewide: Replace pXd_large() with pXd_leaf()". - Nico Pache has fixed a few things with our hugepage selftests in his series "selftests/mm: Improve Hugepage Test Handling in MM Selftests". - Also, of course, many singleton patches to many things. Please see the individual changelogs for details. * tag 'mm-stable-2024-03-13-20-04' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (435 commits) mm/zswap: remove the memcpy if acomp is not sleepable crypto: introduce: acomp_is_async to expose if comp drivers might sleep memtest: use {READ,WRITE}_ONCE in memory scanning mm: prohibit the last subpage from reusing the entire large folio mm: recover pud_leaf() definitions in nopmd case selftests/mm: skip the hugetlb-madvise tests on unmet hugepage requirements selftests/mm: skip uffd hugetlb tests with insufficient hugepages selftests/mm: dont fail testsuite due to a lack of hugepages mm/huge_memory: skip invalid debugfs new_order input for folio split mm/huge_memory: check new folio order when split a folio mm, vmscan: retry kswapd's priority loop with cache_trim_mode off on failure mm: add an explicit smp_wmb() to UFFDIO_CONTINUE mm: fix list corruption in put_pages_list mm: remove folio from deferred split list before uncharging it filemap: avoid unnecessary major faults in filemap_fault() mm,page_owner: drop unnecessary check mm,page_owner: check for null stack_record before bumping its refcount mm: swap: fix race between free_swap_and_cache() and swapoff() mm/treewide: align up pXd_leaf() retval across archs mm/treewide: drop pXd_large() ...
569 lines
17 KiB
C
569 lines
17 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* AMD Memory Encryption Support
|
|
*
|
|
* Copyright (C) 2016 Advanced Micro Devices, Inc.
|
|
*
|
|
* Author: Tom Lendacky <thomas.lendacky@amd.com>
|
|
*/
|
|
|
|
#define DISABLE_BRANCH_PROFILING
|
|
|
|
/*
|
|
* Since we're dealing with identity mappings, physical and virtual
|
|
* addresses are the same, so override these defines which are ultimately
|
|
* used by the headers in misc.h.
|
|
*/
|
|
#define __pa(x) ((unsigned long)(x))
|
|
#define __va(x) ((void *)((unsigned long)(x)))
|
|
|
|
/*
|
|
* Special hack: we have to be careful, because no indirections are
|
|
* allowed here, and paravirt_ops is a kind of one. As it will only run in
|
|
* baremetal anyway, we just keep it from happening. (This list needs to
|
|
* be extended when new paravirt and debugging variants are added.)
|
|
*/
|
|
#undef CONFIG_PARAVIRT
|
|
#undef CONFIG_PARAVIRT_XXL
|
|
#undef CONFIG_PARAVIRT_SPINLOCKS
|
|
|
|
/*
|
|
* This code runs before CPU feature bits are set. By default, the
|
|
* pgtable_l5_enabled() function uses bit X86_FEATURE_LA57 to determine if
|
|
* 5-level paging is active, so that won't work here. USE_EARLY_PGTABLE_L5
|
|
* is provided to handle this situation and, instead, use a variable that
|
|
* has been set by the early boot code.
|
|
*/
|
|
#define USE_EARLY_PGTABLE_L5
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/mem_encrypt.h>
|
|
#include <linux/cc_platform.h>
|
|
|
|
#include <asm/init.h>
|
|
#include <asm/setup.h>
|
|
#include <asm/sections.h>
|
|
#include <asm/coco.h>
|
|
#include <asm/sev.h>
|
|
|
|
#include "mm_internal.h"
|
|
|
|
#define PGD_FLAGS _KERNPG_TABLE_NOENC
|
|
#define P4D_FLAGS _KERNPG_TABLE_NOENC
|
|
#define PUD_FLAGS _KERNPG_TABLE_NOENC
|
|
#define PMD_FLAGS _KERNPG_TABLE_NOENC
|
|
|
|
#define PMD_FLAGS_LARGE (__PAGE_KERNEL_LARGE_EXEC & ~_PAGE_GLOBAL)
|
|
|
|
#define PMD_FLAGS_DEC PMD_FLAGS_LARGE
|
|
#define PMD_FLAGS_DEC_WP ((PMD_FLAGS_DEC & ~_PAGE_LARGE_CACHE_MASK) | \
|
|
(_PAGE_PAT_LARGE | _PAGE_PWT))
|
|
|
|
#define PMD_FLAGS_ENC (PMD_FLAGS_LARGE | _PAGE_ENC)
|
|
|
|
#define PTE_FLAGS (__PAGE_KERNEL_EXEC & ~_PAGE_GLOBAL)
|
|
|
|
#define PTE_FLAGS_DEC PTE_FLAGS
|
|
#define PTE_FLAGS_DEC_WP ((PTE_FLAGS_DEC & ~_PAGE_CACHE_MASK) | \
|
|
(_PAGE_PAT | _PAGE_PWT))
|
|
|
|
#define PTE_FLAGS_ENC (PTE_FLAGS | _PAGE_ENC)
|
|
|
|
struct sme_populate_pgd_data {
|
|
void *pgtable_area;
|
|
pgd_t *pgd;
|
|
|
|
pmdval_t pmd_flags;
|
|
pteval_t pte_flags;
|
|
unsigned long paddr;
|
|
|
|
unsigned long vaddr;
|
|
unsigned long vaddr_end;
|
|
};
|
|
|
|
/*
|
|
* This work area lives in the .init.scratch section, which lives outside of
|
|
* the kernel proper. It is sized to hold the intermediate copy buffer and
|
|
* more than enough pagetable pages.
|
|
*
|
|
* By using this section, the kernel can be encrypted in place and it
|
|
* avoids any possibility of boot parameters or initramfs images being
|
|
* placed such that the in-place encryption logic overwrites them. This
|
|
* section is 2MB aligned to allow for simple pagetable setup using only
|
|
* PMD entries (see vmlinux.lds.S).
|
|
*/
|
|
static char sme_workarea[2 * PMD_SIZE] __section(".init.scratch");
|
|
|
|
static void __head sme_clear_pgd(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
unsigned long pgd_start, pgd_end, pgd_size;
|
|
pgd_t *pgd_p;
|
|
|
|
pgd_start = ppd->vaddr & PGDIR_MASK;
|
|
pgd_end = ppd->vaddr_end & PGDIR_MASK;
|
|
|
|
pgd_size = (((pgd_end - pgd_start) / PGDIR_SIZE) + 1) * sizeof(pgd_t);
|
|
|
|
pgd_p = ppd->pgd + pgd_index(ppd->vaddr);
|
|
|
|
memset(pgd_p, 0, pgd_size);
|
|
}
|
|
|
|
static pud_t __head *sme_prepare_pgd(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
pgd_t *pgd;
|
|
p4d_t *p4d;
|
|
pud_t *pud;
|
|
pmd_t *pmd;
|
|
|
|
pgd = ppd->pgd + pgd_index(ppd->vaddr);
|
|
if (pgd_none(*pgd)) {
|
|
p4d = ppd->pgtable_area;
|
|
memset(p4d, 0, sizeof(*p4d) * PTRS_PER_P4D);
|
|
ppd->pgtable_area += sizeof(*p4d) * PTRS_PER_P4D;
|
|
set_pgd(pgd, __pgd(PGD_FLAGS | __pa(p4d)));
|
|
}
|
|
|
|
p4d = p4d_offset(pgd, ppd->vaddr);
|
|
if (p4d_none(*p4d)) {
|
|
pud = ppd->pgtable_area;
|
|
memset(pud, 0, sizeof(*pud) * PTRS_PER_PUD);
|
|
ppd->pgtable_area += sizeof(*pud) * PTRS_PER_PUD;
|
|
set_p4d(p4d, __p4d(P4D_FLAGS | __pa(pud)));
|
|
}
|
|
|
|
pud = pud_offset(p4d, ppd->vaddr);
|
|
if (pud_none(*pud)) {
|
|
pmd = ppd->pgtable_area;
|
|
memset(pmd, 0, sizeof(*pmd) * PTRS_PER_PMD);
|
|
ppd->pgtable_area += sizeof(*pmd) * PTRS_PER_PMD;
|
|
set_pud(pud, __pud(PUD_FLAGS | __pa(pmd)));
|
|
}
|
|
|
|
if (pud_leaf(*pud))
|
|
return NULL;
|
|
|
|
return pud;
|
|
}
|
|
|
|
static void __head sme_populate_pgd_large(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
pud_t *pud;
|
|
pmd_t *pmd;
|
|
|
|
pud = sme_prepare_pgd(ppd);
|
|
if (!pud)
|
|
return;
|
|
|
|
pmd = pmd_offset(pud, ppd->vaddr);
|
|
if (pmd_leaf(*pmd))
|
|
return;
|
|
|
|
set_pmd(pmd, __pmd(ppd->paddr | ppd->pmd_flags));
|
|
}
|
|
|
|
static void __head sme_populate_pgd(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
pud_t *pud;
|
|
pmd_t *pmd;
|
|
pte_t *pte;
|
|
|
|
pud = sme_prepare_pgd(ppd);
|
|
if (!pud)
|
|
return;
|
|
|
|
pmd = pmd_offset(pud, ppd->vaddr);
|
|
if (pmd_none(*pmd)) {
|
|
pte = ppd->pgtable_area;
|
|
memset(pte, 0, sizeof(*pte) * PTRS_PER_PTE);
|
|
ppd->pgtable_area += sizeof(*pte) * PTRS_PER_PTE;
|
|
set_pmd(pmd, __pmd(PMD_FLAGS | __pa(pte)));
|
|
}
|
|
|
|
if (pmd_leaf(*pmd))
|
|
return;
|
|
|
|
pte = pte_offset_kernel(pmd, ppd->vaddr);
|
|
if (pte_none(*pte))
|
|
set_pte(pte, __pte(ppd->paddr | ppd->pte_flags));
|
|
}
|
|
|
|
static void __head __sme_map_range_pmd(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
while (ppd->vaddr < ppd->vaddr_end) {
|
|
sme_populate_pgd_large(ppd);
|
|
|
|
ppd->vaddr += PMD_SIZE;
|
|
ppd->paddr += PMD_SIZE;
|
|
}
|
|
}
|
|
|
|
static void __head __sme_map_range_pte(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
while (ppd->vaddr < ppd->vaddr_end) {
|
|
sme_populate_pgd(ppd);
|
|
|
|
ppd->vaddr += PAGE_SIZE;
|
|
ppd->paddr += PAGE_SIZE;
|
|
}
|
|
}
|
|
|
|
static void __head __sme_map_range(struct sme_populate_pgd_data *ppd,
|
|
pmdval_t pmd_flags, pteval_t pte_flags)
|
|
{
|
|
unsigned long vaddr_end;
|
|
|
|
ppd->pmd_flags = pmd_flags;
|
|
ppd->pte_flags = pte_flags;
|
|
|
|
/* Save original end value since we modify the struct value */
|
|
vaddr_end = ppd->vaddr_end;
|
|
|
|
/* If start is not 2MB aligned, create PTE entries */
|
|
ppd->vaddr_end = ALIGN(ppd->vaddr, PMD_SIZE);
|
|
__sme_map_range_pte(ppd);
|
|
|
|
/* Create PMD entries */
|
|
ppd->vaddr_end = vaddr_end & PMD_MASK;
|
|
__sme_map_range_pmd(ppd);
|
|
|
|
/* If end is not 2MB aligned, create PTE entries */
|
|
ppd->vaddr_end = vaddr_end;
|
|
__sme_map_range_pte(ppd);
|
|
}
|
|
|
|
static void __head sme_map_range_encrypted(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
__sme_map_range(ppd, PMD_FLAGS_ENC, PTE_FLAGS_ENC);
|
|
}
|
|
|
|
static void __head sme_map_range_decrypted(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
__sme_map_range(ppd, PMD_FLAGS_DEC, PTE_FLAGS_DEC);
|
|
}
|
|
|
|
static void __head sme_map_range_decrypted_wp(struct sme_populate_pgd_data *ppd)
|
|
{
|
|
__sme_map_range(ppd, PMD_FLAGS_DEC_WP, PTE_FLAGS_DEC_WP);
|
|
}
|
|
|
|
static unsigned long __head sme_pgtable_calc(unsigned long len)
|
|
{
|
|
unsigned long entries = 0, tables = 0;
|
|
|
|
/*
|
|
* Perform a relatively simplistic calculation of the pagetable
|
|
* entries that are needed. Those mappings will be covered mostly
|
|
* by 2MB PMD entries so we can conservatively calculate the required
|
|
* number of P4D, PUD and PMD structures needed to perform the
|
|
* mappings. For mappings that are not 2MB aligned, PTE mappings
|
|
* would be needed for the start and end portion of the address range
|
|
* that fall outside of the 2MB alignment. This results in, at most,
|
|
* two extra pages to hold PTE entries for each range that is mapped.
|
|
* Incrementing the count for each covers the case where the addresses
|
|
* cross entries.
|
|
*/
|
|
|
|
/* PGDIR_SIZE is equal to P4D_SIZE on 4-level machine. */
|
|
if (PTRS_PER_P4D > 1)
|
|
entries += (DIV_ROUND_UP(len, PGDIR_SIZE) + 1) * sizeof(p4d_t) * PTRS_PER_P4D;
|
|
entries += (DIV_ROUND_UP(len, P4D_SIZE) + 1) * sizeof(pud_t) * PTRS_PER_PUD;
|
|
entries += (DIV_ROUND_UP(len, PUD_SIZE) + 1) * sizeof(pmd_t) * PTRS_PER_PMD;
|
|
entries += 2 * sizeof(pte_t) * PTRS_PER_PTE;
|
|
|
|
/*
|
|
* Now calculate the added pagetable structures needed to populate
|
|
* the new pagetables.
|
|
*/
|
|
|
|
if (PTRS_PER_P4D > 1)
|
|
tables += DIV_ROUND_UP(entries, PGDIR_SIZE) * sizeof(p4d_t) * PTRS_PER_P4D;
|
|
tables += DIV_ROUND_UP(entries, P4D_SIZE) * sizeof(pud_t) * PTRS_PER_PUD;
|
|
tables += DIV_ROUND_UP(entries, PUD_SIZE) * sizeof(pmd_t) * PTRS_PER_PMD;
|
|
|
|
return entries + tables;
|
|
}
|
|
|
|
void __head sme_encrypt_kernel(struct boot_params *bp)
|
|
{
|
|
unsigned long workarea_start, workarea_end, workarea_len;
|
|
unsigned long execute_start, execute_end, execute_len;
|
|
unsigned long kernel_start, kernel_end, kernel_len;
|
|
unsigned long initrd_start, initrd_end, initrd_len;
|
|
struct sme_populate_pgd_data ppd;
|
|
unsigned long pgtable_area_len;
|
|
unsigned long decrypted_base;
|
|
|
|
/*
|
|
* This is early code, use an open coded check for SME instead of
|
|
* using cc_platform_has(). This eliminates worries about removing
|
|
* instrumentation or checking boot_cpu_data in the cc_platform_has()
|
|
* function.
|
|
*/
|
|
if (!sme_get_me_mask() ||
|
|
RIP_REL_REF(sev_status) & MSR_AMD64_SEV_ENABLED)
|
|
return;
|
|
|
|
/*
|
|
* Prepare for encrypting the kernel and initrd by building new
|
|
* pagetables with the necessary attributes needed to encrypt the
|
|
* kernel in place.
|
|
*
|
|
* One range of virtual addresses will map the memory occupied
|
|
* by the kernel and initrd as encrypted.
|
|
*
|
|
* Another range of virtual addresses will map the memory occupied
|
|
* by the kernel and initrd as decrypted and write-protected.
|
|
*
|
|
* The use of write-protect attribute will prevent any of the
|
|
* memory from being cached.
|
|
*/
|
|
|
|
kernel_start = (unsigned long)RIP_REL_REF(_text);
|
|
kernel_end = ALIGN((unsigned long)RIP_REL_REF(_end), PMD_SIZE);
|
|
kernel_len = kernel_end - kernel_start;
|
|
|
|
initrd_start = 0;
|
|
initrd_end = 0;
|
|
initrd_len = 0;
|
|
#ifdef CONFIG_BLK_DEV_INITRD
|
|
initrd_len = (unsigned long)bp->hdr.ramdisk_size |
|
|
((unsigned long)bp->ext_ramdisk_size << 32);
|
|
if (initrd_len) {
|
|
initrd_start = (unsigned long)bp->hdr.ramdisk_image |
|
|
((unsigned long)bp->ext_ramdisk_image << 32);
|
|
initrd_end = PAGE_ALIGN(initrd_start + initrd_len);
|
|
initrd_len = initrd_end - initrd_start;
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
* Calculate required number of workarea bytes needed:
|
|
* executable encryption area size:
|
|
* stack page (PAGE_SIZE)
|
|
* encryption routine page (PAGE_SIZE)
|
|
* intermediate copy buffer (PMD_SIZE)
|
|
* pagetable structures for the encryption of the kernel
|
|
* pagetable structures for workarea (in case not currently mapped)
|
|
*/
|
|
execute_start = workarea_start = (unsigned long)RIP_REL_REF(sme_workarea);
|
|
execute_end = execute_start + (PAGE_SIZE * 2) + PMD_SIZE;
|
|
execute_len = execute_end - execute_start;
|
|
|
|
/*
|
|
* One PGD for both encrypted and decrypted mappings and a set of
|
|
* PUDs and PMDs for each of the encrypted and decrypted mappings.
|
|
*/
|
|
pgtable_area_len = sizeof(pgd_t) * PTRS_PER_PGD;
|
|
pgtable_area_len += sme_pgtable_calc(execute_end - kernel_start) * 2;
|
|
if (initrd_len)
|
|
pgtable_area_len += sme_pgtable_calc(initrd_len) * 2;
|
|
|
|
/* PUDs and PMDs needed in the current pagetables for the workarea */
|
|
pgtable_area_len += sme_pgtable_calc(execute_len + pgtable_area_len);
|
|
|
|
/*
|
|
* The total workarea includes the executable encryption area and
|
|
* the pagetable area. The start of the workarea is already 2MB
|
|
* aligned, align the end of the workarea on a 2MB boundary so that
|
|
* we don't try to create/allocate PTE entries from the workarea
|
|
* before it is mapped.
|
|
*/
|
|
workarea_len = execute_len + pgtable_area_len;
|
|
workarea_end = ALIGN(workarea_start + workarea_len, PMD_SIZE);
|
|
|
|
/*
|
|
* Set the address to the start of where newly created pagetable
|
|
* structures (PGDs, PUDs and PMDs) will be allocated. New pagetable
|
|
* structures are created when the workarea is added to the current
|
|
* pagetables and when the new encrypted and decrypted kernel
|
|
* mappings are populated.
|
|
*/
|
|
ppd.pgtable_area = (void *)execute_end;
|
|
|
|
/*
|
|
* Make sure the current pagetable structure has entries for
|
|
* addressing the workarea.
|
|
*/
|
|
ppd.pgd = (pgd_t *)native_read_cr3_pa();
|
|
ppd.paddr = workarea_start;
|
|
ppd.vaddr = workarea_start;
|
|
ppd.vaddr_end = workarea_end;
|
|
sme_map_range_decrypted(&ppd);
|
|
|
|
/* Flush the TLB - no globals so cr3 is enough */
|
|
native_write_cr3(__native_read_cr3());
|
|
|
|
/*
|
|
* A new pagetable structure is being built to allow for the kernel
|
|
* and initrd to be encrypted. It starts with an empty PGD that will
|
|
* then be populated with new PUDs and PMDs as the encrypted and
|
|
* decrypted kernel mappings are created.
|
|
*/
|
|
ppd.pgd = ppd.pgtable_area;
|
|
memset(ppd.pgd, 0, sizeof(pgd_t) * PTRS_PER_PGD);
|
|
ppd.pgtable_area += sizeof(pgd_t) * PTRS_PER_PGD;
|
|
|
|
/*
|
|
* A different PGD index/entry must be used to get different
|
|
* pagetable entries for the decrypted mapping. Choose the next
|
|
* PGD index and convert it to a virtual address to be used as
|
|
* the base of the mapping.
|
|
*/
|
|
decrypted_base = (pgd_index(workarea_end) + 1) & (PTRS_PER_PGD - 1);
|
|
if (initrd_len) {
|
|
unsigned long check_base;
|
|
|
|
check_base = (pgd_index(initrd_end) + 1) & (PTRS_PER_PGD - 1);
|
|
decrypted_base = max(decrypted_base, check_base);
|
|
}
|
|
decrypted_base <<= PGDIR_SHIFT;
|
|
|
|
/* Add encrypted kernel (identity) mappings */
|
|
ppd.paddr = kernel_start;
|
|
ppd.vaddr = kernel_start;
|
|
ppd.vaddr_end = kernel_end;
|
|
sme_map_range_encrypted(&ppd);
|
|
|
|
/* Add decrypted, write-protected kernel (non-identity) mappings */
|
|
ppd.paddr = kernel_start;
|
|
ppd.vaddr = kernel_start + decrypted_base;
|
|
ppd.vaddr_end = kernel_end + decrypted_base;
|
|
sme_map_range_decrypted_wp(&ppd);
|
|
|
|
if (initrd_len) {
|
|
/* Add encrypted initrd (identity) mappings */
|
|
ppd.paddr = initrd_start;
|
|
ppd.vaddr = initrd_start;
|
|
ppd.vaddr_end = initrd_end;
|
|
sme_map_range_encrypted(&ppd);
|
|
/*
|
|
* Add decrypted, write-protected initrd (non-identity) mappings
|
|
*/
|
|
ppd.paddr = initrd_start;
|
|
ppd.vaddr = initrd_start + decrypted_base;
|
|
ppd.vaddr_end = initrd_end + decrypted_base;
|
|
sme_map_range_decrypted_wp(&ppd);
|
|
}
|
|
|
|
/* Add decrypted workarea mappings to both kernel mappings */
|
|
ppd.paddr = workarea_start;
|
|
ppd.vaddr = workarea_start;
|
|
ppd.vaddr_end = workarea_end;
|
|
sme_map_range_decrypted(&ppd);
|
|
|
|
ppd.paddr = workarea_start;
|
|
ppd.vaddr = workarea_start + decrypted_base;
|
|
ppd.vaddr_end = workarea_end + decrypted_base;
|
|
sme_map_range_decrypted(&ppd);
|
|
|
|
/* Perform the encryption */
|
|
sme_encrypt_execute(kernel_start, kernel_start + decrypted_base,
|
|
kernel_len, workarea_start, (unsigned long)ppd.pgd);
|
|
|
|
if (initrd_len)
|
|
sme_encrypt_execute(initrd_start, initrd_start + decrypted_base,
|
|
initrd_len, workarea_start,
|
|
(unsigned long)ppd.pgd);
|
|
|
|
/*
|
|
* At this point we are running encrypted. Remove the mappings for
|
|
* the decrypted areas - all that is needed for this is to remove
|
|
* the PGD entry/entries.
|
|
*/
|
|
ppd.vaddr = kernel_start + decrypted_base;
|
|
ppd.vaddr_end = kernel_end + decrypted_base;
|
|
sme_clear_pgd(&ppd);
|
|
|
|
if (initrd_len) {
|
|
ppd.vaddr = initrd_start + decrypted_base;
|
|
ppd.vaddr_end = initrd_end + decrypted_base;
|
|
sme_clear_pgd(&ppd);
|
|
}
|
|
|
|
ppd.vaddr = workarea_start + decrypted_base;
|
|
ppd.vaddr_end = workarea_end + decrypted_base;
|
|
sme_clear_pgd(&ppd);
|
|
|
|
/* Flush the TLB - no globals so cr3 is enough */
|
|
native_write_cr3(__native_read_cr3());
|
|
}
|
|
|
|
void __head sme_enable(struct boot_params *bp)
|
|
{
|
|
unsigned int eax, ebx, ecx, edx;
|
|
unsigned long feature_mask;
|
|
unsigned long me_mask;
|
|
bool snp;
|
|
u64 msr;
|
|
|
|
snp = snp_init(bp);
|
|
|
|
/* Check for the SME/SEV support leaf */
|
|
eax = 0x80000000;
|
|
ecx = 0;
|
|
native_cpuid(&eax, &ebx, &ecx, &edx);
|
|
if (eax < 0x8000001f)
|
|
return;
|
|
|
|
#define AMD_SME_BIT BIT(0)
|
|
#define AMD_SEV_BIT BIT(1)
|
|
|
|
/*
|
|
* Check for the SME/SEV feature:
|
|
* CPUID Fn8000_001F[EAX]
|
|
* - Bit 0 - Secure Memory Encryption support
|
|
* - Bit 1 - Secure Encrypted Virtualization support
|
|
* CPUID Fn8000_001F[EBX]
|
|
* - Bits 5:0 - Pagetable bit position used to indicate encryption
|
|
*/
|
|
eax = 0x8000001f;
|
|
ecx = 0;
|
|
native_cpuid(&eax, &ebx, &ecx, &edx);
|
|
/* Check whether SEV or SME is supported */
|
|
if (!(eax & (AMD_SEV_BIT | AMD_SME_BIT)))
|
|
return;
|
|
|
|
me_mask = 1UL << (ebx & 0x3f);
|
|
|
|
/* Check the SEV MSR whether SEV or SME is enabled */
|
|
RIP_REL_REF(sev_status) = msr = __rdmsr(MSR_AMD64_SEV);
|
|
feature_mask = (msr & MSR_AMD64_SEV_ENABLED) ? AMD_SEV_BIT : AMD_SME_BIT;
|
|
|
|
/* The SEV-SNP CC blob should never be present unless SEV-SNP is enabled. */
|
|
if (snp && !(msr & MSR_AMD64_SEV_SNP_ENABLED))
|
|
snp_abort();
|
|
|
|
/* Check if memory encryption is enabled */
|
|
if (feature_mask == AMD_SME_BIT) {
|
|
if (!(bp->hdr.xloadflags & XLF_MEM_ENCRYPTION))
|
|
return;
|
|
|
|
/*
|
|
* No SME if Hypervisor bit is set. This check is here to
|
|
* prevent a guest from trying to enable SME. For running as a
|
|
* KVM guest the MSR_AMD64_SYSCFG will be sufficient, but there
|
|
* might be other hypervisors which emulate that MSR as non-zero
|
|
* or even pass it through to the guest.
|
|
* A malicious hypervisor can still trick a guest into this
|
|
* path, but there is no way to protect against that.
|
|
*/
|
|
eax = 1;
|
|
ecx = 0;
|
|
native_cpuid(&eax, &ebx, &ecx, &edx);
|
|
if (ecx & BIT(31))
|
|
return;
|
|
|
|
/* For SME, check the SYSCFG MSR */
|
|
msr = __rdmsr(MSR_AMD64_SYSCFG);
|
|
if (!(msr & MSR_AMD64_SYSCFG_MEM_ENCRYPT))
|
|
return;
|
|
}
|
|
|
|
RIP_REL_REF(sme_me_mask) = me_mask;
|
|
physical_mask &= ~me_mask;
|
|
cc_vendor = CC_VENDOR_AMD;
|
|
cc_set_mask(me_mask);
|
|
}
|