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