module.c 6.1 KB

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