blob: ded8a1a042646099ef70d497245f729ee45621f2 [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;
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 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:
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:
Alexander Graf8e5b26b2010-01-08 02:58:01 +010068 kvmppc_set_gpr(vcpu, rt, vcpu->arch.cpr0_cfgaddr);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060069 break;
70 case DCRN_CPR0_CONFIG_DATA:
71 local_irq_disable();
72 mtdcr(DCRN_CPR0_CONFIG_ADDR,
73 vcpu->arch.cpr0_cfgaddr);
Alexander Graf8e5b26b2010-01-08 02:58:01 +010074 kvmppc_set_gpr(vcpu, rt,
75 mfdcr(DCRN_CPR0_CONFIG_DATA));
Hollis Blanchard75f74f02008-11-05 09:36:16 -060076 local_irq_enable();
77 break;
78 default:
79 run->dcr.dcrn = dcrn;
80 run->dcr.data = 0;
81 run->dcr.is_write = 0;
Alexander Graf7d609c82012-10-06 03:56:35 +020082 vcpu->arch.dcr_is_write = 0;
Hollis Blanchard75f74f02008-11-05 09:36:16 -060083 vcpu->arch.io_gpr = rt;
84 vcpu->arch.dcr_needed = 1;
Hollis Blanchard7b701592008-12-02 15:51:58 -060085 kvmppc_account_exit(vcpu, DCR_EXITS);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060086 emulated = EMULATE_DO_DCR;
87 }
88
89 break;
90
91 case XOP_MTDCR:
92 dcrn = get_dcrn(inst);
93 rs = get_rs(inst);
94
95 /* emulate some access in kernel */
96 switch (dcrn) {
97 case DCRN_CPR0_CONFIG_ADDR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +010098 vcpu->arch.cpr0_cfgaddr = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchard75f74f02008-11-05 09:36:16 -060099 break;
100 default:
101 run->dcr.dcrn = dcrn;
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100102 run->dcr.data = kvmppc_get_gpr(vcpu, rs);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600103 run->dcr.is_write = 1;
Alexander Graf7d609c82012-10-06 03:56:35 +0200104 vcpu->arch.dcr_is_write = 1;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600105 vcpu->arch.dcr_needed = 1;
Hollis Blanchard7b701592008-12-02 15:51:58 -0600106 kvmppc_account_exit(vcpu, DCR_EXITS);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600107 emulated = EMULATE_DO_DCR;
108 }
109
110 break;
111
112 case XOP_TLBWE:
113 ra = get_ra(inst);
114 rs = get_rs(inst);
115 ws = get_ws(inst);
116 emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws);
117 break;
118
119 case XOP_TLBSX:
120 rt = get_rt(inst);
121 ra = get_ra(inst);
122 rb = get_rb(inst);
123 rc = get_rc(inst);
124 emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc);
125 break;
126
127 case XOP_ICCCI:
128 break;
129
130 default:
131 emulated = EMULATE_FAIL;
132 }
133
134 break;
135
136 default:
137 emulated = EMULATE_FAIL;
138 }
139
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600140 if (emulated == EMULATE_FAIL)
141 emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance);
142
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600143 return emulated;
144}
145
146int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
147{
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600148 int emulated = EMULATE_DONE;
149
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600150 switch (sprn) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600151 case SPRN_PID:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100152 kvmppc_set_pid(vcpu, kvmppc_get_gpr(vcpu, rs)); break;
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600153 case SPRN_MMUCR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100154 vcpu->arch.mmucr = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600155 case SPRN_CCR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100156 vcpu->arch.ccr0 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600157 case SPRN_CCR1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100158 vcpu->arch.ccr1 = kvmppc_get_gpr(vcpu, rs); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600159 default:
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600160 emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, rs);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600161 }
162
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600163 return emulated;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600164}
165
166int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
167{
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600168 int emulated = EMULATE_DONE;
169
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600170 switch (sprn) {
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600171 case SPRN_PID:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100172 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pid); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600173 case SPRN_MMUCR:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100174 kvmppc_set_gpr(vcpu, rt, vcpu->arch.mmucr); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600175 case SPRN_CCR0:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100176 kvmppc_set_gpr(vcpu, rt, vcpu->arch.ccr0); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600177 case SPRN_CCR1:
Alexander Graf8e5b26b2010-01-08 02:58:01 +0100178 kvmppc_set_gpr(vcpu, rt, vcpu->arch.ccr1); break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600179 default:
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600180 emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, rt);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600181 }
182
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600183 return emulated;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600184}
185