Linux 4.1.18
[linux/fpc-iii.git] / arch / powerpc / kvm / book3s_hv.c
blobf5b3de7f7fa2402740891d75bde94f562f6ac7e2
1 /*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
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 #include <linux/debugfs.h>
37 #include <asm/reg.h>
38 #include <asm/cputable.h>
39 #include <asm/cache.h>
40 #include <asm/cacheflush.h>
41 #include <asm/tlbflush.h>
42 #include <asm/uaccess.h>
43 #include <asm/io.h>
44 #include <asm/kvm_ppc.h>
45 #include <asm/kvm_book3s.h>
46 #include <asm/mmu_context.h>
47 #include <asm/lppaca.h>
48 #include <asm/processor.h>
49 #include <asm/cputhreads.h>
50 #include <asm/page.h>
51 #include <asm/hvcall.h>
52 #include <asm/switch_to.h>
53 #include <asm/smp.h>
54 #include <asm/dbell.h>
55 #include <linux/gfp.h>
56 #include <linux/vmalloc.h>
57 #include <linux/highmem.h>
58 #include <linux/hugetlb.h>
59 #include <linux/module.h>
61 #include "book3s.h"
63 #define CREATE_TRACE_POINTS
64 #include "trace_hv.h"
66 /* #define EXIT_DEBUG */
67 /* #define EXIT_DEBUG_SIMPLE */
68 /* #define EXIT_DEBUG_INT */
70 /* Used to indicate that a guest page fault needs to be handled */
71 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
73 /* Used as a "null" value for timebase values */
74 #define TB_NIL (~(u64)0)
76 static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
78 #if defined(CONFIG_PPC_64K_PAGES)
79 #define MPP_BUFFER_ORDER 0
80 #elif defined(CONFIG_PPC_4K_PAGES)
81 #define MPP_BUFFER_ORDER 3
82 #endif
85 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
86 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
88 static bool kvmppc_ipi_thread(int cpu)
90 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
91 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
92 preempt_disable();
93 if (cpu_first_thread_sibling(cpu) ==
94 cpu_first_thread_sibling(smp_processor_id())) {
95 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
96 msg |= cpu_thread_in_core(cpu);
97 smp_mb();
98 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
99 preempt_enable();
100 return true;
102 preempt_enable();
105 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
106 if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
107 xics_wake_cpu(cpu);
108 return true;
110 #endif
112 return false;
115 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
117 int cpu = vcpu->cpu;
118 wait_queue_head_t *wqp;
120 wqp = kvm_arch_vcpu_wq(vcpu);
121 if (waitqueue_active(wqp)) {
122 wake_up_interruptible(wqp);
123 ++vcpu->stat.halt_wakeup;
126 if (kvmppc_ipi_thread(cpu + vcpu->arch.ptid))
127 return;
129 /* CPU points to the first thread of the core */
130 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
131 smp_send_reschedule(cpu);
135 * We use the vcpu_load/put functions to measure stolen time.
136 * Stolen time is counted as time when either the vcpu is able to
137 * run as part of a virtual core, but the task running the vcore
138 * is preempted or sleeping, or when the vcpu needs something done
139 * in the kernel by the task running the vcpu, but that task is
140 * preempted or sleeping. Those two things have to be counted
141 * separately, since one of the vcpu tasks will take on the job
142 * of running the core, and the other vcpu tasks in the vcore will
143 * sleep waiting for it to do that, but that sleep shouldn't count
144 * as stolen time.
146 * Hence we accumulate stolen time when the vcpu can run as part of
147 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
148 * needs its task to do other things in the kernel (for example,
149 * service a page fault) in busy_stolen. We don't accumulate
150 * stolen time for a vcore when it is inactive, or for a vcpu
151 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
152 * a misnomer; it means that the vcpu task is not executing in
153 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
154 * the kernel. We don't have any way of dividing up that time
155 * between time that the vcpu is genuinely stopped, time that
156 * the task is actively working on behalf of the vcpu, and time
157 * that the task is preempted, so we don't count any of it as
158 * stolen.
160 * Updates to busy_stolen are protected by arch.tbacct_lock;
161 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
162 * lock. The stolen times are measured in units of timebase ticks.
163 * (Note that the != TB_NIL checks below are purely defensive;
164 * they should never fail.)
167 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
169 struct kvmppc_vcore *vc = vcpu->arch.vcore;
170 unsigned long flags;
173 * We can test vc->runner without taking the vcore lock,
174 * because only this task ever sets vc->runner to this
175 * vcpu, and once it is set to this vcpu, only this task
176 * ever sets it to NULL.
178 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
179 spin_lock_irqsave(&vc->stoltb_lock, flags);
180 if (vc->preempt_tb != TB_NIL) {
181 vc->stolen_tb += mftb() - vc->preempt_tb;
182 vc->preempt_tb = TB_NIL;
184 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
186 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
187 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
188 vcpu->arch.busy_preempt != TB_NIL) {
189 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
190 vcpu->arch.busy_preempt = TB_NIL;
192 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
195 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
197 struct kvmppc_vcore *vc = vcpu->arch.vcore;
198 unsigned long flags;
200 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) {
201 spin_lock_irqsave(&vc->stoltb_lock, flags);
202 vc->preempt_tb = mftb();
203 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
205 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
206 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
207 vcpu->arch.busy_preempt = mftb();
208 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
211 static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
214 * Check for illegal transactional state bit combination
215 * and if we find it, force the TS field to a safe state.
217 if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
218 msr &= ~MSR_TS_MASK;
219 vcpu->arch.shregs.msr = msr;
220 kvmppc_end_cede(vcpu);
223 void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
225 vcpu->arch.pvr = pvr;
228 int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
230 unsigned long pcr = 0;
231 struct kvmppc_vcore *vc = vcpu->arch.vcore;
233 if (arch_compat) {
234 switch (arch_compat) {
235 case PVR_ARCH_205:
237 * If an arch bit is set in PCR, all the defined
238 * higher-order arch bits also have to be set.
240 pcr = PCR_ARCH_206 | PCR_ARCH_205;
241 break;
242 case PVR_ARCH_206:
243 case PVR_ARCH_206p:
244 pcr = PCR_ARCH_206;
245 break;
246 case PVR_ARCH_207:
247 break;
248 default:
249 return -EINVAL;
252 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
253 /* POWER7 can't emulate POWER8 */
254 if (!(pcr & PCR_ARCH_206))
255 return -EINVAL;
256 pcr &= ~PCR_ARCH_206;
260 spin_lock(&vc->lock);
261 vc->arch_compat = arch_compat;
262 vc->pcr = pcr;
263 spin_unlock(&vc->lock);
265 return 0;
268 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
270 int r;
272 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
273 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
274 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
275 for (r = 0; r < 16; ++r)
276 pr_err("r%2d = %.16lx r%d = %.16lx\n",
277 r, kvmppc_get_gpr(vcpu, r),
278 r+16, kvmppc_get_gpr(vcpu, r+16));
279 pr_err("ctr = %.16lx lr = %.16lx\n",
280 vcpu->arch.ctr, vcpu->arch.lr);
281 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
282 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
283 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
284 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
285 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
286 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
287 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
288 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
289 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
290 pr_err("fault dar = %.16lx dsisr = %.8x\n",
291 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
292 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
293 for (r = 0; r < vcpu->arch.slb_max; ++r)
294 pr_err(" ESID = %.16llx VSID = %.16llx\n",
295 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
296 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
297 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
298 vcpu->arch.last_inst);
301 struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
303 int r;
304 struct kvm_vcpu *v, *ret = NULL;
306 mutex_lock(&kvm->lock);
307 kvm_for_each_vcpu(r, v, kvm) {
308 if (v->vcpu_id == id) {
309 ret = v;
310 break;
313 mutex_unlock(&kvm->lock);
314 return ret;
317 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
319 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
320 vpa->yield_count = cpu_to_be32(1);
323 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
324 unsigned long addr, unsigned long len)
326 /* check address is cacheline aligned */
327 if (addr & (L1_CACHE_BYTES - 1))
328 return -EINVAL;
329 spin_lock(&vcpu->arch.vpa_update_lock);
330 if (v->next_gpa != addr || v->len != len) {
331 v->next_gpa = addr;
332 v->len = addr ? len : 0;
333 v->update_pending = 1;
335 spin_unlock(&vcpu->arch.vpa_update_lock);
336 return 0;
339 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
340 struct reg_vpa {
341 u32 dummy;
342 union {
343 __be16 hword;
344 __be32 word;
345 } length;
348 static int vpa_is_registered(struct kvmppc_vpa *vpap)
350 if (vpap->update_pending)
351 return vpap->next_gpa != 0;
352 return vpap->pinned_addr != NULL;
355 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
356 unsigned long flags,
357 unsigned long vcpuid, unsigned long vpa)
359 struct kvm *kvm = vcpu->kvm;
360 unsigned long len, nb;
361 void *va;
362 struct kvm_vcpu *tvcpu;
363 int err;
364 int subfunc;
365 struct kvmppc_vpa *vpap;
367 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
368 if (!tvcpu)
369 return H_PARAMETER;
371 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
372 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
373 subfunc == H_VPA_REG_SLB) {
374 /* Registering new area - address must be cache-line aligned */
375 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
376 return H_PARAMETER;
378 /* convert logical addr to kernel addr and read length */
379 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
380 if (va == NULL)
381 return H_PARAMETER;
382 if (subfunc == H_VPA_REG_VPA)
383 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
384 else
385 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
386 kvmppc_unpin_guest_page(kvm, va, vpa, false);
388 /* Check length */
389 if (len > nb || len < sizeof(struct reg_vpa))
390 return H_PARAMETER;
391 } else {
392 vpa = 0;
393 len = 0;
396 err = H_PARAMETER;
397 vpap = NULL;
398 spin_lock(&tvcpu->arch.vpa_update_lock);
400 switch (subfunc) {
401 case H_VPA_REG_VPA: /* register VPA */
402 if (len < sizeof(struct lppaca))
403 break;
404 vpap = &tvcpu->arch.vpa;
405 err = 0;
406 break;
408 case H_VPA_REG_DTL: /* register DTL */
409 if (len < sizeof(struct dtl_entry))
410 break;
411 len -= len % sizeof(struct dtl_entry);
413 /* Check that they have previously registered a VPA */
414 err = H_RESOURCE;
415 if (!vpa_is_registered(&tvcpu->arch.vpa))
416 break;
418 vpap = &tvcpu->arch.dtl;
419 err = 0;
420 break;
422 case H_VPA_REG_SLB: /* register SLB shadow buffer */
423 /* Check that they have previously registered a VPA */
424 err = H_RESOURCE;
425 if (!vpa_is_registered(&tvcpu->arch.vpa))
426 break;
428 vpap = &tvcpu->arch.slb_shadow;
429 err = 0;
430 break;
432 case H_VPA_DEREG_VPA: /* deregister VPA */
433 /* Check they don't still have a DTL or SLB buf registered */
434 err = H_RESOURCE;
435 if (vpa_is_registered(&tvcpu->arch.dtl) ||
436 vpa_is_registered(&tvcpu->arch.slb_shadow))
437 break;
439 vpap = &tvcpu->arch.vpa;
440 err = 0;
441 break;
443 case H_VPA_DEREG_DTL: /* deregister DTL */
444 vpap = &tvcpu->arch.dtl;
445 err = 0;
446 break;
448 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
449 vpap = &tvcpu->arch.slb_shadow;
450 err = 0;
451 break;
454 if (vpap) {
455 vpap->next_gpa = vpa;
456 vpap->len = len;
457 vpap->update_pending = 1;
460 spin_unlock(&tvcpu->arch.vpa_update_lock);
462 return err;
465 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
467 struct kvm *kvm = vcpu->kvm;
468 void *va;
469 unsigned long nb;
470 unsigned long gpa;
473 * We need to pin the page pointed to by vpap->next_gpa,
474 * but we can't call kvmppc_pin_guest_page under the lock
475 * as it does get_user_pages() and down_read(). So we
476 * have to drop the lock, pin the page, then get the lock
477 * again and check that a new area didn't get registered
478 * in the meantime.
480 for (;;) {
481 gpa = vpap->next_gpa;
482 spin_unlock(&vcpu->arch.vpa_update_lock);
483 va = NULL;
484 nb = 0;
485 if (gpa)
486 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
487 spin_lock(&vcpu->arch.vpa_update_lock);
488 if (gpa == vpap->next_gpa)
489 break;
490 /* sigh... unpin that one and try again */
491 if (va)
492 kvmppc_unpin_guest_page(kvm, va, gpa, false);
495 vpap->update_pending = 0;
496 if (va && nb < vpap->len) {
498 * If it's now too short, it must be that userspace
499 * has changed the mappings underlying guest memory,
500 * so unregister the region.
502 kvmppc_unpin_guest_page(kvm, va, gpa, false);
503 va = NULL;
505 if (vpap->pinned_addr)
506 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
507 vpap->dirty);
508 vpap->gpa = gpa;
509 vpap->pinned_addr = va;
510 vpap->dirty = false;
511 if (va)
512 vpap->pinned_end = va + vpap->len;
515 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
517 if (!(vcpu->arch.vpa.update_pending ||
518 vcpu->arch.slb_shadow.update_pending ||
519 vcpu->arch.dtl.update_pending))
520 return;
522 spin_lock(&vcpu->arch.vpa_update_lock);
523 if (vcpu->arch.vpa.update_pending) {
524 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
525 if (vcpu->arch.vpa.pinned_addr)
526 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
528 if (vcpu->arch.dtl.update_pending) {
529 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
530 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
531 vcpu->arch.dtl_index = 0;
533 if (vcpu->arch.slb_shadow.update_pending)
534 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
535 spin_unlock(&vcpu->arch.vpa_update_lock);
539 * Return the accumulated stolen time for the vcore up until `now'.
540 * The caller should hold the vcore lock.
542 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
544 u64 p;
545 unsigned long flags;
547 spin_lock_irqsave(&vc->stoltb_lock, flags);
548 p = vc->stolen_tb;
549 if (vc->vcore_state != VCORE_INACTIVE &&
550 vc->preempt_tb != TB_NIL)
551 p += now - vc->preempt_tb;
552 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
553 return p;
556 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
557 struct kvmppc_vcore *vc)
559 struct dtl_entry *dt;
560 struct lppaca *vpa;
561 unsigned long stolen;
562 unsigned long core_stolen;
563 u64 now;
565 dt = vcpu->arch.dtl_ptr;
566 vpa = vcpu->arch.vpa.pinned_addr;
567 now = mftb();
568 core_stolen = vcore_stolen_time(vc, now);
569 stolen = core_stolen - vcpu->arch.stolen_logged;
570 vcpu->arch.stolen_logged = core_stolen;
571 spin_lock_irq(&vcpu->arch.tbacct_lock);
572 stolen += vcpu->arch.busy_stolen;
573 vcpu->arch.busy_stolen = 0;
574 spin_unlock_irq(&vcpu->arch.tbacct_lock);
575 if (!dt || !vpa)
576 return;
577 memset(dt, 0, sizeof(struct dtl_entry));
578 dt->dispatch_reason = 7;
579 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
580 dt->timebase = cpu_to_be64(now + vc->tb_offset);
581 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
582 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
583 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
584 ++dt;
585 if (dt == vcpu->arch.dtl.pinned_end)
586 dt = vcpu->arch.dtl.pinned_addr;
587 vcpu->arch.dtl_ptr = dt;
588 /* order writing *dt vs. writing vpa->dtl_idx */
589 smp_wmb();
590 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
591 vcpu->arch.dtl.dirty = true;
594 static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
596 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
597 return true;
598 if ((!vcpu->arch.vcore->arch_compat) &&
599 cpu_has_feature(CPU_FTR_ARCH_207S))
600 return true;
601 return false;
604 static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
605 unsigned long resource, unsigned long value1,
606 unsigned long value2)
608 switch (resource) {
609 case H_SET_MODE_RESOURCE_SET_CIABR:
610 if (!kvmppc_power8_compatible(vcpu))
611 return H_P2;
612 if (value2)
613 return H_P4;
614 if (mflags)
615 return H_UNSUPPORTED_FLAG_START;
616 /* Guests can't breakpoint the hypervisor */
617 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
618 return H_P3;
619 vcpu->arch.ciabr = value1;
620 return H_SUCCESS;
621 case H_SET_MODE_RESOURCE_SET_DAWR:
622 if (!kvmppc_power8_compatible(vcpu))
623 return H_P2;
624 if (mflags)
625 return H_UNSUPPORTED_FLAG_START;
626 if (value2 & DABRX_HYP)
627 return H_P4;
628 vcpu->arch.dawr = value1;
629 vcpu->arch.dawrx = value2;
630 return H_SUCCESS;
631 default:
632 return H_TOO_HARD;
636 static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
638 struct kvmppc_vcore *vcore = target->arch.vcore;
641 * We expect to have been called by the real mode handler
642 * (kvmppc_rm_h_confer()) which would have directly returned
643 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
644 * have useful work to do and should not confer) so we don't
645 * recheck that here.
648 spin_lock(&vcore->lock);
649 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
650 vcore->vcore_state != VCORE_INACTIVE)
651 target = vcore->runner;
652 spin_unlock(&vcore->lock);
654 return kvm_vcpu_yield_to(target);
657 static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
659 int yield_count = 0;
660 struct lppaca *lppaca;
662 spin_lock(&vcpu->arch.vpa_update_lock);
663 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
664 if (lppaca)
665 yield_count = be32_to_cpu(lppaca->yield_count);
666 spin_unlock(&vcpu->arch.vpa_update_lock);
667 return yield_count;
670 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
672 unsigned long req = kvmppc_get_gpr(vcpu, 3);
673 unsigned long target, ret = H_SUCCESS;
674 int yield_count;
675 struct kvm_vcpu *tvcpu;
676 int idx, rc;
678 if (req <= MAX_HCALL_OPCODE &&
679 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
680 return RESUME_HOST;
682 switch (req) {
683 case H_CEDE:
684 break;
685 case H_PROD:
686 target = kvmppc_get_gpr(vcpu, 4);
687 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
688 if (!tvcpu) {
689 ret = H_PARAMETER;
690 break;
692 tvcpu->arch.prodded = 1;
693 smp_mb();
694 if (vcpu->arch.ceded) {
695 if (waitqueue_active(&vcpu->wq)) {
696 wake_up_interruptible(&vcpu->wq);
697 vcpu->stat.halt_wakeup++;
700 break;
701 case H_CONFER:
702 target = kvmppc_get_gpr(vcpu, 4);
703 if (target == -1)
704 break;
705 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
706 if (!tvcpu) {
707 ret = H_PARAMETER;
708 break;
710 yield_count = kvmppc_get_gpr(vcpu, 5);
711 if (kvmppc_get_yield_count(tvcpu) != yield_count)
712 break;
713 kvm_arch_vcpu_yield_to(tvcpu);
714 break;
715 case H_REGISTER_VPA:
716 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
717 kvmppc_get_gpr(vcpu, 5),
718 kvmppc_get_gpr(vcpu, 6));
719 break;
720 case H_RTAS:
721 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
722 return RESUME_HOST;
724 idx = srcu_read_lock(&vcpu->kvm->srcu);
725 rc = kvmppc_rtas_hcall(vcpu);
726 srcu_read_unlock(&vcpu->kvm->srcu, idx);
728 if (rc == -ENOENT)
729 return RESUME_HOST;
730 else if (rc == 0)
731 break;
733 /* Send the error out to userspace via KVM_RUN */
734 return rc;
735 case H_LOGICAL_CI_LOAD:
736 ret = kvmppc_h_logical_ci_load(vcpu);
737 if (ret == H_TOO_HARD)
738 return RESUME_HOST;
739 break;
740 case H_LOGICAL_CI_STORE:
741 ret = kvmppc_h_logical_ci_store(vcpu);
742 if (ret == H_TOO_HARD)
743 return RESUME_HOST;
744 break;
745 case H_SET_MODE:
746 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
747 kvmppc_get_gpr(vcpu, 5),
748 kvmppc_get_gpr(vcpu, 6),
749 kvmppc_get_gpr(vcpu, 7));
750 if (ret == H_TOO_HARD)
751 return RESUME_HOST;
752 break;
753 case H_XIRR:
754 case H_CPPR:
755 case H_EOI:
756 case H_IPI:
757 case H_IPOLL:
758 case H_XIRR_X:
759 if (kvmppc_xics_enabled(vcpu)) {
760 ret = kvmppc_xics_hcall(vcpu, req);
761 break;
762 } /* fallthrough */
763 default:
764 return RESUME_HOST;
766 kvmppc_set_gpr(vcpu, 3, ret);
767 vcpu->arch.hcall_needed = 0;
768 return RESUME_GUEST;
771 static int kvmppc_hcall_impl_hv(unsigned long cmd)
773 switch (cmd) {
774 case H_CEDE:
775 case H_PROD:
776 case H_CONFER:
777 case H_REGISTER_VPA:
778 case H_SET_MODE:
779 case H_LOGICAL_CI_LOAD:
780 case H_LOGICAL_CI_STORE:
781 #ifdef CONFIG_KVM_XICS
782 case H_XIRR:
783 case H_CPPR:
784 case H_EOI:
785 case H_IPI:
786 case H_IPOLL:
787 case H_XIRR_X:
788 #endif
789 return 1;
792 /* See if it's in the real-mode table */
793 return kvmppc_hcall_impl_hv_realmode(cmd);
796 static int kvmppc_emulate_debug_inst(struct kvm_run *run,
797 struct kvm_vcpu *vcpu)
799 u32 last_inst;
801 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
802 EMULATE_DONE) {
804 * Fetch failed, so return to guest and
805 * try executing it again.
807 return RESUME_GUEST;
810 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
811 run->exit_reason = KVM_EXIT_DEBUG;
812 run->debug.arch.address = kvmppc_get_pc(vcpu);
813 return RESUME_HOST;
814 } else {
815 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
816 return RESUME_GUEST;
820 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
821 struct task_struct *tsk)
823 int r = RESUME_HOST;
825 vcpu->stat.sum_exits++;
827 run->exit_reason = KVM_EXIT_UNKNOWN;
828 run->ready_for_interrupt_injection = 1;
829 switch (vcpu->arch.trap) {
830 /* We're good on these - the host merely wanted to get our attention */
831 case BOOK3S_INTERRUPT_HV_DECREMENTER:
832 vcpu->stat.dec_exits++;
833 r = RESUME_GUEST;
834 break;
835 case BOOK3S_INTERRUPT_EXTERNAL:
836 case BOOK3S_INTERRUPT_H_DOORBELL:
837 vcpu->stat.ext_intr_exits++;
838 r = RESUME_GUEST;
839 break;
840 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
841 case BOOK3S_INTERRUPT_HMI:
842 case BOOK3S_INTERRUPT_PERFMON:
843 r = RESUME_GUEST;
844 break;
845 case BOOK3S_INTERRUPT_MACHINE_CHECK:
847 * Deliver a machine check interrupt to the guest.
848 * We have to do this, even if the host has handled the
849 * machine check, because machine checks use SRR0/1 and
850 * the interrupt might have trashed guest state in them.
852 kvmppc_book3s_queue_irqprio(vcpu,
853 BOOK3S_INTERRUPT_MACHINE_CHECK);
854 r = RESUME_GUEST;
855 break;
856 case BOOK3S_INTERRUPT_PROGRAM:
858 ulong flags;
860 * Normally program interrupts are delivered directly
861 * to the guest by the hardware, but we can get here
862 * as a result of a hypervisor emulation interrupt
863 * (e40) getting turned into a 700 by BML RTAS.
865 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
866 kvmppc_core_queue_program(vcpu, flags);
867 r = RESUME_GUEST;
868 break;
870 case BOOK3S_INTERRUPT_SYSCALL:
872 /* hcall - punt to userspace */
873 int i;
875 /* hypercall with MSR_PR has already been handled in rmode,
876 * and never reaches here.
879 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
880 for (i = 0; i < 9; ++i)
881 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
882 run->exit_reason = KVM_EXIT_PAPR_HCALL;
883 vcpu->arch.hcall_needed = 1;
884 r = RESUME_HOST;
885 break;
888 * We get these next two if the guest accesses a page which it thinks
889 * it has mapped but which is not actually present, either because
890 * it is for an emulated I/O device or because the corresonding
891 * host page has been paged out. Any other HDSI/HISI interrupts
892 * have been handled already.
894 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
895 r = RESUME_PAGE_FAULT;
896 break;
897 case BOOK3S_INTERRUPT_H_INST_STORAGE:
898 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
899 vcpu->arch.fault_dsisr = 0;
900 r = RESUME_PAGE_FAULT;
901 break;
903 * This occurs if the guest executes an illegal instruction.
904 * If the guest debug is disabled, generate a program interrupt
905 * to the guest. If guest debug is enabled, we need to check
906 * whether the instruction is a software breakpoint instruction.
907 * Accordingly return to Guest or Host.
909 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
910 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
911 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
912 swab32(vcpu->arch.emul_inst) :
913 vcpu->arch.emul_inst;
914 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
915 r = kvmppc_emulate_debug_inst(run, vcpu);
916 } else {
917 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
918 r = RESUME_GUEST;
920 break;
922 * This occurs if the guest (kernel or userspace), does something that
923 * is prohibited by HFSCR. We just generate a program interrupt to
924 * the guest.
926 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
927 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
928 r = RESUME_GUEST;
929 break;
930 default:
931 kvmppc_dump_regs(vcpu);
932 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
933 vcpu->arch.trap, kvmppc_get_pc(vcpu),
934 vcpu->arch.shregs.msr);
935 run->hw.hardware_exit_reason = vcpu->arch.trap;
936 r = RESUME_HOST;
937 break;
940 return r;
943 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
944 struct kvm_sregs *sregs)
946 int i;
948 memset(sregs, 0, sizeof(struct kvm_sregs));
949 sregs->pvr = vcpu->arch.pvr;
950 for (i = 0; i < vcpu->arch.slb_max; i++) {
951 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
952 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
955 return 0;
958 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
959 struct kvm_sregs *sregs)
961 int i, j;
963 /* Only accept the same PVR as the host's, since we can't spoof it */
964 if (sregs->pvr != vcpu->arch.pvr)
965 return -EINVAL;
967 j = 0;
968 for (i = 0; i < vcpu->arch.slb_nr; i++) {
969 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
970 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
971 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
972 ++j;
975 vcpu->arch.slb_max = j;
977 return 0;
980 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
981 bool preserve_top32)
983 struct kvm *kvm = vcpu->kvm;
984 struct kvmppc_vcore *vc = vcpu->arch.vcore;
985 u64 mask;
987 mutex_lock(&kvm->lock);
988 spin_lock(&vc->lock);
990 * If ILE (interrupt little-endian) has changed, update the
991 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
993 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
994 struct kvm_vcpu *vcpu;
995 int i;
997 kvm_for_each_vcpu(i, vcpu, kvm) {
998 if (vcpu->arch.vcore != vc)
999 continue;
1000 if (new_lpcr & LPCR_ILE)
1001 vcpu->arch.intr_msr |= MSR_LE;
1002 else
1003 vcpu->arch.intr_msr &= ~MSR_LE;
1008 * Userspace can only modify DPFD (default prefetch depth),
1009 * ILE (interrupt little-endian) and TC (translation control).
1010 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
1012 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
1013 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1014 mask |= LPCR_AIL;
1016 /* Broken 32-bit version of LPCR must not clear top bits */
1017 if (preserve_top32)
1018 mask &= 0xFFFFFFFF;
1019 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1020 spin_unlock(&vc->lock);
1021 mutex_unlock(&kvm->lock);
1024 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1025 union kvmppc_one_reg *val)
1027 int r = 0;
1028 long int i;
1030 switch (id) {
1031 case KVM_REG_PPC_DEBUG_INST:
1032 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1033 break;
1034 case KVM_REG_PPC_HIOR:
1035 *val = get_reg_val(id, 0);
1036 break;
1037 case KVM_REG_PPC_DABR:
1038 *val = get_reg_val(id, vcpu->arch.dabr);
1039 break;
1040 case KVM_REG_PPC_DABRX:
1041 *val = get_reg_val(id, vcpu->arch.dabrx);
1042 break;
1043 case KVM_REG_PPC_DSCR:
1044 *val = get_reg_val(id, vcpu->arch.dscr);
1045 break;
1046 case KVM_REG_PPC_PURR:
1047 *val = get_reg_val(id, vcpu->arch.purr);
1048 break;
1049 case KVM_REG_PPC_SPURR:
1050 *val = get_reg_val(id, vcpu->arch.spurr);
1051 break;
1052 case KVM_REG_PPC_AMR:
1053 *val = get_reg_val(id, vcpu->arch.amr);
1054 break;
1055 case KVM_REG_PPC_UAMOR:
1056 *val = get_reg_val(id, vcpu->arch.uamor);
1057 break;
1058 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1059 i = id - KVM_REG_PPC_MMCR0;
1060 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1061 break;
1062 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1063 i = id - KVM_REG_PPC_PMC1;
1064 *val = get_reg_val(id, vcpu->arch.pmc[i]);
1065 break;
1066 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1067 i = id - KVM_REG_PPC_SPMC1;
1068 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1069 break;
1070 case KVM_REG_PPC_SIAR:
1071 *val = get_reg_val(id, vcpu->arch.siar);
1072 break;
1073 case KVM_REG_PPC_SDAR:
1074 *val = get_reg_val(id, vcpu->arch.sdar);
1075 break;
1076 case KVM_REG_PPC_SIER:
1077 *val = get_reg_val(id, vcpu->arch.sier);
1078 break;
1079 case KVM_REG_PPC_IAMR:
1080 *val = get_reg_val(id, vcpu->arch.iamr);
1081 break;
1082 case KVM_REG_PPC_PSPB:
1083 *val = get_reg_val(id, vcpu->arch.pspb);
1084 break;
1085 case KVM_REG_PPC_DPDES:
1086 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1087 break;
1088 case KVM_REG_PPC_DAWR:
1089 *val = get_reg_val(id, vcpu->arch.dawr);
1090 break;
1091 case KVM_REG_PPC_DAWRX:
1092 *val = get_reg_val(id, vcpu->arch.dawrx);
1093 break;
1094 case KVM_REG_PPC_CIABR:
1095 *val = get_reg_val(id, vcpu->arch.ciabr);
1096 break;
1097 case KVM_REG_PPC_CSIGR:
1098 *val = get_reg_val(id, vcpu->arch.csigr);
1099 break;
1100 case KVM_REG_PPC_TACR:
1101 *val = get_reg_val(id, vcpu->arch.tacr);
1102 break;
1103 case KVM_REG_PPC_TCSCR:
1104 *val = get_reg_val(id, vcpu->arch.tcscr);
1105 break;
1106 case KVM_REG_PPC_PID:
1107 *val = get_reg_val(id, vcpu->arch.pid);
1108 break;
1109 case KVM_REG_PPC_ACOP:
1110 *val = get_reg_val(id, vcpu->arch.acop);
1111 break;
1112 case KVM_REG_PPC_WORT:
1113 *val = get_reg_val(id, vcpu->arch.wort);
1114 break;
1115 case KVM_REG_PPC_VPA_ADDR:
1116 spin_lock(&vcpu->arch.vpa_update_lock);
1117 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1118 spin_unlock(&vcpu->arch.vpa_update_lock);
1119 break;
1120 case KVM_REG_PPC_VPA_SLB:
1121 spin_lock(&vcpu->arch.vpa_update_lock);
1122 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1123 val->vpaval.length = vcpu->arch.slb_shadow.len;
1124 spin_unlock(&vcpu->arch.vpa_update_lock);
1125 break;
1126 case KVM_REG_PPC_VPA_DTL:
1127 spin_lock(&vcpu->arch.vpa_update_lock);
1128 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1129 val->vpaval.length = vcpu->arch.dtl.len;
1130 spin_unlock(&vcpu->arch.vpa_update_lock);
1131 break;
1132 case KVM_REG_PPC_TB_OFFSET:
1133 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1134 break;
1135 case KVM_REG_PPC_LPCR:
1136 case KVM_REG_PPC_LPCR_64:
1137 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1138 break;
1139 case KVM_REG_PPC_PPR:
1140 *val = get_reg_val(id, vcpu->arch.ppr);
1141 break;
1142 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1143 case KVM_REG_PPC_TFHAR:
1144 *val = get_reg_val(id, vcpu->arch.tfhar);
1145 break;
1146 case KVM_REG_PPC_TFIAR:
1147 *val = get_reg_val(id, vcpu->arch.tfiar);
1148 break;
1149 case KVM_REG_PPC_TEXASR:
1150 *val = get_reg_val(id, vcpu->arch.texasr);
1151 break;
1152 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1153 i = id - KVM_REG_PPC_TM_GPR0;
1154 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1155 break;
1156 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1158 int j;
1159 i = id - KVM_REG_PPC_TM_VSR0;
1160 if (i < 32)
1161 for (j = 0; j < TS_FPRWIDTH; j++)
1162 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1163 else {
1164 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1165 val->vval = vcpu->arch.vr_tm.vr[i-32];
1166 else
1167 r = -ENXIO;
1169 break;
1171 case KVM_REG_PPC_TM_CR:
1172 *val = get_reg_val(id, vcpu->arch.cr_tm);
1173 break;
1174 case KVM_REG_PPC_TM_LR:
1175 *val = get_reg_val(id, vcpu->arch.lr_tm);
1176 break;
1177 case KVM_REG_PPC_TM_CTR:
1178 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1179 break;
1180 case KVM_REG_PPC_TM_FPSCR:
1181 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1182 break;
1183 case KVM_REG_PPC_TM_AMR:
1184 *val = get_reg_val(id, vcpu->arch.amr_tm);
1185 break;
1186 case KVM_REG_PPC_TM_PPR:
1187 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1188 break;
1189 case KVM_REG_PPC_TM_VRSAVE:
1190 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1191 break;
1192 case KVM_REG_PPC_TM_VSCR:
1193 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1194 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1195 else
1196 r = -ENXIO;
1197 break;
1198 case KVM_REG_PPC_TM_DSCR:
1199 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1200 break;
1201 case KVM_REG_PPC_TM_TAR:
1202 *val = get_reg_val(id, vcpu->arch.tar_tm);
1203 break;
1204 #endif
1205 case KVM_REG_PPC_ARCH_COMPAT:
1206 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1207 break;
1208 default:
1209 r = -EINVAL;
1210 break;
1213 return r;
1216 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1217 union kvmppc_one_reg *val)
1219 int r = 0;
1220 long int i;
1221 unsigned long addr, len;
1223 switch (id) {
1224 case KVM_REG_PPC_HIOR:
1225 /* Only allow this to be set to zero */
1226 if (set_reg_val(id, *val))
1227 r = -EINVAL;
1228 break;
1229 case KVM_REG_PPC_DABR:
1230 vcpu->arch.dabr = set_reg_val(id, *val);
1231 break;
1232 case KVM_REG_PPC_DABRX:
1233 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1234 break;
1235 case KVM_REG_PPC_DSCR:
1236 vcpu->arch.dscr = set_reg_val(id, *val);
1237 break;
1238 case KVM_REG_PPC_PURR:
1239 vcpu->arch.purr = set_reg_val(id, *val);
1240 break;
1241 case KVM_REG_PPC_SPURR:
1242 vcpu->arch.spurr = set_reg_val(id, *val);
1243 break;
1244 case KVM_REG_PPC_AMR:
1245 vcpu->arch.amr = set_reg_val(id, *val);
1246 break;
1247 case KVM_REG_PPC_UAMOR:
1248 vcpu->arch.uamor = set_reg_val(id, *val);
1249 break;
1250 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1251 i = id - KVM_REG_PPC_MMCR0;
1252 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1253 break;
1254 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1255 i = id - KVM_REG_PPC_PMC1;
1256 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1257 break;
1258 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1259 i = id - KVM_REG_PPC_SPMC1;
1260 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1261 break;
1262 case KVM_REG_PPC_SIAR:
1263 vcpu->arch.siar = set_reg_val(id, *val);
1264 break;
1265 case KVM_REG_PPC_SDAR:
1266 vcpu->arch.sdar = set_reg_val(id, *val);
1267 break;
1268 case KVM_REG_PPC_SIER:
1269 vcpu->arch.sier = set_reg_val(id, *val);
1270 break;
1271 case KVM_REG_PPC_IAMR:
1272 vcpu->arch.iamr = set_reg_val(id, *val);
1273 break;
1274 case KVM_REG_PPC_PSPB:
1275 vcpu->arch.pspb = set_reg_val(id, *val);
1276 break;
1277 case KVM_REG_PPC_DPDES:
1278 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1279 break;
1280 case KVM_REG_PPC_DAWR:
1281 vcpu->arch.dawr = set_reg_val(id, *val);
1282 break;
1283 case KVM_REG_PPC_DAWRX:
1284 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1285 break;
1286 case KVM_REG_PPC_CIABR:
1287 vcpu->arch.ciabr = set_reg_val(id, *val);
1288 /* Don't allow setting breakpoints in hypervisor code */
1289 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1290 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1291 break;
1292 case KVM_REG_PPC_CSIGR:
1293 vcpu->arch.csigr = set_reg_val(id, *val);
1294 break;
1295 case KVM_REG_PPC_TACR:
1296 vcpu->arch.tacr = set_reg_val(id, *val);
1297 break;
1298 case KVM_REG_PPC_TCSCR:
1299 vcpu->arch.tcscr = set_reg_val(id, *val);
1300 break;
1301 case KVM_REG_PPC_PID:
1302 vcpu->arch.pid = set_reg_val(id, *val);
1303 break;
1304 case KVM_REG_PPC_ACOP:
1305 vcpu->arch.acop = set_reg_val(id, *val);
1306 break;
1307 case KVM_REG_PPC_WORT:
1308 vcpu->arch.wort = set_reg_val(id, *val);
1309 break;
1310 case KVM_REG_PPC_VPA_ADDR:
1311 addr = set_reg_val(id, *val);
1312 r = -EINVAL;
1313 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1314 vcpu->arch.dtl.next_gpa))
1315 break;
1316 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1317 break;
1318 case KVM_REG_PPC_VPA_SLB:
1319 addr = val->vpaval.addr;
1320 len = val->vpaval.length;
1321 r = -EINVAL;
1322 if (addr && !vcpu->arch.vpa.next_gpa)
1323 break;
1324 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1325 break;
1326 case KVM_REG_PPC_VPA_DTL:
1327 addr = val->vpaval.addr;
1328 len = val->vpaval.length;
1329 r = -EINVAL;
1330 if (addr && (len < sizeof(struct dtl_entry) ||
1331 !vcpu->arch.vpa.next_gpa))
1332 break;
1333 len -= len % sizeof(struct dtl_entry);
1334 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1335 break;
1336 case KVM_REG_PPC_TB_OFFSET:
1337 /* round up to multiple of 2^24 */
1338 vcpu->arch.vcore->tb_offset =
1339 ALIGN(set_reg_val(id, *val), 1UL << 24);
1340 break;
1341 case KVM_REG_PPC_LPCR:
1342 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1343 break;
1344 case KVM_REG_PPC_LPCR_64:
1345 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
1346 break;
1347 case KVM_REG_PPC_PPR:
1348 vcpu->arch.ppr = set_reg_val(id, *val);
1349 break;
1350 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1351 case KVM_REG_PPC_TFHAR:
1352 vcpu->arch.tfhar = set_reg_val(id, *val);
1353 break;
1354 case KVM_REG_PPC_TFIAR:
1355 vcpu->arch.tfiar = set_reg_val(id, *val);
1356 break;
1357 case KVM_REG_PPC_TEXASR:
1358 vcpu->arch.texasr = set_reg_val(id, *val);
1359 break;
1360 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1361 i = id - KVM_REG_PPC_TM_GPR0;
1362 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1363 break;
1364 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1366 int j;
1367 i = id - KVM_REG_PPC_TM_VSR0;
1368 if (i < 32)
1369 for (j = 0; j < TS_FPRWIDTH; j++)
1370 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1371 else
1372 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1373 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1374 else
1375 r = -ENXIO;
1376 break;
1378 case KVM_REG_PPC_TM_CR:
1379 vcpu->arch.cr_tm = set_reg_val(id, *val);
1380 break;
1381 case KVM_REG_PPC_TM_LR:
1382 vcpu->arch.lr_tm = set_reg_val(id, *val);
1383 break;
1384 case KVM_REG_PPC_TM_CTR:
1385 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1386 break;
1387 case KVM_REG_PPC_TM_FPSCR:
1388 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1389 break;
1390 case KVM_REG_PPC_TM_AMR:
1391 vcpu->arch.amr_tm = set_reg_val(id, *val);
1392 break;
1393 case KVM_REG_PPC_TM_PPR:
1394 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1395 break;
1396 case KVM_REG_PPC_TM_VRSAVE:
1397 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1398 break;
1399 case KVM_REG_PPC_TM_VSCR:
1400 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1401 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1402 else
1403 r = - ENXIO;
1404 break;
1405 case KVM_REG_PPC_TM_DSCR:
1406 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1407 break;
1408 case KVM_REG_PPC_TM_TAR:
1409 vcpu->arch.tar_tm = set_reg_val(id, *val);
1410 break;
1411 #endif
1412 case KVM_REG_PPC_ARCH_COMPAT:
1413 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1414 break;
1415 default:
1416 r = -EINVAL;
1417 break;
1420 return r;
1423 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1425 struct kvmppc_vcore *vcore;
1427 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1429 if (vcore == NULL)
1430 return NULL;
1432 INIT_LIST_HEAD(&vcore->runnable_threads);
1433 spin_lock_init(&vcore->lock);
1434 spin_lock_init(&vcore->stoltb_lock);
1435 init_waitqueue_head(&vcore->wq);
1436 vcore->preempt_tb = TB_NIL;
1437 vcore->lpcr = kvm->arch.lpcr;
1438 vcore->first_vcpuid = core * threads_per_subcore;
1439 vcore->kvm = kvm;
1441 vcore->mpp_buffer_is_valid = false;
1443 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1444 vcore->mpp_buffer = (void *)__get_free_pages(
1445 GFP_KERNEL|__GFP_ZERO,
1446 MPP_BUFFER_ORDER);
1448 return vcore;
1451 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1452 static struct debugfs_timings_element {
1453 const char *name;
1454 size_t offset;
1455 } timings[] = {
1456 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1457 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1458 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1459 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1460 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1463 #define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1465 struct debugfs_timings_state {
1466 struct kvm_vcpu *vcpu;
1467 unsigned int buflen;
1468 char buf[N_TIMINGS * 100];
1471 static int debugfs_timings_open(struct inode *inode, struct file *file)
1473 struct kvm_vcpu *vcpu = inode->i_private;
1474 struct debugfs_timings_state *p;
1476 p = kzalloc(sizeof(*p), GFP_KERNEL);
1477 if (!p)
1478 return -ENOMEM;
1480 kvm_get_kvm(vcpu->kvm);
1481 p->vcpu = vcpu;
1482 file->private_data = p;
1484 return nonseekable_open(inode, file);
1487 static int debugfs_timings_release(struct inode *inode, struct file *file)
1489 struct debugfs_timings_state *p = file->private_data;
1491 kvm_put_kvm(p->vcpu->kvm);
1492 kfree(p);
1493 return 0;
1496 static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1497 size_t len, loff_t *ppos)
1499 struct debugfs_timings_state *p = file->private_data;
1500 struct kvm_vcpu *vcpu = p->vcpu;
1501 char *s, *buf_end;
1502 struct kvmhv_tb_accumulator tb;
1503 u64 count;
1504 loff_t pos;
1505 ssize_t n;
1506 int i, loops;
1507 bool ok;
1509 if (!p->buflen) {
1510 s = p->buf;
1511 buf_end = s + sizeof(p->buf);
1512 for (i = 0; i < N_TIMINGS; ++i) {
1513 struct kvmhv_tb_accumulator *acc;
1515 acc = (struct kvmhv_tb_accumulator *)
1516 ((unsigned long)vcpu + timings[i].offset);
1517 ok = false;
1518 for (loops = 0; loops < 1000; ++loops) {
1519 count = acc->seqcount;
1520 if (!(count & 1)) {
1521 smp_rmb();
1522 tb = *acc;
1523 smp_rmb();
1524 if (count == acc->seqcount) {
1525 ok = true;
1526 break;
1529 udelay(1);
1531 if (!ok)
1532 snprintf(s, buf_end - s, "%s: stuck\n",
1533 timings[i].name);
1534 else
1535 snprintf(s, buf_end - s,
1536 "%s: %llu %llu %llu %llu\n",
1537 timings[i].name, count / 2,
1538 tb_to_ns(tb.tb_total),
1539 tb_to_ns(tb.tb_min),
1540 tb_to_ns(tb.tb_max));
1541 s += strlen(s);
1543 p->buflen = s - p->buf;
1546 pos = *ppos;
1547 if (pos >= p->buflen)
1548 return 0;
1549 if (len > p->buflen - pos)
1550 len = p->buflen - pos;
1551 n = copy_to_user(buf, p->buf + pos, len);
1552 if (n) {
1553 if (n == len)
1554 return -EFAULT;
1555 len -= n;
1557 *ppos = pos + len;
1558 return len;
1561 static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1562 size_t len, loff_t *ppos)
1564 return -EACCES;
1567 static const struct file_operations debugfs_timings_ops = {
1568 .owner = THIS_MODULE,
1569 .open = debugfs_timings_open,
1570 .release = debugfs_timings_release,
1571 .read = debugfs_timings_read,
1572 .write = debugfs_timings_write,
1573 .llseek = generic_file_llseek,
1576 /* Create a debugfs directory for the vcpu */
1577 static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1579 char buf[16];
1580 struct kvm *kvm = vcpu->kvm;
1582 snprintf(buf, sizeof(buf), "vcpu%u", id);
1583 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1584 return;
1585 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1586 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1587 return;
1588 vcpu->arch.debugfs_timings =
1589 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1590 vcpu, &debugfs_timings_ops);
1593 #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1594 static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1597 #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1599 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1600 unsigned int id)
1602 struct kvm_vcpu *vcpu;
1603 int err = -EINVAL;
1604 int core;
1605 struct kvmppc_vcore *vcore;
1607 core = id / threads_per_subcore;
1608 if (core >= KVM_MAX_VCORES)
1609 goto out;
1611 err = -ENOMEM;
1612 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1613 if (!vcpu)
1614 goto out;
1616 err = kvm_vcpu_init(vcpu, kvm, id);
1617 if (err)
1618 goto free_vcpu;
1620 vcpu->arch.shared = &vcpu->arch.shregs;
1621 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1623 * The shared struct is never shared on HV,
1624 * so we can always use host endianness
1626 #ifdef __BIG_ENDIAN__
1627 vcpu->arch.shared_big_endian = true;
1628 #else
1629 vcpu->arch.shared_big_endian = false;
1630 #endif
1631 #endif
1632 vcpu->arch.mmcr[0] = MMCR0_FC;
1633 vcpu->arch.ctrl = CTRL_RUNLATCH;
1634 /* default to host PVR, since we can't spoof it */
1635 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1636 spin_lock_init(&vcpu->arch.vpa_update_lock);
1637 spin_lock_init(&vcpu->arch.tbacct_lock);
1638 vcpu->arch.busy_preempt = TB_NIL;
1639 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1641 kvmppc_mmu_book3s_hv_init(vcpu);
1643 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1645 init_waitqueue_head(&vcpu->arch.cpu_run);
1647 mutex_lock(&kvm->lock);
1648 vcore = kvm->arch.vcores[core];
1649 if (!vcore) {
1650 vcore = kvmppc_vcore_create(kvm, core);
1651 kvm->arch.vcores[core] = vcore;
1652 kvm->arch.online_vcores++;
1654 mutex_unlock(&kvm->lock);
1656 if (!vcore)
1657 goto free_vcpu;
1659 spin_lock(&vcore->lock);
1660 ++vcore->num_threads;
1661 spin_unlock(&vcore->lock);
1662 vcpu->arch.vcore = vcore;
1663 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1665 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1666 kvmppc_sanity_check(vcpu);
1668 debugfs_vcpu_init(vcpu, id);
1670 return vcpu;
1672 free_vcpu:
1673 kmem_cache_free(kvm_vcpu_cache, vcpu);
1674 out:
1675 return ERR_PTR(err);
1678 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1680 if (vpa->pinned_addr)
1681 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1682 vpa->dirty);
1685 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1687 spin_lock(&vcpu->arch.vpa_update_lock);
1688 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1689 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1690 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1691 spin_unlock(&vcpu->arch.vpa_update_lock);
1692 kvm_vcpu_uninit(vcpu);
1693 kmem_cache_free(kvm_vcpu_cache, vcpu);
1696 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1698 /* Indicate we want to get back into the guest */
1699 return 1;
1702 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1704 unsigned long dec_nsec, now;
1706 now = get_tb();
1707 if (now > vcpu->arch.dec_expires) {
1708 /* decrementer has already gone negative */
1709 kvmppc_core_queue_dec(vcpu);
1710 kvmppc_core_prepare_to_enter(vcpu);
1711 return;
1713 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1714 / tb_ticks_per_sec;
1715 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1716 HRTIMER_MODE_REL);
1717 vcpu->arch.timer_running = 1;
1720 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1722 vcpu->arch.ceded = 0;
1723 if (vcpu->arch.timer_running) {
1724 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1725 vcpu->arch.timer_running = 0;
1729 extern void __kvmppc_vcore_entry(void);
1731 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1732 struct kvm_vcpu *vcpu)
1734 u64 now;
1736 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1737 return;
1738 spin_lock_irq(&vcpu->arch.tbacct_lock);
1739 now = mftb();
1740 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1741 vcpu->arch.stolen_logged;
1742 vcpu->arch.busy_preempt = now;
1743 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1744 spin_unlock_irq(&vcpu->arch.tbacct_lock);
1745 --vc->n_runnable;
1746 list_del(&vcpu->arch.run_list);
1749 static int kvmppc_grab_hwthread(int cpu)
1751 struct paca_struct *tpaca;
1752 long timeout = 10000;
1754 tpaca = &paca[cpu];
1756 /* Ensure the thread won't go into the kernel if it wakes */
1757 tpaca->kvm_hstate.kvm_vcpu = NULL;
1758 tpaca->kvm_hstate.napping = 0;
1759 smp_wmb();
1760 tpaca->kvm_hstate.hwthread_req = 1;
1763 * If the thread is already executing in the kernel (e.g. handling
1764 * a stray interrupt), wait for it to get back to nap mode.
1765 * The smp_mb() is to ensure that our setting of hwthread_req
1766 * is visible before we look at hwthread_state, so if this
1767 * races with the code at system_reset_pSeries and the thread
1768 * misses our setting of hwthread_req, we are sure to see its
1769 * setting of hwthread_state, and vice versa.
1771 smp_mb();
1772 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1773 if (--timeout <= 0) {
1774 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1775 return -EBUSY;
1777 udelay(1);
1779 return 0;
1782 static void kvmppc_release_hwthread(int cpu)
1784 struct paca_struct *tpaca;
1786 tpaca = &paca[cpu];
1787 tpaca->kvm_hstate.hwthread_req = 0;
1788 tpaca->kvm_hstate.kvm_vcpu = NULL;
1791 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1793 int cpu;
1794 struct paca_struct *tpaca;
1795 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1797 if (vcpu->arch.timer_running) {
1798 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1799 vcpu->arch.timer_running = 0;
1801 cpu = vc->pcpu + vcpu->arch.ptid;
1802 tpaca = &paca[cpu];
1803 tpaca->kvm_hstate.kvm_vcore = vc;
1804 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1805 vcpu->cpu = vc->pcpu;
1806 /* Order stores to hstate.kvm_vcore etc. before store to kvm_vcpu */
1807 smp_wmb();
1808 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1809 if (cpu != smp_processor_id())
1810 kvmppc_ipi_thread(cpu);
1813 static void kvmppc_wait_for_nap(void)
1815 int cpu = smp_processor_id();
1816 int i, loops;
1818 for (loops = 0; loops < 1000000; ++loops) {
1820 * Check if all threads are finished.
1821 * We set the vcpu pointer when starting a thread
1822 * and the thread clears it when finished, so we look
1823 * for any threads that still have a non-NULL vcpu ptr.
1825 for (i = 1; i < threads_per_subcore; ++i)
1826 if (paca[cpu + i].kvm_hstate.kvm_vcpu)
1827 break;
1828 if (i == threads_per_subcore) {
1829 HMT_medium();
1830 return;
1832 HMT_low();
1834 HMT_medium();
1835 for (i = 1; i < threads_per_subcore; ++i)
1836 if (paca[cpu + i].kvm_hstate.kvm_vcpu)
1837 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
1841 * Check that we are on thread 0 and that any other threads in
1842 * this core are off-line. Then grab the threads so they can't
1843 * enter the kernel.
1845 static int on_primary_thread(void)
1847 int cpu = smp_processor_id();
1848 int thr;
1850 /* Are we on a primary subcore? */
1851 if (cpu_thread_in_subcore(cpu))
1852 return 0;
1854 thr = 0;
1855 while (++thr < threads_per_subcore)
1856 if (cpu_online(cpu + thr))
1857 return 0;
1859 /* Grab all hw threads so they can't go into the kernel */
1860 for (thr = 1; thr < threads_per_subcore; ++thr) {
1861 if (kvmppc_grab_hwthread(cpu + thr)) {
1862 /* Couldn't grab one; let the others go */
1863 do {
1864 kvmppc_release_hwthread(cpu + thr);
1865 } while (--thr > 0);
1866 return 0;
1869 return 1;
1872 static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1874 phys_addr_t phy_addr, mpp_addr;
1876 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1877 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1879 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1880 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1882 vc->mpp_buffer_is_valid = true;
1885 static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1887 phys_addr_t phy_addr, mpp_addr;
1889 phy_addr = virt_to_phys(vc->mpp_buffer);
1890 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1892 /* We must abort any in-progress save operations to ensure
1893 * the table is valid so that prefetch engine knows when to
1894 * stop prefetching. */
1895 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1896 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1899 static void prepare_threads(struct kvmppc_vcore *vc)
1901 struct kvm_vcpu *vcpu, *vnext;
1903 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1904 arch.run_list) {
1905 if (signal_pending(vcpu->arch.run_task))
1906 vcpu->arch.ret = -EINTR;
1907 else if (vcpu->arch.vpa.update_pending ||
1908 vcpu->arch.slb_shadow.update_pending ||
1909 vcpu->arch.dtl.update_pending)
1910 vcpu->arch.ret = RESUME_GUEST;
1911 else
1912 continue;
1913 kvmppc_remove_runnable(vc, vcpu);
1914 wake_up(&vcpu->arch.cpu_run);
1918 static void post_guest_process(struct kvmppc_vcore *vc)
1920 u64 now;
1921 long ret;
1922 struct kvm_vcpu *vcpu, *vnext;
1924 now = get_tb();
1925 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1926 arch.run_list) {
1927 /* cancel pending dec exception if dec is positive */
1928 if (now < vcpu->arch.dec_expires &&
1929 kvmppc_core_pending_dec(vcpu))
1930 kvmppc_core_dequeue_dec(vcpu);
1932 trace_kvm_guest_exit(vcpu);
1934 ret = RESUME_GUEST;
1935 if (vcpu->arch.trap)
1936 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1937 vcpu->arch.run_task);
1939 vcpu->arch.ret = ret;
1940 vcpu->arch.trap = 0;
1942 if (vcpu->arch.ceded) {
1943 if (!is_kvmppc_resume_guest(ret))
1944 kvmppc_end_cede(vcpu);
1945 else
1946 kvmppc_set_timer(vcpu);
1948 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
1949 kvmppc_remove_runnable(vc, vcpu);
1950 wake_up(&vcpu->arch.cpu_run);
1956 * Run a set of guest threads on a physical core.
1957 * Called with vc->lock held.
1959 static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
1961 struct kvm_vcpu *vcpu, *vnext;
1962 int i;
1963 int srcu_idx;
1966 * Remove from the list any threads that have a signal pending
1967 * or need a VPA update done
1969 prepare_threads(vc);
1971 /* if the runner is no longer runnable, let the caller pick a new one */
1972 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
1973 return;
1976 * Initialize *vc.
1978 vc->entry_exit_map = 0;
1979 vc->preempt_tb = TB_NIL;
1980 vc->in_guest = 0;
1981 vc->napping_threads = 0;
1982 vc->conferring_threads = 0;
1985 * Make sure we are running on primary threads, and that secondary
1986 * threads are offline. Also check if the number of threads in this
1987 * guest are greater than the current system threads per guest.
1989 if ((threads_per_core > 1) &&
1990 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
1991 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1992 arch.run_list) {
1993 vcpu->arch.ret = -EBUSY;
1994 kvmppc_remove_runnable(vc, vcpu);
1995 wake_up(&vcpu->arch.cpu_run);
1997 goto out;
2001 vc->pcpu = smp_processor_id();
2002 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
2003 kvmppc_start_thread(vcpu);
2004 kvmppc_create_dtl_entry(vcpu, vc);
2005 trace_kvm_guest_enter(vcpu);
2008 /* Set this explicitly in case thread 0 doesn't have a vcpu */
2009 get_paca()->kvm_hstate.kvm_vcore = vc;
2010 get_paca()->kvm_hstate.ptid = 0;
2012 vc->vcore_state = VCORE_RUNNING;
2013 preempt_disable();
2015 trace_kvmppc_run_core(vc, 0);
2017 spin_unlock(&vc->lock);
2019 kvm_guest_enter();
2021 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
2023 if (vc->mpp_buffer_is_valid)
2024 kvmppc_start_restoring_l2_cache(vc);
2026 __kvmppc_vcore_entry();
2028 spin_lock(&vc->lock);
2030 if (vc->mpp_buffer)
2031 kvmppc_start_saving_l2_cache(vc);
2033 /* disable sending of IPIs on virtual external irqs */
2034 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
2035 vcpu->cpu = -1;
2036 /* wait for secondary threads to finish writing their state to memory */
2037 kvmppc_wait_for_nap();
2038 for (i = 0; i < threads_per_subcore; ++i)
2039 kvmppc_release_hwthread(vc->pcpu + i);
2040 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
2041 vc->vcore_state = VCORE_EXITING;
2042 spin_unlock(&vc->lock);
2044 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2046 /* make sure updates to secondary vcpu structs are visible now */
2047 smp_mb();
2048 kvm_guest_exit();
2050 preempt_enable();
2052 spin_lock(&vc->lock);
2053 post_guest_process(vc);
2055 out:
2056 vc->vcore_state = VCORE_INACTIVE;
2057 trace_kvmppc_run_core(vc, 1);
2061 * Wait for some other vcpu thread to execute us, and
2062 * wake us up when we need to handle something in the host.
2064 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
2066 DEFINE_WAIT(wait);
2068 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
2069 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
2070 schedule();
2071 finish_wait(&vcpu->arch.cpu_run, &wait);
2075 * All the vcpus in this vcore are idle, so wait for a decrementer
2076 * or external interrupt to one of the vcpus. vc->lock is held.
2078 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2080 struct kvm_vcpu *vcpu;
2081 int do_sleep = 1;
2083 DEFINE_WAIT(wait);
2085 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
2088 * Check one last time for pending exceptions and ceded state after
2089 * we put ourselves on the wait queue
2091 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
2092 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
2093 do_sleep = 0;
2094 break;
2098 if (!do_sleep) {
2099 finish_wait(&vc->wq, &wait);
2100 return;
2103 vc->vcore_state = VCORE_SLEEPING;
2104 trace_kvmppc_vcore_blocked(vc, 0);
2105 spin_unlock(&vc->lock);
2106 schedule();
2107 finish_wait(&vc->wq, &wait);
2108 spin_lock(&vc->lock);
2109 vc->vcore_state = VCORE_INACTIVE;
2110 trace_kvmppc_vcore_blocked(vc, 1);
2113 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2115 int n_ceded;
2116 struct kvmppc_vcore *vc;
2117 struct kvm_vcpu *v, *vn;
2119 trace_kvmppc_run_vcpu_enter(vcpu);
2121 kvm_run->exit_reason = 0;
2122 vcpu->arch.ret = RESUME_GUEST;
2123 vcpu->arch.trap = 0;
2124 kvmppc_update_vpas(vcpu);
2127 * Synchronize with other threads in this virtual core
2129 vc = vcpu->arch.vcore;
2130 spin_lock(&vc->lock);
2131 vcpu->arch.ceded = 0;
2132 vcpu->arch.run_task = current;
2133 vcpu->arch.kvm_run = kvm_run;
2134 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
2135 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
2136 vcpu->arch.busy_preempt = TB_NIL;
2137 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
2138 ++vc->n_runnable;
2141 * This happens the first time this is called for a vcpu.
2142 * If the vcore is already running, we may be able to start
2143 * this thread straight away and have it join in.
2145 if (!signal_pending(current)) {
2146 if (vc->vcore_state == VCORE_RUNNING && !VCORE_IS_EXITING(vc)) {
2147 kvmppc_create_dtl_entry(vcpu, vc);
2148 kvmppc_start_thread(vcpu);
2149 trace_kvm_guest_enter(vcpu);
2150 } else if (vc->vcore_state == VCORE_SLEEPING) {
2151 wake_up(&vc->wq);
2156 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2157 !signal_pending(current)) {
2158 if (vc->vcore_state != VCORE_INACTIVE) {
2159 spin_unlock(&vc->lock);
2160 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
2161 spin_lock(&vc->lock);
2162 continue;
2164 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
2165 arch.run_list) {
2166 kvmppc_core_prepare_to_enter(v);
2167 if (signal_pending(v->arch.run_task)) {
2168 kvmppc_remove_runnable(vc, v);
2169 v->stat.signal_exits++;
2170 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2171 v->arch.ret = -EINTR;
2172 wake_up(&v->arch.cpu_run);
2175 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2176 break;
2177 n_ceded = 0;
2178 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
2179 if (!v->arch.pending_exceptions)
2180 n_ceded += v->arch.ceded;
2181 else
2182 v->arch.ceded = 0;
2184 vc->runner = vcpu;
2185 if (n_ceded == vc->n_runnable) {
2186 kvmppc_vcore_blocked(vc);
2187 } else if (need_resched()) {
2188 vc->vcore_state = VCORE_PREEMPT;
2189 /* Let something else run */
2190 cond_resched_lock(&vc->lock);
2191 vc->vcore_state = VCORE_INACTIVE;
2192 } else {
2193 kvmppc_run_core(vc);
2195 vc->runner = NULL;
2198 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2199 (vc->vcore_state == VCORE_RUNNING ||
2200 vc->vcore_state == VCORE_EXITING)) {
2201 spin_unlock(&vc->lock);
2202 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
2203 spin_lock(&vc->lock);
2206 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2207 kvmppc_remove_runnable(vc, vcpu);
2208 vcpu->stat.signal_exits++;
2209 kvm_run->exit_reason = KVM_EXIT_INTR;
2210 vcpu->arch.ret = -EINTR;
2213 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2214 /* Wake up some vcpu to run the core */
2215 v = list_first_entry(&vc->runnable_threads,
2216 struct kvm_vcpu, arch.run_list);
2217 wake_up(&v->arch.cpu_run);
2220 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
2221 spin_unlock(&vc->lock);
2222 return vcpu->arch.ret;
2225 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
2227 int r;
2228 int srcu_idx;
2230 if (!vcpu->arch.sane) {
2231 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2232 return -EINVAL;
2235 kvmppc_core_prepare_to_enter(vcpu);
2237 /* No need to go into the guest when all we'll do is come back out */
2238 if (signal_pending(current)) {
2239 run->exit_reason = KVM_EXIT_INTR;
2240 return -EINTR;
2243 atomic_inc(&vcpu->kvm->arch.vcpus_running);
2244 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
2245 smp_mb();
2247 /* On the first time here, set up HTAB and VRMA */
2248 if (!vcpu->kvm->arch.hpte_setup_done) {
2249 r = kvmppc_hv_setup_htab_rma(vcpu);
2250 if (r)
2251 goto out;
2254 flush_fp_to_thread(current);
2255 flush_altivec_to_thread(current);
2256 flush_vsx_to_thread(current);
2257 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
2258 vcpu->arch.pgdir = current->mm->pgd;
2259 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
2261 do {
2262 r = kvmppc_run_vcpu(run, vcpu);
2264 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2265 !(vcpu->arch.shregs.msr & MSR_PR)) {
2266 trace_kvm_hcall_enter(vcpu);
2267 r = kvmppc_pseries_do_hcall(vcpu);
2268 trace_kvm_hcall_exit(vcpu, r);
2269 kvmppc_core_prepare_to_enter(vcpu);
2270 } else if (r == RESUME_PAGE_FAULT) {
2271 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2272 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2273 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2274 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2276 } while (is_kvmppc_resume_guest(r));
2278 out:
2279 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
2280 atomic_dec(&vcpu->kvm->arch.vcpus_running);
2281 return r;
2284 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2285 int linux_psize)
2287 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2289 if (!def->shift)
2290 return;
2291 (*sps)->page_shift = def->shift;
2292 (*sps)->slb_enc = def->sllp;
2293 (*sps)->enc[0].page_shift = def->shift;
2294 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
2296 * Add 16MB MPSS support if host supports it
2298 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2299 (*sps)->enc[1].page_shift = 24;
2300 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2302 (*sps)++;
2305 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2306 struct kvm_ppc_smmu_info *info)
2308 struct kvm_ppc_one_seg_page_size *sps;
2310 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2311 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2312 info->flags |= KVM_PPC_1T_SEGMENTS;
2313 info->slb_size = mmu_slb_size;
2315 /* We only support these sizes for now, and no muti-size segments */
2316 sps = &info->sps[0];
2317 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2318 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2319 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2321 return 0;
2325 * Get (and clear) the dirty memory log for a memory slot.
2327 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2328 struct kvm_dirty_log *log)
2330 struct kvm_memory_slot *memslot;
2331 int r;
2332 unsigned long n;
2334 mutex_lock(&kvm->slots_lock);
2336 r = -EINVAL;
2337 if (log->slot >= KVM_USER_MEM_SLOTS)
2338 goto out;
2340 memslot = id_to_memslot(kvm->memslots, log->slot);
2341 r = -ENOENT;
2342 if (!memslot->dirty_bitmap)
2343 goto out;
2345 n = kvm_dirty_bitmap_bytes(memslot);
2346 memset(memslot->dirty_bitmap, 0, n);
2348 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
2349 if (r)
2350 goto out;
2352 r = -EFAULT;
2353 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2354 goto out;
2356 r = 0;
2357 out:
2358 mutex_unlock(&kvm->slots_lock);
2359 return r;
2362 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2363 struct kvm_memory_slot *dont)
2365 if (!dont || free->arch.rmap != dont->arch.rmap) {
2366 vfree(free->arch.rmap);
2367 free->arch.rmap = NULL;
2371 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2372 unsigned long npages)
2374 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2375 if (!slot->arch.rmap)
2376 return -ENOMEM;
2378 return 0;
2381 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2382 struct kvm_memory_slot *memslot,
2383 struct kvm_userspace_memory_region *mem)
2385 return 0;
2388 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2389 struct kvm_userspace_memory_region *mem,
2390 const struct kvm_memory_slot *old)
2392 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2393 struct kvm_memory_slot *memslot;
2395 if (npages && old->npages) {
2397 * If modifying a memslot, reset all the rmap dirty bits.
2398 * If this is a new memslot, we don't need to do anything
2399 * since the rmap array starts out as all zeroes,
2400 * i.e. no pages are dirty.
2402 memslot = id_to_memslot(kvm->memslots, mem->slot);
2403 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2408 * Update LPCR values in kvm->arch and in vcores.
2409 * Caller must hold kvm->lock.
2411 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2413 long int i;
2414 u32 cores_done = 0;
2416 if ((kvm->arch.lpcr & mask) == lpcr)
2417 return;
2419 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2421 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2422 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2423 if (!vc)
2424 continue;
2425 spin_lock(&vc->lock);
2426 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2427 spin_unlock(&vc->lock);
2428 if (++cores_done >= kvm->arch.online_vcores)
2429 break;
2433 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2435 return;
2438 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2440 int err = 0;
2441 struct kvm *kvm = vcpu->kvm;
2442 unsigned long hva;
2443 struct kvm_memory_slot *memslot;
2444 struct vm_area_struct *vma;
2445 unsigned long lpcr = 0, senc;
2446 unsigned long psize, porder;
2447 int srcu_idx;
2449 mutex_lock(&kvm->lock);
2450 if (kvm->arch.hpte_setup_done)
2451 goto out; /* another vcpu beat us to it */
2453 /* Allocate hashed page table (if not done already) and reset it */
2454 if (!kvm->arch.hpt_virt) {
2455 err = kvmppc_alloc_hpt(kvm, NULL);
2456 if (err) {
2457 pr_err("KVM: Couldn't alloc HPT\n");
2458 goto out;
2462 /* Look up the memslot for guest physical address 0 */
2463 srcu_idx = srcu_read_lock(&kvm->srcu);
2464 memslot = gfn_to_memslot(kvm, 0);
2466 /* We must have some memory at 0 by now */
2467 err = -EINVAL;
2468 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2469 goto out_srcu;
2471 /* Look up the VMA for the start of this memory slot */
2472 hva = memslot->userspace_addr;
2473 down_read(&current->mm->mmap_sem);
2474 vma = find_vma(current->mm, hva);
2475 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2476 goto up_out;
2478 psize = vma_kernel_pagesize(vma);
2479 porder = __ilog2(psize);
2481 up_read(&current->mm->mmap_sem);
2483 /* We can handle 4k, 64k or 16M pages in the VRMA */
2484 err = -EINVAL;
2485 if (!(psize == 0x1000 || psize == 0x10000 ||
2486 psize == 0x1000000))
2487 goto out_srcu;
2489 /* Update VRMASD field in the LPCR */
2490 senc = slb_pgsize_encoding(psize);
2491 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2492 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2493 /* the -4 is to account for senc values starting at 0x10 */
2494 lpcr = senc << (LPCR_VRMASD_SH - 4);
2496 /* Create HPTEs in the hash page table for the VRMA */
2497 kvmppc_map_vrma(vcpu, memslot, porder);
2499 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
2501 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
2502 smp_wmb();
2503 kvm->arch.hpte_setup_done = 1;
2504 err = 0;
2505 out_srcu:
2506 srcu_read_unlock(&kvm->srcu, srcu_idx);
2507 out:
2508 mutex_unlock(&kvm->lock);
2509 return err;
2511 up_out:
2512 up_read(&current->mm->mmap_sem);
2513 goto out_srcu;
2516 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2518 unsigned long lpcr, lpid;
2519 char buf[32];
2521 /* Allocate the guest's logical partition ID */
2523 lpid = kvmppc_alloc_lpid();
2524 if ((long)lpid < 0)
2525 return -ENOMEM;
2526 kvm->arch.lpid = lpid;
2529 * Since we don't flush the TLB when tearing down a VM,
2530 * and this lpid might have previously been used,
2531 * make sure we flush on each core before running the new VM.
2533 cpumask_setall(&kvm->arch.need_tlb_flush);
2535 /* Start out with the default set of hcalls enabled */
2536 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2537 sizeof(kvm->arch.enabled_hcalls));
2539 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2541 /* Init LPCR for virtual RMA mode */
2542 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2543 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2544 lpcr &= LPCR_PECE | LPCR_LPES;
2545 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2546 LPCR_VPM0 | LPCR_VPM1;
2547 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2548 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2549 /* On POWER8 turn on online bit to enable PURR/SPURR */
2550 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2551 lpcr |= LPCR_ONL;
2552 kvm->arch.lpcr = lpcr;
2555 * Track that we now have a HV mode VM active. This blocks secondary
2556 * CPU threads from coming online.
2558 kvm_hv_vm_activated();
2561 * Create a debugfs directory for the VM
2563 snprintf(buf, sizeof(buf), "vm%d", current->pid);
2564 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
2565 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
2566 kvmppc_mmu_debugfs_init(kvm);
2568 return 0;
2571 static void kvmppc_free_vcores(struct kvm *kvm)
2573 long int i;
2575 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2576 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2577 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2578 free_pages((unsigned long)vc->mpp_buffer,
2579 MPP_BUFFER_ORDER);
2581 kfree(kvm->arch.vcores[i]);
2583 kvm->arch.online_vcores = 0;
2586 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2588 debugfs_remove_recursive(kvm->arch.debugfs_dir);
2590 kvm_hv_vm_deactivated();
2592 kvmppc_free_vcores(kvm);
2594 kvmppc_free_hpt(kvm);
2597 /* We don't need to emulate any privileged instructions or dcbz */
2598 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2599 unsigned int inst, int *advance)
2601 return EMULATE_FAIL;
2604 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2605 ulong spr_val)
2607 return EMULATE_FAIL;
2610 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2611 ulong *spr_val)
2613 return EMULATE_FAIL;
2616 static int kvmppc_core_check_processor_compat_hv(void)
2618 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2619 !cpu_has_feature(CPU_FTR_ARCH_206))
2620 return -EIO;
2621 return 0;
2624 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2625 unsigned int ioctl, unsigned long arg)
2627 struct kvm *kvm __maybe_unused = filp->private_data;
2628 void __user *argp = (void __user *)arg;
2629 long r;
2631 switch (ioctl) {
2633 case KVM_PPC_ALLOCATE_HTAB: {
2634 u32 htab_order;
2636 r = -EFAULT;
2637 if (get_user(htab_order, (u32 __user *)argp))
2638 break;
2639 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2640 if (r)
2641 break;
2642 r = -EFAULT;
2643 if (put_user(htab_order, (u32 __user *)argp))
2644 break;
2645 r = 0;
2646 break;
2649 case KVM_PPC_GET_HTAB_FD: {
2650 struct kvm_get_htab_fd ghf;
2652 r = -EFAULT;
2653 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2654 break;
2655 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2656 break;
2659 default:
2660 r = -ENOTTY;
2663 return r;
2667 * List of hcall numbers to enable by default.
2668 * For compatibility with old userspace, we enable by default
2669 * all hcalls that were implemented before the hcall-enabling
2670 * facility was added. Note this list should not include H_RTAS.
2672 static unsigned int default_hcall_list[] = {
2673 H_REMOVE,
2674 H_ENTER,
2675 H_READ,
2676 H_PROTECT,
2677 H_BULK_REMOVE,
2678 H_GET_TCE,
2679 H_PUT_TCE,
2680 H_SET_DABR,
2681 H_SET_XDABR,
2682 H_CEDE,
2683 H_PROD,
2684 H_CONFER,
2685 H_REGISTER_VPA,
2686 #ifdef CONFIG_KVM_XICS
2687 H_EOI,
2688 H_CPPR,
2689 H_IPI,
2690 H_IPOLL,
2691 H_XIRR,
2692 H_XIRR_X,
2693 #endif
2697 static void init_default_hcalls(void)
2699 int i;
2700 unsigned int hcall;
2702 for (i = 0; default_hcall_list[i]; ++i) {
2703 hcall = default_hcall_list[i];
2704 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2705 __set_bit(hcall / 4, default_enabled_hcalls);
2709 static struct kvmppc_ops kvm_ops_hv = {
2710 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2711 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2712 .get_one_reg = kvmppc_get_one_reg_hv,
2713 .set_one_reg = kvmppc_set_one_reg_hv,
2714 .vcpu_load = kvmppc_core_vcpu_load_hv,
2715 .vcpu_put = kvmppc_core_vcpu_put_hv,
2716 .set_msr = kvmppc_set_msr_hv,
2717 .vcpu_run = kvmppc_vcpu_run_hv,
2718 .vcpu_create = kvmppc_core_vcpu_create_hv,
2719 .vcpu_free = kvmppc_core_vcpu_free_hv,
2720 .check_requests = kvmppc_core_check_requests_hv,
2721 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2722 .flush_memslot = kvmppc_core_flush_memslot_hv,
2723 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2724 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2725 .unmap_hva = kvm_unmap_hva_hv,
2726 .unmap_hva_range = kvm_unmap_hva_range_hv,
2727 .age_hva = kvm_age_hva_hv,
2728 .test_age_hva = kvm_test_age_hva_hv,
2729 .set_spte_hva = kvm_set_spte_hva_hv,
2730 .mmu_destroy = kvmppc_mmu_destroy_hv,
2731 .free_memslot = kvmppc_core_free_memslot_hv,
2732 .create_memslot = kvmppc_core_create_memslot_hv,
2733 .init_vm = kvmppc_core_init_vm_hv,
2734 .destroy_vm = kvmppc_core_destroy_vm_hv,
2735 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2736 .emulate_op = kvmppc_core_emulate_op_hv,
2737 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2738 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2739 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2740 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2741 .hcall_implemented = kvmppc_hcall_impl_hv,
2744 static int kvmppc_book3s_init_hv(void)
2746 int r;
2748 * FIXME!! Do we need to check on all cpus ?
2750 r = kvmppc_core_check_processor_compat_hv();
2751 if (r < 0)
2752 return -ENODEV;
2754 kvm_ops_hv.owner = THIS_MODULE;
2755 kvmppc_hv_ops = &kvm_ops_hv;
2757 init_default_hcalls();
2759 r = kvmppc_mmu_hv_init();
2760 return r;
2763 static void kvmppc_book3s_exit_hv(void)
2765 kvmppc_hv_ops = NULL;
2768 module_init(kvmppc_book3s_init_hv);
2769 module_exit(kvmppc_book3s_exit_hv);
2770 MODULE_LICENSE("GPL");
2771 MODULE_ALIAS_MISCDEV(KVM_MINOR);
2772 MODULE_ALIAS("devname:kvm");