]>
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> | |
544c6761 | 26 | #include <linux/hrtimer.h> |
bbf45ba5 | 27 | #include <linux/fs.h> |
5a0e3ad6 | 28 | #include <linux/slab.h> |
bbf45ba5 HB |
29 | #include <asm/cputable.h> |
30 | #include <asm/uaccess.h> | |
31 | #include <asm/kvm_ppc.h> | |
83aae4a8 | 32 | #include <asm/tlbflush.h> |
73e75b41 | 33 | #include "timing.h" |
fad7b9b5 | 34 | #include "../mm/mmu_decl.h" |
bbf45ba5 | 35 | |
46f43c6e MT |
36 | #define CREATE_TRACE_POINTS |
37 | #include "trace.h" | |
38 | ||
bbf45ba5 HB |
39 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *v) |
40 | { | |
666e7252 AG |
41 | return !(v->arch.shared->msr & MSR_WE) || |
42 | !!(v->arch.pending_exceptions); | |
bbf45ba5 HB |
43 | } |
44 | ||
2a342ed5 AG |
45 | int kvmppc_kvm_pv(struct kvm_vcpu *vcpu) |
46 | { | |
47 | int nr = kvmppc_get_gpr(vcpu, 11); | |
48 | int r; | |
49 | unsigned long __maybe_unused param1 = kvmppc_get_gpr(vcpu, 3); | |
50 | unsigned long __maybe_unused param2 = kvmppc_get_gpr(vcpu, 4); | |
51 | unsigned long __maybe_unused param3 = kvmppc_get_gpr(vcpu, 5); | |
52 | unsigned long __maybe_unused param4 = kvmppc_get_gpr(vcpu, 6); | |
53 | unsigned long r2 = 0; | |
54 | ||
55 | if (!(vcpu->arch.shared->msr & MSR_SF)) { | |
56 | /* 32 bit mode */ | |
57 | param1 &= 0xffffffff; | |
58 | param2 &= 0xffffffff; | |
59 | param3 &= 0xffffffff; | |
60 | param4 &= 0xffffffff; | |
61 | } | |
62 | ||
63 | switch (nr) { | |
5fc87407 AG |
64 | case HC_VENDOR_KVM | KVM_HC_PPC_MAP_MAGIC_PAGE: |
65 | { | |
66 | vcpu->arch.magic_page_pa = param1; | |
67 | vcpu->arch.magic_page_ea = param2; | |
68 | ||
df1bfa25 | 69 | r2 = KVM_MAGIC_FEAT_SR; |
7508e16c | 70 | |
5fc87407 AG |
71 | r = HC_EV_SUCCESS; |
72 | break; | |
73 | } | |
2a342ed5 AG |
74 | case HC_VENDOR_KVM | KVM_HC_FEATURES: |
75 | r = HC_EV_SUCCESS; | |
5fc87407 AG |
76 | #if defined(CONFIG_PPC_BOOK3S) /* XXX Missing magic page on BookE */ |
77 | r2 |= (1 << KVM_FEATURE_MAGIC_PAGE); | |
78 | #endif | |
2a342ed5 AG |
79 | |
80 | /* Second return value is in r4 */ | |
2a342ed5 AG |
81 | break; |
82 | default: | |
83 | r = HC_EV_UNIMPLEMENTED; | |
84 | break; | |
85 | } | |
86 | ||
7508e16c AG |
87 | kvmppc_set_gpr(vcpu, 4, r2); |
88 | ||
2a342ed5 AG |
89 | return r; |
90 | } | |
bbf45ba5 HB |
91 | |
92 | int kvmppc_emulate_mmio(struct kvm_run *run, struct kvm_vcpu *vcpu) | |
93 | { | |
94 | enum emulation_result er; | |
95 | int r; | |
96 | ||
97 | er = kvmppc_emulate_instruction(run, vcpu); | |
98 | switch (er) { | |
99 | case EMULATE_DONE: | |
100 | /* Future optimization: only reload non-volatiles if they were | |
101 | * actually modified. */ | |
102 | r = RESUME_GUEST_NV; | |
103 | break; | |
104 | case EMULATE_DO_MMIO: | |
105 | run->exit_reason = KVM_EXIT_MMIO; | |
106 | /* We must reload nonvolatiles because "update" load/store | |
107 | * instructions modify register state. */ | |
108 | /* Future optimization: only reload non-volatiles if they were | |
109 | * actually modified. */ | |
110 | r = RESUME_HOST_NV; | |
111 | break; | |
112 | case EMULATE_FAIL: | |
113 | /* XXX Deliver Program interrupt to guest. */ | |
114 | printk(KERN_EMERG "%s: emulation failed (%08x)\n", __func__, | |
c7f38f46 | 115 | kvmppc_get_last_inst(vcpu)); |
bbf45ba5 HB |
116 | r = RESUME_HOST; |
117 | break; | |
118 | default: | |
119 | BUG(); | |
120 | } | |
121 | ||
122 | return r; | |
123 | } | |
124 | ||
10474ae8 | 125 | int kvm_arch_hardware_enable(void *garbage) |
bbf45ba5 | 126 | { |
10474ae8 | 127 | return 0; |
bbf45ba5 HB |
128 | } |
129 | ||
130 | void kvm_arch_hardware_disable(void *garbage) | |
131 | { | |
132 | } | |
133 | ||
134 | int kvm_arch_hardware_setup(void) | |
135 | { | |
136 | return 0; | |
137 | } | |
138 | ||
139 | void kvm_arch_hardware_unsetup(void) | |
140 | { | |
141 | } | |
142 | ||
143 | void kvm_arch_check_processor_compat(void *rtn) | |
144 | { | |
9dd921cf | 145 | *(int *)rtn = kvmppc_core_check_processor_compat(); |
bbf45ba5 HB |
146 | } |
147 | ||
d89f5eff | 148 | int kvm_arch_init_vm(struct kvm *kvm) |
bbf45ba5 | 149 | { |
d89f5eff | 150 | return 0; |
bbf45ba5 HB |
151 | } |
152 | ||
d89f5eff | 153 | void kvm_arch_destroy_vm(struct kvm *kvm) |
bbf45ba5 HB |
154 | { |
155 | unsigned int i; | |
988a2cae | 156 | struct kvm_vcpu *vcpu; |
bbf45ba5 | 157 | |
988a2cae GN |
158 | kvm_for_each_vcpu(i, vcpu, kvm) |
159 | kvm_arch_vcpu_free(vcpu); | |
160 | ||
161 | mutex_lock(&kvm->lock); | |
162 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) | |
163 | kvm->vcpus[i] = NULL; | |
164 | ||
165 | atomic_set(&kvm->online_vcpus, 0); | |
166 | mutex_unlock(&kvm->lock); | |
bbf45ba5 HB |
167 | } |
168 | ||
ad8ba2cd SY |
169 | void kvm_arch_sync_events(struct kvm *kvm) |
170 | { | |
171 | } | |
172 | ||
bbf45ba5 HB |
173 | int kvm_dev_ioctl_check_extension(long ext) |
174 | { | |
175 | int r; | |
176 | ||
177 | switch (ext) { | |
e15a1137 | 178 | case KVM_CAP_PPC_SEGSTATE: |
c10207fe | 179 | case KVM_CAP_PPC_PAIRED_SINGLES: |
18978768 | 180 | case KVM_CAP_PPC_UNSET_IRQ: |
7b4203e8 | 181 | case KVM_CAP_PPC_IRQ_LEVEL: |
71fbfd5f | 182 | case KVM_CAP_ENABLE_CAP: |
ad0a048b | 183 | case KVM_CAP_PPC_OSI: |
15711e9c | 184 | case KVM_CAP_PPC_GET_PVINFO: |
e15a1137 AG |
185 | r = 1; |
186 | break; | |
588968b6 LV |
187 | case KVM_CAP_COALESCED_MMIO: |
188 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; | |
189 | break; | |
bbf45ba5 HB |
190 | default: |
191 | r = 0; | |
192 | break; | |
193 | } | |
194 | return r; | |
195 | ||
196 | } | |
197 | ||
198 | long kvm_arch_dev_ioctl(struct file *filp, | |
199 | unsigned int ioctl, unsigned long arg) | |
200 | { | |
201 | return -EINVAL; | |
202 | } | |
203 | ||
f7784b8e MT |
204 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
205 | struct kvm_memory_slot *memslot, | |
206 | struct kvm_memory_slot old, | |
207 | struct kvm_userspace_memory_region *mem, | |
208 | int user_alloc) | |
bbf45ba5 HB |
209 | { |
210 | return 0; | |
211 | } | |
212 | ||
f7784b8e MT |
213 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
214 | struct kvm_userspace_memory_region *mem, | |
215 | struct kvm_memory_slot old, | |
216 | int user_alloc) | |
217 | { | |
218 | return; | |
219 | } | |
220 | ||
221 | ||
34d4cb8f MT |
222 | void kvm_arch_flush_shadow(struct kvm *kvm) |
223 | { | |
224 | } | |
225 | ||
bbf45ba5 HB |
226 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
227 | { | |
73e75b41 HB |
228 | struct kvm_vcpu *vcpu; |
229 | vcpu = kvmppc_core_vcpu_create(kvm, id); | |
06056bfb WY |
230 | if (!IS_ERR(vcpu)) |
231 | kvmppc_create_vcpu_debugfs(vcpu, id); | |
73e75b41 | 232 | return vcpu; |
bbf45ba5 HB |
233 | } |
234 | ||
235 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) | |
236 | { | |
a595405d AG |
237 | /* Make sure we're not using the vcpu anymore */ |
238 | hrtimer_cancel(&vcpu->arch.dec_timer); | |
239 | tasklet_kill(&vcpu->arch.tasklet); | |
240 | ||
73e75b41 | 241 | kvmppc_remove_vcpu_debugfs(vcpu); |
db93f574 | 242 | kvmppc_core_vcpu_free(vcpu); |
bbf45ba5 HB |
243 | } |
244 | ||
245 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) | |
246 | { | |
247 | kvm_arch_vcpu_free(vcpu); | |
248 | } | |
249 | ||
250 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) | |
251 | { | |
9dd921cf | 252 | return kvmppc_core_pending_dec(vcpu); |
bbf45ba5 HB |
253 | } |
254 | ||
255 | static void kvmppc_decrementer_func(unsigned long data) | |
256 | { | |
257 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; | |
258 | ||
9dd921cf | 259 | kvmppc_core_queue_dec(vcpu); |
45c5eb67 HB |
260 | |
261 | if (waitqueue_active(&vcpu->wq)) { | |
262 | wake_up_interruptible(&vcpu->wq); | |
263 | vcpu->stat.halt_wakeup++; | |
264 | } | |
bbf45ba5 HB |
265 | } |
266 | ||
544c6761 AG |
267 | /* |
268 | * low level hrtimer wake routine. Because this runs in hardirq context | |
269 | * we schedule a tasklet to do the real work. | |
270 | */ | |
271 | enum hrtimer_restart kvmppc_decrementer_wakeup(struct hrtimer *timer) | |
272 | { | |
273 | struct kvm_vcpu *vcpu; | |
274 | ||
275 | vcpu = container_of(timer, struct kvm_vcpu, arch.dec_timer); | |
276 | tasklet_schedule(&vcpu->arch.tasklet); | |
277 | ||
278 | return HRTIMER_NORESTART; | |
279 | } | |
280 | ||
bbf45ba5 HB |
281 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
282 | { | |
544c6761 AG |
283 | hrtimer_init(&vcpu->arch.dec_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS); |
284 | tasklet_init(&vcpu->arch.tasklet, kvmppc_decrementer_func, (ulong)vcpu); | |
285 | vcpu->arch.dec_timer.function = kvmppc_decrementer_wakeup; | |
bbf45ba5 | 286 | |
09000adb BB |
287 | #ifdef CONFIG_KVM_EXIT_TIMING |
288 | mutex_init(&vcpu->arch.exit_timing_lock); | |
289 | #endif | |
290 | ||
bbf45ba5 HB |
291 | return 0; |
292 | } | |
293 | ||
294 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) | |
295 | { | |
ecc0981f | 296 | kvmppc_mmu_destroy(vcpu); |
bbf45ba5 HB |
297 | } |
298 | ||
299 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) | |
300 | { | |
eab17672 SW |
301 | #ifdef CONFIG_BOOKE |
302 | /* | |
303 | * vrsave (formerly usprg0) isn't used by Linux, but may | |
304 | * be used by the guest. | |
305 | * | |
306 | * On non-booke this is associated with Altivec and | |
307 | * is handled by code in book3s.c. | |
308 | */ | |
309 | mtspr(SPRN_VRSAVE, vcpu->arch.vrsave); | |
310 | #endif | |
9dd921cf | 311 | kvmppc_core_vcpu_load(vcpu, cpu); |
bbf45ba5 HB |
312 | } |
313 | ||
314 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) | |
315 | { | |
9dd921cf | 316 | kvmppc_core_vcpu_put(vcpu); |
eab17672 SW |
317 | #ifdef CONFIG_BOOKE |
318 | vcpu->arch.vrsave = mfspr(SPRN_VRSAVE); | |
319 | #endif | |
bbf45ba5 HB |
320 | } |
321 | ||
d0bfb940 | 322 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
f5d0906b | 323 | struct kvm_guest_debug *dbg) |
bbf45ba5 | 324 | { |
f5d0906b | 325 | return -EINVAL; |
bbf45ba5 HB |
326 | } |
327 | ||
328 | static void kvmppc_complete_dcr_load(struct kvm_vcpu *vcpu, | |
329 | struct kvm_run *run) | |
330 | { | |
8e5b26b5 | 331 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, run->dcr.data); |
bbf45ba5 HB |
332 | } |
333 | ||
334 | static void kvmppc_complete_mmio_load(struct kvm_vcpu *vcpu, | |
335 | struct kvm_run *run) | |
336 | { | |
69b61833 | 337 | u64 uninitialized_var(gpr); |
bbf45ba5 | 338 | |
8e5b26b5 | 339 | if (run->mmio.len > sizeof(gpr)) { |
bbf45ba5 HB |
340 | printk(KERN_ERR "bad MMIO length: %d\n", run->mmio.len); |
341 | return; | |
342 | } | |
343 | ||
344 | if (vcpu->arch.mmio_is_bigendian) { | |
345 | switch (run->mmio.len) { | |
b104d066 | 346 | case 8: gpr = *(u64 *)run->mmio.data; break; |
8e5b26b5 AG |
347 | case 4: gpr = *(u32 *)run->mmio.data; break; |
348 | case 2: gpr = *(u16 *)run->mmio.data; break; | |
349 | case 1: gpr = *(u8 *)run->mmio.data; break; | |
bbf45ba5 HB |
350 | } |
351 | } else { | |
352 | /* Convert BE data from userland back to LE. */ | |
353 | switch (run->mmio.len) { | |
8e5b26b5 AG |
354 | case 4: gpr = ld_le32((u32 *)run->mmio.data); break; |
355 | case 2: gpr = ld_le16((u16 *)run->mmio.data); break; | |
356 | case 1: gpr = *(u8 *)run->mmio.data; break; | |
bbf45ba5 HB |
357 | } |
358 | } | |
8e5b26b5 | 359 | |
3587d534 AG |
360 | if (vcpu->arch.mmio_sign_extend) { |
361 | switch (run->mmio.len) { | |
362 | #ifdef CONFIG_PPC64 | |
363 | case 4: | |
364 | gpr = (s64)(s32)gpr; | |
365 | break; | |
366 | #endif | |
367 | case 2: | |
368 | gpr = (s64)(s16)gpr; | |
369 | break; | |
370 | case 1: | |
371 | gpr = (s64)(s8)gpr; | |
372 | break; | |
373 | } | |
374 | } | |
375 | ||
8e5b26b5 | 376 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); |
b104d066 AG |
377 | |
378 | switch (vcpu->arch.io_gpr & KVM_REG_EXT_MASK) { | |
379 | case KVM_REG_GPR: | |
380 | kvmppc_set_gpr(vcpu, vcpu->arch.io_gpr, gpr); | |
381 | break; | |
382 | case KVM_REG_FPR: | |
383 | vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; | |
384 | break; | |
287d5611 | 385 | #ifdef CONFIG_PPC_BOOK3S |
b104d066 AG |
386 | case KVM_REG_QPR: |
387 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; | |
388 | break; | |
389 | case KVM_REG_FQPR: | |
390 | vcpu->arch.fpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; | |
391 | vcpu->arch.qpr[vcpu->arch.io_gpr & KVM_REG_MASK] = gpr; | |
392 | break; | |
287d5611 | 393 | #endif |
b104d066 AG |
394 | default: |
395 | BUG(); | |
396 | } | |
bbf45ba5 HB |
397 | } |
398 | ||
399 | int kvmppc_handle_load(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
400 | unsigned int rt, unsigned int bytes, int is_bigendian) | |
401 | { | |
402 | if (bytes > sizeof(run->mmio.data)) { | |
403 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, | |
404 | run->mmio.len); | |
405 | } | |
406 | ||
407 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; | |
408 | run->mmio.len = bytes; | |
409 | run->mmio.is_write = 0; | |
410 | ||
411 | vcpu->arch.io_gpr = rt; | |
412 | vcpu->arch.mmio_is_bigendian = is_bigendian; | |
413 | vcpu->mmio_needed = 1; | |
414 | vcpu->mmio_is_write = 0; | |
3587d534 | 415 | vcpu->arch.mmio_sign_extend = 0; |
bbf45ba5 HB |
416 | |
417 | return EMULATE_DO_MMIO; | |
418 | } | |
419 | ||
3587d534 AG |
420 | /* Same as above, but sign extends */ |
421 | int kvmppc_handle_loads(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
422 | unsigned int rt, unsigned int bytes, int is_bigendian) | |
423 | { | |
424 | int r; | |
425 | ||
426 | r = kvmppc_handle_load(run, vcpu, rt, bytes, is_bigendian); | |
427 | vcpu->arch.mmio_sign_extend = 1; | |
428 | ||
429 | return r; | |
430 | } | |
431 | ||
bbf45ba5 | 432 | int kvmppc_handle_store(struct kvm_run *run, struct kvm_vcpu *vcpu, |
b104d066 | 433 | u64 val, unsigned int bytes, int is_bigendian) |
bbf45ba5 HB |
434 | { |
435 | void *data = run->mmio.data; | |
436 | ||
437 | if (bytes > sizeof(run->mmio.data)) { | |
438 | printk(KERN_ERR "%s: bad MMIO length: %d\n", __func__, | |
439 | run->mmio.len); | |
440 | } | |
441 | ||
442 | run->mmio.phys_addr = vcpu->arch.paddr_accessed; | |
443 | run->mmio.len = bytes; | |
444 | run->mmio.is_write = 1; | |
445 | vcpu->mmio_needed = 1; | |
446 | vcpu->mmio_is_write = 1; | |
447 | ||
448 | /* Store the value at the lowest bytes in 'data'. */ | |
449 | if (is_bigendian) { | |
450 | switch (bytes) { | |
b104d066 | 451 | case 8: *(u64 *)data = val; break; |
bbf45ba5 HB |
452 | case 4: *(u32 *)data = val; break; |
453 | case 2: *(u16 *)data = val; break; | |
454 | case 1: *(u8 *)data = val; break; | |
455 | } | |
456 | } else { | |
457 | /* Store LE value into 'data'. */ | |
458 | switch (bytes) { | |
459 | case 4: st_le32(data, val); break; | |
460 | case 2: st_le16(data, val); break; | |
461 | case 1: *(u8 *)data = val; break; | |
462 | } | |
463 | } | |
464 | ||
465 | return EMULATE_DO_MMIO; | |
466 | } | |
467 | ||
468 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) | |
469 | { | |
470 | int r; | |
471 | sigset_t sigsaved; | |
472 | ||
473 | if (vcpu->sigset_active) | |
474 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); | |
475 | ||
476 | if (vcpu->mmio_needed) { | |
477 | if (!vcpu->mmio_is_write) | |
478 | kvmppc_complete_mmio_load(vcpu, run); | |
479 | vcpu->mmio_needed = 0; | |
480 | } else if (vcpu->arch.dcr_needed) { | |
481 | if (!vcpu->arch.dcr_is_write) | |
482 | kvmppc_complete_dcr_load(vcpu, run); | |
483 | vcpu->arch.dcr_needed = 0; | |
ad0a048b AG |
484 | } else if (vcpu->arch.osi_needed) { |
485 | u64 *gprs = run->osi.gprs; | |
486 | int i; | |
487 | ||
488 | for (i = 0; i < 32; i++) | |
489 | kvmppc_set_gpr(vcpu, i, gprs[i]); | |
490 | vcpu->arch.osi_needed = 0; | |
bbf45ba5 HB |
491 | } |
492 | ||
9dd921cf | 493 | kvmppc_core_deliver_interrupts(vcpu); |
bbf45ba5 HB |
494 | |
495 | local_irq_disable(); | |
496 | kvm_guest_enter(); | |
497 | r = __kvmppc_vcpu_run(run, vcpu); | |
498 | kvm_guest_exit(); | |
499 | local_irq_enable(); | |
500 | ||
501 | if (vcpu->sigset_active) | |
502 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); | |
503 | ||
504 | return r; | |
505 | } | |
506 | ||
507 | int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq) | |
508 | { | |
18978768 AG |
509 | if (irq->irq == KVM_INTERRUPT_UNSET) |
510 | kvmppc_core_dequeue_external(vcpu, irq); | |
511 | else | |
512 | kvmppc_core_queue_external(vcpu, irq); | |
45c5eb67 HB |
513 | |
514 | if (waitqueue_active(&vcpu->wq)) { | |
515 | wake_up_interruptible(&vcpu->wq); | |
516 | vcpu->stat.halt_wakeup++; | |
517 | } | |
518 | ||
bbf45ba5 HB |
519 | return 0; |
520 | } | |
521 | ||
71fbfd5f AG |
522 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
523 | struct kvm_enable_cap *cap) | |
524 | { | |
525 | int r; | |
526 | ||
527 | if (cap->flags) | |
528 | return -EINVAL; | |
529 | ||
530 | switch (cap->cap) { | |
ad0a048b AG |
531 | case KVM_CAP_PPC_OSI: |
532 | r = 0; | |
533 | vcpu->arch.osi_enabled = true; | |
534 | break; | |
71fbfd5f AG |
535 | default: |
536 | r = -EINVAL; | |
537 | break; | |
538 | } | |
539 | ||
540 | return r; | |
541 | } | |
542 | ||
bbf45ba5 HB |
543 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
544 | struct kvm_mp_state *mp_state) | |
545 | { | |
546 | return -EINVAL; | |
547 | } | |
548 | ||
549 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
550 | struct kvm_mp_state *mp_state) | |
551 | { | |
552 | return -EINVAL; | |
553 | } | |
554 | ||
555 | long kvm_arch_vcpu_ioctl(struct file *filp, | |
556 | unsigned int ioctl, unsigned long arg) | |
557 | { | |
558 | struct kvm_vcpu *vcpu = filp->private_data; | |
559 | void __user *argp = (void __user *)arg; | |
560 | long r; | |
561 | ||
93736624 AK |
562 | switch (ioctl) { |
563 | case KVM_INTERRUPT: { | |
bbf45ba5 HB |
564 | struct kvm_interrupt irq; |
565 | r = -EFAULT; | |
566 | if (copy_from_user(&irq, argp, sizeof(irq))) | |
93736624 | 567 | goto out; |
bbf45ba5 | 568 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
93736624 | 569 | goto out; |
bbf45ba5 | 570 | } |
19483d14 | 571 | |
71fbfd5f AG |
572 | case KVM_ENABLE_CAP: |
573 | { | |
574 | struct kvm_enable_cap cap; | |
575 | r = -EFAULT; | |
576 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
577 | goto out; | |
578 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); | |
579 | break; | |
580 | } | |
bbf45ba5 HB |
581 | default: |
582 | r = -EINVAL; | |
583 | } | |
584 | ||
585 | out: | |
586 | return r; | |
587 | } | |
588 | ||
15711e9c AG |
589 | static int kvm_vm_ioctl_get_pvinfo(struct kvm_ppc_pvinfo *pvinfo) |
590 | { | |
591 | u32 inst_lis = 0x3c000000; | |
592 | u32 inst_ori = 0x60000000; | |
593 | u32 inst_nop = 0x60000000; | |
594 | u32 inst_sc = 0x44000002; | |
595 | u32 inst_imm_mask = 0xffff; | |
596 | ||
597 | /* | |
598 | * The hypercall to get into KVM from within guest context is as | |
599 | * follows: | |
600 | * | |
601 | * lis r0, r0, KVM_SC_MAGIC_R0@h | |
602 | * ori r0, KVM_SC_MAGIC_R0@l | |
603 | * sc | |
604 | * nop | |
605 | */ | |
606 | pvinfo->hcall[0] = inst_lis | ((KVM_SC_MAGIC_R0 >> 16) & inst_imm_mask); | |
607 | pvinfo->hcall[1] = inst_ori | (KVM_SC_MAGIC_R0 & inst_imm_mask); | |
608 | pvinfo->hcall[2] = inst_sc; | |
609 | pvinfo->hcall[3] = inst_nop; | |
610 | ||
611 | return 0; | |
612 | } | |
613 | ||
bbf45ba5 HB |
614 | long kvm_arch_vm_ioctl(struct file *filp, |
615 | unsigned int ioctl, unsigned long arg) | |
616 | { | |
15711e9c | 617 | void __user *argp = (void __user *)arg; |
bbf45ba5 HB |
618 | long r; |
619 | ||
620 | switch (ioctl) { | |
15711e9c AG |
621 | case KVM_PPC_GET_PVINFO: { |
622 | struct kvm_ppc_pvinfo pvinfo; | |
d8cdddcd | 623 | memset(&pvinfo, 0, sizeof(pvinfo)); |
15711e9c AG |
624 | r = kvm_vm_ioctl_get_pvinfo(&pvinfo); |
625 | if (copy_to_user(argp, &pvinfo, sizeof(pvinfo))) { | |
626 | r = -EFAULT; | |
627 | goto out; | |
628 | } | |
629 | ||
630 | break; | |
631 | } | |
bbf45ba5 | 632 | default: |
367e1319 | 633 | r = -ENOTTY; |
bbf45ba5 HB |
634 | } |
635 | ||
15711e9c | 636 | out: |
bbf45ba5 HB |
637 | return r; |
638 | } | |
639 | ||
640 | int kvm_arch_init(void *opaque) | |
641 | { | |
642 | return 0; | |
643 | } | |
644 | ||
645 | void kvm_arch_exit(void) | |
646 | { | |
647 | } |