1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012,2013 - ARM Ltd
6 * Derived from arch/arm/kvm/coproc.h
7 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
11 #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__
12 #define __ARM64_KVM_SYS_REGS_LOCAL_H__
14 #include <linux/bsearch.h>
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)
20 struct sys_reg_params {
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) })
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) })
46 /* Sysreg string for debug */
55 /* MRS/MSR instruction which accesses it. */
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 *);
67 /* Initialization for vcpu. */
68 void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
70 /* Index into sys_reg[], or 0 if we don't need to save it. */
73 /* Value (usually reset value) */
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,
79 int (*set_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
82 /* Return mask of REG_* runtime visibility overrides */
83 unsigned int (*visibility)(const struct kvm_vcpu *vcpu,
84 const struct sys_reg_desc *rd);
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 */
93 inline void print_sys_reg_msg(const struct sys_reg_params *p,
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");
106 static inline void print_sys_reg_instr(const struct sys_reg_params *p)
108 /* GCC warns on an empty format string */
109 print_sys_reg_msg(p, "%s", "");
112 static inline bool ignore_write(struct kvm_vcpu *vcpu,
113 const struct sys_reg_params *p)
118 static inline bool read_zero(struct kvm_vcpu *vcpu,
119 struct sys_reg_params *p)
125 /* Reset functions */
126 static inline void reset_unknown(struct kvm_vcpu *vcpu,
127 const struct sys_reg_desc *r)
130 BUG_ON(r->reg >= NR_SYS_REGS);
131 __vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
134 static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
137 BUG_ON(r->reg >= NR_SYS_REGS);
138 __vcpu_sys_reg(vcpu, r->reg) = r->val;
141 static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
142 const struct sys_reg_desc *r)
144 if (likely(!r->visibility))
147 return r->visibility(vcpu, r);
150 static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
151 const struct sys_reg_desc *r)
153 return sysreg_visibility(vcpu, r) & REG_HIDDEN;
156 static inline bool sysreg_hidden_user(const struct kvm_vcpu *vcpu,
157 const struct sys_reg_desc *r)
159 if (likely(!r->visibility))
162 return r->visibility(vcpu, r) & (REG_HIDDEN | REG_HIDDEN_USER);
165 static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
166 const struct sys_reg_desc *r)
168 return sysreg_visibility(vcpu, r) & REG_RAZ;
171 static inline bool sysreg_user_write_ignore(const struct kvm_vcpu *vcpu,
172 const struct sys_reg_desc *r)
174 return sysreg_visibility(vcpu, r) & REG_USER_WI;
177 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
178 const struct sys_reg_desc *i2)
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;
196 static inline int match_sys_reg(const void *key, const void *elt)
198 const unsigned long pval = (unsigned long)key;
199 const struct sys_reg_desc *r = elt;
201 return pval - reg_to_encoding(r);
204 static inline const struct sys_reg_desc *
205 find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
208 unsigned long pval = reg_to_encoding(params);
210 return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
213 const struct sys_reg_desc *get_reg_by_id(u64 id,
214 const struct sys_reg_desc table[],
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);
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
231 #define SYS_DESC(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))
237 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */