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
6 * Copyright (C) 1996, 97, 2000, 2001 by Ralf Baechle
7 * Copyright (C) 2001 MIPS Technologies, Inc.
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/signal.h>
12 #include <asm/branch.h>
14 #include <asm/cpu-features.h>
16 #include <asm/fpu_emulator.h>
18 #include <asm/ptrace.h>
19 #include <asm/uaccess.h>
22 * Calculate and return exception epc in case of
23 * branch delay slot for microMIPS/MIPS16e
24 * It doesn't clear ISA mode bit.
26 int __isa_exception_epc(struct pt_regs
*regs
)
29 union mips16e_instruction inst
;
31 /* calc exception pc in branch delay slot */
33 if (__get_user(inst
.full
, (u16 __user
*) (epc
& ~MIPS_ISA_MODE
))) {
34 /* it should never happens... because delay slot was checked */
35 force_sig(SIGSEGV
, current
);
39 if (inst
.ri
.opcode
== MIPS16e_jal_op
)
43 } else if (mm_is16bit(inst
.full
))
52 * Compute the return address and do emulate branch simulation in MIPS16e mode,
54 * After exception only - doesn't do 'compact' branch/jumps and can't be used
55 * during interrupt (compact B/J doesn't do exception)
57 int __MIPS16e_compute_return_epc(struct pt_regs
*regs
)
60 union mips16e_instruction inst
;
67 * Read the instruction
69 addr
= (u16 __user
*) (epc
& ~MIPS_ISA_MODE
);
70 if (__get_user(inst
.full
, addr
)) {
71 force_sig(SIGSEGV
, current
);
75 switch (inst
.ri
.opcode
) {
76 case MIPS16e_extend_op
:
81 * JAL and JALX in MIPS16e mode
85 if (__get_user(inst2
, addr
)) {
86 force_sig(SIGSEGV
, current
);
89 fullinst
= ((unsigned)inst
.full
<< 16) | inst2
;
90 regs
->regs
[31] = epc
+ 6;
95 * JAL:5 X:1 TARGET[20-16]:5 TARGET[25:21]:5 TARGET[15:0]:16
97 * ......TARGET[15:0].................TARGET[20:16]...........
101 ((fullinst
& 0xffff) << 2) | ((fullinst
& 0x3e00000) >> 3) |
102 ((fullinst
& 0x1f0000) << 7);
104 epc
|= MIPS_ISA_MODE
; /* set ISA mode 1 */
112 if (inst
.rr
.func
== MIPS16e_jr_func
) {
115 regs
->cp0_epc
= regs
->regs
[31];
118 regs
->regs
[mips16e_reg2gpr
[inst
.rr
.rx
]];
122 regs
->regs
[31] = epc
+ 2;
124 regs
->regs
[31] = epc
+ 4;
131 /* all other cases have no branch delay slot and are 16bits,
132 and branches do not do exception */
139 * Compute the return address and do emulate branch simulation in
140 * microMIPS mode, if required.
141 * After exception only - doesn't do 'compact' branch/jumps and can't be used
142 * during interrupt (compact B/J doesn't do exception)
144 int __microMIPS_compute_return_epc(struct pt_regs
*regs
)
149 unsigned long contpc
;
150 struct decoded_instn mminst
= { 0 };
152 mminst
.micro_mips_mode
= 1;
155 * This load never faults.
157 pc16
= (unsigned short __user
*)(regs
->cp0_epc
& ~MIPS_ISA_MODE
);
158 __get_user(halfword
, pc16
);
160 contpc
= regs
->cp0_epc
+ 2;
161 word
= ((unsigned int)halfword
<< 16);
164 if (!mm_is16bit(halfword
)) {
165 __get_user(halfword
, pc16
);
167 contpc
= regs
->cp0_epc
+ 4;
173 if (get_user(halfword
, pc16
))
175 mminst
.next_pc_inc
= 2;
176 word
= ((unsigned int)halfword
<< 16);
178 if (!mm_is16bit(halfword
)) {
180 if (get_user(halfword
, pc16
))
182 mminst
.next_pc_inc
= 4;
185 mminst
.next_insn
= word
;
187 mm_isBranchInstr(regs
, mminst
, &contpc
);
189 regs
->cp0_epc
= contpc
;
194 force_sig(SIGSEGV
, current
);
199 * Compute the return address and do emulate branch simulation, if required.
200 * This function should be called only in branch delay slot active.
202 int __compute_return_epc(struct pt_regs
*regs
)
204 unsigned int __user
*addr
;
205 unsigned int bit
, fcr31
, dspcontrol
;
207 union mips_instruction insn
;
214 * Read the instruction
216 addr
= (unsigned int __user
*) epc
;
217 if (__get_user(insn
.word
, addr
)) {
218 force_sig(SIGSEGV
, current
);
222 switch (insn
.i_format
.opcode
) {
224 * jr and jalr are in r_format format.
227 switch (insn
.r_format
.func
) {
229 regs
->regs
[insn
.r_format
.rd
] = epc
+ 8;
232 regs
->cp0_epc
= regs
->regs
[insn
.r_format
.rs
];
238 * This group contains:
239 * bltz_op, bgez_op, bltzl_op, bgezl_op,
240 * bltzal_op, bgezal_op, bltzall_op, bgezall_op.
243 switch (insn
.i_format
.rt
) {
246 if ((long)regs
->regs
[insn
.i_format
.rs
] < 0)
247 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
255 if ((long)regs
->regs
[insn
.i_format
.rs
] >= 0)
256 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
264 regs
->regs
[31] = epc
+ 8;
265 if ((long)regs
->regs
[insn
.i_format
.rs
] < 0)
266 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
274 regs
->regs
[31] = epc
+ 8;
275 if ((long)regs
->regs
[insn
.i_format
.rs
] >= 0)
276 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
285 dspcontrol
= rddsp(0x01);
287 if (dspcontrol
>= 32) {
288 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
297 * These are unconditional and in j_format.
300 regs
->regs
[31] = regs
->cp0_epc
+ 8;
305 epc
|= (insn
.j_format
.target
<< 2);
307 if (insn
.i_format
.opcode
== jalx_op
)
308 regs
->cp0_epc
|= MIPS_ISA_MODE
;
312 * These are conditional and in i_format.
316 if (regs
->regs
[insn
.i_format
.rs
] ==
317 regs
->regs
[insn
.i_format
.rt
])
318 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
326 if (regs
->regs
[insn
.i_format
.rs
] !=
327 regs
->regs
[insn
.i_format
.rt
])
328 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
334 case blez_op
: /* not really i_format */
336 /* rt field assumed to be zero */
337 if ((long)regs
->regs
[insn
.i_format
.rs
] <= 0)
338 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
346 /* rt field assumed to be zero */
347 if ((long)regs
->regs
[insn
.i_format
.rs
] > 0)
348 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
355 * And now the FPA/cp1 branch instructions.
360 asm volatile("cfc1\t%0,$31" : "=r" (fcr31
));
362 fcr31
= current
->thread
.fpu
.fcr31
;
365 bit
= (insn
.i_format
.rt
>> 2);
368 switch (insn
.i_format
.rt
& 3) {
371 if (~fcr31
& (1 << bit
))
372 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
380 if (fcr31
& (1 << bit
))
381 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
388 #ifdef CONFIG_CPU_CAVIUM_OCTEON
389 case lwc2_op
: /* This is bbit0 on Octeon */
390 if ((regs
->regs
[insn
.i_format
.rs
] & (1ull << insn
.i_format
.rt
))
392 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
397 case ldc2_op
: /* This is bbit032 on Octeon */
398 if ((regs
->regs
[insn
.i_format
.rs
] &
399 (1ull << (insn
.i_format
.rt
+ 32))) == 0)
400 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
405 case swc2_op
: /* This is bbit1 on Octeon */
406 if (regs
->regs
[insn
.i_format
.rs
] & (1ull << insn
.i_format
.rt
))
407 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
412 case sdc2_op
: /* This is bbit132 on Octeon */
413 if (regs
->regs
[insn
.i_format
.rs
] &
414 (1ull << (insn
.i_format
.rt
+ 32)))
415 epc
= epc
+ 4 + (insn
.i_format
.simmediate
<< 2);
426 printk("%s: unaligned epc - sending SIGBUS.\n", current
->comm
);
427 force_sig(SIGBUS
, current
);
431 printk("%s: DSP branch but not DSP ASE - sending SIGBUS.\n", current
->comm
);
432 force_sig(SIGBUS
, current
);