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