btfixup.c 10 KB

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