module.c 5.1 KB

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