Commit e0d8e991 authored by Aneesh Kumar K.V's avatar Aneesh Kumar K.V Committed by Michael Ellerman

powerpc/book3s64/kuap: Move UAMOR setup to key init function

UAMOR values are not application-specific. The kernel initializes
its value based on different reserved keys. Remove the thread-specific
UAMOR value and don't switch the UAMOR on context switch.

Move UAMOR initialization to key initialization code and remove
thread_struct.uamor because it is not used anymore.

Before commit: 4a4a5e5d ("powerpc/pkeys: key allocation/deallocation must not change pkey registers")
we used to update uamor based on key allocation and free.
Signed-off-by: default avatarAneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
Signed-off-by: default avatarMichael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20200709032946.881753-20-aneesh.kumar@linux.ibm.com
parent 000a42b3
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <asm/book3s/64/hash-pkey.h> #include <asm/book3s/64/hash-pkey.h>
extern u64 __ro_after_init default_uamor;
static inline u64 vmflag_to_pte_pkey_bits(u64 vm_flags) static inline u64 vmflag_to_pte_pkey_bits(u64 vm_flags)
{ {
if (!mmu_has_feature(MMU_FTR_PKEY)) if (!mmu_has_feature(MMU_FTR_PKEY))
......
...@@ -237,7 +237,6 @@ struct thread_struct { ...@@ -237,7 +237,6 @@ struct thread_struct {
#ifdef CONFIG_PPC_MEM_KEYS #ifdef CONFIG_PPC_MEM_KEYS
unsigned long amr; unsigned long amr;
unsigned long iamr; unsigned long iamr;
unsigned long uamor;
#endif #endif
#ifdef CONFIG_KVM_BOOK3S_32_HANDLER #ifdef CONFIG_KVM_BOOK3S_32_HANDLER
void* kvm_shadow_vcpu; /* KVM internal data */ void* kvm_shadow_vcpu; /* KVM internal data */
......
...@@ -488,14 +488,21 @@ static int pkey_active(struct task_struct *target, const struct user_regset *reg ...@@ -488,14 +488,21 @@ static int pkey_active(struct task_struct *target, const struct user_regset *reg
static int pkey_get(struct task_struct *target, const struct user_regset *regset, static int pkey_get(struct task_struct *target, const struct user_regset *regset,
unsigned int pos, unsigned int count, void *kbuf, void __user *ubuf) unsigned int pos, unsigned int count, void *kbuf, void __user *ubuf)
{ {
int ret;
BUILD_BUG_ON(TSO(amr) + sizeof(unsigned long) != TSO(iamr)); BUILD_BUG_ON(TSO(amr) + sizeof(unsigned long) != TSO(iamr));
BUILD_BUG_ON(TSO(iamr) + sizeof(unsigned long) != TSO(uamor));
if (!arch_pkeys_enabled()) if (!arch_pkeys_enabled())
return -ENODEV; return -ENODEV;
return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &target->thread.amr, ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &target->thread.amr,
0, ELF_NPKEY * sizeof(unsigned long)); 0, 2 * sizeof(unsigned long));
if (ret)
return ret;
ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &default_uamor,
2 * sizeof(unsigned long), 3 * sizeof(unsigned long));
return ret;
} }
static int pkey_set(struct task_struct *target, const struct user_regset *regset, static int pkey_set(struct task_struct *target, const struct user_regset *regset,
...@@ -517,9 +524,17 @@ static int pkey_set(struct task_struct *target, const struct user_regset *regset ...@@ -517,9 +524,17 @@ static int pkey_set(struct task_struct *target, const struct user_regset *regset
if (ret) if (ret)
return ret; return ret;
/* UAMOR determines which bits of the AMR can be set from userspace. */ /*
target->thread.amr = (new_amr & target->thread.uamor) | * UAMOR determines which bits of the AMR can be set from userspace.
(target->thread.amr & ~target->thread.uamor); * UAMOR value 0b11 indicates that the AMR value can be modified
* from userspace. If the kernel is using a specific key, we avoid
* userspace modifying the AMR value for that key by masking them
* via UAMOR 0b00.
*
* Pick the AMR values for the keys that kernel is using. This
* will be indicated by the ~default_uamor bits.
*/
target->thread.amr = (new_amr & default_uamor) | (target->thread.amr & ~default_uamor);
return 0; return 0;
} }
......
...@@ -59,6 +59,7 @@ ...@@ -59,6 +59,7 @@
#include <asm/asm-prototypes.h> #include <asm/asm-prototypes.h>
#include <asm/cpu_has_feature.h> #include <asm/cpu_has_feature.h>
#include <asm/ftrace.h> #include <asm/ftrace.h>
#include <asm/kup.h>
#ifdef DEBUG #ifdef DEBUG
#include <asm/udbg.h> #include <asm/udbg.h>
......
...@@ -1110,6 +1110,10 @@ void hash__early_init_mmu_secondary(void) ...@@ -1110,6 +1110,10 @@ void hash__early_init_mmu_secondary(void)
if (cpu_has_feature(CPU_FTR_ARCH_206) if (cpu_has_feature(CPU_FTR_ARCH_206)
&& cpu_has_feature(CPU_FTR_HVMODE)) && cpu_has_feature(CPU_FTR_HVMODE))
tlbiel_all(); tlbiel_all();
#ifdef CONFIG_PPC_MEM_KEYS
mtspr(SPRN_UAMOR, default_uamor);
#endif
} }
#endif /* CONFIG_SMP */ #endif /* CONFIG_SMP */
......
...@@ -27,9 +27,7 @@ static u32 initial_allocation_mask __ro_after_init; ...@@ -27,9 +27,7 @@ static u32 initial_allocation_mask __ro_after_init;
*/ */
static u64 default_amr = ~0x0UL; static u64 default_amr = ~0x0UL;
static u64 default_iamr = 0x5555555555555555UL; static u64 default_iamr = 0x5555555555555555UL;
u64 default_uamor __ro_after_init;
/* Allow all keys to be modified by default */
static u64 default_uamor = ~0x0UL;
/* /*
* Key used to implement PROT_EXEC mmap. Denies READ/WRITE * Key used to implement PROT_EXEC mmap. Denies READ/WRITE
* We pick key 2 because 0 is special key and 1 is reserved as per ISA. * We pick key 2 because 0 is special key and 1 is reserved as per ISA.
...@@ -122,10 +120,11 @@ void __init pkey_early_init_devtree(void) ...@@ -122,10 +120,11 @@ void __init pkey_early_init_devtree(void)
/* scan the device tree for pkey feature */ /* scan the device tree for pkey feature */
pkeys_total = scan_pkey_feature(); pkeys_total = scan_pkey_feature();
if (!pkeys_total) { if (!pkeys_total)
/* No support for pkey. Mark it disabled */ goto out;
return;
} /* Allow all keys to be modified by default */
default_uamor = ~0x0UL;
cur_cpu_spec->mmu_features |= MMU_FTR_PKEY; cur_cpu_spec->mmu_features |= MMU_FTR_PKEY;
...@@ -209,6 +208,12 @@ void __init pkey_early_init_devtree(void) ...@@ -209,6 +208,12 @@ void __init pkey_early_init_devtree(void)
initial_allocation_mask |= reserved_allocation_mask; initial_allocation_mask |= reserved_allocation_mask;
pr_info("Enabling pkeys with max key count %d\n", num_pkey); pr_info("Enabling pkeys with max key count %d\n", num_pkey);
out:
/*
* Setup uamor on boot cpu
*/
mtspr(SPRN_UAMOR, default_uamor);
return; return;
} }
...@@ -251,11 +256,6 @@ static inline u64 read_uamor(void) ...@@ -251,11 +256,6 @@ static inline u64 read_uamor(void)
return mfspr(SPRN_UAMOR); return mfspr(SPRN_UAMOR);
} }
static inline void write_uamor(u64 value)
{
mtspr(SPRN_UAMOR, value);
}
static bool is_pkey_enabled(int pkey) static bool is_pkey_enabled(int pkey)
{ {
u64 uamor = read_uamor(); u64 uamor = read_uamor();
...@@ -326,7 +326,6 @@ void thread_pkey_regs_save(struct thread_struct *thread) ...@@ -326,7 +326,6 @@ void thread_pkey_regs_save(struct thread_struct *thread)
*/ */
thread->amr = read_amr(); thread->amr = read_amr();
thread->iamr = read_iamr(); thread->iamr = read_iamr();
thread->uamor = read_uamor();
} }
void thread_pkey_regs_restore(struct thread_struct *new_thread, void thread_pkey_regs_restore(struct thread_struct *new_thread,
...@@ -339,8 +338,6 @@ void thread_pkey_regs_restore(struct thread_struct *new_thread, ...@@ -339,8 +338,6 @@ void thread_pkey_regs_restore(struct thread_struct *new_thread,
write_amr(new_thread->amr); write_amr(new_thread->amr);
if (old_thread->iamr != new_thread->iamr) if (old_thread->iamr != new_thread->iamr)
write_iamr(new_thread->iamr); write_iamr(new_thread->iamr);
if (old_thread->uamor != new_thread->uamor)
write_uamor(new_thread->uamor);
} }
void thread_pkey_regs_init(struct thread_struct *thread) void thread_pkey_regs_init(struct thread_struct *thread)
...@@ -350,11 +347,9 @@ void thread_pkey_regs_init(struct thread_struct *thread) ...@@ -350,11 +347,9 @@ void thread_pkey_regs_init(struct thread_struct *thread)
thread->amr = default_amr; thread->amr = default_amr;
thread->iamr = default_iamr; thread->iamr = default_iamr;
thread->uamor = default_uamor;
write_amr(default_amr); write_amr(default_amr);
write_iamr(default_iamr); write_iamr(default_iamr);
write_uamor(default_uamor);
} }
int execute_only_pkey(struct mm_struct *mm) int execute_only_pkey(struct mm_struct *mm)
......
...@@ -620,6 +620,10 @@ void setup_kuap(bool disabled) ...@@ -620,6 +620,10 @@ void setup_kuap(bool disabled)
/* Make sure userspace can't change the AMR */ /* Make sure userspace can't change the AMR */
mtspr(SPRN_UAMOR, 0); mtspr(SPRN_UAMOR, 0);
/*
* Set the default kernel AMR values on all cpus.
*/
mtspr(SPRN_AMR, AMR_KUAP_BLOCKED); mtspr(SPRN_AMR, AMR_KUAP_BLOCKED);
isync(); isync();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment