1 /* Kernel module help for PPC64.
2 Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <linux/module.h>
19 #include <linux/elf.h>
20 #include <linux/moduleloader.h>
21 #include <linux/err.h>
22 #include <linux/vmalloc.h>
23 #include <linux/ftrace.h>
24 #include <linux/bug.h>
25 #include <linux/uaccess.h>
26 #include <asm/module.h>
27 #include <asm/firmware.h>
28 #include <asm/code-patching.h>
29 #include <linux/sort.h>
30 #include <asm/setup.h>
32 /* FIXME: We don't do .init separately. To do this, we'd need to have
33 a separate r2 value in the init and core section, and stub between
36 Using a magic allocator which places modules within 32MB solves
37 this, and makes other things simpler. Anton?
42 #define DEBUGP(fmt , ...)
45 #if defined(_CALL_ELF) && _CALL_ELF == 2
46 #define R2_STACK_OFFSET 24
48 /* An address is simply the address of the function. */
49 typedef unsigned long func_desc_t
;
51 static func_desc_t
func_desc(unsigned long addr
)
55 static unsigned long func_addr(unsigned long addr
)
59 static unsigned long stub_func_addr(func_desc_t func
)
64 /* PowerPC64 specific values for the Elf64_Sym st_other field. */
65 #define STO_PPC64_LOCAL_BIT 5
66 #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT)
67 #define PPC64_LOCAL_ENTRY_OFFSET(other) \
68 (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
70 static unsigned int local_entry_offset(const Elf64_Sym
*sym
)
72 /* sym->st_other indicates offset to local entry point
73 * (otherwise it will assume r12 is the address of the start
74 * of function and try to derive r2 from it). */
75 return PPC64_LOCAL_ENTRY_OFFSET(sym
->st_other
);
78 #define R2_STACK_OFFSET 40
80 /* An address is address of the OPD entry, which contains address of fn. */
81 typedef struct ppc64_opd_entry func_desc_t
;
83 static func_desc_t
func_desc(unsigned long addr
)
85 return *(struct ppc64_opd_entry
*)addr
;
87 static unsigned long func_addr(unsigned long addr
)
89 return func_desc(addr
).funcaddr
;
91 static unsigned long stub_func_addr(func_desc_t func
)
95 static unsigned int local_entry_offset(const Elf64_Sym
*sym
)
101 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
102 the kernel itself). But on PPC64, these need to be used for every
103 jump, actually, to reset r2 (TOC+0x8000). */
104 struct ppc64_stub_entry
106 /* 28 byte jump instruction sequence (7 instructions). We only
107 * need 6 instructions on ABIv2 but we always allocate 7 so
108 * so we don't have to modify the trampoline load instruction. */
111 /* Data for the above code */
112 func_desc_t funcdata
;
116 * PPC64 uses 24 bit jumps, but we need to jump into other modules or
117 * the kernel which may be further. So we jump to a stub.
119 * For ELFv1 we need to use this to set up the new r2 value (aka TOC
120 * pointer). For ELFv2 it's the callee's responsibility to set up the
121 * new r2, but for both we need to save the old r2.
123 * We could simply patch the new r2 value and function pointer into
124 * the stub, but it's significantly shorter to put these values at the
125 * end of the stub code, and patch the stub address (32-bits relative
126 * to the TOC ptr, r2) into the stub.
129 static u32 ppc64_stub_insns
[] = {
130 0x3d620000, /* addis r11,r2, <high> */
131 0x396b0000, /* addi r11,r11, <low> */
132 /* Save current r2 value in magic place on the stack. */
133 0xf8410000|R2_STACK_OFFSET
, /* std r2,R2_STACK_OFFSET(r1) */
134 0xe98b0020, /* ld r12,32(r11) */
135 #if !defined(_CALL_ELF) || _CALL_ELF != 2
136 /* Set up new r2 from function descriptor */
137 0xe84b0028, /* ld r2,40(r11) */
139 0x7d8903a6, /* mtctr r12 */
140 0x4e800420 /* bctr */
143 #ifdef CONFIG_DYNAMIC_FTRACE
145 static u32 ppc64_stub_mask
[] = {
150 #if !defined(_CALL_ELF) || _CALL_ELF != 2
157 bool is_module_trampoline(u32
*p
)
160 u32 insns
[ARRAY_SIZE(ppc64_stub_insns
)];
162 BUILD_BUG_ON(sizeof(ppc64_stub_insns
) != sizeof(ppc64_stub_mask
));
164 if (probe_kernel_read(insns
, p
, sizeof(insns
)))
167 for (i
= 0; i
< ARRAY_SIZE(ppc64_stub_insns
); i
++) {
168 u32 insna
= insns
[i
];
169 u32 insnb
= ppc64_stub_insns
[i
];
170 u32 mask
= ppc64_stub_mask
[i
];
172 if ((insna
& mask
) != (insnb
& mask
))
179 int module_trampoline_target(struct module
*mod
, u32
*trampoline
,
180 unsigned long *target
)
187 if (probe_kernel_read(buf
, trampoline
, sizeof(buf
)))
190 upper
= buf
[0] & 0xffff;
191 lower
= buf
[1] & 0xffff;
193 /* perform the addis/addi, both signed */
194 offset
= ((short)upper
<< 16) + (short)lower
;
197 * Now get the address this trampoline jumps to. This
198 * is always 32 bytes into our trampoline stub.
200 toc_entry
= (void *)mod
->arch
.toc
+ offset
+ 32;
202 if (probe_kernel_read(target
, toc_entry
, sizeof(*target
)))
210 /* Count how many different 24-bit relocations (different symbol,
212 static unsigned int count_relocs(const Elf64_Rela
*rela
, unsigned int num
)
214 unsigned int i
, r_info
, r_addend
, _count_relocs
;
216 /* FIXME: Only count external ones --RR */
220 for (i
= 0; i
< num
; i
++)
221 /* Only count 24-bit relocs, others don't need stubs */
222 if (ELF64_R_TYPE(rela
[i
].r_info
) == R_PPC_REL24
&&
223 (r_info
!= ELF64_R_SYM(rela
[i
].r_info
) ||
224 r_addend
!= rela
[i
].r_addend
)) {
226 r_info
= ELF64_R_SYM(rela
[i
].r_info
);
227 r_addend
= rela
[i
].r_addend
;
230 return _count_relocs
;
233 static int relacmp(const void *_x
, const void *_y
)
235 const Elf64_Rela
*x
, *y
;
237 y
= (Elf64_Rela
*)_x
;
238 x
= (Elf64_Rela
*)_y
;
240 /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
241 * make the comparison cheaper/faster. It won't affect the sorting or
242 * the counting algorithms' performance
244 if (x
->r_info
< y
->r_info
)
246 else if (x
->r_info
> y
->r_info
)
248 else if (x
->r_addend
< y
->r_addend
)
250 else if (x
->r_addend
> y
->r_addend
)
256 static void relaswap(void *_x
, void *_y
, int size
)
258 uint64_t *x
, *y
, tmp
;
264 for (i
= 0; i
< sizeof(Elf64_Rela
) / sizeof(uint64_t); i
++) {
271 /* Get size of potential trampolines required. */
272 static unsigned long get_stubs_size(const Elf64_Ehdr
*hdr
,
273 const Elf64_Shdr
*sechdrs
)
275 /* One extra reloc so it's always 0-funcaddr terminated */
276 unsigned long relocs
= 1;
279 /* Every relocated section... */
280 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
281 if (sechdrs
[i
].sh_type
== SHT_RELA
) {
282 DEBUGP("Found relocations in section %u\n", i
);
283 DEBUGP("Ptr: %p. Number: %lu\n",
284 (void *)sechdrs
[i
].sh_addr
,
285 sechdrs
[i
].sh_size
/ sizeof(Elf64_Rela
));
287 /* Sort the relocation information based on a symbol and
288 * addend key. This is a stable O(n*log n) complexity
289 * alogrithm but it will reduce the complexity of
290 * count_relocs() to linear complexity O(n)
292 sort((void *)sechdrs
[i
].sh_addr
,
293 sechdrs
[i
].sh_size
/ sizeof(Elf64_Rela
),
294 sizeof(Elf64_Rela
), relacmp
, relaswap
);
296 relocs
+= count_relocs((void *)sechdrs
[i
].sh_addr
,
298 / sizeof(Elf64_Rela
));
302 #ifdef CONFIG_DYNAMIC_FTRACE
303 /* make the trampoline to the ftrace_caller */
307 DEBUGP("Looks like a total of %lu stubs, max\n", relocs
);
308 return relocs
* sizeof(struct ppc64_stub_entry
);
311 /* Still needed for ELFv2, for .TOC. */
312 static void dedotify_versions(struct modversion_info
*vers
,
315 struct modversion_info
*end
;
317 for (end
= (void *)vers
+ size
; vers
< end
; vers
++)
318 if (vers
->name
[0] == '.')
319 memmove(vers
->name
, vers
->name
+1, strlen(vers
->name
));
322 /* Undefined symbols which refer to .funcname, hack to funcname (or .TOC.) */
323 static void dedotify(Elf64_Sym
*syms
, unsigned int numsyms
, char *strtab
)
327 for (i
= 1; i
< numsyms
; i
++) {
328 if (syms
[i
].st_shndx
== SHN_UNDEF
) {
329 char *name
= strtab
+ syms
[i
].st_name
;
331 memmove(name
, name
+1, strlen(name
));
336 static Elf64_Sym
*find_dot_toc(Elf64_Shdr
*sechdrs
,
338 unsigned int symindex
)
340 unsigned int i
, numsyms
;
343 syms
= (Elf64_Sym
*)sechdrs
[symindex
].sh_addr
;
344 numsyms
= sechdrs
[symindex
].sh_size
/ sizeof(Elf64_Sym
);
346 for (i
= 1; i
< numsyms
; i
++) {
347 if (syms
[i
].st_shndx
== SHN_UNDEF
348 && strcmp(strtab
+ syms
[i
].st_name
, "TOC.") == 0)
354 int module_frob_arch_sections(Elf64_Ehdr
*hdr
,
361 /* Find .toc and .stubs sections, symtab and strtab */
362 for (i
= 1; i
< hdr
->e_shnum
; i
++) {
364 if (strcmp(secstrings
+ sechdrs
[i
].sh_name
, ".stubs") == 0)
365 me
->arch
.stubs_section
= i
;
366 else if (strcmp(secstrings
+ sechdrs
[i
].sh_name
, ".toc") == 0)
367 me
->arch
.toc_section
= i
;
368 else if (strcmp(secstrings
+sechdrs
[i
].sh_name
,"__versions")==0)
369 dedotify_versions((void *)hdr
+ sechdrs
[i
].sh_offset
,
372 /* We don't handle .init for the moment: rename to _init */
373 while ((p
= strstr(secstrings
+ sechdrs
[i
].sh_name
, ".init")))
376 if (sechdrs
[i
].sh_type
== SHT_SYMTAB
)
377 dedotify((void *)hdr
+ sechdrs
[i
].sh_offset
,
378 sechdrs
[i
].sh_size
/ sizeof(Elf64_Sym
),
380 + sechdrs
[sechdrs
[i
].sh_link
].sh_offset
);
383 if (!me
->arch
.stubs_section
) {
384 printk("%s: doesn't contain .stubs.\n", me
->name
);
388 /* If we don't have a .toc, just use .stubs. We need to set r2
389 to some reasonable value in case the module calls out to
390 other functions via a stub, or if a function pointer escapes
391 the module by some means. */
392 if (!me
->arch
.toc_section
)
393 me
->arch
.toc_section
= me
->arch
.stubs_section
;
395 /* Override the stubs size */
396 sechdrs
[me
->arch
.stubs_section
].sh_size
= get_stubs_size(hdr
, sechdrs
);
400 /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this
401 gives the value maximum span in an instruction which uses a signed
403 static inline unsigned long my_r2(Elf64_Shdr
*sechdrs
, struct module
*me
)
405 return sechdrs
[me
->arch
.toc_section
].sh_addr
+ 0x8000;
408 /* Both low and high 16 bits are added as SIGNED additions, so if low
409 16 bits has high bit set, high 16 bits must be adjusted. These
410 macros do that (stolen from binutils). */
411 #define PPC_LO(v) ((v) & 0xffff)
412 #define PPC_HI(v) (((v) >> 16) & 0xffff)
413 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
415 /* Patch stub to reference function and correct r2 value. */
416 static inline int create_stub(Elf64_Shdr
*sechdrs
,
417 struct ppc64_stub_entry
*entry
,
423 memcpy(entry
->jump
, ppc64_stub_insns
, sizeof(ppc64_stub_insns
));
425 /* Stub uses address relative to r2. */
426 reladdr
= (unsigned long)entry
- my_r2(sechdrs
, me
);
427 if (reladdr
> 0x7FFFFFFF || reladdr
< -(0x80000000L
)) {
428 printk("%s: Address %p of stub out of range of %p.\n",
429 me
->name
, (void *)reladdr
, (void *)my_r2
);
432 DEBUGP("Stub %p get data from reladdr %li\n", entry
, reladdr
);
434 entry
->jump
[0] |= PPC_HA(reladdr
);
435 entry
->jump
[1] |= PPC_LO(reladdr
);
436 entry
->funcdata
= func_desc(addr
);
440 /* Create stub to jump to function described in this OPD/ptr: we need the
441 stub to set up the TOC ptr (r2) for the function. */
442 static unsigned long stub_for_addr(Elf64_Shdr
*sechdrs
,
446 struct ppc64_stub_entry
*stubs
;
447 unsigned int i
, num_stubs
;
449 num_stubs
= sechdrs
[me
->arch
.stubs_section
].sh_size
/ sizeof(*stubs
);
451 /* Find this stub, or if that fails, the next avail. entry */
452 stubs
= (void *)sechdrs
[me
->arch
.stubs_section
].sh_addr
;
453 for (i
= 0; stub_func_addr(stubs
[i
].funcdata
); i
++) {
454 BUG_ON(i
>= num_stubs
);
456 if (stub_func_addr(stubs
[i
].funcdata
) == func_addr(addr
))
457 return (unsigned long)&stubs
[i
];
460 if (!create_stub(sechdrs
, &stubs
[i
], addr
, me
))
463 return (unsigned long)&stubs
[i
];
466 /* We expect a noop next: if it is, replace it with instruction to
468 static int restore_r2(u32
*instruction
, struct module
*me
)
470 if (*instruction
!= PPC_INST_NOP
) {
471 printk("%s: Expect noop after relocate, got %08x\n",
472 me
->name
, *instruction
);
475 /* ld r2,R2_STACK_OFFSET(r1) */
476 *instruction
= 0xe8410000 | R2_STACK_OFFSET
;
480 int apply_relocate_add(Elf64_Shdr
*sechdrs
,
482 unsigned int symindex
,
487 Elf64_Rela
*rela
= (void *)sechdrs
[relsec
].sh_addr
;
489 unsigned long *location
;
492 DEBUGP("Applying ADD relocate section %u to %u\n", relsec
,
493 sechdrs
[relsec
].sh_info
);
495 /* First time we're called, we can fix up .TOC. */
496 if (!me
->arch
.toc_fixed
) {
497 sym
= find_dot_toc(sechdrs
, strtab
, symindex
);
498 /* It's theoretically possible that a module doesn't want a
499 * .TOC. so don't fail it just for that. */
501 sym
->st_value
= my_r2(sechdrs
, me
);
502 me
->arch
.toc_fixed
= true;
505 for (i
= 0; i
< sechdrs
[relsec
].sh_size
/ sizeof(*rela
); i
++) {
506 /* This is where to make the change */
507 location
= (void *)sechdrs
[sechdrs
[relsec
].sh_info
].sh_addr
509 /* This is the symbol it is referring to */
510 sym
= (Elf64_Sym
*)sechdrs
[symindex
].sh_addr
511 + ELF64_R_SYM(rela
[i
].r_info
);
513 DEBUGP("RELOC at %p: %li-type as %s (%lu) + %li\n",
514 location
, (long)ELF64_R_TYPE(rela
[i
].r_info
),
515 strtab
+ sym
->st_name
, (unsigned long)sym
->st_value
,
516 (long)rela
[i
].r_addend
);
518 /* `Everything is relative'. */
519 value
= sym
->st_value
+ rela
[i
].r_addend
;
521 switch (ELF64_R_TYPE(rela
[i
].r_info
)) {
524 *(u32
*)location
= value
;
529 *(unsigned long *)location
= value
;
533 *(unsigned long *)location
= my_r2(sechdrs
, me
);
537 /* Subtract TOC pointer */
538 value
-= my_r2(sechdrs
, me
);
539 if (value
+ 0x8000 > 0xffff) {
540 printk("%s: bad TOC16 relocation (%lu)\n",
544 *((uint16_t *) location
)
545 = (*((uint16_t *) location
) & ~0xffff)
549 case R_PPC64_TOC16_LO
:
550 /* Subtract TOC pointer */
551 value
-= my_r2(sechdrs
, me
);
552 *((uint16_t *) location
)
553 = (*((uint16_t *) location
) & ~0xffff)
557 case R_PPC64_TOC16_DS
:
558 /* Subtract TOC pointer */
559 value
-= my_r2(sechdrs
, me
);
560 if ((value
& 3) != 0 || value
+ 0x8000 > 0xffff) {
561 printk("%s: bad TOC16_DS relocation (%lu)\n",
565 *((uint16_t *) location
)
566 = (*((uint16_t *) location
) & ~0xfffc)
570 case R_PPC64_TOC16_LO_DS
:
571 /* Subtract TOC pointer */
572 value
-= my_r2(sechdrs
, me
);
573 if ((value
& 3) != 0) {
574 printk("%s: bad TOC16_LO_DS relocation (%lu)\n",
578 *((uint16_t *) location
)
579 = (*((uint16_t *) location
) & ~0xfffc)
583 case R_PPC64_TOC16_HA
:
584 /* Subtract TOC pointer */
585 value
-= my_r2(sechdrs
, me
);
586 value
= ((value
+ 0x8000) >> 16);
587 *((uint16_t *) location
)
588 = (*((uint16_t *) location
) & ~0xffff)
593 /* FIXME: Handle weak symbols here --RR */
594 if (sym
->st_shndx
== SHN_UNDEF
) {
595 /* External: go via stub */
596 value
= stub_for_addr(sechdrs
, value
, me
);
599 if (!restore_r2((u32
*)location
+ 1, me
))
602 value
+= local_entry_offset(sym
);
604 /* Convert value to relative */
605 value
-= (unsigned long)location
;
606 if (value
+ 0x2000000 > 0x3ffffff || (value
& 3) != 0){
607 printk("%s: REL24 %li out of range!\n",
608 me
->name
, (long int)value
);
612 /* Only replace bits 2 through 26 */
613 *(uint32_t *)location
614 = (*(uint32_t *)location
& ~0x03fffffc)
615 | (value
& 0x03fffffc);
619 /* 64 bits relative (used by features fixups) */
620 *location
= value
- (unsigned long)location
;
623 case R_PPC64_TOCSAVE
:
625 * Marker reloc indicates we don't have to save r2.
626 * That would only save us one instruction, so ignore
631 case R_PPC64_REL16_HA
:
632 /* Subtract location pointer */
633 value
-= (unsigned long)location
;
634 value
= ((value
+ 0x8000) >> 16);
635 *((uint16_t *) location
)
636 = (*((uint16_t *) location
) & ~0xffff)
640 case R_PPC64_REL16_LO
:
641 /* Subtract location pointer */
642 value
-= (unsigned long)location
;
643 *((uint16_t *) location
)
644 = (*((uint16_t *) location
) & ~0xffff)
649 printk("%s: Unknown ADD relocation: %lu\n",
651 (unsigned long)ELF64_R_TYPE(rela
[i
].r_info
));
656 #ifdef CONFIG_DYNAMIC_FTRACE
657 me
->arch
.toc
= my_r2(sechdrs
, me
);
658 me
->arch
.tramp
= stub_for_addr(sechdrs
,
659 (unsigned long)ftrace_caller
,