| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 1 | /* | 
 | 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 Blanchard | fe4e771 | 2008-11-10 14:57:36 -0600 | [diff] [blame] | 24 | #include <asm/kvm_44x.h> | 
| Hollis Blanchard | 73e75b4 | 2008-12-02 15:51:57 -0600 | [diff] [blame] | 25 | #include "timing.h" | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 26 |  | 
 | 27 | #include "booke.h" | 
 | 28 | #include "44x_tlb.h" | 
 | 29 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 30 | #define XOP_MFDCR   323 | 
 | 31 | #define XOP_MTDCR   451 | 
 | 32 | #define XOP_TLBSX   914 | 
 | 33 | #define XOP_ICCCI   966 | 
 | 34 | #define XOP_TLBWE   978 | 
 | 35 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 36 | int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, | 
 | 37 |                            unsigned int inst, int *advance) | 
 | 38 | { | 
 | 39 | 	int emulated = EMULATE_DONE; | 
| Alexander Graf | c46dc9a | 2012-05-04 14:01:33 +0200 | [diff] [blame] | 40 | 	int dcrn = get_dcrn(inst); | 
 | 41 | 	int ra = get_ra(inst); | 
 | 42 | 	int rb = get_rb(inst); | 
 | 43 | 	int rc = get_rc(inst); | 
 | 44 | 	int rs = get_rs(inst); | 
 | 45 | 	int rt = get_rt(inst); | 
 | 46 | 	int ws = get_ws(inst); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 47 |  | 
 | 48 | 	switch (get_op(inst)) { | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 49 | 	case 31: | 
 | 50 | 		switch (get_xop(inst)) { | 
 | 51 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 52 | 		case XOP_MFDCR: | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 53 | 			/* The guest may access CPR0 registers to determine the timebase | 
 | 54 | 			 * frequency, and it must know the real host frequency because it | 
 | 55 | 			 * can directly access the timebase registers. | 
 | 56 | 			 * | 
 | 57 | 			 * It would be possible to emulate those accesses in userspace, | 
 | 58 | 			 * but userspace can really only figure out the end frequency. | 
 | 59 | 			 * We could decompose that into the factors that compute it, but | 
 | 60 | 			 * that's tricky math, and it's easier to just report the real | 
 | 61 | 			 * CPR0 values. | 
 | 62 | 			 */ | 
 | 63 | 			switch (dcrn) { | 
 | 64 | 			case DCRN_CPR0_CONFIG_ADDR: | 
| Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 65 | 				kvmppc_set_gpr(vcpu, rt, vcpu->arch.cpr0_cfgaddr); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 66 | 				break; | 
 | 67 | 			case DCRN_CPR0_CONFIG_DATA: | 
 | 68 | 				local_irq_disable(); | 
 | 69 | 				mtdcr(DCRN_CPR0_CONFIG_ADDR, | 
 | 70 | 					  vcpu->arch.cpr0_cfgaddr); | 
| Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 71 | 				kvmppc_set_gpr(vcpu, rt, | 
 | 72 | 					       mfdcr(DCRN_CPR0_CONFIG_DATA)); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 73 | 				local_irq_enable(); | 
 | 74 | 				break; | 
 | 75 | 			default: | 
 | 76 | 				run->dcr.dcrn = dcrn; | 
 | 77 | 				run->dcr.data =  0; | 
 | 78 | 				run->dcr.is_write = 0; | 
 | 79 | 				vcpu->arch.io_gpr = rt; | 
 | 80 | 				vcpu->arch.dcr_needed = 1; | 
| Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 81 | 				kvmppc_account_exit(vcpu, DCR_EXITS); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 82 | 				emulated = EMULATE_DO_DCR; | 
 | 83 | 			} | 
 | 84 |  | 
 | 85 | 			break; | 
 | 86 |  | 
 | 87 | 		case XOP_MTDCR: | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 88 | 			/* emulate some access in kernel */ | 
 | 89 | 			switch (dcrn) { | 
 | 90 | 			case DCRN_CPR0_CONFIG_ADDR: | 
| Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 91 | 				vcpu->arch.cpr0_cfgaddr = kvmppc_get_gpr(vcpu, rs); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 92 | 				break; | 
 | 93 | 			default: | 
 | 94 | 				run->dcr.dcrn = dcrn; | 
| Alexander Graf | 8e5b26b | 2010-01-08 02:58:01 +0100 | [diff] [blame] | 95 | 				run->dcr.data = kvmppc_get_gpr(vcpu, rs); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 96 | 				run->dcr.is_write = 1; | 
 | 97 | 				vcpu->arch.dcr_needed = 1; | 
| Hollis Blanchard | 7b70159 | 2008-12-02 15:51:58 -0600 | [diff] [blame] | 98 | 				kvmppc_account_exit(vcpu, DCR_EXITS); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 99 | 				emulated = EMULATE_DO_DCR; | 
 | 100 | 			} | 
 | 101 |  | 
 | 102 | 			break; | 
 | 103 |  | 
 | 104 | 		case XOP_TLBWE: | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 105 | 			emulated = kvmppc_44x_emul_tlbwe(vcpu, ra, rs, ws); | 
 | 106 | 			break; | 
 | 107 |  | 
 | 108 | 		case XOP_TLBSX: | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 109 | 			emulated = kvmppc_44x_emul_tlbsx(vcpu, rt, ra, rb, rc); | 
 | 110 | 			break; | 
 | 111 |  | 
 | 112 | 		case XOP_ICCCI: | 
 | 113 | 			break; | 
 | 114 |  | 
 | 115 | 		default: | 
 | 116 | 			emulated = EMULATE_FAIL; | 
 | 117 | 		} | 
 | 118 |  | 
 | 119 | 		break; | 
 | 120 |  | 
 | 121 | 	default: | 
 | 122 | 		emulated = EMULATE_FAIL; | 
 | 123 | 	} | 
 | 124 |  | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 125 | 	if (emulated == EMULATE_FAIL) | 
 | 126 | 		emulated = kvmppc_booke_emulate_op(run, vcpu, inst, advance); | 
 | 127 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 128 | 	return emulated; | 
 | 129 | } | 
 | 130 |  | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 131 | int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, ulong spr_val) | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 132 | { | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 133 | 	int emulated = EMULATE_DONE; | 
 | 134 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 135 | 	switch (sprn) { | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 136 | 	case SPRN_PID: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 137 | 		kvmppc_set_pid(vcpu, spr_val); break; | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 138 | 	case SPRN_MMUCR: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 139 | 		vcpu->arch.mmucr = spr_val; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 140 | 	case SPRN_CCR0: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 141 | 		vcpu->arch.ccr0 = spr_val; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 142 | 	case SPRN_CCR1: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 143 | 		vcpu->arch.ccr1 = spr_val; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 144 | 	default: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 145 | 		emulated = kvmppc_booke_emulate_mtspr(vcpu, sprn, spr_val); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 146 | 	} | 
 | 147 |  | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 148 | 	return emulated; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 149 | } | 
 | 150 |  | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 151 | int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, ulong *spr_val) | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 152 | { | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 153 | 	int emulated = EMULATE_DONE; | 
 | 154 |  | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 155 | 	switch (sprn) { | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 156 | 	case SPRN_PID: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 157 | 		*spr_val = vcpu->arch.pid; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 158 | 	case SPRN_MMUCR: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 159 | 		*spr_val = vcpu->arch.mmucr; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 160 | 	case SPRN_CCR0: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 161 | 		*spr_val = vcpu->arch.ccr0; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 162 | 	case SPRN_CCR1: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 163 | 		*spr_val = vcpu->arch.ccr1; break; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 164 | 	default: | 
| Alexander Graf | 54771e6 | 2012-05-04 14:55:12 +0200 | [diff] [blame] | 165 | 		emulated = kvmppc_booke_emulate_mfspr(vcpu, sprn, spr_val); | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 166 | 	} | 
 | 167 |  | 
| Hollis Blanchard | d0c7dc0 | 2009-01-03 16:23:06 -0600 | [diff] [blame] | 168 | 	return emulated; | 
| Hollis Blanchard | 75f74f0 | 2008-11-05 09:36:16 -0600 | [diff] [blame] | 169 | } | 
 | 170 |  |