Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6-microblaze.git] / arch / arm / kvm / guest.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4  * Author: Christoffer Dall <c.dall@virtualopensystems.com>
5  */
6
7 #include <linux/errno.h>
8 #include <linux/err.h>
9 #include <linux/kvm_host.h>
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <linux/fs.h>
13 #include <kvm/arm_psci.h>
14 #include <asm/cputype.h>
15 #include <linux/uaccess.h>
16 #include <asm/kvm.h>
17 #include <asm/kvm_emulate.h>
18 #include <asm/kvm_coproc.h>
19
20 #define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
21 #define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
22
23 struct kvm_stats_debugfs_item debugfs_entries[] = {
24         VCPU_STAT(halt_successful_poll),
25         VCPU_STAT(halt_attempted_poll),
26         VCPU_STAT(halt_poll_invalid),
27         VCPU_STAT(halt_wakeup),
28         VCPU_STAT(hvc_exit_stat),
29         VCPU_STAT(wfe_exit_stat),
30         VCPU_STAT(wfi_exit_stat),
31         VCPU_STAT(mmio_exit_user),
32         VCPU_STAT(mmio_exit_kernel),
33         VCPU_STAT(exits),
34         { NULL }
35 };
36
37 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
38 {
39         return 0;
40 }
41
42 static u64 core_reg_offset_from_id(u64 id)
43 {
44         return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
45 }
46
47 static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
48 {
49         u32 __user *uaddr = (u32 __user *)(long)reg->addr;
50         struct kvm_regs *regs = &vcpu->arch.ctxt.gp_regs;
51         u64 off;
52
53         if (KVM_REG_SIZE(reg->id) != 4)
54                 return -ENOENT;
55
56         /* Our ID is an index into the kvm_regs struct. */
57         off = core_reg_offset_from_id(reg->id);
58         if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
59                 return -ENOENT;
60
61         return put_user(((u32 *)regs)[off], uaddr);
62 }
63
64 static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
65 {
66         u32 __user *uaddr = (u32 __user *)(long)reg->addr;
67         struct kvm_regs *regs = &vcpu->arch.ctxt.gp_regs;
68         u64 off, val;
69
70         if (KVM_REG_SIZE(reg->id) != 4)
71                 return -ENOENT;
72
73         /* Our ID is an index into the kvm_regs struct. */
74         off = core_reg_offset_from_id(reg->id);
75         if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
76                 return -ENOENT;
77
78         if (get_user(val, uaddr) != 0)
79                 return -EFAULT;
80
81         if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) {
82                 unsigned long mode = val & MODE_MASK;
83                 switch (mode) {
84                 case USR_MODE:
85                 case FIQ_MODE:
86                 case IRQ_MODE:
87                 case SVC_MODE:
88                 case ABT_MODE:
89                 case UND_MODE:
90                         break;
91                 default:
92                         return -EINVAL;
93                 }
94         }
95
96         ((u32 *)regs)[off] = val;
97         return 0;
98 }
99
100 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
101 {
102         return -EINVAL;
103 }
104
105 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
106 {
107         return -EINVAL;
108 }
109
110 #define NUM_TIMER_REGS 3
111
112 static bool is_timer_reg(u64 index)
113 {
114         switch (index) {
115         case KVM_REG_ARM_TIMER_CTL:
116         case KVM_REG_ARM_TIMER_CNT:
117         case KVM_REG_ARM_TIMER_CVAL:
118                 return true;
119         }
120         return false;
121 }
122
123 static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
124 {
125         if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
126                 return -EFAULT;
127         uindices++;
128         if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
129                 return -EFAULT;
130         uindices++;
131         if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
132                 return -EFAULT;
133
134         return 0;
135 }
136
137 static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
138 {
139         void __user *uaddr = (void __user *)(long)reg->addr;
140         u64 val;
141         int ret;
142
143         ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
144         if (ret != 0)
145                 return -EFAULT;
146
147         return kvm_arm_timer_set_reg(vcpu, reg->id, val);
148 }
149
150 static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
151 {
152         void __user *uaddr = (void __user *)(long)reg->addr;
153         u64 val;
154
155         val = kvm_arm_timer_get_reg(vcpu, reg->id);
156         return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id)) ? -EFAULT : 0;
157 }
158
159 static unsigned long num_core_regs(void)
160 {
161         return sizeof(struct kvm_regs) / sizeof(u32);
162 }
163
164 /**
165  * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
166  *
167  * This is for all registers.
168  */
169 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
170 {
171         return num_core_regs() + kvm_arm_num_coproc_regs(vcpu)
172                 + kvm_arm_get_fw_num_regs(vcpu)
173                 + NUM_TIMER_REGS;
174 }
175
176 /**
177  * kvm_arm_copy_reg_indices - get indices of all registers.
178  *
179  * We do core registers right here, then we append coproc regs.
180  */
181 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
182 {
183         unsigned int i;
184         const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
185         int ret;
186
187         for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
188                 if (put_user(core_reg | i, uindices))
189                         return -EFAULT;
190                 uindices++;
191         }
192
193         ret = kvm_arm_copy_fw_reg_indices(vcpu, uindices);
194         if (ret)
195                 return ret;
196         uindices += kvm_arm_get_fw_num_regs(vcpu);
197
198         ret = copy_timer_indices(vcpu, uindices);
199         if (ret)
200                 return ret;
201         uindices += NUM_TIMER_REGS;
202
203         return kvm_arm_copy_coproc_indices(vcpu, uindices);
204 }
205
206 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
207 {
208         /* We currently use nothing arch-specific in upper 32 bits */
209         if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
210                 return -EINVAL;
211
212         /* Register group 16 means we want a core register. */
213         if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
214                 return get_core_reg(vcpu, reg);
215
216         if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_FW)
217                 return kvm_arm_get_fw_reg(vcpu, reg);
218
219         if (is_timer_reg(reg->id))
220                 return get_timer_reg(vcpu, reg);
221
222         return kvm_arm_coproc_get_reg(vcpu, reg);
223 }
224
225 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
226 {
227         /* We currently use nothing arch-specific in upper 32 bits */
228         if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
229                 return -EINVAL;
230
231         /* Register group 16 means we set a core register. */
232         if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
233                 return set_core_reg(vcpu, reg);
234
235         if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_FW)
236                 return kvm_arm_set_fw_reg(vcpu, reg);
237
238         if (is_timer_reg(reg->id))
239                 return set_timer_reg(vcpu, reg);
240
241         return kvm_arm_coproc_set_reg(vcpu, reg);
242 }
243
244 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
245                                   struct kvm_sregs *sregs)
246 {
247         return -EINVAL;
248 }
249
250 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
251                                   struct kvm_sregs *sregs)
252 {
253         return -EINVAL;
254 }
255
256
257 int __kvm_arm_vcpu_get_events(struct kvm_vcpu *vcpu,
258                               struct kvm_vcpu_events *events)
259 {
260         events->exception.serror_pending = !!(*vcpu_hcr(vcpu) & HCR_VA);
261
262         /*
263          * We never return a pending ext_dabt here because we deliver it to
264          * the virtual CPU directly when setting the event and it's no longer
265          * 'pending' at this point.
266          */
267
268         return 0;
269 }
270
271 int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu,
272                               struct kvm_vcpu_events *events)
273 {
274         bool serror_pending = events->exception.serror_pending;
275         bool has_esr = events->exception.serror_has_esr;
276         bool ext_dabt_pending = events->exception.ext_dabt_pending;
277
278         if (serror_pending && has_esr)
279                 return -EINVAL;
280         else if (serror_pending)
281                 kvm_inject_vabt(vcpu);
282
283         if (ext_dabt_pending)
284                 kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu));
285
286         return 0;
287 }
288
289 int __attribute_const__ kvm_target_cpu(void)
290 {
291         switch (read_cpuid_part()) {
292         case ARM_CPU_PART_CORTEX_A7:
293                 return KVM_ARM_TARGET_CORTEX_A7;
294         case ARM_CPU_PART_CORTEX_A15:
295                 return KVM_ARM_TARGET_CORTEX_A15;
296         default:
297                 return -EINVAL;
298         }
299 }
300
301 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
302 {
303         int target = kvm_target_cpu();
304
305         if (target < 0)
306                 return -ENODEV;
307
308         memset(init, 0, sizeof(*init));
309
310         /*
311          * For now, we don't return any features.
312          * In future, we might use features to return target
313          * specific features available for the preferred
314          * target type.
315          */
316         init->target = (__u32)target;
317
318         return 0;
319 }
320
321 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
322 {
323         return -EINVAL;
324 }
325
326 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
327 {
328         return -EINVAL;
329 }
330
331 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
332                                   struct kvm_translation *tr)
333 {
334         return -EINVAL;
335 }
336
337 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
338                                         struct kvm_guest_debug *dbg)
339 {
340         return -EINVAL;
341 }
342
343 int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,
344                                struct kvm_device_attr *attr)
345 {
346         int ret;
347
348         switch (attr->group) {
349         case KVM_ARM_VCPU_TIMER_CTRL:
350                 ret = kvm_arm_timer_set_attr(vcpu, attr);
351                 break;
352         default:
353                 ret = -ENXIO;
354                 break;
355         }
356
357         return ret;
358 }
359
360 int kvm_arm_vcpu_arch_get_attr(struct kvm_vcpu *vcpu,
361                                struct kvm_device_attr *attr)
362 {
363         int ret;
364
365         switch (attr->group) {
366         case KVM_ARM_VCPU_TIMER_CTRL:
367                 ret = kvm_arm_timer_get_attr(vcpu, attr);
368                 break;
369         default:
370                 ret = -ENXIO;
371                 break;
372         }
373
374         return ret;
375 }
376
377 int kvm_arm_vcpu_arch_has_attr(struct kvm_vcpu *vcpu,
378                                struct kvm_device_attr *attr)
379 {
380         int ret;
381
382         switch (attr->group) {
383         case KVM_ARM_VCPU_TIMER_CTRL:
384                 ret = kvm_arm_timer_has_attr(vcpu, attr);
385                 break;
386         default:
387                 ret = -ENXIO;
388                 break;
389         }
390
391         return ret;
392 }