blob: b8f4c67eb2ec4d90c1efd5bcf2a6bfaac9ab233b [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>
40
Bryan Wu1394f032007-05-06 14:50:22 -070041void *module_alloc(unsigned long size)
42{
43 if (size == 0)
44 return NULL;
45 return vmalloc(size);
46}
47
48/* Free memory returned from module_alloc */
49void module_free(struct module *mod, void *module_region)
50{
51 vfree(module_region);
52}
53
54/* Transfer the section to the L1 memory */
55int
56module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
57 char *secstrings, struct module *mod)
58{
Meihui Fan96a87e22008-05-07 11:41:26 +080059 /*
60 * XXX: sechdrs are vmalloced in kernel/module.c
61 * and would be vfreed just after module is loaded,
62 * so we hack to keep the only information we needed
63 * in mod->arch to correctly free L1 I/D sram later.
64 * NOTE: this breaks the semantic of mod->arch structure.
65 */
Bryan Wu1394f032007-05-06 14:50:22 -070066 Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
67 void *dest = NULL;
68
69 for (s = sechdrs; s < sechdrs_end; ++s) {
70 if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080071 ((strcmp(".text", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +080072 (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -070073 dest = l1_inst_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080074 mod->arch.text_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070075 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000076 pr_err("L1 inst memory allocation failed\n",
77 mod->name);
Bryan Wu1394f032007-05-06 14:50:22 -070078 return -1;
79 }
80 dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
81 s->sh_flags &= ~SHF_ALLOC;
82 s->sh_addr = (unsigned long)dest;
83 }
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080084 if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
85 ((strcmp(".data", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +080086 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -070087 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +080088 mod->arch.data_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -070089 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +000090 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -070091 mod->name);
92 return -1;
93 }
94 memcpy(dest, (void *)s->sh_addr, s->sh_size);
95 s->sh_flags &= ~SHF_ALLOC;
96 s->sh_addr = (unsigned long)dest;
97 }
98 if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080099 ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800100 (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
Bryan Wu1394f032007-05-06 14:50:22 -0700101 dest = l1_data_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800102 mod->arch.bss_a_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700103 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000104 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700105 mod->name);
106 return -1;
107 }
108 memset(dest, 0, s->sh_size);
109 s->sh_flags &= ~SHF_ALLOC;
110 s->sh_addr = (unsigned long)dest;
111 }
112 if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) {
Bryan Wu1394f032007-05-06 14:50:22 -0700113 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800114 mod->arch.data_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700115 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000116 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700117 mod->name);
118 return -1;
119 }
120 memcpy(dest, (void *)s->sh_addr, s->sh_size);
121 s->sh_flags &= ~SHF_ALLOC;
122 s->sh_addr = (unsigned long)dest;
123 }
124 if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) {
Bryan Wu1394f032007-05-06 14:50:22 -0700125 dest = l1_data_B_sram_alloc(s->sh_size);
Meihui Fan96a87e22008-05-07 11:41:26 +0800126 mod->arch.bss_b_l1 = dest;
Bryan Wu1394f032007-05-06 14:50:22 -0700127 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000128 pr_err("L1 data memory allocation failed\n",
Bryan Wu1394f032007-05-06 14:50:22 -0700129 mod->name);
130 return -1;
131 }
132 memset(dest, 0, s->sh_size);
133 s->sh_flags &= ~SHF_ALLOC;
134 s->sh_addr = (unsigned long)dest;
135 }
Sonic Zhang262c3822008-07-19 15:42:41 +0800136 if ((strcmp(".l2.text", secstrings + s->sh_name) == 0) ||
137 ((strcmp(".text", secstrings + s->sh_name) == 0) &&
138 (hdr->e_flags & EF_BFIN_CODE_IN_L2) && (s->sh_size > 0))) {
139 dest = l2_sram_alloc(s->sh_size);
140 mod->arch.text_l2 = dest;
141 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000142 pr_err("L2 SRAM allocation failed\n",
143 mod->name);
Sonic Zhang262c3822008-07-19 15:42:41 +0800144 return -1;
145 }
146 memcpy(dest, (void *)s->sh_addr, s->sh_size);
147 s->sh_flags &= ~SHF_ALLOC;
148 s->sh_addr = (unsigned long)dest;
149 }
150 if ((strcmp(".l2.data", secstrings + s->sh_name) == 0) ||
151 ((strcmp(".data", secstrings + s->sh_name) == 0) &&
152 (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
153 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);
161 s->sh_flags &= ~SHF_ALLOC;
162 s->sh_addr = (unsigned long)dest;
163 }
164 if (strcmp(".l2.bss", secstrings + s->sh_name) == 0 ||
165 ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
166 (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
167 dest = l2_sram_alloc(s->sh_size);
168 mod->arch.bss_l2 = dest;
169 if (dest == NULL) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000170 pr_err("L2 SRAM allocation failed\n",
Sonic Zhang262c3822008-07-19 15:42:41 +0800171 mod->name);
172 return -1;
173 }
174 memset(dest, 0, s->sh_size);
175 s->sh_flags &= ~SHF_ALLOC;
176 s->sh_addr = (unsigned long)dest;
177 }
Bryan Wu1394f032007-05-06 14:50:22 -0700178 }
179 return 0;
180}
181
182int
183apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
184 unsigned int symindex, unsigned int relsec, struct module *me)
185{
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000186 pr_err(".rel unsupported\n", me->name);
Bryan Wu1394f032007-05-06 14:50:22 -0700187 return -ENOEXEC;
188}
189
190/*************************************************************************/
191/* FUNCTION : apply_relocate_add */
192/* ABSTRACT : Blackfin specific relocation handling for the loadable */
193/* modules. Modules are expected to be .o files. */
194/* Arithmetic relocations are handled. */
195/* We do not expect LSETUP to be split and hence is not */
196/* handled. */
Mike Frysinger595d6812009-06-02 00:35:33 +0000197/* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
198/* gas does not generate it. */
Bryan Wu1394f032007-05-06 14:50:22 -0700199/*************************************************************************/
200int
201apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
202 unsigned int symindex, unsigned int relsec,
203 struct module *mod)
204{
205 unsigned int i;
206 unsigned short tmp;
207 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
208 Elf32_Sym *sym;
209 uint32_t *location32;
210 uint16_t *location16;
211 uint32_t value;
212
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000213 pr_debug("applying relocate section %u to %u\n", mod->name,
214 relsec, sechdrs[relsec].sh_info);
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 */
217 location16 =
218 (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr +
219 rel[i].r_offset);
220 location32 = (uint32_t *) location16;
221 /* This is the symbol it is referring to. Note that all
222 undefined symbols have been resolved. */
223 sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
224 + ELF32_R_SYM(rel[i].r_info);
Bernd Schmidtd1a85302009-01-07 23:14:39 +0800225 value = sym->st_value;
Bryan Wu1394f032007-05-06 14:50:22 -0700226 value += rel[i].r_addend;
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000227
Graf Yang8f658732008-11-18 17:48:22 +0800228#ifdef CONFIG_SMP
229 if ((unsigned long)location16 >= COREB_L1_DATA_A_START) {
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000230 pr_err("cannot relocate in L1: %u (SMP kernel)",
231 mod->name, ELF32_R_TYPE(rel[i].r_info));
Graf Yang8f658732008-11-18 17:48:22 +0800232 return -ENOEXEC;
233 }
234#endif
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000235
236 pr_debug("location is %lx, value is %x type is %d\n",
237 mod->name, (unsigned long)location32, value,
238 ELF32_R_TYPE(rel[i].r_info));
239
Bryan Wu1394f032007-05-06 14:50:22 -0700240 switch (ELF32_R_TYPE(rel[i].r_info)) {
241
Mike Frysinger595d6812009-06-02 00:35:33 +0000242 case R_BFIN_LUIMM16:
Bryan Wu1394f032007-05-06 14:50:22 -0700243 tmp = (value & 0xffff);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800244 if ((unsigned long)location16 >= L1_CODE_START) {
Bryan Wu1394f032007-05-06 14:50:22 -0700245 dma_memcpy(location16, &tmp, 2);
246 } else
247 *location16 = tmp;
248 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000249 case R_BFIN_HUIMM16:
Bryan Wu1394f032007-05-06 14:50:22 -0700250 tmp = ((value >> 16) & 0xffff);
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800251 if ((unsigned long)location16 >= L1_CODE_START) {
Bryan Wu1394f032007-05-06 14:50:22 -0700252 dma_memcpy(location16, &tmp, 2);
253 } else
254 *location16 = tmp;
255 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000256 case R_BFIN_RIMM16:
Bryan Wu1394f032007-05-06 14:50:22 -0700257 *location16 = (value & 0xffff);
258 break;
Mike Frysinger595d6812009-06-02 00:35:33 +0000259 case R_BFIN_BYTE4_DATA:
Bryan Wu1394f032007-05-06 14:50:22 -0700260 *location32 = value;
261 break;
Robin Getz22532572009-06-25 15:49:38 +0000262 case R_BFIN_PCREL24:
263 case R_BFIN_PCREL24_JUMP_L:
264 case R_BFIN_PCREL12_JUMP:
265 case R_BFIN_PCREL12_JUMP_S:
266 case R_BFIN_PCREL10:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000267 pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
Robin Getz22532572009-06-25 15:49:38 +0000268 mod->name, ELF32_R_TYPE(rel[i].r_info));
269 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700270 default:
Mike Frysingerdc6b1ac2009-06-26 00:35:24 +0000271 pr_err("unknown relocation: %u\n", mod->name,
272 ELF32_R_TYPE(rel[i].r_info));
Bryan Wu1394f032007-05-06 14:50:22 -0700273 return -ENOEXEC;
274 }
275 }
276 return 0;
277}
278
279int
280module_finalize(const Elf_Ehdr * hdr,
281 const Elf_Shdr * sechdrs, struct module *mod)
282{
283 unsigned int i, strindex = 0, symindex = 0;
284 char *secstrings;
Graf Yang8f658732008-11-18 17:48:22 +0800285 long err = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700286
287 secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
288
289 for (i = 1; i < hdr->e_shnum; i++) {
290 /* Internal symbols and strings. */
291 if (sechdrs[i].sh_type == SHT_SYMTAB) {
292 symindex = i;
293 strindex = sechdrs[i].sh_link;
294 }
295 }
296
297 for (i = 1; i < hdr->e_shnum; i++) {
298 const char *strtab = (char *)sechdrs[strindex].sh_addr;
299 unsigned int info = sechdrs[i].sh_info;
300
301 /* Not a valid relocation section? */
302 if (info >= hdr->e_shnum)
303 continue;
304
305 if ((sechdrs[i].sh_type == SHT_RELA) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800306 ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) ||
307 (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800308 ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
Sonic Zhang262c3822008-07-19 15:42:41 +0800309 (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) {
Graf Yang8f658732008-11-18 17:48:22 +0800310 err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
Bryan Wu1394f032007-05-06 14:50:22 -0700311 symindex, i, mod);
Graf Yang8f658732008-11-18 17:48:22 +0800312 if (err < 0)
313 return -ENOEXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700314 }
315 }
316 return 0;
317}
318
319void module_arch_cleanup(struct module *mod)
320{
Sonic Zhang262c3822008-07-19 15:42:41 +0800321 l1_inst_sram_free(mod->arch.text_l1);
322 l1_data_A_sram_free(mod->arch.data_a_l1);
323 l1_data_A_sram_free(mod->arch.bss_a_l1);
324 l1_data_B_sram_free(mod->arch.data_b_l1);
325 l1_data_B_sram_free(mod->arch.bss_b_l1);
326 l2_sram_free(mod->arch.text_l2);
327 l2_sram_free(mod->arch.data_l2);
328 l2_sram_free(mod->arch.bss_l2);
Bryan Wu1394f032007-05-06 14:50:22 -0700329}