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