30a49f8c49b2391a75318a95a70eecfc624464a4
[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  *
17  * Authors: Hollis Blanchard <hollisb@us.ibm.com>
18  */
19
20 #include <linux/jiffies.h>
21 #include <linux/timer.h>
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/kvm_host.h>
25
26 #include <asm/reg.h>
27 #include <asm/time.h>
28 #include <asm/byteorder.h>
29 #include <asm/kvm_ppc.h>
30 #include <asm/disassemble.h>
31
32 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)
33 {
34         if (vcpu->arch.tcr & TCR_DIE) {
35                 /* The decrementer ticks at the same rate as the timebase, so
36                  * that's how we convert the guest DEC value to the number of
37                  * host ticks. */
38                 unsigned long nr_jiffies;
39
40                 nr_jiffies = vcpu->arch.dec / tb_ticks_per_jiffy;
41                 mod_timer(&vcpu->arch.dec_timer,
42                           get_jiffies_64() + nr_jiffies);
43         } else {
44                 del_timer(&vcpu->arch.dec_timer);
45         }
46 }
47
48 /* XXX to do:
49  * lhax
50  * lhaux
51  * lswx
52  * lswi
53  * stswx
54  * stswi
55  * lha
56  * lhau
57  * lmw
58  * stmw
59  *
60  * XXX is_bigendian should depend on MMU mapping or MSR[LE]
61  */
62 /* XXX Should probably auto-generate instruction decoding for a particular core
63  * from opcode tables in the future. */
64 int kvmppc_emulate_instruction(struct kvm_run *run, struct kvm_vcpu *vcpu)
65 {
66         u32 inst = vcpu->arch.last_inst;
67         u32 ea;
68         int ra;
69         int rb;
70         int rs;
71         int rt;
72         int sprn;
73         enum emulation_result emulated = EMULATE_DONE;
74         int advance = 1;
75
76         switch (get_op(inst)) {
77         case 3:                                                 /* trap */
78                 printk("trap!\n");
79                 kvmppc_core_queue_program(vcpu);
80                 advance = 0;
81                 break;
82
83         case 31:
84                 switch (get_xop(inst)) {
85
86                 case 23:                                        /* lwzx */
87                         rt = get_rt(inst);
88                         emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
89                         break;
90
91                 case 87:                                        /* lbzx */
92                         rt = get_rt(inst);
93                         emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
94                         break;
95
96                 case 151:                                       /* stwx */
97                         rs = get_rs(inst);
98                         emulated = kvmppc_handle_store(run, vcpu,
99                                                        vcpu->arch.gpr[rs],
100                                                        4, 1);
101                         break;
102
103                 case 215:                                       /* stbx */
104                         rs = get_rs(inst);
105                         emulated = kvmppc_handle_store(run, vcpu,
106                                                        vcpu->arch.gpr[rs],
107                                                        1, 1);
108                         break;
109
110                 case 247:                                       /* stbux */
111                         rs = get_rs(inst);
112                         ra = get_ra(inst);
113                         rb = get_rb(inst);
114
115                         ea = vcpu->arch.gpr[rb];
116                         if (ra)
117                                 ea += vcpu->arch.gpr[ra];
118
119                         emulated = kvmppc_handle_store(run, vcpu,
120                                                        vcpu->arch.gpr[rs],
121                                                        1, 1);
122                         vcpu->arch.gpr[rs] = ea;
123                         break;
124
125                 case 279:                                       /* lhzx */
126                         rt = get_rt(inst);
127                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
128                         break;
129
130                 case 311:                                       /* lhzux */
131                         rt = get_rt(inst);
132                         ra = get_ra(inst);
133                         rb = get_rb(inst);
134
135                         ea = vcpu->arch.gpr[rb];
136                         if (ra)
137                                 ea += vcpu->arch.gpr[ra];
138
139                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
140                         vcpu->arch.gpr[ra] = ea;
141                         break;
142
143                 case 339:                                       /* mfspr */
144                         sprn = get_sprn(inst);
145                         rt = get_rt(inst);
146
147                         switch (sprn) {
148                         case SPRN_SRR0:
149                                 vcpu->arch.gpr[rt] = vcpu->arch.srr0; break;
150                         case SPRN_SRR1:
151                                 vcpu->arch.gpr[rt] = vcpu->arch.srr1; break;
152                         case SPRN_PVR:
153                                 vcpu->arch.gpr[rt] = vcpu->arch.pvr; break;
154
155                         /* Note: mftb and TBRL/TBWL are user-accessible, so
156                          * the guest can always access the real TB anyways.
157                          * In fact, we probably will never see these traps. */
158                         case SPRN_TBWL:
159                                 vcpu->arch.gpr[rt] = mftbl(); break;
160                         case SPRN_TBWU:
161                                 vcpu->arch.gpr[rt] = mftbu(); break;
162
163                         case SPRN_SPRG0:
164                                 vcpu->arch.gpr[rt] = vcpu->arch.sprg0; break;
165                         case SPRN_SPRG1:
166                                 vcpu->arch.gpr[rt] = vcpu->arch.sprg1; break;
167                         case SPRN_SPRG2:
168                                 vcpu->arch.gpr[rt] = vcpu->arch.sprg2; break;
169                         case SPRN_SPRG3:
170                                 vcpu->arch.gpr[rt] = vcpu->arch.sprg3; break;
171                         /* Note: SPRG4-7 are user-readable, so we don't get
172                          * a trap. */
173
174                         default:
175                                 emulated = kvmppc_core_emulate_mfspr(vcpu, sprn, rt);
176                                 if (emulated == EMULATE_FAIL) {
177                                         printk("mfspr: unknown spr %x\n", sprn);
178                                         vcpu->arch.gpr[rt] = 0;
179                                 }
180                                 break;
181                         }
182                         break;
183
184                 case 407:                                       /* sthx */
185                         rs = get_rs(inst);
186                         ra = get_ra(inst);
187                         rb = get_rb(inst);
188
189                         emulated = kvmppc_handle_store(run, vcpu,
190                                                        vcpu->arch.gpr[rs],
191                                                        2, 1);
192                         break;
193
194                 case 439:                                       /* sthux */
195                         rs = get_rs(inst);
196                         ra = get_ra(inst);
197                         rb = get_rb(inst);
198
199                         ea = vcpu->arch.gpr[rb];
200                         if (ra)
201                                 ea += vcpu->arch.gpr[ra];
202
203                         emulated = kvmppc_handle_store(run, vcpu,
204                                                        vcpu->arch.gpr[rs],
205                                                        2, 1);
206                         vcpu->arch.gpr[ra] = ea;
207                         break;
208
209                 case 467:                                       /* mtspr */
210                         sprn = get_sprn(inst);
211                         rs = get_rs(inst);
212                         switch (sprn) {
213                         case SPRN_SRR0:
214                                 vcpu->arch.srr0 = vcpu->arch.gpr[rs]; break;
215                         case SPRN_SRR1:
216                                 vcpu->arch.srr1 = vcpu->arch.gpr[rs]; break;
217
218                         /* XXX We need to context-switch the timebase for
219                          * watchdog and FIT. */
220                         case SPRN_TBWL: break;
221                         case SPRN_TBWU: break;
222
223                         case SPRN_DEC:
224                                 vcpu->arch.dec = vcpu->arch.gpr[rs];
225                                 kvmppc_emulate_dec(vcpu);
226                                 break;
227
228                         case SPRN_SPRG0:
229                                 vcpu->arch.sprg0 = vcpu->arch.gpr[rs]; break;
230                         case SPRN_SPRG1:
231                                 vcpu->arch.sprg1 = vcpu->arch.gpr[rs]; break;
232                         case SPRN_SPRG2:
233                                 vcpu->arch.sprg2 = vcpu->arch.gpr[rs]; break;
234                         case SPRN_SPRG3:
235                                 vcpu->arch.sprg3 = vcpu->arch.gpr[rs]; break;
236
237                         default:
238                                 emulated = kvmppc_core_emulate_mtspr(vcpu, sprn, rs);
239                                 if (emulated == EMULATE_FAIL)
240                                         printk("mtspr: unknown spr %x\n", sprn);
241                                 break;
242                         }
243                         break;
244
245                 case 470:                                       /* dcbi */
246                         /* Do nothing. The guest is performing dcbi because
247                          * hardware DMA is not snooped by the dcache, but
248                          * emulated DMA either goes through the dcache as
249                          * normal writes, or the host kernel has handled dcache
250                          * coherence. */
251                         break;
252
253                 case 534:                                       /* lwbrx */
254                         rt = get_rt(inst);
255                         emulated = kvmppc_handle_load(run, vcpu, rt, 4, 0);
256                         break;
257
258                 case 566:                                       /* tlbsync */
259                         break;
260
261                 case 662:                                       /* stwbrx */
262                         rs = get_rs(inst);
263                         ra = get_ra(inst);
264                         rb = get_rb(inst);
265
266                         emulated = kvmppc_handle_store(run, vcpu,
267                                                        vcpu->arch.gpr[rs],
268                                                        4, 0);
269                         break;
270
271                 case 790:                                       /* lhbrx */
272                         rt = get_rt(inst);
273                         emulated = kvmppc_handle_load(run, vcpu, rt, 2, 0);
274                         break;
275
276                 case 918:                                       /* sthbrx */
277                         rs = get_rs(inst);
278                         ra = get_ra(inst);
279                         rb = get_rb(inst);
280
281                         emulated = kvmppc_handle_store(run, vcpu,
282                                                        vcpu->arch.gpr[rs],
283                                                        2, 0);
284                         break;
285
286                 default:
287                         /* Attempt core-specific emulation below. */
288                         emulated = EMULATE_FAIL;
289                 }
290                 break;
291
292         case 32:                                                /* lwz */
293                 rt = get_rt(inst);
294                 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
295                 break;
296
297         case 33:                                                /* lwzu */
298                 ra = get_ra(inst);
299                 rt = get_rt(inst);
300                 emulated = kvmppc_handle_load(run, vcpu, rt, 4, 1);
301                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
302                 break;
303
304         case 34:                                                /* lbz */
305                 rt = get_rt(inst);
306                 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
307                 break;
308
309         case 35:                                                /* lbzu */
310                 ra = get_ra(inst);
311                 rt = get_rt(inst);
312                 emulated = kvmppc_handle_load(run, vcpu, rt, 1, 1);
313                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
314                 break;
315
316         case 36:                                                /* stw */
317                 rs = get_rs(inst);
318                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
319                                                4, 1);
320                 break;
321
322         case 37:                                                /* stwu */
323                 ra = get_ra(inst);
324                 rs = get_rs(inst);
325                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
326                                                4, 1);
327                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
328                 break;
329
330         case 38:                                                /* stb */
331                 rs = get_rs(inst);
332                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
333                                                1, 1);
334                 break;
335
336         case 39:                                                /* stbu */
337                 ra = get_ra(inst);
338                 rs = get_rs(inst);
339                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
340                                                1, 1);
341                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
342                 break;
343
344         case 40:                                                /* lhz */
345                 rt = get_rt(inst);
346                 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
347                 break;
348
349         case 41:                                                /* lhzu */
350                 ra = get_ra(inst);
351                 rt = get_rt(inst);
352                 emulated = kvmppc_handle_load(run, vcpu, rt, 2, 1);
353                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
354                 break;
355
356         case 44:                                                /* sth */
357                 rs = get_rs(inst);
358                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
359                                                2, 1);
360                 break;
361
362         case 45:                                                /* sthu */
363                 ra = get_ra(inst);
364                 rs = get_rs(inst);
365                 emulated = kvmppc_handle_store(run, vcpu, vcpu->arch.gpr[rs],
366                                                2, 1);
367                 vcpu->arch.gpr[ra] = vcpu->arch.paddr_accessed;
368                 break;
369
370         default:
371                 emulated = EMULATE_FAIL;
372         }
373
374         if (emulated == EMULATE_FAIL) {
375                 emulated = kvmppc_core_emulate_op(run, vcpu, inst, &advance);
376                 if (emulated == EMULATE_FAIL) {
377                         advance = 0;
378                         printk(KERN_ERR "Couldn't emulate instruction 0x%08x "
379                                "(op %d xop %d)\n", inst, get_op(inst), get_xop(inst));
380                 }
381         }
382
383         KVMTRACE_3D(PPC_INSTR, vcpu, inst, vcpu->arch.pc, emulated, entryexit);
384
385         if (advance)
386                 vcpu->arch.pc += 4; /* Advance past emulated instruction. */
387
388         return emulated;
389 }