| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [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. 2008 | 
|  | 16 | * | 
|  | 17 | * Authors: Hollis Blanchard <hollisb@us.ibm.com> | 
|  | 18 | */ | 
|  | 19 |  | 
|  | 20 | #include <asm/kvm_ppc.h> | 
|  | 21 | #include <asm/dcr.h> | 
|  | 22 | #include <asm/dcr-regs.h> | 
|  | 23 | #include <asm/disassemble.h> | 
| Hollis Blanchard | fe4e771 | 2008-11-10 14:57:36 -0600 | [diff] [blame] | 24 | #include <asm/kvm_44x.h> | 
| Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 25 | #include "timing.h" | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 26 |  | 
|  | 27 | #include "booke.h" | 
|  | 28 | #include "44x_tlb.h" | 
|  | 29 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 30 | #define XOP_MFDCR   323 | 
|  | 31 | #define XOP_MTDCR   451 | 
|  | 32 | #define XOP_TLBSX   914 | 
|  | 33 | #define XOP_ICCCI   966 | 
|  | 34 | #define XOP_TLBWE   978 | 
|  | 35 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 36 | int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, | 
|  | 37 | unsigned int inst, int *advance) | 
|  | 38 | { | 
|  | 39 | int emulated = EMULATE_DONE; | 
|  | 40 | int dcrn; | 
|  | 41 | int ra; | 
|  | 42 | int rb; | 
|  | 43 | int rc; | 
|  | 44 | int rs; | 
|  | 45 | int rt; | 
|  | 46 | int ws; | 
|  | 47 |  | 
|  | 48 | switch (get_op(inst)) { | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 49 | case 31: | 
|  | 50 | switch (get_xop(inst)) { | 
|  | 51 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 52 | case XOP_MFDCR: | 
|  | 53 | dcrn = get_dcrn(inst); | 
|  | 54 | rt = get_rt(inst); | 
|  | 55 |  | 
|  | 56 | /* The guest may access CPR0 registers to determine the timebase | 
|  | 57 | * frequency, and it must know the real host frequency because it | 
|  | 58 | * can directly access the timebase registers. | 
|  | 59 | * | 
|  | 60 | * It would be possible to emulate those accesses in userspace, | 
|  | 61 | * but userspace can really only figure out the end frequency. | 
|  | 62 | * We could decompose that into the factors that compute it, but | 
|  | 63 | * that's tricky math, and it's easier to just report the real | 
|  | 64 | * CPR0 values. | 
|  | 65 | */ | 
|  | 66 | switch (dcrn) { | 
|  | 67 | case DCRN_CPR0_CONFIG_ADDR: | 
|  | 68 | vcpu->arch.gpr[rt] = vcpu->arch.cpr0_cfgaddr; | 
|  | 69 | break; | 
|  | 70 | case DCRN_CPR0_CONFIG_DATA: | 
|  | 71 | local_irq_disable(); | 
|  | 72 | mtdcr(DCRN_CPR0_CONFIG_ADDR, | 
|  | 73 | vcpu->arch.cpr0_cfgaddr); | 
|  | 74 | vcpu->arch.gpr[rt] = mfdcr(DCRN_CPR0_CONFIG_DATA); | 
|  | 75 | local_irq_enable(); | 
|  | 76 | break; | 
|  | 77 | default: | 
|  | 78 | run->dcr.dcrn = dcrn; | 
|  | 79 | run->dcr.data =  0; | 
|  | 80 | run->dcr.is_write = 0; | 
|  | 81 | vcpu->arch.io_gpr = rt; | 
|  | 82 | vcpu->arch.dcr_needed = 1; | 
| Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 83 | kvmppc_account_exit(vcpu, DCR_EXITS); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 84 | emulated = EMULATE_DO_DCR; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | break; | 
|  | 88 |  | 
|  | 89 | case XOP_MTDCR: | 
|  | 90 | dcrn = get_dcrn(inst); | 
|  | 91 | rs = get_rs(inst); | 
|  | 92 |  | 
|  | 93 | /* emulate some access in kernel */ | 
|  | 94 | switch (dcrn) { | 
|  | 95 | case DCRN_CPR0_CONFIG_ADDR: | 
|  | 96 | vcpu->arch.cpr0_cfgaddr = vcpu->arch.gpr[rs]; | 
|  | 97 | break; | 
|  | 98 | default: | 
|  | 99 | run->dcr.dcrn = dcrn; | 
|  | 100 | run->dcr.data = vcpu->arch.gpr[rs]; | 
|  | 101 | run->dcr.is_write = 1; | 
|  | 102 | vcpu->arch.dcr_needed = 1; | 
| Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 103 | kvmppc_account_exit(vcpu, DCR_EXITS); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 104 | emulated = EMULATE_DO_DCR; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | break; | 
|  | 108 |  | 
|  | 109 | case XOP_TLBWE: | 
|  | 110 | ra = get_ra(inst); | 
|  | 111 | rs = get_rs(inst); | 
|  | 112 | ws = get_ws(inst); | 
|  | 113 | emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws); | 
|  | 114 | break; | 
|  | 115 |  | 
|  | 116 | case XOP_TLBSX: | 
|  | 117 | rt = get_rt(inst); | 
|  | 118 | ra = get_ra(inst); | 
|  | 119 | rb = get_rb(inst); | 
|  | 120 | rc = get_rc(inst); | 
|  | 121 | emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc); | 
|  | 122 | break; | 
|  | 123 |  | 
|  | 124 | case XOP_ICCCI: | 
|  | 125 | break; | 
|  | 126 |  | 
|  | 127 | default: | 
|  | 128 | emulated = EMULATE_FAIL; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | break; | 
|  | 132 |  | 
|  | 133 | default: | 
|  | 134 | emulated = EMULATE_FAIL; | 
|  | 135 | } | 
|  | 136 |  | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 137 | if (emulated == EMULATE_FAIL) | 
|  | 138 | emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance); | 
|  | 139 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 140 | return emulated; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs) | 
|  | 144 | { | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 145 | int emulated = EMULATE_DONE; | 
|  | 146 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 147 | switch (sprn) { | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 148 | case SPRN_PID: | 
|  | 149 | kvmppc_set_pid(vcpu, vcpu->arch.gpr[rs]); break; | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 150 | case SPRN_MMUCR: | 
|  | 151 | vcpu->arch.mmucr = vcpu->arch.gpr[rs]; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 152 | case SPRN_CCR0: | 
|  | 153 | vcpu->arch.ccr0 = vcpu->arch.gpr[rs]; break; | 
|  | 154 | case SPRN_CCR1: | 
|  | 155 | vcpu->arch.ccr1 = vcpu->arch.gpr[rs]; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 156 | default: | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 157 | emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, rs); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
| Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 160 | kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS); | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 161 | return emulated; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
|  | 164 | int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt) | 
|  | 165 | { | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 166 | int emulated = EMULATE_DONE; | 
|  | 167 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 168 | switch (sprn) { | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 169 | case SPRN_PID: | 
|  | 170 | vcpu->arch.gpr[rt] = vcpu->arch.pid; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 171 | case SPRN_MMUCR: | 
|  | 172 | vcpu->arch.gpr[rt] = vcpu->arch.mmucr; break; | 
|  | 173 | case SPRN_CCR0: | 
|  | 174 | vcpu->arch.gpr[rt] = vcpu->arch.ccr0; break; | 
|  | 175 | case SPRN_CCR1: | 
|  | 176 | vcpu->arch.gpr[rt] = vcpu->arch.ccr1; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 177 | default: | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 178 | emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, rt); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
| Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 181 | kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS); | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 182 | return emulated; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 183 | } | 
|  | 184 |  |