0001
0002 #ifndef _ASM_POWERPC_KUP_BOOKE_H_
0003 #define _ASM_POWERPC_KUP_BOOKE_H_
0004
0005 #include <asm/bug.h>
0006
0007 #ifdef CONFIG_PPC_KUAP
0008
0009 #ifdef __ASSEMBLY__
0010
0011 .macro kuap_check_amr gpr1, gpr2
0012 .endm
0013
0014 #else
0015
0016 #include <linux/jump_label.h>
0017 #include <linux/sched.h>
0018
0019 #include <asm/reg.h>
0020
0021 extern struct static_key_false disable_kuap_key;
0022
0023 static __always_inline bool kuap_is_disabled(void)
0024 {
0025 return static_branch_unlikely(&disable_kuap_key);
0026 }
0027
0028 static inline void __kuap_lock(void)
0029 {
0030 mtspr(SPRN_PID, 0);
0031 isync();
0032 }
0033
0034 static inline void __kuap_save_and_lock(struct pt_regs *regs)
0035 {
0036 regs->kuap = mfspr(SPRN_PID);
0037 mtspr(SPRN_PID, 0);
0038 isync();
0039 }
0040
0041 static inline void kuap_user_restore(struct pt_regs *regs)
0042 {
0043 if (kuap_is_disabled())
0044 return;
0045
0046 mtspr(SPRN_PID, current->thread.pid);
0047
0048
0049 }
0050
0051 static inline void __kuap_kernel_restore(struct pt_regs *regs, unsigned long kuap)
0052 {
0053 if (regs->kuap)
0054 mtspr(SPRN_PID, current->thread.pid);
0055
0056
0057 }
0058
0059 static inline unsigned long __kuap_get_and_assert_locked(void)
0060 {
0061 unsigned long kuap = mfspr(SPRN_PID);
0062
0063 if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG))
0064 WARN_ON_ONCE(kuap);
0065
0066 return kuap;
0067 }
0068
0069 static inline void __allow_user_access(void __user *to, const void __user *from,
0070 unsigned long size, unsigned long dir)
0071 {
0072 mtspr(SPRN_PID, current->thread.pid);
0073 isync();
0074 }
0075
0076 static inline void __prevent_user_access(unsigned long dir)
0077 {
0078 mtspr(SPRN_PID, 0);
0079 isync();
0080 }
0081
0082 static inline unsigned long __prevent_user_access_return(void)
0083 {
0084 unsigned long flags = mfspr(SPRN_PID);
0085
0086 mtspr(SPRN_PID, 0);
0087 isync();
0088
0089 return flags;
0090 }
0091
0092 static inline void __restore_user_access(unsigned long flags)
0093 {
0094 if (flags) {
0095 mtspr(SPRN_PID, current->thread.pid);
0096 isync();
0097 }
0098 }
0099
0100 static inline bool
0101 __bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
0102 {
0103 return !regs->kuap;
0104 }
0105
0106 #endif
0107
0108 #endif
0109
0110 #endif