xref: /openbmc/linux/arch/arm64/kvm/sys_regs.h (revision b92dd117)
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_RAZ			(1 << 1) /* RAZ from userspace and guest */
89 #define REG_USER_WI		(1 << 2) /* WI from userspace only */
90 
91 static __printf(2, 3)
92 inline void print_sys_reg_msg(const struct sys_reg_params *p,
93 				       char *fmt, ...)
94 {
95 	va_list va;
96 
97 	va_start(va, fmt);
98 	/* Look, we even formatted it for you to paste into the table! */
99 	kvm_pr_unimpl("%pV { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n",
100 		      &(struct va_format){ fmt, &va },
101 		      p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read");
102 	va_end(va);
103 }
104 
105 static inline void print_sys_reg_instr(const struct sys_reg_params *p)
106 {
107 	/* GCC warns on an empty format string */
108 	print_sys_reg_msg(p, "%s", "");
109 }
110 
111 static inline bool ignore_write(struct kvm_vcpu *vcpu,
112 				const struct sys_reg_params *p)
113 {
114 	return true;
115 }
116 
117 static inline bool read_zero(struct kvm_vcpu *vcpu,
118 			     struct sys_reg_params *p)
119 {
120 	p->regval = 0;
121 	return true;
122 }
123 
124 /* Reset functions */
125 static inline void reset_unknown(struct kvm_vcpu *vcpu,
126 				 const struct sys_reg_desc *r)
127 {
128 	BUG_ON(!r->reg);
129 	BUG_ON(r->reg >= NR_SYS_REGS);
130 	__vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
131 }
132 
133 static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
134 {
135 	BUG_ON(!r->reg);
136 	BUG_ON(r->reg >= NR_SYS_REGS);
137 	__vcpu_sys_reg(vcpu, r->reg) = r->val;
138 }
139 
140 static inline unsigned int sysreg_visibility(const struct kvm_vcpu *vcpu,
141 					     const struct sys_reg_desc *r)
142 {
143 	if (likely(!r->visibility))
144 		return 0;
145 
146 	return r->visibility(vcpu, r);
147 }
148 
149 static inline bool sysreg_hidden(const struct kvm_vcpu *vcpu,
150 				 const struct sys_reg_desc *r)
151 {
152 	return sysreg_visibility(vcpu, r) & REG_HIDDEN;
153 }
154 
155 static inline bool sysreg_visible_as_raz(const struct kvm_vcpu *vcpu,
156 					 const struct sys_reg_desc *r)
157 {
158 	return sysreg_visibility(vcpu, r) & REG_RAZ;
159 }
160 
161 static inline bool sysreg_user_write_ignore(const struct kvm_vcpu *vcpu,
162 					    const struct sys_reg_desc *r)
163 {
164 	return sysreg_visibility(vcpu, r) & REG_USER_WI;
165 }
166 
167 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
168 			      const struct sys_reg_desc *i2)
169 {
170 	BUG_ON(i1 == i2);
171 	if (!i1)
172 		return 1;
173 	else if (!i2)
174 		return -1;
175 	if (i1->Op0 != i2->Op0)
176 		return i1->Op0 - i2->Op0;
177 	if (i1->Op1 != i2->Op1)
178 		return i1->Op1 - i2->Op1;
179 	if (i1->CRn != i2->CRn)
180 		return i1->CRn - i2->CRn;
181 	if (i1->CRm != i2->CRm)
182 		return i1->CRm - i2->CRm;
183 	return i1->Op2 - i2->Op2;
184 }
185 
186 static inline int match_sys_reg(const void *key, const void *elt)
187 {
188 	const unsigned long pval = (unsigned long)key;
189 	const struct sys_reg_desc *r = elt;
190 
191 	return pval - reg_to_encoding(r);
192 }
193 
194 static inline const struct sys_reg_desc *
195 find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],
196 	 unsigned int num)
197 {
198 	unsigned long pval = reg_to_encoding(params);
199 
200 	return __inline_bsearch((void *)pval, table, num, sizeof(table[0]), match_sys_reg);
201 }
202 
203 const struct sys_reg_desc *get_reg_by_id(u64 id,
204 					 const struct sys_reg_desc table[],
205 					 unsigned int num);
206 
207 int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
208 int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *);
209 int kvm_sys_reg_get_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
210 			 const struct sys_reg_desc table[], unsigned int num);
211 int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg,
212 			 const struct sys_reg_desc table[], unsigned int num);
213 
214 #define AA32(_x)	.aarch32_map = AA32_##_x
215 #define Op0(_x) 	.Op0 = _x
216 #define Op1(_x) 	.Op1 = _x
217 #define CRn(_x)		.CRn = _x
218 #define CRm(_x) 	.CRm = _x
219 #define Op2(_x) 	.Op2 = _x
220 
221 #define SYS_DESC(reg)					\
222 	.name = #reg,					\
223 	Op0(sys_reg_Op0(reg)), Op1(sys_reg_Op1(reg)),	\
224 	CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)),	\
225 	Op2(sys_reg_Op2(reg))
226 
227 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
228