1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * Copyright (C) 2012,2013 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
5 *
6 * Derived from arch/arm/kvm/coproc.h
7 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
8 * Authors: Christoffer Dall <c.dall@virtualopensystems.com>
9 */
10
11 #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__
12 #define __ARM64_KVM_SYS_REGS_LOCAL_H__
13
14 #include <linux/bsearch.h>
15
16 #define reg_to_encoding(x) \
17 sys_reg((u32)(x)->Op0, (u32)(x)->Op1, \
18 (u32)(x)->CRn, (u32)(x)->CRm, (u32)(x)->Op2)
19
20 struct sys_reg_params {
21 u8 Op0;
22 u8 Op1;
23 u8 CRn;
24 u8 CRm;
25 u8 Op2;
26 u64 regval;
27 bool is_write;
28 };
29
30 #define esr_sys64_to_params(esr) \
31 ((struct sys_reg_params){ .Op0 = ((esr) >> 20) & 3, \
32 .Op1 = ((esr) >> 14) & 0x7, \
33 .CRn = ((esr) >> 10) & 0xf, \
34 .CRm = ((esr) >> 1) & 0xf, \
35 .Op2 = ((esr) >> 17) & 0x7, \
36 .is_write = !((esr) & 1) })
37
38 #define esr_cp1x_32_to_params(esr) \
39 ((struct sys_reg_params){ .Op1 = ((esr) >> 14) & 0x7, \
40 .CRn = ((esr) >> 10) & 0xf, \
41 .CRm = ((esr) >> 1) & 0xf, \
42 .Op2 = ((esr) >> 17) & 0x7, \
43 .is_write = !((esr) & 1) })
44
45 struct sys_reg_desc {
46 /* Sysreg string for debug */
47 const char *name;
48
49 enum {
50 AA32_DIRECT,
51 AA32_LO,
52 AA32_HI,
53 } aarch32_map;
54
55 /* MRS/MSR instruction which accesses it. */
56 u8 Op0;
57 u8 Op1;
58 u8 CRn;
59 u8 CRm;
60 u8 Op2;
61
62 /* Trapped access from guest, if non-NULL. */
63 bool (*access)(struct kvm_vcpu *,
64 struct sys_reg_params *,
65 const struct sys_reg_desc *);
66
67 /* Initialization for vcpu. */
68 void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
69
70 /* Index into sys_reg[], or 0 if we don't need to save it. */
71 int reg;
72
73 /* Value (usually reset value) */
74 u64 val;
75
76 /* Custom get/set_user functions, fallback to generic if NULL */
77 int (*get_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
78 u64 *val);
79 int (*set_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
80 u64 val);
81
82 /* Return mask of REG_* runtime visibility overrides */
83 unsigned int (*visibility)(const struct kvm_vcpu *vcpu,
84 const struct sys_reg_desc *rd);
85 };
86
87 #define REG_HIDDEN (1 << 0) /* hidden from userspace and guest */
88 #define REG_HIDDEN_USER (1 << 1) /* hidden from userspace only */
89 #define REG_RAZ (1 << 2) /* RAZ from userspace and guest */
90 #define REG_USER_WI (1 << 3) /* WI from userspace only */
91
92 static __printf(2, 3)
print_sys_reg_msg(const struct sys_reg_params * p,char * fmt,...)93 inline void print_sys_reg_msg(const struct sys_reg_params *p,
94 char *fmt, ...)
95 {
96 va_list va;
97
98 va_start(va, fmt);
99 /* Look, we even formatted it for you to paste into the table! */
100 kvm_pr_unimpl("%pV { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n",
101 &(struct va_format){ fmt, &va },
102 p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read");
103 va_end(va);
104 }
105
print_sys_reg_instr(const struct sys_reg_params * p)106 static inline void print_sys_reg_instr(const struct sys_reg_params *p)
107 {
108 /* GCC warns on an empty format string */
109 print_sys_reg_msg(p, "%s", "");
110 }
111
ignore_write(struct kvm_vcpu * vcpu,const struct sys_reg_params * p)112 static inline bool ignore_write(struct kvm_vcpu *vcpu,
113 const struct sys_reg_params *p)
114 {
115 return true;
116 }
117
read_zero(struct kvm_vcpu * vcpu,struct sys_reg_params * p)118 static inline bool read_zero(struct kvm_vcpu *vcpu,
119 struct sys_reg_params *p)
120 {
121 p->regval = 0;
122 return true;
123 }
124
125 /* Reset functions */
reset_unknown(struct kvm_vcpu * vcpu,const struct sys_reg_desc * r)126 static inline void reset_unknown(struct kvm_vcpu *vcpu,
127 const struct sys_reg_desc *r)
128 {
129 BUG_ON(!r->reg);
130 BUG_ON(r->reg >= NR_SYS_REGS);
131 __vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
132 }
133
reset_val(struct kvm_vcpu * vcpu,const struct sys_reg_desc * r)134 static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
135 {
136 BUG_ON(!r->reg);
137 BUG_ON(r->reg >= NR_SYS_REGS);
138 __vcpu_sys_reg(vcpu, r->reg) = r->val;
139 }
140
sysreg_visibility(const struct kvm_vcpu * vcpu,const struct sys_reg_desc * r)141 static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
142 const struct sys_reg_desc *r)
143 {
144 if (likely(!r->visibility))
145 return 0;
146
147 return r->visibility(vcpu, r);
148 }
149
sysreg_hidden(const struct kvm_vcpu * vcpu,const struct sys_reg_desc * r)150 static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
151 const struct sys_reg_desc *r)
152 {
153 return sysreg_visibility(vcpu, r) & REG_HIDDEN;
154 }
155
sysreg_hidden_user(const struct kvm_vcpu * vcpu,const struct sys_reg_desc * r)156 static inline bool sysreg_hidden_user(const struct kvm_vcpu *vcpu,
157 const struct sys_reg_desc *r)
158 {
159 if (likely(!r->visibility))
160 return false;
161
162 return r->visibility(vcpu, r) & (REG_HIDDEN | REG_HIDDEN_USER);
163 }
164
sysreg_visible_as_raz(const struct kvm_vcpu * vcpu,const struct sys_reg_desc * r)165 static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
166 const struct sys_reg_desc *r)
167 {
168 return sysreg_visibility(vcpu, r) & REG_RAZ;
169 }
170
sysreg_user_write_ignore(const struct kvm_vcpu * vcpu,const struct sys_reg_desc * r)171 static inline bool sysreg_user_write_ignore(const struct kvm_vcpu *vcpu,
172 const struct sys_reg_desc *r)
173 {
174 return sysreg_visibility(vcpu, r) & REG_USER_WI;
175 }
176
cmp_sys_reg(const struct sys_reg_desc * i1,const struct sys_reg_desc * i2)177 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
178 const struct sys_reg_desc *i2)
179 {
180 BUG_ON(i1 == i2);
181 if (!i1)
182 return 1;
183 else if (!i2)
184 return -1;
185 if (i1->Op0 != i2->Op0)
186 return i1->Op0 - i2->Op0;
187 if (i1->Op1 != i2->Op1)
188 return i1->Op1 - i2->Op1;
189 if (i1->CRn != i2->CRn)
190 return i1->CRn - i2->CRn;
191 if (i1->CRm != i2->CRm)
192 return i1->CRm - i2->CRm;
193 return i1->Op2 - i2->Op2;
194 }
195
match_sys_reg(const void * key,const void * elt)196 static inline int match_sys_reg(const void *key, const void *elt)
197 {
198 const unsigned long pval = (unsigned long)key;
199 const struct sys_reg_desc *r = elt;
200
201 return pval - reg_to_encoding(r);
202 }
203
204 static inline const struct sys_reg_desc *
find_reg(const struct sys_reg_params * params,const struct sys_reg_desc table[],unsigned int num)205 find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
206 unsigned int num)
207 {
208 unsigned long pval = reg_to_encoding(params);
209
210 return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
211 }
212
213 const struct sys_reg_desc *get_reg_by_id(u64 id,
214 const struct sys_reg_desc table[],
215 unsigned int num);
216
217 int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
218 int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
219 int kvm_sys_reg_get_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
220 const struct sys_reg_desc table[], unsigned int num);
221 int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
222 const struct sys_reg_desc table[], unsigned int num);
223
224 #define AA32(_x) .aarch32_map = AA32_##_x
225 #define Op0(_x) .Op0 = _x
226 #define Op1(_x) .Op1 = _x
227 #define CRn(_x) .CRn = _x
228 #define CRm(_x) .CRm = _x
229 #define Op2(_x) .Op2 = _x
230
231 #define SYS_DESC(reg) \
232 .name = #reg, \
233 Op0(sys_reg_Op0(reg)), Op1(sys_reg_Op1(reg)), \
234 CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)), \
235 Op2(sys_reg_Op2(reg))
236
237 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
238