Skip to content

Commit e7bafbf

Browse files
committed
arm64: mm: Add top-level dispatcher for internal mem_encrypt API
Implementing the internal mem_encrypt API for arm64 depends entirely on the Confidential Computing environment in which the kernel is running. Introduce a simple dispatcher so that backend hooks can be registered depending upon the environment in which the kernel finds itself. Reviewed-by: Catalin Marinas <[email protected]> Reviewed-by: Steven Price <[email protected]> Acked-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Will Deacon <[email protected]>
1 parent a06c3fa commit e7bafbf

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

arch/arm64/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ config ARM64
3434
select ARCH_HAS_KERNEL_FPU_SUPPORT if KERNEL_MODE_NEON
3535
select ARCH_HAS_KEEPINITRD
3636
select ARCH_HAS_MEMBARRIER_SYNC_CORE
37+
select ARCH_HAS_MEM_ENCRYPT
3738
select ARCH_HAS_NMI_SAFE_THIS_CPU_OPS
3839
select ARCH_HAS_NON_OVERLAPPING_ADDRESS_SPACE
3940
select ARCH_HAS_PTE_DEVMAP

arch/arm64/include/asm/mem_encrypt.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef __ASM_MEM_ENCRYPT_H
3+
#define __ASM_MEM_ENCRYPT_H
4+
5+
struct arm64_mem_crypt_ops {
6+
int (*encrypt)(unsigned long addr, int numpages);
7+
int (*decrypt)(unsigned long addr, int numpages);
8+
};
9+
10+
int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops);
11+
12+
int set_memory_encrypted(unsigned long addr, int numpages);
13+
int set_memory_decrypted(unsigned long addr, int numpages);
14+
15+
#endif /* __ASM_MEM_ENCRYPT_H */

arch/arm64/include/asm/set_memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef _ASM_ARM64_SET_MEMORY_H
44
#define _ASM_ARM64_SET_MEMORY_H
55

6+
#include <asm/mem_encrypt.h>
67
#include <asm-generic/set_memory.h>
78

89
bool can_set_direct_map(void);

arch/arm64/mm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-y := dma-mapping.o extable.o fault.o init.o \
33
cache.o copypage.o flush.o \
4-
ioremap.o mmap.o pgd.o mmu.o \
4+
ioremap.o mmap.o pgd.o mem_encrypt.o mmu.o \
55
context.o proc.o pageattr.o fixmap.o
66
obj-$(CONFIG_ARM64_CONTPTE) += contpte.o
77
obj-$(CONFIG_HUGETLB_PAGE) += hugetlbpage.o

arch/arm64/mm/mem_encrypt.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Implementation of the memory encryption/decryption API.
4+
*
5+
* Since the low-level details of the operation depend on the
6+
* Confidential Computing environment (e.g. pKVM, CCA, ...), this just
7+
* acts as a top-level dispatcher to whatever hooks may have been
8+
* registered.
9+
*
10+
* Author: Will Deacon <[email protected]>
11+
* Copyright (C) 2024 Google LLC
12+
*
13+
* "Hello, boils and ghouls!"
14+
*/
15+
16+
#include <linux/bug.h>
17+
#include <linux/compiler.h>
18+
#include <linux/err.h>
19+
#include <linux/mm.h>
20+
21+
#include <asm/mem_encrypt.h>
22+
23+
static const struct arm64_mem_crypt_ops *crypt_ops;
24+
25+
int arm64_mem_crypt_ops_register(const struct arm64_mem_crypt_ops *ops)
26+
{
27+
if (WARN_ON(crypt_ops))
28+
return -EBUSY;
29+
30+
crypt_ops = ops;
31+
return 0;
32+
}
33+
34+
int set_memory_encrypted(unsigned long addr, int numpages)
35+
{
36+
if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
37+
return 0;
38+
39+
return crypt_ops->encrypt(addr, numpages);
40+
}
41+
EXPORT_SYMBOL_GPL(set_memory_encrypted);
42+
43+
int set_memory_decrypted(unsigned long addr, int numpages)
44+
{
45+
if (likely(!crypt_ops) || WARN_ON(!PAGE_ALIGNED(addr)))
46+
return 0;
47+
48+
return crypt_ops->decrypt(addr, numpages);
49+
}
50+
EXPORT_SYMBOL_GPL(set_memory_decrypted);

0 commit comments

Comments
 (0)