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 |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 16 | * Copyright 2011 Freescale Semiconductor, Inc. |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 17 | * |
| 18 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> |
| 19 | */ |
| 20 | |
| 21 | #include <linux/jiffies.h> |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 22 | #include <linux/hrtimer.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 23 | #include <linux/types.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/kvm_host.h> |
Bharat Bhushan | 6e35994 | 2012-04-18 06:01:19 +0000 | [diff] [blame] | 26 | #include <linux/clockchips.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 27 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 28 | #include <asm/reg.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 29 | #include <asm/time.h> |
| 30 | #include <asm/byteorder.h> |
| 31 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | c381a04 | 2008-11-05 09:36:15 -0600 | [diff] [blame] | 32 | #include <asm/disassemble.h> |
Hongtao Jia | 9123c5e | 2013-04-28 13:20:07 +0800 | [diff] [blame] | 33 | #include <asm/ppc-opcode.h> |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 34 | #include "timing.h" |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 35 | #include "trace.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 36 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 37 | void kvmppc_emulate_dec(struct kvm_vcpu *vcpu) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 38 | { |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 39 | unsigned long dec_nsec; |
Bharat Bhushan | dc2babfe | 2011-10-19 09:46:06 +0530 | [diff] [blame] | 40 | unsigned long long dec_time; |
Alexander Graf | 9a7a9b0 | 2009-10-30 05:47:15 +0000 | [diff] [blame] | 41 | |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 42 | pr_debug("mtDEC: %x\n", vcpu->arch.dec); |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 43 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
| 44 | |
Alexander Graf | 00c3a37 | 2010-04-16 00:11:42 +0200 | [diff] [blame] | 45 | #ifdef CONFIG_PPC_BOOK3S |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 46 | /* mtdec lowers the interrupt line when positive. */ |
| 47 | kvmppc_core_dequeue_dec(vcpu); |
| 48 | |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 49 | /* POWER4+ triggers a dec interrupt if the value is < 0 */ |
| 50 | if (vcpu->arch.dec & 0x80000000) { |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 51 | kvmppc_core_queue_dec(vcpu); |
| 52 | return; |
| 53 | } |
| 54 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 55 | |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 56 | #ifdef CONFIG_BOOKE |
| 57 | /* On BOOKE, DEC = 0 is as good as decrementer not enabled */ |
| 58 | if (vcpu->arch.dec == 0) |
| 59 | return; |
| 60 | #endif |
| 61 | |
| 62 | /* |
| 63 | * The decrementer ticks at the same rate as the timebase, so |
| 64 | * that's how we convert the guest DEC value to the number of |
| 65 | * host ticks. |
| 66 | */ |
| 67 | |
| 68 | dec_time = vcpu->arch.dec; |
Bharat Bhushan | 6e35994 | 2012-04-18 06:01:19 +0000 | [diff] [blame] | 69 | /* |
| 70 | * Guest timebase ticks at the same frequency as host decrementer. |
| 71 | * So use the host decrementer calculations for decrementer emulation. |
| 72 | */ |
| 73 | dec_time = dec_time << decrementer_clockevent.shift; |
| 74 | do_div(dec_time, decrementer_clockevent.mult); |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 75 | dec_nsec = do_div(dec_time, NSEC_PER_SEC); |
| 76 | hrtimer_start(&vcpu->arch.dec_timer, |
| 77 | ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL); |
| 78 | vcpu->arch.dec_jiffies = get_tb(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 81 | u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb) |
| 82 | { |
| 83 | u64 jd = tb - vcpu->arch.dec_jiffies; |
Scott Wood | dfd4d47 | 2011-11-17 12:39:59 +0000 | [diff] [blame] | 84 | |
| 85 | #ifdef CONFIG_BOOKE |
| 86 | if (vcpu->arch.dec < jd) |
| 87 | return 0; |
| 88 | #endif |
| 89 | |
Scott Wood | 5ce941e | 2011-04-27 17:24:21 -0500 | [diff] [blame] | 90 | return vcpu->arch.dec - jd; |
| 91 | } |
| 92 | |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 93 | static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs) |
| 94 | { |
| 95 | enum emulation_result emulated = EMULATE_DONE; |
| 96 | ulong spr_val = kvmppc_get_gpr(vcpu, rs); |
| 97 | |
| 98 | switch (sprn) { |
| 99 | case SPRN_SRR0: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 100 | kvmppc_set_srr0(vcpu, spr_val); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 101 | break; |
| 102 | case SPRN_SRR1: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 103 | kvmppc_set_srr1(vcpu, spr_val); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 104 | break; |
| 105 | |
| 106 | /* XXX We need to context-switch the timebase for |
| 107 | * watchdog and FIT. */ |
| 108 | case SPRN_TBWL: break; |
| 109 | case SPRN_TBWU: break; |
| 110 | |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 111 | case SPRN_DEC: |
| 112 | vcpu->arch.dec = spr_val; |
| 113 | kvmppc_emulate_dec(vcpu); |
| 114 | break; |
| 115 | |
| 116 | case SPRN_SPRG0: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 117 | kvmppc_set_sprg0(vcpu, spr_val); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 118 | break; |
| 119 | case SPRN_SPRG1: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 120 | kvmppc_set_sprg1(vcpu, spr_val); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 121 | break; |
| 122 | case SPRN_SPRG2: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 123 | kvmppc_set_sprg2(vcpu, spr_val); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 124 | break; |
| 125 | case SPRN_SPRG3: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 126 | kvmppc_set_sprg3(vcpu, spr_val); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 127 | break; |
| 128 | |
Alexander Graf | a3ff5fb | 2013-06-27 01:07:15 +0200 | [diff] [blame] | 129 | /* PIR can legally be written, but we ignore it */ |
| 130 | case SPRN_PIR: break; |
| 131 | |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 132 | default: |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 133 | emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn, |
| 134 | spr_val); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 135 | if (emulated == EMULATE_FAIL) |
| 136 | printk(KERN_INFO "mtspr: unknown spr " |
| 137 | "0x%x\n", sprn); |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS); |
| 142 | |
| 143 | return emulated; |
| 144 | } |
| 145 | |
| 146 | static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt) |
| 147 | { |
| 148 | enum emulation_result emulated = EMULATE_DONE; |
| 149 | ulong spr_val = 0; |
| 150 | |
| 151 | switch (sprn) { |
| 152 | case SPRN_SRR0: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 153 | spr_val = kvmppc_get_srr0(vcpu); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 154 | break; |
| 155 | case SPRN_SRR1: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 156 | spr_val = kvmppc_get_srr1(vcpu); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 157 | break; |
| 158 | case SPRN_PVR: |
| 159 | spr_val = vcpu->arch.pvr; |
| 160 | break; |
| 161 | case SPRN_PIR: |
| 162 | spr_val = vcpu->vcpu_id; |
| 163 | break; |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 164 | |
| 165 | /* Note: mftb and TBRL/TBWL are user-accessible, so |
| 166 | * the guest can always access the real TB anyways. |
| 167 | * In fact, we probably will never see these traps. */ |
| 168 | case SPRN_TBWL: |
| 169 | spr_val = get_tb() >> 32; |
| 170 | break; |
| 171 | case SPRN_TBWU: |
| 172 | spr_val = get_tb(); |
| 173 | break; |
| 174 | |
| 175 | case SPRN_SPRG0: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 176 | spr_val = kvmppc_get_sprg0(vcpu); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 177 | break; |
| 178 | case SPRN_SPRG1: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 179 | spr_val = kvmppc_get_sprg1(vcpu); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 180 | break; |
| 181 | case SPRN_SPRG2: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 182 | spr_val = kvmppc_get_sprg2(vcpu); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 183 | break; |
| 184 | case SPRN_SPRG3: |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 185 | spr_val = kvmppc_get_sprg3(vcpu); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 186 | break; |
| 187 | /* Note: SPRG4-7 are user-readable, so we don't get |
| 188 | * a trap. */ |
| 189 | |
| 190 | case SPRN_DEC: |
| 191 | spr_val = kvmppc_get_dec(vcpu, get_tb()); |
| 192 | break; |
| 193 | default: |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 194 | emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn, |
| 195 | &spr_val); |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 196 | if (unlikely(emulated == EMULATE_FAIL)) { |
| 197 | printk(KERN_INFO "mfspr: unknown spr " |
| 198 | "0x%x\n", sprn); |
| 199 | } |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | if (emulated == EMULATE_DONE) |
| 204 | kvmppc_set_gpr(vcpu, rt, spr_val); |
| 205 | kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS); |
| 206 | |
| 207 | return emulated; |
| 208 | } |
| 209 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 210 | /* XXX to do: |
| 211 | * lhax |
| 212 | * lhaux |
| 213 | * lswx |
| 214 | * lswi |
| 215 | * stswx |
| 216 | * stswi |
| 217 | * lha |
| 218 | * lhau |
| 219 | * lmw |
| 220 | * stmw |
| 221 | * |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 222 | */ |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 223 | /* XXX Should probably auto-generate instruction decoding for a particular core |
| 224 | * from opcode tables in the future. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 225 | int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 226 | { |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 227 | u32 inst = kvmppc_get_last_inst(vcpu); |
Alexander Graf | c46dc9a | 2012-05-04 14:01:33 +0200 | [diff] [blame] | 228 | int ra = get_ra(inst); |
| 229 | int rs = get_rs(inst); |
| 230 | int rt = get_rt(inst); |
| 231 | int sprn = get_sprn(inst); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 232 | enum emulation_result emulated = EMULATE_DONE; |
| 233 | int advance = 1; |
| 234 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 235 | /* this default type might be overwritten by subcategories */ |
| 236 | kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS); |
| 237 | |
Joe Perches | 689fd14 | 2010-09-11 19:10:53 +0000 | [diff] [blame] | 238 | pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst)); |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 239 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 240 | switch (get_op(inst)) { |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 241 | case OP_TRAP: |
Alexander Graf | 00c3a37 | 2010-04-16 00:11:42 +0200 | [diff] [blame] | 242 | #ifdef CONFIG_PPC_BOOK3S |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 243 | case OP_TRAP_64: |
Alexander Graf | 25a8a02 | 2010-01-08 02:58:07 +0100 | [diff] [blame] | 244 | kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP); |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 245 | #else |
Scott Wood | b590497 | 2011-11-08 18:23:30 -0600 | [diff] [blame] | 246 | kvmppc_core_queue_program(vcpu, |
| 247 | vcpu->arch.shared->esr | ESR_PTR); |
Liu Yu | daf5e27 | 2010-02-02 19:44:35 +0800 | [diff] [blame] | 248 | #endif |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 249 | advance = 0; |
| 250 | break; |
| 251 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 252 | case 31: |
| 253 | switch (get_xop(inst)) { |
| 254 | |
Alexander Graf | 6df79df | 2012-03-13 22:15:45 +0100 | [diff] [blame] | 255 | case OP_31_XOP_TRAP: |
| 256 | #ifdef CONFIG_64BIT |
| 257 | case OP_31_XOP_TRAP_64: |
| 258 | #endif |
| 259 | #ifdef CONFIG_PPC_BOOK3S |
| 260 | kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP); |
| 261 | #else |
| 262 | kvmppc_core_queue_program(vcpu, |
| 263 | vcpu->arch.shared->esr | ESR_PTR); |
| 264 | #endif |
| 265 | advance = 0; |
| 266 | break; |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 267 | case OP_31_XOP_LWZX: |
Hollis Blanchard | ac3cd34 | 2008-05-21 18:22:52 -0500 | [diff] [blame] | 268 | emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); |
| 269 | break; |
| 270 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 271 | case OP_31_XOP_LBZX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 272 | emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); |
| 273 | break; |
| 274 | |
Alexander Graf | 1c85e73 | 2010-03-24 21:48:27 +0100 | [diff] [blame] | 275 | case OP_31_XOP_LBZUX: |
Alexander Graf | 1c85e73 | 2010-03-24 21:48:27 +0100 | [diff] [blame] | 276 | emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 277 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Alexander Graf | 1c85e73 | 2010-03-24 21:48:27 +0100 | [diff] [blame] | 278 | break; |
| 279 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 280 | case OP_31_XOP_STWX: |
Hollis Blanchard | ac3cd34 | 2008-05-21 18:22:52 -0500 | [diff] [blame] | 281 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 282 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | ac3cd34 | 2008-05-21 18:22:52 -0500 | [diff] [blame] | 283 | 4, 1); |
| 284 | break; |
| 285 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 286 | case OP_31_XOP_STBX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 287 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 288 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 289 | 1, 1); |
| 290 | break; |
| 291 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 292 | case OP_31_XOP_STBUX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 293 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 294 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 295 | 1, 1); |
Alexander Graf | 978b4fa | 2012-04-27 01:00:17 +0200 | [diff] [blame] | 296 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 297 | break; |
| 298 | |
Alexander Graf | 1c85e73 | 2010-03-24 21:48:27 +0100 | [diff] [blame] | 299 | case OP_31_XOP_LHAX: |
Alexander Graf | 1c85e73 | 2010-03-24 21:48:27 +0100 | [diff] [blame] | 300 | emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1); |
| 301 | break; |
| 302 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 303 | case OP_31_XOP_LHZX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 304 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); |
| 305 | break; |
| 306 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 307 | case OP_31_XOP_LHZUX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 308 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 309 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 310 | break; |
| 311 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 312 | case OP_31_XOP_MFSPR: |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 313 | emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 314 | break; |
| 315 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 316 | case OP_31_XOP_STHX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 317 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 318 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 319 | 2, 1); |
| 320 | break; |
| 321 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 322 | case OP_31_XOP_STHUX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 323 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 324 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 325 | 2, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 326 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 327 | break; |
| 328 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 329 | case OP_31_XOP_MTSPR: |
Alexander Graf | 388cf9e | 2012-10-06 23:19:01 +0200 | [diff] [blame] | 330 | emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 331 | break; |
| 332 | |
Stuart Yoder | c32498e | 2013-04-09 10:36:23 +0000 | [diff] [blame] | 333 | case OP_31_XOP_DCBST: |
Alexander Graf | d328614 | 2013-01-17 13:50:25 +0100 | [diff] [blame] | 334 | case OP_31_XOP_DCBF: |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 335 | case OP_31_XOP_DCBI: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 336 | /* Do nothing. The guest is performing dcbi because |
| 337 | * hardware DMA is not snooped by the dcache, but |
| 338 | * emulated DMA either goes through the dcache as |
| 339 | * normal writes, or the host kernel has handled dcache |
| 340 | * coherence. */ |
| 341 | break; |
| 342 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 343 | case OP_31_XOP_LWBRX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 344 | emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0); |
| 345 | break; |
| 346 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 347 | case OP_31_XOP_TLBSYNC: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 348 | break; |
| 349 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 350 | case OP_31_XOP_STWBRX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 351 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 352 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 353 | 4, 0); |
| 354 | break; |
| 355 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 356 | case OP_31_XOP_LHBRX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 357 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0); |
| 358 | break; |
| 359 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 360 | case OP_31_XOP_STHBRX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 361 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 362 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 363 | 2, 0); |
| 364 | break; |
| 365 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 366 | default: |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 367 | /* Attempt core-specific emulation below. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 368 | emulated = EMULATE_FAIL; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 369 | } |
| 370 | break; |
| 371 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 372 | case OP_LWZ: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 373 | emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); |
| 374 | break; |
| 375 | |
Varun Sethi | 6c5cb73 | 2012-06-18 12:14:55 +0000 | [diff] [blame] | 376 | /* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */ |
| 377 | case OP_LD: |
| 378 | rt = get_rt(inst); |
| 379 | emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1); |
| 380 | break; |
| 381 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 382 | case OP_LWZU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 383 | emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 384 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 385 | break; |
| 386 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 387 | case OP_LBZ: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 388 | emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); |
| 389 | break; |
| 390 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 391 | case OP_LBZU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 392 | emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 393 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 394 | break; |
| 395 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 396 | case OP_STW: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 397 | emulated = kvmppc_handle_store(run, vcpu, |
| 398 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 399 | 4, 1); |
| 400 | break; |
| 401 | |
Varun Sethi | 6c5cb73 | 2012-06-18 12:14:55 +0000 | [diff] [blame] | 402 | /* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */ |
| 403 | case OP_STD: |
| 404 | rs = get_rs(inst); |
| 405 | emulated = kvmppc_handle_store(run, vcpu, |
| 406 | kvmppc_get_gpr(vcpu, rs), |
| 407 | 8, 1); |
| 408 | break; |
| 409 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 410 | case OP_STWU: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 411 | emulated = kvmppc_handle_store(run, vcpu, |
| 412 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 413 | 4, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 414 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 415 | break; |
| 416 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 417 | case OP_STB: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 418 | emulated = kvmppc_handle_store(run, vcpu, |
| 419 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 420 | 1, 1); |
| 421 | break; |
| 422 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 423 | case OP_STBU: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 424 | emulated = kvmppc_handle_store(run, vcpu, |
| 425 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 426 | 1, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 427 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 428 | break; |
| 429 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 430 | case OP_LHZ: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 431 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); |
| 432 | break; |
| 433 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 434 | case OP_LHZU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 435 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 436 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 437 | break; |
| 438 | |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 439 | case OP_LHA: |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 440 | emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1); |
| 441 | break; |
| 442 | |
| 443 | case OP_LHAU: |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 444 | emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 445 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Alexander Graf | 3587d53 | 2010-02-19 11:00:30 +0100 | [diff] [blame] | 446 | break; |
| 447 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 448 | case OP_STH: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 449 | emulated = kvmppc_handle_store(run, vcpu, |
| 450 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 451 | 2, 1); |
| 452 | break; |
| 453 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 454 | case OP_STHU: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 455 | emulated = kvmppc_handle_store(run, vcpu, |
| 456 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 457 | 2, 1); |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 458 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 459 | break; |
| 460 | |
| 461 | default: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 462 | emulated = EMULATE_FAIL; |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | if (emulated == EMULATE_FAIL) { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 466 | emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst, |
| 467 | &advance); |
Alexander Graf | 37f5bca | 2010-02-19 11:00:31 +0100 | [diff] [blame] | 468 | if (emulated == EMULATE_AGAIN) { |
| 469 | advance = 0; |
| 470 | } else if (emulated == EMULATE_FAIL) { |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 471 | advance = 0; |
| 472 | printk(KERN_ERR "Couldn't emulate instruction 0x%08x " |
| 473 | "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst)); |
Alexander Graf | 5f2b105 | 2010-01-10 03:27:32 +0100 | [diff] [blame] | 474 | kvmppc_core_queue_program(vcpu, 0); |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 475 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 476 | } |
| 477 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 478 | trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated); |
Christian Ehrhardt | 3b4bd79 | 2008-07-14 14:00:04 +0200 | [diff] [blame] | 479 | |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 480 | /* Advance past emulated instruction. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 481 | if (advance) |
Alexander Graf | c7f38f4 | 2010-04-16 00:11:40 +0200 | [diff] [blame] | 482 | kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 483 | |
| 484 | return emulated; |
| 485 | } |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 486 | EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction); |