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 void kvmppc_core_check_requests(struct kvm_vcpu *vcpu) 459 { 460 if (kvm_check_request(KVM_REQ_PENDING_TIMER, vcpu)) 461 update_timer_ints(vcpu); 462 #if defined(CONFIG_KVM_E500V2) || defined(CONFIG_KVM_E500MC) 463 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) 464 kvmppc_core_flush_tlb(vcpu); 465 #endif 466 } 467 468 int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) 469 { 470 int ret; 471 #ifdef CONFIG_PPC_FPU 472 unsigned int fpscr; 473 int fpexc_mode; 474 u64 fpr[32]; 475 #endif 476 477 if (!vcpu->arch.sane) { 478 kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR; 479 return -EINVAL; 480 } 481 482 local_irq_disable(); 483 if (kvmppc_prepare_to_enter(vcpu)) { 484 kvm_run->exit_reason = KVM_EXIT_INTR; 485 ret = -EINTR; 486 goto out; 487 } 488 489 kvm_guest_enter(); 490 491 #ifdef CONFIG_PPC_FPU 492 /* Save userspace FPU state in stack */ 493 enable_kernel_fp(); 494 memcpy(fpr, current->thread.fpr, sizeof(current->thread.fpr)); 495 fpscr = current->thread.fpscr.val; 496 fpexc_mode = current->thread.fpexc_mode; 497 498 /* Restore guest FPU state to thread */ 499 memcpy(current->thread.fpr, vcpu->arch.fpr, sizeof(vcpu->arch.fpr)); 500 current->thread.fpscr.val = vcpu->arch.fpscr; 501 502 /* 503 * Since we can't trap on MSR_FP in GS-mode, we consider the guest 504 * as always using the FPU. Kernel usage of FP (via 505 * enable_kernel_fp()) in this thread must not occur while 506 * vcpu->fpu_active is set. 507 */ 508 vcpu->fpu_active = 1; 509 510 kvmppc_load_guest_fp(vcpu); 511 #endif 512 513 ret = __kvmppc_vcpu_run(kvm_run, vcpu); 514 515 #ifdef CONFIG_PPC_FPU 516 kvmppc_save_guest_fp(vcpu); 517 518 vcpu->fpu_active = 0; 519 520 /* Save guest FPU state from thread */ 521 memcpy(vcpu->arch.fpr, current->thread.fpr, sizeof(vcpu->arch.fpr)); 522 vcpu->arch.fpscr = current->thread.fpscr.val; 523 524 /* Restore userspace FPU state from stack */ 525 memcpy(current->thread.fpr, fpr, sizeof(current->thread.fpr)); 526 current->thread.fpscr.val = fpscr; 527 current->thread.fpexc_mode = fpexc_mode; 528 #endif 529 530 kvm_guest_exit(); 531 532 out: 533 vcpu->mode = OUTSIDE_GUEST_MODE; 534 smp_wmb(); 535 local_irq_enable(); 536 return ret; 537 } 538 539 static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) 540 { 541 enum emulation_result er; 542 543 er = kvmppc_emulate_instruction(run, vcpu); 544 switch (er) { 545 case EMULATE_DONE: 546 /* don't overwrite subtypes, just account kvm_stats */ 547 kvmppc_account_exit_stat(vcpu, EMULATED_INST_EXITS); 548 /* Future optimization: only reload non-volatiles if 549 * they were actually modified by emulation. */ 550 return RESUME_GUEST_NV; 551 552 case EMULATE_DO_DCR: 553 run->exit_reason = KVM_EXIT_DCR; 554 return RESUME_HOST; 555 556 case EMULATE_FAIL: 557 printk(KERN_CRIT "%s: emulation at %lx failed (%08x)\n", 558 __func__, vcpu->arch.pc, vcpu->arch.last_inst); 559 /* For debugging, encode the failing instruction and 560 * report it to userspace. */ 561 run->hw.hardware_exit_reason = ~0ULL << 32; 562 run->hw.hardware_exit_reason |= vcpu->arch.last_inst; 563 kvmppc_core_queue_program(vcpu, ESR_PIL); 564 return RESUME_HOST; 565 566 default: 567 BUG(); 568 } 569 } 570 571 static void kvmppc_fill_pt_regs(struct pt_regs *regs) 572 { 573 ulong r1, ip, msr, lr; 574 575 asm("mr %0, 1" : "=r"(r1)); 576 asm("mflr %0" : "=r"(lr)); 577 asm("mfmsr %0" : "=r"(msr)); 578 asm("bl 1f; 1: mflr %0" : "=r"(ip)); 579 580 memset(regs, 0, sizeof(*regs)); 581 regs->gpr[1] = r1; 582 regs->nip = ip; 583 regs->msr = msr; 584 regs->link = lr; 585 } 586 587 /* 588 * For interrupts needed to be handled by host interrupt handlers, 589 * corresponding host handler are called from here in similar way 590 * (but not exact) as they are called from low level handler 591 * (such as from arch/powerpc/kernel/head_fsl_booke.S). 592 */ 593 static void kvmppc_restart_interrupt(struct kvm_vcpu *vcpu, 594 unsigned int exit_nr) 595 { 596 struct pt_regs regs; 597 598 switch (exit_nr) { 599 case BOOKE_INTERRUPT_EXTERNAL: 600 kvmppc_fill_pt_regs(®s); 601 do_IRQ(®s); 602 break; 603 case BOOKE_INTERRUPT_DECREMENTER: 604 kvmppc_fill_pt_regs(®s); 605 timer_interrupt(®s); 606 break; 607 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3E_64) 608 case BOOKE_INTERRUPT_DOORBELL: 609 kvmppc_fill_pt_regs(®s); 610 doorbell_exception(®s); 611 break; 612 #endif 613 case BOOKE_INTERRUPT_MACHINE_CHECK: 614 /* FIXME */ 615 break; 616 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR: 617 kvmppc_fill_pt_regs(®s); 618 performance_monitor_exception(®s); 619 break; 620 case BOOKE_INTERRUPT_WATCHDOG: 621 kvmppc_fill_pt_regs(®s); 622 #ifdef CONFIG_BOOKE_WDT 623 WatchdogException(®s); 624 #else 625 unknown_exception(®s); 626 #endif 627 break; 628 case BOOKE_INTERRUPT_CRITICAL: 629 unknown_exception(®s); 630 break; 631 } 632 } 633 634 /** 635 * kvmppc_handle_exit 636 * 637 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) 638 */ 639 int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, 640 unsigned int exit_nr) 641 { 642 int r = RESUME_HOST; 643 644 /* update before a new last_exit_type is rewritten */ 645 kvmppc_update_timing_stats(vcpu); 646 647 /* restart interrupts if they were meant for the host */ 648 kvmppc_restart_interrupt(vcpu, exit_nr); 649 650 local_irq_enable(); 651 652 trace_kvm_exit(exit_nr, vcpu); 653 kvm_guest_exit(); 654 655 run->exit_reason = KVM_EXIT_UNKNOWN; 656 run->ready_for_interrupt_injection = 1; 657 658 switch (exit_nr) { 659 case BOOKE_INTERRUPT_MACHINE_CHECK: 660 printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR)); 661 kvmppc_dump_vcpu(vcpu); 662 /* For debugging, send invalid exit reason to user space */ 663 run->hw.hardware_exit_reason = ~1ULL << 32; 664 run->hw.hardware_exit_reason |= mfspr(SPRN_MCSR); 665 r = RESUME_HOST; 666 break; 667 668 case BOOKE_INTERRUPT_EXTERNAL: 669 kvmppc_account_exit(vcpu, EXT_INTR_EXITS); 670 r = RESUME_GUEST; 671 break; 672 673 case BOOKE_INTERRUPT_DECREMENTER: 674 kvmppc_account_exit(vcpu, DEC_EXITS); 675 r = RESUME_GUEST; 676 break; 677 678 case BOOKE_INTERRUPT_WATCHDOG: 679 r = RESUME_GUEST; 680 break; 681 682 case BOOKE_INTERRUPT_DOORBELL: 683 kvmppc_account_exit(vcpu, DBELL_EXITS); 684 r = RESUME_GUEST; 685 break; 686 687 case BOOKE_INTERRUPT_GUEST_DBELL_CRIT: 688 kvmppc_account_exit(vcpu, GDBELL_EXITS); 689 690 /* 691 * We are here because there is a pending guest interrupt 692 * which could not be delivered as MSR_CE or MSR_ME was not 693 * set. Once we break from here we will retry delivery. 694 */ 695 r = RESUME_GUEST; 696 break; 697 698 case BOOKE_INTERRUPT_GUEST_DBELL: 699 kvmppc_account_exit(vcpu, GDBELL_EXITS); 700 701 /* 702 * We are here because there is a pending guest interrupt 703 * which could not be delivered as MSR_EE was not set. Once 704 * we break from here we will retry delivery. 705 */ 706 r = RESUME_GUEST; 707 break; 708 709 case BOOKE_INTERRUPT_PERFORMANCE_MONITOR: 710 r = RESUME_GUEST; 711 break; 712 713 case BOOKE_INTERRUPT_HV_PRIV: 714 r = emulation_exit(run, vcpu); 715 break; 716 717 case BOOKE_INTERRUPT_PROGRAM: 718 if (vcpu->arch.shared->msr & (MSR_PR | MSR_GS)) { 719 /* 720 * Program traps generated by user-level software must 721 * be handled by the guest kernel. 722 * 723 * In GS mode, hypervisor privileged instructions trap 724 * on BOOKE_INTERRUPT_HV_PRIV, not here, so these are 725 * actual program interrupts, handled by the guest. 726 */ 727 kvmppc_core_queue_program(vcpu, vcpu->arch.fault_esr); 728 r = RESUME_GUEST; 729 kvmppc_account_exit(vcpu, USR_PR_INST); 730 break; 731 } 732 733 r = emulation_exit(run, vcpu); 734 break; 735 736 case BOOKE_INTERRUPT_FP_UNAVAIL: 737 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_FP_UNAVAIL); 738 kvmppc_account_exit(vcpu, FP_UNAVAIL); 739 r = RESUME_GUEST; 740 break; 741 742 #ifdef CONFIG_SPE 743 case BOOKE_INTERRUPT_SPE_UNAVAIL: { 744 if (vcpu->arch.shared->msr & MSR_SPE) 745 kvmppc_vcpu_enable_spe(vcpu); 746 else 747 kvmppc_booke_queue_irqprio(vcpu, 748 BOOKE_IRQPRIO_SPE_UNAVAIL); 749 r = RESUME_GUEST; 750 break; 751 } 752 753 case BOOKE_INTERRUPT_SPE_FP_DATA: 754 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_DATA); 755 r = RESUME_GUEST; 756 break; 757 758 case BOOKE_INTERRUPT_SPE_FP_ROUND: 759 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SPE_FP_ROUND); 760 r = RESUME_GUEST; 761 break; 762 #else 763 case BOOKE_INTERRUPT_SPE_UNAVAIL: 764 /* 765 * Guest wants SPE, but host kernel doesn't support it. Send 766 * an "unimplemented operation" program check to the guest. 767 */ 768 kvmppc_core_queue_program(vcpu, ESR_PUO | ESR_SPV); 769 r = RESUME_GUEST; 770 break; 771 772 /* 773 * These really should never happen without CONFIG_SPE, 774 * as we should never enable the real MSR[SPE] in the guest. 775 */ 776 case BOOKE_INTERRUPT_SPE_FP_DATA: 777 case BOOKE_INTERRUPT_SPE_FP_ROUND: 778 printk(KERN_CRIT "%s: unexpected SPE interrupt %u at %08lx\n", 779 __func__, exit_nr, vcpu->arch.pc); 780 run->hw.hardware_exit_reason = exit_nr; 781 r = RESUME_HOST; 782 break; 783 #endif 784 785 case BOOKE_INTERRUPT_DATA_STORAGE: 786 kvmppc_core_queue_data_storage(vcpu, vcpu->arch.fault_dear, 787 vcpu->arch.fault_esr); 788 kvmppc_account_exit(vcpu, DSI_EXITS); 789 r = RESUME_GUEST; 790 break; 791 792 case BOOKE_INTERRUPT_INST_STORAGE: 793 kvmppc_core_queue_inst_storage(vcpu, vcpu->arch.fault_esr); 794 kvmppc_account_exit(vcpu, ISI_EXITS); 795 r = RESUME_GUEST; 796 break; 797 798 #ifdef CONFIG_KVM_BOOKE_HV 799 case BOOKE_INTERRUPT_HV_SYSCALL: 800 if (!(vcpu->arch.shared->msr & MSR_PR)) { 801 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); 802 } else { 803 /* 804 * hcall from guest userspace -- send privileged 805 * instruction program check. 806 */ 807 kvmppc_core_queue_program(vcpu, ESR_PPR); 808 } 809 810 r = RESUME_GUEST; 811 break; 812 #else 813 case BOOKE_INTERRUPT_SYSCALL: 814 if (!(vcpu->arch.shared->msr & MSR_PR) && 815 (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) { 816 /* KVM PV hypercalls */ 817 kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); 818 r = RESUME_GUEST; 819 } else { 820 /* Guest syscalls */ 821 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_SYSCALL); 822 } 823 kvmppc_account_exit(vcpu, SYSCALL_EXITS); 824 r = RESUME_GUEST; 825 break; 826 #endif 827 828 case BOOKE_INTERRUPT_DTLB_MISS: { 829 unsigned long eaddr = vcpu->arch.fault_dear; 830 int gtlb_index; 831 gpa_t gpaddr; 832 gfn_t gfn; 833 834 #ifdef CONFIG_KVM_E500V2 835 if (!(vcpu->arch.shared->msr & MSR_PR) && 836 (eaddr & PAGE_MASK) == vcpu->arch.magic_page_ea) { 837 kvmppc_map_magic(vcpu); 838 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS); 839 r = RESUME_GUEST; 840 841 break; 842 } 843 #endif 844 845 /* Check the guest TLB. */ 846 gtlb_index = kvmppc_mmu_dtlb_index(vcpu, eaddr); 847 if (gtlb_index < 0) { 848 /* The guest didn't have a mapping for it. */ 849 kvmppc_core_queue_dtlb_miss(vcpu, 850 vcpu->arch.fault_dear, 851 vcpu->arch.fault_esr); 852 kvmppc_mmu_dtlb_miss(vcpu); 853 kvmppc_account_exit(vcpu, DTLB_REAL_MISS_EXITS); 854 r = RESUME_GUEST; 855 break; 856 } 857 858 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr); 859 gfn = gpaddr >> PAGE_SHIFT; 860 861 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { 862 /* The guest TLB had a mapping, but the shadow TLB 863 * didn't, and it is RAM. This could be because: 864 * a) the entry is mapping the host kernel, or 865 * b) the guest used a large mapping which we're faking 866 * Either way, we need to satisfy the fault without 867 * invoking the guest. */ 868 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); 869 kvmppc_account_exit(vcpu, DTLB_VIRT_MISS_EXITS); 870 r = RESUME_GUEST; 871 } else { 872 /* Guest has mapped and accessed a page which is not 873 * actually RAM. */ 874 vcpu->arch.paddr_accessed = gpaddr; 875 vcpu->arch.vaddr_accessed = eaddr; 876 r = kvmppc_emulate_mmio(run, vcpu); 877 kvmppc_account_exit(vcpu, MMIO_EXITS); 878 } 879 880 break; 881 } 882 883 case BOOKE_INTERRUPT_ITLB_MISS: { 884 unsigned long eaddr = vcpu->arch.pc; 885 gpa_t gpaddr; 886 gfn_t gfn; 887 int gtlb_index; 888 889 r = RESUME_GUEST; 890 891 /* Check the guest TLB. */ 892 gtlb_index = kvmppc_mmu_itlb_index(vcpu, eaddr); 893 if (gtlb_index < 0) { 894 /* The guest didn't have a mapping for it. */ 895 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_ITLB_MISS); 896 kvmppc_mmu_itlb_miss(vcpu); 897 kvmppc_account_exit(vcpu, ITLB_REAL_MISS_EXITS); 898 break; 899 } 900 901 kvmppc_account_exit(vcpu, ITLB_VIRT_MISS_EXITS); 902 903 gpaddr = kvmppc_mmu_xlate(vcpu, gtlb_index, eaddr); 904 gfn = gpaddr >> PAGE_SHIFT; 905 906 if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { 907 /* The guest TLB had a mapping, but the shadow TLB 908 * didn't. This could be because: 909 * a) the entry is mapping the host kernel, or 910 * b) the guest used a large mapping which we're faking 911 * Either way, we need to satisfy the fault without 912 * invoking the guest. */ 913 kvmppc_mmu_map(vcpu, eaddr, gpaddr, gtlb_index); 914 } else { 915 /* Guest mapped and leaped at non-RAM! */ 916 kvmppc_booke_queue_irqprio(vcpu, BOOKE_IRQPRIO_MACHINE_CHECK); 917 } 918 919 break; 920 } 921 922 case BOOKE_INTERRUPT_DEBUG: { 923 u32 dbsr; 924 925 vcpu->arch.pc = mfspr(SPRN_CSRR0); 926 927 /* clear IAC events in DBSR register */ 928 dbsr = mfspr(SPRN_DBSR); 929 dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4; 930 mtspr(SPRN_DBSR, dbsr); 931 932 run->exit_reason = KVM_EXIT_DEBUG; 933 kvmppc_account_exit(vcpu, DEBUG_EXITS); 934 r = RESUME_HOST; 935 break; 936 } 937 938 default: 939 printk(KERN_EMERG "exit_nr %d\n", exit_nr); 940 BUG(); 941 } 942 943 /* 944 * To avoid clobbering exit_reason, only check for signals if we 945 * aren't already exiting to userspace for some other reason. 946 */ 947 if (!(r & RESUME_HOST)) { 948 local_irq_disable(); 949 if (kvmppc_prepare_to_enter(vcpu)) { 950 run->exit_reason = KVM_EXIT_INTR; 951 r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV); 952 kvmppc_account_exit(vcpu, SIGNAL_EXITS); 953 } 954 } 955 956 kvm_guest_enter(); 957 958 return r; 959 } 960 961 /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */ 962 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) 963 { 964 int i; 965 int r; 966 967 vcpu->arch.pc = 0; 968 vcpu->arch.shared->pir = vcpu->vcpu_id; 969 kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */ 970 kvmppc_set_msr(vcpu, 0); 971 972 #ifndef CONFIG_KVM_BOOKE_HV 973 vcpu->arch.shadow_msr = MSR_USER | MSR_DE | MSR_IS | MSR_DS; 974 vcpu->arch.shadow_pid = 1; 975 vcpu->arch.shared->msr = 0; 976 #endif 977 978 /* Eye-catching numbers so we know if the guest takes an interrupt 979 * before it's programmed its own IVPR/IVORs. */ 980 vcpu->arch.ivpr = 0x55550000; 981 for (i = 0; i < BOOKE_IRQPRIO_MAX; i++) 982 vcpu->arch.ivor[i] = 0x7700 | i * 4; 983 984 kvmppc_init_timing_stats(vcpu); 985 986 r = kvmppc_core_vcpu_setup(vcpu); 987 kvmppc_sanity_check(vcpu); 988 return r; 989 } 990 991 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 992 { 993 int i; 994 995 regs->pc = vcpu->arch.pc; 996 regs->cr = kvmppc_get_cr(vcpu); 997 regs->ctr = vcpu->arch.ctr; 998 regs->lr = vcpu->arch.lr; 999 regs->xer = kvmppc_get_xer(vcpu); 1000 regs->msr = vcpu->arch.shared->msr; 1001 regs->srr0 = vcpu->arch.shared->srr0; 1002 regs->srr1 = vcpu->arch.shared->srr1; 1003 regs->pid = vcpu->arch.pid; 1004 regs->sprg0 = vcpu->arch.shared->sprg0; 1005 regs->sprg1 = vcpu->arch.shared->sprg1; 1006 regs->sprg2 = vcpu->arch.shared->sprg2; 1007 regs->sprg3 = vcpu->arch.shared->sprg3; 1008 regs->sprg4 = vcpu->arch.shared->sprg4; 1009 regs->sprg5 = vcpu->arch.shared->sprg5; 1010 regs->sprg6 = vcpu->arch.shared->sprg6; 1011 regs->sprg7 = vcpu->arch.shared->sprg7; 1012 1013 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) 1014 regs->gpr[i] = kvmppc_get_gpr(vcpu, i); 1015 1016 return 0; 1017 } 1018 1019 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) 1020 { 1021 int i; 1022 1023 vcpu->arch.pc = regs->pc; 1024 kvmppc_set_cr(vcpu, regs->cr); 1025 vcpu->arch.ctr = regs->ctr; 1026 vcpu->arch.lr = regs->lr; 1027 kvmppc_set_xer(vcpu, regs->xer); 1028 kvmppc_set_msr(vcpu, regs->msr); 1029 vcpu->arch.shared->srr0 = regs->srr0; 1030 vcpu->arch.shared->srr1 = regs->srr1; 1031 kvmppc_set_pid(vcpu, regs->pid); 1032 vcpu->arch.shared->sprg0 = regs->sprg0; 1033 vcpu->arch.shared->sprg1 = regs->sprg1; 1034 vcpu->arch.shared->sprg2 = regs->sprg2; 1035 vcpu->arch.shared->sprg3 = regs->sprg3; 1036 vcpu->arch.shared->sprg4 = regs->sprg4; 1037 vcpu->arch.shared->sprg5 = regs->sprg5; 1038 vcpu->arch.shared->sprg6 = regs->sprg6; 1039 vcpu->arch.shared->sprg7 = regs->sprg7; 1040 1041 for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) 1042 kvmppc_set_gpr(vcpu, i, regs->gpr[i]); 1043 1044 return 0; 1045 } 1046 1047 static void get_sregs_base(struct kvm_vcpu *vcpu, 1048 struct kvm_sregs *sregs) 1049 { 1050 u64 tb = get_tb(); 1051 1052 sregs->u.e.features |= KVM_SREGS_E_BASE; 1053 1054 sregs->u.e.csrr0 = vcpu->arch.csrr0; 1055 sregs->u.e.csrr1 = vcpu->arch.csrr1; 1056 sregs->u.e.mcsr = vcpu->arch.mcsr; 1057 sregs->u.e.esr = get_guest_esr(vcpu); 1058 sregs->u.e.dear = get_guest_dear(vcpu); 1059 sregs->u.e.tsr = vcpu->arch.tsr; 1060 sregs->u.e.tcr = vcpu->arch.tcr; 1061 sregs->u.e.dec = kvmppc_get_dec(vcpu, tb); 1062 sregs->u.e.tb = tb; 1063 sregs->u.e.vrsave = vcpu->arch.vrsave; 1064 } 1065 1066 static int set_sregs_base(struct kvm_vcpu *vcpu, 1067 struct kvm_sregs *sregs) 1068 { 1069 if (!(sregs->u.e.features & KVM_SREGS_E_BASE)) 1070 return 0; 1071 1072 vcpu->arch.csrr0 = sregs->u.e.csrr0; 1073 vcpu->arch.csrr1 = sregs->u.e.csrr1; 1074 vcpu->arch.mcsr = sregs->u.e.mcsr; 1075 set_guest_esr(vcpu, sregs->u.e.esr); 1076 set_guest_dear(vcpu, sregs->u.e.dear); 1077 vcpu->arch.vrsave = sregs->u.e.vrsave; 1078 kvmppc_set_tcr(vcpu, sregs->u.e.tcr); 1079 1080 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_DEC) { 1081 vcpu->arch.dec = sregs->u.e.dec; 1082 kvmppc_emulate_dec(vcpu); 1083 } 1084 1085 if (sregs->u.e.update_special & KVM_SREGS_E_UPDATE_TSR) { 1086 vcpu->arch.tsr = sregs->u.e.tsr; 1087 update_timer_ints(vcpu); 1088 } 1089 1090 return 0; 1091 } 1092 1093 static void get_sregs_arch206(struct kvm_vcpu *vcpu, 1094 struct kvm_sregs *sregs) 1095 { 1096 sregs->u.e.features |= KVM_SREGS_E_ARCH206; 1097 1098 sregs->u.e.pir = vcpu->vcpu_id; 1099 sregs->u.e.mcsrr0 = vcpu->arch.mcsrr0; 1100 sregs->u.e.mcsrr1 = vcpu->arch.mcsrr1; 1101 sregs->u.e.decar = vcpu->arch.decar; 1102 sregs->u.e.ivpr = vcpu->arch.ivpr; 1103 } 1104 1105 static int set_sregs_arch206(struct kvm_vcpu *vcpu, 1106 struct kvm_sregs *sregs) 1107 { 1108 if (!(sregs->u.e.features & KVM_SREGS_E_ARCH206)) 1109 return 0; 1110 1111 if (sregs->u.e.pir != vcpu->vcpu_id) 1112 return -EINVAL; 1113 1114 vcpu->arch.mcsrr0 = sregs->u.e.mcsrr0; 1115 vcpu->arch.mcsrr1 = sregs->u.e.mcsrr1; 1116 vcpu->arch.decar = sregs->u.e.decar; 1117 vcpu->arch.ivpr = sregs->u.e.ivpr; 1118 1119 return 0; 1120 } 1121 1122 void kvmppc_get_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 1123 { 1124 sregs->u.e.features |= KVM_SREGS_E_IVOR; 1125 1126 sregs->u.e.ivor_low[0] = vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL]; 1127 sregs->u.e.ivor_low[1] = vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK]; 1128 sregs->u.e.ivor_low[2] = vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE]; 1129 sregs->u.e.ivor_low[3] = vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE]; 1130 sregs->u.e.ivor_low[4] = vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL]; 1131 sregs->u.e.ivor_low[5] = vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT]; 1132 sregs->u.e.ivor_low[6] = vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM]; 1133 sregs->u.e.ivor_low[7] = vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL]; 1134 sregs->u.e.ivor_low[8] = vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL]; 1135 sregs->u.e.ivor_low[9] = vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL]; 1136 sregs->u.e.ivor_low[10] = vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER]; 1137 sregs->u.e.ivor_low[11] = vcpu->arch.ivor[BOOKE_IRQPRIO_FIT]; 1138 sregs->u.e.ivor_low[12] = vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG]; 1139 sregs->u.e.ivor_low[13] = vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS]; 1140 sregs->u.e.ivor_low[14] = vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS]; 1141 sregs->u.e.ivor_low[15] = vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG]; 1142 } 1143 1144 int kvmppc_set_sregs_ivor(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) 1145 { 1146 if (!(sregs->u.e.features & KVM_SREGS_E_IVOR)) 1147 return 0; 1148 1149 vcpu->arch.ivor[BOOKE_IRQPRIO_CRITICAL] = sregs->u.e.ivor_low[0]; 1150 vcpu->arch.ivor[BOOKE_IRQPRIO_MACHINE_CHECK] = sregs->u.e.ivor_low[1]; 1151 vcpu->arch.ivor[BOOKE_IRQPRIO_DATA_STORAGE] = sregs->u.e.ivor_low[2]; 1152 vcpu->arch.ivor[BOOKE_IRQPRIO_INST_STORAGE] = sregs->u.e.ivor_low[3]; 1153 vcpu->arch.ivor[BOOKE_IRQPRIO_EXTERNAL] = sregs->u.e.ivor_low[4]; 1154 vcpu->arch.ivor[BOOKE_IRQPRIO_ALIGNMENT] = sregs->u.e.ivor_low[5]; 1155 vcpu->arch.ivor[BOOKE_IRQPRIO_PROGRAM] = sregs->u.e.ivor_low[6]; 1156 vcpu->arch.ivor[BOOKE_IRQPRIO_FP_UNAVAIL] = sregs->u.e.ivor_low[7]; 1157 vcpu->arch.ivor[BOOKE_IRQPRIO_SYSCALL] = sregs->u.e.ivor_low[8]; 1158 vcpu->arch.ivor[BOOKE_IRQPRIO_AP_UNAVAIL] = sregs->u.e.ivor_low[9]; 1159 vcpu->arch.ivor[BOOKE_IRQPRIO_DECREMENTER] = sregs->u.e.ivor_low[10]; 1160 vcpu->arch.ivor[BOOKE_IRQPRIO_FIT] = sregs->u.e.ivor_low[11]; 1161 vcpu->arch.ivor[BOOKE_IRQPRIO_WATCHDOG] = sregs->u.e.ivor_low[12]; 1162 vcpu->arch.ivor[BOOKE_IRQPRIO_DTLB_MISS] = sregs->u.e.ivor_low[13]; 1163 vcpu->arch.ivor[BOOKE_IRQPRIO_ITLB_MISS] = sregs->u.e.ivor_low[14]; 1164 vcpu->arch.ivor[BOOKE_IRQPRIO_DEBUG] = sregs->u.e.ivor_low[15]; 1165 1166 return 0; 1167 } 1168 1169 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, 1170 struct kvm_sregs *sregs) 1171 { 1172 sregs->pvr = vcpu->arch.pvr; 1173 1174 get_sregs_base(vcpu, sregs); 1175 get_sregs_arch206(vcpu, sregs); 1176 kvmppc_core_get_sregs(vcpu, sregs); 1177 return 0; 1178 } 1179 1180 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, 1181 struct kvm_sregs *sregs) 1182 { 1183 int ret; 1184 1185 if (vcpu->arch.pvr != sregs->pvr) 1186 return -EINVAL; 1187 1188 ret = set_sregs_base(vcpu, sregs); 1189 if (ret < 0) 1190 return ret; 1191 1192 ret = set_sregs_arch206(vcpu, sregs); 1193 if (ret < 0) 1194 return ret; 1195 1196 return kvmppc_core_set_sregs(vcpu, sregs); 1197 } 1198 1199 int kvm_vcpu_ioctl_get_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) 1200 { 1201 return -EINVAL; 1202 } 1203 1204 int kvm_vcpu_ioctl_set_one_reg(struct kvm_vcpu *vcpu, struct kvm_one_reg *reg) 1205 { 1206 return -EINVAL; 1207 } 1208 1209 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1210 { 1211 return -ENOTSUPP; 1212 } 1213 1214 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) 1215 { 1216 return -ENOTSUPP; 1217 } 1218 1219 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, 1220 struct kvm_translation *tr) 1221 { 1222 int r; 1223 1224 r = kvmppc_core_vcpu_translate(vcpu, tr); 1225 return r; 1226 } 1227 1228 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) 1229 { 1230 return -ENOTSUPP; 1231 } 1232 1233 int kvmppc_core_prepare_memory_region(struct kvm *kvm, 1234 struct kvm_userspace_memory_region *mem) 1235 { 1236 return 0; 1237 } 1238 1239 void kvmppc_core_commit_memory_region(struct kvm *kvm, 1240 struct kvm_userspace_memory_region *mem) 1241 { 1242 } 1243 1244 void kvmppc_set_tcr(struct kvm_vcpu *vcpu, u32 new_tcr) 1245 { 1246 vcpu->arch.tcr = new_tcr; 1247 update_timer_ints(vcpu); 1248 } 1249 1250 void kvmppc_set_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits) 1251 { 1252 set_bits(tsr_bits, &vcpu->arch.tsr); 1253 smp_wmb(); 1254 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); 1255 kvm_vcpu_kick(vcpu); 1256 } 1257 1258 void kvmppc_clr_tsr_bits(struct kvm_vcpu *vcpu, u32 tsr_bits) 1259 { 1260 clear_bits(tsr_bits, &vcpu->arch.tsr); 1261 update_timer_ints(vcpu); 1262 } 1263 1264 void kvmppc_decrementer_func(unsigned long data) 1265 { 1266 struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; 1267 1268 if (vcpu->arch.tcr & TCR_ARE) { 1269 vcpu->arch.dec = vcpu->arch.decar; 1270 kvmppc_emulate_dec(vcpu); 1271 } 1272 1273 kvmppc_set_tsr_bits(vcpu, TSR_DIS); 1274 } 1275 1276 void kvmppc_booke_vcpu_load(struct kvm_vcpu *vcpu, int cpu) 1277 { 1278 current->thread.kvm_vcpu = vcpu; 1279 } 1280 1281 void kvmppc_booke_vcpu_put(struct kvm_vcpu *vcpu) 1282 { 1283 current->thread.kvm_vcpu = NULL; 1284 } 1285 1286 int __init kvmppc_booke_init(void) 1287 { 1288 #ifndef CONFIG_KVM_BOOKE_HV 1289 unsigned long ivor[16]; 1290 unsigned long max_ivor = 0; 1291 int i; 1292 1293 /* We install our own exception handlers by hijacking IVPR. IVPR must 1294 * be 16-bit aligned, so we need a 64KB allocation. */ 1295 kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO, 1296 VCPU_SIZE_ORDER); 1297 if (!kvmppc_booke_handlers) 1298 return -ENOMEM; 1299 1300 /* XXX make sure our handlers are smaller than Linux's */ 1301 1302 /* Copy our interrupt handlers to match host IVORs. That way we don't 1303 * have to swap the IVORs on every guest/host transition. */ 1304 ivor[0] = mfspr(SPRN_IVOR0); 1305 ivor[1] = mfspr(SPRN_IVOR1); 1306 ivor[2] = mfspr(SPRN_IVOR2); 1307 ivor[3] = mfspr(SPRN_IVOR3); 1308 ivor[4] = mfspr(SPRN_IVOR4); 1309 ivor[5] = mfspr(SPRN_IVOR5); 1310 ivor[6] = mfspr(SPRN_IVOR6); 1311 ivor[7] = mfspr(SPRN_IVOR7); 1312 ivor[8] = mfspr(SPRN_IVOR8); 1313 ivor[9] = mfspr(SPRN_IVOR9); 1314 ivor[10] = mfspr(SPRN_IVOR10); 1315 ivor[11] = mfspr(SPRN_IVOR11); 1316 ivor[12] = mfspr(SPRN_IVOR12); 1317 ivor[13] = mfspr(SPRN_IVOR13); 1318 ivor[14] = mfspr(SPRN_IVOR14); 1319 ivor[15] = mfspr(SPRN_IVOR15); 1320 1321 for (i = 0; i < 16; i++) { 1322 if (ivor[i] > max_ivor) 1323 max_ivor = ivor[i]; 1324 1325 memcpy((void *)kvmppc_booke_handlers + ivor[i], 1326 kvmppc_handlers_start + i * kvmppc_handler_len, 1327 kvmppc_handler_len); 1328 } 1329 flush_icache_range(kvmppc_booke_handlers, 1330 kvmppc_booke_handlers + max_ivor + kvmppc_handler_len); 1331 #endif /* !BOOKE_HV */ 1332 return 0; 1333 } 1334 1335 void __exit kvmppc_booke_exit(void) 1336 { 1337 free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER); 1338 kvm_exit(); 1339 } 1340