/
hkip.h
149 lines (122 loc) · 3.35 KB
/
hkip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2017-2021. All rights reserved.
* Description: HKIP protect hooks
* Date: 2017/08/15
*/
#ifndef __HKIP_H__
#define __HKIP_H__
#include <asm/alternative.h>
#include <asm/cpufeature.h>
#include <linux/sched.h>
#include <linux/threads.h>
#include <linux/types.h>
#include <linux/hisi/hkip_hhee.h>
#include <linux/hisi/hhee_prmem.h>
static inline bool hhee_is_present(void)
{
bool ret = false;
#ifdef CONFIG_HHEE
if (hhee_check_enable() == HHEE_ENABLE)
ret = true;
#endif
return ret;
}
u32 hkip_hvc2(u32, unsigned long);
u32 hkip_hvc3(u32, unsigned long, unsigned long);
u32 hkip_hvc4(u32, unsigned long, unsigned long, unsigned long);
static inline bool hkip_get_bit(const u8 *bits, size_t pos, size_t max)
{
if (unlikely(pos >= max))
return false;
return ((unsigned int)READ_ONCE(bits[pos / 8]) >> (pos % 8)) & 1;
}
static inline void hkip_set_bit(u8 *bits, size_t pos, size_t max, bool value)
{
if (hkip_get_bit(bits, pos, max) == value)
return;
if (unlikely(hkip_hvc4(HKIP_HVC_ROWM_SET_BIT, (unsigned long)(uintptr_t)bits,
pos, value)))
BUG();
}
static inline bool __hkip_get_task_bit(const u8 *bits,
struct task_struct *task)
{
return hkip_get_bit(bits, task_pid_nr(task), PID_MAX_DEFAULT);
}
static inline bool hkip_get_task_bit(const u8 *bits, struct task_struct *task,
bool def_value)
{
pid_t pid = task_pid_nr(task);
if (pid != 0)
return hkip_get_bit(bits, pid, PID_MAX_DEFAULT);
return def_value;
}
static inline void hkip_set_task_bit(u8 *bits, struct task_struct *task,
bool value)
{
pid_t pid = task_pid_nr(task);
if (pid != 0)
hkip_set_bit(bits, pid, PID_MAX_DEFAULT, value);
}
static inline bool __hkip_get_current_bit(const u8 *bits)
{
return __hkip_get_task_bit(bits, current);
}
static inline bool hkip_get_current_bit(const u8 *bits, bool def_value)
{
return hkip_get_task_bit(bits, current, def_value);
}
static inline void hkip_set_current_bit(u8 *bits, bool value)
{
hkip_set_task_bit(bits, current, value);
}
struct cred;
#ifdef CONFIG_UEFI_HHEE
int hkip_register_rox_text(const void *base, size_t size);
#else
static inline int hkip_register_rox_text(const void *base, size_t size)
{
return 0;
}
#endif
#if defined(CONFIG_HHEE) && !defined(CONFIG_UEFI_HHEE)
int hkip_register_xo(const uintptr_t base, size_t size);
extern u8 hkip_addr_limit_bits[];
#define hkip_is_kernel_fs() \
((current_thread_info()->addr_limit == KERNEL_DS) \
&& hkip_get_current_bit(hkip_addr_limit_bits, true))
#define hkip_get_fs() \
(hkip_is_kernel_fs() ? KERNEL_DS : USER_DS)
#define hkip_set_fs(fs) \
hkip_set_current_bit(hkip_addr_limit_bits, (fs) == KERNEL_DS)
#ifndef CONFIG_HKIP_PROTECT_CRED
int hkip_check_uid_root(void);
int hkip_check_gid_root(void);
int hkip_check_xid_root(void);
void hkip_update_xid_root(const struct cred *creds);
#endif
void hkip_init_task(struct task_struct *task);
#else
#define hkip_set_fs(fs) ((void)(fs))
#ifndef CONFIG_HKIP_PROTECT_CRED
static inline int hkip_check_uid_root(void)
{
return 0;
}
static inline int hkip_check_gid_root(void)
{
return 0;
}
static inline int hkip_check_xid_root(void)
{
return 0;
}
static inline void hkip_update_xid_root(const struct cred *creds) { }
#endif
static inline void hkip_init_task(struct task_struct *task) { }
static inline int hkip_register_xo(const uintptr_t base, size_t size)
{
return 0;
}
#endif
#endif /* __HKIP_H__ */