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
6 * Copyright (c) 2014 Imagination Technologies Ltd.
10 * MIPS R2 user space instruction emulator for MIPS R6
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>
23 #include <asm/branch.h>
24 #include <asm/break.h>
25 #include <asm/debug.h>
27 #include <asm/fpu_emulator.h>
29 #include <asm/mips-r2-to-r6-emul.h>
30 #include <asm/local.h>
31 #include <asm/mipsregs.h>
32 #include <asm/ptrace.h>
33 #include <asm/uaccess.h>
36 #define ADDIU "daddiu "
40 #define ADDIU "addiu "
43 #endif /* CONFIG_64BIT */
50 DEFINE_PER_CPU(struct mips_r2_emulator_stats, mipsr2emustats);
51 DEFINE_PER_CPU(struct mips_r2_emulator_stats, mipsr2bdemustats);
52 DEFINE_PER_CPU(struct mips_r2br_emulator_stats, mipsr2bremustats);
54 extern const unsigned int fpucondbit[8];
56 #define MIPS_R2_EMUL_TOTAL_PASS 10
58 int mipsr2_emulation = 0;
60 static int __init mipsr2emu_enable(char *s)
64 pr_info("MIPS R2-to-R6 Emulator Enabled!");
68 __setup("mipsr2emu", mipsr2emu_enable);
71 * mipsr6_emul - Emulate some frequent R2/R5/R6 instructions in delay slot
72 * for performance instead of the traditional way of using a stack trampoline
73 * which is rather slow.
74 * @regs: Process register set
77 static inline int mipsr6_emul(struct pt_regs *regs, u32 ir)
79 switch (MIPSInst_OPCODE(ir)) {
82 regs->regs[MIPSInst_RT(ir)] =
83 (s32)regs->regs[MIPSInst_RS(ir)] +
84 (s32)MIPSInst_SIMM(ir);
87 if (config_enabled(CONFIG_32BIT))
91 regs->regs[MIPSInst_RT(ir)] =
92 (s64)regs->regs[MIPSInst_RS(ir)] +
93 (s64)MIPSInst_SIMM(ir);
99 /* FPU instructions in delay slot */
102 switch (MIPSInst_FUNC(ir)) {
105 regs->regs[MIPSInst_RD(ir)] =
106 regs->regs[MIPSInst_RS(ir)] |
107 regs->regs[MIPSInst_RT(ir)];
114 regs->regs[MIPSInst_RD(ir)] =
115 (s32)(((u32)regs->regs[MIPSInst_RT(ir)]) <<
123 regs->regs[MIPSInst_RD(ir)] =
124 (s32)(((u32)regs->regs[MIPSInst_RT(ir)]) >>
132 regs->regs[MIPSInst_RD(ir)] =
133 (s32)((u32)regs->regs[MIPSInst_RS(ir)] +
134 (u32)regs->regs[MIPSInst_RT(ir)]);
141 regs->regs[MIPSInst_RD(ir)] =
142 (s32)((u32)regs->regs[MIPSInst_RS(ir)] -
143 (u32)regs->regs[MIPSInst_RT(ir)]);
146 if (config_enabled(CONFIG_32BIT) || MIPSInst_RS(ir))
150 regs->regs[MIPSInst_RD(ir)] =
151 (s64)(((u64)regs->regs[MIPSInst_RT(ir)]) <<
155 if (config_enabled(CONFIG_32BIT) || MIPSInst_RS(ir))
159 regs->regs[MIPSInst_RD(ir)] =
160 (s64)(((u64)regs->regs[MIPSInst_RT(ir)]) >>
164 if (config_enabled(CONFIG_32BIT) || MIPSInst_FD(ir))
168 regs->regs[MIPSInst_RD(ir)] =
169 (u64)regs->regs[MIPSInst_RS(ir)] +
170 (u64)regs->regs[MIPSInst_RT(ir)];
173 if (config_enabled(CONFIG_32BIT) || MIPSInst_FD(ir))
177 regs->regs[MIPSInst_RD(ir)] =
178 (s64)((u64)regs->regs[MIPSInst_RS(ir)] -
179 (u64)regs->regs[MIPSInst_RT(ir)]);
184 pr_debug("No fastpath BD emulation for instruction 0x%08x (op: %02x)\n",
185 ir, MIPSInst_OPCODE(ir));
192 * movf_func - Emulate a MOVF instruction
193 * @regs: Process register set
196 * Returns 0 since it always succeeds.
198 static int movf_func(struct pt_regs *regs, u32 ir)
203 csr = current->thread.fpu.fcr31;
204 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
206 if (((csr & cond) == 0) && MIPSInst_RD(ir))
207 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
215 * movt_func - Emulate a MOVT instruction
216 * @regs: Process register set
219 * Returns 0 since it always succeeds.
221 static int movt_func(struct pt_regs *regs, u32 ir)
226 csr = current->thread.fpu.fcr31;
227 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
229 if (((csr & cond) != 0) && MIPSInst_RD(ir))
230 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
238 * jr_func - Emulate a JR instruction.
239 * @pt_regs: Process register set
242 * Returns SIGILL if JR was in delay slot, SIGEMT if we
243 * can't compute the EPC, SIGSEGV if we can't access the
244 * userland instruction or 0 on success.
246 static int jr_func(struct pt_regs *regs, u32 ir)
249 unsigned long cepc, epc, nepc;
252 if (delay_slot(regs))
255 /* EPC after the RI/JR instruction */
256 nepc = regs->cp0_epc;
257 /* Roll back to the reserved R2 JR instruction */
260 err = __compute_return_epc(regs);
267 cepc = regs->cp0_epc;
269 /* Get DS instruction */
270 err = __get_user(nir, (u32 __user *)nepc);
274 MIPS_R2BR_STATS(jrs);
276 /* If nir == 0(NOP), then nothing else to do */
279 * Negative err means FPU instruction in BD-slot,
280 * Zero err means 'BD-slot emulation done'
281 * For anything else we go back to trampoline emulation.
283 err = mipsr6_emul(regs, nir);
285 regs->cp0_epc = nepc;
286 err = mips_dsemul(regs, nir, cepc);
289 MIPS_R2_STATS(dsemul);
297 * movz_func - Emulate a MOVZ instruction
298 * @regs: Process register set
301 * Returns 0 since it always succeeds.
303 static int movz_func(struct pt_regs *regs, u32 ir)
305 if (((regs->regs[MIPSInst_RT(ir)]) == 0) && MIPSInst_RD(ir))
306 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
313 * movn_func - Emulate a MOVZ instruction
314 * @regs: Process register set
317 * Returns 0 since it always succeeds.
319 static int movn_func(struct pt_regs *regs, u32 ir)
321 if (((regs->regs[MIPSInst_RT(ir)]) != 0) && MIPSInst_RD(ir))
322 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
329 * mfhi_func - Emulate a MFHI instruction
330 * @regs: Process register set
333 * Returns 0 since it always succeeds.
335 static int mfhi_func(struct pt_regs *regs, u32 ir)
338 regs->regs[MIPSInst_RD(ir)] = regs->hi;
346 * mthi_func - Emulate a MTHI instruction
347 * @regs: Process register set
350 * Returns 0 since it always succeeds.
352 static int mthi_func(struct pt_regs *regs, u32 ir)
354 regs->hi = regs->regs[MIPSInst_RS(ir)];
362 * mflo_func - Emulate a MFLO instruction
363 * @regs: Process register set
366 * Returns 0 since it always succeeds.
368 static int mflo_func(struct pt_regs *regs, u32 ir)
371 regs->regs[MIPSInst_RD(ir)] = regs->lo;
379 * mtlo_func - Emulate a MTLO instruction
380 * @regs: Process register set
383 * Returns 0 since it always succeeds.
385 static int mtlo_func(struct pt_regs *regs, u32 ir)
387 regs->lo = regs->regs[MIPSInst_RS(ir)];
395 * mult_func - Emulate a MULT instruction
396 * @regs: Process register set
399 * Returns 0 since it always succeeds.
401 static int mult_func(struct pt_regs *regs, u32 ir)
406 rt = regs->regs[MIPSInst_RT(ir)];
407 rs = regs->regs[MIPSInst_RS(ir)];
408 res = (s64)rt * (s64)rs;
422 * multu_func - Emulate a MULTU instruction
423 * @regs: Process register set
426 * Returns 0 since it always succeeds.
428 static int multu_func(struct pt_regs *regs, u32 ir)
433 rt = regs->regs[MIPSInst_RT(ir)];
434 rs = regs->regs[MIPSInst_RS(ir)];
435 res = (u64)rt * (u64)rs;
438 regs->hi = (s64)(res >> 32);
446 * div_func - Emulate a DIV instruction
447 * @regs: Process register set
450 * Returns 0 since it always succeeds.
452 static int div_func(struct pt_regs *regs, u32 ir)
456 rt = regs->regs[MIPSInst_RT(ir)];
457 rs = regs->regs[MIPSInst_RS(ir)];
459 regs->lo = (s64)(rs / rt);
460 regs->hi = (s64)(rs % rt);
468 * divu_func - Emulate a DIVU instruction
469 * @regs: Process register set
472 * Returns 0 since it always succeeds.
474 static int divu_func(struct pt_regs *regs, u32 ir)
478 rt = regs->regs[MIPSInst_RT(ir)];
479 rs = regs->regs[MIPSInst_RS(ir)];
481 regs->lo = (s64)(rs / rt);
482 regs->hi = (s64)(rs % rt);
490 * dmult_func - Emulate a DMULT instruction
491 * @regs: Process register set
494 * Returns 0 on success or SIGILL for 32-bit kernels.
496 static int dmult_func(struct pt_regs *regs, u32 ir)
501 if (config_enabled(CONFIG_32BIT))
504 rt = regs->regs[MIPSInst_RT(ir)];
505 rs = regs->regs[MIPSInst_RS(ir)];
509 __asm__ __volatile__(
510 "dmuh %0, %1, %2\t\n"
522 * dmultu_func - Emulate a DMULTU instruction
523 * @regs: Process register set
526 * Returns 0 on success or SIGILL for 32-bit kernels.
528 static int dmultu_func(struct pt_regs *regs, u32 ir)
533 if (config_enabled(CONFIG_32BIT))
536 rt = regs->regs[MIPSInst_RT(ir)];
537 rs = regs->regs[MIPSInst_RS(ir)];
541 __asm__ __volatile__(
542 "dmuhu %0, %1, %2\t\n"
554 * ddiv_func - Emulate a DDIV instruction
555 * @regs: Process register set
558 * Returns 0 on success or SIGILL for 32-bit kernels.
560 static int ddiv_func(struct pt_regs *regs, u32 ir)
564 if (config_enabled(CONFIG_32BIT))
567 rt = regs->regs[MIPSInst_RT(ir)];
568 rs = regs->regs[MIPSInst_RS(ir)];
579 * ddivu_func - Emulate a DDIVU instruction
580 * @regs: Process register set
583 * Returns 0 on success or SIGILL for 32-bit kernels.
585 static int ddivu_func(struct pt_regs *regs, u32 ir)
589 if (config_enabled(CONFIG_32BIT))
592 rt = regs->regs[MIPSInst_RT(ir)];
593 rs = regs->regs[MIPSInst_RS(ir)];
603 /* R6 removed instructions for the SPECIAL opcode */
604 static struct r2_decoder_table spec_op_table[] = {
605 { 0xfc1ff83f, 0x00000008, jr_func },
606 { 0xfc00ffff, 0x00000018, mult_func },
607 { 0xfc00ffff, 0x00000019, multu_func },
608 { 0xfc00ffff, 0x0000001c, dmult_func },
609 { 0xfc00ffff, 0x0000001d, dmultu_func },
610 { 0xffff07ff, 0x00000010, mfhi_func },
611 { 0xfc1fffff, 0x00000011, mthi_func },
612 { 0xffff07ff, 0x00000012, mflo_func },
613 { 0xfc1fffff, 0x00000013, mtlo_func },
614 { 0xfc0307ff, 0x00000001, movf_func },
615 { 0xfc0307ff, 0x00010001, movt_func },
616 { 0xfc0007ff, 0x0000000a, movz_func },
617 { 0xfc0007ff, 0x0000000b, movn_func },
618 { 0xfc00ffff, 0x0000001a, div_func },
619 { 0xfc00ffff, 0x0000001b, divu_func },
620 { 0xfc00ffff, 0x0000001e, ddiv_func },
621 { 0xfc00ffff, 0x0000001f, ddivu_func },
626 * madd_func - Emulate a MADD instruction
627 * @regs: Process register set
630 * Returns 0 since it always succeeds.
632 static int madd_func(struct pt_regs *regs, u32 ir)
637 rt = regs->regs[MIPSInst_RT(ir)];
638 rs = regs->regs[MIPSInst_RS(ir)];
639 res = (s64)rt * (s64)rs;
642 res += ((((s64)rt) << 32) | (u32)rs);
655 * maddu_func - Emulate a MADDU instruction
656 * @regs: Process register set
659 * Returns 0 since it always succeeds.
661 static int maddu_func(struct pt_regs *regs, u32 ir)
666 rt = regs->regs[MIPSInst_RT(ir)];
667 rs = regs->regs[MIPSInst_RS(ir)];
668 res = (u64)rt * (u64)rs;
671 res += ((((s64)rt) << 32) | (u32)rs);
684 * msub_func - Emulate a MSUB instruction
685 * @regs: Process register set
688 * Returns 0 since it always succeeds.
690 static int msub_func(struct pt_regs *regs, u32 ir)
695 rt = regs->regs[MIPSInst_RT(ir)];
696 rs = regs->regs[MIPSInst_RS(ir)];
697 res = (s64)rt * (s64)rs;
700 res = ((((s64)rt) << 32) | (u32)rs) - res;
713 * msubu_func - Emulate a MSUBU instruction
714 * @regs: Process register set
717 * Returns 0 since it always succeeds.
719 static int msubu_func(struct pt_regs *regs, u32 ir)
724 rt = regs->regs[MIPSInst_RT(ir)];
725 rs = regs->regs[MIPSInst_RS(ir)];
726 res = (u64)rt * (u64)rs;
729 res = ((((s64)rt) << 32) | (u32)rs) - res;
742 * mul_func - Emulate a MUL instruction
743 * @regs: Process register set
746 * Returns 0 since it always succeeds.
748 static int mul_func(struct pt_regs *regs, u32 ir)
753 if (!MIPSInst_RD(ir))
755 rt = regs->regs[MIPSInst_RT(ir)];
756 rs = regs->regs[MIPSInst_RS(ir)];
757 res = (s64)rt * (s64)rs;
760 regs->regs[MIPSInst_RD(ir)] = (s64)rs;
768 * clz_func - Emulate a CLZ instruction
769 * @regs: Process register set
772 * Returns 0 since it always succeeds.
774 static int clz_func(struct pt_regs *regs, u32 ir)
779 if (!MIPSInst_RD(ir))
782 rs = regs->regs[MIPSInst_RS(ir)];
783 __asm__ __volatile__("clz %0, %1" : "=r"(res) : "r"(rs));
784 regs->regs[MIPSInst_RD(ir)] = res;
792 * clo_func - Emulate a CLO instruction
793 * @regs: Process register set
796 * Returns 0 since it always succeeds.
799 static int clo_func(struct pt_regs *regs, u32 ir)
804 if (!MIPSInst_RD(ir))
807 rs = regs->regs[MIPSInst_RS(ir)];
808 __asm__ __volatile__("clo %0, %1" : "=r"(res) : "r"(rs));
809 regs->regs[MIPSInst_RD(ir)] = res;
817 * dclz_func - Emulate a DCLZ instruction
818 * @regs: Process register set
821 * Returns 0 since it always succeeds.
823 static int dclz_func(struct pt_regs *regs, u32 ir)
828 if (config_enabled(CONFIG_32BIT))
831 if (!MIPSInst_RD(ir))
834 rs = regs->regs[MIPSInst_RS(ir)];
835 __asm__ __volatile__("dclz %0, %1" : "=r"(res) : "r"(rs));
836 regs->regs[MIPSInst_RD(ir)] = res;
844 * dclo_func - Emulate a DCLO instruction
845 * @regs: Process register set
848 * Returns 0 since it always succeeds.
850 static int dclo_func(struct pt_regs *regs, u32 ir)
855 if (config_enabled(CONFIG_32BIT))
858 if (!MIPSInst_RD(ir))
861 rs = regs->regs[MIPSInst_RS(ir)];
862 __asm__ __volatile__("dclo %0, %1" : "=r"(res) : "r"(rs));
863 regs->regs[MIPSInst_RD(ir)] = res;
870 /* R6 removed instructions for the SPECIAL2 opcode */
871 static struct r2_decoder_table spec2_op_table[] = {
872 { 0xfc00ffff, 0x70000000, madd_func },
873 { 0xfc00ffff, 0x70000001, maddu_func },
874 { 0xfc0007ff, 0x70000002, mul_func },
875 { 0xfc00ffff, 0x70000004, msub_func },
876 { 0xfc00ffff, 0x70000005, msubu_func },
877 { 0xfc0007ff, 0x70000020, clz_func },
878 { 0xfc0007ff, 0x70000021, clo_func },
879 { 0xfc0007ff, 0x70000024, dclz_func },
880 { 0xfc0007ff, 0x70000025, dclo_func },
884 static inline int mipsr2_find_op_func(struct pt_regs *regs, u32 inst,
885 struct r2_decoder_table *table)
887 struct r2_decoder_table *p;
890 for (p = table; p->func; p++) {
891 if ((inst & p->mask) == p->code) {
892 err = (p->func)(regs, inst);
900 * mipsr2_decoder: Decode and emulate a MIPS R2 instruction
901 * @regs: Process register set
902 * @inst: Instruction to decode and emulate
903 * @fcr31: Floating Point Control and Status Register returned
905 int mipsr2_decoder(struct pt_regs *regs, u32 inst, unsigned long *fcr31)
910 unsigned long cpc, epc, nepc, r31, res, rs, rt;
912 void __user *fault_addr = NULL;
916 r31 = regs->regs[31];
918 err = compute_return_epc(regs);
923 pr_debug("Emulating the 0x%08x R2 instruction @ 0x%08lx (pass=%d))\n",
926 switch (MIPSInst_OPCODE(inst)) {
928 err = mipsr2_find_op_func(regs, inst, spec_op_table);
930 /* FPU instruction under JR */
931 regs->cp0_cause |= CAUSEF_BD;
936 err = mipsr2_find_op_func(regs, inst, spec2_op_table);
939 rt = MIPSInst_RT(inst);
940 rs = MIPSInst_RS(inst);
943 if ((long)regs->regs[rs] >= MIPSInst_SIMM(inst))
944 do_trap_or_bp(regs, 0, 0, "TGEI");
946 MIPS_R2_STATS(traps);
950 if (regs->regs[rs] >= MIPSInst_UIMM(inst))
951 do_trap_or_bp(regs, 0, 0, "TGEIU");
953 MIPS_R2_STATS(traps);
957 if ((long)regs->regs[rs] < MIPSInst_SIMM(inst))
958 do_trap_or_bp(regs, 0, 0, "TLTI");
960 MIPS_R2_STATS(traps);
964 if (regs->regs[rs] < MIPSInst_UIMM(inst))
965 do_trap_or_bp(regs, 0, 0, "TLTIU");
967 MIPS_R2_STATS(traps);
971 if (regs->regs[rs] == MIPSInst_SIMM(inst))
972 do_trap_or_bp(regs, 0, 0, "TEQI");
974 MIPS_R2_STATS(traps);
978 if (regs->regs[rs] != MIPSInst_SIMM(inst))
979 do_trap_or_bp(regs, 0, 0, "TNEI");
981 MIPS_R2_STATS(traps);
988 if (delay_slot(regs)) {
992 regs->regs[31] = r31;
994 err = __compute_return_epc(regs);
997 if (err != BRANCH_LIKELY_TAKEN)
1001 err = __get_user(nir, (u32 __user *)nepc);
1007 * This will probably be optimized away when
1008 * CONFIG_DEBUG_FS is not enabled
1012 MIPS_R2BR_STATS(bltzl);
1015 MIPS_R2BR_STATS(bgezl);
1018 MIPS_R2BR_STATS(bltzall);
1021 MIPS_R2BR_STATS(bgezall);
1025 switch (MIPSInst_OPCODE(nir)) {
1030 regs->cp0_cause |= CAUSEF_BD;
1034 err = mipsr6_emul(regs, nir);
1036 err = mips_dsemul(regs, nir, cpc);
1039 MIPS_R2_STATS(dsemul);
1045 if (delay_slot(regs)) {
1049 regs->regs[31] = r31;
1050 regs->cp0_epc = epc;
1051 err = __compute_return_epc(regs);
1054 cpc = regs->cp0_epc;
1056 err = __get_user(nir, (u32 __user *)nepc);
1062 * This will probably be optimized away when
1063 * CONFIG_DEBUG_FS is not enabled
1067 MIPS_R2BR_STATS(bltzal);
1070 MIPS_R2BR_STATS(bgezal);
1074 switch (MIPSInst_OPCODE(nir)) {
1079 regs->cp0_cause |= CAUSEF_BD;
1083 err = mipsr6_emul(regs, nir);
1085 err = mips_dsemul(regs, nir, cpc);
1088 MIPS_R2_STATS(dsemul);
1093 regs->regs[31] = r31;
1094 regs->cp0_epc = epc;
1104 if (delay_slot(regs)) {
1108 regs->regs[31] = r31;
1109 regs->cp0_epc = epc;
1110 err = __compute_return_epc(regs);
1113 if (err != BRANCH_LIKELY_TAKEN)
1115 cpc = regs->cp0_epc;
1117 err = __get_user(nir, (u32 __user *)nepc);
1123 * This will probably be optimized away when
1124 * CONFIG_DEBUG_FS is not enabled
1126 switch (MIPSInst_OPCODE(inst)) {
1128 MIPS_R2BR_STATS(beql);
1131 MIPS_R2BR_STATS(bnel);
1134 MIPS_R2BR_STATS(blezl);
1137 MIPS_R2BR_STATS(bgtzl);
1141 switch (MIPSInst_OPCODE(nir)) {
1146 regs->cp0_cause |= CAUSEF_BD;
1150 err = mipsr6_emul(regs, nir);
1152 err = mips_dsemul(regs, nir, cpc);
1155 MIPS_R2_STATS(dsemul);
1164 regs->regs[31] = r31;
1165 regs->cp0_epc = epc;
1166 if (!used_math()) { /* First time FPU user. */
1170 lose_fpu(1); /* Save FPU state for the emulator. */
1172 err = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 0,
1174 *fcr31 = current->thread.fpu.fcr31;
1177 * We can't allow the emulated instruction to leave any of
1178 * the cause bits set in $fcr31.
1180 current->thread.fpu.fcr31 &= ~FPU_CSR_ALL_X;
1183 * this is a tricky issue - lose_fpu() uses LL/SC atomics
1184 * if FPU is owned and effectively cancels user level LL/SC.
1185 * So, it could be logical to don't restore FPU ownership here.
1186 * But the sequence of multiple FPU instructions is much much
1187 * more often than LL-FPU-SC and I prefer loop here until
1188 * next scheduler cycle cancels FPU ownership
1190 own_fpu(1); /* Restore FPU state. */
1193 current->thread.cp0_baduaddr = (unsigned long)fault_addr;
1195 MIPS_R2_STATS(fpus);
1200 rt = regs->regs[MIPSInst_RT(inst)];
1201 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1202 if (!access_ok(VERIFY_READ, vaddr, 4)) {
1203 current->thread.cp0_baduaddr = vaddr;
1207 __asm__ __volatile__(
1210 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1211 "1:" LB "%1, 0(%2)\n"
1212 INS "%0, %1, 24, 8\n"
1213 " andi %1, %2, 0x3\n"
1215 ADDIU "%2, %2, -1\n"
1216 "2:" LB "%1, 0(%2)\n"
1217 INS "%0, %1, 16, 8\n"
1218 " andi %1, %2, 0x3\n"
1220 ADDIU "%2, %2, -1\n"
1221 "3:" LB "%1, 0(%2)\n"
1222 INS "%0, %1, 8, 8\n"
1223 " andi %1, %2, 0x3\n"
1225 ADDIU "%2, %2, -1\n"
1226 "4:" LB "%1, 0(%2)\n"
1227 INS "%0, %1, 0, 8\n"
1228 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1229 "1:" LB "%1, 0(%2)\n"
1230 INS "%0, %1, 24, 8\n"
1232 " andi %1, %2, 0x3\n"
1234 "2:" LB "%1, 0(%2)\n"
1235 INS "%0, %1, 16, 8\n"
1237 " andi %1, %2, 0x3\n"
1239 "3:" LB "%1, 0(%2)\n"
1240 INS "%0, %1, 8, 8\n"
1242 " andi %1, %2, 0x3\n"
1244 "4:" LB "%1, 0(%2)\n"
1245 INS "%0, %1, 0, 8\n"
1246 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1247 "9: sll %0, %0, 0\n"
1250 " .section .fixup,\"ax\"\n"
1254 " .section __ex_table,\"a\"\n"
1261 : "+&r"(rt), "=&r"(rs),
1262 "+&r"(vaddr), "+&r"(err)
1265 if (MIPSInst_RT(inst) && !err)
1266 regs->regs[MIPSInst_RT(inst)] = rt;
1268 MIPS_R2_STATS(loads);
1273 rt = regs->regs[MIPSInst_RT(inst)];
1274 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1275 if (!access_ok(VERIFY_READ, vaddr, 4)) {
1276 current->thread.cp0_baduaddr = vaddr;
1280 __asm__ __volatile__(
1283 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1284 "1:" LB "%1, 0(%2)\n"
1285 INS "%0, %1, 0, 8\n"
1287 " andi %1, %2, 0x3\n"
1289 "2:" LB "%1, 0(%2)\n"
1290 INS "%0, %1, 8, 8\n"
1292 " andi %1, %2, 0x3\n"
1294 "3:" LB "%1, 0(%2)\n"
1295 INS "%0, %1, 16, 8\n"
1297 " andi %1, %2, 0x3\n"
1299 "4:" LB "%1, 0(%2)\n"
1300 INS "%0, %1, 24, 8\n"
1302 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1303 "1:" LB "%1, 0(%2)\n"
1304 INS "%0, %1, 0, 8\n"
1305 " andi %1, %2, 0x3\n"
1307 ADDIU "%2, %2, -1\n"
1308 "2:" LB "%1, 0(%2)\n"
1309 INS "%0, %1, 8, 8\n"
1310 " andi %1, %2, 0x3\n"
1312 ADDIU "%2, %2, -1\n"
1313 "3:" LB "%1, 0(%2)\n"
1314 INS "%0, %1, 16, 8\n"
1315 " andi %1, %2, 0x3\n"
1317 ADDIU "%2, %2, -1\n"
1318 "4:" LB "%1, 0(%2)\n"
1319 INS "%0, %1, 24, 8\n"
1321 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1325 " .section .fixup,\"ax\"\n"
1329 " .section __ex_table,\"a\"\n"
1336 : "+&r"(rt), "=&r"(rs),
1337 "+&r"(vaddr), "+&r"(err)
1339 if (MIPSInst_RT(inst) && !err)
1340 regs->regs[MIPSInst_RT(inst)] = rt;
1342 MIPS_R2_STATS(loads);
1347 rt = regs->regs[MIPSInst_RT(inst)];
1348 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1349 if (!access_ok(VERIFY_WRITE, vaddr, 4)) {
1350 current->thread.cp0_baduaddr = vaddr;
1354 __asm__ __volatile__(
1357 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1358 EXT "%1, %0, 24, 8\n"
1359 "1:" SB "%1, 0(%2)\n"
1360 " andi %1, %2, 0x3\n"
1362 ADDIU "%2, %2, -1\n"
1363 EXT "%1, %0, 16, 8\n"
1364 "2:" SB "%1, 0(%2)\n"
1365 " andi %1, %2, 0x3\n"
1367 ADDIU "%2, %2, -1\n"
1368 EXT "%1, %0, 8, 8\n"
1369 "3:" SB "%1, 0(%2)\n"
1370 " andi %1, %2, 0x3\n"
1372 ADDIU "%2, %2, -1\n"
1373 EXT "%1, %0, 0, 8\n"
1374 "4:" SB "%1, 0(%2)\n"
1375 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1376 EXT "%1, %0, 24, 8\n"
1377 "1:" SB "%1, 0(%2)\n"
1379 " andi %1, %2, 0x3\n"
1381 EXT "%1, %0, 16, 8\n"
1382 "2:" SB "%1, 0(%2)\n"
1384 " andi %1, %2, 0x3\n"
1386 EXT "%1, %0, 8, 8\n"
1387 "3:" SB "%1, 0(%2)\n"
1389 " andi %1, %2, 0x3\n"
1391 EXT "%1, %0, 0, 8\n"
1392 "4:" SB "%1, 0(%2)\n"
1393 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1396 " .section .fixup,\"ax\"\n"
1400 " .section __ex_table,\"a\"\n"
1407 : "+&r"(rt), "=&r"(rs),
1408 "+&r"(vaddr), "+&r"(err)
1412 MIPS_R2_STATS(stores);
1417 rt = regs->regs[MIPSInst_RT(inst)];
1418 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1419 if (!access_ok(VERIFY_WRITE, vaddr, 4)) {
1420 current->thread.cp0_baduaddr = vaddr;
1424 __asm__ __volatile__(
1427 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1428 EXT "%1, %0, 0, 8\n"
1429 "1:" SB "%1, 0(%2)\n"
1431 " andi %1, %2, 0x3\n"
1433 EXT "%1, %0, 8, 8\n"
1434 "2:" SB "%1, 0(%2)\n"
1436 " andi %1, %2, 0x3\n"
1438 EXT "%1, %0, 16, 8\n"
1439 "3:" SB "%1, 0(%2)\n"
1441 " andi %1, %2, 0x3\n"
1443 EXT "%1, %0, 24, 8\n"
1444 "4:" SB "%1, 0(%2)\n"
1445 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1446 EXT "%1, %0, 0, 8\n"
1447 "1:" SB "%1, 0(%2)\n"
1448 " andi %1, %2, 0x3\n"
1450 ADDIU "%2, %2, -1\n"
1451 EXT "%1, %0, 8, 8\n"
1452 "2:" SB "%1, 0(%2)\n"
1453 " andi %1, %2, 0x3\n"
1455 ADDIU "%2, %2, -1\n"
1456 EXT "%1, %0, 16, 8\n"
1457 "3:" SB "%1, 0(%2)\n"
1458 " andi %1, %2, 0x3\n"
1460 ADDIU "%2, %2, -1\n"
1461 EXT "%1, %0, 24, 8\n"
1462 "4:" SB "%1, 0(%2)\n"
1463 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1466 " .section .fixup,\"ax\"\n"
1470 " .section __ex_table,\"a\"\n"
1477 : "+&r"(rt), "=&r"(rs),
1478 "+&r"(vaddr), "+&r"(err)
1482 MIPS_R2_STATS(stores);
1487 if (config_enabled(CONFIG_32BIT)) {
1492 rt = regs->regs[MIPSInst_RT(inst)];
1493 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1494 if (!access_ok(VERIFY_READ, vaddr, 8)) {
1495 current->thread.cp0_baduaddr = vaddr;
1499 __asm__ __volatile__(
1502 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1504 " dinsu %0, %1, 56, 8\n"
1505 " andi %1, %2, 0x7\n"
1507 " daddiu %2, %2, -1\n"
1509 " dinsu %0, %1, 48, 8\n"
1510 " andi %1, %2, 0x7\n"
1512 " daddiu %2, %2, -1\n"
1514 " dinsu %0, %1, 40, 8\n"
1515 " andi %1, %2, 0x7\n"
1517 " daddiu %2, %2, -1\n"
1519 " dinsu %0, %1, 32, 8\n"
1520 " andi %1, %2, 0x7\n"
1522 " daddiu %2, %2, -1\n"
1524 " dins %0, %1, 24, 8\n"
1525 " andi %1, %2, 0x7\n"
1527 " daddiu %2, %2, -1\n"
1529 " dins %0, %1, 16, 8\n"
1530 " andi %1, %2, 0x7\n"
1532 " daddiu %2, %2, -1\n"
1534 " dins %0, %1, 8, 8\n"
1535 " andi %1, %2, 0x7\n"
1537 " daddiu %2, %2, -1\n"
1539 " dins %0, %1, 0, 8\n"
1540 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1542 " dinsu %0, %1, 56, 8\n"
1543 " daddiu %2, %2, 1\n"
1544 " andi %1, %2, 0x7\n"
1547 " dinsu %0, %1, 48, 8\n"
1548 " daddiu %2, %2, 1\n"
1549 " andi %1, %2, 0x7\n"
1552 " dinsu %0, %1, 40, 8\n"
1553 " daddiu %2, %2, 1\n"
1554 " andi %1, %2, 0x7\n"
1557 " dinsu %0, %1, 32, 8\n"
1558 " daddiu %2, %2, 1\n"
1559 " andi %1, %2, 0x7\n"
1562 " dins %0, %1, 24, 8\n"
1563 " daddiu %2, %2, 1\n"
1564 " andi %1, %2, 0x7\n"
1567 " dins %0, %1, 16, 8\n"
1568 " daddiu %2, %2, 1\n"
1569 " andi %1, %2, 0x7\n"
1572 " dins %0, %1, 8, 8\n"
1573 " daddiu %2, %2, 1\n"
1574 " andi %1, %2, 0x7\n"
1577 " dins %0, %1, 0, 8\n"
1578 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1581 " .section .fixup,\"ax\"\n"
1585 " .section __ex_table,\"a\"\n"
1596 : "+&r"(rt), "=&r"(rs),
1597 "+&r"(vaddr), "+&r"(err)
1599 if (MIPSInst_RT(inst) && !err)
1600 regs->regs[MIPSInst_RT(inst)] = rt;
1602 MIPS_R2_STATS(loads);
1606 if (config_enabled(CONFIG_32BIT)) {
1611 rt = regs->regs[MIPSInst_RT(inst)];
1612 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1613 if (!access_ok(VERIFY_READ, vaddr, 8)) {
1614 current->thread.cp0_baduaddr = vaddr;
1618 __asm__ __volatile__(
1621 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1623 " dins %0, %1, 0, 8\n"
1624 " daddiu %2, %2, 1\n"
1625 " andi %1, %2, 0x7\n"
1628 " dins %0, %1, 8, 8\n"
1629 " daddiu %2, %2, 1\n"
1630 " andi %1, %2, 0x7\n"
1633 " dins %0, %1, 16, 8\n"
1634 " daddiu %2, %2, 1\n"
1635 " andi %1, %2, 0x7\n"
1638 " dins %0, %1, 24, 8\n"
1639 " daddiu %2, %2, 1\n"
1640 " andi %1, %2, 0x7\n"
1643 " dinsu %0, %1, 32, 8\n"
1644 " daddiu %2, %2, 1\n"
1645 " andi %1, %2, 0x7\n"
1648 " dinsu %0, %1, 40, 8\n"
1649 " daddiu %2, %2, 1\n"
1650 " andi %1, %2, 0x7\n"
1653 " dinsu %0, %1, 48, 8\n"
1654 " daddiu %2, %2, 1\n"
1655 " andi %1, %2, 0x7\n"
1658 " dinsu %0, %1, 56, 8\n"
1659 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1661 " dins %0, %1, 0, 8\n"
1662 " andi %1, %2, 0x7\n"
1664 " daddiu %2, %2, -1\n"
1666 " dins %0, %1, 8, 8\n"
1667 " andi %1, %2, 0x7\n"
1669 " daddiu %2, %2, -1\n"
1671 " dins %0, %1, 16, 8\n"
1672 " andi %1, %2, 0x7\n"
1674 " daddiu %2, %2, -1\n"
1676 " dins %0, %1, 24, 8\n"
1677 " andi %1, %2, 0x7\n"
1679 " daddiu %2, %2, -1\n"
1681 " dinsu %0, %1, 32, 8\n"
1682 " andi %1, %2, 0x7\n"
1684 " daddiu %2, %2, -1\n"
1686 " dinsu %0, %1, 40, 8\n"
1687 " andi %1, %2, 0x7\n"
1689 " daddiu %2, %2, -1\n"
1691 " dinsu %0, %1, 48, 8\n"
1692 " andi %1, %2, 0x7\n"
1694 " daddiu %2, %2, -1\n"
1696 " dinsu %0, %1, 56, 8\n"
1697 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1700 " .section .fixup,\"ax\"\n"
1704 " .section __ex_table,\"a\"\n"
1715 : "+&r"(rt), "=&r"(rs),
1716 "+&r"(vaddr), "+&r"(err)
1718 if (MIPSInst_RT(inst) && !err)
1719 regs->regs[MIPSInst_RT(inst)] = rt;
1721 MIPS_R2_STATS(loads);
1725 if (config_enabled(CONFIG_32BIT)) {
1730 rt = regs->regs[MIPSInst_RT(inst)];
1731 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1732 if (!access_ok(VERIFY_WRITE, vaddr, 8)) {
1733 current->thread.cp0_baduaddr = vaddr;
1737 __asm__ __volatile__(
1740 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1741 " dextu %1, %0, 56, 8\n"
1743 " andi %1, %2, 0x7\n"
1745 " daddiu %2, %2, -1\n"
1746 " dextu %1, %0, 48, 8\n"
1748 " andi %1, %2, 0x7\n"
1750 " daddiu %2, %2, -1\n"
1751 " dextu %1, %0, 40, 8\n"
1753 " andi %1, %2, 0x7\n"
1755 " daddiu %2, %2, -1\n"
1756 " dextu %1, %0, 32, 8\n"
1758 " andi %1, %2, 0x7\n"
1760 " daddiu %2, %2, -1\n"
1761 " dext %1, %0, 24, 8\n"
1763 " andi %1, %2, 0x7\n"
1765 " daddiu %2, %2, -1\n"
1766 " dext %1, %0, 16, 8\n"
1768 " andi %1, %2, 0x7\n"
1770 " daddiu %2, %2, -1\n"
1771 " dext %1, %0, 8, 8\n"
1773 " andi %1, %2, 0x7\n"
1775 " daddiu %2, %2, -1\n"
1776 " dext %1, %0, 0, 8\n"
1778 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1779 " dextu %1, %0, 56, 8\n"
1781 " daddiu %2, %2, 1\n"
1782 " andi %1, %2, 0x7\n"
1784 " dextu %1, %0, 48, 8\n"
1786 " daddiu %2, %2, 1\n"
1787 " andi %1, %2, 0x7\n"
1789 " dextu %1, %0, 40, 8\n"
1791 " daddiu %2, %2, 1\n"
1792 " andi %1, %2, 0x7\n"
1794 " dextu %1, %0, 32, 8\n"
1796 " daddiu %2, %2, 1\n"
1797 " andi %1, %2, 0x7\n"
1799 " dext %1, %0, 24, 8\n"
1801 " daddiu %2, %2, 1\n"
1802 " andi %1, %2, 0x7\n"
1804 " dext %1, %0, 16, 8\n"
1806 " daddiu %2, %2, 1\n"
1807 " andi %1, %2, 0x7\n"
1809 " dext %1, %0, 8, 8\n"
1811 " daddiu %2, %2, 1\n"
1812 " andi %1, %2, 0x7\n"
1814 " dext %1, %0, 0, 8\n"
1816 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1819 " .section .fixup,\"ax\"\n"
1823 " .section __ex_table,\"a\"\n"
1834 : "+&r"(rt), "=&r"(rs),
1835 "+&r"(vaddr), "+&r"(err)
1839 MIPS_R2_STATS(stores);
1843 if (config_enabled(CONFIG_32BIT)) {
1848 rt = regs->regs[MIPSInst_RT(inst)];
1849 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1850 if (!access_ok(VERIFY_WRITE, vaddr, 8)) {
1851 current->thread.cp0_baduaddr = vaddr;
1855 __asm__ __volatile__(
1858 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1859 " dext %1, %0, 0, 8\n"
1861 " daddiu %2, %2, 1\n"
1862 " andi %1, %2, 0x7\n"
1864 " dext %1, %0, 8, 8\n"
1866 " daddiu %2, %2, 1\n"
1867 " andi %1, %2, 0x7\n"
1869 " dext %1, %0, 16, 8\n"
1871 " daddiu %2, %2, 1\n"
1872 " andi %1, %2, 0x7\n"
1874 " dext %1, %0, 24, 8\n"
1876 " daddiu %2, %2, 1\n"
1877 " andi %1, %2, 0x7\n"
1879 " dextu %1, %0, 32, 8\n"
1881 " daddiu %2, %2, 1\n"
1882 " andi %1, %2, 0x7\n"
1884 " dextu %1, %0, 40, 8\n"
1886 " daddiu %2, %2, 1\n"
1887 " andi %1, %2, 0x7\n"
1889 " dextu %1, %0, 48, 8\n"
1891 " daddiu %2, %2, 1\n"
1892 " andi %1, %2, 0x7\n"
1894 " dextu %1, %0, 56, 8\n"
1896 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1897 " dext %1, %0, 0, 8\n"
1899 " andi %1, %2, 0x7\n"
1901 " daddiu %2, %2, -1\n"
1902 " dext %1, %0, 8, 8\n"
1904 " andi %1, %2, 0x7\n"
1906 " daddiu %2, %2, -1\n"
1907 " dext %1, %0, 16, 8\n"
1909 " andi %1, %2, 0x7\n"
1911 " daddiu %2, %2, -1\n"
1912 " dext %1, %0, 24, 8\n"
1914 " andi %1, %2, 0x7\n"
1916 " daddiu %2, %2, -1\n"
1917 " dextu %1, %0, 32, 8\n"
1919 " andi %1, %2, 0x7\n"
1921 " daddiu %2, %2, -1\n"
1922 " dextu %1, %0, 40, 8\n"
1924 " andi %1, %2, 0x7\n"
1926 " daddiu %2, %2, -1\n"
1927 " dextu %1, %0, 48, 8\n"
1929 " andi %1, %2, 0x7\n"
1931 " daddiu %2, %2, -1\n"
1932 " dextu %1, %0, 56, 8\n"
1934 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1937 " .section .fixup,\"ax\"\n"
1941 " .section __ex_table,\"a\"\n"
1952 : "+&r"(rt), "=&r"(rs),
1953 "+&r"(vaddr), "+&r"(err)
1957 MIPS_R2_STATS(stores);
1961 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1963 current->thread.cp0_baduaddr = vaddr;
1967 if (!access_ok(VERIFY_READ, vaddr, 4)) {
1968 current->thread.cp0_baduaddr = vaddr;
1973 if (!cpu_has_rw_llb) {
1975 * An LL/SC block can't be safely emulated without
1976 * a Config5/LLB availability. So it's probably time to
1977 * kill our process before things get any worse. This is
1978 * because Config5/LLB allows us to use ERETNC so that
1979 * the LLAddr/LLB bit is not cleared when we return from
1980 * an exception. MIPS R2 LL/SC instructions trap with an
1981 * RI exception so once we emulate them here, we return
1982 * back to userland with ERETNC. That preserves the
1983 * LLAddr/LLB so the subsequent SC instruction will
1984 * succeed preserving the atomic semantics of the LL/SC
1985 * block. Without that, there is no safe way to emulate
1986 * an LL/SC block in MIPSR2 userland.
1988 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
1993 __asm__ __volatile__(
1998 ".section .fixup,\"ax\"\n"
2003 ".section __ex_table,\"a\"\n"
2006 : "=&r"(res), "+&r"(err)
2007 : "r"(vaddr), "i"(SIGSEGV)
2010 if (MIPSInst_RT(inst) && !err)
2011 regs->regs[MIPSInst_RT(inst)] = res;
2012 MIPS_R2_STATS(llsc);
2017 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2019 current->thread.cp0_baduaddr = vaddr;
2023 if (!access_ok(VERIFY_WRITE, vaddr, 4)) {
2024 current->thread.cp0_baduaddr = vaddr;
2029 if (!cpu_has_rw_llb) {
2031 * An LL/SC block can't be safely emulated without
2032 * a Config5/LLB availability. So it's probably time to
2033 * kill our process before things get any worse. This is
2034 * because Config5/LLB allows us to use ERETNC so that
2035 * the LLAddr/LLB bit is not cleared when we return from
2036 * an exception. MIPS R2 LL/SC instructions trap with an
2037 * RI exception so once we emulate them here, we return
2038 * back to userland with ERETNC. That preserves the
2039 * LLAddr/LLB so the subsequent SC instruction will
2040 * succeed preserving the atomic semantics of the LL/SC
2041 * block. Without that, there is no safe way to emulate
2042 * an LL/SC block in MIPSR2 userland.
2044 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2049 res = regs->regs[MIPSInst_RT(inst)];
2051 __asm__ __volatile__(
2056 ".section .fixup,\"ax\"\n"
2061 ".section __ex_table,\"a\"\n"
2064 : "+&r"(res), "+&r"(err)
2065 : "r"(vaddr), "i"(SIGSEGV));
2067 if (MIPSInst_RT(inst) && !err)
2068 regs->regs[MIPSInst_RT(inst)] = res;
2070 MIPS_R2_STATS(llsc);
2075 if (config_enabled(CONFIG_32BIT)) {
2080 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2082 current->thread.cp0_baduaddr = vaddr;
2086 if (!access_ok(VERIFY_READ, vaddr, 8)) {
2087 current->thread.cp0_baduaddr = vaddr;
2092 if (!cpu_has_rw_llb) {
2094 * An LL/SC block can't be safely emulated without
2095 * a Config5/LLB availability. So it's probably time to
2096 * kill our process before things get any worse. This is
2097 * because Config5/LLB allows us to use ERETNC so that
2098 * the LLAddr/LLB bit is not cleared when we return from
2099 * an exception. MIPS R2 LL/SC instructions trap with an
2100 * RI exception so once we emulate them here, we return
2101 * back to userland with ERETNC. That preserves the
2102 * LLAddr/LLB so the subsequent SC instruction will
2103 * succeed preserving the atomic semantics of the LL/SC
2104 * block. Without that, there is no safe way to emulate
2105 * an LL/SC block in MIPSR2 userland.
2107 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2112 __asm__ __volatile__(
2117 ".section .fixup,\"ax\"\n"
2122 ".section __ex_table,\"a\"\n"
2125 : "=&r"(res), "+&r"(err)
2126 : "r"(vaddr), "i"(SIGSEGV)
2128 if (MIPSInst_RT(inst) && !err)
2129 regs->regs[MIPSInst_RT(inst)] = res;
2131 MIPS_R2_STATS(llsc);
2136 if (config_enabled(CONFIG_32BIT)) {
2141 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2143 current->thread.cp0_baduaddr = vaddr;
2147 if (!access_ok(VERIFY_WRITE, vaddr, 8)) {
2148 current->thread.cp0_baduaddr = vaddr;
2153 if (!cpu_has_rw_llb) {
2155 * An LL/SC block can't be safely emulated without
2156 * a Config5/LLB availability. So it's probably time to
2157 * kill our process before things get any worse. This is
2158 * because Config5/LLB allows us to use ERETNC so that
2159 * the LLAddr/LLB bit is not cleared when we return from
2160 * an exception. MIPS R2 LL/SC instructions trap with an
2161 * RI exception so once we emulate them here, we return
2162 * back to userland with ERETNC. That preserves the
2163 * LLAddr/LLB so the subsequent SC instruction will
2164 * succeed preserving the atomic semantics of the LL/SC
2165 * block. Without that, there is no safe way to emulate
2166 * an LL/SC block in MIPSR2 userland.
2168 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2173 res = regs->regs[MIPSInst_RT(inst)];
2175 __asm__ __volatile__(
2180 ".section .fixup,\"ax\"\n"
2185 ".section __ex_table,\"a\"\n"
2188 : "+&r"(res), "+&r"(err)
2189 : "r"(vaddr), "i"(SIGSEGV));
2191 if (MIPSInst_RT(inst) && !err)
2192 regs->regs[MIPSInst_RT(inst)] = res;
2194 MIPS_R2_STATS(llsc);
2205 * Let's not return to userland just yet. It's costly and
2206 * it's likely we have more R2 instructions to emulate
2208 if (!err && (pass++ < MIPS_R2_EMUL_TOTAL_PASS)) {
2209 regs->cp0_cause &= ~CAUSEF_BD;
2210 err = get_user(inst, (u32 __user *)regs->cp0_epc);
2218 if (err && (err != SIGEMT)) {
2219 regs->regs[31] = r31;
2220 regs->cp0_epc = epc;
2223 /* Likely a MIPS R6 compatible instruction */
2224 if (pass && (err == SIGILL))
2230 #ifdef CONFIG_DEBUG_FS
2232 static int mipsr2_stats_show(struct seq_file *s, void *unused)
2235 seq_printf(s, "Instruction\tTotal\tBDslot\n------------------------------\n");
2236 seq_printf(s, "movs\t\t%ld\t%ld\n",
2237 (unsigned long)__this_cpu_read(mipsr2emustats.movs),
2238 (unsigned long)__this_cpu_read(mipsr2bdemustats.movs));
2239 seq_printf(s, "hilo\t\t%ld\t%ld\n",
2240 (unsigned long)__this_cpu_read(mipsr2emustats.hilo),
2241 (unsigned long)__this_cpu_read(mipsr2bdemustats.hilo));
2242 seq_printf(s, "muls\t\t%ld\t%ld\n",
2243 (unsigned long)__this_cpu_read(mipsr2emustats.muls),
2244 (unsigned long)__this_cpu_read(mipsr2bdemustats.muls));
2245 seq_printf(s, "divs\t\t%ld\t%ld\n",
2246 (unsigned long)__this_cpu_read(mipsr2emustats.divs),
2247 (unsigned long)__this_cpu_read(mipsr2bdemustats.divs));
2248 seq_printf(s, "dsps\t\t%ld\t%ld\n",
2249 (unsigned long)__this_cpu_read(mipsr2emustats.dsps),
2250 (unsigned long)__this_cpu_read(mipsr2bdemustats.dsps));
2251 seq_printf(s, "bops\t\t%ld\t%ld\n",
2252 (unsigned long)__this_cpu_read(mipsr2emustats.bops),
2253 (unsigned long)__this_cpu_read(mipsr2bdemustats.bops));
2254 seq_printf(s, "traps\t\t%ld\t%ld\n",
2255 (unsigned long)__this_cpu_read(mipsr2emustats.traps),
2256 (unsigned long)__this_cpu_read(mipsr2bdemustats.traps));
2257 seq_printf(s, "fpus\t\t%ld\t%ld\n",
2258 (unsigned long)__this_cpu_read(mipsr2emustats.fpus),
2259 (unsigned long)__this_cpu_read(mipsr2bdemustats.fpus));
2260 seq_printf(s, "loads\t\t%ld\t%ld\n",
2261 (unsigned long)__this_cpu_read(mipsr2emustats.loads),
2262 (unsigned long)__this_cpu_read(mipsr2bdemustats.loads));
2263 seq_printf(s, "stores\t\t%ld\t%ld\n",
2264 (unsigned long)__this_cpu_read(mipsr2emustats.stores),
2265 (unsigned long)__this_cpu_read(mipsr2bdemustats.stores));
2266 seq_printf(s, "llsc\t\t%ld\t%ld\n",
2267 (unsigned long)__this_cpu_read(mipsr2emustats.llsc),
2268 (unsigned long)__this_cpu_read(mipsr2bdemustats.llsc));
2269 seq_printf(s, "dsemul\t\t%ld\t%ld\n",
2270 (unsigned long)__this_cpu_read(mipsr2emustats.dsemul),
2271 (unsigned long)__this_cpu_read(mipsr2bdemustats.dsemul));
2272 seq_printf(s, "jr\t\t%ld\n",
2273 (unsigned long)__this_cpu_read(mipsr2bremustats.jrs));
2274 seq_printf(s, "bltzl\t\t%ld\n",
2275 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzl));
2276 seq_printf(s, "bgezl\t\t%ld\n",
2277 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezl));
2278 seq_printf(s, "bltzll\t\t%ld\n",
2279 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzll));
2280 seq_printf(s, "bgezll\t\t%ld\n",
2281 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezll));
2282 seq_printf(s, "bltzal\t\t%ld\n",
2283 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzal));
2284 seq_printf(s, "bgezal\t\t%ld\n",
2285 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezal));
2286 seq_printf(s, "beql\t\t%ld\n",
2287 (unsigned long)__this_cpu_read(mipsr2bremustats.beql));
2288 seq_printf(s, "bnel\t\t%ld\n",
2289 (unsigned long)__this_cpu_read(mipsr2bremustats.bnel));
2290 seq_printf(s, "blezl\t\t%ld\n",
2291 (unsigned long)__this_cpu_read(mipsr2bremustats.blezl));
2292 seq_printf(s, "bgtzl\t\t%ld\n",
2293 (unsigned long)__this_cpu_read(mipsr2bremustats.bgtzl));
2298 static int mipsr2_stats_clear_show(struct seq_file *s, void *unused)
2300 mipsr2_stats_show(s, unused);
2302 __this_cpu_write((mipsr2emustats).movs, 0);
2303 __this_cpu_write((mipsr2bdemustats).movs, 0);
2304 __this_cpu_write((mipsr2emustats).hilo, 0);
2305 __this_cpu_write((mipsr2bdemustats).hilo, 0);
2306 __this_cpu_write((mipsr2emustats).muls, 0);
2307 __this_cpu_write((mipsr2bdemustats).muls, 0);
2308 __this_cpu_write((mipsr2emustats).divs, 0);
2309 __this_cpu_write((mipsr2bdemustats).divs, 0);
2310 __this_cpu_write((mipsr2emustats).dsps, 0);
2311 __this_cpu_write((mipsr2bdemustats).dsps, 0);
2312 __this_cpu_write((mipsr2emustats).bops, 0);
2313 __this_cpu_write((mipsr2bdemustats).bops, 0);
2314 __this_cpu_write((mipsr2emustats).traps, 0);
2315 __this_cpu_write((mipsr2bdemustats).traps, 0);
2316 __this_cpu_write((mipsr2emustats).fpus, 0);
2317 __this_cpu_write((mipsr2bdemustats).fpus, 0);
2318 __this_cpu_write((mipsr2emustats).loads, 0);
2319 __this_cpu_write((mipsr2bdemustats).loads, 0);
2320 __this_cpu_write((mipsr2emustats).stores, 0);
2321 __this_cpu_write((mipsr2bdemustats).stores, 0);
2322 __this_cpu_write((mipsr2emustats).llsc, 0);
2323 __this_cpu_write((mipsr2bdemustats).llsc, 0);
2324 __this_cpu_write((mipsr2emustats).dsemul, 0);
2325 __this_cpu_write((mipsr2bdemustats).dsemul, 0);
2326 __this_cpu_write((mipsr2bremustats).jrs, 0);
2327 __this_cpu_write((mipsr2bremustats).bltzl, 0);
2328 __this_cpu_write((mipsr2bremustats).bgezl, 0);
2329 __this_cpu_write((mipsr2bremustats).bltzll, 0);
2330 __this_cpu_write((mipsr2bremustats).bgezll, 0);
2331 __this_cpu_write((mipsr2bremustats).bltzal, 0);
2332 __this_cpu_write((mipsr2bremustats).bgezal, 0);
2333 __this_cpu_write((mipsr2bremustats).beql, 0);
2334 __this_cpu_write((mipsr2bremustats).bnel, 0);
2335 __this_cpu_write((mipsr2bremustats).blezl, 0);
2336 __this_cpu_write((mipsr2bremustats).bgtzl, 0);
2341 static int mipsr2_stats_open(struct inode *inode, struct file *file)
2343 return single_open(file, mipsr2_stats_show, inode->i_private);
2346 static int mipsr2_stats_clear_open(struct inode *inode, struct file *file)
2348 return single_open(file, mipsr2_stats_clear_show, inode->i_private);
2351 static const struct file_operations mipsr2_emul_fops = {
2352 .open = mipsr2_stats_open,
2354 .llseek = seq_lseek,
2355 .release = single_release,
2358 static const struct file_operations mipsr2_clear_fops = {
2359 .open = mipsr2_stats_clear_open,
2361 .llseek = seq_lseek,
2362 .release = single_release,
2366 static int __init mipsr2_init_debugfs(void)
2368 struct dentry *mipsr2_emul;
2370 if (!mips_debugfs_dir)
2373 mipsr2_emul = debugfs_create_file("r2_emul_stats", S_IRUGO,
2374 mips_debugfs_dir, NULL,
2379 mipsr2_emul = debugfs_create_file("r2_emul_stats_clear", S_IRUGO,
2380 mips_debugfs_dir, NULL,
2381 &mipsr2_clear_fops);
2388 device_initcall(mipsr2_init_debugfs);
2390 #endif /* CONFIG_DEBUG_FS */