Merge tag 'edac_for_4.6' of git://git.kernel.org/pub/scm/linux/kernel/git/bp/bp
[cascardo/linux.git] / arch / arm / kvm / guest.c
1 /*
2  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3  * Author: 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 #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 <asm/cputype.h>
26 #include <asm/uaccess.h>
27 #include <asm/kvm.h>
28 #include <asm/kvm_asm.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
35 struct 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
45 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
46 {
47         return 0;
48 }
49
50 static 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
55 static 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.regs;
59         u64 off;
60
61         if (KVM_REG_SIZE(reg->id) != 4)
62                 return -ENOENT;
63
64         /* Our ID is an index into the kvm_regs struct. */
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
72 static 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.regs;
76         u64 off, val;
77
78         if (KVM_REG_SIZE(reg->id) != 4)
79                 return -ENOENT;
80
81         /* Our ID is an index into the kvm_regs struct. */
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
108 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
109 {
110         return -EINVAL;
111 }
112
113 int 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
120 static 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
131 static 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
145 static 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
158 static 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
167 static unsigned long num_core_regs(void)
168 {
169         return sizeof(struct kvm_regs) / sizeof(u32);
170 }
171
172 /**
173  * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
174  *
175  * This is for all registers.
176  */
177 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
178 {
179         return num_core_regs() + kvm_arm_num_coproc_regs(vcpu)
180                 + NUM_TIMER_REGS;
181 }
182
183 /**
184  * kvm_arm_copy_reg_indices - get indices of all registers.
185  *
186  * We do core registers right here, then we apppend coproc regs.
187  */
188 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
189 {
190         unsigned int i;
191         const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
192         int ret;
193
194         for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
195                 if (put_user(core_reg | i, uindices))
196                         return -EFAULT;
197                 uindices++;
198         }
199
200         ret = copy_timer_indices(vcpu, uindices);
201         if (ret)
202                 return ret;
203         uindices += NUM_TIMER_REGS;
204
205         return kvm_arm_copy_coproc_indices(vcpu, uindices);
206 }
207
208 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
209 {
210         /* We currently use nothing arch-specific in upper 32 bits */
211         if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
212                 return -EINVAL;
213
214         /* Register group 16 means we want a core register. */
215         if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
216                 return get_core_reg(vcpu, reg);
217
218         if (is_timer_reg(reg->id))
219                 return get_timer_reg(vcpu, reg);
220
221         return kvm_arm_coproc_get_reg(vcpu, reg);
222 }
223
224 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
225 {
226         /* We currently use nothing arch-specific in upper 32 bits */
227         if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
228                 return -EINVAL;
229
230         /* Register group 16 means we set a core register. */
231         if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
232                 return set_core_reg(vcpu, reg);
233
234         if (is_timer_reg(reg->id))
235                 return set_timer_reg(vcpu, reg);
236
237         return kvm_arm_coproc_set_reg(vcpu, reg);
238 }
239
240 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
241                                   struct kvm_sregs *sregs)
242 {
243         return -EINVAL;
244 }
245
246 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
247                                   struct kvm_sregs *sregs)
248 {
249         return -EINVAL;
250 }
251
252 int __attribute_const__ kvm_target_cpu(void)
253 {
254         switch (read_cpuid_part()) {
255         case ARM_CPU_PART_CORTEX_A7:
256                 return KVM_ARM_TARGET_CORTEX_A7;
257         case ARM_CPU_PART_CORTEX_A15:
258                 return KVM_ARM_TARGET_CORTEX_A15;
259         default:
260                 return -EINVAL;
261         }
262 }
263
264 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
265 {
266         int target = kvm_target_cpu();
267
268         if (target < 0)
269                 return -ENODEV;
270
271         memset(init, 0, sizeof(*init));
272
273         /*
274          * For now, we don't return any features.
275          * In future, we might use features to return target
276          * specific features available for the preferred
277          * target type.
278          */
279         init->target = (__u32)target;
280
281         return 0;
282 }
283
284 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
285 {
286         return -EINVAL;
287 }
288
289 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
290 {
291         return -EINVAL;
292 }
293
294 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
295                                   struct kvm_translation *tr)
296 {
297         return -EINVAL;
298 }
299
300 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
301                                         struct kvm_guest_debug *dbg)
302 {
303         return -EINVAL;
304 }