~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/powerpc/kvm/book3s_hv.c

Version: ~ [ linux-5.11-rc3 ] ~ [ linux-5.10.7 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.89 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.167 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.215 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.251 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.251 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  3  * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
  4  *
  5  * Authors:
  6  *    Paul Mackerras <paulus@au1.ibm.com>
  7  *    Alexander Graf <agraf@suse.de>
  8  *    Kevin Wolf <mail@kevin-wolf.de>
  9  *
 10  * Description: KVM functions specific to running on Book 3S
 11  * processors in hypervisor mode (specifically POWER7 and later).
 12  *
 13  * This file is derived from arch/powerpc/kvm/book3s.c,
 14  * by Alexander Graf <agraf@suse.de>.
 15  *
 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.
 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 
 35 #include <asm/reg.h>
 36 #include <asm/cputable.h>
 37 #include <asm/cacheflush.h>
 38 #include <asm/tlbflush.h>
 39 #include <asm/uaccess.h>
 40 #include <asm/io.h>
 41 #include <asm/kvm_ppc.h>
 42 #include <asm/kvm_book3s.h>
 43 #include <asm/mmu_context.h>
 44 #include <asm/lppaca.h>
 45 #include <asm/processor.h>
 46 #include <asm/cputhreads.h>
 47 #include <asm/page.h>
 48 #include <asm/hvcall.h>
 49 #include <asm/switch_to.h>
 50 #include <asm/smp.h>
 51 #include <linux/gfp.h>
 52 #include <linux/vmalloc.h>
 53 #include <linux/highmem.h>
 54 #include <linux/hugetlb.h>
 55 #include <linux/module.h>
 56 
 57 #include "book3s.h"
 58 
 59 /* #define EXIT_DEBUG */
 60 /* #define EXIT_DEBUG_SIMPLE */
 61 /* #define EXIT_DEBUG_INT */
 62 
 63 /* Used to indicate that a guest page fault needs to be handled */
 64 #define RESUME_PAGE_FAULT       (RESUME_GUEST | RESUME_FLAG_ARCH1)
 65 
 66 /* Used as a "null" value for timebase values */
 67 #define TB_NIL  (~(u64)0)
 68 
 69 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
 70 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
 71 
 72 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
 73 {
 74         int me;
 75         int cpu = vcpu->cpu;
 76         wait_queue_head_t *wqp;
 77 
 78         wqp = kvm_arch_vcpu_wq(vcpu);
 79         if (waitqueue_active(wqp)) {
 80                 wake_up_interruptible(wqp);
 81                 ++vcpu->stat.halt_wakeup;
 82         }
 83 
 84         me = get_cpu();
 85 
 86         /* CPU points to the first thread of the core */
 87         if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
 88 #ifdef CONFIG_KVM_XICS
 89                 int real_cpu = cpu + vcpu->arch.ptid;
 90                 if (paca[real_cpu].kvm_hstate.xics_phys)
 91                         xics_wake_cpu(real_cpu);
 92                 else
 93 #endif
 94                 if (cpu_online(cpu))
 95                         smp_send_reschedule(cpu);
 96         }
 97         put_cpu();
 98 }
 99 
100 /*
101  * We use the vcpu_load/put functions to measure stolen time.
102  * Stolen time is counted as time when either the vcpu is able to
103  * run as part of a virtual core, but the task running the vcore
104  * is preempted or sleeping, or when the vcpu needs something done
105  * in the kernel by the task running the vcpu, but that task is
106  * preempted or sleeping.  Those two things have to be counted
107  * separately, since one of the vcpu tasks will take on the job
108  * of running the core, and the other vcpu tasks in the vcore will
109  * sleep waiting for it to do that, but that sleep shouldn't count
110  * as stolen time.
111  *
112  * Hence we accumulate stolen time when the vcpu can run as part of
113  * a vcore using vc->stolen_tb, and the stolen time when the vcpu
114  * needs its task to do other things in the kernel (for example,
115  * service a page fault) in busy_stolen.  We don't accumulate
116  * stolen time for a vcore when it is inactive, or for a vcpu
117  * when it is in state RUNNING or NOTREADY.  NOTREADY is a bit of
118  * a misnomer; it means that the vcpu task is not executing in
119  * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
120  * the kernel.  We don't have any way of dividing up that time
121  * between time that the vcpu is genuinely stopped, time that
122  * the task is actively working on behalf of the vcpu, and time
123  * that the task is preempted, so we don't count any of it as
124  * stolen.
125  *
126  * Updates to busy_stolen are protected by arch.tbacct_lock;
127  * updates to vc->stolen_tb are protected by the arch.tbacct_lock
128  * of the vcpu that has taken responsibility for running the vcore
129  * (i.e. vc->runner).  The stolen times are measured in units of
130  * timebase ticks.  (Note that the != TB_NIL checks below are
131  * purely defensive; they should never fail.)
132  */
133 
134 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
135 {
136         struct kvmppc_vcore *vc = vcpu->arch.vcore;
137         unsigned long flags;
138 
139         spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
140         if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
141             vc->preempt_tb != TB_NIL) {
142                 vc->stolen_tb += mftb() - vc->preempt_tb;
143                 vc->preempt_tb = TB_NIL;
144         }
145         if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
146             vcpu->arch.busy_preempt != TB_NIL) {
147                 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
148                 vcpu->arch.busy_preempt = TB_NIL;
149         }
150         spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
151 }
152 
153 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
154 {
155         struct kvmppc_vcore *vc = vcpu->arch.vcore;
156         unsigned long flags;
157 
158         spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
159         if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
160                 vc->preempt_tb = mftb();
161         if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
162                 vcpu->arch.busy_preempt = mftb();
163         spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
164 }
165 
166 static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
167 {
168         vcpu->arch.shregs.msr = msr;
169         kvmppc_end_cede(vcpu);
170 }
171 
172 void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
173 {
174         vcpu->arch.pvr = pvr;
175 }
176 
177 int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
178 {
179         unsigned long pcr = 0;
180         struct kvmppc_vcore *vc = vcpu->arch.vcore;
181 
182         if (arch_compat) {
183                 if (!cpu_has_feature(CPU_FTR_ARCH_206))
184                         return -EINVAL; /* 970 has no compat mode support */
185 
186                 switch (arch_compat) {
187                 case PVR_ARCH_205:
188                         pcr = PCR_ARCH_205;
189                         break;
190                 case PVR_ARCH_206:
191                 case PVR_ARCH_206p:
192                         break;
193                 default:
194                         return -EINVAL;
195                 }
196         }
197 
198         spin_lock(&vc->lock);
199         vc->arch_compat = arch_compat;
200         vc->pcr = pcr;
201         spin_unlock(&vc->lock);
202 
203         return 0;
204 }
205 
206 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
207 {
208         int r;
209 
210         pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
211         pr_err("pc  = %.16lx  msr = %.16llx  trap = %x\n",
212                vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
213         for (r = 0; r < 16; ++r)
214                 pr_err("r%2d = %.16lx  r%d = %.16lx\n",
215                        r, kvmppc_get_gpr(vcpu, r),
216                        r+16, kvmppc_get_gpr(vcpu, r+16));
217         pr_err("ctr = %.16lx  lr  = %.16lx\n",
218                vcpu->arch.ctr, vcpu->arch.lr);
219         pr_err("srr0 = %.16llx srr1 = %.16llx\n",
220                vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
221         pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
222                vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
223         pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
224                vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
225         pr_err("cr = %.8x  xer = %.16lx  dsisr = %.8x\n",
226                vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
227         pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
228         pr_err("fault dar = %.16lx dsisr = %.8x\n",
229                vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
230         pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
231         for (r = 0; r < vcpu->arch.slb_max; ++r)
232                 pr_err("  ESID = %.16llx VSID = %.16llx\n",
233                        vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
234         pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
235                vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
236                vcpu->arch.last_inst);
237 }
238 
239 struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
240 {
241         int r;
242         struct kvm_vcpu *v, *ret = NULL;
243 
244         mutex_lock(&kvm->lock);
245         kvm_for_each_vcpu(r, v, kvm) {
246                 if (v->vcpu_id == id) {
247                         ret = v;
248                         break;
249                 }
250         }
251         mutex_unlock(&kvm->lock);
252         return ret;
253 }
254 
255 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
256 {
257         vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
258         vpa->yield_count = 1;
259 }
260 
261 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
262                    unsigned long addr, unsigned long len)
263 {
264         /* check address is cacheline aligned */
265         if (addr & (L1_CACHE_BYTES - 1))
266                 return -EINVAL;
267         spin_lock(&vcpu->arch.vpa_update_lock);
268         if (v->next_gpa != addr || v->len != len) {
269                 v->next_gpa = addr;
270                 v->len = addr ? len : 0;
271                 v->update_pending = 1;
272         }
273         spin_unlock(&vcpu->arch.vpa_update_lock);
274         return 0;
275 }
276 
277 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
278 struct reg_vpa {
279         u32 dummy;
280         union {
281                 u16 hword;
282                 u32 word;
283         } length;
284 };
285 
286 static int vpa_is_registered(struct kvmppc_vpa *vpap)
287 {
288         if (vpap->update_pending)
289                 return vpap->next_gpa != 0;
290         return vpap->pinned_addr != NULL;
291 }
292 
293 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
294                                        unsigned long flags,
295                                        unsigned long vcpuid, unsigned long vpa)
296 {
297         struct kvm *kvm = vcpu->kvm;
298         unsigned long len, nb;
299         void *va;
300         struct kvm_vcpu *tvcpu;
301         int err;
302         int subfunc;
303         struct kvmppc_vpa *vpap;
304 
305         tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
306         if (!tvcpu)
307                 return H_PARAMETER;
308 
309         subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
310         if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
311             subfunc == H_VPA_REG_SLB) {
312                 /* Registering new area - address must be cache-line aligned */
313                 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
314                         return H_PARAMETER;
315 
316                 /* convert logical addr to kernel addr and read length */
317                 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
318                 if (va == NULL)
319                         return H_PARAMETER;
320                 if (subfunc == H_VPA_REG_VPA)
321                         len = ((struct reg_vpa *)va)->length.hword;
322                 else
323                         len = ((struct reg_vpa *)va)->length.word;
324                 kvmppc_unpin_guest_page(kvm, va, vpa, false);
325 
326                 /* Check length */
327                 if (len > nb || len < sizeof(struct reg_vpa))
328                         return H_PARAMETER;
329         } else {
330                 vpa = 0;
331                 len = 0;
332         }
333 
334         err = H_PARAMETER;
335         vpap = NULL;
336         spin_lock(&tvcpu->arch.vpa_update_lock);
337 
338         switch (subfunc) {
339         case H_VPA_REG_VPA:             /* register VPA */
340                 if (len < sizeof(struct lppaca))
341                         break;
342                 vpap = &tvcpu->arch.vpa;
343                 err = 0;
344                 break;
345 
346         case H_VPA_REG_DTL:             /* register DTL */
347                 if (len < sizeof(struct dtl_entry))
348                         break;
349                 len -= len % sizeof(struct dtl_entry);
350 
351                 /* Check that they have previously registered a VPA */
352                 err = H_RESOURCE;
353                 if (!vpa_is_registered(&tvcpu->arch.vpa))
354                         break;
355 
356                 vpap = &tvcpu->arch.dtl;
357                 err = 0;
358                 break;
359 
360         case H_VPA_REG_SLB:             /* register SLB shadow buffer */
361                 /* Check that they have previously registered a VPA */
362                 err = H_RESOURCE;
363                 if (!vpa_is_registered(&tvcpu->arch.vpa))
364                         break;
365 
366                 vpap = &tvcpu->arch.slb_shadow;
367                 err = 0;
368                 break;
369 
370         case H_VPA_DEREG_VPA:           /* deregister VPA */
371                 /* Check they don't still have a DTL or SLB buf registered */
372                 err = H_RESOURCE;
373                 if (vpa_is_registered(&tvcpu->arch.dtl) ||
374                     vpa_is_registered(&tvcpu->arch.slb_shadow))
375                         break;
376 
377                 vpap = &tvcpu->arch.vpa;
378                 err = 0;
379                 break;
380 
381         case H_VPA_DEREG_DTL:           /* deregister DTL */
382                 vpap = &tvcpu->arch.dtl;
383                 err = 0;
384                 break;
385 
386         case H_VPA_DEREG_SLB:           /* deregister SLB shadow buffer */
387                 vpap = &tvcpu->arch.slb_shadow;
388                 err = 0;
389                 break;
390         }
391 
392         if (vpap) {
393                 vpap->next_gpa = vpa;
394                 vpap->len = len;
395                 vpap->update_pending = 1;
396         }
397 
398         spin_unlock(&tvcpu->arch.vpa_update_lock);
399 
400         return err;
401 }
402 
403 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
404 {
405         struct kvm *kvm = vcpu->kvm;
406         void *va;
407         unsigned long nb;
408         unsigned long gpa;
409 
410         /*
411          * We need to pin the page pointed to by vpap->next_gpa,
412          * but we can't call kvmppc_pin_guest_page under the lock
413          * as it does get_user_pages() and down_read().  So we
414          * have to drop the lock, pin the page, then get the lock
415          * again and check that a new area didn't get registered
416          * in the meantime.
417          */
418         for (;;) {
419                 gpa = vpap->next_gpa;
420                 spin_unlock(&vcpu->arch.vpa_update_lock);
421                 va = NULL;
422                 nb = 0;
423                 if (gpa)
424                         va = kvmppc_pin_guest_page(kvm, gpa, &nb);
425                 spin_lock(&vcpu->arch.vpa_update_lock);
426                 if (gpa == vpap->next_gpa)
427                         break;
428                 /* sigh... unpin that one and try again */
429                 if (va)
430                         kvmppc_unpin_guest_page(kvm, va, gpa, false);
431         }
432 
433         vpap->update_pending = 0;
434         if (va && nb < vpap->len) {
435                 /*
436                  * If it's now too short, it must be that userspace
437                  * has changed the mappings underlying guest memory,
438                  * so unregister the region.
439                  */
440                 kvmppc_unpin_guest_page(kvm, va, gpa, false);
441                 va = NULL;
442         }
443         if (vpap->pinned_addr)
444                 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
445                                         vpap->dirty);
446         vpap->gpa = gpa;
447         vpap->pinned_addr = va;
448         vpap->dirty = false;
449         if (va)
450                 vpap->pinned_end = va + vpap->len;
451 }
452 
453 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
454 {
455         if (!(vcpu->arch.vpa.update_pending ||
456               vcpu->arch.slb_shadow.update_pending ||
457               vcpu->arch.dtl.update_pending))
458                 return;
459 
460         spin_lock(&vcpu->arch.vpa_update_lock);
461         if (vcpu->arch.vpa.update_pending) {
462                 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
463                 if (vcpu->arch.vpa.pinned_addr)
464                         init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
465         }
466         if (vcpu->arch.dtl.update_pending) {
467                 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
468                 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
469                 vcpu->arch.dtl_index = 0;
470         }
471         if (vcpu->arch.slb_shadow.update_pending)
472                 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
473         spin_unlock(&vcpu->arch.vpa_update_lock);
474 }
475 
476 /*
477  * Return the accumulated stolen time for the vcore up until `now'.
478  * The caller should hold the vcore lock.
479  */
480 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
481 {
482         u64 p;
483 
484         /*
485          * If we are the task running the vcore, then since we hold
486          * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
487          * can't be updated, so we don't need the tbacct_lock.
488          * If the vcore is inactive, it can't become active (since we
489          * hold the vcore lock), so the vcpu load/put functions won't
490          * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
491          */
492         if (vc->vcore_state != VCORE_INACTIVE &&
493             vc->runner->arch.run_task != current) {
494                 spin_lock_irq(&vc->runner->arch.tbacct_lock);
495                 p = vc->stolen_tb;
496                 if (vc->preempt_tb != TB_NIL)
497                         p += now - vc->preempt_tb;
498                 spin_unlock_irq(&vc->runner->arch.tbacct_lock);
499         } else {
500                 p = vc->stolen_tb;
501         }
502         return p;
503 }
504 
505 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
506                                     struct kvmppc_vcore *vc)
507 {
508         struct dtl_entry *dt;
509         struct lppaca *vpa;
510         unsigned long stolen;
511         unsigned long core_stolen;
512         u64 now;
513 
514         dt = vcpu->arch.dtl_ptr;
515         vpa = vcpu->arch.vpa.pinned_addr;
516         now = mftb();
517         core_stolen = vcore_stolen_time(vc, now);
518         stolen = core_stolen - vcpu->arch.stolen_logged;
519         vcpu->arch.stolen_logged = core_stolen;
520         spin_lock_irq(&vcpu->arch.tbacct_lock);
521         stolen += vcpu->arch.busy_stolen;
522         vcpu->arch.busy_stolen = 0;
523         spin_unlock_irq(&vcpu->arch.tbacct_lock);
524         if (!dt || !vpa)
525                 return;
526         memset(dt, 0, sizeof(struct dtl_entry));
527         dt->dispatch_reason = 7;
528         dt->processor_id = vc->pcpu + vcpu->arch.ptid;
529         dt->timebase = now + vc->tb_offset;
530         dt->enqueue_to_dispatch_time = stolen;
531         dt->srr0 = kvmppc_get_pc(vcpu);
532         dt->srr1 = vcpu->arch.shregs.msr;
533         ++dt;
534         if (dt == vcpu->arch.dtl.pinned_end)
535                 dt = vcpu->arch.dtl.pinned_addr;
536         vcpu->arch.dtl_ptr = dt;
537         /* order writing *dt vs. writing vpa->dtl_idx */
538         smp_wmb();
539         vpa->dtl_idx = ++vcpu->arch.dtl_index;
540         vcpu->arch.dtl.dirty = true;
541 }
542 
543 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
544 {
545         unsigned long req = kvmppc_get_gpr(vcpu, 3);
546         unsigned long target, ret = H_SUCCESS;
547         struct kvm_vcpu *tvcpu;
548         int idx, rc;
549 
550         switch (req) {
551         case H_ENTER:
552                 idx = srcu_read_lock(&vcpu->kvm->srcu);
553                 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
554                                               kvmppc_get_gpr(vcpu, 5),
555                                               kvmppc_get_gpr(vcpu, 6),
556                                               kvmppc_get_gpr(vcpu, 7));
557                 srcu_read_unlock(&vcpu->kvm->srcu, idx);
558                 break;
559         case H_CEDE:
560                 break;
561         case H_PROD:
562                 target = kvmppc_get_gpr(vcpu, 4);
563                 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
564                 if (!tvcpu) {
565                         ret = H_PARAMETER;
566                         break;
567                 }
568                 tvcpu->arch.prodded = 1;
569                 smp_mb();
570                 if (vcpu->arch.ceded) {
571                         if (waitqueue_active(&vcpu->wq)) {
572                                 wake_up_interruptible(&vcpu->wq);
573                                 vcpu->stat.halt_wakeup++;
574                         }
575                 }
576                 break;
577         case H_CONFER:
578                 target = kvmppc_get_gpr(vcpu, 4);
579                 if (target == -1)
580                         break;
581                 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
582                 if (!tvcpu) {
583                         ret = H_PARAMETER;
584                         break;
585                 }
586                 kvm_vcpu_yield_to(tvcpu);
587                 break;
588         case H_REGISTER_VPA:
589                 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
590                                         kvmppc_get_gpr(vcpu, 5),
591                                         kvmppc_get_gpr(vcpu, 6));
592                 break;
593         case H_RTAS:
594                 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
595                         return RESUME_HOST;
596 
597                 idx = srcu_read_lock(&vcpu->kvm->srcu);
598                 rc = kvmppc_rtas_hcall(vcpu);
599                 srcu_read_unlock(&vcpu->kvm->srcu, idx);
600 
601                 if (rc == -ENOENT)
602                         return RESUME_HOST;
603                 else if (rc == 0)
604                         break;
605 
606                 /* Send the error out to userspace via KVM_RUN */
607                 return rc;
608 
609         case H_XIRR:
610         case H_CPPR:
611         case H_EOI:
612         case H_IPI:
613         case H_IPOLL:
614         case H_XIRR_X:
615                 if (kvmppc_xics_enabled(vcpu)) {
616                         ret = kvmppc_xics_hcall(vcpu, req);
617                         break;
618                 } /* fallthrough */
619         default:
620                 return RESUME_HOST;
621         }
622         kvmppc_set_gpr(vcpu, 3, ret);
623         vcpu->arch.hcall_needed = 0;
624         return RESUME_GUEST;
625 }
626 
627 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
628                                  struct task_struct *tsk)
629 {
630         int r = RESUME_HOST;
631 
632         vcpu->stat.sum_exits++;
633 
634         run->exit_reason = KVM_EXIT_UNKNOWN;
635         run->ready_for_interrupt_injection = 1;
636         switch (vcpu->arch.trap) {
637         /* We're good on these - the host merely wanted to get our attention */
638         case BOOK3S_INTERRUPT_HV_DECREMENTER:
639                 vcpu->stat.dec_exits++;
640                 r = RESUME_GUEST;
641                 break;
642         case BOOK3S_INTERRUPT_EXTERNAL:
643                 vcpu->stat.ext_intr_exits++;
644                 r = RESUME_GUEST;
645                 break;
646         case BOOK3S_INTERRUPT_PERFMON:
647                 r = RESUME_GUEST;
648                 break;
649         case BOOK3S_INTERRUPT_MACHINE_CHECK:
650                 /*
651                  * Deliver a machine check interrupt to the guest.
652                  * We have to do this, even if the host has handled the
653                  * machine check, because machine checks use SRR0/1 and
654                  * the interrupt might have trashed guest state in them.
655                  */
656                 kvmppc_book3s_queue_irqprio(vcpu,
657                                             BOOK3S_INTERRUPT_MACHINE_CHECK);
658                 r = RESUME_GUEST;
659                 break;
660         case BOOK3S_INTERRUPT_PROGRAM:
661         {
662                 ulong flags;
663                 /*
664                  * Normally program interrupts are delivered directly
665                  * to the guest by the hardware, but we can get here
666                  * as a result of a hypervisor emulation interrupt
667                  * (e40) getting turned into a 700 by BML RTAS.
668                  */
669                 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
670                 kvmppc_core_queue_program(vcpu, flags);
671                 r = RESUME_GUEST;
672                 break;
673         }
674         case BOOK3S_INTERRUPT_SYSCALL:
675         {
676                 /* hcall - punt to userspace */
677                 int i;
678 
679                 if (vcpu->arch.shregs.msr & MSR_PR) {
680                         /* sc 1 from userspace - reflect to guest syscall */
681                         kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL);
682                         r = RESUME_GUEST;
683                         break;
684                 }
685                 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
686                 for (i = 0; i < 9; ++i)
687                         run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
688                 run->exit_reason = KVM_EXIT_PAPR_HCALL;
689                 vcpu->arch.hcall_needed = 1;
690                 r = RESUME_HOST;
691                 break;
692         }
693         /*
694          * We get these next two if the guest accesses a page which it thinks
695          * it has mapped but which is not actually present, either because
696          * it is for an emulated I/O device or because the corresonding
697          * host page has been paged out.  Any other HDSI/HISI interrupts
698          * have been handled already.
699          */
700         case BOOK3S_INTERRUPT_H_DATA_STORAGE:
701                 r = RESUME_PAGE_FAULT;
702                 break;
703         case BOOK3S_INTERRUPT_H_INST_STORAGE:
704                 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
705                 vcpu->arch.fault_dsisr = 0;
706                 r = RESUME_PAGE_FAULT;
707                 break;
708         /*
709          * This occurs if the guest executes an illegal instruction.
710          * We just generate a program interrupt to the guest, since
711          * we don't emulate any guest instructions at this stage.
712          */
713         case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
714                 kvmppc_core_queue_program(vcpu, 0x80000);
715                 r = RESUME_GUEST;
716                 break;
717         default:
718                 kvmppc_dump_regs(vcpu);
719                 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
720                         vcpu->arch.trap, kvmppc_get_pc(vcpu),
721                         vcpu->arch.shregs.msr);
722                 run->hw.hardware_exit_reason = vcpu->arch.trap;
723                 r = RESUME_HOST;
724                 break;
725         }
726 
727         return r;
728 }
729 
730 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
731                                             struct kvm_sregs *sregs)
732 {
733         int i;
734 
735         memset(sregs, 0, sizeof(struct kvm_sregs));
736         sregs->pvr = vcpu->arch.pvr;
737         for (i = 0; i < vcpu->arch.slb_max; i++) {
738                 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
739                 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
740         }
741 
742         return 0;
743 }
744 
745 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
746                                             struct kvm_sregs *sregs)
747 {
748         int i, j;
749 
750         kvmppc_set_pvr_hv(vcpu, sregs->pvr);
751 
752         j = 0;
753         for (i = 0; i < vcpu->arch.slb_nr; i++) {
754                 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
755                         vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
756                         vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
757                         ++j;
758                 }
759         }
760         vcpu->arch.slb_max = j;
761 
762         return 0;
763 }
764 
765 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
766 {
767         struct kvmppc_vcore *vc = vcpu->arch.vcore;
768         u64 mask;
769 
770         spin_lock(&vc->lock);
771         /*
772          * Userspace can only modify DPFD (default prefetch depth),
773          * ILE (interrupt little-endian) and TC (translation control).
774          */
775         mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
776         vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
777         spin_unlock(&vc->lock);
778 }
779 
780 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
781                                  union kvmppc_one_reg *val)
782 {
783         int r = 0;
784         long int i;
785 
786         switch (id) {
787         case KVM_REG_PPC_HIOR:
788                 *val = get_reg_val(id, 0);
789                 break;
790         case KVM_REG_PPC_DABR:
791                 *val = get_reg_val(id, vcpu->arch.dabr);
792                 break;
793         case KVM_REG_PPC_DSCR:
794                 *val = get_reg_val(id, vcpu->arch.dscr);
795                 break;
796         case KVM_REG_PPC_PURR:
797                 *val = get_reg_val(id, vcpu->arch.purr);
798                 break;
799         case KVM_REG_PPC_SPURR:
800                 *val = get_reg_val(id, vcpu->arch.spurr);
801                 break;
802         case KVM_REG_PPC_AMR:
803                 *val = get_reg_val(id, vcpu->arch.amr);
804                 break;
805         case KVM_REG_PPC_UAMOR:
806                 *val = get_reg_val(id, vcpu->arch.uamor);
807                 break;
808         case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
809                 i = id - KVM_REG_PPC_MMCR0;
810                 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
811                 break;
812         case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
813                 i = id - KVM_REG_PPC_PMC1;
814                 *val = get_reg_val(id, vcpu->arch.pmc[i]);
815                 break;
816         case KVM_REG_PPC_SIAR:
817                 *val = get_reg_val(id, vcpu->arch.siar);
818                 break;
819         case KVM_REG_PPC_SDAR:
820                 *val = get_reg_val(id, vcpu->arch.sdar);
821                 break;
822 #ifdef CONFIG_VSX
823         case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
824                 if (cpu_has_feature(CPU_FTR_VSX)) {
825                         /* VSX => FP reg i is stored in arch.vsr[2*i] */
826                         long int i = id - KVM_REG_PPC_FPR0;
827                         *val = get_reg_val(id, vcpu->arch.vsr[2 * i]);
828                 } else {
829                         /* let generic code handle it */
830                         r = -EINVAL;
831                 }
832                 break;
833         case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
834                 if (cpu_has_feature(CPU_FTR_VSX)) {
835                         long int i = id - KVM_REG_PPC_VSR0;
836                         val->vsxval[0] = vcpu->arch.vsr[2 * i];
837                         val->vsxval[1] = vcpu->arch.vsr[2 * i + 1];
838                 } else {
839                         r = -ENXIO;
840                 }
841                 break;
842 #endif /* CONFIG_VSX */
843         case KVM_REG_PPC_VPA_ADDR:
844                 spin_lock(&vcpu->arch.vpa_update_lock);
845                 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
846                 spin_unlock(&vcpu->arch.vpa_update_lock);
847                 break;
848         case KVM_REG_PPC_VPA_SLB:
849                 spin_lock(&vcpu->arch.vpa_update_lock);
850                 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
851                 val->vpaval.length = vcpu->arch.slb_shadow.len;
852                 spin_unlock(&vcpu->arch.vpa_update_lock);
853                 break;
854         case KVM_REG_PPC_VPA_DTL:
855                 spin_lock(&vcpu->arch.vpa_update_lock);
856                 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
857                 val->vpaval.length = vcpu->arch.dtl.len;
858                 spin_unlock(&vcpu->arch.vpa_update_lock);
859                 break;
860         case KVM_REG_PPC_TB_OFFSET:
861                 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
862                 break;
863         case KVM_REG_PPC_LPCR:
864                 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
865                 break;
866         case KVM_REG_PPC_PPR:
867                 *val = get_reg_val(id, vcpu->arch.ppr);
868                 break;
869         case KVM_REG_PPC_ARCH_COMPAT:
870                 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
871                 break;
872         default:
873                 r = -EINVAL;
874                 break;
875         }
876 
877         return r;
878 }
879 
880 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
881                                  union kvmppc_one_reg *val)
882 {
883         int r = 0;
884         long int i;
885         unsigned long addr, len;
886 
887         switch (id) {
888         case KVM_REG_PPC_HIOR:
889                 /* Only allow this to be set to zero */
890                 if (set_reg_val(id, *val))
891                         r = -EINVAL;
892                 break;
893         case KVM_REG_PPC_DABR:
894                 vcpu->arch.dabr = set_reg_val(id, *val);
895                 break;
896         case KVM_REG_PPC_DSCR:
897                 vcpu->arch.dscr = set_reg_val(id, *val);
898                 break;
899         case KVM_REG_PPC_PURR:
900                 vcpu->arch.purr = set_reg_val(id, *val);
901                 break;
902         case KVM_REG_PPC_SPURR:
903                 vcpu->arch.spurr = set_reg_val(id, *val);
904                 break;
905         case KVM_REG_PPC_AMR:
906                 vcpu->arch.amr = set_reg_val(id, *val);
907                 break;
908         case KVM_REG_PPC_UAMOR:
909                 vcpu->arch.uamor = set_reg_val(id, *val);
910                 break;
911         case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA:
912                 i = id - KVM_REG_PPC_MMCR0;
913                 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
914                 break;
915         case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
916                 i = id - KVM_REG_PPC_PMC1;
917                 vcpu->arch.pmc[i] = set_reg_val(id, *val);
918                 break;
919         case KVM_REG_PPC_SIAR:
920                 vcpu->arch.siar = set_reg_val(id, *val);
921                 break;
922         case KVM_REG_PPC_SDAR:
923                 vcpu->arch.sdar = set_reg_val(id, *val);
924                 break;
925 #ifdef CONFIG_VSX
926         case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31:
927                 if (cpu_has_feature(CPU_FTR_VSX)) {
928                         /* VSX => FP reg i is stored in arch.vsr[2*i] */
929                         long int i = id - KVM_REG_PPC_FPR0;
930                         vcpu->arch.vsr[2 * i] = set_reg_val(id, *val);
931                 } else {
932                         /* let generic code handle it */
933                         r = -EINVAL;
934                 }
935                 break;
936         case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31:
937                 if (cpu_has_feature(CPU_FTR_VSX)) {
938                         long int i = id - KVM_REG_PPC_VSR0;
939                         vcpu->arch.vsr[2 * i] = val->vsxval[0];
940                         vcpu->arch.vsr[2 * i + 1] = val->vsxval[1];
941                 } else {
942                         r = -ENXIO;
943                 }
944                 break;
945 #endif /* CONFIG_VSX */
946         case KVM_REG_PPC_VPA_ADDR:
947                 addr = set_reg_val(id, *val);
948                 r = -EINVAL;
949                 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
950                               vcpu->arch.dtl.next_gpa))
951                         break;
952                 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
953                 break;
954         case KVM_REG_PPC_VPA_SLB:
955                 addr = val->vpaval.addr;
956                 len = val->vpaval.length;
957                 r = -EINVAL;
958                 if (addr && !vcpu->arch.vpa.next_gpa)
959                         break;
960                 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
961                 break;
962         case KVM_REG_PPC_VPA_DTL:
963                 addr = val->vpaval.addr;
964                 len = val->vpaval.length;
965                 r = -EINVAL;
966                 if (addr && (len < sizeof(struct dtl_entry) ||
967                              !vcpu->arch.vpa.next_gpa))
968                         break;
969                 len -= len % sizeof(struct dtl_entry);
970                 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
971                 break;
972         case KVM_REG_PPC_TB_OFFSET:
973                 /* round up to multiple of 2^24 */
974                 vcpu->arch.vcore->tb_offset =
975                         ALIGN(set_reg_val(id, *val), 1UL << 24);
976                 break;
977         case KVM_REG_PPC_LPCR:
978                 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
979                 break;
980         case KVM_REG_PPC_PPR:
981                 vcpu->arch.ppr = set_reg_val(id, *val);
982                 break;
983         case KVM_REG_PPC_ARCH_COMPAT:
984                 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
985                 break;
986         default:
987                 r = -EINVAL;
988                 break;
989         }
990 
991         return r;
992 }
993 
994 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
995                                                    unsigned int id)
996 {
997         struct kvm_vcpu *vcpu;
998         int err = -EINVAL;
999         int core;
1000         struct kvmppc_vcore *vcore;
1001 
1002         core = id / threads_per_core;
1003         if (core >= KVM_MAX_VCORES)
1004                 goto out;
1005 
1006         err = -ENOMEM;
1007         vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1008         if (!vcpu)
1009                 goto out;
1010 
1011         err = kvm_vcpu_init(vcpu, kvm, id);
1012         if (err)
1013                 goto free_vcpu;
1014 
1015         vcpu->arch.shared = &vcpu->arch.shregs;
1016         vcpu->arch.mmcr[0] = MMCR0_FC;
1017         vcpu->arch.ctrl = CTRL_RUNLATCH;
1018         /* default to host PVR, since we can't spoof it */
1019         kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1020         spin_lock_init(&vcpu->arch.vpa_update_lock);
1021         spin_lock_init(&vcpu->arch.tbacct_lock);
1022         vcpu->arch.busy_preempt = TB_NIL;
1023 
1024         kvmppc_mmu_book3s_hv_init(vcpu);
1025 
1026         vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1027 
1028         init_waitqueue_head(&vcpu->arch.cpu_run);
1029 
1030         mutex_lock(&kvm->lock);
1031         vcore = kvm->arch.vcores[core];
1032         if (!vcore) {
1033                 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1034                 if (vcore) {
1035                         INIT_LIST_HEAD(&vcore->runnable_threads);
1036                         spin_lock_init(&vcore->lock);
1037                         init_waitqueue_head(&vcore->wq);
1038                         vcore->preempt_tb = TB_NIL;
1039                         vcore->lpcr = kvm->arch.lpcr;
1040                 }
1041                 kvm->arch.vcores[core] = vcore;
1042                 kvm->arch.online_vcores++;
1043         }
1044         mutex_unlock(&kvm->lock);
1045 
1046         if (!vcore)
1047                 goto free_vcpu;
1048 
1049         spin_lock(&vcore->lock);
1050         ++vcore->num_threads;
1051         spin_unlock(&vcore->lock);
1052         vcpu->arch.vcore = vcore;
1053 
1054         vcpu->arch.cpu_type = KVM_CPU_3S_64;
1055         kvmppc_sanity_check(vcpu);
1056 
1057         return vcpu;
1058 
1059 free_vcpu:
1060         kmem_cache_free(kvm_vcpu_cache, vcpu);
1061 out:
1062         return ERR_PTR(err);
1063 }
1064 
1065 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1066 {
1067         if (vpa->pinned_addr)
1068                 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1069                                         vpa->dirty);
1070 }
1071 
1072 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1073 {
1074         spin_lock(&vcpu->arch.vpa_update_lock);
1075         unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1076         unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1077         unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1078         spin_unlock(&vcpu->arch.vpa_update_lock);
1079         kvm_vcpu_uninit(vcpu);
1080         kmem_cache_free(kvm_vcpu_cache, vcpu);
1081 }
1082 
1083 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1084 {
1085         /* Indicate we want to get back into the guest */
1086         return 1;
1087 }
1088 
1089 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1090 {
1091         unsigned long dec_nsec, now;
1092 
1093         now = get_tb();
1094         if (now > vcpu->arch.dec_expires) {
1095                 /* decrementer has already gone negative */
1096                 kvmppc_core_queue_dec(vcpu);
1097                 kvmppc_core_prepare_to_enter(vcpu);
1098                 return;
1099         }
1100         dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1101                    / tb_ticks_per_sec;
1102         hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1103                       HRTIMER_MODE_REL);
1104         vcpu->arch.timer_running = 1;
1105 }
1106 
1107 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1108 {
1109         vcpu->arch.ceded = 0;
1110         if (vcpu->arch.timer_running) {
1111                 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1112                 vcpu->arch.timer_running = 0;
1113         }
1114 }
1115 
1116 extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
1117 
1118 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1119                                    struct kvm_vcpu *vcpu)
1120 {
1121         u64 now;
1122 
1123         if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1124                 return;
1125         spin_lock_irq(&vcpu->arch.tbacct_lock);
1126         now = mftb();
1127         vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1128                 vcpu->arch.stolen_logged;
1129         vcpu->arch.busy_preempt = now;
1130         vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1131         spin_unlock_irq(&vcpu->arch.tbacct_lock);
1132         --vc->n_runnable;
1133         list_del(&vcpu->arch.run_list);
1134 }
1135 
1136 static int kvmppc_grab_hwthread(int cpu)
1137 {
1138         struct paca_struct *tpaca;
1139         long timeout = 1000;
1140 
1141         tpaca = &paca[cpu];
1142 
1143         /* Ensure the thread won't go into the kernel if it wakes */
1144         tpaca->kvm_hstate.hwthread_req = 1;
1145         tpaca->kvm_hstate.kvm_vcpu = NULL;
1146 
1147         /*
1148          * If the thread is already executing in the kernel (e.g. handling
1149          * a stray interrupt), wait for it to get back to nap mode.
1150          * The smp_mb() is to ensure that our setting of hwthread_req
1151          * is visible before we look at hwthread_state, so if this
1152          * races with the code at system_reset_pSeries and the thread
1153          * misses our setting of hwthread_req, we are sure to see its
1154          * setting of hwthread_state, and vice versa.
1155          */
1156         smp_mb();
1157         while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1158                 if (--timeout <= 0) {
1159                         pr_err("KVM: couldn't grab cpu %d\n", cpu);
1160                         return -EBUSY;
1161                 }
1162                 udelay(1);
1163         }
1164         return 0;
1165 }
1166 
1167 static void kvmppc_release_hwthread(int cpu)
1168 {
1169         struct paca_struct *tpaca;
1170 
1171         tpaca = &paca[cpu];
1172         tpaca->kvm_hstate.hwthread_req = 0;
1173         tpaca->kvm_hstate.kvm_vcpu = NULL;
1174 }
1175 
1176 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1177 {
1178         int cpu;
1179         struct paca_struct *tpaca;
1180         struct kvmppc_vcore *vc = vcpu->arch.vcore;
1181 
1182         if (vcpu->arch.timer_running) {
1183                 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1184                 vcpu->arch.timer_running = 0;
1185         }
1186         cpu = vc->pcpu + vcpu->arch.ptid;
1187         tpaca = &paca[cpu];
1188         tpaca->kvm_hstate.kvm_vcpu = vcpu;
1189         tpaca->kvm_hstate.kvm_vcore = vc;
1190         tpaca->kvm_hstate.napping = 0;
1191         vcpu->cpu = vc->pcpu;
1192         smp_wmb();
1193 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1194         if (vcpu->arch.ptid) {
1195 #ifdef CONFIG_KVM_XICS
1196                 xics_wake_cpu(cpu);
1197 #endif
1198                 ++vc->n_woken;
1199         }
1200 #endif
1201 }
1202 
1203 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1204 {
1205         int i;
1206 
1207         HMT_low();
1208         i = 0;
1209         while (vc->nap_count < vc->n_woken) {
1210                 if (++i >= 1000000) {
1211                         pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1212                                vc->nap_count, vc->n_woken);
1213                         break;
1214                 }
1215                 cpu_relax();
1216         }
1217         HMT_medium();
1218 }
1219 
1220 /*
1221  * Check that we are on thread 0 and that any other threads in
1222  * this core are off-line.  Then grab the threads so they can't
1223  * enter the kernel.
1224  */
1225 static int on_primary_thread(void)
1226 {
1227         int cpu = smp_processor_id();
1228         int thr = cpu_thread_in_core(cpu);
1229 
1230         if (thr)
1231                 return 0;
1232         while (++thr < threads_per_core)
1233                 if (cpu_online(cpu + thr))
1234                         return 0;
1235 
1236         /* Grab all hw threads so they can't go into the kernel */
1237         for (thr = 1; thr < threads_per_core; ++thr) {
1238                 if (kvmppc_grab_hwthread(cpu + thr)) {
1239                         /* Couldn't grab one; let the others go */
1240                         do {
1241                                 kvmppc_release_hwthread(cpu + thr);
1242                         } while (--thr > 0);
1243                         return 0;
1244                 }
1245         }
1246         return 1;
1247 }
1248 
1249 /*
1250  * Run a set of guest threads on a physical core.
1251  * Called with vc->lock held.
1252  */
1253 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1254 {
1255         struct kvm_vcpu *vcpu, *vcpu0, *vnext;
1256         long ret;
1257         u64 now;
1258         int ptid, i, need_vpa_update;
1259         int srcu_idx;
1260         struct kvm_vcpu *vcpus_to_update[threads_per_core];
1261 
1262         /* don't start if any threads have a signal pending */
1263         need_vpa_update = 0;
1264         list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1265                 if (signal_pending(vcpu->arch.run_task))
1266                         return;
1267                 if (vcpu->arch.vpa.update_pending ||
1268                     vcpu->arch.slb_shadow.update_pending ||
1269                     vcpu->arch.dtl.update_pending)
1270                         vcpus_to_update[need_vpa_update++] = vcpu;
1271         }
1272 
1273         /*
1274          * Initialize *vc, in particular vc->vcore_state, so we can
1275          * drop the vcore lock if necessary.
1276          */
1277         vc->n_woken = 0;
1278         vc->nap_count = 0;
1279         vc->entry_exit_count = 0;
1280         vc->vcore_state = VCORE_STARTING;
1281         vc->in_guest = 0;
1282         vc->napping_threads = 0;
1283 
1284         /*
1285          * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1286          * which can't be called with any spinlocks held.
1287          */
1288         if (need_vpa_update) {
1289                 spin_unlock(&vc->lock);
1290                 for (i = 0; i < need_vpa_update; ++i)
1291                         kvmppc_update_vpas(vcpus_to_update[i]);
1292                 spin_lock(&vc->lock);
1293         }
1294 
1295         /*
1296          * Assign physical thread IDs, first to non-ceded vcpus
1297          * and then to ceded ones.
1298          */
1299         ptid = 0;
1300         vcpu0 = NULL;
1301         list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1302                 if (!vcpu->arch.ceded) {
1303                         if (!ptid)
1304                                 vcpu0 = vcpu;
1305                         vcpu->arch.ptid = ptid++;
1306                 }
1307         }
1308         if (!vcpu0)
1309                 goto out;       /* nothing to run; should never happen */
1310         list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1311                 if (vcpu->arch.ceded)
1312                         vcpu->arch.ptid = ptid++;
1313 
1314         /*
1315          * Make sure we are running on thread 0, and that
1316          * secondary threads are offline.
1317          */
1318         if (threads_per_core > 1 && !on_primary_thread()) {
1319                 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1320                         vcpu->arch.ret = -EBUSY;
1321                 goto out;
1322         }
1323 
1324         vc->pcpu = smp_processor_id();
1325         list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1326                 kvmppc_start_thread(vcpu);
1327                 kvmppc_create_dtl_entry(vcpu, vc);
1328         }
1329 
1330         vc->vcore_state = VCORE_RUNNING;
1331         preempt_disable();
1332         spin_unlock(&vc->lock);
1333 
1334         kvm_guest_enter();
1335 
1336         srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu);
1337 
1338         __kvmppc_vcore_entry(NULL, vcpu0);
1339 
1340         spin_lock(&vc->lock);
1341         /* disable sending of IPIs on virtual external irqs */
1342         list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1343                 vcpu->cpu = -1;
1344         /* wait for secondary threads to finish writing their state to memory */
1345         if (vc->nap_count < vc->n_woken)
1346                 kvmppc_wait_for_nap(vc);
1347         for (i = 0; i < threads_per_core; ++i)
1348                 kvmppc_release_hwthread(vc->pcpu + i);
1349         /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1350         vc->vcore_state = VCORE_EXITING;
1351         spin_unlock(&vc->lock);
1352 
1353         srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx);
1354 
1355         /* make sure updates to secondary vcpu structs are visible now */
1356         smp_mb();
1357         kvm_guest_exit();
1358 
1359         preempt_enable();
1360         kvm_resched(vcpu);
1361 
1362         spin_lock(&vc->lock);
1363         now = get_tb();
1364         list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1365                 /* cancel pending dec exception if dec is positive */
1366                 if (now < vcpu->arch.dec_expires &&
1367                     kvmppc_core_pending_dec(vcpu))
1368                         kvmppc_core_dequeue_dec(vcpu);
1369 
1370                 ret = RESUME_GUEST;
1371                 if (vcpu->arch.trap)
1372                         ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1373                                                     vcpu->arch.run_task);
1374 
1375                 vcpu->arch.ret = ret;
1376                 vcpu->arch.trap = 0;
1377 
1378                 if (vcpu->arch.ceded) {
1379                         if (ret != RESUME_GUEST)
1380                                 kvmppc_end_cede(vcpu);
1381                         else
1382                                 kvmppc_set_timer(vcpu);
1383                 }
1384         }
1385 
1386  out:
1387         vc->vcore_state = VCORE_INACTIVE;
1388         list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1389                                  arch.run_list) {
1390                 if (vcpu->arch.ret != RESUME_GUEST) {
1391                         kvmppc_remove_runnable(vc, vcpu);
1392                         wake_up(&vcpu->arch.cpu_run);
1393                 }
1394         }
1395 }
1396 
1397 /*
1398  * Wait for some other vcpu thread to execute us, and
1399  * wake us up when we need to handle something in the host.
1400  */
1401 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1402 {
1403         DEFINE_WAIT(wait);
1404 
1405         prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1406         if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1407                 schedule();
1408         finish_wait(&vcpu->arch.cpu_run, &wait);
1409 }
1410 
1411 /*
1412  * All the vcpus in this vcore are idle, so wait for a decrementer
1413  * or external interrupt to one of the vcpus.  vc->lock is held.
1414  */
1415 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1416 {
1417         DEFINE_WAIT(wait);
1418 
1419         prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1420         vc->vcore_state = VCORE_SLEEPING;
1421         spin_unlock(&vc->lock);
1422         schedule();
1423         finish_wait(&vc->wq, &wait);
1424         spin_lock(&vc->lock);
1425         vc->vcore_state = VCORE_INACTIVE;
1426 }
1427 
1428 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1429 {
1430         int n_ceded;
1431         struct kvmppc_vcore *vc;
1432         struct kvm_vcpu *v, *vn;
1433 
1434         kvm_run->exit_reason = 0;
1435         vcpu->arch.ret = RESUME_GUEST;
1436         vcpu->arch.trap = 0;
1437         kvmppc_update_vpas(vcpu);
1438 
1439         /*
1440          * Synchronize with other threads in this virtual core
1441          */
1442         vc = vcpu->arch.vcore;
1443         spin_lock(&vc->lock);
1444         vcpu->arch.ceded = 0;
1445         vcpu->arch.run_task = current;
1446         vcpu->arch.kvm_run = kvm_run;
1447         vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1448         vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1449         vcpu->arch.busy_preempt = TB_NIL;
1450         list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1451         ++vc->n_runnable;
1452 
1453         /*
1454          * This happens the first time this is called for a vcpu.
1455          * If the vcore is already running, we may be able to start
1456          * this thread straight away and have it join in.
1457          */
1458         if (!signal_pending(current)) {
1459                 if (vc->vcore_state == VCORE_RUNNING &&
1460                     VCORE_EXIT_COUNT(vc) == 0) {
1461                         vcpu->arch.ptid = vc->n_runnable - 1;
1462                         kvmppc_create_dtl_entry(vcpu, vc);
1463                         kvmppc_start_thread(vcpu);
1464                 } else if (vc->vcore_state == VCORE_SLEEPING) {
1465                         wake_up(&vc->wq);
1466                 }
1467 
1468         }
1469 
1470         while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1471                !signal_pending(current)) {
1472                 if (vc->vcore_state != VCORE_INACTIVE) {
1473                         spin_unlock(&vc->lock);
1474                         kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1475                         spin_lock(&vc->lock);
1476                         continue;
1477                 }
1478                 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1479                                          arch.run_list) {
1480                         kvmppc_core_prepare_to_enter(v);
1481                         if (signal_pending(v->arch.run_task)) {
1482                                 kvmppc_remove_runnable(vc, v);
1483                                 v->stat.signal_exits++;
1484                                 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1485                                 v->arch.ret = -EINTR;
1486                                 wake_up(&v->arch.cpu_run);
1487                         }
1488                 }
1489                 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1490                         break;
1491                 vc->runner = vcpu;
1492                 n_ceded = 0;
1493                 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1494                         if (!v->arch.pending_exceptions)
1495                                 n_ceded += v->arch.ceded;
1496                         else
1497                                 v->arch.ceded = 0;
1498                 }
1499                 if (n_ceded == vc->n_runnable)
1500                         kvmppc_vcore_blocked(vc);
1501                 else
1502                         kvmppc_run_core(vc);
1503                 vc->runner = NULL;
1504         }
1505 
1506         while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1507                (vc->vcore_state == VCORE_RUNNING ||
1508                 vc->vcore_state == VCORE_EXITING)) {
1509                 spin_unlock(&vc->lock);
1510                 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1511                 spin_lock(&vc->lock);
1512         }
1513 
1514         if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1515                 kvmppc_remove_runnable(vc, vcpu);
1516                 vcpu->stat.signal_exits++;
1517                 kvm_run->exit_reason = KVM_EXIT_INTR;
1518                 vcpu->arch.ret = -EINTR;
1519         }
1520 
1521         if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1522                 /* Wake up some vcpu to run the core */
1523                 v = list_first_entry(&vc->runnable_threads,
1524                                      struct kvm_vcpu, arch.run_list);
1525                 wake_up(&v->arch.cpu_run);
1526         }
1527 
1528         spin_unlock(&vc->lock);
1529         return vcpu->arch.ret;
1530 }
1531 
1532 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
1533 {
1534         int r;
1535         int srcu_idx;
1536 
1537         if (!vcpu->arch.sane) {
1538                 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1539                 return -EINVAL;
1540         }
1541 
1542         kvmppc_core_prepare_to_enter(vcpu);
1543 
1544         /* No need to go into the guest when all we'll do is come back out */
1545         if (signal_pending(current)) {
1546                 run->exit_reason = KVM_EXIT_INTR;
1547                 return -EINTR;
1548         }
1549 
1550         atomic_inc(&vcpu->kvm->arch.vcpus_running);
1551         /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1552         smp_mb();
1553 
1554         /* On the first time here, set up HTAB and VRMA or RMA */
1555         if (!vcpu->kvm->arch.rma_setup_done) {
1556                 r = kvmppc_hv_setup_htab_rma(vcpu);
1557                 if (r)
1558                         goto out;
1559         }
1560 
1561         flush_fp_to_thread(current);
1562         flush_altivec_to_thread(current);
1563         flush_vsx_to_thread(current);
1564         vcpu->arch.wqp = &vcpu->arch.vcore->wq;
1565         vcpu->arch.pgdir = current->mm->pgd;
1566         vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1567 
1568         do {
1569                 r = kvmppc_run_vcpu(run, vcpu);
1570 
1571                 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1572                     !(vcpu->arch.shregs.msr & MSR_PR)) {
1573                         r = kvmppc_pseries_do_hcall(vcpu);
1574                         kvmppc_core_prepare_to_enter(vcpu);
1575                 } else if (r == RESUME_PAGE_FAULT) {
1576                         srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1577                         r = kvmppc_book3s_hv_page_fault(run, vcpu,
1578                                 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1579                         srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1580                 }
1581         } while (r == RESUME_GUEST);
1582 
1583  out:
1584         vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1585         atomic_dec(&vcpu->kvm->arch.vcpus_running);
1586         return r;
1587 }
1588 
1589 
1590 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
1591    Assumes POWER7 or PPC970. */
1592 static inline int lpcr_rmls(unsigned long rma_size)
1593 {
1594         switch (rma_size) {
1595         case 32ul << 20:        /* 32 MB */
1596                 if (cpu_has_feature(CPU_FTR_ARCH_206))
1597                         return 8;       /* only supported on POWER7 */
1598                 return -1;
1599         case 64ul << 20:        /* 64 MB */
1600                 return 3;
1601         case 128ul << 20:       /* 128 MB */
1602                 return 7;
1603         case 256ul << 20:       /* 256 MB */
1604                 return 4;
1605         case 1ul << 30:         /* 1 GB */
1606                 return 2;
1607         case 16ul << 30:        /* 16 GB */
1608                 return 1;
1609         case 256ul << 30:       /* 256 GB */
1610                 return 0;
1611         default:
1612                 return -1;
1613         }
1614 }
1615 
1616 static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1617 {
1618         struct page *page;
1619         struct kvm_rma_info *ri = vma->vm_file->private_data;
1620 
1621         if (vmf->pgoff >= kvm_rma_pages)
1622                 return VM_FAULT_SIGBUS;
1623 
1624         page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1625         get_page(page);
1626         vmf->page = page;
1627         return 0;
1628 }
1629 
1630 static const struct vm_operations_struct kvm_rma_vm_ops = {
1631         .fault = kvm_rma_fault,
1632 };
1633 
1634 static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1635 {
1636         vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1637         vma->vm_ops = &kvm_rma_vm_ops;
1638         return 0;
1639 }
1640 
1641 static int kvm_rma_release(struct inode *inode, struct file *filp)
1642 {
1643         struct kvm_rma_info *ri = filp->private_data;
1644 
1645         kvm_release_rma(ri);
1646         return 0;
1647 }
1648 
1649 static const struct file_operations kvm_rma_fops = {
1650         .mmap           = kvm_rma_mmap,
1651         .release        = kvm_rma_release,
1652 };
1653 
1654 static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1655                                       struct kvm_allocate_rma *ret)
1656 {
1657         long fd;
1658         struct kvm_rma_info *ri;
1659         /*
1660          * Only do this on PPC970 in HV mode
1661          */
1662         if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1663             !cpu_has_feature(CPU_FTR_ARCH_201))
1664                 return -EINVAL;
1665 
1666         if (!kvm_rma_pages)
1667                 return -EINVAL;
1668 
1669         ri = kvm_alloc_rma();
1670         if (!ri)
1671                 return -ENOMEM;
1672 
1673         fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
1674         if (fd < 0)
1675                 kvm_release_rma(ri);
1676 
1677         ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
1678         return fd;
1679 }
1680 
1681 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1682                                      int linux_psize)
1683 {
1684         struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1685 
1686         if (!def->shift)
1687                 return;
1688         (*sps)->page_shift = def->shift;
1689         (*sps)->slb_enc = def->sllp;
1690         (*sps)->enc[0].page_shift = def->shift;
1691         /*
1692          * Only return base page encoding. We don't want to return
1693          * all the supporting pte_enc, because our H_ENTER doesn't
1694          * support MPSS yet. Once they do, we can start passing all
1695          * support pte_enc here
1696          */
1697         (*sps)->enc[0].pte_enc = def->penc[linux_psize];
1698         (*sps)++;
1699 }
1700 
1701 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1702                                          struct kvm_ppc_smmu_info *info)
1703 {
1704         struct kvm_ppc_one_seg_page_size *sps;
1705 
1706         info->flags = KVM_PPC_PAGE_SIZES_REAL;
1707         if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1708                 info->flags |= KVM_PPC_1T_SEGMENTS;
1709         info->slb_size = mmu_slb_size;
1710 
1711         /* We only support these sizes for now, and no muti-size segments */
1712         sps = &info->sps[0];
1713         kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1714         kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1715         kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1716 
1717         return 0;
1718 }
1719 
1720 /*
1721  * Get (and clear) the dirty memory log for a memory slot.
1722  */
1723 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1724                                          struct kvm_dirty_log *log)
1725 {
1726         struct kvm_memory_slot *memslot;
1727         int r;
1728         unsigned long n;
1729 
1730         mutex_lock(&kvm->slots_lock);
1731 
1732         r = -EINVAL;
1733         if (log->slot >= KVM_USER_MEM_SLOTS)
1734                 goto out;
1735 
1736         memslot = id_to_memslot(kvm->memslots, log->slot);
1737         r = -ENOENT;
1738         if (!memslot->dirty_bitmap)
1739                 goto out;
1740 
1741         n = kvm_dirty_bitmap_bytes(memslot);
1742         memset(memslot->dirty_bitmap, 0, n);
1743 
1744         r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
1745         if (r)
1746                 goto out;
1747 
1748         r = -EFAULT;
1749         if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1750                 goto out;
1751 
1752         r = 0;
1753 out:
1754         mutex_unlock(&kvm->slots_lock);
1755         return r;
1756 }
1757 
1758 static void unpin_slot(struct kvm_memory_slot *memslot)
1759 {
1760         unsigned long *physp;
1761         unsigned long j, npages, pfn;
1762         struct page *page;
1763 
1764         physp = memslot->arch.slot_phys;
1765         npages = memslot->npages;
1766         if (!physp)
1767                 return;
1768         for (j = 0; j < npages; j++) {
1769                 if (!(physp[j] & KVMPPC_GOT_PAGE))
1770                         continue;
1771                 pfn = physp[j] >> PAGE_SHIFT;
1772                 page = pfn_to_page(pfn);
1773                 SetPageDirty(page);
1774                 put_page(page);
1775         }
1776 }
1777 
1778 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1779                                         struct kvm_memory_slot *dont)
1780 {
1781         if (!dont || free->arch.rmap != dont->arch.rmap) {
1782                 vfree(free->arch.rmap);
1783                 free->arch.rmap = NULL;
1784         }
1785         if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1786                 unpin_slot(free);
1787                 vfree(free->arch.slot_phys);
1788                 free->arch.slot_phys = NULL;
1789         }
1790 }
1791 
1792 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1793                                          unsigned long npages)
1794 {
1795         slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1796         if (!slot->arch.rmap)
1797                 return -ENOMEM;
1798         slot->arch.slot_phys = NULL;
1799 
1800         return 0;
1801 }
1802 
1803 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1804                                         struct kvm_memory_slot *memslot,
1805                                         struct kvm_userspace_memory_region *mem)
1806 {
1807         unsigned long *phys;
1808 
1809         /* Allocate a slot_phys array if needed */
1810         phys = memslot->arch.slot_phys;
1811         if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1812                 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1813                 if (!phys)
1814                         return -ENOMEM;
1815                 memslot->arch.slot_phys = phys;
1816         }
1817 
1818         return 0;
1819 }
1820 
1821 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1822                                 struct kvm_userspace_memory_region *mem,
1823                                 const struct kvm_memory_slot *old)
1824 {
1825         unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1826         struct kvm_memory_slot *memslot;
1827 
1828         if (npages && old->npages) {
1829                 /*
1830                  * If modifying a memslot, reset all the rmap dirty bits.
1831                  * If this is a new memslot, we don't need to do anything
1832                  * since the rmap array starts out as all zeroes,
1833                  * i.e. no pages are dirty.
1834                  */
1835                 memslot = id_to_memslot(kvm->memslots, mem->slot);
1836                 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1837         }
1838 }
1839 
1840 /*
1841  * Update LPCR values in kvm->arch and in vcores.
1842  * Caller must hold kvm->lock.
1843  */
1844 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1845 {
1846         long int i;
1847         u32 cores_done = 0;
1848 
1849         if ((kvm->arch.lpcr & mask) == lpcr)
1850                 return;
1851 
1852         kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1853 
1854         for (i = 0; i < KVM_MAX_VCORES; ++i) {
1855                 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1856                 if (!vc)
1857                         continue;
1858                 spin_lock(&vc->lock);
1859                 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1860                 spin_unlock(&vc->lock);
1861                 if (++cores_done >= kvm->arch.online_vcores)
1862                         break;
1863         }
1864 }
1865 
1866 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
1867 {
1868         return;
1869 }
1870 
1871 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
1872 {
1873         int err = 0;
1874         struct kvm *kvm = vcpu->kvm;
1875         struct kvm_rma_info *ri = NULL;
1876         unsigned long hva;
1877         struct kvm_memory_slot *memslot;
1878         struct vm_area_struct *vma;
1879         unsigned long lpcr = 0, senc;
1880         unsigned long lpcr_mask = 0;
1881         unsigned long psize, porder;
1882         unsigned long rma_size;
1883         unsigned long rmls;
1884         unsigned long *physp;
1885         unsigned long i, npages;
1886         int srcu_idx;
1887 
1888         mutex_lock(&kvm->lock);
1889         if (kvm->arch.rma_setup_done)
1890                 goto out;       /* another vcpu beat us to it */
1891 
1892         /* Allocate hashed page table (if not done already) and reset it */
1893         if (!kvm->arch.hpt_virt) {
1894                 err = kvmppc_alloc_hpt(kvm, NULL);
1895                 if (err) {
1896                         pr_err("KVM: Couldn't alloc HPT\n");
1897                         goto out;
1898                 }
1899         }
1900 
1901         /* Look up the memslot for guest physical address 0 */
1902         srcu_idx = srcu_read_lock(&kvm->srcu);
1903         memslot = gfn_to_memslot(kvm, 0);
1904 
1905         /* We must have some memory at 0 by now */
1906         err = -EINVAL;
1907         if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
1908                 goto out_srcu;
1909 
1910         /* Look up the VMA for the start of this memory slot */
1911         hva = memslot->userspace_addr;
1912         down_read(&current->mm->mmap_sem);
1913         vma = find_vma(current->mm, hva);
1914         if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
1915                 goto up_out;
1916 
1917         psize = vma_kernel_pagesize(vma);
1918         porder = __ilog2(psize);
1919 
1920         /* Is this one of our preallocated RMAs? */
1921         if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
1922             hva == vma->vm_start)
1923                 ri = vma->vm_file->private_data;
1924 
1925         up_read(&current->mm->mmap_sem);
1926 
1927         if (!ri) {
1928                 /* On POWER7, use VRMA; on PPC970, give up */
1929                 err = -EPERM;
1930                 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1931                         pr_err("KVM: CPU requires an RMO\n");
1932                         goto out_srcu;
1933                 }
1934 
1935                 /* We can handle 4k, 64k or 16M pages in the VRMA */
1936                 err = -EINVAL;
1937                 if (!(psize == 0x1000 || psize == 0x10000 ||
1938                       psize == 0x1000000))
1939                         goto out_srcu;
1940 
1941                 /* Update VRMASD field in the LPCR */
1942                 senc = slb_pgsize_encoding(psize);
1943                 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
1944                         (VRMA_VSID << SLB_VSID_SHIFT_1T);
1945                 lpcr_mask = LPCR_VRMASD;
1946                 /* the -4 is to account for senc values starting at 0x10 */
1947                 lpcr = senc << (LPCR_VRMASD_SH - 4);
1948 
1949                 /* Create HPTEs in the hash page table for the VRMA */
1950                 kvmppc_map_vrma(vcpu, memslot, porder);
1951 
1952         } else {
1953                 /* Set up to use an RMO region */
1954                 rma_size = kvm_rma_pages;
1955                 if (rma_size > memslot->npages)
1956                         rma_size = memslot->npages;
1957                 rma_size <<= PAGE_SHIFT;
1958                 rmls = lpcr_rmls(rma_size);
1959                 err = -EINVAL;
1960                 if ((long)rmls < 0) {
1961                         pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
1962                         goto out_srcu;
1963                 }
1964                 atomic_inc(&ri->use_count);
1965                 kvm->arch.rma = ri;
1966 
1967                 /* Update LPCR and RMOR */
1968                 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
1969                         /* PPC970; insert RMLS value (split field) in HID4 */
1970                         lpcr_mask = (1ul << HID4_RMLS0_SH) |
1971                                 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
1972                         lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
1973                                 ((rmls & 3) << HID4_RMLS2_SH);
1974                         /* RMOR is also in HID4 */
1975                         lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
1976                                 << HID4_RMOR_SH;
1977                 } else {
1978                         /* POWER7 */
1979                         lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
1980                         lpcr = rmls << LPCR_RMLS_SH;
1981                         kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
1982                 }
1983                 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
1984                         ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
1985 
1986                 /* Initialize phys addrs of pages in RMO */
1987                 npages = kvm_rma_pages;
1988                 porder = __ilog2(npages);
1989                 physp = memslot->arch.slot_phys;
1990                 if (physp) {
1991                         if (npages > memslot->npages)
1992                                 npages = memslot->npages;
1993                         spin_lock(&kvm->arch.slot_phys_lock);
1994                         for (i = 0; i < npages; ++i)
1995                                 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
1996                                         porder;
1997                         spin_unlock(&kvm->arch.slot_phys_lock);
1998                 }
1999         }
2000 
2001         kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2002 
2003         /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2004         smp_wmb();
2005         kvm->arch.rma_setup_done = 1;
2006         err = 0;
2007  out_srcu:
2008         srcu_read_unlock(&kvm->srcu, srcu_idx);
2009  out:
2010         mutex_unlock(&kvm->lock);
2011         return err;
2012 
2013  up_out:
2014         up_read(&current->mm->mmap_sem);
2015         goto out_srcu;
2016 }
2017 
2018 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2019 {
2020         unsigned long lpcr, lpid;
2021 
2022         /* Allocate the guest's logical partition ID */
2023 
2024         lpid = kvmppc_alloc_lpid();
2025         if ((long)lpid < 0)
2026                 return -ENOMEM;
2027         kvm->arch.lpid = lpid;
2028 
2029         /*
2030          * Since we don't flush the TLB when tearing down a VM,
2031          * and this lpid might have previously been used,
2032          * make sure we flush on each core before running the new VM.
2033          */
2034         cpumask_setall(&kvm->arch.need_tlb_flush);
2035 
2036         kvm->arch.rma = NULL;
2037 
2038         kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2039 
2040         if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2041                 /* PPC970; HID4 is effectively the LPCR */
2042                 kvm->arch.host_lpid = 0;
2043                 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2044                 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2045                 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2046                         ((lpid & 0xf) << HID4_LPID5_SH);
2047         } else {
2048                 /* POWER7; init LPCR for virtual RMA mode */
2049                 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2050                 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2051                 lpcr &= LPCR_PECE | LPCR_LPES;
2052                 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2053                         LPCR_VPM0 | LPCR_VPM1;
2054                 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2055                         (VRMA_VSID << SLB_VSID_SHIFT_1T);
2056         }
2057         kvm->arch.lpcr = lpcr;
2058 
2059         kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
2060         spin_lock_init(&kvm->arch.slot_phys_lock);
2061 
2062         /*
2063          * Don't allow secondary CPU threads to come online
2064          * while any KVM VMs exist.
2065          */
2066         inhibit_secondary_onlining();
2067 
2068         return 0;
2069 }
2070 
2071 static void kvmppc_free_vcores(struct kvm *kvm)
2072 {
2073         long int i;
2074 
2075         for (i = 0; i < KVM_MAX_VCORES; ++i)
2076                 kfree(kvm->arch.vcores[i]);
2077         kvm->arch.online_vcores = 0;
2078 }
2079 
2080 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2081 {
2082         uninhibit_secondary_onlining();
2083 
2084         kvmppc_free_vcores(kvm);
2085         if (kvm->arch.rma) {
2086                 kvm_release_rma(kvm->arch.rma);
2087                 kvm->arch.rma = NULL;
2088         }
2089 
2090         kvmppc_free_hpt(kvm);
2091 }
2092 
2093 /* We don't need to emulate any privileged instructions or dcbz */
2094 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2095                                      unsigned int inst, int *advance)
2096 {
2097         return EMULATE_FAIL;
2098 }
2099 
2100 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2101                                         ulong spr_val)
2102 {
2103         return EMULATE_FAIL;
2104 }
2105 
2106 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2107                                         ulong *spr_val)
2108 {
2109         return EMULATE_FAIL;
2110 }
2111 
2112 static int kvmppc_core_check_processor_compat_hv(void)
2113 {
2114         if (!cpu_has_feature(CPU_FTR_HVMODE))
2115                 return -EIO;
2116         return 0;
2117 }
2118 
2119 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2120                                  unsigned int ioctl, unsigned long arg)
2121 {
2122         struct kvm *kvm __maybe_unused = filp->private_data;
2123         void __user *argp = (void __user *)arg;
2124         long r;
2125 
2126         switch (ioctl) {
2127 
2128         case KVM_ALLOCATE_RMA: {
2129                 struct kvm_allocate_rma rma;
2130                 struct kvm *kvm = filp->private_data;
2131 
2132                 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2133                 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2134                         r = -EFAULT;
2135                 break;
2136         }
2137 
2138         case KVM_PPC_ALLOCATE_HTAB: {
2139                 u32 htab_order;
2140 
2141                 r = -EFAULT;
2142                 if (get_user(htab_order, (u32 __user *)argp))
2143                         break;
2144                 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2145                 if (r)
2146                         break;
2147                 r = -EFAULT;
2148                 if (put_user(htab_order, (u32 __user *)argp))
2149                         break;
2150                 r = 0;
2151                 break;
2152         }
2153 
2154         case KVM_PPC_GET_HTAB_FD: {
2155                 struct kvm_get_htab_fd ghf;
2156 
2157                 r = -EFAULT;
2158                 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2159                         break;
2160                 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2161                 break;
2162         }
2163 
2164         default:
2165                 r = -ENOTTY;
2166         }
2167 
2168         return r;
2169 }
2170 
2171 static struct kvmppc_ops kvm_ops_hv = {
2172         .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2173         .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2174         .get_one_reg = kvmppc_get_one_reg_hv,
2175         .set_one_reg = kvmppc_set_one_reg_hv,
2176         .vcpu_load   = kvmppc_core_vcpu_load_hv,
2177         .vcpu_put    = kvmppc_core_vcpu_put_hv,
2178         .set_msr     = kvmppc_set_msr_hv,
2179         .vcpu_run    = kvmppc_vcpu_run_hv,
2180         .vcpu_create = kvmppc_core_vcpu_create_hv,
2181         .vcpu_free   = kvmppc_core_vcpu_free_hv,
2182         .check_requests = kvmppc_core_check_requests_hv,
2183         .get_dirty_log  = kvm_vm_ioctl_get_dirty_log_hv,
2184         .flush_memslot  = kvmppc_core_flush_memslot_hv,
2185         .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2186         .commit_memory_region  = kvmppc_core_commit_memory_region_hv,
2187         .unmap_hva = kvm_unmap_hva_hv,
2188         .unmap_hva_range = kvm_unmap_hva_range_hv,
2189         .age_hva  = kvm_age_hva_hv,
2190         .test_age_hva = kvm_test_age_hva_hv,
2191         .set_spte_hva = kvm_set_spte_hva_hv,
2192         .mmu_destroy  = kvmppc_mmu_destroy_hv,
2193         .free_memslot = kvmppc_core_free_memslot_hv,
2194         .create_memslot = kvmppc_core_create_memslot_hv,
2195         .init_vm =  kvmppc_core_init_vm_hv,
2196         .destroy_vm = kvmppc_core_destroy_vm_hv,
2197         .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2198         .emulate_op = kvmppc_core_emulate_op_hv,
2199         .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2200         .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2201         .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2202         .arch_vm_ioctl  = kvm_arch_vm_ioctl_hv,
2203 };
2204 
2205 static int kvmppc_book3s_init_hv(void)
2206 {
2207         int r;
2208         /*
2209          * FIXME!! Do we need to check on all cpus ?
2210          */
2211         r = kvmppc_core_check_processor_compat_hv();
2212         if (r < 0)
2213                 return r;
2214 
2215         kvm_ops_hv.owner = THIS_MODULE;
2216         kvmppc_hv_ops = &kvm_ops_hv;
2217 
2218         r = kvmppc_mmu_hv_init();
2219         return r;
2220 }
2221 
2222 static void kvmppc_book3s_exit_hv(void)
2223 {
2224         kvmppc_hv_ops = NULL;
2225 }
2226 
2227 module_init(kvmppc_book3s_init_hv);
2228 module_exit(kvmppc_book3s_exit_hv);
2229 MODULE_LICENSE("GPL");
2230 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp