blob: 2e14cadd4302217871b5d7c290f47bd03a331fb1 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
2 * File: arch/blackfin/kernel/module.c
3 * Based on:
4 * Author:
5 *
6 * Created:
7 * Description:
8 *
9 * Modified:
10 * Copyright 2004-2006 Analog Devices Inc.
11 *
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
29
30
31#include <linux/moduleloader.h>
32#include <linux/elf.h>
33#include <linux/vmalloc.h>
34#include <linux/fs.h>
35#include <linux/string.h>
36#include <linux/kernel.h>
37#include <asm/dma.h>
38#include <asm/cacheflush.h>
39
40/*
41 * handle arithmetic relocations.
42 * See binutils/bfd/elf32-bfin.c for more details
43 */
44#define RELOC_STACK_SIZE 100
45static uint32_t reloc_stack[RELOC_STACK_SIZE];
46static unsigned int reloc_stack_tos;
47
48#define is_reloc_stack_empty() ((reloc_stack_tos > 0)?0:1)
49
50static void reloc_stack_push(uint32_t value)
51{
52 reloc_stack[reloc_stack_tos++] = value;
53}
54
55static uint32_t reloc_stack_pop(void)
56{
57 return reloc_stack[--reloc_stack_tos];
58}
59
60static uint32_t reloc_stack_operate(unsigned int oper, struct module *mod)
61{
62 uint32_t value;
63
64 switch (oper) {
65 case R_add:
66 value = reloc_stack[reloc_stack_tos - 2] +
67 reloc_stack[reloc_stack_tos - 1];
68 reloc_stack_tos -= 2;
69 break;
70 case R_sub:
71 value = reloc_stack[reloc_stack_tos - 2] -
72 reloc_stack[reloc_stack_tos - 1];
73 reloc_stack_tos -= 2;
74 break;
75 case R_mult:
76 value = reloc_stack[reloc_stack_tos - 2] *
77 reloc_stack[reloc_stack_tos - 1];
78 reloc_stack_tos -= 2;
79 break;
80 case R_div:
81 value = reloc_stack[reloc_stack_tos - 2] /
82 reloc_stack[reloc_stack_tos - 1];
83 reloc_stack_tos -= 2;
84 break;
85 case R_mod:
86 value = reloc_stack[reloc_stack_tos - 2] %
87 reloc_stack[reloc_stack_tos - 1];
88 reloc_stack_tos -= 2;
89 break;
90 case R_lshift:
91 value = reloc_stack[reloc_stack_tos - 2] <<
92 reloc_stack[reloc_stack_tos - 1];
93 reloc_stack_tos -= 2;
94 break;
95 case R_rshift:
96 value = reloc_stack[reloc_stack_tos - 2] >>
97 reloc_stack[reloc_stack_tos - 1];
98 reloc_stack_tos -= 2;
99 break;
100 case R_and:
101 value = reloc_stack[reloc_stack_tos - 2] &
102 reloc_stack[reloc_stack_tos - 1];
103 reloc_stack_tos -= 2;
104 break;
105 case R_or:
106 value = reloc_stack[reloc_stack_tos - 2] |
107 reloc_stack[reloc_stack_tos - 1];
108 reloc_stack_tos -= 2;
109 break;
110 case R_xor:
111 value = reloc_stack[reloc_stack_tos - 2] ^
112 reloc_stack[reloc_stack_tos - 1];
113 reloc_stack_tos -= 2;
114 break;
115 case R_land:
116 value = reloc_stack[reloc_stack_tos - 2] &&
117 reloc_stack[reloc_stack_tos - 1];
118 reloc_stack_tos -= 2;
119 break;
120 case R_lor:
121 value = reloc_stack[reloc_stack_tos - 2] ||
122 reloc_stack[reloc_stack_tos - 1];
123 reloc_stack_tos -= 2;
124 break;
125 case R_neg:
126 value = -reloc_stack[reloc_stack_tos - 1];
127 reloc_stack_tos--;
128 break;
129 case R_comp:
130 value = ~reloc_stack[reloc_stack_tos - 1];
131 reloc_stack_tos -= 1;
132 break;
133 default:
134 printk(KERN_WARNING "module %s: unhandled reloction\n",
135 mod->name);
136 return 0;
137 }
138
139 /* now push the new value back on stack */
140 reloc_stack_push(value);
141
142 return value;
143}
144
145void *module_alloc(unsigned long size)
146{
147 if (size == 0)
148 return NULL;
149 return vmalloc(size);
150}
151
152/* Free memory returned from module_alloc */
153void module_free(struct module *mod, void *module_region)
154{
155 vfree(module_region);
156}
157
158/* Transfer the section to the L1 memory */
159int
160module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
161 char *secstrings, struct module *mod)
162{
Meihui Fan96a87e22008-05-07 11:41:26 +0800163 /*
164 * XXX: sechdrs are vmalloced in kernel/module.c
165 * and would be vfreed just after module is loaded,
166 * so we hack to keep the only information we needed
167 * in mod->arch to correctly free L1 I/D sram later.
168 * NOTE: this breaks the semantic of mod->arch structure.
169 */
Bryan Wu1394f032007-05-06 14:50:22 -0700170 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
171 void *dest = NULL;
172
173 for (s = sechdrs; s < sechdrs_end; ++s) {
174 if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800175 ((strcmp(".text", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800176 (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700177 dest = l1_inst_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800178 mod->arch.text_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700179 if (dest == NULL) {
180 printk(KERN_ERR
181 "module %s: L1 instruction memory allocation failed\n",
182 mod->name);
183 return -1;
184 }
185 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
186 s->sh_flags &= ~SHF_ALLOC;
187 s->sh_addr = (unsigned long)dest;
188 }
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800189 if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
190 ((strcmp(".data", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800191 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700192 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800193 mod->arch.data_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700194 if (dest == NULL) {
195 printk(KERN_ERR
196 "module %s: L1 data memory allocation failed\n",
197 mod->name);
198 return -1;
199 }
200 memcpy(dest, (void *)s->sh_addr, s->sh_size);
201 s->sh_flags &= ~SHF_ALLOC;
202 s->sh_addr = (unsigned long)dest;
203 }
204 if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800205 ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800206 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700207 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800208 mod->arch.bss_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700209 if (dest == NULL) {
210 printk(KERN_ERR
211 "module %s: L1 data memory allocation failed\n",
212 mod->name);
213 return -1;
214 }
215 memset(dest, 0, s->sh_size);
216 s->sh_flags &= ~SHF_ALLOC;
217 s->sh_addr = (unsigned long)dest;
218 }
219 if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) {
Bryan Wu1394f032007-05-06 14:50:22 -0700220 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800221 mod->arch.data_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700222 if (dest == NULL) {
223 printk(KERN_ERR
224 "module %s: L1 data memory allocation failed\n",
225 mod->name);
226 return -1;
227 }
228 memcpy(dest, (void *)s->sh_addr, s->sh_size);
229 s->sh_flags &= ~SHF_ALLOC;
230 s->sh_addr = (unsigned long)dest;
231 }
232 if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) {
Bryan Wu1394f032007-05-06 14:50:22 -0700233 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800234 mod->arch.bss_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700235 if (dest == NULL) {
236 printk(KERN_ERR
237 "module %s: L1 data memory allocation failed\n",
238 mod->name);
239 return -1;
240 }
241 memset(dest, 0, s->sh_size);
242 s->sh_flags &= ~SHF_ALLOC;
243 s->sh_addr = (unsigned long)dest;
244 }
Sonic Zhang262c3822008-07-19 15:42:41 +0800245 if ((strcmp(".l2.text", secstrings + s->sh_name) == 0) ||
246 ((strcmp(".text", secstrings + s->sh_name) == 0) &&
247 (hdr->e_flags & EF_BFIN_CODE_IN_L2) && (s->sh_size > 0))) {
248 dest = l2_sram_alloc(s->sh_size);
249 mod->arch.text_l2 = dest;
250 if (dest == NULL) {
251 printk(KERN_ERR
252 "module %s: L2 SRAM allocation failed\n",
253 mod->name);
254 return -1;
255 }
256 memcpy(dest, (void *)s->sh_addr, s->sh_size);
257 s->sh_flags &= ~SHF_ALLOC;
258 s->sh_addr = (unsigned long)dest;
259 }
260 if ((strcmp(".l2.data", secstrings + s->sh_name) == 0) ||
261 ((strcmp(".data", secstrings + s->sh_name) == 0) &&
262 (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
263 dest = l2_sram_alloc(s->sh_size);
264 mod->arch.data_l2 = dest;
265 if (dest == NULL) {
266 printk(KERN_ERR
267 "module %s: L2 SRAM allocation failed\n",
268 mod->name);
269 return -1;
270 }
271 memcpy(dest, (void *)s->sh_addr, s->sh_size);
272 s->sh_flags &= ~SHF_ALLOC;
273 s->sh_addr = (unsigned long)dest;
274 }
275 if (strcmp(".l2.bss", secstrings + s->sh_name) == 0 ||
276 ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
277 (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
278 dest = l2_sram_alloc(s->sh_size);
279 mod->arch.bss_l2 = dest;
280 if (dest == NULL) {
281 printk(KERN_ERR
282 "module %s: L2 SRAM allocation failed\n",
283 mod->name);
284 return -1;
285 }
286 memset(dest, 0, s->sh_size);
287 s->sh_flags &= ~SHF_ALLOC;
288 s->sh_addr = (unsigned long)dest;
289 }
Bryan Wu1394f032007-05-06 14:50:22 -0700290 }
291 return 0;
292}
293
294int
295apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
296 unsigned int symindex, unsigned int relsec, struct module *me)
297{
298 printk(KERN_ERR "module %s: .rel unsupported\n", me->name);
299 return -ENOEXEC;
300}
301
302/*************************************************************************/
303/* FUNCTION : apply_relocate_add */
304/* ABSTRACT : Blackfin specific relocation handling for the loadable */
305/* modules. Modules are expected to be .o files. */
306/* Arithmetic relocations are handled. */
307/* We do not expect LSETUP to be split and hence is not */
308/* handled. */
309/* R_byte and R_byte2 are also not handled as the gas */
310/* does not generate it. */
311/*************************************************************************/
312int
313apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
314 unsigned int symindex, unsigned int relsec,
315 struct module *mod)
316{
317 unsigned int i;
318 unsigned short tmp;
319 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
320 Elf32_Sym *sym;
321 uint32_t *location32;
322 uint16_t *location16;
323 uint32_t value;
324
325 pr_debug("Applying relocate section %u to %u\n", relsec,
326 sechdrs[relsec].sh_info);
327 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
328 /* This is where to make the change */
329 location16 =
330 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr +
331 rel[i].r_offset);
332 location32 = (uint32_t *) location16;
333 /* This is the symbol it is referring to. Note that all
334 undefined symbols have been resolved. */
335 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
336 + ELF32_R_SYM(rel[i].r_info);
337 if (is_reloc_stack_empty()) {
338 value = sym->st_value;
339 } else {
340 value = reloc_stack_pop();
341 }
342 value += rel[i].r_addend;
343 pr_debug("location is %x, value is %x type is %d \n",
344 (unsigned int) location32, value,
345 ELF32_R_TYPE(rel[i].r_info));
Graf Yang8f658732008-11-18 17:48:22 +0800346#ifdef CONFIG_SMP
347 if ((unsigned long)location16 >= COREB_L1_DATA_A_START) {
348 printk(KERN_ERR "module %s: cannot relocate in L1: %u (SMP kernel)",
349 mod->name, ELF32_R_TYPE(rel[i].r_info));
350 return -ENOEXEC;
351 }
352#endif
Bryan Wu1394f032007-05-06 14:50:22 -0700353 switch (ELF32_R_TYPE(rel[i].r_info)) {
354
355 case R_pcrel24:
356 case R_pcrel24_jump_l:
357 /* Add the value, subtract its postition */
358 location16 =
359 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].
360 sh_addr + rel[i].r_offset - 2);
361 location32 = (uint32_t *) location16;
362 value -= (uint32_t) location32;
363 value >>= 1;
364 pr_debug("value is %x, before %x-%x after %x-%x\n", value,
365 *location16, *(location16 + 1),
366 (*location16 & 0xff00) | (value >> 16 & 0x00ff),
367 value & 0xffff);
368 *location16 =
369 (*location16 & 0xff00) | (value >> 16 & 0x00ff);
370 *(location16 + 1) = value & 0xffff;
371 break;
372 case R_pcrel12_jump:
373 case R_pcrel12_jump_s:
374 value -= (uint32_t) location32;
375 value >>= 1;
376 *location16 = (value & 0xfff);
377 break;
378 case R_pcrel10:
379 value -= (uint32_t) location32;
380 value >>= 1;
381 *location16 = (value & 0x3ff);
382 break;
383 case R_luimm16:
384 pr_debug("before %x after %x\n", *location16,
385 (value & 0xffff));
386 tmp = (value & 0xffff);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800387 if ((unsigned long)location16 >= L1_CODE_START) {
Bryan Wu1394f032007-05-06 14:50:22 -0700388 dma_memcpy(location16, &tmp, 2);
389 } else
390 *location16 = tmp;
391 break;
392 case R_huimm16:
393 pr_debug("before %x after %x\n", *location16,
394 ((value >> 16) & 0xffff));
395 tmp = ((value >> 16) & 0xffff);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800396 if ((unsigned long)location16 >= L1_CODE_START) {
Bryan Wu1394f032007-05-06 14:50:22 -0700397 dma_memcpy(location16, &tmp, 2);
398 } else
399 *location16 = tmp;
400 break;
401 case R_rimm16:
402 *location16 = (value & 0xffff);
403 break;
404 case R_byte4_data:
405 pr_debug("before %x after %x\n", *location32, value);
406 *location32 = value;
407 break;
408 case R_push:
409 reloc_stack_push(value);
410 break;
411 case R_const:
412 reloc_stack_push(rel[i].r_addend);
413 break;
414 case R_add:
415 case R_sub:
416 case R_mult:
417 case R_div:
418 case R_mod:
419 case R_lshift:
420 case R_rshift:
421 case R_and:
422 case R_or:
423 case R_xor:
424 case R_land:
425 case R_lor:
426 case R_neg:
427 case R_comp:
428 reloc_stack_operate(ELF32_R_TYPE(rel[i].r_info), mod);
429 break;
430 default:
431 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
432 mod->name, ELF32_R_TYPE(rel[i].r_info));
433 return -ENOEXEC;
434 }
435 }
436 return 0;
437}
438
439int
440module_finalize(const Elf_Ehdr * hdr,
441 const Elf_Shdr * sechdrs, struct module *mod)
442{
443 unsigned int i, strindex = 0, symindex = 0;
444 char *secstrings;
Graf Yang8f658732008-11-18 17:48:22 +0800445 long err = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700446
447 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
448
449 for (i = 1; i < hdr->e_shnum; i++) {
450 /* Internal symbols and strings. */
451 if (sechdrs[i].sh_type == SHT_SYMTAB) {
452 symindex = i;
453 strindex = sechdrs[i].sh_link;
454 }
455 }
456
457 for (i = 1; i < hdr->e_shnum; i++) {
458 const char *strtab = (char *)sechdrs[strindex].sh_addr;
459 unsigned int info = sechdrs[i].sh_info;
460
461 /* Not a valid relocation section? */
462 if (info >= hdr->e_shnum)
463 continue;
464
465 if ((sechdrs[i].sh_type == SHT_RELA) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800466 ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) ||
467 (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800468 ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800469 (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) {
Graf Yang8f658732008-11-18 17:48:22 +0800470 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700471 symindex, i, mod);
Graf Yang8f658732008-11-18 17:48:22 +0800472 if (err < 0)
473 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700474 }
475 }
476 return 0;
477}
478
479void module_arch_cleanup(struct module *mod)
480{
Sonic Zhang262c3822008-07-19 15:42:41 +0800481 l1_inst_sram_free(mod->arch.text_l1);
482 l1_data_A_sram_free(mod->arch.data_a_l1);
483 l1_data_A_sram_free(mod->arch.bss_a_l1);
484 l1_data_B_sram_free(mod->arch.data_b_l1);
485 l1_data_B_sram_free(mod->arch.bss_b_l1);
486 l2_sram_free(mod->arch.text_l2);
487 l2_sram_free(mod->arch.data_l2);
488 l2_sram_free(mod->arch.bss_l2);
Bryan Wu1394f032007-05-06 14:50:22 -0700489}