module.c 5.6 KB

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