blob: fa888bfc347e6e6e10055cd7b2e36a6c5c4ecf2b [file] [log] [blame]
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -05001/*
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. 2007
Scott Wooddfd4d472011-11-17 12:39:59 +000016 * Copyright 2011 Freescale Semiconductor, Inc.
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050017 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 */
20
21#include <linux/jiffies.h>
Alexander Graf544c6762009-11-02 12:02:31 +000022#include <linux/hrtimer.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050023#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kvm_host.h>
Bharat Bhushan6e359942012-04-18 06:01:19 +000026#include <linux/clockchips.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050027
Hollis Blanchard75f74f02008-11-05 09:36:16 -060028#include <asm/reg.h>
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050029#include <asm/time.h>
30#include <asm/byteorder.h>
31#include <asm/kvm_ppc.h>
Hollis Blanchardc381a042008-11-05 09:36:15 -060032#include <asm/disassemble.h>
Hongtao Jia9123c5e2013-04-28 13:20:07 +080033#include <asm/ppc-opcode.h>
Hollis Blanchard73e75b42008-12-02 15:51:57 -060034#include "timing.h"
Marcelo Tosatti46f43c62009-06-18 11:47:27 -030035#include "trace.h"
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050036
Hollis Blanchard75f74f02008-11-05 09:36:16 -060037void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050038{
Alexander Graf544c6762009-11-02 12:02:31 +000039 unsigned long dec_nsec;
Bharat Bhushandc2babfe2011-10-19 09:46:06 +053040 unsigned long long dec_time;
Alexander Graf9a7a9b02009-10-30 05:47:15 +000041
Paul Mackerras1bc3fe82017-05-22 16:55:16 +100042 pr_debug("mtDEC: %lx\n", vcpu->arch.dec);
Scott Wooddfd4d472011-11-17 12:39:59 +000043 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
44
Alexander Graf00c3a372010-04-16 00:11:42 +020045#ifdef CONFIG_PPC_BOOK3S
Alexander Graf7706664d2009-12-21 20:21:24 +010046 /* mtdec lowers the interrupt line when positive. */
47 kvmppc_core_dequeue_dec(vcpu);
Alexander Graf513579e2009-10-30 05:47:16 +000048#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050049
Scott Wooddfd4d472011-11-17 12:39:59 +000050#ifdef CONFIG_BOOKE
51 /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
52 if (vcpu->arch.dec == 0)
53 return;
54#endif
55
56 /*
57 * The decrementer ticks at the same rate as the timebase, so
58 * that's how we convert the guest DEC value to the number of
59 * host ticks.
60 */
61
62 dec_time = vcpu->arch.dec;
Bharat Bhushan6e359942012-04-18 06:01:19 +000063 /*
64 * Guest timebase ticks at the same frequency as host decrementer.
65 * So use the host decrementer calculations for decrementer emulation.
66 */
67 dec_time = dec_time << decrementer_clockevent.shift;
68 do_div(dec_time, decrementer_clockevent.mult);
Scott Wooddfd4d472011-11-17 12:39:59 +000069 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
70 hrtimer_start(&vcpu->arch.dec_timer,
71 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
72 vcpu->arch.dec_jiffies = get_tb();
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -050073}
74
Scott Wood5ce941e2011-04-27 17:24:21 -050075u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
76{
77 u64 jd = tb - vcpu->arch.dec_jiffies;
Scott Wooddfd4d472011-11-17 12:39:59 +000078
79#ifdef CONFIG_BOOKE
80 if (vcpu->arch.dec < jd)
81 return 0;
82#endif
83
Scott Wood5ce941e2011-04-27 17:24:21 -050084 return vcpu->arch.dec - jd;
85}
86
Alexander Graf388cf9e2012-10-06 23:19:01 +020087static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
88{
89 enum emulation_result emulated = EMULATE_DONE;
90 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
91
92 switch (sprn) {
93 case SPRN_SRR0:
Alexander Graf5deb8e72014-04-24 13:46:24 +020094 kvmppc_set_srr0(vcpu, spr_val);
Alexander Graf388cf9e2012-10-06 23:19:01 +020095 break;
96 case SPRN_SRR1:
Alexander Graf5deb8e72014-04-24 13:46:24 +020097 kvmppc_set_srr1(vcpu, spr_val);
Alexander Graf388cf9e2012-10-06 23:19:01 +020098 break;
99
100 /* XXX We need to context-switch the timebase for
101 * watchdog and FIT. */
102 case SPRN_TBWL: break;
103 case SPRN_TBWU: break;
104
Alexander Graf388cf9e2012-10-06 23:19:01 +0200105 case SPRN_DEC:
Paul Mackerras1bc3fe82017-05-22 16:55:16 +1000106 vcpu->arch.dec = (u32) spr_val;
Alexander Graf388cf9e2012-10-06 23:19:01 +0200107 kvmppc_emulate_dec(vcpu);
108 break;
109
110 case SPRN_SPRG0:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200111 kvmppc_set_sprg0(vcpu, spr_val);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200112 break;
113 case SPRN_SPRG1:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200114 kvmppc_set_sprg1(vcpu, spr_val);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200115 break;
116 case SPRN_SPRG2:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200117 kvmppc_set_sprg2(vcpu, spr_val);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200118 break;
119 case SPRN_SPRG3:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200120 kvmppc_set_sprg3(vcpu, spr_val);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200121 break;
122
Alexander Grafa3ff5fb2013-06-27 01:07:15 +0200123 /* PIR can legally be written, but we ignore it */
124 case SPRN_PIR: break;
125
Alexander Graf388cf9e2012-10-06 23:19:01 +0200126 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530127 emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
128 spr_val);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200129 if (emulated == EMULATE_FAIL)
130 printk(KERN_INFO "mtspr: unknown spr "
131 "0x%x\n", sprn);
132 break;
133 }
134
135 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
136
137 return emulated;
138}
139
140static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
141{
142 enum emulation_result emulated = EMULATE_DONE;
143 ulong spr_val = 0;
144
145 switch (sprn) {
146 case SPRN_SRR0:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200147 spr_val = kvmppc_get_srr0(vcpu);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200148 break;
149 case SPRN_SRR1:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200150 spr_val = kvmppc_get_srr1(vcpu);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200151 break;
152 case SPRN_PVR:
153 spr_val = vcpu->arch.pvr;
154 break;
155 case SPRN_PIR:
156 spr_val = vcpu->vcpu_id;
157 break;
Alexander Graf388cf9e2012-10-06 23:19:01 +0200158
159 /* Note: mftb and TBRL/TBWL are user-accessible, so
160 * the guest can always access the real TB anyways.
161 * In fact, we probably will never see these traps. */
162 case SPRN_TBWL:
163 spr_val = get_tb() >> 32;
164 break;
165 case SPRN_TBWU:
166 spr_val = get_tb();
167 break;
168
169 case SPRN_SPRG0:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200170 spr_val = kvmppc_get_sprg0(vcpu);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200171 break;
172 case SPRN_SPRG1:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200173 spr_val = kvmppc_get_sprg1(vcpu);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200174 break;
175 case SPRN_SPRG2:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200176 spr_val = kvmppc_get_sprg2(vcpu);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200177 break;
178 case SPRN_SPRG3:
Alexander Graf5deb8e72014-04-24 13:46:24 +0200179 spr_val = kvmppc_get_sprg3(vcpu);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200180 break;
181 /* Note: SPRG4-7 are user-readable, so we don't get
182 * a trap. */
183
184 case SPRN_DEC:
185 spr_val = kvmppc_get_dec(vcpu, get_tb());
186 break;
187 default:
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530188 emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
189 &spr_val);
Alexander Graf388cf9e2012-10-06 23:19:01 +0200190 if (unlikely(emulated == EMULATE_FAIL)) {
191 printk(KERN_INFO "mfspr: unknown spr "
192 "0x%x\n", sprn);
193 }
194 break;
195 }
196
197 if (emulated == EMULATE_DONE)
198 kvmppc_set_gpr(vcpu, rt, spr_val);
199 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
200
201 return emulated;
202}
203
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600204/* XXX Should probably auto-generate instruction decoding for a particular core
205 * from opcode tables in the future. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500206int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
207{
Mihai Caraman51f04722014-07-23 19:06:21 +0300208 u32 inst;
Alexander Grafd69614a2014-06-18 14:53:49 +0200209 int rs, rt, sprn;
Mihai Caraman51f04722014-07-23 19:06:21 +0300210 enum emulation_result emulated;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500211 int advance = 1;
212
Hollis Blanchard73e75b42008-12-02 15:51:57 -0600213 /* this default type might be overwritten by subcategories */
214 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
215
Alexander Graf8d0eff62014-09-10 14:37:29 +0200216 emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst);
Mihai Caraman51f04722014-07-23 19:06:21 +0300217 if (emulated != EMULATE_DONE)
218 return emulated;
219
Joe Perches689fd142010-09-11 19:10:53 +0000220 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
Alexander Graf513579e2009-10-30 05:47:16 +0000221
Mihai Caraman51f04722014-07-23 19:06:21 +0300222 rs = get_rs(inst);
223 rt = get_rt(inst);
224 sprn = get_sprn(inst);
225
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500226 switch (get_op(inst)) {
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600227 case OP_TRAP:
Alexander Graf00c3a372010-04-16 00:11:42 +0200228#ifdef CONFIG_PPC_BOOK3S
Alexander Graf513579e2009-10-30 05:47:16 +0000229 case OP_TRAP_64:
Alexander Graf25a8a022010-01-08 02:58:07 +0100230 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
Liu Yudaf5e272010-02-02 19:44:35 +0800231#else
Scott Woodb5904972011-11-08 18:23:30 -0600232 kvmppc_core_queue_program(vcpu,
233 vcpu->arch.shared->esr | ESR_PTR);
Liu Yudaf5e272010-02-02 19:44:35 +0800234#endif
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500235 advance = 0;
236 break;
237
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500238 case 31:
239 switch (get_xop(inst)) {
240
Alexander Graf6df79df2012-03-13 22:15:45 +0100241 case OP_31_XOP_TRAP:
242#ifdef CONFIG_64BIT
243 case OP_31_XOP_TRAP_64:
244#endif
245#ifdef CONFIG_PPC_BOOK3S
246 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
247#else
248 kvmppc_core_queue_program(vcpu,
249 vcpu->arch.shared->esr | ESR_PTR);
250#endif
251 advance = 0;
252 break;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500253
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600254 case OP_31_XOP_MFSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200255 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
Thomas Huthfeafd132017-04-05 15:58:51 +0200256 if (emulated == EMULATE_AGAIN) {
257 emulated = EMULATE_DONE;
258 advance = 0;
259 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500260 break;
261
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600262 case OP_31_XOP_MTSPR:
Alexander Graf388cf9e2012-10-06 23:19:01 +0200263 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
Thomas Huthfeafd132017-04-05 15:58:51 +0200264 if (emulated == EMULATE_AGAIN) {
265 emulated = EMULATE_DONE;
266 advance = 0;
267 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500268 break;
269
Hollis Blanchardcea5d8c2009-01-03 16:23:05 -0600270 case OP_31_XOP_TLBSYNC:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500271 break;
272
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500273 default:
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600274 /* Attempt core-specific emulation below. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500275 emulated = EMULATE_FAIL;
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500276 }
277 break;
278
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530279 case 0:
280 /*
281 * Instruction with primary opcode 0. Based on PowerISA
282 * these are illegal instructions.
283 */
284 if (inst == KVMPPC_INST_SW_BREAKPOINT) {
285 run->exit_reason = KVM_EXIT_DEBUG;
286 run->debug.arch.address = kvmppc_get_pc(vcpu);
287 emulated = EMULATE_EXIT_USER;
288 advance = 0;
289 } else
290 emulated = EMULATE_FAIL;
291
292 break;
293
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500294 default:
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500295 emulated = EMULATE_FAIL;
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600296 }
297
298 if (emulated == EMULATE_FAIL) {
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +0530299 emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
300 &advance);
Alexander Graf37f5bca2010-02-19 11:00:31 +0100301 if (emulated == EMULATE_AGAIN) {
302 advance = 0;
303 } else if (emulated == EMULATE_FAIL) {
Hollis Blanchard75f74f02008-11-05 09:36:16 -0600304 advance = 0;
305 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
306 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
307 }
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500308 }
309
Alexander Grafc7f38f42010-04-16 00:11:40 +0200310 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
Christian Ehrhardt3b4bd792008-07-14 14:00:04 +0200311
Alexander Grafc7f38f42010-04-16 00:11:40 +0200312 /* Advance past emulated instruction. */
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500313 if (advance)
Alexander Grafc7f38f42010-04-16 00:11:40 +0200314 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
Hollis Blanchardbbf45ba2008-04-16 23:28:09 -0500315
316 return emulated;
317}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530318EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);