xref: /openbmc/linux/arch/mips/math-emu/dsemul.c (revision 432c6bacbd0c16ec210c43da411ccc3855c4c010)
1*432c6bacSPaul Burton #include <linux/err.h>
2*432c6bacSPaul Burton #include <linux/slab.h>
3*432c6bacSPaul Burton 
41da177e4SLinus Torvalds #include <asm/branch.h>
51da177e4SLinus Torvalds #include <asm/cacheflush.h>
61da177e4SLinus Torvalds #include <asm/fpu_emulator.h>
7cd8ee345SRalf Baechle #include <asm/inst.h>
8cd8ee345SRalf Baechle #include <asm/mipsregs.h>
9cd8ee345SRalf Baechle #include <asm/uaccess.h>
101da177e4SLinus Torvalds 
11*432c6bacSPaul Burton /**
12*432c6bacSPaul Burton  * struct emuframe - The 'emulation' frame structure
13*432c6bacSPaul Burton  * @emul:	The instruction to 'emulate'.
14*432c6bacSPaul Burton  * @badinst:	A break instruction to cause a return to the kernel.
151da177e4SLinus Torvalds  *
16*432c6bacSPaul Burton  * This structure defines the frames placed within the delay slot emulation
17*432c6bacSPaul Burton  * page in response to a call to mips_dsemul(). Each thread may be allocated
18*432c6bacSPaul Burton  * only one frame at any given time. The kernel stores within it the
19*432c6bacSPaul Burton  * instruction to be 'emulated' followed by a break instruction, then
20*432c6bacSPaul Burton  * executes the frame in user mode. The break causes a trap to the kernel
21*432c6bacSPaul Burton  * which leads to do_dsemulret() being called unless the instruction in
22*432c6bacSPaul Burton  * @emul causes a trap itself, is a branch, or a signal is delivered to
23*432c6bacSPaul Burton  * the thread. In these cases the allocated frame will either be reused by
24*432c6bacSPaul Burton  * a subsequent delay slot 'emulation', or be freed during signal delivery or
25*432c6bacSPaul Burton  * upon thread exit.
26*432c6bacSPaul Burton  *
27*432c6bacSPaul Burton  * This approach is used because:
28*432c6bacSPaul Burton  *
29*432c6bacSPaul Burton  * - Actually emulating all instructions isn't feasible. We would need to
30*432c6bacSPaul Burton  *   be able to handle instructions from all revisions of the MIPS ISA,
31*432c6bacSPaul Burton  *   all ASEs & all vendor instruction set extensions. This would be a
32*432c6bacSPaul Burton  *   whole lot of work & continual maintenance burden as new instructions
33*432c6bacSPaul Burton  *   are introduced, and in the case of some vendor extensions may not
34*432c6bacSPaul Burton  *   even be possible. Thus we need to take the approach of actually
35*432c6bacSPaul Burton  *   executing the instruction.
36*432c6bacSPaul Burton  *
37*432c6bacSPaul Burton  * - We must execute the instruction within user context. If we were to
38*432c6bacSPaul Burton  *   execute the instruction in kernel mode then it would have access to
39*432c6bacSPaul Burton  *   kernel resources without very careful checks, leaving us with a
40*432c6bacSPaul Burton  *   high potential for security or stability issues to arise.
41*432c6bacSPaul Burton  *
42*432c6bacSPaul Burton  * - We used to place the frame on the users stack, but this requires
43*432c6bacSPaul Burton  *   that the stack be executable. This is bad for security so the
44*432c6bacSPaul Burton  *   per-process page is now used instead.
45*432c6bacSPaul Burton  *
46*432c6bacSPaul Burton  * - The instruction in @emul may be something entirely invalid for a
47*432c6bacSPaul Burton  *   delay slot. The user may (intentionally or otherwise) place a branch
48*432c6bacSPaul Burton  *   in a delay slot, or a kernel mode instruction, or something else
49*432c6bacSPaul Burton  *   which generates an exception. Thus we can't rely upon the break in
50*432c6bacSPaul Burton  *   @badinst always being hit. For this reason we track the index of the
51*432c6bacSPaul Burton  *   frame allocated to each thread, allowing us to clean it up at later
52*432c6bacSPaul Burton  *   points such as signal delivery or thread exit.
53*432c6bacSPaul Burton  *
54*432c6bacSPaul Burton  * - The user may generate a fake struct emuframe if they wish, invoking
55*432c6bacSPaul Burton  *   the BRK_MEMU break instruction themselves. We must therefore not
56*432c6bacSPaul Burton  *   trust that BRK_MEMU means there's actually a valid frame allocated
57*432c6bacSPaul Burton  *   to the thread, and must not allow the user to do anything they
58*432c6bacSPaul Burton  *   couldn't already.
591da177e4SLinus Torvalds  */
601da177e4SLinus Torvalds struct emuframe {
611da177e4SLinus Torvalds 	mips_instruction	emul;
621da177e4SLinus Torvalds 	mips_instruction	badinst;
631da177e4SLinus Torvalds };
641da177e4SLinus Torvalds 
65*432c6bacSPaul Burton static const int emupage_frame_count = PAGE_SIZE / sizeof(struct emuframe);
66*432c6bacSPaul Burton 
67*432c6bacSPaul Burton static inline __user struct emuframe *dsemul_page(void)
68*432c6bacSPaul Burton {
69*432c6bacSPaul Burton 	return (__user struct emuframe *)STACK_TOP;
70*432c6bacSPaul Burton }
71*432c6bacSPaul Burton 
72*432c6bacSPaul Burton static int alloc_emuframe(void)
73*432c6bacSPaul Burton {
74*432c6bacSPaul Burton 	mm_context_t *mm_ctx = &current->mm->context;
75*432c6bacSPaul Burton 	int idx;
76*432c6bacSPaul Burton 
77*432c6bacSPaul Burton retry:
78*432c6bacSPaul Burton 	spin_lock(&mm_ctx->bd_emupage_lock);
79*432c6bacSPaul Burton 
80*432c6bacSPaul Burton 	/* Ensure we have an allocation bitmap */
81*432c6bacSPaul Burton 	if (!mm_ctx->bd_emupage_allocmap) {
82*432c6bacSPaul Burton 		mm_ctx->bd_emupage_allocmap =
83*432c6bacSPaul Burton 			kcalloc(BITS_TO_LONGS(emupage_frame_count),
84*432c6bacSPaul Burton 					      sizeof(unsigned long),
85*432c6bacSPaul Burton 				GFP_ATOMIC);
86*432c6bacSPaul Burton 
87*432c6bacSPaul Burton 		if (!mm_ctx->bd_emupage_allocmap) {
88*432c6bacSPaul Burton 			idx = BD_EMUFRAME_NONE;
89*432c6bacSPaul Burton 			goto out_unlock;
90*432c6bacSPaul Burton 		}
91*432c6bacSPaul Burton 	}
92*432c6bacSPaul Burton 
93*432c6bacSPaul Burton 	/* Attempt to allocate a single bit/frame */
94*432c6bacSPaul Burton 	idx = bitmap_find_free_region(mm_ctx->bd_emupage_allocmap,
95*432c6bacSPaul Burton 				      emupage_frame_count, 0);
96*432c6bacSPaul Burton 	if (idx < 0) {
97e4553573SMaciej W. Rozycki 		/*
98*432c6bacSPaul Burton 		 * Failed to allocate a frame. We'll wait until one becomes
99*432c6bacSPaul Burton 		 * available. We unlock the page so that other threads actually
100*432c6bacSPaul Burton 		 * get the opportunity to free their frames, which means
101*432c6bacSPaul Burton 		 * technically the result of bitmap_full may be incorrect.
102*432c6bacSPaul Burton 		 * However the worst case is that we repeat all this and end up
103*432c6bacSPaul Burton 		 * back here again.
104e4553573SMaciej W. Rozycki 		 */
105*432c6bacSPaul Burton 		spin_unlock(&mm_ctx->bd_emupage_lock);
106*432c6bacSPaul Burton 		if (!wait_event_killable(mm_ctx->bd_emupage_queue,
107*432c6bacSPaul Burton 			!bitmap_full(mm_ctx->bd_emupage_allocmap,
108*432c6bacSPaul Burton 				     emupage_frame_count)))
109*432c6bacSPaul Burton 			goto retry;
110*432c6bacSPaul Burton 
111*432c6bacSPaul Burton 		/* Received a fatal signal - just give in */
112*432c6bacSPaul Burton 		return BD_EMUFRAME_NONE;
113*432c6bacSPaul Burton 	}
114*432c6bacSPaul Burton 
115*432c6bacSPaul Burton 	/* Success! */
116*432c6bacSPaul Burton 	pr_debug("allocate emuframe %d to %d\n", idx, current->pid);
117*432c6bacSPaul Burton out_unlock:
118*432c6bacSPaul Burton 	spin_unlock(&mm_ctx->bd_emupage_lock);
119*432c6bacSPaul Burton 	return idx;
120*432c6bacSPaul Burton }
121*432c6bacSPaul Burton 
122*432c6bacSPaul Burton static void free_emuframe(int idx, struct mm_struct *mm)
123*432c6bacSPaul Burton {
124*432c6bacSPaul Burton 	mm_context_t *mm_ctx = &mm->context;
125*432c6bacSPaul Burton 
126*432c6bacSPaul Burton 	spin_lock(&mm_ctx->bd_emupage_lock);
127*432c6bacSPaul Burton 
128*432c6bacSPaul Burton 	pr_debug("free emuframe %d from %d\n", idx, current->pid);
129*432c6bacSPaul Burton 	bitmap_clear(mm_ctx->bd_emupage_allocmap, idx, 1);
130*432c6bacSPaul Burton 
131*432c6bacSPaul Burton 	/* If some thread is waiting for a frame, now's its chance */
132*432c6bacSPaul Burton 	wake_up(&mm_ctx->bd_emupage_queue);
133*432c6bacSPaul Burton 
134*432c6bacSPaul Burton 	spin_unlock(&mm_ctx->bd_emupage_lock);
135*432c6bacSPaul Burton }
136*432c6bacSPaul Burton 
137*432c6bacSPaul Burton static bool within_emuframe(struct pt_regs *regs)
138*432c6bacSPaul Burton {
139*432c6bacSPaul Burton 	unsigned long base = (unsigned long)dsemul_page();
140*432c6bacSPaul Burton 
141*432c6bacSPaul Burton 	if (regs->cp0_epc < base)
142*432c6bacSPaul Burton 		return false;
143*432c6bacSPaul Burton 	if (regs->cp0_epc >= (base + PAGE_SIZE))
144*432c6bacSPaul Burton 		return false;
145*432c6bacSPaul Burton 
146*432c6bacSPaul Burton 	return true;
147*432c6bacSPaul Burton }
148*432c6bacSPaul Burton 
149*432c6bacSPaul Burton bool dsemul_thread_cleanup(struct task_struct *tsk)
150*432c6bacSPaul Burton {
151*432c6bacSPaul Burton 	int fr_idx;
152*432c6bacSPaul Burton 
153*432c6bacSPaul Burton 	/* Clear any allocated frame, retrieving its index */
154*432c6bacSPaul Burton 	fr_idx = atomic_xchg(&tsk->thread.bd_emu_frame, BD_EMUFRAME_NONE);
155*432c6bacSPaul Burton 
156*432c6bacSPaul Burton 	/* If no frame was allocated, we're done */
157*432c6bacSPaul Burton 	if (fr_idx == BD_EMUFRAME_NONE)
158*432c6bacSPaul Burton 		return false;
159*432c6bacSPaul Burton 
160*432c6bacSPaul Burton 	task_lock(tsk);
161*432c6bacSPaul Burton 
162*432c6bacSPaul Burton 	/* Free the frame that this thread had allocated */
163*432c6bacSPaul Burton 	if (tsk->mm)
164*432c6bacSPaul Burton 		free_emuframe(fr_idx, tsk->mm);
165*432c6bacSPaul Burton 
166*432c6bacSPaul Burton 	task_unlock(tsk);
167*432c6bacSPaul Burton 	return true;
168*432c6bacSPaul Burton }
169*432c6bacSPaul Burton 
170*432c6bacSPaul Burton bool dsemul_thread_rollback(struct pt_regs *regs)
171*432c6bacSPaul Burton {
172*432c6bacSPaul Burton 	struct emuframe __user *fr;
173*432c6bacSPaul Burton 	int fr_idx;
174*432c6bacSPaul Burton 
175*432c6bacSPaul Burton 	/* Do nothing if we're not executing from a frame */
176*432c6bacSPaul Burton 	if (!within_emuframe(regs))
177*432c6bacSPaul Burton 		return false;
178*432c6bacSPaul Burton 
179*432c6bacSPaul Burton 	/* Find the frame being executed */
180*432c6bacSPaul Burton 	fr_idx = atomic_read(&current->thread.bd_emu_frame);
181*432c6bacSPaul Burton 	if (fr_idx == BD_EMUFRAME_NONE)
182*432c6bacSPaul Burton 		return false;
183*432c6bacSPaul Burton 	fr = &dsemul_page()[fr_idx];
184*432c6bacSPaul Burton 
185*432c6bacSPaul Burton 	/*
186*432c6bacSPaul Burton 	 * If the PC is at the emul instruction, roll back to the branch. If
187*432c6bacSPaul Burton 	 * PC is at the badinst (break) instruction, we've already emulated the
188*432c6bacSPaul Burton 	 * instruction so progress to the continue PC. If it's anything else
189*432c6bacSPaul Burton 	 * then something is amiss & the user has branched into some other area
190*432c6bacSPaul Burton 	 * of the emupage - we'll free the allocated frame anyway.
191*432c6bacSPaul Burton 	 */
192*432c6bacSPaul Burton 	if (msk_isa16_mode(regs->cp0_epc) == (unsigned long)&fr->emul)
193*432c6bacSPaul Burton 		regs->cp0_epc = current->thread.bd_emu_branch_pc;
194*432c6bacSPaul Burton 	else if (msk_isa16_mode(regs->cp0_epc) == (unsigned long)&fr->badinst)
195*432c6bacSPaul Burton 		regs->cp0_epc = current->thread.bd_emu_cont_pc;
196*432c6bacSPaul Burton 
197*432c6bacSPaul Burton 	atomic_set(&current->thread.bd_emu_frame, BD_EMUFRAME_NONE);
198*432c6bacSPaul Burton 	free_emuframe(fr_idx, current->mm);
199*432c6bacSPaul Burton 	return true;
200*432c6bacSPaul Burton }
201*432c6bacSPaul Burton 
202*432c6bacSPaul Burton void dsemul_mm_cleanup(struct mm_struct *mm)
203*432c6bacSPaul Burton {
204*432c6bacSPaul Burton 	mm_context_t *mm_ctx = &mm->context;
205*432c6bacSPaul Burton 
206*432c6bacSPaul Burton 	kfree(mm_ctx->bd_emupage_allocmap);
207*432c6bacSPaul Burton }
208*432c6bacSPaul Burton 
209*432c6bacSPaul Burton int mips_dsemul(struct pt_regs *regs, mips_instruction ir,
210*432c6bacSPaul Burton 		unsigned long branch_pc, unsigned long cont_pc)
2111da177e4SLinus Torvalds {
2126d7b1415SMaciej W. Rozycki 	int isa16 = get_isa16_mode(regs->cp0_epc);
213733b8bc1SMaciej W. Rozycki 	mips_instruction break_math;
2145e0373b8SAtsushi Nemoto 	struct emuframe __user *fr;
215*432c6bacSPaul Burton 	int err, fr_idx;
2161da177e4SLinus Torvalds 
217102cedc3SLeonid Yegoshin 	/* NOP is easy */
21869a1e6cbSMaciej W. Rozycki 	if (ir == 0)
219e4553573SMaciej W. Rozycki 		return -1;
2201da177e4SLinus Torvalds 
22169a1e6cbSMaciej W. Rozycki 	/* microMIPS instructions */
2226d7b1415SMaciej W. Rozycki 	if (isa16) {
22369a1e6cbSMaciej W. Rozycki 		union mips_instruction insn = { .word = ir };
22469a1e6cbSMaciej W. Rozycki 
22569a1e6cbSMaciej W. Rozycki 		/* NOP16 aka MOVE16 $0, $0 */
22669a1e6cbSMaciej W. Rozycki 		if ((ir >> 16) == MM_NOP16)
22769a1e6cbSMaciej W. Rozycki 			return -1;
22869a1e6cbSMaciej W. Rozycki 
22969a1e6cbSMaciej W. Rozycki 		/* ADDIUPC */
23069a1e6cbSMaciej W. Rozycki 		if (insn.mm_a_format.opcode == mm_addiupc_op) {
23169a1e6cbSMaciej W. Rozycki 			unsigned int rs;
23269a1e6cbSMaciej W. Rozycki 			s32 v;
23369a1e6cbSMaciej W. Rozycki 
234036aff91SMaciej W. Rozycki 			rs = (((insn.mm_a_format.rs + 0xe) & 0xf) + 2);
23569a1e6cbSMaciej W. Rozycki 			v = regs->cp0_epc & ~3;
23669a1e6cbSMaciej W. Rozycki 			v += insn.mm_a_format.simmediate << 2;
23769a1e6cbSMaciej W. Rozycki 			regs->regs[rs] = (long)v;
23869a1e6cbSMaciej W. Rozycki 			return -1;
23969a1e6cbSMaciej W. Rozycki 		}
24069a1e6cbSMaciej W. Rozycki 	}
24169a1e6cbSMaciej W. Rozycki 
242*432c6bacSPaul Burton 	pr_debug("dsemul 0x%08lx cont at 0x%08lx\n", regs->cp0_epc, cont_pc);
2431da177e4SLinus Torvalds 
244*432c6bacSPaul Burton 	/* Allocate a frame if we don't already have one */
245*432c6bacSPaul Burton 	fr_idx = atomic_read(&current->thread.bd_emu_frame);
246*432c6bacSPaul Burton 	if (fr_idx == BD_EMUFRAME_NONE)
247*432c6bacSPaul Burton 		fr_idx = alloc_emuframe();
248*432c6bacSPaul Burton 	if (fr_idx == BD_EMUFRAME_NONE)
249*432c6bacSPaul Burton 		return SIGBUS;
250*432c6bacSPaul Burton 	fr = &dsemul_page()[fr_idx];
251*432c6bacSPaul Burton 
252*432c6bacSPaul Burton 	/* Retrieve the appropriately encoded break instruction */
2536d7b1415SMaciej W. Rozycki 	break_math = BREAK_MATH(isa16);
2541da177e4SLinus Torvalds 
255*432c6bacSPaul Burton 	/* Write the instructions to the frame */
2566d7b1415SMaciej W. Rozycki 	if (isa16) {
257a87265cfSMaciej W. Rozycki 		err = __put_user(ir >> 16,
258a87265cfSMaciej W. Rozycki 				 (u16 __user *)(&fr->emul));
259a87265cfSMaciej W. Rozycki 		err |= __put_user(ir & 0xffff,
260a87265cfSMaciej W. Rozycki 				  (u16 __user *)((long)(&fr->emul) + 2));
261733b8bc1SMaciej W. Rozycki 		err |= __put_user(break_math >> 16,
262a87265cfSMaciej W. Rozycki 				  (u16 __user *)(&fr->badinst));
263733b8bc1SMaciej W. Rozycki 		err |= __put_user(break_math & 0xffff,
264a87265cfSMaciej W. Rozycki 				  (u16 __user *)((long)(&fr->badinst) + 2));
265102cedc3SLeonid Yegoshin 	} else {
2661da177e4SLinus Torvalds 		err = __put_user(ir, &fr->emul);
267733b8bc1SMaciej W. Rozycki 		err |= __put_user(break_math, &fr->badinst);
268102cedc3SLeonid Yegoshin 	}
269102cedc3SLeonid Yegoshin 
2701da177e4SLinus Torvalds 	if (unlikely(err)) {
271b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(errors);
272*432c6bacSPaul Burton 		free_emuframe(fr_idx, current->mm);
2731da177e4SLinus Torvalds 		return SIGBUS;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
276*432c6bacSPaul Burton 	/* Record the PC of the branch, PC to continue from & frame index */
277*432c6bacSPaul Burton 	current->thread.bd_emu_branch_pc = branch_pc;
278*432c6bacSPaul Burton 	current->thread.bd_emu_cont_pc = cont_pc;
279*432c6bacSPaul Burton 	atomic_set(&current->thread.bd_emu_frame, fr_idx);
280*432c6bacSPaul Burton 
281*432c6bacSPaul Burton 	/* Change user register context to execute the frame */
2826d7b1415SMaciej W. Rozycki 	regs->cp0_epc = (unsigned long)&fr->emul | isa16;
2831da177e4SLinus Torvalds 
284*432c6bacSPaul Burton 	/* Ensure the icache observes our newly written frame */
2857737b20bSMaciej W. Rozycki 	flush_cache_sigtramp((unsigned long)&fr->emul);
2861da177e4SLinus Torvalds 
2879ab4471cSMaciej W. Rozycki 	return 0;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
290*432c6bacSPaul Burton bool do_dsemulret(struct pt_regs *xcp)
2911da177e4SLinus Torvalds {
292*432c6bacSPaul Burton 	/* Cleanup the allocated frame, returning if there wasn't one */
293*432c6bacSPaul Burton 	if (!dsemul_thread_cleanup(current)) {
294b6ee75edSDavid Daney 		MIPS_FPU_EMU_INC_STATS(errors);
295*432c6bacSPaul Burton 		return false;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	/* Set EPC to return to post-branch instruction */
299*432c6bacSPaul Burton 	xcp->cp0_epc = current->thread.bd_emu_cont_pc;
300*432c6bacSPaul Burton 	pr_debug("dsemulret to 0x%08lx\n", xcp->cp0_epc);
301*432c6bacSPaul Burton 	return true;
3021da177e4SLinus Torvalds }
303