module.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  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. #include <linux/moduleloader.h>
  30. #include <linux/elf.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/fs.h>
  33. #include <linux/string.h>
  34. #include <linux/kernel.h>
  35. #include <asm/dma.h>
  36. #include <asm/cacheflush.h>
  37. /*
  38. * handle arithmetic relocations.
  39. * See binutils/bfd/elf32-bfin.c for more details
  40. */
  41. #define RELOC_STACK_SIZE 100
  42. static uint32_t reloc_stack[RELOC_STACK_SIZE];
  43. static unsigned int reloc_stack_tos;
  44. #define is_reloc_stack_empty() ((reloc_stack_tos > 0)?0:1)
  45. static void reloc_stack_push(uint32_t value)
  46. {
  47. reloc_stack[reloc_stack_tos++] = value;
  48. }
  49. static uint32_t reloc_stack_pop(void)
  50. {
  51. return reloc_stack[--reloc_stack_tos];
  52. }
  53. static uint32_t reloc_stack_operate(unsigned int oper, struct module *mod)
  54. {
  55. uint32_t value;
  56. switch (oper) {
  57. case R_add:
  58. value = reloc_stack[reloc_stack_tos - 2] +
  59. reloc_stack[reloc_stack_tos - 1];
  60. reloc_stack_tos -= 2;
  61. break;
  62. case R_sub:
  63. value = reloc_stack[reloc_stack_tos - 2] -
  64. reloc_stack[reloc_stack_tos - 1];
  65. reloc_stack_tos -= 2;
  66. break;
  67. case R_mult:
  68. value = reloc_stack[reloc_stack_tos - 2] *
  69. reloc_stack[reloc_stack_tos - 1];
  70. reloc_stack_tos -= 2;
  71. break;
  72. case R_div:
  73. value = reloc_stack[reloc_stack_tos - 2] /
  74. reloc_stack[reloc_stack_tos - 1];
  75. reloc_stack_tos -= 2;
  76. break;
  77. case R_mod:
  78. value = reloc_stack[reloc_stack_tos - 2] %
  79. reloc_stack[reloc_stack_tos - 1];
  80. reloc_stack_tos -= 2;
  81. break;
  82. case R_lshift:
  83. value = reloc_stack[reloc_stack_tos - 2] <<
  84. reloc_stack[reloc_stack_tos - 1];
  85. reloc_stack_tos -= 2;
  86. break;
  87. case R_rshift:
  88. value = reloc_stack[reloc_stack_tos - 2] >>
  89. reloc_stack[reloc_stack_tos - 1];
  90. reloc_stack_tos -= 2;
  91. break;
  92. case R_and:
  93. value = reloc_stack[reloc_stack_tos - 2] &
  94. reloc_stack[reloc_stack_tos - 1];
  95. reloc_stack_tos -= 2;
  96. break;
  97. case R_or:
  98. value = reloc_stack[reloc_stack_tos - 2] |
  99. reloc_stack[reloc_stack_tos - 1];
  100. reloc_stack_tos -= 2;
  101. break;
  102. case R_xor:
  103. value = reloc_stack[reloc_stack_tos - 2] ^
  104. reloc_stack[reloc_stack_tos - 1];
  105. reloc_stack_tos -= 2;
  106. break;
  107. case R_land:
  108. value = reloc_stack[reloc_stack_tos - 2] &&
  109. reloc_stack[reloc_stack_tos - 1];
  110. reloc_stack_tos -= 2;
  111. break;
  112. case R_lor:
  113. value = reloc_stack[reloc_stack_tos - 2] ||
  114. reloc_stack[reloc_stack_tos - 1];
  115. reloc_stack_tos -= 2;
  116. break;
  117. case R_neg:
  118. value = -reloc_stack[reloc_stack_tos - 1];
  119. reloc_stack_tos--;
  120. break;
  121. case R_comp:
  122. value = ~reloc_stack[reloc_stack_tos - 1];
  123. reloc_stack_tos -= 1;
  124. break;
  125. default:
  126. printk(KERN_WARNING "module %s: unhandled reloction\n",
  127. mod->name);
  128. return 0;
  129. }
  130. /* now push the new value back on stack */
  131. reloc_stack_push(value);
  132. return value;
  133. }
  134. void *module_alloc(unsigned long size)
  135. {
  136. if (size == 0)
  137. return NULL;
  138. return vmalloc(size);
  139. }
  140. /* Free memory returned from module_alloc */
  141. void module_free(struct module *mod, void *module_region)
  142. {
  143. vfree(module_region);
  144. }
  145. /* Transfer the section to the L1 memory */
  146. int
  147. module_frob_arch_sections(Elf_Ehdr * hdr, Elf_Shdr * sechdrs,
  148. char *secstrings, struct module *mod)
  149. {
  150. /*
  151. * XXX: sechdrs are vmalloced in kernel/module.c
  152. * and would be vfreed just after module is loaded,
  153. * so we hack to keep the only information we needed
  154. * in mod->arch to correctly free L1 I/D sram later.
  155. * NOTE: this breaks the semantic of mod->arch structure.
  156. */
  157. Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
  158. void *dest = NULL;
  159. for (s = sechdrs; s < sechdrs_end; ++s) {
  160. if ((strcmp(".l1.text", secstrings + s->sh_name) == 0) ||
  161. ((strcmp(".text", secstrings + s->sh_name) == 0) &&
  162. (hdr->e_flags & EF_BFIN_CODE_IN_L1) && (s->sh_size > 0))) {
  163. dest = l1_inst_sram_alloc(s->sh_size);
  164. mod->arch.text_l1 = dest;
  165. if (dest == NULL) {
  166. printk(KERN_ERR
  167. "module %s: L1 instruction memory allocation failed\n",
  168. mod->name);
  169. return -1;
  170. }
  171. dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
  172. s->sh_flags &= ~SHF_ALLOC;
  173. s->sh_addr = (unsigned long)dest;
  174. }
  175. if ((strcmp(".l1.data", secstrings + s->sh_name) == 0) ||
  176. ((strcmp(".data", secstrings + s->sh_name) == 0) &&
  177. (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
  178. dest = l1_data_sram_alloc(s->sh_size);
  179. mod->arch.data_a_l1 = dest;
  180. if (dest == NULL) {
  181. printk(KERN_ERR
  182. "module %s: L1 data memory allocation failed\n",
  183. mod->name);
  184. return -1;
  185. }
  186. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  187. s->sh_flags &= ~SHF_ALLOC;
  188. s->sh_addr = (unsigned long)dest;
  189. }
  190. if (strcmp(".l1.bss", secstrings + s->sh_name) == 0 ||
  191. ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
  192. (hdr->e_flags & EF_BFIN_DATA_IN_L1) && (s->sh_size > 0))) {
  193. dest = l1_data_sram_alloc(s->sh_size);
  194. mod->arch.bss_a_l1 = dest;
  195. if (dest == NULL) {
  196. printk(KERN_ERR
  197. "module %s: L1 data memory allocation failed\n",
  198. mod->name);
  199. return -1;
  200. }
  201. memset(dest, 0, s->sh_size);
  202. s->sh_flags &= ~SHF_ALLOC;
  203. s->sh_addr = (unsigned long)dest;
  204. }
  205. if (strcmp(".l1.data.B", secstrings + s->sh_name) == 0) {
  206. dest = l1_data_B_sram_alloc(s->sh_size);
  207. mod->arch.data_b_l1 = dest;
  208. if (dest == NULL) {
  209. printk(KERN_ERR
  210. "module %s: L1 data memory allocation failed\n",
  211. mod->name);
  212. return -1;
  213. }
  214. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  215. s->sh_flags &= ~SHF_ALLOC;
  216. s->sh_addr = (unsigned long)dest;
  217. }
  218. if (strcmp(".l1.bss.B", secstrings + s->sh_name) == 0) {
  219. dest = l1_data_B_sram_alloc(s->sh_size);
  220. mod->arch.bss_b_l1 = dest;
  221. if (dest == NULL) {
  222. printk(KERN_ERR
  223. "module %s: L1 data memory allocation failed\n",
  224. mod->name);
  225. return -1;
  226. }
  227. memset(dest, 0, s->sh_size);
  228. s->sh_flags &= ~SHF_ALLOC;
  229. s->sh_addr = (unsigned long)dest;
  230. }
  231. if ((strcmp(".l2.text", secstrings + s->sh_name) == 0) ||
  232. ((strcmp(".text", secstrings + s->sh_name) == 0) &&
  233. (hdr->e_flags & EF_BFIN_CODE_IN_L2) && (s->sh_size > 0))) {
  234. dest = l2_sram_alloc(s->sh_size);
  235. mod->arch.text_l2 = dest;
  236. if (dest == NULL) {
  237. printk(KERN_ERR
  238. "module %s: L2 SRAM allocation failed\n",
  239. mod->name);
  240. return -1;
  241. }
  242. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  243. s->sh_flags &= ~SHF_ALLOC;
  244. s->sh_addr = (unsigned long)dest;
  245. }
  246. if ((strcmp(".l2.data", secstrings + s->sh_name) == 0) ||
  247. ((strcmp(".data", secstrings + s->sh_name) == 0) &&
  248. (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
  249. dest = l2_sram_alloc(s->sh_size);
  250. mod->arch.data_l2 = dest;
  251. if (dest == NULL) {
  252. printk(KERN_ERR
  253. "module %s: L2 SRAM allocation failed\n",
  254. mod->name);
  255. return -1;
  256. }
  257. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  258. s->sh_flags &= ~SHF_ALLOC;
  259. s->sh_addr = (unsigned long)dest;
  260. }
  261. if (strcmp(".l2.bss", secstrings + s->sh_name) == 0 ||
  262. ((strcmp(".bss", secstrings + s->sh_name) == 0) &&
  263. (hdr->e_flags & EF_BFIN_DATA_IN_L2) && (s->sh_size > 0))) {
  264. dest = l2_sram_alloc(s->sh_size);
  265. mod->arch.bss_l2 = dest;
  266. if (dest == NULL) {
  267. printk(KERN_ERR
  268. "module %s: L2 SRAM allocation failed\n",
  269. mod->name);
  270. return -1;
  271. }
  272. memset(dest, 0, s->sh_size);
  273. s->sh_flags &= ~SHF_ALLOC;
  274. s->sh_addr = (unsigned long)dest;
  275. }
  276. }
  277. return 0;
  278. }
  279. int
  280. apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
  281. unsigned int symindex, unsigned int relsec, struct module *me)
  282. {
  283. printk(KERN_ERR "module %s: .rel unsupported\n", me->name);
  284. return -ENOEXEC;
  285. }
  286. /*************************************************************************/
  287. /* FUNCTION : apply_relocate_add */
  288. /* ABSTRACT : Blackfin specific relocation handling for the loadable */
  289. /* modules. Modules are expected to be .o files. */
  290. /* Arithmetic relocations are handled. */
  291. /* We do not expect LSETUP to be split and hence is not */
  292. /* handled. */
  293. /* R_byte and R_byte2 are also not handled as the gas */
  294. /* does not generate it. */
  295. /*************************************************************************/
  296. int
  297. apply_relocate_add(Elf_Shdr * sechdrs, const char *strtab,
  298. unsigned int symindex, unsigned int relsec,
  299. struct module *mod)
  300. {
  301. unsigned int i;
  302. unsigned short tmp;
  303. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  304. Elf32_Sym *sym;
  305. uint32_t *location32;
  306. uint16_t *location16;
  307. uint32_t value;
  308. pr_debug("Applying relocate section %u to %u\n", relsec,
  309. sechdrs[relsec].sh_info);
  310. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  311. /* This is where to make the change */
  312. location16 =
  313. (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].sh_addr +
  314. rel[i].r_offset);
  315. location32 = (uint32_t *) location16;
  316. /* This is the symbol it is referring to. Note that all
  317. undefined symbols have been resolved. */
  318. sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
  319. + ELF32_R_SYM(rel[i].r_info);
  320. if (is_reloc_stack_empty()) {
  321. value = sym->st_value;
  322. } else {
  323. value = reloc_stack_pop();
  324. }
  325. value += rel[i].r_addend;
  326. pr_debug("location is %x, value is %x type is %d \n",
  327. (unsigned int) location32, value,
  328. ELF32_R_TYPE(rel[i].r_info));
  329. switch (ELF32_R_TYPE(rel[i].r_info)) {
  330. case R_pcrel24:
  331. case R_pcrel24_jump_l:
  332. /* Add the value, subtract its postition */
  333. location16 =
  334. (uint16_t *) (sechdrs[sechdrs[relsec].sh_info].
  335. sh_addr + rel[i].r_offset - 2);
  336. location32 = (uint32_t *) location16;
  337. value -= (uint32_t) location32;
  338. value >>= 1;
  339. pr_debug("value is %x, before %x-%x after %x-%x\n", value,
  340. *location16, *(location16 + 1),
  341. (*location16 & 0xff00) | (value >> 16 & 0x00ff),
  342. value & 0xffff);
  343. *location16 =
  344. (*location16 & 0xff00) | (value >> 16 & 0x00ff);
  345. *(location16 + 1) = value & 0xffff;
  346. break;
  347. case R_pcrel12_jump:
  348. case R_pcrel12_jump_s:
  349. value -= (uint32_t) location32;
  350. value >>= 1;
  351. *location16 = (value & 0xfff);
  352. break;
  353. case R_pcrel10:
  354. value -= (uint32_t) location32;
  355. value >>= 1;
  356. *location16 = (value & 0x3ff);
  357. break;
  358. case R_luimm16:
  359. pr_debug("before %x after %x\n", *location16,
  360. (value & 0xffff));
  361. tmp = (value & 0xffff);
  362. if ((unsigned long)location16 >= L1_CODE_START) {
  363. dma_memcpy(location16, &tmp, 2);
  364. } else
  365. *location16 = tmp;
  366. break;
  367. case R_huimm16:
  368. pr_debug("before %x after %x\n", *location16,
  369. ((value >> 16) & 0xffff));
  370. tmp = ((value >> 16) & 0xffff);
  371. if ((unsigned long)location16 >= L1_CODE_START) {
  372. dma_memcpy(location16, &tmp, 2);
  373. } else
  374. *location16 = tmp;
  375. break;
  376. case R_rimm16:
  377. *location16 = (value & 0xffff);
  378. break;
  379. case R_byte4_data:
  380. pr_debug("before %x after %x\n", *location32, value);
  381. *location32 = value;
  382. break;
  383. case R_push:
  384. reloc_stack_push(value);
  385. break;
  386. case R_const:
  387. reloc_stack_push(rel[i].r_addend);
  388. break;
  389. case R_add:
  390. case R_sub:
  391. case R_mult:
  392. case R_div:
  393. case R_mod:
  394. case R_lshift:
  395. case R_rshift:
  396. case R_and:
  397. case R_or:
  398. case R_xor:
  399. case R_land:
  400. case R_lor:
  401. case R_neg:
  402. case R_comp:
  403. reloc_stack_operate(ELF32_R_TYPE(rel[i].r_info), mod);
  404. break;
  405. default:
  406. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  407. mod->name, ELF32_R_TYPE(rel[i].r_info));
  408. return -ENOEXEC;
  409. }
  410. }
  411. return 0;
  412. }
  413. int
  414. module_finalize(const Elf_Ehdr * hdr,
  415. const Elf_Shdr * sechdrs, struct module *mod)
  416. {
  417. unsigned int i, strindex = 0, symindex = 0;
  418. char *secstrings;
  419. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  420. for (i = 1; i < hdr->e_shnum; i++) {
  421. /* Internal symbols and strings. */
  422. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  423. symindex = i;
  424. strindex = sechdrs[i].sh_link;
  425. }
  426. }
  427. for (i = 1; i < hdr->e_shnum; i++) {
  428. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  429. unsigned int info = sechdrs[i].sh_info;
  430. /* Not a valid relocation section? */
  431. if (info >= hdr->e_shnum)
  432. continue;
  433. if ((sechdrs[i].sh_type == SHT_RELA) &&
  434. ((strcmp(".rela.l2.text", secstrings + sechdrs[i].sh_name) == 0) ||
  435. (strcmp(".rela.l1.text", secstrings + sechdrs[i].sh_name) == 0) ||
  436. ((strcmp(".rela.text", secstrings + sechdrs[i].sh_name) == 0) &&
  437. (hdr->e_flags & (EF_BFIN_CODE_IN_L1|EF_BFIN_CODE_IN_L2))))) {
  438. apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
  439. symindex, i, mod);
  440. }
  441. }
  442. return 0;
  443. }
  444. void module_arch_cleanup(struct module *mod)
  445. {
  446. l1_inst_sram_free(mod->arch.text_l1);
  447. l1_data_A_sram_free(mod->arch.data_a_l1);
  448. l1_data_A_sram_free(mod->arch.bss_a_l1);
  449. l1_data_B_sram_free(mod->arch.data_b_l1);
  450. l1_data_B_sram_free(mod->arch.bss_b_l1);
  451. l2_sram_free(mod->arch.text_l2);
  452. l2_sram_free(mod->arch.data_l2);
  453. l2_sram_free(mod->arch.bss_l2);
  454. }