Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* Kernel module help for sparc64. |
| 2 | * |
| 3 | * Copyright (C) 2001 Rusty Russell. |
| 4 | * Copyright (C) 2002 David S. Miller. |
| 5 | */ |
| 6 | |
| 7 | #include <linux/moduleloader.h> |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/elf.h> |
| 10 | #include <linux/vmalloc.h> |
| 11 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/string.h> |
Sam Ravnborg | e6b04fe | 2008-12-26 15:38:17 -0800 | [diff] [blame] | 14 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mm.h> |
| 16 | |
| 17 | #include <asm/processor.h> |
| 18 | #include <asm/spitfire.h> |
| 19 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 20 | #ifdef CONFIG_SPARC64 |
David S. Miller | dff9d3c | 2010-09-17 11:09:25 -0400 | [diff] [blame] | 21 | |
| 22 | #include <linux/jump_label.h> |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static void *module_map(unsigned long size) |
| 25 | { |
| 26 | struct vm_struct *area; |
| 27 | |
| 28 | size = PAGE_ALIGN(size); |
| 29 | if (!size || size > MODULES_LEN) |
| 30 | return NULL; |
| 31 | |
| 32 | area = __get_vm_area(size, VM_ALLOC, MODULES_VADDR, MODULES_END); |
| 33 | if (!area) |
| 34 | return NULL; |
| 35 | |
| 36 | return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL); |
| 37 | } |
| 38 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 39 | static char *dot2underscore(char *name) |
| 40 | { |
| 41 | return name; |
| 42 | } |
Sam Ravnborg | e6b04fe | 2008-12-26 15:38:17 -0800 | [diff] [blame] | 43 | #else |
| 44 | static void *module_map(unsigned long size) |
| 45 | { |
| 46 | return vmalloc(size); |
| 47 | } |
| 48 | |
| 49 | /* Replace references to .func with _Func */ |
| 50 | static char *dot2underscore(char *name) |
| 51 | { |
| 52 | if (name[0] == '.') { |
| 53 | name[0] = '_'; |
| 54 | name[1] = toupper(name[1]); |
| 55 | } |
| 56 | return name; |
| 57 | } |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 58 | #endif /* CONFIG_SPARC64 */ |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | void *module_alloc(unsigned long size) |
| 61 | { |
| 62 | void *ret; |
| 63 | |
| 64 | /* We handle the zero case fine, unlike vmalloc */ |
| 65 | if (size == 0) |
| 66 | return NULL; |
| 67 | |
| 68 | ret = module_map(size); |
| 69 | if (!ret) |
| 70 | ret = ERR_PTR(-ENOMEM); |
| 71 | else |
| 72 | memset(ret, 0, size); |
| 73 | |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | /* Free memory returned from module_core_alloc/module_init_alloc */ |
| 78 | void module_free(struct module *mod, void *module_region) |
| 79 | { |
| 80 | vfree(module_region); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* Make generic code ignore STT_REGISTER dummy undefined symbols. */ |
| 84 | int module_frob_arch_sections(Elf_Ehdr *hdr, |
| 85 | Elf_Shdr *sechdrs, |
| 86 | char *secstrings, |
| 87 | struct module *mod) |
| 88 | { |
| 89 | unsigned int symidx; |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 90 | Elf_Sym *sym; |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 91 | char *strtab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | int i; |
| 93 | |
| 94 | for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) { |
| 95 | if (symidx == hdr->e_shnum-1) { |
| 96 | printk("%s: no symtab found.\n", mod->name); |
| 97 | return -ENOEXEC; |
| 98 | } |
| 99 | } |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 100 | sym = (Elf_Sym *)sechdrs[symidx].sh_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr; |
| 102 | |
| 103 | for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) { |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 104 | if (sym[i].st_shndx == SHN_UNDEF) { |
| 105 | if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER) { |
| 106 | sym[i].st_shndx = SHN_ABS; |
| 107 | } else { |
| 108 | char *name = strtab + sym[i].st_name; |
| 109 | dot2underscore(name); |
| 110 | } |
| 111 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 116 | int apply_relocate(Elf_Shdr *sechdrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | const char *strtab, |
| 118 | unsigned int symindex, |
| 119 | unsigned int relsec, |
| 120 | struct module *me) |
| 121 | { |
| 122 | printk(KERN_ERR "module %s: non-ADD RELOCATION unsupported\n", |
| 123 | me->name); |
| 124 | return -ENOEXEC; |
| 125 | } |
| 126 | |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 127 | int apply_relocate_add(Elf_Shdr *sechdrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | const char *strtab, |
| 129 | unsigned int symindex, |
| 130 | unsigned int relsec, |
| 131 | struct module *me) |
| 132 | { |
| 133 | unsigned int i; |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 134 | Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr; |
| 135 | Elf_Sym *sym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | u8 *location; |
| 137 | u32 *loc32; |
| 138 | |
| 139 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 140 | Elf_Addr v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | /* This is where to make the change */ |
| 143 | location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 144 | + rel[i].r_offset; |
| 145 | loc32 = (u32 *) location; |
| 146 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 147 | #ifdef CONFIG_SPARC64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | BUG_ON(((u64)location >> (u64)32) != (u64)0); |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 149 | #endif /* CONFIG_SPARC64 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | /* This is the symbol it is referring to. Note that all |
| 152 | undefined symbols have been resolved. */ |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 153 | sym = (Elf_Sym *)sechdrs[symindex].sh_addr |
| 154 | + ELF_R_SYM(rel[i].r_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | v = sym->st_value + rel[i].r_addend; |
| 156 | |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 157 | switch (ELF_R_TYPE(rel[i].r_info) & 0xff) { |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 158 | #ifdef CONFIG_SPARC64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | case R_SPARC_64: |
| 160 | location[0] = v >> 56; |
| 161 | location[1] = v >> 48; |
| 162 | location[2] = v >> 40; |
| 163 | location[3] = v >> 32; |
| 164 | location[4] = v >> 24; |
| 165 | location[5] = v >> 16; |
| 166 | location[6] = v >> 8; |
| 167 | location[7] = v >> 0; |
| 168 | break; |
| 169 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 170 | case R_SPARC_DISP32: |
| 171 | v -= (Elf_Addr) location; |
| 172 | *loc32 = v; |
| 173 | break; |
| 174 | |
| 175 | case R_SPARC_WDISP19: |
| 176 | v -= (Elf_Addr) location; |
| 177 | *loc32 = (*loc32 & ~0x7ffff) | |
| 178 | ((v >> 2) & 0x7ffff); |
| 179 | break; |
| 180 | |
| 181 | case R_SPARC_OLO10: |
| 182 | *loc32 = (*loc32 & ~0x1fff) | |
| 183 | (((v & 0x3ff) + |
| 184 | (ELF_R_TYPE(rel[i].r_info) >> 8)) |
| 185 | & 0x1fff); |
| 186 | break; |
| 187 | #endif /* CONFIG_SPARC64 */ |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | case R_SPARC_32: |
Sam Ravnborg | e6b04fe | 2008-12-26 15:38:17 -0800 | [diff] [blame] | 190 | case R_SPARC_UA32: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | location[0] = v >> 24; |
| 192 | location[1] = v >> 16; |
| 193 | location[2] = v >> 8; |
| 194 | location[3] = v >> 0; |
| 195 | break; |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | case R_SPARC_WDISP30: |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 198 | v -= (Elf_Addr) location; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | *loc32 = (*loc32 & ~0x3fffffff) | |
| 200 | ((v >> 2) & 0x3fffffff); |
| 201 | break; |
| 202 | |
| 203 | case R_SPARC_WDISP22: |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 204 | v -= (Elf_Addr) location; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | *loc32 = (*loc32 & ~0x3fffff) | |
| 206 | ((v >> 2) & 0x3fffff); |
| 207 | break; |
| 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | case R_SPARC_LO10: |
| 210 | *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff); |
| 211 | break; |
| 212 | |
| 213 | case R_SPARC_HI22: |
| 214 | *loc32 = (*loc32 & ~0x3fffff) | |
| 215 | ((v >> 10) & 0x3fffff); |
| 216 | break; |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | default: |
| 219 | printk(KERN_ERR "module %s: Unknown relocation: %x\n", |
| 220 | me->name, |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 221 | (int) (ELF_R_TYPE(rel[i].r_info) & 0xff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return -ENOEXEC; |
| 223 | }; |
| 224 | } |
| 225 | return 0; |
| 226 | } |
| 227 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 228 | #ifdef CONFIG_SPARC64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | int module_finalize(const Elf_Ehdr *hdr, |
| 230 | const Elf_Shdr *sechdrs, |
| 231 | struct module *me) |
| 232 | { |
David S. Miller | dff9d3c | 2010-09-17 11:09:25 -0400 | [diff] [blame] | 233 | /* make jump label nops */ |
| 234 | jump_label_apply_nops(me); |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* Cheetah's I-cache is fully coherent. */ |
| 237 | if (tlb_type == spitfire) { |
| 238 | unsigned long va; |
| 239 | |
| 240 | flushw_all(); |
| 241 | for (va = 0; va < (PAGE_SIZE << 1); va += 32) |
| 242 | spitfire_put_icache_tag(va, 0x0); |
| 243 | __asm__ __volatile__("flush %g6"); |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
Sam Ravnborg | e6b04fe | 2008-12-26 15:38:17 -0800 | [diff] [blame] | 248 | #else |
| 249 | int module_finalize(const Elf_Ehdr *hdr, |
| 250 | const Elf_Shdr *sechdrs, |
| 251 | struct module *me) |
| 252 | { |
| 253 | return 0; |
| 254 | } |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 255 | #endif /* CONFIG_SPARC64 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | void module_arch_cleanup(struct module *mod) |
| 258 | { |
| 259 | } |