1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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: %x\n", vcpu->arch.dec);
43 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
44
45#ifdef CONFIG_PPC_BOOK3S
46
47 kvmppc_core_dequeue_dec(vcpu);
48
49
50 if (vcpu->arch.dec & 0x80000000) {
51 kvmppc_core_queue_dec(vcpu);
52 return;
53 }
54#endif
55
56#ifdef CONFIG_BOOKE
57
58 if (vcpu->arch.dec == 0)
59 return;
60#endif
61
62
63
64
65
66
67
68 dec_time = vcpu->arch.dec;
69
70
71
72
73 dec_time = dec_time << decrementer_clockevent.shift;
74 do_div(dec_time, decrementer_clockevent.mult);
75 dec_nsec = do_div(dec_time, NSEC_PER_SEC);
76 hrtimer_start(&vcpu->arch.dec_timer,
77 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
78 vcpu->arch.dec_jiffies = get_tb();
79}
80
81u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
82{
83 u64 jd = tb - vcpu->arch.dec_jiffies;
84
85#ifdef CONFIG_BOOKE
86 if (vcpu->arch.dec < jd)
87 return 0;
88#endif
89
90 return vcpu->arch.dec - jd;
91}
92
93static int kvmppc_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
94{
95 enum emulation_result emulated = EMULATE_DONE;
96 ulong spr_val = kvmppc_get_gpr(vcpu, rs);
97
98 switch (sprn) {
99 case SPRN_SRR0:
100 kvmppc_set_srr0(vcpu, spr_val);
101 break;
102 case SPRN_SRR1:
103 kvmppc_set_srr1(vcpu, spr_val);
104 break;
105
106
107
108 case SPRN_TBWL: break;
109 case SPRN_TBWU: break;
110
111 case SPRN_DEC:
112 vcpu->arch.dec = spr_val;
113 kvmppc_emulate_dec(vcpu);
114 break;
115
116 case SPRN_SPRG0:
117 kvmppc_set_sprg0(vcpu, spr_val);
118 break;
119 case SPRN_SPRG1:
120 kvmppc_set_sprg1(vcpu, spr_val);
121 break;
122 case SPRN_SPRG2:
123 kvmppc_set_sprg2(vcpu, spr_val);
124 break;
125 case SPRN_SPRG3:
126 kvmppc_set_sprg3(vcpu, spr_val);
127 break;
128
129
130 case SPRN_PIR: break;
131
132 default:
133 emulated = vcpu->kvm->arch.kvm_ops->emulate_mtspr(vcpu, sprn,
134 spr_val);
135 if (emulated == EMULATE_FAIL)
136 printk(KERN_INFO "mtspr: unknown spr "
137 "0x%x\n", sprn);
138 break;
139 }
140
141 kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
142
143 return emulated;
144}
145
146static int kvmppc_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
147{
148 enum emulation_result emulated = EMULATE_DONE;
149 ulong spr_val = 0;
150
151 switch (sprn) {
152 case SPRN_SRR0:
153 spr_val = kvmppc_get_srr0(vcpu);
154 break;
155 case SPRN_SRR1:
156 spr_val = kvmppc_get_srr1(vcpu);
157 break;
158 case SPRN_PVR:
159 spr_val = vcpu->arch.pvr;
160 break;
161 case SPRN_PIR:
162 spr_val = vcpu->vcpu_id;
163 break;
164
165
166
167
168 case SPRN_TBWL:
169 spr_val = get_tb() >> 32;
170 break;
171 case SPRN_TBWU:
172 spr_val = get_tb();
173 break;
174
175 case SPRN_SPRG0:
176 spr_val = kvmppc_get_sprg0(vcpu);
177 break;
178 case SPRN_SPRG1:
179 spr_val = kvmppc_get_sprg1(vcpu);
180 break;
181 case SPRN_SPRG2:
182 spr_val = kvmppc_get_sprg2(vcpu);
183 break;
184 case SPRN_SPRG3:
185 spr_val = kvmppc_get_sprg3(vcpu);
186 break;
187
188
189
190 case SPRN_DEC:
191 spr_val = kvmppc_get_dec(vcpu, get_tb());
192 break;
193 default:
194 emulated = vcpu->kvm->arch.kvm_ops->emulate_mfspr(vcpu, sprn,
195 &spr_val);
196 if (unlikely(emulated == EMULATE_FAIL)) {
197 printk(KERN_INFO "mfspr: unknown spr "
198 "0x%x\n", sprn);
199 }
200 break;
201 }
202
203 if (emulated == EMULATE_DONE)
204 kvmppc_set_gpr(vcpu, rt, spr_val);
205 kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
206
207 return emulated;
208}
209
210
211
212int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
213{
214 u32 inst;
215 int rs, rt, sprn;
216 enum emulation_result emulated;
217 int advance = 1;
218
219
220 kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
221
222 emulated = kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst);
223 if (emulated != EMULATE_DONE)
224 return emulated;
225
226 pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
227
228 rs = get_rs(inst);
229 rt = get_rt(inst);
230 sprn = get_sprn(inst);
231
232 switch (get_op(inst)) {
233 case OP_TRAP:
234#ifdef CONFIG_PPC_BOOK3S
235 case OP_TRAP_64:
236 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
237#else
238 kvmppc_core_queue_program(vcpu,
239 vcpu->arch.shared->esr | ESR_PTR);
240#endif
241 advance = 0;
242 break;
243
244 case 31:
245 switch (get_xop(inst)) {
246
247 case OP_31_XOP_TRAP:
248#ifdef CONFIG_64BIT
249 case OP_31_XOP_TRAP_64:
250#endif
251#ifdef CONFIG_PPC_BOOK3S
252 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
253#else
254 kvmppc_core_queue_program(vcpu,
255 vcpu->arch.shared->esr | ESR_PTR);
256#endif
257 advance = 0;
258 break;
259
260 case OP_31_XOP_MFSPR:
261 emulated = kvmppc_emulate_mfspr(vcpu, sprn, rt);
262 break;
263
264 case OP_31_XOP_MTSPR:
265 emulated = kvmppc_emulate_mtspr(vcpu, sprn, rs);
266 break;
267
268 case OP_31_XOP_TLBSYNC:
269 break;
270
271 default:
272
273 emulated = EMULATE_FAIL;
274 }
275 break;
276
277 case 0:
278
279
280
281
282 if (inst == KVMPPC_INST_SW_BREAKPOINT) {
283 run->exit_reason = KVM_EXIT_DEBUG;
284 run->debug.arch.address = kvmppc_get_pc(vcpu);
285 emulated = EMULATE_EXIT_USER;
286 advance = 0;
287 } else
288 emulated = EMULATE_FAIL;
289
290 break;
291
292 default:
293 emulated = EMULATE_FAIL;
294 }
295
296 if (emulated == EMULATE_FAIL) {
297 emulated = vcpu->kvm->arch.kvm_ops->emulate_op(run, vcpu, inst,
298 &advance);
299 if (emulated == EMULATE_AGAIN) {
300 advance = 0;
301 } else if (emulated == EMULATE_FAIL) {
302 advance = 0;
303 printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
304 "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
305 }
306 }
307
308 trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
309
310
311 if (advance)
312 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
313
314 return emulated;
315}
316EXPORT_SYMBOL_GPL(kvmppc_emulate_instruction);
317