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 | */ |
| 19 | |
| 20 | #include <linux/jiffies.h> |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 21 | #include <linux/hrtimer.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 22 | #include <linux/types.h> |
| 23 | #include <linux/string.h> |
| 24 | #include <linux/kvm_host.h> |
| 25 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 26 | #include <asm/reg.h> |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 27 | #include <asm/time.h> |
| 28 | #include <asm/byteorder.h> |
| 29 | #include <asm/kvm_ppc.h> |
Hollis Blanchard | c381a04 | 2008-11-05 09:36:15 -0600 | [diff] [blame] | 30 | #include <asm/disassemble.h> |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 31 | #include "timing.h" |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 32 | #include "trace.h" |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 33 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 34 | #define OP_TRAP 3 |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 35 | #define OP_TRAP_64 2 |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 36 | |
| 37 | #define OP_31_XOP_LWZX 23 |
| 38 | #define OP_31_XOP_LBZX 87 |
| 39 | #define OP_31_XOP_STWX 151 |
| 40 | #define OP_31_XOP_STBX 215 |
| 41 | #define OP_31_XOP_STBUX 247 |
| 42 | #define OP_31_XOP_LHZX 279 |
| 43 | #define OP_31_XOP_LHZUX 311 |
| 44 | #define OP_31_XOP_MFSPR 339 |
| 45 | #define OP_31_XOP_STHX 407 |
| 46 | #define OP_31_XOP_STHUX 439 |
| 47 | #define OP_31_XOP_MTSPR 467 |
| 48 | #define OP_31_XOP_DCBI 470 |
| 49 | #define OP_31_XOP_LWBRX 534 |
| 50 | #define OP_31_XOP_TLBSYNC 566 |
| 51 | #define OP_31_XOP_STWBRX 662 |
| 52 | #define OP_31_XOP_LHBRX 790 |
| 53 | #define OP_31_XOP_STHBRX 918 |
| 54 | |
| 55 | #define OP_LWZ 32 |
| 56 | #define OP_LWZU 33 |
| 57 | #define OP_LBZ 34 |
| 58 | #define OP_LBZU 35 |
| 59 | #define OP_STW 36 |
| 60 | #define OP_STWU 37 |
| 61 | #define OP_STB 38 |
| 62 | #define OP_STBU 39 |
| 63 | #define OP_LHZ 40 |
| 64 | #define OP_LHZU 41 |
| 65 | #define OP_STH 44 |
| 66 | #define OP_STHU 45 |
| 67 | |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 68 | #ifdef CONFIG_PPC64 |
| 69 | static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu) |
| 70 | { |
| 71 | return 1; |
| 72 | } |
| 73 | #else |
| 74 | static int kvmppc_dec_enabled(struct kvm_vcpu *vcpu) |
| 75 | { |
| 76 | return vcpu->arch.tcr & TCR_DIE; |
| 77 | } |
| 78 | #endif |
| 79 | |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 80 | void kvmppc_emulate_dec(struct kvm_vcpu *vcpu) |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 81 | { |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 82 | unsigned long dec_nsec; |
Alexander Graf | 9a7a9b0 | 2009-10-30 05:47:15 +0000 | [diff] [blame] | 83 | |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 84 | pr_debug("mtDEC: %x\n", vcpu->arch.dec); |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 85 | #ifdef CONFIG_PPC64 |
Alexander Graf | 7706664d | 2009-12-21 20:21:24 +0100 | [diff] [blame] | 86 | /* mtdec lowers the interrupt line when positive. */ |
| 87 | kvmppc_core_dequeue_dec(vcpu); |
| 88 | |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 89 | /* POWER4+ triggers a dec interrupt if the value is < 0 */ |
| 90 | if (vcpu->arch.dec & 0x80000000) { |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 91 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 92 | kvmppc_core_queue_dec(vcpu); |
| 93 | return; |
| 94 | } |
| 95 | #endif |
| 96 | if (kvmppc_dec_enabled(vcpu)) { |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 97 | /* The decrementer ticks at the same rate as the timebase, so |
| 98 | * that's how we convert the guest DEC value to the number of |
| 99 | * host ticks. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 100 | |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 101 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
| 102 | dec_nsec = vcpu->arch.dec; |
| 103 | dec_nsec *= 1000; |
| 104 | dec_nsec /= tb_ticks_per_usec; |
| 105 | hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec), |
| 106 | HRTIMER_MODE_REL); |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 107 | vcpu->arch.dec_jiffies = get_tb(); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 108 | } else { |
Alexander Graf | 544c676 | 2009-11-02 12:02:31 +0000 | [diff] [blame] | 109 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 113 | /* XXX to do: |
| 114 | * lhax |
| 115 | * lhaux |
| 116 | * lswx |
| 117 | * lswi |
| 118 | * stswx |
| 119 | * stswi |
| 120 | * lha |
| 121 | * lhau |
| 122 | * lmw |
| 123 | * stmw |
| 124 | * |
| 125 | * XXX is_bigendian should depend on MMU mapping or MSR[LE] |
| 126 | */ |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 127 | /* XXX Should probably auto-generate instruction decoding for a particular core |
| 128 | * from opcode tables in the future. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 129 | int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 130 | { |
| 131 | u32 inst = vcpu->arch.last_inst; |
| 132 | u32 ea; |
| 133 | int ra; |
| 134 | int rb; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 135 | int rs; |
| 136 | int rt; |
| 137 | int sprn; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 138 | enum emulation_result emulated = EMULATE_DONE; |
| 139 | int advance = 1; |
| 140 | |
Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 141 | /* this default type might be overwritten by subcategories */ |
| 142 | kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS); |
| 143 | |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 144 | pr_debug(KERN_INFO "Emulating opcode %d / %d\n", get_op(inst), get_xop(inst)); |
| 145 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 146 | switch (get_op(inst)) { |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 147 | case OP_TRAP: |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 148 | #ifdef CONFIG_PPC64 |
| 149 | case OP_TRAP_64: |
| 150 | #else |
Hollis Blanchard | fcfdbd2 | 2008-11-05 09:36:24 -0600 | [diff] [blame] | 151 | vcpu->arch.esr |= ESR_PTR; |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 152 | #endif |
Hollis Blanchard | 9dd921c | 2008-11-05 09:36:14 -0600 | [diff] [blame] | 153 | kvmppc_core_queue_program(vcpu); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 154 | advance = 0; |
| 155 | break; |
| 156 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 157 | case 31: |
| 158 | switch (get_xop(inst)) { |
| 159 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 160 | case OP_31_XOP_LWZX: |
Hollis Blanchard | ac3cd34 | 2008-05-21 18:22:52 -0500 | [diff] [blame] | 161 | rt = get_rt(inst); |
| 162 | emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); |
| 163 | break; |
| 164 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 165 | case OP_31_XOP_LBZX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 166 | rt = get_rt(inst); |
| 167 | emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); |
| 168 | break; |
| 169 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 170 | case OP_31_XOP_STWX: |
Hollis Blanchard | ac3cd34 | 2008-05-21 18:22:52 -0500 | [diff] [blame] | 171 | rs = get_rs(inst); |
| 172 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 173 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | ac3cd34 | 2008-05-21 18:22:52 -0500 | [diff] [blame] | 174 | 4, 1); |
| 175 | break; |
| 176 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 177 | case OP_31_XOP_STBX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 178 | rs = get_rs(inst); |
| 179 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 180 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 181 | 1, 1); |
| 182 | break; |
| 183 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 184 | case OP_31_XOP_STBUX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 185 | rs = get_rs(inst); |
| 186 | ra = get_ra(inst); |
| 187 | rb = get_rb(inst); |
| 188 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 189 | ea = kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 190 | if (ra) |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 191 | ea += kvmppc_get_gpr(vcpu, ra); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 192 | |
| 193 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 194 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 195 | 1, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 196 | kvmppc_set_gpr(vcpu, rs, ea); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 197 | break; |
| 198 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 199 | case OP_31_XOP_LHZX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 200 | rt = get_rt(inst); |
| 201 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); |
| 202 | break; |
| 203 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 204 | case OP_31_XOP_LHZUX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 205 | rt = get_rt(inst); |
| 206 | ra = get_ra(inst); |
| 207 | rb = get_rb(inst); |
| 208 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 209 | ea = kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 210 | if (ra) |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 211 | ea += kvmppc_get_gpr(vcpu, ra); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 212 | |
| 213 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 214 | kvmppc_set_gpr(vcpu, ra, ea); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 215 | break; |
| 216 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 217 | case OP_31_XOP_MFSPR: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 218 | sprn = get_sprn(inst); |
| 219 | rt = get_rt(inst); |
| 220 | |
| 221 | switch (sprn) { |
| 222 | case SPRN_SRR0: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 223 | kvmppc_set_gpr(vcpu, rt, vcpu->arch.srr0); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 224 | case SPRN_SRR1: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 225 | kvmppc_set_gpr(vcpu, rt, vcpu->arch.srr1); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 226 | case SPRN_PVR: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 227 | kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break; |
Liu Yu | 06579dd | 2009-06-05 14:54:31 +0800 | [diff] [blame] | 228 | case SPRN_PIR: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 229 | kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break; |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 230 | case SPRN_MSSSR0: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 231 | kvmppc_set_gpr(vcpu, rt, 0); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 232 | |
| 233 | /* Note: mftb and TBRL/TBWL are user-accessible, so |
| 234 | * the guest can always access the real TB anyways. |
| 235 | * In fact, we probably will never see these traps. */ |
| 236 | case SPRN_TBWL: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 237 | kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 238 | case SPRN_TBWU: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 239 | kvmppc_set_gpr(vcpu, rt, get_tb()); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 240 | |
| 241 | case SPRN_SPRG0: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 242 | kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg0); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 243 | case SPRN_SPRG1: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 244 | kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg1); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 245 | case SPRN_SPRG2: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 246 | kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg2); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 247 | case SPRN_SPRG3: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 248 | kvmppc_set_gpr(vcpu, rt, vcpu->arch.sprg3); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 249 | /* Note: SPRG4-7 are user-readable, so we don't get |
| 250 | * a trap. */ |
| 251 | |
Alexander Graf | 9a7a9b0 | 2009-10-30 05:47:15 +0000 | [diff] [blame] | 252 | case SPRN_DEC: |
| 253 | { |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 254 | u64 jd = get_tb() - vcpu->arch.dec_jiffies; |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 255 | kvmppc_set_gpr(vcpu, rt, vcpu->arch.dec - jd); |
| 256 | pr_debug(KERN_INFO "mfDEC: %x - %llx = %lx\n", |
| 257 | vcpu->arch.dec, jd, |
| 258 | kvmppc_get_gpr(vcpu, rt)); |
Alexander Graf | 9a7a9b0 | 2009-10-30 05:47:15 +0000 | [diff] [blame] | 259 | break; |
| 260 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 261 | default: |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 262 | emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt); |
| 263 | if (emulated == EMULATE_FAIL) { |
| 264 | printk("mfspr: unknown spr %x\n", sprn); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 265 | kvmppc_set_gpr(vcpu, rt, 0); |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 266 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | break; |
| 270 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 271 | case OP_31_XOP_STHX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 272 | rs = get_rs(inst); |
| 273 | ra = get_ra(inst); |
| 274 | rb = get_rb(inst); |
| 275 | |
| 276 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 277 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 278 | 2, 1); |
| 279 | break; |
| 280 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 281 | case OP_31_XOP_STHUX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 282 | rs = get_rs(inst); |
| 283 | ra = get_ra(inst); |
| 284 | rb = get_rb(inst); |
| 285 | |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 286 | ea = kvmppc_get_gpr(vcpu, rb); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 287 | if (ra) |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 288 | ea += kvmppc_get_gpr(vcpu, ra); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 289 | |
| 290 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 291 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 292 | 2, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 293 | kvmppc_set_gpr(vcpu, ra, ea); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 294 | break; |
| 295 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 296 | case OP_31_XOP_MTSPR: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 297 | sprn = get_sprn(inst); |
| 298 | rs = get_rs(inst); |
| 299 | switch (sprn) { |
| 300 | case SPRN_SRR0: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 301 | vcpu->arch.srr0 = kvmppc_get_gpr(vcpu, rs); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 302 | case SPRN_SRR1: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 303 | vcpu->arch.srr1 = kvmppc_get_gpr(vcpu, rs); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 304 | |
| 305 | /* XXX We need to context-switch the timebase for |
| 306 | * watchdog and FIT. */ |
| 307 | case SPRN_TBWL: break; |
| 308 | case SPRN_TBWU: break; |
| 309 | |
Alexander Graf | 513579e | 2009-10-30 05:47:16 +0000 | [diff] [blame] | 310 | case SPRN_MSSSR0: break; |
| 311 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 312 | case SPRN_DEC: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 313 | vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 314 | kvmppc_emulate_dec(vcpu); |
| 315 | break; |
| 316 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 317 | case SPRN_SPRG0: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 318 | vcpu->arch.sprg0 = kvmppc_get_gpr(vcpu, rs); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 319 | case SPRN_SPRG1: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 320 | vcpu->arch.sprg1 = kvmppc_get_gpr(vcpu, rs); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 321 | case SPRN_SPRG2: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 322 | vcpu->arch.sprg2 = kvmppc_get_gpr(vcpu, rs); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 323 | case SPRN_SPRG3: |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 324 | vcpu->arch.sprg3 = kvmppc_get_gpr(vcpu, rs); break; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 325 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 326 | default: |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 327 | emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs); |
| 328 | if (emulated == EMULATE_FAIL) |
| 329 | printk("mtspr: unknown spr %x\n", sprn); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | break; |
| 333 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 334 | case OP_31_XOP_DCBI: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 335 | /* Do nothing. The guest is performing dcbi because |
| 336 | * hardware DMA is not snooped by the dcache, but |
| 337 | * emulated DMA either goes through the dcache as |
| 338 | * normal writes, or the host kernel has handled dcache |
| 339 | * coherence. */ |
| 340 | break; |
| 341 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 342 | case OP_31_XOP_LWBRX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 343 | rt = get_rt(inst); |
| 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 | rs = get_rs(inst); |
| 352 | ra = get_ra(inst); |
| 353 | rb = get_rb(inst); |
| 354 | |
| 355 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 356 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 357 | 4, 0); |
| 358 | break; |
| 359 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 360 | case OP_31_XOP_LHBRX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 361 | rt = get_rt(inst); |
| 362 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0); |
| 363 | break; |
| 364 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 365 | case OP_31_XOP_STHBRX: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 366 | rs = get_rs(inst); |
| 367 | ra = get_ra(inst); |
| 368 | rb = get_rb(inst); |
| 369 | |
| 370 | emulated = kvmppc_handle_store(run, vcpu, |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 371 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 372 | 2, 0); |
| 373 | break; |
| 374 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 375 | default: |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 376 | /* Attempt core-specific emulation below. */ |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 377 | emulated = EMULATE_FAIL; |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 378 | } |
| 379 | break; |
| 380 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 381 | case OP_LWZ: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 382 | rt = get_rt(inst); |
| 383 | emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); |
| 384 | break; |
| 385 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 386 | case OP_LWZU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 387 | ra = get_ra(inst); |
| 388 | rt = get_rt(inst); |
| 389 | emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 390 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 391 | break; |
| 392 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 393 | case OP_LBZ: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 394 | rt = get_rt(inst); |
| 395 | emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); |
| 396 | break; |
| 397 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 398 | case OP_LBZU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 399 | ra = get_ra(inst); |
| 400 | rt = get_rt(inst); |
| 401 | emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 402 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 403 | break; |
| 404 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 405 | case OP_STW: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 406 | rs = get_rs(inst); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 407 | emulated = kvmppc_handle_store(run, vcpu, |
| 408 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 409 | 4, 1); |
| 410 | break; |
| 411 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 412 | case OP_STWU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 413 | ra = get_ra(inst); |
| 414 | rs = get_rs(inst); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 415 | emulated = kvmppc_handle_store(run, vcpu, |
| 416 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 417 | 4, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 418 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 419 | break; |
| 420 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 421 | case OP_STB: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 422 | rs = get_rs(inst); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 423 | emulated = kvmppc_handle_store(run, vcpu, |
| 424 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 425 | 1, 1); |
| 426 | break; |
| 427 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 428 | case OP_STBU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 429 | ra = get_ra(inst); |
| 430 | rs = get_rs(inst); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 431 | emulated = kvmppc_handle_store(run, vcpu, |
| 432 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 433 | 1, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 434 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 435 | break; |
| 436 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 437 | case OP_LHZ: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 438 | rt = get_rt(inst); |
| 439 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); |
| 440 | break; |
| 441 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 442 | case OP_LHZU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 443 | ra = get_ra(inst); |
| 444 | rt = get_rt(inst); |
| 445 | emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 446 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 447 | break; |
| 448 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 449 | case OP_STH: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 450 | rs = get_rs(inst); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 451 | emulated = kvmppc_handle_store(run, vcpu, |
| 452 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 453 | 2, 1); |
| 454 | break; |
| 455 | |
Hollis Blanchard | cea5d8c | 2009-01-03 16:23:05 -0600 | [diff] [blame] | 456 | case OP_STHU: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 457 | ra = get_ra(inst); |
| 458 | rs = get_rs(inst); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 459 | emulated = kvmppc_handle_store(run, vcpu, |
| 460 | kvmppc_get_gpr(vcpu, rs), |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 461 | 2, 1); |
Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame^] | 462 | kvmppc_set_gpr(vcpu, ra, vcpu->arch.paddr_accessed); |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 463 | break; |
| 464 | |
| 465 | default: |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 466 | emulated = EMULATE_FAIL; |
Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | if (emulated == EMULATE_FAIL) { |
| 470 | emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance); |
| 471 | if (emulated == EMULATE_FAIL) { |
| 472 | advance = 0; |
| 473 | printk(KERN_ERR "Couldn't emulate instruction 0x%08x " |
| 474 | "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst)); |
| 475 | } |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 476 | } |
| 477 | |
Marcelo Tosatti | 46f43c6 | 2009-06-18 11:47:27 -0300 | [diff] [blame] | 478 | trace_kvm_ppc_instr(inst, vcpu->arch.pc, emulated); |
Christian Ehrhardt | 3b4bd79 | 2008-07-14 14:00:04 +0200 | [diff] [blame] | 479 | |
Hollis Blanchard | bbf45ba | 2008-04-16 23:28:09 -0500 | [diff] [blame] | 480 | if (advance) |
| 481 | vcpu->arch.pc += 4; /* Advance past emulated instruction. */ |
| 482 | |
| 483 | return emulated; |
| 484 | } |