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> |
David Howells | d550bbd | 2012-03-28 18:30:03 +0100 | [diff] [blame] | 19 | #include <asm/cacheflush.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
David S. Miller | 0b64120 | 2011-11-17 22:44:58 -0800 | [diff] [blame] | 21 | #include "entry.h" |
| 22 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 23 | #ifdef CONFIG_SPARC64 |
David S. Miller | dff9d3c | 2010-09-17 11:09:25 -0400 | [diff] [blame] | 24 | |
| 25 | #include <linux/jump_label.h> |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | static void *module_map(unsigned long size) |
| 28 | { |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 29 | if (PAGE_ALIGN(size) > MODULES_LEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | return NULL; |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 31 | return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, |
Jianguo Wu | 40c3baa | 2013-11-12 15:07:27 -0800 | [diff] [blame] | 32 | GFP_KERNEL, PAGE_KERNEL, NUMA_NO_NODE, |
David Rientjes | d0a2126 | 2011-01-13 15:46:02 -0800 | [diff] [blame] | 33 | __builtin_return_address(0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | } |
Sam Ravnborg | e6b04fe | 2008-12-26 15:38:17 -0800 | [diff] [blame] | 35 | #else |
| 36 | static void *module_map(unsigned long size) |
| 37 | { |
| 38 | return vmalloc(size); |
| 39 | } |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 40 | #endif /* CONFIG_SPARC64 */ |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | void *module_alloc(unsigned long size) |
| 43 | { |
| 44 | void *ret; |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | ret = module_map(size); |
Wei Yongjun | a9e8d1a | 2012-09-20 18:57:51 +0000 | [diff] [blame] | 47 | if (ret) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | memset(ret, 0, size); |
| 49 | |
| 50 | return ret; |
| 51 | } |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* Make generic code ignore STT_REGISTER dummy undefined symbols. */ |
| 54 | int module_frob_arch_sections(Elf_Ehdr *hdr, |
| 55 | Elf_Shdr *sechdrs, |
| 56 | char *secstrings, |
| 57 | struct module *mod) |
| 58 | { |
| 59 | unsigned int symidx; |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 60 | Elf_Sym *sym; |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 61 | char *strtab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int i; |
| 63 | |
| 64 | for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) { |
| 65 | if (symidx == hdr->e_shnum-1) { |
| 66 | printk("%s: no symtab found.\n", mod->name); |
| 67 | return -ENOEXEC; |
| 68 | } |
| 69 | } |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 70 | sym = (Elf_Sym *)sechdrs[symidx].sh_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr; |
| 72 | |
| 73 | for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) { |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 74 | if (sym[i].st_shndx == SHN_UNDEF) { |
David S. Miller | 1b35a57 | 2012-05-15 11:23:01 -0700 | [diff] [blame] | 75 | if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER) |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 76 | sym[i].st_shndx = SHN_ABS; |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 77 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 82 | int apply_relocate_add(Elf_Shdr *sechdrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | const char *strtab, |
| 84 | unsigned int symindex, |
| 85 | unsigned int relsec, |
| 86 | struct module *me) |
| 87 | { |
| 88 | unsigned int i; |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 89 | Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr; |
| 90 | Elf_Sym *sym; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | u8 *location; |
| 92 | u32 *loc32; |
| 93 | |
| 94 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 95 | Elf_Addr v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
| 97 | /* This is where to make the change */ |
| 98 | location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 99 | + rel[i].r_offset; |
| 100 | loc32 = (u32 *) location; |
| 101 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 102 | #ifdef CONFIG_SPARC64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | BUG_ON(((u64)location >> (u64)32) != (u64)0); |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 104 | #endif /* CONFIG_SPARC64 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | /* This is the symbol it is referring to. Note that all |
| 107 | undefined symbols have been resolved. */ |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 108 | sym = (Elf_Sym *)sechdrs[symindex].sh_addr |
| 109 | + ELF_R_SYM(rel[i].r_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | v = sym->st_value + rel[i].r_addend; |
| 111 | |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 112 | switch (ELF_R_TYPE(rel[i].r_info) & 0xff) { |
Andreas Larsson | dfa520a | 2012-09-17 19:59:52 +0000 | [diff] [blame] | 113 | case R_SPARC_DISP32: |
| 114 | v -= (Elf_Addr) location; |
| 115 | *loc32 = v; |
| 116 | break; |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 117 | #ifdef CONFIG_SPARC64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | case R_SPARC_64: |
| 119 | location[0] = v >> 56; |
| 120 | location[1] = v >> 48; |
| 121 | location[2] = v >> 40; |
| 122 | location[3] = v >> 32; |
| 123 | location[4] = v >> 24; |
| 124 | location[5] = v >> 16; |
| 125 | location[6] = v >> 8; |
| 126 | location[7] = v >> 0; |
| 127 | break; |
| 128 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 129 | case R_SPARC_WDISP19: |
| 130 | v -= (Elf_Addr) location; |
| 131 | *loc32 = (*loc32 & ~0x7ffff) | |
| 132 | ((v >> 2) & 0x7ffff); |
| 133 | break; |
| 134 | |
| 135 | case R_SPARC_OLO10: |
| 136 | *loc32 = (*loc32 & ~0x1fff) | |
| 137 | (((v & 0x3ff) + |
| 138 | (ELF_R_TYPE(rel[i].r_info) >> 8)) |
| 139 | & 0x1fff); |
| 140 | break; |
| 141 | #endif /* CONFIG_SPARC64 */ |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | case R_SPARC_32: |
Sam Ravnborg | e6b04fe | 2008-12-26 15:38:17 -0800 | [diff] [blame] | 144 | case R_SPARC_UA32: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | location[0] = v >> 24; |
| 146 | location[1] = v >> 16; |
| 147 | location[2] = v >> 8; |
| 148 | location[3] = v >> 0; |
| 149 | break; |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | case R_SPARC_WDISP30: |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 152 | v -= (Elf_Addr) location; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | *loc32 = (*loc32 & ~0x3fffffff) | |
| 154 | ((v >> 2) & 0x3fffffff); |
| 155 | break; |
| 156 | |
| 157 | case R_SPARC_WDISP22: |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 158 | v -= (Elf_Addr) location; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | *loc32 = (*loc32 & ~0x3fffff) | |
| 160 | ((v >> 2) & 0x3fffff); |
| 161 | break; |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | case R_SPARC_LO10: |
| 164 | *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff); |
| 165 | break; |
| 166 | |
| 167 | case R_SPARC_HI22: |
| 168 | *loc32 = (*loc32 & ~0x3fffff) | |
| 169 | ((v >> 10) & 0x3fffff); |
| 170 | break; |
| 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | default: |
| 173 | printk(KERN_ERR "module %s: Unknown relocation: %x\n", |
| 174 | me->name, |
Sam Ravnborg | c45d1c2 | 2008-12-26 15:36:29 -0800 | [diff] [blame] | 175 | (int) (ELF_R_TYPE(rel[i].r_info) & 0xff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | return -ENOEXEC; |
Joe Perches | 6cb79b3 | 2011-06-03 14:45:23 +0000 | [diff] [blame] | 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 182 | #ifdef CONFIG_SPARC64 |
David S. Miller | 0b64120 | 2011-11-17 22:44:58 -0800 | [diff] [blame] | 183 | static void do_patch_sections(const Elf_Ehdr *hdr, |
| 184 | const Elf_Shdr *sechdrs) |
| 185 | { |
| 186 | const Elf_Shdr *s, *sun4v_1insn = NULL, *sun4v_2insn = NULL; |
| 187 | char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
| 188 | |
| 189 | for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { |
| 190 | if (!strcmp(".sun4v_1insn_patch", secstrings + s->sh_name)) |
| 191 | sun4v_1insn = s; |
| 192 | if (!strcmp(".sun4v_2insn_patch", secstrings + s->sh_name)) |
| 193 | sun4v_2insn = s; |
| 194 | } |
| 195 | |
| 196 | if (sun4v_1insn && tlb_type == hypervisor) { |
| 197 | void *p = (void *) sun4v_1insn->sh_addr; |
| 198 | sun4v_patch_1insn_range(p, p + sun4v_1insn->sh_size); |
| 199 | } |
| 200 | if (sun4v_2insn && tlb_type == hypervisor) { |
| 201 | void *p = (void *) sun4v_2insn->sh_addr; |
| 202 | sun4v_patch_2insn_range(p, p + sun4v_2insn->sh_size); |
| 203 | } |
| 204 | } |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | int module_finalize(const Elf_Ehdr *hdr, |
| 207 | const Elf_Shdr *sechdrs, |
| 208 | struct module *me) |
| 209 | { |
David S. Miller | dff9d3c | 2010-09-17 11:09:25 -0400 | [diff] [blame] | 210 | /* make jump label nops */ |
| 211 | jump_label_apply_nops(me); |
| 212 | |
David S. Miller | 0b64120 | 2011-11-17 22:44:58 -0800 | [diff] [blame] | 213 | do_patch_sections(hdr, sechdrs); |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | /* Cheetah's I-cache is fully coherent. */ |
| 216 | if (tlb_type == spitfire) { |
| 217 | unsigned long va; |
| 218 | |
| 219 | flushw_all(); |
| 220 | for (va = 0; va < (PAGE_SIZE << 1); va += 32) |
| 221 | spitfire_put_icache_tag(va, 0x0); |
| 222 | __asm__ __volatile__("flush %g6"); |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
Sam Ravnborg | 627bf2f | 2008-12-26 15:37:24 -0800 | [diff] [blame] | 227 | #endif /* CONFIG_SPARC64 */ |