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> |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 34 | #include <linux/miscdevice.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 35 | |
| 36 | #include <asm/reg.h> |
| 37 | #include <asm/cputable.h> |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 38 | #include <asm/cache.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 39 | #include <asm/cacheflush.h> |
| 40 | #include <asm/tlbflush.h> |
| 41 | #include <asm/uaccess.h> |
| 42 | #include <asm/io.h> |
| 43 | #include <asm/kvm_ppc.h> |
| 44 | #include <asm/kvm_book3s.h> |
| 45 | #include <asm/mmu_context.h> |
| 46 | #include <asm/lppaca.h> |
| 47 | #include <asm/processor.h> |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 48 | #include <asm/cputhreads.h> |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 49 | #include <asm/page.h> |
Michael Neuling | de1d924 | 2011-11-09 20:39:49 +0000 | [diff] [blame] | 50 | #include <asm/hvcall.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 51 | #include <asm/switch_to.h> |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 52 | #include <asm/smp.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 53 | #include <linux/gfp.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 54 | #include <linux/vmalloc.h> |
| 55 | #include <linux/highmem.h> |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 56 | #include <linux/hugetlb.h> |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 57 | #include <linux/module.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 58 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 59 | #include "book3s.h" |
| 60 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 61 | #define CREATE_TRACE_POINTS |
| 62 | #include "trace_hv.h" |
| 63 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 64 | /* #define EXIT_DEBUG */ |
| 65 | /* #define EXIT_DEBUG_SIMPLE */ |
| 66 | /* #define EXIT_DEBUG_INT */ |
| 67 | |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 68 | /* Used to indicate that a guest page fault needs to be handled */ |
| 69 | #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1) |
| 70 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 71 | /* Used as a "null" value for timebase values */ |
| 72 | #define TB_NIL (~(u64)0) |
| 73 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 74 | static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); |
| 75 | |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 76 | #if defined(CONFIG_PPC_64K_PAGES) |
| 77 | #define MPP_BUFFER_ORDER 0 |
| 78 | #elif defined(CONFIG_PPC_4K_PAGES) |
| 79 | #define MPP_BUFFER_ORDER 3 |
| 80 | #endif |
| 81 | |
| 82 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 83 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu); |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 84 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 85 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 86 | static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu) |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 87 | { |
| 88 | int me; |
| 89 | int cpu = vcpu->cpu; |
| 90 | wait_queue_head_t *wqp; |
| 91 | |
| 92 | wqp = kvm_arch_vcpu_wq(vcpu); |
| 93 | if (waitqueue_active(wqp)) { |
| 94 | wake_up_interruptible(wqp); |
| 95 | ++vcpu->stat.halt_wakeup; |
| 96 | } |
| 97 | |
| 98 | me = get_cpu(); |
| 99 | |
| 100 | /* CPU points to the first thread of the core */ |
| 101 | if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) { |
Anton Blanchard | 7505258 | 2014-03-25 10:47:01 +1100 | [diff] [blame] | 102 | #ifdef CONFIG_PPC_ICP_NATIVE |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 103 | int real_cpu = cpu + vcpu->arch.ptid; |
| 104 | if (paca[real_cpu].kvm_hstate.xics_phys) |
| 105 | xics_wake_cpu(real_cpu); |
Andreas Schwab | 48eaef0 | 2013-12-30 15:36:56 +0100 | [diff] [blame] | 106 | else |
| 107 | #endif |
| 108 | if (cpu_online(cpu)) |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 109 | smp_send_reschedule(cpu); |
| 110 | } |
| 111 | put_cpu(); |
| 112 | } |
| 113 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 114 | /* |
| 115 | * We use the vcpu_load/put functions to measure stolen time. |
| 116 | * Stolen time is counted as time when either the vcpu is able to |
| 117 | * run as part of a virtual core, but the task running the vcore |
| 118 | * is preempted or sleeping, or when the vcpu needs something done |
| 119 | * in the kernel by the task running the vcpu, but that task is |
| 120 | * preempted or sleeping. Those two things have to be counted |
| 121 | * separately, since one of the vcpu tasks will take on the job |
| 122 | * of running the core, and the other vcpu tasks in the vcore will |
| 123 | * sleep waiting for it to do that, but that sleep shouldn't count |
| 124 | * as stolen time. |
| 125 | * |
| 126 | * Hence we accumulate stolen time when the vcpu can run as part of |
| 127 | * a vcore using vc->stolen_tb, and the stolen time when the vcpu |
| 128 | * needs its task to do other things in the kernel (for example, |
| 129 | * service a page fault) in busy_stolen. We don't accumulate |
| 130 | * stolen time for a vcore when it is inactive, or for a vcpu |
| 131 | * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of |
| 132 | * a misnomer; it means that the vcpu task is not executing in |
| 133 | * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in |
| 134 | * the kernel. We don't have any way of dividing up that time |
| 135 | * between time that the vcpu is genuinely stopped, time that |
| 136 | * the task is actively working on behalf of the vcpu, and time |
| 137 | * that the task is preempted, so we don't count any of it as |
| 138 | * stolen. |
| 139 | * |
| 140 | * Updates to busy_stolen are protected by arch.tbacct_lock; |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 141 | * updates to vc->stolen_tb are protected by the vcore->stoltb_lock |
| 142 | * lock. The stolen times are measured in units of timebase ticks. |
| 143 | * (Note that the != TB_NIL checks below are purely defensive; |
| 144 | * they should never fail.) |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 145 | */ |
| 146 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 147 | static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 148 | { |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 149 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 150 | unsigned long flags; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 151 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 152 | /* |
| 153 | * We can test vc->runner without taking the vcore lock, |
| 154 | * because only this task ever sets vc->runner to this |
| 155 | * vcpu, and once it is set to this vcpu, only this task |
| 156 | * ever sets it to NULL. |
| 157 | */ |
| 158 | if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) { |
| 159 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
| 160 | if (vc->preempt_tb != TB_NIL) { |
| 161 | vc->stolen_tb += mftb() - vc->preempt_tb; |
| 162 | vc->preempt_tb = TB_NIL; |
| 163 | } |
| 164 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 165 | } |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 166 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 167 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST && |
| 168 | vcpu->arch.busy_preempt != TB_NIL) { |
| 169 | vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt; |
| 170 | vcpu->arch.busy_preempt = TB_NIL; |
| 171 | } |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 172 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 175 | static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 176 | { |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 177 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 178 | unsigned long flags; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 179 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 180 | if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) { |
| 181 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 182 | vc->preempt_tb = mftb(); |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 183 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
| 184 | } |
| 185 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 186 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST) |
| 187 | vcpu->arch.busy_preempt = mftb(); |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 188 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 191 | static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 192 | { |
| 193 | vcpu->arch.shregs.msr = msr; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 194 | kvmppc_end_cede(vcpu); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 197 | void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 198 | { |
| 199 | vcpu->arch.pvr = pvr; |
| 200 | } |
| 201 | |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 202 | int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) |
| 203 | { |
| 204 | unsigned long pcr = 0; |
| 205 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 206 | |
| 207 | if (arch_compat) { |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 208 | switch (arch_compat) { |
| 209 | case PVR_ARCH_205: |
Paul Mackerras | 5557ae0 | 2014-01-08 21:25:24 +1100 | [diff] [blame] | 210 | /* |
| 211 | * If an arch bit is set in PCR, all the defined |
| 212 | * higher-order arch bits also have to be set. |
| 213 | */ |
| 214 | pcr = PCR_ARCH_206 | PCR_ARCH_205; |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 215 | break; |
| 216 | case PVR_ARCH_206: |
| 217 | case PVR_ARCH_206p: |
Paul Mackerras | 5557ae0 | 2014-01-08 21:25:24 +1100 | [diff] [blame] | 218 | pcr = PCR_ARCH_206; |
| 219 | break; |
| 220 | case PVR_ARCH_207: |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 221 | break; |
| 222 | default: |
| 223 | return -EINVAL; |
| 224 | } |
Paul Mackerras | 5557ae0 | 2014-01-08 21:25:24 +1100 | [diff] [blame] | 225 | |
| 226 | if (!cpu_has_feature(CPU_FTR_ARCH_207S)) { |
| 227 | /* POWER7 can't emulate POWER8 */ |
| 228 | if (!(pcr & PCR_ARCH_206)) |
| 229 | return -EINVAL; |
| 230 | pcr &= ~PCR_ARCH_206; |
| 231 | } |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | spin_lock(&vc->lock); |
| 235 | vc->arch_compat = arch_compat; |
| 236 | vc->pcr = pcr; |
| 237 | spin_unlock(&vc->lock); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 242 | void kvmppc_dump_regs(struct kvm_vcpu *vcpu) |
| 243 | { |
| 244 | int r; |
| 245 | |
| 246 | pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id); |
| 247 | pr_err("pc = %.16lx msr = %.16llx trap = %x\n", |
| 248 | vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap); |
| 249 | for (r = 0; r < 16; ++r) |
| 250 | pr_err("r%2d = %.16lx r%d = %.16lx\n", |
| 251 | r, kvmppc_get_gpr(vcpu, r), |
| 252 | r+16, kvmppc_get_gpr(vcpu, r+16)); |
| 253 | pr_err("ctr = %.16lx lr = %.16lx\n", |
| 254 | vcpu->arch.ctr, vcpu->arch.lr); |
| 255 | pr_err("srr0 = %.16llx srr1 = %.16llx\n", |
| 256 | vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1); |
| 257 | pr_err("sprg0 = %.16llx sprg1 = %.16llx\n", |
| 258 | vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1); |
| 259 | pr_err("sprg2 = %.16llx sprg3 = %.16llx\n", |
| 260 | vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); |
| 261 | pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n", |
| 262 | vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr); |
| 263 | pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar); |
| 264 | pr_err("fault dar = %.16lx dsisr = %.8x\n", |
| 265 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); |
| 266 | pr_err("SLB (%d entries):\n", vcpu->arch.slb_max); |
| 267 | for (r = 0; r < vcpu->arch.slb_max; ++r) |
| 268 | pr_err(" ESID = %.16llx VSID = %.16llx\n", |
| 269 | vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv); |
| 270 | pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n", |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 271 | vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1, |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 272 | vcpu->arch.last_inst); |
| 273 | } |
| 274 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 275 | struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id) |
| 276 | { |
| 277 | int r; |
| 278 | struct kvm_vcpu *v, *ret = NULL; |
| 279 | |
| 280 | mutex_lock(&kvm->lock); |
| 281 | kvm_for_each_vcpu(r, v, kvm) { |
| 282 | if (v->vcpu_id == id) { |
| 283 | ret = v; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | mutex_unlock(&kvm->lock); |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa) |
| 292 | { |
Anton Blanchard | f13c13a | 2013-08-07 02:01:26 +1000 | [diff] [blame] | 293 | vpa->__old_status |= LPPACA_OLD_SHARED_PROC; |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 294 | vpa->yield_count = cpu_to_be32(1); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 297 | static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v, |
| 298 | unsigned long addr, unsigned long len) |
| 299 | { |
| 300 | /* check address is cacheline aligned */ |
| 301 | if (addr & (L1_CACHE_BYTES - 1)) |
| 302 | return -EINVAL; |
| 303 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 304 | if (v->next_gpa != addr || v->len != len) { |
| 305 | v->next_gpa = addr; |
| 306 | v->len = addr ? len : 0; |
| 307 | v->update_pending = 1; |
| 308 | } |
| 309 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 310 | return 0; |
| 311 | } |
| 312 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 313 | /* Length for a per-processor buffer is passed in at offset 4 in the buffer */ |
| 314 | struct reg_vpa { |
| 315 | u32 dummy; |
| 316 | union { |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 317 | __be16 hword; |
| 318 | __be32 word; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 319 | } length; |
| 320 | }; |
| 321 | |
| 322 | static int vpa_is_registered(struct kvmppc_vpa *vpap) |
| 323 | { |
| 324 | if (vpap->update_pending) |
| 325 | return vpap->next_gpa != 0; |
| 326 | return vpap->pinned_addr != NULL; |
| 327 | } |
| 328 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 329 | static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu, |
| 330 | unsigned long flags, |
| 331 | unsigned long vcpuid, unsigned long vpa) |
| 332 | { |
| 333 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 334 | unsigned long len, nb; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 335 | void *va; |
| 336 | struct kvm_vcpu *tvcpu; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 337 | int err; |
| 338 | int subfunc; |
| 339 | struct kvmppc_vpa *vpap; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 340 | |
| 341 | tvcpu = kvmppc_find_vcpu(kvm, vcpuid); |
| 342 | if (!tvcpu) |
| 343 | return H_PARAMETER; |
| 344 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 345 | subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK; |
| 346 | if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL || |
| 347 | subfunc == H_VPA_REG_SLB) { |
| 348 | /* Registering new area - address must be cache-line aligned */ |
| 349 | if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 350 | return H_PARAMETER; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 351 | |
| 352 | /* convert logical addr to kernel addr and read length */ |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 353 | va = kvmppc_pin_guest_page(kvm, vpa, &nb); |
| 354 | if (va == NULL) |
Paul Mackerras | b2b2f16 | 2011-12-12 12:28:21 +0000 | [diff] [blame] | 355 | return H_PARAMETER; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 356 | if (subfunc == H_VPA_REG_VPA) |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 357 | len = be16_to_cpu(((struct reg_vpa *)va)->length.hword); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 358 | else |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 359 | len = be32_to_cpu(((struct reg_vpa *)va)->length.word); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 360 | kvmppc_unpin_guest_page(kvm, va, vpa, false); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 361 | |
| 362 | /* Check length */ |
| 363 | if (len > nb || len < sizeof(struct reg_vpa)) |
| 364 | return H_PARAMETER; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 365 | } else { |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 366 | vpa = 0; |
| 367 | len = 0; |
| 368 | } |
| 369 | |
| 370 | err = H_PARAMETER; |
| 371 | vpap = NULL; |
| 372 | spin_lock(&tvcpu->arch.vpa_update_lock); |
| 373 | |
| 374 | switch (subfunc) { |
| 375 | case H_VPA_REG_VPA: /* register VPA */ |
| 376 | if (len < sizeof(struct lppaca)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 377 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 378 | vpap = &tvcpu->arch.vpa; |
| 379 | err = 0; |
| 380 | break; |
| 381 | |
| 382 | case H_VPA_REG_DTL: /* register DTL */ |
| 383 | if (len < sizeof(struct dtl_entry)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 384 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 385 | len -= len % sizeof(struct dtl_entry); |
| 386 | |
| 387 | /* Check that they have previously registered a VPA */ |
| 388 | err = H_RESOURCE; |
| 389 | if (!vpa_is_registered(&tvcpu->arch.vpa)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 390 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 391 | |
| 392 | vpap = &tvcpu->arch.dtl; |
| 393 | err = 0; |
| 394 | break; |
| 395 | |
| 396 | case H_VPA_REG_SLB: /* register SLB shadow buffer */ |
| 397 | /* Check that they have previously registered a VPA */ |
| 398 | err = H_RESOURCE; |
| 399 | if (!vpa_is_registered(&tvcpu->arch.vpa)) |
| 400 | break; |
| 401 | |
| 402 | vpap = &tvcpu->arch.slb_shadow; |
| 403 | err = 0; |
| 404 | break; |
| 405 | |
| 406 | case H_VPA_DEREG_VPA: /* deregister VPA */ |
| 407 | /* Check they don't still have a DTL or SLB buf registered */ |
| 408 | err = H_RESOURCE; |
| 409 | if (vpa_is_registered(&tvcpu->arch.dtl) || |
| 410 | vpa_is_registered(&tvcpu->arch.slb_shadow)) |
| 411 | break; |
| 412 | |
| 413 | vpap = &tvcpu->arch.vpa; |
| 414 | err = 0; |
| 415 | break; |
| 416 | |
| 417 | case H_VPA_DEREG_DTL: /* deregister DTL */ |
| 418 | vpap = &tvcpu->arch.dtl; |
| 419 | err = 0; |
| 420 | break; |
| 421 | |
| 422 | case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */ |
| 423 | vpap = &tvcpu->arch.slb_shadow; |
| 424 | err = 0; |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | if (vpap) { |
| 429 | vpap->next_gpa = vpa; |
| 430 | vpap->len = len; |
| 431 | vpap->update_pending = 1; |
| 432 | } |
| 433 | |
| 434 | spin_unlock(&tvcpu->arch.vpa_update_lock); |
| 435 | |
| 436 | return err; |
| 437 | } |
| 438 | |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 439 | 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] | 440 | { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 441 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 442 | void *va; |
| 443 | unsigned long nb; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 444 | unsigned long gpa; |
| 445 | |
| 446 | /* |
| 447 | * We need to pin the page pointed to by vpap->next_gpa, |
| 448 | * but we can't call kvmppc_pin_guest_page under the lock |
| 449 | * as it does get_user_pages() and down_read(). So we |
| 450 | * have to drop the lock, pin the page, then get the lock |
| 451 | * again and check that a new area didn't get registered |
| 452 | * in the meantime. |
| 453 | */ |
| 454 | for (;;) { |
| 455 | gpa = vpap->next_gpa; |
| 456 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 457 | va = NULL; |
| 458 | nb = 0; |
| 459 | if (gpa) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 460 | va = kvmppc_pin_guest_page(kvm, gpa, &nb); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 461 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 462 | if (gpa == vpap->next_gpa) |
| 463 | break; |
| 464 | /* sigh... unpin that one and try again */ |
| 465 | if (va) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 466 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 467 | } |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 468 | |
| 469 | vpap->update_pending = 0; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 470 | if (va && nb < vpap->len) { |
| 471 | /* |
| 472 | * If it's now too short, it must be that userspace |
| 473 | * has changed the mappings underlying guest memory, |
| 474 | * so unregister the region. |
| 475 | */ |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 476 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 477 | va = NULL; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 478 | } |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 479 | if (vpap->pinned_addr) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 480 | kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa, |
| 481 | vpap->dirty); |
| 482 | vpap->gpa = gpa; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 483 | vpap->pinned_addr = va; |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 484 | vpap->dirty = false; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 485 | if (va) |
| 486 | vpap->pinned_end = va + vpap->len; |
| 487 | } |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 488 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 489 | static void kvmppc_update_vpas(struct kvm_vcpu *vcpu) |
| 490 | { |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 491 | if (!(vcpu->arch.vpa.update_pending || |
| 492 | vcpu->arch.slb_shadow.update_pending || |
| 493 | vcpu->arch.dtl.update_pending)) |
| 494 | return; |
| 495 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 496 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 497 | if (vcpu->arch.vpa.update_pending) { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 498 | kvmppc_update_vpa(vcpu, &vcpu->arch.vpa); |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 499 | if (vcpu->arch.vpa.pinned_addr) |
| 500 | init_vpa(vcpu, vcpu->arch.vpa.pinned_addr); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 501 | } |
| 502 | if (vcpu->arch.dtl.update_pending) { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 503 | kvmppc_update_vpa(vcpu, &vcpu->arch.dtl); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 504 | vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr; |
| 505 | vcpu->arch.dtl_index = 0; |
| 506 | } |
| 507 | if (vcpu->arch.slb_shadow.update_pending) |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 508 | kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 509 | spin_unlock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 512 | /* |
| 513 | * Return the accumulated stolen time for the vcore up until `now'. |
| 514 | * The caller should hold the vcore lock. |
| 515 | */ |
| 516 | static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now) |
| 517 | { |
| 518 | u64 p; |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 519 | unsigned long flags; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 520 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 521 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
| 522 | p = vc->stolen_tb; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 523 | if (vc->vcore_state != VCORE_INACTIVE && |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 524 | vc->preempt_tb != TB_NIL) |
| 525 | p += now - vc->preempt_tb; |
| 526 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 527 | return p; |
| 528 | } |
| 529 | |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 530 | static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, |
| 531 | struct kvmppc_vcore *vc) |
| 532 | { |
| 533 | struct dtl_entry *dt; |
| 534 | struct lppaca *vpa; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 535 | unsigned long stolen; |
| 536 | unsigned long core_stolen; |
| 537 | u64 now; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 538 | |
| 539 | dt = vcpu->arch.dtl_ptr; |
| 540 | vpa = vcpu->arch.vpa.pinned_addr; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 541 | now = mftb(); |
| 542 | core_stolen = vcore_stolen_time(vc, now); |
| 543 | stolen = core_stolen - vcpu->arch.stolen_logged; |
| 544 | vcpu->arch.stolen_logged = core_stolen; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 545 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 546 | stolen += vcpu->arch.busy_stolen; |
| 547 | vcpu->arch.busy_stolen = 0; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 548 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 549 | if (!dt || !vpa) |
| 550 | return; |
| 551 | memset(dt, 0, sizeof(struct dtl_entry)); |
| 552 | dt->dispatch_reason = 7; |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 553 | dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid); |
| 554 | dt->timebase = cpu_to_be64(now + vc->tb_offset); |
| 555 | dt->enqueue_to_dispatch_time = cpu_to_be32(stolen); |
| 556 | dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu)); |
| 557 | dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 558 | ++dt; |
| 559 | if (dt == vcpu->arch.dtl.pinned_end) |
| 560 | dt = vcpu->arch.dtl.pinned_addr; |
| 561 | vcpu->arch.dtl_ptr = dt; |
| 562 | /* order writing *dt vs. writing vpa->dtl_idx */ |
| 563 | smp_wmb(); |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 564 | vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 565 | vcpu->arch.dtl.dirty = true; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 568 | static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu) |
| 569 | { |
| 570 | if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207) |
| 571 | return true; |
| 572 | if ((!vcpu->arch.vcore->arch_compat) && |
| 573 | cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 574 | return true; |
| 575 | return false; |
| 576 | } |
| 577 | |
| 578 | static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags, |
| 579 | unsigned long resource, unsigned long value1, |
| 580 | unsigned long value2) |
| 581 | { |
| 582 | switch (resource) { |
| 583 | case H_SET_MODE_RESOURCE_SET_CIABR: |
| 584 | if (!kvmppc_power8_compatible(vcpu)) |
| 585 | return H_P2; |
| 586 | if (value2) |
| 587 | return H_P4; |
| 588 | if (mflags) |
| 589 | return H_UNSUPPORTED_FLAG_START; |
| 590 | /* Guests can't breakpoint the hypervisor */ |
| 591 | if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER) |
| 592 | return H_P3; |
| 593 | vcpu->arch.ciabr = value1; |
| 594 | return H_SUCCESS; |
| 595 | case H_SET_MODE_RESOURCE_SET_DAWR: |
| 596 | if (!kvmppc_power8_compatible(vcpu)) |
| 597 | return H_P2; |
| 598 | if (mflags) |
| 599 | return H_UNSUPPORTED_FLAG_START; |
| 600 | if (value2 & DABRX_HYP) |
| 601 | return H_P4; |
| 602 | vcpu->arch.dawr = value1; |
| 603 | vcpu->arch.dawrx = value2; |
| 604 | return H_SUCCESS; |
| 605 | default: |
| 606 | return H_TOO_HARD; |
| 607 | } |
| 608 | } |
| 609 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 610 | int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) |
| 611 | { |
| 612 | unsigned long req = kvmppc_get_gpr(vcpu, 3); |
| 613 | unsigned long target, ret = H_SUCCESS; |
| 614 | struct kvm_vcpu *tvcpu; |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 615 | int idx, rc; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 616 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 617 | if (req <= MAX_HCALL_OPCODE && |
| 618 | !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls)) |
| 619 | return RESUME_HOST; |
| 620 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 621 | switch (req) { |
| 622 | case H_CEDE: |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 623 | break; |
| 624 | case H_PROD: |
| 625 | target = kvmppc_get_gpr(vcpu, 4); |
| 626 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); |
| 627 | if (!tvcpu) { |
| 628 | ret = H_PARAMETER; |
| 629 | break; |
| 630 | } |
| 631 | tvcpu->arch.prodded = 1; |
| 632 | smp_mb(); |
| 633 | if (vcpu->arch.ceded) { |
| 634 | if (waitqueue_active(&vcpu->wq)) { |
| 635 | wake_up_interruptible(&vcpu->wq); |
| 636 | vcpu->stat.halt_wakeup++; |
| 637 | } |
| 638 | } |
| 639 | break; |
| 640 | case H_CONFER: |
Paul Mackerras | 42d7604 | 2013-09-06 13:23:21 +1000 | [diff] [blame] | 641 | target = kvmppc_get_gpr(vcpu, 4); |
| 642 | if (target == -1) |
| 643 | break; |
| 644 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); |
| 645 | if (!tvcpu) { |
| 646 | ret = H_PARAMETER; |
| 647 | break; |
| 648 | } |
| 649 | kvm_vcpu_yield_to(tvcpu); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 650 | break; |
| 651 | case H_REGISTER_VPA: |
| 652 | ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 653 | kvmppc_get_gpr(vcpu, 5), |
| 654 | kvmppc_get_gpr(vcpu, 6)); |
| 655 | break; |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 656 | case H_RTAS: |
| 657 | if (list_empty(&vcpu->kvm->arch.rtas_tokens)) |
| 658 | return RESUME_HOST; |
| 659 | |
Paul Mackerras | c943809 | 2013-11-16 17:46:05 +1100 | [diff] [blame] | 660 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 661 | rc = kvmppc_rtas_hcall(vcpu); |
Paul Mackerras | c943809 | 2013-11-16 17:46:05 +1100 | [diff] [blame] | 662 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 663 | |
| 664 | if (rc == -ENOENT) |
| 665 | return RESUME_HOST; |
| 666 | else if (rc == 0) |
| 667 | break; |
| 668 | |
| 669 | /* Send the error out to userspace via KVM_RUN */ |
| 670 | return rc; |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 671 | case H_SET_MODE: |
| 672 | ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 673 | kvmppc_get_gpr(vcpu, 5), |
| 674 | kvmppc_get_gpr(vcpu, 6), |
| 675 | kvmppc_get_gpr(vcpu, 7)); |
| 676 | if (ret == H_TOO_HARD) |
| 677 | return RESUME_HOST; |
| 678 | break; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 679 | case H_XIRR: |
| 680 | case H_CPPR: |
| 681 | case H_EOI: |
| 682 | case H_IPI: |
Paul Mackerras | 8e44ddc | 2013-05-23 15:42:21 +0000 | [diff] [blame] | 683 | case H_IPOLL: |
| 684 | case H_XIRR_X: |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 685 | if (kvmppc_xics_enabled(vcpu)) { |
| 686 | ret = kvmppc_xics_hcall(vcpu, req); |
| 687 | break; |
| 688 | } /* fallthrough */ |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 689 | default: |
| 690 | return RESUME_HOST; |
| 691 | } |
| 692 | kvmppc_set_gpr(vcpu, 3, ret); |
| 693 | vcpu->arch.hcall_needed = 0; |
| 694 | return RESUME_GUEST; |
| 695 | } |
| 696 | |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 697 | static int kvmppc_hcall_impl_hv(unsigned long cmd) |
| 698 | { |
| 699 | switch (cmd) { |
| 700 | case H_CEDE: |
| 701 | case H_PROD: |
| 702 | case H_CONFER: |
| 703 | case H_REGISTER_VPA: |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 704 | case H_SET_MODE: |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 705 | #ifdef CONFIG_KVM_XICS |
| 706 | case H_XIRR: |
| 707 | case H_CPPR: |
| 708 | case H_EOI: |
| 709 | case H_IPI: |
| 710 | case H_IPOLL: |
| 711 | case H_XIRR_X: |
| 712 | #endif |
| 713 | return 1; |
| 714 | } |
| 715 | |
| 716 | /* See if it's in the real-mode table */ |
| 717 | return kvmppc_hcall_impl_hv_realmode(cmd); |
| 718 | } |
| 719 | |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 720 | static int kvmppc_emulate_debug_inst(struct kvm_run *run, |
| 721 | struct kvm_vcpu *vcpu) |
| 722 | { |
| 723 | u32 last_inst; |
| 724 | |
| 725 | if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) != |
| 726 | EMULATE_DONE) { |
| 727 | /* |
| 728 | * Fetch failed, so return to guest and |
| 729 | * try executing it again. |
| 730 | */ |
| 731 | return RESUME_GUEST; |
| 732 | } |
| 733 | |
| 734 | if (last_inst == KVMPPC_INST_SW_BREAKPOINT) { |
| 735 | run->exit_reason = KVM_EXIT_DEBUG; |
| 736 | run->debug.arch.address = kvmppc_get_pc(vcpu); |
| 737 | return RESUME_HOST; |
| 738 | } else { |
| 739 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
| 740 | return RESUME_GUEST; |
| 741 | } |
| 742 | } |
| 743 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 744 | static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 745 | struct task_struct *tsk) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 746 | { |
| 747 | int r = RESUME_HOST; |
| 748 | |
| 749 | vcpu->stat.sum_exits++; |
| 750 | |
| 751 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 752 | run->ready_for_interrupt_injection = 1; |
| 753 | switch (vcpu->arch.trap) { |
| 754 | /* We're good on these - the host merely wanted to get our attention */ |
| 755 | case BOOK3S_INTERRUPT_HV_DECREMENTER: |
| 756 | vcpu->stat.dec_exits++; |
| 757 | r = RESUME_GUEST; |
| 758 | break; |
| 759 | case BOOK3S_INTERRUPT_EXTERNAL: |
Paul Mackerras | 5d00f66 | 2014-01-08 21:25:28 +1100 | [diff] [blame] | 760 | case BOOK3S_INTERRUPT_H_DOORBELL: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 761 | vcpu->stat.ext_intr_exits++; |
| 762 | r = RESUME_GUEST; |
| 763 | break; |
Mahesh Salgaonkar | dee6f24 | 2014-11-03 15:51:57 +1100 | [diff] [blame] | 764 | /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/ |
| 765 | case BOOK3S_INTERRUPT_HMI: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 766 | case BOOK3S_INTERRUPT_PERFMON: |
| 767 | r = RESUME_GUEST; |
| 768 | break; |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 769 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
| 770 | /* |
| 771 | * Deliver a machine check interrupt to the guest. |
| 772 | * We have to do this, even if the host has handled the |
| 773 | * machine check, because machine checks use SRR0/1 and |
| 774 | * the interrupt might have trashed guest state in them. |
| 775 | */ |
| 776 | kvmppc_book3s_queue_irqprio(vcpu, |
| 777 | BOOK3S_INTERRUPT_MACHINE_CHECK); |
| 778 | r = RESUME_GUEST; |
| 779 | break; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 780 | case BOOK3S_INTERRUPT_PROGRAM: |
| 781 | { |
| 782 | ulong flags; |
| 783 | /* |
| 784 | * Normally program interrupts are delivered directly |
| 785 | * to the guest by the hardware, but we can get here |
| 786 | * as a result of a hypervisor emulation interrupt |
| 787 | * (e40) getting turned into a 700 by BML RTAS. |
| 788 | */ |
| 789 | flags = vcpu->arch.shregs.msr & 0x1f0000ull; |
| 790 | kvmppc_core_queue_program(vcpu, flags); |
| 791 | r = RESUME_GUEST; |
| 792 | break; |
| 793 | } |
| 794 | case BOOK3S_INTERRUPT_SYSCALL: |
| 795 | { |
| 796 | /* hcall - punt to userspace */ |
| 797 | int i; |
| 798 | |
Liu Ping Fan | 27025a6 | 2013-11-19 14:12:48 +0800 | [diff] [blame] | 799 | /* hypercall with MSR_PR has already been handled in rmode, |
| 800 | * and never reaches here. |
| 801 | */ |
| 802 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 803 | run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3); |
| 804 | for (i = 0; i < 9; ++i) |
| 805 | run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i); |
| 806 | run->exit_reason = KVM_EXIT_PAPR_HCALL; |
| 807 | vcpu->arch.hcall_needed = 1; |
| 808 | r = RESUME_HOST; |
| 809 | break; |
| 810 | } |
| 811 | /* |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 812 | * We get these next two if the guest accesses a page which it thinks |
| 813 | * it has mapped but which is not actually present, either because |
| 814 | * it is for an emulated I/O device or because the corresonding |
| 815 | * host page has been paged out. Any other HDSI/HISI interrupts |
| 816 | * have been handled already. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 817 | */ |
| 818 | case BOOK3S_INTERRUPT_H_DATA_STORAGE: |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 819 | r = RESUME_PAGE_FAULT; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 820 | break; |
| 821 | case BOOK3S_INTERRUPT_H_INST_STORAGE: |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 822 | vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); |
| 823 | vcpu->arch.fault_dsisr = 0; |
| 824 | r = RESUME_PAGE_FAULT; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 825 | break; |
| 826 | /* |
| 827 | * This occurs if the guest executes an illegal instruction. |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 828 | * If the guest debug is disabled, generate a program interrupt |
| 829 | * to the guest. If guest debug is enabled, we need to check |
| 830 | * whether the instruction is a software breakpoint instruction. |
| 831 | * Accordingly return to Guest or Host. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 832 | */ |
| 833 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 834 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) { |
| 835 | r = kvmppc_emulate_debug_inst(run, vcpu); |
| 836 | } else { |
| 837 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
| 838 | r = RESUME_GUEST; |
| 839 | } |
Michael Ellerman | bd3048b | 2014-01-08 21:25:23 +1100 | [diff] [blame] | 840 | break; |
| 841 | /* |
| 842 | * This occurs if the guest (kernel or userspace), does something that |
| 843 | * is prohibited by HFSCR. We just generate a program interrupt to |
| 844 | * the guest. |
| 845 | */ |
| 846 | case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: |
| 847 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 848 | r = RESUME_GUEST; |
| 849 | break; |
| 850 | default: |
| 851 | kvmppc_dump_regs(vcpu); |
| 852 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", |
| 853 | vcpu->arch.trap, kvmppc_get_pc(vcpu), |
| 854 | vcpu->arch.shregs.msr); |
Paul Mackerras | f3271d4 | 2013-09-20 14:52:41 +1000 | [diff] [blame] | 855 | run->hw.hardware_exit_reason = vcpu->arch.trap; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 856 | r = RESUME_HOST; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 857 | break; |
| 858 | } |
| 859 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 860 | return r; |
| 861 | } |
| 862 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 863 | static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu, |
| 864 | struct kvm_sregs *sregs) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 865 | { |
| 866 | int i; |
| 867 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 868 | memset(sregs, 0, sizeof(struct kvm_sregs)); |
Aneesh Kumar K.V | 8791644 | 2013-08-22 17:08:39 +0530 | [diff] [blame] | 869 | sregs->pvr = vcpu->arch.pvr; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 870 | for (i = 0; i < vcpu->arch.slb_max; i++) { |
| 871 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige; |
| 872 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 878 | static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu, |
| 879 | struct kvm_sregs *sregs) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 880 | { |
| 881 | int i, j; |
| 882 | |
Paul Mackerras | 9333e6c | 2014-09-02 16:14:43 +1000 | [diff] [blame] | 883 | /* Only accept the same PVR as the host's, since we can't spoof it */ |
| 884 | if (sregs->pvr != vcpu->arch.pvr) |
| 885 | return -EINVAL; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 886 | |
| 887 | j = 0; |
| 888 | for (i = 0; i < vcpu->arch.slb_nr; i++) { |
| 889 | if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) { |
| 890 | vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe; |
| 891 | vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv; |
| 892 | ++j; |
| 893 | } |
| 894 | } |
| 895 | vcpu->arch.slb_max = j; |
| 896 | |
| 897 | return 0; |
| 898 | } |
| 899 | |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 900 | static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr, |
| 901 | bool preserve_top32) |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 902 | { |
| 903 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 904 | u64 mask; |
| 905 | |
| 906 | spin_lock(&vc->lock); |
| 907 | /* |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 908 | * If ILE (interrupt little-endian) has changed, update the |
| 909 | * MSR_LE bit in the intr_msr for each vcpu in this vcore. |
| 910 | */ |
| 911 | if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) { |
| 912 | struct kvm *kvm = vcpu->kvm; |
| 913 | struct kvm_vcpu *vcpu; |
| 914 | int i; |
| 915 | |
| 916 | mutex_lock(&kvm->lock); |
| 917 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 918 | if (vcpu->arch.vcore != vc) |
| 919 | continue; |
| 920 | if (new_lpcr & LPCR_ILE) |
| 921 | vcpu->arch.intr_msr |= MSR_LE; |
| 922 | else |
| 923 | vcpu->arch.intr_msr &= ~MSR_LE; |
| 924 | } |
| 925 | mutex_unlock(&kvm->lock); |
| 926 | } |
| 927 | |
| 928 | /* |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 929 | * Userspace can only modify DPFD (default prefetch depth), |
| 930 | * ILE (interrupt little-endian) and TC (translation control). |
Paul Mackerras | e0622bd | 2014-01-08 21:25:27 +1100 | [diff] [blame] | 931 | * On POWER8 userspace can also modify AIL (alt. interrupt loc.) |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 932 | */ |
| 933 | mask = LPCR_DPFD | LPCR_ILE | LPCR_TC; |
Paul Mackerras | e0622bd | 2014-01-08 21:25:27 +1100 | [diff] [blame] | 934 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 935 | mask |= LPCR_AIL; |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 936 | |
| 937 | /* Broken 32-bit version of LPCR must not clear top bits */ |
| 938 | if (preserve_top32) |
| 939 | mask &= 0xFFFFFFFF; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 940 | vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask); |
| 941 | spin_unlock(&vc->lock); |
| 942 | } |
| 943 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 944 | static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
| 945 | union kvmppc_one_reg *val) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 946 | { |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 947 | int r = 0; |
| 948 | long int i; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 949 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 950 | switch (id) { |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 951 | case KVM_REG_PPC_DEBUG_INST: |
| 952 | *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT); |
| 953 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 954 | case KVM_REG_PPC_HIOR: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 955 | *val = get_reg_val(id, 0); |
| 956 | break; |
| 957 | case KVM_REG_PPC_DABR: |
| 958 | *val = get_reg_val(id, vcpu->arch.dabr); |
| 959 | break; |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 960 | case KVM_REG_PPC_DABRX: |
| 961 | *val = get_reg_val(id, vcpu->arch.dabrx); |
| 962 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 963 | case KVM_REG_PPC_DSCR: |
| 964 | *val = get_reg_val(id, vcpu->arch.dscr); |
| 965 | break; |
| 966 | case KVM_REG_PPC_PURR: |
| 967 | *val = get_reg_val(id, vcpu->arch.purr); |
| 968 | break; |
| 969 | case KVM_REG_PPC_SPURR: |
| 970 | *val = get_reg_val(id, vcpu->arch.spurr); |
| 971 | break; |
| 972 | case KVM_REG_PPC_AMR: |
| 973 | *val = get_reg_val(id, vcpu->arch.amr); |
| 974 | break; |
| 975 | case KVM_REG_PPC_UAMOR: |
| 976 | *val = get_reg_val(id, vcpu->arch.uamor); |
| 977 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 978 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 979 | i = id - KVM_REG_PPC_MMCR0; |
| 980 | *val = get_reg_val(id, vcpu->arch.mmcr[i]); |
| 981 | break; |
| 982 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: |
| 983 | i = id - KVM_REG_PPC_PMC1; |
| 984 | *val = get_reg_val(id, vcpu->arch.pmc[i]); |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 985 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 986 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
| 987 | i = id - KVM_REG_PPC_SPMC1; |
| 988 | *val = get_reg_val(id, vcpu->arch.spmc[i]); |
| 989 | break; |
Paul Mackerras | 1494178 | 2013-09-06 13:11:18 +1000 | [diff] [blame] | 990 | case KVM_REG_PPC_SIAR: |
| 991 | *val = get_reg_val(id, vcpu->arch.siar); |
| 992 | break; |
| 993 | case KVM_REG_PPC_SDAR: |
| 994 | *val = get_reg_val(id, vcpu->arch.sdar); |
| 995 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 996 | case KVM_REG_PPC_SIER: |
| 997 | *val = get_reg_val(id, vcpu->arch.sier); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 998 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 999 | case KVM_REG_PPC_IAMR: |
| 1000 | *val = get_reg_val(id, vcpu->arch.iamr); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1001 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1002 | case KVM_REG_PPC_PSPB: |
| 1003 | *val = get_reg_val(id, vcpu->arch.pspb); |
| 1004 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1005 | case KVM_REG_PPC_DPDES: |
| 1006 | *val = get_reg_val(id, vcpu->arch.vcore->dpdes); |
| 1007 | break; |
| 1008 | case KVM_REG_PPC_DAWR: |
| 1009 | *val = get_reg_val(id, vcpu->arch.dawr); |
| 1010 | break; |
| 1011 | case KVM_REG_PPC_DAWRX: |
| 1012 | *val = get_reg_val(id, vcpu->arch.dawrx); |
| 1013 | break; |
| 1014 | case KVM_REG_PPC_CIABR: |
| 1015 | *val = get_reg_val(id, vcpu->arch.ciabr); |
| 1016 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1017 | case KVM_REG_PPC_CSIGR: |
| 1018 | *val = get_reg_val(id, vcpu->arch.csigr); |
| 1019 | break; |
| 1020 | case KVM_REG_PPC_TACR: |
| 1021 | *val = get_reg_val(id, vcpu->arch.tacr); |
| 1022 | break; |
| 1023 | case KVM_REG_PPC_TCSCR: |
| 1024 | *val = get_reg_val(id, vcpu->arch.tcscr); |
| 1025 | break; |
| 1026 | case KVM_REG_PPC_PID: |
| 1027 | *val = get_reg_val(id, vcpu->arch.pid); |
| 1028 | break; |
| 1029 | case KVM_REG_PPC_ACOP: |
| 1030 | *val = get_reg_val(id, vcpu->arch.acop); |
| 1031 | break; |
| 1032 | case KVM_REG_PPC_WORT: |
| 1033 | *val = get_reg_val(id, vcpu->arch.wort); |
| 1034 | break; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1035 | case KVM_REG_PPC_VPA_ADDR: |
| 1036 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1037 | *val = get_reg_val(id, vcpu->arch.vpa.next_gpa); |
| 1038 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1039 | break; |
| 1040 | case KVM_REG_PPC_VPA_SLB: |
| 1041 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1042 | val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa; |
| 1043 | val->vpaval.length = vcpu->arch.slb_shadow.len; |
| 1044 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1045 | break; |
| 1046 | case KVM_REG_PPC_VPA_DTL: |
| 1047 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1048 | val->vpaval.addr = vcpu->arch.dtl.next_gpa; |
| 1049 | val->vpaval.length = vcpu->arch.dtl.len; |
| 1050 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1051 | break; |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 1052 | case KVM_REG_PPC_TB_OFFSET: |
| 1053 | *val = get_reg_val(id, vcpu->arch.vcore->tb_offset); |
| 1054 | break; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1055 | case KVM_REG_PPC_LPCR: |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1056 | case KVM_REG_PPC_LPCR_64: |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1057 | *val = get_reg_val(id, vcpu->arch.vcore->lpcr); |
| 1058 | break; |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 1059 | case KVM_REG_PPC_PPR: |
| 1060 | *val = get_reg_val(id, vcpu->arch.ppr); |
| 1061 | break; |
Michael Neuling | a7d80d0 | 2014-03-25 10:47:03 +1100 | [diff] [blame] | 1062 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1063 | case KVM_REG_PPC_TFHAR: |
| 1064 | *val = get_reg_val(id, vcpu->arch.tfhar); |
| 1065 | break; |
| 1066 | case KVM_REG_PPC_TFIAR: |
| 1067 | *val = get_reg_val(id, vcpu->arch.tfiar); |
| 1068 | break; |
| 1069 | case KVM_REG_PPC_TEXASR: |
| 1070 | *val = get_reg_val(id, vcpu->arch.texasr); |
| 1071 | break; |
| 1072 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: |
| 1073 | i = id - KVM_REG_PPC_TM_GPR0; |
| 1074 | *val = get_reg_val(id, vcpu->arch.gpr_tm[i]); |
| 1075 | break; |
| 1076 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: |
| 1077 | { |
| 1078 | int j; |
| 1079 | i = id - KVM_REG_PPC_TM_VSR0; |
| 1080 | if (i < 32) |
| 1081 | for (j = 0; j < TS_FPRWIDTH; j++) |
| 1082 | val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j]; |
| 1083 | else { |
| 1084 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1085 | val->vval = vcpu->arch.vr_tm.vr[i-32]; |
| 1086 | else |
| 1087 | r = -ENXIO; |
| 1088 | } |
| 1089 | break; |
| 1090 | } |
| 1091 | case KVM_REG_PPC_TM_CR: |
| 1092 | *val = get_reg_val(id, vcpu->arch.cr_tm); |
| 1093 | break; |
| 1094 | case KVM_REG_PPC_TM_LR: |
| 1095 | *val = get_reg_val(id, vcpu->arch.lr_tm); |
| 1096 | break; |
| 1097 | case KVM_REG_PPC_TM_CTR: |
| 1098 | *val = get_reg_val(id, vcpu->arch.ctr_tm); |
| 1099 | break; |
| 1100 | case KVM_REG_PPC_TM_FPSCR: |
| 1101 | *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr); |
| 1102 | break; |
| 1103 | case KVM_REG_PPC_TM_AMR: |
| 1104 | *val = get_reg_val(id, vcpu->arch.amr_tm); |
| 1105 | break; |
| 1106 | case KVM_REG_PPC_TM_PPR: |
| 1107 | *val = get_reg_val(id, vcpu->arch.ppr_tm); |
| 1108 | break; |
| 1109 | case KVM_REG_PPC_TM_VRSAVE: |
| 1110 | *val = get_reg_val(id, vcpu->arch.vrsave_tm); |
| 1111 | break; |
| 1112 | case KVM_REG_PPC_TM_VSCR: |
| 1113 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1114 | *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]); |
| 1115 | else |
| 1116 | r = -ENXIO; |
| 1117 | break; |
| 1118 | case KVM_REG_PPC_TM_DSCR: |
| 1119 | *val = get_reg_val(id, vcpu->arch.dscr_tm); |
| 1120 | break; |
| 1121 | case KVM_REG_PPC_TM_TAR: |
| 1122 | *val = get_reg_val(id, vcpu->arch.tar_tm); |
| 1123 | break; |
| 1124 | #endif |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 1125 | case KVM_REG_PPC_ARCH_COMPAT: |
| 1126 | *val = get_reg_val(id, vcpu->arch.vcore->arch_compat); |
| 1127 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1128 | default: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1129 | r = -EINVAL; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1130 | break; |
| 1131 | } |
| 1132 | |
| 1133 | return r; |
| 1134 | } |
| 1135 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1136 | static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
| 1137 | union kvmppc_one_reg *val) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1138 | { |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1139 | int r = 0; |
| 1140 | long int i; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1141 | unsigned long addr, len; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1142 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1143 | switch (id) { |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1144 | case KVM_REG_PPC_HIOR: |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1145 | /* Only allow this to be set to zero */ |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1146 | if (set_reg_val(id, *val)) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1147 | r = -EINVAL; |
| 1148 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1149 | case KVM_REG_PPC_DABR: |
| 1150 | vcpu->arch.dabr = set_reg_val(id, *val); |
| 1151 | break; |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 1152 | case KVM_REG_PPC_DABRX: |
| 1153 | vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP; |
| 1154 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1155 | case KVM_REG_PPC_DSCR: |
| 1156 | vcpu->arch.dscr = set_reg_val(id, *val); |
| 1157 | break; |
| 1158 | case KVM_REG_PPC_PURR: |
| 1159 | vcpu->arch.purr = set_reg_val(id, *val); |
| 1160 | break; |
| 1161 | case KVM_REG_PPC_SPURR: |
| 1162 | vcpu->arch.spurr = set_reg_val(id, *val); |
| 1163 | break; |
| 1164 | case KVM_REG_PPC_AMR: |
| 1165 | vcpu->arch.amr = set_reg_val(id, *val); |
| 1166 | break; |
| 1167 | case KVM_REG_PPC_UAMOR: |
| 1168 | vcpu->arch.uamor = set_reg_val(id, *val); |
| 1169 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1170 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1171 | i = id - KVM_REG_PPC_MMCR0; |
| 1172 | vcpu->arch.mmcr[i] = set_reg_val(id, *val); |
| 1173 | break; |
| 1174 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: |
| 1175 | i = id - KVM_REG_PPC_PMC1; |
| 1176 | vcpu->arch.pmc[i] = set_reg_val(id, *val); |
| 1177 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1178 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
| 1179 | i = id - KVM_REG_PPC_SPMC1; |
| 1180 | vcpu->arch.spmc[i] = set_reg_val(id, *val); |
| 1181 | break; |
Paul Mackerras | 1494178 | 2013-09-06 13:11:18 +1000 | [diff] [blame] | 1182 | case KVM_REG_PPC_SIAR: |
| 1183 | vcpu->arch.siar = set_reg_val(id, *val); |
| 1184 | break; |
| 1185 | case KVM_REG_PPC_SDAR: |
| 1186 | vcpu->arch.sdar = set_reg_val(id, *val); |
| 1187 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1188 | case KVM_REG_PPC_SIER: |
| 1189 | vcpu->arch.sier = set_reg_val(id, *val); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1190 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1191 | case KVM_REG_PPC_IAMR: |
| 1192 | vcpu->arch.iamr = set_reg_val(id, *val); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1193 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1194 | case KVM_REG_PPC_PSPB: |
| 1195 | vcpu->arch.pspb = set_reg_val(id, *val); |
| 1196 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1197 | case KVM_REG_PPC_DPDES: |
| 1198 | vcpu->arch.vcore->dpdes = set_reg_val(id, *val); |
| 1199 | break; |
| 1200 | case KVM_REG_PPC_DAWR: |
| 1201 | vcpu->arch.dawr = set_reg_val(id, *val); |
| 1202 | break; |
| 1203 | case KVM_REG_PPC_DAWRX: |
| 1204 | vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP; |
| 1205 | break; |
| 1206 | case KVM_REG_PPC_CIABR: |
| 1207 | vcpu->arch.ciabr = set_reg_val(id, *val); |
| 1208 | /* Don't allow setting breakpoints in hypervisor code */ |
| 1209 | if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER) |
| 1210 | vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */ |
| 1211 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1212 | case KVM_REG_PPC_CSIGR: |
| 1213 | vcpu->arch.csigr = set_reg_val(id, *val); |
| 1214 | break; |
| 1215 | case KVM_REG_PPC_TACR: |
| 1216 | vcpu->arch.tacr = set_reg_val(id, *val); |
| 1217 | break; |
| 1218 | case KVM_REG_PPC_TCSCR: |
| 1219 | vcpu->arch.tcscr = set_reg_val(id, *val); |
| 1220 | break; |
| 1221 | case KVM_REG_PPC_PID: |
| 1222 | vcpu->arch.pid = set_reg_val(id, *val); |
| 1223 | break; |
| 1224 | case KVM_REG_PPC_ACOP: |
| 1225 | vcpu->arch.acop = set_reg_val(id, *val); |
| 1226 | break; |
| 1227 | case KVM_REG_PPC_WORT: |
| 1228 | vcpu->arch.wort = set_reg_val(id, *val); |
| 1229 | break; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1230 | case KVM_REG_PPC_VPA_ADDR: |
| 1231 | addr = set_reg_val(id, *val); |
| 1232 | r = -EINVAL; |
| 1233 | if (!addr && (vcpu->arch.slb_shadow.next_gpa || |
| 1234 | vcpu->arch.dtl.next_gpa)) |
| 1235 | break; |
| 1236 | r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca)); |
| 1237 | break; |
| 1238 | case KVM_REG_PPC_VPA_SLB: |
| 1239 | addr = val->vpaval.addr; |
| 1240 | len = val->vpaval.length; |
| 1241 | r = -EINVAL; |
| 1242 | if (addr && !vcpu->arch.vpa.next_gpa) |
| 1243 | break; |
| 1244 | r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len); |
| 1245 | break; |
| 1246 | case KVM_REG_PPC_VPA_DTL: |
| 1247 | addr = val->vpaval.addr; |
| 1248 | len = val->vpaval.length; |
| 1249 | r = -EINVAL; |
Paul Mackerras | 9f8c8c7 | 2012-10-15 01:18:37 +0000 | [diff] [blame] | 1250 | if (addr && (len < sizeof(struct dtl_entry) || |
| 1251 | !vcpu->arch.vpa.next_gpa)) |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1252 | break; |
| 1253 | len -= len % sizeof(struct dtl_entry); |
| 1254 | r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len); |
| 1255 | break; |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 1256 | case KVM_REG_PPC_TB_OFFSET: |
| 1257 | /* round up to multiple of 2^24 */ |
| 1258 | vcpu->arch.vcore->tb_offset = |
| 1259 | ALIGN(set_reg_val(id, *val), 1UL << 24); |
| 1260 | break; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1261 | case KVM_REG_PPC_LPCR: |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1262 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true); |
| 1263 | break; |
| 1264 | case KVM_REG_PPC_LPCR_64: |
| 1265 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false); |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1266 | break; |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 1267 | case KVM_REG_PPC_PPR: |
| 1268 | vcpu->arch.ppr = set_reg_val(id, *val); |
| 1269 | break; |
Michael Neuling | a7d80d0 | 2014-03-25 10:47:03 +1100 | [diff] [blame] | 1270 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1271 | case KVM_REG_PPC_TFHAR: |
| 1272 | vcpu->arch.tfhar = set_reg_val(id, *val); |
| 1273 | break; |
| 1274 | case KVM_REG_PPC_TFIAR: |
| 1275 | vcpu->arch.tfiar = set_reg_val(id, *val); |
| 1276 | break; |
| 1277 | case KVM_REG_PPC_TEXASR: |
| 1278 | vcpu->arch.texasr = set_reg_val(id, *val); |
| 1279 | break; |
| 1280 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: |
| 1281 | i = id - KVM_REG_PPC_TM_GPR0; |
| 1282 | vcpu->arch.gpr_tm[i] = set_reg_val(id, *val); |
| 1283 | break; |
| 1284 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: |
| 1285 | { |
| 1286 | int j; |
| 1287 | i = id - KVM_REG_PPC_TM_VSR0; |
| 1288 | if (i < 32) |
| 1289 | for (j = 0; j < TS_FPRWIDTH; j++) |
| 1290 | vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j]; |
| 1291 | else |
| 1292 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1293 | vcpu->arch.vr_tm.vr[i-32] = val->vval; |
| 1294 | else |
| 1295 | r = -ENXIO; |
| 1296 | break; |
| 1297 | } |
| 1298 | case KVM_REG_PPC_TM_CR: |
| 1299 | vcpu->arch.cr_tm = set_reg_val(id, *val); |
| 1300 | break; |
| 1301 | case KVM_REG_PPC_TM_LR: |
| 1302 | vcpu->arch.lr_tm = set_reg_val(id, *val); |
| 1303 | break; |
| 1304 | case KVM_REG_PPC_TM_CTR: |
| 1305 | vcpu->arch.ctr_tm = set_reg_val(id, *val); |
| 1306 | break; |
| 1307 | case KVM_REG_PPC_TM_FPSCR: |
| 1308 | vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val); |
| 1309 | break; |
| 1310 | case KVM_REG_PPC_TM_AMR: |
| 1311 | vcpu->arch.amr_tm = set_reg_val(id, *val); |
| 1312 | break; |
| 1313 | case KVM_REG_PPC_TM_PPR: |
| 1314 | vcpu->arch.ppr_tm = set_reg_val(id, *val); |
| 1315 | break; |
| 1316 | case KVM_REG_PPC_TM_VRSAVE: |
| 1317 | vcpu->arch.vrsave_tm = set_reg_val(id, *val); |
| 1318 | break; |
| 1319 | case KVM_REG_PPC_TM_VSCR: |
| 1320 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1321 | vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val); |
| 1322 | else |
| 1323 | r = - ENXIO; |
| 1324 | break; |
| 1325 | case KVM_REG_PPC_TM_DSCR: |
| 1326 | vcpu->arch.dscr_tm = set_reg_val(id, *val); |
| 1327 | break; |
| 1328 | case KVM_REG_PPC_TM_TAR: |
| 1329 | vcpu->arch.tar_tm = set_reg_val(id, *val); |
| 1330 | break; |
| 1331 | #endif |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 1332 | case KVM_REG_PPC_ARCH_COMPAT: |
| 1333 | r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val)); |
| 1334 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1335 | default: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1336 | r = -EINVAL; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | |
| 1340 | return r; |
| 1341 | } |
| 1342 | |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 1343 | static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core) |
| 1344 | { |
| 1345 | struct kvmppc_vcore *vcore; |
| 1346 | |
| 1347 | vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL); |
| 1348 | |
| 1349 | if (vcore == NULL) |
| 1350 | return NULL; |
| 1351 | |
| 1352 | INIT_LIST_HEAD(&vcore->runnable_threads); |
| 1353 | spin_lock_init(&vcore->lock); |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 1354 | spin_lock_init(&vcore->stoltb_lock); |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 1355 | init_waitqueue_head(&vcore->wq); |
| 1356 | vcore->preempt_tb = TB_NIL; |
| 1357 | vcore->lpcr = kvm->arch.lpcr; |
| 1358 | vcore->first_vcpuid = core * threads_per_subcore; |
| 1359 | vcore->kvm = kvm; |
| 1360 | |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 1361 | vcore->mpp_buffer_is_valid = false; |
| 1362 | |
| 1363 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 1364 | vcore->mpp_buffer = (void *)__get_free_pages( |
| 1365 | GFP_KERNEL|__GFP_ZERO, |
| 1366 | MPP_BUFFER_ORDER); |
| 1367 | |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 1368 | return vcore; |
| 1369 | } |
| 1370 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1371 | static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm, |
| 1372 | unsigned int id) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1373 | { |
| 1374 | struct kvm_vcpu *vcpu; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1375 | int err = -EINVAL; |
| 1376 | int core; |
| 1377 | struct kvmppc_vcore *vcore; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1378 | |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1379 | core = id / threads_per_subcore; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1380 | if (core >= KVM_MAX_VCORES) |
| 1381 | goto out; |
| 1382 | |
| 1383 | err = -ENOMEM; |
Sasha Levin | 6b75e6b | 2011-12-07 10:24:56 +0200 | [diff] [blame] | 1384 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1385 | if (!vcpu) |
| 1386 | goto out; |
| 1387 | |
| 1388 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 1389 | if (err) |
| 1390 | goto free_vcpu; |
| 1391 | |
| 1392 | vcpu->arch.shared = &vcpu->arch.shregs; |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1393 | #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE |
| 1394 | /* |
| 1395 | * The shared struct is never shared on HV, |
| 1396 | * so we can always use host endianness |
| 1397 | */ |
| 1398 | #ifdef __BIG_ENDIAN__ |
| 1399 | vcpu->arch.shared_big_endian = true; |
| 1400 | #else |
| 1401 | vcpu->arch.shared_big_endian = false; |
| 1402 | #endif |
| 1403 | #endif |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1404 | vcpu->arch.mmcr[0] = MMCR0_FC; |
| 1405 | vcpu->arch.ctrl = CTRL_RUNLATCH; |
| 1406 | /* default to host PVR, since we can't spoof it */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1407 | kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR)); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 1408 | spin_lock_init(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 1409 | spin_lock_init(&vcpu->arch.tbacct_lock); |
| 1410 | vcpu->arch.busy_preempt = TB_NIL; |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1411 | vcpu->arch.intr_msr = MSR_SF | MSR_ME; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1412 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1413 | kvmppc_mmu_book3s_hv_init(vcpu); |
| 1414 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1415 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1416 | |
| 1417 | init_waitqueue_head(&vcpu->arch.cpu_run); |
| 1418 | |
| 1419 | mutex_lock(&kvm->lock); |
| 1420 | vcore = kvm->arch.vcores[core]; |
| 1421 | if (!vcore) { |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 1422 | vcore = kvmppc_vcore_create(kvm, core); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1423 | kvm->arch.vcores[core] = vcore; |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 1424 | kvm->arch.online_vcores++; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1425 | } |
| 1426 | mutex_unlock(&kvm->lock); |
| 1427 | |
| 1428 | if (!vcore) |
| 1429 | goto free_vcpu; |
| 1430 | |
| 1431 | spin_lock(&vcore->lock); |
| 1432 | ++vcore->num_threads; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1433 | spin_unlock(&vcore->lock); |
| 1434 | vcpu->arch.vcore = vcore; |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1435 | vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1436 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1437 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
| 1438 | kvmppc_sanity_check(vcpu); |
| 1439 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1440 | return vcpu; |
| 1441 | |
| 1442 | free_vcpu: |
Sasha Levin | 6b75e6b | 2011-12-07 10:24:56 +0200 | [diff] [blame] | 1443 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1444 | out: |
| 1445 | return ERR_PTR(err); |
| 1446 | } |
| 1447 | |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1448 | static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa) |
| 1449 | { |
| 1450 | if (vpa->pinned_addr) |
| 1451 | kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa, |
| 1452 | vpa->dirty); |
| 1453 | } |
| 1454 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1455 | static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1456 | { |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 1457 | spin_lock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1458 | unpin_vpa(vcpu->kvm, &vcpu->arch.dtl); |
| 1459 | unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow); |
| 1460 | unpin_vpa(vcpu->kvm, &vcpu->arch.vpa); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 1461 | spin_unlock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1462 | kvm_vcpu_uninit(vcpu); |
Sasha Levin | 6b75e6b | 2011-12-07 10:24:56 +0200 | [diff] [blame] | 1463 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1466 | static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu) |
| 1467 | { |
| 1468 | /* Indicate we want to get back into the guest */ |
| 1469 | return 1; |
| 1470 | } |
| 1471 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1472 | static void kvmppc_set_timer(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1473 | { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1474 | unsigned long dec_nsec, now; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1475 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1476 | now = get_tb(); |
| 1477 | if (now > vcpu->arch.dec_expires) { |
| 1478 | /* decrementer has already gone negative */ |
| 1479 | kvmppc_core_queue_dec(vcpu); |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 1480 | kvmppc_core_prepare_to_enter(vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1481 | return; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1482 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1483 | dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC |
| 1484 | / tb_ticks_per_sec; |
| 1485 | hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec), |
| 1486 | HRTIMER_MODE_REL); |
| 1487 | vcpu->arch.timer_running = 1; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1490 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1491 | { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1492 | vcpu->arch.ceded = 0; |
| 1493 | if (vcpu->arch.timer_running) { |
| 1494 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
| 1495 | vcpu->arch.timer_running = 0; |
| 1496 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1499 | extern void __kvmppc_vcore_entry(void); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1500 | |
| 1501 | static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, |
| 1502 | struct kvm_vcpu *vcpu) |
| 1503 | { |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 1504 | u64 now; |
| 1505 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1506 | if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
| 1507 | return; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 1508 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 1509 | now = mftb(); |
| 1510 | vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) - |
| 1511 | vcpu->arch.stolen_logged; |
| 1512 | vcpu->arch.busy_preempt = now; |
| 1513 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 1514 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1515 | --vc->n_runnable; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1516 | list_del(&vcpu->arch.run_list); |
| 1517 | } |
| 1518 | |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 1519 | static int kvmppc_grab_hwthread(int cpu) |
| 1520 | { |
| 1521 | struct paca_struct *tpaca; |
Paul Mackerras | b754c73 | 2014-09-02 16:14:42 +1000 | [diff] [blame] | 1522 | long timeout = 10000; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 1523 | |
| 1524 | tpaca = &paca[cpu]; |
| 1525 | |
| 1526 | /* Ensure the thread won't go into the kernel if it wakes */ |
| 1527 | tpaca->kvm_hstate.hwthread_req = 1; |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1528 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 1529 | |
| 1530 | /* |
| 1531 | * If the thread is already executing in the kernel (e.g. handling |
| 1532 | * a stray interrupt), wait for it to get back to nap mode. |
| 1533 | * The smp_mb() is to ensure that our setting of hwthread_req |
| 1534 | * is visible before we look at hwthread_state, so if this |
| 1535 | * races with the code at system_reset_pSeries and the thread |
| 1536 | * misses our setting of hwthread_req, we are sure to see its |
| 1537 | * setting of hwthread_state, and vice versa. |
| 1538 | */ |
| 1539 | smp_mb(); |
| 1540 | while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) { |
| 1541 | if (--timeout <= 0) { |
| 1542 | pr_err("KVM: couldn't grab cpu %d\n", cpu); |
| 1543 | return -EBUSY; |
| 1544 | } |
| 1545 | udelay(1); |
| 1546 | } |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | static void kvmppc_release_hwthread(int cpu) |
| 1551 | { |
| 1552 | struct paca_struct *tpaca; |
| 1553 | |
| 1554 | tpaca = &paca[cpu]; |
| 1555 | tpaca->kvm_hstate.hwthread_req = 0; |
| 1556 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
| 1557 | } |
| 1558 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1559 | static void kvmppc_start_thread(struct kvm_vcpu *vcpu) |
| 1560 | { |
| 1561 | int cpu; |
| 1562 | struct paca_struct *tpaca; |
| 1563 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 1564 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1565 | if (vcpu->arch.timer_running) { |
| 1566 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
| 1567 | vcpu->arch.timer_running = 0; |
| 1568 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1569 | cpu = vc->pcpu + vcpu->arch.ptid; |
| 1570 | tpaca = &paca[cpu]; |
| 1571 | tpaca->kvm_hstate.kvm_vcpu = vcpu; |
| 1572 | tpaca->kvm_hstate.kvm_vcore = vc; |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1573 | tpaca->kvm_hstate.ptid = vcpu->arch.ptid; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1574 | vcpu->cpu = vc->pcpu; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1575 | smp_wmb(); |
Michael Neuling | 251da03 | 2011-11-10 16:03:20 +0000 | [diff] [blame] | 1576 | #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP) |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1577 | if (cpu != smp_processor_id()) { |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1578 | xics_wake_cpu(cpu); |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1579 | if (vcpu->arch.ptid) |
| 1580 | ++vc->n_woken; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1581 | } |
| 1582 | #endif |
| 1583 | } |
| 1584 | |
| 1585 | static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc) |
| 1586 | { |
| 1587 | int i; |
| 1588 | |
| 1589 | HMT_low(); |
| 1590 | i = 0; |
| 1591 | while (vc->nap_count < vc->n_woken) { |
| 1592 | if (++i >= 1000000) { |
| 1593 | pr_err("kvmppc_wait_for_nap timeout %d %d\n", |
| 1594 | vc->nap_count, vc->n_woken); |
| 1595 | break; |
| 1596 | } |
| 1597 | cpu_relax(); |
| 1598 | } |
| 1599 | HMT_medium(); |
| 1600 | } |
| 1601 | |
| 1602 | /* |
| 1603 | * Check that we are on thread 0 and that any other threads in |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1604 | * this core are off-line. Then grab the threads so they can't |
| 1605 | * enter the kernel. |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1606 | */ |
| 1607 | static int on_primary_thread(void) |
| 1608 | { |
| 1609 | int cpu = smp_processor_id(); |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1610 | int thr; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1611 | |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1612 | /* Are we on a primary subcore? */ |
| 1613 | if (cpu_thread_in_subcore(cpu)) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1614 | return 0; |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1615 | |
| 1616 | thr = 0; |
| 1617 | while (++thr < threads_per_subcore) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1618 | if (cpu_online(cpu + thr)) |
| 1619 | return 0; |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1620 | |
| 1621 | /* Grab all hw threads so they can't go into the kernel */ |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1622 | for (thr = 1; thr < threads_per_subcore; ++thr) { |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1623 | if (kvmppc_grab_hwthread(cpu + thr)) { |
| 1624 | /* Couldn't grab one; let the others go */ |
| 1625 | do { |
| 1626 | kvmppc_release_hwthread(cpu + thr); |
| 1627 | } while (--thr > 0); |
| 1628 | return 0; |
| 1629 | } |
| 1630 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1631 | return 1; |
| 1632 | } |
| 1633 | |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 1634 | static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc) |
| 1635 | { |
| 1636 | phys_addr_t phy_addr, mpp_addr; |
| 1637 | |
| 1638 | phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer); |
| 1639 | mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK; |
| 1640 | |
| 1641 | mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT); |
| 1642 | logmpp(mpp_addr | PPC_LOGMPP_LOG_L2); |
| 1643 | |
| 1644 | vc->mpp_buffer_is_valid = true; |
| 1645 | } |
| 1646 | |
| 1647 | static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc) |
| 1648 | { |
| 1649 | phys_addr_t phy_addr, mpp_addr; |
| 1650 | |
| 1651 | phy_addr = virt_to_phys(vc->mpp_buffer); |
| 1652 | mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK; |
| 1653 | |
| 1654 | /* We must abort any in-progress save operations to ensure |
| 1655 | * the table is valid so that prefetch engine knows when to |
| 1656 | * stop prefetching. */ |
| 1657 | logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT); |
| 1658 | mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE); |
| 1659 | } |
| 1660 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1661 | /* |
| 1662 | * Run a set of guest threads on a physical core. |
| 1663 | * Called with vc->lock held. |
| 1664 | */ |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1665 | static void kvmppc_run_core(struct kvmppc_vcore *vc) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1666 | { |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1667 | struct kvm_vcpu *vcpu, *vnext; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1668 | long ret; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1669 | u64 now; |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1670 | int i, need_vpa_update; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1671 | int srcu_idx; |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1672 | struct kvm_vcpu *vcpus_to_update[threads_per_core]; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1673 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1674 | /* don't start if any threads have a signal pending */ |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 1675 | need_vpa_update = 0; |
| 1676 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1677 | if (signal_pending(vcpu->arch.run_task)) |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1678 | return; |
| 1679 | if (vcpu->arch.vpa.update_pending || |
| 1680 | vcpu->arch.slb_shadow.update_pending || |
| 1681 | vcpu->arch.dtl.update_pending) |
| 1682 | vcpus_to_update[need_vpa_update++] = vcpu; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | /* |
| 1686 | * Initialize *vc, in particular vc->vcore_state, so we can |
| 1687 | * drop the vcore lock if necessary. |
| 1688 | */ |
| 1689 | vc->n_woken = 0; |
| 1690 | vc->nap_count = 0; |
| 1691 | vc->entry_exit_count = 0; |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 1692 | vc->preempt_tb = TB_NIL; |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 1693 | vc->vcore_state = VCORE_STARTING; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 1694 | vc->in_guest = 0; |
| 1695 | vc->napping_threads = 0; |
| 1696 | |
| 1697 | /* |
| 1698 | * Updating any of the vpas requires calling kvmppc_pin_guest_page, |
| 1699 | * which can't be called with any spinlocks held. |
| 1700 | */ |
| 1701 | if (need_vpa_update) { |
| 1702 | spin_unlock(&vc->lock); |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1703 | for (i = 0; i < need_vpa_update; ++i) |
| 1704 | kvmppc_update_vpas(vcpus_to_update[i]); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 1705 | spin_lock(&vc->lock); |
| 1706 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1707 | |
| 1708 | /* |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1709 | * Make sure we are running on primary threads, and that secondary |
| 1710 | * threads are offline. Also check if the number of threads in this |
| 1711 | * guest are greater than the current system threads per guest. |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1712 | */ |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1713 | if ((threads_per_core > 1) && |
| 1714 | ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) { |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1715 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) |
| 1716 | vcpu->arch.ret = -EBUSY; |
| 1717 | goto out; |
| 1718 | } |
| 1719 | |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1720 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1721 | vc->pcpu = smp_processor_id(); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 1722 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1723 | kvmppc_start_thread(vcpu); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 1724 | kvmppc_create_dtl_entry(vcpu, vc); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1725 | trace_kvm_guest_enter(vcpu); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 1726 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1727 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1728 | /* Set this explicitly in case thread 0 doesn't have a vcpu */ |
| 1729 | get_paca()->kvm_hstate.kvm_vcore = vc; |
| 1730 | get_paca()->kvm_hstate.ptid = 0; |
| 1731 | |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 1732 | vc->vcore_state = VCORE_RUNNING; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1733 | preempt_disable(); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1734 | |
| 1735 | trace_kvmppc_run_core(vc, 0); |
| 1736 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1737 | spin_unlock(&vc->lock); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1738 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1739 | kvm_guest_enter(); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1740 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1741 | srcu_idx = srcu_read_lock(&vc->kvm->srcu); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1742 | |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 1743 | if (vc->mpp_buffer_is_valid) |
| 1744 | kvmppc_start_restoring_l2_cache(vc); |
| 1745 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1746 | __kvmppc_vcore_entry(); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1747 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1748 | spin_lock(&vc->lock); |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 1749 | |
| 1750 | if (vc->mpp_buffer) |
| 1751 | kvmppc_start_saving_l2_cache(vc); |
| 1752 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1753 | /* disable sending of IPIs on virtual external irqs */ |
| 1754 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) |
| 1755 | vcpu->cpu = -1; |
| 1756 | /* wait for secondary threads to finish writing their state to memory */ |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1757 | if (vc->nap_count < vc->n_woken) |
| 1758 | kvmppc_wait_for_nap(vc); |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1759 | for (i = 0; i < threads_per_subcore; ++i) |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 1760 | kvmppc_release_hwthread(vc->pcpu + i); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1761 | /* prevent other vcpu threads from doing kvmppc_start_thread() now */ |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1762 | vc->vcore_state = VCORE_EXITING; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1763 | spin_unlock(&vc->lock); |
| 1764 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1765 | srcu_read_unlock(&vc->kvm->srcu, srcu_idx); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 1766 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1767 | /* make sure updates to secondary vcpu structs are visible now */ |
| 1768 | smp_mb(); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1769 | kvm_guest_exit(); |
| 1770 | |
| 1771 | preempt_enable(); |
Takuya Yoshikawa | c08ac06 | 2013-12-13 15:07:21 +0900 | [diff] [blame] | 1772 | cond_resched(); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1773 | |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1774 | spin_lock(&vc->lock); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1775 | now = get_tb(); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1776 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { |
| 1777 | /* cancel pending dec exception if dec is positive */ |
| 1778 | if (now < vcpu->arch.dec_expires && |
| 1779 | kvmppc_core_pending_dec(vcpu)) |
| 1780 | kvmppc_core_dequeue_dec(vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1781 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1782 | trace_kvm_guest_exit(vcpu); |
| 1783 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1784 | ret = RESUME_GUEST; |
| 1785 | if (vcpu->arch.trap) |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1786 | ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu, |
| 1787 | vcpu->arch.run_task); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1788 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1789 | vcpu->arch.ret = ret; |
| 1790 | vcpu->arch.trap = 0; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1791 | |
| 1792 | if (vcpu->arch.ceded) { |
Greg Kurz | e59d24e | 2014-02-06 17:36:56 +0100 | [diff] [blame] | 1793 | if (!is_kvmppc_resume_guest(ret)) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1794 | kvmppc_end_cede(vcpu); |
| 1795 | else |
| 1796 | kvmppc_set_timer(vcpu); |
| 1797 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1798 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1799 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1800 | out: |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1801 | vc->vcore_state = VCORE_INACTIVE; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1802 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, |
| 1803 | arch.run_list) { |
Greg Kurz | e59d24e | 2014-02-06 17:36:56 +0100 | [diff] [blame] | 1804 | if (!is_kvmppc_resume_guest(vcpu->arch.ret)) { |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1805 | kvmppc_remove_runnable(vc, vcpu); |
| 1806 | wake_up(&vcpu->arch.cpu_run); |
| 1807 | } |
| 1808 | } |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1809 | |
| 1810 | trace_kvmppc_run_core(vc, 1); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1813 | /* |
| 1814 | * Wait for some other vcpu thread to execute us, and |
| 1815 | * wake us up when we need to handle something in the host. |
| 1816 | */ |
| 1817 | 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] | 1818 | { |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1819 | DEFINE_WAIT(wait); |
| 1820 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1821 | prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state); |
| 1822 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) |
| 1823 | schedule(); |
| 1824 | finish_wait(&vcpu->arch.cpu_run, &wait); |
| 1825 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1826 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1827 | /* |
| 1828 | * All the vcpus in this vcore are idle, so wait for a decrementer |
| 1829 | * or external interrupt to one of the vcpus. vc->lock is held. |
| 1830 | */ |
| 1831 | static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) |
| 1832 | { |
Suresh E. Warrier | 1bc5d59 | 2014-11-03 15:52:00 +1100 | [diff] [blame] | 1833 | struct kvm_vcpu *vcpu; |
| 1834 | int do_sleep = 1; |
| 1835 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1836 | DEFINE_WAIT(wait); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1837 | |
| 1838 | prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE); |
Suresh E. Warrier | 1bc5d59 | 2014-11-03 15:52:00 +1100 | [diff] [blame] | 1839 | |
| 1840 | /* |
| 1841 | * Check one last time for pending exceptions and ceded state after |
| 1842 | * we put ourselves on the wait queue |
| 1843 | */ |
| 1844 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { |
| 1845 | if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) { |
| 1846 | do_sleep = 0; |
| 1847 | break; |
| 1848 | } |
| 1849 | } |
| 1850 | |
| 1851 | if (!do_sleep) { |
| 1852 | finish_wait(&vc->wq, &wait); |
| 1853 | return; |
| 1854 | } |
| 1855 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1856 | vc->vcore_state = VCORE_SLEEPING; |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1857 | trace_kvmppc_vcore_blocked(vc, 0); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1858 | spin_unlock(&vc->lock); |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1859 | schedule(); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1860 | finish_wait(&vc->wq, &wait); |
| 1861 | spin_lock(&vc->lock); |
| 1862 | vc->vcore_state = VCORE_INACTIVE; |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1863 | trace_kvmppc_vcore_blocked(vc, 1); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 1867 | { |
| 1868 | int n_ceded; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1869 | struct kvmppc_vcore *vc; |
| 1870 | struct kvm_vcpu *v, *vn; |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 1871 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1872 | trace_kvmppc_run_vcpu_enter(vcpu); |
| 1873 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1874 | kvm_run->exit_reason = 0; |
| 1875 | vcpu->arch.ret = RESUME_GUEST; |
| 1876 | vcpu->arch.trap = 0; |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 1877 | kvmppc_update_vpas(vcpu); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1878 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1879 | /* |
| 1880 | * Synchronize with other threads in this virtual core |
| 1881 | */ |
| 1882 | vc = vcpu->arch.vcore; |
| 1883 | spin_lock(&vc->lock); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1884 | vcpu->arch.ceded = 0; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1885 | vcpu->arch.run_task = current; |
| 1886 | vcpu->arch.kvm_run = kvm_run; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 1887 | vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb()); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1888 | vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 1889 | vcpu->arch.busy_preempt = TB_NIL; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1890 | list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads); |
| 1891 | ++vc->n_runnable; |
| 1892 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1893 | /* |
| 1894 | * This happens the first time this is called for a vcpu. |
| 1895 | * If the vcore is already running, we may be able to start |
| 1896 | * this thread straight away and have it join in. |
| 1897 | */ |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1898 | if (!signal_pending(current)) { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1899 | if (vc->vcore_state == VCORE_RUNNING && |
| 1900 | VCORE_EXIT_COUNT(vc) == 0) { |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 1901 | kvmppc_create_dtl_entry(vcpu, vc); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1902 | kvmppc_start_thread(vcpu); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1903 | trace_kvm_guest_enter(vcpu); |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1904 | } else if (vc->vcore_state == VCORE_SLEEPING) { |
| 1905 | wake_up(&vc->wq); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1906 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1907 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1908 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1909 | |
| 1910 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
| 1911 | !signal_pending(current)) { |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1912 | if (vc->vcore_state != VCORE_INACTIVE) { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1913 | spin_unlock(&vc->lock); |
| 1914 | kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE); |
| 1915 | spin_lock(&vc->lock); |
| 1916 | continue; |
| 1917 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1918 | list_for_each_entry_safe(v, vn, &vc->runnable_threads, |
| 1919 | arch.run_list) { |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 1920 | kvmppc_core_prepare_to_enter(v); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1921 | if (signal_pending(v->arch.run_task)) { |
| 1922 | kvmppc_remove_runnable(vc, v); |
| 1923 | v->stat.signal_exits++; |
| 1924 | v->arch.kvm_run->exit_reason = KVM_EXIT_INTR; |
| 1925 | v->arch.ret = -EINTR; |
| 1926 | wake_up(&v->arch.cpu_run); |
| 1927 | } |
| 1928 | } |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1929 | if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
| 1930 | break; |
| 1931 | vc->runner = vcpu; |
| 1932 | n_ceded = 0; |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 1933 | list_for_each_entry(v, &vc->runnable_threads, arch.run_list) { |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1934 | if (!v->arch.pending_exceptions) |
| 1935 | n_ceded += v->arch.ceded; |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 1936 | else |
| 1937 | v->arch.ceded = 0; |
| 1938 | } |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1939 | if (n_ceded == vc->n_runnable) |
| 1940 | kvmppc_vcore_blocked(vc); |
| 1941 | else |
| 1942 | kvmppc_run_core(vc); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 1943 | vc->runner = NULL; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1946 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
| 1947 | (vc->vcore_state == VCORE_RUNNING || |
| 1948 | vc->vcore_state == VCORE_EXITING)) { |
| 1949 | spin_unlock(&vc->lock); |
| 1950 | kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE); |
| 1951 | spin_lock(&vc->lock); |
| 1952 | } |
| 1953 | |
| 1954 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
| 1955 | kvmppc_remove_runnable(vc, vcpu); |
| 1956 | vcpu->stat.signal_exits++; |
| 1957 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 1958 | vcpu->arch.ret = -EINTR; |
| 1959 | } |
| 1960 | |
| 1961 | if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) { |
| 1962 | /* Wake up some vcpu to run the core */ |
| 1963 | v = list_first_entry(&vc->runnable_threads, |
| 1964 | struct kvm_vcpu, arch.run_list); |
| 1965 | wake_up(&v->arch.cpu_run); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1966 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1967 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 1968 | trace_kvmppc_run_vcpu_exit(vcpu, kvm_run); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1969 | spin_unlock(&vc->lock); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1970 | return vcpu->arch.ret; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1973 | static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1974 | { |
| 1975 | int r; |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1976 | int srcu_idx; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1977 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1978 | if (!vcpu->arch.sane) { |
| 1979 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1980 | return -EINVAL; |
| 1981 | } |
| 1982 | |
Scott Wood | 25051b5a | 2011-11-08 18:23:23 -0600 | [diff] [blame] | 1983 | kvmppc_core_prepare_to_enter(vcpu); |
| 1984 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1985 | /* No need to go into the guest when all we'll do is come back out */ |
| 1986 | if (signal_pending(current)) { |
| 1987 | run->exit_reason = KVM_EXIT_INTR; |
| 1988 | return -EINTR; |
| 1989 | } |
| 1990 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 1991 | atomic_inc(&vcpu->kvm->arch.vcpus_running); |
| 1992 | /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */ |
| 1993 | smp_mb(); |
| 1994 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame^] | 1995 | /* On the first time here, set up HTAB and VRMA */ |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 1996 | if (!vcpu->kvm->arch.rma_setup_done) { |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 1997 | r = kvmppc_hv_setup_htab_rma(vcpu); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 1998 | if (r) |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 1999 | goto out; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2000 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2001 | |
| 2002 | flush_fp_to_thread(current); |
| 2003 | flush_altivec_to_thread(current); |
| 2004 | flush_vsx_to_thread(current); |
| 2005 | vcpu->arch.wqp = &vcpu->arch.vcore->wq; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 2006 | vcpu->arch.pgdir = current->mm->pgd; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2007 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2008 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2009 | do { |
| 2010 | r = kvmppc_run_vcpu(run, vcpu); |
| 2011 | |
| 2012 | if (run->exit_reason == KVM_EXIT_PAPR_HCALL && |
| 2013 | !(vcpu->arch.shregs.msr & MSR_PR)) { |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2014 | trace_kvm_hcall_enter(vcpu); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2015 | r = kvmppc_pseries_do_hcall(vcpu); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2016 | trace_kvm_hcall_exit(vcpu, r); |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 2017 | kvmppc_core_prepare_to_enter(vcpu); |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 2018 | } else if (r == RESUME_PAGE_FAULT) { |
| 2019 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 2020 | r = kvmppc_book3s_hv_page_fault(run, vcpu, |
| 2021 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); |
| 2022 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2023 | } |
Greg Kurz | e59d24e | 2014-02-06 17:36:56 +0100 | [diff] [blame] | 2024 | } while (is_kvmppc_resume_guest(r)); |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2025 | |
| 2026 | out: |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2027 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2028 | atomic_dec(&vcpu->kvm->arch.vcpus_running); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2029 | return r; |
| 2030 | } |
| 2031 | |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2032 | static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps, |
| 2033 | int linux_psize) |
| 2034 | { |
| 2035 | struct mmu_psize_def *def = &mmu_psize_defs[linux_psize]; |
| 2036 | |
| 2037 | if (!def->shift) |
| 2038 | return; |
| 2039 | (*sps)->page_shift = def->shift; |
| 2040 | (*sps)->slb_enc = def->sllp; |
| 2041 | (*sps)->enc[0].page_shift = def->shift; |
Aneesh Kumar K.V | b1022fb | 2013-04-28 09:37:35 +0000 | [diff] [blame] | 2042 | (*sps)->enc[0].pte_enc = def->penc[linux_psize]; |
Aneesh Kumar K.V | 1f365bb | 2014-05-06 23:31:36 +0530 | [diff] [blame] | 2043 | /* |
| 2044 | * Add 16MB MPSS support if host supports it |
| 2045 | */ |
| 2046 | if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) { |
| 2047 | (*sps)->enc[1].page_shift = 24; |
| 2048 | (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M]; |
| 2049 | } |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2050 | (*sps)++; |
| 2051 | } |
| 2052 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2053 | static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, |
| 2054 | struct kvm_ppc_smmu_info *info) |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2055 | { |
| 2056 | struct kvm_ppc_one_seg_page_size *sps; |
| 2057 | |
| 2058 | info->flags = KVM_PPC_PAGE_SIZES_REAL; |
| 2059 | if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) |
| 2060 | info->flags |= KVM_PPC_1T_SEGMENTS; |
| 2061 | info->slb_size = mmu_slb_size; |
| 2062 | |
| 2063 | /* We only support these sizes for now, and no muti-size segments */ |
| 2064 | sps = &info->sps[0]; |
| 2065 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K); |
| 2066 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K); |
| 2067 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M); |
| 2068 | |
| 2069 | return 0; |
| 2070 | } |
| 2071 | |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2072 | /* |
| 2073 | * Get (and clear) the dirty memory log for a memory slot. |
| 2074 | */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2075 | static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm, |
| 2076 | struct kvm_dirty_log *log) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2077 | { |
| 2078 | struct kvm_memory_slot *memslot; |
| 2079 | int r; |
| 2080 | unsigned long n; |
| 2081 | |
| 2082 | mutex_lock(&kvm->slots_lock); |
| 2083 | |
| 2084 | r = -EINVAL; |
Alex Williamson | bbacc0c | 2012-12-10 10:33:09 -0700 | [diff] [blame] | 2085 | if (log->slot >= KVM_USER_MEM_SLOTS) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2086 | goto out; |
| 2087 | |
| 2088 | memslot = id_to_memslot(kvm->memslots, log->slot); |
| 2089 | r = -ENOENT; |
| 2090 | if (!memslot->dirty_bitmap) |
| 2091 | goto out; |
| 2092 | |
| 2093 | n = kvm_dirty_bitmap_bytes(memslot); |
| 2094 | memset(memslot->dirty_bitmap, 0, n); |
| 2095 | |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 2096 | r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap); |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2097 | if (r) |
| 2098 | goto out; |
| 2099 | |
| 2100 | r = -EFAULT; |
| 2101 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 2102 | goto out; |
| 2103 | |
| 2104 | r = 0; |
| 2105 | out: |
| 2106 | mutex_unlock(&kvm->slots_lock); |
| 2107 | return r; |
| 2108 | } |
| 2109 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2110 | static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free, |
| 2111 | struct kvm_memory_slot *dont) |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2112 | { |
| 2113 | if (!dont || free->arch.rmap != dont->arch.rmap) { |
| 2114 | vfree(free->arch.rmap); |
| 2115 | free->arch.rmap = NULL; |
| 2116 | } |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2119 | static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot, |
| 2120 | unsigned long npages) |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2121 | { |
| 2122 | slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap)); |
| 2123 | if (!slot->arch.rmap) |
| 2124 | return -ENOMEM; |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2125 | |
| 2126 | return 0; |
| 2127 | } |
| 2128 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2129 | static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm, |
| 2130 | struct kvm_memory_slot *memslot, |
| 2131 | struct kvm_userspace_memory_region *mem) |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2132 | { |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2133 | return 0; |
| 2134 | } |
| 2135 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2136 | static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, |
| 2137 | struct kvm_userspace_memory_region *mem, |
| 2138 | const struct kvm_memory_slot *old) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2139 | { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 2140 | unsigned long npages = mem->memory_size >> PAGE_SHIFT; |
| 2141 | struct kvm_memory_slot *memslot; |
| 2142 | |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 2143 | if (npages && old->npages) { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 2144 | /* |
| 2145 | * If modifying a memslot, reset all the rmap dirty bits. |
| 2146 | * If this is a new memslot, we don't need to do anything |
| 2147 | * since the rmap array starts out as all zeroes, |
| 2148 | * i.e. no pages are dirty. |
| 2149 | */ |
| 2150 | memslot = id_to_memslot(kvm->memslots, mem->slot); |
| 2151 | kvmppc_hv_get_dirty_log(kvm, memslot, NULL); |
| 2152 | } |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 2155 | /* |
| 2156 | * Update LPCR values in kvm->arch and in vcores. |
| 2157 | * Caller must hold kvm->lock. |
| 2158 | */ |
| 2159 | void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask) |
| 2160 | { |
| 2161 | long int i; |
| 2162 | u32 cores_done = 0; |
| 2163 | |
| 2164 | if ((kvm->arch.lpcr & mask) == lpcr) |
| 2165 | return; |
| 2166 | |
| 2167 | kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr; |
| 2168 | |
| 2169 | for (i = 0; i < KVM_MAX_VCORES; ++i) { |
| 2170 | struct kvmppc_vcore *vc = kvm->arch.vcores[i]; |
| 2171 | if (!vc) |
| 2172 | continue; |
| 2173 | spin_lock(&vc->lock); |
| 2174 | vc->lpcr = (vc->lpcr & ~mask) | lpcr; |
| 2175 | spin_unlock(&vc->lock); |
| 2176 | if (++cores_done >= kvm->arch.online_vcores) |
| 2177 | break; |
| 2178 | } |
| 2179 | } |
| 2180 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2181 | static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu) |
| 2182 | { |
| 2183 | return; |
| 2184 | } |
| 2185 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2186 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2187 | { |
| 2188 | int err = 0; |
| 2189 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2190 | unsigned long hva; |
| 2191 | struct kvm_memory_slot *memslot; |
| 2192 | struct vm_area_struct *vma; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 2193 | unsigned long lpcr = 0, senc; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2194 | unsigned long psize, porder; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2195 | int srcu_idx; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2196 | |
| 2197 | mutex_lock(&kvm->lock); |
| 2198 | if (kvm->arch.rma_setup_done) |
| 2199 | goto out; /* another vcpu beat us to it */ |
| 2200 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2201 | /* Allocate hashed page table (if not done already) and reset it */ |
| 2202 | if (!kvm->arch.hpt_virt) { |
| 2203 | err = kvmppc_alloc_hpt(kvm, NULL); |
| 2204 | if (err) { |
| 2205 | pr_err("KVM: Couldn't alloc HPT\n"); |
| 2206 | goto out; |
| 2207 | } |
| 2208 | } |
| 2209 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2210 | /* Look up the memslot for guest physical address 0 */ |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2211 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2212 | memslot = gfn_to_memslot(kvm, 0); |
| 2213 | |
| 2214 | /* We must have some memory at 0 by now */ |
| 2215 | err = -EINVAL; |
| 2216 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2217 | goto out_srcu; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2218 | |
| 2219 | /* Look up the VMA for the start of this memory slot */ |
| 2220 | hva = memslot->userspace_addr; |
| 2221 | down_read(¤t->mm->mmap_sem); |
| 2222 | vma = find_vma(current->mm, hva); |
| 2223 | if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO)) |
| 2224 | goto up_out; |
| 2225 | |
| 2226 | psize = vma_kernel_pagesize(vma); |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 2227 | porder = __ilog2(psize); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2228 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2229 | up_read(¤t->mm->mmap_sem); |
| 2230 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame^] | 2231 | /* We can handle 4k, 64k or 16M pages in the VRMA */ |
| 2232 | err = -EINVAL; |
| 2233 | if (!(psize == 0x1000 || psize == 0x10000 || |
| 2234 | psize == 0x1000000)) |
| 2235 | goto out_srcu; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2236 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame^] | 2237 | /* Update VRMASD field in the LPCR */ |
| 2238 | senc = slb_pgsize_encoding(psize); |
| 2239 | kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | |
| 2240 | (VRMA_VSID << SLB_VSID_SHIFT_1T); |
| 2241 | /* the -4 is to account for senc values starting at 0x10 */ |
| 2242 | lpcr = senc << (LPCR_VRMASD_SH - 4); |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 2243 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame^] | 2244 | /* Create HPTEs in the hash page table for the VRMA */ |
| 2245 | kvmppc_map_vrma(vcpu, memslot, porder); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2246 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame^] | 2247 | kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD); |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 2248 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2249 | /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */ |
| 2250 | smp_wmb(); |
| 2251 | kvm->arch.rma_setup_done = 1; |
| 2252 | err = 0; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2253 | out_srcu: |
| 2254 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2255 | out: |
| 2256 | mutex_unlock(&kvm->lock); |
| 2257 | return err; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2258 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2259 | up_out: |
| 2260 | up_read(¤t->mm->mmap_sem); |
Lai Jiangshan | 505d642 | 2013-03-16 00:50:49 +0800 | [diff] [blame] | 2261 | goto out_srcu; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2264 | static int kvmppc_core_init_vm_hv(struct kvm *kvm) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2265 | { |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2266 | unsigned long lpcr, lpid; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2267 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2268 | /* Allocate the guest's logical partition ID */ |
| 2269 | |
| 2270 | lpid = kvmppc_alloc_lpid(); |
Chen Gang | 5d226ae | 2013-07-22 14:32:35 +0800 | [diff] [blame] | 2271 | if ((long)lpid < 0) |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2272 | return -ENOMEM; |
| 2273 | kvm->arch.lpid = lpid; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2274 | |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 2275 | /* |
| 2276 | * Since we don't flush the TLB when tearing down a VM, |
| 2277 | * and this lpid might have previously been used, |
| 2278 | * make sure we flush on each core before running the new VM. |
| 2279 | */ |
| 2280 | cpumask_setall(&kvm->arch.need_tlb_flush); |
| 2281 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2282 | /* Start out with the default set of hcalls enabled */ |
| 2283 | memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls, |
| 2284 | sizeof(kvm->arch.enabled_hcalls)); |
| 2285 | |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 2286 | kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2287 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame^] | 2288 | /* Init LPCR for virtual RMA mode */ |
| 2289 | kvm->arch.host_lpid = mfspr(SPRN_LPID); |
| 2290 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); |
| 2291 | lpcr &= LPCR_PECE | LPCR_LPES; |
| 2292 | lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | |
| 2293 | LPCR_VPM0 | LPCR_VPM1; |
| 2294 | kvm->arch.vrma_slb_v = SLB_VSID_B_1T | |
| 2295 | (VRMA_VSID << SLB_VSID_SHIFT_1T); |
| 2296 | /* On POWER8 turn on online bit to enable PURR/SPURR */ |
| 2297 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 2298 | lpcr |= LPCR_ONL; |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 2299 | kvm->arch.lpcr = lpcr; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2300 | |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 2301 | /* |
Michael Ellerman | 441c19c | 2014-05-23 18:15:25 +1000 | [diff] [blame] | 2302 | * Track that we now have a HV mode VM active. This blocks secondary |
| 2303 | * CPU threads from coming online. |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 2304 | */ |
Michael Ellerman | 441c19c | 2014-05-23 18:15:25 +1000 | [diff] [blame] | 2305 | kvm_hv_vm_activated(); |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 2306 | |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 2307 | return 0; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 2310 | static void kvmppc_free_vcores(struct kvm *kvm) |
| 2311 | { |
| 2312 | long int i; |
| 2313 | |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 2314 | for (i = 0; i < KVM_MAX_VCORES; ++i) { |
| 2315 | if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) { |
| 2316 | struct kvmppc_vcore *vc = kvm->arch.vcores[i]; |
| 2317 | free_pages((unsigned long)vc->mpp_buffer, |
| 2318 | MPP_BUFFER_ORDER); |
| 2319 | } |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 2320 | kfree(kvm->arch.vcores[i]); |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 2321 | } |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 2322 | kvm->arch.online_vcores = 0; |
| 2323 | } |
| 2324 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2325 | static void kvmppc_core_destroy_vm_hv(struct kvm *kvm) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2326 | { |
Michael Ellerman | 441c19c | 2014-05-23 18:15:25 +1000 | [diff] [blame] | 2327 | kvm_hv_vm_deactivated(); |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 2328 | |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 2329 | kvmppc_free_vcores(kvm); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2330 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2331 | kvmppc_free_hpt(kvm); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | /* We don't need to emulate any privileged instructions or dcbz */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2335 | static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 2336 | unsigned int inst, int *advance) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2337 | { |
| 2338 | return EMULATE_FAIL; |
| 2339 | } |
| 2340 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2341 | static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn, |
| 2342 | ulong spr_val) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2343 | { |
| 2344 | return EMULATE_FAIL; |
| 2345 | } |
| 2346 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2347 | static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, |
| 2348 | ulong *spr_val) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2349 | { |
| 2350 | return EMULATE_FAIL; |
| 2351 | } |
| 2352 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2353 | static int kvmppc_core_check_processor_compat_hv(void) |
| 2354 | { |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame^] | 2355 | if (!cpu_has_feature(CPU_FTR_HVMODE) || |
| 2356 | !cpu_has_feature(CPU_FTR_ARCH_206)) |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2357 | return -EIO; |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | static long kvm_arch_vm_ioctl_hv(struct file *filp, |
| 2362 | unsigned int ioctl, unsigned long arg) |
| 2363 | { |
| 2364 | struct kvm *kvm __maybe_unused = filp->private_data; |
| 2365 | void __user *argp = (void __user *)arg; |
| 2366 | long r; |
| 2367 | |
| 2368 | switch (ioctl) { |
| 2369 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2370 | case KVM_PPC_ALLOCATE_HTAB: { |
| 2371 | u32 htab_order; |
| 2372 | |
| 2373 | r = -EFAULT; |
| 2374 | if (get_user(htab_order, (u32 __user *)argp)) |
| 2375 | break; |
| 2376 | r = kvmppc_alloc_reset_hpt(kvm, &htab_order); |
| 2377 | if (r) |
| 2378 | break; |
| 2379 | r = -EFAULT; |
| 2380 | if (put_user(htab_order, (u32 __user *)argp)) |
| 2381 | break; |
| 2382 | r = 0; |
| 2383 | break; |
| 2384 | } |
| 2385 | |
| 2386 | case KVM_PPC_GET_HTAB_FD: { |
| 2387 | struct kvm_get_htab_fd ghf; |
| 2388 | |
| 2389 | r = -EFAULT; |
| 2390 | if (copy_from_user(&ghf, argp, sizeof(ghf))) |
| 2391 | break; |
| 2392 | r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf); |
| 2393 | break; |
| 2394 | } |
| 2395 | |
| 2396 | default: |
| 2397 | r = -ENOTTY; |
| 2398 | } |
| 2399 | |
| 2400 | return r; |
| 2401 | } |
| 2402 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2403 | /* |
| 2404 | * List of hcall numbers to enable by default. |
| 2405 | * For compatibility with old userspace, we enable by default |
| 2406 | * all hcalls that were implemented before the hcall-enabling |
| 2407 | * facility was added. Note this list should not include H_RTAS. |
| 2408 | */ |
| 2409 | static unsigned int default_hcall_list[] = { |
| 2410 | H_REMOVE, |
| 2411 | H_ENTER, |
| 2412 | H_READ, |
| 2413 | H_PROTECT, |
| 2414 | H_BULK_REMOVE, |
| 2415 | H_GET_TCE, |
| 2416 | H_PUT_TCE, |
| 2417 | H_SET_DABR, |
| 2418 | H_SET_XDABR, |
| 2419 | H_CEDE, |
| 2420 | H_PROD, |
| 2421 | H_CONFER, |
| 2422 | H_REGISTER_VPA, |
| 2423 | #ifdef CONFIG_KVM_XICS |
| 2424 | H_EOI, |
| 2425 | H_CPPR, |
| 2426 | H_IPI, |
| 2427 | H_IPOLL, |
| 2428 | H_XIRR, |
| 2429 | H_XIRR_X, |
| 2430 | #endif |
| 2431 | 0 |
| 2432 | }; |
| 2433 | |
| 2434 | static void init_default_hcalls(void) |
| 2435 | { |
| 2436 | int i; |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 2437 | unsigned int hcall; |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2438 | |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 2439 | for (i = 0; default_hcall_list[i]; ++i) { |
| 2440 | hcall = default_hcall_list[i]; |
| 2441 | WARN_ON(!kvmppc_hcall_impl_hv(hcall)); |
| 2442 | __set_bit(hcall / 4, default_enabled_hcalls); |
| 2443 | } |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2444 | } |
| 2445 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2446 | static struct kvmppc_ops kvm_ops_hv = { |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2447 | .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, |
| 2448 | .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, |
| 2449 | .get_one_reg = kvmppc_get_one_reg_hv, |
| 2450 | .set_one_reg = kvmppc_set_one_reg_hv, |
| 2451 | .vcpu_load = kvmppc_core_vcpu_load_hv, |
| 2452 | .vcpu_put = kvmppc_core_vcpu_put_hv, |
| 2453 | .set_msr = kvmppc_set_msr_hv, |
| 2454 | .vcpu_run = kvmppc_vcpu_run_hv, |
| 2455 | .vcpu_create = kvmppc_core_vcpu_create_hv, |
| 2456 | .vcpu_free = kvmppc_core_vcpu_free_hv, |
| 2457 | .check_requests = kvmppc_core_check_requests_hv, |
| 2458 | .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv, |
| 2459 | .flush_memslot = kvmppc_core_flush_memslot_hv, |
| 2460 | .prepare_memory_region = kvmppc_core_prepare_memory_region_hv, |
| 2461 | .commit_memory_region = kvmppc_core_commit_memory_region_hv, |
| 2462 | .unmap_hva = kvm_unmap_hva_hv, |
| 2463 | .unmap_hva_range = kvm_unmap_hva_range_hv, |
| 2464 | .age_hva = kvm_age_hva_hv, |
| 2465 | .test_age_hva = kvm_test_age_hva_hv, |
| 2466 | .set_spte_hva = kvm_set_spte_hva_hv, |
| 2467 | .mmu_destroy = kvmppc_mmu_destroy_hv, |
| 2468 | .free_memslot = kvmppc_core_free_memslot_hv, |
| 2469 | .create_memslot = kvmppc_core_create_memslot_hv, |
| 2470 | .init_vm = kvmppc_core_init_vm_hv, |
| 2471 | .destroy_vm = kvmppc_core_destroy_vm_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2472 | .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv, |
| 2473 | .emulate_op = kvmppc_core_emulate_op_hv, |
| 2474 | .emulate_mtspr = kvmppc_core_emulate_mtspr_hv, |
| 2475 | .emulate_mfspr = kvmppc_core_emulate_mfspr_hv, |
| 2476 | .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv, |
| 2477 | .arch_vm_ioctl = kvm_arch_vm_ioctl_hv, |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 2478 | .hcall_implemented = kvmppc_hcall_impl_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2479 | }; |
| 2480 | |
| 2481 | static int kvmppc_book3s_init_hv(void) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2482 | { |
| 2483 | int r; |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2484 | /* |
| 2485 | * FIXME!! Do we need to check on all cpus ? |
| 2486 | */ |
| 2487 | r = kvmppc_core_check_processor_compat_hv(); |
| 2488 | if (r < 0) |
Paul Mackerras | 739e242 | 2014-03-25 10:47:05 +1100 | [diff] [blame] | 2489 | return -ENODEV; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2490 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2491 | kvm_ops_hv.owner = THIS_MODULE; |
| 2492 | kvmppc_hv_ops = &kvm_ops_hv; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2493 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2494 | init_default_hcalls(); |
| 2495 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2496 | r = kvmppc_mmu_hv_init(); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2497 | return r; |
| 2498 | } |
| 2499 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2500 | static void kvmppc_book3s_exit_hv(void) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2501 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2502 | kvmppc_hv_ops = NULL; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2505 | module_init(kvmppc_book3s_init_hv); |
| 2506 | module_exit(kvmppc_book3s_exit_hv); |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 2507 | MODULE_LICENSE("GPL"); |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 2508 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
| 2509 | MODULE_ALIAS("devname:kvm"); |