blob: f1d1b42d19021ace747391fd7f28d388150647fb [file] [log] [blame]
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +00001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (c) 2014 Imagination Technologies Ltd.
7 * Author: Leonid Yegoshin <Leonid.Yegoshin@imgtec.com>
8 * Author: Markos Chandras <markos.chandras@imgtec.com>
9 *
10 * MIPS R2 user space instruction emulator for MIPS R6
11 *
12 */
13#include <linux/bug.h>
14#include <linux/compiler.h>
15#include <linux/debugfs.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/ptrace.h>
20#include <linux/seq_file.h>
21
22#include <asm/asm.h>
23#include <asm/branch.h>
24#include <asm/break.h>
25#include <asm/fpu.h>
26#include <asm/fpu_emulator.h>
27#include <asm/inst.h>
28#include <asm/mips-r2-to-r6-emul.h>
29#include <asm/local.h>
30#include <asm/ptrace.h>
31#include <asm/uaccess.h>
32
33#ifdef CONFIG_64BIT
34#define ADDIU "daddiu "
35#define INS "dins "
36#define EXT "dext "
37#else
38#define ADDIU "addiu "
39#define INS "ins "
40#define EXT "ext "
41#endif /* CONFIG_64BIT */
42
43#define SB "sb "
44#define LB "lb "
45#define LL "ll "
46#define SC "sc "
47
48DEFINE_PER_CPU(struct mips_r2_emulator_stats, mipsr2emustats);
49DEFINE_PER_CPU(struct mips_r2_emulator_stats, mipsr2bdemustats);
50DEFINE_PER_CPU(struct mips_r2br_emulator_stats, mipsr2bremustats);
51
52extern const unsigned int fpucondbit[8];
53
54#define MIPS_R2_EMUL_TOTAL_PASS 10
55
56int mipsr2_emulation = 0;
57
58static int __init mipsr2emu_enable(char *s)
59{
60 mipsr2_emulation = 1;
61
62 pr_info("MIPS R2-to-R6 Emulator Enabled!");
63
64 return 1;
65}
66__setup("mipsr2emu", mipsr2emu_enable);
67
68/**
69 * mipsr6_emul - Emulate some frequent R2/R5/R6 instructions in delay slot
70 * for performance instead of the traditional way of using a stack trampoline
71 * which is rather slow.
72 * @regs: Process register set
73 * @ir: Instruction
74 */
75static inline int mipsr6_emul(struct pt_regs *regs, u32 ir)
76{
77 switch (MIPSInst_OPCODE(ir)) {
78 case addiu_op:
79 if (MIPSInst_RT(ir))
80 regs->regs[MIPSInst_RT(ir)] =
81 (s32)regs->regs[MIPSInst_RS(ir)] +
82 (s32)MIPSInst_SIMM(ir);
83 return 0;
84 case daddiu_op:
85 if (config_enabled(CONFIG_32BIT))
86 break;
87
88 if (MIPSInst_RT(ir))
89 regs->regs[MIPSInst_RT(ir)] =
90 (s64)regs->regs[MIPSInst_RS(ir)] +
91 (s64)MIPSInst_SIMM(ir);
92 return 0;
93 case lwc1_op:
94 case swc1_op:
95 case cop1_op:
96 case cop1x_op:
97 /* FPU instructions in delay slot */
98 return -SIGFPE;
99 case spec_op:
100 switch (MIPSInst_FUNC(ir)) {
101 case or_op:
102 if (MIPSInst_RD(ir))
103 regs->regs[MIPSInst_RD(ir)] =
104 regs->regs[MIPSInst_RS(ir)] |
105 regs->regs[MIPSInst_RT(ir)];
106 return 0;
107 case sll_op:
108 if (MIPSInst_RS(ir))
109 break;
110
111 if (MIPSInst_RD(ir))
112 regs->regs[MIPSInst_RD(ir)] =
113 (s32)(((u32)regs->regs[MIPSInst_RT(ir)]) <<
114 MIPSInst_FD(ir));
115 return 0;
116 case srl_op:
117 if (MIPSInst_RS(ir))
118 break;
119
120 if (MIPSInst_RD(ir))
121 regs->regs[MIPSInst_RD(ir)] =
122 (s32)(((u32)regs->regs[MIPSInst_RT(ir)]) >>
123 MIPSInst_FD(ir));
124 return 0;
125 case addu_op:
126 if (MIPSInst_FD(ir))
127 break;
128
129 if (MIPSInst_RD(ir))
130 regs->regs[MIPSInst_RD(ir)] =
131 (s32)((u32)regs->regs[MIPSInst_RS(ir)] +
132 (u32)regs->regs[MIPSInst_RT(ir)]);
133 return 0;
134 case subu_op:
135 if (MIPSInst_FD(ir))
136 break;
137
138 if (MIPSInst_RD(ir))
139 regs->regs[MIPSInst_RD(ir)] =
140 (s32)((u32)regs->regs[MIPSInst_RS(ir)] -
141 (u32)regs->regs[MIPSInst_RT(ir)]);
142 return 0;
143 case dsll_op:
144 if (config_enabled(CONFIG_32BIT) || MIPSInst_RS(ir))
145 break;
146
147 if (MIPSInst_RD(ir))
148 regs->regs[MIPSInst_RD(ir)] =
149 (s64)(((u64)regs->regs[MIPSInst_RT(ir)]) <<
150 MIPSInst_FD(ir));
151 return 0;
152 case dsrl_op:
153 if (config_enabled(CONFIG_32BIT) || MIPSInst_RS(ir))
154 break;
155
156 if (MIPSInst_RD(ir))
157 regs->regs[MIPSInst_RD(ir)] =
158 (s64)(((u64)regs->regs[MIPSInst_RT(ir)]) >>
159 MIPSInst_FD(ir));
160 return 0;
161 case daddu_op:
162 if (config_enabled(CONFIG_32BIT) || MIPSInst_FD(ir))
163 break;
164
165 if (MIPSInst_RD(ir))
166 regs->regs[MIPSInst_RD(ir)] =
167 (u64)regs->regs[MIPSInst_RS(ir)] +
168 (u64)regs->regs[MIPSInst_RT(ir)];
169 return 0;
170 case dsubu_op:
171 if (config_enabled(CONFIG_32BIT) || MIPSInst_FD(ir))
172 break;
173
174 if (MIPSInst_RD(ir))
175 regs->regs[MIPSInst_RD(ir)] =
176 (s64)((u64)regs->regs[MIPSInst_RS(ir)] -
177 (u64)regs->regs[MIPSInst_RT(ir)]);
178 return 0;
179 }
180 break;
181 default:
182 pr_debug("No fastpath BD emulation for instruction 0x%08x (op: %02x)\n",
183 ir, MIPSInst_OPCODE(ir));
184 }
185
186 return SIGILL;
187}
188
189/**
Maciej W. Rozycki241e9c42015-04-03 23:24:29 +0100190 * movf_func - Emulate a MOVF instruction
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +0000191 * @regs: Process register set
192 * @ir: Instruction
193 *
194 * Returns 0 since it always succeeds.
195 */
196static int movf_func(struct pt_regs *regs, u32 ir)
197{
198 u32 csr;
199 u32 cond;
200
201 csr = current->thread.fpu.fcr31;
202 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
Maciej W. Rozycki241e9c42015-04-03 23:24:29 +0100203
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +0000204 if (((csr & cond) == 0) && MIPSInst_RD(ir))
205 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
Maciej W. Rozycki241e9c42015-04-03 23:24:29 +0100206
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +0000207 MIPS_R2_STATS(movs);
Maciej W. Rozycki241e9c42015-04-03 23:24:29 +0100208
Leonid Yegoshinb0a668f2014-12-03 15:47:03 +0000209 return 0;
210}
211
212/**
213 * movt_func - Emulate a MOVT instruction
214 * @regs: Process register set
215 * @ir: Instruction
216 *
217 * Returns 0 since it always succeeds.
218 */
219static int movt_func(struct pt_regs *regs, u32 ir)
220{
221 u32 csr;
222 u32 cond;
223
224 csr = current->thread.fpu.fcr31;
225 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
226
227 if (((csr & cond) != 0) && MIPSInst_RD(ir))
228 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
229
230 MIPS_R2_STATS(movs);
231
232 return 0;
233}
234
235/**
236 * jr_func - Emulate a JR instruction.
237 * @pt_regs: Process register set
238 * @ir: Instruction
239 *
240 * Returns SIGILL if JR was in delay slot, SIGEMT if we
241 * can't compute the EPC, SIGSEGV if we can't access the
242 * userland instruction or 0 on success.
243 */
244static int jr_func(struct pt_regs *regs, u32 ir)
245{
246 int err;
247 unsigned long cepc, epc, nepc;
248 u32 nir;
249
250 if (delay_slot(regs))
251 return SIGILL;
252
253 /* EPC after the RI/JR instruction */
254 nepc = regs->cp0_epc;
255 /* Roll back to the reserved R2 JR instruction */
256 regs->cp0_epc -= 4;
257 epc = regs->cp0_epc;
258 err = __compute_return_epc(regs);
259
260 if (err < 0)
261 return SIGEMT;
262
263
264 /* Computed EPC */
265 cepc = regs->cp0_epc;
266
267 /* Get DS instruction */
268 err = __get_user(nir, (u32 __user *)nepc);
269 if (err)
270 return SIGSEGV;
271
272 MIPS_R2BR_STATS(jrs);
273
274 /* If nir == 0(NOP), then nothing else to do */
275 if (nir) {
276 /*
277 * Negative err means FPU instruction in BD-slot,
278 * Zero err means 'BD-slot emulation done'
279 * For anything else we go back to trampoline emulation.
280 */
281 err = mipsr6_emul(regs, nir);
282 if (err > 0) {
283 regs->cp0_epc = nepc;
284 err = mips_dsemul(regs, nir, cepc);
285 if (err == SIGILL)
286 err = SIGEMT;
287 MIPS_R2_STATS(dsemul);
288 }
289 }
290
291 return err;
292}
293
294/**
295 * movz_func - Emulate a MOVZ instruction
296 * @regs: Process register set
297 * @ir: Instruction
298 *
299 * Returns 0 since it always succeeds.
300 */
301static int movz_func(struct pt_regs *regs, u32 ir)
302{
303 if (((regs->regs[MIPSInst_RT(ir)]) == 0) && MIPSInst_RD(ir))
304 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
305 MIPS_R2_STATS(movs);
306
307 return 0;
308}
309
310/**
311 * movn_func - Emulate a MOVZ instruction
312 * @regs: Process register set
313 * @ir: Instruction
314 *
315 * Returns 0 since it always succeeds.
316 */
317static int movn_func(struct pt_regs *regs, u32 ir)
318{
319 if (((regs->regs[MIPSInst_RT(ir)]) != 0) && MIPSInst_RD(ir))
320 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
321 MIPS_R2_STATS(movs);
322
323 return 0;
324}
325
326/**
327 * mfhi_func - Emulate a MFHI instruction
328 * @regs: Process register set
329 * @ir: Instruction
330 *
331 * Returns 0 since it always succeeds.
332 */
333static int mfhi_func(struct pt_regs *regs, u32 ir)
334{
335 if (MIPSInst_RD(ir))
336 regs->regs[MIPSInst_RD(ir)] = regs->hi;
337
338 MIPS_R2_STATS(hilo);
339
340 return 0;
341}
342
343/**
344 * mthi_func - Emulate a MTHI instruction
345 * @regs: Process register set
346 * @ir: Instruction
347 *
348 * Returns 0 since it always succeeds.
349 */
350static int mthi_func(struct pt_regs *regs, u32 ir)
351{
352 regs->hi = regs->regs[MIPSInst_RS(ir)];
353
354 MIPS_R2_STATS(hilo);
355
356 return 0;
357}
358
359/**
360 * mflo_func - Emulate a MFLO instruction
361 * @regs: Process register set
362 * @ir: Instruction
363 *
364 * Returns 0 since it always succeeds.
365 */
366static int mflo_func(struct pt_regs *regs, u32 ir)
367{
368 if (MIPSInst_RD(ir))
369 regs->regs[MIPSInst_RD(ir)] = regs->lo;
370
371 MIPS_R2_STATS(hilo);
372
373 return 0;
374}
375
376/**
377 * mtlo_func - Emulate a MTLO instruction
378 * @regs: Process register set
379 * @ir: Instruction
380 *
381 * Returns 0 since it always succeeds.
382 */
383static int mtlo_func(struct pt_regs *regs, u32 ir)
384{
385 regs->lo = regs->regs[MIPSInst_RS(ir)];
386
387 MIPS_R2_STATS(hilo);
388
389 return 0;
390}
391
392/**
393 * mult_func - Emulate a MULT instruction
394 * @regs: Process register set
395 * @ir: Instruction
396 *
397 * Returns 0 since it always succeeds.
398 */
399static int mult_func(struct pt_regs *regs, u32 ir)
400{
401 s64 res;
402 s32 rt, rs;
403
404 rt = regs->regs[MIPSInst_RT(ir)];
405 rs = regs->regs[MIPSInst_RS(ir)];
406 res = (s64)rt * (s64)rs;
407
408 rs = res;
409 regs->lo = (s64)rs;
410 rt = res >> 32;
411 res = (s64)rt;
412 regs->hi = res;
413
414 MIPS_R2_STATS(muls);
415
416 return 0;
417}
418
419/**
420 * multu_func - Emulate a MULTU instruction
421 * @regs: Process register set
422 * @ir: Instruction
423 *
424 * Returns 0 since it always succeeds.
425 */
426static int multu_func(struct pt_regs *regs, u32 ir)
427{
428 u64 res;
429 u32 rt, rs;
430
431 rt = regs->regs[MIPSInst_RT(ir)];
432 rs = regs->regs[MIPSInst_RS(ir)];
433 res = (u64)rt * (u64)rs;
434 rt = res;
435 regs->lo = (s64)rt;
436 regs->hi = (s64)(res >> 32);
437
438 MIPS_R2_STATS(muls);
439
440 return 0;
441}
442
443/**
444 * div_func - Emulate a DIV instruction
445 * @regs: Process register set
446 * @ir: Instruction
447 *
448 * Returns 0 since it always succeeds.
449 */
450static int div_func(struct pt_regs *regs, u32 ir)
451{
452 s32 rt, rs;
453
454 rt = regs->regs[MIPSInst_RT(ir)];
455 rs = regs->regs[MIPSInst_RS(ir)];
456
457 regs->lo = (s64)(rs / rt);
458 regs->hi = (s64)(rs % rt);
459
460 MIPS_R2_STATS(divs);
461
462 return 0;
463}
464
465/**
466 * divu_func - Emulate a DIVU instruction
467 * @regs: Process register set
468 * @ir: Instruction
469 *
470 * Returns 0 since it always succeeds.
471 */
472static int divu_func(struct pt_regs *regs, u32 ir)
473{
474 u32 rt, rs;
475
476 rt = regs->regs[MIPSInst_RT(ir)];
477 rs = regs->regs[MIPSInst_RS(ir)];
478
479 regs->lo = (s64)(rs / rt);
480 regs->hi = (s64)(rs % rt);
481
482 MIPS_R2_STATS(divs);
483
484 return 0;
485}
486
487/**
488 * dmult_func - Emulate a DMULT instruction
489 * @regs: Process register set
490 * @ir: Instruction
491 *
492 * Returns 0 on success or SIGILL for 32-bit kernels.
493 */
494static int dmult_func(struct pt_regs *regs, u32 ir)
495{
496 s64 res;
497 s64 rt, rs;
498
499 if (config_enabled(CONFIG_32BIT))
500 return SIGILL;
501
502 rt = regs->regs[MIPSInst_RT(ir)];
503 rs = regs->regs[MIPSInst_RS(ir)];
504 res = rt * rs;
505
506 regs->lo = res;
507 __asm__ __volatile__(
508 "dmuh %0, %1, %2\t\n"
509 : "=r"(res)
510 : "r"(rt), "r"(rs));
511
512 regs->hi = res;
513
514 MIPS_R2_STATS(muls);
515
516 return 0;
517}
518
519/**
520 * dmultu_func - Emulate a DMULTU instruction
521 * @regs: Process register set
522 * @ir: Instruction
523 *
524 * Returns 0 on success or SIGILL for 32-bit kernels.
525 */
526static int dmultu_func(struct pt_regs *regs, u32 ir)
527{
528 u64 res;
529 u64 rt, rs;
530
531 if (config_enabled(CONFIG_32BIT))
532 return SIGILL;
533
534 rt = regs->regs[MIPSInst_RT(ir)];
535 rs = regs->regs[MIPSInst_RS(ir)];
536 res = rt * rs;
537
538 regs->lo = res;
539 __asm__ __volatile__(
540 "dmuhu %0, %1, %2\t\n"
541 : "=r"(res)
542 : "r"(rt), "r"(rs));
543
544 regs->hi = res;
545
546 MIPS_R2_STATS(muls);
547
548 return 0;
549}
550
551/**
552 * ddiv_func - Emulate a DDIV instruction
553 * @regs: Process register set
554 * @ir: Instruction
555 *
556 * Returns 0 on success or SIGILL for 32-bit kernels.
557 */
558static int ddiv_func(struct pt_regs *regs, u32 ir)
559{
560 s64 rt, rs;
561
562 if (config_enabled(CONFIG_32BIT))
563 return SIGILL;
564
565 rt = regs->regs[MIPSInst_RT(ir)];
566 rs = regs->regs[MIPSInst_RS(ir)];
567
568 regs->lo = rs / rt;
569 regs->hi = rs % rt;
570
571 MIPS_R2_STATS(divs);
572
573 return 0;
574}
575
576/**
577 * ddivu_func - Emulate a DDIVU instruction
578 * @regs: Process register set
579 * @ir: Instruction
580 *
581 * Returns 0 on success or SIGILL for 32-bit kernels.
582 */
583static int ddivu_func(struct pt_regs *regs, u32 ir)
584{
585 u64 rt, rs;
586
587 if (config_enabled(CONFIG_32BIT))
588 return SIGILL;
589
590 rt = regs->regs[MIPSInst_RT(ir)];
591 rs = regs->regs[MIPSInst_RS(ir)];
592
593 regs->lo = rs / rt;
594 regs->hi = rs % rt;
595
596 MIPS_R2_STATS(divs);
597
598 return 0;
599}
600
601/* R6 removed instructions for the SPECIAL opcode */
602static struct r2_decoder_table spec_op_table[] = {
603 { 0xfc1ff83f, 0x00000008, jr_func },
604 { 0xfc00ffff, 0x00000018, mult_func },
605 { 0xfc00ffff, 0x00000019, multu_func },
606 { 0xfc00ffff, 0x0000001c, dmult_func },
607 { 0xfc00ffff, 0x0000001d, dmultu_func },
608 { 0xffff07ff, 0x00000010, mfhi_func },
609 { 0xfc1fffff, 0x00000011, mthi_func },
610 { 0xffff07ff, 0x00000012, mflo_func },
611 { 0xfc1fffff, 0x00000013, mtlo_func },
612 { 0xfc0307ff, 0x00000001, movf_func },
613 { 0xfc0307ff, 0x00010001, movt_func },
614 { 0xfc0007ff, 0x0000000a, movz_func },
615 { 0xfc0007ff, 0x0000000b, movn_func },
616 { 0xfc00ffff, 0x0000001a, div_func },
617 { 0xfc00ffff, 0x0000001b, divu_func },
618 { 0xfc00ffff, 0x0000001e, ddiv_func },
619 { 0xfc00ffff, 0x0000001f, ddivu_func },
620 {}
621};
622
623/**
624 * madd_func - Emulate a MADD instruction
625 * @regs: Process register set
626 * @ir: Instruction
627 *
628 * Returns 0 since it always succeeds.
629 */
630static int madd_func(struct pt_regs *regs, u32 ir)
631{
632 s64 res;
633 s32 rt, rs;
634
635 rt = regs->regs[MIPSInst_RT(ir)];
636 rs = regs->regs[MIPSInst_RS(ir)];
637 res = (s64)rt * (s64)rs;
638 rt = regs->hi;
639 rs = regs->lo;
640 res += ((((s64)rt) << 32) | (u32)rs);
641
642 rt = res;
643 regs->lo = (s64)rt;
644 rs = res >> 32;
645 regs->hi = (s64)rs;
646
647 MIPS_R2_STATS(dsps);
648
649 return 0;
650}
651
652/**
653 * maddu_func - Emulate a MADDU instruction
654 * @regs: Process register set
655 * @ir: Instruction
656 *
657 * Returns 0 since it always succeeds.
658 */
659static int maddu_func(struct pt_regs *regs, u32 ir)
660{
661 u64 res;
662 u32 rt, rs;
663
664 rt = regs->regs[MIPSInst_RT(ir)];
665 rs = regs->regs[MIPSInst_RS(ir)];
666 res = (u64)rt * (u64)rs;
667 rt = regs->hi;
668 rs = regs->lo;
669 res += ((((s64)rt) << 32) | (u32)rs);
670
671 rt = res;
672 regs->lo = (s64)rt;
673 rs = res >> 32;
674 regs->hi = (s64)rs;
675
676 MIPS_R2_STATS(dsps);
677
678 return 0;
679}
680
681/**
682 * msub_func - Emulate a MSUB instruction
683 * @regs: Process register set
684 * @ir: Instruction
685 *
686 * Returns 0 since it always succeeds.
687 */
688static int msub_func(struct pt_regs *regs, u32 ir)
689{
690 s64 res;
691 s32 rt, rs;
692
693 rt = regs->regs[MIPSInst_RT(ir)];
694 rs = regs->regs[MIPSInst_RS(ir)];
695 res = (s64)rt * (s64)rs;
696 rt = regs->hi;
697 rs = regs->lo;
698 res = ((((s64)rt) << 32) | (u32)rs) - res;
699
700 rt = res;
701 regs->lo = (s64)rt;
702 rs = res >> 32;
703 regs->hi = (s64)rs;
704
705 MIPS_R2_STATS(dsps);
706
707 return 0;
708}
709
710/**
711 * msubu_func - Emulate a MSUBU instruction
712 * @regs: Process register set
713 * @ir: Instruction
714 *
715 * Returns 0 since it always succeeds.
716 */
717static int msubu_func(struct pt_regs *regs, u32 ir)
718{
719 u64 res;
720 u32 rt, rs;
721
722 rt = regs->regs[MIPSInst_RT(ir)];
723 rs = regs->regs[MIPSInst_RS(ir)];
724 res = (u64)rt * (u64)rs;
725 rt = regs->hi;
726 rs = regs->lo;
727 res = ((((s64)rt) << 32) | (u32)rs) - res;
728
729 rt = res;
730 regs->lo = (s64)rt;
731 rs = res >> 32;
732 regs->hi = (s64)rs;
733
734 MIPS_R2_STATS(dsps);
735
736 return 0;
737}
738
739/**
740 * mul_func - Emulate a MUL instruction
741 * @regs: Process register set
742 * @ir: Instruction
743 *
744 * Returns 0 since it always succeeds.
745 */
746static int mul_func(struct pt_regs *regs, u32 ir)
747{
748 s64 res;
749 s32 rt, rs;
750
751 if (!MIPSInst_RD(ir))
752 return 0;
753 rt = regs->regs[MIPSInst_RT(ir)];
754 rs = regs->regs[MIPSInst_RS(ir)];
755 res = (s64)rt * (s64)rs;
756
757 rs = res;
758 regs->regs[MIPSInst_RD(ir)] = (s64)rs;
759
760 MIPS_R2_STATS(muls);
761
762 return 0;
763}
764
765/**
766 * clz_func - Emulate a CLZ instruction
767 * @regs: Process register set
768 * @ir: Instruction
769 *
770 * Returns 0 since it always succeeds.
771 */
772static int clz_func(struct pt_regs *regs, u32 ir)
773{
774 u32 res;
775 u32 rs;
776
777 if (!MIPSInst_RD(ir))
778 return 0;
779
780 rs = regs->regs[MIPSInst_RS(ir)];
781 __asm__ __volatile__("clz %0, %1" : "=r"(res) : "r"(rs));
782 regs->regs[MIPSInst_RD(ir)] = res;
783
784 MIPS_R2_STATS(bops);
785
786 return 0;
787}
788
789/**
790 * clo_func - Emulate a CLO instruction
791 * @regs: Process register set
792 * @ir: Instruction
793 *
794 * Returns 0 since it always succeeds.
795 */
796
797static int clo_func(struct pt_regs *regs, u32 ir)
798{
799 u32 res;
800 u32 rs;
801
802 if (!MIPSInst_RD(ir))
803 return 0;
804
805 rs = regs->regs[MIPSInst_RS(ir)];
806 __asm__ __volatile__("clo %0, %1" : "=r"(res) : "r"(rs));
807 regs->regs[MIPSInst_RD(ir)] = res;
808
809 MIPS_R2_STATS(bops);
810
811 return 0;
812}
813
814/**
815 * dclz_func - Emulate a DCLZ instruction
816 * @regs: Process register set
817 * @ir: Instruction
818 *
819 * Returns 0 since it always succeeds.
820 */
821static int dclz_func(struct pt_regs *regs, u32 ir)
822{
823 u64 res;
824 u64 rs;
825
826 if (config_enabled(CONFIG_32BIT))
827 return SIGILL;
828
829 if (!MIPSInst_RD(ir))
830 return 0;
831
832 rs = regs->regs[MIPSInst_RS(ir)];
833 __asm__ __volatile__("dclz %0, %1" : "=r"(res) : "r"(rs));
834 regs->regs[MIPSInst_RD(ir)] = res;
835
836 MIPS_R2_STATS(bops);
837
838 return 0;
839}
840
841/**
842 * dclo_func - Emulate a DCLO instruction
843 * @regs: Process register set
844 * @ir: Instruction
845 *
846 * Returns 0 since it always succeeds.
847 */
848static int dclo_func(struct pt_regs *regs, u32 ir)
849{
850 u64 res;
851 u64 rs;
852
853 if (config_enabled(CONFIG_32BIT))
854 return SIGILL;
855
856 if (!MIPSInst_RD(ir))
857 return 0;
858
859 rs = regs->regs[MIPSInst_RS(ir)];
860 __asm__ __volatile__("dclo %0, %1" : "=r"(res) : "r"(rs));
861 regs->regs[MIPSInst_RD(ir)] = res;
862
863 MIPS_R2_STATS(bops);
864
865 return 0;
866}
867
868/* R6 removed instructions for the SPECIAL2 opcode */
869static struct r2_decoder_table spec2_op_table[] = {
870 { 0xfc00ffff, 0x70000000, madd_func },
871 { 0xfc00ffff, 0x70000001, maddu_func },
872 { 0xfc0007ff, 0x70000002, mul_func },
873 { 0xfc00ffff, 0x70000004, msub_func },
874 { 0xfc00ffff, 0x70000005, msubu_func },
875 { 0xfc0007ff, 0x70000020, clz_func },
876 { 0xfc0007ff, 0x70000021, clo_func },
877 { 0xfc0007ff, 0x70000024, dclz_func },
878 { 0xfc0007ff, 0x70000025, dclo_func },
879 { }
880};
881
882static inline int mipsr2_find_op_func(struct pt_regs *regs, u32 inst,
883 struct r2_decoder_table *table)
884{
885 struct r2_decoder_table *p;
886 int err;
887
888 for (p = table; p->func; p++) {
889 if ((inst & p->mask) == p->code) {
890 err = (p->func)(regs, inst);
891 return err;
892 }
893 }
894 return SIGILL;
895}
896
897/**
898 * mipsr2_decoder: Decode and emulate a MIPS R2 instruction
899 * @regs: Process register set
900 * @inst: Instruction to decode and emulate
901 */
902int mipsr2_decoder(struct pt_regs *regs, u32 inst)
903{
904 int err = 0;
905 unsigned long vaddr;
906 u32 nir;
907 unsigned long cpc, epc, nepc, r31, res, rs, rt;
908
909 void __user *fault_addr = NULL;
910 int pass = 0;
911
912repeat:
913 r31 = regs->regs[31];
914 epc = regs->cp0_epc;
915 err = compute_return_epc(regs);
916 if (err < 0) {
917 BUG();
918 return SIGEMT;
919 }
920 pr_debug("Emulating the 0x%08x R2 instruction @ 0x%08lx (pass=%d))\n",
921 inst, epc, pass);
922
923 switch (MIPSInst_OPCODE(inst)) {
924 case spec_op:
925 err = mipsr2_find_op_func(regs, inst, spec_op_table);
926 if (err < 0) {
927 /* FPU instruction under JR */
928 regs->cp0_cause |= CAUSEF_BD;
929 goto fpu_emul;
930 }
931 break;
932 case spec2_op:
933 err = mipsr2_find_op_func(regs, inst, spec2_op_table);
934 break;
935 case bcond_op:
936 rt = MIPSInst_RT(inst);
937 rs = MIPSInst_RS(inst);
938 switch (rt) {
939 case tgei_op:
940 if ((long)regs->regs[rs] >= MIPSInst_SIMM(inst))
941 do_trap_or_bp(regs, 0, "TGEI");
942
943 MIPS_R2_STATS(traps);
944
945 break;
946 case tgeiu_op:
947 if (regs->regs[rs] >= MIPSInst_UIMM(inst))
948 do_trap_or_bp(regs, 0, "TGEIU");
949
950 MIPS_R2_STATS(traps);
951
952 break;
953 case tlti_op:
954 if ((long)regs->regs[rs] < MIPSInst_SIMM(inst))
955 do_trap_or_bp(regs, 0, "TLTI");
956
957 MIPS_R2_STATS(traps);
958
959 break;
960 case tltiu_op:
961 if (regs->regs[rs] < MIPSInst_UIMM(inst))
962 do_trap_or_bp(regs, 0, "TLTIU");
963
964 MIPS_R2_STATS(traps);
965
966 break;
967 case teqi_op:
968 if (regs->regs[rs] == MIPSInst_SIMM(inst))
969 do_trap_or_bp(regs, 0, "TEQI");
970
971 MIPS_R2_STATS(traps);
972
973 break;
974 case tnei_op:
975 if (regs->regs[rs] != MIPSInst_SIMM(inst))
976 do_trap_or_bp(regs, 0, "TNEI");
977
978 MIPS_R2_STATS(traps);
979
980 break;
981 case bltzl_op:
982 case bgezl_op:
983 case bltzall_op:
984 case bgezall_op:
985 if (delay_slot(regs)) {
986 err = SIGILL;
987 break;
988 }
989 regs->regs[31] = r31;
990 regs->cp0_epc = epc;
991 err = __compute_return_epc(regs);
992 if (err < 0)
993 return SIGEMT;
994 if (err != BRANCH_LIKELY_TAKEN)
995 break;
996 cpc = regs->cp0_epc;
997 nepc = epc + 4;
998 err = __get_user(nir, (u32 __user *)nepc);
999 if (err) {
1000 err = SIGSEGV;
1001 break;
1002 }
1003 /*
1004 * This will probably be optimized away when
1005 * CONFIG_DEBUG_FS is not enabled
1006 */
1007 switch (rt) {
1008 case bltzl_op:
1009 MIPS_R2BR_STATS(bltzl);
1010 break;
1011 case bgezl_op:
1012 MIPS_R2BR_STATS(bgezl);
1013 break;
1014 case bltzall_op:
1015 MIPS_R2BR_STATS(bltzall);
1016 break;
1017 case bgezall_op:
1018 MIPS_R2BR_STATS(bgezall);
1019 break;
1020 }
1021
1022 switch (MIPSInst_OPCODE(nir)) {
1023 case cop1_op:
1024 case cop1x_op:
1025 case lwc1_op:
1026 case swc1_op:
1027 regs->cp0_cause |= CAUSEF_BD;
1028 goto fpu_emul;
1029 }
1030 if (nir) {
1031 err = mipsr6_emul(regs, nir);
1032 if (err > 0) {
1033 err = mips_dsemul(regs, nir, cpc);
1034 if (err == SIGILL)
1035 err = SIGEMT;
1036 MIPS_R2_STATS(dsemul);
1037 }
1038 }
1039 break;
1040 case bltzal_op:
1041 case bgezal_op:
1042 if (delay_slot(regs)) {
1043 err = SIGILL;
1044 break;
1045 }
1046 regs->regs[31] = r31;
1047 regs->cp0_epc = epc;
1048 err = __compute_return_epc(regs);
1049 if (err < 0)
1050 return SIGEMT;
1051 cpc = regs->cp0_epc;
1052 nepc = epc + 4;
1053 err = __get_user(nir, (u32 __user *)nepc);
1054 if (err) {
1055 err = SIGSEGV;
1056 break;
1057 }
1058 /*
1059 * This will probably be optimized away when
1060 * CONFIG_DEBUG_FS is not enabled
1061 */
1062 switch (rt) {
1063 case bltzal_op:
1064 MIPS_R2BR_STATS(bltzal);
1065 break;
1066 case bgezal_op:
1067 MIPS_R2BR_STATS(bgezal);
1068 break;
1069 }
1070
1071 switch (MIPSInst_OPCODE(nir)) {
1072 case cop1_op:
1073 case cop1x_op:
1074 case lwc1_op:
1075 case swc1_op:
1076 regs->cp0_cause |= CAUSEF_BD;
1077 goto fpu_emul;
1078 }
1079 if (nir) {
1080 err = mipsr6_emul(regs, nir);
1081 if (err > 0) {
1082 err = mips_dsemul(regs, nir, cpc);
1083 if (err == SIGILL)
1084 err = SIGEMT;
1085 MIPS_R2_STATS(dsemul);
1086 }
1087 }
1088 break;
1089 default:
1090 regs->regs[31] = r31;
1091 regs->cp0_epc = epc;
1092 err = SIGILL;
1093 break;
1094 }
1095 break;
1096
1097 case beql_op:
1098 case bnel_op:
1099 case blezl_op:
1100 case bgtzl_op:
1101 if (delay_slot(regs)) {
1102 err = SIGILL;
1103 break;
1104 }
1105 regs->regs[31] = r31;
1106 regs->cp0_epc = epc;
1107 err = __compute_return_epc(regs);
1108 if (err < 0)
1109 return SIGEMT;
1110 if (err != BRANCH_LIKELY_TAKEN)
1111 break;
1112 cpc = regs->cp0_epc;
1113 nepc = epc + 4;
1114 err = __get_user(nir, (u32 __user *)nepc);
1115 if (err) {
1116 err = SIGSEGV;
1117 break;
1118 }
1119 /*
1120 * This will probably be optimized away when
1121 * CONFIG_DEBUG_FS is not enabled
1122 */
1123 switch (MIPSInst_OPCODE(inst)) {
1124 case beql_op:
1125 MIPS_R2BR_STATS(beql);
1126 break;
1127 case bnel_op:
1128 MIPS_R2BR_STATS(bnel);
1129 break;
1130 case blezl_op:
1131 MIPS_R2BR_STATS(blezl);
1132 break;
1133 case bgtzl_op:
1134 MIPS_R2BR_STATS(bgtzl);
1135 break;
1136 }
1137
1138 switch (MIPSInst_OPCODE(nir)) {
1139 case cop1_op:
1140 case cop1x_op:
1141 case lwc1_op:
1142 case swc1_op:
1143 regs->cp0_cause |= CAUSEF_BD;
1144 goto fpu_emul;
1145 }
1146 if (nir) {
1147 err = mipsr6_emul(regs, nir);
1148 if (err > 0) {
1149 err = mips_dsemul(regs, nir, cpc);
1150 if (err == SIGILL)
1151 err = SIGEMT;
1152 MIPS_R2_STATS(dsemul);
1153 }
1154 }
1155 break;
1156 case lwc1_op:
1157 case swc1_op:
1158 case cop1_op:
1159 case cop1x_op:
1160fpu_emul:
1161 regs->regs[31] = r31;
1162 regs->cp0_epc = epc;
1163 if (!used_math()) { /* First time FPU user. */
1164 err = init_fpu();
1165 set_used_math();
1166 }
1167 lose_fpu(1); /* Save FPU state for the emulator. */
1168
1169 err = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 0,
1170 &fault_addr);
1171
1172 /*
1173 * this is a tricky issue - lose_fpu() uses LL/SC atomics
1174 * if FPU is owned and effectively cancels user level LL/SC.
1175 * So, it could be logical to don't restore FPU ownership here.
1176 * But the sequence of multiple FPU instructions is much much
1177 * more often than LL-FPU-SC and I prefer loop here until
1178 * next scheduler cycle cancels FPU ownership
1179 */
1180 own_fpu(1); /* Restore FPU state. */
1181
1182 if (err)
1183 current->thread.cp0_baduaddr = (unsigned long)fault_addr;
1184
1185 MIPS_R2_STATS(fpus);
1186
1187 break;
1188
1189 case lwl_op:
1190 rt = regs->regs[MIPSInst_RT(inst)];
1191 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1192 if (!access_ok(VERIFY_READ, vaddr, 4)) {
1193 current->thread.cp0_baduaddr = vaddr;
1194 err = SIGSEGV;
1195 break;
1196 }
1197 __asm__ __volatile__(
1198 " .set push\n"
1199 " .set reorder\n"
1200#ifdef CONFIG_CPU_LITTLE_ENDIAN
1201 "1:" LB "%1, 0(%2)\n"
1202 INS "%0, %1, 24, 8\n"
1203 " andi %1, %2, 0x3\n"
1204 " beq $0, %1, 9f\n"
1205 ADDIU "%2, %2, -1\n"
1206 "2:" LB "%1, 0(%2)\n"
1207 INS "%0, %1, 16, 8\n"
1208 " andi %1, %2, 0x3\n"
1209 " beq $0, %1, 9f\n"
1210 ADDIU "%2, %2, -1\n"
1211 "3:" LB "%1, 0(%2)\n"
1212 INS "%0, %1, 8, 8\n"
1213 " andi %1, %2, 0x3\n"
1214 " beq $0, %1, 9f\n"
1215 ADDIU "%2, %2, -1\n"
1216 "4:" LB "%1, 0(%2)\n"
1217 INS "%0, %1, 0, 8\n"
1218#else /* !CONFIG_CPU_LITTLE_ENDIAN */
1219 "1:" LB "%1, 0(%2)\n"
1220 INS "%0, %1, 24, 8\n"
1221 ADDIU "%2, %2, 1\n"
1222 " andi %1, %2, 0x3\n"
1223 " beq $0, %1, 9f\n"
1224 "2:" LB "%1, 0(%2)\n"
1225 INS "%0, %1, 16, 8\n"
1226 ADDIU "%2, %2, 1\n"
1227 " andi %1, %2, 0x3\n"
1228 " beq $0, %1, 9f\n"
1229 "3:" LB "%1, 0(%2)\n"
1230 INS "%0, %1, 8, 8\n"
1231 ADDIU "%2, %2, 1\n"
1232 " andi %1, %2, 0x3\n"
1233 " beq $0, %1, 9f\n"
1234 "4:" LB "%1, 0(%2)\n"
1235 INS "%0, %1, 0, 8\n"
1236#endif /* CONFIG_CPU_LITTLE_ENDIAN */
1237 "9: sll %0, %0, 0\n"
1238 "10:\n"
1239 " .insn\n"
1240 " .section .fixup,\"ax\"\n"
1241 "8: li %3,%4\n"
1242 " j 10b\n"
1243 " .previous\n"
1244 " .section __ex_table,\"a\"\n"
1245 " .word 1b,8b\n"
1246 " .word 2b,8b\n"
1247 " .word 3b,8b\n"
1248 " .word 4b,8b\n"
1249 " .previous\n"
1250 " .set pop\n"
1251 : "+&r"(rt), "=&r"(rs),
1252 "+&r"(vaddr), "+&r"(err)
1253 : "i"(SIGSEGV));
1254
1255 if (MIPSInst_RT(inst) && !err)
1256 regs->regs[MIPSInst_RT(inst)] = rt;
1257
1258 MIPS_R2_STATS(loads);
1259
1260 break;
1261
1262 case lwr_op:
1263 rt = regs->regs[MIPSInst_RT(inst)];
1264 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1265 if (!access_ok(VERIFY_READ, vaddr, 4)) {
1266 current->thread.cp0_baduaddr = vaddr;
1267 err = SIGSEGV;
1268 break;
1269 }
1270 __asm__ __volatile__(
1271 " .set push\n"
1272 " .set reorder\n"
1273#ifdef CONFIG_CPU_LITTLE_ENDIAN
1274 "1:" LB "%1, 0(%2)\n"
1275 INS "%0, %1, 0, 8\n"
1276 ADDIU "%2, %2, 1\n"
1277 " andi %1, %2, 0x3\n"
1278 " beq $0, %1, 9f\n"
1279 "2:" LB "%1, 0(%2)\n"
1280 INS "%0, %1, 8, 8\n"
1281 ADDIU "%2, %2, 1\n"
1282 " andi %1, %2, 0x3\n"
1283 " beq $0, %1, 9f\n"
1284 "3:" LB "%1, 0(%2)\n"
1285 INS "%0, %1, 16, 8\n"
1286 ADDIU "%2, %2, 1\n"
1287 " andi %1, %2, 0x3\n"
1288 " beq $0, %1, 9f\n"
1289 "4:" LB "%1, 0(%2)\n"
1290 INS "%0, %1, 24, 8\n"
1291 " sll %0, %0, 0\n"
1292#else /* !CONFIG_CPU_LITTLE_ENDIAN */
1293 "1:" LB "%1, 0(%2)\n"
1294 INS "%0, %1, 0, 8\n"
1295 " andi %1, %2, 0x3\n"
1296 " beq $0, %1, 9f\n"
1297 ADDIU "%2, %2, -1\n"
1298 "2:" LB "%1, 0(%2)\n"
1299 INS "%0, %1, 8, 8\n"
1300 " andi %1, %2, 0x3\n"
1301 " beq $0, %1, 9f\n"
1302 ADDIU "%2, %2, -1\n"
1303 "3:" LB "%1, 0(%2)\n"
1304 INS "%0, %1, 16, 8\n"
1305 " andi %1, %2, 0x3\n"
1306 " beq $0, %1, 9f\n"
1307 ADDIU "%2, %2, -1\n"
1308 "4:" LB "%1, 0(%2)\n"
1309 INS "%0, %1, 24, 8\n"
1310 " sll %0, %0, 0\n"
1311#endif /* CONFIG_CPU_LITTLE_ENDIAN */
1312 "9:\n"
1313 "10:\n"
1314 " .insn\n"
1315 " .section .fixup,\"ax\"\n"
1316 "8: li %3,%4\n"
1317 " j 10b\n"
1318 " .previous\n"
1319 " .section __ex_table,\"a\"\n"
1320 " .word 1b,8b\n"
1321 " .word 2b,8b\n"
1322 " .word 3b,8b\n"
1323 " .word 4b,8b\n"
1324 " .previous\n"
1325 " .set pop\n"
1326 : "+&r"(rt), "=&r"(rs),
1327 "+&r"(vaddr), "+&r"(err)
1328 : "i"(SIGSEGV));
1329 if (MIPSInst_RT(inst) && !err)
1330 regs->regs[MIPSInst_RT(inst)] = rt;
1331
1332 MIPS_R2_STATS(loads);
1333
1334 break;
1335
1336 case swl_op:
1337 rt = regs->regs[MIPSInst_RT(inst)];
1338 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1339 if (!access_ok(VERIFY_WRITE, vaddr, 4)) {
1340 current->thread.cp0_baduaddr = vaddr;
1341 err = SIGSEGV;
1342 break;
1343 }
1344 __asm__ __volatile__(
1345 " .set push\n"
1346 " .set reorder\n"
1347#ifdef CONFIG_CPU_LITTLE_ENDIAN
1348 EXT "%1, %0, 24, 8\n"
1349 "1:" SB "%1, 0(%2)\n"
1350 " andi %1, %2, 0x3\n"
1351 " beq $0, %1, 9f\n"
1352 ADDIU "%2, %2, -1\n"
1353 EXT "%1, %0, 16, 8\n"
1354 "2:" SB "%1, 0(%2)\n"
1355 " andi %1, %2, 0x3\n"
1356 " beq $0, %1, 9f\n"
1357 ADDIU "%2, %2, -1\n"
1358 EXT "%1, %0, 8, 8\n"
1359 "3:" SB "%1, 0(%2)\n"
1360 " andi %1, %2, 0x3\n"
1361 " beq $0, %1, 9f\n"
1362 ADDIU "%2, %2, -1\n"
1363 EXT "%1, %0, 0, 8\n"
1364 "4:" SB "%1, 0(%2)\n"
1365#else /* !CONFIG_CPU_LITTLE_ENDIAN */
1366 EXT "%1, %0, 24, 8\n"
1367 "1:" SB "%1, 0(%2)\n"
1368 ADDIU "%2, %2, 1\n"
1369 " andi %1, %2, 0x3\n"
1370 " beq $0, %1, 9f\n"
1371 EXT "%1, %0, 16, 8\n"
1372 "2:" SB "%1, 0(%2)\n"
1373 ADDIU "%2, %2, 1\n"
1374 " andi %1, %2, 0x3\n"
1375 " beq $0, %1, 9f\n"
1376 EXT "%1, %0, 8, 8\n"
1377 "3:" SB "%1, 0(%2)\n"
1378 ADDIU "%2, %2, 1\n"
1379 " andi %1, %2, 0x3\n"
1380 " beq $0, %1, 9f\n"
1381 EXT "%1, %0, 0, 8\n"
1382 "4:" SB "%1, 0(%2)\n"
1383#endif /* CONFIG_CPU_LITTLE_ENDIAN */
1384 "9:\n"
1385 " .insn\n"
1386 " .section .fixup,\"ax\"\n"
1387 "8: li %3,%4\n"
1388 " j 9b\n"
1389 " .previous\n"
1390 " .section __ex_table,\"a\"\n"
1391 " .word 1b,8b\n"
1392 " .word 2b,8b\n"
1393 " .word 3b,8b\n"
1394 " .word 4b,8b\n"
1395 " .previous\n"
1396 " .set pop\n"
1397 : "+&r"(rt), "=&r"(rs),
1398 "+&r"(vaddr), "+&r"(err)
1399 : "i"(SIGSEGV)
1400 : "memory");
1401
1402 MIPS_R2_STATS(stores);
1403
1404 break;
1405
1406 case swr_op:
1407 rt = regs->regs[MIPSInst_RT(inst)];
1408 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1409 if (!access_ok(VERIFY_WRITE, vaddr, 4)) {
1410 current->thread.cp0_baduaddr = vaddr;
1411 err = SIGSEGV;
1412 break;
1413 }
1414 __asm__ __volatile__(
1415 " .set push\n"
1416 " .set reorder\n"
1417#ifdef CONFIG_CPU_LITTLE_ENDIAN
1418 EXT "%1, %0, 0, 8\n"
1419 "1:" SB "%1, 0(%2)\n"
1420 ADDIU "%2, %2, 1\n"
1421 " andi %1, %2, 0x3\n"
1422 " beq $0, %1, 9f\n"
1423 EXT "%1, %0, 8, 8\n"
1424 "2:" SB "%1, 0(%2)\n"
1425 ADDIU "%2, %2, 1\n"
1426 " andi %1, %2, 0x3\n"
1427 " beq $0, %1, 9f\n"
1428 EXT "%1, %0, 16, 8\n"
1429 "3:" SB "%1, 0(%2)\n"
1430 ADDIU "%2, %2, 1\n"
1431 " andi %1, %2, 0x3\n"
1432 " beq $0, %1, 9f\n"
1433 EXT "%1, %0, 24, 8\n"
1434 "4:" SB "%1, 0(%2)\n"
1435#else /* !CONFIG_CPU_LITTLE_ENDIAN */
1436 EXT "%1, %0, 0, 8\n"
1437 "1:" SB "%1, 0(%2)\n"
1438 " andi %1, %2, 0x3\n"
1439 " beq $0, %1, 9f\n"
1440 ADDIU "%2, %2, -1\n"
1441 EXT "%1, %0, 8, 8\n"
1442 "2:" SB "%1, 0(%2)\n"
1443 " andi %1, %2, 0x3\n"
1444 " beq $0, %1, 9f\n"
1445 ADDIU "%2, %2, -1\n"
1446 EXT "%1, %0, 16, 8\n"
1447 "3:" SB "%1, 0(%2)\n"
1448 " andi %1, %2, 0x3\n"
1449 " beq $0, %1, 9f\n"
1450 ADDIU "%2, %2, -1\n"
1451 EXT "%1, %0, 24, 8\n"
1452 "4:" SB "%1, 0(%2)\n"
1453#endif /* CONFIG_CPU_LITTLE_ENDIAN */
1454 "9:\n"
1455 " .insn\n"
1456 " .section .fixup,\"ax\"\n"
1457 "8: li %3,%4\n"
1458 " j 9b\n"
1459 " .previous\n"
1460 " .section __ex_table,\"a\"\n"
1461 " .word 1b,8b\n"
1462 " .word 2b,8b\n"
1463 " .word 3b,8b\n"
1464 " .word 4b,8b\n"
1465 " .previous\n"
1466 " .set pop\n"
1467 : "+&r"(rt), "=&r"(rs),
1468 "+&r"(vaddr), "+&r"(err)
1469 : "i"(SIGSEGV)
1470 : "memory");
1471
1472 MIPS_R2_STATS(stores);
1473
1474 break;
1475
1476 case ldl_op:
1477 if (config_enabled(CONFIG_32BIT)) {
1478 err = SIGILL;
1479 break;
1480 }
1481
1482 rt = regs->regs[MIPSInst_RT(inst)];
1483 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1484 if (!access_ok(VERIFY_READ, vaddr, 8)) {
1485 current->thread.cp0_baduaddr = vaddr;
1486 err = SIGSEGV;
1487 break;
1488 }
1489 __asm__ __volatile__(
1490 " .set push\n"
1491 " .set reorder\n"
1492#ifdef CONFIG_CPU_LITTLE_ENDIAN
1493 "1: lb %1, 0(%2)\n"
1494 " dinsu %0, %1, 56, 8\n"
1495 " andi %1, %2, 0x7\n"
1496 " beq $0, %1, 9f\n"
1497 " daddiu %2, %2, -1\n"
1498 "2: lb %1, 0(%2)\n"
1499 " dinsu %0, %1, 48, 8\n"
1500 " andi %1, %2, 0x7\n"
1501 " beq $0, %1, 9f\n"
1502 " daddiu %2, %2, -1\n"
1503 "3: lb %1, 0(%2)\n"
1504 " dinsu %0, %1, 40, 8\n"
1505 " andi %1, %2, 0x7\n"
1506 " beq $0, %1, 9f\n"
1507 " daddiu %2, %2, -1\n"
1508 "4: lb %1, 0(%2)\n"
1509 " dinsu %0, %1, 32, 8\n"
1510 " andi %1, %2, 0x7\n"
1511 " beq $0, %1, 9f\n"
1512 " daddiu %2, %2, -1\n"
1513 "5: lb %1, 0(%2)\n"
1514 " dins %0, %1, 24, 8\n"
1515 " andi %1, %2, 0x7\n"
1516 " beq $0, %1, 9f\n"
1517 " daddiu %2, %2, -1\n"
1518 "6: lb %1, 0(%2)\n"
1519 " dins %0, %1, 16, 8\n"
1520 " andi %1, %2, 0x7\n"
1521 " beq $0, %1, 9f\n"
1522 " daddiu %2, %2, -1\n"
1523 "7: lb %1, 0(%2)\n"
1524 " dins %0, %1, 8, 8\n"
1525 " andi %1, %2, 0x7\n"
1526 " beq $0, %1, 9f\n"
1527 " daddiu %2, %2, -1\n"
1528 "0: lb %1, 0(%2)\n"
1529 " dins %0, %1, 0, 8\n"
1530#else /* !CONFIG_CPU_LITTLE_ENDIAN */
1531 "1: lb %1, 0(%2)\n"
1532 " dinsu %0, %1, 56, 8\n"
1533 " daddiu %2, %2, 1\n"
1534 " andi %1, %2, 0x7\n"
1535 " beq $0, %1, 9f\n"
1536 "2: lb %1, 0(%2)\n"
1537 " dinsu %0, %1, 48, 8\n"
1538 " daddiu %2, %2, 1\n"
1539 " andi %1, %2, 0x7\n"
1540 " beq $0, %1, 9f\n"
1541 "3: lb %1, 0(%2)\n"
1542 " dinsu %0, %1, 40, 8\n"
1543 " daddiu %2, %2, 1\n"
1544 " andi %1, %2, 0x7\n"
1545 " beq $0, %1, 9f\n"
1546 "4: lb %1, 0(%2)\n"
1547 " dinsu %0, %1, 32, 8\n"
1548 " daddiu %2, %2, 1\n"
1549 " andi %1, %2, 0x7\n"
1550 " beq $0, %1, 9f\n"
1551 "5: lb %1, 0(%2)\n"
1552 " dins %0, %1, 24, 8\n"
1553 " daddiu %2, %2, 1\n"
1554 " andi %1, %2, 0x7\n"
1555 " beq $0, %1, 9f\n"
1556 "6: lb %1, 0(%2)\n"
1557 " dins %0, %1, 16, 8\n"
1558 " daddiu %2, %2, 1\n"
1559 " andi %1, %2, 0x7\n"
1560 " beq $0, %1, 9f\n"
1561 "7: lb %1, 0(%2)\n"
1562 " dins %0, %1, 8, 8\n"
1563 " daddiu %2, %2, 1\n"
1564 " andi %1, %2, 0x7\n"
1565 " beq $0, %1, 9f\n"
1566 "0: lb %1, 0(%2)\n"
1567 " dins %0, %1, 0, 8\n"
1568#endif /* CONFIG_CPU_LITTLE_ENDIAN */
1569 "9:\n"
1570 " .insn\n"
1571 " .section .fixup,\"ax\"\n"
1572 "8: li %3,%4\n"
1573 " j 9b\n"
1574 " .previous\n"
1575 " .section __ex_table,\"a\"\n"
1576 " .word 1b,8b\n"
1577 " .word 2b,8b\n"
1578 " .word 3b,8b\n"
1579 " .word 4b,8b\n"
1580 " .word 5b,8b\n"
1581 " .word 6b,8b\n"
1582 " .word 7b,8b\n"
1583 " .word 0b,8b\n"
1584 " .previous\n"
1585 " .set pop\n"
1586 : "+&r"(rt), "=&r"(rs),
1587 "+&r"(vaddr), "+&r"(err)
1588 : "i"(SIGSEGV));
1589 if (MIPSInst_RT(inst) && !err)
1590 regs->regs[MIPSInst_RT(inst)] = rt;
1591
1592 MIPS_R2_STATS(loads);
1593 break;
1594
1595 case ldr_op:
1596 if (config_enabled(CONFIG_32BIT)) {
1597 err = SIGILL;
1598 break;
1599 }
1600
1601 rt = regs->regs[MIPSInst_RT(inst)];
1602 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1603 if (!access_ok(VERIFY_READ, vaddr, 8)) {
1604 current->thread.cp0_baduaddr = vaddr;
1605 err = SIGSEGV;
1606 break;
1607 }
1608 __asm__ __volatile__(
1609 " .set push\n"
1610 " .set reorder\n"
1611#ifdef CONFIG_CPU_LITTLE_ENDIAN
1612 "1: lb %1, 0(%2)\n"
1613 " dins %0, %1, 0, 8\n"
1614 " daddiu %2, %2, 1\n"
1615 " andi %1, %2, 0x7\n"
1616 " beq $0, %1, 9f\n"
1617 "2: lb %1, 0(%2)\n"
1618 " dins %0, %1, 8, 8\n"
1619 " daddiu %2, %2, 1\n"
1620 " andi %1, %2, 0x7\n"
1621 " beq $0, %1, 9f\n"
1622 "3: lb %1, 0(%2)\n"
1623 " dins %0, %1, 16, 8\n"
1624 " daddiu %2, %2, 1\n"
1625 " andi %1, %2, 0x7\n"
1626 " beq $0, %1, 9f\n"
1627 "4: lb %1, 0(%2)\n"
1628 " dins %0, %1, 24, 8\n"
1629 " daddiu %2, %2, 1\n"
1630 " andi %1, %2, 0x7\n"
1631 " beq $0, %1, 9f\n"
1632 "5: lb %1, 0(%2)\n"
1633 " dinsu %0, %1, 32, 8\n"
1634 " daddiu %2, %2, 1\n"
1635 " andi %1, %2, 0x7\n"
1636 " beq $0, %1, 9f\n"
1637 "6: lb %1, 0(%2)\n"
1638 " dinsu %0, %1, 40, 8\n"
1639 " daddiu %2, %2, 1\n"
1640 " andi %1, %2, 0x7\n"
1641 " beq $0, %1, 9f\n"
1642 "7: lb %1, 0(%2)\n"
1643 " dinsu %0, %1, 48, 8\n"
1644 " daddiu %2, %2, 1\n"
1645 " andi %1, %2, 0x7\n"
1646 " beq $0, %1, 9f\n"
1647 "0: lb %1, 0(%2)\n"
1648 " dinsu %0, %1, 56, 8\n"
1649#else /* !CONFIG_CPU_LITTLE_ENDIAN */
1650 "1: lb %1, 0(%2)\n"
1651 " dins %0, %1, 0, 8\n"
1652 " andi %1, %2, 0x7\n"
1653 " beq $0, %1, 9f\n"
1654 " daddiu %2, %2, -1\n"
1655 "2: lb %1, 0(%2)\n"
1656 " dins %0, %1, 8, 8\n"
1657 " andi %1, %2, 0x7\n"
1658 " beq $0, %1, 9f\n"
1659 " daddiu %2, %2, -1\n"
1660 "3: lb %1, 0(%2)\n"
1661 " dins %0, %1, 16, 8\n"
1662 " andi %1, %2, 0x7\n"
1663 " beq $0, %1, 9f\n"
1664 " daddiu %2, %2, -1\n"
1665 "4: lb %1, 0(%2)\n"
1666 " dins %0, %1, 24, 8\n"
1667 " andi %1, %2, 0x7\n"
1668 " beq $0, %1, 9f\n"
1669 " daddiu %2, %2, -1\n"
1670 "5: lb %1, 0(%2)\n"
1671 " dinsu %0, %1, 32, 8\n"
1672 " andi %1, %2, 0x7\n"
1673 " beq $0, %1, 9f\n"
1674 " daddiu %2, %2, -1\n"
1675 "6: lb %1, 0(%2)\n"
1676 " dinsu %0, %1, 40, 8\n"
1677 " andi %1, %2, 0x7\n"
1678 " beq $0, %1, 9f\n"
1679 " daddiu %2, %2, -1\n"
1680 "7: lb %1, 0(%2)\n"
1681 " dinsu %0, %1, 48, 8\n"
1682 " andi %1, %2, 0x7\n"
1683 " beq $0, %1, 9f\n"
1684 " daddiu %2, %2, -1\n"
1685 "0: lb %1, 0(%2)\n"
1686 " dinsu %0, %1, 56, 8\n"
1687#endif /* CONFIG_CPU_LITTLE_ENDIAN */
1688 "9:\n"
1689 " .insn\n"
1690 " .section .fixup,\"ax\"\n"
1691 "8: li %3,%4\n"
1692 " j 9b\n"
1693 " .previous\n"
1694 " .section __ex_table,\"a\"\n"
1695 " .word 1b,8b\n"
1696 " .word 2b,8b\n"
1697 " .word 3b,8b\n"
1698 " .word 4b,8b\n"
1699 " .word 5b,8b\n"
1700 " .word 6b,8b\n"
1701 " .word 7b,8b\n"
1702 " .word 0b,8b\n"
1703 " .previous\n"
1704 " .set pop\n"
1705 : "+&r"(rt), "=&r"(rs),
1706 "+&r"(vaddr), "+&r"(err)
1707 : "i"(SIGSEGV));
1708 if (MIPSInst_RT(inst) && !err)
1709 regs->regs[MIPSInst_RT(inst)] = rt;
1710
1711 MIPS_R2_STATS(loads);
1712 break;
1713
1714 case sdl_op:
1715 if (config_enabled(CONFIG_32BIT)) {
1716 err = SIGILL;
1717 break;
1718 }
1719
1720 rt = regs->regs[MIPSInst_RT(inst)];
1721 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1722 if (!access_ok(VERIFY_WRITE, vaddr, 8)) {
1723 current->thread.cp0_baduaddr = vaddr;
1724 err = SIGSEGV;
1725 break;
1726 }
1727 __asm__ __volatile__(
1728 " .set push\n"
1729 " .set reorder\n"
1730#ifdef CONFIG_CPU_LITTLE_ENDIAN
1731 " dextu %1, %0, 56, 8\n"
1732 "1: sb %1, 0(%2)\n"
1733 " andi %1, %2, 0x7\n"
1734 " beq $0, %1, 9f\n"
1735 " daddiu %2, %2, -1\n"
1736 " dextu %1, %0, 48, 8\n"
1737 "2: sb %1, 0(%2)\n"
1738 " andi %1, %2, 0x7\n"
1739 " beq $0, %1, 9f\n"
1740 " daddiu %2, %2, -1\n"
1741 " dextu %1, %0, 40, 8\n"
1742 "3: sb %1, 0(%2)\n"
1743 " andi %1, %2, 0x7\n"
1744 " beq $0, %1, 9f\n"
1745 " daddiu %2, %2, -1\n"
1746 " dextu %1, %0, 32, 8\n"
1747 "4: sb %1, 0(%2)\n"
1748 " andi %1, %2, 0x7\n"
1749 " beq $0, %1, 9f\n"
1750 " daddiu %2, %2, -1\n"
1751 " dext %1, %0, 24, 8\n"
1752 "5: sb %1, 0(%2)\n"
1753 " andi %1, %2, 0x7\n"
1754 " beq $0, %1, 9f\n"
1755 " daddiu %2, %2, -1\n"
1756 " dext %1, %0, 16, 8\n"
1757 "6: sb %1, 0(%2)\n"
1758 " andi %1, %2, 0x7\n"
1759 " beq $0, %1, 9f\n"
1760 " daddiu %2, %2, -1\n"
1761 " dext %1, %0, 8, 8\n"
1762 "7: sb %1, 0(%2)\n"
1763 " andi %1, %2, 0x7\n"
1764 " beq $0, %1, 9f\n"
1765 " daddiu %2, %2, -1\n"
1766 " dext %1, %0, 0, 8\n"
1767 "0: sb %1, 0(%2)\n"
1768#else /* !CONFIG_CPU_LITTLE_ENDIAN */
1769 " dextu %1, %0, 56, 8\n"
1770 "1: sb %1, 0(%2)\n"
1771 " daddiu %2, %2, 1\n"
1772 " andi %1, %2, 0x7\n"
1773 " beq $0, %1, 9f\n"
1774 " dextu %1, %0, 48, 8\n"
1775 "2: sb %1, 0(%2)\n"
1776 " daddiu %2, %2, 1\n"
1777 " andi %1, %2, 0x7\n"
1778 " beq $0, %1, 9f\n"
1779 " dextu %1, %0, 40, 8\n"
1780 "3: sb %1, 0(%2)\n"
1781 " daddiu %2, %2, 1\n"
1782 " andi %1, %2, 0x7\n"
1783 " beq $0, %1, 9f\n"
1784 " dextu %1, %0, 32, 8\n"
1785 "4: sb %1, 0(%2)\n"
1786 " daddiu %2, %2, 1\n"
1787 " andi %1, %2, 0x7\n"
1788 " beq $0, %1, 9f\n"
1789 " dext %1, %0, 24, 8\n"
1790 "5: sb %1, 0(%2)\n"
1791 " daddiu %2, %2, 1\n"
1792 " andi %1, %2, 0x7\n"
1793 " beq $0, %1, 9f\n"
1794 " dext %1, %0, 16, 8\n"
1795 "6: sb %1, 0(%2)\n"
1796 " daddiu %2, %2, 1\n"
1797 " andi %1, %2, 0x7\n"
1798 " beq $0, %1, 9f\n"
1799 " dext %1, %0, 8, 8\n"
1800 "7: sb %1, 0(%2)\n"
1801 " daddiu %2, %2, 1\n"
1802 " andi %1, %2, 0x7\n"
1803 " beq $0, %1, 9f\n"
1804 " dext %1, %0, 0, 8\n"
1805 "0: sb %1, 0(%2)\n"
1806#endif /* CONFIG_CPU_LITTLE_ENDIAN */
1807 "9:\n"
1808 " .insn\n"
1809 " .section .fixup,\"ax\"\n"
1810 "8: li %3,%4\n"
1811 " j 9b\n"
1812 " .previous\n"
1813 " .section __ex_table,\"a\"\n"
1814 " .word 1b,8b\n"
1815 " .word 2b,8b\n"
1816 " .word 3b,8b\n"
1817 " .word 4b,8b\n"
1818 " .word 5b,8b\n"
1819 " .word 6b,8b\n"
1820 " .word 7b,8b\n"
1821 " .word 0b,8b\n"
1822 " .previous\n"
1823 " .set pop\n"
1824 : "+&r"(rt), "=&r"(rs),
1825 "+&r"(vaddr), "+&r"(err)
1826 : "i"(SIGSEGV)
1827 : "memory");
1828
1829 MIPS_R2_STATS(stores);
1830 break;
1831
1832 case sdr_op:
1833 if (config_enabled(CONFIG_32BIT)) {
1834 err = SIGILL;
1835 break;
1836 }
1837
1838 rt = regs->regs[MIPSInst_RT(inst)];
1839 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1840 if (!access_ok(VERIFY_WRITE, vaddr, 8)) {
1841 current->thread.cp0_baduaddr = vaddr;
1842 err = SIGSEGV;
1843 break;
1844 }
1845 __asm__ __volatile__(
1846 " .set push\n"
1847 " .set reorder\n"
1848#ifdef CONFIG_CPU_LITTLE_ENDIAN
1849 " dext %1, %0, 0, 8\n"
1850 "1: sb %1, 0(%2)\n"
1851 " daddiu %2, %2, 1\n"
1852 " andi %1, %2, 0x7\n"
1853 " beq $0, %1, 9f\n"
1854 " dext %1, %0, 8, 8\n"
1855 "2: sb %1, 0(%2)\n"
1856 " daddiu %2, %2, 1\n"
1857 " andi %1, %2, 0x7\n"
1858 " beq $0, %1, 9f\n"
1859 " dext %1, %0, 16, 8\n"
1860 "3: sb %1, 0(%2)\n"
1861 " daddiu %2, %2, 1\n"
1862 " andi %1, %2, 0x7\n"
1863 " beq $0, %1, 9f\n"
1864 " dext %1, %0, 24, 8\n"
1865 "4: sb %1, 0(%2)\n"
1866 " daddiu %2, %2, 1\n"
1867 " andi %1, %2, 0x7\n"
1868 " beq $0, %1, 9f\n"
1869 " dextu %1, %0, 32, 8\n"
1870 "5: sb %1, 0(%2)\n"
1871 " daddiu %2, %2, 1\n"
1872 " andi %1, %2, 0x7\n"
1873 " beq $0, %1, 9f\n"
1874 " dextu %1, %0, 40, 8\n"
1875 "6: sb %1, 0(%2)\n"
1876 " daddiu %2, %2, 1\n"
1877 " andi %1, %2, 0x7\n"
1878 " beq $0, %1, 9f\n"
1879 " dextu %1, %0, 48, 8\n"
1880 "7: sb %1, 0(%2)\n"
1881 " daddiu %2, %2, 1\n"
1882 " andi %1, %2, 0x7\n"
1883 " beq $0, %1, 9f\n"
1884 " dextu %1, %0, 56, 8\n"
1885 "0: sb %1, 0(%2)\n"
1886#else /* !CONFIG_CPU_LITTLE_ENDIAN */
1887 " dext %1, %0, 0, 8\n"
1888 "1: sb %1, 0(%2)\n"
1889 " andi %1, %2, 0x7\n"
1890 " beq $0, %1, 9f\n"
1891 " daddiu %2, %2, -1\n"
1892 " dext %1, %0, 8, 8\n"
1893 "2: sb %1, 0(%2)\n"
1894 " andi %1, %2, 0x7\n"
1895 " beq $0, %1, 9f\n"
1896 " daddiu %2, %2, -1\n"
1897 " dext %1, %0, 16, 8\n"
1898 "3: sb %1, 0(%2)\n"
1899 " andi %1, %2, 0x7\n"
1900 " beq $0, %1, 9f\n"
1901 " daddiu %2, %2, -1\n"
1902 " dext %1, %0, 24, 8\n"
1903 "4: sb %1, 0(%2)\n"
1904 " andi %1, %2, 0x7\n"
1905 " beq $0, %1, 9f\n"
1906 " daddiu %2, %2, -1\n"
1907 " dextu %1, %0, 32, 8\n"
1908 "5: sb %1, 0(%2)\n"
1909 " andi %1, %2, 0x7\n"
1910 " beq $0, %1, 9f\n"
1911 " daddiu %2, %2, -1\n"
1912 " dextu %1, %0, 40, 8\n"
1913 "6: sb %1, 0(%2)\n"
1914 " andi %1, %2, 0x7\n"
1915 " beq $0, %1, 9f\n"
1916 " daddiu %2, %2, -1\n"
1917 " dextu %1, %0, 48, 8\n"
1918 "7: sb %1, 0(%2)\n"
1919 " andi %1, %2, 0x7\n"
1920 " beq $0, %1, 9f\n"
1921 " daddiu %2, %2, -1\n"
1922 " dextu %1, %0, 56, 8\n"
1923 "0: sb %1, 0(%2)\n"
1924#endif /* CONFIG_CPU_LITTLE_ENDIAN */
1925 "9:\n"
1926 " .insn\n"
1927 " .section .fixup,\"ax\"\n"
1928 "8: li %3,%4\n"
1929 " j 9b\n"
1930 " .previous\n"
1931 " .section __ex_table,\"a\"\n"
1932 " .word 1b,8b\n"
1933 " .word 2b,8b\n"
1934 " .word 3b,8b\n"
1935 " .word 4b,8b\n"
1936 " .word 5b,8b\n"
1937 " .word 6b,8b\n"
1938 " .word 7b,8b\n"
1939 " .word 0b,8b\n"
1940 " .previous\n"
1941 " .set pop\n"
1942 : "+&r"(rt), "=&r"(rs),
1943 "+&r"(vaddr), "+&r"(err)
1944 : "i"(SIGSEGV)
1945 : "memory");
1946
1947 MIPS_R2_STATS(stores);
1948
1949 break;
1950 case ll_op:
1951 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1952 if (vaddr & 0x3) {
1953 current->thread.cp0_baduaddr = vaddr;
1954 err = SIGBUS;
1955 break;
1956 }
1957 if (!access_ok(VERIFY_READ, vaddr, 4)) {
1958 current->thread.cp0_baduaddr = vaddr;
1959 err = SIGBUS;
1960 break;
1961 }
1962
1963 if (!cpu_has_rw_llb) {
1964 /*
1965 * An LL/SC block can't be safely emulated without
1966 * a Config5/LLB availability. So it's probably time to
1967 * kill our process before things get any worse. This is
1968 * because Config5/LLB allows us to use ERETNC so that
1969 * the LLAddr/LLB bit is not cleared when we return from
1970 * an exception. MIPS R2 LL/SC instructions trap with an
1971 * RI exception so once we emulate them here, we return
1972 * back to userland with ERETNC. That preserves the
1973 * LLAddr/LLB so the subsequent SC instruction will
1974 * succeed preserving the atomic semantics of the LL/SC
1975 * block. Without that, there is no safe way to emulate
1976 * an LL/SC block in MIPSR2 userland.
1977 */
1978 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
1979 err = SIGKILL;
1980 break;
1981 }
1982
1983 __asm__ __volatile__(
1984 "1:\n"
1985 "ll %0, 0(%2)\n"
1986 "2:\n"
1987 ".insn\n"
1988 ".section .fixup,\"ax\"\n"
1989 "3:\n"
1990 "li %1, %3\n"
1991 "j 2b\n"
1992 ".previous\n"
1993 ".section __ex_table,\"a\"\n"
1994 ".word 1b, 3b\n"
1995 ".previous\n"
1996 : "=&r"(res), "+&r"(err)
1997 : "r"(vaddr), "i"(SIGSEGV)
1998 : "memory");
1999
2000 if (MIPSInst_RT(inst) && !err)
2001 regs->regs[MIPSInst_RT(inst)] = res;
2002 MIPS_R2_STATS(llsc);
2003
2004 break;
2005
2006 case sc_op:
2007 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2008 if (vaddr & 0x3) {
2009 current->thread.cp0_baduaddr = vaddr;
2010 err = SIGBUS;
2011 break;
2012 }
2013 if (!access_ok(VERIFY_WRITE, vaddr, 4)) {
2014 current->thread.cp0_baduaddr = vaddr;
2015 err = SIGBUS;
2016 break;
2017 }
2018
2019 if (!cpu_has_rw_llb) {
2020 /*
2021 * An LL/SC block can't be safely emulated without
2022 * a Config5/LLB availability. So it's probably time to
2023 * kill our process before things get any worse. This is
2024 * because Config5/LLB allows us to use ERETNC so that
2025 * the LLAddr/LLB bit is not cleared when we return from
2026 * an exception. MIPS R2 LL/SC instructions trap with an
2027 * RI exception so once we emulate them here, we return
2028 * back to userland with ERETNC. That preserves the
2029 * LLAddr/LLB so the subsequent SC instruction will
2030 * succeed preserving the atomic semantics of the LL/SC
2031 * block. Without that, there is no safe way to emulate
2032 * an LL/SC block in MIPSR2 userland.
2033 */
2034 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2035 err = SIGKILL;
2036 break;
2037 }
2038
2039 res = regs->regs[MIPSInst_RT(inst)];
2040
2041 __asm__ __volatile__(
2042 "1:\n"
2043 "sc %0, 0(%2)\n"
2044 "2:\n"
2045 ".insn\n"
2046 ".section .fixup,\"ax\"\n"
2047 "3:\n"
2048 "li %1, %3\n"
2049 "j 2b\n"
2050 ".previous\n"
2051 ".section __ex_table,\"a\"\n"
2052 ".word 1b, 3b\n"
2053 ".previous\n"
2054 : "+&r"(res), "+&r"(err)
2055 : "r"(vaddr), "i"(SIGSEGV));
2056
2057 if (MIPSInst_RT(inst) && !err)
2058 regs->regs[MIPSInst_RT(inst)] = res;
2059
2060 MIPS_R2_STATS(llsc);
2061
2062 break;
2063
2064 case lld_op:
2065 if (config_enabled(CONFIG_32BIT)) {
2066 err = SIGILL;
2067 break;
2068 }
2069
2070 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2071 if (vaddr & 0x7) {
2072 current->thread.cp0_baduaddr = vaddr;
2073 err = SIGBUS;
2074 break;
2075 }
2076 if (!access_ok(VERIFY_READ, vaddr, 8)) {
2077 current->thread.cp0_baduaddr = vaddr;
2078 err = SIGBUS;
2079 break;
2080 }
2081
2082 if (!cpu_has_rw_llb) {
2083 /*
2084 * An LL/SC block can't be safely emulated without
2085 * a Config5/LLB availability. So it's probably time to
2086 * kill our process before things get any worse. This is
2087 * because Config5/LLB allows us to use ERETNC so that
2088 * the LLAddr/LLB bit is not cleared when we return from
2089 * an exception. MIPS R2 LL/SC instructions trap with an
2090 * RI exception so once we emulate them here, we return
2091 * back to userland with ERETNC. That preserves the
2092 * LLAddr/LLB so the subsequent SC instruction will
2093 * succeed preserving the atomic semantics of the LL/SC
2094 * block. Without that, there is no safe way to emulate
2095 * an LL/SC block in MIPSR2 userland.
2096 */
2097 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2098 err = SIGKILL;
2099 break;
2100 }
2101
2102 __asm__ __volatile__(
2103 "1:\n"
2104 "lld %0, 0(%2)\n"
2105 "2:\n"
2106 ".insn\n"
2107 ".section .fixup,\"ax\"\n"
2108 "3:\n"
2109 "li %1, %3\n"
2110 "j 2b\n"
2111 ".previous\n"
2112 ".section __ex_table,\"a\"\n"
2113 ".word 1b, 3b\n"
2114 ".previous\n"
2115 : "=&r"(res), "+&r"(err)
2116 : "r"(vaddr), "i"(SIGSEGV)
2117 : "memory");
2118 if (MIPSInst_RT(inst) && !err)
2119 regs->regs[MIPSInst_RT(inst)] = res;
2120
2121 MIPS_R2_STATS(llsc);
2122
2123 break;
2124
2125 case scd_op:
2126 if (config_enabled(CONFIG_32BIT)) {
2127 err = SIGILL;
2128 break;
2129 }
2130
2131 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2132 if (vaddr & 0x7) {
2133 current->thread.cp0_baduaddr = vaddr;
2134 err = SIGBUS;
2135 break;
2136 }
2137 if (!access_ok(VERIFY_WRITE, vaddr, 8)) {
2138 current->thread.cp0_baduaddr = vaddr;
2139 err = SIGBUS;
2140 break;
2141 }
2142
2143 if (!cpu_has_rw_llb) {
2144 /*
2145 * An LL/SC block can't be safely emulated without
2146 * a Config5/LLB availability. So it's probably time to
2147 * kill our process before things get any worse. This is
2148 * because Config5/LLB allows us to use ERETNC so that
2149 * the LLAddr/LLB bit is not cleared when we return from
2150 * an exception. MIPS R2 LL/SC instructions trap with an
2151 * RI exception so once we emulate them here, we return
2152 * back to userland with ERETNC. That preserves the
2153 * LLAddr/LLB so the subsequent SC instruction will
2154 * succeed preserving the atomic semantics of the LL/SC
2155 * block. Without that, there is no safe way to emulate
2156 * an LL/SC block in MIPSR2 userland.
2157 */
2158 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2159 err = SIGKILL;
2160 break;
2161 }
2162
2163 res = regs->regs[MIPSInst_RT(inst)];
2164
2165 __asm__ __volatile__(
2166 "1:\n"
2167 "scd %0, 0(%2)\n"
2168 "2:\n"
2169 ".insn\n"
2170 ".section .fixup,\"ax\"\n"
2171 "3:\n"
2172 "li %1, %3\n"
2173 "j 2b\n"
2174 ".previous\n"
2175 ".section __ex_table,\"a\"\n"
2176 ".word 1b, 3b\n"
2177 ".previous\n"
2178 : "+&r"(res), "+&r"(err)
2179 : "r"(vaddr), "i"(SIGSEGV));
2180
2181 if (MIPSInst_RT(inst) && !err)
2182 regs->regs[MIPSInst_RT(inst)] = res;
2183
2184 MIPS_R2_STATS(llsc);
2185
2186 break;
2187 case pref_op:
2188 /* skip it */
2189 break;
2190 default:
2191 err = SIGILL;
2192 }
2193
2194 /*
2195 * Lets not return to userland just yet. It's constly and
2196 * it's likely we have more R2 instructions to emulate
2197 */
2198 if (!err && (pass++ < MIPS_R2_EMUL_TOTAL_PASS)) {
2199 regs->cp0_cause &= ~CAUSEF_BD;
2200 err = get_user(inst, (u32 __user *)regs->cp0_epc);
2201 if (!err)
2202 goto repeat;
2203
2204 if (err < 0)
2205 err = SIGSEGV;
2206 }
2207
2208 if (err && (err != SIGEMT)) {
2209 regs->regs[31] = r31;
2210 regs->cp0_epc = epc;
2211 }
2212
2213 /* Likely a MIPS R6 compatible instruction */
2214 if (pass && (err == SIGILL))
2215 err = 0;
2216
2217 return err;
2218}
2219
2220#ifdef CONFIG_DEBUG_FS
2221
2222static int mipsr2_stats_show(struct seq_file *s, void *unused)
2223{
2224
2225 seq_printf(s, "Instruction\tTotal\tBDslot\n------------------------------\n");
2226 seq_printf(s, "movs\t\t%ld\t%ld\n",
2227 (unsigned long)__this_cpu_read(mipsr2emustats.movs),
2228 (unsigned long)__this_cpu_read(mipsr2bdemustats.movs));
2229 seq_printf(s, "hilo\t\t%ld\t%ld\n",
2230 (unsigned long)__this_cpu_read(mipsr2emustats.hilo),
2231 (unsigned long)__this_cpu_read(mipsr2bdemustats.hilo));
2232 seq_printf(s, "muls\t\t%ld\t%ld\n",
2233 (unsigned long)__this_cpu_read(mipsr2emustats.muls),
2234 (unsigned long)__this_cpu_read(mipsr2bdemustats.muls));
2235 seq_printf(s, "divs\t\t%ld\t%ld\n",
2236 (unsigned long)__this_cpu_read(mipsr2emustats.divs),
2237 (unsigned long)__this_cpu_read(mipsr2bdemustats.divs));
2238 seq_printf(s, "dsps\t\t%ld\t%ld\n",
2239 (unsigned long)__this_cpu_read(mipsr2emustats.dsps),
2240 (unsigned long)__this_cpu_read(mipsr2bdemustats.dsps));
2241 seq_printf(s, "bops\t\t%ld\t%ld\n",
2242 (unsigned long)__this_cpu_read(mipsr2emustats.bops),
2243 (unsigned long)__this_cpu_read(mipsr2bdemustats.bops));
2244 seq_printf(s, "traps\t\t%ld\t%ld\n",
2245 (unsigned long)__this_cpu_read(mipsr2emustats.traps),
2246 (unsigned long)__this_cpu_read(mipsr2bdemustats.traps));
2247 seq_printf(s, "fpus\t\t%ld\t%ld\n",
2248 (unsigned long)__this_cpu_read(mipsr2emustats.fpus),
2249 (unsigned long)__this_cpu_read(mipsr2bdemustats.fpus));
2250 seq_printf(s, "loads\t\t%ld\t%ld\n",
2251 (unsigned long)__this_cpu_read(mipsr2emustats.loads),
2252 (unsigned long)__this_cpu_read(mipsr2bdemustats.loads));
2253 seq_printf(s, "stores\t\t%ld\t%ld\n",
2254 (unsigned long)__this_cpu_read(mipsr2emustats.stores),
2255 (unsigned long)__this_cpu_read(mipsr2bdemustats.stores));
2256 seq_printf(s, "llsc\t\t%ld\t%ld\n",
2257 (unsigned long)__this_cpu_read(mipsr2emustats.llsc),
2258 (unsigned long)__this_cpu_read(mipsr2bdemustats.llsc));
2259 seq_printf(s, "dsemul\t\t%ld\t%ld\n",
2260 (unsigned long)__this_cpu_read(mipsr2emustats.dsemul),
2261 (unsigned long)__this_cpu_read(mipsr2bdemustats.dsemul));
2262 seq_printf(s, "jr\t\t%ld\n",
2263 (unsigned long)__this_cpu_read(mipsr2bremustats.jrs));
2264 seq_printf(s, "bltzl\t\t%ld\n",
2265 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzl));
2266 seq_printf(s, "bgezl\t\t%ld\n",
2267 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezl));
2268 seq_printf(s, "bltzll\t\t%ld\n",
2269 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzll));
2270 seq_printf(s, "bgezll\t\t%ld\n",
2271 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezll));
2272 seq_printf(s, "bltzal\t\t%ld\n",
2273 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzal));
2274 seq_printf(s, "bgezal\t\t%ld\n",
2275 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezal));
2276 seq_printf(s, "beql\t\t%ld\n",
2277 (unsigned long)__this_cpu_read(mipsr2bremustats.beql));
2278 seq_printf(s, "bnel\t\t%ld\n",
2279 (unsigned long)__this_cpu_read(mipsr2bremustats.bnel));
2280 seq_printf(s, "blezl\t\t%ld\n",
2281 (unsigned long)__this_cpu_read(mipsr2bremustats.blezl));
2282 seq_printf(s, "bgtzl\t\t%ld\n",
2283 (unsigned long)__this_cpu_read(mipsr2bremustats.bgtzl));
2284
2285 return 0;
2286}
2287
2288static int mipsr2_stats_clear_show(struct seq_file *s, void *unused)
2289{
2290 mipsr2_stats_show(s, unused);
2291
2292 __this_cpu_write((mipsr2emustats).movs, 0);
2293 __this_cpu_write((mipsr2bdemustats).movs, 0);
2294 __this_cpu_write((mipsr2emustats).hilo, 0);
2295 __this_cpu_write((mipsr2bdemustats).hilo, 0);
2296 __this_cpu_write((mipsr2emustats).muls, 0);
2297 __this_cpu_write((mipsr2bdemustats).muls, 0);
2298 __this_cpu_write((mipsr2emustats).divs, 0);
2299 __this_cpu_write((mipsr2bdemustats).divs, 0);
2300 __this_cpu_write((mipsr2emustats).dsps, 0);
2301 __this_cpu_write((mipsr2bdemustats).dsps, 0);
2302 __this_cpu_write((mipsr2emustats).bops, 0);
2303 __this_cpu_write((mipsr2bdemustats).bops, 0);
2304 __this_cpu_write((mipsr2emustats).traps, 0);
2305 __this_cpu_write((mipsr2bdemustats).traps, 0);
2306 __this_cpu_write((mipsr2emustats).fpus, 0);
2307 __this_cpu_write((mipsr2bdemustats).fpus, 0);
2308 __this_cpu_write((mipsr2emustats).loads, 0);
2309 __this_cpu_write((mipsr2bdemustats).loads, 0);
2310 __this_cpu_write((mipsr2emustats).stores, 0);
2311 __this_cpu_write((mipsr2bdemustats).stores, 0);
2312 __this_cpu_write((mipsr2emustats).llsc, 0);
2313 __this_cpu_write((mipsr2bdemustats).llsc, 0);
2314 __this_cpu_write((mipsr2emustats).dsemul, 0);
2315 __this_cpu_write((mipsr2bdemustats).dsemul, 0);
2316 __this_cpu_write((mipsr2bremustats).jrs, 0);
2317 __this_cpu_write((mipsr2bremustats).bltzl, 0);
2318 __this_cpu_write((mipsr2bremustats).bgezl, 0);
2319 __this_cpu_write((mipsr2bremustats).bltzll, 0);
2320 __this_cpu_write((mipsr2bremustats).bgezll, 0);
2321 __this_cpu_write((mipsr2bremustats).bltzal, 0);
2322 __this_cpu_write((mipsr2bremustats).bgezal, 0);
2323 __this_cpu_write((mipsr2bremustats).beql, 0);
2324 __this_cpu_write((mipsr2bremustats).bnel, 0);
2325 __this_cpu_write((mipsr2bremustats).blezl, 0);
2326 __this_cpu_write((mipsr2bremustats).bgtzl, 0);
2327
2328 return 0;
2329}
2330
2331static int mipsr2_stats_open(struct inode *inode, struct file *file)
2332{
2333 return single_open(file, mipsr2_stats_show, inode->i_private);
2334}
2335
2336static int mipsr2_stats_clear_open(struct inode *inode, struct file *file)
2337{
2338 return single_open(file, mipsr2_stats_clear_show, inode->i_private);
2339}
2340
2341static const struct file_operations mipsr2_emul_fops = {
2342 .open = mipsr2_stats_open,
2343 .read = seq_read,
2344 .llseek = seq_lseek,
2345 .release = single_release,
2346};
2347
2348static const struct file_operations mipsr2_clear_fops = {
2349 .open = mipsr2_stats_clear_open,
2350 .read = seq_read,
2351 .llseek = seq_lseek,
2352 .release = single_release,
2353};
2354
2355
2356static int __init mipsr2_init_debugfs(void)
2357{
2358 extern struct dentry *mips_debugfs_dir;
2359 struct dentry *mipsr2_emul;
2360
2361 if (!mips_debugfs_dir)
2362 return -ENODEV;
2363
2364 mipsr2_emul = debugfs_create_file("r2_emul_stats", S_IRUGO,
2365 mips_debugfs_dir, NULL,
2366 &mipsr2_emul_fops);
2367 if (!mipsr2_emul)
2368 return -ENOMEM;
2369
2370 mipsr2_emul = debugfs_create_file("r2_emul_stats_clear", S_IRUGO,
2371 mips_debugfs_dir, NULL,
2372 &mipsr2_clear_fops);
2373 if (!mipsr2_emul)
2374 return -ENOMEM;
2375
2376 return 0;
2377}
2378
2379device_initcall(mipsr2_init_debugfs);
2380
2381#endif /* CONFIG_DEBUG_FS */