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