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.
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.
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.
15 * Copyright IBM Corp. 2008
16 * Copyright 2011 Freescale Semiconductor, Inc.
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
21 #include <linux/kvm_host.h>
22 #include <asm/disassemble.h>
26 #define OP_19_XOP_RFI 50
27 #define OP_19_XOP_RFCI 51
29 #define OP_31_XOP_MFMSR 83
30 #define OP_31_XOP_WRTEE 131
31 #define OP_31_XOP_MTMSR 146
32 #define OP_31_XOP_WRTEEI 163
34 static void kvmppc_emul_rfi(struct kvm_vcpu
*vcpu
)
36 vcpu
->arch
.pc
= vcpu
->arch
.shared
->srr0
;
37 kvmppc_set_msr(vcpu
, vcpu
->arch
.shared
->srr1
);
40 static void kvmppc_emul_rfci(struct kvm_vcpu
*vcpu
)
42 vcpu
->arch
.pc
= vcpu
->arch
.csrr0
;
43 kvmppc_set_msr(vcpu
, vcpu
->arch
.csrr1
);
46 int kvmppc_booke_emulate_op(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
47 unsigned int inst
, int *advance
)
49 int emulated
= EMULATE_DONE
;
50 int rs
= get_rs(inst
);
51 int rt
= get_rt(inst
);
53 switch (get_op(inst
)) {
55 switch (get_xop(inst
)) {
57 kvmppc_emul_rfi(vcpu
);
58 kvmppc_set_exit_type(vcpu
, EMULATED_RFI_EXITS
);
63 kvmppc_emul_rfci(vcpu
);
64 kvmppc_set_exit_type(vcpu
, EMULATED_RFCI_EXITS
);
69 emulated
= EMULATE_FAIL
;
75 switch (get_xop(inst
)) {
78 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->msr
);
79 kvmppc_set_exit_type(vcpu
, EMULATED_MFMSR_EXITS
);
83 kvmppc_set_exit_type(vcpu
, EMULATED_MTMSR_EXITS
);
84 kvmppc_set_msr(vcpu
, kvmppc_get_gpr(vcpu
, rs
));
88 vcpu
->arch
.shared
->msr
= (vcpu
->arch
.shared
->msr
& ~MSR_EE
)
89 | (kvmppc_get_gpr(vcpu
, rs
) & MSR_EE
);
90 kvmppc_set_exit_type(vcpu
, EMULATED_WRTEE_EXITS
);
93 case OP_31_XOP_WRTEEI
:
94 vcpu
->arch
.shared
->msr
= (vcpu
->arch
.shared
->msr
& ~MSR_EE
)
96 kvmppc_set_exit_type(vcpu
, EMULATED_WRTEE_EXITS
);
100 emulated
= EMULATE_FAIL
;
106 emulated
= EMULATE_FAIL
;
113 * NOTE: some of these registers are not emulated on BOOKE_HV (GS-mode).
114 * Their backing store is in real registers, and these functions
115 * will return the wrong result if called for them in another context
116 * (such as debugging).
118 int kvmppc_booke_emulate_mtspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong spr_val
)
120 int emulated
= EMULATE_DONE
;
124 vcpu
->arch
.shared
->dar
= spr_val
;
127 vcpu
->arch
.shared
->esr
= spr_val
;
130 vcpu
->arch
.csrr0
= spr_val
;
133 vcpu
->arch
.csrr1
= spr_val
;
136 vcpu
->arch
.dbg_reg
.dbcr0
= spr_val
;
139 vcpu
->arch
.dbg_reg
.dbcr1
= spr_val
;
142 vcpu
->arch
.dbsr
&= ~spr_val
;
145 kvmppc_clr_tsr_bits(vcpu
, spr_val
);
149 * WRC is a 2-bit field that is supposed to preserve its
150 * value once written to non-zero.
152 if (vcpu
->arch
.tcr
& TCR_WRC_MASK
) {
153 spr_val
&= ~TCR_WRC_MASK
;
154 spr_val
|= vcpu
->arch
.tcr
& TCR_WRC_MASK
;
156 kvmppc_set_tcr(vcpu
, spr_val
);
160 vcpu
->arch
.decar
= spr_val
;
163 * Note: SPRG4-7 are user-readable.
164 * These values are loaded into the real SPRGs when resuming the
165 * guest (PR-mode only).
168 vcpu
->arch
.shared
->sprg4
= spr_val
;
171 vcpu
->arch
.shared
->sprg5
= spr_val
;
174 vcpu
->arch
.shared
->sprg6
= spr_val
;
177 vcpu
->arch
.shared
->sprg7
= spr_val
;
181 vcpu
->arch
.ivpr
= spr_val
;
182 #ifdef CONFIG_KVM_BOOKE_HV
183 mtspr(SPRN_GIVPR
, spr_val
);
187 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_CRITICAL
] = spr_val
;
190 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_MACHINE_CHECK
] = spr_val
;
193 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DATA_STORAGE
] = spr_val
;
194 #ifdef CONFIG_KVM_BOOKE_HV
195 mtspr(SPRN_GIVOR2
, spr_val
);
199 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_INST_STORAGE
] = spr_val
;
202 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_EXTERNAL
] = spr_val
;
205 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ALIGNMENT
] = spr_val
;
208 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_PROGRAM
] = spr_val
;
211 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FP_UNAVAIL
] = spr_val
;
214 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_SYSCALL
] = spr_val
;
215 #ifdef CONFIG_KVM_BOOKE_HV
216 mtspr(SPRN_GIVOR8
, spr_val
);
220 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_AP_UNAVAIL
] = spr_val
;
223 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DECREMENTER
] = spr_val
;
226 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FIT
] = spr_val
;
229 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_WATCHDOG
] = spr_val
;
232 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DTLB_MISS
] = spr_val
;
235 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ITLB_MISS
] = spr_val
;
238 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DEBUG
] = spr_val
;
241 vcpu
->arch
.mcsr
&= ~spr_val
;
243 #if defined(CONFIG_64BIT)
245 kvmppc_set_epcr(vcpu
, spr_val
);
246 #ifdef CONFIG_KVM_BOOKE_HV
247 mtspr(SPRN_EPCR
, vcpu
->arch
.shadow_epcr
);
252 emulated
= EMULATE_FAIL
;
258 int kvmppc_booke_emulate_mfspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong
*spr_val
)
260 int emulated
= EMULATE_DONE
;
264 *spr_val
= vcpu
->arch
.ivpr
;
267 *spr_val
= vcpu
->arch
.shared
->dar
;
270 *spr_val
= vcpu
->arch
.shared
->esr
;
273 *spr_val
= vcpu
->arch
.epr
;
276 *spr_val
= vcpu
->arch
.csrr0
;
279 *spr_val
= vcpu
->arch
.csrr1
;
282 *spr_val
= vcpu
->arch
.dbg_reg
.dbcr0
;
285 *spr_val
= vcpu
->arch
.dbg_reg
.dbcr1
;
288 *spr_val
= vcpu
->arch
.dbsr
;
291 *spr_val
= vcpu
->arch
.tsr
;
294 *spr_val
= vcpu
->arch
.tcr
;
298 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_CRITICAL
];
301 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_MACHINE_CHECK
];
304 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DATA_STORAGE
];
307 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_INST_STORAGE
];
310 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_EXTERNAL
];
313 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ALIGNMENT
];
316 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_PROGRAM
];
319 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FP_UNAVAIL
];
322 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_SYSCALL
];
325 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_AP_UNAVAIL
];
328 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DECREMENTER
];
331 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FIT
];
334 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_WATCHDOG
];
337 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DTLB_MISS
];
340 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ITLB_MISS
];
343 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DEBUG
];
346 *spr_val
= vcpu
->arch
.mcsr
;
348 #if defined(CONFIG_64BIT)
350 *spr_val
= vcpu
->arch
.epcr
;
355 emulated
= EMULATE_FAIL
;