blob: 3a41b7d1eb86ef5800b17f4690f5fdd96f3233d2 [file] [log] [blame]
Christoffer Dall5b3e5e52013-01-20 18:28:09 -05001/*
2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Authors: Christoffer Dall <c.dall@virtualopensystems.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#ifndef __ARM_KVM_COPROC_LOCAL_H__
20#define __ARM_KVM_COPROC_LOCAL_H__
21
22struct coproc_params {
23 unsigned long CRn;
24 unsigned long CRm;
25 unsigned long Op1;
26 unsigned long Op2;
27 unsigned long Rt1;
28 unsigned long Rt2;
29 bool is_64bit;
30 bool is_write;
31};
32
33struct coproc_reg {
34 /* MRC/MCR/MRRC/MCRR instruction which accesses it. */
35 unsigned long CRn;
36 unsigned long CRm;
37 unsigned long Op1;
38 unsigned long Op2;
39
Marc Zyngierf1d67d42016-01-21 17:04:52 +000040 bool is_64bit;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050041
42 /* Trapped access from guest, if non-NULL. */
43 bool (*access)(struct kvm_vcpu *,
44 const struct coproc_params *,
45 const struct coproc_reg *);
46
47 /* Initialization for vcpu. */
48 void (*reset)(struct kvm_vcpu *, const struct coproc_reg *);
49
Marc Zyngierfb32a522016-01-03 11:26:01 +000050 /* Index into vcpu_cp15(vcpu, ...), or 0 if we don't need to save it. */
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050051 unsigned long reg;
52
53 /* Value (usually reset value) */
54 u64 val;
55};
56
57static inline void print_cp_instr(const struct coproc_params *p)
58{
59 /* Look, we even formatted it for you to paste into the table! */
60 if (p->is_64bit) {
Marc Zyngier46c214d2014-01-21 18:56:26 +000061 kvm_pr_unimpl(" { CRm64(%2lu), Op1(%2lu), is64, func_%s },\n",
62 p->CRn, p->Op1, p->is_write ? "write" : "read");
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050063 } else {
64 kvm_pr_unimpl(" { CRn(%2lu), CRm(%2lu), Op1(%2lu), Op2(%2lu), is32,"
65 " func_%s },\n",
66 p->CRn, p->CRm, p->Op1, p->Op2,
67 p->is_write ? "write" : "read");
68 }
69}
70
71static inline bool ignore_write(struct kvm_vcpu *vcpu,
72 const struct coproc_params *p)
73{
74 return true;
75}
76
77static inline bool read_zero(struct kvm_vcpu *vcpu,
78 const struct coproc_params *p)
79{
80 *vcpu_reg(vcpu, p->Rt1) = 0;
81 return true;
82}
83
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050084/* Reset functions */
85static inline void reset_unknown(struct kvm_vcpu *vcpu,
86 const struct coproc_reg *r)
87{
88 BUG_ON(!r->reg);
Marc Zyngierfb32a522016-01-03 11:26:01 +000089 BUG_ON(r->reg >= ARRAY_SIZE(vcpu->arch.ctxt.cp15));
90 vcpu_cp15(vcpu, r->reg) = 0xdecafbad;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050091}
92
93static inline void reset_val(struct kvm_vcpu *vcpu, const struct coproc_reg *r)
94{
95 BUG_ON(!r->reg);
Marc Zyngierfb32a522016-01-03 11:26:01 +000096 BUG_ON(r->reg >= ARRAY_SIZE(vcpu->arch.ctxt.cp15));
97 vcpu_cp15(vcpu, r->reg) = r->val;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -050098}
99
100static inline void reset_unknown64(struct kvm_vcpu *vcpu,
101 const struct coproc_reg *r)
102{
103 BUG_ON(!r->reg);
Marc Zyngierfb32a522016-01-03 11:26:01 +0000104 BUG_ON(r->reg + 1 >= ARRAY_SIZE(vcpu->arch.ctxt.cp15));
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500105
Marc Zyngierfb32a522016-01-03 11:26:01 +0000106 vcpu_cp15(vcpu, r->reg) = 0xdecafbad;
107 vcpu_cp15(vcpu, r->reg+1) = 0xd0c0ffee;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500108}
109
110static inline int cmp_reg(const struct coproc_reg *i1,
111 const struct coproc_reg *i2)
112{
113 BUG_ON(i1 == i2);
114 if (!i1)
115 return 1;
116 else if (!i2)
117 return -1;
118 if (i1->CRn != i2->CRn)
119 return i1->CRn - i2->CRn;
120 if (i1->CRm != i2->CRm)
121 return i1->CRm - i2->CRm;
122 if (i1->Op1 != i2->Op1)
123 return i1->Op1 - i2->Op1;
Marc Zyngier547f7812014-01-21 18:56:26 +0000124 if (i1->Op2 != i2->Op2)
125 return i1->Op2 - i2->Op2;
Marc Zyngierf1d67d42016-01-21 17:04:52 +0000126 return i2->is_64bit - i1->is_64bit;
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500127}
128
129
130#define CRn(_x) .CRn = _x
131#define CRm(_x) .CRm = _x
Christoffer Dall240e99c2013-08-05 18:08:41 -0700132#define CRm64(_x) .CRn = _x, .CRm = 0
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500133#define Op1(_x) .Op1 = _x
134#define Op2(_x) .Op2 = _x
Marc Zyngierf1d67d42016-01-21 17:04:52 +0000135#define is64 .is_64bit = true
136#define is32 .is_64bit = false
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500137
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000138bool access_vm_reg(struct kvm_vcpu *vcpu,
139 const struct coproc_params *p,
140 const struct coproc_reg *r);
Marc Zyngier80346992014-01-14 18:00:55 +0000141
Christoffer Dall5b3e5e52013-01-20 18:28:09 -0500142#endif /* __ARM_KVM_COPROC_LOCAL_H__ */