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