Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef _PKEYS_POWERPC_H
0004 #define _PKEYS_POWERPC_H
0005 
0006 #ifndef SYS_mprotect_key
0007 # define SYS_mprotect_key   386
0008 #endif
0009 #ifndef SYS_pkey_alloc
0010 # define SYS_pkey_alloc     384
0011 # define SYS_pkey_free      385
0012 #endif
0013 #define REG_IP_IDX      PT_NIP
0014 #define REG_TRAPNO      PT_TRAP
0015 #define gregs           gp_regs
0016 #define fpregs          fp_regs
0017 #define si_pkey_offset      0x20
0018 
0019 #undef PKEY_DISABLE_ACCESS
0020 #define PKEY_DISABLE_ACCESS 0x3  /* disable read and write */
0021 
0022 #undef PKEY_DISABLE_WRITE
0023 #define PKEY_DISABLE_WRITE  0x2
0024 
0025 #define NR_PKEYS        32
0026 #define NR_RESERVED_PKEYS_4K    27 /* pkey-0, pkey-1, exec-only-pkey
0027                       and 24 other keys that cannot be
0028                       represented in the PTE */
0029 #define NR_RESERVED_PKEYS_64K_3KEYS 3 /* PowerNV and KVM: pkey-0,
0030                          pkey-1 and exec-only key */
0031 #define NR_RESERVED_PKEYS_64K_4KEYS 4 /* PowerVM: pkey-0, pkey-1,
0032                          pkey-31 and exec-only key */
0033 #define PKEY_BITS_PER_PKEY  2
0034 #define HPAGE_SIZE      (1UL << 24)
0035 #define PAGE_SIZE       sysconf(_SC_PAGESIZE)
0036 
0037 static inline u32 pkey_bit_position(int pkey)
0038 {
0039     return (NR_PKEYS - pkey - 1) * PKEY_BITS_PER_PKEY;
0040 }
0041 
0042 static inline u64 __read_pkey_reg(void)
0043 {
0044     u64 pkey_reg;
0045 
0046     asm volatile("mfspr %0, 0xd" : "=r" (pkey_reg));
0047 
0048     return pkey_reg;
0049 }
0050 
0051 static inline void __write_pkey_reg(u64 pkey_reg)
0052 {
0053     u64 amr = pkey_reg;
0054 
0055     dprintf4("%s() changing %016llx to %016llx\n",
0056              __func__, __read_pkey_reg(), pkey_reg);
0057 
0058     asm volatile("isync; mtspr 0xd, %0; isync"
0059              : : "r" ((unsigned long)(amr)) : "memory");
0060 
0061     dprintf4("%s() pkey register after changing %016llx to %016llx\n",
0062             __func__, __read_pkey_reg(), pkey_reg);
0063 }
0064 
0065 static inline int cpu_has_pkeys(void)
0066 {
0067     /* No simple way to determine this */
0068     return 1;
0069 }
0070 
0071 static inline bool arch_is_powervm()
0072 {
0073     struct stat buf;
0074 
0075     if ((stat("/sys/firmware/devicetree/base/ibm,partition-name", &buf) == 0) &&
0076         (stat("/sys/firmware/devicetree/base/hmc-managed?", &buf) == 0) &&
0077         (stat("/sys/firmware/devicetree/base/chosen/qemu,graphic-width", &buf) == -1) )
0078         return true;
0079 
0080     return false;
0081 }
0082 
0083 static inline int get_arch_reserved_keys(void)
0084 {
0085     if (sysconf(_SC_PAGESIZE) == 4096)
0086         return NR_RESERVED_PKEYS_4K;
0087     else
0088         if (arch_is_powervm())
0089             return NR_RESERVED_PKEYS_64K_4KEYS;
0090         else
0091             return NR_RESERVED_PKEYS_64K_3KEYS;
0092 }
0093 
0094 void expect_fault_on_read_execonly_key(void *p1, int pkey)
0095 {
0096     /*
0097      * powerpc does not allow userspace to change permissions of exec-only
0098      * keys since those keys are not allocated by userspace. The signal
0099      * handler wont be able to reset the permissions, which means the code
0100      * will infinitely continue to segfault here.
0101      */
0102     return;
0103 }
0104 
0105 /* 4-byte instructions * 16384 = 64K page */
0106 #define __page_o_noops() asm(".rept 16384 ; nop; .endr")
0107 
0108 void *malloc_pkey_with_mprotect_subpage(long size, int prot, u16 pkey)
0109 {
0110     void *ptr;
0111     int ret;
0112 
0113     dprintf1("doing %s(size=%ld, prot=0x%x, pkey=%d)\n", __func__,
0114             size, prot, pkey);
0115     pkey_assert(pkey < NR_PKEYS);
0116     ptr = mmap(NULL, size, prot, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
0117     pkey_assert(ptr != (void *)-1);
0118 
0119     ret = syscall(__NR_subpage_prot, ptr, size, NULL);
0120     if (ret) {
0121         perror("subpage_perm");
0122         return PTR_ERR_ENOTSUP;
0123     }
0124 
0125     ret = mprotect_pkey((void *)ptr, PAGE_SIZE, prot, pkey);
0126     pkey_assert(!ret);
0127     record_pkey_malloc(ptr, size, prot);
0128 
0129     dprintf1("%s() for pkey %d @ %p\n", __func__, pkey, ptr);
0130     return ptr;
0131 }
0132 
0133 #endif /* _PKEYS_POWERPC_H */