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