r4k-bugs64.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2003, 2004, 2007 Maciej W. Rozycki
  4. */
  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>
  10. #include <asm/bugs.h>
  11. #include <asm/compiler.h>
  12. #include <asm/cpu.h>
  13. #include <asm/fpu.h>
  14. #include <asm/mipsregs.h>
  15. #include <asm/setup.h>
  16. #include <asm/traps.h>
  17. static char bug64hit[] __initdata =
  18. "reliable operation impossible!\n%s";
  19. static char nowar[] __initdata =
  20. "Please report to <linux-mips@vger.kernel.org>.";
  21. static char r4kwar[] __initdata =
  22. "Enable CPU_R4000_WORKAROUNDS to rectify.";
  23. static char daddiwar[] __initdata =
  24. "Enable CPU_DADDI_WORKAROUNDS to rectify.";
  25. static __always_inline __init
  26. void align_mod(const int align, const int mod)
  27. {
  28. asm volatile(
  29. ".set push\n\t"
  30. ".set noreorder\n\t"
  31. ".balign %0\n\t"
  32. ".rept %1\n\t"
  33. "nop\n\t"
  34. ".endr\n\t"
  35. ".set pop"
  36. :
  37. : "n"(align), "n"(mod));
  38. }
  39. static __always_inline __init
  40. void mult_sh_align_mod(long *v1, long *v2, long *w,
  41. const int align, const int mod)
  42. {
  43. unsigned long flags;
  44. int m1, m2;
  45. long p, s, lv1, lv2, lw;
  46. /*
  47. * We want the multiply and the shift to be isolated from the
  48. * rest of the code to disable gcc optimizations. Hence the
  49. * asm statements that execute nothing, but make gcc not know
  50. * what the values of m1, m2 and s are and what lv2 and p are
  51. * used for.
  52. */
  53. local_irq_save(flags);
  54. /*
  55. * The following code leads to a wrong result of the first
  56. * dsll32 when executed on R4000 rev. 2.2 or 3.0 (PRId
  57. * 00000422 or 00000430, respectively).
  58. *
  59. * See "MIPS R4000PC/SC Errata, Processor Revision 2.2 and
  60. * 3.0" by MIPS Technologies, Inc., errata #16 and #28 for
  61. * details. I got no permission to duplicate them here,
  62. * sigh... --macro
  63. */
  64. asm volatile(
  65. ""
  66. : "=r" (m1), "=r" (m2), "=r" (s)
  67. : "0" (5), "1" (8), "2" (5));
  68. align_mod(align, mod);
  69. /*
  70. * The trailing nop is needed to fulfill the two-instruction
  71. * requirement between reading hi/lo and staring a mult/div.
  72. * Leaving it out may cause gas insert a nop itself breaking
  73. * the desired alignment of the next chunk.
  74. */
  75. asm volatile(
  76. ".set push\n\t"
  77. ".set noat\n\t"
  78. ".set noreorder\n\t"
  79. ".set nomacro\n\t"
  80. "mult %2, %3\n\t"
  81. "dsll32 %0, %4, %5\n\t"
  82. "mflo $0\n\t"
  83. "dsll32 %1, %4, %5\n\t"
  84. "nop\n\t"
  85. ".set pop"
  86. : "=&r" (lv1), "=r" (lw)
  87. : "r" (m1), "r" (m2), "r" (s), "I" (0)
  88. : "hi", "lo", "$0");
  89. /* We have to use single integers for m1 and m2 and a double
  90. * one for p to be sure the mulsidi3 gcc's RTL multiplication
  91. * instruction has the workaround applied. Older versions of
  92. * gcc have correct umulsi3 and mulsi3, but other
  93. * multiplication variants lack the workaround.
  94. */
  95. asm volatile(
  96. ""
  97. : "=r" (m1), "=r" (m2), "=r" (s)
  98. : "0" (m1), "1" (m2), "2" (s));
  99. align_mod(align, mod);
  100. p = m1 * m2;
  101. lv2 = s << 32;
  102. asm volatile(
  103. ""
  104. : "=r" (lv2)
  105. : "0" (lv2), "r" (p));
  106. local_irq_restore(flags);
  107. *v1 = lv1;
  108. *v2 = lv2;
  109. *w = lw;
  110. }
  111. static __always_inline __init void check_mult_sh(void)
  112. {
  113. long v1[8], v2[8], w[8];
  114. int bug, fix, i;
  115. printk("Checking for the multiply/shift bug... ");
  116. /*
  117. * Testing discovered false negatives for certain code offsets
  118. * into cache lines. Hence we test all possible offsets for
  119. * the worst assumption of an R4000 I-cache line width of 32
  120. * bytes.
  121. *
  122. * We can't use a loop as alignment directives need to be
  123. * immediates.
  124. */
  125. mult_sh_align_mod(&v1[0], &v2[0], &w[0], 32, 0);
  126. mult_sh_align_mod(&v1[1], &v2[1], &w[1], 32, 1);
  127. mult_sh_align_mod(&v1[2], &v2[2], &w[2], 32, 2);
  128. mult_sh_align_mod(&v1[3], &v2[3], &w[3], 32, 3);
  129. mult_sh_align_mod(&v1[4], &v2[4], &w[4], 32, 4);
  130. mult_sh_align_mod(&v1[5], &v2[5], &w[5], 32, 5);
  131. mult_sh_align_mod(&v1[6], &v2[6], &w[6], 32, 6);
  132. mult_sh_align_mod(&v1[7], &v2[7], &w[7], 32, 7);
  133. bug = 0;
  134. for (i = 0; i < 8; i++)
  135. if (v1[i] != w[i])
  136. bug = 1;
  137. if (bug == 0) {
  138. pr_cont("no.\n");
  139. return;
  140. }
  141. pr_cont("yes, workaround... ");
  142. fix = 1;
  143. for (i = 0; i < 8; i++)
  144. if (v2[i] != w[i])
  145. fix = 0;
  146. if (fix == 1) {
  147. pr_cont("yes.\n");
  148. return;
  149. }
  150. pr_cont("no.\n");
  151. panic(bug64hit,
  152. IS_ENABLED(CONFIG_CPU_R4000_WORKAROUNDS) ? nowar : r4kwar);
  153. }
  154. static volatile int daddi_ov;
  155. asmlinkage void __init do_daddi_ov(struct pt_regs *regs)
  156. {
  157. enum ctx_state prev_state;
  158. prev_state = exception_enter();
  159. daddi_ov = 1;
  160. regs->cp0_epc += 4;
  161. exception_exit(prev_state);
  162. }
  163. static __init void check_daddi(void)
  164. {
  165. extern asmlinkage void handle_daddi_ov(void);
  166. unsigned long flags;
  167. void *handler;
  168. long v, tmp;
  169. printk("Checking for the daddi bug... ");
  170. local_irq_save(flags);
  171. handler = set_except_vector(EXCCODE_OV, handle_daddi_ov);
  172. /*
  173. * The following code fails to trigger an overflow exception
  174. * when executed on R4000 rev. 2.2 or 3.0 (PRId 00000422 or
  175. * 00000430, respectively).
  176. *
  177. * See "MIPS R4000PC/SC Errata, Processor Revision 2.2 and
  178. * 3.0" by MIPS Technologies, Inc., erratum #23 for details.
  179. * I got no permission to duplicate it here, sigh... --macro
  180. */
  181. asm volatile(
  182. ".set push\n\t"
  183. ".set noat\n\t"
  184. ".set noreorder\n\t"
  185. ".set nomacro\n\t"
  186. "addiu %1, $0, %2\n\t"
  187. "dsrl %1, %1, 1\n\t"
  188. #ifdef HAVE_AS_SET_DADDI
  189. ".set daddi\n\t"
  190. #endif
  191. "daddi %0, %1, %3\n\t"
  192. ".set pop"
  193. : "=r" (v), "=&r" (tmp)
  194. : "I" (0xffffffffffffdb9aUL), "I" (0x1234));
  195. set_except_vector(EXCCODE_OV, handler);
  196. local_irq_restore(flags);
  197. if (daddi_ov) {
  198. pr_cont("no.\n");
  199. return;
  200. }
  201. pr_cont("yes, workaround... ");
  202. local_irq_save(flags);
  203. handler = set_except_vector(EXCCODE_OV, handle_daddi_ov);
  204. asm volatile(
  205. "addiu %1, $0, %2\n\t"
  206. "dsrl %1, %1, 1\n\t"
  207. "daddi %0, %1, %3"
  208. : "=r" (v), "=&r" (tmp)
  209. : "I" (0xffffffffffffdb9aUL), "I" (0x1234));
  210. set_except_vector(EXCCODE_OV, handler);
  211. local_irq_restore(flags);
  212. if (daddi_ov) {
  213. pr_cont("yes.\n");
  214. return;
  215. }
  216. pr_cont("no.\n");
  217. panic(bug64hit,
  218. IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS) ? nowar : daddiwar);
  219. }
  220. int daddiu_bug = -1;
  221. static __init void check_daddiu(void)
  222. {
  223. long v, w, tmp;
  224. printk("Checking for the daddiu bug... ");
  225. /*
  226. * The following code leads to a wrong result of daddiu when
  227. * executed on R4400 rev. 1.0 (PRId 00000440).
  228. *
  229. * See "MIPS R4400PC/SC Errata, Processor Revision 1.0" by
  230. * MIPS Technologies, Inc., erratum #7 for details.
  231. *
  232. * According to "MIPS R4000PC/SC Errata, Processor Revision
  233. * 2.2 and 3.0" by MIPS Technologies, Inc., erratum #41 this
  234. * problem affects R4000 rev. 2.2 and 3.0 (PRId 00000422 and
  235. * 00000430, respectively), too. Testing failed to trigger it
  236. * so far.
  237. *
  238. * I got no permission to duplicate the errata here, sigh...
  239. * --macro
  240. */
  241. asm volatile(
  242. ".set push\n\t"
  243. ".set noat\n\t"
  244. ".set noreorder\n\t"
  245. ".set nomacro\n\t"
  246. "addiu %2, $0, %3\n\t"
  247. "dsrl %2, %2, 1\n\t"
  248. #ifdef HAVE_AS_SET_DADDI
  249. ".set daddi\n\t"
  250. #endif
  251. "daddiu %0, %2, %4\n\t"
  252. "addiu %1, $0, %4\n\t"
  253. "daddu %1, %2\n\t"
  254. ".set pop"
  255. : "=&r" (v), "=&r" (w), "=&r" (tmp)
  256. : "I" (0xffffffffffffdb9aUL), "I" (0x1234));
  257. daddiu_bug = v != w;
  258. if (!daddiu_bug) {
  259. pr_cont("no.\n");
  260. return;
  261. }
  262. pr_cont("yes, workaround... ");
  263. asm volatile(
  264. "addiu %2, $0, %3\n\t"
  265. "dsrl %2, %2, 1\n\t"
  266. "daddiu %0, %2, %4\n\t"
  267. "addiu %1, $0, %4\n\t"
  268. "daddu %1, %2"
  269. : "=&r" (v), "=&r" (w), "=&r" (tmp)
  270. : "I" (0xffffffffffffdb9aUL), "I" (0x1234));
  271. if (v == w) {
  272. pr_cont("yes.\n");
  273. return;
  274. }
  275. pr_cont("no.\n");
  276. panic(bug64hit,
  277. IS_ENABLED(CONFIG_CPU_DADDI_WORKAROUNDS) ? nowar : daddiwar);
  278. }
  279. void __init check_bugs64_early(void)
  280. {
  281. check_mult_sh();
  282. check_daddiu();
  283. }
  284. void __init check_bugs64(void)
  285. {
  286. check_daddi();
  287. }