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