blob: da0c6c70ccb2c0a783043c151bd82d3dc289d86a [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{
David Rientjesd0a21262011-01-13 15:46:02 -080026 if (PAGE_ALIGN(size) > MODULES_LEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 return NULL;
David Rientjesd0a21262011-01-13 15:46:02 -080028 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
29 GFP_KERNEL, PAGE_KERNEL, -1,
30 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
32
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080033static char *dot2underscore(char *name)
34{
35 return name;
36}
Sam Ravnborge6b04fe2008-12-26 15:38:17 -080037#else
38static void *module_map(unsigned long size)
39{
40 return vmalloc(size);
41}
42
43/* Replace references to .func with _Func */
44static char *dot2underscore(char *name)
45{
46 if (name[0] == '.') {
47 name[0] = '_';
48 name[1] = toupper(name[1]);
49 }
50 return name;
51}
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080052#endif /* CONFIG_SPARC64 */
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054void *module_alloc(unsigned long size)
55{
56 void *ret;
57
58 /* We handle the zero case fine, unlike vmalloc */
59 if (size == 0)
60 return NULL;
61
62 ret = module_map(size);
63 if (!ret)
64 ret = ERR_PTR(-ENOMEM);
65 else
66 memset(ret, 0, size);
67
68 return ret;
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Make generic code ignore STT_REGISTER dummy undefined symbols. */
72int module_frob_arch_sections(Elf_Ehdr *hdr,
73 Elf_Shdr *sechdrs,
74 char *secstrings,
75 struct module *mod)
76{
77 unsigned int symidx;
Sam Ravnborgc45d1c22008-12-26 15:36:29 -080078 Elf_Sym *sym;
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080079 char *strtab;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int i;
81
82 for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
83 if (symidx == hdr->e_shnum-1) {
84 printk("%s: no symtab found.\n", mod->name);
85 return -ENOEXEC;
86 }
87 }
Sam Ravnborgc45d1c22008-12-26 15:36:29 -080088 sym = (Elf_Sym *)sechdrs[symidx].sh_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
90
91 for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
Sam Ravnborg627bf2f2008-12-26 15:37:24 -080092 if (sym[i].st_shndx == SHN_UNDEF) {
93 if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER) {
94 sym[i].st_shndx = SHN_ABS;
95 } else {
96 char *name = strtab + sym[i].st_name;
97 dot2underscore(name);
98 }
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 return 0;
102}
103
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800104int apply_relocate_add(Elf_Shdr *sechdrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 const char *strtab,
106 unsigned int symindex,
107 unsigned int relsec,
108 struct module *me)
109{
110 unsigned int i;
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800111 Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
112 Elf_Sym *sym;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 u8 *location;
114 u32 *loc32;
115
116 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800117 Elf_Addr v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 /* This is where to make the change */
120 location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
121 + rel[i].r_offset;
122 loc32 = (u32 *) location;
123
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800124#ifdef CONFIG_SPARC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 BUG_ON(((u64)location >> (u64)32) != (u64)0);
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800126#endif /* CONFIG_SPARC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* This is the symbol it is referring to. Note that all
129 undefined symbols have been resolved. */
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800130 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
131 + ELF_R_SYM(rel[i].r_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 v = sym->st_value + rel[i].r_addend;
133
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800134 switch (ELF_R_TYPE(rel[i].r_info) & 0xff) {
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800135#ifdef CONFIG_SPARC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 case R_SPARC_64:
137 location[0] = v >> 56;
138 location[1] = v >> 48;
139 location[2] = v >> 40;
140 location[3] = v >> 32;
141 location[4] = v >> 24;
142 location[5] = v >> 16;
143 location[6] = v >> 8;
144 location[7] = v >> 0;
145 break;
146
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800147 case R_SPARC_DISP32:
148 v -= (Elf_Addr) location;
149 *loc32 = v;
150 break;
151
152 case R_SPARC_WDISP19:
153 v -= (Elf_Addr) location;
154 *loc32 = (*loc32 & ~0x7ffff) |
155 ((v >> 2) & 0x7ffff);
156 break;
157
158 case R_SPARC_OLO10:
159 *loc32 = (*loc32 & ~0x1fff) |
160 (((v & 0x3ff) +
161 (ELF_R_TYPE(rel[i].r_info) >> 8))
162 & 0x1fff);
163 break;
164#endif /* CONFIG_SPARC64 */
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 case R_SPARC_32:
Sam Ravnborge6b04fe2008-12-26 15:38:17 -0800167 case R_SPARC_UA32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 location[0] = v >> 24;
169 location[1] = v >> 16;
170 location[2] = v >> 8;
171 location[3] = v >> 0;
172 break;
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 case R_SPARC_WDISP30:
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800175 v -= (Elf_Addr) location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 *loc32 = (*loc32 & ~0x3fffffff) |
177 ((v >> 2) & 0x3fffffff);
178 break;
179
180 case R_SPARC_WDISP22:
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800181 v -= (Elf_Addr) location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *loc32 = (*loc32 & ~0x3fffff) |
183 ((v >> 2) & 0x3fffff);
184 break;
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 case R_SPARC_LO10:
187 *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
188 break;
189
190 case R_SPARC_HI22:
191 *loc32 = (*loc32 & ~0x3fffff) |
192 ((v >> 10) & 0x3fffff);
193 break;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 default:
196 printk(KERN_ERR "module %s: Unknown relocation: %x\n",
197 me->name,
Sam Ravnborgc45d1c22008-12-26 15:36:29 -0800198 (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENOEXEC;
Joe Perches6cb79b32011-06-03 14:45:23 +0000200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202 return 0;
203}
204
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800205#ifdef CONFIG_SPARC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206int module_finalize(const Elf_Ehdr *hdr,
207 const Elf_Shdr *sechdrs,
208 struct module *me)
209{
David S. Millerdff9d3c2010-09-17 11:09:25 -0400210 /* make jump label nops */
211 jump_label_apply_nops(me);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Cheetah's I-cache is fully coherent. */
214 if (tlb_type == spitfire) {
215 unsigned long va;
216
217 flushw_all();
218 for (va = 0; va < (PAGE_SIZE << 1); va += 32)
219 spitfire_put_icache_tag(va, 0x0);
220 __asm__ __volatile__("flush %g6");
221 }
222
223 return 0;
224}
Sam Ravnborg627bf2f2008-12-26 15:37:24 -0800225#endif /* CONFIG_SPARC64 */