blob: ee3c7dde8d9fbd5af21f5dbf555628eff1c64b9c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/string.h>
Sam Ravnborge6b04fe2008-12-26 15:38:17 -080014#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/mm.h>
16
17#include <asm/processor.h>
18#include <asm/spitfire.h>
19
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080020#ifdef CONFIG_SPARC64
David S. Millerdff9d3c2010-09-17 11:09:25 -040021
22#include <linux/jump_label.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024static 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 Ravnborg627bf2f2008-12-26 15:37:24 -080039static char *dot2underscore(char *name)
40{
41 return name;
42}
Sam Ravnborge6b04fe2008-12-26 15:38:17 -080043#else
44static void *module_map(unsigned long size)
45{
46 return vmalloc(size);
47}
48
49/* Replace references to .func with _Func */
50static char *dot2underscore(char *name)
51{
52 if (name[0] == '.') {
53 name[0] = '_';
54 name[1] = toupper(name[1]);
55 }
56 return name;
57}
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080058#endif /* CONFIG_SPARC64 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060void *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 */
78void module_free(struct module *mod, void *module_region)
79{
80 vfree(module_region);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83/* Make generic code ignore STT_REGISTER dummy undefined symbols. */
84int module_frob_arch_sections(Elf_Ehdr *hdr,
85 Elf_Shdr *sechdrs,
86 char *secstrings,
87 struct module *mod)
88{
89 unsigned int symidx;
Sam Ravnborgc45d1c22008-12-26 15:36:29 -080090 Elf_Sym *sym;
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080091 char *strtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 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 Ravnborgc45d1c22008-12-26 15:36:29 -0800100 sym = (Elf_Sym *)sechdrs[symidx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
102
103 for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800104 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 Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113 return 0;
114}
115
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800116int apply_relocate(Elf_Shdr *sechdrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 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 Ravnborgc45d1c22008-12-26 15:36:29 -0800127int apply_relocate_add(Elf_Shdr *sechdrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 const char *strtab,
129 unsigned int symindex,
130 unsigned int relsec,
131 struct module *me)
132{
133 unsigned int i;
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800134 Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
135 Elf_Sym *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 u8 *location;
137 u32 *loc32;
138
139 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800140 Elf_Addr v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
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 Ravnborg627bf2f2008-12-26 15:37:24 -0800147#ifdef CONFIG_SPARC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 BUG_ON(((u64)location >> (u64)32) != (u64)0);
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800149#endif /* CONFIG_SPARC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* This is the symbol it is referring to. Note that all
152 undefined symbols have been resolved. */
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800153 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
154 + ELF_R_SYM(rel[i].r_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 v = sym->st_value + rel[i].r_addend;
156
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800157 switch (ELF_R_TYPE(rel[i].r_info) & 0xff) {
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800158#ifdef CONFIG_SPARC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 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 Ravnborg627bf2f2008-12-26 15:37:24 -0800170 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 Torvalds1da177e2005-04-16 15:20:36 -0700189 case R_SPARC_32:
Sam Ravnborge6b04fe2008-12-26 15:38:17 -0800190 case R_SPARC_UA32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 location[0] = v >> 24;
192 location[1] = v >> 16;
193 location[2] = v >> 8;
194 location[3] = v >> 0;
195 break;
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 case R_SPARC_WDISP30:
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800198 v -= (Elf_Addr) location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *loc32 = (*loc32 & ~0x3fffffff) |
200 ((v >> 2) & 0x3fffffff);
201 break;
202
203 case R_SPARC_WDISP22:
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800204 v -= (Elf_Addr) location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *loc32 = (*loc32 & ~0x3fffff) |
206 ((v >> 2) & 0x3fffff);
207 break;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 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 Torvalds1da177e2005-04-16 15:20:36 -0700218 default:
219 printk(KERN_ERR "module %s: Unknown relocation: %x\n",
220 me->name,
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800221 (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return -ENOEXEC;
223 };
224 }
225 return 0;
226}
227
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800228#ifdef CONFIG_SPARC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229int module_finalize(const Elf_Ehdr *hdr,
230 const Elf_Shdr *sechdrs,
231 struct module *me)
232{
David S. Millerdff9d3c2010-09-17 11:09:25 -0400233 /* make jump label nops */
234 jump_label_apply_nops(me);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 /* 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 Ravnborge6b04fe2008-12-26 15:38:17 -0800248#else
249int module_finalize(const Elf_Ehdr *hdr,
250 const Elf_Shdr *sechdrs,
251 struct module *me)
252{
253 return 0;
254}
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800255#endif /* CONFIG_SPARC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257void module_arch_cleanup(struct module *mod)
258{
259}