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