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