KVM: PPC: Emulator: clean up instruction parsing
[linux-3.10.git] / arch / powerpc / kvm / emulate.c
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 "timing.h"
34 #include "trace.h"
35
36 #define OP_TRAP 3
37 #define OP_TRAP_64 2
38
39 #define OP_31_XOP_TRAP      4
40 #define OP_31_XOP_LWZX      23
41 #define OP_31_XOP_TRAP_64   68
42 #define OP_31_XOP_LBZX      87
43 #define OP_31_XOP_STWX      151
44 #define OP_31_XOP_STBX      215
45 #define OP_31_XOP_LBZUX     119
46 #define OP_31_XOP_STBUX     247
47 #define OP_31_XOP_LHZX      279
48 #define OP_31_XOP_LHZUX     311
49 #define OP_31_XOP_MFSPR     339
50 #define OP_31_XOP_LHAX      343
51 #define OP_31_XOP_STHX      407
52 #define OP_31_XOP_STHUX     439
53 #define OP_31_XOP_MTSPR     467
54 #define OP_31_XOP_DCBI      470
55 #define OP_31_XOP_LWBRX     534
56 #define OP_31_XOP_TLBSYNC   566
57 #define OP_31_XOP_STWBRX    662
58 #define OP_31_XOP_LHBRX     790
59 #define OP_31_XOP_STHBRX    918
60
61 #define OP_LWZ  32
62 #define OP_LWZU 33
63 #define OP_LBZ  34
64 #define OP_LBZU 35
65 #define OP_STW  36
66 #define OP_STWU 37
67 #define OP_STB  38
68 #define OP_STBU 39
69 #define OP_LHZ  40
70 #define OP_LHZU 41
71 #define OP_LHA  42
72 #define OP_LHAU 43
73 #define OP_STH  44
74 #define OP_STHU 45
75
76 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
77 {
78         unsigned long dec_nsec;
79         unsigned long long dec_time;
80
81         pr_debug("mtDEC: %x\n", vcpu->arch.dec);
82         hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
83
84 #ifdef CONFIG_PPC_BOOK3S
85         /* mtdec lowers the interrupt line when positive. */
86         kvmppc_core_dequeue_dec(vcpu);
87
88         /* POWER4+ triggers a dec interrupt if the value is < 0 */
89         if (vcpu->arch.dec & 0x80000000) {
90                 kvmppc_core_queue_dec(vcpu);
91                 return;
92         }
93 #endif
94
95 #ifdef CONFIG_BOOKE
96         /* On BOOKE, DEC = 0 is as good as decrementer not enabled */
97         if (vcpu->arch.dec == 0)
98                 return;
99 #endif
100
101         /*
102          * The decrementer ticks at the same rate as the timebase, so
103          * that's how we convert the guest DEC value to the number of
104          * host ticks.
105          */
106
107         dec_time = vcpu->arch.dec;
108         /*
109          * Guest timebase ticks at the same frequency as host decrementer.
110          * So use the host decrementer calculations for decrementer emulation.
111          */
112         dec_time = dec_time << decrementer_clockevent.shift;
113         do_div(dec_time, decrementer_clockevent.mult);
114         dec_nsec = do_div(dec_time, NSEC_PER_SEC);
115         hrtimer_start(&vcpu->arch.dec_timer,
116                 ktime_set(dec_time, dec_nsec), HRTIMER_MODE_REL);
117         vcpu->arch.dec_jiffies = get_tb();
118 }
119
120 u32 kvmppc_get_dec(struct kvm_vcpu *vcpu, u64 tb)
121 {
122         u64 jd = tb - vcpu->arch.dec_jiffies;
123
124 #ifdef CONFIG_BOOKE
125         if (vcpu->arch.dec < jd)
126                 return 0;
127 #endif
128
129         return vcpu->arch.dec - jd;
130 }
131
132 /* XXX to do:
133  * lhax
134  * lhaux
135  * lswx
136  * lswi
137  * stswx
138  * stswi
139  * lha
140  * lhau
141  * lmw
142  * stmw
143  *
144  * XXX is_bigendian should depend on MMU mapping or MSR[LE]
145  */
146 /* XXX Should probably auto-generate instruction decoding for a particular core
147  * from opcode tables in the future. */
148 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
149 {
150         u32 inst = kvmppc_get_last_inst(vcpu);
151         int ra = get_ra(inst);
152         int rs = get_rs(inst);
153         int rt = get_rt(inst);
154         int sprn = get_sprn(inst);
155         enum emulation_result emulated = EMULATE_DONE;
156         int advance = 1;
157
158         /* this default type might be overwritten by subcategories */
159         kvmppc_set_exit_type(vcpu, EMULATED_INST_EXITS);
160
161         pr_debug("Emulating opcode %d / %d\n", get_op(inst), get_xop(inst));
162
163         switch (get_op(inst)) {
164         case OP_TRAP:
165 #ifdef CONFIG_PPC_BOOK3S
166         case OP_TRAP_64:
167                 kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
168 #else
169                 kvmppc_core_queue_program(vcpu,
170                                           vcpu->arch.shared->esr | ESR_PTR);
171 #endif
172                 advance = 0;
173                 break;
174
175         case 31:
176                 switch (get_xop(inst)) {
177
178                 case OP_31_XOP_TRAP:
179 #ifdef CONFIG_64BIT
180                 case OP_31_XOP_TRAP_64:
181 #endif
182 #ifdef CONFIG_PPC_BOOK3S
183                         kvmppc_core_queue_program(vcpu, SRR1_PROGTRAP);
184 #else
185                         kvmppc_core_queue_program(vcpu,
186                                         vcpu->arch.shared->esr | ESR_PTR);
187 #endif
188                         advance = 0;
189                         break;
190                 case OP_31_XOP_LWZX:
191                         emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
192                         break;
193
194                 case OP_31_XOP_LBZX:
195                         emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
196                         break;
197
198                 case OP_31_XOP_LBZUX:
199                         emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
200                         kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
201                         break;
202
203                 case OP_31_XOP_STWX:
204                         emulated = kvmppc_handle_store(run, vcpu,
205                                                        kvmppc_get_gpr(vcpu, rs),
206                                                        4, 1);
207                         break;
208
209                 case OP_31_XOP_STBX:
210                         emulated = kvmppc_handle_store(run, vcpu,
211                                                        kvmppc_get_gpr(vcpu, rs),
212                                                        1, 1);
213                         break;
214
215                 case OP_31_XOP_STBUX:
216                         emulated = kvmppc_handle_store(run, vcpu,
217                                                        kvmppc_get_gpr(vcpu, rs),
218                                                        1, 1);
219                         kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
220                         break;
221
222                 case OP_31_XOP_LHAX:
223                         emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
224                         break;
225
226                 case OP_31_XOP_LHZX:
227                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
228                         break;
229
230                 case OP_31_XOP_LHZUX:
231                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
232                         kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
233                         break;
234
235                 case OP_31_XOP_MFSPR:
236                         switch (sprn) {
237                         case SPRN_SRR0:
238                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr0);
239                                 break;
240                         case SPRN_SRR1:
241                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->srr1);
242                                 break;
243                         case SPRN_PVR:
244                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.pvr); break;
245                         case SPRN_PIR:
246                                 kvmppc_set_gpr(vcpu, rt, vcpu->vcpu_id); break;
247                         case SPRN_MSSSR0:
248                                 kvmppc_set_gpr(vcpu, rt, 0); break;
249
250                         /* Note: mftb and TBRL/TBWL are user-accessible, so
251                          * the guest can always access the real TB anyways.
252                          * In fact, we probably will never see these traps. */
253                         case SPRN_TBWL:
254                                 kvmppc_set_gpr(vcpu, rt, get_tb() >> 32); break;
255                         case SPRN_TBWU:
256                                 kvmppc_set_gpr(vcpu, rt, get_tb()); break;
257
258                         case SPRN_SPRG0:
259                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg0);
260                                 break;
261                         case SPRN_SPRG1:
262                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg1);
263                                 break;
264                         case SPRN_SPRG2:
265                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg2);
266                                 break;
267                         case SPRN_SPRG3:
268                                 kvmppc_set_gpr(vcpu, rt, vcpu->arch.shared->sprg3);
269                                 break;
270                         /* Note: SPRG4-7 are user-readable, so we don't get
271                          * a trap. */
272
273                         case SPRN_DEC:
274                         {
275                                 kvmppc_set_gpr(vcpu, rt,
276                                                kvmppc_get_dec(vcpu, get_tb()));
277                                 break;
278                         }
279                         default:
280                                 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
281                                 if (emulated == EMULATE_FAIL) {
282                                         printk("mfspr: unknown spr %x\n", sprn);
283                                         kvmppc_set_gpr(vcpu, rt, 0);
284                                 }
285                                 break;
286                         }
287                         kvmppc_set_exit_type(vcpu, EMULATED_MFSPR_EXITS);
288                         break;
289
290                 case OP_31_XOP_STHX:
291                         emulated = kvmppc_handle_store(run, vcpu,
292                                                        kvmppc_get_gpr(vcpu, rs),
293                                                        2, 1);
294                         break;
295
296                 case OP_31_XOP_STHUX:
297                         emulated = kvmppc_handle_store(run, vcpu,
298                                                        kvmppc_get_gpr(vcpu, rs),
299                                                        2, 1);
300                         kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
301                         break;
302
303                 case OP_31_XOP_MTSPR:
304                         switch (sprn) {
305                         case SPRN_SRR0:
306                                 vcpu->arch.shared->srr0 = kvmppc_get_gpr(vcpu, rs);
307                                 break;
308                         case SPRN_SRR1:
309                                 vcpu->arch.shared->srr1 = kvmppc_get_gpr(vcpu, rs);
310                                 break;
311
312                         /* XXX We need to context-switch the timebase for
313                          * watchdog and FIT. */
314                         case SPRN_TBWL: break;
315                         case SPRN_TBWU: break;
316
317                         case SPRN_MSSSR0: break;
318
319                         case SPRN_DEC:
320                                 vcpu->arch.dec = kvmppc_get_gpr(vcpu, rs);
321                                 kvmppc_emulate_dec(vcpu);
322                                 break;
323
324                         case SPRN_SPRG0:
325                                 vcpu->arch.shared->sprg0 = kvmppc_get_gpr(vcpu, rs);
326                                 break;
327                         case SPRN_SPRG1:
328                                 vcpu->arch.shared->sprg1 = kvmppc_get_gpr(vcpu, rs);
329                                 break;
330                         case SPRN_SPRG2:
331                                 vcpu->arch.shared->sprg2 = kvmppc_get_gpr(vcpu, rs);
332                                 break;
333                         case SPRN_SPRG3:
334                                 vcpu->arch.shared->sprg3 = kvmppc_get_gpr(vcpu, rs);
335                                 break;
336
337                         default:
338                                 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
339                                 if (emulated == EMULATE_FAIL)
340                                         printk("mtspr: unknown spr %x\n", sprn);
341                                 break;
342                         }
343                         kvmppc_set_exit_type(vcpu, EMULATED_MTSPR_EXITS);
344                         break;
345
346                 case OP_31_XOP_DCBI:
347                         /* Do nothing. The guest is performing dcbi because
348                          * hardware DMA is not snooped by the dcache, but
349                          * emulated DMA either goes through the dcache as
350                          * normal writes, or the host kernel has handled dcache
351                          * coherence. */
352                         break;
353
354                 case OP_31_XOP_LWBRX:
355                         emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
356                         break;
357
358                 case OP_31_XOP_TLBSYNC:
359                         break;
360
361                 case OP_31_XOP_STWBRX:
362                         emulated = kvmppc_handle_store(run, vcpu,
363                                                        kvmppc_get_gpr(vcpu, rs),
364                                                        4, 0);
365                         break;
366
367                 case OP_31_XOP_LHBRX:
368                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
369                         break;
370
371                 case OP_31_XOP_STHBRX:
372                         emulated = kvmppc_handle_store(run, vcpu,
373                                                        kvmppc_get_gpr(vcpu, rs),
374                                                        2, 0);
375                         break;
376
377                 default:
378                         /* Attempt core-specific emulation below. */
379                         emulated = EMULATE_FAIL;
380                 }
381                 break;
382
383         case OP_LWZ:
384                 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
385                 break;
386
387         case OP_LWZU:
388                 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
389                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
390                 break;
391
392         case OP_LBZ:
393                 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
394                 break;
395
396         case OP_LBZU:
397                 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
398                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
399                 break;
400
401         case OP_STW:
402                 emulated = kvmppc_handle_store(run, vcpu,
403                                                kvmppc_get_gpr(vcpu, rs),
404                                                4, 1);
405                 break;
406
407         case OP_STWU:
408                 emulated = kvmppc_handle_store(run, vcpu,
409                                                kvmppc_get_gpr(vcpu, rs),
410                                                4, 1);
411                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
412                 break;
413
414         case OP_STB:
415                 emulated = kvmppc_handle_store(run, vcpu,
416                                                kvmppc_get_gpr(vcpu, rs),
417                                                1, 1);
418                 break;
419
420         case OP_STBU:
421                 emulated = kvmppc_handle_store(run, vcpu,
422                                                kvmppc_get_gpr(vcpu, rs),
423                                                1, 1);
424                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
425                 break;
426
427         case OP_LHZ:
428                 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
429                 break;
430
431         case OP_LHZU:
432                 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
433                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
434                 break;
435
436         case OP_LHA:
437                 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
438                 break;
439
440         case OP_LHAU:
441                 emulated = kvmppc_handle_loads(run, vcpu, rt, 2, 1);
442                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
443                 break;
444
445         case OP_STH:
446                 emulated = kvmppc_handle_store(run, vcpu,
447                                                kvmppc_get_gpr(vcpu, rs),
448                                                2, 1);
449                 break;
450
451         case OP_STHU:
452                 emulated = kvmppc_handle_store(run, vcpu,
453                                                kvmppc_get_gpr(vcpu, rs),
454                                                2, 1);
455                 kvmppc_set_gpr(vcpu, ra, vcpu->arch.vaddr_accessed);
456                 break;
457
458         default:
459                 emulated = EMULATE_FAIL;
460         }
461
462         if (emulated == EMULATE_FAIL) {
463                 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
464                 if (emulated == EMULATE_AGAIN) {
465                         advance = 0;
466                 } else if (emulated == EMULATE_FAIL) {
467                         advance = 0;
468                         printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
469                                "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
470                         kvmppc_core_queue_program(vcpu, 0);
471                 }
472         }
473
474         trace_kvm_ppc_instr(inst, kvmppc_get_pc(vcpu), emulated);
475
476         /* Advance past emulated instruction. */
477         if (advance)
478                 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
479
480         return emulated;
481 }