Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 106 | return vmalloc_exec(size); |
| 107 | } |
| 108 | |
| 109 | /* Free memory returned from module_alloc */ |
| 110 | void module_free(struct module *mod, void *module_region) |
| 111 | { |
| 112 | vfree(module_region); |
| 113 | /* FIXME: If module_region == mod->init_region, trim exception |
| 114 | table entries. */ |
| 115 | } |
| 116 | |
| 117 | /* Get size of potential trampolines required. */ |
| 118 | static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, |
| 119 | const Elf64_Shdr *sechdrs) |
| 120 | { |
| 121 | /* One extra reloc so it's always 0-funcaddr terminated */ |
| 122 | unsigned long relocs = 1; |
| 123 | unsigned i; |
| 124 | |
| 125 | /* Every relocated section... */ |
| 126 | for (i = 1; i < hdr->e_shnum; i++) { |
| 127 | if (sechdrs[i].sh_type == SHT_RELA) { |
| 128 | DEBUGP("Found relocations in section %u\n", i); |
| 129 | DEBUGP("Ptr: %p. Number: %lu\n", |
| 130 | (void *)sechdrs[i].sh_addr, |
| 131 | sechdrs[i].sh_size / sizeof(Elf64_Rela)); |
| 132 | relocs += count_relocs((void *)sechdrs[i].sh_addr, |
| 133 | sechdrs[i].sh_size |
| 134 | / sizeof(Elf64_Rela)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | DEBUGP("Looks like a total of %lu stubs, max\n", relocs); |
| 139 | return relocs * sizeof(struct ppc64_stub_entry); |
| 140 | } |
| 141 | |
| 142 | static void dedotify_versions(struct modversion_info *vers, |
| 143 | unsigned long size) |
| 144 | { |
| 145 | struct modversion_info *end; |
| 146 | |
| 147 | for (end = (void *)vers + size; vers < end; vers++) |
| 148 | if (vers->name[0] == '.') |
| 149 | memmove(vers->name, vers->name+1, strlen(vers->name)); |
| 150 | } |
| 151 | |
| 152 | /* Undefined symbols which refer to .funcname, hack to funcname */ |
| 153 | static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab) |
| 154 | { |
| 155 | unsigned int i; |
| 156 | |
| 157 | for (i = 1; i < numsyms; i++) { |
| 158 | if (syms[i].st_shndx == SHN_UNDEF) { |
| 159 | char *name = strtab + syms[i].st_name; |
| 160 | if (name[0] == '.') |
| 161 | memmove(name, name+1, strlen(name)); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | int module_frob_arch_sections(Elf64_Ehdr *hdr, |
| 167 | Elf64_Shdr *sechdrs, |
| 168 | char *secstrings, |
| 169 | struct module *me) |
| 170 | { |
| 171 | unsigned int i; |
| 172 | |
| 173 | /* Find .toc and .stubs sections, symtab and strtab */ |
| 174 | for (i = 1; i < hdr->e_shnum; i++) { |
| 175 | char *p; |
| 176 | if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0) |
| 177 | me->arch.stubs_section = i; |
| 178 | else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) |
| 179 | me->arch.toc_section = i; |
| 180 | else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) |
| 181 | dedotify_versions((void *)hdr + sechdrs[i].sh_offset, |
| 182 | sechdrs[i].sh_size); |
| 183 | |
| 184 | /* We don't handle .init for the moment: rename to _init */ |
| 185 | while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init"))) |
| 186 | p[0] = '_'; |
| 187 | |
| 188 | if (sechdrs[i].sh_type == SHT_SYMTAB) |
| 189 | dedotify((void *)hdr + sechdrs[i].sh_offset, |
| 190 | sechdrs[i].sh_size / sizeof(Elf64_Sym), |
| 191 | (void *)hdr |
| 192 | + sechdrs[sechdrs[i].sh_link].sh_offset); |
| 193 | } |
| 194 | if (!me->arch.stubs_section || !me->arch.toc_section) { |
| 195 | printk("%s: doesn't contain .toc or .stubs.\n", me->name); |
| 196 | return -ENOEXEC; |
| 197 | } |
| 198 | |
| 199 | /* Override the stubs size */ |
| 200 | sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs); |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int apply_relocate(Elf64_Shdr *sechdrs, |
| 205 | const char *strtab, |
| 206 | unsigned int symindex, |
| 207 | unsigned int relsec, |
| 208 | struct module *me) |
| 209 | { |
| 210 | printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n", me->name); |
| 211 | return -ENOEXEC; |
| 212 | } |
| 213 | |
| 214 | /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this |
| 215 | gives the value maximum span in an instruction which uses a signed |
| 216 | offset) */ |
| 217 | static inline unsigned long my_r2(Elf64_Shdr *sechdrs, struct module *me) |
| 218 | { |
| 219 | return sechdrs[me->arch.toc_section].sh_addr + 0x8000; |
| 220 | } |
| 221 | |
| 222 | /* Both low and high 16 bits are added as SIGNED additions, so if low |
| 223 | 16 bits has high bit set, high 16 bits must be adjusted. These |
| 224 | macros do that (stolen from binutils). */ |
| 225 | #define PPC_LO(v) ((v) & 0xffff) |
| 226 | #define PPC_HI(v) (((v) >> 16) & 0xffff) |
| 227 | #define PPC_HA(v) PPC_HI ((v) + 0x8000) |
| 228 | |
| 229 | /* Patch stub to reference function and correct r2 value. */ |
| 230 | static inline int create_stub(Elf64_Shdr *sechdrs, |
| 231 | struct ppc64_stub_entry *entry, |
| 232 | struct ppc64_opd_entry *opd, |
| 233 | struct module *me) |
| 234 | { |
| 235 | Elf64_Half *loc1, *loc2; |
| 236 | long reladdr; |
| 237 | |
| 238 | *entry = ppc64_stub; |
| 239 | |
| 240 | loc1 = (Elf64_Half *)&entry->jump[2]; |
| 241 | loc2 = (Elf64_Half *)&entry->jump[6]; |
| 242 | |
| 243 | /* Stub uses address relative to r2. */ |
| 244 | reladdr = (unsigned long)entry - my_r2(sechdrs, me); |
| 245 | if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { |
| 246 | printk("%s: Address %p of stub out of range of %p.\n", |
| 247 | me->name, (void *)reladdr, (void *)my_r2); |
| 248 | return 0; |
| 249 | } |
| 250 | DEBUGP("Stub %p get data from reladdr %li\n", entry, reladdr); |
| 251 | |
| 252 | *loc1 = PPC_HA(reladdr); |
| 253 | *loc2 = PPC_LO(reladdr); |
| 254 | entry->opd.funcaddr = opd->funcaddr; |
| 255 | entry->opd.r2 = opd->r2; |
| 256 | return 1; |
| 257 | } |
| 258 | |
| 259 | /* Create stub to jump to function described in this OPD: we need the |
| 260 | stub to set up the TOC ptr (r2) for the function. */ |
| 261 | static unsigned long stub_for_addr(Elf64_Shdr *sechdrs, |
| 262 | unsigned long opdaddr, |
| 263 | struct module *me) |
| 264 | { |
| 265 | struct ppc64_stub_entry *stubs; |
| 266 | struct ppc64_opd_entry *opd = (void *)opdaddr; |
| 267 | unsigned int i, num_stubs; |
| 268 | |
| 269 | num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs); |
| 270 | |
| 271 | /* Find this stub, or if that fails, the next avail. entry */ |
| 272 | stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr; |
| 273 | for (i = 0; stubs[i].opd.funcaddr; i++) { |
| 274 | BUG_ON(i >= num_stubs); |
| 275 | |
| 276 | if (stubs[i].opd.funcaddr == opd->funcaddr) |
| 277 | return (unsigned long)&stubs[i]; |
| 278 | } |
| 279 | |
| 280 | if (!create_stub(sechdrs, &stubs[i], opd, me)) |
| 281 | return 0; |
| 282 | |
| 283 | return (unsigned long)&stubs[i]; |
| 284 | } |
| 285 | |
| 286 | /* We expect a noop next: if it is, replace it with instruction to |
| 287 | restore r2. */ |
| 288 | static int restore_r2(u32 *instruction, struct module *me) |
| 289 | { |
| 290 | if (*instruction != 0x60000000) { |
| 291 | printk("%s: Expect noop after relocate, got %08x\n", |
| 292 | me->name, *instruction); |
| 293 | return 0; |
| 294 | } |
| 295 | *instruction = 0xe8410028; /* ld r2,40(r1) */ |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | int apply_relocate_add(Elf64_Shdr *sechdrs, |
| 300 | const char *strtab, |
| 301 | unsigned int symindex, |
| 302 | unsigned int relsec, |
| 303 | struct module *me) |
| 304 | { |
| 305 | unsigned int i; |
| 306 | Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; |
| 307 | Elf64_Sym *sym; |
| 308 | unsigned long *location; |
| 309 | unsigned long value; |
| 310 | |
| 311 | DEBUGP("Applying ADD relocate section %u to %u\n", relsec, |
| 312 | sechdrs[relsec].sh_info); |
| 313 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { |
| 314 | /* This is where to make the change */ |
| 315 | location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 316 | + rela[i].r_offset; |
| 317 | /* This is the symbol it is referring to */ |
| 318 | sym = (Elf64_Sym *)sechdrs[symindex].sh_addr |
| 319 | + ELF64_R_SYM(rela[i].r_info); |
| 320 | |
| 321 | DEBUGP("RELOC at %p: %li-type as %s (%lu) + %li\n", |
| 322 | location, (long)ELF64_R_TYPE(rela[i].r_info), |
| 323 | strtab + sym->st_name, (unsigned long)sym->st_value, |
| 324 | (long)rela[i].r_addend); |
| 325 | |
| 326 | /* `Everything is relative'. */ |
| 327 | value = sym->st_value + rela[i].r_addend; |
| 328 | |
| 329 | switch (ELF64_R_TYPE(rela[i].r_info)) { |
| 330 | case R_PPC64_ADDR32: |
| 331 | /* Simply set it */ |
| 332 | *(u32 *)location = value; |
| 333 | break; |
| 334 | |
| 335 | case R_PPC64_ADDR64: |
| 336 | /* Simply set it */ |
| 337 | *(unsigned long *)location = value; |
| 338 | break; |
| 339 | |
| 340 | case R_PPC64_TOC: |
| 341 | *(unsigned long *)location = my_r2(sechdrs, me); |
| 342 | break; |
| 343 | |
| 344 | case R_PPC64_TOC16_DS: |
| 345 | /* Subtact TOC pointer */ |
| 346 | value -= my_r2(sechdrs, me); |
| 347 | if ((value & 3) != 0 || value + 0x8000 > 0xffff) { |
| 348 | printk("%s: bad TOC16_DS relocation (%lu)\n", |
| 349 | me->name, value); |
| 350 | return -ENOEXEC; |
| 351 | } |
| 352 | *((uint16_t *) location) |
| 353 | = (*((uint16_t *) location) & ~0xfffc) |
| 354 | | (value & 0xfffc); |
| 355 | break; |
| 356 | |
| 357 | case R_PPC_REL24: |
| 358 | /* FIXME: Handle weak symbols here --RR */ |
| 359 | if (sym->st_shndx == SHN_UNDEF) { |
| 360 | /* External: go via stub */ |
| 361 | value = stub_for_addr(sechdrs, value, me); |
| 362 | if (!value) |
| 363 | return -ENOENT; |
| 364 | if (!restore_r2((u32 *)location + 1, me)) |
| 365 | return -ENOEXEC; |
| 366 | } |
| 367 | |
| 368 | /* Convert value to relative */ |
| 369 | value -= (unsigned long)location; |
| 370 | if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ |
| 371 | printk("%s: REL24 %li out of range!\n", |
| 372 | me->name, (long int)value); |
| 373 | return -ENOEXEC; |
| 374 | } |
| 375 | |
| 376 | /* Only replace bits 2 through 26 */ |
| 377 | *(uint32_t *)location |
| 378 | = (*(uint32_t *)location & ~0x03fffffc) |
| 379 | | (value & 0x03fffffc); |
| 380 | break; |
| 381 | |
| 382 | default: |
| 383 | printk("%s: Unknown ADD relocation: %lu\n", |
| 384 | me->name, |
| 385 | (unsigned long)ELF64_R_TYPE(rela[i].r_info)); |
| 386 | return -ENOEXEC; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | LIST_HEAD(module_bug_list); |
| 394 | |
| 395 | int module_finalize(const Elf_Ehdr *hdr, |
| 396 | const Elf_Shdr *sechdrs, struct module *me) |
| 397 | { |
| 398 | char *secstrings; |
| 399 | unsigned int i; |
| 400 | |
| 401 | me->arch.bug_table = NULL; |
| 402 | me->arch.num_bugs = 0; |
| 403 | |
| 404 | /* Find the __bug_table section, if present */ |
| 405 | secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
| 406 | for (i = 1; i < hdr->e_shnum; i++) { |
| 407 | if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table")) |
| 408 | continue; |
| 409 | me->arch.bug_table = (void *) sechdrs[i].sh_addr; |
| 410 | me->arch.num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry); |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Strictly speaking this should have a spinlock to protect against |
| 416 | * traversals, but since we only traverse on BUG()s, a spinlock |
| 417 | * could potentially lead to deadlock and thus be counter-productive. |
| 418 | */ |
| 419 | list_add(&me->arch.bug_list, &module_bug_list); |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | void module_arch_cleanup(struct module *mod) |
| 425 | { |
| 426 | list_del(&mod->arch.bug_list); |
| 427 | } |
| 428 | |
| 429 | struct bug_entry *module_find_bug(unsigned long bugaddr) |
| 430 | { |
| 431 | struct mod_arch_specific *mod; |
| 432 | unsigned int i; |
| 433 | struct bug_entry *bug; |
| 434 | |
| 435 | list_for_each_entry(mod, &module_bug_list, bug_list) { |
| 436 | bug = mod->bug_table; |
| 437 | for (i = 0; i < mod->num_bugs; ++i, ++bug) |
| 438 | if (bugaddr == bug->bug_addr) |
| 439 | return bug; |
| 440 | } |
| 441 | return NULL; |
| 442 | } |