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