xref: /OK3568_Linux_fs/kernel/arch/powerpc/mm/book3s64/pkeys.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * PowerPC Memory Protection Keys management
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2017, Ram Pai, IBM Corporation.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <asm/mman.h>
9*4882a593Smuzhiyun #include <asm/mmu_context.h>
10*4882a593Smuzhiyun #include <asm/mmu.h>
11*4882a593Smuzhiyun #include <asm/setup.h>
12*4882a593Smuzhiyun #include <linux/pkeys.h>
13*4882a593Smuzhiyun #include <linux/of_fdt.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun int  num_pkey;		/* Max number of pkeys supported */
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun  *  Keys marked in the reservation list cannot be allocated by  userspace
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun u32 reserved_allocation_mask __ro_after_init;
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /* Bits set for the initially allocated keys */
22*4882a593Smuzhiyun static u32 initial_allocation_mask __ro_after_init;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Even if we allocate keys with sys_pkey_alloc(), we need to make sure
26*4882a593Smuzhiyun  * other thread still find the access denied using the same keys.
27*4882a593Smuzhiyun  */
28*4882a593Smuzhiyun static u64 default_amr = ~0x0UL;
29*4882a593Smuzhiyun static u64 default_iamr = 0x5555555555555555UL;
30*4882a593Smuzhiyun u64 default_uamor __ro_after_init;
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun  * Key used to implement PROT_EXEC mmap. Denies READ/WRITE
33*4882a593Smuzhiyun  * We pick key 2 because 0 is special key and 1 is reserved as per ISA.
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun static int execute_only_key = 2;
36*4882a593Smuzhiyun static bool pkey_execute_disable_supported;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define AMR_BITS_PER_PKEY 2
40*4882a593Smuzhiyun #define AMR_RD_BIT 0x1UL
41*4882a593Smuzhiyun #define AMR_WR_BIT 0x2UL
42*4882a593Smuzhiyun #define IAMR_EX_BIT 0x1UL
43*4882a593Smuzhiyun #define PKEY_REG_BITS (sizeof(u64) * 8)
44*4882a593Smuzhiyun #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
45*4882a593Smuzhiyun 
dt_scan_storage_keys(unsigned long node,const char * uname,int depth,void * data)46*4882a593Smuzhiyun static int __init dt_scan_storage_keys(unsigned long node,
47*4882a593Smuzhiyun 				       const char *uname, int depth,
48*4882a593Smuzhiyun 				       void *data)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
51*4882a593Smuzhiyun 	const __be32 *prop;
52*4882a593Smuzhiyun 	int *pkeys_total = (int *) data;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/* We are scanning "cpu" nodes only */
55*4882a593Smuzhiyun 	if (type == NULL || strcmp(type, "cpu") != 0)
56*4882a593Smuzhiyun 		return 0;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	prop = of_get_flat_dt_prop(node, "ibm,processor-storage-keys", NULL);
59*4882a593Smuzhiyun 	if (!prop)
60*4882a593Smuzhiyun 		return 0;
61*4882a593Smuzhiyun 	*pkeys_total = be32_to_cpu(prop[0]);
62*4882a593Smuzhiyun 	return 1;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
scan_pkey_feature(void)65*4882a593Smuzhiyun static int scan_pkey_feature(void)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	int ret;
68*4882a593Smuzhiyun 	int pkeys_total = 0;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/*
71*4882a593Smuzhiyun 	 * Pkey is not supported with Radix translation.
72*4882a593Smuzhiyun 	 */
73*4882a593Smuzhiyun 	if (early_radix_enabled())
74*4882a593Smuzhiyun 		return 0;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	ret = of_scan_flat_dt(dt_scan_storage_keys, &pkeys_total);
77*4882a593Smuzhiyun 	if (ret == 0) {
78*4882a593Smuzhiyun 		/*
79*4882a593Smuzhiyun 		 * Let's assume 32 pkeys on P8/P9 bare metal, if its not defined by device
80*4882a593Smuzhiyun 		 * tree. We make this exception since some version of skiboot forgot to
81*4882a593Smuzhiyun 		 * expose this property on power8/9.
82*4882a593Smuzhiyun 		 */
83*4882a593Smuzhiyun 		if (!firmware_has_feature(FW_FEATURE_LPAR)) {
84*4882a593Smuzhiyun 			unsigned long pvr = mfspr(SPRN_PVR);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 			if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
87*4882a593Smuzhiyun 			    PVR_VER(pvr) == PVR_POWER8NVL || PVR_VER(pvr) == PVR_POWER9)
88*4882a593Smuzhiyun 				pkeys_total = 32;
89*4882a593Smuzhiyun 		}
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/*
93*4882a593Smuzhiyun 	 * Adjust the upper limit, based on the number of bits supported by
94*4882a593Smuzhiyun 	 * arch-neutral code.
95*4882a593Smuzhiyun 	 */
96*4882a593Smuzhiyun 	pkeys_total = min_t(int, pkeys_total,
97*4882a593Smuzhiyun 			    ((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) + 1));
98*4882a593Smuzhiyun 	return pkeys_total;
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
pkey_early_init_devtree(void)101*4882a593Smuzhiyun void __init pkey_early_init_devtree(void)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	int pkeys_total, i;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	/*
106*4882a593Smuzhiyun 	 * We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral
107*4882a593Smuzhiyun 	 * generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE.
108*4882a593Smuzhiyun 	 * Ensure that the bits a distinct.
109*4882a593Smuzhiyun 	 */
110*4882a593Smuzhiyun 	BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
111*4882a593Smuzhiyun 		     (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	/*
114*4882a593Smuzhiyun 	 * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
115*4882a593Smuzhiyun 	 * in the vmaflag. Make sure that is really the case.
116*4882a593Smuzhiyun 	 */
117*4882a593Smuzhiyun 	BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
118*4882a593Smuzhiyun 		     __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
119*4882a593Smuzhiyun 				!= (sizeof(u64) * BITS_PER_BYTE));
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	/*
122*4882a593Smuzhiyun 	 * Only P7 and above supports SPRN_AMR update with MSR[PR] = 1
123*4882a593Smuzhiyun 	 */
124*4882a593Smuzhiyun 	if (!early_cpu_has_feature(CPU_FTR_ARCH_206))
125*4882a593Smuzhiyun 		return;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	/* scan the device tree for pkey feature */
128*4882a593Smuzhiyun 	pkeys_total = scan_pkey_feature();
129*4882a593Smuzhiyun 	if (!pkeys_total)
130*4882a593Smuzhiyun 		goto out;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/* Allow all keys to be modified by default */
133*4882a593Smuzhiyun 	default_uamor = ~0x0UL;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	cur_cpu_spec->mmu_features |= MMU_FTR_PKEY;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/*
138*4882a593Smuzhiyun 	 * The device tree cannot be relied to indicate support for
139*4882a593Smuzhiyun 	 * execute_disable support. Instead we use a PVR check.
140*4882a593Smuzhiyun 	 */
141*4882a593Smuzhiyun 	if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
142*4882a593Smuzhiyun 		pkey_execute_disable_supported = false;
143*4882a593Smuzhiyun 	else
144*4882a593Smuzhiyun 		pkey_execute_disable_supported = true;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun #ifdef CONFIG_PPC_4K_PAGES
147*4882a593Smuzhiyun 	/*
148*4882a593Smuzhiyun 	 * The OS can manage only 8 pkeys due to its inability to represent them
149*4882a593Smuzhiyun 	 * in the Linux 4K PTE. Mark all other keys reserved.
150*4882a593Smuzhiyun 	 */
151*4882a593Smuzhiyun 	num_pkey = min(8, pkeys_total);
152*4882a593Smuzhiyun #else
153*4882a593Smuzhiyun 	num_pkey = pkeys_total;
154*4882a593Smuzhiyun #endif
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	if (unlikely(num_pkey <= execute_only_key) || !pkey_execute_disable_supported) {
157*4882a593Smuzhiyun 		/*
158*4882a593Smuzhiyun 		 * Insufficient number of keys to support
159*4882a593Smuzhiyun 		 * execute only key. Mark it unavailable.
160*4882a593Smuzhiyun 		 */
161*4882a593Smuzhiyun 		execute_only_key = -1;
162*4882a593Smuzhiyun 	} else {
163*4882a593Smuzhiyun 		/*
164*4882a593Smuzhiyun 		 * Mark the execute_only_pkey as not available for
165*4882a593Smuzhiyun 		 * user allocation via pkey_alloc.
166*4882a593Smuzhiyun 		 */
167*4882a593Smuzhiyun 		reserved_allocation_mask |= (0x1 << execute_only_key);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 		/*
170*4882a593Smuzhiyun 		 * Deny READ/WRITE for execute_only_key.
171*4882a593Smuzhiyun 		 * Allow execute in IAMR.
172*4882a593Smuzhiyun 		 */
173*4882a593Smuzhiyun 		default_amr  |= (0x3ul << pkeyshift(execute_only_key));
174*4882a593Smuzhiyun 		default_iamr &= ~(0x1ul << pkeyshift(execute_only_key));
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 		/*
177*4882a593Smuzhiyun 		 * Clear the uamor bits for this key.
178*4882a593Smuzhiyun 		 */
179*4882a593Smuzhiyun 		default_uamor &= ~(0x3ul << pkeyshift(execute_only_key));
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/*
183*4882a593Smuzhiyun 	 * Allow access for only key 0. And prevent any other modification.
184*4882a593Smuzhiyun 	 */
185*4882a593Smuzhiyun 	default_amr   &= ~(0x3ul << pkeyshift(0));
186*4882a593Smuzhiyun 	default_iamr  &= ~(0x1ul << pkeyshift(0));
187*4882a593Smuzhiyun 	default_uamor &= ~(0x3ul << pkeyshift(0));
188*4882a593Smuzhiyun 	/*
189*4882a593Smuzhiyun 	 * key 0 is special in that we want to consider it an allocated
190*4882a593Smuzhiyun 	 * key which is preallocated. We don't allow changing AMR bits
191*4882a593Smuzhiyun 	 * w.r.t key 0. But one can pkey_free(key0)
192*4882a593Smuzhiyun 	 */
193*4882a593Smuzhiyun 	initial_allocation_mask |= (0x1 << 0);
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	/*
196*4882a593Smuzhiyun 	 * key 1 is recommended not to be used. PowerISA(3.0) page 1015,
197*4882a593Smuzhiyun 	 * programming note.
198*4882a593Smuzhiyun 	 */
199*4882a593Smuzhiyun 	reserved_allocation_mask |= (0x1 << 1);
200*4882a593Smuzhiyun 	default_uamor &= ~(0x3ul << pkeyshift(1));
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	/*
203*4882a593Smuzhiyun 	 * Prevent the usage of OS reserved keys. Update UAMOR
204*4882a593Smuzhiyun 	 * for those keys. Also mark the rest of the bits in the
205*4882a593Smuzhiyun 	 * 32 bit mask as reserved.
206*4882a593Smuzhiyun 	 */
207*4882a593Smuzhiyun 	for (i = num_pkey; i < 32 ; i++) {
208*4882a593Smuzhiyun 		reserved_allocation_mask |= (0x1 << i);
209*4882a593Smuzhiyun 		default_uamor &= ~(0x3ul << pkeyshift(i));
210*4882a593Smuzhiyun 	}
211*4882a593Smuzhiyun 	/*
212*4882a593Smuzhiyun 	 * Prevent the allocation of reserved keys too.
213*4882a593Smuzhiyun 	 */
214*4882a593Smuzhiyun 	initial_allocation_mask |= reserved_allocation_mask;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	pr_info("Enabling pkeys with max key count %d\n", num_pkey);
217*4882a593Smuzhiyun out:
218*4882a593Smuzhiyun 	/*
219*4882a593Smuzhiyun 	 * Setup uamor on boot cpu
220*4882a593Smuzhiyun 	 */
221*4882a593Smuzhiyun 	mtspr(SPRN_UAMOR, default_uamor);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	return;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
pkey_mm_init(struct mm_struct * mm)226*4882a593Smuzhiyun void pkey_mm_init(struct mm_struct *mm)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun 	if (!mmu_has_feature(MMU_FTR_PKEY))
229*4882a593Smuzhiyun 		return;
230*4882a593Smuzhiyun 	mm_pkey_allocation_map(mm) = initial_allocation_mask;
231*4882a593Smuzhiyun 	mm->context.execute_only_pkey = execute_only_key;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
read_amr(void)234*4882a593Smuzhiyun static inline u64 read_amr(void)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	return mfspr(SPRN_AMR);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun 
write_amr(u64 value)239*4882a593Smuzhiyun static inline void write_amr(u64 value)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	mtspr(SPRN_AMR, value);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
read_iamr(void)244*4882a593Smuzhiyun static inline u64 read_iamr(void)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun 	if (!likely(pkey_execute_disable_supported))
247*4882a593Smuzhiyun 		return 0x0UL;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	return mfspr(SPRN_IAMR);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
write_iamr(u64 value)252*4882a593Smuzhiyun static inline void write_iamr(u64 value)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	if (!likely(pkey_execute_disable_supported))
255*4882a593Smuzhiyun 		return;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	mtspr(SPRN_IAMR, value);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun 
init_amr(int pkey,u8 init_bits)260*4882a593Smuzhiyun static inline void init_amr(int pkey, u8 init_bits)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
263*4882a593Smuzhiyun 	u64 old_amr = read_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	write_amr(old_amr | new_amr_bits);
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun 
init_iamr(int pkey,u8 init_bits)268*4882a593Smuzhiyun static inline void init_iamr(int pkey, u8 init_bits)
269*4882a593Smuzhiyun {
270*4882a593Smuzhiyun 	u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
271*4882a593Smuzhiyun 	u64 old_iamr = read_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	write_iamr(old_iamr | new_iamr_bits);
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun  * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
278*4882a593Smuzhiyun  * specified in @init_val.
279*4882a593Smuzhiyun  */
__arch_set_user_pkey_access(struct task_struct * tsk,int pkey,unsigned long init_val)280*4882a593Smuzhiyun int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
281*4882a593Smuzhiyun 				unsigned long init_val)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	u64 new_amr_bits = 0x0ul;
284*4882a593Smuzhiyun 	u64 new_iamr_bits = 0x0ul;
285*4882a593Smuzhiyun 	u64 pkey_bits, uamor_pkey_bits;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	/*
288*4882a593Smuzhiyun 	 * Check whether the key is disabled by UAMOR.
289*4882a593Smuzhiyun 	 */
290*4882a593Smuzhiyun 	pkey_bits = 0x3ul << pkeyshift(pkey);
291*4882a593Smuzhiyun 	uamor_pkey_bits = (default_uamor & pkey_bits);
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	/*
294*4882a593Smuzhiyun 	 * Both the bits in UAMOR corresponding to the key should be set
295*4882a593Smuzhiyun 	 */
296*4882a593Smuzhiyun 	if (uamor_pkey_bits != pkey_bits)
297*4882a593Smuzhiyun 		return -EINVAL;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	if (init_val & PKEY_DISABLE_EXECUTE) {
300*4882a593Smuzhiyun 		if (!pkey_execute_disable_supported)
301*4882a593Smuzhiyun 			return -EINVAL;
302*4882a593Smuzhiyun 		new_iamr_bits |= IAMR_EX_BIT;
303*4882a593Smuzhiyun 	}
304*4882a593Smuzhiyun 	init_iamr(pkey, new_iamr_bits);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	/* Set the bits we need in AMR: */
307*4882a593Smuzhiyun 	if (init_val & PKEY_DISABLE_ACCESS)
308*4882a593Smuzhiyun 		new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
309*4882a593Smuzhiyun 	else if (init_val & PKEY_DISABLE_WRITE)
310*4882a593Smuzhiyun 		new_amr_bits |= AMR_WR_BIT;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	init_amr(pkey, new_amr_bits);
313*4882a593Smuzhiyun 	return 0;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
thread_pkey_regs_save(struct thread_struct * thread)316*4882a593Smuzhiyun void thread_pkey_regs_save(struct thread_struct *thread)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun 	if (!mmu_has_feature(MMU_FTR_PKEY))
319*4882a593Smuzhiyun 		return;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	/*
322*4882a593Smuzhiyun 	 * TODO: Skip saving registers if @thread hasn't used any keys yet.
323*4882a593Smuzhiyun 	 */
324*4882a593Smuzhiyun 	thread->amr = read_amr();
325*4882a593Smuzhiyun 	thread->iamr = read_iamr();
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun 
thread_pkey_regs_restore(struct thread_struct * new_thread,struct thread_struct * old_thread)328*4882a593Smuzhiyun void thread_pkey_regs_restore(struct thread_struct *new_thread,
329*4882a593Smuzhiyun 			      struct thread_struct *old_thread)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	if (!mmu_has_feature(MMU_FTR_PKEY))
332*4882a593Smuzhiyun 		return;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	if (old_thread->amr != new_thread->amr)
335*4882a593Smuzhiyun 		write_amr(new_thread->amr);
336*4882a593Smuzhiyun 	if (old_thread->iamr != new_thread->iamr)
337*4882a593Smuzhiyun 		write_iamr(new_thread->iamr);
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
thread_pkey_regs_init(struct thread_struct * thread)340*4882a593Smuzhiyun void thread_pkey_regs_init(struct thread_struct *thread)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun 	if (!mmu_has_feature(MMU_FTR_PKEY))
343*4882a593Smuzhiyun 		return;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	thread->amr   = default_amr;
346*4882a593Smuzhiyun 	thread->iamr  = default_iamr;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	write_amr(default_amr);
349*4882a593Smuzhiyun 	write_iamr(default_iamr);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun 
execute_only_pkey(struct mm_struct * mm)352*4882a593Smuzhiyun int execute_only_pkey(struct mm_struct *mm)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	return mm->context.execute_only_pkey;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
vma_is_pkey_exec_only(struct vm_area_struct * vma)357*4882a593Smuzhiyun static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	/* Do this check first since the vm_flags should be hot */
360*4882a593Smuzhiyun 	if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
361*4882a593Smuzhiyun 		return false;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun /*
367*4882a593Smuzhiyun  * This should only be called for *plain* mprotect calls.
368*4882a593Smuzhiyun  */
__arch_override_mprotect_pkey(struct vm_area_struct * vma,int prot,int pkey)369*4882a593Smuzhiyun int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
370*4882a593Smuzhiyun 				  int pkey)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun 	/*
373*4882a593Smuzhiyun 	 * If the currently associated pkey is execute-only, but the requested
374*4882a593Smuzhiyun 	 * protection is not execute-only, move it back to the default pkey.
375*4882a593Smuzhiyun 	 */
376*4882a593Smuzhiyun 	if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
377*4882a593Smuzhiyun 		return 0;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	/*
380*4882a593Smuzhiyun 	 * The requested protection is execute-only. Hence let's use an
381*4882a593Smuzhiyun 	 * execute-only pkey.
382*4882a593Smuzhiyun 	 */
383*4882a593Smuzhiyun 	if (prot == PROT_EXEC) {
384*4882a593Smuzhiyun 		pkey = execute_only_pkey(vma->vm_mm);
385*4882a593Smuzhiyun 		if (pkey > 0)
386*4882a593Smuzhiyun 			return pkey;
387*4882a593Smuzhiyun 	}
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/* Nothing to override. */
390*4882a593Smuzhiyun 	return vma_pkey(vma);
391*4882a593Smuzhiyun }
392*4882a593Smuzhiyun 
pkey_access_permitted(int pkey,bool write,bool execute)393*4882a593Smuzhiyun static bool pkey_access_permitted(int pkey, bool write, bool execute)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	int pkey_shift;
396*4882a593Smuzhiyun 	u64 amr;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	pkey_shift = pkeyshift(pkey);
399*4882a593Smuzhiyun 	if (execute)
400*4882a593Smuzhiyun 		return !(read_iamr() & (IAMR_EX_BIT << pkey_shift));
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	amr = read_amr();
403*4882a593Smuzhiyun 	if (write)
404*4882a593Smuzhiyun 		return !(amr & (AMR_WR_BIT << pkey_shift));
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	return !(amr & (AMR_RD_BIT << pkey_shift));
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun 
arch_pte_access_permitted(u64 pte,bool write,bool execute)409*4882a593Smuzhiyun bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	if (!mmu_has_feature(MMU_FTR_PKEY))
412*4882a593Smuzhiyun 		return true;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun /*
418*4882a593Smuzhiyun  * We only want to enforce protection keys on the current thread because we
419*4882a593Smuzhiyun  * effectively have no access to AMR/IAMR for other threads or any way to tell
420*4882a593Smuzhiyun  * which AMR/IAMR in a threaded process we could use.
421*4882a593Smuzhiyun  *
422*4882a593Smuzhiyun  * So do not enforce things if the VMA is not from the current mm, or if we are
423*4882a593Smuzhiyun  * in a kernel thread.
424*4882a593Smuzhiyun  */
arch_vma_access_permitted(struct vm_area_struct * vma,bool write,bool execute,bool foreign)425*4882a593Smuzhiyun bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
426*4882a593Smuzhiyun 			       bool execute, bool foreign)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	if (!mmu_has_feature(MMU_FTR_PKEY))
429*4882a593Smuzhiyun 		return true;
430*4882a593Smuzhiyun 	/*
431*4882a593Smuzhiyun 	 * Do not enforce our key-permissions on a foreign vma.
432*4882a593Smuzhiyun 	 */
433*4882a593Smuzhiyun 	if (foreign || vma_is_foreign(vma))
434*4882a593Smuzhiyun 		return true;
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	return pkey_access_permitted(vma_pkey(vma), write, execute);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
arch_dup_pkeys(struct mm_struct * oldmm,struct mm_struct * mm)439*4882a593Smuzhiyun void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	if (!mmu_has_feature(MMU_FTR_PKEY))
442*4882a593Smuzhiyun 		return;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	/* Duplicate the oldmm pkey state in mm: */
445*4882a593Smuzhiyun 	mm_pkey_allocation_map(mm) = mm_pkey_allocation_map(oldmm);
446*4882a593Smuzhiyun 	mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
447*4882a593Smuzhiyun }
448