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