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/ptrace.h>
19 #include <linux/seq_file.h>
22 #include <asm/branch.h>
23 #include <asm/break.h>
24 #include <asm/debug.h>
26 #include <asm/fpu_emulator.h>
28 #include <asm/mips-r2-to-r6-emul.h>
29 #include <asm/local.h>
30 #include <asm/mipsregs.h>
31 #include <asm/ptrace.h>
32 #include <linux/uaccess.h>
35 #define ADDIU "daddiu "
39 #define ADDIU "addiu "
42 #endif /* CONFIG_64BIT */
49 #ifdef CONFIG_DEBUG_FS
50 static DEFINE_PER_CPU(struct mips_r2_emulator_stats, mipsr2emustats);
51 static DEFINE_PER_CPU(struct mips_r2_emulator_stats, mipsr2bdemustats);
52 static DEFINE_PER_CPU(struct mips_r2br_emulator_stats, mipsr2bremustats);
55 extern const unsigned int fpucondbit[8];
57 #define MIPS_R2_EMUL_TOTAL_PASS 10
59 int mipsr2_emulation = 0;
61 static int __init mipsr2emu_enable(char *s)
65 pr_info("MIPS R2-to-R6 Emulator Enabled!");
69 __setup("mipsr2emu", mipsr2emu_enable);
72 * mipsr6_emul - Emulate some frequent R2/R5/R6 instructions in delay slot
73 * for performance instead of the traditional way of using a stack trampoline
74 * which is rather slow.
75 * @regs: Process register set
78 static inline int mipsr6_emul(struct pt_regs *regs, u32 ir)
80 switch (MIPSInst_OPCODE(ir)) {
83 regs->regs[MIPSInst_RT(ir)] =
84 (s32)regs->regs[MIPSInst_RS(ir)] +
85 (s32)MIPSInst_SIMM(ir);
88 if (IS_ENABLED(CONFIG_32BIT))
92 regs->regs[MIPSInst_RT(ir)] =
93 (s64)regs->regs[MIPSInst_RS(ir)] +
94 (s64)MIPSInst_SIMM(ir);
100 /* FPU instructions in delay slot */
103 switch (MIPSInst_FUNC(ir)) {
106 regs->regs[MIPSInst_RD(ir)] =
107 regs->regs[MIPSInst_RS(ir)] |
108 regs->regs[MIPSInst_RT(ir)];
115 regs->regs[MIPSInst_RD(ir)] =
116 (s32)(((u32)regs->regs[MIPSInst_RT(ir)]) <<
124 regs->regs[MIPSInst_RD(ir)] =
125 (s32)(((u32)regs->regs[MIPSInst_RT(ir)]) >>
133 regs->regs[MIPSInst_RD(ir)] =
134 (s32)((u32)regs->regs[MIPSInst_RS(ir)] +
135 (u32)regs->regs[MIPSInst_RT(ir)]);
142 regs->regs[MIPSInst_RD(ir)] =
143 (s32)((u32)regs->regs[MIPSInst_RS(ir)] -
144 (u32)regs->regs[MIPSInst_RT(ir)]);
147 if (IS_ENABLED(CONFIG_32BIT) || MIPSInst_RS(ir))
151 regs->regs[MIPSInst_RD(ir)] =
152 (s64)(((u64)regs->regs[MIPSInst_RT(ir)]) <<
156 if (IS_ENABLED(CONFIG_32BIT) || MIPSInst_RS(ir))
160 regs->regs[MIPSInst_RD(ir)] =
161 (s64)(((u64)regs->regs[MIPSInst_RT(ir)]) >>
165 if (IS_ENABLED(CONFIG_32BIT) || MIPSInst_FD(ir))
169 regs->regs[MIPSInst_RD(ir)] =
170 (u64)regs->regs[MIPSInst_RS(ir)] +
171 (u64)regs->regs[MIPSInst_RT(ir)];
174 if (IS_ENABLED(CONFIG_32BIT) || MIPSInst_FD(ir))
178 regs->regs[MIPSInst_RD(ir)] =
179 (s64)((u64)regs->regs[MIPSInst_RS(ir)] -
180 (u64)regs->regs[MIPSInst_RT(ir)]);
185 pr_debug("No fastpath BD emulation for instruction 0x%08x (op: %02x)\n",
186 ir, MIPSInst_OPCODE(ir));
193 * movf_func - Emulate a MOVF instruction
194 * @regs: Process register set
197 * Returns 0 since it always succeeds.
199 static int movf_func(struct pt_regs *regs, u32 ir)
204 csr = current->thread.fpu.fcr31;
205 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
207 if (((csr & cond) == 0) && MIPSInst_RD(ir))
208 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
216 * movt_func - Emulate a MOVT instruction
217 * @regs: Process register set
220 * Returns 0 since it always succeeds.
222 static int movt_func(struct pt_regs *regs, u32 ir)
227 csr = current->thread.fpu.fcr31;
228 cond = fpucondbit[MIPSInst_RT(ir) >> 2];
230 if (((csr & cond) != 0) && MIPSInst_RD(ir))
231 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
239 * jr_func - Emulate a JR instruction.
240 * @pt_regs: Process register set
243 * Returns SIGILL if JR was in delay slot, SIGEMT if we
244 * can't compute the EPC, SIGSEGV if we can't access the
245 * userland instruction or 0 on success.
247 static int jr_func(struct pt_regs *regs, u32 ir)
250 unsigned long cepc, epc, nepc;
253 if (delay_slot(regs))
256 /* EPC after the RI/JR instruction */
257 nepc = regs->cp0_epc;
258 /* Roll back to the reserved R2 JR instruction */
261 err = __compute_return_epc(regs);
268 cepc = regs->cp0_epc;
270 /* Get DS instruction */
271 err = __get_user(nir, (u32 __user *)nepc);
275 MIPS_R2BR_STATS(jrs);
277 /* If nir == 0(NOP), then nothing else to do */
280 * Negative err means FPU instruction in BD-slot,
281 * Zero err means 'BD-slot emulation done'
282 * For anything else we go back to trampoline emulation.
284 err = mipsr6_emul(regs, nir);
286 regs->cp0_epc = nepc;
287 err = mips_dsemul(regs, nir, epc, cepc);
290 MIPS_R2_STATS(dsemul);
298 * movz_func - Emulate a MOVZ instruction
299 * @regs: Process register set
302 * Returns 0 since it always succeeds.
304 static int movz_func(struct pt_regs *regs, u32 ir)
306 if (((regs->regs[MIPSInst_RT(ir)]) == 0) && MIPSInst_RD(ir))
307 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
314 * movn_func - Emulate a MOVZ instruction
315 * @regs: Process register set
318 * Returns 0 since it always succeeds.
320 static int movn_func(struct pt_regs *regs, u32 ir)
322 if (((regs->regs[MIPSInst_RT(ir)]) != 0) && MIPSInst_RD(ir))
323 regs->regs[MIPSInst_RD(ir)] = regs->regs[MIPSInst_RS(ir)];
330 * mfhi_func - Emulate a MFHI instruction
331 * @regs: Process register set
334 * Returns 0 since it always succeeds.
336 static int mfhi_func(struct pt_regs *regs, u32 ir)
339 regs->regs[MIPSInst_RD(ir)] = regs->hi;
347 * mthi_func - Emulate a MTHI instruction
348 * @regs: Process register set
351 * Returns 0 since it always succeeds.
353 static int mthi_func(struct pt_regs *regs, u32 ir)
355 regs->hi = regs->regs[MIPSInst_RS(ir)];
363 * mflo_func - Emulate a MFLO instruction
364 * @regs: Process register set
367 * Returns 0 since it always succeeds.
369 static int mflo_func(struct pt_regs *regs, u32 ir)
372 regs->regs[MIPSInst_RD(ir)] = regs->lo;
380 * mtlo_func - Emulate a MTLO instruction
381 * @regs: Process register set
384 * Returns 0 since it always succeeds.
386 static int mtlo_func(struct pt_regs *regs, u32 ir)
388 regs->lo = regs->regs[MIPSInst_RS(ir)];
396 * mult_func - Emulate a MULT instruction
397 * @regs: Process register set
400 * Returns 0 since it always succeeds.
402 static int mult_func(struct pt_regs *regs, u32 ir)
407 rt = regs->regs[MIPSInst_RT(ir)];
408 rs = regs->regs[MIPSInst_RS(ir)];
409 res = (s64)rt * (s64)rs;
423 * multu_func - Emulate a MULTU instruction
424 * @regs: Process register set
427 * Returns 0 since it always succeeds.
429 static int multu_func(struct pt_regs *regs, u32 ir)
434 rt = regs->regs[MIPSInst_RT(ir)];
435 rs = regs->regs[MIPSInst_RS(ir)];
436 res = (u64)rt * (u64)rs;
438 regs->lo = (s64)(s32)rt;
439 regs->hi = (s64)(s32)(res >> 32);
447 * div_func - Emulate a DIV instruction
448 * @regs: Process register set
451 * Returns 0 since it always succeeds.
453 static int div_func(struct pt_regs *regs, u32 ir)
457 rt = regs->regs[MIPSInst_RT(ir)];
458 rs = regs->regs[MIPSInst_RS(ir)];
460 regs->lo = (s64)(rs / rt);
461 regs->hi = (s64)(rs % rt);
469 * divu_func - Emulate a DIVU instruction
470 * @regs: Process register set
473 * Returns 0 since it always succeeds.
475 static int divu_func(struct pt_regs *regs, u32 ir)
479 rt = regs->regs[MIPSInst_RT(ir)];
480 rs = regs->regs[MIPSInst_RS(ir)];
482 regs->lo = (s64)(rs / rt);
483 regs->hi = (s64)(rs % rt);
491 * dmult_func - Emulate a DMULT instruction
492 * @regs: Process register set
495 * Returns 0 on success or SIGILL for 32-bit kernels.
497 static int dmult_func(struct pt_regs *regs, u32 ir)
502 if (IS_ENABLED(CONFIG_32BIT))
505 rt = regs->regs[MIPSInst_RT(ir)];
506 rs = regs->regs[MIPSInst_RS(ir)];
510 __asm__ __volatile__(
511 "dmuh %0, %1, %2\t\n"
523 * dmultu_func - Emulate a DMULTU instruction
524 * @regs: Process register set
527 * Returns 0 on success or SIGILL for 32-bit kernels.
529 static int dmultu_func(struct pt_regs *regs, u32 ir)
534 if (IS_ENABLED(CONFIG_32BIT))
537 rt = regs->regs[MIPSInst_RT(ir)];
538 rs = regs->regs[MIPSInst_RS(ir)];
542 __asm__ __volatile__(
543 "dmuhu %0, %1, %2\t\n"
555 * ddiv_func - Emulate a DDIV instruction
556 * @regs: Process register set
559 * Returns 0 on success or SIGILL for 32-bit kernels.
561 static int ddiv_func(struct pt_regs *regs, u32 ir)
565 if (IS_ENABLED(CONFIG_32BIT))
568 rt = regs->regs[MIPSInst_RT(ir)];
569 rs = regs->regs[MIPSInst_RS(ir)];
580 * ddivu_func - Emulate a DDIVU instruction
581 * @regs: Process register set
584 * Returns 0 on success or SIGILL for 32-bit kernels.
586 static int ddivu_func(struct pt_regs *regs, u32 ir)
590 if (IS_ENABLED(CONFIG_32BIT))
593 rt = regs->regs[MIPSInst_RT(ir)];
594 rs = regs->regs[MIPSInst_RS(ir)];
604 /* R6 removed instructions for the SPECIAL opcode */
605 static const struct r2_decoder_table spec_op_table[] = {
606 { 0xfc1ff83f, 0x00000008, jr_func },
607 { 0xfc00ffff, 0x00000018, mult_func },
608 { 0xfc00ffff, 0x00000019, multu_func },
609 { 0xfc00ffff, 0x0000001c, dmult_func },
610 { 0xfc00ffff, 0x0000001d, dmultu_func },
611 { 0xffff07ff, 0x00000010, mfhi_func },
612 { 0xfc1fffff, 0x00000011, mthi_func },
613 { 0xffff07ff, 0x00000012, mflo_func },
614 { 0xfc1fffff, 0x00000013, mtlo_func },
615 { 0xfc0307ff, 0x00000001, movf_func },
616 { 0xfc0307ff, 0x00010001, movt_func },
617 { 0xfc0007ff, 0x0000000a, movz_func },
618 { 0xfc0007ff, 0x0000000b, movn_func },
619 { 0xfc00ffff, 0x0000001a, div_func },
620 { 0xfc00ffff, 0x0000001b, divu_func },
621 { 0xfc00ffff, 0x0000001e, ddiv_func },
622 { 0xfc00ffff, 0x0000001f, ddivu_func },
627 * madd_func - Emulate a MADD instruction
628 * @regs: Process register set
631 * Returns 0 since it always succeeds.
633 static int madd_func(struct pt_regs *regs, u32 ir)
638 rt = regs->regs[MIPSInst_RT(ir)];
639 rs = regs->regs[MIPSInst_RS(ir)];
640 res = (s64)rt * (s64)rs;
643 res += ((((s64)rt) << 32) | (u32)rs);
656 * maddu_func - Emulate a MADDU instruction
657 * @regs: Process register set
660 * Returns 0 since it always succeeds.
662 static int maddu_func(struct pt_regs *regs, u32 ir)
667 rt = regs->regs[MIPSInst_RT(ir)];
668 rs = regs->regs[MIPSInst_RS(ir)];
669 res = (u64)rt * (u64)rs;
672 res += ((((s64)rt) << 32) | (u32)rs);
675 regs->lo = (s64)(s32)rt;
677 regs->hi = (s64)(s32)rs;
685 * msub_func - Emulate a MSUB instruction
686 * @regs: Process register set
689 * Returns 0 since it always succeeds.
691 static int msub_func(struct pt_regs *regs, u32 ir)
696 rt = regs->regs[MIPSInst_RT(ir)];
697 rs = regs->regs[MIPSInst_RS(ir)];
698 res = (s64)rt * (s64)rs;
701 res = ((((s64)rt) << 32) | (u32)rs) - res;
714 * msubu_func - Emulate a MSUBU instruction
715 * @regs: Process register set
718 * Returns 0 since it always succeeds.
720 static int msubu_func(struct pt_regs *regs, u32 ir)
725 rt = regs->regs[MIPSInst_RT(ir)];
726 rs = regs->regs[MIPSInst_RS(ir)];
727 res = (u64)rt * (u64)rs;
730 res = ((((s64)rt) << 32) | (u32)rs) - res;
733 regs->lo = (s64)(s32)rt;
735 regs->hi = (s64)(s32)rs;
743 * mul_func - Emulate a MUL instruction
744 * @regs: Process register set
747 * Returns 0 since it always succeeds.
749 static int mul_func(struct pt_regs *regs, u32 ir)
754 if (!MIPSInst_RD(ir))
756 rt = regs->regs[MIPSInst_RT(ir)];
757 rs = regs->regs[MIPSInst_RS(ir)];
758 res = (s64)rt * (s64)rs;
761 regs->regs[MIPSInst_RD(ir)] = (s64)rs;
769 * clz_func - Emulate a CLZ instruction
770 * @regs: Process register set
773 * Returns 0 since it always succeeds.
775 static int clz_func(struct pt_regs *regs, u32 ir)
780 if (!MIPSInst_RD(ir))
783 rs = regs->regs[MIPSInst_RS(ir)];
784 __asm__ __volatile__("clz %0, %1" : "=r"(res) : "r"(rs));
785 regs->regs[MIPSInst_RD(ir)] = res;
793 * clo_func - Emulate a CLO instruction
794 * @regs: Process register set
797 * Returns 0 since it always succeeds.
800 static int clo_func(struct pt_regs *regs, u32 ir)
805 if (!MIPSInst_RD(ir))
808 rs = regs->regs[MIPSInst_RS(ir)];
809 __asm__ __volatile__("clo %0, %1" : "=r"(res) : "r"(rs));
810 regs->regs[MIPSInst_RD(ir)] = res;
818 * dclz_func - Emulate a DCLZ instruction
819 * @regs: Process register set
822 * Returns 0 since it always succeeds.
824 static int dclz_func(struct pt_regs *regs, u32 ir)
829 if (IS_ENABLED(CONFIG_32BIT))
832 if (!MIPSInst_RD(ir))
835 rs = regs->regs[MIPSInst_RS(ir)];
836 __asm__ __volatile__("dclz %0, %1" : "=r"(res) : "r"(rs));
837 regs->regs[MIPSInst_RD(ir)] = res;
845 * dclo_func - Emulate a DCLO instruction
846 * @regs: Process register set
849 * Returns 0 since it always succeeds.
851 static int dclo_func(struct pt_regs *regs, u32 ir)
856 if (IS_ENABLED(CONFIG_32BIT))
859 if (!MIPSInst_RD(ir))
862 rs = regs->regs[MIPSInst_RS(ir)];
863 __asm__ __volatile__("dclo %0, %1" : "=r"(res) : "r"(rs));
864 regs->regs[MIPSInst_RD(ir)] = res;
871 /* R6 removed instructions for the SPECIAL2 opcode */
872 static const struct r2_decoder_table spec2_op_table[] = {
873 { 0xfc00ffff, 0x70000000, madd_func },
874 { 0xfc00ffff, 0x70000001, maddu_func },
875 { 0xfc0007ff, 0x70000002, mul_func },
876 { 0xfc00ffff, 0x70000004, msub_func },
877 { 0xfc00ffff, 0x70000005, msubu_func },
878 { 0xfc0007ff, 0x70000020, clz_func },
879 { 0xfc0007ff, 0x70000021, clo_func },
880 { 0xfc0007ff, 0x70000024, dclz_func },
881 { 0xfc0007ff, 0x70000025, dclo_func },
885 static inline int mipsr2_find_op_func(struct pt_regs *regs, u32 inst,
886 const struct r2_decoder_table *table)
888 const struct r2_decoder_table *p;
891 for (p = table; p->func; p++) {
892 if ((inst & p->mask) == p->code) {
893 err = (p->func)(regs, inst);
901 * mipsr2_decoder: Decode and emulate a MIPS R2 instruction
902 * @regs: Process register set
903 * @inst: Instruction to decode and emulate
904 * @fcr31: Floating Point Control and Status Register Cause bits returned
906 int mipsr2_decoder(struct pt_regs *regs, u32 inst, unsigned long *fcr31)
911 unsigned long cpc, epc, nepc, r31, res, rs, rt;
913 void __user *fault_addr = NULL;
917 r31 = regs->regs[31];
919 err = compute_return_epc(regs);
924 pr_debug("Emulating the 0x%08x R2 instruction @ 0x%08lx (pass=%d))\n",
927 switch (MIPSInst_OPCODE(inst)) {
929 err = mipsr2_find_op_func(regs, inst, spec_op_table);
931 /* FPU instruction under JR */
932 regs->cp0_cause |= CAUSEF_BD;
937 err = mipsr2_find_op_func(regs, inst, spec2_op_table);
940 rt = MIPSInst_RT(inst);
941 rs = MIPSInst_RS(inst);
944 if ((long)regs->regs[rs] >= MIPSInst_SIMM(inst))
945 do_trap_or_bp(regs, 0, 0, "TGEI");
947 MIPS_R2_STATS(traps);
951 if (regs->regs[rs] >= MIPSInst_UIMM(inst))
952 do_trap_or_bp(regs, 0, 0, "TGEIU");
954 MIPS_R2_STATS(traps);
958 if ((long)regs->regs[rs] < MIPSInst_SIMM(inst))
959 do_trap_or_bp(regs, 0, 0, "TLTI");
961 MIPS_R2_STATS(traps);
965 if (regs->regs[rs] < MIPSInst_UIMM(inst))
966 do_trap_or_bp(regs, 0, 0, "TLTIU");
968 MIPS_R2_STATS(traps);
972 if (regs->regs[rs] == MIPSInst_SIMM(inst))
973 do_trap_or_bp(regs, 0, 0, "TEQI");
975 MIPS_R2_STATS(traps);
979 if (regs->regs[rs] != MIPSInst_SIMM(inst))
980 do_trap_or_bp(regs, 0, 0, "TNEI");
982 MIPS_R2_STATS(traps);
989 if (delay_slot(regs)) {
993 regs->regs[31] = r31;
995 err = __compute_return_epc(regs);
998 if (err != BRANCH_LIKELY_TAKEN)
1000 cpc = regs->cp0_epc;
1002 err = __get_user(nir, (u32 __user *)nepc);
1008 * This will probably be optimized away when
1009 * CONFIG_DEBUG_FS is not enabled
1013 MIPS_R2BR_STATS(bltzl);
1016 MIPS_R2BR_STATS(bgezl);
1019 MIPS_R2BR_STATS(bltzall);
1022 MIPS_R2BR_STATS(bgezall);
1026 switch (MIPSInst_OPCODE(nir)) {
1031 regs->cp0_cause |= CAUSEF_BD;
1035 err = mipsr6_emul(regs, nir);
1037 err = mips_dsemul(regs, nir, epc, cpc);
1040 MIPS_R2_STATS(dsemul);
1046 if (delay_slot(regs)) {
1050 regs->regs[31] = r31;
1051 regs->cp0_epc = epc;
1052 err = __compute_return_epc(regs);
1055 cpc = regs->cp0_epc;
1057 err = __get_user(nir, (u32 __user *)nepc);
1063 * This will probably be optimized away when
1064 * CONFIG_DEBUG_FS is not enabled
1068 MIPS_R2BR_STATS(bltzal);
1071 MIPS_R2BR_STATS(bgezal);
1075 switch (MIPSInst_OPCODE(nir)) {
1080 regs->cp0_cause |= CAUSEF_BD;
1084 err = mipsr6_emul(regs, nir);
1086 err = mips_dsemul(regs, nir, epc, cpc);
1089 MIPS_R2_STATS(dsemul);
1094 regs->regs[31] = r31;
1095 regs->cp0_epc = epc;
1104 * For BLEZL and BGTZL, rt field must be set to 0. If this
1105 * is not the case, this may be an encoding of a MIPS R6
1106 * instruction, so return to CPU execution if this occurs
1108 if (MIPSInst_RT(inst)) {
1115 if (delay_slot(regs)) {
1119 regs->regs[31] = r31;
1120 regs->cp0_epc = epc;
1121 err = __compute_return_epc(regs);
1124 if (err != BRANCH_LIKELY_TAKEN)
1126 cpc = regs->cp0_epc;
1128 err = __get_user(nir, (u32 __user *)nepc);
1134 * This will probably be optimized away when
1135 * CONFIG_DEBUG_FS is not enabled
1137 switch (MIPSInst_OPCODE(inst)) {
1139 MIPS_R2BR_STATS(beql);
1142 MIPS_R2BR_STATS(bnel);
1145 MIPS_R2BR_STATS(blezl);
1148 MIPS_R2BR_STATS(bgtzl);
1152 switch (MIPSInst_OPCODE(nir)) {
1157 regs->cp0_cause |= CAUSEF_BD;
1161 err = mipsr6_emul(regs, nir);
1163 err = mips_dsemul(regs, nir, epc, cpc);
1166 MIPS_R2_STATS(dsemul);
1175 regs->regs[31] = r31;
1176 regs->cp0_epc = epc;
1178 err = fpu_emulator_cop1Handler(regs, ¤t->thread.fpu, 0,
1182 * We can't allow the emulated instruction to leave any
1183 * enabled Cause bits set in $fcr31.
1185 *fcr31 = res = mask_fcr31_x(current->thread.fpu.fcr31);
1186 current->thread.fpu.fcr31 &= ~res;
1189 * this is a tricky issue - lose_fpu() uses LL/SC atomics
1190 * if FPU is owned and effectively cancels user level LL/SC.
1191 * So, it could be logical to don't restore FPU ownership here.
1192 * But the sequence of multiple FPU instructions is much much
1193 * more often than LL-FPU-SC and I prefer loop here until
1194 * next scheduler cycle cancels FPU ownership
1196 own_fpu(1); /* Restore FPU state. */
1199 current->thread.cp0_baduaddr = (unsigned long)fault_addr;
1201 MIPS_R2_STATS(fpus);
1206 rt = regs->regs[MIPSInst_RT(inst)];
1207 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1208 if (!access_ok((void __user *)vaddr, 4)) {
1209 current->thread.cp0_baduaddr = vaddr;
1213 __asm__ __volatile__(
1216 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1217 "1:" LB "%1, 0(%2)\n"
1218 INS "%0, %1, 24, 8\n"
1219 " andi %1, %2, 0x3\n"
1221 ADDIU "%2, %2, -1\n"
1222 "2:" LB "%1, 0(%2)\n"
1223 INS "%0, %1, 16, 8\n"
1224 " andi %1, %2, 0x3\n"
1226 ADDIU "%2, %2, -1\n"
1227 "3:" LB "%1, 0(%2)\n"
1228 INS "%0, %1, 8, 8\n"
1229 " andi %1, %2, 0x3\n"
1231 ADDIU "%2, %2, -1\n"
1232 "4:" LB "%1, 0(%2)\n"
1233 INS "%0, %1, 0, 8\n"
1234 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1235 "1:" LB "%1, 0(%2)\n"
1236 INS "%0, %1, 24, 8\n"
1238 " andi %1, %2, 0x3\n"
1240 "2:" LB "%1, 0(%2)\n"
1241 INS "%0, %1, 16, 8\n"
1243 " andi %1, %2, 0x3\n"
1245 "3:" LB "%1, 0(%2)\n"
1246 INS "%0, %1, 8, 8\n"
1248 " andi %1, %2, 0x3\n"
1250 "4:" LB "%1, 0(%2)\n"
1251 INS "%0, %1, 0, 8\n"
1252 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1253 "9: sll %0, %0, 0\n"
1256 " .section .fixup,\"ax\"\n"
1260 " .section __ex_table,\"a\"\n"
1267 : "+&r"(rt), "=&r"(rs),
1268 "+&r"(vaddr), "+&r"(err)
1271 if (MIPSInst_RT(inst) && !err)
1272 regs->regs[MIPSInst_RT(inst)] = rt;
1274 MIPS_R2_STATS(loads);
1279 rt = regs->regs[MIPSInst_RT(inst)];
1280 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1281 if (!access_ok((void __user *)vaddr, 4)) {
1282 current->thread.cp0_baduaddr = vaddr;
1286 __asm__ __volatile__(
1289 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1290 "1:" LB "%1, 0(%2)\n"
1291 INS "%0, %1, 0, 8\n"
1293 " andi %1, %2, 0x3\n"
1295 "2:" LB "%1, 0(%2)\n"
1296 INS "%0, %1, 8, 8\n"
1298 " andi %1, %2, 0x3\n"
1300 "3:" LB "%1, 0(%2)\n"
1301 INS "%0, %1, 16, 8\n"
1303 " andi %1, %2, 0x3\n"
1305 "4:" LB "%1, 0(%2)\n"
1306 INS "%0, %1, 24, 8\n"
1308 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1309 "1:" LB "%1, 0(%2)\n"
1310 INS "%0, %1, 0, 8\n"
1311 " andi %1, %2, 0x3\n"
1313 ADDIU "%2, %2, -1\n"
1314 "2:" LB "%1, 0(%2)\n"
1315 INS "%0, %1, 8, 8\n"
1316 " andi %1, %2, 0x3\n"
1318 ADDIU "%2, %2, -1\n"
1319 "3:" LB "%1, 0(%2)\n"
1320 INS "%0, %1, 16, 8\n"
1321 " andi %1, %2, 0x3\n"
1323 ADDIU "%2, %2, -1\n"
1324 "4:" LB "%1, 0(%2)\n"
1325 INS "%0, %1, 24, 8\n"
1327 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1331 " .section .fixup,\"ax\"\n"
1335 " .section __ex_table,\"a\"\n"
1342 : "+&r"(rt), "=&r"(rs),
1343 "+&r"(vaddr), "+&r"(err)
1345 if (MIPSInst_RT(inst) && !err)
1346 regs->regs[MIPSInst_RT(inst)] = rt;
1348 MIPS_R2_STATS(loads);
1353 rt = regs->regs[MIPSInst_RT(inst)];
1354 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1355 if (!access_ok((void __user *)vaddr, 4)) {
1356 current->thread.cp0_baduaddr = vaddr;
1360 __asm__ __volatile__(
1363 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1364 EXT "%1, %0, 24, 8\n"
1365 "1:" SB "%1, 0(%2)\n"
1366 " andi %1, %2, 0x3\n"
1368 ADDIU "%2, %2, -1\n"
1369 EXT "%1, %0, 16, 8\n"
1370 "2:" SB "%1, 0(%2)\n"
1371 " andi %1, %2, 0x3\n"
1373 ADDIU "%2, %2, -1\n"
1374 EXT "%1, %0, 8, 8\n"
1375 "3:" SB "%1, 0(%2)\n"
1376 " andi %1, %2, 0x3\n"
1378 ADDIU "%2, %2, -1\n"
1379 EXT "%1, %0, 0, 8\n"
1380 "4:" SB "%1, 0(%2)\n"
1381 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1382 EXT "%1, %0, 24, 8\n"
1383 "1:" SB "%1, 0(%2)\n"
1385 " andi %1, %2, 0x3\n"
1387 EXT "%1, %0, 16, 8\n"
1388 "2:" SB "%1, 0(%2)\n"
1390 " andi %1, %2, 0x3\n"
1392 EXT "%1, %0, 8, 8\n"
1393 "3:" SB "%1, 0(%2)\n"
1395 " andi %1, %2, 0x3\n"
1397 EXT "%1, %0, 0, 8\n"
1398 "4:" SB "%1, 0(%2)\n"
1399 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1402 " .section .fixup,\"ax\"\n"
1406 " .section __ex_table,\"a\"\n"
1413 : "+&r"(rt), "=&r"(rs),
1414 "+&r"(vaddr), "+&r"(err)
1418 MIPS_R2_STATS(stores);
1423 rt = regs->regs[MIPSInst_RT(inst)];
1424 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1425 if (!access_ok((void __user *)vaddr, 4)) {
1426 current->thread.cp0_baduaddr = vaddr;
1430 __asm__ __volatile__(
1433 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1434 EXT "%1, %0, 0, 8\n"
1435 "1:" SB "%1, 0(%2)\n"
1437 " andi %1, %2, 0x3\n"
1439 EXT "%1, %0, 8, 8\n"
1440 "2:" SB "%1, 0(%2)\n"
1442 " andi %1, %2, 0x3\n"
1444 EXT "%1, %0, 16, 8\n"
1445 "3:" SB "%1, 0(%2)\n"
1447 " andi %1, %2, 0x3\n"
1449 EXT "%1, %0, 24, 8\n"
1450 "4:" SB "%1, 0(%2)\n"
1451 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1452 EXT "%1, %0, 0, 8\n"
1453 "1:" SB "%1, 0(%2)\n"
1454 " andi %1, %2, 0x3\n"
1456 ADDIU "%2, %2, -1\n"
1457 EXT "%1, %0, 8, 8\n"
1458 "2:" SB "%1, 0(%2)\n"
1459 " andi %1, %2, 0x3\n"
1461 ADDIU "%2, %2, -1\n"
1462 EXT "%1, %0, 16, 8\n"
1463 "3:" SB "%1, 0(%2)\n"
1464 " andi %1, %2, 0x3\n"
1466 ADDIU "%2, %2, -1\n"
1467 EXT "%1, %0, 24, 8\n"
1468 "4:" SB "%1, 0(%2)\n"
1469 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1472 " .section .fixup,\"ax\"\n"
1476 " .section __ex_table,\"a\"\n"
1483 : "+&r"(rt), "=&r"(rs),
1484 "+&r"(vaddr), "+&r"(err)
1488 MIPS_R2_STATS(stores);
1493 if (IS_ENABLED(CONFIG_32BIT)) {
1498 rt = regs->regs[MIPSInst_RT(inst)];
1499 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1500 if (!access_ok((void __user *)vaddr, 8)) {
1501 current->thread.cp0_baduaddr = vaddr;
1505 __asm__ __volatile__(
1508 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1510 " dinsu %0, %1, 56, 8\n"
1511 " andi %1, %2, 0x7\n"
1513 " daddiu %2, %2, -1\n"
1515 " dinsu %0, %1, 48, 8\n"
1516 " andi %1, %2, 0x7\n"
1518 " daddiu %2, %2, -1\n"
1520 " dinsu %0, %1, 40, 8\n"
1521 " andi %1, %2, 0x7\n"
1523 " daddiu %2, %2, -1\n"
1525 " dinsu %0, %1, 32, 8\n"
1526 " andi %1, %2, 0x7\n"
1528 " daddiu %2, %2, -1\n"
1530 " dins %0, %1, 24, 8\n"
1531 " andi %1, %2, 0x7\n"
1533 " daddiu %2, %2, -1\n"
1535 " dins %0, %1, 16, 8\n"
1536 " andi %1, %2, 0x7\n"
1538 " daddiu %2, %2, -1\n"
1540 " dins %0, %1, 8, 8\n"
1541 " andi %1, %2, 0x7\n"
1543 " daddiu %2, %2, -1\n"
1545 " dins %0, %1, 0, 8\n"
1546 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1548 " dinsu %0, %1, 56, 8\n"
1549 " daddiu %2, %2, 1\n"
1550 " andi %1, %2, 0x7\n"
1553 " dinsu %0, %1, 48, 8\n"
1554 " daddiu %2, %2, 1\n"
1555 " andi %1, %2, 0x7\n"
1558 " dinsu %0, %1, 40, 8\n"
1559 " daddiu %2, %2, 1\n"
1560 " andi %1, %2, 0x7\n"
1563 " dinsu %0, %1, 32, 8\n"
1564 " daddiu %2, %2, 1\n"
1565 " andi %1, %2, 0x7\n"
1568 " dins %0, %1, 24, 8\n"
1569 " daddiu %2, %2, 1\n"
1570 " andi %1, %2, 0x7\n"
1573 " dins %0, %1, 16, 8\n"
1574 " daddiu %2, %2, 1\n"
1575 " andi %1, %2, 0x7\n"
1578 " dins %0, %1, 8, 8\n"
1579 " daddiu %2, %2, 1\n"
1580 " andi %1, %2, 0x7\n"
1583 " dins %0, %1, 0, 8\n"
1584 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1587 " .section .fixup,\"ax\"\n"
1591 " .section __ex_table,\"a\"\n"
1602 : "+&r"(rt), "=&r"(rs),
1603 "+&r"(vaddr), "+&r"(err)
1605 if (MIPSInst_RT(inst) && !err)
1606 regs->regs[MIPSInst_RT(inst)] = rt;
1608 MIPS_R2_STATS(loads);
1612 if (IS_ENABLED(CONFIG_32BIT)) {
1617 rt = regs->regs[MIPSInst_RT(inst)];
1618 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1619 if (!access_ok((void __user *)vaddr, 8)) {
1620 current->thread.cp0_baduaddr = vaddr;
1624 __asm__ __volatile__(
1627 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1629 " dins %0, %1, 0, 8\n"
1630 " daddiu %2, %2, 1\n"
1631 " andi %1, %2, 0x7\n"
1634 " dins %0, %1, 8, 8\n"
1635 " daddiu %2, %2, 1\n"
1636 " andi %1, %2, 0x7\n"
1639 " dins %0, %1, 16, 8\n"
1640 " daddiu %2, %2, 1\n"
1641 " andi %1, %2, 0x7\n"
1644 " dins %0, %1, 24, 8\n"
1645 " daddiu %2, %2, 1\n"
1646 " andi %1, %2, 0x7\n"
1649 " dinsu %0, %1, 32, 8\n"
1650 " daddiu %2, %2, 1\n"
1651 " andi %1, %2, 0x7\n"
1654 " dinsu %0, %1, 40, 8\n"
1655 " daddiu %2, %2, 1\n"
1656 " andi %1, %2, 0x7\n"
1659 " dinsu %0, %1, 48, 8\n"
1660 " daddiu %2, %2, 1\n"
1661 " andi %1, %2, 0x7\n"
1664 " dinsu %0, %1, 56, 8\n"
1665 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1667 " dins %0, %1, 0, 8\n"
1668 " andi %1, %2, 0x7\n"
1670 " daddiu %2, %2, -1\n"
1672 " dins %0, %1, 8, 8\n"
1673 " andi %1, %2, 0x7\n"
1675 " daddiu %2, %2, -1\n"
1677 " dins %0, %1, 16, 8\n"
1678 " andi %1, %2, 0x7\n"
1680 " daddiu %2, %2, -1\n"
1682 " dins %0, %1, 24, 8\n"
1683 " andi %1, %2, 0x7\n"
1685 " daddiu %2, %2, -1\n"
1687 " dinsu %0, %1, 32, 8\n"
1688 " andi %1, %2, 0x7\n"
1690 " daddiu %2, %2, -1\n"
1692 " dinsu %0, %1, 40, 8\n"
1693 " andi %1, %2, 0x7\n"
1695 " daddiu %2, %2, -1\n"
1697 " dinsu %0, %1, 48, 8\n"
1698 " andi %1, %2, 0x7\n"
1700 " daddiu %2, %2, -1\n"
1702 " dinsu %0, %1, 56, 8\n"
1703 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1706 " .section .fixup,\"ax\"\n"
1710 " .section __ex_table,\"a\"\n"
1721 : "+&r"(rt), "=&r"(rs),
1722 "+&r"(vaddr), "+&r"(err)
1724 if (MIPSInst_RT(inst) && !err)
1725 regs->regs[MIPSInst_RT(inst)] = rt;
1727 MIPS_R2_STATS(loads);
1731 if (IS_ENABLED(CONFIG_32BIT)) {
1736 rt = regs->regs[MIPSInst_RT(inst)];
1737 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1738 if (!access_ok((void __user *)vaddr, 8)) {
1739 current->thread.cp0_baduaddr = vaddr;
1743 __asm__ __volatile__(
1746 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1747 " dextu %1, %0, 56, 8\n"
1749 " andi %1, %2, 0x7\n"
1751 " daddiu %2, %2, -1\n"
1752 " dextu %1, %0, 48, 8\n"
1754 " andi %1, %2, 0x7\n"
1756 " daddiu %2, %2, -1\n"
1757 " dextu %1, %0, 40, 8\n"
1759 " andi %1, %2, 0x7\n"
1761 " daddiu %2, %2, -1\n"
1762 " dextu %1, %0, 32, 8\n"
1764 " andi %1, %2, 0x7\n"
1766 " daddiu %2, %2, -1\n"
1767 " dext %1, %0, 24, 8\n"
1769 " andi %1, %2, 0x7\n"
1771 " daddiu %2, %2, -1\n"
1772 " dext %1, %0, 16, 8\n"
1774 " andi %1, %2, 0x7\n"
1776 " daddiu %2, %2, -1\n"
1777 " dext %1, %0, 8, 8\n"
1779 " andi %1, %2, 0x7\n"
1781 " daddiu %2, %2, -1\n"
1782 " dext %1, %0, 0, 8\n"
1784 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1785 " dextu %1, %0, 56, 8\n"
1787 " daddiu %2, %2, 1\n"
1788 " andi %1, %2, 0x7\n"
1790 " dextu %1, %0, 48, 8\n"
1792 " daddiu %2, %2, 1\n"
1793 " andi %1, %2, 0x7\n"
1795 " dextu %1, %0, 40, 8\n"
1797 " daddiu %2, %2, 1\n"
1798 " andi %1, %2, 0x7\n"
1800 " dextu %1, %0, 32, 8\n"
1802 " daddiu %2, %2, 1\n"
1803 " andi %1, %2, 0x7\n"
1805 " dext %1, %0, 24, 8\n"
1807 " daddiu %2, %2, 1\n"
1808 " andi %1, %2, 0x7\n"
1810 " dext %1, %0, 16, 8\n"
1812 " daddiu %2, %2, 1\n"
1813 " andi %1, %2, 0x7\n"
1815 " dext %1, %0, 8, 8\n"
1817 " daddiu %2, %2, 1\n"
1818 " andi %1, %2, 0x7\n"
1820 " dext %1, %0, 0, 8\n"
1822 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1825 " .section .fixup,\"ax\"\n"
1829 " .section __ex_table,\"a\"\n"
1840 : "+&r"(rt), "=&r"(rs),
1841 "+&r"(vaddr), "+&r"(err)
1845 MIPS_R2_STATS(stores);
1849 if (IS_ENABLED(CONFIG_32BIT)) {
1854 rt = regs->regs[MIPSInst_RT(inst)];
1855 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1856 if (!access_ok((void __user *)vaddr, 8)) {
1857 current->thread.cp0_baduaddr = vaddr;
1861 __asm__ __volatile__(
1864 #ifdef CONFIG_CPU_LITTLE_ENDIAN
1865 " dext %1, %0, 0, 8\n"
1867 " daddiu %2, %2, 1\n"
1868 " andi %1, %2, 0x7\n"
1870 " dext %1, %0, 8, 8\n"
1872 " daddiu %2, %2, 1\n"
1873 " andi %1, %2, 0x7\n"
1875 " dext %1, %0, 16, 8\n"
1877 " daddiu %2, %2, 1\n"
1878 " andi %1, %2, 0x7\n"
1880 " dext %1, %0, 24, 8\n"
1882 " daddiu %2, %2, 1\n"
1883 " andi %1, %2, 0x7\n"
1885 " dextu %1, %0, 32, 8\n"
1887 " daddiu %2, %2, 1\n"
1888 " andi %1, %2, 0x7\n"
1890 " dextu %1, %0, 40, 8\n"
1892 " daddiu %2, %2, 1\n"
1893 " andi %1, %2, 0x7\n"
1895 " dextu %1, %0, 48, 8\n"
1897 " daddiu %2, %2, 1\n"
1898 " andi %1, %2, 0x7\n"
1900 " dextu %1, %0, 56, 8\n"
1902 #else /* !CONFIG_CPU_LITTLE_ENDIAN */
1903 " dext %1, %0, 0, 8\n"
1905 " andi %1, %2, 0x7\n"
1907 " daddiu %2, %2, -1\n"
1908 " dext %1, %0, 8, 8\n"
1910 " andi %1, %2, 0x7\n"
1912 " daddiu %2, %2, -1\n"
1913 " dext %1, %0, 16, 8\n"
1915 " andi %1, %2, 0x7\n"
1917 " daddiu %2, %2, -1\n"
1918 " dext %1, %0, 24, 8\n"
1920 " andi %1, %2, 0x7\n"
1922 " daddiu %2, %2, -1\n"
1923 " dextu %1, %0, 32, 8\n"
1925 " andi %1, %2, 0x7\n"
1927 " daddiu %2, %2, -1\n"
1928 " dextu %1, %0, 40, 8\n"
1930 " andi %1, %2, 0x7\n"
1932 " daddiu %2, %2, -1\n"
1933 " dextu %1, %0, 48, 8\n"
1935 " andi %1, %2, 0x7\n"
1937 " daddiu %2, %2, -1\n"
1938 " dextu %1, %0, 56, 8\n"
1940 #endif /* CONFIG_CPU_LITTLE_ENDIAN */
1943 " .section .fixup,\"ax\"\n"
1947 " .section __ex_table,\"a\"\n"
1958 : "+&r"(rt), "=&r"(rs),
1959 "+&r"(vaddr), "+&r"(err)
1963 MIPS_R2_STATS(stores);
1967 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
1969 current->thread.cp0_baduaddr = vaddr;
1973 if (!access_ok((void __user *)vaddr, 4)) {
1974 current->thread.cp0_baduaddr = vaddr;
1979 if (!cpu_has_rw_llb) {
1981 * An LL/SC block can't be safely emulated without
1982 * a Config5/LLB availability. So it's probably time to
1983 * kill our process before things get any worse. This is
1984 * because Config5/LLB allows us to use ERETNC so that
1985 * the LLAddr/LLB bit is not cleared when we return from
1986 * an exception. MIPS R2 LL/SC instructions trap with an
1987 * RI exception so once we emulate them here, we return
1988 * back to userland with ERETNC. That preserves the
1989 * LLAddr/LLB so the subsequent SC instruction will
1990 * succeed preserving the atomic semantics of the LL/SC
1991 * block. Without that, there is no safe way to emulate
1992 * an LL/SC block in MIPSR2 userland.
1994 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
1999 __asm__ __volatile__(
2004 ".section .fixup,\"ax\"\n"
2009 ".section __ex_table,\"a\"\n"
2012 : "=&r"(res), "+&r"(err)
2013 : "r"(vaddr), "i"(SIGSEGV)
2016 if (MIPSInst_RT(inst) && !err)
2017 regs->regs[MIPSInst_RT(inst)] = res;
2018 MIPS_R2_STATS(llsc);
2023 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2025 current->thread.cp0_baduaddr = vaddr;
2029 if (!access_ok((void __user *)vaddr, 4)) {
2030 current->thread.cp0_baduaddr = vaddr;
2035 if (!cpu_has_rw_llb) {
2037 * An LL/SC block can't be safely emulated without
2038 * a Config5/LLB availability. So it's probably time to
2039 * kill our process before things get any worse. This is
2040 * because Config5/LLB allows us to use ERETNC so that
2041 * the LLAddr/LLB bit is not cleared when we return from
2042 * an exception. MIPS R2 LL/SC instructions trap with an
2043 * RI exception so once we emulate them here, we return
2044 * back to userland with ERETNC. That preserves the
2045 * LLAddr/LLB so the subsequent SC instruction will
2046 * succeed preserving the atomic semantics of the LL/SC
2047 * block. Without that, there is no safe way to emulate
2048 * an LL/SC block in MIPSR2 userland.
2050 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2055 res = regs->regs[MIPSInst_RT(inst)];
2057 __asm__ __volatile__(
2062 ".section .fixup,\"ax\"\n"
2067 ".section __ex_table,\"a\"\n"
2070 : "+&r"(res), "+&r"(err)
2071 : "r"(vaddr), "i"(SIGSEGV));
2073 if (MIPSInst_RT(inst) && !err)
2074 regs->regs[MIPSInst_RT(inst)] = res;
2076 MIPS_R2_STATS(llsc);
2081 if (IS_ENABLED(CONFIG_32BIT)) {
2086 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2088 current->thread.cp0_baduaddr = vaddr;
2092 if (!access_ok((void __user *)vaddr, 8)) {
2093 current->thread.cp0_baduaddr = vaddr;
2098 if (!cpu_has_rw_llb) {
2100 * An LL/SC block can't be safely emulated without
2101 * a Config5/LLB availability. So it's probably time to
2102 * kill our process before things get any worse. This is
2103 * because Config5/LLB allows us to use ERETNC so that
2104 * the LLAddr/LLB bit is not cleared when we return from
2105 * an exception. MIPS R2 LL/SC instructions trap with an
2106 * RI exception so once we emulate them here, we return
2107 * back to userland with ERETNC. That preserves the
2108 * LLAddr/LLB so the subsequent SC instruction will
2109 * succeed preserving the atomic semantics of the LL/SC
2110 * block. Without that, there is no safe way to emulate
2111 * an LL/SC block in MIPSR2 userland.
2113 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2118 __asm__ __volatile__(
2123 ".section .fixup,\"ax\"\n"
2128 ".section __ex_table,\"a\"\n"
2131 : "=&r"(res), "+&r"(err)
2132 : "r"(vaddr), "i"(SIGSEGV)
2134 if (MIPSInst_RT(inst) && !err)
2135 regs->regs[MIPSInst_RT(inst)] = res;
2137 MIPS_R2_STATS(llsc);
2142 if (IS_ENABLED(CONFIG_32BIT)) {
2147 vaddr = regs->regs[MIPSInst_RS(inst)] + MIPSInst_SIMM(inst);
2149 current->thread.cp0_baduaddr = vaddr;
2153 if (!access_ok((void __user *)vaddr, 8)) {
2154 current->thread.cp0_baduaddr = vaddr;
2159 if (!cpu_has_rw_llb) {
2161 * An LL/SC block can't be safely emulated without
2162 * a Config5/LLB availability. So it's probably time to
2163 * kill our process before things get any worse. This is
2164 * because Config5/LLB allows us to use ERETNC so that
2165 * the LLAddr/LLB bit is not cleared when we return from
2166 * an exception. MIPS R2 LL/SC instructions trap with an
2167 * RI exception so once we emulate them here, we return
2168 * back to userland with ERETNC. That preserves the
2169 * LLAddr/LLB so the subsequent SC instruction will
2170 * succeed preserving the atomic semantics of the LL/SC
2171 * block. Without that, there is no safe way to emulate
2172 * an LL/SC block in MIPSR2 userland.
2174 pr_err("Can't emulate MIPSR2 LL/SC without Config5/LLB\n");
2179 res = regs->regs[MIPSInst_RT(inst)];
2181 __asm__ __volatile__(
2186 ".section .fixup,\"ax\"\n"
2191 ".section __ex_table,\"a\"\n"
2194 : "+&r"(res), "+&r"(err)
2195 : "r"(vaddr), "i"(SIGSEGV));
2197 if (MIPSInst_RT(inst) && !err)
2198 regs->regs[MIPSInst_RT(inst)] = res;
2200 MIPS_R2_STATS(llsc);
2211 * Let's not return to userland just yet. It's costly and
2212 * it's likely we have more R2 instructions to emulate
2214 if (!err && (pass++ < MIPS_R2_EMUL_TOTAL_PASS)) {
2215 regs->cp0_cause &= ~CAUSEF_BD;
2216 err = get_user(inst, (u32 __user *)regs->cp0_epc);
2224 if (err && (err != SIGEMT)) {
2225 regs->regs[31] = r31;
2226 regs->cp0_epc = epc;
2229 /* Likely a MIPS R6 compatible instruction */
2230 if (pass && (err == SIGILL))
2236 #ifdef CONFIG_DEBUG_FS
2238 static int mipsr2_emul_show(struct seq_file *s, void *unused)
2241 seq_printf(s, "Instruction\tTotal\tBDslot\n------------------------------\n");
2242 seq_printf(s, "movs\t\t%ld\t%ld\n",
2243 (unsigned long)__this_cpu_read(mipsr2emustats.movs),
2244 (unsigned long)__this_cpu_read(mipsr2bdemustats.movs));
2245 seq_printf(s, "hilo\t\t%ld\t%ld\n",
2246 (unsigned long)__this_cpu_read(mipsr2emustats.hilo),
2247 (unsigned long)__this_cpu_read(mipsr2bdemustats.hilo));
2248 seq_printf(s, "muls\t\t%ld\t%ld\n",
2249 (unsigned long)__this_cpu_read(mipsr2emustats.muls),
2250 (unsigned long)__this_cpu_read(mipsr2bdemustats.muls));
2251 seq_printf(s, "divs\t\t%ld\t%ld\n",
2252 (unsigned long)__this_cpu_read(mipsr2emustats.divs),
2253 (unsigned long)__this_cpu_read(mipsr2bdemustats.divs));
2254 seq_printf(s, "dsps\t\t%ld\t%ld\n",
2255 (unsigned long)__this_cpu_read(mipsr2emustats.dsps),
2256 (unsigned long)__this_cpu_read(mipsr2bdemustats.dsps));
2257 seq_printf(s, "bops\t\t%ld\t%ld\n",
2258 (unsigned long)__this_cpu_read(mipsr2emustats.bops),
2259 (unsigned long)__this_cpu_read(mipsr2bdemustats.bops));
2260 seq_printf(s, "traps\t\t%ld\t%ld\n",
2261 (unsigned long)__this_cpu_read(mipsr2emustats.traps),
2262 (unsigned long)__this_cpu_read(mipsr2bdemustats.traps));
2263 seq_printf(s, "fpus\t\t%ld\t%ld\n",
2264 (unsigned long)__this_cpu_read(mipsr2emustats.fpus),
2265 (unsigned long)__this_cpu_read(mipsr2bdemustats.fpus));
2266 seq_printf(s, "loads\t\t%ld\t%ld\n",
2267 (unsigned long)__this_cpu_read(mipsr2emustats.loads),
2268 (unsigned long)__this_cpu_read(mipsr2bdemustats.loads));
2269 seq_printf(s, "stores\t\t%ld\t%ld\n",
2270 (unsigned long)__this_cpu_read(mipsr2emustats.stores),
2271 (unsigned long)__this_cpu_read(mipsr2bdemustats.stores));
2272 seq_printf(s, "llsc\t\t%ld\t%ld\n",
2273 (unsigned long)__this_cpu_read(mipsr2emustats.llsc),
2274 (unsigned long)__this_cpu_read(mipsr2bdemustats.llsc));
2275 seq_printf(s, "dsemul\t\t%ld\t%ld\n",
2276 (unsigned long)__this_cpu_read(mipsr2emustats.dsemul),
2277 (unsigned long)__this_cpu_read(mipsr2bdemustats.dsemul));
2278 seq_printf(s, "jr\t\t%ld\n",
2279 (unsigned long)__this_cpu_read(mipsr2bremustats.jrs));
2280 seq_printf(s, "bltzl\t\t%ld\n",
2281 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzl));
2282 seq_printf(s, "bgezl\t\t%ld\n",
2283 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezl));
2284 seq_printf(s, "bltzll\t\t%ld\n",
2285 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzll));
2286 seq_printf(s, "bgezll\t\t%ld\n",
2287 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezll));
2288 seq_printf(s, "bltzal\t\t%ld\n",
2289 (unsigned long)__this_cpu_read(mipsr2bremustats.bltzal));
2290 seq_printf(s, "bgezal\t\t%ld\n",
2291 (unsigned long)__this_cpu_read(mipsr2bremustats.bgezal));
2292 seq_printf(s, "beql\t\t%ld\n",
2293 (unsigned long)__this_cpu_read(mipsr2bremustats.beql));
2294 seq_printf(s, "bnel\t\t%ld\n",
2295 (unsigned long)__this_cpu_read(mipsr2bremustats.bnel));
2296 seq_printf(s, "blezl\t\t%ld\n",
2297 (unsigned long)__this_cpu_read(mipsr2bremustats.blezl));
2298 seq_printf(s, "bgtzl\t\t%ld\n",
2299 (unsigned long)__this_cpu_read(mipsr2bremustats.bgtzl));
2304 static int mipsr2_clear_show(struct seq_file *s, void *unused)
2306 mipsr2_emul_show(s, unused);
2308 __this_cpu_write((mipsr2emustats).movs, 0);
2309 __this_cpu_write((mipsr2bdemustats).movs, 0);
2310 __this_cpu_write((mipsr2emustats).hilo, 0);
2311 __this_cpu_write((mipsr2bdemustats).hilo, 0);
2312 __this_cpu_write((mipsr2emustats).muls, 0);
2313 __this_cpu_write((mipsr2bdemustats).muls, 0);
2314 __this_cpu_write((mipsr2emustats).divs, 0);
2315 __this_cpu_write((mipsr2bdemustats).divs, 0);
2316 __this_cpu_write((mipsr2emustats).dsps, 0);
2317 __this_cpu_write((mipsr2bdemustats).dsps, 0);
2318 __this_cpu_write((mipsr2emustats).bops, 0);
2319 __this_cpu_write((mipsr2bdemustats).bops, 0);
2320 __this_cpu_write((mipsr2emustats).traps, 0);
2321 __this_cpu_write((mipsr2bdemustats).traps, 0);
2322 __this_cpu_write((mipsr2emustats).fpus, 0);
2323 __this_cpu_write((mipsr2bdemustats).fpus, 0);
2324 __this_cpu_write((mipsr2emustats).loads, 0);
2325 __this_cpu_write((mipsr2bdemustats).loads, 0);
2326 __this_cpu_write((mipsr2emustats).stores, 0);
2327 __this_cpu_write((mipsr2bdemustats).stores, 0);
2328 __this_cpu_write((mipsr2emustats).llsc, 0);
2329 __this_cpu_write((mipsr2bdemustats).llsc, 0);
2330 __this_cpu_write((mipsr2emustats).dsemul, 0);
2331 __this_cpu_write((mipsr2bdemustats).dsemul, 0);
2332 __this_cpu_write((mipsr2bremustats).jrs, 0);
2333 __this_cpu_write((mipsr2bremustats).bltzl, 0);
2334 __this_cpu_write((mipsr2bremustats).bgezl, 0);
2335 __this_cpu_write((mipsr2bremustats).bltzll, 0);
2336 __this_cpu_write((mipsr2bremustats).bgezll, 0);
2337 __this_cpu_write((mipsr2bremustats).bltzall, 0);
2338 __this_cpu_write((mipsr2bremustats).bgezall, 0);
2339 __this_cpu_write((mipsr2bremustats).bltzal, 0);
2340 __this_cpu_write((mipsr2bremustats).bgezal, 0);
2341 __this_cpu_write((mipsr2bremustats).beql, 0);
2342 __this_cpu_write((mipsr2bremustats).bnel, 0);
2343 __this_cpu_write((mipsr2bremustats).blezl, 0);
2344 __this_cpu_write((mipsr2bremustats).bgtzl, 0);
2349 DEFINE_SHOW_ATTRIBUTE(mipsr2_emul);
2350 DEFINE_SHOW_ATTRIBUTE(mipsr2_clear);
2352 static int __init mipsr2_init_debugfs(void)
2354 debugfs_create_file("r2_emul_stats", S_IRUGO, mips_debugfs_dir, NULL,
2356 debugfs_create_file("r2_emul_stats_clear", S_IRUGO, mips_debugfs_dir,
2357 NULL, &mipsr2_clear_fops);
2361 device_initcall(mipsr2_init_debugfs);
2363 #endif /* CONFIG_DEBUG_FS */