btfixup.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* btfixup.c: Boot time code fixup and relocator, so that
  2. * we can get rid of most indirect calls to achieve single
  3. * image sun4c and srmmu kernel.
  4. *
  5. * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/init.h>
  9. #include <asm/btfixup.h>
  10. #include <asm/page.h>
  11. #include <asm/pgalloc.h>
  12. #include <asm/pgtable.h>
  13. #include <asm/oplib.h>
  14. #include <asm/cacheflush.h>
  15. #define BTFIXUP_OPTIMIZE_NOP
  16. #define BTFIXUP_OPTIMIZE_OTHER
  17. extern char *srmmu_name;
  18. static char version[] __initdata = "Boot time fixup v1.6. 4/Mar/98 Jakub Jelinek (jj@ultra.linux.cz). Patching kernel for ";
  19. static char str_srmmu[] __initdata = "srmmu[%s]/";
  20. static char str_iommu[] __initdata = "iommu\n";
  21. static char str_iounit[] __initdata = "io-unit\n";
  22. static int visited __initdata = 0;
  23. extern unsigned int ___btfixup_start[], ___btfixup_end[], __init_begin[], __init_end[], __init_text_end[];
  24. extern unsigned int _stext[], _end[], __start___ksymtab[], __stop___ksymtab[];
  25. static char wrong_f[] __initdata = "Trying to set f fixup %p to invalid function %08x\n";
  26. static char wrong_b[] __initdata = "Trying to set b fixup %p to invalid function %08x\n";
  27. static char wrong_s[] __initdata = "Trying to set s fixup %p to invalid value %08x\n";
  28. static char wrong_h[] __initdata = "Trying to set h fixup %p to invalid value %08x\n";
  29. static char wrong_a[] __initdata = "Trying to set a fixup %p to invalid value %08x\n";
  30. static char wrong[] __initdata = "Wrong address for %c fixup %p\n";
  31. static char insn_f[] __initdata = "Fixup f %p refers to weird instructions at %p[%08x,%08x]\n";
  32. static char insn_b[] __initdata = "Fixup b %p doesn't refer to a SETHI at %p[%08x]\n";
  33. static char insn_s[] __initdata = "Fixup s %p doesn't refer to an OR at %p[%08x]\n";
  34. static char insn_h[] __initdata = "Fixup h %p doesn't refer to a SETHI at %p[%08x]\n";
  35. static char insn_a[] __initdata = "Fixup a %p doesn't refer to a SETHI nor OR at %p[%08x]\n";
  36. static char insn_i[] __initdata = "Fixup i %p doesn't refer to a valid instruction at %p[%08x]\n";
  37. static char wrong_setaddr[] __initdata = "Garbled CALL/INT patch at %p[%08x,%08x,%08x]=%08x\n";
  38. #ifdef BTFIXUP_OPTIMIZE_OTHER
  39. static void __init set_addr(unsigned int *addr, unsigned int q1, int fmangled, unsigned int value)
  40. {
  41. if (!fmangled)
  42. *addr = value;
  43. else {
  44. unsigned int *q = (unsigned int *)q1;
  45. if (*addr == 0x01000000) {
  46. /* Noped */
  47. *q = value;
  48. } else if (addr[-1] == *q) {
  49. /* Moved */
  50. addr[-1] = value;
  51. *q = value;
  52. } else {
  53. prom_printf(wrong_setaddr, addr-1, addr[-1], *addr, *q, value);
  54. prom_halt();
  55. }
  56. }
  57. }
  58. #else
  59. static inline void set_addr(unsigned int *addr, unsigned int q1, int fmangled, unsigned int value)
  60. {
  61. *addr = value;
  62. }
  63. #endif
  64. void __init btfixup(void)
  65. {
  66. unsigned int *p, *q;
  67. int type, count;
  68. unsigned insn;
  69. unsigned *addr;
  70. int fmangled = 0;
  71. if (!visited) {
  72. visited++;
  73. printk(version);
  74. printk(str_srmmu, srmmu_name);
  75. if (sparc_cpu_model == sun4d)
  76. printk(str_iounit);
  77. else
  78. printk(str_iommu);
  79. }
  80. for (p = ___btfixup_start; p < ___btfixup_end; ) {
  81. count = p[2];
  82. q = p + 3;
  83. switch (type = *(unsigned char *)p) {
  84. case 'f':
  85. count = p[3];
  86. q = p + 4;
  87. if (((p[0] & 1) || p[1])
  88. && ((p[1] & 3) || (unsigned *)(p[1]) < _stext || (unsigned *)(p[1]) >= _end)) {
  89. prom_printf(wrong_f, p, p[1]);
  90. prom_halt();
  91. }
  92. break;
  93. case 'b':
  94. if (p[1] < (unsigned long)__init_begin || p[1] >= (unsigned long)__init_text_end || (p[1] & 3)) {
  95. prom_printf(wrong_b, p, p[1]);
  96. prom_halt();
  97. }
  98. break;
  99. case 's':
  100. if (p[1] + 0x1000 >= 0x2000) {
  101. prom_printf(wrong_s, p, p[1]);
  102. prom_halt();
  103. }
  104. break;
  105. case 'h':
  106. if (p[1] & 0x3ff) {
  107. prom_printf(wrong_h, p, p[1]);
  108. prom_halt();
  109. }
  110. break;
  111. case 'a':
  112. if (p[1] + 0x1000 >= 0x2000 && (p[1] & 0x3ff)) {
  113. prom_printf(wrong_a, p, p[1]);
  114. prom_halt();
  115. }
  116. break;
  117. }
  118. if (p[0] & 1) {
  119. p[0] &= ~1;
  120. while (count) {
  121. fmangled = 0;
  122. addr = (unsigned *)*q;
  123. if (addr < _stext || addr >= _end) {
  124. prom_printf(wrong, type, p);
  125. prom_halt();
  126. }
  127. insn = *addr;
  128. #ifdef BTFIXUP_OPTIMIZE_OTHER
  129. if (type != 'f' && q[1]) {
  130. insn = *(unsigned int *)q[1];
  131. if (!insn || insn == 1)
  132. insn = *addr;
  133. else
  134. fmangled = 1;
  135. }
  136. #endif
  137. switch (type) {
  138. case 'f': /* CALL */
  139. if (addr >= __start___ksymtab && addr < __stop___ksymtab) {
  140. *addr = p[1];
  141. break;
  142. } else if (!q[1]) {
  143. if ((insn & 0xc1c00000) == 0x01000000) { /* SETHI */
  144. *addr = (insn & 0xffc00000) | (p[1] >> 10); break;
  145. } else if ((insn & 0xc1f82000) == 0x80102000) { /* OR X, %LO(i), Y */
  146. *addr = (insn & 0xffffe000) | (p[1] & 0x3ff); break;
  147. } else if ((insn & 0xc0000000) != 0x40000000) { /* !CALL */
  148. bad_f:
  149. prom_printf(insn_f, p, addr, insn, addr[1]);
  150. prom_halt();
  151. }
  152. } else if (q[1] != 1)
  153. addr[1] = q[1];
  154. if (p[2] == BTFIXUPCALL_NORM) {
  155. norm_f:
  156. *addr = 0x40000000 | ((p[1] - (unsigned)addr) >> 2);
  157. q[1] = 0;
  158. break;
  159. }
  160. #ifndef BTFIXUP_OPTIMIZE_NOP
  161. goto norm_f;
  162. #else
  163. if (!(addr[1] & 0x80000000)) {
  164. if ((addr[1] & 0xc1c00000) != 0x01000000) /* !SETHI */
  165. goto bad_f; /* CALL, Bicc, FBfcc, CBccc are weird in delay slot, aren't they? */
  166. } else {
  167. if ((addr[1] & 0x01800000) == 0x01800000) {
  168. if ((addr[1] & 0x01f80000) == 0x01e80000) {
  169. /* RESTORE */
  170. goto norm_f; /* It is dangerous to patch that */
  171. }
  172. goto bad_f;
  173. }
  174. if ((addr[1] & 0xffffe003) == 0x9e03e000) {
  175. /* ADD %O7, XX, %o7 */
  176. int displac = (addr[1] << 19);
  177. displac = (displac >> 21) + 2;
  178. *addr = (0x10800000) + (displac & 0x3fffff);
  179. q[1] = addr[1];
  180. addr[1] = p[2];
  181. break;
  182. }
  183. if ((addr[1] & 0x201f) == 0x200f || (addr[1] & 0x7c000) == 0x3c000)
  184. goto norm_f; /* Someone is playing bad tricks with us: rs1 or rs2 is o7 */
  185. if ((addr[1] & 0x3e000000) == 0x1e000000)
  186. goto norm_f; /* rd is %o7. We'd better take care. */
  187. }
  188. if (p[2] == BTFIXUPCALL_NOP) {
  189. *addr = 0x01000000;
  190. q[1] = 1;
  191. break;
  192. }
  193. #ifndef BTFIXUP_OPTIMIZE_OTHER
  194. goto norm_f;
  195. #else
  196. if (addr[1] == 0x01000000) { /* NOP in the delay slot */
  197. q[1] = addr[1];
  198. *addr = p[2];
  199. break;
  200. }
  201. if ((addr[1] & 0xc0000000) != 0xc0000000) {
  202. /* Not a memory operation */
  203. if ((addr[1] & 0x30000000) == 0x10000000) {
  204. /* Ok, non-memory op with rd %oX */
  205. if ((addr[1] & 0x3e000000) == 0x1c000000)
  206. goto bad_f; /* Aiee. Someone is playing strange %sp tricks */
  207. if ((addr[1] & 0x3e000000) > 0x12000000 ||
  208. ((addr[1] & 0x3e000000) == 0x12000000 &&
  209. p[2] != BTFIXUPCALL_STO1O0 && p[2] != BTFIXUPCALL_SWAPO0O1) ||
  210. ((p[2] & 0xffffe000) == BTFIXUPCALL_RETINT(0))) {
  211. /* Nobody uses the result. We can nop it out. */
  212. *addr = p[2];
  213. q[1] = addr[1];
  214. addr[1] = 0x01000000;
  215. break;
  216. }
  217. if ((addr[1] & 0xf1ffffe0) == 0x90100000) {
  218. /* MOV %reg, %Ox */
  219. if ((addr[1] & 0x3e000000) == 0x10000000 &&
  220. (p[2] & 0x7c000) == 0x20000) {
  221. /* Ok, it is call xx; mov reg, %o0 and call optimizes
  222. to doing something on %o0. Patch the patch. */
  223. *addr = (p[2] & ~0x7c000) | ((addr[1] & 0x1f) << 14);
  224. q[1] = addr[1];
  225. addr[1] = 0x01000000;
  226. break;
  227. }
  228. if ((addr[1] & 0x3e000000) == 0x12000000 &&
  229. p[2] == BTFIXUPCALL_STO1O0) {
  230. *addr = (p[2] & ~0x3e000000) | ((addr[1] & 0x1f) << 25);
  231. q[1] = addr[1];
  232. addr[1] = 0x01000000;
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. *addr = addr[1];
  239. q[1] = addr[1];
  240. addr[1] = p[2];
  241. break;
  242. #endif /* BTFIXUP_OPTIMIZE_OTHER */
  243. #endif /* BTFIXUP_OPTIMIZE_NOP */
  244. case 'b': /* BLACKBOX */
  245. /* Has to be sethi i, xx */
  246. if ((insn & 0xc1c00000) != 0x01000000) {
  247. prom_printf(insn_b, p, addr, insn);
  248. prom_halt();
  249. } else {
  250. void (*do_fixup)(unsigned *);
  251. do_fixup = (void (*)(unsigned *))p[1];
  252. do_fixup(addr);
  253. }
  254. break;
  255. case 's': /* SIMM13 */
  256. /* Has to be or %g0, i, xx */
  257. if ((insn & 0xc1ffe000) != 0x80102000) {
  258. prom_printf(insn_s, p, addr, insn);
  259. prom_halt();
  260. }
  261. set_addr(addr, q[1], fmangled, (insn & 0xffffe000) | (p[1] & 0x1fff));
  262. break;
  263. case 'h': /* SETHI */
  264. /* Has to be sethi i, xx */
  265. if ((insn & 0xc1c00000) != 0x01000000) {
  266. prom_printf(insn_h, p, addr, insn);
  267. prom_halt();
  268. }
  269. set_addr(addr, q[1], fmangled, (insn & 0xffc00000) | (p[1] >> 10));
  270. break;
  271. case 'a': /* HALF */
  272. /* Has to be sethi i, xx or or %g0, i, xx */
  273. if ((insn & 0xc1c00000) != 0x01000000 &&
  274. (insn & 0xc1ffe000) != 0x80102000) {
  275. prom_printf(insn_a, p, addr, insn);
  276. prom_halt();
  277. }
  278. if (p[1] & 0x3ff)
  279. set_addr(addr, q[1], fmangled,
  280. (insn & 0x3e000000) | 0x80102000 | (p[1] & 0x1fff));
  281. else
  282. set_addr(addr, q[1], fmangled,
  283. (insn & 0x3e000000) | 0x01000000 | (p[1] >> 10));
  284. break;
  285. case 'i': /* INT */
  286. if ((insn & 0xc1c00000) == 0x01000000) /* %HI */
  287. set_addr(addr, q[1], fmangled, (insn & 0xffc00000) | (p[1] >> 10));
  288. else if ((insn & 0x80002000) == 0x80002000) /* %LO */
  289. set_addr(addr, q[1], fmangled, (insn & 0xffffe000) | (p[1] & 0x3ff));
  290. else {
  291. prom_printf(insn_i, p, addr, insn);
  292. prom_halt();
  293. }
  294. break;
  295. }
  296. count -= 2;
  297. q += 2;
  298. }
  299. } else
  300. p = q + count;
  301. }
  302. #ifdef CONFIG_SMP
  303. local_ops->cache_all();
  304. #else
  305. sparc32_cachetlb_ops->cache_all();
  306. #endif
  307. }