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