1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012,2013 - ARM Ltd
4 * Author: Marc Zyngier <marc.zyngier@arm.com>
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>
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) })
39 /* Sysreg string for debug */
48 /* MRS/MSR instruction which accesses it. */
55 /* Trapped access from guest, if non-NULL. */
56 bool (*access)(struct kvm_vcpu *,
57 struct sys_reg_params *,
58 const struct sys_reg_desc *);
60 /* Initialization for vcpu. */
61 void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
63 /* Index into sys_reg[], or 0 if we don't need to save it. */
66 /* Value (usually reset value) */
69 /* Custom get/set_user functions, fallback to generic if NULL */
70 int (*get_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
71 const struct kvm_one_reg *reg, void __user *uaddr);
72 int (*set_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
73 const struct kvm_one_reg *reg, void __user *uaddr);
75 /* Return mask of REG_* runtime visibility overrides */
76 unsigned int (*visibility)(const struct kvm_vcpu *vcpu,
77 const struct sys_reg_desc *rd);
80 #define REG_HIDDEN (1 << 0) /* hidden from userspace and guest */
81 #define REG_RAZ (1 << 1) /* RAZ from userspace and guest */
84 inline void print_sys_reg_msg(const struct sys_reg_params *p,
90 /* Look, we even formatted it for you to paste into the table! */
91 kvm_pr_unimpl("%pV { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n",
92 &(struct va_format){ fmt, &va },
93 p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read");
97 static inline void print_sys_reg_instr(const struct sys_reg_params *p)
99 /* GCC warns on an empty format string */
100 print_sys_reg_msg(p, "%s", "");
103 static inline bool ignore_write(struct kvm_vcpu *vcpu,
104 const struct sys_reg_params *p)
109 static inline bool read_zero(struct kvm_vcpu *vcpu,
110 struct sys_reg_params *p)
116 /* Reset functions */
117 static inline void reset_unknown(struct kvm_vcpu *vcpu,
118 const struct sys_reg_desc *r)
121 BUG_ON(r->reg >= NR_SYS_REGS);
122 __vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
125 static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
128 BUG_ON(r->reg >= NR_SYS_REGS);
129 __vcpu_sys_reg(vcpu, r->reg) = r->val;
132 static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
133 const struct sys_reg_desc *r)
135 if (likely(!r->visibility))
138 return r->visibility(vcpu, r) & REG_HIDDEN;
141 static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
142 const struct sys_reg_desc *r)
144 if (likely(!r->visibility))
147 return r->visibility(vcpu, r) & REG_RAZ;
150 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
151 const struct sys_reg_desc *i2)
158 if (i1->Op0 != i2->Op0)
159 return i1->Op0 - i2->Op0;
160 if (i1->Op1 != i2->Op1)
161 return i1->Op1 - i2->Op1;
162 if (i1->CRn != i2->CRn)
163 return i1->CRn - i2->CRn;
164 if (i1->CRm != i2->CRm)
165 return i1->CRm - i2->CRm;
166 return i1->Op2 - i2->Op2;
169 static inline int match_sys_reg(const void *key, const void *elt)
171 const unsigned long pval = (unsigned long)key;
172 const struct sys_reg_desc *r = elt;
174 return pval - reg_to_encoding(r);
177 static inline const struct sys_reg_desc *
178 find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
181 unsigned long pval = reg_to_encoding(params);
183 return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
186 const struct sys_reg_desc *find_reg_by_id(u64 id,
187 struct sys_reg_params *params,
188 const struct sys_reg_desc table[],
191 #define AA32(_x) .aarch32_map = AA32_##_x
192 #define Op0(_x) .Op0 = _x
193 #define Op1(_x) .Op1 = _x
194 #define CRn(_x) .CRn = _x
195 #define CRm(_x) .CRm = _x
196 #define Op2(_x) .Op2 = _x
198 #define SYS_DESC(reg) \
200 Op0(sys_reg_Op0(reg)), Op1(sys_reg_Op1(reg)), \
201 CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)), \
202 Op2(sys_reg_Op2(reg))
204 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */