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
28 #define OP_19_XOP_RFDI 39
30 #define OP_31_XOP_MFMSR 83
31 #define OP_31_XOP_WRTEE 131
32 #define OP_31_XOP_MTMSR 146
33 #define OP_31_XOP_WRTEEI 163
35 static void kvmppc_emul_rfi(struct kvm_vcpu
*vcpu
)
37 vcpu
->arch
.pc
= vcpu
->arch
.shared
->srr0
;
38 kvmppc_set_msr(vcpu
, vcpu
->arch
.shared
->srr1
);
41 static void kvmppc_emul_rfdi(struct kvm_vcpu
*vcpu
)
43 vcpu
->arch
.pc
= vcpu
->arch
.dsrr0
;
44 kvmppc_set_msr(vcpu
, vcpu
->arch
.dsrr1
);
47 static void kvmppc_emul_rfci(struct kvm_vcpu
*vcpu
)
49 vcpu
->arch
.pc
= vcpu
->arch
.csrr0
;
50 kvmppc_set_msr(vcpu
, vcpu
->arch
.csrr1
);
53 int kvmppc_booke_emulate_op(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
54 unsigned int inst
, int *advance
)
56 int emulated
= EMULATE_DONE
;
57 int rs
= get_rs(inst
);
58 int rt
= get_rt(inst
);
60 switch (get_op(inst
)) {
62 switch (get_xop(inst
)) {
64 kvmppc_emul_rfi(vcpu
);
65 kvmppc_set_exit_type(vcpu
, EMULATED_RFI_EXITS
);
70 kvmppc_emul_rfci(vcpu
);
71 kvmppc_set_exit_type(vcpu
, EMULATED_RFCI_EXITS
);
76 kvmppc_emul_rfdi(vcpu
);
77 kvmppc_set_exit_type(vcpu
, EMULATED_RFDI_EXITS
);
82 emulated
= EMULATE_FAIL
;
88 switch (get_xop(inst
)) {
91 kvmppc_set_gpr(vcpu
, rt
, vcpu
->arch
.shared
->msr
);
92 kvmppc_set_exit_type(vcpu
, EMULATED_MFMSR_EXITS
);
96 kvmppc_set_exit_type(vcpu
, EMULATED_MTMSR_EXITS
);
97 kvmppc_set_msr(vcpu
, kvmppc_get_gpr(vcpu
, rs
));
100 case OP_31_XOP_WRTEE
:
101 vcpu
->arch
.shared
->msr
= (vcpu
->arch
.shared
->msr
& ~MSR_EE
)
102 | (kvmppc_get_gpr(vcpu
, rs
) & MSR_EE
);
103 kvmppc_set_exit_type(vcpu
, EMULATED_WRTEE_EXITS
);
106 case OP_31_XOP_WRTEEI
:
107 vcpu
->arch
.shared
->msr
= (vcpu
->arch
.shared
->msr
& ~MSR_EE
)
109 kvmppc_set_exit_type(vcpu
, EMULATED_WRTEE_EXITS
);
113 emulated
= EMULATE_FAIL
;
119 emulated
= EMULATE_FAIL
;
126 * NOTE: some of these registers are not emulated on BOOKE_HV (GS-mode).
127 * Their backing store is in real registers, and these functions
128 * will return the wrong result if called for them in another context
129 * (such as debugging).
131 int kvmppc_booke_emulate_mtspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong spr_val
)
133 int emulated
= EMULATE_DONE
;
134 bool debug_inst
= false;
138 vcpu
->arch
.shared
->dar
= spr_val
;
141 vcpu
->arch
.shared
->esr
= spr_val
;
144 vcpu
->arch
.csrr0
= spr_val
;
147 vcpu
->arch
.csrr1
= spr_val
;
150 vcpu
->arch
.dsrr0
= spr_val
;
153 vcpu
->arch
.dsrr1
= spr_val
;
157 * If userspace is debugging guest then guest
158 * can not access debug registers.
160 if (vcpu
->guest_debug
)
164 vcpu
->arch
.dbg_reg
.iac1
= spr_val
;
168 * If userspace is debugging guest then guest
169 * can not access debug registers.
171 if (vcpu
->guest_debug
)
175 vcpu
->arch
.dbg_reg
.iac2
= spr_val
;
177 #if CONFIG_PPC_ADV_DEBUG_IACS > 2
180 * If userspace is debugging guest then guest
181 * can not access debug registers.
183 if (vcpu
->guest_debug
)
187 vcpu
->arch
.dbg_reg
.iac3
= spr_val
;
191 * If userspace is debugging guest then guest
192 * can not access debug registers.
194 if (vcpu
->guest_debug
)
198 vcpu
->arch
.dbg_reg
.iac4
= spr_val
;
203 * If userspace is debugging guest then guest
204 * can not access debug registers.
206 if (vcpu
->guest_debug
)
210 vcpu
->arch
.dbg_reg
.dac1
= spr_val
;
214 * If userspace is debugging guest then guest
215 * can not access debug registers.
217 if (vcpu
->guest_debug
)
221 vcpu
->arch
.dbg_reg
.dac2
= spr_val
;
225 * If userspace is debugging guest then guest
226 * can not access debug registers.
228 if (vcpu
->guest_debug
)
232 spr_val
&= (DBCR0_IDM
| DBCR0_IC
| DBCR0_BT
| DBCR0_TIE
|
233 DBCR0_IAC1
| DBCR0_IAC2
| DBCR0_IAC3
| DBCR0_IAC4
|
234 DBCR0_DAC1R
| DBCR0_DAC1W
| DBCR0_DAC2R
| DBCR0_DAC2W
);
236 vcpu
->arch
.dbg_reg
.dbcr0
= spr_val
;
240 * If userspace is debugging guest then guest
241 * can not access debug registers.
243 if (vcpu
->guest_debug
)
247 vcpu
->arch
.dbg_reg
.dbcr1
= spr_val
;
251 * If userspace is debugging guest then guest
252 * can not access debug registers.
254 if (vcpu
->guest_debug
)
258 vcpu
->arch
.dbg_reg
.dbcr2
= spr_val
;
262 * If userspace is debugging guest then guest
263 * can not access debug registers.
265 if (vcpu
->guest_debug
)
268 vcpu
->arch
.dbsr
&= ~spr_val
;
269 if (!(vcpu
->arch
.dbsr
& ~DBSR_IDE
))
270 kvmppc_core_dequeue_debug(vcpu
);
273 kvmppc_clr_tsr_bits(vcpu
, spr_val
);
277 * WRC is a 2-bit field that is supposed to preserve its
278 * value once written to non-zero.
280 if (vcpu
->arch
.tcr
& TCR_WRC_MASK
) {
281 spr_val
&= ~TCR_WRC_MASK
;
282 spr_val
|= vcpu
->arch
.tcr
& TCR_WRC_MASK
;
284 kvmppc_set_tcr(vcpu
, spr_val
);
288 vcpu
->arch
.decar
= spr_val
;
291 * Note: SPRG4-7 are user-readable.
292 * These values are loaded into the real SPRGs when resuming the
293 * guest (PR-mode only).
296 kvmppc_set_sprg4(vcpu
, spr_val
);
299 kvmppc_set_sprg5(vcpu
, spr_val
);
302 kvmppc_set_sprg6(vcpu
, spr_val
);
305 kvmppc_set_sprg7(vcpu
, spr_val
);
309 vcpu
->arch
.ivpr
= spr_val
;
310 #ifdef CONFIG_KVM_BOOKE_HV
311 mtspr(SPRN_GIVPR
, spr_val
);
315 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_CRITICAL
] = spr_val
;
318 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_MACHINE_CHECK
] = spr_val
;
321 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DATA_STORAGE
] = spr_val
;
322 #ifdef CONFIG_KVM_BOOKE_HV
323 mtspr(SPRN_GIVOR2
, spr_val
);
327 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_INST_STORAGE
] = spr_val
;
330 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_EXTERNAL
] = spr_val
;
333 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ALIGNMENT
] = spr_val
;
336 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_PROGRAM
] = spr_val
;
339 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FP_UNAVAIL
] = spr_val
;
342 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_SYSCALL
] = spr_val
;
343 #ifdef CONFIG_KVM_BOOKE_HV
344 mtspr(SPRN_GIVOR8
, spr_val
);
348 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_AP_UNAVAIL
] = spr_val
;
351 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DECREMENTER
] = spr_val
;
354 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FIT
] = spr_val
;
357 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_WATCHDOG
] = spr_val
;
360 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DTLB_MISS
] = spr_val
;
363 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ITLB_MISS
] = spr_val
;
366 vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DEBUG
] = spr_val
;
369 vcpu
->arch
.mcsr
&= ~spr_val
;
371 #if defined(CONFIG_64BIT)
373 kvmppc_set_epcr(vcpu
, spr_val
);
374 #ifdef CONFIG_KVM_BOOKE_HV
375 mtspr(SPRN_EPCR
, vcpu
->arch
.shadow_epcr
);
380 emulated
= EMULATE_FAIL
;
384 current
->thread
.debug
= vcpu
->arch
.dbg_reg
;
385 switch_booke_debug_regs(&vcpu
->arch
.dbg_reg
);
390 int kvmppc_booke_emulate_mfspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong
*spr_val
)
392 int emulated
= EMULATE_DONE
;
396 *spr_val
= vcpu
->arch
.ivpr
;
399 *spr_val
= vcpu
->arch
.shared
->dar
;
402 *spr_val
= vcpu
->arch
.shared
->esr
;
405 *spr_val
= vcpu
->arch
.epr
;
408 *spr_val
= vcpu
->arch
.csrr0
;
411 *spr_val
= vcpu
->arch
.csrr1
;
414 *spr_val
= vcpu
->arch
.dsrr0
;
417 *spr_val
= vcpu
->arch
.dsrr1
;
420 *spr_val
= vcpu
->arch
.dbg_reg
.iac1
;
423 *spr_val
= vcpu
->arch
.dbg_reg
.iac2
;
425 #if CONFIG_PPC_ADV_DEBUG_IACS > 2
427 *spr_val
= vcpu
->arch
.dbg_reg
.iac3
;
430 *spr_val
= vcpu
->arch
.dbg_reg
.iac4
;
434 *spr_val
= vcpu
->arch
.dbg_reg
.dac1
;
437 *spr_val
= vcpu
->arch
.dbg_reg
.dac2
;
440 *spr_val
= vcpu
->arch
.dbg_reg
.dbcr0
;
441 if (vcpu
->guest_debug
)
442 *spr_val
= *spr_val
| DBCR0_EDM
;
445 *spr_val
= vcpu
->arch
.dbg_reg
.dbcr1
;
448 *spr_val
= vcpu
->arch
.dbg_reg
.dbcr2
;
451 *spr_val
= vcpu
->arch
.dbsr
;
454 *spr_val
= vcpu
->arch
.tsr
;
457 *spr_val
= vcpu
->arch
.tcr
;
461 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_CRITICAL
];
464 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_MACHINE_CHECK
];
467 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DATA_STORAGE
];
470 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_INST_STORAGE
];
473 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_EXTERNAL
];
476 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ALIGNMENT
];
479 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_PROGRAM
];
482 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FP_UNAVAIL
];
485 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_SYSCALL
];
488 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_AP_UNAVAIL
];
491 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DECREMENTER
];
494 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_FIT
];
497 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_WATCHDOG
];
500 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DTLB_MISS
];
503 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_ITLB_MISS
];
506 *spr_val
= vcpu
->arch
.ivor
[BOOKE_IRQPRIO_DEBUG
];
509 *spr_val
= vcpu
->arch
.mcsr
;
511 #if defined(CONFIG_64BIT)
513 *spr_val
= vcpu
->arch
.epcr
;
518 emulated
= EMULATE_FAIL
;