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
.dbcr0
= spr_val
;
139 vcpu
->arch
.dbcr1
= spr_val
;
142 vcpu
->arch
.dbsr
&= ~spr_val
;
145 kvmppc_clr_tsr_bits(vcpu
, spr_val
);
148 kvmppc_set_tcr(vcpu
, spr_val
);
152 vcpu
->arch
.decar
= spr_val
;
155 * Note: SPRG4-7 are user-readable.
156 * These values are loaded into the real SPRGs when resuming the
157 * guest (PR-mode only).
160 vcpu
->arch
.shared
->sprg4
= spr_val
;
163 vcpu
->arch
.shared
->sprg5
= spr_val
;
166 vcpu
->arch
.shared
->sprg6
= spr_val
;
169 vcpu
->arch
.shared
->sprg7
= spr_val
;
173 vcpu
->arch
.ivpr
= spr_val
;
174 #ifdef CONFIG_KVM_BOOKE_HV
175 mtspr(SPRN_GIVPR
, spr_val
);
179 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_CRITICAL
] = spr_val
;
182 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_MACHINE_CHECK
] = spr_val
;
185 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DATA_STORAGE
] = spr_val
;
186 #ifdef CONFIG_KVM_BOOKE_HV
187 mtspr(SPRN_GIVOR2
, spr_val
);
191 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_INST_STORAGE
] = spr_val
;
194 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_EXTERNAL
] = spr_val
;
197 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ALIGNMENT
] = spr_val
;
200 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_PROGRAM
] = spr_val
;
203 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FP_UNAVAIL
] = spr_val
;
206 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_SYSCALL
] = spr_val
;
207 #ifdef CONFIG_KVM_BOOKE_HV
208 mtspr(SPRN_GIVOR8
, spr_val
);
212 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_AP_UNAVAIL
] = spr_val
;
215 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DECREMENTER
] = spr_val
;
218 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FIT
] = spr_val
;
221 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_WATCHDOG
] = spr_val
;
224 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DTLB_MISS
] = spr_val
;
227 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ITLB_MISS
] = spr_val
;
230 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DEBUG
] = spr_val
;
234 emulated
= EMULATE_FAIL
;
240 int kvmppc_booke_emulate_mfspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong
*spr_val
)
242 int emulated
= EMULATE_DONE
;
246 *spr_val
= vcpu
->arch
.ivpr
;
249 *spr_val
= vcpu
->arch
.shared
->dar
;
252 *spr_val
= vcpu
->arch
.shared
->esr
;
255 *spr_val
= vcpu
->arch
.csrr0
;
258 *spr_val
= vcpu
->arch
.csrr1
;
261 *spr_val
= vcpu
->arch
.dbcr0
;
264 *spr_val
= vcpu
->arch
.dbcr1
;
267 *spr_val
= vcpu
->arch
.dbsr
;
270 *spr_val
= vcpu
->arch
.tsr
;
273 *spr_val
= vcpu
->arch
.tcr
;
277 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_CRITICAL
];
280 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_MACHINE_CHECK
];
283 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DATA_STORAGE
];
286 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_INST_STORAGE
];
289 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_EXTERNAL
];
292 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ALIGNMENT
];
295 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_PROGRAM
];
298 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FP_UNAVAIL
];
301 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_SYSCALL
];
304 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_AP_UNAVAIL
];
307 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DECREMENTER
];
310 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FIT
];
313 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_WATCHDOG
];
316 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DTLB_MISS
];
319 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ITLB_MISS
];
322 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DEBUG
];
326 emulated
= EMULATE_FAIL
;