1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> 4 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved. 5 * 6 * Authors: 7 * Paul Mackerras <paulus@au1.ibm.com> 8 * Alexander Graf <agraf@suse.de> 9 * Kevin Wolf <mail@kevin-wolf.de> 10 * 11 * Description: KVM functions specific to running on Book 3S 12 * processors in hypervisor mode (specifically POWER7 and later). 13 * 14 * This file is derived from arch/powerpc/kvm/book3s.c, 15 * by Alexander Graf <agraf@suse.de>. 16 */ 17 18 #include <linux/kvm_host.h> 19 #include <linux/kernel.h> 20 #include <linux/err.h> 21 #include <linux/slab.h> 22 #include <linux/preempt.h> 23 #include <linux/sched/signal.h> 24 #include <linux/sched/stat.h> 25 #include <linux/delay.h> 26 #include <linux/export.h> 27 #include <linux/fs.h> 28 #include <linux/anon_inodes.h> 29 #include <linux/cpu.h> 30 #include <linux/cpumask.h> 31 #include <linux/spinlock.h> 32 #include <linux/page-flags.h> 33 #include <linux/srcu.h> 34 #include <linux/miscdevice.h> 35 #include <linux/debugfs.h> 36 #include <linux/gfp.h> 37 #include <linux/vmalloc.h> 38 #include <linux/highmem.h> 39 #include <linux/hugetlb.h> 40 #include <linux/kvm_irqfd.h> 41 #include <linux/irqbypass.h> 42 #include <linux/module.h> 43 #include <linux/compiler.h> 44 #include <linux/of.h> 45 #include <linux/irqdomain.h> 46 #include <linux/smp.h> 47 48 #include <asm/ftrace.h> 49 #include <asm/reg.h> 50 #include <asm/ppc-opcode.h> 51 #include <asm/asm-prototypes.h> 52 #include <asm/archrandom.h> 53 #include <asm/debug.h> 54 #include <asm/disassemble.h> 55 #include <asm/cputable.h> 56 #include <asm/cacheflush.h> 57 #include <linux/uaccess.h> 58 #include <asm/interrupt.h> 59 #include <asm/io.h> 60 #include <asm/kvm_ppc.h> 61 #include <asm/kvm_book3s.h> 62 #include <asm/mmu_context.h> 63 #include <asm/lppaca.h> 64 #include <asm/pmc.h> 65 #include <asm/processor.h> 66 #include <asm/cputhreads.h> 67 #include <asm/page.h> 68 #include <asm/hvcall.h> 69 #include <asm/switch_to.h> 70 #include <asm/smp.h> 71 #include <asm/dbell.h> 72 #include <asm/hmi.h> 73 #include <asm/pnv-pci.h> 74 #include <asm/mmu.h> 75 #include <asm/opal.h> 76 #include <asm/xics.h> 77 #include <asm/xive.h> 78 #include <asm/hw_breakpoint.h> 79 #include <asm/kvm_book3s_uvmem.h> 80 #include <asm/ultravisor.h> 81 #include <asm/dtl.h> 82 #include <asm/plpar_wrappers.h> 83 84 #include <trace/events/ipi.h> 85 86 #include "book3s.h" 87 #include "book3s_hv.h" 88 89 #define CREATE_TRACE_POINTS 90 #include "trace_hv.h" 91 92 /* #define EXIT_DEBUG */ 93 /* #define EXIT_DEBUG_SIMPLE */ 94 /* #define EXIT_DEBUG_INT */ 95 96 /* Used to indicate that a guest page fault needs to be handled */ 97 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1) 98 /* Used to indicate that a guest passthrough interrupt needs to be handled */ 99 #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2) 100 101 /* Used as a "null" value for timebase values */ 102 #define TB_NIL (~(u64)0) 103 104 static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); 105 106 static int dynamic_mt_modes = 6; 107 module_param(dynamic_mt_modes, int, 0644); 108 MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)"); 109 static int target_smt_mode; 110 module_param(target_smt_mode, int, 0644); 111 MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)"); 112 113 static bool one_vm_per_core; 114 module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR); 115 MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires POWER8 or older)"); 116 117 #ifdef CONFIG_KVM_XICS 118 static const struct kernel_param_ops module_param_ops = { 119 .set = param_set_int, 120 .get = param_get_int, 121 }; 122 123 module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644); 124 MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization"); 125 126 module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644); 127 MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core"); 128 #endif 129 130 /* If set, guests are allowed to create and control nested guests */ 131 static bool nested = true; 132 module_param(nested, bool, S_IRUGO | S_IWUSR); 133 MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)"); 134 135 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu); 136 137 /* 138 * RWMR values for POWER8. These control the rate at which PURR 139 * and SPURR count and should be set according to the number of 140 * online threads in the vcore being run. 141 */ 142 #define RWMR_RPA_P8_1THREAD 0x164520C62609AECAUL 143 #define RWMR_RPA_P8_2THREAD 0x7FFF2908450D8DA9UL 144 #define RWMR_RPA_P8_3THREAD 0x164520C62609AECAUL 145 #define RWMR_RPA_P8_4THREAD 0x199A421245058DA9UL 146 #define RWMR_RPA_P8_5THREAD 0x164520C62609AECAUL 147 #define RWMR_RPA_P8_6THREAD 0x164520C62609AECAUL 148 #define RWMR_RPA_P8_7THREAD 0x164520C62609AECAUL 149 #define RWMR_RPA_P8_8THREAD 0x164520C62609AECAUL 150 151 static unsigned long p8_rwmr_values[MAX_SMT_THREADS + 1] = { 152 RWMR_RPA_P8_1THREAD, 153 RWMR_RPA_P8_1THREAD, 154 RWMR_RPA_P8_2THREAD, 155 RWMR_RPA_P8_3THREAD, 156 RWMR_RPA_P8_4THREAD, 157 RWMR_RPA_P8_5THREAD, 158 RWMR_RPA_P8_6THREAD, 159 RWMR_RPA_P8_7THREAD, 160 RWMR_RPA_P8_8THREAD, 161 }; 162 163 static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc, 164 int *ip) 165 { 166 int i = *ip; 167 struct kvm_vcpu *vcpu; 168 169 while (++i < MAX_SMT_THREADS) { 170 vcpu = READ_ONCE(vc->runnable_threads[i]); 171 if (vcpu) { 172 *ip = i; 173 return vcpu; 174 } 175 } 176 return NULL; 177 } 178 179 /* Used to traverse the list of runnable threads for a given vcore */ 180 #define for_each_runnable_thread(i, vcpu, vc) \ 181 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); ) 182 183 static bool kvmppc_ipi_thread(int cpu) 184 { 185 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER); 186 187 /* If we're a nested hypervisor, fall back to ordinary IPIs for now */ 188 if (kvmhv_on_pseries()) 189 return false; 190 191 /* On POWER9 we can use msgsnd to IPI any cpu */ 192 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 193 msg |= get_hard_smp_processor_id(cpu); 194 smp_mb(); 195 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg)); 196 return true; 197 } 198 199 /* On POWER8 for IPIs to threads in the same core, use msgsnd */ 200 if (cpu_has_feature(CPU_FTR_ARCH_207S)) { 201 preempt_disable(); 202 if (cpu_first_thread_sibling(cpu) == 203 cpu_first_thread_sibling(smp_processor_id())) { 204 msg |= cpu_thread_in_core(cpu); 205 smp_mb(); 206 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg)); 207 preempt_enable(); 208 return true; 209 } 210 preempt_enable(); 211 } 212 213 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP) 214 if (cpu >= 0 && cpu < nr_cpu_ids) { 215 if (paca_ptrs[cpu]->kvm_hstate.xics_phys) { 216 xics_wake_cpu(cpu); 217 return true; 218 } 219 opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY); 220 return true; 221 } 222 #endif 223 224 return false; 225 } 226 227 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu) 228 { 229 int cpu; 230 struct rcuwait *waitp; 231 232 /* 233 * rcuwait_wake_up contains smp_mb() which orders prior stores that 234 * create pending work vs below loads of cpu fields. The other side 235 * is the barrier in vcpu run that orders setting the cpu fields vs 236 * testing for pending work. 237 */ 238 239 waitp = kvm_arch_vcpu_get_wait(vcpu); 240 if (rcuwait_wake_up(waitp)) 241 ++vcpu->stat.generic.halt_wakeup; 242 243 cpu = READ_ONCE(vcpu->arch.thread_cpu); 244 if (cpu >= 0 && kvmppc_ipi_thread(cpu)) 245 return; 246 247 /* CPU points to the first thread of the core */ 248 cpu = vcpu->cpu; 249 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu)) 250 smp_send_reschedule(cpu); 251 } 252 253 /* 254 * We use the vcpu_load/put functions to measure stolen time. 255 * 256 * Stolen time is counted as time when either the vcpu is able to 257 * run as part of a virtual core, but the task running the vcore 258 * is preempted or sleeping, or when the vcpu needs something done 259 * in the kernel by the task running the vcpu, but that task is 260 * preempted or sleeping. Those two things have to be counted 261 * separately, since one of the vcpu tasks will take on the job 262 * of running the core, and the other vcpu tasks in the vcore will 263 * sleep waiting for it to do that, but that sleep shouldn't count 264 * as stolen time. 265 * 266 * Hence we accumulate stolen time when the vcpu can run as part of 267 * a vcore using vc->stolen_tb, and the stolen time when the vcpu 268 * needs its task to do other things in the kernel (for example, 269 * service a page fault) in busy_stolen. We don't accumulate 270 * stolen time for a vcore when it is inactive, or for a vcpu 271 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of 272 * a misnomer; it means that the vcpu task is not executing in 273 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in 274 * the kernel. We don't have any way of dividing up that time 275 * between time that the vcpu is genuinely stopped, time that 276 * the task is actively working on behalf of the vcpu, and time 277 * that the task is preempted, so we don't count any of it as 278 * stolen. 279 * 280 * Updates to busy_stolen are protected by arch.tbacct_lock; 281 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock 282 * lock. The stolen times are measured in units of timebase ticks. 283 * (Note that the != TB_NIL checks below are purely defensive; 284 * they should never fail.) 285 * 286 * The POWER9 path is simpler, one vcpu per virtual core so the 287 * former case does not exist. If a vcpu is preempted when it is 288 * BUSY_IN_HOST and not ceded or otherwise blocked, then accumulate 289 * the stolen cycles in busy_stolen. RUNNING is not a preemptible 290 * state in the P9 path. 291 */ 292 293 static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc, u64 tb) 294 { 295 unsigned long flags; 296 297 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)); 298 299 spin_lock_irqsave(&vc->stoltb_lock, flags); 300 vc->preempt_tb = tb; 301 spin_unlock_irqrestore(&vc->stoltb_lock, flags); 302 } 303 304 static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc, u64 tb) 305 { 306 unsigned long flags; 307 308 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)); 309 310 spin_lock_irqsave(&vc->stoltb_lock, flags); 311 if (vc->preempt_tb != TB_NIL) { 312 vc->stolen_tb += tb - vc->preempt_tb; 313 vc->preempt_tb = TB_NIL; 314 } 315 spin_unlock_irqrestore(&vc->stoltb_lock, flags); 316 } 317 318 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu) 319 { 320 struct kvmppc_vcore *vc = vcpu->arch.vcore; 321 unsigned long flags; 322 u64 now; 323 324 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 325 if (vcpu->arch.busy_preempt != TB_NIL) { 326 WARN_ON_ONCE(vcpu->arch.state != KVMPPC_VCPU_BUSY_IN_HOST); 327 vc->stolen_tb += mftb() - vcpu->arch.busy_preempt; 328 vcpu->arch.busy_preempt = TB_NIL; 329 } 330 return; 331 } 332 333 now = mftb(); 334 335 /* 336 * We can test vc->runner without taking the vcore lock, 337 * because only this task ever sets vc->runner to this 338 * vcpu, and once it is set to this vcpu, only this task 339 * ever sets it to NULL. 340 */ 341 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) 342 kvmppc_core_end_stolen(vc, now); 343 344 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); 345 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST && 346 vcpu->arch.busy_preempt != TB_NIL) { 347 vcpu->arch.busy_stolen += now - vcpu->arch.busy_preempt; 348 vcpu->arch.busy_preempt = TB_NIL; 349 } 350 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); 351 } 352 353 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu) 354 { 355 struct kvmppc_vcore *vc = vcpu->arch.vcore; 356 unsigned long flags; 357 u64 now; 358 359 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 360 /* 361 * In the P9 path, RUNNABLE is not preemptible 362 * (nor takes host interrupts) 363 */ 364 WARN_ON_ONCE(vcpu->arch.state == KVMPPC_VCPU_RUNNABLE); 365 /* 366 * Account stolen time when preempted while the vcpu task is 367 * running in the kernel (but not in qemu, which is INACTIVE). 368 */ 369 if (task_is_running(current) && 370 vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST) 371 vcpu->arch.busy_preempt = mftb(); 372 return; 373 } 374 375 now = mftb(); 376 377 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) 378 kvmppc_core_start_stolen(vc, now); 379 380 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); 381 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST) 382 vcpu->arch.busy_preempt = now; 383 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); 384 } 385 386 static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr) 387 { 388 vcpu->arch.pvr = pvr; 389 } 390 391 /* Dummy value used in computing PCR value below */ 392 #define PCR_ARCH_31 (PCR_ARCH_300 << 1) 393 394 static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) 395 { 396 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0, cap = 0; 397 struct kvmppc_vcore *vc = vcpu->arch.vcore; 398 399 /* We can (emulate) our own architecture version and anything older */ 400 if (cpu_has_feature(CPU_FTR_ARCH_31)) 401 host_pcr_bit = PCR_ARCH_31; 402 else if (cpu_has_feature(CPU_FTR_ARCH_300)) 403 host_pcr_bit = PCR_ARCH_300; 404 else if (cpu_has_feature(CPU_FTR_ARCH_207S)) 405 host_pcr_bit = PCR_ARCH_207; 406 else if (cpu_has_feature(CPU_FTR_ARCH_206)) 407 host_pcr_bit = PCR_ARCH_206; 408 else 409 host_pcr_bit = PCR_ARCH_205; 410 411 /* Determine lowest PCR bit needed to run guest in given PVR level */ 412 guest_pcr_bit = host_pcr_bit; 413 if (arch_compat) { 414 switch (arch_compat) { 415 case PVR_ARCH_205: 416 guest_pcr_bit = PCR_ARCH_205; 417 break; 418 case PVR_ARCH_206: 419 case PVR_ARCH_206p: 420 guest_pcr_bit = PCR_ARCH_206; 421 break; 422 case PVR_ARCH_207: 423 guest_pcr_bit = PCR_ARCH_207; 424 break; 425 case PVR_ARCH_300: 426 guest_pcr_bit = PCR_ARCH_300; 427 cap = H_GUEST_CAP_POWER9; 428 break; 429 case PVR_ARCH_31: 430 guest_pcr_bit = PCR_ARCH_31; 431 cap = H_GUEST_CAP_POWER10; 432 break; 433 default: 434 return -EINVAL; 435 } 436 } 437 438 /* Check requested PCR bits don't exceed our capabilities */ 439 if (guest_pcr_bit > host_pcr_bit) 440 return -EINVAL; 441 442 if (kvmhv_on_pseries() && kvmhv_is_nestedv2()) { 443 if (!(cap & nested_capabilities)) 444 return -EINVAL; 445 } 446 447 spin_lock(&vc->lock); 448 vc->arch_compat = arch_compat; 449 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LOGICAL_PVR); 450 /* 451 * Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit 452 * Also set all reserved PCR bits 453 */ 454 vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK; 455 spin_unlock(&vc->lock); 456 457 return 0; 458 } 459 460 static void kvmppc_dump_regs(struct kvm_vcpu *vcpu) 461 { 462 int r; 463 464 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id); 465 pr_err("pc = %.16lx msr = %.16llx trap = %x\n", 466 vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap); 467 for (r = 0; r < 16; ++r) 468 pr_err("r%2d = %.16lx r%d = %.16lx\n", 469 r, kvmppc_get_gpr(vcpu, r), 470 r+16, kvmppc_get_gpr(vcpu, r+16)); 471 pr_err("ctr = %.16lx lr = %.16lx\n", 472 vcpu->arch.regs.ctr, vcpu->arch.regs.link); 473 pr_err("srr0 = %.16llx srr1 = %.16llx\n", 474 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1); 475 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n", 476 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1); 477 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n", 478 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); 479 pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n", 480 vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr); 481 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar); 482 pr_err("fault dar = %.16lx dsisr = %.8x\n", 483 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); 484 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max); 485 for (r = 0; r < vcpu->arch.slb_max; ++r) 486 pr_err(" ESID = %.16llx VSID = %.16llx\n", 487 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv); 488 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.16lx\n", 489 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1, 490 vcpu->arch.last_inst); 491 } 492 493 static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id) 494 { 495 return kvm_get_vcpu_by_id(kvm, id); 496 } 497 498 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa) 499 { 500 vpa->__old_status |= LPPACA_OLD_SHARED_PROC; 501 vpa->yield_count = cpu_to_be32(1); 502 } 503 504 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v, 505 unsigned long addr, unsigned long len) 506 { 507 /* check address is cacheline aligned */ 508 if (addr & (L1_CACHE_BYTES - 1)) 509 return -EINVAL; 510 spin_lock(&vcpu->arch.vpa_update_lock); 511 if (v->next_gpa != addr || v->len != len) { 512 v->next_gpa = addr; 513 v->len = addr ? len : 0; 514 v->update_pending = 1; 515 } 516 spin_unlock(&vcpu->arch.vpa_update_lock); 517 return 0; 518 } 519 520 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */ 521 struct reg_vpa { 522 u32 dummy; 523 union { 524 __be16 hword; 525 __be32 word; 526 } length; 527 }; 528 529 static int vpa_is_registered(struct kvmppc_vpa *vpap) 530 { 531 if (vpap->update_pending) 532 return vpap->next_gpa != 0; 533 return vpap->pinned_addr != NULL; 534 } 535 536 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu, 537 unsigned long flags, 538 unsigned long vcpuid, unsigned long vpa) 539 { 540 struct kvm *kvm = vcpu->kvm; 541 unsigned long len, nb; 542 void *va; 543 struct kvm_vcpu *tvcpu; 544 int err; 545 int subfunc; 546 struct kvmppc_vpa *vpap; 547 548 tvcpu = kvmppc_find_vcpu(kvm, vcpuid); 549 if (!tvcpu) 550 return H_PARAMETER; 551 552 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK; 553 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL || 554 subfunc == H_VPA_REG_SLB) { 555 /* Registering new area - address must be cache-line aligned */ 556 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa) 557 return H_PARAMETER; 558 559 /* convert logical addr to kernel addr and read length */ 560 va = kvmppc_pin_guest_page(kvm, vpa, &nb); 561 if (va == NULL) 562 return H_PARAMETER; 563 if (subfunc == H_VPA_REG_VPA) 564 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword); 565 else 566 len = be32_to_cpu(((struct reg_vpa *)va)->length.word); 567 kvmppc_unpin_guest_page(kvm, va, vpa, false); 568 569 /* Check length */ 570 if (len > nb || len < sizeof(struct reg_vpa)) 571 return H_PARAMETER; 572 } else { 573 vpa = 0; 574 len = 0; 575 } 576 577 err = H_PARAMETER; 578 vpap = NULL; 579 spin_lock(&tvcpu->arch.vpa_update_lock); 580 581 switch (subfunc) { 582 case H_VPA_REG_VPA: /* register VPA */ 583 /* 584 * The size of our lppaca is 1kB because of the way we align 585 * it for the guest to avoid crossing a 4kB boundary. We only 586 * use 640 bytes of the structure though, so we should accept 587 * clients that set a size of 640. 588 */ 589 BUILD_BUG_ON(sizeof(struct lppaca) != 640); 590 if (len < sizeof(struct lppaca)) 591 break; 592 vpap = &tvcpu->arch.vpa; 593 err = 0; 594 break; 595 596 case H_VPA_REG_DTL: /* register DTL */ 597 if (len < sizeof(struct dtl_entry)) 598 break; 599 len -= len % sizeof(struct dtl_entry); 600 601 /* Check that they have previously registered a VPA */ 602 err = H_RESOURCE; 603 if (!vpa_is_registered(&tvcpu->arch.vpa)) 604 break; 605 606 vpap = &tvcpu->arch.dtl; 607 err = 0; 608 break; 609 610 case H_VPA_REG_SLB: /* register SLB shadow buffer */ 611 /* Check that they have previously registered a VPA */ 612 err = H_RESOURCE; 613 if (!vpa_is_registered(&tvcpu->arch.vpa)) 614 break; 615 616 vpap = &tvcpu->arch.slb_shadow; 617 err = 0; 618 break; 619 620 case H_VPA_DEREG_VPA: /* deregister VPA */ 621 /* Check they don't still have a DTL or SLB buf registered */ 622 err = H_RESOURCE; 623 if (vpa_is_registered(&tvcpu->arch.dtl) || 624 vpa_is_registered(&tvcpu->arch.slb_shadow)) 625 break; 626 627 vpap = &tvcpu->arch.vpa; 628 err = 0; 629 break; 630 631 case H_VPA_DEREG_DTL: /* deregister DTL */ 632 vpap = &tvcpu->arch.dtl; 633 err = 0; 634 break; 635 636 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */ 637 vpap = &tvcpu->arch.slb_shadow; 638 err = 0; 639 break; 640 } 641 642 if (vpap) { 643 vpap->next_gpa = vpa; 644 vpap->len = len; 645 vpap->update_pending = 1; 646 } 647 648 spin_unlock(&tvcpu->arch.vpa_update_lock); 649 650 return err; 651 } 652 653 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap) 654 { 655 struct kvm *kvm = vcpu->kvm; 656 void *va; 657 unsigned long nb; 658 unsigned long gpa; 659 660 /* 661 * We need to pin the page pointed to by vpap->next_gpa, 662 * but we can't call kvmppc_pin_guest_page under the lock 663 * as it does get_user_pages() and down_read(). So we 664 * have to drop the lock, pin the page, then get the lock 665 * again and check that a new area didn't get registered 666 * in the meantime. 667 */ 668 for (;;) { 669 gpa = vpap->next_gpa; 670 spin_unlock(&vcpu->arch.vpa_update_lock); 671 va = NULL; 672 nb = 0; 673 if (gpa) 674 va = kvmppc_pin_guest_page(kvm, gpa, &nb); 675 spin_lock(&vcpu->arch.vpa_update_lock); 676 if (gpa == vpap->next_gpa) 677 break; 678 /* sigh... unpin that one and try again */ 679 if (va) 680 kvmppc_unpin_guest_page(kvm, va, gpa, false); 681 } 682 683 vpap->update_pending = 0; 684 if (va && nb < vpap->len) { 685 /* 686 * If it's now too short, it must be that userspace 687 * has changed the mappings underlying guest memory, 688 * so unregister the region. 689 */ 690 kvmppc_unpin_guest_page(kvm, va, gpa, false); 691 va = NULL; 692 } 693 if (vpap->pinned_addr) 694 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa, 695 vpap->dirty); 696 vpap->gpa = gpa; 697 vpap->pinned_addr = va; 698 vpap->dirty = false; 699 if (va) 700 vpap->pinned_end = va + vpap->len; 701 } 702 703 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu) 704 { 705 if (!(vcpu->arch.vpa.update_pending || 706 vcpu->arch.slb_shadow.update_pending || 707 vcpu->arch.dtl.update_pending)) 708 return; 709 710 spin_lock(&vcpu->arch.vpa_update_lock); 711 if (vcpu->arch.vpa.update_pending) { 712 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa); 713 if (vcpu->arch.vpa.pinned_addr) 714 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr); 715 } 716 if (vcpu->arch.dtl.update_pending) { 717 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl); 718 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr; 719 vcpu->arch.dtl_index = 0; 720 } 721 if (vcpu->arch.slb_shadow.update_pending) 722 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow); 723 spin_unlock(&vcpu->arch.vpa_update_lock); 724 } 725 726 /* 727 * Return the accumulated stolen time for the vcore up until `now'. 728 * The caller should hold the vcore lock. 729 */ 730 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now) 731 { 732 u64 p; 733 unsigned long flags; 734 735 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)); 736 737 spin_lock_irqsave(&vc->stoltb_lock, flags); 738 p = vc->stolen_tb; 739 if (vc->vcore_state != VCORE_INACTIVE && 740 vc->preempt_tb != TB_NIL) 741 p += now - vc->preempt_tb; 742 spin_unlock_irqrestore(&vc->stoltb_lock, flags); 743 return p; 744 } 745 746 static void __kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, 747 struct lppaca *vpa, 748 unsigned int pcpu, u64 now, 749 unsigned long stolen) 750 { 751 struct dtl_entry *dt; 752 753 dt = vcpu->arch.dtl_ptr; 754 755 if (!dt) 756 return; 757 758 dt->dispatch_reason = 7; 759 dt->preempt_reason = 0; 760 dt->processor_id = cpu_to_be16(pcpu + vcpu->arch.ptid); 761 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen); 762 dt->ready_to_enqueue_time = 0; 763 dt->waiting_to_ready_time = 0; 764 dt->timebase = cpu_to_be64(now); 765 dt->fault_addr = 0; 766 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu)); 767 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr); 768 769 ++dt; 770 if (dt == vcpu->arch.dtl.pinned_end) 771 dt = vcpu->arch.dtl.pinned_addr; 772 vcpu->arch.dtl_ptr = dt; 773 /* order writing *dt vs. writing vpa->dtl_idx */ 774 smp_wmb(); 775 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index); 776 777 /* vcpu->arch.dtl.dirty is set by the caller */ 778 } 779 780 static void kvmppc_update_vpa_dispatch(struct kvm_vcpu *vcpu, 781 struct kvmppc_vcore *vc) 782 { 783 struct lppaca *vpa; 784 unsigned long stolen; 785 unsigned long core_stolen; 786 u64 now; 787 unsigned long flags; 788 789 vpa = vcpu->arch.vpa.pinned_addr; 790 if (!vpa) 791 return; 792 793 now = mftb(); 794 795 core_stolen = vcore_stolen_time(vc, now); 796 stolen = core_stolen - vcpu->arch.stolen_logged; 797 vcpu->arch.stolen_logged = core_stolen; 798 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); 799 stolen += vcpu->arch.busy_stolen; 800 vcpu->arch.busy_stolen = 0; 801 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); 802 803 vpa->enqueue_dispatch_tb = cpu_to_be64(be64_to_cpu(vpa->enqueue_dispatch_tb) + stolen); 804 805 __kvmppc_create_dtl_entry(vcpu, vpa, vc->pcpu, now + kvmppc_get_tb_offset(vcpu), stolen); 806 807 vcpu->arch.vpa.dirty = true; 808 } 809 810 static void kvmppc_update_vpa_dispatch_p9(struct kvm_vcpu *vcpu, 811 struct kvmppc_vcore *vc, 812 u64 now) 813 { 814 struct lppaca *vpa; 815 unsigned long stolen; 816 unsigned long stolen_delta; 817 818 vpa = vcpu->arch.vpa.pinned_addr; 819 if (!vpa) 820 return; 821 822 stolen = vc->stolen_tb; 823 stolen_delta = stolen - vcpu->arch.stolen_logged; 824 vcpu->arch.stolen_logged = stolen; 825 826 vpa->enqueue_dispatch_tb = cpu_to_be64(stolen); 827 828 __kvmppc_create_dtl_entry(vcpu, vpa, vc->pcpu, now, stolen_delta); 829 830 vcpu->arch.vpa.dirty = true; 831 } 832 833 /* See if there is a doorbell interrupt pending for a vcpu */ 834 static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu) 835 { 836 int thr; 837 struct kvmppc_vcore *vc; 838 839 if (vcpu->arch.doorbell_request) 840 return true; 841 if (cpu_has_feature(CPU_FTR_ARCH_300)) 842 return false; 843 /* 844 * Ensure that the read of vcore->dpdes comes after the read 845 * of vcpu->doorbell_request. This barrier matches the 846 * smp_wmb() in kvmppc_guest_entry_inject(). 847 */ 848 smp_rmb(); 849 vc = vcpu->arch.vcore; 850 thr = vcpu->vcpu_id - vc->first_vcpuid; 851 return !!(vc->dpdes & (1 << thr)); 852 } 853 854 static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu) 855 { 856 if (kvmppc_get_arch_compat(vcpu) >= PVR_ARCH_207) 857 return true; 858 if ((!kvmppc_get_arch_compat(vcpu)) && 859 cpu_has_feature(CPU_FTR_ARCH_207S)) 860 return true; 861 return false; 862 } 863 864 static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags, 865 unsigned long resource, unsigned long value1, 866 unsigned long value2) 867 { 868 switch (resource) { 869 case H_SET_MODE_RESOURCE_SET_CIABR: 870 if (!kvmppc_power8_compatible(vcpu)) 871 return H_P2; 872 if (value2) 873 return H_P4; 874 if (mflags) 875 return H_UNSUPPORTED_FLAG_START; 876 /* Guests can't breakpoint the hypervisor */ 877 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER) 878 return H_P3; 879 kvmppc_set_ciabr_hv(vcpu, value1); 880 return H_SUCCESS; 881 case H_SET_MODE_RESOURCE_SET_DAWR0: 882 if (!kvmppc_power8_compatible(vcpu)) 883 return H_P2; 884 if (!ppc_breakpoint_available()) 885 return H_P2; 886 if (mflags) 887 return H_UNSUPPORTED_FLAG_START; 888 if (value2 & DABRX_HYP) 889 return H_P4; 890 kvmppc_set_dawr0_hv(vcpu, value1); 891 kvmppc_set_dawrx0_hv(vcpu, value2); 892 return H_SUCCESS; 893 case H_SET_MODE_RESOURCE_SET_DAWR1: 894 if (!kvmppc_power8_compatible(vcpu)) 895 return H_P2; 896 if (!ppc_breakpoint_available()) 897 return H_P2; 898 if (!cpu_has_feature(CPU_FTR_DAWR1)) 899 return H_P2; 900 if (!vcpu->kvm->arch.dawr1_enabled) 901 return H_FUNCTION; 902 if (mflags) 903 return H_UNSUPPORTED_FLAG_START; 904 if (value2 & DABRX_HYP) 905 return H_P4; 906 kvmppc_set_dawr1_hv(vcpu, value1); 907 kvmppc_set_dawrx1_hv(vcpu, value2); 908 return H_SUCCESS; 909 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE: 910 /* 911 * KVM does not support mflags=2 (AIL=2) and AIL=1 is reserved. 912 * Keep this in synch with kvmppc_filter_guest_lpcr_hv. 913 */ 914 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) && 915 kvmhv_vcpu_is_radix(vcpu) && mflags == 3) 916 return H_UNSUPPORTED_FLAG_START; 917 return H_TOO_HARD; 918 default: 919 return H_TOO_HARD; 920 } 921 } 922 923 /* Copy guest memory in place - must reside within a single memslot */ 924 static int kvmppc_copy_guest(struct kvm *kvm, gpa_t to, gpa_t from, 925 unsigned long len) 926 { 927 struct kvm_memory_slot *to_memslot = NULL; 928 struct kvm_memory_slot *from_memslot = NULL; 929 unsigned long to_addr, from_addr; 930 int r; 931 932 /* Get HPA for from address */ 933 from_memslot = gfn_to_memslot(kvm, from >> PAGE_SHIFT); 934 if (!from_memslot) 935 return -EFAULT; 936 if ((from + len) >= ((from_memslot->base_gfn + from_memslot->npages) 937 << PAGE_SHIFT)) 938 return -EINVAL; 939 from_addr = gfn_to_hva_memslot(from_memslot, from >> PAGE_SHIFT); 940 if (kvm_is_error_hva(from_addr)) 941 return -EFAULT; 942 from_addr |= (from & (PAGE_SIZE - 1)); 943 944 /* Get HPA for to address */ 945 to_memslot = gfn_to_memslot(kvm, to >> PAGE_SHIFT); 946 if (!to_memslot) 947 return -EFAULT; 948 if ((to + len) >= ((to_memslot->base_gfn + to_memslot->npages) 949 << PAGE_SHIFT)) 950 return -EINVAL; 951 to_addr = gfn_to_hva_memslot(to_memslot, to >> PAGE_SHIFT); 952 if (kvm_is_error_hva(to_addr)) 953 return -EFAULT; 954 to_addr |= (to & (PAGE_SIZE - 1)); 955 956 /* Perform copy */ 957 r = raw_copy_in_user((void __user *)to_addr, (void __user *)from_addr, 958 len); 959 if (r) 960 return -EFAULT; 961 mark_page_dirty(kvm, to >> PAGE_SHIFT); 962 return 0; 963 } 964 965 static long kvmppc_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags, 966 unsigned long dest, unsigned long src) 967 { 968 u64 pg_sz = SZ_4K; /* 4K page size */ 969 u64 pg_mask = SZ_4K - 1; 970 int ret; 971 972 /* Check for invalid flags (H_PAGE_SET_LOANED covers all CMO flags) */ 973 if (flags & ~(H_ICACHE_INVALIDATE | H_ICACHE_SYNCHRONIZE | 974 H_ZERO_PAGE | H_COPY_PAGE | H_PAGE_SET_LOANED)) 975 return H_PARAMETER; 976 977 /* dest (and src if copy_page flag set) must be page aligned */ 978 if ((dest & pg_mask) || ((flags & H_COPY_PAGE) && (src & pg_mask))) 979 return H_PARAMETER; 980 981 /* zero and/or copy the page as determined by the flags */ 982 if (flags & H_COPY_PAGE) { 983 ret = kvmppc_copy_guest(vcpu->kvm, dest, src, pg_sz); 984 if (ret < 0) 985 return H_PARAMETER; 986 } else if (flags & H_ZERO_PAGE) { 987 ret = kvm_clear_guest(vcpu->kvm, dest, pg_sz); 988 if (ret < 0) 989 return H_PARAMETER; 990 } 991 992 /* We can ignore the remaining flags */ 993 994 return H_SUCCESS; 995 } 996 997 static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target) 998 { 999 struct kvmppc_vcore *vcore = target->arch.vcore; 1000 1001 /* 1002 * We expect to have been called by the real mode handler 1003 * (kvmppc_rm_h_confer()) which would have directly returned 1004 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may 1005 * have useful work to do and should not confer) so we don't 1006 * recheck that here. 1007 * 1008 * In the case of the P9 single vcpu per vcore case, the real 1009 * mode handler is not called but no other threads are in the 1010 * source vcore. 1011 */ 1012 if (!cpu_has_feature(CPU_FTR_ARCH_300)) { 1013 spin_lock(&vcore->lock); 1014 if (target->arch.state == KVMPPC_VCPU_RUNNABLE && 1015 vcore->vcore_state != VCORE_INACTIVE && 1016 vcore->runner) 1017 target = vcore->runner; 1018 spin_unlock(&vcore->lock); 1019 } 1020 1021 return kvm_vcpu_yield_to(target); 1022 } 1023 1024 static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu) 1025 { 1026 int yield_count = 0; 1027 struct lppaca *lppaca; 1028 1029 spin_lock(&vcpu->arch.vpa_update_lock); 1030 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr; 1031 if (lppaca) 1032 yield_count = be32_to_cpu(lppaca->yield_count); 1033 spin_unlock(&vcpu->arch.vpa_update_lock); 1034 return yield_count; 1035 } 1036 1037 /* 1038 * H_RPT_INVALIDATE hcall handler for nested guests. 1039 * 1040 * Handles only nested process-scoped invalidation requests in L0. 1041 */ 1042 static int kvmppc_nested_h_rpt_invalidate(struct kvm_vcpu *vcpu) 1043 { 1044 unsigned long type = kvmppc_get_gpr(vcpu, 6); 1045 unsigned long pid, pg_sizes, start, end; 1046 1047 /* 1048 * The partition-scoped invalidations aren't handled here in L0. 1049 */ 1050 if (type & H_RPTI_TYPE_NESTED) 1051 return RESUME_HOST; 1052 1053 pid = kvmppc_get_gpr(vcpu, 4); 1054 pg_sizes = kvmppc_get_gpr(vcpu, 7); 1055 start = kvmppc_get_gpr(vcpu, 8); 1056 end = kvmppc_get_gpr(vcpu, 9); 1057 1058 do_h_rpt_invalidate_prt(pid, vcpu->arch.nested->shadow_lpid, 1059 type, pg_sizes, start, end); 1060 1061 kvmppc_set_gpr(vcpu, 3, H_SUCCESS); 1062 return RESUME_GUEST; 1063 } 1064 1065 static long kvmppc_h_rpt_invalidate(struct kvm_vcpu *vcpu, 1066 unsigned long id, unsigned long target, 1067 unsigned long type, unsigned long pg_sizes, 1068 unsigned long start, unsigned long end) 1069 { 1070 if (!kvm_is_radix(vcpu->kvm)) 1071 return H_UNSUPPORTED; 1072 1073 if (end < start) 1074 return H_P5; 1075 1076 /* 1077 * Partition-scoped invalidation for nested guests. 1078 */ 1079 if (type & H_RPTI_TYPE_NESTED) { 1080 if (!nesting_enabled(vcpu->kvm)) 1081 return H_FUNCTION; 1082 1083 /* Support only cores as target */ 1084 if (target != H_RPTI_TARGET_CMMU) 1085 return H_P2; 1086 1087 return do_h_rpt_invalidate_pat(vcpu, id, type, pg_sizes, 1088 start, end); 1089 } 1090 1091 /* 1092 * Process-scoped invalidation for L1 guests. 1093 */ 1094 do_h_rpt_invalidate_prt(id, vcpu->kvm->arch.lpid, 1095 type, pg_sizes, start, end); 1096 return H_SUCCESS; 1097 } 1098 1099 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) 1100 { 1101 struct kvm *kvm = vcpu->kvm; 1102 unsigned long req = kvmppc_get_gpr(vcpu, 3); 1103 unsigned long target, ret = H_SUCCESS; 1104 int yield_count; 1105 struct kvm_vcpu *tvcpu; 1106 int idx, rc; 1107 1108 if (req <= MAX_HCALL_OPCODE && 1109 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls)) 1110 return RESUME_HOST; 1111 1112 switch (req) { 1113 case H_REMOVE: 1114 ret = kvmppc_h_remove(vcpu, kvmppc_get_gpr(vcpu, 4), 1115 kvmppc_get_gpr(vcpu, 5), 1116 kvmppc_get_gpr(vcpu, 6)); 1117 if (ret == H_TOO_HARD) 1118 return RESUME_HOST; 1119 break; 1120 case H_ENTER: 1121 ret = kvmppc_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4), 1122 kvmppc_get_gpr(vcpu, 5), 1123 kvmppc_get_gpr(vcpu, 6), 1124 kvmppc_get_gpr(vcpu, 7)); 1125 if (ret == H_TOO_HARD) 1126 return RESUME_HOST; 1127 break; 1128 case H_READ: 1129 ret = kvmppc_h_read(vcpu, kvmppc_get_gpr(vcpu, 4), 1130 kvmppc_get_gpr(vcpu, 5)); 1131 if (ret == H_TOO_HARD) 1132 return RESUME_HOST; 1133 break; 1134 case H_CLEAR_MOD: 1135 ret = kvmppc_h_clear_mod(vcpu, kvmppc_get_gpr(vcpu, 4), 1136 kvmppc_get_gpr(vcpu, 5)); 1137 if (ret == H_TOO_HARD) 1138 return RESUME_HOST; 1139 break; 1140 case H_CLEAR_REF: 1141 ret = kvmppc_h_clear_ref(vcpu, kvmppc_get_gpr(vcpu, 4), 1142 kvmppc_get_gpr(vcpu, 5)); 1143 if (ret == H_TOO_HARD) 1144 return RESUME_HOST; 1145 break; 1146 case H_PROTECT: 1147 ret = kvmppc_h_protect(vcpu, kvmppc_get_gpr(vcpu, 4), 1148 kvmppc_get_gpr(vcpu, 5), 1149 kvmppc_get_gpr(vcpu, 6)); 1150 if (ret == H_TOO_HARD) 1151 return RESUME_HOST; 1152 break; 1153 case H_BULK_REMOVE: 1154 ret = kvmppc_h_bulk_remove(vcpu); 1155 if (ret == H_TOO_HARD) 1156 return RESUME_HOST; 1157 break; 1158 1159 case H_CEDE: 1160 break; 1161 case H_PROD: 1162 target = kvmppc_get_gpr(vcpu, 4); 1163 tvcpu = kvmppc_find_vcpu(kvm, target); 1164 if (!tvcpu) { 1165 ret = H_PARAMETER; 1166 break; 1167 } 1168 tvcpu->arch.prodded = 1; 1169 smp_mb(); /* This orders prodded store vs ceded load */ 1170 if (tvcpu->arch.ceded) 1171 kvmppc_fast_vcpu_kick_hv(tvcpu); 1172 break; 1173 case H_CONFER: 1174 target = kvmppc_get_gpr(vcpu, 4); 1175 if (target == -1) 1176 break; 1177 tvcpu = kvmppc_find_vcpu(kvm, target); 1178 if (!tvcpu) { 1179 ret = H_PARAMETER; 1180 break; 1181 } 1182 yield_count = kvmppc_get_gpr(vcpu, 5); 1183 if (kvmppc_get_yield_count(tvcpu) != yield_count) 1184 break; 1185 kvm_arch_vcpu_yield_to(tvcpu); 1186 break; 1187 case H_REGISTER_VPA: 1188 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4), 1189 kvmppc_get_gpr(vcpu, 5), 1190 kvmppc_get_gpr(vcpu, 6)); 1191 break; 1192 case H_RTAS: 1193 if (list_empty(&kvm->arch.rtas_tokens)) 1194 return RESUME_HOST; 1195 1196 idx = srcu_read_lock(&kvm->srcu); 1197 rc = kvmppc_rtas_hcall(vcpu); 1198 srcu_read_unlock(&kvm->srcu, idx); 1199 1200 if (rc == -ENOENT) 1201 return RESUME_HOST; 1202 else if (rc == 0) 1203 break; 1204 1205 /* Send the error out to userspace via KVM_RUN */ 1206 return rc; 1207 case H_LOGICAL_CI_LOAD: 1208 ret = kvmppc_h_logical_ci_load(vcpu); 1209 if (ret == H_TOO_HARD) 1210 return RESUME_HOST; 1211 break; 1212 case H_LOGICAL_CI_STORE: 1213 ret = kvmppc_h_logical_ci_store(vcpu); 1214 if (ret == H_TOO_HARD) 1215 return RESUME_HOST; 1216 break; 1217 case H_SET_MODE: 1218 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4), 1219 kvmppc_get_gpr(vcpu, 5), 1220 kvmppc_get_gpr(vcpu, 6), 1221 kvmppc_get_gpr(vcpu, 7)); 1222 if (ret == H_TOO_HARD) 1223 return RESUME_HOST; 1224 break; 1225 case H_XIRR: 1226 case H_CPPR: 1227 case H_EOI: 1228 case H_IPI: 1229 case H_IPOLL: 1230 case H_XIRR_X: 1231 if (kvmppc_xics_enabled(vcpu)) { 1232 if (xics_on_xive()) { 1233 ret = H_NOT_AVAILABLE; 1234 return RESUME_GUEST; 1235 } 1236 ret = kvmppc_xics_hcall(vcpu, req); 1237 break; 1238 } 1239 return RESUME_HOST; 1240 case H_SET_DABR: 1241 ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4)); 1242 break; 1243 case H_SET_XDABR: 1244 ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4), 1245 kvmppc_get_gpr(vcpu, 5)); 1246 break; 1247 #ifdef CONFIG_SPAPR_TCE_IOMMU 1248 case H_GET_TCE: 1249 ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4), 1250 kvmppc_get_gpr(vcpu, 5)); 1251 if (ret == H_TOO_HARD) 1252 return RESUME_HOST; 1253 break; 1254 case H_PUT_TCE: 1255 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4), 1256 kvmppc_get_gpr(vcpu, 5), 1257 kvmppc_get_gpr(vcpu, 6)); 1258 if (ret == H_TOO_HARD) 1259 return RESUME_HOST; 1260 break; 1261 case H_PUT_TCE_INDIRECT: 1262 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4), 1263 kvmppc_get_gpr(vcpu, 5), 1264 kvmppc_get_gpr(vcpu, 6), 1265 kvmppc_get_gpr(vcpu, 7)); 1266 if (ret == H_TOO_HARD) 1267 return RESUME_HOST; 1268 break; 1269 case H_STUFF_TCE: 1270 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4), 1271 kvmppc_get_gpr(vcpu, 5), 1272 kvmppc_get_gpr(vcpu, 6), 1273 kvmppc_get_gpr(vcpu, 7)); 1274 if (ret == H_TOO_HARD) 1275 return RESUME_HOST; 1276 break; 1277 #endif 1278 case H_RANDOM: { 1279 unsigned long rand; 1280 1281 if (!arch_get_random_seed_longs(&rand, 1)) 1282 ret = H_HARDWARE; 1283 kvmppc_set_gpr(vcpu, 4, rand); 1284 break; 1285 } 1286 case H_RPT_INVALIDATE: 1287 ret = kvmppc_h_rpt_invalidate(vcpu, kvmppc_get_gpr(vcpu, 4), 1288 kvmppc_get_gpr(vcpu, 5), 1289 kvmppc_get_gpr(vcpu, 6), 1290 kvmppc_get_gpr(vcpu, 7), 1291 kvmppc_get_gpr(vcpu, 8), 1292 kvmppc_get_gpr(vcpu, 9)); 1293 break; 1294 1295 case H_SET_PARTITION_TABLE: 1296 ret = H_FUNCTION; 1297 if (nesting_enabled(kvm)) 1298 ret = kvmhv_set_partition_table(vcpu); 1299 break; 1300 case H_ENTER_NESTED: 1301 ret = H_FUNCTION; 1302 if (!nesting_enabled(kvm)) 1303 break; 1304 ret = kvmhv_enter_nested_guest(vcpu); 1305 if (ret == H_INTERRUPT) { 1306 kvmppc_set_gpr(vcpu, 3, 0); 1307 vcpu->arch.hcall_needed = 0; 1308 return -EINTR; 1309 } else if (ret == H_TOO_HARD) { 1310 kvmppc_set_gpr(vcpu, 3, 0); 1311 vcpu->arch.hcall_needed = 0; 1312 return RESUME_HOST; 1313 } 1314 break; 1315 case H_TLB_INVALIDATE: 1316 ret = H_FUNCTION; 1317 if (nesting_enabled(kvm)) 1318 ret = kvmhv_do_nested_tlbie(vcpu); 1319 break; 1320 case H_COPY_TOFROM_GUEST: 1321 ret = H_FUNCTION; 1322 if (nesting_enabled(kvm)) 1323 ret = kvmhv_copy_tofrom_guest_nested(vcpu); 1324 break; 1325 case H_PAGE_INIT: 1326 ret = kvmppc_h_page_init(vcpu, kvmppc_get_gpr(vcpu, 4), 1327 kvmppc_get_gpr(vcpu, 5), 1328 kvmppc_get_gpr(vcpu, 6)); 1329 break; 1330 case H_SVM_PAGE_IN: 1331 ret = H_UNSUPPORTED; 1332 if (kvmppc_get_srr1(vcpu) & MSR_S) 1333 ret = kvmppc_h_svm_page_in(kvm, 1334 kvmppc_get_gpr(vcpu, 4), 1335 kvmppc_get_gpr(vcpu, 5), 1336 kvmppc_get_gpr(vcpu, 6)); 1337 break; 1338 case H_SVM_PAGE_OUT: 1339 ret = H_UNSUPPORTED; 1340 if (kvmppc_get_srr1(vcpu) & MSR_S) 1341 ret = kvmppc_h_svm_page_out(kvm, 1342 kvmppc_get_gpr(vcpu, 4), 1343 kvmppc_get_gpr(vcpu, 5), 1344 kvmppc_get_gpr(vcpu, 6)); 1345 break; 1346 case H_SVM_INIT_START: 1347 ret = H_UNSUPPORTED; 1348 if (kvmppc_get_srr1(vcpu) & MSR_S) 1349 ret = kvmppc_h_svm_init_start(kvm); 1350 break; 1351 case H_SVM_INIT_DONE: 1352 ret = H_UNSUPPORTED; 1353 if (kvmppc_get_srr1(vcpu) & MSR_S) 1354 ret = kvmppc_h_svm_init_done(kvm); 1355 break; 1356 case H_SVM_INIT_ABORT: 1357 /* 1358 * Even if that call is made by the Ultravisor, the SSR1 value 1359 * is the guest context one, with the secure bit clear as it has 1360 * not yet been secured. So we can't check it here. 1361 * Instead the kvm->arch.secure_guest flag is checked inside 1362 * kvmppc_h_svm_init_abort(). 1363 */ 1364 ret = kvmppc_h_svm_init_abort(kvm); 1365 break; 1366 1367 default: 1368 return RESUME_HOST; 1369 } 1370 WARN_ON_ONCE(ret == H_TOO_HARD); 1371 kvmppc_set_gpr(vcpu, 3, ret); 1372 vcpu->arch.hcall_needed = 0; 1373 return RESUME_GUEST; 1374 } 1375 1376 /* 1377 * Handle H_CEDE in the P9 path where we don't call the real-mode hcall 1378 * handlers in book3s_hv_rmhandlers.S. 1379 * 1380 * This has to be done early, not in kvmppc_pseries_do_hcall(), so 1381 * that the cede logic in kvmppc_run_single_vcpu() works properly. 1382 */ 1383 static void kvmppc_cede(struct kvm_vcpu *vcpu) 1384 { 1385 __kvmppc_set_msr_hv(vcpu, __kvmppc_get_msr_hv(vcpu) | MSR_EE); 1386 vcpu->arch.ceded = 1; 1387 smp_mb(); 1388 if (vcpu->arch.prodded) { 1389 vcpu->arch.prodded = 0; 1390 smp_mb(); 1391 vcpu->arch.ceded = 0; 1392 } 1393 } 1394 1395 static int kvmppc_hcall_impl_hv(unsigned long cmd) 1396 { 1397 switch (cmd) { 1398 case H_CEDE: 1399 case H_PROD: 1400 case H_CONFER: 1401 case H_REGISTER_VPA: 1402 case H_SET_MODE: 1403 #ifdef CONFIG_SPAPR_TCE_IOMMU 1404 case H_GET_TCE: 1405 case H_PUT_TCE: 1406 case H_PUT_TCE_INDIRECT: 1407 case H_STUFF_TCE: 1408 #endif 1409 case H_LOGICAL_CI_LOAD: 1410 case H_LOGICAL_CI_STORE: 1411 #ifdef CONFIG_KVM_XICS 1412 case H_XIRR: 1413 case H_CPPR: 1414 case H_EOI: 1415 case H_IPI: 1416 case H_IPOLL: 1417 case H_XIRR_X: 1418 #endif 1419 case H_PAGE_INIT: 1420 case H_RPT_INVALIDATE: 1421 return 1; 1422 } 1423 1424 /* See if it's in the real-mode table */ 1425 return kvmppc_hcall_impl_hv_realmode(cmd); 1426 } 1427 1428 static int kvmppc_emulate_debug_inst(struct kvm_vcpu *vcpu) 1429 { 1430 ppc_inst_t last_inst; 1431 1432 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) != 1433 EMULATE_DONE) { 1434 /* 1435 * Fetch failed, so return to guest and 1436 * try executing it again. 1437 */ 1438 return RESUME_GUEST; 1439 } 1440 1441 if (ppc_inst_val(last_inst) == KVMPPC_INST_SW_BREAKPOINT) { 1442 vcpu->run->exit_reason = KVM_EXIT_DEBUG; 1443 vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu); 1444 return RESUME_HOST; 1445 } else { 1446 kvmppc_core_queue_program(vcpu, SRR1_PROGILL | 1447 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED)); 1448 return RESUME_GUEST; 1449 } 1450 } 1451 1452 static void do_nothing(void *x) 1453 { 1454 } 1455 1456 static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu) 1457 { 1458 int thr, cpu, pcpu, nthreads; 1459 struct kvm_vcpu *v; 1460 unsigned long dpdes; 1461 1462 nthreads = vcpu->kvm->arch.emul_smt_mode; 1463 dpdes = 0; 1464 cpu = vcpu->vcpu_id & ~(nthreads - 1); 1465 for (thr = 0; thr < nthreads; ++thr, ++cpu) { 1466 v = kvmppc_find_vcpu(vcpu->kvm, cpu); 1467 if (!v) 1468 continue; 1469 /* 1470 * If the vcpu is currently running on a physical cpu thread, 1471 * interrupt it in order to pull it out of the guest briefly, 1472 * which will update its vcore->dpdes value. 1473 */ 1474 pcpu = READ_ONCE(v->cpu); 1475 if (pcpu >= 0) 1476 smp_call_function_single(pcpu, do_nothing, NULL, 1); 1477 if (kvmppc_doorbell_pending(v)) 1478 dpdes |= 1 << thr; 1479 } 1480 return dpdes; 1481 } 1482 1483 /* 1484 * On POWER9, emulate doorbell-related instructions in order to 1485 * give the guest the illusion of running on a multi-threaded core. 1486 * The instructions emulated are msgsndp, msgclrp, mfspr TIR, 1487 * and mfspr DPDES. 1488 */ 1489 static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu) 1490 { 1491 u32 inst, rb, thr; 1492 unsigned long arg; 1493 struct kvm *kvm = vcpu->kvm; 1494 struct kvm_vcpu *tvcpu; 1495 ppc_inst_t pinst; 1496 1497 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst) != EMULATE_DONE) 1498 return RESUME_GUEST; 1499 inst = ppc_inst_val(pinst); 1500 if (get_op(inst) != 31) 1501 return EMULATE_FAIL; 1502 rb = get_rb(inst); 1503 thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1); 1504 switch (get_xop(inst)) { 1505 case OP_31_XOP_MSGSNDP: 1506 arg = kvmppc_get_gpr(vcpu, rb); 1507 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER) 1508 break; 1509 arg &= 0x7f; 1510 if (arg >= kvm->arch.emul_smt_mode) 1511 break; 1512 tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg); 1513 if (!tvcpu) 1514 break; 1515 if (!tvcpu->arch.doorbell_request) { 1516 tvcpu->arch.doorbell_request = 1; 1517 kvmppc_fast_vcpu_kick_hv(tvcpu); 1518 } 1519 break; 1520 case OP_31_XOP_MSGCLRP: 1521 arg = kvmppc_get_gpr(vcpu, rb); 1522 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER) 1523 break; 1524 vcpu->arch.vcore->dpdes = 0; 1525 vcpu->arch.doorbell_request = 0; 1526 break; 1527 case OP_31_XOP_MFSPR: 1528 switch (get_sprn(inst)) { 1529 case SPRN_TIR: 1530 arg = thr; 1531 break; 1532 case SPRN_DPDES: 1533 arg = kvmppc_read_dpdes(vcpu); 1534 break; 1535 default: 1536 return EMULATE_FAIL; 1537 } 1538 kvmppc_set_gpr(vcpu, get_rt(inst), arg); 1539 break; 1540 default: 1541 return EMULATE_FAIL; 1542 } 1543 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4); 1544 return RESUME_GUEST; 1545 } 1546 1547 /* 1548 * If the lppaca had pmcregs_in_use clear when we exited the guest, then 1549 * HFSCR_PM is cleared for next entry. If the guest then tries to access 1550 * the PMU SPRs, we get this facility unavailable interrupt. Putting HFSCR_PM 1551 * back in the guest HFSCR will cause the next entry to load the PMU SPRs and 1552 * allow the guest access to continue. 1553 */ 1554 static int kvmppc_pmu_unavailable(struct kvm_vcpu *vcpu) 1555 { 1556 if (!(vcpu->arch.hfscr_permitted & HFSCR_PM)) 1557 return EMULATE_FAIL; 1558 1559 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_PM); 1560 1561 return RESUME_GUEST; 1562 } 1563 1564 static int kvmppc_ebb_unavailable(struct kvm_vcpu *vcpu) 1565 { 1566 if (!(vcpu->arch.hfscr_permitted & HFSCR_EBB)) 1567 return EMULATE_FAIL; 1568 1569 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_EBB); 1570 1571 return RESUME_GUEST; 1572 } 1573 1574 static int kvmppc_tm_unavailable(struct kvm_vcpu *vcpu) 1575 { 1576 if (!(vcpu->arch.hfscr_permitted & HFSCR_TM)) 1577 return EMULATE_FAIL; 1578 1579 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_TM); 1580 1581 return RESUME_GUEST; 1582 } 1583 1584 static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu, 1585 struct task_struct *tsk) 1586 { 1587 struct kvm_run *run = vcpu->run; 1588 int r = RESUME_HOST; 1589 1590 vcpu->stat.sum_exits++; 1591 1592 /* 1593 * This can happen if an interrupt occurs in the last stages 1594 * of guest entry or the first stages of guest exit (i.e. after 1595 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV 1596 * and before setting it to KVM_GUEST_MODE_HOST_HV). 1597 * That can happen due to a bug, or due to a machine check 1598 * occurring at just the wrong time. 1599 */ 1600 if (__kvmppc_get_msr_hv(vcpu) & MSR_HV) { 1601 printk(KERN_EMERG "KVM trap in HV mode!\n"); 1602 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", 1603 vcpu->arch.trap, kvmppc_get_pc(vcpu), 1604 vcpu->arch.shregs.msr); 1605 kvmppc_dump_regs(vcpu); 1606 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 1607 run->hw.hardware_exit_reason = vcpu->arch.trap; 1608 return RESUME_HOST; 1609 } 1610 run->exit_reason = KVM_EXIT_UNKNOWN; 1611 run->ready_for_interrupt_injection = 1; 1612 switch (vcpu->arch.trap) { 1613 /* We're good on these - the host merely wanted to get our attention */ 1614 case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER: 1615 WARN_ON_ONCE(1); /* Should never happen */ 1616 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER; 1617 fallthrough; 1618 case BOOK3S_INTERRUPT_HV_DECREMENTER: 1619 vcpu->stat.dec_exits++; 1620 r = RESUME_GUEST; 1621 break; 1622 case BOOK3S_INTERRUPT_EXTERNAL: 1623 case BOOK3S_INTERRUPT_H_DOORBELL: 1624 case BOOK3S_INTERRUPT_H_VIRT: 1625 vcpu->stat.ext_intr_exits++; 1626 r = RESUME_GUEST; 1627 break; 1628 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/ 1629 case BOOK3S_INTERRUPT_HMI: 1630 case BOOK3S_INTERRUPT_PERFMON: 1631 case BOOK3S_INTERRUPT_SYSTEM_RESET: 1632 r = RESUME_GUEST; 1633 break; 1634 case BOOK3S_INTERRUPT_MACHINE_CHECK: { 1635 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, 1636 DEFAULT_RATELIMIT_BURST); 1637 /* 1638 * Print the MCE event to host console. Ratelimit so the guest 1639 * can't flood the host log. 1640 */ 1641 if (__ratelimit(&rs)) 1642 machine_check_print_event_info(&vcpu->arch.mce_evt,false, true); 1643 1644 /* 1645 * If the guest can do FWNMI, exit to userspace so it can 1646 * deliver a FWNMI to the guest. 1647 * Otherwise we synthesize a machine check for the guest 1648 * so that it knows that the machine check occurred. 1649 */ 1650 if (!vcpu->kvm->arch.fwnmi_enabled) { 1651 ulong flags = (__kvmppc_get_msr_hv(vcpu) & 0x083c0000) | 1652 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED); 1653 kvmppc_core_queue_machine_check(vcpu, flags); 1654 r = RESUME_GUEST; 1655 break; 1656 } 1657 1658 /* Exit to guest with KVM_EXIT_NMI as exit reason */ 1659 run->exit_reason = KVM_EXIT_NMI; 1660 run->hw.hardware_exit_reason = vcpu->arch.trap; 1661 /* Clear out the old NMI status from run->flags */ 1662 run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK; 1663 /* Now set the NMI status */ 1664 if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED) 1665 run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV; 1666 else 1667 run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV; 1668 1669 r = RESUME_HOST; 1670 break; 1671 } 1672 case BOOK3S_INTERRUPT_PROGRAM: 1673 { 1674 ulong flags; 1675 /* 1676 * Normally program interrupts are delivered directly 1677 * to the guest by the hardware, but we can get here 1678 * as a result of a hypervisor emulation interrupt 1679 * (e40) getting turned into a 700 by BML RTAS. 1680 */ 1681 flags = (__kvmppc_get_msr_hv(vcpu) & 0x1f0000ull) | 1682 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED); 1683 kvmppc_core_queue_program(vcpu, flags); 1684 r = RESUME_GUEST; 1685 break; 1686 } 1687 case BOOK3S_INTERRUPT_SYSCALL: 1688 { 1689 int i; 1690 1691 if (unlikely(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) { 1692 /* 1693 * Guest userspace executed sc 1. This can only be 1694 * reached by the P9 path because the old path 1695 * handles this case in realmode hcall handlers. 1696 */ 1697 if (!kvmhv_vcpu_is_radix(vcpu)) { 1698 /* 1699 * A guest could be running PR KVM, so this 1700 * may be a PR KVM hcall. It must be reflected 1701 * to the guest kernel as a sc interrupt. 1702 */ 1703 kvmppc_core_queue_syscall(vcpu); 1704 } else { 1705 /* 1706 * Radix guests can not run PR KVM or nested HV 1707 * hash guests which might run PR KVM, so this 1708 * is always a privilege fault. Send a program 1709 * check to guest kernel. 1710 */ 1711 kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV); 1712 } 1713 r = RESUME_GUEST; 1714 break; 1715 } 1716 1717 /* 1718 * hcall - gather args and set exit_reason. This will next be 1719 * handled by kvmppc_pseries_do_hcall which may be able to deal 1720 * with it and resume guest, or may punt to userspace. 1721 */ 1722 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3); 1723 for (i = 0; i < 9; ++i) 1724 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i); 1725 run->exit_reason = KVM_EXIT_PAPR_HCALL; 1726 vcpu->arch.hcall_needed = 1; 1727 r = RESUME_HOST; 1728 break; 1729 } 1730 /* 1731 * We get these next two if the guest accesses a page which it thinks 1732 * it has mapped but which is not actually present, either because 1733 * it is for an emulated I/O device or because the corresonding 1734 * host page has been paged out. 1735 * 1736 * Any other HDSI/HISI interrupts have been handled already for P7/8 1737 * guests. For POWER9 hash guests not using rmhandlers, basic hash 1738 * fault handling is done here. 1739 */ 1740 case BOOK3S_INTERRUPT_H_DATA_STORAGE: { 1741 unsigned long vsid; 1742 long err; 1743 1744 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) && 1745 unlikely(vcpu->arch.fault_dsisr == HDSISR_CANARY)) { 1746 r = RESUME_GUEST; /* Just retry if it's the canary */ 1747 break; 1748 } 1749 1750 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) { 1751 /* 1752 * Radix doesn't require anything, and pre-ISAv3.0 hash 1753 * already attempted to handle this in rmhandlers. The 1754 * hash fault handling below is v3 only (it uses ASDR 1755 * via fault_gpa). 1756 */ 1757 r = RESUME_PAGE_FAULT; 1758 break; 1759 } 1760 1761 if (!(vcpu->arch.fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT))) { 1762 kvmppc_core_queue_data_storage(vcpu, 1763 kvmppc_get_msr(vcpu) & SRR1_PREFIXED, 1764 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); 1765 r = RESUME_GUEST; 1766 break; 1767 } 1768 1769 if (!(__kvmppc_get_msr_hv(vcpu) & MSR_DR)) 1770 vsid = vcpu->kvm->arch.vrma_slb_v; 1771 else 1772 vsid = vcpu->arch.fault_gpa; 1773 1774 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar, 1775 vsid, vcpu->arch.fault_dsisr, true); 1776 if (err == 0) { 1777 r = RESUME_GUEST; 1778 } else if (err == -1 || err == -2) { 1779 r = RESUME_PAGE_FAULT; 1780 } else { 1781 kvmppc_core_queue_data_storage(vcpu, 1782 kvmppc_get_msr(vcpu) & SRR1_PREFIXED, 1783 vcpu->arch.fault_dar, err); 1784 r = RESUME_GUEST; 1785 } 1786 break; 1787 } 1788 case BOOK3S_INTERRUPT_H_INST_STORAGE: { 1789 unsigned long vsid; 1790 long err; 1791 1792 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); 1793 vcpu->arch.fault_dsisr = __kvmppc_get_msr_hv(vcpu) & 1794 DSISR_SRR1_MATCH_64S; 1795 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) { 1796 /* 1797 * Radix doesn't require anything, and pre-ISAv3.0 hash 1798 * already attempted to handle this in rmhandlers. The 1799 * hash fault handling below is v3 only (it uses ASDR 1800 * via fault_gpa). 1801 */ 1802 if (__kvmppc_get_msr_hv(vcpu) & HSRR1_HISI_WRITE) 1803 vcpu->arch.fault_dsisr |= DSISR_ISSTORE; 1804 r = RESUME_PAGE_FAULT; 1805 break; 1806 } 1807 1808 if (!(vcpu->arch.fault_dsisr & SRR1_ISI_NOPT)) { 1809 kvmppc_core_queue_inst_storage(vcpu, 1810 vcpu->arch.fault_dsisr | 1811 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED)); 1812 r = RESUME_GUEST; 1813 break; 1814 } 1815 1816 if (!(__kvmppc_get_msr_hv(vcpu) & MSR_IR)) 1817 vsid = vcpu->kvm->arch.vrma_slb_v; 1818 else 1819 vsid = vcpu->arch.fault_gpa; 1820 1821 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar, 1822 vsid, vcpu->arch.fault_dsisr, false); 1823 if (err == 0) { 1824 r = RESUME_GUEST; 1825 } else if (err == -1) { 1826 r = RESUME_PAGE_FAULT; 1827 } else { 1828 kvmppc_core_queue_inst_storage(vcpu, 1829 err | (kvmppc_get_msr(vcpu) & SRR1_PREFIXED)); 1830 r = RESUME_GUEST; 1831 } 1832 break; 1833 } 1834 1835 /* 1836 * This occurs if the guest executes an illegal instruction. 1837 * If the guest debug is disabled, generate a program interrupt 1838 * to the guest. If guest debug is enabled, we need to check 1839 * whether the instruction is a software breakpoint instruction. 1840 * Accordingly return to Guest or Host. 1841 */ 1842 case BOOK3S_INTERRUPT_H_EMUL_ASSIST: 1843 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED) 1844 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ? 1845 swab32(vcpu->arch.emul_inst) : 1846 vcpu->arch.emul_inst; 1847 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) { 1848 r = kvmppc_emulate_debug_inst(vcpu); 1849 } else { 1850 kvmppc_core_queue_program(vcpu, SRR1_PROGILL | 1851 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED)); 1852 r = RESUME_GUEST; 1853 } 1854 break; 1855 1856 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 1857 case BOOK3S_INTERRUPT_HV_SOFTPATCH: 1858 /* 1859 * This occurs for various TM-related instructions that 1860 * we need to emulate on POWER9 DD2.2. We have already 1861 * handled the cases where the guest was in real-suspend 1862 * mode and was transitioning to transactional state. 1863 */ 1864 r = kvmhv_p9_tm_emulation(vcpu); 1865 if (r != -1) 1866 break; 1867 fallthrough; /* go to facility unavailable handler */ 1868 #endif 1869 1870 /* 1871 * This occurs if the guest (kernel or userspace), does something that 1872 * is prohibited by HFSCR. 1873 * On POWER9, this could be a doorbell instruction that we need 1874 * to emulate. 1875 * Otherwise, we just generate a program interrupt to the guest. 1876 */ 1877 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: { 1878 u64 cause = kvmppc_get_hfscr_hv(vcpu) >> 56; 1879 1880 r = EMULATE_FAIL; 1881 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 1882 if (cause == FSCR_MSGP_LG) 1883 r = kvmppc_emulate_doorbell_instr(vcpu); 1884 if (cause == FSCR_PM_LG) 1885 r = kvmppc_pmu_unavailable(vcpu); 1886 if (cause == FSCR_EBB_LG) 1887 r = kvmppc_ebb_unavailable(vcpu); 1888 if (cause == FSCR_TM_LG) 1889 r = kvmppc_tm_unavailable(vcpu); 1890 } 1891 if (r == EMULATE_FAIL) { 1892 kvmppc_core_queue_program(vcpu, SRR1_PROGILL | 1893 (kvmppc_get_msr(vcpu) & SRR1_PREFIXED)); 1894 r = RESUME_GUEST; 1895 } 1896 break; 1897 } 1898 1899 case BOOK3S_INTERRUPT_HV_RM_HARD: 1900 r = RESUME_PASSTHROUGH; 1901 break; 1902 default: 1903 kvmppc_dump_regs(vcpu); 1904 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", 1905 vcpu->arch.trap, kvmppc_get_pc(vcpu), 1906 __kvmppc_get_msr_hv(vcpu)); 1907 run->hw.hardware_exit_reason = vcpu->arch.trap; 1908 r = RESUME_HOST; 1909 break; 1910 } 1911 1912 return r; 1913 } 1914 1915 static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu) 1916 { 1917 int r; 1918 int srcu_idx; 1919 1920 vcpu->stat.sum_exits++; 1921 1922 /* 1923 * This can happen if an interrupt occurs in the last stages 1924 * of guest entry or the first stages of guest exit (i.e. after 1925 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV 1926 * and before setting it to KVM_GUEST_MODE_HOST_HV). 1927 * That can happen due to a bug, or due to a machine check 1928 * occurring at just the wrong time. 1929 */ 1930 if (__kvmppc_get_msr_hv(vcpu) & MSR_HV) { 1931 pr_emerg("KVM trap in HV mode while nested!\n"); 1932 pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n", 1933 vcpu->arch.trap, kvmppc_get_pc(vcpu), 1934 __kvmppc_get_msr_hv(vcpu)); 1935 kvmppc_dump_regs(vcpu); 1936 return RESUME_HOST; 1937 } 1938 switch (vcpu->arch.trap) { 1939 /* We're good on these - the host merely wanted to get our attention */ 1940 case BOOK3S_INTERRUPT_HV_DECREMENTER: 1941 vcpu->stat.dec_exits++; 1942 r = RESUME_GUEST; 1943 break; 1944 case BOOK3S_INTERRUPT_EXTERNAL: 1945 vcpu->stat.ext_intr_exits++; 1946 r = RESUME_HOST; 1947 break; 1948 case BOOK3S_INTERRUPT_H_DOORBELL: 1949 case BOOK3S_INTERRUPT_H_VIRT: 1950 vcpu->stat.ext_intr_exits++; 1951 r = RESUME_GUEST; 1952 break; 1953 /* These need to go to the nested HV */ 1954 case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER: 1955 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER; 1956 vcpu->stat.dec_exits++; 1957 r = RESUME_HOST; 1958 break; 1959 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/ 1960 case BOOK3S_INTERRUPT_HMI: 1961 case BOOK3S_INTERRUPT_PERFMON: 1962 case BOOK3S_INTERRUPT_SYSTEM_RESET: 1963 r = RESUME_GUEST; 1964 break; 1965 case BOOK3S_INTERRUPT_MACHINE_CHECK: 1966 { 1967 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL, 1968 DEFAULT_RATELIMIT_BURST); 1969 /* Pass the machine check to the L1 guest */ 1970 r = RESUME_HOST; 1971 /* Print the MCE event to host console. */ 1972 if (__ratelimit(&rs)) 1973 machine_check_print_event_info(&vcpu->arch.mce_evt, false, true); 1974 break; 1975 } 1976 /* 1977 * We get these next two if the guest accesses a page which it thinks 1978 * it has mapped but which is not actually present, either because 1979 * it is for an emulated I/O device or because the corresonding 1980 * host page has been paged out. 1981 */ 1982 case BOOK3S_INTERRUPT_H_DATA_STORAGE: 1983 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 1984 r = kvmhv_nested_page_fault(vcpu); 1985 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 1986 break; 1987 case BOOK3S_INTERRUPT_H_INST_STORAGE: 1988 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); 1989 vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) & 1990 DSISR_SRR1_MATCH_64S; 1991 if (__kvmppc_get_msr_hv(vcpu) & HSRR1_HISI_WRITE) 1992 vcpu->arch.fault_dsisr |= DSISR_ISSTORE; 1993 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); 1994 r = kvmhv_nested_page_fault(vcpu); 1995 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); 1996 break; 1997 1998 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 1999 case BOOK3S_INTERRUPT_HV_SOFTPATCH: 2000 /* 2001 * This occurs for various TM-related instructions that 2002 * we need to emulate on POWER9 DD2.2. We have already 2003 * handled the cases where the guest was in real-suspend 2004 * mode and was transitioning to transactional state. 2005 */ 2006 r = kvmhv_p9_tm_emulation(vcpu); 2007 if (r != -1) 2008 break; 2009 fallthrough; /* go to facility unavailable handler */ 2010 #endif 2011 2012 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: { 2013 u64 cause = vcpu->arch.hfscr >> 56; 2014 2015 /* 2016 * Only pass HFU interrupts to the L1 if the facility is 2017 * permitted but disabled by the L1's HFSCR, otherwise 2018 * the interrupt does not make sense to the L1 so turn 2019 * it into a HEAI. 2020 */ 2021 if (!(vcpu->arch.hfscr_permitted & (1UL << cause)) || 2022 (vcpu->arch.nested_hfscr & (1UL << cause))) { 2023 ppc_inst_t pinst; 2024 vcpu->arch.trap = BOOK3S_INTERRUPT_H_EMUL_ASSIST; 2025 2026 /* 2027 * If the fetch failed, return to guest and 2028 * try executing it again. 2029 */ 2030 r = kvmppc_get_last_inst(vcpu, INST_GENERIC, &pinst); 2031 vcpu->arch.emul_inst = ppc_inst_val(pinst); 2032 if (r != EMULATE_DONE) 2033 r = RESUME_GUEST; 2034 else 2035 r = RESUME_HOST; 2036 } else { 2037 r = RESUME_HOST; 2038 } 2039 2040 break; 2041 } 2042 2043 case BOOK3S_INTERRUPT_HV_RM_HARD: 2044 vcpu->arch.trap = 0; 2045 r = RESUME_GUEST; 2046 if (!xics_on_xive()) 2047 kvmppc_xics_rm_complete(vcpu, 0); 2048 break; 2049 case BOOK3S_INTERRUPT_SYSCALL: 2050 { 2051 unsigned long req = kvmppc_get_gpr(vcpu, 3); 2052 2053 /* 2054 * The H_RPT_INVALIDATE hcalls issued by nested 2055 * guests for process-scoped invalidations when 2056 * GTSE=0, are handled here in L0. 2057 */ 2058 if (req == H_RPT_INVALIDATE) { 2059 r = kvmppc_nested_h_rpt_invalidate(vcpu); 2060 break; 2061 } 2062 2063 r = RESUME_HOST; 2064 break; 2065 } 2066 default: 2067 r = RESUME_HOST; 2068 break; 2069 } 2070 2071 return r; 2072 } 2073 2074 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu, 2075 struct kvm_sregs *sregs) 2076 { 2077 int i; 2078 2079 memset(sregs, 0, sizeof(struct kvm_sregs)); 2080 sregs->pvr = vcpu->arch.pvr; 2081 for (i = 0; i < vcpu->arch.slb_max; i++) { 2082 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige; 2083 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; 2084 } 2085 2086 return 0; 2087 } 2088 2089 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu, 2090 struct kvm_sregs *sregs) 2091 { 2092 int i, j; 2093 2094 /* Only accept the same PVR as the host's, since we can't spoof it */ 2095 if (sregs->pvr != vcpu->arch.pvr) 2096 return -EINVAL; 2097 2098 j = 0; 2099 for (i = 0; i < vcpu->arch.slb_nr; i++) { 2100 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) { 2101 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe; 2102 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv; 2103 ++j; 2104 } 2105 } 2106 vcpu->arch.slb_max = j; 2107 2108 return 0; 2109 } 2110 2111 /* 2112 * Enforce limits on guest LPCR values based on hardware availability, 2113 * guest configuration, and possibly hypervisor support and security 2114 * concerns. 2115 */ 2116 unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm, unsigned long lpcr) 2117 { 2118 /* LPCR_TC only applies to HPT guests */ 2119 if (kvm_is_radix(kvm)) 2120 lpcr &= ~LPCR_TC; 2121 2122 /* On POWER8 and above, userspace can modify AIL */ 2123 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) 2124 lpcr &= ~LPCR_AIL; 2125 if ((lpcr & LPCR_AIL) != LPCR_AIL_3) 2126 lpcr &= ~LPCR_AIL; /* LPCR[AIL]=1/2 is disallowed */ 2127 /* 2128 * On some POWER9s we force AIL off for radix guests to prevent 2129 * executing in MSR[HV]=1 mode with the MMU enabled and PIDR set to 2130 * guest, which can result in Q0 translations with LPID=0 PID=PIDR to 2131 * be cached, which the host TLB management does not expect. 2132 */ 2133 if (kvm_is_radix(kvm) && cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG)) 2134 lpcr &= ~LPCR_AIL; 2135 2136 /* 2137 * On POWER9, allow userspace to enable large decrementer for the 2138 * guest, whether or not the host has it enabled. 2139 */ 2140 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 2141 lpcr &= ~LPCR_LD; 2142 2143 return lpcr; 2144 } 2145 2146 static void verify_lpcr(struct kvm *kvm, unsigned long lpcr) 2147 { 2148 if (lpcr != kvmppc_filter_lpcr_hv(kvm, lpcr)) { 2149 WARN_ONCE(1, "lpcr 0x%lx differs from filtered 0x%lx\n", 2150 lpcr, kvmppc_filter_lpcr_hv(kvm, lpcr)); 2151 } 2152 } 2153 2154 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr, 2155 bool preserve_top32) 2156 { 2157 struct kvm *kvm = vcpu->kvm; 2158 struct kvmppc_vcore *vc = vcpu->arch.vcore; 2159 u64 mask; 2160 2161 spin_lock(&vc->lock); 2162 2163 /* 2164 * Userspace can only modify 2165 * DPFD (default prefetch depth), ILE (interrupt little-endian), 2166 * TC (translation control), AIL (alternate interrupt location), 2167 * LD (large decrementer). 2168 * These are subject to restrictions from kvmppc_filter_lcpr_hv(). 2169 */ 2170 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD; 2171 2172 /* Broken 32-bit version of LPCR must not clear top bits */ 2173 if (preserve_top32) 2174 mask &= 0xFFFFFFFF; 2175 2176 new_lpcr = kvmppc_filter_lpcr_hv(kvm, 2177 (vc->lpcr & ~mask) | (new_lpcr & mask)); 2178 2179 /* 2180 * If ILE (interrupt little-endian) has changed, update the 2181 * MSR_LE bit in the intr_msr for each vcpu in this vcore. 2182 */ 2183 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) { 2184 struct kvm_vcpu *vcpu; 2185 unsigned long i; 2186 2187 kvm_for_each_vcpu(i, vcpu, kvm) { 2188 if (vcpu->arch.vcore != vc) 2189 continue; 2190 if (new_lpcr & LPCR_ILE) 2191 vcpu->arch.intr_msr |= MSR_LE; 2192 else 2193 vcpu->arch.intr_msr &= ~MSR_LE; 2194 } 2195 } 2196 2197 vc->lpcr = new_lpcr; 2198 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LPCR); 2199 2200 spin_unlock(&vc->lock); 2201 } 2202 2203 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, 2204 union kvmppc_one_reg *val) 2205 { 2206 int r = 0; 2207 long int i; 2208 2209 switch (id) { 2210 case KVM_REG_PPC_DEBUG_INST: 2211 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT); 2212 break; 2213 case KVM_REG_PPC_HIOR: 2214 *val = get_reg_val(id, 0); 2215 break; 2216 case KVM_REG_PPC_DABR: 2217 *val = get_reg_val(id, vcpu->arch.dabr); 2218 break; 2219 case KVM_REG_PPC_DABRX: 2220 *val = get_reg_val(id, vcpu->arch.dabrx); 2221 break; 2222 case KVM_REG_PPC_DSCR: 2223 *val = get_reg_val(id, kvmppc_get_dscr_hv(vcpu)); 2224 break; 2225 case KVM_REG_PPC_PURR: 2226 *val = get_reg_val(id, kvmppc_get_purr_hv(vcpu)); 2227 break; 2228 case KVM_REG_PPC_SPURR: 2229 *val = get_reg_val(id, kvmppc_get_spurr_hv(vcpu)); 2230 break; 2231 case KVM_REG_PPC_AMR: 2232 *val = get_reg_val(id, kvmppc_get_amr_hv(vcpu)); 2233 break; 2234 case KVM_REG_PPC_UAMOR: 2235 *val = get_reg_val(id, kvmppc_get_uamor_hv(vcpu)); 2236 break; 2237 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1: 2238 i = id - KVM_REG_PPC_MMCR0; 2239 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, i)); 2240 break; 2241 case KVM_REG_PPC_MMCR2: 2242 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, 2)); 2243 break; 2244 case KVM_REG_PPC_MMCRA: 2245 *val = get_reg_val(id, kvmppc_get_mmcra_hv(vcpu)); 2246 break; 2247 case KVM_REG_PPC_MMCRS: 2248 *val = get_reg_val(id, vcpu->arch.mmcrs); 2249 break; 2250 case KVM_REG_PPC_MMCR3: 2251 *val = get_reg_val(id, kvmppc_get_mmcr_hv(vcpu, 3)); 2252 break; 2253 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: 2254 i = id - KVM_REG_PPC_PMC1; 2255 *val = get_reg_val(id, kvmppc_get_pmc_hv(vcpu, i)); 2256 break; 2257 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: 2258 i = id - KVM_REG_PPC_SPMC1; 2259 *val = get_reg_val(id, vcpu->arch.spmc[i]); 2260 break; 2261 case KVM_REG_PPC_SIAR: 2262 *val = get_reg_val(id, kvmppc_get_siar_hv(vcpu)); 2263 break; 2264 case KVM_REG_PPC_SDAR: 2265 *val = get_reg_val(id, kvmppc_get_siar_hv(vcpu)); 2266 break; 2267 case KVM_REG_PPC_SIER: 2268 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 0)); 2269 break; 2270 case KVM_REG_PPC_SIER2: 2271 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 1)); 2272 break; 2273 case KVM_REG_PPC_SIER3: 2274 *val = get_reg_val(id, kvmppc_get_sier_hv(vcpu, 2)); 2275 break; 2276 case KVM_REG_PPC_IAMR: 2277 *val = get_reg_val(id, kvmppc_get_iamr_hv(vcpu)); 2278 break; 2279 case KVM_REG_PPC_PSPB: 2280 *val = get_reg_val(id, kvmppc_get_pspb_hv(vcpu)); 2281 break; 2282 case KVM_REG_PPC_DPDES: 2283 /* 2284 * On POWER9, where we are emulating msgsndp etc., 2285 * we return 1 bit for each vcpu, which can come from 2286 * either vcore->dpdes or doorbell_request. 2287 * On POWER8, doorbell_request is 0. 2288 */ 2289 if (cpu_has_feature(CPU_FTR_ARCH_300)) 2290 *val = get_reg_val(id, vcpu->arch.doorbell_request); 2291 else 2292 *val = get_reg_val(id, vcpu->arch.vcore->dpdes); 2293 break; 2294 case KVM_REG_PPC_VTB: 2295 *val = get_reg_val(id, kvmppc_get_vtb(vcpu)); 2296 break; 2297 case KVM_REG_PPC_DAWR: 2298 *val = get_reg_val(id, kvmppc_get_dawr0_hv(vcpu)); 2299 break; 2300 case KVM_REG_PPC_DAWRX: 2301 *val = get_reg_val(id, kvmppc_get_dawrx0_hv(vcpu)); 2302 break; 2303 case KVM_REG_PPC_DAWR1: 2304 *val = get_reg_val(id, kvmppc_get_dawr1_hv(vcpu)); 2305 break; 2306 case KVM_REG_PPC_DAWRX1: 2307 *val = get_reg_val(id, kvmppc_get_dawrx1_hv(vcpu)); 2308 break; 2309 case KVM_REG_PPC_CIABR: 2310 *val = get_reg_val(id, kvmppc_get_ciabr_hv(vcpu)); 2311 break; 2312 case KVM_REG_PPC_CSIGR: 2313 *val = get_reg_val(id, vcpu->arch.csigr); 2314 break; 2315 case KVM_REG_PPC_TACR: 2316 *val = get_reg_val(id, vcpu->arch.tacr); 2317 break; 2318 case KVM_REG_PPC_TCSCR: 2319 *val = get_reg_val(id, vcpu->arch.tcscr); 2320 break; 2321 case KVM_REG_PPC_PID: 2322 *val = get_reg_val(id, kvmppc_get_pid(vcpu)); 2323 break; 2324 case KVM_REG_PPC_ACOP: 2325 *val = get_reg_val(id, vcpu->arch.acop); 2326 break; 2327 case KVM_REG_PPC_WORT: 2328 *val = get_reg_val(id, kvmppc_get_wort_hv(vcpu)); 2329 break; 2330 case KVM_REG_PPC_TIDR: 2331 *val = get_reg_val(id, vcpu->arch.tid); 2332 break; 2333 case KVM_REG_PPC_PSSCR: 2334 *val = get_reg_val(id, vcpu->arch.psscr); 2335 break; 2336 case KVM_REG_PPC_VPA_ADDR: 2337 spin_lock(&vcpu->arch.vpa_update_lock); 2338 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa); 2339 spin_unlock(&vcpu->arch.vpa_update_lock); 2340 break; 2341 case KVM_REG_PPC_VPA_SLB: 2342 spin_lock(&vcpu->arch.vpa_update_lock); 2343 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa; 2344 val->vpaval.length = vcpu->arch.slb_shadow.len; 2345 spin_unlock(&vcpu->arch.vpa_update_lock); 2346 break; 2347 case KVM_REG_PPC_VPA_DTL: 2348 spin_lock(&vcpu->arch.vpa_update_lock); 2349 val->vpaval.addr = vcpu->arch.dtl.next_gpa; 2350 val->vpaval.length = vcpu->arch.dtl.len; 2351 spin_unlock(&vcpu->arch.vpa_update_lock); 2352 break; 2353 case KVM_REG_PPC_TB_OFFSET: 2354 *val = get_reg_val(id, kvmppc_get_tb_offset(vcpu)); 2355 break; 2356 case KVM_REG_PPC_LPCR: 2357 case KVM_REG_PPC_LPCR_64: 2358 *val = get_reg_val(id, kvmppc_get_lpcr(vcpu)); 2359 break; 2360 case KVM_REG_PPC_PPR: 2361 *val = get_reg_val(id, kvmppc_get_ppr_hv(vcpu)); 2362 break; 2363 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 2364 case KVM_REG_PPC_TFHAR: 2365 *val = get_reg_val(id, vcpu->arch.tfhar); 2366 break; 2367 case KVM_REG_PPC_TFIAR: 2368 *val = get_reg_val(id, vcpu->arch.tfiar); 2369 break; 2370 case KVM_REG_PPC_TEXASR: 2371 *val = get_reg_val(id, vcpu->arch.texasr); 2372 break; 2373 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: 2374 i = id - KVM_REG_PPC_TM_GPR0; 2375 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]); 2376 break; 2377 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: 2378 { 2379 int j; 2380 i = id - KVM_REG_PPC_TM_VSR0; 2381 if (i < 32) 2382 for (j = 0; j < TS_FPRWIDTH; j++) 2383 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j]; 2384 else { 2385 if (cpu_has_feature(CPU_FTR_ALTIVEC)) 2386 val->vval = vcpu->arch.vr_tm.vr[i-32]; 2387 else 2388 r = -ENXIO; 2389 } 2390 break; 2391 } 2392 case KVM_REG_PPC_TM_CR: 2393 *val = get_reg_val(id, vcpu->arch.cr_tm); 2394 break; 2395 case KVM_REG_PPC_TM_XER: 2396 *val = get_reg_val(id, vcpu->arch.xer_tm); 2397 break; 2398 case KVM_REG_PPC_TM_LR: 2399 *val = get_reg_val(id, vcpu->arch.lr_tm); 2400 break; 2401 case KVM_REG_PPC_TM_CTR: 2402 *val = get_reg_val(id, vcpu->arch.ctr_tm); 2403 break; 2404 case KVM_REG_PPC_TM_FPSCR: 2405 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr); 2406 break; 2407 case KVM_REG_PPC_TM_AMR: 2408 *val = get_reg_val(id, vcpu->arch.amr_tm); 2409 break; 2410 case KVM_REG_PPC_TM_PPR: 2411 *val = get_reg_val(id, vcpu->arch.ppr_tm); 2412 break; 2413 case KVM_REG_PPC_TM_VRSAVE: 2414 *val = get_reg_val(id, vcpu->arch.vrsave_tm); 2415 break; 2416 case KVM_REG_PPC_TM_VSCR: 2417 if (cpu_has_feature(CPU_FTR_ALTIVEC)) 2418 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]); 2419 else 2420 r = -ENXIO; 2421 break; 2422 case KVM_REG_PPC_TM_DSCR: 2423 *val = get_reg_val(id, vcpu->arch.dscr_tm); 2424 break; 2425 case KVM_REG_PPC_TM_TAR: 2426 *val = get_reg_val(id, vcpu->arch.tar_tm); 2427 break; 2428 #endif 2429 case KVM_REG_PPC_ARCH_COMPAT: 2430 *val = get_reg_val(id, kvmppc_get_arch_compat(vcpu)); 2431 break; 2432 case KVM_REG_PPC_DEC_EXPIRY: 2433 *val = get_reg_val(id, kvmppc_get_dec_expires(vcpu)); 2434 break; 2435 case KVM_REG_PPC_ONLINE: 2436 *val = get_reg_val(id, vcpu->arch.online); 2437 break; 2438 case KVM_REG_PPC_PTCR: 2439 *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr); 2440 break; 2441 case KVM_REG_PPC_FSCR: 2442 *val = get_reg_val(id, kvmppc_get_fscr_hv(vcpu)); 2443 break; 2444 default: 2445 r = -EINVAL; 2446 break; 2447 } 2448 2449 return r; 2450 } 2451 2452 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, 2453 union kvmppc_one_reg *val) 2454 { 2455 int r = 0; 2456 long int i; 2457 unsigned long addr, len; 2458 2459 switch (id) { 2460 case KVM_REG_PPC_HIOR: 2461 /* Only allow this to be set to zero */ 2462 if (set_reg_val(id, *val)) 2463 r = -EINVAL; 2464 break; 2465 case KVM_REG_PPC_DABR: 2466 vcpu->arch.dabr = set_reg_val(id, *val); 2467 break; 2468 case KVM_REG_PPC_DABRX: 2469 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP; 2470 break; 2471 case KVM_REG_PPC_DSCR: 2472 kvmppc_set_dscr_hv(vcpu, set_reg_val(id, *val)); 2473 break; 2474 case KVM_REG_PPC_PURR: 2475 kvmppc_set_purr_hv(vcpu, set_reg_val(id, *val)); 2476 break; 2477 case KVM_REG_PPC_SPURR: 2478 kvmppc_set_spurr_hv(vcpu, set_reg_val(id, *val)); 2479 break; 2480 case KVM_REG_PPC_AMR: 2481 kvmppc_set_amr_hv(vcpu, set_reg_val(id, *val)); 2482 break; 2483 case KVM_REG_PPC_UAMOR: 2484 kvmppc_set_uamor_hv(vcpu, set_reg_val(id, *val)); 2485 break; 2486 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1: 2487 i = id - KVM_REG_PPC_MMCR0; 2488 kvmppc_set_mmcr_hv(vcpu, i, set_reg_val(id, *val)); 2489 break; 2490 case KVM_REG_PPC_MMCR2: 2491 kvmppc_set_mmcr_hv(vcpu, 2, set_reg_val(id, *val)); 2492 break; 2493 case KVM_REG_PPC_MMCRA: 2494 kvmppc_set_mmcra_hv(vcpu, set_reg_val(id, *val)); 2495 break; 2496 case KVM_REG_PPC_MMCRS: 2497 vcpu->arch.mmcrs = set_reg_val(id, *val); 2498 break; 2499 case KVM_REG_PPC_MMCR3: 2500 *val = get_reg_val(id, vcpu->arch.mmcr[3]); 2501 break; 2502 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: 2503 i = id - KVM_REG_PPC_PMC1; 2504 kvmppc_set_pmc_hv(vcpu, i, set_reg_val(id, *val)); 2505 break; 2506 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: 2507 i = id - KVM_REG_PPC_SPMC1; 2508 vcpu->arch.spmc[i] = set_reg_val(id, *val); 2509 break; 2510 case KVM_REG_PPC_SIAR: 2511 kvmppc_set_siar_hv(vcpu, set_reg_val(id, *val)); 2512 break; 2513 case KVM_REG_PPC_SDAR: 2514 kvmppc_set_sdar_hv(vcpu, set_reg_val(id, *val)); 2515 break; 2516 case KVM_REG_PPC_SIER: 2517 kvmppc_set_sier_hv(vcpu, 0, set_reg_val(id, *val)); 2518 break; 2519 case KVM_REG_PPC_SIER2: 2520 kvmppc_set_sier_hv(vcpu, 1, set_reg_val(id, *val)); 2521 break; 2522 case KVM_REG_PPC_SIER3: 2523 kvmppc_set_sier_hv(vcpu, 2, set_reg_val(id, *val)); 2524 break; 2525 case KVM_REG_PPC_IAMR: 2526 kvmppc_set_iamr_hv(vcpu, set_reg_val(id, *val)); 2527 break; 2528 case KVM_REG_PPC_PSPB: 2529 kvmppc_set_pspb_hv(vcpu, set_reg_val(id, *val)); 2530 break; 2531 case KVM_REG_PPC_DPDES: 2532 if (cpu_has_feature(CPU_FTR_ARCH_300)) 2533 vcpu->arch.doorbell_request = set_reg_val(id, *val) & 1; 2534 else 2535 vcpu->arch.vcore->dpdes = set_reg_val(id, *val); 2536 break; 2537 case KVM_REG_PPC_VTB: 2538 kvmppc_set_vtb(vcpu, set_reg_val(id, *val)); 2539 break; 2540 case KVM_REG_PPC_DAWR: 2541 kvmppc_set_dawr0_hv(vcpu, set_reg_val(id, *val)); 2542 break; 2543 case KVM_REG_PPC_DAWRX: 2544 kvmppc_set_dawrx0_hv(vcpu, set_reg_val(id, *val) & ~DAWRX_HYP); 2545 break; 2546 case KVM_REG_PPC_DAWR1: 2547 kvmppc_set_dawr1_hv(vcpu, set_reg_val(id, *val)); 2548 break; 2549 case KVM_REG_PPC_DAWRX1: 2550 kvmppc_set_dawrx1_hv(vcpu, set_reg_val(id, *val) & ~DAWRX_HYP); 2551 break; 2552 case KVM_REG_PPC_CIABR: 2553 kvmppc_set_ciabr_hv(vcpu, set_reg_val(id, *val)); 2554 /* Don't allow setting breakpoints in hypervisor code */ 2555 if ((kvmppc_get_ciabr_hv(vcpu) & CIABR_PRIV) == CIABR_PRIV_HYPER) 2556 kvmppc_set_ciabr_hv(vcpu, kvmppc_get_ciabr_hv(vcpu) & ~CIABR_PRIV); 2557 break; 2558 case KVM_REG_PPC_CSIGR: 2559 vcpu->arch.csigr = set_reg_val(id, *val); 2560 break; 2561 case KVM_REG_PPC_TACR: 2562 vcpu->arch.tacr = set_reg_val(id, *val); 2563 break; 2564 case KVM_REG_PPC_TCSCR: 2565 vcpu->arch.tcscr = set_reg_val(id, *val); 2566 break; 2567 case KVM_REG_PPC_PID: 2568 kvmppc_set_pid(vcpu, set_reg_val(id, *val)); 2569 break; 2570 case KVM_REG_PPC_ACOP: 2571 vcpu->arch.acop = set_reg_val(id, *val); 2572 break; 2573 case KVM_REG_PPC_WORT: 2574 kvmppc_set_wort_hv(vcpu, set_reg_val(id, *val)); 2575 break; 2576 case KVM_REG_PPC_TIDR: 2577 vcpu->arch.tid = set_reg_val(id, *val); 2578 break; 2579 case KVM_REG_PPC_PSSCR: 2580 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS; 2581 break; 2582 case KVM_REG_PPC_VPA_ADDR: 2583 addr = set_reg_val(id, *val); 2584 r = -EINVAL; 2585 if (!addr && (vcpu->arch.slb_shadow.next_gpa || 2586 vcpu->arch.dtl.next_gpa)) 2587 break; 2588 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca)); 2589 break; 2590 case KVM_REG_PPC_VPA_SLB: 2591 addr = val->vpaval.addr; 2592 len = val->vpaval.length; 2593 r = -EINVAL; 2594 if (addr && !vcpu->arch.vpa.next_gpa) 2595 break; 2596 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len); 2597 break; 2598 case KVM_REG_PPC_VPA_DTL: 2599 addr = val->vpaval.addr; 2600 len = val->vpaval.length; 2601 r = -EINVAL; 2602 if (addr && (len < sizeof(struct dtl_entry) || 2603 !vcpu->arch.vpa.next_gpa)) 2604 break; 2605 len -= len % sizeof(struct dtl_entry); 2606 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len); 2607 break; 2608 case KVM_REG_PPC_TB_OFFSET: 2609 { 2610 /* round up to multiple of 2^24 */ 2611 u64 tb_offset = ALIGN(set_reg_val(id, *val), 1UL << 24); 2612 2613 /* 2614 * Now that we know the timebase offset, update the 2615 * decrementer expiry with a guest timebase value. If 2616 * the userspace does not set DEC_EXPIRY, this ensures 2617 * a migrated vcpu at least starts with an expired 2618 * decrementer, which is better than a large one that 2619 * causes a hang. 2620 */ 2621 kvmppc_set_tb_offset(vcpu, tb_offset); 2622 if (!kvmppc_get_dec_expires(vcpu) && tb_offset) 2623 kvmppc_set_dec_expires(vcpu, get_tb() + tb_offset); 2624 2625 kvmppc_set_tb_offset(vcpu, tb_offset); 2626 break; 2627 } 2628 case KVM_REG_PPC_LPCR: 2629 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true); 2630 break; 2631 case KVM_REG_PPC_LPCR_64: 2632 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false); 2633 break; 2634 case KVM_REG_PPC_PPR: 2635 kvmppc_set_ppr_hv(vcpu, set_reg_val(id, *val)); 2636 break; 2637 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 2638 case KVM_REG_PPC_TFHAR: 2639 vcpu->arch.tfhar = set_reg_val(id, *val); 2640 break; 2641 case KVM_REG_PPC_TFIAR: 2642 vcpu->arch.tfiar = set_reg_val(id, *val); 2643 break; 2644 case KVM_REG_PPC_TEXASR: 2645 vcpu->arch.texasr = set_reg_val(id, *val); 2646 break; 2647 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: 2648 i = id - KVM_REG_PPC_TM_GPR0; 2649 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val); 2650 break; 2651 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: 2652 { 2653 int j; 2654 i = id - KVM_REG_PPC_TM_VSR0; 2655 if (i < 32) 2656 for (j = 0; j < TS_FPRWIDTH; j++) 2657 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j]; 2658 else 2659 if (cpu_has_feature(CPU_FTR_ALTIVEC)) 2660 vcpu->arch.vr_tm.vr[i-32] = val->vval; 2661 else 2662 r = -ENXIO; 2663 break; 2664 } 2665 case KVM_REG_PPC_TM_CR: 2666 vcpu->arch.cr_tm = set_reg_val(id, *val); 2667 break; 2668 case KVM_REG_PPC_TM_XER: 2669 vcpu->arch.xer_tm = set_reg_val(id, *val); 2670 break; 2671 case KVM_REG_PPC_TM_LR: 2672 vcpu->arch.lr_tm = set_reg_val(id, *val); 2673 break; 2674 case KVM_REG_PPC_TM_CTR: 2675 vcpu->arch.ctr_tm = set_reg_val(id, *val); 2676 break; 2677 case KVM_REG_PPC_TM_FPSCR: 2678 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val); 2679 break; 2680 case KVM_REG_PPC_TM_AMR: 2681 vcpu->arch.amr_tm = set_reg_val(id, *val); 2682 break; 2683 case KVM_REG_PPC_TM_PPR: 2684 vcpu->arch.ppr_tm = set_reg_val(id, *val); 2685 break; 2686 case KVM_REG_PPC_TM_VRSAVE: 2687 vcpu->arch.vrsave_tm = set_reg_val(id, *val); 2688 break; 2689 case KVM_REG_PPC_TM_VSCR: 2690 if (cpu_has_feature(CPU_FTR_ALTIVEC)) 2691 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val); 2692 else 2693 r = - ENXIO; 2694 break; 2695 case KVM_REG_PPC_TM_DSCR: 2696 vcpu->arch.dscr_tm = set_reg_val(id, *val); 2697 break; 2698 case KVM_REG_PPC_TM_TAR: 2699 vcpu->arch.tar_tm = set_reg_val(id, *val); 2700 break; 2701 #endif 2702 case KVM_REG_PPC_ARCH_COMPAT: 2703 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val)); 2704 break; 2705 case KVM_REG_PPC_DEC_EXPIRY: 2706 kvmppc_set_dec_expires(vcpu, set_reg_val(id, *val)); 2707 break; 2708 case KVM_REG_PPC_ONLINE: 2709 i = set_reg_val(id, *val); 2710 if (i && !vcpu->arch.online) 2711 atomic_inc(&vcpu->arch.vcore->online_count); 2712 else if (!i && vcpu->arch.online) 2713 atomic_dec(&vcpu->arch.vcore->online_count); 2714 vcpu->arch.online = i; 2715 break; 2716 case KVM_REG_PPC_PTCR: 2717 vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val); 2718 break; 2719 case KVM_REG_PPC_FSCR: 2720 kvmppc_set_fscr_hv(vcpu, set_reg_val(id, *val)); 2721 break; 2722 default: 2723 r = -EINVAL; 2724 break; 2725 } 2726 2727 return r; 2728 } 2729 2730 /* 2731 * On POWER9, threads are independent and can be in different partitions. 2732 * Therefore we consider each thread to be a subcore. 2733 * There is a restriction that all threads have to be in the same 2734 * MMU mode (radix or HPT), unfortunately, but since we only support 2735 * HPT guests on a HPT host so far, that isn't an impediment yet. 2736 */ 2737 static int threads_per_vcore(struct kvm *kvm) 2738 { 2739 if (cpu_has_feature(CPU_FTR_ARCH_300)) 2740 return 1; 2741 return threads_per_subcore; 2742 } 2743 2744 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id) 2745 { 2746 struct kvmppc_vcore *vcore; 2747 2748 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL); 2749 2750 if (vcore == NULL) 2751 return NULL; 2752 2753 spin_lock_init(&vcore->lock); 2754 spin_lock_init(&vcore->stoltb_lock); 2755 rcuwait_init(&vcore->wait); 2756 vcore->preempt_tb = TB_NIL; 2757 vcore->lpcr = kvm->arch.lpcr; 2758 vcore->first_vcpuid = id; 2759 vcore->kvm = kvm; 2760 INIT_LIST_HEAD(&vcore->preempt_list); 2761 2762 return vcore; 2763 } 2764 2765 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING 2766 static struct debugfs_timings_element { 2767 const char *name; 2768 size_t offset; 2769 } timings[] = { 2770 #ifdef CONFIG_KVM_BOOK3S_HV_P9_TIMING 2771 {"vcpu_entry", offsetof(struct kvm_vcpu, arch.vcpu_entry)}, 2772 {"guest_entry", offsetof(struct kvm_vcpu, arch.guest_entry)}, 2773 {"in_guest", offsetof(struct kvm_vcpu, arch.in_guest)}, 2774 {"guest_exit", offsetof(struct kvm_vcpu, arch.guest_exit)}, 2775 {"vcpu_exit", offsetof(struct kvm_vcpu, arch.vcpu_exit)}, 2776 {"hypercall", offsetof(struct kvm_vcpu, arch.hcall)}, 2777 {"page_fault", offsetof(struct kvm_vcpu, arch.pg_fault)}, 2778 #else 2779 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)}, 2780 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)}, 2781 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)}, 2782 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)}, 2783 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)}, 2784 #endif 2785 }; 2786 2787 #define N_TIMINGS (ARRAY_SIZE(timings)) 2788 2789 struct debugfs_timings_state { 2790 struct kvm_vcpu *vcpu; 2791 unsigned int buflen; 2792 char buf[N_TIMINGS * 100]; 2793 }; 2794 2795 static int debugfs_timings_open(struct inode *inode, struct file *file) 2796 { 2797 struct kvm_vcpu *vcpu = inode->i_private; 2798 struct debugfs_timings_state *p; 2799 2800 p = kzalloc(sizeof(*p), GFP_KERNEL); 2801 if (!p) 2802 return -ENOMEM; 2803 2804 kvm_get_kvm(vcpu->kvm); 2805 p->vcpu = vcpu; 2806 file->private_data = p; 2807 2808 return nonseekable_open(inode, file); 2809 } 2810 2811 static int debugfs_timings_release(struct inode *inode, struct file *file) 2812 { 2813 struct debugfs_timings_state *p = file->private_data; 2814 2815 kvm_put_kvm(p->vcpu->kvm); 2816 kfree(p); 2817 return 0; 2818 } 2819 2820 static ssize_t debugfs_timings_read(struct file *file, char __user *buf, 2821 size_t len, loff_t *ppos) 2822 { 2823 struct debugfs_timings_state *p = file->private_data; 2824 struct kvm_vcpu *vcpu = p->vcpu; 2825 char *s, *buf_end; 2826 struct kvmhv_tb_accumulator tb; 2827 u64 count; 2828 loff_t pos; 2829 ssize_t n; 2830 int i, loops; 2831 bool ok; 2832 2833 if (!p->buflen) { 2834 s = p->buf; 2835 buf_end = s + sizeof(p->buf); 2836 for (i = 0; i < N_TIMINGS; ++i) { 2837 struct kvmhv_tb_accumulator *acc; 2838 2839 acc = (struct kvmhv_tb_accumulator *) 2840 ((unsigned long)vcpu + timings[i].offset); 2841 ok = false; 2842 for (loops = 0; loops < 1000; ++loops) { 2843 count = acc->seqcount; 2844 if (!(count & 1)) { 2845 smp_rmb(); 2846 tb = *acc; 2847 smp_rmb(); 2848 if (count == acc->seqcount) { 2849 ok = true; 2850 break; 2851 } 2852 } 2853 udelay(1); 2854 } 2855 if (!ok) 2856 snprintf(s, buf_end - s, "%s: stuck\n", 2857 timings[i].name); 2858 else 2859 snprintf(s, buf_end - s, 2860 "%s: %llu %llu %llu %llu\n", 2861 timings[i].name, count / 2, 2862 tb_to_ns(tb.tb_total), 2863 tb_to_ns(tb.tb_min), 2864 tb_to_ns(tb.tb_max)); 2865 s += strlen(s); 2866 } 2867 p->buflen = s - p->buf; 2868 } 2869 2870 pos = *ppos; 2871 if (pos >= p->buflen) 2872 return 0; 2873 if (len > p->buflen - pos) 2874 len = p->buflen - pos; 2875 n = copy_to_user(buf, p->buf + pos, len); 2876 if (n) { 2877 if (n == len) 2878 return -EFAULT; 2879 len -= n; 2880 } 2881 *ppos = pos + len; 2882 return len; 2883 } 2884 2885 static ssize_t debugfs_timings_write(struct file *file, const char __user *buf, 2886 size_t len, loff_t *ppos) 2887 { 2888 return -EACCES; 2889 } 2890 2891 static const struct file_operations debugfs_timings_ops = { 2892 .owner = THIS_MODULE, 2893 .open = debugfs_timings_open, 2894 .release = debugfs_timings_release, 2895 .read = debugfs_timings_read, 2896 .write = debugfs_timings_write, 2897 .llseek = generic_file_llseek, 2898 }; 2899 2900 /* Create a debugfs directory for the vcpu */ 2901 static int kvmppc_arch_create_vcpu_debugfs_hv(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry) 2902 { 2903 if (cpu_has_feature(CPU_FTR_ARCH_300) == IS_ENABLED(CONFIG_KVM_BOOK3S_HV_P9_TIMING)) 2904 debugfs_create_file("timings", 0444, debugfs_dentry, vcpu, 2905 &debugfs_timings_ops); 2906 return 0; 2907 } 2908 2909 #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ 2910 static int kvmppc_arch_create_vcpu_debugfs_hv(struct kvm_vcpu *vcpu, struct dentry *debugfs_dentry) 2911 { 2912 return 0; 2913 } 2914 #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ 2915 2916 static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu) 2917 { 2918 int err; 2919 int core; 2920 struct kvmppc_vcore *vcore; 2921 struct kvm *kvm; 2922 unsigned int id; 2923 2924 kvm = vcpu->kvm; 2925 id = vcpu->vcpu_id; 2926 2927 vcpu->arch.shared = &vcpu->arch.shregs; 2928 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE 2929 /* 2930 * The shared struct is never shared on HV, 2931 * so we can always use host endianness 2932 */ 2933 #ifdef __BIG_ENDIAN__ 2934 vcpu->arch.shared_big_endian = true; 2935 #else 2936 vcpu->arch.shared_big_endian = false; 2937 #endif 2938 #endif 2939 2940 if (kvmhv_is_nestedv2()) { 2941 err = kvmhv_nestedv2_vcpu_create(vcpu, &vcpu->arch.nestedv2_io); 2942 if (err < 0) 2943 return err; 2944 } 2945 2946 kvmppc_set_mmcr_hv(vcpu, 0, MMCR0_FC); 2947 if (cpu_has_feature(CPU_FTR_ARCH_31)) { 2948 kvmppc_set_mmcr_hv(vcpu, 0, kvmppc_get_mmcr_hv(vcpu, 0) | MMCR0_PMCCEXT); 2949 kvmppc_set_mmcra_hv(vcpu, MMCRA_BHRB_DISABLE); 2950 } 2951 2952 kvmppc_set_ctrl_hv(vcpu, CTRL_RUNLATCH); 2953 /* default to host PVR, since we can't spoof it */ 2954 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR)); 2955 spin_lock_init(&vcpu->arch.vpa_update_lock); 2956 spin_lock_init(&vcpu->arch.tbacct_lock); 2957 vcpu->arch.busy_preempt = TB_NIL; 2958 __kvmppc_set_msr_hv(vcpu, MSR_ME); 2959 vcpu->arch.intr_msr = MSR_SF | MSR_ME; 2960 2961 /* 2962 * Set the default HFSCR for the guest from the host value. 2963 * This value is only used on POWER9 and later. 2964 * On >= POWER9, we want to virtualize the doorbell facility, so we 2965 * don't set the HFSCR_MSGP bit, and that causes those instructions 2966 * to trap and then we emulate them. 2967 */ 2968 kvmppc_set_hfscr_hv(vcpu, HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB | 2969 HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP); 2970 2971 /* On POWER10 and later, allow prefixed instructions */ 2972 if (cpu_has_feature(CPU_FTR_ARCH_31)) 2973 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_PREFIX); 2974 2975 if (cpu_has_feature(CPU_FTR_HVMODE)) { 2976 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) & mfspr(SPRN_HFSCR)); 2977 2978 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 2979 if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) 2980 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) | HFSCR_TM); 2981 #endif 2982 } 2983 if (cpu_has_feature(CPU_FTR_TM_COMP)) 2984 vcpu->arch.hfscr |= HFSCR_TM; 2985 2986 vcpu->arch.hfscr_permitted = kvmppc_get_hfscr_hv(vcpu); 2987 2988 /* 2989 * PM, EBB, TM are demand-faulted so start with it clear. 2990 */ 2991 kvmppc_set_hfscr_hv(vcpu, kvmppc_get_hfscr_hv(vcpu) & ~(HFSCR_PM | HFSCR_EBB | HFSCR_TM)); 2992 2993 kvmppc_mmu_book3s_hv_init(vcpu); 2994 2995 vcpu->arch.state = KVMPPC_VCPU_NOTREADY; 2996 2997 init_waitqueue_head(&vcpu->arch.cpu_run); 2998 2999 mutex_lock(&kvm->lock); 3000 vcore = NULL; 3001 err = -EINVAL; 3002 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 3003 if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) { 3004 pr_devel("KVM: VCPU ID too high\n"); 3005 core = KVM_MAX_VCORES; 3006 } else { 3007 BUG_ON(kvm->arch.smt_mode != 1); 3008 core = kvmppc_pack_vcpu_id(kvm, id); 3009 } 3010 } else { 3011 core = id / kvm->arch.smt_mode; 3012 } 3013 if (core < KVM_MAX_VCORES) { 3014 vcore = kvm->arch.vcores[core]; 3015 if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) { 3016 pr_devel("KVM: collision on id %u", id); 3017 vcore = NULL; 3018 } else if (!vcore) { 3019 /* 3020 * Take mmu_setup_lock for mutual exclusion 3021 * with kvmppc_update_lpcr(). 3022 */ 3023 err = -ENOMEM; 3024 vcore = kvmppc_vcore_create(kvm, 3025 id & ~(kvm->arch.smt_mode - 1)); 3026 mutex_lock(&kvm->arch.mmu_setup_lock); 3027 kvm->arch.vcores[core] = vcore; 3028 kvm->arch.online_vcores++; 3029 mutex_unlock(&kvm->arch.mmu_setup_lock); 3030 } 3031 } 3032 mutex_unlock(&kvm->lock); 3033 3034 if (!vcore) 3035 return err; 3036 3037 spin_lock(&vcore->lock); 3038 ++vcore->num_threads; 3039 spin_unlock(&vcore->lock); 3040 vcpu->arch.vcore = vcore; 3041 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid; 3042 vcpu->arch.thread_cpu = -1; 3043 vcpu->arch.prev_cpu = -1; 3044 3045 vcpu->arch.cpu_type = KVM_CPU_3S_64; 3046 kvmppc_sanity_check(vcpu); 3047 3048 return 0; 3049 } 3050 3051 static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode, 3052 unsigned long flags) 3053 { 3054 int err; 3055 int esmt = 0; 3056 3057 if (flags) 3058 return -EINVAL; 3059 if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode)) 3060 return -EINVAL; 3061 if (!cpu_has_feature(CPU_FTR_ARCH_300)) { 3062 /* 3063 * On POWER8 (or POWER7), the threading mode is "strict", 3064 * so we pack smt_mode vcpus per vcore. 3065 */ 3066 if (smt_mode > threads_per_subcore) 3067 return -EINVAL; 3068 } else { 3069 /* 3070 * On POWER9, the threading mode is "loose", 3071 * so each vcpu gets its own vcore. 3072 */ 3073 esmt = smt_mode; 3074 smt_mode = 1; 3075 } 3076 mutex_lock(&kvm->lock); 3077 err = -EBUSY; 3078 if (!kvm->arch.online_vcores) { 3079 kvm->arch.smt_mode = smt_mode; 3080 kvm->arch.emul_smt_mode = esmt; 3081 err = 0; 3082 } 3083 mutex_unlock(&kvm->lock); 3084 3085 return err; 3086 } 3087 3088 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa) 3089 { 3090 if (vpa->pinned_addr) 3091 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa, 3092 vpa->dirty); 3093 } 3094 3095 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu) 3096 { 3097 spin_lock(&vcpu->arch.vpa_update_lock); 3098 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl); 3099 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow); 3100 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa); 3101 spin_unlock(&vcpu->arch.vpa_update_lock); 3102 if (kvmhv_is_nestedv2()) 3103 kvmhv_nestedv2_vcpu_free(vcpu, &vcpu->arch.nestedv2_io); 3104 } 3105 3106 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu) 3107 { 3108 /* Indicate we want to get back into the guest */ 3109 return 1; 3110 } 3111 3112 static void kvmppc_set_timer(struct kvm_vcpu *vcpu) 3113 { 3114 unsigned long dec_nsec, now; 3115 3116 now = get_tb(); 3117 if (now > kvmppc_dec_expires_host_tb(vcpu)) { 3118 /* decrementer has already gone negative */ 3119 kvmppc_core_queue_dec(vcpu); 3120 kvmppc_core_prepare_to_enter(vcpu); 3121 return; 3122 } 3123 dec_nsec = tb_to_ns(kvmppc_dec_expires_host_tb(vcpu) - now); 3124 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL); 3125 vcpu->arch.timer_running = 1; 3126 } 3127 3128 extern int __kvmppc_vcore_entry(void); 3129 3130 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, 3131 struct kvm_vcpu *vcpu, u64 tb) 3132 { 3133 u64 now; 3134 3135 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) 3136 return; 3137 spin_lock_irq(&vcpu->arch.tbacct_lock); 3138 now = tb; 3139 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) - 3140 vcpu->arch.stolen_logged; 3141 vcpu->arch.busy_preempt = now; 3142 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; 3143 spin_unlock_irq(&vcpu->arch.tbacct_lock); 3144 --vc->n_runnable; 3145 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL); 3146 } 3147 3148 static int kvmppc_grab_hwthread(int cpu) 3149 { 3150 struct paca_struct *tpaca; 3151 long timeout = 10000; 3152 3153 tpaca = paca_ptrs[cpu]; 3154 3155 /* Ensure the thread won't go into the kernel if it wakes */ 3156 tpaca->kvm_hstate.kvm_vcpu = NULL; 3157 tpaca->kvm_hstate.kvm_vcore = NULL; 3158 tpaca->kvm_hstate.napping = 0; 3159 smp_wmb(); 3160 tpaca->kvm_hstate.hwthread_req = 1; 3161 3162 /* 3163 * If the thread is already executing in the kernel (e.g. handling 3164 * a stray interrupt), wait for it to get back to nap mode. 3165 * The smp_mb() is to ensure that our setting of hwthread_req 3166 * is visible before we look at hwthread_state, so if this 3167 * races with the code at system_reset_pSeries and the thread 3168 * misses our setting of hwthread_req, we are sure to see its 3169 * setting of hwthread_state, and vice versa. 3170 */ 3171 smp_mb(); 3172 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) { 3173 if (--timeout <= 0) { 3174 pr_err("KVM: couldn't grab cpu %d\n", cpu); 3175 return -EBUSY; 3176 } 3177 udelay(1); 3178 } 3179 return 0; 3180 } 3181 3182 static void kvmppc_release_hwthread(int cpu) 3183 { 3184 struct paca_struct *tpaca; 3185 3186 tpaca = paca_ptrs[cpu]; 3187 tpaca->kvm_hstate.hwthread_req = 0; 3188 tpaca->kvm_hstate.kvm_vcpu = NULL; 3189 tpaca->kvm_hstate.kvm_vcore = NULL; 3190 tpaca->kvm_hstate.kvm_split_mode = NULL; 3191 } 3192 3193 static DEFINE_PER_CPU(struct kvm *, cpu_in_guest); 3194 3195 static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu) 3196 { 3197 struct kvm_nested_guest *nested = vcpu->arch.nested; 3198 cpumask_t *need_tlb_flush; 3199 int i; 3200 3201 if (nested) 3202 need_tlb_flush = &nested->need_tlb_flush; 3203 else 3204 need_tlb_flush = &kvm->arch.need_tlb_flush; 3205 3206 cpu = cpu_first_tlb_thread_sibling(cpu); 3207 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu); 3208 i += cpu_tlb_thread_sibling_step()) 3209 cpumask_set_cpu(i, need_tlb_flush); 3210 3211 /* 3212 * Make sure setting of bit in need_tlb_flush precedes testing of 3213 * cpu_in_guest. The matching barrier on the other side is hwsync 3214 * when switching to guest MMU mode, which happens between 3215 * cpu_in_guest being set to the guest kvm, and need_tlb_flush bit 3216 * being tested. 3217 */ 3218 smp_mb(); 3219 3220 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu); 3221 i += cpu_tlb_thread_sibling_step()) { 3222 struct kvm *running = *per_cpu_ptr(&cpu_in_guest, i); 3223 3224 if (running == kvm) 3225 smp_call_function_single(i, do_nothing, NULL, 1); 3226 } 3227 } 3228 3229 static void do_migrate_away_vcpu(void *arg) 3230 { 3231 struct kvm_vcpu *vcpu = arg; 3232 struct kvm *kvm = vcpu->kvm; 3233 3234 /* 3235 * If the guest has GTSE, it may execute tlbie, so do a eieio; tlbsync; 3236 * ptesync sequence on the old CPU before migrating to a new one, in 3237 * case we interrupted the guest between a tlbie ; eieio ; 3238 * tlbsync; ptesync sequence. 3239 * 3240 * Otherwise, ptesync is sufficient for ordering tlbiel sequences. 3241 */ 3242 if (kvm->arch.lpcr & LPCR_GTSE) 3243 asm volatile("eieio; tlbsync; ptesync"); 3244 else 3245 asm volatile("ptesync"); 3246 } 3247 3248 static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu) 3249 { 3250 struct kvm_nested_guest *nested = vcpu->arch.nested; 3251 struct kvm *kvm = vcpu->kvm; 3252 int prev_cpu; 3253 3254 if (!cpu_has_feature(CPU_FTR_HVMODE)) 3255 return; 3256 3257 if (nested) 3258 prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id]; 3259 else 3260 prev_cpu = vcpu->arch.prev_cpu; 3261 3262 /* 3263 * With radix, the guest can do TLB invalidations itself, 3264 * and it could choose to use the local form (tlbiel) if 3265 * it is invalidating a translation that has only ever been 3266 * used on one vcpu. However, that doesn't mean it has 3267 * only ever been used on one physical cpu, since vcpus 3268 * can move around between pcpus. To cope with this, when 3269 * a vcpu moves from one pcpu to another, we need to tell 3270 * any vcpus running on the same core as this vcpu previously 3271 * ran to flush the TLB. 3272 */ 3273 if (prev_cpu != pcpu) { 3274 if (prev_cpu >= 0) { 3275 if (cpu_first_tlb_thread_sibling(prev_cpu) != 3276 cpu_first_tlb_thread_sibling(pcpu)) 3277 radix_flush_cpu(kvm, prev_cpu, vcpu); 3278 3279 smp_call_function_single(prev_cpu, 3280 do_migrate_away_vcpu, vcpu, 1); 3281 } 3282 if (nested) 3283 nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu; 3284 else 3285 vcpu->arch.prev_cpu = pcpu; 3286 } 3287 } 3288 3289 static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc) 3290 { 3291 int cpu; 3292 struct paca_struct *tpaca; 3293 3294 cpu = vc->pcpu; 3295 if (vcpu) { 3296 if (vcpu->arch.timer_running) { 3297 hrtimer_try_to_cancel(&vcpu->arch.dec_timer); 3298 vcpu->arch.timer_running = 0; 3299 } 3300 cpu += vcpu->arch.ptid; 3301 vcpu->cpu = vc->pcpu; 3302 vcpu->arch.thread_cpu = cpu; 3303 } 3304 tpaca = paca_ptrs[cpu]; 3305 tpaca->kvm_hstate.kvm_vcpu = vcpu; 3306 tpaca->kvm_hstate.ptid = cpu - vc->pcpu; 3307 tpaca->kvm_hstate.fake_suspend = 0; 3308 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */ 3309 smp_wmb(); 3310 tpaca->kvm_hstate.kvm_vcore = vc; 3311 if (cpu != smp_processor_id()) 3312 kvmppc_ipi_thread(cpu); 3313 } 3314 3315 static void kvmppc_wait_for_nap(int n_threads) 3316 { 3317 int cpu = smp_processor_id(); 3318 int i, loops; 3319 3320 if (n_threads <= 1) 3321 return; 3322 for (loops = 0; loops < 1000000; ++loops) { 3323 /* 3324 * Check if all threads are finished. 3325 * We set the vcore pointer when starting a thread 3326 * and the thread clears it when finished, so we look 3327 * for any threads that still have a non-NULL vcore ptr. 3328 */ 3329 for (i = 1; i < n_threads; ++i) 3330 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore) 3331 break; 3332 if (i == n_threads) { 3333 HMT_medium(); 3334 return; 3335 } 3336 HMT_low(); 3337 } 3338 HMT_medium(); 3339 for (i = 1; i < n_threads; ++i) 3340 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore) 3341 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i); 3342 } 3343 3344 /* 3345 * Check that we are on thread 0 and that any other threads in 3346 * this core are off-line. Then grab the threads so they can't 3347 * enter the kernel. 3348 */ 3349 static int on_primary_thread(void) 3350 { 3351 int cpu = smp_processor_id(); 3352 int thr; 3353 3354 /* Are we on a primary subcore? */ 3355 if (cpu_thread_in_subcore(cpu)) 3356 return 0; 3357 3358 thr = 0; 3359 while (++thr < threads_per_subcore) 3360 if (cpu_online(cpu + thr)) 3361 return 0; 3362 3363 /* Grab all hw threads so they can't go into the kernel */ 3364 for (thr = 1; thr < threads_per_subcore; ++thr) { 3365 if (kvmppc_grab_hwthread(cpu + thr)) { 3366 /* Couldn't grab one; let the others go */ 3367 do { 3368 kvmppc_release_hwthread(cpu + thr); 3369 } while (--thr > 0); 3370 return 0; 3371 } 3372 } 3373 return 1; 3374 } 3375 3376 /* 3377 * A list of virtual cores for each physical CPU. 3378 * These are vcores that could run but their runner VCPU tasks are 3379 * (or may be) preempted. 3380 */ 3381 struct preempted_vcore_list { 3382 struct list_head list; 3383 spinlock_t lock; 3384 }; 3385 3386 static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores); 3387 3388 static void init_vcore_lists(void) 3389 { 3390 int cpu; 3391 3392 for_each_possible_cpu(cpu) { 3393 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu); 3394 spin_lock_init(&lp->lock); 3395 INIT_LIST_HEAD(&lp->list); 3396 } 3397 } 3398 3399 static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc) 3400 { 3401 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); 3402 3403 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)); 3404 3405 vc->vcore_state = VCORE_PREEMPT; 3406 vc->pcpu = smp_processor_id(); 3407 if (vc->num_threads < threads_per_vcore(vc->kvm)) { 3408 spin_lock(&lp->lock); 3409 list_add_tail(&vc->preempt_list, &lp->list); 3410 spin_unlock(&lp->lock); 3411 } 3412 3413 /* Start accumulating stolen time */ 3414 kvmppc_core_start_stolen(vc, mftb()); 3415 } 3416 3417 static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc) 3418 { 3419 struct preempted_vcore_list *lp; 3420 3421 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)); 3422 3423 kvmppc_core_end_stolen(vc, mftb()); 3424 if (!list_empty(&vc->preempt_list)) { 3425 lp = &per_cpu(preempted_vcores, vc->pcpu); 3426 spin_lock(&lp->lock); 3427 list_del_init(&vc->preempt_list); 3428 spin_unlock(&lp->lock); 3429 } 3430 vc->vcore_state = VCORE_INACTIVE; 3431 } 3432 3433 /* 3434 * This stores information about the virtual cores currently 3435 * assigned to a physical core. 3436 */ 3437 struct core_info { 3438 int n_subcores; 3439 int max_subcore_threads; 3440 int total_threads; 3441 int subcore_threads[MAX_SUBCORES]; 3442 struct kvmppc_vcore *vc[MAX_SUBCORES]; 3443 }; 3444 3445 /* 3446 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7 3447 * respectively in 2-way micro-threading (split-core) mode on POWER8. 3448 */ 3449 static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 }; 3450 3451 static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc) 3452 { 3453 memset(cip, 0, sizeof(*cip)); 3454 cip->n_subcores = 1; 3455 cip->max_subcore_threads = vc->num_threads; 3456 cip->total_threads = vc->num_threads; 3457 cip->subcore_threads[0] = vc->num_threads; 3458 cip->vc[0] = vc; 3459 } 3460 3461 static bool subcore_config_ok(int n_subcores, int n_threads) 3462 { 3463 /* 3464 * POWER9 "SMT4" cores are permanently in what is effectively a 4-way 3465 * split-core mode, with one thread per subcore. 3466 */ 3467 if (cpu_has_feature(CPU_FTR_ARCH_300)) 3468 return n_subcores <= 4 && n_threads == 1; 3469 3470 /* On POWER8, can only dynamically split if unsplit to begin with */ 3471 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS) 3472 return false; 3473 if (n_subcores > MAX_SUBCORES) 3474 return false; 3475 if (n_subcores > 1) { 3476 if (!(dynamic_mt_modes & 2)) 3477 n_subcores = 4; 3478 if (n_subcores > 2 && !(dynamic_mt_modes & 4)) 3479 return false; 3480 } 3481 3482 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS; 3483 } 3484 3485 static void init_vcore_to_run(struct kvmppc_vcore *vc) 3486 { 3487 vc->entry_exit_map = 0; 3488 vc->in_guest = 0; 3489 vc->napping_threads = 0; 3490 vc->conferring_threads = 0; 3491 vc->tb_offset_applied = 0; 3492 } 3493 3494 static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip) 3495 { 3496 int n_threads = vc->num_threads; 3497 int sub; 3498 3499 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) 3500 return false; 3501 3502 /* In one_vm_per_core mode, require all vcores to be from the same vm */ 3503 if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm) 3504 return false; 3505 3506 if (n_threads < cip->max_subcore_threads) 3507 n_threads = cip->max_subcore_threads; 3508 if (!subcore_config_ok(cip->n_subcores + 1, n_threads)) 3509 return false; 3510 cip->max_subcore_threads = n_threads; 3511 3512 sub = cip->n_subcores; 3513 ++cip->n_subcores; 3514 cip->total_threads += vc->num_threads; 3515 cip->subcore_threads[sub] = vc->num_threads; 3516 cip->vc[sub] = vc; 3517 init_vcore_to_run(vc); 3518 list_del_init(&vc->preempt_list); 3519 3520 return true; 3521 } 3522 3523 /* 3524 * Work out whether it is possible to piggyback the execution of 3525 * vcore *pvc onto the execution of the other vcores described in *cip. 3526 */ 3527 static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip, 3528 int target_threads) 3529 { 3530 if (cip->total_threads + pvc->num_threads > target_threads) 3531 return false; 3532 3533 return can_dynamic_split(pvc, cip); 3534 } 3535 3536 static void prepare_threads(struct kvmppc_vcore *vc) 3537 { 3538 int i; 3539 struct kvm_vcpu *vcpu; 3540 3541 for_each_runnable_thread(i, vcpu, vc) { 3542 if (signal_pending(vcpu->arch.run_task)) 3543 vcpu->arch.ret = -EINTR; 3544 else if (vcpu->arch.vpa.update_pending || 3545 vcpu->arch.slb_shadow.update_pending || 3546 vcpu->arch.dtl.update_pending) 3547 vcpu->arch.ret = RESUME_GUEST; 3548 else 3549 continue; 3550 kvmppc_remove_runnable(vc, vcpu, mftb()); 3551 wake_up(&vcpu->arch.cpu_run); 3552 } 3553 } 3554 3555 static void collect_piggybacks(struct core_info *cip, int target_threads) 3556 { 3557 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); 3558 struct kvmppc_vcore *pvc, *vcnext; 3559 3560 spin_lock(&lp->lock); 3561 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) { 3562 if (!spin_trylock(&pvc->lock)) 3563 continue; 3564 prepare_threads(pvc); 3565 if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) { 3566 list_del_init(&pvc->preempt_list); 3567 if (pvc->runner == NULL) { 3568 pvc->vcore_state = VCORE_INACTIVE; 3569 kvmppc_core_end_stolen(pvc, mftb()); 3570 } 3571 spin_unlock(&pvc->lock); 3572 continue; 3573 } 3574 if (!can_piggyback(pvc, cip, target_threads)) { 3575 spin_unlock(&pvc->lock); 3576 continue; 3577 } 3578 kvmppc_core_end_stolen(pvc, mftb()); 3579 pvc->vcore_state = VCORE_PIGGYBACK; 3580 if (cip->total_threads >= target_threads) 3581 break; 3582 } 3583 spin_unlock(&lp->lock); 3584 } 3585 3586 static bool recheck_signals_and_mmu(struct core_info *cip) 3587 { 3588 int sub, i; 3589 struct kvm_vcpu *vcpu; 3590 struct kvmppc_vcore *vc; 3591 3592 for (sub = 0; sub < cip->n_subcores; ++sub) { 3593 vc = cip->vc[sub]; 3594 if (!vc->kvm->arch.mmu_ready) 3595 return true; 3596 for_each_runnable_thread(i, vcpu, vc) 3597 if (signal_pending(vcpu->arch.run_task)) 3598 return true; 3599 } 3600 return false; 3601 } 3602 3603 static void post_guest_process(struct kvmppc_vcore *vc, bool is_master) 3604 { 3605 int still_running = 0, i; 3606 u64 now; 3607 long ret; 3608 struct kvm_vcpu *vcpu; 3609 3610 spin_lock(&vc->lock); 3611 now = get_tb(); 3612 for_each_runnable_thread(i, vcpu, vc) { 3613 /* 3614 * It's safe to unlock the vcore in the loop here, because 3615 * for_each_runnable_thread() is safe against removal of 3616 * the vcpu, and the vcore state is VCORE_EXITING here, 3617 * so any vcpus becoming runnable will have their arch.trap 3618 * set to zero and can't actually run in the guest. 3619 */ 3620 spin_unlock(&vc->lock); 3621 /* cancel pending dec exception if dec is positive */ 3622 if (now < kvmppc_dec_expires_host_tb(vcpu) && 3623 kvmppc_core_pending_dec(vcpu)) 3624 kvmppc_core_dequeue_dec(vcpu); 3625 3626 trace_kvm_guest_exit(vcpu); 3627 3628 ret = RESUME_GUEST; 3629 if (vcpu->arch.trap) 3630 ret = kvmppc_handle_exit_hv(vcpu, 3631 vcpu->arch.run_task); 3632 3633 vcpu->arch.ret = ret; 3634 vcpu->arch.trap = 0; 3635 3636 spin_lock(&vc->lock); 3637 if (is_kvmppc_resume_guest(vcpu->arch.ret)) { 3638 if (vcpu->arch.pending_exceptions) 3639 kvmppc_core_prepare_to_enter(vcpu); 3640 if (vcpu->arch.ceded) 3641 kvmppc_set_timer(vcpu); 3642 else 3643 ++still_running; 3644 } else { 3645 kvmppc_remove_runnable(vc, vcpu, mftb()); 3646 wake_up(&vcpu->arch.cpu_run); 3647 } 3648 } 3649 if (!is_master) { 3650 if (still_running > 0) { 3651 kvmppc_vcore_preempt(vc); 3652 } else if (vc->runner) { 3653 vc->vcore_state = VCORE_PREEMPT; 3654 kvmppc_core_start_stolen(vc, mftb()); 3655 } else { 3656 vc->vcore_state = VCORE_INACTIVE; 3657 } 3658 if (vc->n_runnable > 0 && vc->runner == NULL) { 3659 /* make sure there's a candidate runner awake */ 3660 i = -1; 3661 vcpu = next_runnable_thread(vc, &i); 3662 wake_up(&vcpu->arch.cpu_run); 3663 } 3664 } 3665 spin_unlock(&vc->lock); 3666 } 3667 3668 /* 3669 * Clear core from the list of active host cores as we are about to 3670 * enter the guest. Only do this if it is the primary thread of the 3671 * core (not if a subcore) that is entering the guest. 3672 */ 3673 static inline int kvmppc_clear_host_core(unsigned int cpu) 3674 { 3675 int core; 3676 3677 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu)) 3678 return 0; 3679 /* 3680 * Memory barrier can be omitted here as we will do a smp_wmb() 3681 * later in kvmppc_start_thread and we need ensure that state is 3682 * visible to other CPUs only after we enter guest. 3683 */ 3684 core = cpu >> threads_shift; 3685 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0; 3686 return 0; 3687 } 3688 3689 /* 3690 * Advertise this core as an active host core since we exited the guest 3691 * Only need to do this if it is the primary thread of the core that is 3692 * exiting. 3693 */ 3694 static inline int kvmppc_set_host_core(unsigned int cpu) 3695 { 3696 int core; 3697 3698 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu)) 3699 return 0; 3700 3701 /* 3702 * Memory barrier can be omitted here because we do a spin_unlock 3703 * immediately after this which provides the memory barrier. 3704 */ 3705 core = cpu >> threads_shift; 3706 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1; 3707 return 0; 3708 } 3709 3710 static void set_irq_happened(int trap) 3711 { 3712 switch (trap) { 3713 case BOOK3S_INTERRUPT_EXTERNAL: 3714 local_paca->irq_happened |= PACA_IRQ_EE; 3715 break; 3716 case BOOK3S_INTERRUPT_H_DOORBELL: 3717 local_paca->irq_happened |= PACA_IRQ_DBELL; 3718 break; 3719 case BOOK3S_INTERRUPT_HMI: 3720 local_paca->irq_happened |= PACA_IRQ_HMI; 3721 break; 3722 case BOOK3S_INTERRUPT_SYSTEM_RESET: 3723 replay_system_reset(); 3724 break; 3725 } 3726 } 3727 3728 /* 3729 * Run a set of guest threads on a physical core. 3730 * Called with vc->lock held. 3731 */ 3732 static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) 3733 { 3734 struct kvm_vcpu *vcpu; 3735 int i; 3736 int srcu_idx; 3737 struct core_info core_info; 3738 struct kvmppc_vcore *pvc; 3739 struct kvm_split_mode split_info, *sip; 3740 int split, subcore_size, active; 3741 int sub; 3742 bool thr0_done; 3743 unsigned long cmd_bit, stat_bit; 3744 int pcpu, thr; 3745 int target_threads; 3746 int controlled_threads; 3747 int trap; 3748 bool is_power8; 3749 3750 if (WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300))) 3751 return; 3752 3753 /* 3754 * Remove from the list any threads that have a signal pending 3755 * or need a VPA update done 3756 */ 3757 prepare_threads(vc); 3758 3759 /* if the runner is no longer runnable, let the caller pick a new one */ 3760 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE) 3761 return; 3762 3763 /* 3764 * Initialize *vc. 3765 */ 3766 init_vcore_to_run(vc); 3767 vc->preempt_tb = TB_NIL; 3768 3769 /* 3770 * Number of threads that we will be controlling: the same as 3771 * the number of threads per subcore, except on POWER9, 3772 * where it's 1 because the threads are (mostly) independent. 3773 */ 3774 controlled_threads = threads_per_vcore(vc->kvm); 3775 3776 /* 3777 * Make sure we are running on primary threads, and that secondary 3778 * threads are offline. Also check if the number of threads in this 3779 * guest are greater than the current system threads per guest. 3780 */ 3781 if ((controlled_threads > 1) && 3782 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) { 3783 for_each_runnable_thread(i, vcpu, vc) { 3784 vcpu->arch.ret = -EBUSY; 3785 kvmppc_remove_runnable(vc, vcpu, mftb()); 3786 wake_up(&vcpu->arch.cpu_run); 3787 } 3788 goto out; 3789 } 3790 3791 /* 3792 * See if we could run any other vcores on the physical core 3793 * along with this one. 3794 */ 3795 init_core_info(&core_info, vc); 3796 pcpu = smp_processor_id(); 3797 target_threads = controlled_threads; 3798 if (target_smt_mode && target_smt_mode < target_threads) 3799 target_threads = target_smt_mode; 3800 if (vc->num_threads < target_threads) 3801 collect_piggybacks(&core_info, target_threads); 3802 3803 /* 3804 * Hard-disable interrupts, and check resched flag and signals. 3805 * If we need to reschedule or deliver a signal, clean up 3806 * and return without going into the guest(s). 3807 * If the mmu_ready flag has been cleared, don't go into the 3808 * guest because that means a HPT resize operation is in progress. 3809 */ 3810 local_irq_disable(); 3811 hard_irq_disable(); 3812 if (lazy_irq_pending() || need_resched() || 3813 recheck_signals_and_mmu(&core_info)) { 3814 local_irq_enable(); 3815 vc->vcore_state = VCORE_INACTIVE; 3816 /* Unlock all except the primary vcore */ 3817 for (sub = 1; sub < core_info.n_subcores; ++sub) { 3818 pvc = core_info.vc[sub]; 3819 /* Put back on to the preempted vcores list */ 3820 kvmppc_vcore_preempt(pvc); 3821 spin_unlock(&pvc->lock); 3822 } 3823 for (i = 0; i < controlled_threads; ++i) 3824 kvmppc_release_hwthread(pcpu + i); 3825 return; 3826 } 3827 3828 kvmppc_clear_host_core(pcpu); 3829 3830 /* Decide on micro-threading (split-core) mode */ 3831 subcore_size = threads_per_subcore; 3832 cmd_bit = stat_bit = 0; 3833 split = core_info.n_subcores; 3834 sip = NULL; 3835 is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S); 3836 3837 if (split > 1) { 3838 sip = &split_info; 3839 memset(&split_info, 0, sizeof(split_info)); 3840 for (sub = 0; sub < core_info.n_subcores; ++sub) 3841 split_info.vc[sub] = core_info.vc[sub]; 3842 3843 if (is_power8) { 3844 if (split == 2 && (dynamic_mt_modes & 2)) { 3845 cmd_bit = HID0_POWER8_1TO2LPAR; 3846 stat_bit = HID0_POWER8_2LPARMODE; 3847 } else { 3848 split = 4; 3849 cmd_bit = HID0_POWER8_1TO4LPAR; 3850 stat_bit = HID0_POWER8_4LPARMODE; 3851 } 3852 subcore_size = MAX_SMT_THREADS / split; 3853 split_info.rpr = mfspr(SPRN_RPR); 3854 split_info.pmmar = mfspr(SPRN_PMMAR); 3855 split_info.ldbar = mfspr(SPRN_LDBAR); 3856 split_info.subcore_size = subcore_size; 3857 } else { 3858 split_info.subcore_size = 1; 3859 } 3860 3861 /* order writes to split_info before kvm_split_mode pointer */ 3862 smp_wmb(); 3863 } 3864 3865 for (thr = 0; thr < controlled_threads; ++thr) { 3866 struct paca_struct *paca = paca_ptrs[pcpu + thr]; 3867 3868 paca->kvm_hstate.napping = 0; 3869 paca->kvm_hstate.kvm_split_mode = sip; 3870 } 3871 3872 /* Initiate micro-threading (split-core) on POWER8 if required */ 3873 if (cmd_bit) { 3874 unsigned long hid0 = mfspr(SPRN_HID0); 3875 3876 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS; 3877 mb(); 3878 mtspr(SPRN_HID0, hid0); 3879 isync(); 3880 for (;;) { 3881 hid0 = mfspr(SPRN_HID0); 3882 if (hid0 & stat_bit) 3883 break; 3884 cpu_relax(); 3885 } 3886 } 3887 3888 /* 3889 * On POWER8, set RWMR register. 3890 * Since it only affects PURR and SPURR, it doesn't affect 3891 * the host, so we don't save/restore the host value. 3892 */ 3893 if (is_power8) { 3894 unsigned long rwmr_val = RWMR_RPA_P8_8THREAD; 3895 int n_online = atomic_read(&vc->online_count); 3896 3897 /* 3898 * Use the 8-thread value if we're doing split-core 3899 * or if the vcore's online count looks bogus. 3900 */ 3901 if (split == 1 && threads_per_subcore == MAX_SMT_THREADS && 3902 n_online >= 1 && n_online <= MAX_SMT_THREADS) 3903 rwmr_val = p8_rwmr_values[n_online]; 3904 mtspr(SPRN_RWMR, rwmr_val); 3905 } 3906 3907 /* Start all the threads */ 3908 active = 0; 3909 for (sub = 0; sub < core_info.n_subcores; ++sub) { 3910 thr = is_power8 ? subcore_thread_map[sub] : sub; 3911 thr0_done = false; 3912 active |= 1 << thr; 3913 pvc = core_info.vc[sub]; 3914 pvc->pcpu = pcpu + thr; 3915 for_each_runnable_thread(i, vcpu, pvc) { 3916 /* 3917 * XXX: is kvmppc_start_thread called too late here? 3918 * It updates vcpu->cpu and vcpu->arch.thread_cpu 3919 * which are used by kvmppc_fast_vcpu_kick_hv(), but 3920 * kick is called after new exceptions become available 3921 * and exceptions are checked earlier than here, by 3922 * kvmppc_core_prepare_to_enter. 3923 */ 3924 kvmppc_start_thread(vcpu, pvc); 3925 kvmppc_update_vpa_dispatch(vcpu, pvc); 3926 trace_kvm_guest_enter(vcpu); 3927 if (!vcpu->arch.ptid) 3928 thr0_done = true; 3929 active |= 1 << (thr + vcpu->arch.ptid); 3930 } 3931 /* 3932 * We need to start the first thread of each subcore 3933 * even if it doesn't have a vcpu. 3934 */ 3935 if (!thr0_done) 3936 kvmppc_start_thread(NULL, pvc); 3937 } 3938 3939 /* 3940 * Ensure that split_info.do_nap is set after setting 3941 * the vcore pointer in the PACA of the secondaries. 3942 */ 3943 smp_mb(); 3944 3945 /* 3946 * When doing micro-threading, poke the inactive threads as well. 3947 * This gets them to the nap instruction after kvm_do_nap, 3948 * which reduces the time taken to unsplit later. 3949 */ 3950 if (cmd_bit) { 3951 split_info.do_nap = 1; /* ask secondaries to nap when done */ 3952 for (thr = 1; thr < threads_per_subcore; ++thr) 3953 if (!(active & (1 << thr))) 3954 kvmppc_ipi_thread(pcpu + thr); 3955 } 3956 3957 vc->vcore_state = VCORE_RUNNING; 3958 preempt_disable(); 3959 3960 trace_kvmppc_run_core(vc, 0); 3961 3962 for (sub = 0; sub < core_info.n_subcores; ++sub) 3963 spin_unlock(&core_info.vc[sub]->lock); 3964 3965 guest_timing_enter_irqoff(); 3966 3967 srcu_idx = srcu_read_lock(&vc->kvm->srcu); 3968 3969 guest_state_enter_irqoff(); 3970 this_cpu_disable_ftrace(); 3971 3972 trap = __kvmppc_vcore_entry(); 3973 3974 this_cpu_enable_ftrace(); 3975 guest_state_exit_irqoff(); 3976 3977 srcu_read_unlock(&vc->kvm->srcu, srcu_idx); 3978 3979 set_irq_happened(trap); 3980 3981 spin_lock(&vc->lock); 3982 /* prevent other vcpu threads from doing kvmppc_start_thread() now */ 3983 vc->vcore_state = VCORE_EXITING; 3984 3985 /* wait for secondary threads to finish writing their state to memory */ 3986 kvmppc_wait_for_nap(controlled_threads); 3987 3988 /* Return to whole-core mode if we split the core earlier */ 3989 if (cmd_bit) { 3990 unsigned long hid0 = mfspr(SPRN_HID0); 3991 unsigned long loops = 0; 3992 3993 hid0 &= ~HID0_POWER8_DYNLPARDIS; 3994 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE; 3995 mb(); 3996 mtspr(SPRN_HID0, hid0); 3997 isync(); 3998 for (;;) { 3999 hid0 = mfspr(SPRN_HID0); 4000 if (!(hid0 & stat_bit)) 4001 break; 4002 cpu_relax(); 4003 ++loops; 4004 } 4005 split_info.do_nap = 0; 4006 } 4007 4008 kvmppc_set_host_core(pcpu); 4009 4010 if (!vtime_accounting_enabled_this_cpu()) { 4011 local_irq_enable(); 4012 /* 4013 * Service IRQs here before guest_timing_exit_irqoff() so any 4014 * ticks that occurred while running the guest are accounted to 4015 * the guest. If vtime accounting is enabled, accounting uses 4016 * TB rather than ticks, so it can be done without enabling 4017 * interrupts here, which has the problem that it accounts 4018 * interrupt processing overhead to the host. 4019 */ 4020 local_irq_disable(); 4021 } 4022 guest_timing_exit_irqoff(); 4023 4024 local_irq_enable(); 4025 4026 /* Let secondaries go back to the offline loop */ 4027 for (i = 0; i < controlled_threads; ++i) { 4028 kvmppc_release_hwthread(pcpu + i); 4029 if (sip && sip->napped[i]) 4030 kvmppc_ipi_thread(pcpu + i); 4031 } 4032 4033 spin_unlock(&vc->lock); 4034 4035 /* make sure updates to secondary vcpu structs are visible now */ 4036 smp_mb(); 4037 4038 preempt_enable(); 4039 4040 for (sub = 0; sub < core_info.n_subcores; ++sub) { 4041 pvc = core_info.vc[sub]; 4042 post_guest_process(pvc, pvc == vc); 4043 } 4044 4045 spin_lock(&vc->lock); 4046 4047 out: 4048 vc->vcore_state = VCORE_INACTIVE; 4049 trace_kvmppc_run_core(vc, 1); 4050 } 4051 4052 static inline bool hcall_is_xics(unsigned long req) 4053 { 4054 return req == H_EOI || req == H_CPPR || req == H_IPI || 4055 req == H_IPOLL || req == H_XIRR || req == H_XIRR_X; 4056 } 4057 4058 static void vcpu_vpa_increment_dispatch(struct kvm_vcpu *vcpu) 4059 { 4060 struct lppaca *lp = vcpu->arch.vpa.pinned_addr; 4061 if (lp) { 4062 u32 yield_count = be32_to_cpu(lp->yield_count) + 1; 4063 lp->yield_count = cpu_to_be32(yield_count); 4064 vcpu->arch.vpa.dirty = 1; 4065 } 4066 } 4067 4068 static int kvmhv_vcpu_entry_nestedv2(struct kvm_vcpu *vcpu, u64 time_limit, 4069 unsigned long lpcr, u64 *tb) 4070 { 4071 struct kvmhv_nestedv2_io *io; 4072 unsigned long msr, i; 4073 int trap; 4074 long rc; 4075 4076 io = &vcpu->arch.nestedv2_io; 4077 4078 msr = mfmsr(); 4079 kvmppc_msr_hard_disable_set_facilities(vcpu, msr); 4080 if (lazy_irq_pending()) 4081 return 0; 4082 4083 rc = kvmhv_nestedv2_flush_vcpu(vcpu, time_limit); 4084 if (rc < 0) 4085 return -EINVAL; 4086 4087 accumulate_time(vcpu, &vcpu->arch.in_guest); 4088 rc = plpar_guest_run_vcpu(0, vcpu->kvm->arch.lpid, vcpu->vcpu_id, 4089 &trap, &i); 4090 4091 if (rc != H_SUCCESS) { 4092 pr_err("KVM Guest Run VCPU hcall failed\n"); 4093 if (rc == H_INVALID_ELEMENT_ID) 4094 pr_err("KVM: Guest Run VCPU invalid element id at %ld\n", i); 4095 else if (rc == H_INVALID_ELEMENT_SIZE) 4096 pr_err("KVM: Guest Run VCPU invalid element size at %ld\n", i); 4097 else if (rc == H_INVALID_ELEMENT_VALUE) 4098 pr_err("KVM: Guest Run VCPU invalid element value at %ld\n", i); 4099 return -EINVAL; 4100 } 4101 accumulate_time(vcpu, &vcpu->arch.guest_exit); 4102 4103 *tb = mftb(); 4104 kvmppc_gsm_reset(io->vcpu_message); 4105 kvmppc_gsm_reset(io->vcore_message); 4106 kvmppc_gsbm_zero(&io->valids); 4107 4108 rc = kvmhv_nestedv2_parse_output(vcpu); 4109 if (rc < 0) 4110 return -EINVAL; 4111 4112 timer_rearm_host_dec(*tb); 4113 4114 return trap; 4115 } 4116 4117 /* call our hypervisor to load up HV regs and go */ 4118 static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcr, u64 *tb) 4119 { 4120 unsigned long host_psscr; 4121 unsigned long msr; 4122 struct hv_guest_state hvregs; 4123 struct p9_host_os_sprs host_os_sprs; 4124 s64 dec; 4125 int trap; 4126 4127 msr = mfmsr(); 4128 4129 save_p9_host_os_sprs(&host_os_sprs); 4130 4131 /* 4132 * We need to save and restore the guest visible part of the 4133 * psscr (i.e. using SPRN_PSSCR_PR) since the hypervisor 4134 * doesn't do this for us. Note only required if pseries since 4135 * this is done in kvmhv_vcpu_entry_p9() below otherwise. 4136 */ 4137 host_psscr = mfspr(SPRN_PSSCR_PR); 4138 4139 kvmppc_msr_hard_disable_set_facilities(vcpu, msr); 4140 if (lazy_irq_pending()) 4141 return 0; 4142 4143 if (unlikely(load_vcpu_state(vcpu, &host_os_sprs))) 4144 msr = mfmsr(); /* TM restore can update msr */ 4145 4146 if (vcpu->arch.psscr != host_psscr) 4147 mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr); 4148 4149 kvmhv_save_hv_regs(vcpu, &hvregs); 4150 hvregs.lpcr = lpcr; 4151 hvregs.amor = ~0; 4152 vcpu->arch.regs.msr = vcpu->arch.shregs.msr; 4153 hvregs.version = HV_GUEST_STATE_VERSION; 4154 if (vcpu->arch.nested) { 4155 hvregs.lpid = vcpu->arch.nested->shadow_lpid; 4156 hvregs.vcpu_token = vcpu->arch.nested_vcpu_id; 4157 } else { 4158 hvregs.lpid = vcpu->kvm->arch.lpid; 4159 hvregs.vcpu_token = vcpu->vcpu_id; 4160 } 4161 hvregs.hdec_expiry = time_limit; 4162 4163 /* 4164 * When setting DEC, we must always deal with irq_work_raise 4165 * via NMI vs setting DEC. The problem occurs right as we 4166 * switch into guest mode if a NMI hits and sets pending work 4167 * and sets DEC, then that will apply to the guest and not 4168 * bring us back to the host. 4169 * 4170 * irq_work_raise could check a flag (or possibly LPCR[HDICE] 4171 * for example) and set HDEC to 1? That wouldn't solve the 4172 * nested hv case which needs to abort the hcall or zero the 4173 * time limit. 4174 * 4175 * XXX: Another day's problem. 4176 */ 4177 mtspr(SPRN_DEC, kvmppc_dec_expires_host_tb(vcpu) - *tb); 4178 4179 mtspr(SPRN_DAR, vcpu->arch.shregs.dar); 4180 mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr); 4181 switch_pmu_to_guest(vcpu, &host_os_sprs); 4182 accumulate_time(vcpu, &vcpu->arch.in_guest); 4183 trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs), 4184 __pa(&vcpu->arch.regs)); 4185 accumulate_time(vcpu, &vcpu->arch.guest_exit); 4186 kvmhv_restore_hv_return_state(vcpu, &hvregs); 4187 switch_pmu_to_host(vcpu, &host_os_sprs); 4188 vcpu->arch.shregs.msr = vcpu->arch.regs.msr; 4189 vcpu->arch.shregs.dar = mfspr(SPRN_DAR); 4190 vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR); 4191 vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR); 4192 4193 store_vcpu_state(vcpu); 4194 4195 dec = mfspr(SPRN_DEC); 4196 if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */ 4197 dec = (s32) dec; 4198 *tb = mftb(); 4199 vcpu->arch.dec_expires = dec + (*tb + kvmppc_get_tb_offset(vcpu)); 4200 4201 timer_rearm_host_dec(*tb); 4202 4203 restore_p9_host_os_sprs(vcpu, &host_os_sprs); 4204 if (vcpu->arch.psscr != host_psscr) 4205 mtspr(SPRN_PSSCR_PR, host_psscr); 4206 4207 return trap; 4208 } 4209 4210 /* 4211 * Guest entry for POWER9 and later CPUs. 4212 */ 4213 static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit, 4214 unsigned long lpcr, u64 *tb) 4215 { 4216 struct kvm *kvm = vcpu->kvm; 4217 struct kvm_nested_guest *nested = vcpu->arch.nested; 4218 u64 next_timer; 4219 int trap; 4220 4221 next_timer = timer_get_next_tb(); 4222 if (*tb >= next_timer) 4223 return BOOK3S_INTERRUPT_HV_DECREMENTER; 4224 if (next_timer < time_limit) 4225 time_limit = next_timer; 4226 else if (*tb >= time_limit) /* nested time limit */ 4227 return BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER; 4228 4229 vcpu->arch.ceded = 0; 4230 4231 vcpu_vpa_increment_dispatch(vcpu); 4232 4233 if (kvmhv_on_pseries()) { 4234 if (kvmhv_is_nestedv1()) 4235 trap = kvmhv_vcpu_entry_p9_nested(vcpu, time_limit, lpcr, tb); 4236 else 4237 trap = kvmhv_vcpu_entry_nestedv2(vcpu, time_limit, lpcr, tb); 4238 4239 /* H_CEDE has to be handled now, not later */ 4240 if (trap == BOOK3S_INTERRUPT_SYSCALL && !nested && 4241 kvmppc_get_gpr(vcpu, 3) == H_CEDE) { 4242 kvmppc_cede(vcpu); 4243 kvmppc_set_gpr(vcpu, 3, 0); 4244 trap = 0; 4245 } 4246 4247 } else if (nested) { 4248 __this_cpu_write(cpu_in_guest, kvm); 4249 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb); 4250 __this_cpu_write(cpu_in_guest, NULL); 4251 4252 } else { 4253 kvmppc_xive_push_vcpu(vcpu); 4254 4255 __this_cpu_write(cpu_in_guest, kvm); 4256 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb); 4257 __this_cpu_write(cpu_in_guest, NULL); 4258 4259 if (trap == BOOK3S_INTERRUPT_SYSCALL && 4260 !(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) { 4261 unsigned long req = kvmppc_get_gpr(vcpu, 3); 4262 4263 /* 4264 * XIVE rearm and XICS hcalls must be handled 4265 * before xive context is pulled (is this 4266 * true?) 4267 */ 4268 if (req == H_CEDE) { 4269 /* H_CEDE has to be handled now */ 4270 kvmppc_cede(vcpu); 4271 if (!kvmppc_xive_rearm_escalation(vcpu)) { 4272 /* 4273 * Pending escalation so abort 4274 * the cede. 4275 */ 4276 vcpu->arch.ceded = 0; 4277 } 4278 kvmppc_set_gpr(vcpu, 3, 0); 4279 trap = 0; 4280 4281 } else if (req == H_ENTER_NESTED) { 4282 /* 4283 * L2 should not run with the L1 4284 * context so rearm and pull it. 4285 */ 4286 if (!kvmppc_xive_rearm_escalation(vcpu)) { 4287 /* 4288 * Pending escalation so abort 4289 * H_ENTER_NESTED. 4290 */ 4291 kvmppc_set_gpr(vcpu, 3, 0); 4292 trap = 0; 4293 } 4294 4295 } else if (hcall_is_xics(req)) { 4296 int ret; 4297 4298 ret = kvmppc_xive_xics_hcall(vcpu, req); 4299 if (ret != H_TOO_HARD) { 4300 kvmppc_set_gpr(vcpu, 3, ret); 4301 trap = 0; 4302 } 4303 } 4304 } 4305 kvmppc_xive_pull_vcpu(vcpu); 4306 4307 if (kvm_is_radix(kvm)) 4308 vcpu->arch.slb_max = 0; 4309 } 4310 4311 vcpu_vpa_increment_dispatch(vcpu); 4312 4313 return trap; 4314 } 4315 4316 /* 4317 * Wait for some other vcpu thread to execute us, and 4318 * wake us up when we need to handle something in the host. 4319 */ 4320 static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc, 4321 struct kvm_vcpu *vcpu, int wait_state) 4322 { 4323 DEFINE_WAIT(wait); 4324 4325 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state); 4326 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { 4327 spin_unlock(&vc->lock); 4328 schedule(); 4329 spin_lock(&vc->lock); 4330 } 4331 finish_wait(&vcpu->arch.cpu_run, &wait); 4332 } 4333 4334 static void grow_halt_poll_ns(struct kvmppc_vcore *vc) 4335 { 4336 if (!halt_poll_ns_grow) 4337 return; 4338 4339 vc->halt_poll_ns *= halt_poll_ns_grow; 4340 if (vc->halt_poll_ns < halt_poll_ns_grow_start) 4341 vc->halt_poll_ns = halt_poll_ns_grow_start; 4342 } 4343 4344 static void shrink_halt_poll_ns(struct kvmppc_vcore *vc) 4345 { 4346 if (halt_poll_ns_shrink == 0) 4347 vc->halt_poll_ns = 0; 4348 else 4349 vc->halt_poll_ns /= halt_poll_ns_shrink; 4350 } 4351 4352 #ifdef CONFIG_KVM_XICS 4353 static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu) 4354 { 4355 if (!xics_on_xive()) 4356 return false; 4357 return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr < 4358 vcpu->arch.xive_saved_state.cppr; 4359 } 4360 #else 4361 static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu) 4362 { 4363 return false; 4364 } 4365 #endif /* CONFIG_KVM_XICS */ 4366 4367 static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu) 4368 { 4369 if (vcpu->arch.pending_exceptions || vcpu->arch.prodded || 4370 kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu)) 4371 return true; 4372 4373 return false; 4374 } 4375 4376 static bool kvmppc_vcpu_check_block(struct kvm_vcpu *vcpu) 4377 { 4378 if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu)) 4379 return true; 4380 return false; 4381 } 4382 4383 /* 4384 * Check to see if any of the runnable vcpus on the vcore have pending 4385 * exceptions or are no longer ceded 4386 */ 4387 static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc) 4388 { 4389 struct kvm_vcpu *vcpu; 4390 int i; 4391 4392 for_each_runnable_thread(i, vcpu, vc) { 4393 if (kvmppc_vcpu_check_block(vcpu)) 4394 return 1; 4395 } 4396 4397 return 0; 4398 } 4399 4400 /* 4401 * All the vcpus in this vcore are idle, so wait for a decrementer 4402 * or external interrupt to one of the vcpus. vc->lock is held. 4403 */ 4404 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) 4405 { 4406 ktime_t cur, start_poll, start_wait; 4407 int do_sleep = 1; 4408 u64 block_ns; 4409 4410 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)); 4411 4412 /* Poll for pending exceptions and ceded state */ 4413 cur = start_poll = ktime_get(); 4414 if (vc->halt_poll_ns) { 4415 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns); 4416 ++vc->runner->stat.generic.halt_attempted_poll; 4417 4418 vc->vcore_state = VCORE_POLLING; 4419 spin_unlock(&vc->lock); 4420 4421 do { 4422 if (kvmppc_vcore_check_block(vc)) { 4423 do_sleep = 0; 4424 break; 4425 } 4426 cur = ktime_get(); 4427 } while (kvm_vcpu_can_poll(cur, stop)); 4428 4429 spin_lock(&vc->lock); 4430 vc->vcore_state = VCORE_INACTIVE; 4431 4432 if (!do_sleep) { 4433 ++vc->runner->stat.generic.halt_successful_poll; 4434 goto out; 4435 } 4436 } 4437 4438 prepare_to_rcuwait(&vc->wait); 4439 set_current_state(TASK_INTERRUPTIBLE); 4440 if (kvmppc_vcore_check_block(vc)) { 4441 finish_rcuwait(&vc->wait); 4442 do_sleep = 0; 4443 /* If we polled, count this as a successful poll */ 4444 if (vc->halt_poll_ns) 4445 ++vc->runner->stat.generic.halt_successful_poll; 4446 goto out; 4447 } 4448 4449 start_wait = ktime_get(); 4450 4451 vc->vcore_state = VCORE_SLEEPING; 4452 trace_kvmppc_vcore_blocked(vc->runner, 0); 4453 spin_unlock(&vc->lock); 4454 schedule(); 4455 finish_rcuwait(&vc->wait); 4456 spin_lock(&vc->lock); 4457 vc->vcore_state = VCORE_INACTIVE; 4458 trace_kvmppc_vcore_blocked(vc->runner, 1); 4459 ++vc->runner->stat.halt_successful_wait; 4460 4461 cur = ktime_get(); 4462 4463 out: 4464 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll); 4465 4466 /* Attribute wait time */ 4467 if (do_sleep) { 4468 vc->runner->stat.generic.halt_wait_ns += 4469 ktime_to_ns(cur) - ktime_to_ns(start_wait); 4470 KVM_STATS_LOG_HIST_UPDATE( 4471 vc->runner->stat.generic.halt_wait_hist, 4472 ktime_to_ns(cur) - ktime_to_ns(start_wait)); 4473 /* Attribute failed poll time */ 4474 if (vc->halt_poll_ns) { 4475 vc->runner->stat.generic.halt_poll_fail_ns += 4476 ktime_to_ns(start_wait) - 4477 ktime_to_ns(start_poll); 4478 KVM_STATS_LOG_HIST_UPDATE( 4479 vc->runner->stat.generic.halt_poll_fail_hist, 4480 ktime_to_ns(start_wait) - 4481 ktime_to_ns(start_poll)); 4482 } 4483 } else { 4484 /* Attribute successful poll time */ 4485 if (vc->halt_poll_ns) { 4486 vc->runner->stat.generic.halt_poll_success_ns += 4487 ktime_to_ns(cur) - 4488 ktime_to_ns(start_poll); 4489 KVM_STATS_LOG_HIST_UPDATE( 4490 vc->runner->stat.generic.halt_poll_success_hist, 4491 ktime_to_ns(cur) - ktime_to_ns(start_poll)); 4492 } 4493 } 4494 4495 /* Adjust poll time */ 4496 if (halt_poll_ns) { 4497 if (block_ns <= vc->halt_poll_ns) 4498 ; 4499 /* We slept and blocked for longer than the max halt time */ 4500 else if (vc->halt_poll_ns && block_ns > halt_poll_ns) 4501 shrink_halt_poll_ns(vc); 4502 /* We slept and our poll time is too small */ 4503 else if (vc->halt_poll_ns < halt_poll_ns && 4504 block_ns < halt_poll_ns) 4505 grow_halt_poll_ns(vc); 4506 if (vc->halt_poll_ns > halt_poll_ns) 4507 vc->halt_poll_ns = halt_poll_ns; 4508 } else 4509 vc->halt_poll_ns = 0; 4510 4511 trace_kvmppc_vcore_wakeup(do_sleep, block_ns); 4512 } 4513 4514 /* 4515 * This never fails for a radix guest, as none of the operations it does 4516 * for a radix guest can fail or have a way to report failure. 4517 */ 4518 static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu) 4519 { 4520 int r = 0; 4521 struct kvm *kvm = vcpu->kvm; 4522 4523 mutex_lock(&kvm->arch.mmu_setup_lock); 4524 if (!kvm->arch.mmu_ready) { 4525 if (!kvm_is_radix(kvm)) 4526 r = kvmppc_hv_setup_htab_rma(vcpu); 4527 if (!r) { 4528 if (cpu_has_feature(CPU_FTR_ARCH_300)) 4529 kvmppc_setup_partition_table(kvm); 4530 kvm->arch.mmu_ready = 1; 4531 } 4532 } 4533 mutex_unlock(&kvm->arch.mmu_setup_lock); 4534 return r; 4535 } 4536 4537 static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu) 4538 { 4539 struct kvm_run *run = vcpu->run; 4540 int n_ceded, i, r; 4541 struct kvmppc_vcore *vc; 4542 struct kvm_vcpu *v; 4543 4544 trace_kvmppc_run_vcpu_enter(vcpu); 4545 4546 run->exit_reason = 0; 4547 vcpu->arch.ret = RESUME_GUEST; 4548 vcpu->arch.trap = 0; 4549 kvmppc_update_vpas(vcpu); 4550 4551 /* 4552 * Synchronize with other threads in this virtual core 4553 */ 4554 vc = vcpu->arch.vcore; 4555 spin_lock(&vc->lock); 4556 vcpu->arch.ceded = 0; 4557 vcpu->arch.run_task = current; 4558 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb()); 4559 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; 4560 vcpu->arch.busy_preempt = TB_NIL; 4561 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu); 4562 ++vc->n_runnable; 4563 4564 /* 4565 * This happens the first time this is called for a vcpu. 4566 * If the vcore is already running, we may be able to start 4567 * this thread straight away and have it join in. 4568 */ 4569 if (!signal_pending(current)) { 4570 if ((vc->vcore_state == VCORE_PIGGYBACK || 4571 vc->vcore_state == VCORE_RUNNING) && 4572 !VCORE_IS_EXITING(vc)) { 4573 kvmppc_update_vpa_dispatch(vcpu, vc); 4574 kvmppc_start_thread(vcpu, vc); 4575 trace_kvm_guest_enter(vcpu); 4576 } else if (vc->vcore_state == VCORE_SLEEPING) { 4577 rcuwait_wake_up(&vc->wait); 4578 } 4579 4580 } 4581 4582 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && 4583 !signal_pending(current)) { 4584 /* See if the MMU is ready to go */ 4585 if (!vcpu->kvm->arch.mmu_ready) { 4586 spin_unlock(&vc->lock); 4587 r = kvmhv_setup_mmu(vcpu); 4588 spin_lock(&vc->lock); 4589 if (r) { 4590 run->exit_reason = KVM_EXIT_FAIL_ENTRY; 4591 run->fail_entry. 4592 hardware_entry_failure_reason = 0; 4593 vcpu->arch.ret = r; 4594 break; 4595 } 4596 } 4597 4598 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) 4599 kvmppc_vcore_end_preempt(vc); 4600 4601 if (vc->vcore_state != VCORE_INACTIVE) { 4602 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE); 4603 continue; 4604 } 4605 for_each_runnable_thread(i, v, vc) { 4606 kvmppc_core_prepare_to_enter(v); 4607 if (signal_pending(v->arch.run_task)) { 4608 kvmppc_remove_runnable(vc, v, mftb()); 4609 v->stat.signal_exits++; 4610 v->run->exit_reason = KVM_EXIT_INTR; 4611 v->arch.ret = -EINTR; 4612 wake_up(&v->arch.cpu_run); 4613 } 4614 } 4615 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) 4616 break; 4617 n_ceded = 0; 4618 for_each_runnable_thread(i, v, vc) { 4619 if (!kvmppc_vcpu_woken(v)) 4620 n_ceded += v->arch.ceded; 4621 else 4622 v->arch.ceded = 0; 4623 } 4624 vc->runner = vcpu; 4625 if (n_ceded == vc->n_runnable) { 4626 kvmppc_vcore_blocked(vc); 4627 } else if (need_resched()) { 4628 kvmppc_vcore_preempt(vc); 4629 /* Let something else run */ 4630 cond_resched_lock(&vc->lock); 4631 if (vc->vcore_state == VCORE_PREEMPT) 4632 kvmppc_vcore_end_preempt(vc); 4633 } else { 4634 kvmppc_run_core(vc); 4635 } 4636 vc->runner = NULL; 4637 } 4638 4639 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && 4640 (vc->vcore_state == VCORE_RUNNING || 4641 vc->vcore_state == VCORE_EXITING || 4642 vc->vcore_state == VCORE_PIGGYBACK)) 4643 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE); 4644 4645 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) 4646 kvmppc_vcore_end_preempt(vc); 4647 4648 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { 4649 kvmppc_remove_runnable(vc, vcpu, mftb()); 4650 vcpu->stat.signal_exits++; 4651 run->exit_reason = KVM_EXIT_INTR; 4652 vcpu->arch.ret = -EINTR; 4653 } 4654 4655 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) { 4656 /* Wake up some vcpu to run the core */ 4657 i = -1; 4658 v = next_runnable_thread(vc, &i); 4659 wake_up(&v->arch.cpu_run); 4660 } 4661 4662 trace_kvmppc_run_vcpu_exit(vcpu); 4663 spin_unlock(&vc->lock); 4664 return vcpu->arch.ret; 4665 } 4666 4667 int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, 4668 unsigned long lpcr) 4669 { 4670 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); 4671 struct kvm_run *run = vcpu->run; 4672 int trap, r, pcpu; 4673 int srcu_idx; 4674 struct kvmppc_vcore *vc; 4675 struct kvm *kvm = vcpu->kvm; 4676 struct kvm_nested_guest *nested = vcpu->arch.nested; 4677 unsigned long flags; 4678 u64 tb; 4679 4680 trace_kvmppc_run_vcpu_enter(vcpu); 4681 4682 run->exit_reason = 0; 4683 vcpu->arch.ret = RESUME_GUEST; 4684 vcpu->arch.trap = 0; 4685 4686 vc = vcpu->arch.vcore; 4687 vcpu->arch.ceded = 0; 4688 vcpu->arch.run_task = current; 4689 vcpu->arch.last_inst = KVM_INST_FETCH_FAILED; 4690 4691 /* See if the MMU is ready to go */ 4692 if (unlikely(!kvm->arch.mmu_ready)) { 4693 r = kvmhv_setup_mmu(vcpu); 4694 if (r) { 4695 run->exit_reason = KVM_EXIT_FAIL_ENTRY; 4696 run->fail_entry.hardware_entry_failure_reason = 0; 4697 vcpu->arch.ret = r; 4698 return r; 4699 } 4700 } 4701 4702 if (need_resched()) 4703 cond_resched(); 4704 4705 kvmppc_update_vpas(vcpu); 4706 4707 preempt_disable(); 4708 pcpu = smp_processor_id(); 4709 if (kvm_is_radix(kvm)) 4710 kvmppc_prepare_radix_vcpu(vcpu, pcpu); 4711 4712 /* flags save not required, but irq_pmu has no disable/enable API */ 4713 powerpc_local_irq_pmu_save(flags); 4714 4715 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; 4716 4717 if (signal_pending(current)) 4718 goto sigpend; 4719 if (need_resched() || !kvm->arch.mmu_ready) 4720 goto out; 4721 4722 vcpu->cpu = pcpu; 4723 vcpu->arch.thread_cpu = pcpu; 4724 vc->pcpu = pcpu; 4725 local_paca->kvm_hstate.kvm_vcpu = vcpu; 4726 local_paca->kvm_hstate.ptid = 0; 4727 local_paca->kvm_hstate.fake_suspend = 0; 4728 4729 /* 4730 * Orders set cpu/thread_cpu vs testing for pending interrupts and 4731 * doorbells below. The other side is when these fields are set vs 4732 * kvmppc_fast_vcpu_kick_hv reading the cpu/thread_cpu fields to 4733 * kick a vCPU to notice the pending interrupt. 4734 */ 4735 smp_mb(); 4736 4737 if (!nested) { 4738 kvmppc_core_prepare_to_enter(vcpu); 4739 if (__kvmppc_get_msr_hv(vcpu) & MSR_EE) { 4740 if (xive_interrupt_pending(vcpu)) 4741 kvmppc_inject_interrupt_hv(vcpu, 4742 BOOK3S_INTERRUPT_EXTERNAL, 0); 4743 } else if (test_bit(BOOK3S_IRQPRIO_EXTERNAL, 4744 &vcpu->arch.pending_exceptions)) { 4745 lpcr |= LPCR_MER; 4746 } 4747 } else if (vcpu->arch.pending_exceptions || 4748 vcpu->arch.doorbell_request || 4749 xive_interrupt_pending(vcpu)) { 4750 vcpu->arch.ret = RESUME_HOST; 4751 goto out; 4752 } 4753 4754 if (vcpu->arch.timer_running) { 4755 hrtimer_try_to_cancel(&vcpu->arch.dec_timer); 4756 vcpu->arch.timer_running = 0; 4757 } 4758 4759 tb = mftb(); 4760 4761 kvmppc_update_vpa_dispatch_p9(vcpu, vc, tb + kvmppc_get_tb_offset(vcpu)); 4762 4763 trace_kvm_guest_enter(vcpu); 4764 4765 guest_timing_enter_irqoff(); 4766 4767 srcu_idx = srcu_read_lock(&kvm->srcu); 4768 4769 guest_state_enter_irqoff(); 4770 this_cpu_disable_ftrace(); 4771 4772 trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr, &tb); 4773 vcpu->arch.trap = trap; 4774 4775 this_cpu_enable_ftrace(); 4776 guest_state_exit_irqoff(); 4777 4778 srcu_read_unlock(&kvm->srcu, srcu_idx); 4779 4780 set_irq_happened(trap); 4781 4782 vcpu->cpu = -1; 4783 vcpu->arch.thread_cpu = -1; 4784 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; 4785 4786 if (!vtime_accounting_enabled_this_cpu()) { 4787 powerpc_local_irq_pmu_restore(flags); 4788 /* 4789 * Service IRQs here before guest_timing_exit_irqoff() so any 4790 * ticks that occurred while running the guest are accounted to 4791 * the guest. If vtime accounting is enabled, accounting uses 4792 * TB rather than ticks, so it can be done without enabling 4793 * interrupts here, which has the problem that it accounts 4794 * interrupt processing overhead to the host. 4795 */ 4796 powerpc_local_irq_pmu_save(flags); 4797 } 4798 guest_timing_exit_irqoff(); 4799 4800 powerpc_local_irq_pmu_restore(flags); 4801 4802 preempt_enable(); 4803 4804 /* 4805 * cancel pending decrementer exception if DEC is now positive, or if 4806 * entering a nested guest in which case the decrementer is now owned 4807 * by L2 and the L1 decrementer is provided in hdec_expires 4808 */ 4809 if (kvmppc_core_pending_dec(vcpu) && 4810 ((tb < kvmppc_dec_expires_host_tb(vcpu)) || 4811 (trap == BOOK3S_INTERRUPT_SYSCALL && 4812 kvmppc_get_gpr(vcpu, 3) == H_ENTER_NESTED))) 4813 kvmppc_core_dequeue_dec(vcpu); 4814 4815 trace_kvm_guest_exit(vcpu); 4816 r = RESUME_GUEST; 4817 if (trap) { 4818 if (!nested) 4819 r = kvmppc_handle_exit_hv(vcpu, current); 4820 else 4821 r = kvmppc_handle_nested_exit(vcpu); 4822 } 4823 vcpu->arch.ret = r; 4824 4825 if (is_kvmppc_resume_guest(r) && !kvmppc_vcpu_check_block(vcpu)) { 4826 kvmppc_set_timer(vcpu); 4827 4828 prepare_to_rcuwait(wait); 4829 for (;;) { 4830 set_current_state(TASK_INTERRUPTIBLE); 4831 if (signal_pending(current)) { 4832 vcpu->stat.signal_exits++; 4833 run->exit_reason = KVM_EXIT_INTR; 4834 vcpu->arch.ret = -EINTR; 4835 break; 4836 } 4837 4838 if (kvmppc_vcpu_check_block(vcpu)) 4839 break; 4840 4841 trace_kvmppc_vcore_blocked(vcpu, 0); 4842 schedule(); 4843 trace_kvmppc_vcore_blocked(vcpu, 1); 4844 } 4845 finish_rcuwait(wait); 4846 } 4847 vcpu->arch.ceded = 0; 4848 4849 done: 4850 trace_kvmppc_run_vcpu_exit(vcpu); 4851 4852 return vcpu->arch.ret; 4853 4854 sigpend: 4855 vcpu->stat.signal_exits++; 4856 run->exit_reason = KVM_EXIT_INTR; 4857 vcpu->arch.ret = -EINTR; 4858 out: 4859 vcpu->cpu = -1; 4860 vcpu->arch.thread_cpu = -1; 4861 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; 4862 powerpc_local_irq_pmu_restore(flags); 4863 preempt_enable(); 4864 goto done; 4865 } 4866 4867 static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu) 4868 { 4869 struct kvm_run *run = vcpu->run; 4870 int r; 4871 int srcu_idx; 4872 struct kvm *kvm; 4873 unsigned long msr; 4874 4875 start_timing(vcpu, &vcpu->arch.vcpu_entry); 4876 4877 if (!vcpu->arch.sane) { 4878 run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 4879 return -EINVAL; 4880 } 4881 4882 /* No need to go into the guest when all we'll do is come back out */ 4883 if (signal_pending(current)) { 4884 run->exit_reason = KVM_EXIT_INTR; 4885 return -EINTR; 4886 } 4887 4888 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM 4889 /* 4890 * Don't allow entry with a suspended transaction, because 4891 * the guest entry/exit code will lose it. 4892 */ 4893 if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs && 4894 (current->thread.regs->msr & MSR_TM)) { 4895 if (MSR_TM_ACTIVE(current->thread.regs->msr)) { 4896 run->exit_reason = KVM_EXIT_FAIL_ENTRY; 4897 run->fail_entry.hardware_entry_failure_reason = 0; 4898 return -EINVAL; 4899 } 4900 } 4901 #endif 4902 4903 /* 4904 * Force online to 1 for the sake of old userspace which doesn't 4905 * set it. 4906 */ 4907 if (!vcpu->arch.online) { 4908 atomic_inc(&vcpu->arch.vcore->online_count); 4909 vcpu->arch.online = 1; 4910 } 4911 4912 kvmppc_core_prepare_to_enter(vcpu); 4913 4914 kvm = vcpu->kvm; 4915 atomic_inc(&kvm->arch.vcpus_running); 4916 /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */ 4917 smp_mb(); 4918 4919 msr = 0; 4920 if (IS_ENABLED(CONFIG_PPC_FPU)) 4921 msr |= MSR_FP; 4922 if (cpu_has_feature(CPU_FTR_ALTIVEC)) 4923 msr |= MSR_VEC; 4924 if (cpu_has_feature(CPU_FTR_VSX)) 4925 msr |= MSR_VSX; 4926 if ((cpu_has_feature(CPU_FTR_TM) || 4927 cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) && 4928 (kvmppc_get_hfscr_hv(vcpu) & HFSCR_TM)) 4929 msr |= MSR_TM; 4930 msr = msr_check_and_set(msr); 4931 4932 kvmppc_save_user_regs(); 4933 4934 kvmppc_save_current_sprs(); 4935 4936 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 4937 vcpu->arch.waitp = &vcpu->arch.vcore->wait; 4938 vcpu->arch.pgdir = kvm->mm->pgd; 4939 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; 4940 4941 do { 4942 accumulate_time(vcpu, &vcpu->arch.guest_entry); 4943 if (cpu_has_feature(CPU_FTR_ARCH_300)) 4944 r = kvmhv_run_single_vcpu(vcpu, ~(u64)0, 4945 vcpu->arch.vcore->lpcr); 4946 else 4947 r = kvmppc_run_vcpu(vcpu); 4948 4949 if (run->exit_reason == KVM_EXIT_PAPR_HCALL) { 4950 accumulate_time(vcpu, &vcpu->arch.hcall); 4951 4952 if (WARN_ON_ONCE(__kvmppc_get_msr_hv(vcpu) & MSR_PR)) { 4953 /* 4954 * These should have been caught reflected 4955 * into the guest by now. Final sanity check: 4956 * don't allow userspace to execute hcalls in 4957 * the hypervisor. 4958 */ 4959 r = RESUME_GUEST; 4960 continue; 4961 } 4962 trace_kvm_hcall_enter(vcpu); 4963 r = kvmppc_pseries_do_hcall(vcpu); 4964 trace_kvm_hcall_exit(vcpu, r); 4965 kvmppc_core_prepare_to_enter(vcpu); 4966 } else if (r == RESUME_PAGE_FAULT) { 4967 accumulate_time(vcpu, &vcpu->arch.pg_fault); 4968 srcu_idx = srcu_read_lock(&kvm->srcu); 4969 r = kvmppc_book3s_hv_page_fault(vcpu, 4970 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); 4971 srcu_read_unlock(&kvm->srcu, srcu_idx); 4972 } else if (r == RESUME_PASSTHROUGH) { 4973 if (WARN_ON(xics_on_xive())) 4974 r = H_SUCCESS; 4975 else 4976 r = kvmppc_xics_rm_complete(vcpu, 0); 4977 } 4978 } while (is_kvmppc_resume_guest(r)); 4979 accumulate_time(vcpu, &vcpu->arch.vcpu_exit); 4980 4981 vcpu->arch.state = KVMPPC_VCPU_NOTREADY; 4982 atomic_dec(&kvm->arch.vcpus_running); 4983 4984 srr_regs_clobbered(); 4985 4986 end_timing(vcpu); 4987 4988 return r; 4989 } 4990 4991 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps, 4992 int shift, int sllp) 4993 { 4994 (*sps)->page_shift = shift; 4995 (*sps)->slb_enc = sllp; 4996 (*sps)->enc[0].page_shift = shift; 4997 (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift); 4998 /* 4999 * Add 16MB MPSS support (may get filtered out by userspace) 5000 */ 5001 if (shift != 24) { 5002 int penc = kvmppc_pgsize_lp_encoding(shift, 24); 5003 if (penc != -1) { 5004 (*sps)->enc[1].page_shift = 24; 5005 (*sps)->enc[1].pte_enc = penc; 5006 } 5007 } 5008 (*sps)++; 5009 } 5010 5011 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, 5012 struct kvm_ppc_smmu_info *info) 5013 { 5014 struct kvm_ppc_one_seg_page_size *sps; 5015 5016 /* 5017 * POWER7, POWER8 and POWER9 all support 32 storage keys for data. 5018 * POWER7 doesn't support keys for instruction accesses, 5019 * POWER8 and POWER9 do. 5020 */ 5021 info->data_keys = 32; 5022 info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0; 5023 5024 /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */ 5025 info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS; 5026 info->slb_size = 32; 5027 5028 /* We only support these sizes for now, and no muti-size segments */ 5029 sps = &info->sps[0]; 5030 kvmppc_add_seg_page_size(&sps, 12, 0); 5031 kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01); 5032 kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L); 5033 5034 /* If running as a nested hypervisor, we don't support HPT guests */ 5035 if (kvmhv_on_pseries()) 5036 info->flags |= KVM_PPC_NO_HASH; 5037 5038 return 0; 5039 } 5040 5041 /* 5042 * Get (and clear) the dirty memory log for a memory slot. 5043 */ 5044 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm, 5045 struct kvm_dirty_log *log) 5046 { 5047 struct kvm_memslots *slots; 5048 struct kvm_memory_slot *memslot; 5049 int r; 5050 unsigned long n, i; 5051 unsigned long *buf, *p; 5052 struct kvm_vcpu *vcpu; 5053 5054 mutex_lock(&kvm->slots_lock); 5055 5056 r = -EINVAL; 5057 if (log->slot >= KVM_USER_MEM_SLOTS) 5058 goto out; 5059 5060 slots = kvm_memslots(kvm); 5061 memslot = id_to_memslot(slots, log->slot); 5062 r = -ENOENT; 5063 if (!memslot || !memslot->dirty_bitmap) 5064 goto out; 5065 5066 /* 5067 * Use second half of bitmap area because both HPT and radix 5068 * accumulate bits in the first half. 5069 */ 5070 n = kvm_dirty_bitmap_bytes(memslot); 5071 buf = memslot->dirty_bitmap + n / sizeof(long); 5072 memset(buf, 0, n); 5073 5074 if (kvm_is_radix(kvm)) 5075 r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf); 5076 else 5077 r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf); 5078 if (r) 5079 goto out; 5080 5081 /* 5082 * We accumulate dirty bits in the first half of the 5083 * memslot's dirty_bitmap area, for when pages are paged 5084 * out or modified by the host directly. Pick up these 5085 * bits and add them to the map. 5086 */ 5087 p = memslot->dirty_bitmap; 5088 for (i = 0; i < n / sizeof(long); ++i) 5089 buf[i] |= xchg(&p[i], 0); 5090 5091 /* Harvest dirty bits from VPA and DTL updates */ 5092 /* Note: we never modify the SLB shadow buffer areas */ 5093 kvm_for_each_vcpu(i, vcpu, kvm) { 5094 spin_lock(&vcpu->arch.vpa_update_lock); 5095 kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf); 5096 kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf); 5097 spin_unlock(&vcpu->arch.vpa_update_lock); 5098 } 5099 5100 r = -EFAULT; 5101 if (copy_to_user(log->dirty_bitmap, buf, n)) 5102 goto out; 5103 5104 r = 0; 5105 out: 5106 mutex_unlock(&kvm->slots_lock); 5107 return r; 5108 } 5109 5110 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot) 5111 { 5112 vfree(slot->arch.rmap); 5113 slot->arch.rmap = NULL; 5114 } 5115 5116 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm, 5117 const struct kvm_memory_slot *old, 5118 struct kvm_memory_slot *new, 5119 enum kvm_mr_change change) 5120 { 5121 if (change == KVM_MR_CREATE) { 5122 unsigned long size = array_size(new->npages, sizeof(*new->arch.rmap)); 5123 5124 if ((size >> PAGE_SHIFT) > totalram_pages()) 5125 return -ENOMEM; 5126 5127 new->arch.rmap = vzalloc(size); 5128 if (!new->arch.rmap) 5129 return -ENOMEM; 5130 } else if (change != KVM_MR_DELETE) { 5131 new->arch.rmap = old->arch.rmap; 5132 } 5133 5134 return 0; 5135 } 5136 5137 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, 5138 struct kvm_memory_slot *old, 5139 const struct kvm_memory_slot *new, 5140 enum kvm_mr_change change) 5141 { 5142 /* 5143 * If we are creating or modifying a memslot, it might make 5144 * some address that was previously cached as emulated 5145 * MMIO be no longer emulated MMIO, so invalidate 5146 * all the caches of emulated MMIO translations. 5147 */ 5148 if (change != KVM_MR_DELETE) 5149 atomic64_inc(&kvm->arch.mmio_update); 5150 5151 /* 5152 * For change == KVM_MR_MOVE or KVM_MR_DELETE, higher levels 5153 * have already called kvm_arch_flush_shadow_memslot() to 5154 * flush shadow mappings. For KVM_MR_CREATE we have no 5155 * previous mappings. So the only case to handle is 5156 * KVM_MR_FLAGS_ONLY when the KVM_MEM_LOG_DIRTY_PAGES bit 5157 * has been changed. 5158 * For radix guests, we flush on setting KVM_MEM_LOG_DIRTY_PAGES 5159 * to get rid of any THP PTEs in the partition-scoped page tables 5160 * so we can track dirtiness at the page level; we flush when 5161 * clearing KVM_MEM_LOG_DIRTY_PAGES so that we can go back to 5162 * using THP PTEs. 5163 */ 5164 if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) && 5165 ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES)) 5166 kvmppc_radix_flush_memslot(kvm, old); 5167 /* 5168 * If UV hasn't yet called H_SVM_INIT_START, don't register memslots. 5169 */ 5170 if (!kvm->arch.secure_guest) 5171 return; 5172 5173 switch (change) { 5174 case KVM_MR_CREATE: 5175 /* 5176 * @TODO kvmppc_uvmem_memslot_create() can fail and 5177 * return error. Fix this. 5178 */ 5179 kvmppc_uvmem_memslot_create(kvm, new); 5180 break; 5181 case KVM_MR_DELETE: 5182 kvmppc_uvmem_memslot_delete(kvm, old); 5183 break; 5184 default: 5185 /* TODO: Handle KVM_MR_MOVE */ 5186 break; 5187 } 5188 } 5189 5190 /* 5191 * Update LPCR values in kvm->arch and in vcores. 5192 * Caller must hold kvm->arch.mmu_setup_lock (for mutual exclusion 5193 * of kvm->arch.lpcr update). 5194 */ 5195 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask) 5196 { 5197 long int i; 5198 u32 cores_done = 0; 5199 5200 if ((kvm->arch.lpcr & mask) == lpcr) 5201 return; 5202 5203 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr; 5204 5205 for (i = 0; i < KVM_MAX_VCORES; ++i) { 5206 struct kvmppc_vcore *vc = kvm->arch.vcores[i]; 5207 if (!vc) 5208 continue; 5209 5210 spin_lock(&vc->lock); 5211 vc->lpcr = (vc->lpcr & ~mask) | lpcr; 5212 verify_lpcr(kvm, vc->lpcr); 5213 spin_unlock(&vc->lock); 5214 if (++cores_done >= kvm->arch.online_vcores) 5215 break; 5216 } 5217 5218 if (kvmhv_is_nestedv2()) { 5219 struct kvm_vcpu *vcpu; 5220 5221 kvm_for_each_vcpu(i, vcpu, kvm) { 5222 kvmhv_nestedv2_mark_dirty(vcpu, KVMPPC_GSID_LPCR); 5223 } 5224 } 5225 } 5226 5227 void kvmppc_setup_partition_table(struct kvm *kvm) 5228 { 5229 unsigned long dw0, dw1; 5230 5231 if (!kvm_is_radix(kvm)) { 5232 /* PS field - page size for VRMA */ 5233 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) | 5234 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1); 5235 /* HTABSIZE and HTABORG fields */ 5236 dw0 |= kvm->arch.sdr1; 5237 5238 /* Second dword as set by userspace */ 5239 dw1 = kvm->arch.process_table; 5240 } else { 5241 dw0 = PATB_HR | radix__get_tree_size() | 5242 __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE; 5243 dw1 = PATB_GR | kvm->arch.process_table; 5244 } 5245 kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1); 5246 } 5247 5248 /* 5249 * Set up HPT (hashed page table) and RMA (real-mode area). 5250 * Must be called with kvm->arch.mmu_setup_lock held. 5251 */ 5252 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) 5253 { 5254 int err = 0; 5255 struct kvm *kvm = vcpu->kvm; 5256 unsigned long hva; 5257 struct kvm_memory_slot *memslot; 5258 struct vm_area_struct *vma; 5259 unsigned long lpcr = 0, senc; 5260 unsigned long psize, porder; 5261 int srcu_idx; 5262 5263 /* Allocate hashed page table (if not done already) and reset it */ 5264 if (!kvm->arch.hpt.virt) { 5265 int order = KVM_DEFAULT_HPT_ORDER; 5266 struct kvm_hpt_info info; 5267 5268 err = kvmppc_allocate_hpt(&info, order); 5269 /* If we get here, it means userspace didn't specify a 5270 * size explicitly. So, try successively smaller 5271 * sizes if the default failed. */ 5272 while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER) 5273 err = kvmppc_allocate_hpt(&info, order); 5274 5275 if (err < 0) { 5276 pr_err("KVM: Couldn't alloc HPT\n"); 5277 goto out; 5278 } 5279 5280 kvmppc_set_hpt(kvm, &info); 5281 } 5282 5283 /* Look up the memslot for guest physical address 0 */ 5284 srcu_idx = srcu_read_lock(&kvm->srcu); 5285 memslot = gfn_to_memslot(kvm, 0); 5286 5287 /* We must have some memory at 0 by now */ 5288 err = -EINVAL; 5289 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) 5290 goto out_srcu; 5291 5292 /* Look up the VMA for the start of this memory slot */ 5293 hva = memslot->userspace_addr; 5294 mmap_read_lock(kvm->mm); 5295 vma = vma_lookup(kvm->mm, hva); 5296 if (!vma || (vma->vm_flags & VM_IO)) 5297 goto up_out; 5298 5299 psize = vma_kernel_pagesize(vma); 5300 5301 mmap_read_unlock(kvm->mm); 5302 5303 /* We can handle 4k, 64k or 16M pages in the VRMA */ 5304 if (psize >= 0x1000000) 5305 psize = 0x1000000; 5306 else if (psize >= 0x10000) 5307 psize = 0x10000; 5308 else 5309 psize = 0x1000; 5310 porder = __ilog2(psize); 5311 5312 senc = slb_pgsize_encoding(psize); 5313 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | 5314 (VRMA_VSID << SLB_VSID_SHIFT_1T); 5315 /* Create HPTEs in the hash page table for the VRMA */ 5316 kvmppc_map_vrma(vcpu, memslot, porder); 5317 5318 /* Update VRMASD field in the LPCR */ 5319 if (!cpu_has_feature(CPU_FTR_ARCH_300)) { 5320 /* the -4 is to account for senc values starting at 0x10 */ 5321 lpcr = senc << (LPCR_VRMASD_SH - 4); 5322 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD); 5323 } 5324 5325 /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */ 5326 smp_wmb(); 5327 err = 0; 5328 out_srcu: 5329 srcu_read_unlock(&kvm->srcu, srcu_idx); 5330 out: 5331 return err; 5332 5333 up_out: 5334 mmap_read_unlock(kvm->mm); 5335 goto out_srcu; 5336 } 5337 5338 /* 5339 * Must be called with kvm->arch.mmu_setup_lock held and 5340 * mmu_ready = 0 and no vcpus running. 5341 */ 5342 int kvmppc_switch_mmu_to_hpt(struct kvm *kvm) 5343 { 5344 unsigned long lpcr, lpcr_mask; 5345 5346 if (nesting_enabled(kvm)) 5347 kvmhv_release_all_nested(kvm); 5348 kvmppc_rmap_reset(kvm); 5349 kvm->arch.process_table = 0; 5350 /* Mutual exclusion with kvm_unmap_gfn_range etc. */ 5351 spin_lock(&kvm->mmu_lock); 5352 kvm->arch.radix = 0; 5353 spin_unlock(&kvm->mmu_lock); 5354 kvmppc_free_radix(kvm); 5355 5356 lpcr = LPCR_VPM1; 5357 lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR; 5358 if (cpu_has_feature(CPU_FTR_ARCH_31)) 5359 lpcr_mask |= LPCR_HAIL; 5360 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask); 5361 5362 return 0; 5363 } 5364 5365 /* 5366 * Must be called with kvm->arch.mmu_setup_lock held and 5367 * mmu_ready = 0 and no vcpus running. 5368 */ 5369 int kvmppc_switch_mmu_to_radix(struct kvm *kvm) 5370 { 5371 unsigned long lpcr, lpcr_mask; 5372 int err; 5373 5374 err = kvmppc_init_vm_radix(kvm); 5375 if (err) 5376 return err; 5377 kvmppc_rmap_reset(kvm); 5378 /* Mutual exclusion with kvm_unmap_gfn_range etc. */ 5379 spin_lock(&kvm->mmu_lock); 5380 kvm->arch.radix = 1; 5381 spin_unlock(&kvm->mmu_lock); 5382 kvmppc_free_hpt(&kvm->arch.hpt); 5383 5384 lpcr = LPCR_UPRT | LPCR_GTSE | LPCR_HR; 5385 lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR; 5386 if (cpu_has_feature(CPU_FTR_ARCH_31)) { 5387 lpcr_mask |= LPCR_HAIL; 5388 if (cpu_has_feature(CPU_FTR_HVMODE) && 5389 (kvm->arch.host_lpcr & LPCR_HAIL)) 5390 lpcr |= LPCR_HAIL; 5391 } 5392 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask); 5393 5394 return 0; 5395 } 5396 5397 #ifdef CONFIG_KVM_XICS 5398 /* 5399 * Allocate a per-core structure for managing state about which cores are 5400 * running in the host versus the guest and for exchanging data between 5401 * real mode KVM and CPU running in the host. 5402 * This is only done for the first VM. 5403 * The allocated structure stays even if all VMs have stopped. 5404 * It is only freed when the kvm-hv module is unloaded. 5405 * It's OK for this routine to fail, we just don't support host 5406 * core operations like redirecting H_IPI wakeups. 5407 */ 5408 void kvmppc_alloc_host_rm_ops(void) 5409 { 5410 struct kvmppc_host_rm_ops *ops; 5411 unsigned long l_ops; 5412 int cpu, core; 5413 int size; 5414 5415 if (cpu_has_feature(CPU_FTR_ARCH_300)) 5416 return; 5417 5418 /* Not the first time here ? */ 5419 if (kvmppc_host_rm_ops_hv != NULL) 5420 return; 5421 5422 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL); 5423 if (!ops) 5424 return; 5425 5426 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core); 5427 ops->rm_core = kzalloc(size, GFP_KERNEL); 5428 5429 if (!ops->rm_core) { 5430 kfree(ops); 5431 return; 5432 } 5433 5434 cpus_read_lock(); 5435 5436 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) { 5437 if (!cpu_online(cpu)) 5438 continue; 5439 5440 core = cpu >> threads_shift; 5441 ops->rm_core[core].rm_state.in_host = 1; 5442 } 5443 5444 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv; 5445 5446 /* 5447 * Make the contents of the kvmppc_host_rm_ops structure visible 5448 * to other CPUs before we assign it to the global variable. 5449 * Do an atomic assignment (no locks used here), but if someone 5450 * beats us to it, just free our copy and return. 5451 */ 5452 smp_wmb(); 5453 l_ops = (unsigned long) ops; 5454 5455 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) { 5456 cpus_read_unlock(); 5457 kfree(ops->rm_core); 5458 kfree(ops); 5459 return; 5460 } 5461 5462 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE, 5463 "ppc/kvm_book3s:prepare", 5464 kvmppc_set_host_core, 5465 kvmppc_clear_host_core); 5466 cpus_read_unlock(); 5467 } 5468 5469 void kvmppc_free_host_rm_ops(void) 5470 { 5471 if (kvmppc_host_rm_ops_hv) { 5472 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE); 5473 kfree(kvmppc_host_rm_ops_hv->rm_core); 5474 kfree(kvmppc_host_rm_ops_hv); 5475 kvmppc_host_rm_ops_hv = NULL; 5476 } 5477 } 5478 #endif 5479 5480 static int kvmppc_core_init_vm_hv(struct kvm *kvm) 5481 { 5482 unsigned long lpcr, lpid; 5483 int ret; 5484 5485 mutex_init(&kvm->arch.uvmem_lock); 5486 INIT_LIST_HEAD(&kvm->arch.uvmem_pfns); 5487 mutex_init(&kvm->arch.mmu_setup_lock); 5488 5489 /* Allocate the guest's logical partition ID */ 5490 5491 if (!kvmhv_is_nestedv2()) { 5492 lpid = kvmppc_alloc_lpid(); 5493 if ((long)lpid < 0) 5494 return -ENOMEM; 5495 kvm->arch.lpid = lpid; 5496 } 5497 5498 kvmppc_alloc_host_rm_ops(); 5499 5500 kvmhv_vm_nested_init(kvm); 5501 5502 if (kvmhv_is_nestedv2()) { 5503 long rc; 5504 unsigned long guest_id; 5505 5506 rc = plpar_guest_create(0, &guest_id); 5507 5508 if (rc != H_SUCCESS) 5509 pr_err("KVM: Create Guest hcall failed, rc=%ld\n", rc); 5510 5511 switch (rc) { 5512 case H_PARAMETER: 5513 case H_FUNCTION: 5514 case H_STATE: 5515 return -EINVAL; 5516 case H_NOT_ENOUGH_RESOURCES: 5517 case H_ABORTED: 5518 return -ENOMEM; 5519 case H_AUTHORITY: 5520 return -EPERM; 5521 case H_NOT_AVAILABLE: 5522 return -EBUSY; 5523 } 5524 kvm->arch.lpid = guest_id; 5525 } 5526 5527 5528 /* 5529 * Since we don't flush the TLB when tearing down a VM, 5530 * and this lpid might have previously been used, 5531 * make sure we flush on each core before running the new VM. 5532 * On POWER9, the tlbie in mmu_partition_table_set_entry() 5533 * does this flush for us. 5534 */ 5535 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 5536 cpumask_setall(&kvm->arch.need_tlb_flush); 5537 5538 /* Start out with the default set of hcalls enabled */ 5539 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls, 5540 sizeof(kvm->arch.enabled_hcalls)); 5541 5542 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 5543 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); 5544 5545 /* Init LPCR for virtual RMA mode */ 5546 if (cpu_has_feature(CPU_FTR_HVMODE)) { 5547 kvm->arch.host_lpid = mfspr(SPRN_LPID); 5548 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); 5549 lpcr &= LPCR_PECE | LPCR_LPES; 5550 } else { 5551 /* 5552 * The L2 LPES mode will be set by the L0 according to whether 5553 * or not it needs to take external interrupts in HV mode. 5554 */ 5555 lpcr = 0; 5556 } 5557 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | 5558 LPCR_VPM0 | LPCR_VPM1; 5559 kvm->arch.vrma_slb_v = SLB_VSID_B_1T | 5560 (VRMA_VSID << SLB_VSID_SHIFT_1T); 5561 /* On POWER8 turn on online bit to enable PURR/SPURR */ 5562 if (cpu_has_feature(CPU_FTR_ARCH_207S)) 5563 lpcr |= LPCR_ONL; 5564 /* 5565 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed) 5566 * Set HVICE bit to enable hypervisor virtualization interrupts. 5567 * Set HEIC to prevent OS interrupts to go to hypervisor (should 5568 * be unnecessary but better safe than sorry in case we re-enable 5569 * EE in HV mode with this LPCR still set) 5570 */ 5571 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 5572 lpcr &= ~LPCR_VPM0; 5573 lpcr |= LPCR_HVICE | LPCR_HEIC; 5574 5575 /* 5576 * If xive is enabled, we route 0x500 interrupts directly 5577 * to the guest. 5578 */ 5579 if (xics_on_xive()) 5580 lpcr |= LPCR_LPES; 5581 } 5582 5583 /* 5584 * If the host uses radix, the guest starts out as radix. 5585 */ 5586 if (radix_enabled()) { 5587 kvm->arch.radix = 1; 5588 kvm->arch.mmu_ready = 1; 5589 lpcr &= ~LPCR_VPM1; 5590 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR; 5591 if (cpu_has_feature(CPU_FTR_HVMODE) && 5592 cpu_has_feature(CPU_FTR_ARCH_31) && 5593 (kvm->arch.host_lpcr & LPCR_HAIL)) 5594 lpcr |= LPCR_HAIL; 5595 ret = kvmppc_init_vm_radix(kvm); 5596 if (ret) { 5597 if (kvmhv_is_nestedv2()) 5598 plpar_guest_delete(0, kvm->arch.lpid); 5599 else 5600 kvmppc_free_lpid(kvm->arch.lpid); 5601 return ret; 5602 } 5603 kvmppc_setup_partition_table(kvm); 5604 } 5605 5606 verify_lpcr(kvm, lpcr); 5607 kvm->arch.lpcr = lpcr; 5608 5609 /* Initialization for future HPT resizes */ 5610 kvm->arch.resize_hpt = NULL; 5611 5612 /* 5613 * Work out how many sets the TLB has, for the use of 5614 * the TLB invalidation loop in book3s_hv_rmhandlers.S. 5615 */ 5616 if (cpu_has_feature(CPU_FTR_ARCH_31)) { 5617 /* 5618 * P10 will flush all the congruence class with a single tlbiel 5619 */ 5620 kvm->arch.tlb_sets = 1; 5621 } else if (radix_enabled()) 5622 kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */ 5623 else if (cpu_has_feature(CPU_FTR_ARCH_300)) 5624 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */ 5625 else if (cpu_has_feature(CPU_FTR_ARCH_207S)) 5626 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */ 5627 else 5628 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */ 5629 5630 /* 5631 * Track that we now have a HV mode VM active. This blocks secondary 5632 * CPU threads from coming online. 5633 */ 5634 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 5635 kvm_hv_vm_activated(); 5636 5637 /* 5638 * Initialize smt_mode depending on processor. 5639 * POWER8 and earlier have to use "strict" threading, where 5640 * all vCPUs in a vcore have to run on the same (sub)core, 5641 * whereas on POWER9 the threads can each run a different 5642 * guest. 5643 */ 5644 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 5645 kvm->arch.smt_mode = threads_per_subcore; 5646 else 5647 kvm->arch.smt_mode = 1; 5648 kvm->arch.emul_smt_mode = 1; 5649 5650 return 0; 5651 } 5652 5653 static int kvmppc_arch_create_vm_debugfs_hv(struct kvm *kvm) 5654 { 5655 kvmppc_mmu_debugfs_init(kvm); 5656 if (radix_enabled()) 5657 kvmhv_radix_debugfs_init(kvm); 5658 return 0; 5659 } 5660 5661 static void kvmppc_free_vcores(struct kvm *kvm) 5662 { 5663 long int i; 5664 5665 for (i = 0; i < KVM_MAX_VCORES; ++i) 5666 kfree(kvm->arch.vcores[i]); 5667 kvm->arch.online_vcores = 0; 5668 } 5669 5670 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm) 5671 { 5672 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 5673 kvm_hv_vm_deactivated(); 5674 5675 kvmppc_free_vcores(kvm); 5676 5677 5678 if (kvm_is_radix(kvm)) 5679 kvmppc_free_radix(kvm); 5680 else 5681 kvmppc_free_hpt(&kvm->arch.hpt); 5682 5683 /* Perform global invalidation and return lpid to the pool */ 5684 if (cpu_has_feature(CPU_FTR_ARCH_300)) { 5685 if (nesting_enabled(kvm)) 5686 kvmhv_release_all_nested(kvm); 5687 kvm->arch.process_table = 0; 5688 if (kvm->arch.secure_guest) 5689 uv_svm_terminate(kvm->arch.lpid); 5690 if (!kvmhv_is_nestedv2()) 5691 kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0); 5692 } 5693 5694 if (kvmhv_is_nestedv2()) 5695 plpar_guest_delete(0, kvm->arch.lpid); 5696 else 5697 kvmppc_free_lpid(kvm->arch.lpid); 5698 5699 kvmppc_free_pimap(kvm); 5700 } 5701 5702 /* We don't need to emulate any privileged instructions or dcbz */ 5703 static int kvmppc_core_emulate_op_hv(struct kvm_vcpu *vcpu, 5704 unsigned int inst, int *advance) 5705 { 5706 return EMULATE_FAIL; 5707 } 5708 5709 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn, 5710 ulong spr_val) 5711 { 5712 return EMULATE_FAIL; 5713 } 5714 5715 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, 5716 ulong *spr_val) 5717 { 5718 return EMULATE_FAIL; 5719 } 5720 5721 static int kvmppc_core_check_processor_compat_hv(void) 5722 { 5723 if (cpu_has_feature(CPU_FTR_HVMODE) && 5724 cpu_has_feature(CPU_FTR_ARCH_206)) 5725 return 0; 5726 5727 /* POWER9 in radix mode is capable of being a nested hypervisor. */ 5728 if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled()) 5729 return 0; 5730 5731 return -EIO; 5732 } 5733 5734 #ifdef CONFIG_KVM_XICS 5735 5736 void kvmppc_free_pimap(struct kvm *kvm) 5737 { 5738 kfree(kvm->arch.pimap); 5739 } 5740 5741 static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void) 5742 { 5743 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL); 5744 } 5745 5746 static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi) 5747 { 5748 struct irq_desc *desc; 5749 struct kvmppc_irq_map *irq_map; 5750 struct kvmppc_passthru_irqmap *pimap; 5751 struct irq_chip *chip; 5752 int i, rc = 0; 5753 struct irq_data *host_data; 5754 5755 if (!kvm_irq_bypass) 5756 return 1; 5757 5758 desc = irq_to_desc(host_irq); 5759 if (!desc) 5760 return -EIO; 5761 5762 mutex_lock(&kvm->lock); 5763 5764 pimap = kvm->arch.pimap; 5765 if (pimap == NULL) { 5766 /* First call, allocate structure to hold IRQ map */ 5767 pimap = kvmppc_alloc_pimap(); 5768 if (pimap == NULL) { 5769 mutex_unlock(&kvm->lock); 5770 return -ENOMEM; 5771 } 5772 kvm->arch.pimap = pimap; 5773 } 5774 5775 /* 5776 * For now, we only support interrupts for which the EOI operation 5777 * is an OPAL call followed by a write to XIRR, since that's 5778 * what our real-mode EOI code does, or a XIVE interrupt 5779 */ 5780 chip = irq_data_get_irq_chip(&desc->irq_data); 5781 if (!chip || !is_pnv_opal_msi(chip)) { 5782 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n", 5783 host_irq, guest_gsi); 5784 mutex_unlock(&kvm->lock); 5785 return -ENOENT; 5786 } 5787 5788 /* 5789 * See if we already have an entry for this guest IRQ number. 5790 * If it's mapped to a hardware IRQ number, that's an error, 5791 * otherwise re-use this entry. 5792 */ 5793 for (i = 0; i < pimap->n_mapped; i++) { 5794 if (guest_gsi == pimap->mapped[i].v_hwirq) { 5795 if (pimap->mapped[i].r_hwirq) { 5796 mutex_unlock(&kvm->lock); 5797 return -EINVAL; 5798 } 5799 break; 5800 } 5801 } 5802 5803 if (i == KVMPPC_PIRQ_MAPPED) { 5804 mutex_unlock(&kvm->lock); 5805 return -EAGAIN; /* table is full */ 5806 } 5807 5808 irq_map = &pimap->mapped[i]; 5809 5810 irq_map->v_hwirq = guest_gsi; 5811 irq_map->desc = desc; 5812 5813 /* 5814 * Order the above two stores before the next to serialize with 5815 * the KVM real mode handler. 5816 */ 5817 smp_wmb(); 5818 5819 /* 5820 * The 'host_irq' number is mapped in the PCI-MSI domain but 5821 * the underlying calls, which will EOI the interrupt in real 5822 * mode, need an HW IRQ number mapped in the XICS IRQ domain. 5823 */ 5824 host_data = irq_domain_get_irq_data(irq_get_default_host(), host_irq); 5825 irq_map->r_hwirq = (unsigned int)irqd_to_hwirq(host_data); 5826 5827 if (i == pimap->n_mapped) 5828 pimap->n_mapped++; 5829 5830 if (xics_on_xive()) 5831 rc = kvmppc_xive_set_mapped(kvm, guest_gsi, host_irq); 5832 else 5833 kvmppc_xics_set_mapped(kvm, guest_gsi, irq_map->r_hwirq); 5834 if (rc) 5835 irq_map->r_hwirq = 0; 5836 5837 mutex_unlock(&kvm->lock); 5838 5839 return 0; 5840 } 5841 5842 static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi) 5843 { 5844 struct irq_desc *desc; 5845 struct kvmppc_passthru_irqmap *pimap; 5846 int i, rc = 0; 5847 5848 if (!kvm_irq_bypass) 5849 return 0; 5850 5851 desc = irq_to_desc(host_irq); 5852 if (!desc) 5853 return -EIO; 5854 5855 mutex_lock(&kvm->lock); 5856 if (!kvm->arch.pimap) 5857 goto unlock; 5858 5859 pimap = kvm->arch.pimap; 5860 5861 for (i = 0; i < pimap->n_mapped; i++) { 5862 if (guest_gsi == pimap->mapped[i].v_hwirq) 5863 break; 5864 } 5865 5866 if (i == pimap->n_mapped) { 5867 mutex_unlock(&kvm->lock); 5868 return -ENODEV; 5869 } 5870 5871 if (xics_on_xive()) 5872 rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, host_irq); 5873 else 5874 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq); 5875 5876 /* invalidate the entry (what to do on error from the above ?) */ 5877 pimap->mapped[i].r_hwirq = 0; 5878 5879 /* 5880 * We don't free this structure even when the count goes to 5881 * zero. The structure is freed when we destroy the VM. 5882 */ 5883 unlock: 5884 mutex_unlock(&kvm->lock); 5885 return rc; 5886 } 5887 5888 static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons, 5889 struct irq_bypass_producer *prod) 5890 { 5891 int ret = 0; 5892 struct kvm_kernel_irqfd *irqfd = 5893 container_of(cons, struct kvm_kernel_irqfd, consumer); 5894 5895 irqfd->producer = prod; 5896 5897 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi); 5898 if (ret) 5899 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n", 5900 prod->irq, irqfd->gsi, ret); 5901 5902 return ret; 5903 } 5904 5905 static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons, 5906 struct irq_bypass_producer *prod) 5907 { 5908 int ret; 5909 struct kvm_kernel_irqfd *irqfd = 5910 container_of(cons, struct kvm_kernel_irqfd, consumer); 5911 5912 irqfd->producer = NULL; 5913 5914 /* 5915 * When producer of consumer is unregistered, we change back to 5916 * default external interrupt handling mode - KVM real mode 5917 * will switch back to host. 5918 */ 5919 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi); 5920 if (ret) 5921 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n", 5922 prod->irq, irqfd->gsi, ret); 5923 } 5924 #endif 5925 5926 static int kvm_arch_vm_ioctl_hv(struct file *filp, 5927 unsigned int ioctl, unsigned long arg) 5928 { 5929 struct kvm *kvm __maybe_unused = filp->private_data; 5930 void __user *argp = (void __user *)arg; 5931 int r; 5932 5933 switch (ioctl) { 5934 5935 case KVM_PPC_ALLOCATE_HTAB: { 5936 u32 htab_order; 5937 5938 /* If we're a nested hypervisor, we currently only support radix */ 5939 if (kvmhv_on_pseries()) { 5940 r = -EOPNOTSUPP; 5941 break; 5942 } 5943 5944 r = -EFAULT; 5945 if (get_user(htab_order, (u32 __user *)argp)) 5946 break; 5947 r = kvmppc_alloc_reset_hpt(kvm, htab_order); 5948 if (r) 5949 break; 5950 r = 0; 5951 break; 5952 } 5953 5954 case KVM_PPC_GET_HTAB_FD: { 5955 struct kvm_get_htab_fd ghf; 5956 5957 r = -EFAULT; 5958 if (copy_from_user(&ghf, argp, sizeof(ghf))) 5959 break; 5960 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf); 5961 break; 5962 } 5963 5964 case KVM_PPC_RESIZE_HPT_PREPARE: { 5965 struct kvm_ppc_resize_hpt rhpt; 5966 5967 r = -EFAULT; 5968 if (copy_from_user(&rhpt, argp, sizeof(rhpt))) 5969 break; 5970 5971 r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt); 5972 break; 5973 } 5974 5975 case KVM_PPC_RESIZE_HPT_COMMIT: { 5976 struct kvm_ppc_resize_hpt rhpt; 5977 5978 r = -EFAULT; 5979 if (copy_from_user(&rhpt, argp, sizeof(rhpt))) 5980 break; 5981 5982 r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt); 5983 break; 5984 } 5985 5986 default: 5987 r = -ENOTTY; 5988 } 5989 5990 return r; 5991 } 5992 5993 /* 5994 * List of hcall numbers to enable by default. 5995 * For compatibility with old userspace, we enable by default 5996 * all hcalls that were implemented before the hcall-enabling 5997 * facility was added. Note this list should not include H_RTAS. 5998 */ 5999 static unsigned int default_hcall_list[] = { 6000 H_REMOVE, 6001 H_ENTER, 6002 H_READ, 6003 H_PROTECT, 6004 H_BULK_REMOVE, 6005 #ifdef CONFIG_SPAPR_TCE_IOMMU 6006 H_GET_TCE, 6007 H_PUT_TCE, 6008 #endif 6009 H_SET_DABR, 6010 H_SET_XDABR, 6011 H_CEDE, 6012 H_PROD, 6013 H_CONFER, 6014 H_REGISTER_VPA, 6015 #ifdef CONFIG_KVM_XICS 6016 H_EOI, 6017 H_CPPR, 6018 H_IPI, 6019 H_IPOLL, 6020 H_XIRR, 6021 H_XIRR_X, 6022 #endif 6023 0 6024 }; 6025 6026 static void init_default_hcalls(void) 6027 { 6028 int i; 6029 unsigned int hcall; 6030 6031 for (i = 0; default_hcall_list[i]; ++i) { 6032 hcall = default_hcall_list[i]; 6033 WARN_ON(!kvmppc_hcall_impl_hv(hcall)); 6034 __set_bit(hcall / 4, default_enabled_hcalls); 6035 } 6036 } 6037 6038 static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg) 6039 { 6040 unsigned long lpcr; 6041 int radix; 6042 int err; 6043 6044 /* If not on a POWER9, reject it */ 6045 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 6046 return -ENODEV; 6047 6048 /* If any unknown flags set, reject it */ 6049 if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE)) 6050 return -EINVAL; 6051 6052 /* GR (guest radix) bit in process_table field must match */ 6053 radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX); 6054 if (!!(cfg->process_table & PATB_GR) != radix) 6055 return -EINVAL; 6056 6057 /* Process table size field must be reasonable, i.e. <= 24 */ 6058 if ((cfg->process_table & PRTS_MASK) > 24) 6059 return -EINVAL; 6060 6061 /* We can change a guest to/from radix now, if the host is radix */ 6062 if (radix && !radix_enabled()) 6063 return -EINVAL; 6064 6065 /* If we're a nested hypervisor, we currently only support radix */ 6066 if (kvmhv_on_pseries() && !radix) 6067 return -EINVAL; 6068 6069 mutex_lock(&kvm->arch.mmu_setup_lock); 6070 if (radix != kvm_is_radix(kvm)) { 6071 if (kvm->arch.mmu_ready) { 6072 kvm->arch.mmu_ready = 0; 6073 /* order mmu_ready vs. vcpus_running */ 6074 smp_mb(); 6075 if (atomic_read(&kvm->arch.vcpus_running)) { 6076 kvm->arch.mmu_ready = 1; 6077 err = -EBUSY; 6078 goto out_unlock; 6079 } 6080 } 6081 if (radix) 6082 err = kvmppc_switch_mmu_to_radix(kvm); 6083 else 6084 err = kvmppc_switch_mmu_to_hpt(kvm); 6085 if (err) 6086 goto out_unlock; 6087 } 6088 6089 kvm->arch.process_table = cfg->process_table; 6090 kvmppc_setup_partition_table(kvm); 6091 6092 lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0; 6093 kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE); 6094 err = 0; 6095 6096 out_unlock: 6097 mutex_unlock(&kvm->arch.mmu_setup_lock); 6098 return err; 6099 } 6100 6101 static int kvmhv_enable_nested(struct kvm *kvm) 6102 { 6103 if (!nested) 6104 return -EPERM; 6105 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 6106 return -ENODEV; 6107 if (!radix_enabled()) 6108 return -ENODEV; 6109 if (kvmhv_is_nestedv2()) 6110 return -ENODEV; 6111 6112 /* kvm == NULL means the caller is testing if the capability exists */ 6113 if (kvm) 6114 kvm->arch.nested_enable = true; 6115 return 0; 6116 } 6117 6118 static int kvmhv_load_from_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr, 6119 int size) 6120 { 6121 int rc = -EINVAL; 6122 6123 if (kvmhv_vcpu_is_radix(vcpu)) { 6124 rc = kvmhv_copy_from_guest_radix(vcpu, *eaddr, ptr, size); 6125 6126 if (rc > 0) 6127 rc = -EINVAL; 6128 } 6129 6130 /* For now quadrants are the only way to access nested guest memory */ 6131 if (rc && vcpu->arch.nested) 6132 rc = -EAGAIN; 6133 6134 return rc; 6135 } 6136 6137 static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr, 6138 int size) 6139 { 6140 int rc = -EINVAL; 6141 6142 if (kvmhv_vcpu_is_radix(vcpu)) { 6143 rc = kvmhv_copy_to_guest_radix(vcpu, *eaddr, ptr, size); 6144 6145 if (rc > 0) 6146 rc = -EINVAL; 6147 } 6148 6149 /* For now quadrants are the only way to access nested guest memory */ 6150 if (rc && vcpu->arch.nested) 6151 rc = -EAGAIN; 6152 6153 return rc; 6154 } 6155 6156 static void unpin_vpa_reset(struct kvm *kvm, struct kvmppc_vpa *vpa) 6157 { 6158 unpin_vpa(kvm, vpa); 6159 vpa->gpa = 0; 6160 vpa->pinned_addr = NULL; 6161 vpa->dirty = false; 6162 vpa->update_pending = 0; 6163 } 6164 6165 /* 6166 * Enable a guest to become a secure VM, or test whether 6167 * that could be enabled. 6168 * Called when the KVM_CAP_PPC_SECURE_GUEST capability is 6169 * tested (kvm == NULL) or enabled (kvm != NULL). 6170 */ 6171 static int kvmhv_enable_svm(struct kvm *kvm) 6172 { 6173 if (!kvmppc_uvmem_available()) 6174 return -EINVAL; 6175 if (kvm) 6176 kvm->arch.svm_enabled = 1; 6177 return 0; 6178 } 6179 6180 /* 6181 * IOCTL handler to turn off secure mode of guest 6182 * 6183 * - Release all device pages 6184 * - Issue ucall to terminate the guest on the UV side 6185 * - Unpin the VPA pages. 6186 * - Reinit the partition scoped page tables 6187 */ 6188 static int kvmhv_svm_off(struct kvm *kvm) 6189 { 6190 struct kvm_vcpu *vcpu; 6191 int mmu_was_ready; 6192 int srcu_idx; 6193 int ret = 0; 6194 unsigned long i; 6195 6196 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START)) 6197 return ret; 6198 6199 mutex_lock(&kvm->arch.mmu_setup_lock); 6200 mmu_was_ready = kvm->arch.mmu_ready; 6201 if (kvm->arch.mmu_ready) { 6202 kvm->arch.mmu_ready = 0; 6203 /* order mmu_ready vs. vcpus_running */ 6204 smp_mb(); 6205 if (atomic_read(&kvm->arch.vcpus_running)) { 6206 kvm->arch.mmu_ready = 1; 6207 ret = -EBUSY; 6208 goto out; 6209 } 6210 } 6211 6212 srcu_idx = srcu_read_lock(&kvm->srcu); 6213 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { 6214 struct kvm_memory_slot *memslot; 6215 struct kvm_memslots *slots = __kvm_memslots(kvm, i); 6216 int bkt; 6217 6218 if (!slots) 6219 continue; 6220 6221 kvm_for_each_memslot(memslot, bkt, slots) { 6222 kvmppc_uvmem_drop_pages(memslot, kvm, true); 6223 uv_unregister_mem_slot(kvm->arch.lpid, memslot->id); 6224 } 6225 } 6226 srcu_read_unlock(&kvm->srcu, srcu_idx); 6227 6228 ret = uv_svm_terminate(kvm->arch.lpid); 6229 if (ret != U_SUCCESS) { 6230 ret = -EINVAL; 6231 goto out; 6232 } 6233 6234 /* 6235 * When secure guest is reset, all the guest pages are sent 6236 * to UV via UV_PAGE_IN before the non-boot vcpus get a 6237 * chance to run and unpin their VPA pages. Unpinning of all 6238 * VPA pages is done here explicitly so that VPA pages 6239 * can be migrated to the secure side. 6240 * 6241 * This is required to for the secure SMP guest to reboot 6242 * correctly. 6243 */ 6244 kvm_for_each_vcpu(i, vcpu, kvm) { 6245 spin_lock(&vcpu->arch.vpa_update_lock); 6246 unpin_vpa_reset(kvm, &vcpu->arch.dtl); 6247 unpin_vpa_reset(kvm, &vcpu->arch.slb_shadow); 6248 unpin_vpa_reset(kvm, &vcpu->arch.vpa); 6249 spin_unlock(&vcpu->arch.vpa_update_lock); 6250 } 6251 6252 kvmppc_setup_partition_table(kvm); 6253 kvm->arch.secure_guest = 0; 6254 kvm->arch.mmu_ready = mmu_was_ready; 6255 out: 6256 mutex_unlock(&kvm->arch.mmu_setup_lock); 6257 return ret; 6258 } 6259 6260 static int kvmhv_enable_dawr1(struct kvm *kvm) 6261 { 6262 if (!cpu_has_feature(CPU_FTR_DAWR1)) 6263 return -ENODEV; 6264 6265 /* kvm == NULL means the caller is testing if the capability exists */ 6266 if (kvm) 6267 kvm->arch.dawr1_enabled = true; 6268 return 0; 6269 } 6270 6271 static bool kvmppc_hash_v3_possible(void) 6272 { 6273 if (!cpu_has_feature(CPU_FTR_ARCH_300)) 6274 return false; 6275 6276 if (!cpu_has_feature(CPU_FTR_HVMODE)) 6277 return false; 6278 6279 /* 6280 * POWER9 chips before version 2.02 can't have some threads in 6281 * HPT mode and some in radix mode on the same core. 6282 */ 6283 if (radix_enabled()) { 6284 unsigned int pvr = mfspr(SPRN_PVR); 6285 if ((pvr >> 16) == PVR_POWER9 && 6286 (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) || 6287 ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101))) 6288 return false; 6289 } 6290 6291 return true; 6292 } 6293 6294 static struct kvmppc_ops kvm_ops_hv = { 6295 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, 6296 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, 6297 .get_one_reg = kvmppc_get_one_reg_hv, 6298 .set_one_reg = kvmppc_set_one_reg_hv, 6299 .vcpu_load = kvmppc_core_vcpu_load_hv, 6300 .vcpu_put = kvmppc_core_vcpu_put_hv, 6301 .inject_interrupt = kvmppc_inject_interrupt_hv, 6302 .set_msr = kvmppc_set_msr_hv, 6303 .vcpu_run = kvmppc_vcpu_run_hv, 6304 .vcpu_create = kvmppc_core_vcpu_create_hv, 6305 .vcpu_free = kvmppc_core_vcpu_free_hv, 6306 .check_requests = kvmppc_core_check_requests_hv, 6307 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv, 6308 .flush_memslot = kvmppc_core_flush_memslot_hv, 6309 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv, 6310 .commit_memory_region = kvmppc_core_commit_memory_region_hv, 6311 .unmap_gfn_range = kvm_unmap_gfn_range_hv, 6312 .age_gfn = kvm_age_gfn_hv, 6313 .test_age_gfn = kvm_test_age_gfn_hv, 6314 .set_spte_gfn = kvm_set_spte_gfn_hv, 6315 .free_memslot = kvmppc_core_free_memslot_hv, 6316 .init_vm = kvmppc_core_init_vm_hv, 6317 .destroy_vm = kvmppc_core_destroy_vm_hv, 6318 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv, 6319 .emulate_op = kvmppc_core_emulate_op_hv, 6320 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv, 6321 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv, 6322 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv, 6323 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv, 6324 .hcall_implemented = kvmppc_hcall_impl_hv, 6325 #ifdef CONFIG_KVM_XICS 6326 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv, 6327 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv, 6328 #endif 6329 .configure_mmu = kvmhv_configure_mmu, 6330 .get_rmmu_info = kvmhv_get_rmmu_info, 6331 .set_smt_mode = kvmhv_set_smt_mode, 6332 .enable_nested = kvmhv_enable_nested, 6333 .load_from_eaddr = kvmhv_load_from_eaddr, 6334 .store_to_eaddr = kvmhv_store_to_eaddr, 6335 .enable_svm = kvmhv_enable_svm, 6336 .svm_off = kvmhv_svm_off, 6337 .enable_dawr1 = kvmhv_enable_dawr1, 6338 .hash_v3_possible = kvmppc_hash_v3_possible, 6339 .create_vcpu_debugfs = kvmppc_arch_create_vcpu_debugfs_hv, 6340 .create_vm_debugfs = kvmppc_arch_create_vm_debugfs_hv, 6341 }; 6342 6343 static int kvm_init_subcore_bitmap(void) 6344 { 6345 int i, j; 6346 int nr_cores = cpu_nr_cores(); 6347 struct sibling_subcore_state *sibling_subcore_state; 6348 6349 for (i = 0; i < nr_cores; i++) { 6350 int first_cpu = i * threads_per_core; 6351 int node = cpu_to_node(first_cpu); 6352 6353 /* Ignore if it is already allocated. */ 6354 if (paca_ptrs[first_cpu]->sibling_subcore_state) 6355 continue; 6356 6357 sibling_subcore_state = 6358 kzalloc_node(sizeof(struct sibling_subcore_state), 6359 GFP_KERNEL, node); 6360 if (!sibling_subcore_state) 6361 return -ENOMEM; 6362 6363 6364 for (j = 0; j < threads_per_core; j++) { 6365 int cpu = first_cpu + j; 6366 6367 paca_ptrs[cpu]->sibling_subcore_state = 6368 sibling_subcore_state; 6369 } 6370 } 6371 return 0; 6372 } 6373 6374 static int kvmppc_radix_possible(void) 6375 { 6376 return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled(); 6377 } 6378 6379 static int kvmppc_book3s_init_hv(void) 6380 { 6381 int r; 6382 6383 if (!tlbie_capable) { 6384 pr_err("KVM-HV: Host does not support TLBIE\n"); 6385 return -ENODEV; 6386 } 6387 6388 /* 6389 * FIXME!! Do we need to check on all cpus ? 6390 */ 6391 r = kvmppc_core_check_processor_compat_hv(); 6392 if (r < 0) 6393 return -ENODEV; 6394 6395 r = kvmhv_nested_init(); 6396 if (r) 6397 return r; 6398 6399 if (!cpu_has_feature(CPU_FTR_ARCH_300)) { 6400 r = kvm_init_subcore_bitmap(); 6401 if (r) 6402 goto err; 6403 } 6404 6405 /* 6406 * We need a way of accessing the XICS interrupt controller, 6407 * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or 6408 * indirectly, via OPAL. 6409 */ 6410 #ifdef CONFIG_SMP 6411 if (!xics_on_xive() && !kvmhv_on_pseries() && 6412 !local_paca->kvm_hstate.xics_phys) { 6413 struct device_node *np; 6414 6415 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc"); 6416 if (!np) { 6417 pr_err("KVM-HV: Cannot determine method for accessing XICS\n"); 6418 r = -ENODEV; 6419 goto err; 6420 } 6421 /* presence of intc confirmed - node can be dropped again */ 6422 of_node_put(np); 6423 } 6424 #endif 6425 6426 init_default_hcalls(); 6427 6428 init_vcore_lists(); 6429 6430 r = kvmppc_mmu_hv_init(); 6431 if (r) 6432 goto err; 6433 6434 if (kvmppc_radix_possible()) { 6435 r = kvmppc_radix_init(); 6436 if (r) 6437 goto err; 6438 } 6439 6440 r = kvmppc_uvmem_init(); 6441 if (r < 0) { 6442 pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r); 6443 return r; 6444 } 6445 6446 kvm_ops_hv.owner = THIS_MODULE; 6447 kvmppc_hv_ops = &kvm_ops_hv; 6448 6449 return 0; 6450 6451 err: 6452 kvmhv_nested_exit(); 6453 kvmppc_radix_exit(); 6454 6455 return r; 6456 } 6457 6458 static void kvmppc_book3s_exit_hv(void) 6459 { 6460 kvmppc_uvmem_free(); 6461 kvmppc_free_host_rm_ops(); 6462 if (kvmppc_radix_possible()) 6463 kvmppc_radix_exit(); 6464 kvmppc_hv_ops = NULL; 6465 kvmhv_nested_exit(); 6466 } 6467 6468 module_init(kvmppc_book3s_init_hv); 6469 module_exit(kvmppc_book3s_exit_hv); 6470 MODULE_LICENSE("GPL"); 6471 MODULE_ALIAS_MISCDEV(KVM_MINOR); 6472 MODULE_ALIAS("devname:kvm"); 6473