blob: 67fc7a56c865211cef10d339f0dc4fecac696336 [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
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000030#define pr_fmt(fmt) "module %s: " fmt
Bryan Wu1394f032007-05-06 14:50:22 -070031
32#include <linux/moduleloader.h>
33#include <linux/elf.h>
34#include <linux/vmalloc.h>
35#include <linux/fs.h>
36#include <linux/string.h>
37#include <linux/kernel.h>
38#include <asm/dma.h>
39#include <asm/cacheflush.h>
Mike Frysingera7690942009-06-26 00:49:51 +000040#include <asm/uaccess.h>
Bryan Wu1394f032007-05-06 14:50:22 -070041
Bryan Wu1394f032007-05-06 14:50:22 -070042void *module_alloc(unsigned long size)
43{
44 if (size == 0)
45 return NULL;
46 return vmalloc(size);
47}
48
49/* Free memory returned from module_alloc */
50void module_free(struct module *mod, void *module_region)
51{
52 vfree(module_region);
53}
54
55/* Transfer the section to the L1 memory */
56int
Mike Frysinger459fec92009-06-26 00:48:33 +000057module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
Bryan Wu1394f032007-05-06 14:50:22 -070058 char *secstrings, struct module *mod)
59{
Meihui Fan96a87e22008-05-07 11:41:26 +080060 /*
61 * XXX: sechdrs are vmalloced in kernel/module.c
62 * and would be vfreed just after module is loaded,
63 * so we hack to keep the only information we needed
64 * in mod->arch to correctly free L1 I/D sram later.
65 * NOTE: this breaks the semantic of mod->arch structure.
66 */
Bryan Wu1394f032007-05-06 14:50:22 -070067 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
Mike Frysinger459fec92009-06-26 00:48:33 +000068 void *dest;
Bryan Wu1394f032007-05-06 14:50:22 -070069
70 for (s = sechdrs; s < sechdrs_end; ++s) {
Mike Frysinger459fec92009-06-26 00:48:33 +000071 const char *shname = secstrings + s->sh_name;
72
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
Bryan Wu1394f032007-05-06 14:50:22 -070080 dest = l1_inst_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080081 mod->arch.text_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070082 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000083 pr_err("L1 inst memory allocation failed\n",
84 mod->name);
Bryan Wu1394f032007-05-06 14:50:22 -070085 return -1;
86 }
87 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +000088
89 } else if (!strcmp(".l1.data", shname) ||
90 (!strcmp(".data", shname) &&
91 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
92
Bryan Wu1394f032007-05-06 14:50:22 -070093 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080094 mod->arch.data_a_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", shname) ||
103 (!strcmp(".bss", shname) &&
104 (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
105
Mike Frysinger70deca92009-06-26 00:37:40 +0000106 dest = l1_data_sram_zalloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800107 mod->arch.bss_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700108 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000109 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700110 mod->name);
111 return -1;
112 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000113
114 } else if (!strcmp(".l1.data.B", shname)) {
115
Bryan Wu1394f032007-05-06 14:50:22 -0700116 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800117 mod->arch.data_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700118 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000119 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700120 mod->name);
121 return -1;
122 }
123 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000124
125 } else if (!strcmp(".l1.bss.B", shname)) {
126
Bryan Wu1394f032007-05-06 14:50:22 -0700127 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800128 mod->arch.bss_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700129 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000130 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700131 mod->name);
132 return -1;
133 }
134 memset(dest, 0, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000135
136 } else if (!strcmp(".l2.text", shname) ||
137 (!strcmp(".text", shname) &&
138 (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
139
Sonic Zhang262c3822008-07-19 15:42:41 +0800140 dest = l2_sram_alloc(s->sh_size);
141 mod->arch.text_l2 = dest;
142 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000143 pr_err("L2 SRAM allocation failed\n",
144 mod->name);
Sonic Zhang262c3822008-07-19 15:42:41 +0800145 return -1;
146 }
147 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000148
149 } else if (!strcmp(".l2.data", shname) ||
150 (!strcmp(".data", shname) &&
151 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
152
Sonic Zhang262c3822008-07-19 15:42:41 +0800153 dest = l2_sram_alloc(s->sh_size);
154 mod->arch.data_l2 = dest;
155 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000156 pr_err("L2 SRAM allocation failed\n",
Sonic Zhang262c3822008-07-19 15:42:41 +0800157 mod->name);
158 return -1;
159 }
160 memcpy(dest, (void *)s->sh_addr, s->sh_size);
Mike Frysinger459fec92009-06-26 00:48:33 +0000161
162 } else if (!strcmp(".l2.bss", shname) ||
163 (!strcmp(".bss", shname) &&
164 (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
165
Mike Frysinger70deca92009-06-26 00:37:40 +0000166 dest = l2_sram_zalloc(s->sh_size);
Sonic Zhang262c3822008-07-19 15:42:41 +0800167 mod->arch.bss_l2 = dest;
168 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000169 pr_err("L2 SRAM allocation failed\n",
Sonic Zhang262c3822008-07-19 15:42:41 +0800170 mod->name);
171 return -1;
172 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000173
174 } else
175 continue;
176
177 s->sh_flags &= ~SHF_ALLOC;
178 s->sh_addr = (unsigned long)dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700179 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000180
Bryan Wu1394f032007-05-06 14:50:22 -0700181 return 0;
182}
183
184int
185apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
186 unsigned int symindex, unsigned int relsec, struct module *me)
187{
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000188 pr_err(".rel unsupported\n", me->name);
Bryan Wu1394f032007-05-06 14:50:22 -0700189 return -ENOEXEC;
190}
191
192/*************************************************************************/
193/* FUNCTION : apply_relocate_add */
194/* ABSTRACT : Blackfin specific relocation handling for the loadable */
195/* modules. Modules are expected to be .o files. */
196/* Arithmetic relocations are handled. */
197/* We do not expect LSETUP to be split and hence is not */
198/* handled. */
Mike Frysinger595d6812009-06-02 00:35:33 +0000199/* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
200/* gas does not generate it. */
Bryan Wu1394f032007-05-06 14:50:22 -0700201/*************************************************************************/
202int
Mike Frysingera7690942009-06-26 00:49:51 +0000203apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700204 unsigned int symindex, unsigned int relsec,
205 struct module *mod)
206{
207 unsigned int i;
Bryan Wu1394f032007-05-06 14:50:22 -0700208 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
209 Elf32_Sym *sym;
Mike Frysingera7690942009-06-26 00:49:51 +0000210 unsigned long location, value, size;
Bryan Wu1394f032007-05-06 14:50:22 -0700211
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000212 pr_debug("applying relocate section %u to %u\n", mod->name,
213 relsec, sechdrs[relsec].sh_info);
Mike Frysingera7690942009-06-26 00:49:51 +0000214
Bryan Wu1394f032007-05-06 14:50:22 -0700215 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
216 /* This is where to make the change */
Mike Frysingera7690942009-06-26 00:49:51 +0000217 location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
218 rel[i].r_offset;
219
Bryan Wu1394f032007-05-06 14:50:22 -0700220 /* This is the symbol it is referring to. Note that all
221 undefined symbols have been resolved. */
222 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
223 + ELF32_R_SYM(rel[i].r_info);
Bernd Schmidtd1a85302009-01-07 23:14:39 +0800224 value = sym->st_value;
Bryan Wu1394f032007-05-06 14:50:22 -0700225 value += rel[i].r_addend;
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000226
Graf Yang8f658732008-11-18 17:48:22 +0800227#ifdef CONFIG_SMP
Mike Frysingera7690942009-06-26 00:49:51 +0000228 if (location >= COREB_L1_DATA_A_START) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000229 pr_err("cannot relocate in L1: %u (SMP kernel)",
230 mod->name, ELF32_R_TYPE(rel[i].r_info));
Graf Yang8f658732008-11-18 17:48:22 +0800231 return -ENOEXEC;
232 }
233#endif
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000234
Mike Frysingera7690942009-06-26 00:49:51 +0000235 pr_debug("location is %lx, value is %lx type is %d\n",
236 mod->name, location, value, ELF32_R_TYPE(rel[i].r_info));
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000237
Bryan Wu1394f032007-05-06 14:50:22 -0700238 switch (ELF32_R_TYPE(rel[i].r_info)) {
239
Mike Frysinger595d6812009-06-02 00:35:33 +0000240 case R_BFIN_HUIMM16:
Mike Frysingera7690942009-06-26 00:49:51 +0000241 value >>= 16;
242 case R_BFIN_LUIMM16:
Mike Frysinger595d6812009-06-02 00:35:33 +0000243 case R_BFIN_RIMM16:
Mike Frysingera7690942009-06-26 00:49:51 +0000244 size = 2;
Bryan Wu1394f032007-05-06 14:50:22 -0700245 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000246 case R_BFIN_BYTE4_DATA:
Mike Frysingera7690942009-06-26 00:49:51 +0000247 size = 4;
Bryan Wu1394f032007-05-06 14:50:22 -0700248 break;
Mike Frysingera7690942009-06-26 00:49:51 +0000249
Robin Getz22532572009-06-25 15:49:38 +0000250 case R_BFIN_PCREL24:
251 case R_BFIN_PCREL24_JUMP_L:
252 case R_BFIN_PCREL12_JUMP:
253 case R_BFIN_PCREL12_JUMP_S:
254 case R_BFIN_PCREL10:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000255 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
Robin Getz22532572009-06-25 15:49:38 +0000256 mod->name, ELF32_R_TYPE(rel[i].r_info));
257 return -ENOEXEC;
Mike Frysingera7690942009-06-26 00:49:51 +0000258
Bryan Wu1394f032007-05-06 14:50:22 -0700259 default:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000260 pr_err("unknown relocation: %u\n", mod->name,
261 ELF32_R_TYPE(rel[i].r_info));
Bryan Wu1394f032007-05-06 14:50:22 -0700262 return -ENOEXEC;
263 }
Mike Frysingera7690942009-06-26 00:49:51 +0000264
265 switch (bfin_mem_access_type(location, size)) {
266 case BFIN_MEM_ACCESS_CORE:
267 case BFIN_MEM_ACCESS_CORE_ONLY:
268 memcpy((void *)location, &value, size);
269 break;
270 case BFIN_MEM_ACCESS_DMA:
271 dma_memcpy((void *)location, &value, size);
272 break;
273 case BFIN_MEM_ACCESS_ITEST:
274 isram_memcpy((void *)location, &value, size);
275 break;
276 default:
277 pr_err("invalid relocation for %#lx\n",
278 mod->name, location);
279 return -ENOEXEC;
280 }
Bryan Wu1394f032007-05-06 14:50:22 -0700281 }
Mike Frysingera7690942009-06-26 00:49:51 +0000282
Bryan Wu1394f032007-05-06 14:50:22 -0700283 return 0;
284}
285
286int
287module_finalize(const Elf_Ehdr * hdr,
288 const Elf_Shdr * sechdrs, struct module *mod)
289{
290 unsigned int i, strindex = 0, symindex = 0;
291 char *secstrings;
Graf Yang8f658732008-11-18 17:48:22 +0800292 long err = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700293
294 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
295
296 for (i = 1; i < hdr->e_shnum; i++) {
297 /* Internal symbols and strings. */
298 if (sechdrs[i].sh_type == SHT_SYMTAB) {
299 symindex = i;
300 strindex = sechdrs[i].sh_link;
301 }
302 }
303
304 for (i = 1; i < hdr->e_shnum; i++) {
305 const char *strtab = (char *)sechdrs[strindex].sh_addr;
306 unsigned int info = sechdrs[i].sh_info;
Mike Frysinger459fec92009-06-26 00:48:33 +0000307 const char *shname = secstrings + sechdrs[i].sh_name;
Bryan Wu1394f032007-05-06 14:50:22 -0700308
309 /* Not a valid relocation section? */
310 if (info >= hdr->e_shnum)
311 continue;
312
Mike Frysinger459fec92009-06-26 00:48:33 +0000313 /* Only support RELA relocation types */
314 if (sechdrs[i].sh_type != SHT_RELA)
315 continue;
316
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
Graf Yang8f658732008-11-18 17:48:22 +0800322 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700323 symindex, i, mod);
Graf Yang8f658732008-11-18 17:48:22 +0800324 if (err < 0)
325 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700326 }
327 }
Mike Frysinger459fec92009-06-26 00:48:33 +0000328
Bryan Wu1394f032007-05-06 14:50:22 -0700329 return 0;
330}
331
332void module_arch_cleanup(struct module *mod)
333{
Sonic Zhang262c3822008-07-19 15:42:41 +0800334 l1_inst_sram_free(mod->arch.text_l1);
335 l1_data_A_sram_free(mod->arch.data_a_l1);
336 l1_data_A_sram_free(mod->arch.bss_a_l1);
337 l1_data_B_sram_free(mod->arch.data_b_l1);
338 l1_data_B_sram_free(mod->arch.bss_b_l1);
339 l2_sram_free(mod->arch.text_l2);
340 l2_sram_free(mod->arch.data_l2);
341 l2_sram_free(mod->arch.bss_l2);
Bryan Wu1394f032007-05-06 14:50:22 -0700342}