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.
6 * Copyright (C) 1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <asm/btfixup.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>
20 #define BTFIXUP_OPTIMIZE_NOP
21 #define BTFIXUP_OPTIMIZE_OTHER
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 ";
26 static char str_sun4c
[] __initdata
= "sun4\n";
28 static char str_sun4c
[] __initdata
= "sun4c\n";
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";
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";
52 #ifdef BTFIXUP_OPTIMIZE_OTHER
53 static void __init
set_addr(unsigned int *addr
, unsigned int q1
, int fmangled
, unsigned int value
)
58 unsigned int *q
= (unsigned int *)q1
;
59 if (*addr
== 0x01000000) {
62 } else if (addr
[-1] == *q
) {
67 prom_printf(wrong_setaddr
, addr
-1, addr
[-1], *addr
, *q
, value
);
73 static __inline__
void set_addr(unsigned int *addr
, unsigned int q1
, int fmangled
, unsigned int value
)
79 void __init
btfixup(void)
86 void (*flush_cacheall
)(void);
94 printk(str_srmmu
, srmmu_name
);
95 if (sparc_cpu_model
== sun4d
)
101 for (p
= ___btfixup_start
; p
< ___btfixup_end
; ) {
104 switch (type
= *(unsigned char *)p
) {
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]);
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]);
121 if (p
[1] + 0x1000 >= 0x2000) {
122 prom_printf(wrong_s
, p
, p
[1]);
128 prom_printf(wrong_h
, p
, p
[1]);
133 if (p
[1] + 0x1000 >= 0x2000 && (p
[1] & 0x3ff)) {
134 prom_printf(wrong_a
, p
, p
[1]);
143 addr
= (unsigned *)*q
;
144 if (addr
< _stext
|| addr
>= _end
) {
145 prom_printf(wrong
, type
, p
);
149 #ifdef BTFIXUP_OPTIMIZE_OTHER
150 if (type
!= 'f' && q
[1]) {
151 insn
= *(unsigned int *)q
[1];
152 if (!insn
|| insn
== 1)
160 if (addr
>= __start___ksymtab
&& addr
< __stop___ksymtab
) {
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 */
170 prom_printf(insn_f
, p
, addr
, insn
, addr
[1]);
173 } else if (q
[1] != 1)
175 if (p
[2] == BTFIXUPCALL_NORM
) {
177 *addr
= 0x40000000 | ((p
[1] - (unsigned)addr
) >> 2);
181 #ifndef BTFIXUP_OPTIMIZE_NOP
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? */
188 if ((addr
[1] & 0x01800000) == 0x01800000) {
189 if ((addr
[1] & 0x01f80000) == 0x01e80000) {
191 goto norm_f
; /* It is dangerous to patch that */
195 if ((addr
[1] & 0xffffe003) == 0x9e03e000) {
196 /* ADD %O7, XX, %o7 */
197 int displac
= (addr
[1] << 19);
199 displac
= (displac
>> 21) + 2;
200 *addr
= (0x10800000) + (displac
& 0x3fffff);
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. */
210 if (p
[2] == BTFIXUPCALL_NOP
) {
215 #ifndef BTFIXUP_OPTIMIZE_OTHER
218 if (addr
[1] == 0x01000000) { /* NOP in the delay slot */
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. */
236 addr
[1] = 0x01000000;
239 if ((addr
[1] & 0xf1ffffe0) == 0x90100000) {
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);
247 addr
[1] = 0x01000000;
250 if ((addr
[1] & 0x3e000000) == 0x12000000 &&
251 p
[2] == BTFIXUPCALL_STO1O0
) {
252 *addr
= (p
[2] & ~0x3e000000) | ((addr
[1] & 0x1f) << 25);
254 addr
[1] = 0x01000000;
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
);
272 void (*do_fixup
)(unsigned *);
274 do_fixup
= (void (*)(unsigned *))p
[1];
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
);
284 set_addr(addr
, q
[1], fmangled
, (insn
& 0xffffe000) | (p
[1] & 0x1fff));
286 case 'h': /* SETHI */
287 /* Has to be sethi i, xx */
288 if ((insn
& 0xc1c00000) != 0x01000000) {
289 prom_printf(insn_h
, p
, addr
, insn
);
292 set_addr(addr
, q
[1], fmangled
, (insn
& 0xffc00000) | (p
[1] >> 10));
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
);
302 set_addr(addr
, q
[1], fmangled
,
303 (insn
& 0x3e000000) | 0x80102000 | (p
[1] & 0x1fff));
305 set_addr(addr
, q
[1], fmangled
,
306 (insn
& 0x3e000000) | 0x01000000 | (p
[1] >> 10));
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));
315 prom_printf(insn_i
, p
, addr
, insn
);
327 flush_cacheall
= (void (*)(void))BTFIXUPVAL_CALL(local_flush_cache_all
);
329 flush_cacheall
= (void (*)(void))BTFIXUPVAL_CALL(flush_cache_all
);
331 if (!flush_cacheall
) {
332 prom_printf(fca_und
);