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
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <asm/btfixup.h>
11 #include <asm/page.h>
12 #include <asm/pgalloc.h>
13 #include <asm/pgtable.h>
14 #include <asm/oplib.h>
15 #include <asm/cacheflush.h>
16
17 #define BTFIXUP_OPTIMIZE_NOP
18 #define BTFIXUP_OPTIMIZE_OTHER
19
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 static char str_sun4c[] __initdata = "sun4c\n";
23 static char str_srmmu[] __initdata = "srmmu[%s]/";
24 static char str_iommu[] __initdata = "iommu\n";
25 static char str_iounit[] __initdata = "io-unit\n";
26
27 static int visited __initdata = 0;
28 extern unsigned int ___btfixup_start[], ___btfixup_end[], __init_begin[], __init_end[], __init_text_end[];
29 extern unsigned int _stext[], _end[], __start___ksymtab[], __stop___ksymtab[];
30 static char wrong_f[] __initdata = "Trying to set f fixup %p to invalid function %08x\n";
31 static char wrong_b[] __initdata = "Trying to set b fixup %p to invalid function %08x\n";
32 static char wrong_s[] __initdata = "Trying to set s fixup %p to invalid value %08x\n";
33 static char wrong_h[] __initdata = "Trying to set h fixup %p to invalid value %08x\n";
34 static char wrong_a[] __initdata = "Trying to set a fixup %p to invalid value %08x\n";
35 static char wrong[] __initdata = "Wrong address for %c fixup %p\n";
36 static char insn_f[] __initdata = "Fixup f %p refers to weird instructions at %p[%08x,%08x]\n";
37 static char insn_b[] __initdata = "Fixup b %p doesn't refer to a SETHI at %p[%08x]\n";
38 static char insn_s[] __initdata = "Fixup s %p doesn't refer to an OR at %p[%08x]\n";
39 static char insn_h[] __initdata = "Fixup h %p doesn't refer to a SETHI at %p[%08x]\n";
40 static char insn_a[] __initdata = "Fixup a %p doesn't refer to a SETHI nor OR at %p[%08x]\n";
41 static char insn_i[] __initdata = "Fixup i %p doesn't refer to a valid instruction at %p[%08x]\n";
42 static char fca_und[] __initdata = "flush_cache_all undefined in btfixup()\n";
43 static char wrong_setaddr[] __initdata = "Garbled CALL/INT patch at %p[%08x,%08x,%08x]=%08x\n";
44
45 #ifdef BTFIXUP_OPTIMIZE_OTHER
set_addr(unsigned int * addr,unsigned int q1,int fmangled,unsigned int value)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
set_addr(unsigned int * addr,unsigned int q1,int fmangled,unsigned int value)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
btfixup(void)72 void __init btfixup(void)
73 {
74 unsigned int *p, *q;
75 int type, count;
76 unsigned insn;
77 unsigned *addr;
78 int fmangled = 0;
79 void (*flush_cacheall)(void);
80
81 if (!visited) {
82 visited++;
83 printk(version);
84 if (ARCH_SUN4C)
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
192 displac = (displac >> 21) + 2;
193 *addr = (0x10800000) + (displac & 0x3fffff);
194 q[1] = addr[1];
195 addr[1] = p[2];
196 break;
197 }
198 if ((addr[1] & 0x201f) == 0x200f || (addr[1] & 0x7c000) == 0x3c000)
199 goto norm_f; /* Someone is playing bad tricks with us: rs1 or rs2 is o7 */
200 if ((addr[1] & 0x3e000000) == 0x1e000000)
201 goto norm_f; /* rd is %o7. We'd better take care. */
202 }
203 if (p[2] == BTFIXUPCALL_NOP) {
204 *addr = 0x01000000;
205 q[1] = 1;
206 break;
207 }
208 #ifndef BTFIXUP_OPTIMIZE_OTHER
209 goto norm_f;
210 #else
211 if (addr[1] == 0x01000000) { /* NOP in the delay slot */
212 q[1] = addr[1];
213 *addr = p[2];
214 break;
215 }
216 if ((addr[1] & 0xc0000000) != 0xc0000000) {
217 /* Not a memory operation */
218 if ((addr[1] & 0x30000000) == 0x10000000) {
219 /* Ok, non-memory op with rd %oX */
220 if ((addr[1] & 0x3e000000) == 0x1c000000)
221 goto bad_f; /* Aiee. Someone is playing strange %sp tricks */
222 if ((addr[1] & 0x3e000000) > 0x12000000 ||
223 ((addr[1] & 0x3e000000) == 0x12000000 &&
224 p[2] != BTFIXUPCALL_STO1O0 && p[2] != BTFIXUPCALL_SWAPO0O1) ||
225 ((p[2] & 0xffffe000) == BTFIXUPCALL_RETINT(0))) {
226 /* Nobody uses the result. We can nop it out. */
227 *addr = p[2];
228 q[1] = addr[1];
229 addr[1] = 0x01000000;
230 break;
231 }
232 if ((addr[1] & 0xf1ffffe0) == 0x90100000) {
233 /* MOV %reg, %Ox */
234 if ((addr[1] & 0x3e000000) == 0x10000000 &&
235 (p[2] & 0x7c000) == 0x20000) {
236 /* Ok, it is call xx; mov reg, %o0 and call optimizes
237 to doing something on %o0. Patch the patch. */
238 *addr = (p[2] & ~0x7c000) | ((addr[1] & 0x1f) << 14);
239 q[1] = addr[1];
240 addr[1] = 0x01000000;
241 break;
242 }
243 if ((addr[1] & 0x3e000000) == 0x12000000 &&
244 p[2] == BTFIXUPCALL_STO1O0) {
245 *addr = (p[2] & ~0x3e000000) | ((addr[1] & 0x1f) << 25);
246 q[1] = addr[1];
247 addr[1] = 0x01000000;
248 break;
249 }
250 }
251 }
252 }
253 *addr = addr[1];
254 q[1] = addr[1];
255 addr[1] = p[2];
256 break;
257 #endif /* BTFIXUP_OPTIMIZE_OTHER */
258 #endif /* BTFIXUP_OPTIMIZE_NOP */
259 case 'b': /* BLACKBOX */
260 /* Has to be sethi i, xx */
261 if ((insn & 0xc1c00000) != 0x01000000) {
262 prom_printf(insn_b, p, addr, insn);
263 prom_halt();
264 } else {
265 void (*do_fixup)(unsigned *);
266
267 do_fixup = (void (*)(unsigned *))p[1];
268 do_fixup(addr);
269 }
270 break;
271 case 's': /* SIMM13 */
272 /* Has to be or %g0, i, xx */
273 if ((insn & 0xc1ffe000) != 0x80102000) {
274 prom_printf(insn_s, p, addr, insn);
275 prom_halt();
276 }
277 set_addr(addr, q[1], fmangled, (insn & 0xffffe000) | (p[1] & 0x1fff));
278 break;
279 case 'h': /* SETHI */
280 /* Has to be sethi i, xx */
281 if ((insn & 0xc1c00000) != 0x01000000) {
282 prom_printf(insn_h, p, addr, insn);
283 prom_halt();
284 }
285 set_addr(addr, q[1], fmangled, (insn & 0xffc00000) | (p[1] >> 10));
286 break;
287 case 'a': /* HALF */
288 /* Has to be sethi i, xx or or %g0, i, xx */
289 if ((insn & 0xc1c00000) != 0x01000000 &&
290 (insn & 0xc1ffe000) != 0x80102000) {
291 prom_printf(insn_a, p, addr, insn);
292 prom_halt();
293 }
294 if (p[1] & 0x3ff)
295 set_addr(addr, q[1], fmangled,
296 (insn & 0x3e000000) | 0x80102000 | (p[1] & 0x1fff));
297 else
298 set_addr(addr, q[1], fmangled,
299 (insn & 0x3e000000) | 0x01000000 | (p[1] >> 10));
300 break;
301 case 'i': /* INT */
302 if ((insn & 0xc1c00000) == 0x01000000) /* %HI */
303 set_addr(addr, q[1], fmangled, (insn & 0xffc00000) | (p[1] >> 10));
304 else if ((insn & 0x80002000) == 0x80002000) /* %LO */
305 set_addr(addr, q[1], fmangled, (insn & 0xffffe000) | (p[1] & 0x3ff));
306 else {
307 prom_printf(insn_i, p, addr, insn);
308 prom_halt();
309 }
310 break;
311 }
312 count -= 2;
313 q += 2;
314 }
315 } else
316 p = q + count;
317 }
318 #ifdef CONFIG_SMP
319 flush_cacheall = (void (*)(void))BTFIXUPVAL_CALL(local_flush_cache_all);
320 #else
321 flush_cacheall = (void (*)(void))BTFIXUPVAL_CALL(flush_cache_all);
322 #endif
323 if (!flush_cacheall) {
324 prom_printf(fca_und);
325 prom_halt();
326 }
327 (*flush_cacheall)();
328 }
329