xref: /openbmc/linux/arch/mips/math-emu/dsemul.c (revision 733b8bc183f491e8263009edf8ef184fb44a6882)
11da177e4SLinus Torvalds #include <asm/branch.h>
21da177e4SLinus Torvalds #include <asm/cacheflush.h>
31da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
4cd8ee345SRalf Baechle #include <asm/inst.h>
5cd8ee345SRalf Baechle #include <asm/mipsregs.h>
6cd8ee345SRalf Baechle #include <asm/uaccess.h>
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include "ieee754.h"
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  * Emulate the arbritrary instruction ir at xcp->cp0_epc.  Required when
121da177e4SLinus Torvalds  * we have to emulate the instruction in a COP1 branch delay slot.  Do
131da177e4SLinus Torvalds  * not change cp0_epc due to the instruction
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * According to the spec:
1625985edcSLucas De Marchi  * 1) it shouldn't be a branch :-)
171da177e4SLinus Torvalds  * 2) it can be a COP instruction :-(
181da177e4SLinus Torvalds  * 3) if we are tring to run a protected memory space we must take
191da177e4SLinus Torvalds  *    special care on memory access instructions :-(
201da177e4SLinus Torvalds  */
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds /*
231da177e4SLinus Torvalds  * "Trampoline" return routine to catch exception following
241da177e4SLinus Torvalds  *  execution of delay-slot instruction execution.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds struct emuframe {
281da177e4SLinus Torvalds 	mips_instruction	emul;
291da177e4SLinus Torvalds 	mips_instruction	badinst;
301da177e4SLinus Torvalds 	mips_instruction	cookie;
31333d1f67SRalf Baechle 	unsigned long		epc;
321da177e4SLinus Torvalds };
331da177e4SLinus Torvalds 
34e4553573SMaciej W. Rozycki /*
35e4553573SMaciej W. Rozycki  * Set up an emulation frame for instruction IR, from a delay slot of
36e4553573SMaciej W. Rozycki  * a branch jumping to CPC.  Return 0 if successful, -1 if no emulation
37e4553573SMaciej W. Rozycki  * required, otherwise a signal number causing a frame setup failure.
38e4553573SMaciej W. Rozycki  */
39333d1f67SRalf Baechle int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc)
401da177e4SLinus Torvalds {
41*733b8bc1SMaciej W. Rozycki 	mips_instruction break_math;
425e0373b8SAtsushi Nemoto 	struct emuframe __user *fr;
431da177e4SLinus Torvalds 	int err;
441da177e4SLinus Torvalds 
45102cedc3SLeonid Yegoshin 	/* NOP is easy */
46e4553573SMaciej W. Rozycki 	if ((get_isa16_mode(regs->cp0_epc) && ((ir >> 16) == MM_NOP16)) ||
47e4553573SMaciej W. Rozycki 	    (ir == 0))
48e4553573SMaciej W. Rozycki 		return -1;
491da177e4SLinus Torvalds 
5092df0f8bSRalf Baechle 	pr_debug("dsemul %lx %lx\n", regs->cp0_epc, cpc);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	/*
531da177e4SLinus Torvalds 	 * The strategy is to push the instruction onto the user stack
541da177e4SLinus Torvalds 	 * and put a trap after it which we can catch and jump to
551da177e4SLinus Torvalds 	 * the required address any alternative apart from full
561da177e4SLinus Torvalds 	 * instruction emulation!!.
571da177e4SLinus Torvalds 	 *
581da177e4SLinus Torvalds 	 * Algorithmics used a system call instruction, and
591da177e4SLinus Torvalds 	 * borrowed that vector.  MIPS/Linux version is a bit
601da177e4SLinus Torvalds 	 * more heavyweight in the interests of portability and
611da177e4SLinus Torvalds 	 * multiprocessor support.  For Linux we generate a
621da177e4SLinus Torvalds 	 * an unaligned access and force an address error exception.
631da177e4SLinus Torvalds 	 *
641da177e4SLinus Torvalds 	 * For embedded systems (stand-alone) we prefer to use a
651da177e4SLinus Torvalds 	 * non-existing CP1 instruction. This prevents us from emulating
661da177e4SLinus Torvalds 	 * branches, but gives us a cleaner interface to the exception
671da177e4SLinus Torvalds 	 * handler (single entry point).
681da177e4SLinus Torvalds 	 */
69*733b8bc1SMaciej W. Rozycki 	break_math = BREAK_MATH(get_isa16_mode(regs->cp0_epc));
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	/* Ensure that the two instructions are in the same cache line */
725e0373b8SAtsushi Nemoto 	fr = (struct emuframe __user *)
735e0373b8SAtsushi Nemoto 		((regs->regs[29] - sizeof(struct emuframe)) & ~0x7);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	/* Verify that the stack pointer is not competely insane */
761da177e4SLinus Torvalds 	if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe))))
771da177e4SLinus Torvalds 		return SIGBUS;
781da177e4SLinus Torvalds 
79102cedc3SLeonid Yegoshin 	if (get_isa16_mode(regs->cp0_epc)) {
80a87265cfSMaciej W. Rozycki 		err = __put_user(ir >> 16,
81a87265cfSMaciej W. Rozycki 				 (u16 __user *)(&fr->emul));
82a87265cfSMaciej W. Rozycki 		err |= __put_user(ir & 0xffff,
83a87265cfSMaciej W. Rozycki 				  (u16 __user *)((long)(&fr->emul) + 2));
84*733b8bc1SMaciej W. Rozycki 		err |= __put_user(break_math >> 16,
85a87265cfSMaciej W. Rozycki 				  (u16 __user *)(&fr->badinst));
86*733b8bc1SMaciej W. Rozycki 		err |= __put_user(break_math & 0xffff,
87a87265cfSMaciej W. Rozycki 				  (u16 __user *)((long)(&fr->badinst) + 2));
88102cedc3SLeonid Yegoshin 	} else {
891da177e4SLinus Torvalds 		err = __put_user(ir, &fr->emul);
90*733b8bc1SMaciej W. Rozycki 		err |= __put_user(break_math, &fr->badinst);
91102cedc3SLeonid Yegoshin 	}
92102cedc3SLeonid Yegoshin 
931da177e4SLinus Torvalds 	err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie);
941da177e4SLinus Torvalds 	err |= __put_user(cpc, &fr->epc);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	if (unlikely(err)) {
97b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(errors);
981da177e4SLinus Torvalds 		return SIGBUS;
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds 
101102cedc3SLeonid Yegoshin 	regs->cp0_epc = ((unsigned long) &fr->emul) |
102102cedc3SLeonid Yegoshin 		get_isa16_mode(regs->cp0_epc);
1031da177e4SLinus Torvalds 
1047737b20bSMaciej W. Rozycki 	flush_cache_sigtramp((unsigned long)&fr->emul);
1051da177e4SLinus Torvalds 
1069ab4471cSMaciej W. Rozycki 	return 0;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds int do_dsemulret(struct pt_regs *xcp)
1101da177e4SLinus Torvalds {
1115e0373b8SAtsushi Nemoto 	struct emuframe __user *fr;
112333d1f67SRalf Baechle 	unsigned long epc;
1131da177e4SLinus Torvalds 	u32 insn, cookie;
1141da177e4SLinus Torvalds 	int err = 0;
115102cedc3SLeonid Yegoshin 	u16 instr[2];
1161da177e4SLinus Torvalds 
1175e0373b8SAtsushi Nemoto 	fr = (struct emuframe __user *)
118102cedc3SLeonid Yegoshin 		(msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction));
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	/*
1211da177e4SLinus Torvalds 	 * If we can't even access the area, something is very wrong, but we'll
1221da177e4SLinus Torvalds 	 * leave that to the default handling
1231da177e4SLinus Torvalds 	 */
1241da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe)))
1251da177e4SLinus Torvalds 		return 0;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/*
1281da177e4SLinus Torvalds 	 * Do some sanity checking on the stackframe:
1291da177e4SLinus Torvalds 	 *
130ba3049edSRalf Baechle 	 *  - Is the instruction pointed to by the EPC an BREAK_MATH?
1311da177e4SLinus Torvalds 	 *  - Is the following memory word the BD_COOKIE?
1321da177e4SLinus Torvalds 	 */
133102cedc3SLeonid Yegoshin 	if (get_isa16_mode(xcp->cp0_epc)) {
134a87265cfSMaciej W. Rozycki 		err = __get_user(instr[0],
135a87265cfSMaciej W. Rozycki 				 (u16 __user *)(&fr->badinst));
136a87265cfSMaciej W. Rozycki 		err |= __get_user(instr[1],
137a87265cfSMaciej W. Rozycki 				  (u16 __user *)((long)(&fr->badinst) + 2));
138102cedc3SLeonid Yegoshin 		insn = (instr[0] << 16) | instr[1];
139102cedc3SLeonid Yegoshin 	} else {
1401da177e4SLinus Torvalds 		err = __get_user(insn, &fr->badinst);
141102cedc3SLeonid Yegoshin 	}
1421da177e4SLinus Torvalds 	err |= __get_user(cookie, &fr->cookie);
1431da177e4SLinus Torvalds 
144*733b8bc1SMaciej W. Rozycki 	if (unlikely(err || insn != BREAK_MATH(get_isa16_mode(xcp->cp0_epc)) ||
145*733b8bc1SMaciej W. Rozycki 		     cookie != BD_COOKIE)) {
146b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(errors);
1471da177e4SLinus Torvalds 		return 0;
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds 	/*
1511da177e4SLinus Torvalds 	 * At this point, we are satisfied that it's a BD emulation trap.  Yes,
1521da177e4SLinus Torvalds 	 * a user might have deliberately put two malformed and useless
1531da177e4SLinus Torvalds 	 * instructions in a row in his program, in which case he's in for a
1541da177e4SLinus Torvalds 	 * nasty surprise - the next instruction will be treated as a
1551da177e4SLinus Torvalds 	 * continuation address!  Alas, this seems to be the only way that we
1561da177e4SLinus Torvalds 	 * can handle signals, recursion, and longjmps() in the context of
1571da177e4SLinus Torvalds 	 * emulating the branch delay instruction.
1581da177e4SLinus Torvalds 	 */
1591da177e4SLinus Torvalds 
16092df0f8bSRalf Baechle 	pr_debug("dsemulret\n");
16192df0f8bSRalf Baechle 
1621da177e4SLinus Torvalds 	if (__get_user(epc, &fr->epc)) {		/* Saved EPC */
1631da177e4SLinus Torvalds 		/* This is not a good situation to be in */
1641da177e4SLinus Torvalds 		force_sig(SIGBUS, current);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 		return 0;
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	/* Set EPC to return to post-branch instruction */
1701da177e4SLinus Torvalds 	xcp->cp0_epc = epc;
1712707cd29SDavid Daney 	MIPS_FPU_EMU_INC_STATS(ds_emul);
1721da177e4SLinus Torvalds 	return 1;
1731da177e4SLinus Torvalds }
174