1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_POWERPC_KUP_H_ 3 #define _ASM_POWERPC_KUP_H_ 4 5 #define KUAP_READ 1 6 #define KUAP_WRITE 2 7 #define KUAP_READ_WRITE (KUAP_READ | KUAP_WRITE) 8 9 #ifndef __ASSEMBLER__ 10 #include <linux/types.h> 11 12 static __always_inline bool kuap_is_disabled(void); 13 #endif 14 15 #ifdef CONFIG_PPC_BOOK3S_64 16 #include <asm/book3s/64/kup.h> 17 #endif 18 19 #ifdef CONFIG_PPC_8xx 20 #include <asm/nohash/32/kup-8xx.h> 21 #endif 22 23 #ifdef CONFIG_BOOKE 24 #include <asm/nohash/kup-booke.h> 25 #endif 26 27 #ifdef CONFIG_PPC_BOOK3S_32 28 #include <asm/book3s/32/kup.h> 29 #endif 30 31 #ifdef __ASSEMBLER__ 32 #ifndef CONFIG_PPC_KUAP 33 .macro kuap_check_amr gpr1, gpr2 34 .endm 35 36 #endif 37 38 #else /* !__ASSEMBLER__ */ 39 40 extern bool disable_kuep; 41 extern bool disable_kuap; 42 43 #include <linux/pgtable.h> 44 45 void setup_kup(void); 46 void setup_kuep(bool disabled); 47 48 #ifdef CONFIG_PPC_KUAP 49 void setup_kuap(bool disabled); 50 51 static __always_inline bool kuap_is_disabled(void) 52 { 53 return !mmu_has_feature(MMU_FTR_KUAP); 54 } 55 #else 56 static inline void setup_kuap(bool disabled) { } 57 58 static __always_inline bool kuap_is_disabled(void) { return true; } 59 60 static __always_inline bool 61 __bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write) 62 { 63 return false; 64 } 65 66 static __always_inline void kuap_user_restore(struct pt_regs *regs) { } 67 static __always_inline void __kuap_kernel_restore(struct pt_regs *regs, unsigned long amr) { } 68 69 /* 70 * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush 71 * the L1D cache after user accesses. Only include the empty stubs for other 72 * platforms. 73 */ 74 #ifndef CONFIG_PPC_BOOK3S_64 75 static __always_inline void allow_user_access(void __user *to, unsigned long dir) { } 76 static __always_inline void prevent_user_access(unsigned long dir) { } 77 static __always_inline unsigned long prevent_user_access_return(void) { return 0UL; } 78 static __always_inline void restore_user_access(unsigned long flags) { } 79 #endif /* CONFIG_PPC_BOOK3S_64 */ 80 #endif /* CONFIG_PPC_KUAP */ 81 82 static __always_inline bool 83 bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write) 84 { 85 if (kuap_is_disabled()) 86 return false; 87 88 return __bad_kuap_fault(regs, address, is_write); 89 } 90 91 static __always_inline void kuap_lock(void) 92 { 93 #ifdef __kuap_lock 94 if (kuap_is_disabled()) 95 return; 96 97 __kuap_lock(); 98 #endif 99 } 100 101 static __always_inline void kuap_save_and_lock(struct pt_regs *regs) 102 { 103 #ifdef __kuap_save_and_lock 104 if (kuap_is_disabled()) 105 return; 106 107 __kuap_save_and_lock(regs); 108 #endif 109 } 110 111 static __always_inline void kuap_kernel_restore(struct pt_regs *regs, unsigned long amr) 112 { 113 if (kuap_is_disabled()) 114 return; 115 116 __kuap_kernel_restore(regs, amr); 117 } 118 119 static __always_inline unsigned long kuap_get_and_assert_locked(void) 120 { 121 #ifdef __kuap_get_and_assert_locked 122 if (!kuap_is_disabled()) 123 return __kuap_get_and_assert_locked(); 124 #endif 125 return 0; 126 } 127 128 static __always_inline void kuap_assert_locked(void) 129 { 130 if (IS_ENABLED(CONFIG_PPC_KUAP_DEBUG)) 131 kuap_get_and_assert_locked(); 132 } 133 134 #endif /* !__ASSEMBLER__ */ 135 136 #endif /* _ASM_POWERPC_KUAP_H_ */ 137