module.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Based on i386 version, copyright (C) 2001 Rusty Russell.
  15. */
  16. #include <linux/moduleloader.h>
  17. #include <linux/elf.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/fs.h>
  20. #include <linux/string.h>
  21. #include <linux/kernel.h>
  22. #include <asm/opcode-tile.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/homecache.h>
  25. #ifdef __tilegx__
  26. # define Elf_Rela Elf64_Rela
  27. # define ELF_R_SYM ELF64_R_SYM
  28. # define ELF_R_TYPE ELF64_R_TYPE
  29. #else
  30. # define Elf_Rela Elf32_Rela
  31. # define ELF_R_SYM ELF32_R_SYM
  32. # define ELF_R_TYPE ELF32_R_TYPE
  33. #endif
  34. #ifdef MODULE_DEBUG
  35. #define DEBUGP printk
  36. #else
  37. #define DEBUGP(fmt...)
  38. #endif
  39. /*
  40. * Allocate some address space in the range MEM_MODULE_START to
  41. * MEM_MODULE_END and populate it with memory.
  42. */
  43. void *module_alloc(unsigned long size)
  44. {
  45. struct page **pages;
  46. pgprot_t prot_rwx = __pgprot(_PAGE_KERNEL | _PAGE_KERNEL_EXEC);
  47. struct vm_struct *area;
  48. int i = 0;
  49. int npages;
  50. if (size == 0)
  51. return NULL;
  52. npages = (size + PAGE_SIZE - 1) / PAGE_SIZE;
  53. pages = kmalloc(npages * sizeof(struct page *), GFP_KERNEL);
  54. if (pages == NULL)
  55. return NULL;
  56. for (; i < npages; ++i) {
  57. pages[i] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
  58. if (!pages[i])
  59. goto error;
  60. }
  61. area = __get_vm_area(size, VM_ALLOC, MEM_MODULE_START, MEM_MODULE_END);
  62. if (!area)
  63. goto error;
  64. if (map_vm_area(area, prot_rwx, &pages)) {
  65. vunmap(area->addr);
  66. goto error;
  67. }
  68. return area->addr;
  69. error:
  70. while (--i >= 0)
  71. __free_page(pages[i]);
  72. kfree(pages);
  73. return NULL;
  74. }
  75. /* Free memory returned from module_alloc */
  76. void module_free(struct module *mod, void *module_region)
  77. {
  78. vfree(module_region);
  79. /* Globally flush the L1 icache. */
  80. flush_remote(0, HV_FLUSH_EVICT_L1I, cpu_online_mask,
  81. 0, 0, 0, NULL, NULL, 0);
  82. /*
  83. * FIXME: If module_region == mod->module_init, trim exception
  84. * table entries.
  85. */
  86. }
  87. /* We don't need anything special. */
  88. int module_frob_arch_sections(Elf_Ehdr *hdr,
  89. Elf_Shdr *sechdrs,
  90. char *secstrings,
  91. struct module *mod)
  92. {
  93. return 0;
  94. }
  95. int apply_relocate(Elf_Shdr *sechdrs,
  96. const char *strtab,
  97. unsigned int symindex,
  98. unsigned int relsec,
  99. struct module *me)
  100. {
  101. pr_err("module %s: .rel relocation unsupported\n", me->name);
  102. return -ENOEXEC;
  103. }
  104. #ifdef __tilegx__
  105. /*
  106. * Validate that the high 16 bits of "value" is just the sign-extension of
  107. * the low 48 bits.
  108. */
  109. static int validate_hw2_last(long value, struct module *me)
  110. {
  111. if (((value << 16) >> 16) != value) {
  112. pr_warning("module %s: Out of range HW2_LAST value %#lx\n",
  113. me->name, value);
  114. return 0;
  115. }
  116. return 1;
  117. }
  118. /*
  119. * Validate that "value" isn't too big to hold in a JumpOff relocation.
  120. */
  121. static int validate_jumpoff(long value)
  122. {
  123. /* Determine size of jump offset. */
  124. int shift = __builtin_clzl(get_JumpOff_X1(create_JumpOff_X1(-1)));
  125. /* Check to see if it fits into the relocation slot. */
  126. long f = get_JumpOff_X1(create_JumpOff_X1(value));
  127. f = (f << shift) >> shift;
  128. return f == value;
  129. }
  130. #endif
  131. int apply_relocate_add(Elf_Shdr *sechdrs,
  132. const char *strtab,
  133. unsigned int symindex,
  134. unsigned int relsec,
  135. struct module *me)
  136. {
  137. unsigned int i;
  138. Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  139. Elf_Sym *sym;
  140. u64 *location;
  141. unsigned long value;
  142. DEBUGP("Applying relocate section %u to %u\n", relsec,
  143. sechdrs[relsec].sh_info);
  144. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  145. /* This is where to make the change */
  146. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  147. + rel[i].r_offset;
  148. /*
  149. * This is the symbol it is referring to.
  150. * Note that all undefined symbols have been resolved.
  151. */
  152. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  153. + ELF_R_SYM(rel[i].r_info);
  154. value = sym->st_value + rel[i].r_addend;
  155. switch (ELF_R_TYPE(rel[i].r_info)) {
  156. #define MUNGE(func) (*location = ((*location & ~func(-1)) | func(value)))
  157. #ifndef __tilegx__
  158. case R_TILE_32:
  159. *(uint32_t *)location = value;
  160. break;
  161. case R_TILE_IMM16_X0_HA:
  162. value = (value + 0x8000) >> 16;
  163. /*FALLTHROUGH*/
  164. case R_TILE_IMM16_X0_LO:
  165. MUNGE(create_Imm16_X0);
  166. break;
  167. case R_TILE_IMM16_X1_HA:
  168. value = (value + 0x8000) >> 16;
  169. /*FALLTHROUGH*/
  170. case R_TILE_IMM16_X1_LO:
  171. MUNGE(create_Imm16_X1);
  172. break;
  173. case R_TILE_JOFFLONG_X1:
  174. value -= (unsigned long) location; /* pc-relative */
  175. value = (long) value >> 3; /* count by instrs */
  176. MUNGE(create_JOffLong_X1);
  177. break;
  178. #else
  179. case R_TILEGX_64:
  180. *location = value;
  181. break;
  182. case R_TILEGX_IMM16_X0_HW2_LAST:
  183. if (!validate_hw2_last(value, me))
  184. return -ENOEXEC;
  185. value >>= 16;
  186. /*FALLTHROUGH*/
  187. case R_TILEGX_IMM16_X0_HW1:
  188. value >>= 16;
  189. /*FALLTHROUGH*/
  190. case R_TILEGX_IMM16_X0_HW0:
  191. MUNGE(create_Imm16_X0);
  192. break;
  193. case R_TILEGX_IMM16_X1_HW2_LAST:
  194. if (!validate_hw2_last(value, me))
  195. return -ENOEXEC;
  196. value >>= 16;
  197. /*FALLTHROUGH*/
  198. case R_TILEGX_IMM16_X1_HW1:
  199. value >>= 16;
  200. /*FALLTHROUGH*/
  201. case R_TILEGX_IMM16_X1_HW0:
  202. MUNGE(create_Imm16_X1);
  203. break;
  204. case R_TILEGX_JUMPOFF_X1:
  205. value -= (unsigned long) location; /* pc-relative */
  206. value = (long) value >> 3; /* count by instrs */
  207. if (!validate_jumpoff(value)) {
  208. pr_warning("module %s: Out of range jump to"
  209. " %#llx at %#llx (%p)\n", me->name,
  210. sym->st_value + rel[i].r_addend,
  211. rel[i].r_offset, location);
  212. return -ENOEXEC;
  213. }
  214. MUNGE(create_JumpOff_X1);
  215. break;
  216. #endif
  217. #undef MUNGE
  218. default:
  219. pr_err("module %s: Unknown relocation: %d\n",
  220. me->name, (int) ELF_R_TYPE(rel[i].r_info));
  221. return -ENOEXEC;
  222. }
  223. }
  224. return 0;
  225. }
  226. int module_finalize(const Elf_Ehdr *hdr,
  227. const Elf_Shdr *sechdrs,
  228. struct module *me)
  229. {
  230. /* FIXME: perhaps remove the "writable" bit from the TLB? */
  231. return 0;
  232. }
  233. void module_arch_cleanup(struct module *mod)
  234. {
  235. }