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