aboutsummaryrefslogtreecommitdiffstats
path: root/arch/blackfin/kernel/module.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/blackfin/kernel/module.c')
-rw-r--r--arch/blackfin/kernel/module.c266
1 files changed, 122 insertions, 144 deletions
diff --git a/arch/blackfin/kernel/module.c b/arch/blackfin/kernel/module.c
index d5aee362668..67fc7a56c86 100644
--- a/arch/blackfin/kernel/module.c
+++ b/arch/blackfin/kernel/module.c
@@ -27,6 +27,7 @@
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */ 28 */
29 29
30#define pr_fmt(fmt) "module %s: " fmt
30 31
31#include <linux/moduleloader.h> 32#include <linux/moduleloader.h>
32#include <linux/elf.h> 33#include <linux/elf.h>
@@ -36,6 +37,7 @@
36#include <linux/kernel.h> 37#include <linux/kernel.h>
37#include <asm/dma.h> 38#include <asm/dma.h>
38#include <asm/cacheflush.h> 39#include <asm/cacheflush.h>
40#include <asm/uaccess.h>
39 41
40void *module_alloc(unsigned long size) 42void *module_alloc(unsigned long size)
41{ 43{
@@ -52,7 +54,7 @@ void module_free(struct module *mod, void *module_region)
52 54
53/* Transfer the section to the L1 memory */ 55/* Transfer the section to the L1 memory */
54int 56int
55module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs, 57module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
56 char *secstrings, struct module *mod) 58 char *secstrings, struct module *mod)
57{ 59{
58 /* 60 /*
@@ -63,126 +65,119 @@ module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
63 * NOTE: this breaks the semantic of mod->arch structure. 65 * NOTE: this breaks the semantic of mod->arch structure.
64 */ 66 */
65 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum; 67 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
66 void *dest = NULL; 68 void *dest;
67 69
68 for (s = sechdrs; s < sechdrs_end; ++s) { 70 for (s = sechdrs; s < sechdrs_end; ++s) {
69 if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) || 71 const char *shname = secstrings + s->sh_name;
70 ((strcmp(".text", secstrings + s->sh_name) == 0) && 72
71 (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) { 73 if (s->sh_size == 0)
74 continue;
75
76 if (!strcmp(".l1.text", shname) ||
77 (!strcmp(".text", shname) &&
78 (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
79
72 dest = l1_inst_sram_alloc(s->sh_size); 80 dest = l1_inst_sram_alloc(s->sh_size);
73 mod->arch.text_l1 = dest; 81 mod->arch.text_l1 = dest;
74 if (dest == NULL) { 82 if (dest == NULL) {
75 printk(KERN_ERR 83 pr_err("L1 inst memory allocation failed\n",
76 "module %s: L1 instruction memory allocation failed\n", 84 mod->name);
77 mod->name);
78 return -1; 85 return -1;
79 } 86 }
80 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size); 87 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
81 s->sh_flags &= ~SHF_ALLOC; 88
82 s->sh_addr = (unsigned long)dest; 89 } else if (!strcmp(".l1.data", shname) ||
83 } 90 (!strcmp(".data", shname) &&
84 if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) || 91 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
85 ((strcmp(".data", secstrings + s->sh_name) == 0) && 92
86 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
87 dest = l1_data_sram_alloc(s->sh_size); 93 dest = l1_data_sram_alloc(s->sh_size);
88 mod->arch.data_a_l1 = dest; 94 mod->arch.data_a_l1 = dest;
89 if (dest == NULL) { 95 if (dest == NULL) {
90 printk(KERN_ERR 96 pr_err("L1 data memory allocation failed\n",
91 "module %s: L1 data memory allocation failed\n",
92 mod->name); 97 mod->name);
93 return -1; 98 return -1;
94 } 99 }
95 memcpy(dest, (void *)s->sh_addr, s->sh_size); 100 memcpy(dest, (void *)s->sh_addr, s->sh_size);
96 s->sh_flags &= ~SHF_ALLOC; 101
97 s->sh_addr = (unsigned long)dest; 102 } else if (!strcmp(".l1.bss", shname) ||
98 } 103 (!strcmp(".bss", shname) &&
99 if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 || 104 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
100 ((strcmp(".bss", secstrings + s->sh_name) == 0) && 105
101 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) { 106 dest = l1_data_sram_zalloc(s->sh_size);
102 dest = l1_data_sram_alloc(s->sh_size);
103 mod->arch.bss_a_l1 = dest; 107 mod->arch.bss_a_l1 = dest;
104 if (dest == NULL) { 108 if (dest == NULL) {
105 printk(KERN_ERR 109 pr_err("L1 data memory allocation failed\n",
106 "module %s: L1 data memory allocation failed\n",
107 mod->name); 110 mod->name);
108 return -1; 111 return -1;
109 } 112 }
110 memset(dest, 0, s->sh_size); 113
111 s->sh_flags &= ~SHF_ALLOC; 114 } else if (!strcmp(".l1.data.B", shname)) {
112 s->sh_addr = (unsigned long)dest; 115
113 }
114 if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) {
115 dest = l1_data_B_sram_alloc(s->sh_size); 116 dest = l1_data_B_sram_alloc(s->sh_size);
116 mod->arch.data_b_l1 = dest; 117 mod->arch.data_b_l1 = dest;
117 if (dest == NULL) { 118 if (dest == NULL) {
118 printk(KERN_ERR 119 pr_err("L1 data memory allocation failed\n",
119 "module %s: L1 data memory allocation failed\n",
120 mod->name); 120 mod->name);
121 return -1; 121 return -1;
122 } 122 }
123 memcpy(dest, (void *)s->sh_addr, s->sh_size); 123 memcpy(dest, (void *)s->sh_addr, s->sh_size);
124 s->sh_flags &= ~SHF_ALLOC; 124
125 s->sh_addr = (unsigned long)dest; 125 } else if (!strcmp(".l1.bss.B", shname)) {
126 } 126
127 if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) {
128 dest = l1_data_B_sram_alloc(s->sh_size); 127 dest = l1_data_B_sram_alloc(s->sh_size);
129 mod->arch.bss_b_l1 = dest; 128 mod->arch.bss_b_l1 = dest;
130 if (dest == NULL) { 129 if (dest == NULL) {
131 printk(KERN_ERR 130 pr_err("L1 data memory allocation failed\n",
132 "module %s: L1 data memory allocation failed\n",
133 mod->name); 131 mod->name);
134 return -1; 132 return -1;
135 } 133 }
136 memset(dest, 0, s->sh_size); 134 memset(dest, 0, s->sh_size);
137 s->sh_flags &= ~SHF_ALLOC; 135
138 s->sh_addr = (unsigned long)dest; 136 } else if (!strcmp(".l2.text", shname) ||
139 } 137 (!strcmp(".text", shname) &&
140 if ((strcmp(".l2.text", secstrings + s->sh_name) == 0) || 138 (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
141 ((strcmp(".text", secstrings + s->sh_name) == 0) && 139
142 (hdr->e_flags & EF_BFIN_CODE_IN_L2) && (s->sh_size > 0))) {
143 dest = l2_sram_alloc(s->sh_size); 140 dest = l2_sram_alloc(s->sh_size);
144 mod->arch.text_l2 = dest; 141 mod->arch.text_l2 = dest;
145 if (dest == NULL) { 142 if (dest == NULL) {
146 printk(KERN_ERR 143 pr_err("L2 SRAM allocation failed\n",
147 "module %s: L2 SRAM allocation failed\n", 144 mod->name);
148 mod->name);
149 return -1; 145 return -1;
150 } 146 }
151 memcpy(dest, (void *)s->sh_addr, s->sh_size); 147 memcpy(dest, (void *)s->sh_addr, s->sh_size);
152 s->sh_flags &= ~SHF_ALLOC; 148
153 s->sh_addr = (unsigned long)dest; 149 } else if (!strcmp(".l2.data", shname) ||
154 } 150 (!strcmp(".data", shname) &&
155 if ((strcmp(".l2.data", secstrings + s->sh_name) == 0) || 151 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
156 ((strcmp(".data", secstrings + s->sh_name) == 0) && 152
157 (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
158 dest = l2_sram_alloc(s->sh_size); 153 dest = l2_sram_alloc(s->sh_size);
159 mod->arch.data_l2 = dest; 154 mod->arch.data_l2 = dest;
160 if (dest == NULL) { 155 if (dest == NULL) {
161 printk(KERN_ERR 156 pr_err("L2 SRAM allocation failed\n",
162 "module %s: L2 SRAM allocation failed\n",
163 mod->name); 157 mod->name);
164 return -1; 158 return -1;
165 } 159 }
166 memcpy(dest, (void *)s->sh_addr, s->sh_size); 160 memcpy(dest, (void *)s->sh_addr, s->sh_size);
167 s->sh_flags &= ~SHF_ALLOC; 161
168 s->sh_addr = (unsigned long)dest; 162 } else if (!strcmp(".l2.bss", shname) ||
169 } 163 (!strcmp(".bss", shname) &&
170 if (strcmp(".l2.bss", secstrings + s->sh_name) == 0 || 164 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
171 ((strcmp(".bss", secstrings + s->sh_name) == 0) && 165
172 (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) { 166 dest = l2_sram_zalloc(s->sh_size);
173 dest = l2_sram_alloc(s->sh_size);
174 mod->arch.bss_l2 = dest; 167 mod->arch.bss_l2 = dest;
175 if (dest == NULL) { 168 if (dest == NULL) {
176 printk(KERN_ERR 169 pr_err("L2 SRAM allocation failed\n",
177 "module %s: L2 SRAM allocation failed\n",
178 mod->name); 170 mod->name);
179 return -1; 171 return -1;
180 } 172 }
181 memset(dest, 0, s->sh_size); 173
182 s->sh_flags &= ~SHF_ALLOC; 174 } else
183 s->sh_addr = (unsigned long)dest; 175 continue;
184 } 176
177 s->sh_flags &= ~SHF_ALLOC;
178 s->sh_addr = (unsigned long)dest;
185 } 179 }
180
186 return 0; 181 return 0;
187} 182}
188 183
@@ -190,7 +185,7 @@ int
190apply_relocate(Elf_Shdr * sechdrs, const char *strtab, 185apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
191 unsigned int symindex, unsigned int relsec, struct module *me) 186 unsigned int symindex, unsigned int relsec, struct module *me)
192{ 187{
193 printk(KERN_ERR "module %s: .rel unsupported\n", me->name); 188 pr_err(".rel unsupported\n", me->name);
194 return -ENOEXEC; 189 return -ENOEXEC;
195} 190}
196 191
@@ -205,109 +200,86 @@ apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
205/* gas does not generate it. */ 200/* gas does not generate it. */
206/*************************************************************************/ 201/*************************************************************************/
207int 202int
208apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab, 203apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
209 unsigned int symindex, unsigned int relsec, 204 unsigned int symindex, unsigned int relsec,
210 struct module *mod) 205 struct module *mod)
211{ 206{
212 unsigned int i; 207 unsigned int i;
213 unsigned short tmp;
214 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; 208 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
215 Elf32_Sym *sym; 209 Elf32_Sym *sym;
216 uint32_t *location32; 210 unsigned long location, value, size;
217 uint16_t *location16; 211
218 uint32_t value; 212 pr_debug("applying relocate section %u to %u\n", mod->name,
213 relsec, sechdrs[relsec].sh_info);
219 214
220 pr_debug("Applying relocate section %u to %u\n", relsec,
221 sechdrs[relsec].sh_info);
222 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 215 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
223 /* This is where to make the change */ 216 /* This is where to make the change */
224 location16 = 217 location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
225 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr + 218 rel[i].r_offset;
226 rel[i].r_offset); 219
227 location32 = (uint32_t *) location16;
228 /* This is the symbol it is referring to. Note that all 220 /* This is the symbol it is referring to. Note that all
229 undefined symbols have been resolved. */ 221 undefined symbols have been resolved. */
230 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr 222 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
231 + ELF32_R_SYM(rel[i].r_info); 223 + ELF32_R_SYM(rel[i].r_info);
232 value = sym->st_value; 224 value = sym->st_value;
233 value += rel[i].r_addend; 225 value += rel[i].r_addend;
234 pr_debug("location is %x, value is %x type is %d \n", 226
235 (unsigned int) location32, value,
236 ELF32_R_TYPE(rel[i].r_info));
237#ifdef CONFIG_SMP 227#ifdef CONFIG_SMP
238 if ((unsigned long)location16 >= COREB_L1_DATA_A_START) { 228 if (location >= COREB_L1_DATA_A_START) {
239 printk(KERN_ERR "module %s: cannot relocate in L1: %u (SMP kernel)", 229 pr_err("cannot relocate in L1: %u (SMP kernel)",
240 mod->name, ELF32_R_TYPE(rel[i].r_info)); 230 mod->name, ELF32_R_TYPE(rel[i].r_info));
241 return -ENOEXEC; 231 return -ENOEXEC;
242 } 232 }
243#endif 233#endif
234
235 pr_debug("location is %lx, value is %lx type is %d\n",
236 mod->name, location, value, ELF32_R_TYPE(rel[i].r_info));
237
244 switch (ELF32_R_TYPE(rel[i].r_info)) { 238 switch (ELF32_R_TYPE(rel[i].r_info)) {
245 239
240 case R_BFIN_HUIMM16:
241 value >>= 16;
242 case R_BFIN_LUIMM16:
243 case R_BFIN_RIMM16:
244 size = 2;
245 break;
246 case R_BFIN_BYTE4_DATA:
247 size = 4;
248 break;
249
246 case R_BFIN_PCREL24: 250 case R_BFIN_PCREL24:
247 case R_BFIN_PCREL24_JUMP_L: 251 case R_BFIN_PCREL24_JUMP_L:
248 /* Add the value, subtract its postition */
249 location16 =
250 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].
251 sh_addr + rel[i].r_offset - 2);
252 location32 = (uint32_t *) location16;
253 value -= (uint32_t) location32;
254 value >>= 1;
255 if ((value & 0xFF000000) != 0 &&
256 (value & 0xFF000000) != 0xFF000000) {
257 printk(KERN_ERR "module %s: relocation overflow\n",
258 mod->name);
259 return -ENOEXEC;
260 }
261 pr_debug("value is %x, before %x-%x after %x-%x\n", value,
262 *location16, *(location16 + 1),
263 (*location16 & 0xff00) | (value >> 16 & 0x00ff),
264 value & 0xffff);
265 *location16 =
266 (*location16 & 0xff00) | (value >> 16 & 0x00ff);
267 *(location16 + 1) = value & 0xffff;
268 break;
269 case R_BFIN_PCREL12_JUMP: 252 case R_BFIN_PCREL12_JUMP:
270 case R_BFIN_PCREL12_JUMP_S: 253 case R_BFIN_PCREL12_JUMP_S:
271 value -= (uint32_t) location32;
272 value >>= 1;
273 *location16 = (value & 0xfff);
274 break;
275 case R_BFIN_PCREL10: 254 case R_BFIN_PCREL10:
276 value -= (uint32_t) location32; 255 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
277 value >>= 1; 256 mod->name, ELF32_R_TYPE(rel[i].r_info));
278 *location16 = (value & 0x3ff); 257 return -ENOEXEC;
279 break; 258
280 case R_BFIN_LUIMM16: 259 default:
281 pr_debug("before %x after %x\n", *location16, 260 pr_err("unknown relocation: %u\n", mod->name,
282 (value & 0xffff)); 261 ELF32_R_TYPE(rel[i].r_info));
283 tmp = (value & 0xffff); 262 return -ENOEXEC;
284 if ((unsigned long)location16 >= L1_CODE_START) { 263 }
285 dma_memcpy(location16, &tmp, 2); 264
286 } else 265 switch (bfin_mem_access_type(location, size)) {
287 *location16 = tmp; 266 case BFIN_MEM_ACCESS_CORE:
288 break; 267 case BFIN_MEM_ACCESS_CORE_ONLY:
289 case R_BFIN_HUIMM16: 268 memcpy((void *)location, &value, size);
290 pr_debug("before %x after %x\n", *location16,
291 ((value >> 16) & 0xffff));
292 tmp = ((value >> 16) & 0xffff);
293 if ((unsigned long)location16 >= L1_CODE_START) {
294 dma_memcpy(location16, &tmp, 2);
295 } else
296 *location16 = tmp;
297 break; 269 break;
298 case R_BFIN_RIMM16: 270 case BFIN_MEM_ACCESS_DMA:
299 *location16 = (value & 0xffff); 271 dma_memcpy((void *)location, &value, size);
300 break; 272 break;
301 case R_BFIN_BYTE4_DATA: 273 case BFIN_MEM_ACCESS_ITEST:
302 pr_debug("before %x after %x\n", *location32, value); 274 isram_memcpy((void *)location, &value, size);
303 *location32 = value;
304 break; 275 break;
305 default: 276 default:
306 printk(KERN_ERR "module %s: Unknown relocation: %u\n", 277 pr_err("invalid relocation for %#lx\n",
307 mod->name, ELF32_R_TYPE(rel[i].r_info)); 278 mod->name, location);
308 return -ENOEXEC; 279 return -ENOEXEC;
309 } 280 }
310 } 281 }
282
311 return 0; 283 return 0;
312} 284}
313 285
@@ -332,22 +304,28 @@ module_finalize(const Elf_Ehdr * hdr,
332 for (i = 1; i < hdr->e_shnum; i++) { 304 for (i = 1; i < hdr->e_shnum; i++) {
333 const char *strtab = (char *)sechdrs[strindex].sh_addr; 305 const char *strtab = (char *)sechdrs[strindex].sh_addr;
334 unsigned int info = sechdrs[i].sh_info; 306 unsigned int info = sechdrs[i].sh_info;
307 const char *shname = secstrings + sechdrs[i].sh_name;
335 308
336 /* Not a valid relocation section? */ 309 /* Not a valid relocation section? */
337 if (info >= hdr->e_shnum) 310 if (info >= hdr->e_shnum)
338 continue; 311 continue;
339 312
340 if ((sechdrs[i].sh_type == SHT_RELA) && 313 /* Only support RELA relocation types */
341 ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) || 314 if (sechdrs[i].sh_type != SHT_RELA)
342 (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) || 315 continue;
343 ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) && 316
344 (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) { 317 if (!strcmp(".rela.l2.text", shname) ||
318 !strcmp(".rela.l1.text", shname) ||
319 (!strcmp(".rela.text", shname) &&
320 (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
321
345 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab, 322 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
346 symindex, i, mod); 323 symindex, i, mod);
347 if (err < 0) 324 if (err < 0)
348 return -ENOEXEC; 325 return -ENOEXEC;
349 } 326 }
350 } 327 }
328
351 return 0; 329 return 0;
352} 330}
353 331