module.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/pgtable.h>
  23. #include <asm/homecache.h>
  24. #include <arch/opcode.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. area->nr_pages = npages;
  65. area->pages = pages;
  66. if (map_vm_area(area, prot_rwx, &pages)) {
  67. vunmap(area->addr);
  68. goto error;
  69. }
  70. return area->addr;
  71. error:
  72. while (--i >= 0)
  73. __free_page(pages[i]);
  74. kfree(pages);
  75. return NULL;
  76. }
  77. /* Free memory returned from module_alloc */
  78. void module_free(struct module *mod, void *module_region)
  79. {
  80. vfree(module_region);
  81. /* Globally flush the L1 icache. */
  82. flush_remote(0, HV_FLUSH_EVICT_L1I, cpu_online_mask,
  83. 0, 0, 0, NULL, NULL, 0);
  84. /*
  85. * FIXME: If module_region == mod->module_init, trim exception
  86. * table entries.
  87. */
  88. }
  89. #ifdef __tilegx__
  90. /*
  91. * Validate that the high 16 bits of "value" is just the sign-extension of
  92. * the low 48 bits.
  93. */
  94. static int validate_hw2_last(long value, struct module *me)
  95. {
  96. if (((value << 16) >> 16) != value) {
  97. pr_warning("module %s: Out of range HW2_LAST value %#lx\n",
  98. me->name, value);
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. /*
  104. * Validate that "value" isn't too big to hold in a JumpOff relocation.
  105. */
  106. static int validate_jumpoff(long value)
  107. {
  108. /* Determine size of jump offset. */
  109. int shift = __builtin_clzl(get_JumpOff_X1(create_JumpOff_X1(-1)));
  110. /* Check to see if it fits into the relocation slot. */
  111. long f = get_JumpOff_X1(create_JumpOff_X1(value));
  112. f = (f << shift) >> shift;
  113. return f == value;
  114. }
  115. #endif
  116. int apply_relocate_add(Elf_Shdr *sechdrs,
  117. const char *strtab,
  118. unsigned int symindex,
  119. unsigned int relsec,
  120. struct module *me)
  121. {
  122. unsigned int i;
  123. Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  124. Elf_Sym *sym;
  125. u64 *location;
  126. unsigned long value;
  127. DEBUGP("Applying relocate section %u to %u\n", relsec,
  128. sechdrs[relsec].sh_info);
  129. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  130. /* This is where to make the change */
  131. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  132. + rel[i].r_offset;
  133. /*
  134. * This is the symbol it is referring to.
  135. * Note that all undefined symbols have been resolved.
  136. */
  137. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  138. + ELF_R_SYM(rel[i].r_info);
  139. value = sym->st_value + rel[i].r_addend;
  140. switch (ELF_R_TYPE(rel[i].r_info)) {
  141. #ifdef __LITTLE_ENDIAN
  142. # define MUNGE(func) \
  143. (*location = ((*location & ~func(-1)) | func(value)))
  144. #else
  145. /*
  146. * Instructions are always little-endian, so when we read them as data,
  147. * we have to swap them around before and after modifying them.
  148. */
  149. # define MUNGE(func) \
  150. (*location = swab64((swab64(*location) & ~func(-1)) | func(value)))
  151. #endif
  152. #ifndef __tilegx__
  153. case R_TILE_32:
  154. *(uint32_t *)location = value;
  155. break;
  156. case R_TILE_IMM16_X0_HA:
  157. value = (value + 0x8000) >> 16;
  158. /*FALLTHROUGH*/
  159. case R_TILE_IMM16_X0_LO:
  160. MUNGE(create_Imm16_X0);
  161. break;
  162. case R_TILE_IMM16_X1_HA:
  163. value = (value + 0x8000) >> 16;
  164. /*FALLTHROUGH*/
  165. case R_TILE_IMM16_X1_LO:
  166. MUNGE(create_Imm16_X1);
  167. break;
  168. case R_TILE_JOFFLONG_X1:
  169. value -= (unsigned long) location; /* pc-relative */
  170. value = (long) value >> 3; /* count by instrs */
  171. MUNGE(create_JOffLong_X1);
  172. break;
  173. #else
  174. case R_TILEGX_64:
  175. *location = value;
  176. break;
  177. case R_TILEGX_IMM16_X0_HW2_LAST:
  178. if (!validate_hw2_last(value, me))
  179. return -ENOEXEC;
  180. value >>= 16;
  181. /*FALLTHROUGH*/
  182. case R_TILEGX_IMM16_X0_HW1:
  183. value >>= 16;
  184. /*FALLTHROUGH*/
  185. case R_TILEGX_IMM16_X0_HW0:
  186. MUNGE(create_Imm16_X0);
  187. break;
  188. case R_TILEGX_IMM16_X1_HW2_LAST:
  189. if (!validate_hw2_last(value, me))
  190. return -ENOEXEC;
  191. value >>= 16;
  192. /*FALLTHROUGH*/
  193. case R_TILEGX_IMM16_X1_HW1:
  194. value >>= 16;
  195. /*FALLTHROUGH*/
  196. case R_TILEGX_IMM16_X1_HW0:
  197. MUNGE(create_Imm16_X1);
  198. break;
  199. case R_TILEGX_JUMPOFF_X1:
  200. value -= (unsigned long) location; /* pc-relative */
  201. value = (long) value >> 3; /* count by instrs */
  202. if (!validate_jumpoff(value)) {
  203. pr_warning("module %s: Out of range jump to"
  204. " %#llx at %#llx (%p)\n", me->name,
  205. sym->st_value + rel[i].r_addend,
  206. rel[i].r_offset, location);
  207. return -ENOEXEC;
  208. }
  209. MUNGE(create_JumpOff_X1);
  210. break;
  211. #endif
  212. #undef MUNGE
  213. default:
  214. pr_err("module %s: Unknown relocation: %d\n",
  215. me->name, (int) ELF_R_TYPE(rel[i].r_info));
  216. return -ENOEXEC;
  217. }
  218. }
  219. return 0;
  220. }