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