dsemul.c 5.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
#include <asm/branch.h>
#include <asm/cacheflush.h>
#include <asm/fpu_emulator.h>
4 5 6
#include <asm/inst.h>
#include <asm/mipsregs.h>
#include <asm/uaccess.h>
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15

#include "ieee754.h"

/*
 * Emulate the arbritrary instruction ir at xcp->cp0_epc.  Required when
 * we have to emulate the instruction in a COP1 branch delay slot.  Do
 * not change cp0_epc due to the instruction
 *
 * According to the spec:
L
Lucas De Marchi 已提交
16
 * 1) it shouldn't be a branch :-)
L
Linus Torvalds 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * 2) it can be a COP instruction :-(
 * 3) if we are tring to run a protected memory space we must take
 *    special care on memory access instructions :-(
 */

/*
 * "Trampoline" return routine to catch exception following
 *  execution of delay-slot instruction execution.
 */

struct emuframe {
	mips_instruction	emul;
	mips_instruction	badinst;
	mips_instruction	cookie;
31
	unsigned long		epc;
L
Linus Torvalds 已提交
32 33
};

34 35 36 37 38
/*
 * Set up an emulation frame for instruction IR, from a delay slot of
 * a branch jumping to CPC.  Return 0 if successful, -1 if no emulation
 * required, otherwise a signal number causing a frame setup failure.
 */
39
int mips_dsemul(struct pt_regs *regs, mips_instruction ir, unsigned long cpc)
L
Linus Torvalds 已提交
40
{
41
	int isa16 = get_isa16_mode(regs->cp0_epc);
42
	mips_instruction break_math;
A
Atsushi Nemoto 已提交
43
	struct emuframe __user *fr;
L
Linus Torvalds 已提交
44 45
	int err;

46
	/* NOP is easy */
47
	if (ir == 0)
48
		return -1;
L
Linus Torvalds 已提交
49

50
	/* microMIPS instructions */
51
	if (isa16) {
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
		union mips_instruction insn = { .word = ir };

		/* NOP16 aka MOVE16 $0, $0 */
		if ((ir >> 16) == MM_NOP16)
			return -1;

		/* ADDIUPC */
		if (insn.mm_a_format.opcode == mm_addiupc_op) {
			unsigned int rs;
			s32 v;

			rs = (((insn.mm_a_format.rs + 0x1e) & 0xf) + 2);
			v = regs->cp0_epc & ~3;
			v += insn.mm_a_format.simmediate << 2;
			regs->regs[rs] = (long)v;
			return -1;
		}
	}

71
	pr_debug("dsemul %lx %lx\n", regs->cp0_epc, cpc);
L
Linus Torvalds 已提交
72 73 74 75 76 77 78 79 80 81

	/*
	 * The strategy is to push the instruction onto the user stack
	 * and put a trap after it which we can catch and jump to
	 * the required address any alternative apart from full
	 * instruction emulation!!.
	 *
	 * Algorithmics used a system call instruction, and
	 * borrowed that vector.  MIPS/Linux version is a bit
	 * more heavyweight in the interests of portability and
82 83
	 * multiprocessor support.  For Linux we use a BREAK 514
	 * instruction causing a breakpoint exception.
L
Linus Torvalds 已提交
84
	 */
85
	break_math = BREAK_MATH(isa16);
L
Linus Torvalds 已提交
86 87

	/* Ensure that the two instructions are in the same cache line */
A
Atsushi Nemoto 已提交
88 89
	fr = (struct emuframe __user *)
		((regs->regs[29] - sizeof(struct emuframe)) & ~0x7);
L
Linus Torvalds 已提交
90 91 92 93 94

	/* Verify that the stack pointer is not competely insane */
	if (unlikely(!access_ok(VERIFY_WRITE, fr, sizeof(struct emuframe))))
		return SIGBUS;

95
	if (isa16) {
96 97 98 99
		err = __put_user(ir >> 16,
				 (u16 __user *)(&fr->emul));
		err |= __put_user(ir & 0xffff,
				  (u16 __user *)((long)(&fr->emul) + 2));
100
		err |= __put_user(break_math >> 16,
101
				  (u16 __user *)(&fr->badinst));
102
		err |= __put_user(break_math & 0xffff,
103
				  (u16 __user *)((long)(&fr->badinst) + 2));
104 105
	} else {
		err = __put_user(ir, &fr->emul);
106
		err |= __put_user(break_math, &fr->badinst);
107 108
	}

L
Linus Torvalds 已提交
109 110 111 112
	err |= __put_user((mips_instruction)BD_COOKIE, &fr->cookie);
	err |= __put_user(cpc, &fr->epc);

	if (unlikely(err)) {
113
		MIPS_FPU_EMU_INC_STATS(errors);
L
Linus Torvalds 已提交
114 115 116
		return SIGBUS;
	}

117
	regs->cp0_epc = (unsigned long)&fr->emul | isa16;
L
Linus Torvalds 已提交
118

119
	flush_cache_sigtramp((unsigned long)&fr->emul);
L
Linus Torvalds 已提交
120

121
	return 0;
L
Linus Torvalds 已提交
122 123 124 125
}

int do_dsemulret(struct pt_regs *xcp)
{
126
	int isa16 = get_isa16_mode(xcp->cp0_epc);
A
Atsushi Nemoto 已提交
127
	struct emuframe __user *fr;
128
	unsigned long epc;
L
Linus Torvalds 已提交
129 130
	u32 insn, cookie;
	int err = 0;
131
	u16 instr[2];
L
Linus Torvalds 已提交
132

A
Atsushi Nemoto 已提交
133
	fr = (struct emuframe __user *)
134
		(msk_isa16_mode(xcp->cp0_epc) - sizeof(mips_instruction));
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145

	/*
	 * If we can't even access the area, something is very wrong, but we'll
	 * leave that to the default handling
	 */
	if (!access_ok(VERIFY_READ, fr, sizeof(struct emuframe)))
		return 0;

	/*
	 * Do some sanity checking on the stackframe:
	 *
146
	 *  - Is the instruction pointed to by the EPC an BREAK_MATH?
L
Linus Torvalds 已提交
147 148
	 *  - Is the following memory word the BD_COOKIE?
	 */
149
	if (isa16) {
150 151 152 153
		err = __get_user(instr[0],
				 (u16 __user *)(&fr->badinst));
		err |= __get_user(instr[1],
				  (u16 __user *)((long)(&fr->badinst) + 2));
154 155 156 157
		insn = (instr[0] << 16) | instr[1];
	} else {
		err = __get_user(insn, &fr->badinst);
	}
L
Linus Torvalds 已提交
158 159
	err |= __get_user(cookie, &fr->cookie);

160 161
	if (unlikely(err ||
		     insn != BREAK_MATH(isa16) || cookie != BD_COOKIE)) {
162
		MIPS_FPU_EMU_INC_STATS(errors);
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175
		return 0;
	}

	/*
	 * At this point, we are satisfied that it's a BD emulation trap.  Yes,
	 * a user might have deliberately put two malformed and useless
	 * instructions in a row in his program, in which case he's in for a
	 * nasty surprise - the next instruction will be treated as a
	 * continuation address!  Alas, this seems to be the only way that we
	 * can handle signals, recursion, and longjmps() in the context of
	 * emulating the branch delay instruction.
	 */

176 177
	pr_debug("dsemulret\n");

L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186
	if (__get_user(epc, &fr->epc)) {		/* Saved EPC */
		/* This is not a good situation to be in */
		force_sig(SIGBUS, current);

		return 0;
	}

	/* Set EPC to return to post-branch instruction */
	xcp->cp0_epc = epc;
187
	MIPS_FPU_EMU_INC_STATS(ds_emul);
L
Linus Torvalds 已提交
188 189
	return 1;
}