1 /* 2 * This program is free software; you can redistribute it and/or modify 3 * it under the terms of the GNU General Public License, version 2, as 4 * published by the Free Software Foundation. 5 * 6 * This program is distributed in the hope that it will be useful, 7 * but WITHOUT ANY WARRANTY; without even the implied warranty of 8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 * GNU General Public License for more details. 10 * 11 * You should have received a copy of the GNU General Public License 12 * along with this program; if not, write to the Free Software 13 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 14 * 15 * Copyright IBM Corp. 2007 16 * Copyright 2010-2011 Freescale Semiconductor, Inc. 17 * 18 * Authors: Hollis Blanchard <hollisb@us.ibm.com> 19 * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> 20 * Scott Wood <scottwood@freescale.com> 21 * Varun Sethi <varun.sethi@freescale.com> 22 */ 23 24 #include <linux/errno.h> 25 #include <linux/err.h> 26 #include <linux/kvm_host.h> 27 #include <linux/gfp.h> 28 #include <linux/module.h> 29 #include <linux/vmalloc.h> 30 #include <linux/fs.h> 31 32 #include <asm/cputable.h> 33 #include <asm/uaccess.h> 34 #include <asm/kvm_ppc.h> 35 #include <asm/cacheflush.h> 36 #include <asm/dbell.h> 37 #include <asm/hw_irq.h> 38 #include <asm/irq.h> 39 40 #include "timing.h" 41 #include "booke.h" 42 #include "trace.h" 43 44 unsigned long kvmppc_booke_handlers; 45 46 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM 47 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU 48 49 struct kvm_stats_debugfs_item debugfs_entries[] = { 50 { "mmio", VCPU_STAT(mmio_exits) }, 51 { "dcr", VCPU_STAT(dcr_exits) }, 52 { "sig", VCPU_STAT(signal_exits) }, 53 { "itlb_r", VCPU_STAT(itlb_real_miss_exits) }, 54 { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) }, 55 { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) }, 56 { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) }, 57 { "sysc", VCPU_STAT(syscall_exits) }, 58 { "isi", VCPU_STAT(isi_exits) }, 59 { "dsi", VCPU_STAT(dsi_exits) }, 60 { "inst_emu", VCPU_STAT(emulated_inst_exits) }, 61 { "dec", VCPU_STAT(dec_exits) }, 62 { "ext_intr", VCPU_STAT(ext_intr_exits) }, 63 { "halt_wakeup", VCPU_STAT(halt_wakeup) }, 64 { "doorbell", VCPU_STAT(dbell_exits) }, 65 { "guest doorbell", VCPU_STAT(gdbell_exits) }, 66 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) }, 67 { NULL } 68 }; 69 70 /* TODO: use vcpu_printf() */ 71 void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu) 72 { 73 int i; 74 75 printk("pc: %08lx msr: %08llx\n", vcpu->arch.pc, vcpu->arch.shared->msr); 76 printk("lr: %08lx ctr: %08lx\n", vcpu->arch.lr, vcpu->arch.ctr); 77 printk("srr0: %08llx srr1: %08llx\n", vcpu->arch.shared->srr0, 78 vcpu->arch.shared->srr1); 79 80 printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions); 81 82 for (i = 0; i < 32; i += 4) { 83 printk("gpr%02d: %08lx %08lx %08lx %08lx\n", i, 84 kvmppc_get_gpr(vcpu, i), 85 kvmppc_get_gpr(vcpu, i+1), 86 kvmppc_get_gpr(vcpu, i+2), 87 kvmppc_get_gpr(vcpu, i+3)); 88 } 89 } 90 91 #ifdef CONFIG_SPE 92 void kvmppc_vcpu_disable_spe(struct kvm_vcpu *vcpu) 93 { 94 preempt_disable(); 95 enable_kernel_spe(); 96 kvmppc_save_guest_spe(vcpu); 97 vcpu->arch.shadow_msr &= ~MSR_SPE; 98 preempt_enable(); 99 } 100 101 static void kvmppc_vcpu_enable_spe(struct kvm_vcpu *vcpu) 102 { 103 preempt_disable(); 104 enable_kernel_spe(); 105 kvmppc_load_guest_spe(vcpu); 106 vcpu->arch.shadow_msr |= MSR_SPE; 107 preempt_enable(); 108 } 109 110 static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu) 111 { 112 if (vcpu->arch.shared->msr & MSR_SPE) { 113 if (!(vcpu->arch.shadow_msr & MSR_SPE)) 114 kvmppc_vcpu_enable_spe(vcpu); 115 } else if (vcpu->arch.shadow_msr & MSR_SPE) { 116 kvmppc_vcpu_disable_spe(vcpu); 117 } 118 } 119 #else 120 static void kvmppc_vcpu_sync_spe(struct kvm_vcpu *vcpu) 121 { 122 } 123 #endif 124 125 /* 126 * Helper function for "full" MSR writes. No need to call this if only 127 * EE/CE/ME/DE/RI are changing. 128 */ 129 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u32 new_msr) 130 { 131 u32 old_msr = vcpu->arch.shared->msr; 132 133 #ifdef CONFIG_KVM_BOOKE_HV 134 new_msr |= MSR_GS; 135 #endif 136 137 vcpu->arch.shared->msr = new_msr; 138 139 kvmppc_mmu_msr_notify(vcpu, old_msr); 140 kvmppc_vcpu_sync_spe(vcpu); 141 } 142 143 static void kvmppc_booke_queue_irqprio(struct kvm_vcpu *vcpu, 144 unsigned int priority) 145 { 146 trace_kvm_booke_queue_irqprio(vcpu, priority); 147 set_bit(priority, &vcpu->arch.pending_exceptions); 148 } 149 150 static void kvmppc_core_queue_dtlb_miss(struct kvm_vcpu *vcpu, 151 ulong dear_flags, ulong esr_flags) 152 { 153 vcpu->arch.queued_dear = dear_flags; 154 vcpu->arch.queued_esr = esr_flags; 155 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DTLB_MISS); 156 } 157 158 static void kvmppc_core_queue_data_storage(struct kvm_vcpu *vcpu, 159 ulong dear_flags, ulong esr_flags) 160 { 161 vcpu->arch.queued_dear = dear_flags; 162 vcpu->arch.queued_esr = esr_flags; 163 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DATA_STORAGE); 164 } 165 166 static void kvmppc_core_queue_inst_storage(struct kvm_vcpu *vcpu, 167 ulong esr_flags) 168 { 169 vcpu->arch.queued_esr = esr_flags; 170 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_INST_STORAGE); 171 } 172 173 void kvmppc_core_queue_program(struct kvm_vcpu *vcpu, ulong esr_flags) 174 { 175 vcpu->arch.queued_esr = esr_flags; 176 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_PROGRAM); 177 } 178 179 void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu) 180 { 181 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_DECREMENTER); 182 } 183 184 int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu) 185 { 186 return test_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions); 187 } 188 189 void kvmppc_core_dequeue_dec(struct kvm_vcpu *vcpu) 190 { 191 clear_bit(BOOKE_IRQPRIO_DECREMENTER, &vcpu->arch.pending_exceptions); 192 } 193 194 void kvmppc_core_queue_external(struct kvm_vcpu *vcpu, 195 struct kvm_interrupt *irq) 196 { 197 unsigned int prio = BOOKE_IRQPRIO_EXTERNAL; 198 199 if (irq->irq == KVM_INTERRUPT_SET_LEVEL) 200 prio = BOOKE_IRQPRIO_EXTERNAL_LEVEL; 201 202 kvmppc_booke_queue_irqprio(vcpu, prio); 203 } 204 205 void kvmppc_core_dequeue_external(struct kvm_vcpu *vcpu, 206 struct kvm_interrupt *irq) 207 { 208 clear_bit(BOOKE_IRQPRIO_EXTERNAL, &vcpu->arch.pending_exceptions); 209 clear_bit(BOOKE_IRQPRIO_EXTERNAL_LEVEL, &vcpu->arch.pending_exceptions); 210 } 211 212 static void set_guest_srr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) 213 { 214 #ifdef CONFIG_KVM_BOOKE_HV 215 mtspr(SPRN_GSRR0, srr0); 216 mtspr(SPRN_GSRR1, srr1); 217 #else 218 vcpu->arch.shared->srr0 = srr0; 219 vcpu->arch.shared->srr1 = srr1; 220 #endif 221 } 222 223 static void set_guest_csrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) 224 { 225 vcpu->arch.csrr0 = srr0; 226 vcpu->arch.csrr1 = srr1; 227 } 228 229 static void set_guest_dsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) 230 { 231 if (cpu_has_feature(CPU_FTR_DEBUG_LVL_EXC)) { 232 vcpu->arch.dsrr0 = srr0; 233 vcpu->arch.dsrr1 = srr1; 234 } else { 235 set_guest_csrr(vcpu, srr0, srr1); 236 } 237 } 238 239 static void set_guest_mcsrr(struct kvm_vcpu *vcpu, unsigned long srr0, u32 srr1) 240 { 241 vcpu->arch.mcsrr0 = srr0; 242 vcpu->arch.mcsrr1 = srr1; 243 } 244 245 static unsigned long get_guest_dear(struct kvm_vcpu *vcpu) 246 { 247 #ifdef CONFIG_KVM_BOOKE_HV 248 return mfspr(SPRN_GDEAR); 249 #else 250 return vcpu->arch.shared->dar; 251 #endif 252 } 253 254 static void set_guest_dear(struct kvm_vcpu *vcpu, unsigned long dear) 255 { 256 #ifdef CONFIG_KVM_BOOKE_HV 257 mtspr(SPRN_GDEAR, dear); 258 #else 259 vcpu->arch.shared->dar = dear; 260 #endif 261 } 262 263 static unsigned long get_guest_esr(struct kvm_vcpu *vcpu) 264 { 265 #ifdef CONFIG_KVM_BOOKE_HV 266 return mfspr(SPRN_GESR); 267 #else 268 return vcpu->arch.shared->esr; 269 #endif 270 } 271 272 static void set_guest_esr(struct kvm_vcpu *vcpu, u32 esr) 273 { 274 #ifdef CONFIG_KVM_BOOKE_HV 275 mtspr(SPRN_GESR, esr); 276 #else 277 vcpu->arch.shared->esr = esr; 278 #endif 279 } 280 281 /* Deliver the interrupt of the corresponding priority, if possible. */ 282 static int kvmppc_booke_irqprio_deliver(struct kvm_vcpu *vcpu, 283 unsigned int priority) 284 { 285 int allowed = 0; 286 ulong msr_mask = 0; 287 bool update_esr = false, update_dear = false; 288 ulong crit_raw = vcpu->arch.shared->critical; 289 ulong crit_r1 = kvmppc_get_gpr(vcpu, 1); 290 bool crit; 291 bool keep_irq = false; 292 enum int_class int_class; 293 294 /* Truncate crit indicators in 32 bit mode */ 295 if (!(vcpu->arch.shared->msr & MSR_SF)) { 296 crit_raw &= 0xffffffff; 297 crit_r1 &= 0xffffffff; 298 } 299 300 /* Critical section when crit == r1 */ 301 crit = (crit_raw == crit_r1); 302 /* ... and we're in supervisor mode */ 303 crit = crit && !(vcpu->arch.shared->msr & MSR_PR); 304 305 if (priority == BOOKE_IRQPRIO_EXTERNAL_LEVEL) { 306 priority = BOOKE_IRQPRIO_EXTERNAL; 307 keep_irq = true; 308 } 309 310 switch (priority) { 311 case BOOKE_IRQPRIO_DTLB_MISS: 312 case BOOKE_IRQPRIO_DATA_STORAGE: 313 update_dear = true; 314 /* fall through */ 315 case BOOKE_IRQPRIO_INST_STORAGE: 316 case BOOKE_IRQPRIO_PROGRAM: 317 update_esr = true; 318 /* fall through */ 319 case BOOKE_IRQPRIO_ITLB_MISS: 320 case BOOKE_IRQPRIO_SYSCALL: 321 case BOOKE_IRQPRIO_FP_UNAVAIL: 322 case BOOKE_IRQPRIO_SPE_UNAVAIL: 323 case BOOKE_IRQPRIO_SPE_FP_DATA: 324 case BOOKE_IRQPRIO_SPE_FP_ROUND: 325 case BOOKE_IRQPRIO_AP_UNAVAIL: 326 case BOOKE_IRQPRIO_ALIGNMENT: 327 allowed = 1; 328 msr_mask = MSR_CE | MSR_ME | MSR_DE; 329 int_class = INT_CLASS_NONCRIT; 330 break; 331 case BOOKE_IRQPRIO_CRITICAL: 332 case BOOKE_IRQPRIO_DBELL_CRIT: 333 allowed = vcpu->arch.shared->msr & MSR_CE; 334 allowed = allowed && !crit; 335 msr_mask = MSR_ME; 336 int_class = INT_CLASS_CRIT; 337 break; 338 case BOOKE_IRQPRIO_MACHINE_CHECK: 339 allowed = vcpu->arch.shared->msr & MSR_ME; 340 allowed = allowed && !crit; 341 int_class = INT_CLASS_MC; 342 break; 343 case BOOKE_IRQPRIO_DECREMENTER: 344 case BOOKE_IRQPRIO_FIT: 345 keep_irq = true; 346 /* fall through */ 347 case BOOKE_IRQPRIO_EXTERNAL: 348 case BOOKE_IRQPRIO_DBELL: 349 allowed = vcpu->arch.shared->msr & MSR_EE; 350 allowed = allowed && !crit; 351 msr_mask = MSR_CE | MSR_ME | MSR_DE; 352 int_class = INT_CLASS_NONCRIT; 353 break; 354 case BOOKE_IRQPRIO_DEBUG: 355 allowed = vcpu->arch.shared->msr & MSR_DE; 356 allowed = allowed && !crit; 357 msr_mask = MSR_ME; 358 int_class = INT_CLASS_CRIT; 359 break; 360 } 361 362 if (allowed) { 363 switch (int_class) { 364 case INT_CLASS_NONCRIT: 365 set_guest_srr(vcpu, vcpu->arch.pc, 366 vcpu->arch.shared->msr); 367 break; 368 case INT_CLASS_CRIT: 369 set_guest_csrr(vcpu, vcpu->arch.pc, 370 vcpu->arch.shared->msr); 371 break; 372 case INT_CLASS_DBG: 373 set_guest_dsrr(vcpu, vcpu->arch.pc, 374 vcpu->arch.shared->msr); 375 break; 376 case INT_CLASS_MC: 377 set_guest_mcsrr(vcpu, vcpu->arch.pc, 378 vcpu->arch.shared->msr); 379 break; 380 } 381 382 vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[priority]; 383 if (update_esr == true) 384 set_guest_esr(vcpu, vcpu->arch.queued_esr); 385 if (update_dear == true) 386 set_guest_dear(vcpu, vcpu->arch.queued_dear); 387 kvmppc_set_msr(vcpu, vcpu->arch.shared->msr & msr_mask); 388 389 if (!keep_irq) 390 clear_bit(priority, &vcpu->arch.pending_exceptions); 391 } 392 393 #ifdef CONFIG_KVM_BOOKE_HV 394 /* 395 * If an interrupt is pending but masked, raise a guest doorbell 396 * so that we are notified when the guest enables the relevant 397 * MSR bit. 398 */ 399 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_EE) 400 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_NONCRIT); 401 if (vcpu->arch.pending_exceptions & BOOKE_IRQMASK_CE) 402 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_CRIT); 403 if (vcpu->arch.pending_exceptions & BOOKE_IRQPRIO_MACHINE_CHECK) 404 kvmppc_set_pending_interrupt(vcpu, INT_CLASS_MC); 405 #endif 406 407 return allowed; 408 } 409 410 static void update_timer_ints(struct kvm_vcpu *vcpu) 411 { 412 if ((vcpu->arch.tcr & TCR_DIE) && (vcpu->arch.tsr & TSR_DIS)) 413 kvmppc_core_queue_dec(vcpu); 414 else 415 kvmppc_core_dequeue_dec(vcpu); 416 } 417 418 static void kvmppc_core_check_exceptions(struct kvm_vcpu *vcpu) 419 { 420 unsigned long *pending = &vcpu->arch.pending_exceptions; 421 unsigned int priority; 422 423 priority = __ffs(*pending); 424 while (priority < BOOKE_IRQPRIO_MAX) { 425 if (kvmppc_booke_irqprio_deliver(vcpu, priority)) 426 break; 427 428 priority = find_next_bit(pending, 429 BITS_PER_BYTE * sizeof(*pending), 430 priority + 1); 431 } 432 433 /* Tell the guest about our interrupt status */ 434 vcpu->arch.shared->int_pending = !!*pending; 435 } 436 437 /* Check pending exceptions and deliver one, if possible. */ 438 int kvmppc_core_prepare_to_enter(struct kvm_vcpu *vcpu) 439 { 440 int r = 0; 441 WARN_ON_ONCE(!irqs_disabled()); 442 443 kvmppc_core_check_exceptions(vcpu); 444 445 if (vcpu->arch.shared->msr & MSR_WE) { 446 local_irq_enable(); 447 kvm_vcpu_block(vcpu); 448 clear_bit(KVM_REQ_UNHALT, &vcpu->requests); 449 local_irq_disable(); 450 451 kvmppc_set_exit_type(vcpu, EMULATED_MTMSRWE_EXITS); 452 r = 1; 453 }; 454 455 return r; 456 } 457 458 int kvmppc_core_check_requests(struct kvm_vcpu *vcpu) 459 { 460 int r = 1; /* Indicate we want to get back into the guest */ 461 462 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) 463 update_timer_ints(vcpu); 464 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) 465 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) 466 kvmppc_core_flush_tlb(vcpu); 467 #endif 468 469 return r; 470 } 471 472 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) 473 { 474 int ret, s; 475 #ifdef CONFIG_PPC_FPU 476 unsigned int fpscr; 477 int fpexc_mode; 478 u64 fpr[32]; 479 #endif 480 481 if (!vcpu->arch.sane) { 482 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 483 return -EINVAL; 484 } 485 486 local_irq_disable(); 487 s = kvmppc_prepare_to_enter(vcpu); 488 if (s <= 0) { 489 local_irq_enable(); 490 ret = s; 491 goto out; 492 } 493 kvmppc_lazy_ee_enable(); 494 495 kvm_guest_enter(); 496 497 #ifdef CONFIG_PPC_FPU 498 /* Save userspace FPU state in stack */ 499 enable_kernel_fp(); 500 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr)); 501 fpscr = current->thread.fpscr.val; 502 fpexc_mode = current->thread.fpexc_mode; 503 504 /* Restore guest FPU state to thread */ 505 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr)); 506 current->thread.fpscr.val = vcpu->arch.fpscr; 507 508 /* 509 * Since we can't trap on MSR_FP in GS-mode, we consider the guest 510 * as always using the FPU. Kernel usage of FP (via 511 * enable_kernel_fp()) in this thread must not occur while 512 * vcpu->fpu_active is set. 513 */ 514 vcpu->fpu_active = 1; 515 516 kvmppc_load_guest_fp(vcpu); 517 #endif 518 519 ret = __kvmppc_vcpu_run(kvm_run, vcpu); 520 521 /* No need for kvm_guest_exit. It's done in handle_exit. 522 We also get here with interrupts enabled. */ 523 524 #ifdef CONFIG_PPC_FPU 525 kvmppc_save_guest_fp(vcpu); 526 527 vcpu->fpu_active = 0; 528 529 /* Save guest FPU state from thread */ 530 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr)); 531 vcpu->arch.fpscr = current->thread.fpscr.val; 532 533 /* Restore userspace FPU state from stack */ 534 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr)); 535 current->thread.fpscr.val = fpscr; 536 current->thread.fpexc_mode = fpexc_mode; 537 #endif 538 539 out: 540 vcpu->mode = OUTSIDE_GUEST_MODE; 541 smp_wmb(); 542 return ret; 543 } 544 545 static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) 546 { 547 enum emulation_result er; 548 549 er = kvmppc_emulate_instruction(run, vcpu); 550 switch (er) { 551 case EMULATE_DONE: 552 /* don't overwrite subtypes, just account kvm_stats */ 553 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS); 554 /* Future optimization: only reload non-volatiles if 555 * they were actually modified by emulation. */ 556 return RESUME_GUEST_NV; 557 558 case EMULATE_DO_DCR: 559 run->exit_reason = KVM_EXIT_DCR; 560 return RESUME_HOST; 561 562 case EMULATE_FAIL: 563 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n", 564 __func__, vcpu->arch.pc, vcpu->arch.last_inst); 565 /* For debugging, encode the failing instruction and 566 * report it to userspace. */ 567 run->hw.hardware_exit_reason = ~0ULL << 32; 568 run->hw.hardware_exit_reason |= vcpu->arch.last_inst; 569 kvmppc_core_queue_program(vcpu, ESR_PIL); 570 return RESUME_HOST; 571 572 default: 573 BUG(); 574 } 575 } 576 577 static void kvmppc_fill_pt_regs(struct pt_regs *regs) 578 { 579 ulong r1, ip, msr, lr; 580 581 asm("mr %0, 1" : "=r"(r1)); 582 asm("mflr %0" : "=r"(lr)); 583 asm("mfmsr %0" : "=r"(msr)); 584 asm("bl 1f; 1: mflr %0" : "=r"(ip)); 585 586 memset(regs, 0, sizeof(*regs)); 587 regs->gpr[1] = r1; 588 regs->nip = ip; 589 regs->msr = msr; 590 regs->link = lr; 591 } 592 593 /* 594 * For interrupts needed to be handled by host interrupt handlers, 595 * corresponding host handler are called from here in similar way 596 * (but not exact) as they are called from low level handler 597 * (such as from arch/powerpc/kernel/head_fsl_booke.S). 598 */ 599 static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu, 600 unsigned int exit_nr) 601 { 602 struct pt_regs regs; 603 604 switch (exit_nr) { 605 case BOOKE_INTERRUPT_EXTERNAL: 606 kvmppc_fill_pt_regs(®s); 607 do_IRQ(®s); 608 break; 609 case BOOKE_INTERRUPT_DECREMENTER: 610 kvmppc_fill_pt_regs(®s); 611 timer_interrupt(®s); 612 break; 613 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64) 614 case BOOKE_INTERRUPT_DOORBELL: 615 kvmppc_fill_pt_regs(®s); 616 doorbell_exception(®s); 617 break; 618 #endif 619 case BOOKE_INTERRUPT_MACHINE_CHECK: 620 /* FIXME */ 621 break; 622 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR: 623 kvmppc_fill_pt_regs(®s); 624 performance_monitor_exception(®s); 625 break; 626 case BOOKE_INTERRUPT_WATCHDOG: 627 kvmppc_fill_pt_regs(®s); 628 #ifdef CONFIG_BOOKE_WDT 629 WatchdogException(®s); 630 #else 631 unknown_exception(®s); 632 #endif 633 break; 634 case BOOKE_INTERRUPT_CRITICAL: 635 unknown_exception(®s); 636 break; 637 } 638 } 639 640 /** 641 * kvmppc_handle_exit 642 * 643 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) 644 */ 645 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, 646 unsigned int exit_nr) 647 { 648 int r = RESUME_HOST; 649 int s; 650 651 /* update before a new last_exit_type is rewritten */ 652 kvmppc_update_timing_stats(vcpu); 653 654 /* restart interrupts if they were meant for the host */ 655 kvmppc_restart_interrupt(vcpu, exit_nr); 656 657 local_irq_enable(); 658 659 trace_kvm_exit(exit_nr, vcpu); 660 kvm_guest_exit(); 661 662 run->exit_reason = KVM_EXIT_UNKNOWN; 663 run->ready_for_interrupt_injection = 1; 664 665 switch (exit_nr) { 666 case BOOKE_INTERRUPT_MACHINE_CHECK: 667 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR)); 668 kvmppc_dump_vcpu(vcpu); 669 /* For debugging, send invalid exit reason to user space */ 670 run->hw.hardware_exit_reason = ~1ULL << 32; 671 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR); 672 r = RESUME_HOST; 673 break; 674 675 case BOOKE_INTERRUPT_EXTERNAL: 676 kvmppc_account_exit(vcpu, EXT_INTR_EXITS); 677 r = RESUME_GUEST; 678 break; 679 680 case BOOKE_INTERRUPT_DECREMENTER: 681 kvmppc_account_exit(vcpu, DEC_EXITS); 682 r = RESUME_GUEST; 683 break; 684 685 case BOOKE_INTERRUPT_WATCHDOG: 686 r = RESUME_GUEST; 687 break; 688 689 case BOOKE_INTERRUPT_DOORBELL: 690 kvmppc_account_exit(vcpu, DBELL_EXITS); 691 r = RESUME_GUEST; 692 break; 693 694 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT: 695 kvmppc_account_exit(vcpu, GDBELL_EXITS); 696 697 /* 698 * We are here because there is a pending guest interrupt 699 * which could not be delivered as MSR_CE or MSR_ME was not 700 * set. Once we break from here we will retry delivery. 701 */ 702 r = RESUME_GUEST; 703 break; 704 705 case BOOKE_INTERRUPT_GUEST_DBELL: 706 kvmppc_account_exit(vcpu, GDBELL_EXITS); 707 708 /* 709 * We are here because there is a pending guest interrupt 710 * which could not be delivered as MSR_EE was not set. Once 711 * we break from here we will retry delivery. 712 */ 713 r = RESUME_GUEST; 714 break; 715 716 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR: 717 r = RESUME_GUEST; 718 break; 719 720 case BOOKE_INTERRUPT_HV_PRIV: 721 r = emulation_exit(run, vcpu); 722 break; 723 724 case BOOKE_INTERRUPT_PROGRAM: 725 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) { 726 /* 727 * Program traps generated by user-level software must 728 * be handled by the guest kernel. 729 * 730 * In GS mode, hypervisor privileged instructions trap 731 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are 732 * actual program interrupts, handled by the guest. 733 */ 734 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr); 735 r = RESUME_GUEST; 736 kvmppc_account_exit(vcpu, USR_PR_INST); 737 break; 738 } 739 740 r = emulation_exit(run, vcpu); 741 break; 742 743 case BOOKE_INTERRUPT_FP_UNAVAIL: 744 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL); 745 kvmppc_account_exit(vcpu, FP_UNAVAIL); 746 r = RESUME_GUEST; 747 break; 748 749 #ifdef CONFIG_SPE 750 case BOOKE_INTERRUPT_SPE_UNAVAIL: { 751 if (vcpu->arch.shared->msr & MSR_SPE) 752 kvmppc_vcpu_enable_spe(vcpu); 753 else 754 kvmppc_booke_queue_irqprio(vcpu, 755 BOOKE_IRQPRIO_SPE_UNAVAIL); 756 r = RESUME_GUEST; 757 break; 758 } 759 760 case BOOKE_INTERRUPT_SPE_FP_DATA: 761 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA); 762 r = RESUME_GUEST; 763 break; 764 765 case BOOKE_INTERRUPT_SPE_FP_ROUND: 766 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND); 767 r = RESUME_GUEST; 768 break; 769 #else 770 case BOOKE_INTERRUPT_SPE_UNAVAIL: 771 /* 772 * Guest wants SPE, but host kernel doesn't support it. Send 773 * an "unimplemented operation" program check to the guest. 774 */ 775 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV); 776 r = RESUME_GUEST; 777 break; 778 779 /* 780 * These really should never happen without CONFIG_SPE, 781 * as we should never enable the real MSR[SPE] in the guest. 782 */ 783 case BOOKE_INTERRUPT_SPE_FP_DATA: 784 case BOOKE_INTERRUPT_SPE_FP_ROUND: 785 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n", 786 __func__, exit_nr, vcpu->arch.pc); 787 run->hw.hardware_exit_reason = exit_nr; 788 r = RESUME_HOST; 789 break; 790 #endif 791 792 case BOOKE_INTERRUPT_DATA_STORAGE: 793 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear, 794 vcpu->arch.fault_esr); 795 kvmppc_account_exit(vcpu, DSI_EXITS); 796 r = RESUME_GUEST; 797 break; 798 799 case BOOKE_INTERRUPT_INST_STORAGE: 800 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr); 801 kvmppc_account_exit(vcpu, ISI_EXITS); 802 r = RESUME_GUEST; 803 break; 804 805 #ifdef CONFIG_KVM_BOOKE_HV 806 case BOOKE_INTERRUPT_HV_SYSCALL: 807 if (!(vcpu->arch.shared->msr & MSR_PR)) { 808 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); 809 } else { 810 /* 811 * hcall from guest userspace -- send privileged 812 * instruction program check. 813 */ 814 kvmppc_core_queue_program(vcpu, ESR_PPR); 815 } 816 817 r = RESUME_GUEST; 818 break; 819 #else 820 case BOOKE_INTERRUPT_SYSCALL: 821 if (!(vcpu->arch.shared->msr & MSR_PR) && 822 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) { 823 /* KVM PV hypercalls */ 824 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); 825 r = RESUME_GUEST; 826 } else { 827 /* Guest syscalls */ 828 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL); 829 } 830 kvmppc_account_exit(vcpu, SYSCALL_EXITS); 831 r = RESUME_GUEST; 832 break; 833 #endif 834 835 case BOOKE_INTERRUPT_DTLB_MISS: { 836 unsigned long eaddr = vcpu->arch.fault_dear; 837 int gtlb_index; 838 gpa_t gpaddr; 839 gfn_t gfn; 840 841 #ifdef CONFIG_KVM_E500V2 842 if (!(vcpu->arch.shared->msr & MSR_PR) && 843 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) { 844 kvmppc_map_magic(vcpu); 845 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS); 846 r = RESUME_GUEST; 847 848 break; 849 } 850 #endif 851 852 /* Check the guest TLB. */ 853 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr); 854 if (gtlb_index < 0) { 855 /* The guest didn't have a mapping for it. */ 856 kvmppc_core_queue_dtlb_miss(vcpu, 857 vcpu->arch.fault_dear, 858 vcpu->arch.fault_esr); 859 kvmppc_mmu_dtlb_miss(vcpu); 860 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS); 861 r = RESUME_GUEST; 862 break; 863 } 864 865 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr); 866 gfn = gpaddr >> PAGE_SHIFT; 867 868 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { 869 /* The guest TLB had a mapping, but the shadow TLB 870 * didn't, and it is RAM. This could be because: 871 * a) the entry is mapping the host kernel, or 872 * b) the guest used a large mapping which we're faking 873 * Either way, we need to satisfy the fault without 874 * invoking the guest. */ 875 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); 876 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS); 877 r = RESUME_GUEST; 878 } else { 879 /* Guest has mapped and accessed a page which is not 880 * actually RAM. */ 881 vcpu->arch.paddr_accessed = gpaddr; 882 vcpu->arch.vaddr_accessed = eaddr; 883 r = kvmppc_emulate_mmio(run, vcpu); 884 kvmppc_account_exit(vcpu, MMIO_EXITS); 885 } 886 887 break; 888 } 889 890 case BOOKE_INTERRUPT_ITLB_MISS: { 891 unsigned long eaddr = vcpu->arch.pc; 892 gpa_t gpaddr; 893 gfn_t gfn; 894 int gtlb_index; 895 896 r = RESUME_GUEST; 897 898 /* Check the guest TLB. */ 899 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr); 900 if (gtlb_index < 0) { 901 /* The guest didn't have a mapping for it. */ 902 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS); 903 kvmppc_mmu_itlb_miss(vcpu); 904 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS); 905 break; 906 } 907 908 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS); 909 910 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr); 911 gfn = gpaddr >> PAGE_SHIFT; 912 913 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { 914 /* The guest TLB had a mapping, but the shadow TLB 915 * didn't. This could be because: 916 * a) the entry is mapping the host kernel, or 917 * b) the guest used a large mapping which we're faking 918 * Either way, we need to satisfy the fault without 919 * invoking the guest. */ 920 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); 921 } else { 922 /* Guest mapped and leaped at non-RAM! */ 923 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK); 924 } 925 926 break; 927 } 928 929 case BOOKE_INTERRUPT_DEBUG: { 930 u32 dbsr; 931 932 vcpu->arch.pc = mfspr(SPRN_CSRR0); 933 934 /* clear IAC events in DBSR register */ 935 dbsr = mfspr(SPRN_DBSR); 936 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4; 937 mtspr(SPRN_DBSR, dbsr); 938 939 run->exit_reason = KVM_EXIT_DEBUG; 940 kvmppc_account_exit(vcpu, DEBUG_EXITS); 941 r = RESUME_HOST; 942 break; 943 } 944 945 default: 946 printk(KERN_EMERG "exit_nr %d\n", exit_nr); 947 BUG(); 948 } 949 950 /* 951 * To avoid clobbering exit_reason, only check for signals if we 952 * aren't already exiting to userspace for some other reason. 953 */ 954 if (!(r & RESUME_HOST)) { 955 local_irq_disable(); 956 s = kvmppc_prepare_to_enter(vcpu); 957 if (s <= 0) { 958 local_irq_enable(); 959 r = (s << 2) | RESUME_HOST | (r & RESUME_FLAG_NV); 960 } else { 961 kvmppc_lazy_ee_enable(); 962 } 963 } 964 965 return r; 966 } 967 968 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */ 969 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 970 { 971 int i; 972 int r; 973 974 vcpu->arch.pc = 0; 975 vcpu->arch.shared->pir = vcpu->vcpu_id; 976 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */ 977 kvmppc_set_msr(vcpu, 0); 978 979 #ifndef CONFIG_KVM_BOOKE_HV 980 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS; 981 vcpu->arch.shadow_pid = 1; 982 vcpu->arch.shared->msr = 0; 983 #endif 984 985 /* Eye-catching numbers so we know if the guest takes an interrupt 986 * before it's programmed its own IVPR/IVORs. */ 987 vcpu->arch.ivpr = 0x55550000; 988 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++) 989 vcpu->arch.ivor[i] = 0x7700 | i * 4; 990 991 kvmppc_init_timing_stats(vcpu); 992 993 r = kvmppc_core_vcpu_setup(vcpu); 994 kvmppc_sanity_check(vcpu); 995 return r; 996 } 997 998 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 999 { 1000 int i; 1001 1002 regs->pc = vcpu->arch.pc; 1003 regs->cr = kvmppc_get_cr(vcpu); 1004 regs->ctr = vcpu->arch.ctr; 1005 regs->lr = vcpu->arch.lr; 1006 regs->xer = kvmppc_get_xer(vcpu); 1007 regs->msr = vcpu->arch.shared->msr; 1008 regs->srr0 = vcpu->arch.shared->srr0; 1009 regs->srr1 = vcpu->arch.shared->srr1; 1010 regs->pid = vcpu->arch.pid; 1011 regs->sprg0 = vcpu->arch.shared->sprg0; 1012 regs->sprg1 = vcpu->arch.shared->sprg1; 1013 regs->sprg2 = vcpu->arch.shared->sprg2; 1014 regs->sprg3 = vcpu->arch.shared->sprg3; 1015 regs->sprg4 = vcpu->arch.shared->sprg4; 1016 regs->sprg5 = vcpu->arch.shared->sprg5; 1017 regs->sprg6 = vcpu->arch.shared->sprg6; 1018 regs->sprg7 = vcpu->arch.shared->sprg7; 1019 1020 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) 1021 regs->gpr[i] = kvmppc_get_gpr(vcpu, i); 1022 1023 return 0; 1024 } 1025 1026 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 1027 { 1028 int i; 1029 1030 vcpu->arch.pc = regs->pc; 1031 kvmppc_set_cr(vcpu, regs->cr); 1032 vcpu->arch.ctr = regs->ctr; 1033 vcpu->arch.lr = regs->lr; 1034 kvmppc_set_xer(vcpu, regs->xer); 1035 kvmppc_set_msr(vcpu, regs->msr); 1036 vcpu->arch.shared->srr0 = regs->srr0; 1037 vcpu->arch.shared->srr1 = regs->srr1; 1038 kvmppc_set_pid(vcpu, regs->pid); 1039 vcpu->arch.shared->sprg0 = regs->sprg0; 1040 vcpu->arch.shared->sprg1 = regs->sprg1; 1041 vcpu->arch.shared->sprg2 = regs->sprg2; 1042 vcpu->arch.shared->sprg3 = regs->sprg3; 1043 vcpu->arch.shared->sprg4 = regs->sprg4; 1044 vcpu->arch.shared->sprg5 = regs->sprg5; 1045 vcpu->arch.shared->sprg6 = regs->sprg6; 1046 vcpu->arch.shared->sprg7 = regs->sprg7; 1047 1048 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) 1049 kvmppc_set_gpr(vcpu, i, regs->gpr[i]); 1050 1051 return 0; 1052 } 1053 1054 static void get_sregs_base(struct kvm_vcpu *vcpu, 1055 struct kvm_sregs *sregs) 1056 { 1057 u64 tb = get_tb(); 1058 1059 sregs->u.e.features |= KVM_SREGS_E_BASE; 1060 1061 sregs->u.e.csrr0 = vcpu->arch.csrr0; 1062 sregs->u.e.csrr1 = vcpu->arch.csrr1; 1063 sregs->u.e.mcsr = vcpu->arch.mcsr; 1064 sregs->u.e.esr = get_guest_esr(vcpu); 1065 sregs->u.e.dear = get_guest_dear(vcpu); 1066 sregs->u.e.tsr = vcpu->arch.tsr; 1067 sregs->u.e.tcr = vcpu->arch.tcr; 1068 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb); 1069 sregs->u.e.tb = tb; 1070 sregs->u.e.vrsave = vcpu->arch.vrsave; 1071 } 1072 1073 static int set_sregs_base(struct kvm_vcpu *vcpu, 1074 struct kvm_sregs *sregs) 1075 { 1076 if (!(sregs->u.e.features & KVM_SREGS_E_BASE)) 1077 return 0; 1078 1079 vcpu->arch.csrr0 = sregs->u.e.csrr0; 1080 vcpu->arch.csrr1 = sregs->u.e.csrr1; 1081 vcpu->arch.mcsr = sregs->u.e.mcsr; 1082 set_guest_esr(vcpu, sregs->u.e.esr); 1083 set_guest_dear(vcpu, sregs->u.e.dear); 1084 vcpu->arch.vrsave = sregs->u.e.vrsave; 1085 kvmppc_set_tcr(vcpu, sregs->u.e.tcr); 1086 1087 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) { 1088 vcpu->arch.dec = sregs->u.e.dec; 1089 kvmppc_emulate_dec(vcpu); 1090 } 1091 1092 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) { 1093 vcpu->arch.tsr = sregs->u.e.tsr; 1094 update_timer_ints(vcpu); 1095 } 1096 1097 return 0; 1098 } 1099 1100 static void get_sregs_arch206(struct kvm_vcpu *vcpu, 1101 struct kvm_sregs *sregs) 1102 { 1103 sregs->u.e.features |= KVM_SREGS_E_ARCH206; 1104 1105 sregs->u.e.pir = vcpu->vcpu_id; 1106 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0; 1107 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1; 1108 sregs->u.e.decar = vcpu->arch.decar; 1109 sregs->u.e.ivpr = vcpu->arch.ivpr; 1110 } 1111 1112 static int set_sregs_arch206(struct kvm_vcpu *vcpu, 1113 struct kvm_sregs *sregs) 1114 { 1115 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206)) 1116 return 0; 1117 1118 if (sregs->u.e.pir != vcpu->vcpu_id) 1119 return -EINVAL; 1120 1121 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0; 1122 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1; 1123 vcpu->arch.decar = sregs->u.e.decar; 1124 vcpu->arch.ivpr = sregs->u.e.ivpr; 1125 1126 return 0; 1127 } 1128 1129 void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 1130 { 1131 sregs->u.e.features |= KVM_SREGS_E_IVOR; 1132 1133 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL]; 1134 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK]; 1135 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]; 1136 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE]; 1137 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL]; 1138 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT]; 1139 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM]; 1140 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL]; 1141 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]; 1142 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL]; 1143 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER]; 1144 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT]; 1145 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG]; 1146 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS]; 1147 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS]; 1148 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG]; 1149 } 1150 1151 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 1152 { 1153 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR)) 1154 return 0; 1155 1156 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0]; 1157 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1]; 1158 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2]; 1159 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3]; 1160 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4]; 1161 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5]; 1162 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6]; 1163 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7]; 1164 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8]; 1165 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9]; 1166 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10]; 1167 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11]; 1168 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12]; 1169 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13]; 1170 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14]; 1171 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15]; 1172 1173 return 0; 1174 } 1175 1176 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 1177 struct kvm_sregs *sregs) 1178 { 1179 sregs->pvr = vcpu->arch.pvr; 1180 1181 get_sregs_base(vcpu, sregs); 1182 get_sregs_arch206(vcpu, sregs); 1183 kvmppc_core_get_sregs(vcpu, sregs); 1184 return 0; 1185 } 1186 1187 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 1188 struct kvm_sregs *sregs) 1189 { 1190 int ret; 1191 1192 if (vcpu->arch.pvr != sregs->pvr) 1193 return -EINVAL; 1194 1195 ret = set_sregs_base(vcpu, sregs); 1196 if (ret < 0) 1197 return ret; 1198 1199 ret = set_sregs_arch206(vcpu, sregs); 1200 if (ret < 0) 1201 return ret; 1202 1203 return kvmppc_core_set_sregs(vcpu, sregs); 1204 } 1205 1206 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) 1207 { 1208 return -EINVAL; 1209 } 1210 1211 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) 1212 { 1213 return -EINVAL; 1214 } 1215 1216 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1217 { 1218 return -ENOTSUPP; 1219 } 1220 1221 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1222 { 1223 return -ENOTSUPP; 1224 } 1225 1226 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 1227 struct kvm_translation *tr) 1228 { 1229 int r; 1230 1231 r = kvmppc_core_vcpu_translate(vcpu, tr); 1232 return r; 1233 } 1234 1235 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) 1236 { 1237 return -ENOTSUPP; 1238 } 1239 1240 int kvmppc_core_prepare_memory_region(struct kvm *kvm, 1241 struct kvm_userspace_memory_region *mem) 1242 { 1243 return 0; 1244 } 1245 1246 void kvmppc_core_commit_memory_region(struct kvm *kvm, 1247 struct kvm_userspace_memory_region *mem) 1248 { 1249 } 1250 1251 void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr) 1252 { 1253 vcpu->arch.tcr = new_tcr; 1254 update_timer_ints(vcpu); 1255 } 1256 1257 void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits) 1258 { 1259 set_bits(tsr_bits, &vcpu->arch.tsr); 1260 smp_wmb(); 1261 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); 1262 kvm_vcpu_kick(vcpu); 1263 } 1264 1265 void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits) 1266 { 1267 clear_bits(tsr_bits, &vcpu->arch.tsr); 1268 update_timer_ints(vcpu); 1269 } 1270 1271 void kvmppc_decrementer_func(unsigned long data) 1272 { 1273 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; 1274 1275 if (vcpu->arch.tcr & TCR_ARE) { 1276 vcpu->arch.dec = vcpu->arch.decar; 1277 kvmppc_emulate_dec(vcpu); 1278 } 1279 1280 kvmppc_set_tsr_bits(vcpu, TSR_DIS); 1281 } 1282 1283 void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 1284 { 1285 current->thread.kvm_vcpu = vcpu; 1286 } 1287 1288 void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu) 1289 { 1290 current->thread.kvm_vcpu = NULL; 1291 } 1292 1293 int __init kvmppc_booke_init(void) 1294 { 1295 #ifndef CONFIG_KVM_BOOKE_HV 1296 unsigned long ivor[16]; 1297 unsigned long max_ivor = 0; 1298 int i; 1299 1300 /* We install our own exception handlers by hijacking IVPR. IVPR must 1301 * be 16-bit aligned, so we need a 64KB allocation. */ 1302 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO, 1303 VCPU_SIZE_ORDER); 1304 if (!kvmppc_booke_handlers) 1305 return -ENOMEM; 1306 1307 /* XXX make sure our handlers are smaller than Linux's */ 1308 1309 /* Copy our interrupt handlers to match host IVORs. That way we don't 1310 * have to swap the IVORs on every guest/host transition. */ 1311 ivor[0] = mfspr(SPRN_IVOR0); 1312 ivor[1] = mfspr(SPRN_IVOR1); 1313 ivor[2] = mfspr(SPRN_IVOR2); 1314 ivor[3] = mfspr(SPRN_IVOR3); 1315 ivor[4] = mfspr(SPRN_IVOR4); 1316 ivor[5] = mfspr(SPRN_IVOR5); 1317 ivor[6] = mfspr(SPRN_IVOR6); 1318 ivor[7] = mfspr(SPRN_IVOR7); 1319 ivor[8] = mfspr(SPRN_IVOR8); 1320 ivor[9] = mfspr(SPRN_IVOR9); 1321 ivor[10] = mfspr(SPRN_IVOR10); 1322 ivor[11] = mfspr(SPRN_IVOR11); 1323 ivor[12] = mfspr(SPRN_IVOR12); 1324 ivor[13] = mfspr(SPRN_IVOR13); 1325 ivor[14] = mfspr(SPRN_IVOR14); 1326 ivor[15] = mfspr(SPRN_IVOR15); 1327 1328 for (i = 0; i < 16; i++) { 1329 if (ivor[i] > max_ivor) 1330 max_ivor = ivor[i]; 1331 1332 memcpy((void *)kvmppc_booke_handlers + ivor[i], 1333 kvmppc_handlers_start + i * kvmppc_handler_len, 1334 kvmppc_handler_len); 1335 } 1336 flush_icache_range(kvmppc_booke_handlers, 1337 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len); 1338 #endif /* !BOOKE_HV */ 1339 return 0; 1340 } 1341 1342 void __exit kvmppc_booke_exit(void) 1343 { 1344 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER); 1345 kvm_exit(); 1346 } 1347