1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2003, 2004, 2007 Maciej W. Rozycki
5 #include <linux/context_tracking.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/ptrace.h>
9 #include <linux/stddef.h>
12 #include <asm/compiler.h>
15 #include <asm/mipsregs.h>
16 #include <asm/setup.h>
18 static char bug64hit
[] __initdata
=
19 "reliable operation impossible!\n%s";
20 static char nowar
[] __initdata
=
21 "Please report to <linux-mips@linux-mips.org>.";
22 static char r4kwar
[] __initdata
=
23 "Enable CPU_R4000_WORKAROUNDS to rectify.";
24 static char daddiwar
[] __initdata
=
25 "Enable CPU_DADDI_WORKAROUNDS to rectify.";
27 static __always_inline __init
28 void align_mod(const int align
, const int mod
)
39 : "n"(align
), "n"(mod
));
42 static __always_inline __init
43 void mult_sh_align_mod(long *v1
, long *v2
, long *w
,
44 const int align
, const int mod
)
48 long p
, s
, lv1
, lv2
, lw
;
51 * We want the multiply and the shift to be isolated from the
52 * rest of the code to disable gcc optimizations. Hence the
53 * asm statements that execute nothing, but make gcc not know
54 * what the values of m1, m2 and s are and what lv2 and p are
58 local_irq_save(flags
);
60 * The following code leads to a wrong result of the first
61 * dsll32 when executed on R4000 rev. 2.2 or 3.0 (PRId
62 * 00000422 or 00000430, respectively).
64 * See "MIPS R4000PC/SC Errata, Processor Revision 2.2 and
65 * 3.0" by MIPS Technologies, Inc., errata #16 and #28 for
66 * details. I got no permission to duplicate them here,
71 : "=r" (m1
), "=r" (m2
), "=r" (s
)
72 : "0" (5), "1" (8), "2" (5));
73 align_mod(align
, mod
);
75 * The trailing nop is needed to fulfill the two-instruction
76 * requirement between reading hi/lo and staring a mult/div.
77 * Leaving it out may cause gas insert a nop itself breaking
78 * the desired alignment of the next chunk.
86 "dsll32 %0, %4, %5\n\t"
88 "dsll32 %1, %4, %5\n\t"
91 : "=&r" (lv1
), "=r" (lw
)
92 : "r" (m1
), "r" (m2
), "r" (s
), "I" (0)
94 /* We have to use single integers for m1 and m2 and a double
95 * one for p to be sure the mulsidi3 gcc's RTL multiplication
96 * instruction has the workaround applied. Older versions of
97 * gcc have correct umulsi3 and mulsi3, but other
98 * multiplication variants lack the workaround.
102 : "=r" (m1
), "=r" (m2
), "=r" (s
)
103 : "0" (m1
), "1" (m2
), "2" (s
));
104 align_mod(align
, mod
);
110 : "0" (lv2
), "r" (p
));
111 local_irq_restore(flags
);
118 static __always_inline __init
void check_mult_sh(void)
120 long v1
[8], v2
[8], w
[8];
123 printk("Checking for the multiply/shift bug... ");
126 * Testing discovered false negatives for certain code offsets
127 * into cache lines. Hence we test all possible offsets for
128 * the worst assumption of an R4000 I-cache line width of 32
131 * We can't use a loop as alignment directives need to be
134 mult_sh_align_mod(&v1
[0], &v2
[0], &w
[0], 32, 0);
135 mult_sh_align_mod(&v1
[1], &v2
[1], &w
[1], 32, 1);
136 mult_sh_align_mod(&v1
[2], &v2
[2], &w
[2], 32, 2);
137 mult_sh_align_mod(&v1
[3], &v2
[3], &w
[3], 32, 3);
138 mult_sh_align_mod(&v1
[4], &v2
[4], &w
[4], 32, 4);
139 mult_sh_align_mod(&v1
[5], &v2
[5], &w
[5], 32, 5);
140 mult_sh_align_mod(&v1
[6], &v2
[6], &w
[6], 32, 6);
141 mult_sh_align_mod(&v1
[7], &v2
[7], &w
[7], 32, 7);
144 for (i
= 0; i
< 8; i
++)
153 pr_cont("yes, workaround... ");
156 for (i
= 0; i
< 8; i
++)
166 panic(bug64hit
, !R4000_WAR
? r4kwar
: nowar
);
169 static volatile int daddi_ov
;
171 asmlinkage
void __init
do_daddi_ov(struct pt_regs
*regs
)
173 enum ctx_state prev_state
;
175 prev_state
= exception_enter();
178 exception_exit(prev_state
);
181 static __init
void check_daddi(void)
183 extern asmlinkage
void handle_daddi_ov(void);
188 printk("Checking for the daddi bug... ");
190 local_irq_save(flags
);
191 handler
= set_except_vector(EXCCODE_OV
, handle_daddi_ov
);
193 * The following code fails to trigger an overflow exception
194 * when executed on R4000 rev. 2.2 or 3.0 (PRId 00000422 or
195 * 00000430, respectively).
197 * See "MIPS R4000PC/SC Errata, Processor Revision 2.2 and
198 * 3.0" by MIPS Technologies, Inc., erratum #23 for details.
199 * I got no permission to duplicate it here, sigh... --macro
206 "addiu %1, $0, %2\n\t"
208 #ifdef HAVE_AS_SET_DADDI
211 "daddi %0, %1, %3\n\t"
213 : "=r" (v
), "=&r" (tmp
)
214 : "I" (0xffffffffffffdb9aUL
), "I" (0x1234));
215 set_except_vector(EXCCODE_OV
, handler
);
216 local_irq_restore(flags
);
223 pr_cont("yes, workaround... ");
225 local_irq_save(flags
);
226 handler
= set_except_vector(EXCCODE_OV
, handle_daddi_ov
);
228 "addiu %1, $0, %2\n\t"
231 : "=r" (v
), "=&r" (tmp
)
232 : "I" (0xffffffffffffdb9aUL
), "I" (0x1234));
233 set_except_vector(EXCCODE_OV
, handler
);
234 local_irq_restore(flags
);
242 panic(bug64hit
, !DADDI_WAR
? daddiwar
: nowar
);
247 static __init
void check_daddiu(void)
251 printk("Checking for the daddiu bug... ");
254 * The following code leads to a wrong result of daddiu when
255 * executed on R4400 rev. 1.0 (PRId 00000440).
257 * See "MIPS R4400PC/SC Errata, Processor Revision 1.0" by
258 * MIPS Technologies, Inc., erratum #7 for details.
260 * According to "MIPS R4000PC/SC Errata, Processor Revision
261 * 2.2 and 3.0" by MIPS Technologies, Inc., erratum #41 this
262 * problem affects R4000 rev. 2.2 and 3.0 (PRId 00000422 and
263 * 00000430, respectively), too. Testing failed to trigger it
266 * I got no permission to duplicate the errata here, sigh...
274 "addiu %2, $0, %3\n\t"
276 #ifdef HAVE_AS_SET_DADDI
279 "daddiu %0, %2, %4\n\t"
280 "addiu %1, $0, %4\n\t"
283 : "=&r" (v
), "=&r" (w
), "=&r" (tmp
)
284 : "I" (0xffffffffffffdb9aUL
), "I" (0x1234));
293 pr_cont("yes, workaround... ");
296 "addiu %2, $0, %3\n\t"
298 "daddiu %0, %2, %4\n\t"
299 "addiu %1, $0, %4\n\t"
301 : "=&r" (v
), "=&r" (w
), "=&r" (tmp
)
302 : "I" (0xffffffffffffdb9aUL
), "I" (0x1234));
310 panic(bug64hit
, !DADDI_WAR
? daddiwar
: nowar
);
313 void __init
check_bugs64_early(void)
319 void __init
check_bugs64(void)