btfixup.c 10 KB

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