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