Merge branch 'linux-2.6' into for-linus
[pandora-kernel.git] / arch / powerpc / kernel / module_64.c
1 /*  Kernel module help for PPC64.
2     Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
3
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.
8
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.
13
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
17 */
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 <asm/module.h>
24 #include <asm/uaccess.h>
25 #include <asm/firmware.h>
26
27 #include "setup.h"
28
29 /* FIXME: We don't do .init separately.  To do this, we'd need to have
30    a separate r2 value in the init and core section, and stub between
31    them, too.
32
33    Using a magic allocator which places modules within 32MB solves
34    this, and makes other things simpler.  Anton?
35    --RR.  */
36 #if 0
37 #define DEBUGP printk
38 #else
39 #define DEBUGP(fmt , ...)
40 #endif
41
42 /* There's actually a third entry here, but it's unused */
43 struct ppc64_opd_entry
44 {
45         unsigned long funcaddr;
46         unsigned long r2;
47 };
48
49 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
50    the kernel itself).  But on PPC64, these need to be used for every
51    jump, actually, to reset r2 (TOC+0x8000). */
52 struct ppc64_stub_entry
53 {
54         /* 28 byte jump instruction sequence (7 instructions) */
55         unsigned char jump[28];
56         unsigned char unused[4];
57         /* Data for the above code */
58         struct ppc64_opd_entry opd;
59 };
60
61 /* We use a stub to fix up r2 (TOC ptr) and to jump to the (external)
62    function which may be more than 24-bits away.  We could simply
63    patch the new r2 value and function pointer into the stub, but it's
64    significantly shorter to put these values at the end of the stub
65    code, and patch the stub address (32-bits relative to the TOC ptr,
66    r2) into the stub. */
67 static struct ppc64_stub_entry ppc64_stub =
68 { .jump = {
69         0x3d, 0x82, 0x00, 0x00, /* addis   r12,r2, <high> */
70         0x39, 0x8c, 0x00, 0x00, /* addi    r12,r12, <low> */
71         /* Save current r2 value in magic place on the stack. */
72         0xf8, 0x41, 0x00, 0x28, /* std     r2,40(r1) */
73         0xe9, 0x6c, 0x00, 0x20, /* ld      r11,32(r12) */
74         0xe8, 0x4c, 0x00, 0x28, /* ld      r2,40(r12) */
75         0x7d, 0x69, 0x03, 0xa6, /* mtctr   r11 */
76         0x4e, 0x80, 0x04, 0x20  /* bctr */
77 } };
78
79 /* Count how many different 24-bit relocations (different symbol,
80    different addend) */
81 static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
82 {
83         unsigned int i, j, ret = 0;
84
85         /* FIXME: Only count external ones --RR */
86         /* Sure, this is order(n^2), but it's usually short, and not
87            time critical */
88         for (i = 0; i < num; i++) {
89                 /* Only count 24-bit relocs, others don't need stubs */
90                 if (ELF64_R_TYPE(rela[i].r_info) != R_PPC_REL24)
91                         continue;
92                 for (j = 0; j < i; j++) {
93                         /* If this addend appeared before, it's
94                            already been counted */
95                         if (rela[i].r_info == rela[j].r_info
96                             && rela[i].r_addend == rela[j].r_addend)
97                                 break;
98                 }
99                 if (j == i) ret++;
100         }
101         return ret;
102 }
103
104 void *module_alloc(unsigned long size)
105 {
106         if (size == 0)
107                 return NULL;
108
109         return vmalloc_exec(size);
110 }
111
112 /* Free memory returned from module_alloc */
113 void module_free(struct module *mod, void *module_region)
114 {
115         vfree(module_region);
116         /* FIXME: If module_region == mod->init_region, trim exception
117            table entries. */
118 }
119
120 /* Get size of potential trampolines required. */
121 static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
122                                     const Elf64_Shdr *sechdrs)
123 {
124         /* One extra reloc so it's always 0-funcaddr terminated */
125         unsigned long relocs = 1;
126         unsigned i;
127
128         /* Every relocated section... */
129         for (i = 1; i < hdr->e_shnum; i++) {
130                 if (sechdrs[i].sh_type == SHT_RELA) {
131                         DEBUGP("Found relocations in section %u\n", i);
132                         DEBUGP("Ptr: %p.  Number: %lu\n",
133                                (void *)sechdrs[i].sh_addr,
134                                sechdrs[i].sh_size / sizeof(Elf64_Rela));
135                         relocs += count_relocs((void *)sechdrs[i].sh_addr,
136                                                sechdrs[i].sh_size
137                                                / sizeof(Elf64_Rela));
138                 }
139         }
140
141         DEBUGP("Looks like a total of %lu stubs, max\n", relocs);
142         return relocs * sizeof(struct ppc64_stub_entry);
143 }
144
145 static void dedotify_versions(struct modversion_info *vers,
146                               unsigned long size)
147 {
148         struct modversion_info *end;
149
150         for (end = (void *)vers + size; vers < end; vers++)
151                 if (vers->name[0] == '.')
152                         memmove(vers->name, vers->name+1, strlen(vers->name));
153 }
154
155 /* Undefined symbols which refer to .funcname, hack to funcname */
156 static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
157 {
158         unsigned int i;
159
160         for (i = 1; i < numsyms; i++) {
161                 if (syms[i].st_shndx == SHN_UNDEF) {
162                         char *name = strtab + syms[i].st_name;
163                         if (name[0] == '.')
164                                 memmove(name, name+1, strlen(name));
165                 }
166         }
167 }
168
169 int module_frob_arch_sections(Elf64_Ehdr *hdr,
170                               Elf64_Shdr *sechdrs,
171                               char *secstrings,
172                               struct module *me)
173 {
174         unsigned int i;
175
176         /* Find .toc and .stubs sections, symtab and strtab */
177         for (i = 1; i < hdr->e_shnum; i++) {
178                 char *p;
179                 if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
180                         me->arch.stubs_section = i;
181                 else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0)
182                         me->arch.toc_section = i;
183                 else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
184                         dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
185                                           sechdrs[i].sh_size);
186
187                 /* We don't handle .init for the moment: rename to _init */
188                 while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
189                         p[0] = '_';
190
191                 if (sechdrs[i].sh_type == SHT_SYMTAB)
192                         dedotify((void *)hdr + sechdrs[i].sh_offset,
193                                  sechdrs[i].sh_size / sizeof(Elf64_Sym),
194                                  (void *)hdr
195                                  + sechdrs[sechdrs[i].sh_link].sh_offset);
196         }
197
198         if (!me->arch.stubs_section) {
199                 printk("%s: doesn't contain .stubs.\n", me->name);
200                 return -ENOEXEC;
201         }
202
203         /* If we don't have a .toc, just use .stubs.  We need to set r2
204            to some reasonable value in case the module calls out to
205            other functions via a stub, or if a function pointer escapes
206            the module by some means.  */
207         if (!me->arch.toc_section)
208                 me->arch.toc_section = me->arch.stubs_section;
209
210         /* Override the stubs size */
211         sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
212         return 0;
213 }
214
215 int apply_relocate(Elf64_Shdr *sechdrs,
216                    const char *strtab,
217                    unsigned int symindex,
218                    unsigned int relsec,
219                    struct module *me)
220 {
221         printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n", me->name);
222         return -ENOEXEC;
223 }
224
225 /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this
226    gives the value maximum span in an instruction which uses a signed
227    offset) */
228 static inline unsigned long my_r2(Elf64_Shdr *sechdrs, struct module *me)
229 {
230         return sechdrs[me->arch.toc_section].sh_addr + 0x8000;
231 }
232
233 /* Both low and high 16 bits are added as SIGNED additions, so if low
234    16 bits has high bit set, high 16 bits must be adjusted.  These
235    macros do that (stolen from binutils). */
236 #define PPC_LO(v) ((v) & 0xffff)
237 #define PPC_HI(v) (((v) >> 16) & 0xffff)
238 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
239
240 /* Patch stub to reference function and correct r2 value. */
241 static inline int create_stub(Elf64_Shdr *sechdrs,
242                               struct ppc64_stub_entry *entry,
243                               struct ppc64_opd_entry *opd,
244                               struct module *me)
245 {
246         Elf64_Half *loc1, *loc2;
247         long reladdr;
248
249         *entry = ppc64_stub;
250
251         loc1 = (Elf64_Half *)&entry->jump[2];
252         loc2 = (Elf64_Half *)&entry->jump[6];
253
254         /* Stub uses address relative to r2. */
255         reladdr = (unsigned long)entry - my_r2(sechdrs, me);
256         if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
257                 printk("%s: Address %p of stub out of range of %p.\n",
258                        me->name, (void *)reladdr, (void *)my_r2);
259                 return 0;
260         }
261         DEBUGP("Stub %p get data from reladdr %li\n", entry, reladdr);
262
263         *loc1 = PPC_HA(reladdr);
264         *loc2 = PPC_LO(reladdr);
265         entry->opd.funcaddr = opd->funcaddr;
266         entry->opd.r2 = opd->r2;
267         return 1;
268 }
269
270 /* Create stub to jump to function described in this OPD: we need the
271    stub to set up the TOC ptr (r2) for the function. */
272 static unsigned long stub_for_addr(Elf64_Shdr *sechdrs,
273                                    unsigned long opdaddr,
274                                    struct module *me)
275 {
276         struct ppc64_stub_entry *stubs;
277         struct ppc64_opd_entry *opd = (void *)opdaddr;
278         unsigned int i, num_stubs;
279
280         num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
281
282         /* Find this stub, or if that fails, the next avail. entry */
283         stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
284         for (i = 0; stubs[i].opd.funcaddr; i++) {
285                 BUG_ON(i >= num_stubs);
286
287                 if (stubs[i].opd.funcaddr == opd->funcaddr)
288                         return (unsigned long)&stubs[i];
289         }
290
291         if (!create_stub(sechdrs, &stubs[i], opd, me))
292                 return 0;
293
294         return (unsigned long)&stubs[i];
295 }
296
297 /* We expect a noop next: if it is, replace it with instruction to
298    restore r2. */
299 static int restore_r2(u32 *instruction, struct module *me)
300 {
301         if (*instruction != 0x60000000) {
302                 printk("%s: Expect noop after relocate, got %08x\n",
303                        me->name, *instruction);
304                 return 0;
305         }
306         *instruction = 0xe8410028;      /* ld r2,40(r1) */
307         return 1;
308 }
309
310 int apply_relocate_add(Elf64_Shdr *sechdrs,
311                        const char *strtab,
312                        unsigned int symindex,
313                        unsigned int relsec,
314                        struct module *me)
315 {
316         unsigned int i;
317         Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
318         Elf64_Sym *sym;
319         unsigned long *location;
320         unsigned long value;
321
322         DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
323                sechdrs[relsec].sh_info);
324         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
325                 /* This is where to make the change */
326                 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
327                         + rela[i].r_offset;
328                 /* This is the symbol it is referring to */
329                 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
330                         + ELF64_R_SYM(rela[i].r_info);
331
332                 DEBUGP("RELOC at %p: %li-type as %s (%lu) + %li\n",
333                        location, (long)ELF64_R_TYPE(rela[i].r_info),
334                        strtab + sym->st_name, (unsigned long)sym->st_value,
335                        (long)rela[i].r_addend);
336
337                 /* `Everything is relative'. */
338                 value = sym->st_value + rela[i].r_addend;
339
340                 switch (ELF64_R_TYPE(rela[i].r_info)) {
341                 case R_PPC64_ADDR32:
342                         /* Simply set it */
343                         *(u32 *)location = value;
344                         break;
345                         
346                 case R_PPC64_ADDR64:
347                         /* Simply set it */
348                         *(unsigned long *)location = value;
349                         break;
350
351                 case R_PPC64_TOC:
352                         *(unsigned long *)location = my_r2(sechdrs, me);
353                         break;
354
355                 case R_PPC64_TOC16:
356                         /* Subtract TOC pointer */
357                         value -= my_r2(sechdrs, me);
358                         if (value + 0x8000 > 0xffff) {
359                                 printk("%s: bad TOC16 relocation (%lu)\n",
360                                        me->name, value);
361                                 return -ENOEXEC;
362                         }
363                         *((uint16_t *) location)
364                                 = (*((uint16_t *) location) & ~0xffff)
365                                 | (value & 0xffff);
366                         break;
367
368                 case R_PPC64_TOC16_DS:
369                         /* Subtract TOC pointer */
370                         value -= my_r2(sechdrs, me);
371                         if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
372                                 printk("%s: bad TOC16_DS relocation (%lu)\n",
373                                        me->name, value);
374                                 return -ENOEXEC;
375                         }
376                         *((uint16_t *) location)
377                                 = (*((uint16_t *) location) & ~0xfffc)
378                                 | (value & 0xfffc);
379                         break;
380
381                 case R_PPC_REL24:
382                         /* FIXME: Handle weak symbols here --RR */
383                         if (sym->st_shndx == SHN_UNDEF) {
384                                 /* External: go via stub */
385                                 value = stub_for_addr(sechdrs, value, me);
386                                 if (!value)
387                                         return -ENOENT;
388                                 if (!restore_r2((u32 *)location + 1, me))
389                                         return -ENOEXEC;
390                         }
391
392                         /* Convert value to relative */
393                         value -= (unsigned long)location;
394                         if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
395                                 printk("%s: REL24 %li out of range!\n",
396                                        me->name, (long int)value);
397                                 return -ENOEXEC;
398                         }
399
400                         /* Only replace bits 2 through 26 */
401                         *(uint32_t *)location 
402                                 = (*(uint32_t *)location & ~0x03fffffc)
403                                 | (value & 0x03fffffc);
404                         break;
405
406                 case R_PPC64_REL64:
407                         /* 64 bits relative (used by features fixups) */
408                         *location = value - (unsigned long)location;
409                         break;
410
411                 default:
412                         printk("%s: Unknown ADD relocation: %lu\n",
413                                me->name,
414                                (unsigned long)ELF64_R_TYPE(rela[i].r_info));
415                         return -ENOEXEC;
416                 }
417         }
418
419         return 0;
420 }
421
422 LIST_HEAD(module_bug_list);
423
424 static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
425                                     const Elf_Shdr *sechdrs,
426                                     const char *name)
427 {
428         char *secstrings;
429         unsigned int i;
430
431         secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
432         for (i = 1; i < hdr->e_shnum; i++)
433                 if (strcmp(secstrings+sechdrs[i].sh_name, name) == 0)
434                         return &sechdrs[i];
435         return NULL;
436 }
437
438 int module_finalize(const Elf_Ehdr *hdr,
439                 const Elf_Shdr *sechdrs, struct module *me)
440 {
441         const Elf_Shdr *sect;
442
443         me->arch.bug_table = NULL;
444         me->arch.num_bugs = 0;
445
446         /* Find the __bug_table section, if present */
447         sect = find_section(hdr, sechdrs, "__bug_table");
448         if (sect != NULL) {
449                 me->arch.bug_table = (void *) sect->sh_addr;
450                 me->arch.num_bugs = sect->sh_size / sizeof(struct bug_entry);
451         }
452
453         /*
454          * Strictly speaking this should have a spinlock to protect against
455          * traversals, but since we only traverse on BUG()s, a spinlock
456          * could potentially lead to deadlock and thus be counter-productive.
457          */
458         list_add(&me->arch.bug_list, &module_bug_list);
459
460         /* Apply feature fixups */
461         sect = find_section(hdr, sechdrs, "__ftr_fixup");
462         if (sect != NULL)
463                 do_feature_fixups(cur_cpu_spec->cpu_features,
464                                   (void *)sect->sh_addr,
465                                   (void *)sect->sh_addr + sect->sh_size);
466
467         sect = find_section(hdr, sechdrs, "__fw_ftr_fixup");
468         if (sect != NULL)
469                 do_feature_fixups(powerpc_firmware_features,
470                                   (void *)sect->sh_addr,
471                                   (void *)sect->sh_addr + sect->sh_size);
472
473         return 0;
474 }
475
476 void module_arch_cleanup(struct module *mod)
477 {
478         list_del(&mod->arch.bug_list);
479 }
480
481 struct bug_entry *module_find_bug(unsigned long bugaddr)
482 {
483         struct mod_arch_specific *mod;
484         unsigned int i;
485         struct bug_entry *bug;
486
487         list_for_each_entry(mod, &module_bug_list, bug_list) {
488                 bug = mod->bug_table;
489                 for (i = 0; i < mod->num_bugs; ++i, ++bug)
490                         if (bugaddr == bug->bug_addr)
491                                 return bug;
492         }
493         return NULL;
494 }