module.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #define MUNGE(func) (*location = ((*location & ~func(-1)) | func(value)))
  142. #ifndef __tilegx__
  143. case R_TILE_32:
  144. *(uint32_t *)location = value;
  145. break;
  146. case R_TILE_IMM16_X0_HA:
  147. value = (value + 0x8000) >> 16;
  148. /*FALLTHROUGH*/
  149. case R_TILE_IMM16_X0_LO:
  150. MUNGE(create_Imm16_X0);
  151. break;
  152. case R_TILE_IMM16_X1_HA:
  153. value = (value + 0x8000) >> 16;
  154. /*FALLTHROUGH*/
  155. case R_TILE_IMM16_X1_LO:
  156. MUNGE(create_Imm16_X1);
  157. break;
  158. case R_TILE_JOFFLONG_X1:
  159. value -= (unsigned long) location; /* pc-relative */
  160. value = (long) value >> 3; /* count by instrs */
  161. MUNGE(create_JOffLong_X1);
  162. break;
  163. #else
  164. case R_TILEGX_64:
  165. *location = value;
  166. break;
  167. case R_TILEGX_IMM16_X0_HW2_LAST:
  168. if (!validate_hw2_last(value, me))
  169. return -ENOEXEC;
  170. value >>= 16;
  171. /*FALLTHROUGH*/
  172. case R_TILEGX_IMM16_X0_HW1:
  173. value >>= 16;
  174. /*FALLTHROUGH*/
  175. case R_TILEGX_IMM16_X0_HW0:
  176. MUNGE(create_Imm16_X0);
  177. break;
  178. case R_TILEGX_IMM16_X1_HW2_LAST:
  179. if (!validate_hw2_last(value, me))
  180. return -ENOEXEC;
  181. value >>= 16;
  182. /*FALLTHROUGH*/
  183. case R_TILEGX_IMM16_X1_HW1:
  184. value >>= 16;
  185. /*FALLTHROUGH*/
  186. case R_TILEGX_IMM16_X1_HW0:
  187. MUNGE(create_Imm16_X1);
  188. break;
  189. case R_TILEGX_JUMPOFF_X1:
  190. value -= (unsigned long) location; /* pc-relative */
  191. value = (long) value >> 3; /* count by instrs */
  192. if (!validate_jumpoff(value)) {
  193. pr_warning("module %s: Out of range jump to"
  194. " %#llx at %#llx (%p)\n", me->name,
  195. sym->st_value + rel[i].r_addend,
  196. rel[i].r_offset, location);
  197. return -ENOEXEC;
  198. }
  199. MUNGE(create_JumpOff_X1);
  200. break;
  201. #endif
  202. #undef MUNGE
  203. default:
  204. pr_err("module %s: Unknown relocation: %d\n",
  205. me->name, (int) ELF_R_TYPE(rel[i].r_info));
  206. return -ENOEXEC;
  207. }
  208. }
  209. return 0;
  210. }