| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License, version 2, as | 
					
						
							|  |  |  |  * published by the Free Software Foundation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright IBM Corp. 2007 | 
					
						
							| 
									
										
										
										
											2011-11-17 12:39:59 +00:00
										 |  |  |  * Copyright 2011 Freescale Semiconductor, Inc. | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Authors: Hollis Blanchard <hollisb@us.ibm.com> | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/jiffies.h>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 12:02:31 +00:00
										 |  |  | #include <linux/hrtimer.h>
 | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | #include <linux/types.h>
 | 
					
						
							|  |  |  | #include <linux/string.h>
 | 
					
						
							|  |  |  | #include <linux/kvm_host.h>
 | 
					
						
							| 
									
										
										
										
											2012-04-18 06:01:19 +00:00
										 |  |  | #include <linux/clockchips.h>
 | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 09:36:16 -06:00
										 |  |  | #include <asm/reg.h>
 | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | #include <asm/time.h>
 | 
					
						
							|  |  |  | #include <asm/byteorder.h>
 | 
					
						
							|  |  |  | #include <asm/kvm_ppc.h>
 | 
					
						
							| 
									
										
										
										
											2008-11-05 09:36:15 -06:00
										 |  |  | #include <asm/disassemble.h>
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:20:07 +08:00
										 |  |  | #include <asm/ppc-opcode.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-02 15:51:57 -06:00
										 |  |  | #include "timing.h"
 | 
					
						
							| 
									
										
										
										
											2009-06-18 11:47:27 -03:00
										 |  |  | #include "trace.h"
 | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-05 09:36:16 -06:00
										 |  |  | void kvmppc_emulate_dec(struct kvm_vcpu *vcpu) | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-02 12:02:31 +00:00
										 |  |  | 	unsigned long dec_nsec; | 
					
						
							| 
									
										
										
										
											2011-10-19 09:46:06 +05:30
										 |  |  | 	unsigned long long dec_time; | 
					
						
							| 
									
										
										
										
											2009-10-30 05:47:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 12:02:31 +00:00
										 |  |  | 	pr_debug("mtDEC: %x\n", vcpu->arch.dec); | 
					
						
							| 
									
										
										
										
											2011-11-17 12:39:59 +00:00
										 |  |  | 	hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-16 00:11:42 +02:00
										 |  |  | #ifdef CONFIG_PPC_BOOK3S
 | 
					
						
							| 
									
										
										
										
											2009-12-21 20:21:24 +01:00
										 |  |  | 	/* mtdec lowers the interrupt line when positive. */ | 
					
						
							|  |  |  | 	kvmppc_core_dequeue_dec(vcpu); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-30 05:47:16 +00:00
										 |  |  | 	/* POWER4+ triggers a dec interrupt if the value is < 0 */ | 
					
						
							|  |  |  | 	if (vcpu->arch.dec & 0x80000000) { | 
					
						
							|  |  |  | 		kvmppc_core_queue_dec(vcpu); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-11-17 12:39:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_BOOKE
 | 
					
						
							|  |  |  | 	/* On BOOKE, DEC = 0 is as good as decrementer not enabled */ | 
					
						
							|  |  |  | 	if (vcpu->arch.dec == 0) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * The decrementer ticks at the same rate as the timebase, so | 
					
						
							|  |  |  | 	 * that's how we convert the guest DEC value to the number of | 
					
						
							|  |  |  | 	 * host ticks. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dec_time = vcpu->arch.dec; | 
					
						
							| 
									
										
										
										
											2012-04-18 06:01:19 +00:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Guest timebase ticks at the same frequency as host decrementer. | 
					
						
							|  |  |  | 	 * So use the host decrementer calculations for decrementer emulation. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	dec_time = dec_time << decrementer_clockevent.shift; | 
					
						
							|  |  |  | 	do_div(dec_time, decrementer_clockevent.mult); | 
					
						
							| 
									
										
										
										
											2011-11-17 12:39:59 +00:00
										 |  |  | 	dec_nsec = do_div(dec_time, NSEC_PER_SEC); | 
					
						
							|  |  |  | 	hrtimer_start(&vcpu->arch.dec_timer, | 
					
						
							|  |  |  | 		ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL); | 
					
						
							|  |  |  | 	vcpu->arch.dec_jiffies = get_tb(); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-27 17:24:21 -05:00
										 |  |  | u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u64 jd = tb - vcpu->arch.dec_jiffies; | 
					
						
							| 
									
										
										
										
											2011-11-17 12:39:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_BOOKE
 | 
					
						
							|  |  |  | 	if (vcpu->arch.dec < jd) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-04-27 17:24:21 -05:00
										 |  |  | 	return vcpu->arch.dec - jd; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-06 23:19:01 +02:00
										 |  |  | static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	enum emulation_result emulated = EMULATE_DONE; | 
					
						
							|  |  |  | 	ulong spr_val = kvmppc_get_gpr(vcpu, rs); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (sprn) { | 
					
						
							|  |  |  | 	case SPRN_SRR0: | 
					
						
							|  |  |  | 		vcpu->arch.shared->srr0 = spr_val; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_SRR1: | 
					
						
							|  |  |  | 		vcpu->arch.shared->srr1 = spr_val; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* XXX We need to context-switch the timebase for
 | 
					
						
							|  |  |  | 	 * watchdog and FIT. */ | 
					
						
							|  |  |  | 	case SPRN_TBWL: break; | 
					
						
							|  |  |  | 	case SPRN_TBWU: break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case SPRN_DEC: | 
					
						
							|  |  |  | 		vcpu->arch.dec = spr_val; | 
					
						
							|  |  |  | 		kvmppc_emulate_dec(vcpu); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case SPRN_SPRG0: | 
					
						
							|  |  |  | 		vcpu->arch.shared->sprg0 = spr_val; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_SPRG1: | 
					
						
							|  |  |  | 		vcpu->arch.shared->sprg1 = spr_val; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_SPRG2: | 
					
						
							|  |  |  | 		vcpu->arch.shared->sprg2 = spr_val; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_SPRG3: | 
					
						
							|  |  |  | 		vcpu->arch.shared->sprg3 = spr_val; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-27 01:07:15 +02:00
										 |  |  | 	/* PIR can legally be written, but we ignore it */ | 
					
						
							|  |  |  | 	case SPRN_PIR: break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-06 23:19:01 +02:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2013-10-07 22:18:01 +05:30
										 |  |  | 		emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn, | 
					
						
							|  |  |  | 								  spr_val); | 
					
						
							| 
									
										
										
										
											2012-10-06 23:19:01 +02:00
										 |  |  | 		if (emulated == EMULATE_FAIL) | 
					
						
							|  |  |  | 			printk(KERN_INFO "mtspr: unknown spr " | 
					
						
							|  |  |  | 				"0x%x\n", sprn); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return emulated; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	enum emulation_result emulated = EMULATE_DONE; | 
					
						
							|  |  |  | 	ulong spr_val = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (sprn) { | 
					
						
							|  |  |  | 	case SPRN_SRR0: | 
					
						
							|  |  |  | 		spr_val = vcpu->arch.shared->srr0; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_SRR1: | 
					
						
							|  |  |  | 		spr_val = vcpu->arch.shared->srr1; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_PVR: | 
					
						
							|  |  |  | 		spr_val = vcpu->arch.pvr; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_PIR: | 
					
						
							|  |  |  | 		spr_val = vcpu->vcpu_id; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Note: mftb and TBRL/TBWL are user-accessible, so
 | 
					
						
							|  |  |  | 	 * the guest can always access the real TB anyways. | 
					
						
							|  |  |  | 	 * In fact, we probably will never see these traps. */ | 
					
						
							|  |  |  | 	case SPRN_TBWL: | 
					
						
							|  |  |  | 		spr_val = get_tb() >> 32; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_TBWU: | 
					
						
							|  |  |  | 		spr_val = get_tb(); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case SPRN_SPRG0: | 
					
						
							|  |  |  | 		spr_val = vcpu->arch.shared->sprg0; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_SPRG1: | 
					
						
							|  |  |  | 		spr_val = vcpu->arch.shared->sprg1; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_SPRG2: | 
					
						
							|  |  |  | 		spr_val = vcpu->arch.shared->sprg2; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case SPRN_SPRG3: | 
					
						
							|  |  |  | 		spr_val = vcpu->arch.shared->sprg3; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	/* Note: SPRG4-7 are user-readable, so we don't get
 | 
					
						
							|  |  |  | 	 * a trap. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case SPRN_DEC: | 
					
						
							|  |  |  | 		spr_val = kvmppc_get_dec(vcpu, get_tb()); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2013-10-07 22:18:01 +05:30
										 |  |  | 		emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn, | 
					
						
							|  |  |  | 								  &spr_val); | 
					
						
							| 
									
										
										
										
											2012-10-06 23:19:01 +02:00
										 |  |  | 		if (unlikely(emulated == EMULATE_FAIL)) { | 
					
						
							|  |  |  | 			printk(KERN_INFO "mfspr: unknown spr " | 
					
						
							|  |  |  | 				"0x%x\n", sprn); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (emulated == EMULATE_DONE) | 
					
						
							|  |  |  | 		kvmppc_set_gpr(vcpu, rt, spr_val); | 
					
						
							|  |  |  | 	kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return emulated; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | /* XXX to do:
 | 
					
						
							|  |  |  |  * lhax | 
					
						
							|  |  |  |  * lhaux | 
					
						
							|  |  |  |  * lswx | 
					
						
							|  |  |  |  * lswi | 
					
						
							|  |  |  |  * stswx | 
					
						
							|  |  |  |  * stswi | 
					
						
							|  |  |  |  * lha | 
					
						
							|  |  |  |  * lhau | 
					
						
							|  |  |  |  * lmw | 
					
						
							|  |  |  |  * stmw | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * XXX is_bigendian should depend on MMU mapping or MSR[LE] | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2008-11-05 09:36:16 -06:00
										 |  |  | /* XXX Should probably auto-generate instruction decoding for a particular core
 | 
					
						
							|  |  |  |  * from opcode tables in the future. */ | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-04-16 00:11:40 +02:00
										 |  |  | 	u32 inst = kvmppc_get_last_inst(vcpu); | 
					
						
							| 
									
										
										
										
											2012-05-04 14:01:33 +02:00
										 |  |  | 	int ra = get_ra(inst); | 
					
						
							|  |  |  | 	int rs = get_rs(inst); | 
					
						
							|  |  |  | 	int rt = get_rt(inst); | 
					
						
							|  |  |  | 	int sprn = get_sprn(inst); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 	enum emulation_result emulated = EMULATE_DONE; | 
					
						
							|  |  |  | 	int advance = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-02 15:51:57 -06:00
										 |  |  | 	/* this default type might be overwritten by subcategories */ | 
					
						
							|  |  |  | 	kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-11 19:10:53 +00:00
										 |  |  | 	pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst)); | 
					
						
							| 
									
										
										
										
											2009-10-30 05:47:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 	switch (get_op(inst)) { | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_TRAP: | 
					
						
							| 
									
										
										
										
											2010-04-16 00:11:42 +02:00
										 |  |  | #ifdef CONFIG_PPC_BOOK3S
 | 
					
						
							| 
									
										
										
										
											2009-10-30 05:47:16 +00:00
										 |  |  | 	case OP_TRAP_64: | 
					
						
							| 
									
										
										
										
											2010-02-02 19:44:35 +08:00
										 |  |  | 		kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP); | 
					
						
							| 
									
										
										
										
											2009-10-30 05:47:16 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
											
												KVM: PPC: Paravirtualize SPRG4-7, ESR, PIR, MASn
This allows additional registers to be accessed by the guest
in PR-mode KVM without trapping.
SPRG4-7 are readable from userspace.  On booke, KVM will sync
these registers when it enters the guest, so that accesses from
guest userspace will work.  The guest kernel, OTOH, must consistently
use either the real registers or the shared area between exits.  This
also applies to the already-paravirted SPRG3.
On non-booke, it's not clear to what extent SPRG4-7 are supported
(they're not architected for book3s, but exist on at least some classic
chips).  They are copied in the get/set regs ioctls, but I do not see any
non-booke emulation.  I also do not see any syncing with real registers
(in PR-mode) including the user-readable SPRG3.  This patch should not
make that situation any worse.
Signed-off-by: Scott Wood <scottwood@freescale.com>
Signed-off-by: Alexander Graf <agraf@suse.de>
Signed-off-by: Avi Kivity <avi@redhat.com>
											
										 
											2011-11-08 18:23:30 -06:00
										 |  |  | 		kvmppc_core_queue_program(vcpu, | 
					
						
							|  |  |  | 					  vcpu->arch.shared->esr | ESR_PTR); | 
					
						
							| 
									
										
										
										
											2009-10-30 05:47:16 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		advance = 0; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case 31: | 
					
						
							|  |  |  | 		switch (get_xop(inst)) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-13 22:15:45 +01:00
										 |  |  | 		case OP_31_XOP_TRAP: | 
					
						
							|  |  |  | #ifdef CONFIG_64BIT
 | 
					
						
							|  |  |  | 		case OP_31_XOP_TRAP_64: | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef CONFIG_PPC_BOOK3S
 | 
					
						
							|  |  |  | 			kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 			kvmppc_core_queue_program(vcpu, | 
					
						
							|  |  |  | 					vcpu->arch.shared->esr | ESR_PTR); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 			advance = 0; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_LWZX: | 
					
						
							| 
									
										
										
										
											2008-05-21 18:22:52 -05:00
										 |  |  | 			emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_LBZX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-24 21:48:27 +01:00
										 |  |  | 		case OP_31_XOP_LBZUX: | 
					
						
							|  |  |  | 			emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2010-03-24 21:48:27 +01:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_STWX: | 
					
						
							| 
									
										
										
										
											2008-05-21 18:22:52 -05:00
										 |  |  | 			emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 						       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-05-21 18:22:52 -05:00
										 |  |  | 			                               4, 1); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_STBX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 						       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			                               1, 1); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_STBUX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 						       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			                               1, 1); | 
					
						
							| 
									
										
										
										
											2012-04-27 01:00:17 +02:00
										 |  |  | 			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-24 21:48:27 +01:00
										 |  |  | 		case OP_31_XOP_LHAX: | 
					
						
							|  |  |  | 			emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_LHZX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_LHZUX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_MFSPR: | 
					
						
							| 
									
										
										
										
											2012-10-06 23:19:01 +02:00
										 |  |  | 			emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_STHX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 						       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			                               2, 1); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_STHUX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 						       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			                               2, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 			kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_MTSPR: | 
					
						
							| 
									
										
										
										
											2012-10-06 23:19:01 +02:00
										 |  |  | 			emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-09 10:36:23 +00:00
										 |  |  | 		case OP_31_XOP_DCBST: | 
					
						
							| 
									
										
										
										
											2013-01-17 13:50:25 +01:00
										 |  |  | 		case OP_31_XOP_DCBF: | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_DCBI: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			/* Do nothing. The guest is performing dcbi because
 | 
					
						
							|  |  |  | 			 * hardware DMA is not snooped by the dcache, but | 
					
						
							|  |  |  | 			 * emulated DMA either goes through the dcache as | 
					
						
							|  |  |  | 			 * normal writes, or the host kernel has handled dcache | 
					
						
							|  |  |  | 			 * coherence. */ | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_LWBRX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_TLBSYNC: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_STWBRX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 						       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			                               4, 0); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_LHBRX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 		case OP_31_XOP_STHBRX: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 						       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			                               2, 0); | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2008-11-05 09:36:16 -06:00
										 |  |  | 			/* Attempt core-specific emulation below. */ | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 			emulated = EMULATE_FAIL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_LWZ: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-18 12:14:55 +00:00
										 |  |  | 	/* TBD: Add support for other 64 bit load variants like ldu, ldux, ldx etc. */ | 
					
						
							|  |  |  | 	case OP_LD: | 
					
						
							|  |  |  | 		rt = get_rt(inst); | 
					
						
							|  |  |  | 		emulated = kvmppc_handle_load(run, vcpu, rt, 8, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_LWZU: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_LBZ: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_LBZU: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_STW: | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 		emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							|  |  |  | 					       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		                               4, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-18 12:14:55 +00:00
										 |  |  | 	/* TBD: Add support for other 64 bit store variants like stdu, stdux, stdx etc. */ | 
					
						
							|  |  |  | 	case OP_STD: | 
					
						
							|  |  |  | 		rs = get_rs(inst); | 
					
						
							|  |  |  | 		emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							|  |  |  | 					       kvmppc_get_gpr(vcpu, rs), | 
					
						
							|  |  |  | 		                               8, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_STWU: | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 		emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							|  |  |  | 					       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		                               4, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_STB: | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 		emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							|  |  |  | 					       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		                               1, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_STBU: | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 		emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							|  |  |  | 					       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		                               1, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_LHZ: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_LHZU: | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-19 11:00:30 +01:00
										 |  |  | 	case OP_LHA: | 
					
						
							|  |  |  | 		emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case OP_LHAU: | 
					
						
							|  |  |  | 		emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2010-02-19 11:00:30 +01:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_STH: | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 		emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							|  |  |  | 					       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		                               2, 1); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-03 16:23:05 -06:00
										 |  |  | 	case OP_STHU: | 
					
						
							| 
									
										
										
										
											2010-01-08 02:58:01 +01:00
										 |  |  | 		emulated = kvmppc_handle_store(run, vcpu, | 
					
						
							|  |  |  | 					       kvmppc_get_gpr(vcpu, rs), | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		                               2, 1); | 
					
						
							| 
									
										
										
										
											2012-03-12 02:26:30 +01:00
										 |  |  | 		kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		emulated = EMULATE_FAIL; | 
					
						
							| 
									
										
										
										
											2008-11-05 09:36:16 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (emulated == EMULATE_FAIL) { | 
					
						
							| 
									
										
										
										
											2013-10-07 22:18:01 +05:30
										 |  |  | 		emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst, | 
					
						
							|  |  |  | 							       &advance); | 
					
						
							| 
									
										
										
										
											2010-02-19 11:00:31 +01:00
										 |  |  | 		if (emulated == EMULATE_AGAIN) { | 
					
						
							|  |  |  | 			advance = 0; | 
					
						
							|  |  |  | 		} else if (emulated == EMULATE_FAIL) { | 
					
						
							| 
									
										
										
										
											2008-11-05 09:36:16 -06:00
										 |  |  | 			advance = 0; | 
					
						
							|  |  |  | 			printk(KERN_ERR "Couldn't emulate instruction 0x%08x " | 
					
						
							|  |  |  | 			       "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst)); | 
					
						
							| 
									
										
										
										
											2010-01-10 03:27:32 +01:00
										 |  |  | 			kvmppc_core_queue_program(vcpu, 0); | 
					
						
							| 
									
										
										
										
											2008-11-05 09:36:16 -06:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-16 00:11:40 +02:00
										 |  |  | 	trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated); | 
					
						
							| 
									
										
										
										
											2008-07-14 14:00:04 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-16 00:11:40 +02:00
										 |  |  | 	/* Advance past emulated instruction. */ | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 	if (advance) | 
					
						
							| 
									
										
										
										
											2010-04-16 00:11:40 +02:00
										 |  |  | 		kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4); | 
					
						
							| 
									
										
										
										
											2008-04-16 23:28:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return emulated; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-10-07 22:17:59 +05:30
										 |  |  | EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction); |