xref: /openbmc/linux/arch/mips/kernel/branch.c (revision 5ec17af7)
1 /*
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
4  * for more details.
5  *
6  * Copyright (C) 1996, 97, 2000, 2001 by Ralf Baechle
7  * Copyright (C) 2001 MIPS Technologies, Inc.
8  */
9 #include <linux/kernel.h>
10 #include <linux/sched/signal.h>
11 #include <linux/signal.h>
12 #include <linux/export.h>
13 #include <asm/branch.h>
14 #include <asm/cpu.h>
15 #include <asm/cpu-features.h>
16 #include <asm/fpu.h>
17 #include <asm/fpu_emulator.h>
18 #include <asm/inst.h>
19 #include <asm/mips-r2-to-r6-emul.h>
20 #include <asm/ptrace.h>
21 #include <linux/uaccess.h>
22 
23 /*
24  * Calculate and return exception PC in case of branch delay slot
25  * for microMIPS and MIPS16e. It does not clear the ISA mode bit.
26  */
27 int __isa_exception_epc(struct pt_regs *regs)
28 {
29 	unsigned short inst;
30 	long epc = regs->cp0_epc;
31 
32 	/* Calculate exception PC in branch delay slot. */
33 	if (__get_user(inst, (u16 __user *) msk_isa16_mode(epc))) {
34 		/* This should never happen because delay slot was checked. */
35 		force_sig(SIGSEGV, current);
36 		return epc;
37 	}
38 	if (cpu_has_mips16) {
39 		union mips16e_instruction inst_mips16e;
40 
41 		inst_mips16e.full = inst;
42 		if (inst_mips16e.ri.opcode == MIPS16e_jal_op)
43 			epc += 4;
44 		else
45 			epc += 2;
46 	} else if (mm_insn_16bit(inst))
47 		epc += 2;
48 	else
49 		epc += 4;
50 
51 	return epc;
52 }
53 
54 /* (microMIPS) Convert 16-bit register encoding to 32-bit register encoding. */
55 static const unsigned int reg16to32map[8] = {16, 17, 2, 3, 4, 5, 6, 7};
56 
57 int __mm_isBranchInstr(struct pt_regs *regs, struct mm_decoded_insn dec_insn,
58 		       unsigned long *contpc)
59 {
60 	union mips_instruction insn = (union mips_instruction)dec_insn.insn;
61 
62 	if (!cpu_has_mmips)
63 		return 0;
64 
65 	switch (insn.mm_i_format.opcode) {
66 	case mm_pool32a_op:
67 		if ((insn.mm_i_format.simmediate & MM_POOL32A_MINOR_MASK) ==
68 		    mm_pool32axf_op) {
69 			switch (insn.mm_i_format.simmediate >>
70 				MM_POOL32A_MINOR_SHIFT) {
71 			case mm_jalr_op:
72 			case mm_jalrhb_op:
73 			case mm_jalrs_op:
74 			case mm_jalrshb_op:
75 				if (insn.mm_i_format.rt != 0)	/* Not mm_jr */
76 					regs->regs[insn.mm_i_format.rt] =
77 						regs->cp0_epc +
78 						dec_insn.pc_inc +
79 						dec_insn.next_pc_inc;
80 				*contpc = regs->regs[insn.mm_i_format.rs];
81 				return 1;
82 			}
83 		}
84 		break;
85 	case mm_pool32i_op:
86 		switch (insn.mm_i_format.rt) {
87 		case mm_bltzals_op:
88 		case mm_bltzal_op:
89 			regs->regs[31] = regs->cp0_epc +
90 				dec_insn.pc_inc +
91 				dec_insn.next_pc_inc;
92 			/* Fall through */
93 		case mm_bltz_op:
94 			if ((long)regs->regs[insn.mm_i_format.rs] < 0)
95 				*contpc = regs->cp0_epc +
96 					dec_insn.pc_inc +
97 					(insn.mm_i_format.simmediate << 1);
98 			else
99 				*contpc = regs->cp0_epc +
100 					dec_insn.pc_inc +
101 					dec_insn.next_pc_inc;
102 			return 1;
103 		case mm_bgezals_op:
104 		case mm_bgezal_op:
105 			regs->regs[31] = regs->cp0_epc +
106 					dec_insn.pc_inc +
107 					dec_insn.next_pc_inc;
108 			/* Fall through */
109 		case mm_bgez_op:
110 			if ((long)regs->regs[insn.mm_i_format.rs] >= 0)
111 				*contpc = regs->cp0_epc +
112 					dec_insn.pc_inc +
113 					(insn.mm_i_format.simmediate << 1);
114 			else
115 				*contpc = regs->cp0_epc +
116 					dec_insn.pc_inc +
117 					dec_insn.next_pc_inc;
118 			return 1;
119 		case mm_blez_op:
120 			if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
121 				*contpc = regs->cp0_epc +
122 					dec_insn.pc_inc +
123 					(insn.mm_i_format.simmediate << 1);
124 			else
125 				*contpc = regs->cp0_epc +
126 					dec_insn.pc_inc +
127 					dec_insn.next_pc_inc;
128 			return 1;
129 		case mm_bgtz_op:
130 			if ((long)regs->regs[insn.mm_i_format.rs] <= 0)
131 				*contpc = regs->cp0_epc +
132 					dec_insn.pc_inc +
133 					(insn.mm_i_format.simmediate << 1);
134 			else
135 				*contpc = regs->cp0_epc +
136 					dec_insn.pc_inc +
137 					dec_insn.next_pc_inc;
138 			return 1;
139 #ifdef CONFIG_MIPS_FP_SUPPORT
140 		case mm_bc2f_op:
141 		case mm_bc1f_op: {
142 			int bc_false = 0;
143 			unsigned int fcr31;
144 			unsigned int bit;
145 
146 			bc_false = 1;
147 			/* Fall through */
148 		case mm_bc2t_op:
149 		case mm_bc1t_op:
150 			preempt_disable();
151 			if (is_fpu_owner())
152 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
153 			else
154 				fcr31 = current->thread.fpu.fcr31;
155 			preempt_enable();
156 
157 			if (bc_false)
158 				fcr31 = ~fcr31;
159 
160 			bit = (insn.mm_i_format.rs >> 2);
161 			bit += (bit != 0);
162 			bit += 23;
163 			if (fcr31 & (1 << bit))
164 				*contpc = regs->cp0_epc +
165 					dec_insn.pc_inc +
166 					(insn.mm_i_format.simmediate << 1);
167 			else
168 				*contpc = regs->cp0_epc +
169 					dec_insn.pc_inc + dec_insn.next_pc_inc;
170 			return 1;
171 		}
172 #endif /* CONFIG_MIPS_FP_SUPPORT */
173 		}
174 		break;
175 	case mm_pool16c_op:
176 		switch (insn.mm_i_format.rt) {
177 		case mm_jalr16_op:
178 		case mm_jalrs16_op:
179 			regs->regs[31] = regs->cp0_epc +
180 				dec_insn.pc_inc + dec_insn.next_pc_inc;
181 			/* Fall through */
182 		case mm_jr16_op:
183 			*contpc = regs->regs[insn.mm_i_format.rs];
184 			return 1;
185 		}
186 		break;
187 	case mm_beqz16_op:
188 		if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] == 0)
189 			*contpc = regs->cp0_epc +
190 				dec_insn.pc_inc +
191 				(insn.mm_b1_format.simmediate << 1);
192 		else
193 			*contpc = regs->cp0_epc +
194 				dec_insn.pc_inc + dec_insn.next_pc_inc;
195 		return 1;
196 	case mm_bnez16_op:
197 		if ((long)regs->regs[reg16to32map[insn.mm_b1_format.rs]] != 0)
198 			*contpc = regs->cp0_epc +
199 				dec_insn.pc_inc +
200 				(insn.mm_b1_format.simmediate << 1);
201 		else
202 			*contpc = regs->cp0_epc +
203 				dec_insn.pc_inc + dec_insn.next_pc_inc;
204 		return 1;
205 	case mm_b16_op:
206 		*contpc = regs->cp0_epc + dec_insn.pc_inc +
207 			 (insn.mm_b0_format.simmediate << 1);
208 		return 1;
209 	case mm_beq32_op:
210 		if (regs->regs[insn.mm_i_format.rs] ==
211 		    regs->regs[insn.mm_i_format.rt])
212 			*contpc = regs->cp0_epc +
213 				dec_insn.pc_inc +
214 				(insn.mm_i_format.simmediate << 1);
215 		else
216 			*contpc = regs->cp0_epc +
217 				dec_insn.pc_inc +
218 				dec_insn.next_pc_inc;
219 		return 1;
220 	case mm_bne32_op:
221 		if (regs->regs[insn.mm_i_format.rs] !=
222 		    regs->regs[insn.mm_i_format.rt])
223 			*contpc = regs->cp0_epc +
224 				dec_insn.pc_inc +
225 				(insn.mm_i_format.simmediate << 1);
226 		else
227 			*contpc = regs->cp0_epc +
228 				dec_insn.pc_inc + dec_insn.next_pc_inc;
229 		return 1;
230 	case mm_jalx32_op:
231 		regs->regs[31] = regs->cp0_epc +
232 			dec_insn.pc_inc + dec_insn.next_pc_inc;
233 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
234 		*contpc >>= 28;
235 		*contpc <<= 28;
236 		*contpc |= (insn.j_format.target << 2);
237 		return 1;
238 	case mm_jals32_op:
239 	case mm_jal32_op:
240 		regs->regs[31] = regs->cp0_epc +
241 			dec_insn.pc_inc + dec_insn.next_pc_inc;
242 		/* Fall through */
243 	case mm_j32_op:
244 		*contpc = regs->cp0_epc + dec_insn.pc_inc;
245 		*contpc >>= 27;
246 		*contpc <<= 27;
247 		*contpc |= (insn.j_format.target << 1);
248 		set_isa16_mode(*contpc);
249 		return 1;
250 	}
251 	return 0;
252 }
253 
254 /*
255  * Compute return address and emulate branch in microMIPS mode after an
256  * exception only. It does not handle compact branches/jumps and cannot
257  * be used in interrupt context. (Compact branches/jumps do not cause
258  * exceptions.)
259  */
260 int __microMIPS_compute_return_epc(struct pt_regs *regs)
261 {
262 	u16 __user *pc16;
263 	u16 halfword;
264 	unsigned int word;
265 	unsigned long contpc;
266 	struct mm_decoded_insn mminsn = { 0 };
267 
268 	mminsn.micro_mips_mode = 1;
269 
270 	/* This load never faults. */
271 	pc16 = (unsigned short __user *)msk_isa16_mode(regs->cp0_epc);
272 	__get_user(halfword, pc16);
273 	pc16++;
274 	contpc = regs->cp0_epc + 2;
275 	word = ((unsigned int)halfword << 16);
276 	mminsn.pc_inc = 2;
277 
278 	if (!mm_insn_16bit(halfword)) {
279 		__get_user(halfword, pc16);
280 		pc16++;
281 		contpc = regs->cp0_epc + 4;
282 		mminsn.pc_inc = 4;
283 		word |= halfword;
284 	}
285 	mminsn.insn = word;
286 
287 	if (get_user(halfword, pc16))
288 		goto sigsegv;
289 	mminsn.next_pc_inc = 2;
290 	word = ((unsigned int)halfword << 16);
291 
292 	if (!mm_insn_16bit(halfword)) {
293 		pc16++;
294 		if (get_user(halfword, pc16))
295 			goto sigsegv;
296 		mminsn.next_pc_inc = 4;
297 		word |= halfword;
298 	}
299 	mminsn.next_insn = word;
300 
301 	mm_isBranchInstr(regs, mminsn, &contpc);
302 
303 	regs->cp0_epc = contpc;
304 
305 	return 0;
306 
307 sigsegv:
308 	force_sig(SIGSEGV, current);
309 	return -EFAULT;
310 }
311 
312 /*
313  * Compute return address and emulate branch in MIPS16e mode after an
314  * exception only. It does not handle compact branches/jumps and cannot
315  * be used in interrupt context. (Compact branches/jumps do not cause
316  * exceptions.)
317  */
318 int __MIPS16e_compute_return_epc(struct pt_regs *regs)
319 {
320 	u16 __user *addr;
321 	union mips16e_instruction inst;
322 	u16 inst2;
323 	u32 fullinst;
324 	long epc;
325 
326 	epc = regs->cp0_epc;
327 
328 	/* Read the instruction. */
329 	addr = (u16 __user *)msk_isa16_mode(epc);
330 	if (__get_user(inst.full, addr)) {
331 		force_sig(SIGSEGV, current);
332 		return -EFAULT;
333 	}
334 
335 	switch (inst.ri.opcode) {
336 	case MIPS16e_extend_op:
337 		regs->cp0_epc += 4;
338 		return 0;
339 
340 		/*
341 		 *  JAL and JALX in MIPS16e mode
342 		 */
343 	case MIPS16e_jal_op:
344 		addr += 1;
345 		if (__get_user(inst2, addr)) {
346 			force_sig(SIGSEGV, current);
347 			return -EFAULT;
348 		}
349 		fullinst = ((unsigned)inst.full << 16) | inst2;
350 		regs->regs[31] = epc + 6;
351 		epc += 4;
352 		epc >>= 28;
353 		epc <<= 28;
354 		/*
355 		 * JAL:5 X:1 TARGET[20-16]:5 TARGET[25:21]:5 TARGET[15:0]:16
356 		 *
357 		 * ......TARGET[15:0].................TARGET[20:16]...........
358 		 * ......TARGET[25:21]
359 		 */
360 		epc |=
361 		    ((fullinst & 0xffff) << 2) | ((fullinst & 0x3e00000) >> 3) |
362 		    ((fullinst & 0x1f0000) << 7);
363 		if (!inst.jal.x)
364 			set_isa16_mode(epc);	/* Set ISA mode bit. */
365 		regs->cp0_epc = epc;
366 		return 0;
367 
368 		/*
369 		 *  J(AL)R(C)
370 		 */
371 	case MIPS16e_rr_op:
372 		if (inst.rr.func == MIPS16e_jr_func) {
373 
374 			if (inst.rr.ra)
375 				regs->cp0_epc = regs->regs[31];
376 			else
377 				regs->cp0_epc =
378 				    regs->regs[reg16to32[inst.rr.rx]];
379 
380 			if (inst.rr.l) {
381 				if (inst.rr.nd)
382 					regs->regs[31] = epc + 2;
383 				else
384 					regs->regs[31] = epc + 4;
385 			}
386 			return 0;
387 		}
388 		break;
389 	}
390 
391 	/*
392 	 * All other cases have no branch delay slot and are 16-bits.
393 	 * Branches do not cause an exception.
394 	 */
395 	regs->cp0_epc += 2;
396 
397 	return 0;
398 }
399 
400 /**
401  * __compute_return_epc_for_insn - Computes the return address and do emulate
402  *				    branch simulation, if required.
403  *
404  * @regs:	Pointer to pt_regs
405  * @insn:	branch instruction to decode
406  * Return:	-EFAULT on error and forces SIGILL, and on success
407  *		returns 0 or BRANCH_LIKELY_TAKEN as appropriate after
408  *		evaluating the branch.
409  *
410  * MIPS R6 Compact branches and forbidden slots:
411  *	Compact branches do not throw exceptions because they do
412  *	not have delay slots. The forbidden slot instruction ($PC+4)
413  *	is only executed if the branch was not taken. Otherwise the
414  *	forbidden slot is skipped entirely. This means that the
415  *	only possible reason to be here because of a MIPS R6 compact
416  *	branch instruction is that the forbidden slot has thrown one.
417  *	In that case the branch was not taken, so the EPC can be safely
418  *	set to EPC + 8.
419  */
420 int __compute_return_epc_for_insn(struct pt_regs *regs,
421 				   union mips_instruction insn)
422 {
423 	long epc = regs->cp0_epc;
424 	unsigned int dspcontrol;
425 	int ret = 0;
426 
427 	switch (insn.i_format.opcode) {
428 	/*
429 	 * jr and jalr are in r_format format.
430 	 */
431 	case spec_op:
432 		switch (insn.r_format.func) {
433 		case jalr_op:
434 			regs->regs[insn.r_format.rd] = epc + 8;
435 			/* Fall through */
436 		case jr_op:
437 			if (NO_R6EMU && insn.r_format.func == jr_op)
438 				goto sigill_r2r6;
439 			regs->cp0_epc = regs->regs[insn.r_format.rs];
440 			break;
441 		}
442 		break;
443 
444 	/*
445 	 * This group contains:
446 	 * bltz_op, bgez_op, bltzl_op, bgezl_op,
447 	 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
448 	 */
449 	case bcond_op:
450 		switch (insn.i_format.rt) {
451 		case bltzl_op:
452 			if (NO_R6EMU)
453 				goto sigill_r2r6;
454 		case bltz_op:
455 			if ((long)regs->regs[insn.i_format.rs] < 0) {
456 				epc = epc + 4 + (insn.i_format.simmediate << 2);
457 				if (insn.i_format.rt == bltzl_op)
458 					ret = BRANCH_LIKELY_TAKEN;
459 			} else
460 				epc += 8;
461 			regs->cp0_epc = epc;
462 			break;
463 
464 		case bgezl_op:
465 			if (NO_R6EMU)
466 				goto sigill_r2r6;
467 		case bgez_op:
468 			if ((long)regs->regs[insn.i_format.rs] >= 0) {
469 				epc = epc + 4 + (insn.i_format.simmediate << 2);
470 				if (insn.i_format.rt == bgezl_op)
471 					ret = BRANCH_LIKELY_TAKEN;
472 			} else
473 				epc += 8;
474 			regs->cp0_epc = epc;
475 			break;
476 
477 		case bltzal_op:
478 		case bltzall_op:
479 			if (NO_R6EMU && (insn.i_format.rs ||
480 			    insn.i_format.rt == bltzall_op))
481 				goto sigill_r2r6;
482 			regs->regs[31] = epc + 8;
483 			/*
484 			 * OK we are here either because we hit a NAL
485 			 * instruction or because we are emulating an
486 			 * old bltzal{,l} one. Let's figure out what the
487 			 * case really is.
488 			 */
489 			if (!insn.i_format.rs) {
490 				/*
491 				 * NAL or BLTZAL with rs == 0
492 				 * Doesn't matter if we are R6 or not. The
493 				 * result is the same
494 				 */
495 				regs->cp0_epc += 4 +
496 					(insn.i_format.simmediate << 2);
497 				break;
498 			}
499 			/* Now do the real thing for non-R6 BLTZAL{,L} */
500 			if ((long)regs->regs[insn.i_format.rs] < 0) {
501 				epc = epc + 4 + (insn.i_format.simmediate << 2);
502 				if (insn.i_format.rt == bltzall_op)
503 					ret = BRANCH_LIKELY_TAKEN;
504 			} else
505 				epc += 8;
506 			regs->cp0_epc = epc;
507 			break;
508 
509 		case bgezal_op:
510 		case bgezall_op:
511 			if (NO_R6EMU && (insn.i_format.rs ||
512 			    insn.i_format.rt == bgezall_op))
513 				goto sigill_r2r6;
514 			regs->regs[31] = epc + 8;
515 			/*
516 			 * OK we are here either because we hit a BAL
517 			 * instruction or because we are emulating an
518 			 * old bgezal{,l} one. Let's figure out what the
519 			 * case really is.
520 			 */
521 			if (!insn.i_format.rs) {
522 				/*
523 				 * BAL or BGEZAL with rs == 0
524 				 * Doesn't matter if we are R6 or not. The
525 				 * result is the same
526 				 */
527 				regs->cp0_epc += 4 +
528 					(insn.i_format.simmediate << 2);
529 				break;
530 			}
531 			/* Now do the real thing for non-R6 BGEZAL{,L} */
532 			if ((long)regs->regs[insn.i_format.rs] >= 0) {
533 				epc = epc + 4 + (insn.i_format.simmediate << 2);
534 				if (insn.i_format.rt == bgezall_op)
535 					ret = BRANCH_LIKELY_TAKEN;
536 			} else
537 				epc += 8;
538 			regs->cp0_epc = epc;
539 			break;
540 
541 		case bposge32_op:
542 			if (!cpu_has_dsp)
543 				goto sigill_dsp;
544 
545 			dspcontrol = rddsp(0x01);
546 
547 			if (dspcontrol >= 32) {
548 				epc = epc + 4 + (insn.i_format.simmediate << 2);
549 			} else
550 				epc += 8;
551 			regs->cp0_epc = epc;
552 			break;
553 		}
554 		break;
555 
556 	/*
557 	 * These are unconditional and in j_format.
558 	 */
559 	case jalx_op:
560 	case jal_op:
561 		regs->regs[31] = regs->cp0_epc + 8;
562 	case j_op:
563 		epc += 4;
564 		epc >>= 28;
565 		epc <<= 28;
566 		epc |= (insn.j_format.target << 2);
567 		regs->cp0_epc = epc;
568 		if (insn.i_format.opcode == jalx_op)
569 			set_isa16_mode(regs->cp0_epc);
570 		break;
571 
572 	/*
573 	 * These are conditional and in i_format.
574 	 */
575 	case beql_op:
576 		if (NO_R6EMU)
577 			goto sigill_r2r6;
578 	case beq_op:
579 		if (regs->regs[insn.i_format.rs] ==
580 		    regs->regs[insn.i_format.rt]) {
581 			epc = epc + 4 + (insn.i_format.simmediate << 2);
582 			if (insn.i_format.opcode == beql_op)
583 				ret = BRANCH_LIKELY_TAKEN;
584 		} else
585 			epc += 8;
586 		regs->cp0_epc = epc;
587 		break;
588 
589 	case bnel_op:
590 		if (NO_R6EMU)
591 			goto sigill_r2r6;
592 	case bne_op:
593 		if (regs->regs[insn.i_format.rs] !=
594 		    regs->regs[insn.i_format.rt]) {
595 			epc = epc + 4 + (insn.i_format.simmediate << 2);
596 			if (insn.i_format.opcode == bnel_op)
597 				ret = BRANCH_LIKELY_TAKEN;
598 		} else
599 			epc += 8;
600 		regs->cp0_epc = epc;
601 		break;
602 
603 	case blezl_op: /* not really i_format */
604 		if (!insn.i_format.rt && NO_R6EMU)
605 			goto sigill_r2r6;
606 	case blez_op:
607 		/*
608 		 * Compact branches for R6 for the
609 		 * blez and blezl opcodes.
610 		 * BLEZ  | rs = 0 | rt != 0  == BLEZALC
611 		 * BLEZ  | rs = rt != 0      == BGEZALC
612 		 * BLEZ  | rs != 0 | rt != 0 == BGEUC
613 		 * BLEZL | rs = 0 | rt != 0  == BLEZC
614 		 * BLEZL | rs = rt != 0      == BGEZC
615 		 * BLEZL | rs != 0 | rt != 0 == BGEC
616 		 *
617 		 * For real BLEZ{,L}, rt is always 0.
618 		 */
619 
620 		if (cpu_has_mips_r6 && insn.i_format.rt) {
621 			if ((insn.i_format.opcode == blez_op) &&
622 			    ((!insn.i_format.rs && insn.i_format.rt) ||
623 			     (insn.i_format.rs == insn.i_format.rt)))
624 				regs->regs[31] = epc + 4;
625 			regs->cp0_epc += 8;
626 			break;
627 		}
628 		/* rt field assumed to be zero */
629 		if ((long)regs->regs[insn.i_format.rs] <= 0) {
630 			epc = epc + 4 + (insn.i_format.simmediate << 2);
631 			if (insn.i_format.opcode == blezl_op)
632 				ret = BRANCH_LIKELY_TAKEN;
633 		} else
634 			epc += 8;
635 		regs->cp0_epc = epc;
636 		break;
637 
638 	case bgtzl_op:
639 		if (!insn.i_format.rt && NO_R6EMU)
640 			goto sigill_r2r6;
641 	case bgtz_op:
642 		/*
643 		 * Compact branches for R6 for the
644 		 * bgtz and bgtzl opcodes.
645 		 * BGTZ  | rs = 0 | rt != 0  == BGTZALC
646 		 * BGTZ  | rs = rt != 0      == BLTZALC
647 		 * BGTZ  | rs != 0 | rt != 0 == BLTUC
648 		 * BGTZL | rs = 0 | rt != 0  == BGTZC
649 		 * BGTZL | rs = rt != 0      == BLTZC
650 		 * BGTZL | rs != 0 | rt != 0 == BLTC
651 		 *
652 		 * *ZALC varint for BGTZ &&& rt != 0
653 		 * For real GTZ{,L}, rt is always 0.
654 		 */
655 		if (cpu_has_mips_r6 && insn.i_format.rt) {
656 			if ((insn.i_format.opcode == blez_op) &&
657 			    ((!insn.i_format.rs && insn.i_format.rt) ||
658 			    (insn.i_format.rs == insn.i_format.rt)))
659 				regs->regs[31] = epc + 4;
660 			regs->cp0_epc += 8;
661 			break;
662 		}
663 
664 		/* rt field assumed to be zero */
665 		if ((long)regs->regs[insn.i_format.rs] > 0) {
666 			epc = epc + 4 + (insn.i_format.simmediate << 2);
667 			if (insn.i_format.opcode == bgtzl_op)
668 				ret = BRANCH_LIKELY_TAKEN;
669 		} else
670 			epc += 8;
671 		regs->cp0_epc = epc;
672 		break;
673 
674 #ifdef CONFIG_MIPS_FP_SUPPORT
675 	/*
676 	 * And now the FPA/cp1 branch instructions.
677 	 */
678 	case cop1_op: {
679 		unsigned int bit, fcr31, reg;
680 
681 		if (cpu_has_mips_r6 &&
682 		    ((insn.i_format.rs == bc1eqz_op) ||
683 		     (insn.i_format.rs == bc1nez_op))) {
684 			if (!init_fp_ctx(current))
685 				lose_fpu(1);
686 			reg = insn.i_format.rt;
687 			bit = get_fpr32(&current->thread.fpu.fpr[reg], 0) & 0x1;
688 			if (insn.i_format.rs == bc1eqz_op)
689 				bit = !bit;
690 			own_fpu(1);
691 			if (bit)
692 				epc = epc + 4 +
693 					(insn.i_format.simmediate << 2);
694 			else
695 				epc += 8;
696 			regs->cp0_epc = epc;
697 
698 			break;
699 		} else {
700 
701 			preempt_disable();
702 			if (is_fpu_owner())
703 			        fcr31 = read_32bit_cp1_register(CP1_STATUS);
704 			else
705 				fcr31 = current->thread.fpu.fcr31;
706 			preempt_enable();
707 
708 			bit = (insn.i_format.rt >> 2);
709 			bit += (bit != 0);
710 			bit += 23;
711 			switch (insn.i_format.rt & 3) {
712 			case 0: /* bc1f */
713 			case 2: /* bc1fl */
714 				if (~fcr31 & (1 << bit)) {
715 					epc = epc + 4 +
716 						(insn.i_format.simmediate << 2);
717 					if (insn.i_format.rt == 2)
718 						ret = BRANCH_LIKELY_TAKEN;
719 				} else
720 					epc += 8;
721 				regs->cp0_epc = epc;
722 				break;
723 
724 			case 1: /* bc1t */
725 			case 3: /* bc1tl */
726 				if (fcr31 & (1 << bit)) {
727 					epc = epc + 4 +
728 						(insn.i_format.simmediate << 2);
729 					if (insn.i_format.rt == 3)
730 						ret = BRANCH_LIKELY_TAKEN;
731 				} else
732 					epc += 8;
733 				regs->cp0_epc = epc;
734 				break;
735 			}
736 			break;
737 		}
738 	}
739 #endif /* CONFIG_MIPS_FP_SUPPORT */
740 
741 #ifdef CONFIG_CPU_CAVIUM_OCTEON
742 	case lwc2_op: /* This is bbit0 on Octeon */
743 		if ((regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
744 		     == 0)
745 			epc = epc + 4 + (insn.i_format.simmediate << 2);
746 		else
747 			epc += 8;
748 		regs->cp0_epc = epc;
749 		break;
750 	case ldc2_op: /* This is bbit032 on Octeon */
751 		if ((regs->regs[insn.i_format.rs] &
752 		    (1ull<<(insn.i_format.rt+32))) == 0)
753 			epc = epc + 4 + (insn.i_format.simmediate << 2);
754 		else
755 			epc += 8;
756 		regs->cp0_epc = epc;
757 		break;
758 	case swc2_op: /* This is bbit1 on Octeon */
759 		if (regs->regs[insn.i_format.rs] & (1ull<<insn.i_format.rt))
760 			epc = epc + 4 + (insn.i_format.simmediate << 2);
761 		else
762 			epc += 8;
763 		regs->cp0_epc = epc;
764 		break;
765 	case sdc2_op: /* This is bbit132 on Octeon */
766 		if (regs->regs[insn.i_format.rs] &
767 		    (1ull<<(insn.i_format.rt+32)))
768 			epc = epc + 4 + (insn.i_format.simmediate << 2);
769 		else
770 			epc += 8;
771 		regs->cp0_epc = epc;
772 		break;
773 #else
774 	case bc6_op:
775 		/* Only valid for MIPS R6 */
776 		if (!cpu_has_mips_r6)
777 			goto sigill_r6;
778 		regs->cp0_epc += 8;
779 		break;
780 	case balc6_op:
781 		if (!cpu_has_mips_r6)
782 			goto sigill_r6;
783 		/* Compact branch: BALC */
784 		regs->regs[31] = epc + 4;
785 		epc += 4 + (insn.i_format.simmediate << 2);
786 		regs->cp0_epc = epc;
787 		break;
788 	case pop66_op:
789 		if (!cpu_has_mips_r6)
790 			goto sigill_r6;
791 		/* Compact branch: BEQZC || JIC */
792 		regs->cp0_epc += 8;
793 		break;
794 	case pop76_op:
795 		if (!cpu_has_mips_r6)
796 			goto sigill_r6;
797 		/* Compact branch: BNEZC || JIALC */
798 		if (!insn.i_format.rs) {
799 			/* JIALC: set $31/ra */
800 			regs->regs[31] = epc + 4;
801 		}
802 		regs->cp0_epc += 8;
803 		break;
804 #endif
805 	case pop10_op:
806 	case pop30_op:
807 		/* Only valid for MIPS R6 */
808 		if (!cpu_has_mips_r6)
809 			goto sigill_r6;
810 		/*
811 		 * Compact branches:
812 		 * bovc, beqc, beqzalc, bnvc, bnec, bnezlac
813 		 */
814 		if (insn.i_format.rt && !insn.i_format.rs)
815 			regs->regs[31] = epc + 4;
816 		regs->cp0_epc += 8;
817 		break;
818 	}
819 
820 	return ret;
821 
822 sigill_dsp:
823 	pr_debug("%s: DSP branch but not DSP ASE - sending SIGILL.\n",
824 		 current->comm);
825 	force_sig(SIGILL, current);
826 	return -EFAULT;
827 sigill_r2r6:
828 	pr_debug("%s: R2 branch but r2-to-r6 emulator is not present - sending SIGILL.\n",
829 		 current->comm);
830 	force_sig(SIGILL, current);
831 	return -EFAULT;
832 sigill_r6:
833 	pr_debug("%s: R6 branch but no MIPSr6 ISA support - sending SIGILL.\n",
834 		 current->comm);
835 	force_sig(SIGILL, current);
836 	return -EFAULT;
837 }
838 EXPORT_SYMBOL_GPL(__compute_return_epc_for_insn);
839 
840 int __compute_return_epc(struct pt_regs *regs)
841 {
842 	unsigned int __user *addr;
843 	long epc;
844 	union mips_instruction insn;
845 
846 	epc = regs->cp0_epc;
847 	if (epc & 3)
848 		goto unaligned;
849 
850 	/*
851 	 * Read the instruction
852 	 */
853 	addr = (unsigned int __user *) epc;
854 	if (__get_user(insn.word, addr)) {
855 		force_sig(SIGSEGV, current);
856 		return -EFAULT;
857 	}
858 
859 	return __compute_return_epc_for_insn(regs, insn);
860 
861 unaligned:
862 	printk("%s: unaligned epc - sending SIGBUS.\n", current->comm);
863 	force_sig(SIGBUS, current);
864 	return -EFAULT;
865 }
866 
867 #if (defined CONFIG_KPROBES) || (defined CONFIG_UPROBES)
868 
869 int __insn_is_compact_branch(union mips_instruction insn)
870 {
871 	if (!cpu_has_mips_r6)
872 		return 0;
873 
874 	switch (insn.i_format.opcode) {
875 	case blezl_op:
876 	case bgtzl_op:
877 	case blez_op:
878 	case bgtz_op:
879 		/*
880 		 * blez[l] and bgtz[l] opcodes with non-zero rt
881 		 * are MIPS R6 compact branches
882 		 */
883 		if (insn.i_format.rt)
884 			return 1;
885 		break;
886 	case bc6_op:
887 	case balc6_op:
888 	case pop10_op:
889 	case pop30_op:
890 	case pop66_op:
891 	case pop76_op:
892 		return 1;
893 	}
894 
895 	return 0;
896 }
897 EXPORT_SYMBOL_GPL(__insn_is_compact_branch);
898 
899 #endif  /* CONFIG_KPROBES || CONFIG_UPROBES */
900