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