MIPS: math-emu: Correct the emulation of microMIPS ADDIUPC instruction
[linux-drm-fsl-dcu.git] / arch / mips / math-emu / dsemul.c
1 #include <asm/branch.h>
2 #include <asm/cacheflush.h>
3 #include <asm/fpu_emulator.h>
4 #include <asm/inst.h>
5 #include <asm/mipsregs.h>
6 #include <asm/uaccess.h>
7
8 #include "ieee754.h"
9
10 /*
11  * Emulate the arbritrary instruction ir at xcp->cp0_epc.  Required when
12  * we have to emulate the instruction in a COP1 branch delay slot.  Do
13  * not change cp0_epc due to the instruction
14  *
15  * According to the spec:
16  * 1) it shouldn't be a branch :-)
17  * 2) it can be a COP instruction :-(
18  * 3) if we are tring to run a protected memory space we must take
19  *    special care on memory access instructions :-(
20  */
21
22 /*
23  * "Trampoline" return routine to catch exception following
24  *  execution of delay-slot instruction execution.
25  */
26
27 struct emuframe {
28         mips_instruction        emul;
29         mips_instruction        badinst;
30         mips_instruction        cookie;
31         unsigned long           epc;
32 };
33
34 /*
35  * Set up an emulation frame for instruction IR, from a delay slot of
36  * a branch jumping to CPC.  Return 0 if successful, -1 if no emulation
37  * required, otherwise a signal number causing a frame setup failure.
38  */
39 int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc)
40 {
41         mips_instruction break_math;
42         struct emuframe __user *fr;
43         int err;
44
45         /* NOP is easy */
46         if (ir == 0)
47                 return -1;
48
49         /* microMIPS instructions */
50         if (get_isa16_mode(regs->cp0_epc)) {
51                 union mips_instruction insn = { .word = ir };
52
53                 /* NOP16 aka MOVE16 $0, $0 */
54                 if ((ir >> 16) == MM_NOP16)
55                         return -1;
56
57                 /* ADDIUPC */
58                 if (insn.mm_a_format.opcode == mm_addiupc_op) {
59                         unsigned int rs;
60                         s32 v;
61
62                         rs = (((insn.mm_a_format.rs + 0x1e) & 0xf) + 2);
63                         v = regs->cp0_epc & ~3;
64                         v += insn.mm_a_format.simmediate << 2;
65                         regs->regs[rs] = (long)v;
66                         return -1;
67                 }
68         }
69
70         pr_debug("dsemul %lx %lx\n", regs->cp0_epc, cpc);
71
72         /*
73          * The strategy is to push the instruction onto the user stack
74          * and put a trap after it which we can catch and jump to
75          * the required address any alternative apart from full
76          * instruction emulation!!.
77          *
78          * Algorithmics used a system call instruction, and
79          * borrowed that vector.  MIPS/Linux version is a bit
80          * more heavyweight in the interests of portability and
81          * multiprocessor support.  For Linux we generate a
82          * an unaligned access and force an address error exception.
83          *
84          * For embedded systems (stand-alone) we prefer to use a
85          * non-existing CP1 instruction. This prevents us from emulating
86          * branches, but gives us a cleaner interface to the exception
87          * handler (single entry point).
88          */
89         break_math = BREAK_MATH(get_isa16_mode(regs->cp0_epc));
90
91         /* Ensure that the two instructions are in the same cache line */
92         fr = (struct emuframe __user *)
93                 ((regs->regs[29] - sizeof(struct emuframe)) & ~0x7);
94
95         /* Verify that the stack pointer is not competely insane */
96         if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe))))
97                 return SIGBUS;
98
99         if (get_isa16_mode(regs->cp0_epc)) {
100                 err = __put_user(ir >> 16,
101                                  (u16 __user *)(&fr->emul));
102                 err |= __put_user(ir & 0xffff,
103                                   (u16 __user *)((long)(&fr->emul) + 2));
104                 err |= __put_user(break_math >> 16,
105                                   (u16 __user *)(&fr->badinst));
106                 err |= __put_user(break_math & 0xffff,
107                                   (u16 __user *)((long)(&fr->badinst) + 2));
108         } else {
109                 err = __put_user(ir, &fr->emul);
110                 err |= __put_user(break_math, &fr->badinst);
111         }
112
113         err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie);
114         err |= __put_user(cpc, &fr->epc);
115
116         if (unlikely(err)) {
117                 MIPS_FPU_EMU_INC_STATS(errors);
118                 return SIGBUS;
119         }
120
121         regs->cp0_epc = ((unsigned long) &fr->emul) |
122                 get_isa16_mode(regs->cp0_epc);
123
124         flush_cache_sigtramp((unsigned long)&fr->emul);
125
126         return 0;
127 }
128
129 int do_dsemulret(struct pt_regs *xcp)
130 {
131         struct emuframe __user *fr;
132         unsigned long epc;
133         u32 insn, cookie;
134         int err = 0;
135         u16 instr[2];
136
137         fr = (struct emuframe __user *)
138                 (msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction));
139
140         /*
141          * If we can't even access the area, something is very wrong, but we'll
142          * leave that to the default handling
143          */
144         if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe)))
145                 return 0;
146
147         /*
148          * Do some sanity checking on the stackframe:
149          *
150          *  - Is the instruction pointed to by the EPC an BREAK_MATH?
151          *  - Is the following memory word the BD_COOKIE?
152          */
153         if (get_isa16_mode(xcp->cp0_epc)) {
154                 err = __get_user(instr[0],
155                                  (u16 __user *)(&fr->badinst));
156                 err |= __get_user(instr[1],
157                                   (u16 __user *)((long)(&fr->badinst) + 2));
158                 insn = (instr[0] << 16) | instr[1];
159         } else {
160                 err = __get_user(insn, &fr->badinst);
161         }
162         err |= __get_user(cookie, &fr->cookie);
163
164         if (unlikely(err || insn != BREAK_MATH(get_isa16_mode(xcp->cp0_epc)) ||
165                      cookie != BD_COOKIE)) {
166                 MIPS_FPU_EMU_INC_STATS(errors);
167                 return 0;
168         }
169
170         /*
171          * At this point, we are satisfied that it's a BD emulation trap.  Yes,
172          * a user might have deliberately put two malformed and useless
173          * instructions in a row in his program, in which case he's in for a
174          * nasty surprise - the next instruction will be treated as a
175          * continuation address!  Alas, this seems to be the only way that we
176          * can handle signals, recursion, and longjmps() in the context of
177          * emulating the branch delay instruction.
178          */
179
180         pr_debug("dsemulret\n");
181
182         if (__get_user(epc, &fr->epc)) {                /* Saved EPC */
183                 /* This is not a good situation to be in */
184                 force_sig(SIGBUS, current);
185
186                 return 0;
187         }
188
189         /* Set EPC to return to post-branch instruction */
190         xcp->cp0_epc = epc;
191         MIPS_FPU_EMU_INC_STATS(ds_emul);
192         return 1;
193 }