module.c 5.7 KB

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