Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / arch / powerpc / kernel / module_64.c
blob077d2ce6c5a7c64b2a51404e7e33fbb5004541b9
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
34 them, too.
36 Using a magic allocator which places modules within 32MB solves
37 this, and makes other things simpler. Anton?
38 --RR. */
39 #if 0
40 #define DEBUGP printk
41 #else
42 #define DEBUGP(fmt , ...)
43 #endif
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)
53 return addr;
55 static unsigned long func_addr(unsigned long addr)
57 return addr;
59 static unsigned long stub_func_addr(func_desc_t func)
61 return 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);
77 #else
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)
93 return func.funcaddr;
95 static unsigned int local_entry_offset(const Elf64_Sym *sym)
97 return 0;
99 #endif
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. */
109 u32 jump[7];
110 u32 unused;
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) */
138 #endif
139 0x7d8903a6, /* mtctr r12 */
140 0x4e800420 /* bctr */
143 #ifdef CONFIG_DYNAMIC_FTRACE
145 static u32 ppc64_stub_mask[] = {
146 0xffff0000,
147 0xffff0000,
148 0xffffffff,
149 0xffffffff,
150 #if !defined(_CALL_ELF) || _CALL_ELF != 2
151 0xffffffff,
152 #endif
153 0xffffffff,
154 0xffffffff
157 bool is_module_trampoline(u32 *p)
159 unsigned int i;
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)))
165 return -EFAULT;
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))
173 return false;
176 return true;
179 int module_trampoline_target(struct module *mod, u32 *trampoline,
180 unsigned long *target)
182 u32 buf[2];
183 u16 upper, lower;
184 long offset;
185 void *toc_entry;
187 if (probe_kernel_read(buf, trampoline, sizeof(buf)))
188 return -EFAULT;
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)))
203 return -EFAULT;
205 return 0;
208 #endif
210 /* Count how many different 24-bit relocations (different symbol,
211 different addend) */
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 */
217 _count_relocs = 0;
218 r_info = 0;
219 r_addend = 0;
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)) {
225 _count_relocs++;
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)
245 return -1;
246 else if (x->r_info > y->r_info)
247 return 1;
248 else if (x->r_addend < y->r_addend)
249 return -1;
250 else if (x->r_addend > y->r_addend)
251 return 1;
252 else
253 return 0;
256 static void relaswap(void *_x, void *_y, int size)
258 uint64_t *x, *y, tmp;
259 int i;
261 y = (uint64_t *)_x;
262 x = (uint64_t *)_y;
264 for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) {
265 tmp = x[i];
266 x[i] = y[i];
267 y[i] = tmp;
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;
277 unsigned i;
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,
297 sechdrs[i].sh_size
298 / sizeof(Elf64_Rela));
302 #ifdef CONFIG_DYNAMIC_FTRACE
303 /* make the trampoline to the ftrace_caller */
304 relocs++;
305 #endif
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,
313 unsigned long size)
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)
325 unsigned int i;
327 for (i = 1; i < numsyms; i++) {
328 if (syms[i].st_shndx == SHN_UNDEF) {
329 char *name = strtab + syms[i].st_name;
330 if (name[0] == '.')
331 memmove(name, name+1, strlen(name));
336 static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
337 const char *strtab,
338 unsigned int symindex)
340 unsigned int i, numsyms;
341 Elf64_Sym *syms;
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)
349 return &syms[i];
351 return NULL;
354 int module_frob_arch_sections(Elf64_Ehdr *hdr,
355 Elf64_Shdr *sechdrs,
356 char *secstrings,
357 struct module *me)
359 unsigned int i;
361 /* Find .toc and .stubs sections, symtab and strtab */
362 for (i = 1; i < hdr->e_shnum; i++) {
363 char *p;
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,
370 sechdrs[i].sh_size);
372 /* We don't handle .init for the moment: rename to _init */
373 while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
374 p[0] = '_';
376 if (sechdrs[i].sh_type == SHT_SYMTAB)
377 dedotify((void *)hdr + sechdrs[i].sh_offset,
378 sechdrs[i].sh_size / sizeof(Elf64_Sym),
379 (void *)hdr
380 + sechdrs[sechdrs[i].sh_link].sh_offset);
383 if (!me->arch.stubs_section) {
384 printk("%s: doesn't contain .stubs.\n", me->name);
385 return -ENOEXEC;
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);
397 return 0;
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
402 offset) */
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,
418 unsigned long addr,
419 struct module *me)
421 long reladdr;
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);
430 return 0;
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);
437 return 1;
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,
443 unsigned long addr,
444 struct module *me)
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))
461 return 0;
463 return (unsigned long)&stubs[i];
466 /* We expect a noop next: if it is, replace it with instruction to
467 restore r2. */
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);
473 return 0;
475 /* ld r2,R2_STACK_OFFSET(r1) */
476 *instruction = 0xe8410000 | R2_STACK_OFFSET;
477 return 1;
480 int apply_relocate_add(Elf64_Shdr *sechdrs,
481 const char *strtab,
482 unsigned int symindex,
483 unsigned int relsec,
484 struct module *me)
486 unsigned int i;
487 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
488 Elf64_Sym *sym;
489 unsigned long *location;
490 unsigned long value;
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. */
500 if (sym)
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
508 + rela[i].r_offset;
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)) {
522 case R_PPC64_ADDR32:
523 /* Simply set it */
524 *(u32 *)location = value;
525 break;
527 case R_PPC64_ADDR64:
528 /* Simply set it */
529 *(unsigned long *)location = value;
530 break;
532 case R_PPC64_TOC:
533 *(unsigned long *)location = my_r2(sechdrs, me);
534 break;
536 case R_PPC64_TOC16:
537 /* Subtract TOC pointer */
538 value -= my_r2(sechdrs, me);
539 if (value + 0x8000 > 0xffff) {
540 printk("%s: bad TOC16 relocation (%lu)\n",
541 me->name, value);
542 return -ENOEXEC;
544 *((uint16_t *) location)
545 = (*((uint16_t *) location) & ~0xffff)
546 | (value & 0xffff);
547 break;
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)
554 | (value & 0xffff);
555 break;
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",
562 me->name, value);
563 return -ENOEXEC;
565 *((uint16_t *) location)
566 = (*((uint16_t *) location) & ~0xfffc)
567 | (value & 0xfffc);
568 break;
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",
575 me->name, value);
576 return -ENOEXEC;
578 *((uint16_t *) location)
579 = (*((uint16_t *) location) & ~0xfffc)
580 | (value & 0xfffc);
581 break;
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)
589 | (value & 0xffff);
590 break;
592 case R_PPC_REL24:
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);
597 if (!value)
598 return -ENOENT;
599 if (!restore_r2((u32 *)location + 1, me))
600 return -ENOEXEC;
601 } else
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);
609 return -ENOEXEC;
612 /* Only replace bits 2 through 26 */
613 *(uint32_t *)location
614 = (*(uint32_t *)location & ~0x03fffffc)
615 | (value & 0x03fffffc);
616 break;
618 case R_PPC64_REL64:
619 /* 64 bits relative (used by features fixups) */
620 *location = value - (unsigned long)location;
621 break;
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
627 * it.
629 break;
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)
637 | (value & 0xffff);
638 break;
640 case R_PPC64_REL16_LO:
641 /* Subtract location pointer */
642 value -= (unsigned long)location;
643 *((uint16_t *) location)
644 = (*((uint16_t *) location) & ~0xffff)
645 | (value & 0xffff);
646 break;
648 default:
649 printk("%s: Unknown ADD relocation: %lu\n",
650 me->name,
651 (unsigned long)ELF64_R_TYPE(rela[i].r_info));
652 return -ENOEXEC;
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,
660 me);
661 #endif
663 return 0;