blob: ed4f5f536fc1d7da38f348fc5c0e1bf869e42294 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_POWERPC_KUP_H_
3#define _ASM_POWERPC_KUP_H_
4
Olivier Deprez0e641232021-09-23 10:07:05 +02005#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 Brazdil0f672f62019-12-10 10:32:29 +000010#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 Deprez0e641232021-09-23 10:07:05 +020027.macro kuap_restore_amr gpr
28.endm
29
David Brazdil0f672f62019-12-10 10:32:29 +000030.macro kuap_check current, gpr
31.endm
32
Olivier Deprez0e641232021-09-23 10:07:05 +020033.macro kuap_check_amr gpr1, gpr2
34.endm
35
David Brazdil0f672f62019-12-10 10:32:29 +000036#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) { }
Olivier Deprez0e641232021-09-23 10:07:05 +020054
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
David Brazdil0f672f62019-12-10 10:32:29 +000069static inline void allow_user_access(void __user *to, const void __user *from,
Olivier Deprez0e641232021-09-23 10:07:05 +020070 unsigned long size, unsigned long dir) { }
David Brazdil0f672f62019-12-10 10:32:29 +000071static inline void prevent_user_access(void __user *to, const void __user *from,
Olivier Deprez0e641232021-09-23 10:07:05 +020072 unsigned long size, unsigned long dir) { }
73#endif /* CONFIG_PPC_BOOK3S_64 */
David Brazdil0f672f62019-12-10 10:32:29 +000074#endif /* CONFIG_PPC_KUAP */
75
76static inline void allow_read_from_user(const void __user *from, unsigned long size)
77{
Olivier Deprez0e641232021-09-23 10:07:05 +020078 allow_user_access(NULL, from, size, KUAP_READ);
David Brazdil0f672f62019-12-10 10:32:29 +000079}
80
81static inline void allow_write_to_user(void __user *to, unsigned long size)
82{
Olivier Deprez0e641232021-09-23 10:07:05 +020083 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);
David Brazdil0f672f62019-12-10 10:32:29 +000090}
91
92static inline void prevent_read_from_user(const void __user *from, unsigned long size)
93{
Olivier Deprez0e641232021-09-23 10:07:05 +020094 prevent_user_access(NULL, from, size, KUAP_READ);
David Brazdil0f672f62019-12-10 10:32:29 +000095}
96
97static inline void prevent_write_to_user(void __user *to, unsigned long size)
98{
Olivier Deprez0e641232021-09-23 10:07:05 +020099 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);
David Brazdil0f672f62019-12-10 10:32:29 +0000106}
107
108#endif /* !__ASSEMBLY__ */
109
Olivier Deprez0e641232021-09-23 10:07:05 +0200110#endif /* _ASM_POWERPC_KUAP_H_ */