blob: da81a2d92380a45bc230a499654c3c0d6c9621a6 [file] [log] [blame]
Hollis Blanchard75f74f02008-11-05 09:36:16 -06001/*
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 Blanchardfe4e7712008-11-10 14:57:36 -060024#include <asm/kvm_44x.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060025#include "timing.h"
Hollis Blanchard75f74f02008-11-05 09:36:16 -060026
27#include "booke.h"
28#include "44x_tlb.h"
29
Hollis Blanchard75f74f02008-11-05 09:36:16 -060030#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 Blanchard75f74f02008-11-05 09:36:16 -060036int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
37 unsigned int inst, int *advance)
38{
39 int emulated = EMULATE_DONE;
Alexander Grafc46dc9a2012-05-04 14:01:33 +020040 int dcrn = get_dcrn(inst);
41 int ra = get_ra(inst);
42 int rb = get_rb(inst);
43 int rc = get_rc(inst);
44 int rs = get_rs(inst);
45 int rt = get_rt(inst);
46 int ws = get_ws(inst);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060047
48 switch (get_op(inst)) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -060049 case 31:
50 switch (get_xop(inst)) {
51
Hollis Blanchard75f74f02008-11-05 09:36:16 -060052 case XOP_MFDCR:
Hollis Blanchard75f74f02008-11-05 09:36:16 -060053 /* The guest may access CPR0 registers to determine the timebase
54 * frequency, and it must know the real host frequency because it
55 * can directly access the timebase registers.
56 *
57 * It would be possible to emulate those accesses in userspace,
58 * but userspace can really only figure out the end frequency.
59 * We could decompose that into the factors that compute it, but
60 * that's tricky math, and it's easier to just report the real
61 * CPR0 values.
62 */
63 switch (dcrn) {
64 case DCRN_CPR0_CONFIG_ADDR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +010065 kvmppc_set_gpr(vcpu, rt, vcpu->arch.cpr0_cfgaddr);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060066 break;
67 case DCRN_CPR0_CONFIG_DATA:
68 local_irq_disable();
69 mtdcr(DCRN_CPR0_CONFIG_ADDR,
70 vcpu->arch.cpr0_cfgaddr);
Alexander Graf8e5b26b2010-01-08 02:58:01 +010071 kvmppc_set_gpr(vcpu, rt,
72 mfdcr(DCRN_CPR0_CONFIG_DATA));
Hollis Blanchard75f74f02008-11-05 09:36:16 -060073 local_irq_enable();
74 break;
75 default:
76 run->dcr.dcrn = dcrn;
77 run->dcr.data = 0;
78 run->dcr.is_write = 0;
79 vcpu->arch.io_gpr = rt;
80 vcpu->arch.dcr_needed = 1;
Hollis Blanchard7b701592008-12-02 15:51:58 -060081 kvmppc_account_exit(vcpu, DCR_EXITS);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060082 emulated = EMULATE_DO_DCR;
83 }
84
85 break;
86
87 case XOP_MTDCR:
Hollis Blanchard75f74f02008-11-05 09:36:16 -060088 /* emulate some access in kernel */
89 switch (dcrn) {
90 case DCRN_CPR0_CONFIG_ADDR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +010091 vcpu->arch.cpr0_cfgaddr = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060092 break;
93 default:
94 run->dcr.dcrn = dcrn;
Alexander Graf8e5b26b2010-01-08 02:58:01 +010095 run->dcr.data = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060096 run->dcr.is_write = 1;
97 vcpu->arch.dcr_needed = 1;
Hollis Blanchard7b701592008-12-02 15:51:58 -060098 kvmppc_account_exit(vcpu, DCR_EXITS);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060099 emulated = EMULATE_DO_DCR;
100 }
101
102 break;
103
104 case XOP_TLBWE:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600105 emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws);
106 break;
107
108 case XOP_TLBSX:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600109 emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc);
110 break;
111
112 case XOP_ICCCI:
113 break;
114
115 default:
116 emulated = EMULATE_FAIL;
117 }
118
119 break;
120
121 default:
122 emulated = EMULATE_FAIL;
123 }
124
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600125 if (emulated == EMULATE_FAIL)
126 emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance);
127
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600128 return emulated;
129}
130
131int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
132{
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600133 int emulated = EMULATE_DONE;
134
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600135 switch (sprn) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600136 case SPRN_PID:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100137 kvmppc_set_pid(vcpu, kvmppc_get_gpr(vcpu, rs)); break;
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600138 case SPRN_MMUCR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100139 vcpu->arch.mmucr = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600140 case SPRN_CCR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100141 vcpu->arch.ccr0 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600142 case SPRN_CCR1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100143 vcpu->arch.ccr1 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600144 default:
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600145 emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, rs);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600146 }
147
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600148 return emulated;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600149}
150
151int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
152{
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600153 int emulated = EMULATE_DONE;
154
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600155 switch (sprn) {
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600156 case SPRN_PID:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100157 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pid); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600158 case SPRN_MMUCR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100159 kvmppc_set_gpr(vcpu, rt, vcpu->arch.mmucr); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600160 case SPRN_CCR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100161 kvmppc_set_gpr(vcpu, rt, vcpu->arch.ccr0); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600162 case SPRN_CCR1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100163 kvmppc_set_gpr(vcpu, rt, vcpu->arch.ccr1); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600164 default:
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600165 emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, rt);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600166 }
167
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600168 return emulated;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600169}
170