1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/errno.h>
20#include <linux/err.h>
21#include <linux/kvm_host.h>
22#include <linux/module.h>
23#include <linux/vmalloc.h>
24#include <linux/fs.h>
25#include <kvm/arm_psci.h>
26#include <asm/cputype.h>
27#include <linux/uaccess.h>
28#include <asm/kvm.h>
29#include <asm/kvm_emulate.h>
30#include <asm/kvm_coproc.h>
31
32#define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
33#define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
34
35struct kvm_stats_debugfs_item debugfs_entries[] = {
36 VCPU_STAT(hvc_exit_stat),
37 VCPU_STAT(wfe_exit_stat),
38 VCPU_STAT(wfi_exit_stat),
39 VCPU_STAT(mmio_exit_user),
40 VCPU_STAT(mmio_exit_kernel),
41 VCPU_STAT(exits),
42 { NULL }
43};
44
45int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
46{
47 return 0;
48}
49
50static u64 core_reg_offset_from_id(u64 id)
51{
52 return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
53}
54
55static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
56{
57 u32 __user *uaddr = (u32 __user *)(long)reg->addr;
58 struct kvm_regs *regs = &vcpu->arch.ctxt.gp_regs;
59 u64 off;
60
61 if (KVM_REG_SIZE(reg->id) != 4)
62 return -ENOENT;
63
64
65 off = core_reg_offset_from_id(reg->id);
66 if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
67 return -ENOENT;
68
69 return put_user(((u32 *)regs)[off], uaddr);
70}
71
72static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
73{
74 u32 __user *uaddr = (u32 __user *)(long)reg->addr;
75 struct kvm_regs *regs = &vcpu->arch.ctxt.gp_regs;
76 u64 off, val;
77
78 if (KVM_REG_SIZE(reg->id) != 4)
79 return -ENOENT;
80
81
82 off = core_reg_offset_from_id(reg->id);
83 if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
84 return -ENOENT;
85
86 if (get_user(val, uaddr) != 0)
87 return -EFAULT;
88
89 if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) {
90 unsigned long mode = val & MODE_MASK;
91 switch (mode) {
92 case USR_MODE:
93 case FIQ_MODE:
94 case IRQ_MODE:
95 case SVC_MODE:
96 case ABT_MODE:
97 case UND_MODE:
98 break;
99 default:
100 return -EINVAL;
101 }
102 }
103
104 ((u32 *)regs)[off] = val;
105 return 0;
106}
107
108int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
109{
110 return -EINVAL;
111}
112
113int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
114{
115 return -EINVAL;
116}
117
118#define NUM_TIMER_REGS 3
119
120static bool is_timer_reg(u64 index)
121{
122 switch (index) {
123 case KVM_REG_ARM_TIMER_CTL:
124 case KVM_REG_ARM_TIMER_CNT:
125 case KVM_REG_ARM_TIMER_CVAL:
126 return true;
127 }
128 return false;
129}
130
131static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
132{
133 if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
134 return -EFAULT;
135 uindices++;
136 if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
137 return -EFAULT;
138 uindices++;
139 if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
140 return -EFAULT;
141
142 return 0;
143}
144
145static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
146{
147 void __user *uaddr = (void __user *)(long)reg->addr;
148 u64 val;
149 int ret;
150
151 ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
152 if (ret != 0)
153 return -EFAULT;
154
155 return kvm_arm_timer_set_reg(vcpu, reg->id, val);
156}
157
158static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
159{
160 void __user *uaddr = (void __user *)(long)reg->addr;
161 u64 val;
162
163 val = kvm_arm_timer_get_reg(vcpu, reg->id);
164 return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)) ? -EFAULT : 0;
165}
166
167static unsigned long num_core_regs(void)
168{
169 return sizeof(struct kvm_regs) / sizeof(u32);
170}
171
172
173
174
175
176
177unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
178{
179 return num_core_regs() + kvm_arm_num_coproc_regs(vcpu)
180 + kvm_arm_get_fw_num_regs(vcpu)
181 + NUM_TIMER_REGS;
182}
183
184
185
186
187
188
189int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
190{
191 unsigned int i;
192 const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
193 int ret;
194
195 for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
196 if (put_user(core_reg | i, uindices))
197 return -EFAULT;
198 uindices++;
199 }
200
201 ret = kvm_arm_copy_fw_reg_indices(vcpu, uindices);
202 if (ret)
203 return ret;
204 uindices += kvm_arm_get_fw_num_regs(vcpu);
205
206 ret = copy_timer_indices(vcpu, uindices);
207 if (ret)
208 return ret;
209 uindices += NUM_TIMER_REGS;
210
211 return kvm_arm_copy_coproc_indices(vcpu, uindices);
212}
213
214int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
215{
216
217 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
218 return -EINVAL;
219
220
221 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
222 return get_core_reg(vcpu, reg);
223
224 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_FW)
225 return kvm_arm_get_fw_reg(vcpu, reg);
226
227 if (is_timer_reg(reg->id))
228 return get_timer_reg(vcpu, reg);
229
230 return kvm_arm_coproc_get_reg(vcpu, reg);
231}
232
233int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
234{
235
236 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
237 return -EINVAL;
238
239
240 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
241 return set_core_reg(vcpu, reg);
242
243 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_FW)
244 return kvm_arm_set_fw_reg(vcpu, reg);
245
246 if (is_timer_reg(reg->id))
247 return set_timer_reg(vcpu, reg);
248
249 return kvm_arm_coproc_set_reg(vcpu, reg);
250}
251
252int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
253 struct kvm_sregs *sregs)
254{
255 return -EINVAL;
256}
257
258int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
259 struct kvm_sregs *sregs)
260{
261 return -EINVAL;
262}
263
264
265int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
266 struct kvm_vcpu_events *events)
267{
268 events->exception.serror_pending = !!(*vcpu_hcr(vcpu) & HCR_VA);
269
270 return 0;
271}
272
273int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
274 struct kvm_vcpu_events *events)
275{
276 bool serror_pending = events->exception.serror_pending;
277 bool has_esr = events->exception.serror_has_esr;
278
279 if (serror_pending && has_esr)
280 return -EINVAL;
281 else if (serror_pending)
282 kvm_inject_vabt(vcpu);
283
284 return 0;
285}
286
287int __attribute_const__ kvm_target_cpu(void)
288{
289 switch (read_cpuid_part()) {
290 case ARM_CPU_PART_CORTEX_A7:
291 return KVM_ARM_TARGET_CORTEX_A7;
292 case ARM_CPU_PART_CORTEX_A15:
293 return KVM_ARM_TARGET_CORTEX_A15;
294 default:
295 return -EINVAL;
296 }
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
335int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
336 struct kvm_guest_debug *dbg)
337{
338 return -EINVAL;
339}
340
341int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
342 struct kvm_device_attr *attr)
343{
344 int ret;
345
346 switch (attr->group) {
347 case KVM_ARM_VCPU_TIMER_CTRL:
348 ret = kvm_arm_timer_set_attr(vcpu, attr);
349 break;
350 default:
351 ret = -ENXIO;
352 break;
353 }
354
355 return ret;
356}
357
358int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
359 struct kvm_device_attr *attr)
360{
361 int ret;
362
363 switch (attr->group) {
364 case KVM_ARM_VCPU_TIMER_CTRL:
365 ret = kvm_arm_timer_get_attr(vcpu, attr);
366 break;
367 default:
368 ret = -ENXIO;
369 break;
370 }
371
372 return ret;
373}
374
375int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
376 struct kvm_device_attr *attr)
377{
378 int ret;
379
380 switch (attr->group) {
381 case KVM_ARM_VCPU_TIMER_CTRL:
382 ret = kvm_arm_timer_has_attr(vcpu, attr);
383 break;
384 default:
385 ret = -ENXIO;
386 break;
387 }
388
389 return ret;
390}
391