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