Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 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 | * |
| 17 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> |
| 18 | * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> |
| 19 | */ |
| 20 | |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/err.h> |
| 23 | #include <linux/kvm_host.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/vmalloc.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <asm/cputable.h> |
| 28 | #include <asm/uaccess.h> |
| 29 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 30 | #include <asm/cacheflush.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 31 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 32 | #include "booke.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 33 | #include "44x_tlb.h" |
| 34 | |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 35 | unsigned long kvmppc_booke_handlers; |
| 36 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 37 | #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM |
| 38 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
| 39 | |
| 40 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
| 41 | { "exits", VCPU_STAT(sum_exits) }, |
| 42 | { "mmio", VCPU_STAT(mmio_exits) }, |
| 43 | { "dcr", VCPU_STAT(dcr_exits) }, |
| 44 | { "sig", VCPU_STAT(signal_exits) }, |
| 45 | { "light", VCPU_STAT(light_exits) }, |
| 46 | { "itlb_r", VCPU_STAT(itlb_real_miss_exits) }, |
| 47 | { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) }, |
| 48 | { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) }, |
| 49 | { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) }, |
| 50 | { "sysc", VCPU_STAT(syscall_exits) }, |
| 51 | { "isi", VCPU_STAT(isi_exits) }, |
| 52 | { "dsi", VCPU_STAT(dsi_exits) }, |
| 53 | { "inst_emu", VCPU_STAT(emulated_inst_exits) }, |
| 54 | { "dec", VCPU_STAT(dec_exits) }, |
| 55 | { "ext_intr", VCPU_STAT(ext_intr_exits) }, |
Hollis Blanchard | 45c5eb6 | 2008-04-25 17:55:49 -0500 | [diff] [blame] | 56 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 57 | { NULL } |
| 58 | }; |
| 59 | |
| 60 | static const u32 interrupt_msr_mask[16] = { |
| 61 | [BOOKE_INTERRUPT_CRITICAL] = MSR_ME, |
| 62 | [BOOKE_INTERRUPT_MACHINE_CHECK] = 0, |
| 63 | [BOOKE_INTERRUPT_DATA_STORAGE] = MSR_CE|MSR_ME|MSR_DE, |
| 64 | [BOOKE_INTERRUPT_INST_STORAGE] = MSR_CE|MSR_ME|MSR_DE, |
| 65 | [BOOKE_INTERRUPT_EXTERNAL] = MSR_CE|MSR_ME|MSR_DE, |
| 66 | [BOOKE_INTERRUPT_ALIGNMENT] = MSR_CE|MSR_ME|MSR_DE, |
| 67 | [BOOKE_INTERRUPT_PROGRAM] = MSR_CE|MSR_ME|MSR_DE, |
| 68 | [BOOKE_INTERRUPT_FP_UNAVAIL] = MSR_CE|MSR_ME|MSR_DE, |
| 69 | [BOOKE_INTERRUPT_SYSCALL] = MSR_CE|MSR_ME|MSR_DE, |
| 70 | [BOOKE_INTERRUPT_AP_UNAVAIL] = MSR_CE|MSR_ME|MSR_DE, |
| 71 | [BOOKE_INTERRUPT_DECREMENTER] = MSR_CE|MSR_ME|MSR_DE, |
| 72 | [BOOKE_INTERRUPT_FIT] = MSR_CE|MSR_ME|MSR_DE, |
| 73 | [BOOKE_INTERRUPT_WATCHDOG] = MSR_ME, |
| 74 | [BOOKE_INTERRUPT_DTLB_MISS] = MSR_CE|MSR_ME|MSR_DE, |
| 75 | [BOOKE_INTERRUPT_ITLB_MISS] = MSR_CE|MSR_ME|MSR_DE, |
| 76 | [BOOKE_INTERRUPT_DEBUG] = MSR_ME, |
| 77 | }; |
| 78 | |
| 79 | const unsigned char exception_priority[] = { |
| 80 | [BOOKE_INTERRUPT_DATA_STORAGE] = 0, |
| 81 | [BOOKE_INTERRUPT_INST_STORAGE] = 1, |
| 82 | [BOOKE_INTERRUPT_ALIGNMENT] = 2, |
| 83 | [BOOKE_INTERRUPT_PROGRAM] = 3, |
| 84 | [BOOKE_INTERRUPT_FP_UNAVAIL] = 4, |
| 85 | [BOOKE_INTERRUPT_SYSCALL] = 5, |
| 86 | [BOOKE_INTERRUPT_AP_UNAVAIL] = 6, |
| 87 | [BOOKE_INTERRUPT_DTLB_MISS] = 7, |
| 88 | [BOOKE_INTERRUPT_ITLB_MISS] = 8, |
| 89 | [BOOKE_INTERRUPT_MACHINE_CHECK] = 9, |
| 90 | [BOOKE_INTERRUPT_DEBUG] = 10, |
| 91 | [BOOKE_INTERRUPT_CRITICAL] = 11, |
| 92 | [BOOKE_INTERRUPT_WATCHDOG] = 12, |
| 93 | [BOOKE_INTERRUPT_EXTERNAL] = 13, |
| 94 | [BOOKE_INTERRUPT_FIT] = 14, |
| 95 | [BOOKE_INTERRUPT_DECREMENTER] = 15, |
| 96 | }; |
| 97 | |
| 98 | const unsigned char priority_exception[] = { |
| 99 | BOOKE_INTERRUPT_DATA_STORAGE, |
| 100 | BOOKE_INTERRUPT_INST_STORAGE, |
| 101 | BOOKE_INTERRUPT_ALIGNMENT, |
| 102 | BOOKE_INTERRUPT_PROGRAM, |
| 103 | BOOKE_INTERRUPT_FP_UNAVAIL, |
| 104 | BOOKE_INTERRUPT_SYSCALL, |
| 105 | BOOKE_INTERRUPT_AP_UNAVAIL, |
| 106 | BOOKE_INTERRUPT_DTLB_MISS, |
| 107 | BOOKE_INTERRUPT_ITLB_MISS, |
| 108 | BOOKE_INTERRUPT_MACHINE_CHECK, |
| 109 | BOOKE_INTERRUPT_DEBUG, |
| 110 | BOOKE_INTERRUPT_CRITICAL, |
| 111 | BOOKE_INTERRUPT_WATCHDOG, |
| 112 | BOOKE_INTERRUPT_EXTERNAL, |
| 113 | BOOKE_INTERRUPT_FIT, |
| 114 | BOOKE_INTERRUPT_DECREMENTER, |
| 115 | }; |
| 116 | |
| 117 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 118 | /* TODO: use vcpu_printf() */ |
| 119 | void kvmppc_dump_vcpu(struct kvm_vcpu *vcpu) |
| 120 | { |
| 121 | int i; |
| 122 | |
| 123 | printk("pc: %08x msr: %08x\n", vcpu->arch.pc, vcpu->arch.msr); |
| 124 | printk("lr: %08x ctr: %08x\n", vcpu->arch.lr, vcpu->arch.ctr); |
| 125 | printk("srr0: %08x srr1: %08x\n", vcpu->arch.srr0, vcpu->arch.srr1); |
| 126 | |
| 127 | printk("exceptions: %08lx\n", vcpu->arch.pending_exceptions); |
| 128 | |
| 129 | for (i = 0; i < 32; i += 4) { |
| 130 | printk("gpr%02d: %08x %08x %08x %08x\n", i, |
| 131 | vcpu->arch.gpr[i], |
| 132 | vcpu->arch.gpr[i+1], |
| 133 | vcpu->arch.gpr[i+2], |
| 134 | vcpu->arch.gpr[i+3]); |
| 135 | } |
| 136 | } |
| 137 | |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 138 | static void kvmppc_booke_queue_exception(struct kvm_vcpu *vcpu, int exception) |
| 139 | { |
| 140 | unsigned int priority = exception_priority[exception]; |
| 141 | set_bit(priority, &vcpu->arch.pending_exceptions); |
| 142 | } |
| 143 | |
| 144 | static void kvmppc_booke_clear_exception(struct kvm_vcpu *vcpu, int exception) |
| 145 | { |
| 146 | unsigned int priority = exception_priority[exception]; |
| 147 | clear_bit(priority, &vcpu->arch.pending_exceptions); |
| 148 | } |
| 149 | |
| 150 | void kvmppc_core_queue_program(struct kvm_vcpu *vcpu) |
| 151 | { |
| 152 | kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM); |
| 153 | } |
| 154 | |
| 155 | void kvmppc_core_queue_dec(struct kvm_vcpu *vcpu) |
| 156 | { |
| 157 | kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_DECREMENTER); |
| 158 | } |
| 159 | |
| 160 | int kvmppc_core_pending_dec(struct kvm_vcpu *vcpu) |
| 161 | { |
| 162 | unsigned int priority = exception_priority[BOOKE_INTERRUPT_DECREMENTER]; |
| 163 | return test_bit(priority, &vcpu->arch.pending_exceptions); |
| 164 | } |
| 165 | |
| 166 | void kvmppc_core_queue_external(struct kvm_vcpu *vcpu, |
| 167 | struct kvm_interrupt *irq) |
| 168 | { |
| 169 | kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_EXTERNAL); |
| 170 | } |
| 171 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 172 | /* Check if we are ready to deliver the interrupt */ |
| 173 | static int kvmppc_can_deliver_interrupt(struct kvm_vcpu *vcpu, int interrupt) |
| 174 | { |
| 175 | int r; |
| 176 | |
| 177 | switch (interrupt) { |
| 178 | case BOOKE_INTERRUPT_CRITICAL: |
| 179 | r = vcpu->arch.msr & MSR_CE; |
| 180 | break; |
| 181 | case BOOKE_INTERRUPT_MACHINE_CHECK: |
| 182 | r = vcpu->arch.msr & MSR_ME; |
| 183 | break; |
| 184 | case BOOKE_INTERRUPT_EXTERNAL: |
| 185 | r = vcpu->arch.msr & MSR_EE; |
| 186 | break; |
| 187 | case BOOKE_INTERRUPT_DECREMENTER: |
| 188 | r = vcpu->arch.msr & MSR_EE; |
| 189 | break; |
| 190 | case BOOKE_INTERRUPT_FIT: |
| 191 | r = vcpu->arch.msr & MSR_EE; |
| 192 | break; |
| 193 | case BOOKE_INTERRUPT_WATCHDOG: |
| 194 | r = vcpu->arch.msr & MSR_CE; |
| 195 | break; |
| 196 | case BOOKE_INTERRUPT_DEBUG: |
| 197 | r = vcpu->arch.msr & MSR_DE; |
| 198 | break; |
| 199 | default: |
| 200 | r = 1; |
| 201 | } |
| 202 | |
| 203 | return r; |
| 204 | } |
| 205 | |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 206 | static void kvmppc_booke_deliver_interrupt(struct kvm_vcpu *vcpu, int interrupt) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 207 | { |
| 208 | switch (interrupt) { |
| 209 | case BOOKE_INTERRUPT_DECREMENTER: |
| 210 | vcpu->arch.tsr |= TSR_DIS; |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | vcpu->arch.srr0 = vcpu->arch.pc; |
| 215 | vcpu->arch.srr1 = vcpu->arch.msr; |
| 216 | vcpu->arch.pc = vcpu->arch.ivpr | vcpu->arch.ivor[interrupt]; |
| 217 | kvmppc_set_msr(vcpu, vcpu->arch.msr & interrupt_msr_mask[interrupt]); |
| 218 | } |
| 219 | |
| 220 | /* Check pending exceptions and deliver one, if possible. */ |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 221 | void kvmppc_core_deliver_interrupts(struct kvm_vcpu *vcpu) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 222 | { |
| 223 | unsigned long *pending = &vcpu->arch.pending_exceptions; |
| 224 | unsigned int exception; |
| 225 | unsigned int priority; |
| 226 | |
| 227 | priority = find_first_bit(pending, BITS_PER_BYTE * sizeof(*pending)); |
| 228 | while (priority <= BOOKE_MAX_INTERRUPT) { |
| 229 | exception = priority_exception[priority]; |
| 230 | if (kvmppc_can_deliver_interrupt(vcpu, exception)) { |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 231 | kvmppc_booke_clear_exception(vcpu, exception); |
| 232 | kvmppc_booke_deliver_interrupt(vcpu, exception); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | |
| 236 | priority = find_next_bit(pending, |
| 237 | BITS_PER_BYTE * sizeof(*pending), |
| 238 | priority + 1); |
| 239 | } |
| 240 | } |
| 241 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 242 | /** |
| 243 | * kvmppc_handle_exit |
| 244 | * |
| 245 | * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) |
| 246 | */ |
| 247 | int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 248 | unsigned int exit_nr) |
| 249 | { |
| 250 | enum emulation_result er; |
| 251 | int r = RESUME_HOST; |
| 252 | |
| 253 | local_irq_enable(); |
| 254 | |
| 255 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 256 | run->ready_for_interrupt_injection = 1; |
| 257 | |
| 258 | switch (exit_nr) { |
| 259 | case BOOKE_INTERRUPT_MACHINE_CHECK: |
| 260 | printk("MACHINE CHECK: %lx\n", mfspr(SPRN_MCSR)); |
| 261 | kvmppc_dump_vcpu(vcpu); |
| 262 | r = RESUME_HOST; |
| 263 | break; |
| 264 | |
| 265 | case BOOKE_INTERRUPT_EXTERNAL: |
| 266 | case BOOKE_INTERRUPT_DECREMENTER: |
| 267 | /* Since we switched IVPR back to the host's value, the host |
| 268 | * handled this interrupt the moment we enabled interrupts. |
| 269 | * Now we just offer it a chance to reschedule the guest. */ |
| 270 | |
| 271 | /* XXX At this point the TLB still holds our shadow TLB, so if |
| 272 | * we do reschedule the host will fault over it. Perhaps we |
| 273 | * should politely restore the host's entries to minimize |
| 274 | * misses before ceding control. */ |
| 275 | if (need_resched()) |
| 276 | cond_resched(); |
| 277 | if (exit_nr == BOOKE_INTERRUPT_DECREMENTER) |
| 278 | vcpu->stat.dec_exits++; |
| 279 | else |
| 280 | vcpu->stat.ext_intr_exits++; |
| 281 | r = RESUME_GUEST; |
| 282 | break; |
| 283 | |
| 284 | case BOOKE_INTERRUPT_PROGRAM: |
| 285 | if (vcpu->arch.msr & MSR_PR) { |
| 286 | /* Program traps generated by user-level software must be handled |
| 287 | * by the guest kernel. */ |
| 288 | vcpu->arch.esr = vcpu->arch.fault_esr; |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 289 | kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_PROGRAM); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 290 | r = RESUME_GUEST; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | er = kvmppc_emulate_instruction(run, vcpu); |
| 295 | switch (er) { |
| 296 | case EMULATE_DONE: |
| 297 | /* Future optimization: only reload non-volatiles if |
| 298 | * they were actually modified by emulation. */ |
| 299 | vcpu->stat.emulated_inst_exits++; |
| 300 | r = RESUME_GUEST_NV; |
| 301 | break; |
| 302 | case EMULATE_DO_DCR: |
| 303 | run->exit_reason = KVM_EXIT_DCR; |
| 304 | r = RESUME_HOST; |
| 305 | break; |
| 306 | case EMULATE_FAIL: |
| 307 | /* XXX Deliver Program interrupt to guest. */ |
| 308 | printk(KERN_CRIT "%s: emulation at %x failed (%08x)\n", |
| 309 | __func__, vcpu->arch.pc, vcpu->arch.last_inst); |
| 310 | /* For debugging, encode the failing instruction and |
| 311 | * report it to userspace. */ |
| 312 | run->hw.hardware_exit_reason = ~0ULL << 32; |
| 313 | run->hw.hardware_exit_reason |= vcpu->arch.last_inst; |
| 314 | r = RESUME_HOST; |
| 315 | break; |
| 316 | default: |
| 317 | BUG(); |
| 318 | } |
| 319 | break; |
| 320 | |
Christian Ehrhardt | de368dc | 2008-04-29 18:18:23 +0200 | [diff] [blame] | 321 | case BOOKE_INTERRUPT_FP_UNAVAIL: |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 322 | kvmppc_booke_queue_exception(vcpu, exit_nr); |
Christian Ehrhardt | de368dc | 2008-04-29 18:18:23 +0200 | [diff] [blame] | 323 | r = RESUME_GUEST; |
| 324 | break; |
| 325 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 326 | case BOOKE_INTERRUPT_DATA_STORAGE: |
| 327 | vcpu->arch.dear = vcpu->arch.fault_dear; |
| 328 | vcpu->arch.esr = vcpu->arch.fault_esr; |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 329 | kvmppc_booke_queue_exception(vcpu, exit_nr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 330 | vcpu->stat.dsi_exits++; |
| 331 | r = RESUME_GUEST; |
| 332 | break; |
| 333 | |
| 334 | case BOOKE_INTERRUPT_INST_STORAGE: |
| 335 | vcpu->arch.esr = vcpu->arch.fault_esr; |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 336 | kvmppc_booke_queue_exception(vcpu, exit_nr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 337 | vcpu->stat.isi_exits++; |
| 338 | r = RESUME_GUEST; |
| 339 | break; |
| 340 | |
| 341 | case BOOKE_INTERRUPT_SYSCALL: |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 342 | kvmppc_booke_queue_exception(vcpu, exit_nr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 343 | vcpu->stat.syscall_exits++; |
| 344 | r = RESUME_GUEST; |
| 345 | break; |
| 346 | |
| 347 | case BOOKE_INTERRUPT_DTLB_MISS: { |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 348 | struct kvmppc_44x_tlbe *gtlbe; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 349 | unsigned long eaddr = vcpu->arch.fault_dear; |
| 350 | gfn_t gfn; |
| 351 | |
| 352 | /* Check the guest TLB. */ |
| 353 | gtlbe = kvmppc_44x_dtlb_search(vcpu, eaddr); |
| 354 | if (!gtlbe) { |
| 355 | /* The guest didn't have a mapping for it. */ |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 356 | kvmppc_booke_queue_exception(vcpu, exit_nr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 357 | vcpu->arch.dear = vcpu->arch.fault_dear; |
| 358 | vcpu->arch.esr = vcpu->arch.fault_esr; |
| 359 | vcpu->stat.dtlb_real_miss_exits++; |
| 360 | r = RESUME_GUEST; |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | vcpu->arch.paddr_accessed = tlb_xlate(gtlbe, eaddr); |
| 365 | gfn = vcpu->arch.paddr_accessed >> PAGE_SHIFT; |
| 366 | |
| 367 | if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { |
| 368 | /* The guest TLB had a mapping, but the shadow TLB |
| 369 | * didn't, and it is RAM. This could be because: |
| 370 | * a) the entry is mapping the host kernel, or |
| 371 | * b) the guest used a large mapping which we're faking |
| 372 | * Either way, we need to satisfy the fault without |
| 373 | * invoking the guest. */ |
| 374 | kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid, |
| 375 | gtlbe->word2); |
| 376 | vcpu->stat.dtlb_virt_miss_exits++; |
| 377 | r = RESUME_GUEST; |
| 378 | } else { |
| 379 | /* Guest has mapped and accessed a page which is not |
| 380 | * actually RAM. */ |
| 381 | r = kvmppc_emulate_mmio(run, vcpu); |
| 382 | } |
| 383 | |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | case BOOKE_INTERRUPT_ITLB_MISS: { |
Hollis Blanchard | 0f55dc4 | 2008-11-05 09:36:12 -0600 | [diff] [blame] | 388 | struct kvmppc_44x_tlbe *gtlbe; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 389 | unsigned long eaddr = vcpu->arch.pc; |
| 390 | gfn_t gfn; |
| 391 | |
| 392 | r = RESUME_GUEST; |
| 393 | |
| 394 | /* Check the guest TLB. */ |
| 395 | gtlbe = kvmppc_44x_itlb_search(vcpu, eaddr); |
| 396 | if (!gtlbe) { |
| 397 | /* The guest didn't have a mapping for it. */ |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 398 | kvmppc_booke_queue_exception(vcpu, exit_nr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 399 | vcpu->stat.itlb_real_miss_exits++; |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | vcpu->stat.itlb_virt_miss_exits++; |
| 404 | |
| 405 | gfn = tlb_xlate(gtlbe, eaddr) >> PAGE_SHIFT; |
| 406 | |
| 407 | if (kvm_is_visible_gfn(vcpu->kvm, gfn)) { |
| 408 | /* The guest TLB had a mapping, but the shadow TLB |
| 409 | * didn't. This could be because: |
| 410 | * a) the entry is mapping the host kernel, or |
| 411 | * b) the guest used a large mapping which we're faking |
| 412 | * Either way, we need to satisfy the fault without |
| 413 | * invoking the guest. */ |
| 414 | kvmppc_mmu_map(vcpu, eaddr, gfn, gtlbe->tid, |
| 415 | gtlbe->word2); |
| 416 | } else { |
| 417 | /* Guest mapped and leaped at non-RAM! */ |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 418 | kvmppc_booke_queue_exception(vcpu, BOOKE_INTERRUPT_MACHINE_CHECK); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | break; |
| 422 | } |
| 423 | |
Hollis Blanchard | 6a0ab73 | 2008-07-25 13:54:49 -0500 | [diff] [blame] | 424 | case BOOKE_INTERRUPT_DEBUG: { |
| 425 | u32 dbsr; |
| 426 | |
| 427 | vcpu->arch.pc = mfspr(SPRN_CSRR0); |
| 428 | |
| 429 | /* clear IAC events in DBSR register */ |
| 430 | dbsr = mfspr(SPRN_DBSR); |
| 431 | dbsr &= DBSR_IAC1 | DBSR_IAC2 | DBSR_IAC3 | DBSR_IAC4; |
| 432 | mtspr(SPRN_DBSR, dbsr); |
| 433 | |
| 434 | run->exit_reason = KVM_EXIT_DEBUG; |
| 435 | r = RESUME_HOST; |
| 436 | break; |
| 437 | } |
| 438 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 439 | default: |
| 440 | printk(KERN_EMERG "exit_nr %d\n", exit_nr); |
| 441 | BUG(); |
| 442 | } |
| 443 | |
| 444 | local_irq_disable(); |
| 445 | |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 446 | kvmppc_core_deliver_interrupts(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 447 | |
| 448 | /* Do some exit accounting. */ |
| 449 | vcpu->stat.sum_exits++; |
| 450 | if (!(r & RESUME_HOST)) { |
| 451 | /* To avoid clobbering exit_reason, only check for signals if |
| 452 | * we aren't already exiting to userspace for some other |
| 453 | * reason. */ |
| 454 | if (signal_pending(current)) { |
| 455 | run->exit_reason = KVM_EXIT_INTR; |
| 456 | r = (-EINTR << 2) | RESUME_HOST | (r & RESUME_FLAG_NV); |
| 457 | |
| 458 | vcpu->stat.signal_exits++; |
| 459 | } else { |
| 460 | vcpu->stat.light_exits++; |
| 461 | } |
| 462 | } else { |
| 463 | switch (run->exit_reason) { |
| 464 | case KVM_EXIT_MMIO: |
| 465 | vcpu->stat.mmio_exits++; |
| 466 | break; |
| 467 | case KVM_EXIT_DCR: |
| 468 | vcpu->stat.dcr_exits++; |
| 469 | break; |
| 470 | case KVM_EXIT_INTR: |
| 471 | vcpu->stat.signal_exits++; |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | return r; |
| 477 | } |
| 478 | |
| 479 | /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */ |
| 480 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 481 | { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 482 | vcpu->arch.pc = 0; |
| 483 | vcpu->arch.msr = 0; |
| 484 | vcpu->arch.gpr[1] = (16<<20) - 8; /* -8 for the callee-save LR slot */ |
| 485 | |
Hollis Blanchard | 49dd2c4 | 2008-07-25 13:54:53 -0500 | [diff] [blame] | 486 | vcpu->arch.shadow_pid = 1; |
| 487 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 488 | /* Eye-catching number so we know if the guest takes an interrupt |
| 489 | * before it's programmed its own IVPR. */ |
| 490 | vcpu->arch.ivpr = 0x55550000; |
| 491 | |
Hollis Blanchard | 5cbb510 | 2008-11-05 09:36:17 -0600 | [diff] [blame^] | 492 | return kvmppc_core_vcpu_setup(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 496 | { |
| 497 | int i; |
| 498 | |
| 499 | regs->pc = vcpu->arch.pc; |
| 500 | regs->cr = vcpu->arch.cr; |
| 501 | regs->ctr = vcpu->arch.ctr; |
| 502 | regs->lr = vcpu->arch.lr; |
| 503 | regs->xer = vcpu->arch.xer; |
| 504 | regs->msr = vcpu->arch.msr; |
| 505 | regs->srr0 = vcpu->arch.srr0; |
| 506 | regs->srr1 = vcpu->arch.srr1; |
| 507 | regs->pid = vcpu->arch.pid; |
| 508 | regs->sprg0 = vcpu->arch.sprg0; |
| 509 | regs->sprg1 = vcpu->arch.sprg1; |
| 510 | regs->sprg2 = vcpu->arch.sprg2; |
| 511 | regs->sprg3 = vcpu->arch.sprg3; |
| 512 | regs->sprg5 = vcpu->arch.sprg4; |
| 513 | regs->sprg6 = vcpu->arch.sprg5; |
| 514 | regs->sprg7 = vcpu->arch.sprg6; |
| 515 | |
| 516 | for (i = 0; i < ARRAY_SIZE(regs->gpr); i++) |
| 517 | regs->gpr[i] = vcpu->arch.gpr[i]; |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 523 | { |
| 524 | int i; |
| 525 | |
| 526 | vcpu->arch.pc = regs->pc; |
| 527 | vcpu->arch.cr = regs->cr; |
| 528 | vcpu->arch.ctr = regs->ctr; |
| 529 | vcpu->arch.lr = regs->lr; |
| 530 | vcpu->arch.xer = regs->xer; |
| 531 | vcpu->arch.msr = regs->msr; |
| 532 | vcpu->arch.srr0 = regs->srr0; |
| 533 | vcpu->arch.srr1 = regs->srr1; |
| 534 | vcpu->arch.sprg0 = regs->sprg0; |
| 535 | vcpu->arch.sprg1 = regs->sprg1; |
| 536 | vcpu->arch.sprg2 = regs->sprg2; |
| 537 | vcpu->arch.sprg3 = regs->sprg3; |
| 538 | vcpu->arch.sprg5 = regs->sprg4; |
| 539 | vcpu->arch.sprg6 = regs->sprg5; |
| 540 | vcpu->arch.sprg7 = regs->sprg6; |
| 541 | |
| 542 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.gpr); i++) |
| 543 | vcpu->arch.gpr[i] = regs->gpr[i]; |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
| 549 | struct kvm_sregs *sregs) |
| 550 | { |
| 551 | return -ENOTSUPP; |
| 552 | } |
| 553 | |
| 554 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, |
| 555 | struct kvm_sregs *sregs) |
| 556 | { |
| 557 | return -ENOTSUPP; |
| 558 | } |
| 559 | |
| 560 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 561 | { |
| 562 | return -ENOTSUPP; |
| 563 | } |
| 564 | |
| 565 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 566 | { |
| 567 | return -ENOTSUPP; |
| 568 | } |
| 569 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 570 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, |
| 571 | struct kvm_translation *tr) |
| 572 | { |
Hollis Blanchard | 5cbb510 | 2008-11-05 09:36:17 -0600 | [diff] [blame^] | 573 | return kvmppc_core_vcpu_translate(vcpu, tr); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 574 | } |
Hollis Blanchard | d9fbd03 | 2008-11-05 09:36:13 -0600 | [diff] [blame] | 575 | |
| 576 | static int kvmppc_booke_init(void) |
| 577 | { |
| 578 | unsigned long ivor[16]; |
| 579 | unsigned long max_ivor = 0; |
| 580 | int i; |
| 581 | |
| 582 | /* We install our own exception handlers by hijacking IVPR. IVPR must |
| 583 | * be 16-bit aligned, so we need a 64KB allocation. */ |
| 584 | kvmppc_booke_handlers = __get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 585 | VCPU_SIZE_ORDER); |
| 586 | if (!kvmppc_booke_handlers) |
| 587 | return -ENOMEM; |
| 588 | |
| 589 | /* XXX make sure our handlers are smaller than Linux's */ |
| 590 | |
| 591 | /* Copy our interrupt handlers to match host IVORs. That way we don't |
| 592 | * have to swap the IVORs on every guest/host transition. */ |
| 593 | ivor[0] = mfspr(SPRN_IVOR0); |
| 594 | ivor[1] = mfspr(SPRN_IVOR1); |
| 595 | ivor[2] = mfspr(SPRN_IVOR2); |
| 596 | ivor[3] = mfspr(SPRN_IVOR3); |
| 597 | ivor[4] = mfspr(SPRN_IVOR4); |
| 598 | ivor[5] = mfspr(SPRN_IVOR5); |
| 599 | ivor[6] = mfspr(SPRN_IVOR6); |
| 600 | ivor[7] = mfspr(SPRN_IVOR7); |
| 601 | ivor[8] = mfspr(SPRN_IVOR8); |
| 602 | ivor[9] = mfspr(SPRN_IVOR9); |
| 603 | ivor[10] = mfspr(SPRN_IVOR10); |
| 604 | ivor[11] = mfspr(SPRN_IVOR11); |
| 605 | ivor[12] = mfspr(SPRN_IVOR12); |
| 606 | ivor[13] = mfspr(SPRN_IVOR13); |
| 607 | ivor[14] = mfspr(SPRN_IVOR14); |
| 608 | ivor[15] = mfspr(SPRN_IVOR15); |
| 609 | |
| 610 | for (i = 0; i < 16; i++) { |
| 611 | if (ivor[i] > max_ivor) |
| 612 | max_ivor = ivor[i]; |
| 613 | |
| 614 | memcpy((void *)kvmppc_booke_handlers + ivor[i], |
| 615 | kvmppc_handlers_start + i * kvmppc_handler_len, |
| 616 | kvmppc_handler_len); |
| 617 | } |
| 618 | flush_icache_range(kvmppc_booke_handlers, |
| 619 | kvmppc_booke_handlers + max_ivor + kvmppc_handler_len); |
| 620 | |
| 621 | return kvm_init(NULL, sizeof(struct kvm_vcpu), THIS_MODULE); |
| 622 | } |
| 623 | |
| 624 | static void __exit kvmppc_booke_exit(void) |
| 625 | { |
| 626 | free_pages(kvmppc_booke_handlers, VCPU_SIZE_ORDER); |
| 627 | kvm_exit(); |
| 628 | } |
| 629 | |
| 630 | module_init(kvmppc_booke_init) |
| 631 | module_exit(kvmppc_booke_exit) |