module.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <asm/cacheflush.h>
  18. #include "entry.h"
  19. #ifdef CONFIG_SPARC64
  20. #include <linux/jump_label.h>
  21. static void *module_map(unsigned long size)
  22. {
  23. if (PAGE_ALIGN(size) > MODULES_LEN)
  24. return NULL;
  25. return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  26. GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
  27. __builtin_return_address(0));
  28. }
  29. #else
  30. static void *module_map(unsigned long size)
  31. {
  32. return vmalloc(size);
  33. }
  34. #endif /* CONFIG_SPARC64 */
  35. void *module_alloc(unsigned long size)
  36. {
  37. void *ret;
  38. ret = module_map(size);
  39. if (ret)
  40. memset(ret, 0, size);
  41. return ret;
  42. }
  43. /* Make generic code ignore STT_REGISTER dummy undefined symbols. */
  44. int module_frob_arch_sections(Elf_Ehdr *hdr,
  45. Elf_Shdr *sechdrs,
  46. char *secstrings,
  47. struct module *mod)
  48. {
  49. unsigned int symidx;
  50. Elf_Sym *sym;
  51. char *strtab;
  52. int i;
  53. for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
  54. if (symidx == hdr->e_shnum-1) {
  55. printk("%s: no symtab found.\n", mod->name);
  56. return -ENOEXEC;
  57. }
  58. }
  59. sym = (Elf_Sym *)sechdrs[symidx].sh_addr;
  60. strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
  61. for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
  62. if (sym[i].st_shndx == SHN_UNDEF) {
  63. if (ELF_ST_TYPE(sym[i].st_info) == STT_REGISTER)
  64. sym[i].st_shndx = SHN_ABS;
  65. }
  66. }
  67. return 0;
  68. }
  69. int apply_relocate_add(Elf_Shdr *sechdrs,
  70. const char *strtab,
  71. unsigned int symindex,
  72. unsigned int relsec,
  73. struct module *me)
  74. {
  75. unsigned int i;
  76. Elf_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  77. Elf_Sym *sym;
  78. u8 *location;
  79. u32 *loc32;
  80. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  81. Elf_Addr v;
  82. /* This is where to make the change */
  83. location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  84. + rel[i].r_offset;
  85. loc32 = (u32 *) location;
  86. #ifdef CONFIG_SPARC64
  87. BUG_ON(((u64)location >> (u64)32) != (u64)0);
  88. #endif /* CONFIG_SPARC64 */
  89. /* This is the symbol it is referring to. Note that all
  90. undefined symbols have been resolved. */
  91. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  92. + ELF_R_SYM(rel[i].r_info);
  93. v = sym->st_value + rel[i].r_addend;
  94. switch (ELF_R_TYPE(rel[i].r_info) & 0xff) {
  95. case R_SPARC_DISP32:
  96. v -= (Elf_Addr) location;
  97. *loc32 = v;
  98. break;
  99. #ifdef CONFIG_SPARC64
  100. case R_SPARC_64:
  101. location[0] = v >> 56;
  102. location[1] = v >> 48;
  103. location[2] = v >> 40;
  104. location[3] = v >> 32;
  105. location[4] = v >> 24;
  106. location[5] = v >> 16;
  107. location[6] = v >> 8;
  108. location[7] = v >> 0;
  109. break;
  110. case R_SPARC_WDISP19:
  111. v -= (Elf_Addr) location;
  112. *loc32 = (*loc32 & ~0x7ffff) |
  113. ((v >> 2) & 0x7ffff);
  114. break;
  115. case R_SPARC_OLO10:
  116. *loc32 = (*loc32 & ~0x1fff) |
  117. (((v & 0x3ff) +
  118. (ELF_R_TYPE(rel[i].r_info) >> 8))
  119. & 0x1fff);
  120. break;
  121. #endif /* CONFIG_SPARC64 */
  122. case R_SPARC_32:
  123. case R_SPARC_UA32:
  124. location[0] = v >> 24;
  125. location[1] = v >> 16;
  126. location[2] = v >> 8;
  127. location[3] = v >> 0;
  128. break;
  129. case R_SPARC_WDISP30:
  130. v -= (Elf_Addr) location;
  131. *loc32 = (*loc32 & ~0x3fffffff) |
  132. ((v >> 2) & 0x3fffffff);
  133. break;
  134. case R_SPARC_WDISP22:
  135. v -= (Elf_Addr) location;
  136. *loc32 = (*loc32 & ~0x3fffff) |
  137. ((v >> 2) & 0x3fffff);
  138. break;
  139. case R_SPARC_LO10:
  140. *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
  141. break;
  142. case R_SPARC_HI22:
  143. *loc32 = (*loc32 & ~0x3fffff) |
  144. ((v >> 10) & 0x3fffff);
  145. break;
  146. default:
  147. printk(KERN_ERR "module %s: Unknown relocation: %x\n",
  148. me->name,
  149. (int) (ELF_R_TYPE(rel[i].r_info) & 0xff));
  150. return -ENOEXEC;
  151. }
  152. }
  153. return 0;
  154. }
  155. #ifdef CONFIG_SPARC64
  156. static void do_patch_sections(const Elf_Ehdr *hdr,
  157. const Elf_Shdr *sechdrs)
  158. {
  159. const Elf_Shdr *s, *sun4v_1insn = NULL, *sun4v_2insn = NULL;
  160. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  161. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  162. if (!strcmp(".sun4v_1insn_patch", secstrings + s->sh_name))
  163. sun4v_1insn = s;
  164. if (!strcmp(".sun4v_2insn_patch", secstrings + s->sh_name))
  165. sun4v_2insn = s;
  166. }
  167. if (sun4v_1insn && tlb_type == hypervisor) {
  168. void *p = (void *) sun4v_1insn->sh_addr;
  169. sun4v_patch_1insn_range(p, p + sun4v_1insn->sh_size);
  170. }
  171. if (sun4v_2insn && tlb_type == hypervisor) {
  172. void *p = (void *) sun4v_2insn->sh_addr;
  173. sun4v_patch_2insn_range(p, p + sun4v_2insn->sh_size);
  174. }
  175. }
  176. int module_finalize(const Elf_Ehdr *hdr,
  177. const Elf_Shdr *sechdrs,
  178. struct module *me)
  179. {
  180. /* make jump label nops */
  181. jump_label_apply_nops(me);
  182. do_patch_sections(hdr, sechdrs);
  183. /* Cheetah's I-cache is fully coherent. */
  184. if (tlb_type == spitfire) {
  185. unsigned long va;
  186. flushw_all();
  187. for (va = 0; va < (PAGE_SIZE << 1); va += 32)
  188. spitfire_put_icache_tag(va, 0x0);
  189. __asm__ __volatile__("flush %g6");
  190. }
  191. return 0;
  192. }
  193. #endif /* CONFIG_SPARC64 */