162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2016 ARM Ltd.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci#ifndef __ASM_PGTABLE_PROT_H
662306a36Sopenharmony_ci#define __ASM_PGTABLE_PROT_H
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <asm/memory.h>
962306a36Sopenharmony_ci#include <asm/pgtable-hwdef.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/const.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/*
1462306a36Sopenharmony_ci * Software defined PTE bits definition.
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci#define PTE_WRITE		(PTE_DBM)		 /* same as DBM (51) */
1762306a36Sopenharmony_ci#define PTE_SWP_EXCLUSIVE	(_AT(pteval_t, 1) << 2)	 /* only for swp ptes */
1862306a36Sopenharmony_ci#define PTE_DIRTY		(_AT(pteval_t, 1) << 55)
1962306a36Sopenharmony_ci#define PTE_SPECIAL		(_AT(pteval_t, 1) << 56)
2062306a36Sopenharmony_ci#define PTE_DEVMAP		(_AT(pteval_t, 1) << 57)
2162306a36Sopenharmony_ci#define PTE_PROT_NONE		(_AT(pteval_t, 1) << 58) /* only when !PTE_VALID */
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/*
2462306a36Sopenharmony_ci * This bit indicates that the entry is present i.e. pmd_page()
2562306a36Sopenharmony_ci * still points to a valid huge page in memory even if the pmd
2662306a36Sopenharmony_ci * has been invalidated.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci#define PMD_PRESENT_INVALID	(_AT(pteval_t, 1) << 59) /* only when !PMD_SECT_VALID */
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define _PROT_DEFAULT		(PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)
3162306a36Sopenharmony_ci#define _PROT_SECT_DEFAULT	(PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#define PROT_DEFAULT		(_PROT_DEFAULT | PTE_MAYBE_NG)
3462306a36Sopenharmony_ci#define PROT_SECT_DEFAULT	(_PROT_SECT_DEFAULT | PMD_MAYBE_NG)
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define PROT_DEVICE_nGnRnE	(PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_WRITE | PTE_ATTRINDX(MT_DEVICE_nGnRnE))
3762306a36Sopenharmony_ci#define PROT_DEVICE_nGnRE	(PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_WRITE | PTE_ATTRINDX(MT_DEVICE_nGnRE))
3862306a36Sopenharmony_ci#define PROT_NORMAL_NC		(PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_WRITE | PTE_ATTRINDX(MT_NORMAL_NC))
3962306a36Sopenharmony_ci#define PROT_NORMAL		(PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_WRITE | PTE_ATTRINDX(MT_NORMAL))
4062306a36Sopenharmony_ci#define PROT_NORMAL_TAGGED	(PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_WRITE | PTE_ATTRINDX(MT_NORMAL_TAGGED))
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define PROT_SECT_DEVICE_nGnRE	(PROT_SECT_DEFAULT | PMD_SECT_PXN | PMD_SECT_UXN | PMD_ATTRINDX(MT_DEVICE_nGnRE))
4362306a36Sopenharmony_ci#define PROT_SECT_NORMAL	(PROT_SECT_DEFAULT | PMD_SECT_PXN | PMD_SECT_UXN | PTE_WRITE | PMD_ATTRINDX(MT_NORMAL))
4462306a36Sopenharmony_ci#define PROT_SECT_NORMAL_EXEC	(PROT_SECT_DEFAULT | PMD_SECT_UXN | PMD_ATTRINDX(MT_NORMAL))
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define _PAGE_DEFAULT		(_PROT_DEFAULT | PTE_ATTRINDX(MT_NORMAL))
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci#define _PAGE_KERNEL		(PROT_NORMAL)
4962306a36Sopenharmony_ci#define _PAGE_KERNEL_RO		((PROT_NORMAL & ~PTE_WRITE) | PTE_RDONLY)
5062306a36Sopenharmony_ci#define _PAGE_KERNEL_ROX	((PROT_NORMAL & ~(PTE_WRITE | PTE_PXN)) | PTE_RDONLY)
5162306a36Sopenharmony_ci#define _PAGE_KERNEL_EXEC	(PROT_NORMAL & ~PTE_PXN)
5262306a36Sopenharmony_ci#define _PAGE_KERNEL_EXEC_CONT	((PROT_NORMAL & ~PTE_PXN) | PTE_CONT)
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define _PAGE_SHARED		(_PAGE_DEFAULT | PTE_USER | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN | PTE_WRITE)
5562306a36Sopenharmony_ci#define _PAGE_SHARED_EXEC	(_PAGE_DEFAULT | PTE_USER | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_WRITE)
5662306a36Sopenharmony_ci#define _PAGE_READONLY		(_PAGE_DEFAULT | PTE_USER | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)
5762306a36Sopenharmony_ci#define _PAGE_READONLY_EXEC	(_PAGE_DEFAULT | PTE_USER | PTE_RDONLY | PTE_NG | PTE_PXN)
5862306a36Sopenharmony_ci#define _PAGE_EXECONLY		(_PAGE_DEFAULT | PTE_RDONLY | PTE_NG | PTE_PXN)
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#ifdef __ASSEMBLY__
6162306a36Sopenharmony_ci#define PTE_MAYBE_NG	0
6262306a36Sopenharmony_ci#endif
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#ifndef __ASSEMBLY__
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#include <asm/cpufeature.h>
6762306a36Sopenharmony_ci#include <asm/pgtable-types.h>
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciextern bool arm64_use_ng_mappings;
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#define PTE_MAYBE_NG		(arm64_use_ng_mappings ? PTE_NG : 0)
7262306a36Sopenharmony_ci#define PMD_MAYBE_NG		(arm64_use_ng_mappings ? PMD_SECT_NG : 0)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/*
7562306a36Sopenharmony_ci * If we have userspace only BTI we don't want to mark kernel pages
7662306a36Sopenharmony_ci * guarded even if the system does support BTI.
7762306a36Sopenharmony_ci */
7862306a36Sopenharmony_ci#ifdef CONFIG_ARM64_BTI_KERNEL
7962306a36Sopenharmony_ci#define PTE_MAYBE_GP		(system_supports_bti() ? PTE_GP : 0)
8062306a36Sopenharmony_ci#else
8162306a36Sopenharmony_ci#define PTE_MAYBE_GP		0
8262306a36Sopenharmony_ci#endif
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci#define PAGE_KERNEL		__pgprot(_PAGE_KERNEL)
8562306a36Sopenharmony_ci#define PAGE_KERNEL_RO		__pgprot(_PAGE_KERNEL_RO)
8662306a36Sopenharmony_ci#define PAGE_KERNEL_ROX		__pgprot(_PAGE_KERNEL_ROX)
8762306a36Sopenharmony_ci#define PAGE_KERNEL_EXEC	__pgprot(_PAGE_KERNEL_EXEC)
8862306a36Sopenharmony_ci#define PAGE_KERNEL_EXEC_CONT	__pgprot(_PAGE_KERNEL_EXEC_CONT)
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci#define PAGE_S2_MEMATTR(attr, has_fwb)					\
9162306a36Sopenharmony_ci	({								\
9262306a36Sopenharmony_ci		u64 __val;						\
9362306a36Sopenharmony_ci		if (has_fwb)						\
9462306a36Sopenharmony_ci			__val = PTE_S2_MEMATTR(MT_S2_FWB_ ## attr);	\
9562306a36Sopenharmony_ci		else							\
9662306a36Sopenharmony_ci			__val = PTE_S2_MEMATTR(MT_S2_ ## attr);		\
9762306a36Sopenharmony_ci		__val;							\
9862306a36Sopenharmony_ci	 })
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci#define PAGE_NONE		__pgprot(((_PAGE_DEFAULT) & ~PTE_VALID) | PTE_PROT_NONE | PTE_RDONLY | PTE_NG | PTE_PXN | PTE_UXN)
10162306a36Sopenharmony_ci/* shared+writable pages are clean by default, hence PTE_RDONLY|PTE_WRITE */
10262306a36Sopenharmony_ci#define PAGE_SHARED		__pgprot(_PAGE_SHARED)
10362306a36Sopenharmony_ci#define PAGE_SHARED_EXEC	__pgprot(_PAGE_SHARED_EXEC)
10462306a36Sopenharmony_ci#define PAGE_READONLY		__pgprot(_PAGE_READONLY)
10562306a36Sopenharmony_ci#define PAGE_READONLY_EXEC	__pgprot(_PAGE_READONLY_EXEC)
10662306a36Sopenharmony_ci#define PAGE_EXECONLY		__pgprot(_PAGE_EXECONLY)
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci#define pte_pi_index(pte) ( \
11162306a36Sopenharmony_ci	((pte & BIT(PTE_PI_IDX_3)) >> (PTE_PI_IDX_3 - 3)) | \
11262306a36Sopenharmony_ci	((pte & BIT(PTE_PI_IDX_2)) >> (PTE_PI_IDX_2 - 2)) | \
11362306a36Sopenharmony_ci	((pte & BIT(PTE_PI_IDX_1)) >> (PTE_PI_IDX_1 - 1)) | \
11462306a36Sopenharmony_ci	((pte & BIT(PTE_PI_IDX_0)) >> (PTE_PI_IDX_0 - 0)))
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci/*
11762306a36Sopenharmony_ci * Page types used via Permission Indirection Extension (PIE). PIE uses
11862306a36Sopenharmony_ci * the USER, DBM, PXN and UXN bits to to generate an index which is used
11962306a36Sopenharmony_ci * to look up the actual permission in PIR_ELx and PIRE0_EL1. We define
12062306a36Sopenharmony_ci * combinations we use on non-PIE systems with the same encoding, for
12162306a36Sopenharmony_ci * convenience these are listed here as comments as are the unallocated
12262306a36Sopenharmony_ci * encodings.
12362306a36Sopenharmony_ci */
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci/* 0: PAGE_DEFAULT                                                  */
12662306a36Sopenharmony_ci/* 1:                                                      PTE_USER */
12762306a36Sopenharmony_ci/* 2:                                          PTE_WRITE            */
12862306a36Sopenharmony_ci/* 3:                                          PTE_WRITE | PTE_USER */
12962306a36Sopenharmony_ci/* 4: PAGE_EXECONLY                  PTE_PXN                        */
13062306a36Sopenharmony_ci/* 5: PAGE_READONLY_EXEC             PTE_PXN |             PTE_USER */
13162306a36Sopenharmony_ci/* 6:                                PTE_PXN | PTE_WRITE            */
13262306a36Sopenharmony_ci/* 7: PAGE_SHARED_EXEC               PTE_PXN | PTE_WRITE | PTE_USER */
13362306a36Sopenharmony_ci/* 8: PAGE_KERNEL_ROX      PTE_UXN                                  */
13462306a36Sopenharmony_ci/* 9:                      PTE_UXN |                       PTE_USER */
13562306a36Sopenharmony_ci/* a: PAGE_KERNEL_EXEC     PTE_UXN |           PTE_WRITE            */
13662306a36Sopenharmony_ci/* b:                      PTE_UXN |           PTE_WRITE | PTE_USER */
13762306a36Sopenharmony_ci/* c: PAGE_KERNEL_RO       PTE_UXN | PTE_PXN                        */
13862306a36Sopenharmony_ci/* d: PAGE_READONLY        PTE_UXN | PTE_PXN |             PTE_USER */
13962306a36Sopenharmony_ci/* e: PAGE_KERNEL          PTE_UXN | PTE_PXN | PTE_WRITE            */
14062306a36Sopenharmony_ci/* f: PAGE_SHARED          PTE_UXN | PTE_PXN | PTE_WRITE | PTE_USER */
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci#define PIE_E0	( \
14362306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_EXECONLY),      PIE_X_O) | \
14462306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_READONLY_EXEC), PIE_RX)  | \
14562306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_SHARED_EXEC),   PIE_RWX) | \
14662306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_READONLY),      PIE_R)   | \
14762306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_SHARED),        PIE_RW))
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci#define PIE_E1	( \
15062306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_EXECONLY),      PIE_NONE_O) | \
15162306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_READONLY_EXEC), PIE_R)      | \
15262306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_SHARED_EXEC),   PIE_RW)     | \
15362306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_READONLY),      PIE_R)      | \
15462306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_SHARED),        PIE_RW)     | \
15562306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_KERNEL_ROX),    PIE_RX)     | \
15662306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_KERNEL_EXEC),   PIE_RWX)    | \
15762306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_KERNEL_RO),     PIE_R)      | \
15862306a36Sopenharmony_ci	PIRx_ELx_PERM(pte_pi_index(_PAGE_KERNEL),        PIE_RW))
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci#endif /* __ASM_PGTABLE_PROT_H */
161