module.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifdef __tilegx__
  88. /*
  89. * Validate that the high 16 bits of "value" is just the sign-extension of
  90. * the low 48 bits.
  91. */
  92. static int validate_hw2_last(long value, struct module *me)
  93. {
  94. if (((value << 16) >> 16) != value) {
  95. pr_warning("module %s: Out of range HW2_LAST value %#lx\n",
  96. me->name, value);
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. /*
  102. * Validate that "value" isn't too big to hold in a JumpOff relocation.
  103. */
  104. static int validate_jumpoff(long value)
  105. {
  106. /* Determine size of jump offset. */
  107. int shift = __builtin_clzl(get_JumpOff_X1(create_JumpOff_X1(-1)));
  108. /* Check to see if it fits into the relocation slot. */
  109. long f = get_JumpOff_X1(create_JumpOff_X1(value));
  110. f = (f << shift) >> shift;
  111. return f == value;
  112. }
  113. #endif
  114. int apply_relocate_add(Elf_Shdr *sechdrs,
  115. const char *strtab,
  116. unsigned int symindex,
  117. unsigned int relsec,
  118. struct module *me)
  119. {
  120. unsigned int i;
  121. Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  122. Elf_Sym *sym;
  123. u64 *location;
  124. unsigned long value;
  125. DEBUGP("Applying relocate section %u to %u\n", relsec,
  126. sechdrs[relsec].sh_info);
  127. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  128. /* This is where to make the change */
  129. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  130. + rel[i].r_offset;
  131. /*
  132. * This is the symbol it is referring to.
  133. * Note that all undefined symbols have been resolved.
  134. */
  135. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  136. + ELF_R_SYM(rel[i].r_info);
  137. value = sym->st_value + rel[i].r_addend;
  138. switch (ELF_R_TYPE(rel[i].r_info)) {
  139. #define MUNGE(func) (*location = ((*location & ~func(-1)) | func(value)))
  140. #ifndef __tilegx__
  141. case R_TILE_32:
  142. *(uint32_t *)location = value;
  143. break;
  144. case R_TILE_IMM16_X0_HA:
  145. value = (value + 0x8000) >> 16;
  146. /*FALLTHROUGH*/
  147. case R_TILE_IMM16_X0_LO:
  148. MUNGE(create_Imm16_X0);
  149. break;
  150. case R_TILE_IMM16_X1_HA:
  151. value = (value + 0x8000) >> 16;
  152. /*FALLTHROUGH*/
  153. case R_TILE_IMM16_X1_LO:
  154. MUNGE(create_Imm16_X1);
  155. break;
  156. case R_TILE_JOFFLONG_X1:
  157. value -= (unsigned long) location; /* pc-relative */
  158. value = (long) value >> 3; /* count by instrs */
  159. MUNGE(create_JOffLong_X1);
  160. break;
  161. #else
  162. case R_TILEGX_64:
  163. *location = value;
  164. break;
  165. case R_TILEGX_IMM16_X0_HW2_LAST:
  166. if (!validate_hw2_last(value, me))
  167. return -ENOEXEC;
  168. value >>= 16;
  169. /*FALLTHROUGH*/
  170. case R_TILEGX_IMM16_X0_HW1:
  171. value >>= 16;
  172. /*FALLTHROUGH*/
  173. case R_TILEGX_IMM16_X0_HW0:
  174. MUNGE(create_Imm16_X0);
  175. break;
  176. case R_TILEGX_IMM16_X1_HW2_LAST:
  177. if (!validate_hw2_last(value, me))
  178. return -ENOEXEC;
  179. value >>= 16;
  180. /*FALLTHROUGH*/
  181. case R_TILEGX_IMM16_X1_HW1:
  182. value >>= 16;
  183. /*FALLTHROUGH*/
  184. case R_TILEGX_IMM16_X1_HW0:
  185. MUNGE(create_Imm16_X1);
  186. break;
  187. case R_TILEGX_JUMPOFF_X1:
  188. value -= (unsigned long) location; /* pc-relative */
  189. value = (long) value >> 3; /* count by instrs */
  190. if (!validate_jumpoff(value)) {
  191. pr_warning("module %s: Out of range jump to"
  192. " %#llx at %#llx (%p)\n", me->name,
  193. sym->st_value + rel[i].r_addend,
  194. rel[i].r_offset, location);
  195. return -ENOEXEC;
  196. }
  197. MUNGE(create_JumpOff_X1);
  198. break;
  199. #endif
  200. #undef MUNGE
  201. default:
  202. pr_err("module %s: Unknown relocation: %d\n",
  203. me->name, (int) ELF_R_TYPE(rel[i].r_info));
  204. return -ENOEXEC;
  205. }
  206. }
  207. return 0;
  208. }