module.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright 2004-2009 Analog Devices Inc.
  3. *
  4. * Licensed under the GPL-2 or later
  5. */
  6. #define pr_fmt(fmt) "module %s: " fmt, mod->name
  7. #include <linux/moduleloader.h>
  8. #include <linux/elf.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/fs.h>
  11. #include <linux/string.h>
  12. #include <linux/kernel.h>
  13. #include <asm/dma.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/uaccess.h>
  16. void *module_alloc(unsigned long size)
  17. {
  18. if (size == 0)
  19. return NULL;
  20. return vmalloc(size);
  21. }
  22. /* Free memory returned from module_alloc */
  23. void module_free(struct module *mod, void *module_region)
  24. {
  25. vfree(module_region);
  26. }
  27. /* Transfer the section to the L1 memory */
  28. int
  29. module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  30. char *secstrings, struct module *mod)
  31. {
  32. /*
  33. * XXX: sechdrs are vmalloced in kernel/module.c
  34. * and would be vfreed just after module is loaded,
  35. * so we hack to keep the only information we needed
  36. * in mod->arch to correctly free L1 I/D sram later.
  37. * NOTE: this breaks the semantic of mod->arch structure.
  38. */
  39. Elf_Shdr *s, *sechdrs_end = sechdrs + hdr->e_shnum;
  40. void *dest;
  41. for (s = sechdrs; s < sechdrs_end; ++s) {
  42. const char *shname = secstrings + s->sh_name;
  43. if (s->sh_size == 0)
  44. continue;
  45. if (!strcmp(".l1.text", shname) ||
  46. (!strcmp(".text", shname) &&
  47. (hdr->e_flags & EF_BFIN_CODE_IN_L1))) {
  48. dest = l1_inst_sram_alloc(s->sh_size);
  49. mod->arch.text_l1 = dest;
  50. if (dest == NULL) {
  51. pr_err("L1 inst memory allocation failed\n");
  52. return -1;
  53. }
  54. dma_memcpy(dest, (void *)s->sh_addr, s->sh_size);
  55. } else if (!strcmp(".l1.data", shname) ||
  56. (!strcmp(".data", shname) &&
  57. (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
  58. dest = l1_data_sram_alloc(s->sh_size);
  59. mod->arch.data_a_l1 = dest;
  60. if (dest == NULL) {
  61. pr_err("L1 data memory allocation failed\n");
  62. return -1;
  63. }
  64. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  65. } else if (!strcmp(".l1.bss", shname) ||
  66. (!strcmp(".bss", shname) &&
  67. (hdr->e_flags & EF_BFIN_DATA_IN_L1))) {
  68. dest = l1_data_sram_zalloc(s->sh_size);
  69. mod->arch.bss_a_l1 = dest;
  70. if (dest == NULL) {
  71. pr_err("L1 data memory allocation failed\n");
  72. return -1;
  73. }
  74. } else if (!strcmp(".l1.data.B", shname)) {
  75. dest = l1_data_B_sram_alloc(s->sh_size);
  76. mod->arch.data_b_l1 = dest;
  77. if (dest == NULL) {
  78. pr_err("L1 data memory allocation failed\n");
  79. return -1;
  80. }
  81. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  82. } else if (!strcmp(".l1.bss.B", shname)) {
  83. dest = l1_data_B_sram_alloc(s->sh_size);
  84. mod->arch.bss_b_l1 = dest;
  85. if (dest == NULL) {
  86. pr_err("L1 data memory allocation failed\n");
  87. return -1;
  88. }
  89. memset(dest, 0, s->sh_size);
  90. } else if (!strcmp(".l2.text", shname) ||
  91. (!strcmp(".text", shname) &&
  92. (hdr->e_flags & EF_BFIN_CODE_IN_L2))) {
  93. dest = l2_sram_alloc(s->sh_size);
  94. mod->arch.text_l2 = dest;
  95. if (dest == NULL) {
  96. pr_err("L2 SRAM allocation failed\n");
  97. return -1;
  98. }
  99. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  100. } else if (!strcmp(".l2.data", shname) ||
  101. (!strcmp(".data", shname) &&
  102. (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
  103. dest = l2_sram_alloc(s->sh_size);
  104. mod->arch.data_l2 = dest;
  105. if (dest == NULL) {
  106. pr_err("L2 SRAM allocation failed\n");
  107. return -1;
  108. }
  109. memcpy(dest, (void *)s->sh_addr, s->sh_size);
  110. } else if (!strcmp(".l2.bss", shname) ||
  111. (!strcmp(".bss", shname) &&
  112. (hdr->e_flags & EF_BFIN_DATA_IN_L2))) {
  113. dest = l2_sram_zalloc(s->sh_size);
  114. mod->arch.bss_l2 = dest;
  115. if (dest == NULL) {
  116. pr_err("L2 SRAM allocation failed\n");
  117. return -1;
  118. }
  119. } else
  120. continue;
  121. s->sh_flags &= ~SHF_ALLOC;
  122. s->sh_addr = (unsigned long)dest;
  123. }
  124. return 0;
  125. }
  126. int
  127. apply_relocate(Elf_Shdr * sechdrs, const char *strtab,
  128. unsigned int symindex, unsigned int relsec, struct module *mod)
  129. {
  130. pr_err(".rel unsupported\n");
  131. return -ENOEXEC;
  132. }
  133. /*************************************************************************/
  134. /* FUNCTION : apply_relocate_add */
  135. /* ABSTRACT : Blackfin specific relocation handling for the loadable */
  136. /* modules. Modules are expected to be .o files. */
  137. /* Arithmetic relocations are handled. */
  138. /* We do not expect LSETUP to be split and hence is not */
  139. /* handled. */
  140. /* R_BFIN_BYTE and R_BFIN_BYTE2 are also not handled as the */
  141. /* gas does not generate it. */
  142. /*************************************************************************/
  143. int
  144. apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  145. unsigned int symindex, unsigned int relsec,
  146. struct module *mod)
  147. {
  148. unsigned int i;
  149. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  150. Elf32_Sym *sym;
  151. unsigned long location, value, size;
  152. pr_debug("applying relocate section %u to %u\n",
  153. relsec, sechdrs[relsec].sh_info);
  154. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  155. /* This is where to make the change */
  156. location = sechdrs[sechdrs[relsec].sh_info].sh_addr +
  157. rel[i].r_offset;
  158. /* This is the symbol it is referring to. Note that all
  159. undefined symbols have been resolved. */
  160. sym = (Elf32_Sym *) sechdrs[symindex].sh_addr
  161. + ELF32_R_SYM(rel[i].r_info);
  162. value = sym->st_value;
  163. value += rel[i].r_addend;
  164. #ifdef CONFIG_SMP
  165. if (location >= COREB_L1_DATA_A_START) {
  166. pr_err("cannot relocate in L1: %u (SMP kernel)\n",
  167. ELF32_R_TYPE(rel[i].r_info));
  168. return -ENOEXEC;
  169. }
  170. #endif
  171. pr_debug("location is %lx, value is %lx type is %d\n",
  172. location, value, ELF32_R_TYPE(rel[i].r_info));
  173. switch (ELF32_R_TYPE(rel[i].r_info)) {
  174. case R_BFIN_HUIMM16:
  175. value >>= 16;
  176. case R_BFIN_LUIMM16:
  177. case R_BFIN_RIMM16:
  178. size = 2;
  179. break;
  180. case R_BFIN_BYTE4_DATA:
  181. size = 4;
  182. break;
  183. case R_BFIN_PCREL24:
  184. case R_BFIN_PCREL24_JUMP_L:
  185. case R_BFIN_PCREL12_JUMP:
  186. case R_BFIN_PCREL12_JUMP_S:
  187. case R_BFIN_PCREL10:
  188. pr_err("unsupported relocation: %u (no -mlong-calls?)\n",
  189. ELF32_R_TYPE(rel[i].r_info));
  190. return -ENOEXEC;
  191. default:
  192. pr_err("unknown relocation: %u\n",
  193. ELF32_R_TYPE(rel[i].r_info));
  194. return -ENOEXEC;
  195. }
  196. switch (bfin_mem_access_type(location, size)) {
  197. case BFIN_MEM_ACCESS_CORE:
  198. case BFIN_MEM_ACCESS_CORE_ONLY:
  199. memcpy((void *)location, &value, size);
  200. break;
  201. case BFIN_MEM_ACCESS_DMA:
  202. dma_memcpy((void *)location, &value, size);
  203. break;
  204. case BFIN_MEM_ACCESS_ITEST:
  205. isram_memcpy((void *)location, &value, size);
  206. break;
  207. default:
  208. pr_err("invalid relocation for %#lx\n", location);
  209. return -ENOEXEC;
  210. }
  211. }
  212. return 0;
  213. }
  214. int
  215. module_finalize(const Elf_Ehdr * hdr,
  216. const Elf_Shdr * sechdrs, struct module *mod)
  217. {
  218. unsigned int i, strindex = 0, symindex = 0;
  219. char *secstrings;
  220. long err = 0;
  221. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  222. for (i = 1; i < hdr->e_shnum; i++) {
  223. /* Internal symbols and strings. */
  224. if (sechdrs[i].sh_type == SHT_SYMTAB) {
  225. symindex = i;
  226. strindex = sechdrs[i].sh_link;
  227. }
  228. }
  229. for (i = 1; i < hdr->e_shnum; i++) {
  230. const char *strtab = (char *)sechdrs[strindex].sh_addr;
  231. unsigned int info = sechdrs[i].sh_info;
  232. const char *shname = secstrings + sechdrs[i].sh_name;
  233. /* Not a valid relocation section? */
  234. if (info >= hdr->e_shnum)
  235. continue;
  236. /* Only support RELA relocation types */
  237. if (sechdrs[i].sh_type != SHT_RELA)
  238. continue;
  239. if (!strcmp(".rela.l2.text", shname) ||
  240. !strcmp(".rela.l1.text", shname) ||
  241. (!strcmp(".rela.text", shname) &&
  242. (hdr->e_flags & (EF_BFIN_CODE_IN_L1 | EF_BFIN_CODE_IN_L2)))) {
  243. err = apply_relocate_add((Elf_Shdr *) sechdrs, strtab,
  244. symindex, i, mod);
  245. if (err < 0)
  246. return -ENOEXEC;
  247. }
  248. }
  249. return 0;
  250. }
  251. void module_arch_cleanup(struct module *mod)
  252. {
  253. l1_inst_sram_free(mod->arch.text_l1);
  254. l1_data_A_sram_free(mod->arch.data_a_l1);
  255. l1_data_A_sram_free(mod->arch.bss_a_l1);
  256. l1_data_B_sram_free(mod->arch.data_b_l1);
  257. l1_data_B_sram_free(mod->arch.bss_b_l1);
  258. l2_sram_free(mod->arch.text_l2);
  259. l2_sram_free(mod->arch.data_l2);
  260. l2_sram_free(mod->arch.bss_l2);
  261. }