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