btfixup.c 10 KB

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