Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_POWERPC_KUP_8XX_H_
3#define _ASM_POWERPC_KUP_8XX_H_
4
5#include <asm/bug.h>
6#include <asm/mmu.h>
7
8#ifdef CONFIG_PPC_KUAP
9
10#ifndef __ASSEMBLER__
11
12#include <asm/reg.h>
13
14static __always_inline void __kuap_save_and_lock(struct pt_regs *regs)
15{
16 regs->kuap = mfspr(SPRN_MD_AP);
17 mtspr(SPRN_MD_AP, MD_APG_KUAP);
18}
19#define __kuap_save_and_lock __kuap_save_and_lock
20
21static __always_inline void kuap_user_restore(struct pt_regs *regs)
22{
23}
24
25static __always_inline void __kuap_kernel_restore(struct pt_regs *regs, unsigned long kuap)
26{
27 mtspr(SPRN_MD_AP, regs->kuap);
28}
29
30#ifdef CONFIG_PPC_KUAP_DEBUG
31static __always_inline unsigned long __kuap_get_and_assert_locked(void)
32{
33 WARN_ON_ONCE(mfspr(SPRN_MD_AP) >> 16 != MD_APG_KUAP >> 16);
34
35 return 0;
36}
37#define __kuap_get_and_assert_locked __kuap_get_and_assert_locked
38#endif
39
40static __always_inline void uaccess_begin_8xx(unsigned long val)
41{
42 asm(ASM_MMU_FTR_IFSET("mtspr %0, %1", "", %2) : :
43 "i"(SPRN_MD_AP), "r"(val), "i"(MMU_FTR_KUAP) : "memory");
44}
45
46static __always_inline void uaccess_end_8xx(void)
47{
48 asm(ASM_MMU_FTR_IFSET("mtspr %0, %1", "", %2) : :
49 "i"(SPRN_MD_AP), "r"(MD_APG_KUAP), "i"(MMU_FTR_KUAP) : "memory");
50}
51
52static __always_inline void allow_user_access(void __user *to, unsigned long dir)
53{
54 uaccess_begin_8xx(MD_APG_INIT);
55}
56
57static __always_inline void prevent_user_access(unsigned long dir)
58{
59 uaccess_end_8xx();
60}
61
62static __always_inline unsigned long prevent_user_access_return(void)
63{
64 unsigned long flags;
65
66 flags = mfspr(SPRN_MD_AP);
67
68 uaccess_end_8xx();
69
70 return flags;
71}
72
73static __always_inline void restore_user_access(unsigned long flags)
74{
75 uaccess_begin_8xx(flags);
76}
77
78static __always_inline bool
79__bad_kuap_fault(struct pt_regs *regs, unsigned long address, bool is_write)
80{
81 return !((regs->kuap ^ MD_APG_KUAP) & 0xff000000);
82}
83
84#endif /* !__ASSEMBLER__ */
85
86#endif /* CONFIG_PPC_KUAP */
87
88#endif /* _ASM_POWERPC_KUP_8XX_H_ */