pkeys.c 11 KB

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