0001
0002
0003
0004
0005
0006
0007
0008 #include <asm/mman.h>
0009 #include <asm/mmu_context.h>
0010 #include <asm/mmu.h>
0011 #include <asm/setup.h>
0012 #include <asm/smp.h>
0013 #include <asm/firmware.h>
0014
0015 #include <linux/pkeys.h>
0016 #include <linux/of_fdt.h>
0017
0018
0019 int num_pkey;
0020
0021
0022
0023 u32 reserved_allocation_mask __ro_after_init;
0024
0025
0026 static u32 initial_allocation_mask __ro_after_init;
0027
0028
0029
0030
0031
0032 u64 default_amr __ro_after_init = ~0x0UL;
0033 u64 default_iamr __ro_after_init = 0x5555555555555555UL;
0034 u64 default_uamor __ro_after_init;
0035 EXPORT_SYMBOL(default_amr);
0036
0037
0038
0039
0040 static int execute_only_key = 2;
0041 static bool pkey_execute_disable_supported;
0042
0043
0044 #define AMR_BITS_PER_PKEY 2
0045 #define AMR_RD_BIT 0x1UL
0046 #define AMR_WR_BIT 0x2UL
0047 #define IAMR_EX_BIT 0x1UL
0048 #define PKEY_REG_BITS (sizeof(u64) * 8)
0049 #define pkeyshift(pkey) (PKEY_REG_BITS - ((pkey+1) * AMR_BITS_PER_PKEY))
0050
0051 static int __init dt_scan_storage_keys(unsigned long node,
0052 const char *uname, int depth,
0053 void *data)
0054 {
0055 const char *type = of_get_flat_dt_prop(node, "device_type", NULL);
0056 const __be32 *prop;
0057 int *pkeys_total = (int *) data;
0058
0059
0060 if (type == NULL || strcmp(type, "cpu") != 0)
0061 return 0;
0062
0063 prop = of_get_flat_dt_prop(node, "ibm,processor-storage-keys", NULL);
0064 if (!prop)
0065 return 0;
0066 *pkeys_total = be32_to_cpu(prop[0]);
0067 return 1;
0068 }
0069
0070 static int __init scan_pkey_feature(void)
0071 {
0072 int ret;
0073 int pkeys_total = 0;
0074
0075
0076
0077
0078 if (early_radix_enabled())
0079 return 0;
0080
0081 ret = of_scan_flat_dt(dt_scan_storage_keys, &pkeys_total);
0082 if (ret == 0) {
0083
0084
0085
0086
0087
0088 if (!firmware_has_feature(FW_FEATURE_LPAR)) {
0089 unsigned long pvr = mfspr(SPRN_PVR);
0090
0091 if (PVR_VER(pvr) == PVR_POWER8 || PVR_VER(pvr) == PVR_POWER8E ||
0092 PVR_VER(pvr) == PVR_POWER8NVL || PVR_VER(pvr) == PVR_POWER9)
0093 pkeys_total = 32;
0094 }
0095 }
0096
0097 #ifdef CONFIG_PPC_MEM_KEYS
0098
0099
0100
0101
0102 pkeys_total = min_t(int, pkeys_total,
0103 ((ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) + 1));
0104 #endif
0105 return pkeys_total;
0106 }
0107
0108 void __init pkey_early_init_devtree(void)
0109 {
0110 int pkeys_total, i;
0111
0112 #ifdef CONFIG_PPC_MEM_KEYS
0113
0114
0115
0116
0117
0118 BUILD_BUG_ON(PKEY_DISABLE_EXECUTE &
0119 (PKEY_DISABLE_ACCESS | PKEY_DISABLE_WRITE));
0120
0121
0122
0123
0124
0125 BUILD_BUG_ON(__builtin_clzl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT) +
0126 __builtin_popcountl(ARCH_VM_PKEY_FLAGS >> VM_PKEY_SHIFT)
0127 != (sizeof(u64) * BITS_PER_BYTE));
0128 #endif
0129
0130
0131
0132 if (!early_cpu_has_feature(CPU_FTR_ARCH_206))
0133 return;
0134
0135
0136 pkeys_total = scan_pkey_feature();
0137 if (!pkeys_total)
0138 goto out;
0139
0140
0141 default_uamor = ~0x0UL;
0142
0143 cur_cpu_spec->mmu_features |= MMU_FTR_PKEY;
0144
0145
0146
0147
0148
0149 if (pvr_version_is(PVR_POWER7) || pvr_version_is(PVR_POWER7p))
0150 pkey_execute_disable_supported = false;
0151 else
0152 pkey_execute_disable_supported = true;
0153
0154 #ifdef CONFIG_PPC_4K_PAGES
0155
0156
0157
0158
0159 num_pkey = min(8, pkeys_total);
0160 #else
0161 num_pkey = pkeys_total;
0162 #endif
0163
0164 if (unlikely(num_pkey <= execute_only_key) || !pkey_execute_disable_supported) {
0165
0166
0167
0168
0169 execute_only_key = -1;
0170 } else {
0171
0172
0173
0174
0175 reserved_allocation_mask |= (0x1 << execute_only_key);
0176
0177
0178
0179
0180
0181 default_amr |= (0x3ul << pkeyshift(execute_only_key));
0182 default_iamr &= ~(0x1ul << pkeyshift(execute_only_key));
0183
0184
0185
0186
0187 default_uamor &= ~(0x3ul << pkeyshift(execute_only_key));
0188 }
0189
0190 if (unlikely(num_pkey <= 3)) {
0191
0192
0193
0194
0195 disable_kuep = true;
0196 disable_kuap = true;
0197 WARN(1, "Disabling kernel user protection due to low (%d) max supported keys\n", num_pkey);
0198 } else {
0199
0200 reserved_allocation_mask |= (0x1 << 3);
0201
0202
0203
0204
0205
0206 default_amr &= ~(0x3ul << pkeyshift(3));
0207 default_iamr &= ~(0x1ul << pkeyshift(3));
0208 default_uamor &= ~(0x3ul << pkeyshift(3));
0209 }
0210
0211
0212
0213
0214 default_amr &= ~(0x3ul << pkeyshift(0));
0215 default_iamr &= ~(0x1ul << pkeyshift(0));
0216 default_uamor &= ~(0x3ul << pkeyshift(0));
0217
0218
0219
0220
0221
0222 initial_allocation_mask |= (0x1 << 0);
0223
0224
0225
0226
0227
0228 reserved_allocation_mask |= (0x1 << 1);
0229 default_uamor &= ~(0x3ul << pkeyshift(1));
0230
0231
0232
0233
0234
0235
0236 for (i = num_pkey; i < 32 ; i++) {
0237 reserved_allocation_mask |= (0x1 << i);
0238 default_uamor &= ~(0x3ul << pkeyshift(i));
0239 }
0240
0241
0242
0243 initial_allocation_mask |= reserved_allocation_mask;
0244
0245 pr_info("Enabling pkeys with max key count %d\n", num_pkey);
0246 out:
0247
0248
0249
0250 mtspr(SPRN_UAMOR, default_uamor);
0251
0252 return;
0253 }
0254
0255 #ifdef CONFIG_PPC_KUEP
0256 void setup_kuep(bool disabled)
0257 {
0258 if (disabled)
0259 return;
0260
0261
0262
0263 if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
0264 return;
0265
0266 if (smp_processor_id() == boot_cpuid) {
0267 pr_info("Activating Kernel Userspace Execution Prevention\n");
0268 cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUEP;
0269 }
0270
0271
0272
0273
0274
0275
0276 mtspr(SPRN_IAMR, AMR_KUEP_BLOCKED);
0277 isync();
0278 }
0279 #endif
0280
0281 #ifdef CONFIG_PPC_KUAP
0282 void setup_kuap(bool disabled)
0283 {
0284 if (disabled)
0285 return;
0286
0287
0288
0289 if (!early_radix_enabled() && !early_mmu_has_feature(MMU_FTR_PKEY))
0290 return;
0291
0292 if (smp_processor_id() == boot_cpuid) {
0293 pr_info("Activating Kernel Userspace Access Prevention\n");
0294 cur_cpu_spec->mmu_features |= MMU_FTR_BOOK3S_KUAP;
0295 }
0296
0297
0298
0299
0300 mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
0301 isync();
0302 }
0303 #endif
0304
0305 #ifdef CONFIG_PPC_MEM_KEYS
0306 void pkey_mm_init(struct mm_struct *mm)
0307 {
0308 if (!mmu_has_feature(MMU_FTR_PKEY))
0309 return;
0310 mm_pkey_allocation_map(mm) = initial_allocation_mask;
0311 mm->context.execute_only_pkey = execute_only_key;
0312 }
0313
0314 static inline void init_amr(int pkey, u8 init_bits)
0315 {
0316 u64 new_amr_bits = (((u64)init_bits & 0x3UL) << pkeyshift(pkey));
0317 u64 old_amr = current_thread_amr() & ~((u64)(0x3ul) << pkeyshift(pkey));
0318
0319 current->thread.regs->amr = old_amr | new_amr_bits;
0320 }
0321
0322 static inline void init_iamr(int pkey, u8 init_bits)
0323 {
0324 u64 new_iamr_bits = (((u64)init_bits & 0x1UL) << pkeyshift(pkey));
0325 u64 old_iamr = current_thread_iamr() & ~((u64)(0x1ul) << pkeyshift(pkey));
0326
0327 if (!likely(pkey_execute_disable_supported))
0328 return;
0329
0330 current->thread.regs->iamr = old_iamr | new_iamr_bits;
0331 }
0332
0333
0334
0335
0336
0337 int __arch_set_user_pkey_access(struct task_struct *tsk, int pkey,
0338 unsigned long init_val)
0339 {
0340 u64 new_amr_bits = 0x0ul;
0341 u64 new_iamr_bits = 0x0ul;
0342 u64 pkey_bits, uamor_pkey_bits;
0343
0344
0345
0346
0347 pkey_bits = 0x3ul << pkeyshift(pkey);
0348 uamor_pkey_bits = (default_uamor & pkey_bits);
0349
0350
0351
0352
0353 if (uamor_pkey_bits != pkey_bits)
0354 return -EINVAL;
0355
0356 if (init_val & PKEY_DISABLE_EXECUTE) {
0357 if (!pkey_execute_disable_supported)
0358 return -EINVAL;
0359 new_iamr_bits |= IAMR_EX_BIT;
0360 }
0361 init_iamr(pkey, new_iamr_bits);
0362
0363
0364 if (init_val & PKEY_DISABLE_ACCESS)
0365 new_amr_bits |= AMR_RD_BIT | AMR_WR_BIT;
0366 else if (init_val & PKEY_DISABLE_WRITE)
0367 new_amr_bits |= AMR_WR_BIT;
0368
0369 init_amr(pkey, new_amr_bits);
0370 return 0;
0371 }
0372
0373 int execute_only_pkey(struct mm_struct *mm)
0374 {
0375 return mm->context.execute_only_pkey;
0376 }
0377
0378 static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
0379 {
0380
0381 if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
0382 return false;
0383
0384 return (vma_pkey(vma) == vma->vm_mm->context.execute_only_pkey);
0385 }
0386
0387
0388
0389
0390 int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot,
0391 int pkey)
0392 {
0393
0394
0395
0396
0397 if (vma_is_pkey_exec_only(vma) && (prot != PROT_EXEC))
0398 return 0;
0399
0400
0401
0402
0403
0404 if (prot == PROT_EXEC) {
0405 pkey = execute_only_pkey(vma->vm_mm);
0406 if (pkey > 0)
0407 return pkey;
0408 }
0409
0410
0411 return vma_pkey(vma);
0412 }
0413
0414 static bool pkey_access_permitted(int pkey, bool write, bool execute)
0415 {
0416 int pkey_shift;
0417 u64 amr;
0418
0419 pkey_shift = pkeyshift(pkey);
0420 if (execute)
0421 return !(current_thread_iamr() & (IAMR_EX_BIT << pkey_shift));
0422
0423 amr = current_thread_amr();
0424 if (write)
0425 return !(amr & (AMR_WR_BIT << pkey_shift));
0426
0427 return !(amr & (AMR_RD_BIT << pkey_shift));
0428 }
0429
0430 bool arch_pte_access_permitted(u64 pte, bool write, bool execute)
0431 {
0432 if (!mmu_has_feature(MMU_FTR_PKEY))
0433 return true;
0434
0435 return pkey_access_permitted(pte_to_pkey_bits(pte), write, execute);
0436 }
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446 bool arch_vma_access_permitted(struct vm_area_struct *vma, bool write,
0447 bool execute, bool foreign)
0448 {
0449 if (!mmu_has_feature(MMU_FTR_PKEY))
0450 return true;
0451
0452
0453
0454 if (foreign || vma_is_foreign(vma))
0455 return true;
0456
0457 return pkey_access_permitted(vma_pkey(vma), write, execute);
0458 }
0459
0460 void arch_dup_pkeys(struct mm_struct *oldmm, struct mm_struct *mm)
0461 {
0462 if (!mmu_has_feature(MMU_FTR_PKEY))
0463 return;
0464
0465
0466 mm_pkey_allocation_map(mm) = mm_pkey_allocation_map(oldmm);
0467 mm->context.execute_only_pkey = oldmm->context.execute_only_pkey;
0468 }
0469
0470 #endif