module.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Kernel module help for sparc64.
  2. *
  3. * Copyright (C) 2001 Rusty Russell.
  4. * Copyright (C) 2002 David S. Miller.
  5. */
  6. #include <linux/moduleloader.h>
  7. #include <linux/kernel.h>
  8. #include <linux/elf.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/fs.h>
  11. #include <linux/gfp.h>
  12. #include <linux/string.h>
  13. #include <linux/ctype.h>
  14. #include <linux/mm.h>
  15. #include <asm/processor.h>
  16. #include <asm/spitfire.h>
  17. #include "entry.h"
  18. #ifdef CONFIG_SPARC64
  19. #include <linux/jump_label.h>
  20. static void *module_map(unsigned long size)
  21. {
  22. if (PAGE_ALIGN(size) > MODULES_LEN)
  23. return NULL;
  24. return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  25. GFP_KERNEL, PAGE_KERNEL, -1,
  26. __builtin_return_address(0));
  27. }
  28. static char *dot2underscore(char *name)
  29. {
  30. return name;
  31. }
  32. #else
  33. static void *module_map(unsigned long size)
  34. {
  35. return vmalloc(size);
  36. }
  37. /* Replace references to .func with _Func */
  38. static char *dot2underscore(char *name)
  39. {
  40. if (name[0] == '.') {
  41. name[0] = '_';
  42. name[1] = toupper(name[1]);
  43. }
  44. return name;
  45. }
  46. #endif /* CONFIG_SPARC64 */
  47. void *module_alloc(unsigned long size)
  48. {
  49. void *ret;
  50. /* We handle the zero case fine, unlike vmalloc */
  51. if (size == 0)
  52. return NULL;
  53. ret = module_map(size);
  54. if (!ret)
  55. ret = ERR_PTR(-ENOMEM);
  56. else
  57. memset(ret, 0, size);
  58. return ret;
  59. }
  60. /* Free memory returned from module_core_alloc/module_init_alloc */
  61. void module_free(struct module *mod, void *module_region)
  62. {
  63. vfree(module_region);
  64. }
  65. /* Make generic code ignore STT_REGISTER dummy undefined symbols. */
  66. int module_frob_arch_sections(Elf_Ehdr *hdr,
  67. Elf_Shdr *sechdrs,
  68. char *secstrings,
  69. struct module *mod)
  70. {
  71. unsigned int symidx;
  72. Elf_Sym *sym;
  73. char *strtab;
  74. int i;
  75. for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
  76. if (symidx == hdr->e_shnum-1) {
  77. printk("%s: no symtab found.\n", mod->name);
  78. return -ENOEXEC;
  79. }
  80. }
  81. sym = (Elf_Sym *)sechdrs[symidx].sh_addr;
  82. strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
  83. for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
  84. if (sym[i].st_shndx == SHN_UNDEF) {
  85. if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER) {
  86. sym[i].st_shndx = SHN_ABS;
  87. } else {
  88. char *name = strtab + sym[i].st_name;
  89. dot2underscore(name);
  90. }
  91. }
  92. }
  93. return 0;
  94. }
  95. int apply_relocate(Elf_Shdr *sechdrs,
  96. const char *strtab,
  97. unsigned int symindex,
  98. unsigned int relsec,
  99. struct module *me)
  100. {
  101. printk(KERN_ERR "module %s: non-ADD RELOCATION unsupported\n",
  102. me->name);
  103. return -ENOEXEC;
  104. }
  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. u8 *location;
  115. u32 *loc32;
  116. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  117. Elf_Addr v;
  118. /* This is where to make the change */
  119. location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  120. + rel[i].r_offset;
  121. loc32 = (u32 *) location;
  122. #ifdef CONFIG_SPARC64
  123. BUG_ON(((u64)location >> (u64)32) != (u64)0);
  124. #endif /* CONFIG_SPARC64 */
  125. /* This is the symbol it is referring to. Note that all
  126. undefined symbols have been resolved. */
  127. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  128. + ELF_R_SYM(rel[i].r_info);
  129. v = sym->st_value + rel[i].r_addend;
  130. switch (ELF_R_TYPE(rel[i].r_info) & 0xff) {
  131. #ifdef CONFIG_SPARC64
  132. case R_SPARC_64:
  133. location[0] = v >> 56;
  134. location[1] = v >> 48;
  135. location[2] = v >> 40;
  136. location[3] = v >> 32;
  137. location[4] = v >> 24;
  138. location[5] = v >> 16;
  139. location[6] = v >> 8;
  140. location[7] = v >> 0;
  141. break;
  142. case R_SPARC_DISP32:
  143. v -= (Elf_Addr) location;
  144. *loc32 = v;
  145. break;
  146. case R_SPARC_WDISP19:
  147. v -= (Elf_Addr) location;
  148. *loc32 = (*loc32 & ~0x7ffff) |
  149. ((v >> 2) & 0x7ffff);
  150. break;
  151. case R_SPARC_OLO10:
  152. *loc32 = (*loc32 & ~0x1fff) |
  153. (((v & 0x3ff) +
  154. (ELF_R_TYPE(rel[i].r_info) >> 8))
  155. & 0x1fff);
  156. break;
  157. #endif /* CONFIG_SPARC64 */
  158. case R_SPARC_32:
  159. case R_SPARC_UA32:
  160. location[0] = v >> 24;
  161. location[1] = v >> 16;
  162. location[2] = v >> 8;
  163. location[3] = v >> 0;
  164. break;
  165. case R_SPARC_WDISP30:
  166. v -= (Elf_Addr) location;
  167. *loc32 = (*loc32 & ~0x3fffffff) |
  168. ((v >> 2) & 0x3fffffff);
  169. break;
  170. case R_SPARC_WDISP22:
  171. v -= (Elf_Addr) location;
  172. *loc32 = (*loc32 & ~0x3fffff) |
  173. ((v >> 2) & 0x3fffff);
  174. break;
  175. case R_SPARC_LO10:
  176. *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
  177. break;
  178. case R_SPARC_HI22:
  179. *loc32 = (*loc32 & ~0x3fffff) |
  180. ((v >> 10) & 0x3fffff);
  181. break;
  182. default:
  183. printk(KERN_ERR "module %s: Unknown relocation: %x\n",
  184. me->name,
  185. (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
  186. return -ENOEXEC;
  187. }
  188. }
  189. return 0;
  190. }
  191. #ifdef CONFIG_SPARC64
  192. static void do_patch_sections(const Elf_Ehdr *hdr,
  193. const Elf_Shdr *sechdrs)
  194. {
  195. const Elf_Shdr *s, *sun4v_1insn = NULL, *sun4v_2insn = NULL;
  196. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  197. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  198. if (!strcmp(".sun4v_1insn_patch", secstrings + s->sh_name))
  199. sun4v_1insn = s;
  200. if (!strcmp(".sun4v_2insn_patch", secstrings + s->sh_name))
  201. sun4v_2insn = s;
  202. }
  203. if (sun4v_1insn && tlb_type == hypervisor) {
  204. void *p = (void *) sun4v_1insn->sh_addr;
  205. sun4v_patch_1insn_range(p, p + sun4v_1insn->sh_size);
  206. }
  207. if (sun4v_2insn && tlb_type == hypervisor) {
  208. void *p = (void *) sun4v_2insn->sh_addr;
  209. sun4v_patch_2insn_range(p, p + sun4v_2insn->sh_size);
  210. }
  211. }
  212. int module_finalize(const Elf_Ehdr *hdr,
  213. const Elf_Shdr *sechdrs,
  214. struct module *me)
  215. {
  216. /* make jump label nops */
  217. jump_label_apply_nops(me);
  218. do_patch_sections(hdr, sechdrs);
  219. /* Cheetah's I-cache is fully coherent. */
  220. if (tlb_type == spitfire) {
  221. unsigned long va;
  222. flushw_all();
  223. for (va = 0; va < (PAGE_SIZE << 1); va += 32)
  224. spitfire_put_icache_tag(va, 0x0);
  225. __asm__ __volatile__("flush %g6");
  226. }
  227. return 0;
  228. }
  229. #else
  230. int module_finalize(const Elf_Ehdr *hdr,
  231. const Elf_Shdr *sechdrs,
  232. struct module *me)
  233. {
  234. return 0;
  235. }
  236. #endif /* CONFIG_SPARC64 */
  237. void module_arch_cleanup(struct module *mod)
  238. {
  239. }