blob: 35e350cad9d943a0c733c2488dfef18f19a30f9e [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
Joe Perchesb75a9e62010-10-20 11:11:51 -07007#define pr_fmt(fmt) "module %s: " fmt, mod->name
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) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070060 pr_err("L1 inst memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070061 return -1;
62 }
63 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000064
65 } else if (!strcmp(".l1.data", shname) ||
66 (!strcmp(".data", shname) &&
67 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
68
Bryan Wu1394f032007-05-06 14:50:22 -070069 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080070 mod->arch.data_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070071 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070072 pr_err("L1 data memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070073 return -1;
74 }
75 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000076
77 } else if (!strcmp(".l1.bss", shname) ||
78 (!strcmp(".bss", shname) &&
79 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
80
Mike Frysinger70deca92009-06-26 00:37:40 +000081 dest = l1_data_sram_zalloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080082 mod->arch.bss_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070083 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070084 pr_err("L1 data memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070085 return -1;
86 }
Mike Frysinger459fec92009-06-26 00:48:33 +000087
88 } else if (!strcmp(".l1.data.B", shname)) {
89
Bryan Wu1394f032007-05-06 14:50:22 -070090 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080091 mod->arch.data_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070092 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -070093 pr_err("L1 data memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -070094 return -1;
95 }
96 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000097
98 } else if (!strcmp(".l1.bss.B", shname)) {
99
Bryan Wu1394f032007-05-06 14:50:22 -0700100 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800101 mod->arch.bss_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700102 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700103 pr_err("L1 data memory allocation failed\n");
Bryan Wu1394f032007-05-06 14:50:22 -0700104 return -1;
105 }
106 memset(dest, 0, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000107
108 } else if (!strcmp(".l2.text", shname) ||
109 (!strcmp(".text", shname) &&
110 (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
111
Sonic Zhang262c3822008-07-19 15:42:41 +0800112 dest = l2_sram_alloc(s->sh_size);
113 mod->arch.text_l2 = dest;
114 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700115 pr_err("L2 SRAM allocation failed\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800116 return -1;
117 }
118 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000119
120 } else if (!strcmp(".l2.data", shname) ||
121 (!strcmp(".data", shname) &&
122 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
123
Sonic Zhang262c3822008-07-19 15:42:41 +0800124 dest = l2_sram_alloc(s->sh_size);
125 mod->arch.data_l2 = dest;
126 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700127 pr_err("L2 SRAM allocation failed\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800128 return -1;
129 }
130 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000131
132 } else if (!strcmp(".l2.bss", shname) ||
133 (!strcmp(".bss", shname) &&
134 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
135
Mike Frysinger70deca92009-06-26 00:37:40 +0000136 dest = l2_sram_zalloc(s->sh_size);
Sonic Zhang262c3822008-07-19 15:42:41 +0800137 mod->arch.bss_l2 = dest;
138 if (dest == NULL) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700139 pr_err("L2 SRAM allocation failed\n");
Sonic Zhang262c3822008-07-19 15:42:41 +0800140 return -1;
141 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000142
143 } else
144 continue;
145
146 s->sh_flags &= ~SHF_ALLOC;
147 s->sh_addr = (unsigned long)dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700148 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000149
Bryan Wu1394f032007-05-06 14:50:22 -0700150 return 0;
151}
152
153int
154apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
Joe Perchesb75a9e62010-10-20 11:11:51 -0700155 unsigned int symindex, unsigned int relsec, struct module *mod)
Bryan Wu1394f032007-05-06 14:50:22 -0700156{
Joe Perchesb75a9e62010-10-20 11:11:51 -0700157 pr_err(".rel unsupported\n");
Bryan Wu1394f032007-05-06 14:50:22 -0700158 return -ENOEXEC;
159}
160
161/*************************************************************************/
162/* FUNCTION : apply_relocate_add */
163/* ABSTRACT : Blackfin specific relocation handling for the loadable */
164/* modules. Modules are expected to be .o files. */
165/* Arithmetic relocations are handled. */
166/* We do not expect LSETUP to be split and hence is not */
167/* handled. */
Mike Frysinger595d6812009-06-02 00:35:33 +0000168/* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
169/* gas does not generate it. */
Bryan Wu1394f032007-05-06 14:50:22 -0700170/*************************************************************************/
171int
Mike Frysingera7690942009-06-26 00:49:51 +0000172apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700173 unsigned int symindex, unsigned int relsec,
174 struct module *mod)
175{
176 unsigned int i;
Bryan Wu1394f032007-05-06 14:50:22 -0700177 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
178 Elf32_Sym *sym;
Mike Frysingera7690942009-06-26 00:49:51 +0000179 unsigned long location, value, size;
Bryan Wu1394f032007-05-06 14:50:22 -0700180
Joe Perchesb75a9e62010-10-20 11:11:51 -0700181 pr_debug("applying relocate section %u to %u\n",
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000182 relsec, sechdrs[relsec].sh_info);
Mike Frysingera7690942009-06-26 00:49:51 +0000183
Bryan Wu1394f032007-05-06 14:50:22 -0700184 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
185 /* This is where to make the change */
Mike Frysingera7690942009-06-26 00:49:51 +0000186 location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
187 rel[i].r_offset;
188
Bryan Wu1394f032007-05-06 14:50:22 -0700189 /* This is the symbol it is referring to. Note that all
190 undefined symbols have been resolved. */
191 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
192 + ELF32_R_SYM(rel[i].r_info);
Bernd Schmidtd1a85302009-01-07 23:14:39 +0800193 value = sym->st_value;
Bryan Wu1394f032007-05-06 14:50:22 -0700194 value += rel[i].r_addend;
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000195
Graf Yang8f658732008-11-18 17:48:22 +0800196#ifdef CONFIG_SMP
Mike Frysingera7690942009-06-26 00:49:51 +0000197 if (location >= COREB_L1_DATA_A_START) {
Joe Perchesb75a9e62010-10-20 11:11:51 -0700198 pr_err("cannot relocate in L1: %u (SMP kernel)\n",
199 ELF32_R_TYPE(rel[i].r_info));
Graf Yang8f658732008-11-18 17:48:22 +0800200 return -ENOEXEC;
201 }
202#endif
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000203
Mike Frysingera7690942009-06-26 00:49:51 +0000204 pr_debug("location is %lx, value is %lx type is %d\n",
Joe Perchesb75a9e62010-10-20 11:11:51 -0700205 location, value, ELF32_R_TYPE(rel[i].r_info));
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000206
Bryan Wu1394f032007-05-06 14:50:22 -0700207 switch (ELF32_R_TYPE(rel[i].r_info)) {
208
Mike Frysinger595d6812009-06-02 00:35:33 +0000209 case R_BFIN_HUIMM16:
Mike Frysingera7690942009-06-26 00:49:51 +0000210 value >>= 16;
211 case R_BFIN_LUIMM16:
Mike Frysinger595d6812009-06-02 00:35:33 +0000212 case R_BFIN_RIMM16:
Mike Frysingera7690942009-06-26 00:49:51 +0000213 size = 2;
Bryan Wu1394f032007-05-06 14:50:22 -0700214 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000215 case R_BFIN_BYTE4_DATA:
Mike Frysingera7690942009-06-26 00:49:51 +0000216 size = 4;
Bryan Wu1394f032007-05-06 14:50:22 -0700217 break;
Mike Frysingera7690942009-06-26 00:49:51 +0000218
Robin Getz22532572009-06-25 15:49:38 +0000219 case R_BFIN_PCREL24:
220 case R_BFIN_PCREL24_JUMP_L:
221 case R_BFIN_PCREL12_JUMP:
222 case R_BFIN_PCREL12_JUMP_S:
223 case R_BFIN_PCREL10:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000224 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
Joe Perchesb75a9e62010-10-20 11:11:51 -0700225 ELF32_R_TYPE(rel[i].r_info));
Robin Getz22532572009-06-25 15:49:38 +0000226 return -ENOEXEC;
Mike Frysingera7690942009-06-26 00:49:51 +0000227
Bryan Wu1394f032007-05-06 14:50:22 -0700228 default:
Joe Perchesb75a9e62010-10-20 11:11:51 -0700229 pr_err("unknown relocation: %u\n",
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000230 ELF32_R_TYPE(rel[i].r_info));
Bryan Wu1394f032007-05-06 14:50:22 -0700231 return -ENOEXEC;
232 }
Mike Frysingera7690942009-06-26 00:49:51 +0000233
234 switch (bfin_mem_access_type(location, size)) {
235 case BFIN_MEM_ACCESS_CORE:
236 case BFIN_MEM_ACCESS_CORE_ONLY:
237 memcpy((void *)location, &value, size);
238 break;
239 case BFIN_MEM_ACCESS_DMA:
240 dma_memcpy((void *)location, &value, size);
241 break;
242 case BFIN_MEM_ACCESS_ITEST:
243 isram_memcpy((void *)location, &value, size);
244 break;
245 default:
Joe Perchesb75a9e62010-10-20 11:11:51 -0700246 pr_err("invalid relocation for %#lx\n", location);
Mike Frysingera7690942009-06-26 00:49:51 +0000247 return -ENOEXEC;
248 }
Bryan Wu1394f032007-05-06 14:50:22 -0700249 }
Mike Frysingera7690942009-06-26 00:49:51 +0000250
Bryan Wu1394f032007-05-06 14:50:22 -0700251 return 0;
252}
253
254int
255module_finalize(const Elf_Ehdr * hdr,
256 const Elf_Shdr * sechdrs, struct module *mod)
257{
258 unsigned int i, strindex = 0, symindex = 0;
259 char *secstrings;
Graf Yang8f658732008-11-18 17:48:22 +0800260 long err = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700261
262 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
263
264 for (i = 1; i < hdr->e_shnum; i++) {
265 /* Internal symbols and strings. */
266 if (sechdrs[i].sh_type == SHT_SYMTAB) {
267 symindex = i;
268 strindex = sechdrs[i].sh_link;
269 }
270 }
271
272 for (i = 1; i < hdr->e_shnum; i++) {
273 const char *strtab = (char *)sechdrs[strindex].sh_addr;
274 unsigned int info = sechdrs[i].sh_info;
Mike Frysinger459fec92009-06-26 00:48:33 +0000275 const char *shname = secstrings + sechdrs[i].sh_name;
Bryan Wu1394f032007-05-06 14:50:22 -0700276
277 /* Not a valid relocation section? */
278 if (info >= hdr->e_shnum)
279 continue;
280
Mike Frysinger459fec92009-06-26 00:48:33 +0000281 /* Only support RELA relocation types */
282 if (sechdrs[i].sh_type != SHT_RELA)
283 continue;
284
285 if (!strcmp(".rela.l2.text", shname) ||
286 !strcmp(".rela.l1.text", shname) ||
287 (!strcmp(".rela.text", shname) &&
288 (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
289
Graf Yang8f658732008-11-18 17:48:22 +0800290 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700291 symindex, i, mod);
Graf Yang8f658732008-11-18 17:48:22 +0800292 if (err < 0)
293 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700294 }
295 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000296
Bryan Wu1394f032007-05-06 14:50:22 -0700297 return 0;
298}
299
300void module_arch_cleanup(struct module *mod)
301{
Sonic Zhang262c3822008-07-19 15:42:41 +0800302 l1_inst_sram_free(mod->arch.text_l1);
303 l1_data_A_sram_free(mod->arch.data_a_l1);
304 l1_data_A_sram_free(mod->arch.bss_a_l1);
305 l1_data_B_sram_free(mod->arch.data_b_l1);
306 l1_data_B_sram_free(mod->arch.bss_b_l1);
307 l2_sram_free(mod->arch.text_l2);
308 l2_sram_free(mod->arch.data_l2);
309 l2_sram_free(mod->arch.bss_l2);
Bryan Wu1394f032007-05-06 14:50:22 -0700310}