David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _ASM_POWERPC_KUP_H_ |
| 3 | #define _ASM_POWERPC_KUP_H_ |
| 4 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5 | #define KUAP_READ 1 |
| 6 | #define KUAP_WRITE 2 |
| 7 | #define KUAP_READ_WRITE (KUAP_READ | KUAP_WRITE) |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 8 | /* |
| 9 | * For prevent_user_access() only. |
| 10 | * Use the current saved situation instead of the to/from/size params. |
| 11 | * Used on book3s/32 |
| 12 | */ |
| 13 | #define KUAP_CURRENT_READ 4 |
| 14 | #define KUAP_CURRENT_WRITE 8 |
| 15 | #define KUAP_CURRENT (KUAP_CURRENT_READ | KUAP_CURRENT_WRITE) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 16 | |
| 17 | #ifdef CONFIG_PPC_BOOK3S_64 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 18 | #include <asm/book3s/64/kup-radix.h> |
| 19 | #endif |
| 20 | #ifdef CONFIG_PPC_8xx |
| 21 | #include <asm/nohash/32/kup-8xx.h> |
| 22 | #endif |
| 23 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 24 | #include <asm/book3s/32/kup.h> |
| 25 | #endif |
| 26 | |
| 27 | #ifdef __ASSEMBLY__ |
| 28 | #ifndef CONFIG_PPC_KUAP |
| 29 | .macro kuap_save_and_lock sp, thread, gpr1, gpr2, gpr3 |
| 30 | .endm |
| 31 | |
| 32 | .macro kuap_restore sp, current, gpr1, gpr2, gpr3 |
| 33 | .endm |
| 34 | |
| 35 | .macro kuap_check current, gpr |
| 36 | .endm |
| 37 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 38 | .macro kuap_check_amr gpr1, gpr2 |
| 39 | .endm |
| 40 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #else /* !__ASSEMBLY__ */ |
| 44 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 45 | #include <linux/pgtable.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 46 | |
| 47 | void setup_kup(void); |
| 48 | |
| 49 | #ifdef CONFIG_PPC_KUEP |
| 50 | void setup_kuep(bool disabled); |
| 51 | #else |
| 52 | static inline void setup_kuep(bool disabled) { } |
| 53 | #endif /* CONFIG_PPC_KUEP */ |
| 54 | |
| 55 | #ifdef CONFIG_PPC_KUAP |
| 56 | void setup_kuap(bool disabled); |
| 57 | #else |
| 58 | static inline void setup_kuap(bool disabled) { } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 59 | |
| 60 | static inline bool |
| 61 | bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write) |
| 62 | { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | static inline void kuap_check_amr(void) { } |
| 67 | |
| 68 | /* |
| 69 | * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush |
| 70 | * the L1D cache after user accesses. Only include the empty stubs for other |
| 71 | * platforms. |
| 72 | */ |
| 73 | #ifndef CONFIG_PPC_BOOK3S_64 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 74 | static inline void allow_user_access(void __user *to, const void __user *from, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 75 | unsigned long size, unsigned long dir) { } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 76 | static inline void prevent_user_access(void __user *to, const void __user *from, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 77 | unsigned long size, unsigned long dir) { } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 78 | static inline unsigned long prevent_user_access_return(void) { return 0UL; } |
| 79 | static inline void restore_user_access(unsigned long flags) { } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 80 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 81 | #endif /* CONFIG_PPC_KUAP */ |
| 82 | |
| 83 | static inline void allow_read_from_user(const void __user *from, unsigned long size) |
| 84 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 85 | allow_user_access(NULL, from, size, KUAP_READ); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static inline void allow_write_to_user(void __user *to, unsigned long size) |
| 89 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 90 | allow_user_access(to, NULL, size, KUAP_WRITE); |
| 91 | } |
| 92 | |
| 93 | static inline void allow_read_write_user(void __user *to, const void __user *from, |
| 94 | unsigned long size) |
| 95 | { |
| 96 | allow_user_access(to, from, size, KUAP_READ_WRITE); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static inline void prevent_read_from_user(const void __user *from, unsigned long size) |
| 100 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 101 | prevent_user_access(NULL, from, size, KUAP_READ); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static inline void prevent_write_to_user(void __user *to, unsigned long size) |
| 105 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 106 | prevent_user_access(to, NULL, size, KUAP_WRITE); |
| 107 | } |
| 108 | |
| 109 | static inline void prevent_read_write_user(void __user *to, const void __user *from, |
| 110 | unsigned long size) |
| 111 | { |
| 112 | prevent_user_access(to, from, size, KUAP_READ_WRITE); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 115 | static inline void prevent_current_access_user(void) |
| 116 | { |
| 117 | prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT); |
| 118 | } |
| 119 | |
| 120 | static inline void prevent_current_read_from_user(void) |
| 121 | { |
| 122 | prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_READ); |
| 123 | } |
| 124 | |
| 125 | static inline void prevent_current_write_to_user(void) |
| 126 | { |
| 127 | prevent_user_access(NULL, NULL, ~0UL, KUAP_CURRENT_WRITE); |
| 128 | } |
| 129 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 130 | #endif /* !__ASSEMBLY__ */ |
| 131 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 132 | #endif /* _ASM_POWERPC_KUAP_H_ */ |