pkey-powerpc.h 3.3 KB

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