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