Commit | Line | Data |
---|---|---|
749cf76c CD |
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> | |
728d577d | 25 | #include <asm/cputype.h> |
749cf76c CD |
26 | #include <asm/uaccess.h> |
27 | #include <asm/kvm.h> | |
28 | #include <asm/kvm_asm.h> | |
29 | #include <asm/kvm_emulate.h> | |
1138245c | 30 | #include <asm/kvm_coproc.h> |
749cf76c CD |
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 | { | |
ac30a11e | 41 | vcpu->arch.hcr = HCR_GUEST_MASK; |
749cf76c CD |
42 | return 0; |
43 | } | |
44 | ||
45 | static u64 core_reg_offset_from_id(u64 id) | |
46 | { | |
47 | return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE); | |
48 | } | |
49 | ||
50 | static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | |
51 | { | |
52 | u32 __user *uaddr = (u32 __user *)(long)reg->addr; | |
53 | struct kvm_regs *regs = &vcpu->arch.regs; | |
54 | u64 off; | |
55 | ||
56 | if (KVM_REG_SIZE(reg->id) != 4) | |
57 | return -ENOENT; | |
58 | ||
59 | /* Our ID is an index into the kvm_regs struct. */ | |
60 | off = core_reg_offset_from_id(reg->id); | |
61 | if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id)) | |
62 | return -ENOENT; | |
63 | ||
64 | return put_user(((u32 *)regs)[off], uaddr); | |
65 | } | |
66 | ||
67 | static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | |
68 | { | |
69 | u32 __user *uaddr = (u32 __user *)(long)reg->addr; | |
70 | struct kvm_regs *regs = &vcpu->arch.regs; | |
71 | u64 off, val; | |
72 | ||
73 | if (KVM_REG_SIZE(reg->id) != 4) | |
74 | return -ENOENT; | |
75 | ||
76 | /* Our ID is an index into the kvm_regs struct. */ | |
77 | off = core_reg_offset_from_id(reg->id); | |
78 | if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id)) | |
79 | return -ENOENT; | |
80 | ||
81 | if (get_user(val, uaddr) != 0) | |
82 | return -EFAULT; | |
83 | ||
84 | if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) { | |
85 | unsigned long mode = val & MODE_MASK; | |
86 | switch (mode) { | |
87 | case USR_MODE: | |
88 | case FIQ_MODE: | |
89 | case IRQ_MODE: | |
90 | case SVC_MODE: | |
91 | case ABT_MODE: | |
92 | case UND_MODE: | |
93 | break; | |
94 | default: | |
95 | return -EINVAL; | |
96 | } | |
97 | } | |
98 | ||
99 | ((u32 *)regs)[off] = val; | |
100 | return 0; | |
101 | } | |
102 | ||
103 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
104 | { | |
105 | return -EINVAL; | |
106 | } | |
107 | ||
108 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | |
109 | { | |
110 | return -EINVAL; | |
111 | } | |
112 | ||
39735a3a AP |
113 | #ifndef CONFIG_KVM_ARM_TIMER |
114 | ||
115 | #define NUM_TIMER_REGS 0 | |
116 | ||
117 | static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) | |
118 | { | |
119 | return 0; | |
120 | } | |
121 | ||
122 | static bool is_timer_reg(u64 index) | |
123 | { | |
124 | return false; | |
125 | } | |
126 | ||
127 | int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) | |
128 | { | |
129 | return 0; | |
130 | } | |
131 | ||
132 | u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid) | |
133 | { | |
134 | return 0; | |
135 | } | |
136 | ||
137 | #else | |
138 | ||
139 | #define NUM_TIMER_REGS 3 | |
140 | ||
141 | static bool is_timer_reg(u64 index) | |
142 | { | |
143 | switch (index) { | |
144 | case KVM_REG_ARM_TIMER_CTL: | |
145 | case KVM_REG_ARM_TIMER_CNT: | |
146 | case KVM_REG_ARM_TIMER_CVAL: | |
147 | return true; | |
148 | } | |
149 | return false; | |
150 | } | |
151 | ||
152 | static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) | |
153 | { | |
154 | if (put_user(KVM_REG_ARM_TIMER_CTL, uindices)) | |
155 | return -EFAULT; | |
156 | uindices++; | |
157 | if (put_user(KVM_REG_ARM_TIMER_CNT, uindices)) | |
158 | return -EFAULT; | |
159 | uindices++; | |
160 | if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices)) | |
161 | return -EFAULT; | |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
166 | #endif | |
167 | ||
168 | static 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 ret; | |
177 | ||
178 | return kvm_arm_timer_set_reg(vcpu, reg->id, val); | |
179 | } | |
180 | ||
181 | static 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 | ||
749cf76c CD |
190 | static unsigned long num_core_regs(void) |
191 | { | |
192 | return sizeof(struct kvm_regs) / sizeof(u32); | |
193 | } | |
194 | ||
195 | /** | |
196 | * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG | |
197 | * | |
198 | * This is for all registers. | |
199 | */ | |
200 | unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu) | |
201 | { | |
39735a3a AP |
202 | return num_core_regs() + kvm_arm_num_coproc_regs(vcpu) |
203 | + NUM_TIMER_REGS; | |
749cf76c CD |
204 | } |
205 | ||
206 | /** | |
207 | * kvm_arm_copy_reg_indices - get indices of all registers. | |
208 | * | |
209 | * We do core registers right here, then we apppend coproc regs. | |
210 | */ | |
211 | int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) | |
212 | { | |
213 | unsigned int i; | |
214 | const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE; | |
39735a3a | 215 | int ret; |
749cf76c CD |
216 | |
217 | for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) { | |
218 | if (put_user(core_reg | i, uindices)) | |
219 | return -EFAULT; | |
220 | uindices++; | |
221 | } | |
222 | ||
39735a3a AP |
223 | ret = copy_timer_indices(vcpu, uindices); |
224 | if (ret) | |
225 | return ret; | |
226 | uindices += NUM_TIMER_REGS; | |
227 | ||
1138245c | 228 | return kvm_arm_copy_coproc_indices(vcpu, uindices); |
749cf76c CD |
229 | } |
230 | ||
231 | int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | |
232 | { | |
233 | /* We currently use nothing arch-specific in upper 32 bits */ | |
234 | if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32) | |
235 | return -EINVAL; | |
236 | ||
237 | /* Register group 16 means we want a core register. */ | |
238 | if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE) | |
239 | return get_core_reg(vcpu, reg); | |
240 | ||
39735a3a AP |
241 | if (is_timer_reg(reg->id)) |
242 | return get_timer_reg(vcpu, reg); | |
243 | ||
1138245c | 244 | return kvm_arm_coproc_get_reg(vcpu, reg); |
749cf76c CD |
245 | } |
246 | ||
247 | int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | |
248 | { | |
249 | /* We currently use nothing arch-specific in upper 32 bits */ | |
250 | if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32) | |
251 | return -EINVAL; | |
252 | ||
253 | /* Register group 16 means we set a core register. */ | |
254 | if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE) | |
255 | return set_core_reg(vcpu, reg); | |
39735a3a AP |
256 | |
257 | if (is_timer_reg(reg->id)) | |
258 | return set_timer_reg(vcpu, reg); | |
749cf76c | 259 | |
1138245c | 260 | return kvm_arm_coproc_set_reg(vcpu, reg); |
749cf76c CD |
261 | } |
262 | ||
263 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | |
264 | struct kvm_sregs *sregs) | |
265 | { | |
266 | return -EINVAL; | |
267 | } | |
268 | ||
269 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
270 | struct kvm_sregs *sregs) | |
271 | { | |
272 | return -EINVAL; | |
273 | } | |
274 | ||
728d577d MZ |
275 | int __attribute_const__ kvm_target_cpu(void) |
276 | { | |
af040ffc | 277 | switch (read_cpuid_part()) { |
e8c2d99f JA |
278 | case ARM_CPU_PART_CORTEX_A7: |
279 | return KVM_ARM_TARGET_CORTEX_A7; | |
728d577d MZ |
280 | case ARM_CPU_PART_CORTEX_A15: |
281 | return KVM_ARM_TARGET_CORTEX_A15; | |
282 | default: | |
283 | return -EINVAL; | |
284 | } | |
285 | } | |
286 | ||
749cf76c CD |
287 | int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, |
288 | const struct kvm_vcpu_init *init) | |
289 | { | |
290 | unsigned int i; | |
291 | ||
e8c2d99f | 292 | /* We can only cope with guest==host and only on A15/A7 (for now). */ |
749cf76c CD |
293 | if (init->target != kvm_target_cpu()) |
294 | return -EINVAL; | |
295 | ||
296 | vcpu->arch.target = init->target; | |
297 | bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); | |
298 | ||
299 | /* -ENOENT for unknown features, -EINVAL for invalid combinations. */ | |
300 | for (i = 0; i < sizeof(init->features) * 8; i++) { | |
301 | if (test_bit(i, (void *)init->features)) { | |
302 | if (i >= KVM_VCPU_MAX_FEATURES) | |
303 | return -ENOENT; | |
304 | set_bit(i, vcpu->arch.features); | |
305 | } | |
306 | } | |
307 | ||
308 | /* Now we know what it is, we can reset it. */ | |
309 | return kvm_reset_vcpu(vcpu); | |
310 | } | |
311 | ||
4a6fee80 AP |
312 | int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init) |
313 | { | |
314 | int target = kvm_target_cpu(); | |
315 | ||
316 | if (target < 0) | |
317 | return -ENODEV; | |
318 | ||
319 | memset(init, 0, sizeof(*init)); | |
320 | ||
321 | /* | |
322 | * For now, we don't return any features. | |
323 | * In future, we might use features to return target | |
324 | * specific features available for the preferred | |
325 | * target type. | |
326 | */ | |
327 | init->target = (__u32)target; | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
749cf76c CD |
332 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
333 | { | |
334 | return -EINVAL; | |
335 | } | |
336 | ||
337 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
338 | { | |
339 | return -EINVAL; | |
340 | } | |
341 | ||
342 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
343 | struct kvm_translation *tr) | |
344 | { | |
345 | return -EINVAL; | |
346 | } |