1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/kvm_host.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/preempt.h>
25#include <linux/sched.h>
26#include <linux/delay.h>
27#include <linux/export.h>
28#include <linux/fs.h>
29#include <linux/anon_inodes.h>
30#include <linux/cpumask.h>
31#include <linux/spinlock.h>
32#include <linux/page-flags.h>
33#include <linux/srcu.h>
34#include <linux/miscdevice.h>
35
36#include <asm/reg.h>
37#include <asm/cputable.h>
38#include <asm/cache.h>
39#include <asm/cacheflush.h>
40#include <asm/tlbflush.h>
41#include <asm/uaccess.h>
42#include <asm/io.h>
43#include <asm/kvm_ppc.h>
44#include <asm/kvm_book3s.h>
45#include <asm/mmu_context.h>
46#include <asm/lppaca.h>
47#include <asm/processor.h>
48#include <asm/cputhreads.h>
49#include <asm/page.h>
50#include <asm/hvcall.h>
51#include <asm/switch_to.h>
52#include <asm/smp.h>
53#include <linux/gfp.h>
54#include <linux/vmalloc.h>
55#include <linux/highmem.h>
56#include <linux/hugetlb.h>
57#include <linux/module.h>
58
59#include "book3s.h"
60
61
62
63
64
65
66#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
67
68
69#define TB_NIL (~(u64)0)
70
71static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
72
73#if defined(CONFIG_PPC_64K_PAGES)
74#define MPP_BUFFER_ORDER 0
75#elif defined(CONFIG_PPC_4K_PAGES)
76#define MPP_BUFFER_ORDER 3
77#endif
78
79
80static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
81static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
82
83static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
84{
85 int me;
86 int cpu = vcpu->cpu;
87 wait_queue_head_t *wqp;
88
89 wqp = kvm_arch_vcpu_wq(vcpu);
90 if (waitqueue_active(wqp)) {
91 wake_up_interruptible(wqp);
92 ++vcpu->stat.halt_wakeup;
93 }
94
95 me = get_cpu();
96
97
98 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
99#ifdef CONFIG_PPC_ICP_NATIVE
100 int real_cpu = cpu + vcpu->arch.ptid;
101 if (paca[real_cpu].kvm_hstate.xics_phys)
102 xics_wake_cpu(real_cpu);
103 else
104#endif
105 if (cpu_online(cpu))
106 smp_send_reschedule(cpu);
107 }
108 put_cpu();
109}
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
146{
147 struct kvmppc_vcore *vc = vcpu->arch.vcore;
148 unsigned long flags;
149
150 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
151 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
152 vc->preempt_tb != TB_NIL) {
153 vc->stolen_tb += mftb() - vc->preempt_tb;
154 vc->preempt_tb = TB_NIL;
155 }
156 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
157 vcpu->arch.busy_preempt != TB_NIL) {
158 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
159 vcpu->arch.busy_preempt = TB_NIL;
160 }
161 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
162}
163
164static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
165{
166 struct kvmppc_vcore *vc = vcpu->arch.vcore;
167 unsigned long flags;
168
169 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
170 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
171 vc->preempt_tb = mftb();
172 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
173 vcpu->arch.busy_preempt = mftb();
174 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
175}
176
177static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
178{
179 vcpu->arch.shregs.msr = msr;
180 kvmppc_end_cede(vcpu);
181}
182
183void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
184{
185 vcpu->arch.pvr = pvr;
186}
187
188int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
189{
190 unsigned long pcr = 0;
191 struct kvmppc_vcore *vc = vcpu->arch.vcore;
192
193 if (arch_compat) {
194 if (!cpu_has_feature(CPU_FTR_ARCH_206))
195 return -EINVAL;
196
197 switch (arch_compat) {
198 case PVR_ARCH_205:
199
200
201
202
203 pcr = PCR_ARCH_206 | PCR_ARCH_205;
204 break;
205 case PVR_ARCH_206:
206 case PVR_ARCH_206p:
207 pcr = PCR_ARCH_206;
208 break;
209 case PVR_ARCH_207:
210 break;
211 default:
212 return -EINVAL;
213 }
214
215 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
216
217 if (!(pcr & PCR_ARCH_206))
218 return -EINVAL;
219 pcr &= ~PCR_ARCH_206;
220 }
221 }
222
223 spin_lock(&vc->lock);
224 vc->arch_compat = arch_compat;
225 vc->pcr = pcr;
226 spin_unlock(&vc->lock);
227
228 return 0;
229}
230
231void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
232{
233 int r;
234
235 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
236 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
237 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
238 for (r = 0; r < 16; ++r)
239 pr_err("r%2d = %.16lx r%d = %.16lx\n",
240 r, kvmppc_get_gpr(vcpu, r),
241 r+16, kvmppc_get_gpr(vcpu, r+16));
242 pr_err("ctr = %.16lx lr = %.16lx\n",
243 vcpu->arch.ctr, vcpu->arch.lr);
244 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
245 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
246 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
247 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
248 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
249 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
250 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
251 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
252 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
253 pr_err("fault dar = %.16lx dsisr = %.8x\n",
254 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
255 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
256 for (r = 0; r < vcpu->arch.slb_max; ++r)
257 pr_err(" ESID = %.16llx VSID = %.16llx\n",
258 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
259 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
260 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
261 vcpu->arch.last_inst);
262}
263
264struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
265{
266 int r;
267 struct kvm_vcpu *v, *ret = NULL;
268
269 mutex_lock(&kvm->lock);
270 kvm_for_each_vcpu(r, v, kvm) {
271 if (v->vcpu_id == id) {
272 ret = v;
273 break;
274 }
275 }
276 mutex_unlock(&kvm->lock);
277 return ret;
278}
279
280static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
281{
282 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
283 vpa->yield_count = cpu_to_be32(1);
284}
285
286static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
287 unsigned long addr, unsigned long len)
288{
289
290 if (addr & (L1_CACHE_BYTES - 1))
291 return -EINVAL;
292 spin_lock(&vcpu->arch.vpa_update_lock);
293 if (v->next_gpa != addr || v->len != len) {
294 v->next_gpa = addr;
295 v->len = addr ? len : 0;
296 v->update_pending = 1;
297 }
298 spin_unlock(&vcpu->arch.vpa_update_lock);
299 return 0;
300}
301
302
303struct reg_vpa {
304 u32 dummy;
305 union {
306 __be16 hword;
307 __be32 word;
308 } length;
309};
310
311static int vpa_is_registered(struct kvmppc_vpa *vpap)
312{
313 if (vpap->update_pending)
314 return vpap->next_gpa != 0;
315 return vpap->pinned_addr != NULL;
316}
317
318static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
319 unsigned long flags,
320 unsigned long vcpuid, unsigned long vpa)
321{
322 struct kvm *kvm = vcpu->kvm;
323 unsigned long len, nb;
324 void *va;
325 struct kvm_vcpu *tvcpu;
326 int err;
327 int subfunc;
328 struct kvmppc_vpa *vpap;
329
330 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
331 if (!tvcpu)
332 return H_PARAMETER;
333
334 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
335 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
336 subfunc == H_VPA_REG_SLB) {
337
338 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
339 return H_PARAMETER;
340
341
342 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
343 if (va == NULL)
344 return H_PARAMETER;
345 if (subfunc == H_VPA_REG_VPA)
346 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
347 else
348 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
349 kvmppc_unpin_guest_page(kvm, va, vpa, false);
350
351
352 if (len > nb || len < sizeof(struct reg_vpa))
353 return H_PARAMETER;
354 } else {
355 vpa = 0;
356 len = 0;
357 }
358
359 err = H_PARAMETER;
360 vpap = NULL;
361 spin_lock(&tvcpu->arch.vpa_update_lock);
362
363 switch (subfunc) {
364 case H_VPA_REG_VPA:
365 if (len < sizeof(struct lppaca))
366 break;
367 vpap = &tvcpu->arch.vpa;
368 err = 0;
369 break;
370
371 case H_VPA_REG_DTL:
372 if (len < sizeof(struct dtl_entry))
373 break;
374 len -= len % sizeof(struct dtl_entry);
375
376
377 err = H_RESOURCE;
378 if (!vpa_is_registered(&tvcpu->arch.vpa))
379 break;
380
381 vpap = &tvcpu->arch.dtl;
382 err = 0;
383 break;
384
385 case H_VPA_REG_SLB:
386
387 err = H_RESOURCE;
388 if (!vpa_is_registered(&tvcpu->arch.vpa))
389 break;
390
391 vpap = &tvcpu->arch.slb_shadow;
392 err = 0;
393 break;
394
395 case H_VPA_DEREG_VPA:
396
397 err = H_RESOURCE;
398 if (vpa_is_registered(&tvcpu->arch.dtl) ||
399 vpa_is_registered(&tvcpu->arch.slb_shadow))
400 break;
401
402 vpap = &tvcpu->arch.vpa;
403 err = 0;
404 break;
405
406 case H_VPA_DEREG_DTL:
407 vpap = &tvcpu->arch.dtl;
408 err = 0;
409 break;
410
411 case H_VPA_DEREG_SLB:
412 vpap = &tvcpu->arch.slb_shadow;
413 err = 0;
414 break;
415 }
416
417 if (vpap) {
418 vpap->next_gpa = vpa;
419 vpap->len = len;
420 vpap->update_pending = 1;
421 }
422
423 spin_unlock(&tvcpu->arch.vpa_update_lock);
424
425 return err;
426}
427
428static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
429{
430 struct kvm *kvm = vcpu->kvm;
431 void *va;
432 unsigned long nb;
433 unsigned long gpa;
434
435
436
437
438
439
440
441
442
443 for (;;) {
444 gpa = vpap->next_gpa;
445 spin_unlock(&vcpu->arch.vpa_update_lock);
446 va = NULL;
447 nb = 0;
448 if (gpa)
449 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
450 spin_lock(&vcpu->arch.vpa_update_lock);
451 if (gpa == vpap->next_gpa)
452 break;
453
454 if (va)
455 kvmppc_unpin_guest_page(kvm, va, gpa, false);
456 }
457
458 vpap->update_pending = 0;
459 if (va && nb < vpap->len) {
460
461
462
463
464
465 kvmppc_unpin_guest_page(kvm, va, gpa, false);
466 va = NULL;
467 }
468 if (vpap->pinned_addr)
469 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
470 vpap->dirty);
471 vpap->gpa = gpa;
472 vpap->pinned_addr = va;
473 vpap->dirty = false;
474 if (va)
475 vpap->pinned_end = va + vpap->len;
476}
477
478static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
479{
480 if (!(vcpu->arch.vpa.update_pending ||
481 vcpu->arch.slb_shadow.update_pending ||
482 vcpu->arch.dtl.update_pending))
483 return;
484
485 spin_lock(&vcpu->arch.vpa_update_lock);
486 if (vcpu->arch.vpa.update_pending) {
487 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
488 if (vcpu->arch.vpa.pinned_addr)
489 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
490 }
491 if (vcpu->arch.dtl.update_pending) {
492 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
493 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
494 vcpu->arch.dtl_index = 0;
495 }
496 if (vcpu->arch.slb_shadow.update_pending)
497 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
498 spin_unlock(&vcpu->arch.vpa_update_lock);
499}
500
501
502
503
504
505static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
506{
507 u64 p;
508
509
510
511
512
513
514
515
516
517 if (vc->vcore_state != VCORE_INACTIVE &&
518 vc->runner->arch.run_task != current) {
519 spin_lock_irq(&vc->runner->arch.tbacct_lock);
520 p = vc->stolen_tb;
521 if (vc->preempt_tb != TB_NIL)
522 p += now - vc->preempt_tb;
523 spin_unlock_irq(&vc->runner->arch.tbacct_lock);
524 } else {
525 p = vc->stolen_tb;
526 }
527 return p;
528}
529
530static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
531 struct kvmppc_vcore *vc)
532{
533 struct dtl_entry *dt;
534 struct lppaca *vpa;
535 unsigned long stolen;
536 unsigned long core_stolen;
537 u64 now;
538
539 dt = vcpu->arch.dtl_ptr;
540 vpa = vcpu->arch.vpa.pinned_addr;
541 now = mftb();
542 core_stolen = vcore_stolen_time(vc, now);
543 stolen = core_stolen - vcpu->arch.stolen_logged;
544 vcpu->arch.stolen_logged = core_stolen;
545 spin_lock_irq(&vcpu->arch.tbacct_lock);
546 stolen += vcpu->arch.busy_stolen;
547 vcpu->arch.busy_stolen = 0;
548 spin_unlock_irq(&vcpu->arch.tbacct_lock);
549 if (!dt || !vpa)
550 return;
551 memset(dt, 0, sizeof(struct dtl_entry));
552 dt->dispatch_reason = 7;
553 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
554 dt->timebase = cpu_to_be64(now + vc->tb_offset);
555 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
556 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
557 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
558 ++dt;
559 if (dt == vcpu->arch.dtl.pinned_end)
560 dt = vcpu->arch.dtl.pinned_addr;
561 vcpu->arch.dtl_ptr = dt;
562
563 smp_wmb();
564 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
565 vcpu->arch.dtl.dirty = true;
566}
567
568static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
569{
570 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
571 return true;
572 if ((!vcpu->arch.vcore->arch_compat) &&
573 cpu_has_feature(CPU_FTR_ARCH_207S))
574 return true;
575 return false;
576}
577
578static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
579 unsigned long resource, unsigned long value1,
580 unsigned long value2)
581{
582 switch (resource) {
583 case H_SET_MODE_RESOURCE_SET_CIABR:
584 if (!kvmppc_power8_compatible(vcpu))
585 return H_P2;
586 if (value2)
587 return H_P4;
588 if (mflags)
589 return H_UNSUPPORTED_FLAG_START;
590
591 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
592 return H_P3;
593 vcpu->arch.ciabr = value1;
594 return H_SUCCESS;
595 case H_SET_MODE_RESOURCE_SET_DAWR:
596 if (!kvmppc_power8_compatible(vcpu))
597 return H_P2;
598 if (mflags)
599 return H_UNSUPPORTED_FLAG_START;
600 if (value2 & DABRX_HYP)
601 return H_P4;
602 vcpu->arch.dawr = value1;
603 vcpu->arch.dawrx = value2;
604 return H_SUCCESS;
605 default:
606 return H_TOO_HARD;
607 }
608}
609
610int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
611{
612 unsigned long req = kvmppc_get_gpr(vcpu, 3);
613 unsigned long target, ret = H_SUCCESS;
614 struct kvm_vcpu *tvcpu;
615 int idx, rc;
616
617 if (req <= MAX_HCALL_OPCODE &&
618 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
619 return RESUME_HOST;
620
621 switch (req) {
622 case H_ENTER:
623 idx = srcu_read_lock(&vcpu->kvm->srcu);
624 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
625 kvmppc_get_gpr(vcpu, 5),
626 kvmppc_get_gpr(vcpu, 6),
627 kvmppc_get_gpr(vcpu, 7));
628 srcu_read_unlock(&vcpu->kvm->srcu, idx);
629 break;
630 case H_CEDE:
631 break;
632 case H_PROD:
633 target = kvmppc_get_gpr(vcpu, 4);
634 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
635 if (!tvcpu) {
636 ret = H_PARAMETER;
637 break;
638 }
639 tvcpu->arch.prodded = 1;
640 smp_mb();
641 if (vcpu->arch.ceded) {
642 if (waitqueue_active(&vcpu->wq)) {
643 wake_up_interruptible(&vcpu->wq);
644 vcpu->stat.halt_wakeup++;
645 }
646 }
647 break;
648 case H_CONFER:
649 target = kvmppc_get_gpr(vcpu, 4);
650 if (target == -1)
651 break;
652 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
653 if (!tvcpu) {
654 ret = H_PARAMETER;
655 break;
656 }
657 kvm_vcpu_yield_to(tvcpu);
658 break;
659 case H_REGISTER_VPA:
660 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
661 kvmppc_get_gpr(vcpu, 5),
662 kvmppc_get_gpr(vcpu, 6));
663 break;
664 case H_RTAS:
665 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
666 return RESUME_HOST;
667
668 idx = srcu_read_lock(&vcpu->kvm->srcu);
669 rc = kvmppc_rtas_hcall(vcpu);
670 srcu_read_unlock(&vcpu->kvm->srcu, idx);
671
672 if (rc == -ENOENT)
673 return RESUME_HOST;
674 else if (rc == 0)
675 break;
676
677
678 return rc;
679 case H_SET_MODE:
680 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
681 kvmppc_get_gpr(vcpu, 5),
682 kvmppc_get_gpr(vcpu, 6),
683 kvmppc_get_gpr(vcpu, 7));
684 if (ret == H_TOO_HARD)
685 return RESUME_HOST;
686 break;
687 case H_XIRR:
688 case H_CPPR:
689 case H_EOI:
690 case H_IPI:
691 case H_IPOLL:
692 case H_XIRR_X:
693 if (kvmppc_xics_enabled(vcpu)) {
694 ret = kvmppc_xics_hcall(vcpu, req);
695 break;
696 }
697 default:
698 return RESUME_HOST;
699 }
700 kvmppc_set_gpr(vcpu, 3, ret);
701 vcpu->arch.hcall_needed = 0;
702 return RESUME_GUEST;
703}
704
705static int kvmppc_hcall_impl_hv(unsigned long cmd)
706{
707 switch (cmd) {
708 case H_CEDE:
709 case H_PROD:
710 case H_CONFER:
711 case H_REGISTER_VPA:
712 case H_SET_MODE:
713#ifdef CONFIG_KVM_XICS
714 case H_XIRR:
715 case H_CPPR:
716 case H_EOI:
717 case H_IPI:
718 case H_IPOLL:
719 case H_XIRR_X:
720#endif
721 return 1;
722 }
723
724
725 return kvmppc_hcall_impl_hv_realmode(cmd);
726}
727
728static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
729 struct task_struct *tsk)
730{
731 int r = RESUME_HOST;
732
733 vcpu->stat.sum_exits++;
734
735 run->exit_reason = KVM_EXIT_UNKNOWN;
736 run->ready_for_interrupt_injection = 1;
737 switch (vcpu->arch.trap) {
738
739 case BOOK3S_INTERRUPT_HV_DECREMENTER:
740 vcpu->stat.dec_exits++;
741 r = RESUME_GUEST;
742 break;
743 case BOOK3S_INTERRUPT_EXTERNAL:
744 case BOOK3S_INTERRUPT_H_DOORBELL:
745 vcpu->stat.ext_intr_exits++;
746 r = RESUME_GUEST;
747 break;
748 case BOOK3S_INTERRUPT_PERFMON:
749 r = RESUME_GUEST;
750 break;
751 case BOOK3S_INTERRUPT_MACHINE_CHECK:
752
753
754
755
756
757
758 kvmppc_book3s_queue_irqprio(vcpu,
759 BOOK3S_INTERRUPT_MACHINE_CHECK);
760 r = RESUME_GUEST;
761 break;
762 case BOOK3S_INTERRUPT_PROGRAM:
763 {
764 ulong flags;
765
766
767
768
769
770
771 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
772 kvmppc_core_queue_program(vcpu, flags);
773 r = RESUME_GUEST;
774 break;
775 }
776 case BOOK3S_INTERRUPT_SYSCALL:
777 {
778
779 int i;
780
781
782
783
784
785 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
786 for (i = 0; i < 9; ++i)
787 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
788 run->exit_reason = KVM_EXIT_PAPR_HCALL;
789 vcpu->arch.hcall_needed = 1;
790 r = RESUME_HOST;
791 break;
792 }
793
794
795
796
797
798
799
800 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
801 r = RESUME_PAGE_FAULT;
802 break;
803 case BOOK3S_INTERRUPT_H_INST_STORAGE:
804 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
805 vcpu->arch.fault_dsisr = 0;
806 r = RESUME_PAGE_FAULT;
807 break;
808
809
810
811
812
813 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
814 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
815 r = RESUME_GUEST;
816 break;
817
818
819
820
821
822 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
823 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
824 r = RESUME_GUEST;
825 break;
826 default:
827 kvmppc_dump_regs(vcpu);
828 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
829 vcpu->arch.trap, kvmppc_get_pc(vcpu),
830 vcpu->arch.shregs.msr);
831 run->hw.hardware_exit_reason = vcpu->arch.trap;
832 r = RESUME_HOST;
833 break;
834 }
835
836 return r;
837}
838
839static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
840 struct kvm_sregs *sregs)
841{
842 int i;
843
844 memset(sregs, 0, sizeof(struct kvm_sregs));
845 sregs->pvr = vcpu->arch.pvr;
846 for (i = 0; i < vcpu->arch.slb_max; i++) {
847 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
848 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
849 }
850
851 return 0;
852}
853
854static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
855 struct kvm_sregs *sregs)
856{
857 int i, j;
858
859 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
860
861 j = 0;
862 for (i = 0; i < vcpu->arch.slb_nr; i++) {
863 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
864 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
865 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
866 ++j;
867 }
868 }
869 vcpu->arch.slb_max = j;
870
871 return 0;
872}
873
874static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
875 bool preserve_top32)
876{
877 struct kvmppc_vcore *vc = vcpu->arch.vcore;
878 u64 mask;
879
880 spin_lock(&vc->lock);
881
882
883
884
885 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
886 struct kvm *kvm = vcpu->kvm;
887 struct kvm_vcpu *vcpu;
888 int i;
889
890 mutex_lock(&kvm->lock);
891 kvm_for_each_vcpu(i, vcpu, kvm) {
892 if (vcpu->arch.vcore != vc)
893 continue;
894 if (new_lpcr & LPCR_ILE)
895 vcpu->arch.intr_msr |= MSR_LE;
896 else
897 vcpu->arch.intr_msr &= ~MSR_LE;
898 }
899 mutex_unlock(&kvm->lock);
900 }
901
902
903
904
905
906
907 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
908 if (cpu_has_feature(CPU_FTR_ARCH_207S))
909 mask |= LPCR_AIL;
910
911
912 if (preserve_top32)
913 mask &= 0xFFFFFFFF;
914 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
915 spin_unlock(&vc->lock);
916}
917
918static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
919 union kvmppc_one_reg *val)
920{
921 int r = 0;
922 long int i;
923
924 switch (id) {
925 case KVM_REG_PPC_HIOR:
926 *val = get_reg_val(id, 0);
927 break;
928 case KVM_REG_PPC_DABR:
929 *val = get_reg_val(id, vcpu->arch.dabr);
930 break;
931 case KVM_REG_PPC_DABRX:
932 *val = get_reg_val(id, vcpu->arch.dabrx);
933 break;
934 case KVM_REG_PPC_DSCR:
935 *val = get_reg_val(id, vcpu->arch.dscr);
936 break;
937 case KVM_REG_PPC_PURR:
938 *val = get_reg_val(id, vcpu->arch.purr);
939 break;
940 case KVM_REG_PPC_SPURR:
941 *val = get_reg_val(id, vcpu->arch.spurr);
942 break;
943 case KVM_REG_PPC_AMR:
944 *val = get_reg_val(id, vcpu->arch.amr);
945 break;
946 case KVM_REG_PPC_UAMOR:
947 *val = get_reg_val(id, vcpu->arch.uamor);
948 break;
949 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
950 i = id - KVM_REG_PPC_MMCR0;
951 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
952 break;
953 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
954 i = id - KVM_REG_PPC_PMC1;
955 *val = get_reg_val(id, vcpu->arch.pmc[i]);
956 break;
957 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
958 i = id - KVM_REG_PPC_SPMC1;
959 *val = get_reg_val(id, vcpu->arch.spmc[i]);
960 break;
961 case KVM_REG_PPC_SIAR:
962 *val = get_reg_val(id, vcpu->arch.siar);
963 break;
964 case KVM_REG_PPC_SDAR:
965 *val = get_reg_val(id, vcpu->arch.sdar);
966 break;
967 case KVM_REG_PPC_SIER:
968 *val = get_reg_val(id, vcpu->arch.sier);
969 break;
970 case KVM_REG_PPC_IAMR:
971 *val = get_reg_val(id, vcpu->arch.iamr);
972 break;
973 case KVM_REG_PPC_PSPB:
974 *val = get_reg_val(id, vcpu->arch.pspb);
975 break;
976 case KVM_REG_PPC_DPDES:
977 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
978 break;
979 case KVM_REG_PPC_DAWR:
980 *val = get_reg_val(id, vcpu->arch.dawr);
981 break;
982 case KVM_REG_PPC_DAWRX:
983 *val = get_reg_val(id, vcpu->arch.dawrx);
984 break;
985 case KVM_REG_PPC_CIABR:
986 *val = get_reg_val(id, vcpu->arch.ciabr);
987 break;
988 case KVM_REG_PPC_CSIGR:
989 *val = get_reg_val(id, vcpu->arch.csigr);
990 break;
991 case KVM_REG_PPC_TACR:
992 *val = get_reg_val(id, vcpu->arch.tacr);
993 break;
994 case KVM_REG_PPC_TCSCR:
995 *val = get_reg_val(id, vcpu->arch.tcscr);
996 break;
997 case KVM_REG_PPC_PID:
998 *val = get_reg_val(id, vcpu->arch.pid);
999 break;
1000 case KVM_REG_PPC_ACOP:
1001 *val = get_reg_val(id, vcpu->arch.acop);
1002 break;
1003 case KVM_REG_PPC_WORT:
1004 *val = get_reg_val(id, vcpu->arch.wort);
1005 break;
1006 case KVM_REG_PPC_VPA_ADDR:
1007 spin_lock(&vcpu->arch.vpa_update_lock);
1008 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1009 spin_unlock(&vcpu->arch.vpa_update_lock);
1010 break;
1011 case KVM_REG_PPC_VPA_SLB:
1012 spin_lock(&vcpu->arch.vpa_update_lock);
1013 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1014 val->vpaval.length = vcpu->arch.slb_shadow.len;
1015 spin_unlock(&vcpu->arch.vpa_update_lock);
1016 break;
1017 case KVM_REG_PPC_VPA_DTL:
1018 spin_lock(&vcpu->arch.vpa_update_lock);
1019 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1020 val->vpaval.length = vcpu->arch.dtl.len;
1021 spin_unlock(&vcpu->arch.vpa_update_lock);
1022 break;
1023 case KVM_REG_PPC_TB_OFFSET:
1024 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1025 break;
1026 case KVM_REG_PPC_LPCR:
1027 case KVM_REG_PPC_LPCR_64:
1028 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1029 break;
1030 case KVM_REG_PPC_PPR:
1031 *val = get_reg_val(id, vcpu->arch.ppr);
1032 break;
1033#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1034 case KVM_REG_PPC_TFHAR:
1035 *val = get_reg_val(id, vcpu->arch.tfhar);
1036 break;
1037 case KVM_REG_PPC_TFIAR:
1038 *val = get_reg_val(id, vcpu->arch.tfiar);
1039 break;
1040 case KVM_REG_PPC_TEXASR:
1041 *val = get_reg_val(id, vcpu->arch.texasr);
1042 break;
1043 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1044 i = id - KVM_REG_PPC_TM_GPR0;
1045 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1046 break;
1047 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1048 {
1049 int j;
1050 i = id - KVM_REG_PPC_TM_VSR0;
1051 if (i < 32)
1052 for (j = 0; j < TS_FPRWIDTH; j++)
1053 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1054 else {
1055 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1056 val->vval = vcpu->arch.vr_tm.vr[i-32];
1057 else
1058 r = -ENXIO;
1059 }
1060 break;
1061 }
1062 case KVM_REG_PPC_TM_CR:
1063 *val = get_reg_val(id, vcpu->arch.cr_tm);
1064 break;
1065 case KVM_REG_PPC_TM_LR:
1066 *val = get_reg_val(id, vcpu->arch.lr_tm);
1067 break;
1068 case KVM_REG_PPC_TM_CTR:
1069 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1070 break;
1071 case KVM_REG_PPC_TM_FPSCR:
1072 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1073 break;
1074 case KVM_REG_PPC_TM_AMR:
1075 *val = get_reg_val(id, vcpu->arch.amr_tm);
1076 break;
1077 case KVM_REG_PPC_TM_PPR:
1078 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1079 break;
1080 case KVM_REG_PPC_TM_VRSAVE:
1081 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1082 break;
1083 case KVM_REG_PPC_TM_VSCR:
1084 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1085 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1086 else
1087 r = -ENXIO;
1088 break;
1089 case KVM_REG_PPC_TM_DSCR:
1090 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1091 break;
1092 case KVM_REG_PPC_TM_TAR:
1093 *val = get_reg_val(id, vcpu->arch.tar_tm);
1094 break;
1095#endif
1096 case KVM_REG_PPC_ARCH_COMPAT:
1097 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1098 break;
1099 default:
1100 r = -EINVAL;
1101 break;
1102 }
1103
1104 return r;
1105}
1106
1107static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1108 union kvmppc_one_reg *val)
1109{
1110 int r = 0;
1111 long int i;
1112 unsigned long addr, len;
1113
1114 switch (id) {
1115 case KVM_REG_PPC_HIOR:
1116
1117 if (set_reg_val(id, *val))
1118 r = -EINVAL;
1119 break;
1120 case KVM_REG_PPC_DABR:
1121 vcpu->arch.dabr = set_reg_val(id, *val);
1122 break;
1123 case KVM_REG_PPC_DABRX:
1124 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1125 break;
1126 case KVM_REG_PPC_DSCR:
1127 vcpu->arch.dscr = set_reg_val(id, *val);
1128 break;
1129 case KVM_REG_PPC_PURR:
1130 vcpu->arch.purr = set_reg_val(id, *val);
1131 break;
1132 case KVM_REG_PPC_SPURR:
1133 vcpu->arch.spurr = set_reg_val(id, *val);
1134 break;
1135 case KVM_REG_PPC_AMR:
1136 vcpu->arch.amr = set_reg_val(id, *val);
1137 break;
1138 case KVM_REG_PPC_UAMOR:
1139 vcpu->arch.uamor = set_reg_val(id, *val);
1140 break;
1141 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1142 i = id - KVM_REG_PPC_MMCR0;
1143 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1144 break;
1145 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1146 i = id - KVM_REG_PPC_PMC1;
1147 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1148 break;
1149 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1150 i = id - KVM_REG_PPC_SPMC1;
1151 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1152 break;
1153 case KVM_REG_PPC_SIAR:
1154 vcpu->arch.siar = set_reg_val(id, *val);
1155 break;
1156 case KVM_REG_PPC_SDAR:
1157 vcpu->arch.sdar = set_reg_val(id, *val);
1158 break;
1159 case KVM_REG_PPC_SIER:
1160 vcpu->arch.sier = set_reg_val(id, *val);
1161 break;
1162 case KVM_REG_PPC_IAMR:
1163 vcpu->arch.iamr = set_reg_val(id, *val);
1164 break;
1165 case KVM_REG_PPC_PSPB:
1166 vcpu->arch.pspb = set_reg_val(id, *val);
1167 break;
1168 case KVM_REG_PPC_DPDES:
1169 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1170 break;
1171 case KVM_REG_PPC_DAWR:
1172 vcpu->arch.dawr = set_reg_val(id, *val);
1173 break;
1174 case KVM_REG_PPC_DAWRX:
1175 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1176 break;
1177 case KVM_REG_PPC_CIABR:
1178 vcpu->arch.ciabr = set_reg_val(id, *val);
1179
1180 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1181 vcpu->arch.ciabr &= ~CIABR_PRIV;
1182 break;
1183 case KVM_REG_PPC_CSIGR:
1184 vcpu->arch.csigr = set_reg_val(id, *val);
1185 break;
1186 case KVM_REG_PPC_TACR:
1187 vcpu->arch.tacr = set_reg_val(id, *val);
1188 break;
1189 case KVM_REG_PPC_TCSCR:
1190 vcpu->arch.tcscr = set_reg_val(id, *val);
1191 break;
1192 case KVM_REG_PPC_PID:
1193 vcpu->arch.pid = set_reg_val(id, *val);
1194 break;
1195 case KVM_REG_PPC_ACOP:
1196 vcpu->arch.acop = set_reg_val(id, *val);
1197 break;
1198 case KVM_REG_PPC_WORT:
1199 vcpu->arch.wort = set_reg_val(id, *val);
1200 break;
1201 case KVM_REG_PPC_VPA_ADDR:
1202 addr = set_reg_val(id, *val);
1203 r = -EINVAL;
1204 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1205 vcpu->arch.dtl.next_gpa))
1206 break;
1207 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1208 break;
1209 case KVM_REG_PPC_VPA_SLB:
1210 addr = val->vpaval.addr;
1211 len = val->vpaval.length;
1212 r = -EINVAL;
1213 if (addr && !vcpu->arch.vpa.next_gpa)
1214 break;
1215 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1216 break;
1217 case KVM_REG_PPC_VPA_DTL:
1218 addr = val->vpaval.addr;
1219 len = val->vpaval.length;
1220 r = -EINVAL;
1221 if (addr && (len < sizeof(struct dtl_entry) ||
1222 !vcpu->arch.vpa.next_gpa))
1223 break;
1224 len -= len % sizeof(struct dtl_entry);
1225 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1226 break;
1227 case KVM_REG_PPC_TB_OFFSET:
1228
1229 vcpu->arch.vcore->tb_offset =
1230 ALIGN(set_reg_val(id, *val), 1UL << 24);
1231 break;
1232 case KVM_REG_PPC_LPCR:
1233 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1234 break;
1235 case KVM_REG_PPC_LPCR_64:
1236 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
1237 break;
1238 case KVM_REG_PPC_PPR:
1239 vcpu->arch.ppr = set_reg_val(id, *val);
1240 break;
1241#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1242 case KVM_REG_PPC_TFHAR:
1243 vcpu->arch.tfhar = set_reg_val(id, *val);
1244 break;
1245 case KVM_REG_PPC_TFIAR:
1246 vcpu->arch.tfiar = set_reg_val(id, *val);
1247 break;
1248 case KVM_REG_PPC_TEXASR:
1249 vcpu->arch.texasr = set_reg_val(id, *val);
1250 break;
1251 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1252 i = id - KVM_REG_PPC_TM_GPR0;
1253 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1254 break;
1255 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1256 {
1257 int j;
1258 i = id - KVM_REG_PPC_TM_VSR0;
1259 if (i < 32)
1260 for (j = 0; j < TS_FPRWIDTH; j++)
1261 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1262 else
1263 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1264 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1265 else
1266 r = -ENXIO;
1267 break;
1268 }
1269 case KVM_REG_PPC_TM_CR:
1270 vcpu->arch.cr_tm = set_reg_val(id, *val);
1271 break;
1272 case KVM_REG_PPC_TM_LR:
1273 vcpu->arch.lr_tm = set_reg_val(id, *val);
1274 break;
1275 case KVM_REG_PPC_TM_CTR:
1276 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1277 break;
1278 case KVM_REG_PPC_TM_FPSCR:
1279 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1280 break;
1281 case KVM_REG_PPC_TM_AMR:
1282 vcpu->arch.amr_tm = set_reg_val(id, *val);
1283 break;
1284 case KVM_REG_PPC_TM_PPR:
1285 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1286 break;
1287 case KVM_REG_PPC_TM_VRSAVE:
1288 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1289 break;
1290 case KVM_REG_PPC_TM_VSCR:
1291 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1292 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1293 else
1294 r = - ENXIO;
1295 break;
1296 case KVM_REG_PPC_TM_DSCR:
1297 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1298 break;
1299 case KVM_REG_PPC_TM_TAR:
1300 vcpu->arch.tar_tm = set_reg_val(id, *val);
1301 break;
1302#endif
1303 case KVM_REG_PPC_ARCH_COMPAT:
1304 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1305 break;
1306 default:
1307 r = -EINVAL;
1308 break;
1309 }
1310
1311 return r;
1312}
1313
1314static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1315{
1316 struct kvmppc_vcore *vcore;
1317
1318 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1319
1320 if (vcore == NULL)
1321 return NULL;
1322
1323 INIT_LIST_HEAD(&vcore->runnable_threads);
1324 spin_lock_init(&vcore->lock);
1325 init_waitqueue_head(&vcore->wq);
1326 vcore->preempt_tb = TB_NIL;
1327 vcore->lpcr = kvm->arch.lpcr;
1328 vcore->first_vcpuid = core * threads_per_subcore;
1329 vcore->kvm = kvm;
1330
1331 vcore->mpp_buffer_is_valid = false;
1332
1333 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1334 vcore->mpp_buffer = (void *)__get_free_pages(
1335 GFP_KERNEL|__GFP_ZERO,
1336 MPP_BUFFER_ORDER);
1337
1338 return vcore;
1339}
1340
1341static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1342 unsigned int id)
1343{
1344 struct kvm_vcpu *vcpu;
1345 int err = -EINVAL;
1346 int core;
1347 struct kvmppc_vcore *vcore;
1348
1349 core = id / threads_per_subcore;
1350 if (core >= KVM_MAX_VCORES)
1351 goto out;
1352
1353 err = -ENOMEM;
1354 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1355 if (!vcpu)
1356 goto out;
1357
1358 err = kvm_vcpu_init(vcpu, kvm, id);
1359 if (err)
1360 goto free_vcpu;
1361
1362 vcpu->arch.shared = &vcpu->arch.shregs;
1363#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1364
1365
1366
1367
1368#ifdef __BIG_ENDIAN__
1369 vcpu->arch.shared_big_endian = true;
1370#else
1371 vcpu->arch.shared_big_endian = false;
1372#endif
1373#endif
1374 vcpu->arch.mmcr[0] = MMCR0_FC;
1375 vcpu->arch.ctrl = CTRL_RUNLATCH;
1376
1377 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1378 spin_lock_init(&vcpu->arch.vpa_update_lock);
1379 spin_lock_init(&vcpu->arch.tbacct_lock);
1380 vcpu->arch.busy_preempt = TB_NIL;
1381 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1382
1383 kvmppc_mmu_book3s_hv_init(vcpu);
1384
1385 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1386
1387 init_waitqueue_head(&vcpu->arch.cpu_run);
1388
1389 mutex_lock(&kvm->lock);
1390 vcore = kvm->arch.vcores[core];
1391 if (!vcore) {
1392 vcore = kvmppc_vcore_create(kvm, core);
1393 kvm->arch.vcores[core] = vcore;
1394 kvm->arch.online_vcores++;
1395 }
1396 mutex_unlock(&kvm->lock);
1397
1398 if (!vcore)
1399 goto free_vcpu;
1400
1401 spin_lock(&vcore->lock);
1402 ++vcore->num_threads;
1403 spin_unlock(&vcore->lock);
1404 vcpu->arch.vcore = vcore;
1405 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1406
1407 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1408 kvmppc_sanity_check(vcpu);
1409
1410 return vcpu;
1411
1412free_vcpu:
1413 kmem_cache_free(kvm_vcpu_cache, vcpu);
1414out:
1415 return ERR_PTR(err);
1416}
1417
1418static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1419{
1420 if (vpa->pinned_addr)
1421 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1422 vpa->dirty);
1423}
1424
1425static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1426{
1427 spin_lock(&vcpu->arch.vpa_update_lock);
1428 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1429 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1430 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1431 spin_unlock(&vcpu->arch.vpa_update_lock);
1432 kvm_vcpu_uninit(vcpu);
1433 kmem_cache_free(kvm_vcpu_cache, vcpu);
1434}
1435
1436static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1437{
1438
1439 return 1;
1440}
1441
1442static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1443{
1444 unsigned long dec_nsec, now;
1445
1446 now = get_tb();
1447 if (now > vcpu->arch.dec_expires) {
1448
1449 kvmppc_core_queue_dec(vcpu);
1450 kvmppc_core_prepare_to_enter(vcpu);
1451 return;
1452 }
1453 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1454 / tb_ticks_per_sec;
1455 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1456 HRTIMER_MODE_REL);
1457 vcpu->arch.timer_running = 1;
1458}
1459
1460static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1461{
1462 vcpu->arch.ceded = 0;
1463 if (vcpu->arch.timer_running) {
1464 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1465 vcpu->arch.timer_running = 0;
1466 }
1467}
1468
1469extern void __kvmppc_vcore_entry(void);
1470
1471static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1472 struct kvm_vcpu *vcpu)
1473{
1474 u64 now;
1475
1476 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1477 return;
1478 spin_lock_irq(&vcpu->arch.tbacct_lock);
1479 now = mftb();
1480 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1481 vcpu->arch.stolen_logged;
1482 vcpu->arch.busy_preempt = now;
1483 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1484 spin_unlock_irq(&vcpu->arch.tbacct_lock);
1485 --vc->n_runnable;
1486 list_del(&vcpu->arch.run_list);
1487}
1488
1489static int kvmppc_grab_hwthread(int cpu)
1490{
1491 struct paca_struct *tpaca;
1492 long timeout = 1000;
1493
1494 tpaca = &paca[cpu];
1495
1496
1497 tpaca->kvm_hstate.hwthread_req = 1;
1498 tpaca->kvm_hstate.kvm_vcpu = NULL;
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509 smp_mb();
1510 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1511 if (--timeout <= 0) {
1512 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1513 return -EBUSY;
1514 }
1515 udelay(1);
1516 }
1517 return 0;
1518}
1519
1520static void kvmppc_release_hwthread(int cpu)
1521{
1522 struct paca_struct *tpaca;
1523
1524 tpaca = &paca[cpu];
1525 tpaca->kvm_hstate.hwthread_req = 0;
1526 tpaca->kvm_hstate.kvm_vcpu = NULL;
1527}
1528
1529static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1530{
1531 int cpu;
1532 struct paca_struct *tpaca;
1533 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1534
1535 if (vcpu->arch.timer_running) {
1536 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1537 vcpu->arch.timer_running = 0;
1538 }
1539 cpu = vc->pcpu + vcpu->arch.ptid;
1540 tpaca = &paca[cpu];
1541 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1542 tpaca->kvm_hstate.kvm_vcore = vc;
1543 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1544 vcpu->cpu = vc->pcpu;
1545 smp_wmb();
1546#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1547 if (cpu != smp_processor_id()) {
1548 xics_wake_cpu(cpu);
1549 if (vcpu->arch.ptid)
1550 ++vc->n_woken;
1551 }
1552#endif
1553}
1554
1555static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1556{
1557 int i;
1558
1559 HMT_low();
1560 i = 0;
1561 while (vc->nap_count < vc->n_woken) {
1562 if (++i >= 1000000) {
1563 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1564 vc->nap_count, vc->n_woken);
1565 break;
1566 }
1567 cpu_relax();
1568 }
1569 HMT_medium();
1570}
1571
1572
1573
1574
1575
1576
1577static int on_primary_thread(void)
1578{
1579 int cpu = smp_processor_id();
1580 int thr;
1581
1582
1583 if (cpu_thread_in_subcore(cpu))
1584 return 0;
1585
1586 thr = 0;
1587 while (++thr < threads_per_subcore)
1588 if (cpu_online(cpu + thr))
1589 return 0;
1590
1591
1592 for (thr = 1; thr < threads_per_subcore; ++thr) {
1593 if (kvmppc_grab_hwthread(cpu + thr)) {
1594
1595 do {
1596 kvmppc_release_hwthread(cpu + thr);
1597 } while (--thr > 0);
1598 return 0;
1599 }
1600 }
1601 return 1;
1602}
1603
1604static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1605{
1606 phys_addr_t phy_addr, mpp_addr;
1607
1608 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1609 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1610
1611 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1612 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1613
1614 vc->mpp_buffer_is_valid = true;
1615}
1616
1617static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1618{
1619 phys_addr_t phy_addr, mpp_addr;
1620
1621 phy_addr = virt_to_phys(vc->mpp_buffer);
1622 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1623
1624
1625
1626
1627 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1628 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1629}
1630
1631
1632
1633
1634
1635static void kvmppc_run_core(struct kvmppc_vcore *vc)
1636{
1637 struct kvm_vcpu *vcpu, *vnext;
1638 long ret;
1639 u64 now;
1640 int i, need_vpa_update;
1641 int srcu_idx;
1642 struct kvm_vcpu *vcpus_to_update[threads_per_core];
1643
1644
1645 need_vpa_update = 0;
1646 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1647 if (signal_pending(vcpu->arch.run_task))
1648 return;
1649 if (vcpu->arch.vpa.update_pending ||
1650 vcpu->arch.slb_shadow.update_pending ||
1651 vcpu->arch.dtl.update_pending)
1652 vcpus_to_update[need_vpa_update++] = vcpu;
1653 }
1654
1655
1656
1657
1658
1659 vc->n_woken = 0;
1660 vc->nap_count = 0;
1661 vc->entry_exit_count = 0;
1662 vc->vcore_state = VCORE_STARTING;
1663 vc->in_guest = 0;
1664 vc->napping_threads = 0;
1665
1666
1667
1668
1669
1670 if (need_vpa_update) {
1671 spin_unlock(&vc->lock);
1672 for (i = 0; i < need_vpa_update; ++i)
1673 kvmppc_update_vpas(vcpus_to_update[i]);
1674 spin_lock(&vc->lock);
1675 }
1676
1677
1678
1679
1680
1681
1682 if ((threads_per_core > 1) &&
1683 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
1684 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1685 vcpu->arch.ret = -EBUSY;
1686 goto out;
1687 }
1688
1689
1690 vc->pcpu = smp_processor_id();
1691 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1692 kvmppc_start_thread(vcpu);
1693 kvmppc_create_dtl_entry(vcpu, vc);
1694 }
1695
1696
1697 get_paca()->kvm_hstate.kvm_vcore = vc;
1698 get_paca()->kvm_hstate.ptid = 0;
1699
1700 vc->vcore_state = VCORE_RUNNING;
1701 preempt_disable();
1702 spin_unlock(&vc->lock);
1703
1704 kvm_guest_enter();
1705
1706 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
1707
1708 if (vc->mpp_buffer_is_valid)
1709 kvmppc_start_restoring_l2_cache(vc);
1710
1711 __kvmppc_vcore_entry();
1712
1713 spin_lock(&vc->lock);
1714
1715 if (vc->mpp_buffer)
1716 kvmppc_start_saving_l2_cache(vc);
1717
1718
1719 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1720 vcpu->cpu = -1;
1721
1722 if (vc->nap_count < vc->n_woken)
1723 kvmppc_wait_for_nap(vc);
1724 for (i = 0; i < threads_per_subcore; ++i)
1725 kvmppc_release_hwthread(vc->pcpu + i);
1726
1727 vc->vcore_state = VCORE_EXITING;
1728 spin_unlock(&vc->lock);
1729
1730 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
1731
1732
1733 smp_mb();
1734 kvm_guest_exit();
1735
1736 preempt_enable();
1737 cond_resched();
1738
1739 spin_lock(&vc->lock);
1740 now = get_tb();
1741 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1742
1743 if (now < vcpu->arch.dec_expires &&
1744 kvmppc_core_pending_dec(vcpu))
1745 kvmppc_core_dequeue_dec(vcpu);
1746
1747 ret = RESUME_GUEST;
1748 if (vcpu->arch.trap)
1749 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1750 vcpu->arch.run_task);
1751
1752 vcpu->arch.ret = ret;
1753 vcpu->arch.trap = 0;
1754
1755 if (vcpu->arch.ceded) {
1756 if (!is_kvmppc_resume_guest(ret))
1757 kvmppc_end_cede(vcpu);
1758 else
1759 kvmppc_set_timer(vcpu);
1760 }
1761 }
1762
1763 out:
1764 vc->vcore_state = VCORE_INACTIVE;
1765 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1766 arch.run_list) {
1767 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
1768 kvmppc_remove_runnable(vc, vcpu);
1769 wake_up(&vcpu->arch.cpu_run);
1770 }
1771 }
1772}
1773
1774
1775
1776
1777
1778static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1779{
1780 DEFINE_WAIT(wait);
1781
1782 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1783 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1784 schedule();
1785 finish_wait(&vcpu->arch.cpu_run, &wait);
1786}
1787
1788
1789
1790
1791
1792static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1793{
1794 DEFINE_WAIT(wait);
1795
1796 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1797 vc->vcore_state = VCORE_SLEEPING;
1798 spin_unlock(&vc->lock);
1799 schedule();
1800 finish_wait(&vc->wq, &wait);
1801 spin_lock(&vc->lock);
1802 vc->vcore_state = VCORE_INACTIVE;
1803}
1804
1805static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1806{
1807 int n_ceded;
1808 struct kvmppc_vcore *vc;
1809 struct kvm_vcpu *v, *vn;
1810
1811 kvm_run->exit_reason = 0;
1812 vcpu->arch.ret = RESUME_GUEST;
1813 vcpu->arch.trap = 0;
1814 kvmppc_update_vpas(vcpu);
1815
1816
1817
1818
1819 vc = vcpu->arch.vcore;
1820 spin_lock(&vc->lock);
1821 vcpu->arch.ceded = 0;
1822 vcpu->arch.run_task = current;
1823 vcpu->arch.kvm_run = kvm_run;
1824 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1825 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1826 vcpu->arch.busy_preempt = TB_NIL;
1827 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1828 ++vc->n_runnable;
1829
1830
1831
1832
1833
1834
1835 if (!signal_pending(current)) {
1836 if (vc->vcore_state == VCORE_RUNNING &&
1837 VCORE_EXIT_COUNT(vc) == 0) {
1838 kvmppc_create_dtl_entry(vcpu, vc);
1839 kvmppc_start_thread(vcpu);
1840 } else if (vc->vcore_state == VCORE_SLEEPING) {
1841 wake_up(&vc->wq);
1842 }
1843
1844 }
1845
1846 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1847 !signal_pending(current)) {
1848 if (vc->vcore_state != VCORE_INACTIVE) {
1849 spin_unlock(&vc->lock);
1850 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1851 spin_lock(&vc->lock);
1852 continue;
1853 }
1854 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1855 arch.run_list) {
1856 kvmppc_core_prepare_to_enter(v);
1857 if (signal_pending(v->arch.run_task)) {
1858 kvmppc_remove_runnable(vc, v);
1859 v->stat.signal_exits++;
1860 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1861 v->arch.ret = -EINTR;
1862 wake_up(&v->arch.cpu_run);
1863 }
1864 }
1865 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1866 break;
1867 vc->runner = vcpu;
1868 n_ceded = 0;
1869 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1870 if (!v->arch.pending_exceptions)
1871 n_ceded += v->arch.ceded;
1872 else
1873 v->arch.ceded = 0;
1874 }
1875 if (n_ceded == vc->n_runnable)
1876 kvmppc_vcore_blocked(vc);
1877 else
1878 kvmppc_run_core(vc);
1879 vc->runner = NULL;
1880 }
1881
1882 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1883 (vc->vcore_state == VCORE_RUNNING ||
1884 vc->vcore_state == VCORE_EXITING)) {
1885 spin_unlock(&vc->lock);
1886 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1887 spin_lock(&vc->lock);
1888 }
1889
1890 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1891 kvmppc_remove_runnable(vc, vcpu);
1892 vcpu->stat.signal_exits++;
1893 kvm_run->exit_reason = KVM_EXIT_INTR;
1894 vcpu->arch.ret = -EINTR;
1895 }
1896
1897 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1898
1899 v = list_first_entry(&vc->runnable_threads,
1900 struct kvm_vcpu, arch.run_list);
1901 wake_up(&v->arch.cpu_run);
1902 }
1903
1904 spin_unlock(&vc->lock);
1905 return vcpu->arch.ret;
1906}
1907
1908static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
1909{
1910 int r;
1911 int srcu_idx;
1912
1913 if (!vcpu->arch.sane) {
1914 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1915 return -EINVAL;
1916 }
1917
1918 kvmppc_core_prepare_to_enter(vcpu);
1919
1920
1921 if (signal_pending(current)) {
1922 run->exit_reason = KVM_EXIT_INTR;
1923 return -EINTR;
1924 }
1925
1926 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1927
1928 smp_mb();
1929
1930
1931 if (!vcpu->kvm->arch.rma_setup_done) {
1932 r = kvmppc_hv_setup_htab_rma(vcpu);
1933 if (r)
1934 goto out;
1935 }
1936
1937 flush_fp_to_thread(current);
1938 flush_altivec_to_thread(current);
1939 flush_vsx_to_thread(current);
1940 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
1941 vcpu->arch.pgdir = current->mm->pgd;
1942 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1943
1944 do {
1945 r = kvmppc_run_vcpu(run, vcpu);
1946
1947 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1948 !(vcpu->arch.shregs.msr & MSR_PR)) {
1949 r = kvmppc_pseries_do_hcall(vcpu);
1950 kvmppc_core_prepare_to_enter(vcpu);
1951 } else if (r == RESUME_PAGE_FAULT) {
1952 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1953 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1954 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1955 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1956 }
1957 } while (is_kvmppc_resume_guest(r));
1958
1959 out:
1960 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1961 atomic_dec(&vcpu->kvm->arch.vcpus_running);
1962 return r;
1963}
1964
1965
1966
1967
1968static inline int lpcr_rmls(unsigned long rma_size)
1969{
1970 switch (rma_size) {
1971 case 32ul << 20:
1972 if (cpu_has_feature(CPU_FTR_ARCH_206))
1973 return 8;
1974 return -1;
1975 case 64ul << 20:
1976 return 3;
1977 case 128ul << 20:
1978 return 7;
1979 case 256ul << 20:
1980 return 4;
1981 case 1ul << 30:
1982 return 2;
1983 case 16ul << 30:
1984 return 1;
1985 case 256ul << 30:
1986 return 0;
1987 default:
1988 return -1;
1989 }
1990}
1991
1992static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1993{
1994 struct page *page;
1995 struct kvm_rma_info *ri = vma->vm_file->private_data;
1996
1997 if (vmf->pgoff >= kvm_rma_pages)
1998 return VM_FAULT_SIGBUS;
1999
2000 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
2001 get_page(page);
2002 vmf->page = page;
2003 return 0;
2004}
2005
2006static const struct vm_operations_struct kvm_rma_vm_ops = {
2007 .fault = kvm_rma_fault,
2008};
2009
2010static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
2011{
2012 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2013 vma->vm_ops = &kvm_rma_vm_ops;
2014 return 0;
2015}
2016
2017static int kvm_rma_release(struct inode *inode, struct file *filp)
2018{
2019 struct kvm_rma_info *ri = filp->private_data;
2020
2021 kvm_release_rma(ri);
2022 return 0;
2023}
2024
2025static const struct file_operations kvm_rma_fops = {
2026 .mmap = kvm_rma_mmap,
2027 .release = kvm_rma_release,
2028};
2029
2030static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
2031 struct kvm_allocate_rma *ret)
2032{
2033 long fd;
2034 struct kvm_rma_info *ri;
2035
2036
2037
2038 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2039 !cpu_has_feature(CPU_FTR_ARCH_201))
2040 return -EINVAL;
2041
2042 if (!kvm_rma_pages)
2043 return -EINVAL;
2044
2045 ri = kvm_alloc_rma();
2046 if (!ri)
2047 return -ENOMEM;
2048
2049 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
2050 if (fd < 0)
2051 kvm_release_rma(ri);
2052
2053 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
2054 return fd;
2055}
2056
2057static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2058 int linux_psize)
2059{
2060 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2061
2062 if (!def->shift)
2063 return;
2064 (*sps)->page_shift = def->shift;
2065 (*sps)->slb_enc = def->sllp;
2066 (*sps)->enc[0].page_shift = def->shift;
2067 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
2068
2069
2070
2071 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2072 (*sps)->enc[1].page_shift = 24;
2073 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2074 }
2075 (*sps)++;
2076}
2077
2078static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2079 struct kvm_ppc_smmu_info *info)
2080{
2081 struct kvm_ppc_one_seg_page_size *sps;
2082
2083 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2084 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2085 info->flags |= KVM_PPC_1T_SEGMENTS;
2086 info->slb_size = mmu_slb_size;
2087
2088
2089 sps = &info->sps[0];
2090 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2091 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2092 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2093
2094 return 0;
2095}
2096
2097
2098
2099
2100static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2101 struct kvm_dirty_log *log)
2102{
2103 struct kvm_memory_slot *memslot;
2104 int r;
2105 unsigned long n;
2106
2107 mutex_lock(&kvm->slots_lock);
2108
2109 r = -EINVAL;
2110 if (log->slot >= KVM_USER_MEM_SLOTS)
2111 goto out;
2112
2113 memslot = id_to_memslot(kvm->memslots, log->slot);
2114 r = -ENOENT;
2115 if (!memslot->dirty_bitmap)
2116 goto out;
2117
2118 n = kvm_dirty_bitmap_bytes(memslot);
2119 memset(memslot->dirty_bitmap, 0, n);
2120
2121 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
2122 if (r)
2123 goto out;
2124
2125 r = -EFAULT;
2126 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2127 goto out;
2128
2129 r = 0;
2130out:
2131 mutex_unlock(&kvm->slots_lock);
2132 return r;
2133}
2134
2135static void unpin_slot(struct kvm_memory_slot *memslot)
2136{
2137 unsigned long *physp;
2138 unsigned long j, npages, pfn;
2139 struct page *page;
2140
2141 physp = memslot->arch.slot_phys;
2142 npages = memslot->npages;
2143 if (!physp)
2144 return;
2145 for (j = 0; j < npages; j++) {
2146 if (!(physp[j] & KVMPPC_GOT_PAGE))
2147 continue;
2148 pfn = physp[j] >> PAGE_SHIFT;
2149 page = pfn_to_page(pfn);
2150 SetPageDirty(page);
2151 put_page(page);
2152 }
2153}
2154
2155static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2156 struct kvm_memory_slot *dont)
2157{
2158 if (!dont || free->arch.rmap != dont->arch.rmap) {
2159 vfree(free->arch.rmap);
2160 free->arch.rmap = NULL;
2161 }
2162 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
2163 unpin_slot(free);
2164 vfree(free->arch.slot_phys);
2165 free->arch.slot_phys = NULL;
2166 }
2167}
2168
2169static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2170 unsigned long npages)
2171{
2172 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2173 if (!slot->arch.rmap)
2174 return -ENOMEM;
2175 slot->arch.slot_phys = NULL;
2176
2177 return 0;
2178}
2179
2180static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2181 struct kvm_memory_slot *memslot,
2182 struct kvm_userspace_memory_region *mem)
2183{
2184 unsigned long *phys;
2185
2186
2187 phys = memslot->arch.slot_phys;
2188 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
2189 phys = vzalloc(memslot->npages * sizeof(unsigned long));
2190 if (!phys)
2191 return -ENOMEM;
2192 memslot->arch.slot_phys = phys;
2193 }
2194
2195 return 0;
2196}
2197
2198static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2199 struct kvm_userspace_memory_region *mem,
2200 const struct kvm_memory_slot *old)
2201{
2202 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2203 struct kvm_memory_slot *memslot;
2204
2205 if (npages && old->npages) {
2206
2207
2208
2209
2210
2211
2212 memslot = id_to_memslot(kvm->memslots, mem->slot);
2213 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2214 }
2215}
2216
2217
2218
2219
2220
2221void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2222{
2223 long int i;
2224 u32 cores_done = 0;
2225
2226 if ((kvm->arch.lpcr & mask) == lpcr)
2227 return;
2228
2229 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2230
2231 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2232 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2233 if (!vc)
2234 continue;
2235 spin_lock(&vc->lock);
2236 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2237 spin_unlock(&vc->lock);
2238 if (++cores_done >= kvm->arch.online_vcores)
2239 break;
2240 }
2241}
2242
2243static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2244{
2245 return;
2246}
2247
2248static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2249{
2250 int err = 0;
2251 struct kvm *kvm = vcpu->kvm;
2252 struct kvm_rma_info *ri = NULL;
2253 unsigned long hva;
2254 struct kvm_memory_slot *memslot;
2255 struct vm_area_struct *vma;
2256 unsigned long lpcr = 0, senc;
2257 unsigned long lpcr_mask = 0;
2258 unsigned long psize, porder;
2259 unsigned long rma_size;
2260 unsigned long rmls;
2261 unsigned long *physp;
2262 unsigned long i, npages;
2263 int srcu_idx;
2264
2265 mutex_lock(&kvm->lock);
2266 if (kvm->arch.rma_setup_done)
2267 goto out;
2268
2269
2270 if (!kvm->arch.hpt_virt) {
2271 err = kvmppc_alloc_hpt(kvm, NULL);
2272 if (err) {
2273 pr_err("KVM: Couldn't alloc HPT\n");
2274 goto out;
2275 }
2276 }
2277
2278
2279 srcu_idx = srcu_read_lock(&kvm->srcu);
2280 memslot = gfn_to_memslot(kvm, 0);
2281
2282
2283 err = -EINVAL;
2284 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2285 goto out_srcu;
2286
2287
2288 hva = memslot->userspace_addr;
2289 down_read(¤t->mm->mmap_sem);
2290 vma = find_vma(current->mm, hva);
2291 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2292 goto up_out;
2293
2294 psize = vma_kernel_pagesize(vma);
2295 porder = __ilog2(psize);
2296
2297
2298 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2299 hva == vma->vm_start)
2300 ri = vma->vm_file->private_data;
2301
2302 up_read(¤t->mm->mmap_sem);
2303
2304 if (!ri) {
2305
2306 err = -EPERM;
2307 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2308 pr_err("KVM: CPU requires an RMO\n");
2309 goto out_srcu;
2310 }
2311
2312
2313 err = -EINVAL;
2314 if (!(psize == 0x1000 || psize == 0x10000 ||
2315 psize == 0x1000000))
2316 goto out_srcu;
2317
2318
2319 senc = slb_pgsize_encoding(psize);
2320 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2321 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2322 lpcr_mask = LPCR_VRMASD;
2323
2324 lpcr = senc << (LPCR_VRMASD_SH - 4);
2325
2326
2327 kvmppc_map_vrma(vcpu, memslot, porder);
2328
2329 } else {
2330
2331 rma_size = kvm_rma_pages;
2332 if (rma_size > memslot->npages)
2333 rma_size = memslot->npages;
2334 rma_size <<= PAGE_SHIFT;
2335 rmls = lpcr_rmls(rma_size);
2336 err = -EINVAL;
2337 if ((long)rmls < 0) {
2338 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
2339 goto out_srcu;
2340 }
2341 atomic_inc(&ri->use_count);
2342 kvm->arch.rma = ri;
2343
2344
2345 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2346
2347 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2348 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2349 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
2350 ((rmls & 3) << HID4_RMLS2_SH);
2351
2352 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2353 << HID4_RMOR_SH;
2354 } else {
2355
2356 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2357 lpcr = rmls << LPCR_RMLS_SH;
2358 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
2359 }
2360 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
2361 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
2362
2363
2364 npages = kvm_rma_pages;
2365 porder = __ilog2(npages);
2366 physp = memslot->arch.slot_phys;
2367 if (physp) {
2368 if (npages > memslot->npages)
2369 npages = memslot->npages;
2370 spin_lock(&kvm->arch.slot_phys_lock);
2371 for (i = 0; i < npages; ++i)
2372 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2373 porder;
2374 spin_unlock(&kvm->arch.slot_phys_lock);
2375 }
2376 }
2377
2378 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2379
2380
2381 smp_wmb();
2382 kvm->arch.rma_setup_done = 1;
2383 err = 0;
2384 out_srcu:
2385 srcu_read_unlock(&kvm->srcu, srcu_idx);
2386 out:
2387 mutex_unlock(&kvm->lock);
2388 return err;
2389
2390 up_out:
2391 up_read(¤t->mm->mmap_sem);
2392 goto out_srcu;
2393}
2394
2395static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2396{
2397 unsigned long lpcr, lpid;
2398
2399
2400
2401 lpid = kvmppc_alloc_lpid();
2402 if ((long)lpid < 0)
2403 return -ENOMEM;
2404 kvm->arch.lpid = lpid;
2405
2406
2407
2408
2409
2410
2411 cpumask_setall(&kvm->arch.need_tlb_flush);
2412
2413
2414 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2415 sizeof(kvm->arch.enabled_hcalls));
2416
2417 kvm->arch.rma = NULL;
2418
2419 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2420
2421 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2422
2423 kvm->arch.host_lpid = 0;
2424 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2425 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2426 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2427 ((lpid & 0xf) << HID4_LPID5_SH);
2428 } else {
2429
2430 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2431 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2432 lpcr &= LPCR_PECE | LPCR_LPES;
2433 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2434 LPCR_VPM0 | LPCR_VPM1;
2435 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2436 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2437
2438 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2439 lpcr |= LPCR_ONL;
2440 }
2441 kvm->arch.lpcr = lpcr;
2442
2443 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
2444 spin_lock_init(&kvm->arch.slot_phys_lock);
2445
2446
2447
2448
2449
2450 kvm_hv_vm_activated();
2451
2452 return 0;
2453}
2454
2455static void kvmppc_free_vcores(struct kvm *kvm)
2456{
2457 long int i;
2458
2459 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2460 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2461 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2462 free_pages((unsigned long)vc->mpp_buffer,
2463 MPP_BUFFER_ORDER);
2464 }
2465 kfree(kvm->arch.vcores[i]);
2466 }
2467 kvm->arch.online_vcores = 0;
2468}
2469
2470static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2471{
2472 kvm_hv_vm_deactivated();
2473
2474 kvmppc_free_vcores(kvm);
2475 if (kvm->arch.rma) {
2476 kvm_release_rma(kvm->arch.rma);
2477 kvm->arch.rma = NULL;
2478 }
2479
2480 kvmppc_free_hpt(kvm);
2481}
2482
2483
2484static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2485 unsigned int inst, int *advance)
2486{
2487 return EMULATE_FAIL;
2488}
2489
2490static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2491 ulong spr_val)
2492{
2493 return EMULATE_FAIL;
2494}
2495
2496static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2497 ulong *spr_val)
2498{
2499 return EMULATE_FAIL;
2500}
2501
2502static int kvmppc_core_check_processor_compat_hv(void)
2503{
2504 if (!cpu_has_feature(CPU_FTR_HVMODE))
2505 return -EIO;
2506 return 0;
2507}
2508
2509static long kvm_arch_vm_ioctl_hv(struct file *filp,
2510 unsigned int ioctl, unsigned long arg)
2511{
2512 struct kvm *kvm __maybe_unused = filp->private_data;
2513 void __user *argp = (void __user *)arg;
2514 long r;
2515
2516 switch (ioctl) {
2517
2518 case KVM_ALLOCATE_RMA: {
2519 struct kvm_allocate_rma rma;
2520 struct kvm *kvm = filp->private_data;
2521
2522 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2523 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2524 r = -EFAULT;
2525 break;
2526 }
2527
2528 case KVM_PPC_ALLOCATE_HTAB: {
2529 u32 htab_order;
2530
2531 r = -EFAULT;
2532 if (get_user(htab_order, (u32 __user *)argp))
2533 break;
2534 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2535 if (r)
2536 break;
2537 r = -EFAULT;
2538 if (put_user(htab_order, (u32 __user *)argp))
2539 break;
2540 r = 0;
2541 break;
2542 }
2543
2544 case KVM_PPC_GET_HTAB_FD: {
2545 struct kvm_get_htab_fd ghf;
2546
2547 r = -EFAULT;
2548 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2549 break;
2550 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2551 break;
2552 }
2553
2554 default:
2555 r = -ENOTTY;
2556 }
2557
2558 return r;
2559}
2560
2561
2562
2563
2564
2565
2566
2567static unsigned int default_hcall_list[] = {
2568 H_REMOVE,
2569 H_ENTER,
2570 H_READ,
2571 H_PROTECT,
2572 H_BULK_REMOVE,
2573 H_GET_TCE,
2574 H_PUT_TCE,
2575 H_SET_DABR,
2576 H_SET_XDABR,
2577 H_CEDE,
2578 H_PROD,
2579 H_CONFER,
2580 H_REGISTER_VPA,
2581#ifdef CONFIG_KVM_XICS
2582 H_EOI,
2583 H_CPPR,
2584 H_IPI,
2585 H_IPOLL,
2586 H_XIRR,
2587 H_XIRR_X,
2588#endif
2589 0
2590};
2591
2592static void init_default_hcalls(void)
2593{
2594 int i;
2595 unsigned int hcall;
2596
2597 for (i = 0; default_hcall_list[i]; ++i) {
2598 hcall = default_hcall_list[i];
2599 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2600 __set_bit(hcall / 4, default_enabled_hcalls);
2601 }
2602}
2603
2604static struct kvmppc_ops kvm_ops_hv = {
2605 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2606 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2607 .get_one_reg = kvmppc_get_one_reg_hv,
2608 .set_one_reg = kvmppc_set_one_reg_hv,
2609 .vcpu_load = kvmppc_core_vcpu_load_hv,
2610 .vcpu_put = kvmppc_core_vcpu_put_hv,
2611 .set_msr = kvmppc_set_msr_hv,
2612 .vcpu_run = kvmppc_vcpu_run_hv,
2613 .vcpu_create = kvmppc_core_vcpu_create_hv,
2614 .vcpu_free = kvmppc_core_vcpu_free_hv,
2615 .check_requests = kvmppc_core_check_requests_hv,
2616 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2617 .flush_memslot = kvmppc_core_flush_memslot_hv,
2618 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2619 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2620 .unmap_hva = kvm_unmap_hva_hv,
2621 .unmap_hva_range = kvm_unmap_hva_range_hv,
2622 .age_hva = kvm_age_hva_hv,
2623 .test_age_hva = kvm_test_age_hva_hv,
2624 .set_spte_hva = kvm_set_spte_hva_hv,
2625 .mmu_destroy = kvmppc_mmu_destroy_hv,
2626 .free_memslot = kvmppc_core_free_memslot_hv,
2627 .create_memslot = kvmppc_core_create_memslot_hv,
2628 .init_vm = kvmppc_core_init_vm_hv,
2629 .destroy_vm = kvmppc_core_destroy_vm_hv,
2630 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2631 .emulate_op = kvmppc_core_emulate_op_hv,
2632 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2633 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2634 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2635 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2636 .hcall_implemented = kvmppc_hcall_impl_hv,
2637};
2638
2639static int kvmppc_book3s_init_hv(void)
2640{
2641 int r;
2642
2643
2644
2645 r = kvmppc_core_check_processor_compat_hv();
2646 if (r < 0)
2647 return -ENODEV;
2648
2649 kvm_ops_hv.owner = THIS_MODULE;
2650 kvmppc_hv_ops = &kvm_ops_hv;
2651
2652 init_default_hcalls();
2653
2654 r = kvmppc_mmu_hv_init();
2655 return r;
2656}
2657
2658static void kvmppc_book3s_exit_hv(void)
2659{
2660 kvmppc_hv_ops = NULL;
2661}
2662
2663module_init(kvmppc_book3s_init_hv);
2664module_exit(kvmppc_book3s_exit_hv);
2665MODULE_LICENSE("GPL");
2666MODULE_ALIAS_MISCDEV(KVM_MINOR);
2667MODULE_ALIAS("devname:kvm");
2668