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