Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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. 2007
16 * Copyright 2011 Freescale Semiconductor, Inc.
17 *
18 * Authors: Hollis Blanchard <hollisb@us.ibm.com>
19 */
20
21#include <linux/jiffies.h>
22#include <linux/hrtimer.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/kvm_host.h>
26#include <linux/clockchips.h>
27
28#include <asm/reg.h>
29#include <asm/time.h>
30#include <asm/byteorder.h>
31#include <asm/kvm_ppc.h>
32#include <asm/disassemble.h>
33#include <asm/ppc-opcode.h>
34#include "timing.h"
35#include "trace.h"
36
37void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
38{
39 unsigned long dec_nsec;
40 unsigned long long dec_time;
41
42 pr_debug("mtDEC: %lx\n", vcpu->arch.dec);
43 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
44
45#ifdef CONFIG_PPC_BOOK3S
46 /* mtdec lowers the interrupt line when positive. */
47 kvmppc_core_dequeue_dec(vcpu);
48#endif
49
50#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;
63 /*
64 * Guest timebase ticks at the same frequency as host timebase.
65 * So use the host timebase calculations for decrementer emulation.
66 */
67 dec_time = tb_to_ns(dec_time);
68 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
69 hrtimer_start(&vcpu->arch.dec_timer,
70 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
71 vcpu->arch.dec_jiffies = get_tb();
72}
73
74u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
75{
76 u64 jd = tb - vcpu->arch.dec_jiffies;
77
78#ifdef CONFIG_BOOKE
79 if (vcpu->arch.dec < jd)
80 return 0;
81#endif
82
83 return vcpu->arch.dec - jd;
84}
85
86static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
87{
88 enum emulation_result emulated = EMULATE_DONE;
89 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
90
91 switch (sprn) {
92 case SPRN_SRR0:
93 kvmppc_set_srr0(vcpu, spr_val);
94 break;
95 case SPRN_SRR1:
96 kvmppc_set_srr1(vcpu, spr_val);
97 break;
98
99 /* XXX We need to context-switch the timebase for
100 * watchdog and FIT. */
101 case SPRN_TBWL: break;
102 case SPRN_TBWU: break;
103
104 case SPRN_DEC:
105 vcpu->arch.dec = (u32) spr_val;
106 kvmppc_emulate_dec(vcpu);
107 break;
108
109 case SPRN_SPRG0:
110 kvmppc_set_sprg0(vcpu, spr_val);
111 break;
112 case SPRN_SPRG1:
113 kvmppc_set_sprg1(vcpu, spr_val);
114 break;
115 case SPRN_SPRG2:
116 kvmppc_set_sprg2(vcpu, spr_val);
117 break;
118 case SPRN_SPRG3:
119 kvmppc_set_sprg3(vcpu, spr_val);
120 break;
121
122 /* PIR can legally be written, but we ignore it */
123 case SPRN_PIR: break;
124
125 default:
126 emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
127 spr_val);
128 if (emulated == EMULATE_FAIL)
129 printk(KERN_INFO "mtspr: unknown spr "
130 "0x%x\n", sprn);
131 break;
132 }
133
134 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
135
136 return emulated;
137}
138
139static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
140{
141 enum emulation_result emulated = EMULATE_DONE;
142 ulong spr_val = 0;
143
144 switch (sprn) {
145 case SPRN_SRR0:
146 spr_val = kvmppc_get_srr0(vcpu);
147 break;
148 case SPRN_SRR1:
149 spr_val = kvmppc_get_srr1(vcpu);
150 break;
151 case SPRN_PVR:
152 spr_val = vcpu->arch.pvr;
153 break;
154 case SPRN_PIR:
155 spr_val = vcpu->vcpu_id;
156 break;
157
158 /* Note: mftb and TBRL/TBWL are user-accessible, so
159 * the guest can always access the real TB anyways.
160 * In fact, we probably will never see these traps. */
161 case SPRN_TBWL:
162 spr_val = get_tb() >> 32;
163 break;
164 case SPRN_TBWU:
165 spr_val = get_tb();
166 break;
167
168 case SPRN_SPRG0:
169 spr_val = kvmppc_get_sprg0(vcpu);
170 break;
171 case SPRN_SPRG1:
172 spr_val = kvmppc_get_sprg1(vcpu);
173 break;
174 case SPRN_SPRG2:
175 spr_val = kvmppc_get_sprg2(vcpu);
176 break;
177 case SPRN_SPRG3:
178 spr_val = kvmppc_get_sprg3(vcpu);
179 break;
180 /* Note: SPRG4-7 are user-readable, so we don't get
181 * a trap. */
182
183 case SPRN_DEC:
184 spr_val = kvmppc_get_dec(vcpu, get_tb());
185 break;
186 default:
187 emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
188 &spr_val);
189 if (unlikely(emulated == EMULATE_FAIL)) {
190 printk(KERN_INFO "mfspr: unknown spr "
191 "0x%x\n", sprn);
192 }
193 break;
194 }
195
196 if (emulated == EMULATE_DONE)
197 kvmppc_set_gpr(vcpu, rt, spr_val);
198 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
199
200 return emulated;
201}
202
203/* XXX Should probably auto-generate instruction decoding for a particular core
204 * from opcode tables in the future. */
205int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
206{
207 u32 inst;
208 int rs, rt, sprn;
209 enum emulation_result emulated;
210 int advance = 1;
211
212 /* this default type might be overwritten by subcategories */
213 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
214
215 emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst);
216 if (emulated != EMULATE_DONE)
217 return emulated;
218
219 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
220
221 rs = get_rs(inst);
222 rt = get_rt(inst);
223 sprn = get_sprn(inst);
224
225 switch (get_op(inst)) {
226 case OP_TRAP:
227#ifdef CONFIG_PPC_BOOK3S
228 case OP_TRAP_64:
229 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
230#else
231 kvmppc_core_queue_program(vcpu,
232 vcpu->arch.shared->esr | ESR_PTR);
233#endif
234 advance = 0;
235 break;
236
237 case 31:
238 switch (get_xop(inst)) {
239
240 case OP_31_XOP_TRAP:
241#ifdef CONFIG_64BIT
242 case OP_31_XOP_TRAP_64:
243#endif
244#ifdef CONFIG_PPC_BOOK3S
245 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
246#else
247 kvmppc_core_queue_program(vcpu,
248 vcpu->arch.shared->esr | ESR_PTR);
249#endif
250 advance = 0;
251 break;
252
253 case OP_31_XOP_MFSPR:
254 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
255 if (emulated == EMULATE_AGAIN) {
256 emulated = EMULATE_DONE;
257 advance = 0;
258 }
259 break;
260
261 case OP_31_XOP_MTSPR:
262 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
263 if (emulated == EMULATE_AGAIN) {
264 emulated = EMULATE_DONE;
265 advance = 0;
266 }
267 break;
268
269 case OP_31_XOP_TLBSYNC:
270 break;
271
272 default:
273 /* Attempt core-specific emulation below. */
274 emulated = EMULATE_FAIL;
275 }
276 break;
277
278 case 0:
279 /*
280 * Instruction with primary opcode 0. Based on PowerISA
281 * these are illegal instructions.
282 */
283 if (inst == KVMPPC_INST_SW_BREAKPOINT) {
284 run->exit_reason = KVM_EXIT_DEBUG;
285 run->debug.arch.address = kvmppc_get_pc(vcpu);
286 emulated = EMULATE_EXIT_USER;
287 advance = 0;
288 } else
289 emulated = EMULATE_FAIL;
290
291 break;
292
293 default:
294 emulated = EMULATE_FAIL;
295 }
296
297 if (emulated == EMULATE_FAIL) {
298 emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
299 &advance);
300 if (emulated == EMULATE_AGAIN) {
301 advance = 0;
302 } else if (emulated == EMULATE_FAIL) {
303 advance = 0;
304 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
305 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
306 }
307 }
308
309 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
310
311 /* Advance past emulated instruction. */
312 if (advance)
313 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
314
315 return emulated;
316}
317EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);