pkeys.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PowerPC Memory Protection Keys management
  4. *
  5. * Copyright 2017, Ram Pai, IBM Corporation.
  6. */
  7. #include <asm/mman.h>
  8. #include <asm/setup.h>
  9. #include <linux/pkeys.h>
  10. #include <linux/of_device.h>
  11. DEFINE_STATIC_KEY_TRUE(pkey_disabled);
  12. bool pkey_execute_disable_supported;
  13. int pkeys_total; /* Total pkeys as per device tree */
  14. bool pkeys_devtree_defined; /* pkey property exported by device tree */
  15. u32 initial_allocation_mask; /* Bits set for the initially allocated keys */
  16. u32 reserved_allocation_mask; /* Bits set for reserved keys */
  17. u64 pkey_amr_mask; /* Bits in AMR not to be touched */
  18. u64 pkey_iamr_mask; /* Bits in AMR not to be touched */
  19. u64 pkey_uamor_mask; /* Bits in UMOR not to be touched */
  20. int execute_only_key = 2;
  21. #define AMR_BITS_PER_PKEY 2
  22. #define AMR_RD_BIT 0x1UL
  23. #define AMR_WR_BIT 0x2UL
  24. #define IAMR_EX_BIT 0x1UL
  25. #define PKEY_REG_BITS (sizeof(u64)*8)
  26. #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
  27. static void scan_pkey_feature(void)
  28. {
  29. u32 vals[2];
  30. struct device_node *cpu;
  31. cpu = of_find_node_by_type(NULL, "cpu");
  32. if (!cpu)
  33. return;
  34. if (of_property_read_u32_array(cpu,
  35. "ibm,processor-storage-keys", vals, 2))
  36. return;
  37. /*
  38. * Since any pkey can be used for data or execute, we will just treat
  39. * all keys as equal and track them as one entity.
  40. */
  41. pkeys_total = vals[0];
  42. pkeys_devtree_defined = true;
  43. }
  44. static inline bool pkey_mmu_enabled(void)
  45. {
  46. if (firmware_has_feature(FW_FEATURE_LPAR))
  47. return pkeys_total;
  48. else
  49. return cpu_has_feature(CPU_FTR_PKEY);
  50. }
  51. int pkey_initialize(void)
  52. {
  53. int os_reserved, i;
  54. /*
  55. * We define PKEY_DISABLE_EXECUTE in addition to the arch-neutral
  56. * generic defines for PKEY_DISABLE_ACCESS and PKEY_DISABLE_WRITE.
  57. * Ensure that the bits a distinct.
  58. */
  59. BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
  60. (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
  61. /*
  62. * pkey_to_vmflag_bits() assumes that the pkey bits are contiguous
  63. * in the vmaflag. Make sure that is really the case.
  64. */
  65. BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
  66. __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
  67. != (sizeof(u64) * BITS_PER_BYTE));
  68. /* scan the device tree for pkey feature */
  69. scan_pkey_feature();
  70. /*
  71. * Let's assume 32 pkeys on P8 bare metal, if its not defined by device
  72. * tree. We make this exception since skiboot forgot to expose this
  73. * property on power8.
  74. */
  75. if (!pkeys_devtree_defined && !firmware_has_feature(FW_FEATURE_LPAR) &&
  76. cpu_has_feature(CPU_FTRS_POWER8))
  77. pkeys_total = 32;
  78. /*
  79. * Adjust the upper limit, based on the number of bits supported by
  80. * arch-neutral code.
  81. */
  82. pkeys_total = min_t(int, pkeys_total,
  83. ((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)+1));
  84. if (!pkey_mmu_enabled() || radix_enabled() || !pkeys_total)
  85. static_branch_enable(&pkey_disabled);
  86. else
  87. static_branch_disable(&pkey_disabled);
  88. if (static_branch_likely(&pkey_disabled))
  89. return 0;
  90. /*
  91. * The device tree cannot be relied to indicate support for
  92. * execute_disable support. Instead we use a PVR check.
  93. */
  94. if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
  95. pkey_execute_disable_supported = false;
  96. else
  97. pkey_execute_disable_supported = true;
  98. #ifdef CONFIG_PPC_4K_PAGES
  99. /*
  100. * The OS can manage only 8 pkeys due to its inability to represent them
  101. * in the Linux 4K PTE.
  102. */
  103. os_reserved = pkeys_total - 8;
  104. #else
  105. os_reserved = 0;
  106. #endif
  107. /* Bits are in LE format. */
  108. reserved_allocation_mask = (0x1 << 1) | (0x1 << execute_only_key);
  109. /* register mask is in BE format */
  110. pkey_amr_mask = ~0x0ul;
  111. pkey_amr_mask &= ~(0x3ul << pkeyshift(0));
  112. pkey_iamr_mask = ~0x0ul;
  113. pkey_iamr_mask &= ~(0x3ul << pkeyshift(0));
  114. pkey_iamr_mask &= ~(0x3ul << pkeyshift(execute_only_key));
  115. pkey_uamor_mask = ~0x0ul;
  116. pkey_uamor_mask &= ~(0x3ul << pkeyshift(0));
  117. pkey_uamor_mask &= ~(0x3ul << pkeyshift(execute_only_key));
  118. /* mark the rest of the keys as reserved and hence unavailable */
  119. for (i = (pkeys_total - os_reserved); i < pkeys_total; i++) {
  120. reserved_allocation_mask |= (0x1 << i);
  121. pkey_uamor_mask &= ~(0x3ul << pkeyshift(i));
  122. }
  123. initial_allocation_mask = reserved_allocation_mask | (0x1 << 0);
  124. if (unlikely((pkeys_total - os_reserved) <= execute_only_key)) {
  125. /*
  126. * Insufficient number of keys to support
  127. * execute only key. Mark it unavailable.
  128. * Any AMR, UAMOR, IAMR bit set for
  129. * this key is irrelevant since this key
  130. * can never be allocated.
  131. */
  132. execute_only_key = -1;
  133. }
  134. return 0;
  135. }
  136. arch_initcall(pkey_initialize);
  137. void pkey_mm_init(struct mm_struct *mm)
  138. {
  139. if (static_branch_likely(&pkey_disabled))
  140. return;
  141. mm_pkey_allocation_map(mm) = initial_allocation_mask;
  142. mm->context.execute_only_pkey = execute_only_key;
  143. }
  144. static inline u64 read_amr(void)
  145. {
  146. return mfspr(SPRN_AMR);
  147. }
  148. static inline void write_amr(u64 value)
  149. {
  150. mtspr(SPRN_AMR, value);
  151. }
  152. static inline u64 read_iamr(void)
  153. {
  154. if (!likely(pkey_execute_disable_supported))
  155. return 0x0UL;
  156. return mfspr(SPRN_IAMR);
  157. }
  158. static inline void write_iamr(u64 value)
  159. {
  160. if (!likely(pkey_execute_disable_supported))
  161. return;
  162. mtspr(SPRN_IAMR, value);
  163. }
  164. static inline u64 read_uamor(void)
  165. {
  166. return mfspr(SPRN_UAMOR);
  167. }
  168. static inline void write_uamor(u64 value)
  169. {
  170. mtspr(SPRN_UAMOR, value);
  171. }
  172. static bool is_pkey_enabled(int pkey)
  173. {
  174. u64 uamor = read_uamor();
  175. u64 pkey_bits = 0x3ul << pkeyshift(pkey);
  176. u64 uamor_pkey_bits = (uamor & pkey_bits);
  177. /*
  178. * Both the bits in UAMOR corresponding to the key should be set or
  179. * reset.
  180. */
  181. WARN_ON(uamor_pkey_bits && (uamor_pkey_bits != pkey_bits));
  182. return !!(uamor_pkey_bits);
  183. }
  184. static inline void init_amr(int pkey, u8 init_bits)
  185. {
  186. u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
  187. u64 old_amr = read_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
  188. write_amr(old_amr | new_amr_bits);
  189. }
  190. static inline void init_iamr(int pkey, u8 init_bits)
  191. {
  192. u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
  193. u64 old_iamr = read_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
  194. write_iamr(old_iamr | new_iamr_bits);
  195. }
  196. /*
  197. * Set the access rights in AMR IAMR and UAMOR registers for @pkey to that
  198. * specified in @init_val.
  199. */
  200. int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
  201. unsigned long init_val)
  202. {
  203. u64 new_amr_bits = 0x0ul;
  204. u64 new_iamr_bits = 0x0ul;
  205. if (!is_pkey_enabled(pkey))
  206. return -EINVAL;
  207. if (init_val & PKEY_DISABLE_EXECUTE) {
  208. if (!pkey_execute_disable_supported)
  209. return -EINVAL;
  210. new_iamr_bits |= IAMR_EX_BIT;
  211. }
  212. init_iamr(pkey, new_iamr_bits);
  213. /* Set the bits we need in AMR: */
  214. if (init_val & PKEY_DISABLE_ACCESS)
  215. new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
  216. else if (init_val & PKEY_DISABLE_WRITE)
  217. new_amr_bits |= AMR_WR_BIT;
  218. init_amr(pkey, new_amr_bits);
  219. return 0;
  220. }
  221. void thread_pkey_regs_save(struct thread_struct *thread)
  222. {
  223. if (static_branch_likely(&pkey_disabled))
  224. return;
  225. /*
  226. * TODO: Skip saving registers if @thread hasn't used any keys yet.
  227. */
  228. thread->amr = read_amr();
  229. thread->iamr = read_iamr();
  230. thread->uamor = read_uamor();
  231. }
  232. void thread_pkey_regs_restore(struct thread_struct *new_thread,
  233. struct thread_struct *old_thread)
  234. {
  235. if (static_branch_likely(&pkey_disabled))
  236. return;
  237. if (old_thread->amr != new_thread->amr)
  238. write_amr(new_thread->amr);
  239. if (old_thread->iamr != new_thread->iamr)
  240. write_iamr(new_thread->iamr);
  241. if (old_thread->uamor != new_thread->uamor)
  242. write_uamor(new_thread->uamor);
  243. }
  244. void thread_pkey_regs_init(struct thread_struct *thread)
  245. {
  246. if (static_branch_likely(&pkey_disabled))
  247. return;
  248. thread->amr = pkey_amr_mask;
  249. thread->iamr = pkey_iamr_mask;
  250. thread->uamor = pkey_uamor_mask;
  251. write_uamor(pkey_uamor_mask);
  252. write_amr(pkey_amr_mask);
  253. write_iamr(pkey_iamr_mask);
  254. }
  255. static inline bool pkey_allows_readwrite(int pkey)
  256. {
  257. int pkey_shift = pkeyshift(pkey);
  258. if (!is_pkey_enabled(pkey))
  259. return true;
  260. return !(read_amr() & ((AMR_RD_BIT|AMR_WR_BIT) << pkey_shift));
  261. }
  262. int __execute_only_pkey(struct mm_struct *mm)
  263. {
  264. return mm->context.execute_only_pkey;
  265. }
  266. static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
  267. {
  268. /* Do this check first since the vm_flags should be hot */
  269. if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC)
  270. return false;
  271. return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
  272. }
  273. /*
  274. * This should only be called for *plain* mprotect calls.
  275. */
  276. int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
  277. int pkey)
  278. {
  279. /*
  280. * If the currently associated pkey is execute-only, but the requested
  281. * protection is not execute-only, move it back to the default pkey.
  282. */
  283. if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
  284. return 0;
  285. /*
  286. * The requested protection is execute-only. Hence let's use an
  287. * execute-only pkey.
  288. */
  289. if (prot == PROT_EXEC) {
  290. pkey = execute_only_pkey(vma->vm_mm);
  291. if (pkey > 0)
  292. return pkey;
  293. }
  294. /* Nothing to override. */
  295. return vma_pkey(vma);
  296. }
  297. static bool pkey_access_permitted(int pkey, bool write, bool execute)
  298. {
  299. int pkey_shift;
  300. u64 amr;
  301. if (!is_pkey_enabled(pkey))
  302. return true;
  303. pkey_shift = pkeyshift(pkey);
  304. if (execute && !(read_iamr() & (IAMR_EX_BIT << pkey_shift)))
  305. return true;
  306. amr = read_amr(); /* Delay reading amr until absolutely needed */
  307. return ((!write && !(amr & (AMR_RD_BIT << pkey_shift))) ||
  308. (write && !(amr & (AMR_WR_BIT << pkey_shift))));
  309. }
  310. bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
  311. {
  312. if (static_branch_likely(&pkey_disabled))
  313. return true;
  314. return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
  315. }
  316. /*
  317. * We only want to enforce protection keys on the current thread because we
  318. * effectively have no access to AMR/IAMR for other threads or any way to tell
  319. * which AMR/IAMR in a threaded process we could use.
  320. *
  321. * So do not enforce things if the VMA is not from the current mm, or if we are
  322. * in a kernel thread.
  323. */
  324. static inline bool vma_is_foreign(struct vm_area_struct *vma)
  325. {
  326. if (!current->mm)
  327. return true;
  328. /* if it is not our ->mm, it has to be foreign */
  329. if (current->mm != vma->vm_mm)
  330. return true;
  331. return false;
  332. }
  333. bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
  334. bool execute, bool foreign)
  335. {
  336. if (static_branch_likely(&pkey_disabled))
  337. return true;
  338. /*
  339. * Do not enforce our key-permissions on a foreign vma.
  340. */
  341. if (foreign || vma_is_foreign(vma))
  342. return true;
  343. return pkey_access_permitted(vma_pkey(vma), write, execute);
  344. }