xref: /openbmc/linux/tools/testing/selftests/mm/pkey-powerpc.h (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1*baa489faSSeongJae Park /* SPDX-License-Identifier: GPL-2.0 */
2*baa489faSSeongJae Park 
3*baa489faSSeongJae Park #ifndef _PKEYS_POWERPC_H
4*baa489faSSeongJae Park #define _PKEYS_POWERPC_H
5*baa489faSSeongJae Park 
6*baa489faSSeongJae Park #ifndef SYS_pkey_alloc
7*baa489faSSeongJae Park # define SYS_pkey_alloc		384
8*baa489faSSeongJae Park # define SYS_pkey_free		385
9*baa489faSSeongJae Park #endif
10*baa489faSSeongJae Park #define REG_IP_IDX		PT_NIP
11*baa489faSSeongJae Park #define REG_TRAPNO		PT_TRAP
12*baa489faSSeongJae Park #define gregs			gp_regs
13*baa489faSSeongJae Park #define fpregs			fp_regs
14*baa489faSSeongJae Park #define si_pkey_offset		0x20
15*baa489faSSeongJae Park 
16*baa489faSSeongJae Park #undef PKEY_DISABLE_ACCESS
17*baa489faSSeongJae Park #define PKEY_DISABLE_ACCESS	0x3  /* disable read and write */
18*baa489faSSeongJae Park 
19*baa489faSSeongJae Park #undef PKEY_DISABLE_WRITE
20*baa489faSSeongJae Park #define PKEY_DISABLE_WRITE	0x2
21*baa489faSSeongJae Park 
22*baa489faSSeongJae Park #define NR_PKEYS		32
23*baa489faSSeongJae Park #define NR_RESERVED_PKEYS_4K	27 /* pkey-0, pkey-1, exec-only-pkey
24*baa489faSSeongJae Park 				      and 24 other keys that cannot be
25*baa489faSSeongJae Park 				      represented in the PTE */
26*baa489faSSeongJae Park #define NR_RESERVED_PKEYS_64K_3KEYS	3 /* PowerNV and KVM: pkey-0,
27*baa489faSSeongJae Park 					     pkey-1 and exec-only key */
28*baa489faSSeongJae Park #define NR_RESERVED_PKEYS_64K_4KEYS	4 /* PowerVM: pkey-0, pkey-1,
29*baa489faSSeongJae Park 					     pkey-31 and exec-only key */
30*baa489faSSeongJae Park #define PKEY_BITS_PER_PKEY	2
31*baa489faSSeongJae Park #define HPAGE_SIZE		(1UL << 24)
32*baa489faSSeongJae Park #define PAGE_SIZE		sysconf(_SC_PAGESIZE)
33*baa489faSSeongJae Park 
pkey_bit_position(int pkey)34*baa489faSSeongJae Park static inline u32 pkey_bit_position(int pkey)
35*baa489faSSeongJae Park {
36*baa489faSSeongJae Park 	return (NR_PKEYS - pkey - 1) * PKEY_BITS_PER_PKEY;
37*baa489faSSeongJae Park }
38*baa489faSSeongJae Park 
__read_pkey_reg(void)39*baa489faSSeongJae Park static inline u64 __read_pkey_reg(void)
40*baa489faSSeongJae Park {
41*baa489faSSeongJae Park 	u64 pkey_reg;
42*baa489faSSeongJae Park 
43*baa489faSSeongJae Park 	asm volatile("mfspr %0, 0xd" : "=r" (pkey_reg));
44*baa489faSSeongJae Park 
45*baa489faSSeongJae Park 	return pkey_reg;
46*baa489faSSeongJae Park }
47*baa489faSSeongJae Park 
__write_pkey_reg(u64 pkey_reg)48*baa489faSSeongJae Park static inline void __write_pkey_reg(u64 pkey_reg)
49*baa489faSSeongJae Park {
50*baa489faSSeongJae Park 	u64 amr = pkey_reg;
51*baa489faSSeongJae Park 
52*baa489faSSeongJae Park 	dprintf4("%s() changing %016llx to %016llx\n",
53*baa489faSSeongJae Park 			 __func__, __read_pkey_reg(), pkey_reg);
54*baa489faSSeongJae Park 
55*baa489faSSeongJae Park 	asm volatile("isync; mtspr 0xd, %0; isync"
56*baa489faSSeongJae Park 		     : : "r" ((unsigned long)(amr)) : "memory");
57*baa489faSSeongJae Park 
58*baa489faSSeongJae Park 	dprintf4("%s() pkey register after changing %016llx to %016llx\n",
59*baa489faSSeongJae Park 			__func__, __read_pkey_reg(), pkey_reg);
60*baa489faSSeongJae Park }
61*baa489faSSeongJae Park 
cpu_has_pkeys(void)62*baa489faSSeongJae Park static inline int cpu_has_pkeys(void)
63*baa489faSSeongJae Park {
64*baa489faSSeongJae Park 	/* No simple way to determine this */
65*baa489faSSeongJae Park 	return 1;
66*baa489faSSeongJae Park }
67*baa489faSSeongJae Park 
arch_is_powervm()68*baa489faSSeongJae Park static inline bool arch_is_powervm()
69*baa489faSSeongJae Park {
70*baa489faSSeongJae Park 	struct stat buf;
71*baa489faSSeongJae Park 
72*baa489faSSeongJae Park 	if ((stat("/sys/firmware/devicetree/base/ibm,partition-name", &buf) == 0) &&
73*baa489faSSeongJae Park 	    (stat("/sys/firmware/devicetree/base/hmc-managed?", &buf) == 0) &&
74*baa489faSSeongJae Park 	    (stat("/sys/firmware/devicetree/base/chosen/qemu,graphic-width", &buf) == -1) )
75*baa489faSSeongJae Park 		return true;
76*baa489faSSeongJae Park 
77*baa489faSSeongJae Park 	return false;
78*baa489faSSeongJae Park }
79*baa489faSSeongJae Park 
get_arch_reserved_keys(void)80*baa489faSSeongJae Park static inline int get_arch_reserved_keys(void)
81*baa489faSSeongJae Park {
82*baa489faSSeongJae Park 	if (sysconf(_SC_PAGESIZE) == 4096)
83*baa489faSSeongJae Park 		return NR_RESERVED_PKEYS_4K;
84*baa489faSSeongJae Park 	else
85*baa489faSSeongJae Park 		if (arch_is_powervm())
86*baa489faSSeongJae Park 			return NR_RESERVED_PKEYS_64K_4KEYS;
87*baa489faSSeongJae Park 		else
88*baa489faSSeongJae Park 			return NR_RESERVED_PKEYS_64K_3KEYS;
89*baa489faSSeongJae Park }
90*baa489faSSeongJae Park 
expect_fault_on_read_execonly_key(void * p1,int pkey)91*baa489faSSeongJae Park void expect_fault_on_read_execonly_key(void *p1, int pkey)
92*baa489faSSeongJae Park {
93*baa489faSSeongJae Park 	/*
94*baa489faSSeongJae Park 	 * powerpc does not allow userspace to change permissions of exec-only
95*baa489faSSeongJae Park 	 * keys since those keys are not allocated by userspace. The signal
96*baa489faSSeongJae Park 	 * handler wont be able to reset the permissions, which means the code
97*baa489faSSeongJae Park 	 * will infinitely continue to segfault here.
98*baa489faSSeongJae Park 	 */
99*baa489faSSeongJae Park 	return;
100*baa489faSSeongJae Park }
101*baa489faSSeongJae Park 
102*baa489faSSeongJae Park /* 4-byte instructions * 16384 = 64K page */
103*baa489faSSeongJae Park #define __page_o_noops() asm(".rept 16384 ; nop; .endr")
104*baa489faSSeongJae Park 
malloc_pkey_with_mprotect_subpage(long size,int prot,u16 pkey)105*baa489faSSeongJae Park void *malloc_pkey_with_mprotect_subpage(long size, int prot, u16 pkey)
106*baa489faSSeongJae Park {
107*baa489faSSeongJae Park 	void *ptr;
108*baa489faSSeongJae Park 	int ret;
109*baa489faSSeongJae Park 
110*baa489faSSeongJae Park 	dprintf1("doing %s(size=%ld, prot=0x%x, pkey=%d)\n", __func__,
111*baa489faSSeongJae Park 			size, prot, pkey);
112*baa489faSSeongJae Park 	pkey_assert(pkey < NR_PKEYS);
113*baa489faSSeongJae Park 	ptr = mmap(NULL, size, prot, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
114*baa489faSSeongJae Park 	pkey_assert(ptr != (void *)-1);
115*baa489faSSeongJae Park 
116*baa489faSSeongJae Park 	ret = syscall(__NR_subpage_prot, ptr, size, NULL);
117*baa489faSSeongJae Park 	if (ret) {
118*baa489faSSeongJae Park 		perror("subpage_perm");
119*baa489faSSeongJae Park 		return PTR_ERR_ENOTSUP;
120*baa489faSSeongJae Park 	}
121*baa489faSSeongJae Park 
122*baa489faSSeongJae Park 	ret = mprotect_pkey((void *)ptr, PAGE_SIZE, prot, pkey);
123*baa489faSSeongJae Park 	pkey_assert(!ret);
124*baa489faSSeongJae Park 	record_pkey_malloc(ptr, size, prot);
125*baa489faSSeongJae Park 
126*baa489faSSeongJae Park 	dprintf1("%s() for pkey %d @ %p\n", __func__, pkey, ptr);
127*baa489faSSeongJae Park 	return ptr;
128*baa489faSSeongJae Park }
129*baa489faSSeongJae Park 
130*baa489faSSeongJae Park #endif /* _PKEYS_POWERPC_H */
131