Commit | Line | Data |
---|---|---|
bbf45ba5 HB |
1 | /* |
2 | * This program is free software; you can redistribute it and/or modify | |
3 | * it under the terms of the GNU General Public License, version 2, as | |
4 | * published by the Free Software Foundation. | |
5 | * | |
6 | * This program is distributed in the hope that it will be useful, | |
7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 | * GNU General Public License for more details. | |
10 | * | |
11 | * You should have received a copy of the GNU General Public License | |
12 | * along with this program; if not, write to the Free Software | |
13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
14 | * | |
15 | * Copyright IBM Corp. 2007 | |
16 | * | |
17 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> | |
18 | * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> | |
19 | */ | |
20 | ||
21 | #include <linux/errno.h> | |
22 | #include <linux/err.h> | |
23 | #include <linux/kvm_host.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/vmalloc.h> | |
26 | #include <linux/fs.h> | |
27 | #include <asm/cputable.h> | |
28 | #include <asm/uaccess.h> | |
29 | #include <asm/kvm_ppc.h> | |
83aae4a8 | 30 | #include <asm/tlbflush.h> |
73e75b41 | 31 | #include "timing.h" |
fad7b9b5 | 32 | #include "../mm/mmu_decl.h" |
bbf45ba5 | 33 | |
46f43c6e MT |
34 | #define CREATE_TRACE_POINTS |
35 | #include "trace.h" | |
36 | ||
bbf45ba5 HB |
37 | gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn) |
38 | { | |
39 | return gfn; | |
40 | } | |
41 | ||
42 | int kvm_cpu_has_interrupt(struct kvm_vcpu *v) | |
43 | { | |
45c5eb67 | 44 | return !!(v->arch.pending_exceptions); |
bbf45ba5 HB |
45 | } |
46 | ||
78646121 GN |
47 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) |
48 | { | |
49 | /* do real check here */ | |
50 | return 1; | |
51 | } | |
52 | ||
bbf45ba5 HB |
53 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
54 | { | |
45c5eb67 | 55 | return !(v->arch.msr & MSR_WE); |
bbf45ba5 HB |
56 | } |
57 | ||
58 | ||
59 | int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) | |
60 | { | |
61 | enum emulation_result er; | |
62 | int r; | |
63 | ||
64 | er = kvmppc_emulate_instruction(run, vcpu); | |
65 | switch (er) { | |
66 | case EMULATE_DONE: | |
67 | /* Future optimization: only reload non-volatiles if they were | |
68 | * actually modified. */ | |
69 | r = RESUME_GUEST_NV; | |
70 | break; | |
71 | case EMULATE_DO_MMIO: | |
72 | run->exit_reason = KVM_EXIT_MMIO; | |
73 | /* We must reload nonvolatiles because "update" load/store | |
74 | * instructions modify register state. */ | |
75 | /* Future optimization: only reload non-volatiles if they were | |
76 | * actually modified. */ | |
77 | r = RESUME_HOST_NV; | |
78 | break; | |
79 | case EMULATE_FAIL: | |
80 | /* XXX Deliver Program interrupt to guest. */ | |
81 | printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__, | |
82 | vcpu->arch.last_inst); | |
83 | r = RESUME_HOST; | |
84 | break; | |
85 | default: | |
86 | BUG(); | |
87 | } | |
88 | ||
89 | return r; | |
90 | } | |
91 | ||
92 | void kvm_arch_hardware_enable(void *garbage) | |
93 | { | |
94 | } | |
95 | ||
96 | void kvm_arch_hardware_disable(void *garbage) | |
97 | { | |
98 | } | |
99 | ||
100 | int kvm_arch_hardware_setup(void) | |
101 | { | |
102 | return 0; | |
103 | } | |
104 | ||
105 | void kvm_arch_hardware_unsetup(void) | |
106 | { | |
107 | } | |
108 | ||
109 | void kvm_arch_check_processor_compat(void *rtn) | |
110 | { | |
9dd921cf | 111 | *(int *)rtn = kvmppc_core_check_processor_compat(); |
bbf45ba5 HB |
112 | } |
113 | ||
114 | struct kvm *kvm_arch_create_vm(void) | |
115 | { | |
116 | struct kvm *kvm; | |
117 | ||
118 | kvm = kzalloc(sizeof(struct kvm), GFP_KERNEL); | |
119 | if (!kvm) | |
120 | return ERR_PTR(-ENOMEM); | |
121 | ||
122 | return kvm; | |
123 | } | |
124 | ||
125 | static void kvmppc_free_vcpus(struct kvm *kvm) | |
126 | { | |
127 | unsigned int i; | |
988a2cae | 128 | struct kvm_vcpu *vcpu; |
bbf45ba5 | 129 | |
988a2cae GN |
130 | kvm_for_each_vcpu(i, vcpu, kvm) |
131 | kvm_arch_vcpu_free(vcpu); | |
132 | ||
133 | mutex_lock(&kvm->lock); | |
134 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) | |
135 | kvm->vcpus[i] = NULL; | |
136 | ||
137 | atomic_set(&kvm->online_vcpus, 0); | |
138 | mutex_unlock(&kvm->lock); | |
bbf45ba5 HB |
139 | } |
140 | ||
ad8ba2cd SY |
141 | void kvm_arch_sync_events(struct kvm *kvm) |
142 | { | |
143 | } | |
144 | ||
bbf45ba5 HB |
145 | void kvm_arch_destroy_vm(struct kvm *kvm) |
146 | { | |
147 | kvmppc_free_vcpus(kvm); | |
148 | kvm_free_physmem(kvm); | |
149 | kfree(kvm); | |
150 | } | |
151 | ||
152 | int kvm_dev_ioctl_check_extension(long ext) | |
153 | { | |
154 | int r; | |
155 | ||
156 | switch (ext) { | |
588968b6 LV |
157 | case KVM_CAP_COALESCED_MMIO: |
158 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; | |
159 | break; | |
bbf45ba5 HB |
160 | default: |
161 | r = 0; | |
162 | break; | |
163 | } | |
164 | return r; | |
165 | ||
166 | } | |
167 | ||
168 | long kvm_arch_dev_ioctl(struct file *filp, | |
169 | unsigned int ioctl, unsigned long arg) | |
170 | { | |
171 | return -EINVAL; | |
172 | } | |
173 | ||
174 | int kvm_arch_set_memory_region(struct kvm *kvm, | |
175 | struct kvm_userspace_memory_region *mem, | |
176 | struct kvm_memory_slot old, | |
177 | int user_alloc) | |
178 | { | |
179 | return 0; | |
180 | } | |
181 | ||
34d4cb8f MT |
182 | void kvm_arch_flush_shadow(struct kvm *kvm) |
183 | { | |
184 | } | |
185 | ||
bbf45ba5 HB |
186 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
187 | { | |
73e75b41 HB |
188 | struct kvm_vcpu *vcpu; |
189 | vcpu = kvmppc_core_vcpu_create(kvm, id); | |
190 | kvmppc_create_vcpu_debugfs(vcpu, id); | |
191 | return vcpu; | |
bbf45ba5 HB |
192 | } |
193 | ||
194 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) | |
195 | { | |
73e75b41 | 196 | kvmppc_remove_vcpu_debugfs(vcpu); |
db93f574 | 197 | kvmppc_core_vcpu_free(vcpu); |
bbf45ba5 HB |
198 | } |
199 | ||
200 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) | |
201 | { | |
202 | kvm_arch_vcpu_free(vcpu); | |
203 | } | |
204 | ||
205 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) | |
206 | { | |
9dd921cf | 207 | return kvmppc_core_pending_dec(vcpu); |
bbf45ba5 HB |
208 | } |
209 | ||
210 | static void kvmppc_decrementer_func(unsigned long data) | |
211 | { | |
212 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; | |
213 | ||
9dd921cf | 214 | kvmppc_core_queue_dec(vcpu); |
45c5eb67 HB |
215 | |
216 | if (waitqueue_active(&vcpu->wq)) { | |
217 | wake_up_interruptible(&vcpu->wq); | |
218 | vcpu->stat.halt_wakeup++; | |
219 | } | |
bbf45ba5 HB |
220 | } |
221 | ||
222 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) | |
223 | { | |
224 | setup_timer(&vcpu->arch.dec_timer, kvmppc_decrementer_func, | |
225 | (unsigned long)vcpu); | |
226 | ||
227 | return 0; | |
228 | } | |
229 | ||
230 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) | |
231 | { | |
ecc0981f | 232 | kvmppc_mmu_destroy(vcpu); |
bbf45ba5 HB |
233 | } |
234 | ||
235 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) | |
236 | { | |
9dd921cf | 237 | kvmppc_core_vcpu_load(vcpu, cpu); |
bbf45ba5 HB |
238 | } |
239 | ||
240 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | |
241 | { | |
9dd921cf | 242 | kvmppc_core_vcpu_put(vcpu); |
bbf45ba5 HB |
243 | } |
244 | ||
d0bfb940 | 245 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
f5d0906b | 246 | struct kvm_guest_debug *dbg) |
bbf45ba5 | 247 | { |
f5d0906b | 248 | return -EINVAL; |
bbf45ba5 HB |
249 | } |
250 | ||
251 | static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu, | |
252 | struct kvm_run *run) | |
253 | { | |
5cf8ca22 | 254 | ulong *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr]; |
bbf45ba5 HB |
255 | *gpr = run->dcr.data; |
256 | } | |
257 | ||
258 | static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, | |
259 | struct kvm_run *run) | |
260 | { | |
5cf8ca22 | 261 | ulong *gpr = &vcpu->arch.gpr[vcpu->arch.io_gpr]; |
bbf45ba5 HB |
262 | |
263 | if (run->mmio.len > sizeof(*gpr)) { | |
264 | printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); | |
265 | return; | |
266 | } | |
267 | ||
268 | if (vcpu->arch.mmio_is_bigendian) { | |
269 | switch (run->mmio.len) { | |
270 | case 4: *gpr = *(u32 *)run->mmio.data; break; | |
271 | case 2: *gpr = *(u16 *)run->mmio.data; break; | |
272 | case 1: *gpr = *(u8 *)run->mmio.data; break; | |
273 | } | |
274 | } else { | |
275 | /* Convert BE data from userland back to LE. */ | |
276 | switch (run->mmio.len) { | |
277 | case 4: *gpr = ld_le32((u32 *)run->mmio.data); break; | |
278 | case 2: *gpr = ld_le16((u16 *)run->mmio.data); break; | |
279 | case 1: *gpr = *(u8 *)run->mmio.data; break; | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
285 | unsigned int rt, unsigned int bytes, int is_bigendian) | |
286 | { | |
287 | if (bytes > sizeof(run->mmio.data)) { | |
288 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, | |
289 | run->mmio.len); | |
290 | } | |
291 | ||
292 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; | |
293 | run->mmio.len = bytes; | |
294 | run->mmio.is_write = 0; | |
295 | ||
296 | vcpu->arch.io_gpr = rt; | |
297 | vcpu->arch.mmio_is_bigendian = is_bigendian; | |
298 | vcpu->mmio_needed = 1; | |
299 | vcpu->mmio_is_write = 0; | |
300 | ||
301 | return EMULATE_DO_MMIO; | |
302 | } | |
303 | ||
304 | int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
305 | u32 val, unsigned int bytes, int is_bigendian) | |
306 | { | |
307 | void *data = run->mmio.data; | |
308 | ||
309 | if (bytes > sizeof(run->mmio.data)) { | |
310 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, | |
311 | run->mmio.len); | |
312 | } | |
313 | ||
314 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; | |
315 | run->mmio.len = bytes; | |
316 | run->mmio.is_write = 1; | |
317 | vcpu->mmio_needed = 1; | |
318 | vcpu->mmio_is_write = 1; | |
319 | ||
320 | /* Store the value at the lowest bytes in 'data'. */ | |
321 | if (is_bigendian) { | |
322 | switch (bytes) { | |
323 | case 4: *(u32 *)data = val; break; | |
324 | case 2: *(u16 *)data = val; break; | |
325 | case 1: *(u8 *)data = val; break; | |
326 | } | |
327 | } else { | |
328 | /* Store LE value into 'data'. */ | |
329 | switch (bytes) { | |
330 | case 4: st_le32(data, val); break; | |
331 | case 2: st_le16(data, val); break; | |
332 | case 1: *(u8 *)data = val; break; | |
333 | } | |
334 | } | |
335 | ||
336 | return EMULATE_DO_MMIO; | |
337 | } | |
338 | ||
339 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) | |
340 | { | |
341 | int r; | |
342 | sigset_t sigsaved; | |
343 | ||
45c5eb67 HB |
344 | vcpu_load(vcpu); |
345 | ||
bbf45ba5 HB |
346 | if (vcpu->sigset_active) |
347 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
348 | ||
349 | if (vcpu->mmio_needed) { | |
350 | if (!vcpu->mmio_is_write) | |
351 | kvmppc_complete_mmio_load(vcpu, run); | |
352 | vcpu->mmio_needed = 0; | |
353 | } else if (vcpu->arch.dcr_needed) { | |
354 | if (!vcpu->arch.dcr_is_write) | |
355 | kvmppc_complete_dcr_load(vcpu, run); | |
356 | vcpu->arch.dcr_needed = 0; | |
357 | } | |
358 | ||
9dd921cf | 359 | kvmppc_core_deliver_interrupts(vcpu); |
bbf45ba5 HB |
360 | |
361 | local_irq_disable(); | |
362 | kvm_guest_enter(); | |
363 | r = __kvmppc_vcpu_run(run, vcpu); | |
364 | kvm_guest_exit(); | |
365 | local_irq_enable(); | |
366 | ||
367 | if (vcpu->sigset_active) | |
368 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
369 | ||
45c5eb67 HB |
370 | vcpu_put(vcpu); |
371 | ||
bbf45ba5 HB |
372 | return r; |
373 | } | |
374 | ||
375 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) | |
376 | { | |
9dd921cf | 377 | kvmppc_core_queue_external(vcpu, irq); |
45c5eb67 HB |
378 | |
379 | if (waitqueue_active(&vcpu->wq)) { | |
380 | wake_up_interruptible(&vcpu->wq); | |
381 | vcpu->stat.halt_wakeup++; | |
382 | } | |
383 | ||
bbf45ba5 HB |
384 | return 0; |
385 | } | |
386 | ||
387 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, | |
388 | struct kvm_mp_state *mp_state) | |
389 | { | |
390 | return -EINVAL; | |
391 | } | |
392 | ||
393 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
394 | struct kvm_mp_state *mp_state) | |
395 | { | |
396 | return -EINVAL; | |
397 | } | |
398 | ||
399 | long kvm_arch_vcpu_ioctl(struct file *filp, | |
400 | unsigned int ioctl, unsigned long arg) | |
401 | { | |
402 | struct kvm_vcpu *vcpu = filp->private_data; | |
403 | void __user *argp = (void __user *)arg; | |
404 | long r; | |
405 | ||
406 | switch (ioctl) { | |
407 | case KVM_INTERRUPT: { | |
408 | struct kvm_interrupt irq; | |
409 | r = -EFAULT; | |
410 | if (copy_from_user(&irq, argp, sizeof(irq))) | |
411 | goto out; | |
412 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); | |
413 | break; | |
414 | } | |
415 | default: | |
416 | r = -EINVAL; | |
417 | } | |
418 | ||
419 | out: | |
420 | return r; | |
421 | } | |
422 | ||
423 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) | |
424 | { | |
425 | return -ENOTSUPP; | |
426 | } | |
427 | ||
428 | long kvm_arch_vm_ioctl(struct file *filp, | |
429 | unsigned int ioctl, unsigned long arg) | |
430 | { | |
431 | long r; | |
432 | ||
433 | switch (ioctl) { | |
434 | default: | |
435 | r = -EINVAL; | |
436 | } | |
437 | ||
438 | return r; | |
439 | } | |
440 | ||
441 | int kvm_arch_init(void *opaque) | |
442 | { | |
443 | return 0; | |
444 | } | |
445 | ||
446 | void kvm_arch_exit(void) | |
447 | { | |
448 | } |