blob: ed4f5f536fc1d7da38f348fc5c0e1bf869e42294 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* 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#ifdef CONFIG_PPC_BOOK3S_64
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
27.macro kuap_restore_amr gpr
28.endm
29
30.macro kuap_check current, gpr
31.endm
32
33.macro kuap_check_amr gpr1, gpr2
34.endm
35
36#endif
37
38#else /* !__ASSEMBLY__ */
39
40#include <asm/pgtable.h>
41
42void setup_kup(void);
43
44#ifdef CONFIG_PPC_KUEP
45void setup_kuep(bool disabled);
46#else
47static inline void setup_kuep(bool disabled) { }
48#endif /* CONFIG_PPC_KUEP */
49
50#ifdef CONFIG_PPC_KUAP
51void setup_kuap(bool disabled);
52#else
53static inline void setup_kuap(bool disabled) { }
54
55static inline bool
56bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
57{
58 return false;
59}
60
61static 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
69static inline void allow_user_access(void __user *to, const void __user *from,
70 unsigned long size, unsigned long dir) { }
71static inline void prevent_user_access(void __user *to, const void __user *from,
72 unsigned long size, unsigned long dir) { }
73#endif /* CONFIG_PPC_BOOK3S_64 */
74#endif /* CONFIG_PPC_KUAP */
75
76static inline void allow_read_from_user(const void __user *from, unsigned long size)
77{
78 allow_user_access(NULL, from, size, KUAP_READ);
79}
80
81static inline void allow_write_to_user(void __user *to, unsigned long size)
82{
83 allow_user_access(to, NULL, size, KUAP_WRITE);
84}
85
86static 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);
90}
91
92static inline void prevent_read_from_user(const void __user *from, unsigned long size)
93{
94 prevent_user_access(NULL, from, size, KUAP_READ);
95}
96
97static inline void prevent_write_to_user(void __user *to, unsigned long size)
98{
99 prevent_user_access(to, NULL, size, KUAP_WRITE);
100}
101
102static 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);
106}
107
108#endif /* !__ASSEMBLY__ */
109
110#endif /* _ASM_POWERPC_KUAP_H_ */