Merge remote-tracking branch 'spi/fix/core' into spi-linus
[linux-drm-fsl-dcu.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         { NULL }
37 };
38
39 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
40 {
41         return 0;
42 }
43
44 static 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
49 static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
50 {
51         u32 __user *uaddr = (u32 __user *)(long)reg->addr;
52         struct kvm_regs *regs = &vcpu->arch.regs;
53         u64 off;
54
55         if (KVM_REG_SIZE(reg->id) != 4)
56                 return -ENOENT;
57
58         /* Our ID is an index into the kvm_regs struct. */
59         off = core_reg_offset_from_id(reg->id);
60         if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
61                 return -ENOENT;
62
63         return put_user(((u32 *)regs)[off], uaddr);
64 }
65
66 static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
67 {
68         u32 __user *uaddr = (u32 __user *)(long)reg->addr;
69         struct kvm_regs *regs = &vcpu->arch.regs;
70         u64 off, val;
71
72         if (KVM_REG_SIZE(reg->id) != 4)
73                 return -ENOENT;
74
75         /* Our ID is an index into the kvm_regs struct. */
76         off = core_reg_offset_from_id(reg->id);
77         if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
78                 return -ENOENT;
79
80         if (get_user(val, uaddr) != 0)
81                 return -EFAULT;
82
83         if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) {
84                 unsigned long mode = val & MODE_MASK;
85                 switch (mode) {
86                 case USR_MODE:
87                 case FIQ_MODE:
88                 case IRQ_MODE:
89                 case SVC_MODE:
90                 case ABT_MODE:
91                 case UND_MODE:
92                         break;
93                 default:
94                         return -EINVAL;
95                 }
96         }
97
98         ((u32 *)regs)[off] = val;
99         return 0;
100 }
101
102 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
103 {
104         return -EINVAL;
105 }
106
107 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
108 {
109         return -EINVAL;
110 }
111
112 static unsigned long num_core_regs(void)
113 {
114         return sizeof(struct kvm_regs) / sizeof(u32);
115 }
116
117 /**
118  * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
119  *
120  * This is for all registers.
121  */
122 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
123 {
124         return num_core_regs() + kvm_arm_num_coproc_regs(vcpu);
125 }
126
127 /**
128  * kvm_arm_copy_reg_indices - get indices of all registers.
129  *
130  * We do core registers right here, then we apppend coproc regs.
131  */
132 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
133 {
134         unsigned int i;
135         const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
136
137         for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
138                 if (put_user(core_reg | i, uindices))
139                         return -EFAULT;
140                 uindices++;
141         }
142
143         return kvm_arm_copy_coproc_indices(vcpu, uindices);
144 }
145
146 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
147 {
148         /* We currently use nothing arch-specific in upper 32 bits */
149         if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
150                 return -EINVAL;
151
152         /* Register group 16 means we want a core register. */
153         if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
154                 return get_core_reg(vcpu, reg);
155
156         return kvm_arm_coproc_get_reg(vcpu, reg);
157 }
158
159 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
160 {
161         /* We currently use nothing arch-specific in upper 32 bits */
162         if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
163                 return -EINVAL;
164
165         /* Register group 16 means we set a core register. */
166         if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
167                 return set_core_reg(vcpu, reg);
168
169         return kvm_arm_coproc_set_reg(vcpu, reg);
170 }
171
172 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
173                                   struct kvm_sregs *sregs)
174 {
175         return -EINVAL;
176 }
177
178 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
179                                   struct kvm_sregs *sregs)
180 {
181         return -EINVAL;
182 }
183
184 int __attribute_const__ kvm_target_cpu(void)
185 {
186         unsigned long implementor = read_cpuid_implementor();
187         unsigned long part_number = read_cpuid_part_number();
188
189         if (implementor != ARM_CPU_IMP_ARM)
190                 return -EINVAL;
191
192         switch (part_number) {
193         case ARM_CPU_PART_CORTEX_A7:
194                 return KVM_ARM_TARGET_CORTEX_A7;
195         case ARM_CPU_PART_CORTEX_A15:
196                 return KVM_ARM_TARGET_CORTEX_A15;
197         default:
198                 return -EINVAL;
199         }
200 }
201
202 int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
203                         const struct kvm_vcpu_init *init)
204 {
205         unsigned int i;
206
207         /* We can only cope with guest==host and only on A15/A7 (for now). */
208         if (init->target != kvm_target_cpu())
209                 return -EINVAL;
210
211         vcpu->arch.target = init->target;
212         bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
213
214         /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
215         for (i = 0; i < sizeof(init->features) * 8; i++) {
216                 if (test_bit(i, (void *)init->features)) {
217                         if (i >= KVM_VCPU_MAX_FEATURES)
218                                 return -ENOENT;
219                         set_bit(i, vcpu->arch.features);
220                 }
221         }
222
223         /* Now we know what it is, we can reset it. */
224         return kvm_reset_vcpu(vcpu);
225 }
226
227 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
228 {
229         int target = kvm_target_cpu();
230
231         if (target < 0)
232                 return -ENODEV;
233
234         memset(init, 0, sizeof(*init));
235
236         /*
237          * For now, we don't return any features.
238          * In future, we might use features to return target
239          * specific features available for the preferred
240          * target type.
241          */
242         init->target = (__u32)target;
243
244         return 0;
245 }
246
247 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
248 {
249         return -EINVAL;
250 }
251
252 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
253 {
254         return -EINVAL;
255 }
256
257 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
258                                   struct kvm_translation *tr)
259 {
260         return -EINVAL;
261 }