1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/errno.h>
23#include <linux/err.h>
24#include <linux/kvm_host.h>
25#include <linux/module.h>
26#include <linux/vmalloc.h>
27#include <linux/fs.h>
28#include <asm/cputype.h>
29#include <asm/uaccess.h>
30#include <asm/kvm.h>
31#include <asm/kvm_asm.h>
32#include <asm/kvm_emulate.h>
33#include <asm/kvm_coproc.h>
34
35struct kvm_stats_debugfs_item debugfs_entries[] = {
36 { NULL }
37};
38
39int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
40{
41 return 0;
42}
43
44static u64 core_reg_offset_from_id(u64 id)
45{
46 return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
47}
48
49static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
50{
51
52
53
54
55
56
57 __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
58 struct kvm_regs *regs = vcpu_gp_regs(vcpu);
59 int nr_regs = sizeof(*regs) / sizeof(__u32);
60 u32 off;
61
62
63 off = core_reg_offset_from_id(reg->id);
64 if (off >= nr_regs ||
65 (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
66 return -ENOENT;
67
68 if (copy_to_user(uaddr, ((u32 *)regs) + off, KVM_REG_SIZE(reg->id)))
69 return -EFAULT;
70
71 return 0;
72}
73
74static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
75{
76 __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
77 struct kvm_regs *regs = vcpu_gp_regs(vcpu);
78 int nr_regs = sizeof(*regs) / sizeof(__u32);
79 __uint128_t tmp;
80 void *valp = &tmp;
81 u64 off;
82 int err = 0;
83
84
85 off = core_reg_offset_from_id(reg->id);
86 if (off >= nr_regs ||
87 (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
88 return -ENOENT;
89
90 if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
91 return -EINVAL;
92
93 if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
94 err = -EFAULT;
95 goto out;
96 }
97
98 if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
99 u32 mode = (*(u32 *)valp) & COMPAT_PSR_MODE_MASK;
100 switch (mode) {
101 case COMPAT_PSR_MODE_USR:
102 case COMPAT_PSR_MODE_FIQ:
103 case COMPAT_PSR_MODE_IRQ:
104 case COMPAT_PSR_MODE_SVC:
105 case COMPAT_PSR_MODE_ABT:
106 case COMPAT_PSR_MODE_UND:
107 case PSR_MODE_EL0t:
108 case PSR_MODE_EL1t:
109 case PSR_MODE_EL1h:
110 break;
111 default:
112 err = -EINVAL;
113 goto out;
114 }
115 }
116
117 memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
118out:
119 return err;
120}
121
122int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
123{
124 return -EINVAL;
125}
126
127int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
128{
129 return -EINVAL;
130}
131
132static unsigned long num_core_regs(void)
133{
134 return sizeof(struct kvm_regs) / sizeof(__u32);
135}
136
137
138
139
140
141#define NUM_TIMER_REGS 3
142
143static bool is_timer_reg(u64 index)
144{
145 switch (index) {
146 case KVM_REG_ARM_TIMER_CTL:
147 case KVM_REG_ARM_TIMER_CNT:
148 case KVM_REG_ARM_TIMER_CVAL:
149 return true;
150 }
151 return false;
152}
153
154static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
155{
156 if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
157 return -EFAULT;
158 uindices++;
159 if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
160 return -EFAULT;
161 uindices++;
162 if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
163 return -EFAULT;
164
165 return 0;
166}
167
168static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
169{
170 void __user *uaddr = (void __user *)(long)reg->addr;
171 u64 val;
172 int ret;
173
174 ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
175 if (ret != 0)
176 return -EFAULT;
177
178 return kvm_arm_timer_set_reg(vcpu, reg->id, val);
179}
180
181static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
182{
183 void __user *uaddr = (void __user *)(long)reg->addr;
184 u64 val;
185
186 val = kvm_arm_timer_get_reg(vcpu, reg->id);
187 return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id));
188}
189
190
191
192
193
194
195unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
196{
197 return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu)
198 + NUM_TIMER_REGS;
199}
200
201
202
203
204
205
206int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
207{
208 unsigned int i;
209 const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
210 int ret;
211
212 for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
213 if (put_user(core_reg | i, uindices))
214 return -EFAULT;
215 uindices++;
216 }
217
218 ret = copy_timer_indices(vcpu, uindices);
219 if (ret)
220 return ret;
221 uindices += NUM_TIMER_REGS;
222
223 return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
224}
225
226int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
227{
228
229 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
230 return -EINVAL;
231
232
233 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
234 return get_core_reg(vcpu, reg);
235
236 if (is_timer_reg(reg->id))
237 return get_timer_reg(vcpu, reg);
238
239 return kvm_arm_sys_reg_get_reg(vcpu, reg);
240}
241
242int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
243{
244
245 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
246 return -EINVAL;
247
248
249 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
250 return set_core_reg(vcpu, reg);
251
252 if (is_timer_reg(reg->id))
253 return set_timer_reg(vcpu, reg);
254
255 return kvm_arm_sys_reg_set_reg(vcpu, reg);
256}
257
258int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
259 struct kvm_sregs *sregs)
260{
261 return -EINVAL;
262}
263
264int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
265 struct kvm_sregs *sregs)
266{
267 return -EINVAL;
268}
269
270int __attribute_const__ kvm_target_cpu(void)
271{
272 unsigned long implementor = read_cpuid_implementor();
273 unsigned long part_number = read_cpuid_part_number();
274
275 switch (implementor) {
276 case ARM_CPU_IMP_ARM:
277 switch (part_number) {
278 case ARM_CPU_PART_AEM_V8:
279 return KVM_ARM_TARGET_AEM_V8;
280 case ARM_CPU_PART_FOUNDATION:
281 return KVM_ARM_TARGET_FOUNDATION_V8;
282 case ARM_CPU_PART_CORTEX_A53:
283 return KVM_ARM_TARGET_CORTEX_A53;
284 case ARM_CPU_PART_CORTEX_A57:
285 return KVM_ARM_TARGET_CORTEX_A57;
286 };
287 break;
288 case ARM_CPU_IMP_APM:
289 switch (part_number) {
290 case APM_CPU_PART_POTENZA:
291 return KVM_ARM_TARGET_XGENE_POTENZA;
292 };
293 break;
294 };
295
296 return -EINVAL;
297}
298
299int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
300{
301 int target = kvm_target_cpu();
302
303 if (target < 0)
304 return -ENODEV;
305
306 memset(init, 0, sizeof(*init));
307
308
309
310
311
312
313
314 init->target = (__u32)target;
315
316 return 0;
317}
318
319int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
320{
321 return -EINVAL;
322}
323
324int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
325{
326 return -EINVAL;
327}
328
329int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
330 struct kvm_translation *tr)
331{
332 return -EINVAL;
333}
334