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