blob: a6dfa6b71e63c89fa45f2cd260db215cd61d525e [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * Copyright 2004-2009 Analog Devices Inc.
Bryan Wu1394f032007-05-06 14:50:22 -07003 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Licensed under the GPL-2 or later
Bryan Wu1394f032007-05-06 14:50:22 -07005 */
6
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +00007#define pr_fmt(fmt) "module %s: " fmt
Bryan Wu1394f032007-05-06 14:50:22 -07008
9#include <linux/moduleloader.h>
10#include <linux/elf.h>
11#include <linux/vmalloc.h>
12#include <linux/fs.h>
13#include <linux/string.h>
14#include <linux/kernel.h>
15#include <asm/dma.h>
16#include <asm/cacheflush.h>
Mike Frysingera7690942009-06-26 00:49:51 +000017#include <asm/uaccess.h>
Bryan Wu1394f032007-05-06 14:50:22 -070018
Bryan Wu1394f032007-05-06 14:50:22 -070019void *module_alloc(unsigned long size)
20{
21 if (size == 0)
22 return NULL;
23 return vmalloc(size);
24}
25
26/* Free memory returned from module_alloc */
27void module_free(struct module *mod, void *module_region)
28{
29 vfree(module_region);
30}
31
32/* Transfer the section to the L1 memory */
33int
Mike Frysinger459fec92009-06-26 00:48:33 +000034module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
Bryan Wu1394f032007-05-06 14:50:22 -070035 char *secstrings, struct module *mod)
36{
Meihui Fan96a87e22008-05-07 11:41:26 +080037 /*
38 * XXX: sechdrs are vmalloced in kernel/module.c
39 * and would be vfreed just after module is loaded,
40 * so we hack to keep the only information we needed
41 * in mod->arch to correctly free L1 I/D sram later.
42 * NOTE: this breaks the semantic of mod->arch structure.
43 */
Bryan Wu1394f032007-05-06 14:50:22 -070044 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
Mike Frysinger459fec92009-06-26 00:48:33 +000045 void *dest;
Bryan Wu1394f032007-05-06 14:50:22 -070046
47 for (s = sechdrs; s < sechdrs_end; ++s) {
Mike Frysinger459fec92009-06-26 00:48:33 +000048 const char *shname = secstrings + s->sh_name;
49
50 if (s->sh_size == 0)
51 continue;
52
53 if (!strcmp(".l1.text", shname) ||
54 (!strcmp(".text", shname) &&
55 (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
56
Bryan Wu1394f032007-05-06 14:50:22 -070057 dest = l1_inst_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080058 mod->arch.text_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070059 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000060 pr_err("L1 inst memory allocation failed\n",
61 mod->name);
Bryan Wu1394f032007-05-06 14:50:22 -070062 return -1;
63 }
64 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000065
66 } else if (!strcmp(".l1.data", shname) ||
67 (!strcmp(".data", shname) &&
68 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
69
Bryan Wu1394f032007-05-06 14:50:22 -070070 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080071 mod->arch.data_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070072 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000073 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -070074 mod->name);
75 return -1;
76 }
77 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000078
79 } else if (!strcmp(".l1.bss", shname) ||
80 (!strcmp(".bss", shname) &&
81 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
82
Mike Frysinger70deca92009-06-26 00:37:40 +000083 dest = l1_data_sram_zalloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080084 mod->arch.bss_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070085 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000086 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -070087 mod->name);
88 return -1;
89 }
Mike Frysinger459fec92009-06-26 00:48:33 +000090
91 } else if (!strcmp(".l1.data.B", shname)) {
92
Bryan Wu1394f032007-05-06 14:50:22 -070093 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080094 mod->arch.data_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070095 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000096 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -070097 mod->name);
98 return -1;
99 }
100 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000101
102 } else if (!strcmp(".l1.bss.B", shname)) {
103
Bryan Wu1394f032007-05-06 14:50:22 -0700104 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800105 mod->arch.bss_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700106 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000107 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700108 mod->name);
109 return -1;
110 }
111 memset(dest, 0, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000112
113 } else if (!strcmp(".l2.text", shname) ||
114 (!strcmp(".text", shname) &&
115 (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
116
Sonic Zhang262c3822008-07-19 15:42:41 +0800117 dest = l2_sram_alloc(s->sh_size);
118 mod->arch.text_l2 = dest;
119 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000120 pr_err("L2 SRAM allocation failed\n",
121 mod->name);
Sonic Zhang262c3822008-07-19 15:42:41 +0800122 return -1;
123 }
124 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000125
126 } else if (!strcmp(".l2.data", shname) ||
127 (!strcmp(".data", shname) &&
128 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
129
Sonic Zhang262c3822008-07-19 15:42:41 +0800130 dest = l2_sram_alloc(s->sh_size);
131 mod->arch.data_l2 = dest;
132 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000133 pr_err("L2 SRAM allocation failed\n",
Sonic Zhang262c3822008-07-19 15:42:41 +0800134 mod->name);
135 return -1;
136 }
137 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000138
139 } else if (!strcmp(".l2.bss", shname) ||
140 (!strcmp(".bss", shname) &&
141 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
142
Mike Frysinger70deca92009-06-26 00:37:40 +0000143 dest = l2_sram_zalloc(s->sh_size);
Sonic Zhang262c3822008-07-19 15:42:41 +0800144 mod->arch.bss_l2 = dest;
145 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000146 pr_err("L2 SRAM allocation failed\n",
Sonic Zhang262c3822008-07-19 15:42:41 +0800147 mod->name);
148 return -1;
149 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000150
151 } else
152 continue;
153
154 s->sh_flags &= ~SHF_ALLOC;
155 s->sh_addr = (unsigned long)dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700156 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000157
Bryan Wu1394f032007-05-06 14:50:22 -0700158 return 0;
159}
160
161int
162apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
163 unsigned int symindex, unsigned int relsec, struct module *me)
164{
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000165 pr_err(".rel unsupported\n", me->name);
Bryan Wu1394f032007-05-06 14:50:22 -0700166 return -ENOEXEC;
167}
168
169/*************************************************************************/
170/* FUNCTION : apply_relocate_add */
171/* ABSTRACT : Blackfin specific relocation handling for the loadable */
172/* modules. Modules are expected to be .o files. */
173/* Arithmetic relocations are handled. */
174/* We do not expect LSETUP to be split and hence is not */
175/* handled. */
Mike Frysinger595d6812009-06-02 00:35:33 +0000176/* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
177/* gas does not generate it. */
Bryan Wu1394f032007-05-06 14:50:22 -0700178/*************************************************************************/
179int
Mike Frysingera7690942009-06-26 00:49:51 +0000180apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700181 unsigned int symindex, unsigned int relsec,
182 struct module *mod)
183{
184 unsigned int i;
Bryan Wu1394f032007-05-06 14:50:22 -0700185 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
186 Elf32_Sym *sym;
Mike Frysingera7690942009-06-26 00:49:51 +0000187 unsigned long location, value, size;
Bryan Wu1394f032007-05-06 14:50:22 -0700188
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000189 pr_debug("applying relocate section %u to %u\n", mod->name,
190 relsec, sechdrs[relsec].sh_info);
Mike Frysingera7690942009-06-26 00:49:51 +0000191
Bryan Wu1394f032007-05-06 14:50:22 -0700192 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
193 /* This is where to make the change */
Mike Frysingera7690942009-06-26 00:49:51 +0000194 location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
195 rel[i].r_offset;
196
Bryan Wu1394f032007-05-06 14:50:22 -0700197 /* This is the symbol it is referring to. Note that all
198 undefined symbols have been resolved. */
199 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
200 + ELF32_R_SYM(rel[i].r_info);
Bernd Schmidtd1a85302009-01-07 23:14:39 +0800201 value = sym->st_value;
Bryan Wu1394f032007-05-06 14:50:22 -0700202 value += rel[i].r_addend;
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000203
Graf Yang8f658732008-11-18 17:48:22 +0800204#ifdef CONFIG_SMP
Mike Frysingera7690942009-06-26 00:49:51 +0000205 if (location >= COREB_L1_DATA_A_START) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000206 pr_err("cannot relocate in L1: %u (SMP kernel)",
207 mod->name, ELF32_R_TYPE(rel[i].r_info));
Graf Yang8f658732008-11-18 17:48:22 +0800208 return -ENOEXEC;
209 }
210#endif
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000211
Mike Frysingera7690942009-06-26 00:49:51 +0000212 pr_debug("location is %lx, value is %lx type is %d\n",
213 mod->name, location, value, ELF32_R_TYPE(rel[i].r_info));
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000214
Bryan Wu1394f032007-05-06 14:50:22 -0700215 switch (ELF32_R_TYPE(rel[i].r_info)) {
216
Mike Frysinger595d6812009-06-02 00:35:33 +0000217 case R_BFIN_HUIMM16:
Mike Frysingera7690942009-06-26 00:49:51 +0000218 value >>= 16;
219 case R_BFIN_LUIMM16:
Mike Frysinger595d6812009-06-02 00:35:33 +0000220 case R_BFIN_RIMM16:
Mike Frysingera7690942009-06-26 00:49:51 +0000221 size = 2;
Bryan Wu1394f032007-05-06 14:50:22 -0700222 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000223 case R_BFIN_BYTE4_DATA:
Mike Frysingera7690942009-06-26 00:49:51 +0000224 size = 4;
Bryan Wu1394f032007-05-06 14:50:22 -0700225 break;
Mike Frysingera7690942009-06-26 00:49:51 +0000226
Robin Getz22532572009-06-25 15:49:38 +0000227 case R_BFIN_PCREL24:
228 case R_BFIN_PCREL24_JUMP_L:
229 case R_BFIN_PCREL12_JUMP:
230 case R_BFIN_PCREL12_JUMP_S:
231 case R_BFIN_PCREL10:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000232 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
Robin Getz22532572009-06-25 15:49:38 +0000233 mod->name, ELF32_R_TYPE(rel[i].r_info));
234 return -ENOEXEC;
Mike Frysingera7690942009-06-26 00:49:51 +0000235
Bryan Wu1394f032007-05-06 14:50:22 -0700236 default:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000237 pr_err("unknown relocation: %u\n", mod->name,
238 ELF32_R_TYPE(rel[i].r_info));
Bryan Wu1394f032007-05-06 14:50:22 -0700239 return -ENOEXEC;
240 }
Mike Frysingera7690942009-06-26 00:49:51 +0000241
242 switch (bfin_mem_access_type(location, size)) {
243 case BFIN_MEM_ACCESS_CORE:
244 case BFIN_MEM_ACCESS_CORE_ONLY:
245 memcpy((void *)location, &value, size);
246 break;
247 case BFIN_MEM_ACCESS_DMA:
248 dma_memcpy((void *)location, &value, size);
249 break;
250 case BFIN_MEM_ACCESS_ITEST:
251 isram_memcpy((void *)location, &value, size);
252 break;
253 default:
254 pr_err("invalid relocation for %#lx\n",
255 mod->name, location);
256 return -ENOEXEC;
257 }
Bryan Wu1394f032007-05-06 14:50:22 -0700258 }
Mike Frysingera7690942009-06-26 00:49:51 +0000259
Bryan Wu1394f032007-05-06 14:50:22 -0700260 return 0;
261}
262
263int
264module_finalize(const Elf_Ehdr * hdr,
265 const Elf_Shdr * sechdrs, struct module *mod)
266{
267 unsigned int i, strindex = 0, symindex = 0;
268 char *secstrings;
Graf Yang8f658732008-11-18 17:48:22 +0800269 long err = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700270
271 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
272
273 for (i = 1; i < hdr->e_shnum; i++) {
274 /* Internal symbols and strings. */
275 if (sechdrs[i].sh_type == SHT_SYMTAB) {
276 symindex = i;
277 strindex = sechdrs[i].sh_link;
278 }
279 }
280
281 for (i = 1; i < hdr->e_shnum; i++) {
282 const char *strtab = (char *)sechdrs[strindex].sh_addr;
283 unsigned int info = sechdrs[i].sh_info;
Mike Frysinger459fec92009-06-26 00:48:33 +0000284 const char *shname = secstrings + sechdrs[i].sh_name;
Bryan Wu1394f032007-05-06 14:50:22 -0700285
286 /* Not a valid relocation section? */
287 if (info >= hdr->e_shnum)
288 continue;
289
Mike Frysinger459fec92009-06-26 00:48:33 +0000290 /* Only support RELA relocation types */
291 if (sechdrs[i].sh_type != SHT_RELA)
292 continue;
293
294 if (!strcmp(".rela.l2.text", shname) ||
295 !strcmp(".rela.l1.text", shname) ||
296 (!strcmp(".rela.text", shname) &&
297 (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
298
Graf Yang8f658732008-11-18 17:48:22 +0800299 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700300 symindex, i, mod);
Graf Yang8f658732008-11-18 17:48:22 +0800301 if (err < 0)
302 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700303 }
304 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000305
Bryan Wu1394f032007-05-06 14:50:22 -0700306 return 0;
307}
308
309void module_arch_cleanup(struct module *mod)
310{
Sonic Zhang262c3822008-07-19 15:42:41 +0800311 l1_inst_sram_free(mod->arch.text_l1);
312 l1_data_A_sram_free(mod->arch.data_a_l1);
313 l1_data_A_sram_free(mod->arch.bss_a_l1);
314 l1_data_B_sram_free(mod->arch.data_b_l1);
315 l1_data_B_sram_free(mod->arch.bss_b_l1);
316 l2_sram_free(mod->arch.text_l2);
317 l2_sram_free(mod->arch.data_l2);
318 l2_sram_free(mod->arch.bss_l2);
Bryan Wu1394f032007-05-06 14:50:22 -0700319}