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) |
| 8 | |
| 9 | #ifdef CONFIG_PPC_BOOK3S_64 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10 | #include <asm/book3s/64/kup-radix.h> |
| 11 | #endif |
| 12 | #ifdef CONFIG_PPC_8xx |
| 13 | #include <asm/nohash/32/kup-8xx.h> |
| 14 | #endif |
| 15 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 16 | #include <asm/book3s/32/kup.h> |
| 17 | #endif |
| 18 | |
| 19 | #ifdef __ASSEMBLY__ |
| 20 | #ifndef CONFIG_PPC_KUAP |
| 21 | .macro kuap_save_and_lock sp, thread, gpr1, gpr2, gpr3 |
| 22 | .endm |
| 23 | |
| 24 | .macro kuap_restore sp, current, gpr1, gpr2, gpr3 |
| 25 | .endm |
| 26 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 27 | .macro kuap_restore_amr gpr |
| 28 | .endm |
| 29 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 30 | .macro kuap_check current, gpr |
| 31 | .endm |
| 32 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 33 | .macro kuap_check_amr gpr1, gpr2 |
| 34 | .endm |
| 35 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | #else /* !__ASSEMBLY__ */ |
| 39 | |
| 40 | #include <asm/pgtable.h> |
| 41 | |
| 42 | void setup_kup(void); |
| 43 | |
| 44 | #ifdef CONFIG_PPC_KUEP |
| 45 | void setup_kuep(bool disabled); |
| 46 | #else |
| 47 | static inline void setup_kuep(bool disabled) { } |
| 48 | #endif /* CONFIG_PPC_KUEP */ |
| 49 | |
| 50 | #ifdef CONFIG_PPC_KUAP |
| 51 | void setup_kuap(bool disabled); |
| 52 | #else |
| 53 | static inline void setup_kuap(bool disabled) { } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 54 | |
| 55 | static inline bool |
| 56 | bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write) |
| 57 | { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | static inline void kuap_check_amr(void) { } |
| 62 | |
| 63 | /* |
| 64 | * book3s/64/kup-radix.h defines these functions for the !KUAP case to flush |
| 65 | * the L1D cache after user accesses. Only include the empty stubs for other |
| 66 | * platforms. |
| 67 | */ |
| 68 | #ifndef CONFIG_PPC_BOOK3S_64 |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 69 | 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^] | 70 | unsigned long size, unsigned long dir) { } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 71 | 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^] | 72 | unsigned long size, unsigned long dir) { } |
| 73 | #endif /* CONFIG_PPC_BOOK3S_64 */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 74 | #endif /* CONFIG_PPC_KUAP */ |
| 75 | |
| 76 | static inline void allow_read_from_user(const void __user *from, unsigned long size) |
| 77 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 78 | allow_user_access(NULL, from, size, KUAP_READ); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static inline void allow_write_to_user(void __user *to, unsigned long size) |
| 82 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 83 | allow_user_access(to, NULL, size, KUAP_WRITE); |
| 84 | } |
| 85 | |
| 86 | static inline void allow_read_write_user(void __user *to, const void __user *from, |
| 87 | unsigned long size) |
| 88 | { |
| 89 | allow_user_access(to, from, size, KUAP_READ_WRITE); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static inline void prevent_read_from_user(const void __user *from, unsigned long size) |
| 93 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 94 | prevent_user_access(NULL, from, size, KUAP_READ); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static inline void prevent_write_to_user(void __user *to, unsigned long size) |
| 98 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 99 | prevent_user_access(to, NULL, size, KUAP_WRITE); |
| 100 | } |
| 101 | |
| 102 | static inline void prevent_read_write_user(void __user *to, const void __user *from, |
| 103 | unsigned long size) |
| 104 | { |
| 105 | prevent_user_access(to, from, size, KUAP_READ_WRITE); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | #endif /* !__ASSEMBLY__ */ |
| 109 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 110 | #endif /* _ASM_POWERPC_KUAP_H_ */ |