xref: /openbmc/linux/arch/arm64/kvm/sys_regs.h (revision 7f34e409)
1 /*
2  * Copyright (C) 2012,2013 - ARM Ltd
3  * Author: Marc Zyngier <marc.zyngier@arm.com>
4  *
5  * Derived from arch/arm/kvm/coproc.h
6  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
7  * Authors: Christoffer Dall <c.dall@virtualopensystems.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License, version 2, as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef __ARM64_KVM_SYS_REGS_LOCAL_H__
23 #define __ARM64_KVM_SYS_REGS_LOCAL_H__
24 
25 struct sys_reg_params {
26 	u8	Op0;
27 	u8	Op1;
28 	u8	CRn;
29 	u8	CRm;
30 	u8	Op2;
31 	u64	regval;
32 	bool	is_write;
33 	bool	is_aarch32;
34 	bool	is_32bit;	/* Only valid if is_aarch32 is true */
35 };
36 
37 struct sys_reg_desc {
38 	/* Sysreg string for debug */
39 	const char *name;
40 
41 	/* MRS/MSR instruction which accesses it. */
42 	u8	Op0;
43 	u8	Op1;
44 	u8	CRn;
45 	u8	CRm;
46 	u8	Op2;
47 
48 	/* Trapped access from guest, if non-NULL. */
49 	bool (*access)(struct kvm_vcpu *,
50 		       struct sys_reg_params *,
51 		       const struct sys_reg_desc *);
52 
53 	/* Initialization for vcpu. */
54 	void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
55 
56 	/* Index into sys_reg[], or 0 if we don't need to save it. */
57 	int reg;
58 
59 	/* Value (usually reset value) */
60 	u64 val;
61 
62 	/* Custom get/set_user functions, fallback to generic if NULL */
63 	int (*get_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
64 			const struct kvm_one_reg *reg, void __user *uaddr);
65 	int (*set_user)(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
66 			const struct kvm_one_reg *reg, void __user *uaddr);
67 
68 	/* Return mask of REG_* runtime visibility overrides */
69 	unsigned int (*visibility)(const struct kvm_vcpu *vcpu,
70 				   const struct sys_reg_desc *rd);
71 };
72 
73 #define REG_HIDDEN_USER		(1 << 0) /* hidden from userspace ioctls */
74 #define REG_HIDDEN_GUEST	(1 << 1) /* hidden from guest */
75 
76 static inline void print_sys_reg_instr(const struct sys_reg_params *p)
77 {
78 	/* Look, we even formatted it for you to paste into the table! */
79 	kvm_pr_unimpl(" { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n",
80 		      p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read");
81 }
82 
83 static inline bool ignore_write(struct kvm_vcpu *vcpu,
84 				const struct sys_reg_params *p)
85 {
86 	return true;
87 }
88 
89 static inline bool read_zero(struct kvm_vcpu *vcpu,
90 			     struct sys_reg_params *p)
91 {
92 	p->regval = 0;
93 	return true;
94 }
95 
96 /* Reset functions */
97 static inline void reset_unknown(struct kvm_vcpu *vcpu,
98 				 const struct sys_reg_desc *r)
99 {
100 	BUG_ON(!r->reg);
101 	BUG_ON(r->reg >= NR_SYS_REGS);
102 	__vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
103 }
104 
105 static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
106 {
107 	BUG_ON(!r->reg);
108 	BUG_ON(r->reg >= NR_SYS_REGS);
109 	__vcpu_sys_reg(vcpu, r->reg) = r->val;
110 }
111 
112 static inline bool sysreg_hidden_from_guest(const struct kvm_vcpu *vcpu,
113 					    const struct sys_reg_desc *r)
114 {
115 	if (likely(!r->visibility))
116 		return false;
117 
118 	return r->visibility(vcpu, r) & REG_HIDDEN_GUEST;
119 }
120 
121 static inline bool sysreg_hidden_from_user(const struct kvm_vcpu *vcpu,
122 					   const struct sys_reg_desc *r)
123 {
124 	if (likely(!r->visibility))
125 		return false;
126 
127 	return r->visibility(vcpu, r) & REG_HIDDEN_USER;
128 }
129 
130 static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
131 			      const struct sys_reg_desc *i2)
132 {
133 	BUG_ON(i1 == i2);
134 	if (!i1)
135 		return 1;
136 	else if (!i2)
137 		return -1;
138 	if (i1->Op0 != i2->Op0)
139 		return i1->Op0 - i2->Op0;
140 	if (i1->Op1 != i2->Op1)
141 		return i1->Op1 - i2->Op1;
142 	if (i1->CRn != i2->CRn)
143 		return i1->CRn - i2->CRn;
144 	if (i1->CRm != i2->CRm)
145 		return i1->CRm - i2->CRm;
146 	return i1->Op2 - i2->Op2;
147 }
148 
149 const struct sys_reg_desc *find_reg_by_id(u64 id,
150 					  struct sys_reg_params *params,
151 					  const struct sys_reg_desc table[],
152 					  unsigned int num);
153 
154 #define Op0(_x) 	.Op0 = _x
155 #define Op1(_x) 	.Op1 = _x
156 #define CRn(_x)		.CRn = _x
157 #define CRm(_x) 	.CRm = _x
158 #define Op2(_x) 	.Op2 = _x
159 
160 #define SYS_DESC(reg)					\
161 	.name = #reg,					\
162 	Op0(sys_reg_Op0(reg)), Op1(sys_reg_Op1(reg)),	\
163 	CRn(sys_reg_CRn(reg)), CRm(sys_reg_CRm(reg)),	\
164 	Op2(sys_reg_Op2(reg))
165 
166 #endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
167