blob: 1a793c4c4a6793cfa8880b7064611e8958856c05 [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
Alexander Grafceb985f2012-08-16 00:34:58 +020030#define XOP_MFDCRX 259
Hollis Blanchard75f74f02008-11-05 09:36:16 -060031#define XOP_MFDCR 323
Alexander Grafe4dcfe82012-08-16 00:28:09 +020032#define XOP_MTDCRX 387
Hollis Blanchard75f74f02008-11-05 09:36:16 -060033#define XOP_MTDCR 451
34#define XOP_TLBSX 914
35#define XOP_ICCCI 966
36#define XOP_TLBWE 978
37
Alexander Grafe4dcfe82012-08-16 00:28:09 +020038static int emulate_mtdcr(struct kvm_vcpu *vcpu, int rs, int dcrn)
39{
40 /* emulate some access in kernel */
41 switch (dcrn) {
42 case DCRN_CPR0_CONFIG_ADDR:
43 vcpu->arch.cpr0_cfgaddr = kvmppc_get_gpr(vcpu, rs);
44 return EMULATE_DONE;
45 default:
46 vcpu->run->dcr.dcrn = dcrn;
47 vcpu->run->dcr.data = kvmppc_get_gpr(vcpu, rs);
48 vcpu->run->dcr.is_write = 1;
49 vcpu->arch.dcr_needed = 1;
50 kvmppc_account_exit(vcpu, DCR_EXITS);
51 return EMULATE_DO_DCR;
52 }
53}
54
Alexander Grafceb985f2012-08-16 00:34:58 +020055static int emulate_mfdcr(struct kvm_vcpu *vcpu, int rt, int dcrn)
56{
57 /* The guest may access CPR0 registers to determine the timebase
58 * frequency, and it must know the real host frequency because it
59 * can directly access the timebase registers.
60 *
61 * It would be possible to emulate those accesses in userspace,
62 * but userspace can really only figure out the end frequency.
63 * We could decompose that into the factors that compute it, but
64 * that's tricky math, and it's easier to just report the real
65 * CPR0 values.
66 */
67 switch (dcrn) {
68 case DCRN_CPR0_CONFIG_ADDR:
69 kvmppc_set_gpr(vcpu, rt, vcpu->arch.cpr0_cfgaddr);
70 break;
71 case DCRN_CPR0_CONFIG_DATA:
72 local_irq_disable();
73 mtdcr(DCRN_CPR0_CONFIG_ADDR,
74 vcpu->arch.cpr0_cfgaddr);
75 kvmppc_set_gpr(vcpu, rt,
76 mfdcr(DCRN_CPR0_CONFIG_DATA));
77 local_irq_enable();
78 break;
79 default:
80 vcpu->run->dcr.dcrn = dcrn;
81 vcpu->run->dcr.data = 0;
82 vcpu->run->dcr.is_write = 0;
83 vcpu->arch.io_gpr = rt;
84 vcpu->arch.dcr_needed = 1;
85 kvmppc_account_exit(vcpu, DCR_EXITS);
86 return EMULATE_DO_DCR;
87 }
88
89 return EMULATE_DONE;
90}
91
Hollis Blanchard75f74f02008-11-05 09:36:16 -060092int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu,
93 unsigned int inst, int *advance)
94{
95 int emulated = EMULATE_DONE;
Alexander Grafc46dc9a2012-05-04 14:01:33 +020096 int dcrn = get_dcrn(inst);
97 int ra = get_ra(inst);
98 int rb = get_rb(inst);
99 int rc = get_rc(inst);
100 int rs = get_rs(inst);
101 int rt = get_rt(inst);
102 int ws = get_ws(inst);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600103
104 switch (get_op(inst)) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600105 case 31:
106 switch (get_xop(inst)) {
107
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600108 case XOP_MFDCR:
Alexander Grafceb985f2012-08-16 00:34:58 +0200109 emulated = emulate_mfdcr(vcpu, rt, dcrn);
110 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600111
Alexander Grafceb985f2012-08-16 00:34:58 +0200112 case XOP_MFDCRX:
113 emulated = emulate_mfdcr(vcpu, rt,
114 kvmppc_get_gpr(vcpu, ra));
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600115 break;
116
117 case XOP_MTDCR:
Alexander Grafe4dcfe82012-08-16 00:28:09 +0200118 emulated = emulate_mtdcr(vcpu, rs, dcrn);
119 break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600120
Alexander Grafe4dcfe82012-08-16 00:28:09 +0200121 case XOP_MTDCRX:
122 emulated = emulate_mtdcr(vcpu, rs,
123 kvmppc_get_gpr(vcpu, ra));
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600124 break;
125
126 case XOP_TLBWE:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600127 emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws);
128 break;
129
130 case XOP_TLBSX:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600131 emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc);
132 break;
133
134 case XOP_ICCCI:
135 break;
136
137 default:
138 emulated = EMULATE_FAIL;
139 }
140
141 break;
142
143 default:
144 emulated = EMULATE_FAIL;
145 }
146
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600147 if (emulated == EMULATE_FAIL)
148 emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance);
149
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600150 return emulated;
151}
152
Alexander Graf54771e62012-05-04 14:55:12 +0200153int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val)
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600154{
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600155 int emulated = EMULATE_DONE;
156
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600157 switch (sprn) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600158 case SPRN_PID:
Alexander Graf54771e62012-05-04 14:55:12 +0200159 kvmppc_set_pid(vcpu, spr_val); break;
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600160 case SPRN_MMUCR:
Alexander Graf54771e62012-05-04 14:55:12 +0200161 vcpu->arch.mmucr = spr_val; break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600162 case SPRN_CCR0:
Alexander Graf54771e62012-05-04 14:55:12 +0200163 vcpu->arch.ccr0 = spr_val; break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600164 case SPRN_CCR1:
Alexander Graf54771e62012-05-04 14:55:12 +0200165 vcpu->arch.ccr1 = spr_val; break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600166 default:
Alexander Graf54771e62012-05-04 14:55:12 +0200167 emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, spr_val);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600168 }
169
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600170 return emulated;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600171}
172
Alexander Graf54771e62012-05-04 14:55:12 +0200173int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val)
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600174{
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600175 int emulated = EMULATE_DONE;
176
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600177 switch (sprn) {
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600178 case SPRN_PID:
Alexander Graf54771e62012-05-04 14:55:12 +0200179 *spr_val = vcpu->arch.pid; break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600180 case SPRN_MMUCR:
Alexander Graf54771e62012-05-04 14:55:12 +0200181 *spr_val = vcpu->arch.mmucr; break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600182 case SPRN_CCR0:
Alexander Graf54771e62012-05-04 14:55:12 +0200183 *spr_val = vcpu->arch.ccr0; break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600184 case SPRN_CCR1:
Alexander Graf54771e62012-05-04 14:55:12 +0200185 *spr_val = vcpu->arch.ccr1; break;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600186 default:
Alexander Graf54771e62012-05-04 14:55:12 +0200187 emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, spr_val);
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600188 }
189
Hollis Blanchardd0c7dc02009-01-03 16:23:06 -0600190 return emulated;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600191}
192