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