btfixup.c 10.0 KB

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