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
25struct sys_reg_params {
26	u8	Op0;
27	u8	Op1;
28	u8	CRn;
29	u8	CRm;
30	u8	Op2;
31	u8	Rt;
32	bool	is_write;
33	bool	is_aarch32;
34	bool	is_32bit;	/* Only valid if is_aarch32 is true */
35};
36
37struct sys_reg_desc {
38	/* MRS/MSR instruction which accesses it. */
39	u8	Op0;
40	u8	Op1;
41	u8	CRn;
42	u8	CRm;
43	u8	Op2;
44
45	/* Trapped access from guest, if non-NULL. */
46	bool (*access)(struct kvm_vcpu *,
47		       const struct sys_reg_params *,
48		       const struct sys_reg_desc *);
49
50	/* Initialization for vcpu. */
51	void (*reset)(struct kvm_vcpu *, const struct sys_reg_desc *);
52
53	/* Index into sys_reg[], or 0 if we don't need to save it. */
54	int reg;
55
56	/* Value (usually reset value) */
57	u64 val;
58};
59
60static inline void print_sys_reg_instr(const struct sys_reg_params *p)
61{
62	/* Look, we even formatted it for you to paste into the table! */
63	kvm_pr_unimpl(" { Op0(%2u), Op1(%2u), CRn(%2u), CRm(%2u), Op2(%2u), func_%s },\n",
64		      p->Op0, p->Op1, p->CRn, p->CRm, p->Op2, p->is_write ? "write" : "read");
65}
66
67static inline bool ignore_write(struct kvm_vcpu *vcpu,
68				const struct sys_reg_params *p)
69{
70	return true;
71}
72
73static inline bool read_zero(struct kvm_vcpu *vcpu,
74			     const struct sys_reg_params *p)
75{
76	*vcpu_reg(vcpu, p->Rt) = 0;
77	return true;
78}
79
80static inline bool write_to_read_only(struct kvm_vcpu *vcpu,
81				      const struct sys_reg_params *params)
82{
83	kvm_debug("sys_reg write to read-only register at: %lx\n",
84		  *vcpu_pc(vcpu));
85	print_sys_reg_instr(params);
86	return false;
87}
88
89static inline bool read_from_write_only(struct kvm_vcpu *vcpu,
90					const struct sys_reg_params *params)
91{
92	kvm_debug("sys_reg read to write-only register at: %lx\n",
93		  *vcpu_pc(vcpu));
94	print_sys_reg_instr(params);
95	return false;
96}
97
98/* Reset functions */
99static inline void reset_unknown(struct kvm_vcpu *vcpu,
100				 const struct sys_reg_desc *r)
101{
102	BUG_ON(!r->reg);
103	BUG_ON(r->reg >= NR_SYS_REGS);
104	vcpu_sys_reg(vcpu, r->reg) = 0x1de7ec7edbadc0deULL;
105}
106
107static inline void reset_val(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
108{
109	BUG_ON(!r->reg);
110	BUG_ON(r->reg >= NR_SYS_REGS);
111	vcpu_sys_reg(vcpu, r->reg) = r->val;
112}
113
114static inline int cmp_sys_reg(const struct sys_reg_desc *i1,
115			      const struct sys_reg_desc *i2)
116{
117	BUG_ON(i1 == i2);
118	if (!i1)
119		return 1;
120	else if (!i2)
121		return -1;
122	if (i1->Op0 != i2->Op0)
123		return i1->Op0 - i2->Op0;
124	if (i1->Op1 != i2->Op1)
125		return i1->Op1 - i2->Op1;
126	if (i1->CRn != i2->CRn)
127		return i1->CRn - i2->CRn;
128	if (i1->CRm != i2->CRm)
129		return i1->CRm - i2->CRm;
130	return i1->Op2 - i2->Op2;
131}
132
133
134#define Op0(_x) 	.Op0 = _x
135#define Op1(_x) 	.Op1 = _x
136#define CRn(_x)		.CRn = _x
137#define CRm(_x) 	.CRm = _x
138#define Op2(_x) 	.Op2 = _x
139
140#endif /* __ARM64_KVM_SYS_REGS_LOCAL_H__ */
141