module_64.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* Kernel module help for PPC64.
  2. Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <linux/module.h>
  16. #include <linux/elf.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/err.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/ftrace.h>
  21. #include <linux/bug.h>
  22. #include <asm/module.h>
  23. #include <asm/firmware.h>
  24. #include <asm/code-patching.h>
  25. #include <linux/sort.h>
  26. #include "setup.h"
  27. /* FIXME: We don't do .init separately. To do this, we'd need to have
  28. a separate r2 value in the init and core section, and stub between
  29. them, too.
  30. Using a magic allocator which places modules within 32MB solves
  31. this, and makes other things simpler. Anton?
  32. --RR. */
  33. #if 0
  34. #define DEBUGP printk
  35. #else
  36. #define DEBUGP(fmt , ...)
  37. #endif
  38. /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
  39. the kernel itself). But on PPC64, these need to be used for every
  40. jump, actually, to reset r2 (TOC+0x8000). */
  41. struct ppc64_stub_entry
  42. {
  43. /* 28 byte jump instruction sequence (7 instructions) */
  44. unsigned char jump[28];
  45. unsigned char unused[4];
  46. /* Data for the above code */
  47. struct ppc64_opd_entry opd;
  48. };
  49. /* We use a stub to fix up r2 (TOC ptr) and to jump to the (external)
  50. function which may be more than 24-bits away. We could simply
  51. patch the new r2 value and function pointer into the stub, but it's
  52. significantly shorter to put these values at the end of the stub
  53. code, and patch the stub address (32-bits relative to the TOC ptr,
  54. r2) into the stub. */
  55. static struct ppc64_stub_entry ppc64_stub =
  56. { .jump = {
  57. 0x3d, 0x82, 0x00, 0x00, /* addis r12,r2, <high> */
  58. 0x39, 0x8c, 0x00, 0x00, /* addi r12,r12, <low> */
  59. /* Save current r2 value in magic place on the stack. */
  60. 0xf8, 0x41, 0x00, 0x28, /* std r2,40(r1) */
  61. 0xe9, 0x6c, 0x00, 0x20, /* ld r11,32(r12) */
  62. 0xe8, 0x4c, 0x00, 0x28, /* ld r2,40(r12) */
  63. 0x7d, 0x69, 0x03, 0xa6, /* mtctr r11 */
  64. 0x4e, 0x80, 0x04, 0x20 /* bctr */
  65. } };
  66. /* Count how many different 24-bit relocations (different symbol,
  67. different addend) */
  68. static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
  69. {
  70. unsigned int i, r_info, r_addend, _count_relocs;
  71. /* FIXME: Only count external ones --RR */
  72. _count_relocs = 0;
  73. r_info = 0;
  74. r_addend = 0;
  75. for (i = 0; i < num; i++)
  76. /* Only count 24-bit relocs, others don't need stubs */
  77. if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
  78. (r_info != ELF64_R_SYM(rela[i].r_info) ||
  79. r_addend != rela[i].r_addend)) {
  80. _count_relocs++;
  81. r_info = ELF64_R_SYM(rela[i].r_info);
  82. r_addend = rela[i].r_addend;
  83. }
  84. return _count_relocs;
  85. }
  86. static int relacmp(const void *_x, const void *_y)
  87. {
  88. const Elf64_Rela *x, *y;
  89. y = (Elf64_Rela *)_x;
  90. x = (Elf64_Rela *)_y;
  91. /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
  92. * make the comparison cheaper/faster. It won't affect the sorting or
  93. * the counting algorithms' performance
  94. */
  95. if (x->r_info < y->r_info)
  96. return -1;
  97. else if (x->r_info > y->r_info)
  98. return 1;
  99. else if (x->r_addend < y->r_addend)
  100. return -1;
  101. else if (x->r_addend > y->r_addend)
  102. return 1;
  103. else
  104. return 0;
  105. }
  106. static void relaswap(void *_x, void *_y, int size)
  107. {
  108. uint64_t *x, *y, tmp;
  109. int i;
  110. y = (uint64_t *)_x;
  111. x = (uint64_t *)_y;
  112. for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) {
  113. tmp = x[i];
  114. x[i] = y[i];
  115. y[i] = tmp;
  116. }
  117. }
  118. /* Get size of potential trampolines required. */
  119. static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
  120. const Elf64_Shdr *sechdrs)
  121. {
  122. /* One extra reloc so it's always 0-funcaddr terminated */
  123. unsigned long relocs = 1;
  124. unsigned i;
  125. /* Every relocated section... */
  126. for (i = 1; i < hdr->e_shnum; i++) {
  127. if (sechdrs[i].sh_type == SHT_RELA) {
  128. DEBUGP("Found relocations in section %u\n", i);
  129. DEBUGP("Ptr: %p. Number: %lu\n",
  130. (void *)sechdrs[i].sh_addr,
  131. sechdrs[i].sh_size / sizeof(Elf64_Rela));
  132. /* Sort the relocation information based on a symbol and
  133. * addend key. This is a stable O(n*log n) complexity
  134. * alogrithm but it will reduce the complexity of
  135. * count_relocs() to linear complexity O(n)
  136. */
  137. sort((void *)sechdrs[i].sh_addr,
  138. sechdrs[i].sh_size / sizeof(Elf64_Rela),
  139. sizeof(Elf64_Rela), relacmp, relaswap);
  140. relocs += count_relocs((void *)sechdrs[i].sh_addr,
  141. sechdrs[i].sh_size
  142. / sizeof(Elf64_Rela));
  143. }
  144. }
  145. #ifdef CONFIG_DYNAMIC_FTRACE
  146. /* make the trampoline to the ftrace_caller */
  147. relocs++;
  148. #endif
  149. DEBUGP("Looks like a total of %lu stubs, max\n", relocs);
  150. return relocs * sizeof(struct ppc64_stub_entry);
  151. }
  152. static void dedotify_versions(struct modversion_info *vers,
  153. unsigned long size)
  154. {
  155. struct modversion_info *end;
  156. for (end = (void *)vers + size; vers < end; vers++)
  157. if (vers->name[0] == '.')
  158. memmove(vers->name, vers->name+1, strlen(vers->name));
  159. }
  160. /* Undefined symbols which refer to .funcname, hack to funcname */
  161. static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
  162. {
  163. unsigned int i;
  164. for (i = 1; i < numsyms; i++) {
  165. if (syms[i].st_shndx == SHN_UNDEF) {
  166. char *name = strtab + syms[i].st_name;
  167. if (name[0] == '.')
  168. memmove(name, name+1, strlen(name));
  169. }
  170. }
  171. }
  172. int module_frob_arch_sections(Elf64_Ehdr *hdr,
  173. Elf64_Shdr *sechdrs,
  174. char *secstrings,
  175. struct module *me)
  176. {
  177. unsigned int i;
  178. /* Find .toc and .stubs sections, symtab and strtab */
  179. for (i = 1; i < hdr->e_shnum; i++) {
  180. char *p;
  181. if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
  182. me->arch.stubs_section = i;
  183. else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0)
  184. me->arch.toc_section = i;
  185. else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
  186. dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
  187. sechdrs[i].sh_size);
  188. /* We don't handle .init for the moment: rename to _init */
  189. while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
  190. p[0] = '_';
  191. if (sechdrs[i].sh_type == SHT_SYMTAB)
  192. dedotify((void *)hdr + sechdrs[i].sh_offset,
  193. sechdrs[i].sh_size / sizeof(Elf64_Sym),
  194. (void *)hdr
  195. + sechdrs[sechdrs[i].sh_link].sh_offset);
  196. }
  197. if (!me->arch.stubs_section) {
  198. printk("%s: doesn't contain .stubs.\n", me->name);
  199. return -ENOEXEC;
  200. }
  201. /* If we don't have a .toc, just use .stubs. We need to set r2
  202. to some reasonable value in case the module calls out to
  203. other functions via a stub, or if a function pointer escapes
  204. the module by some means. */
  205. if (!me->arch.toc_section)
  206. me->arch.toc_section = me->arch.stubs_section;
  207. /* Override the stubs size */
  208. sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
  209. return 0;
  210. }
  211. /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this
  212. gives the value maximum span in an instruction which uses a signed
  213. offset) */
  214. static inline unsigned long my_r2(Elf64_Shdr *sechdrs, struct module *me)
  215. {
  216. return sechdrs[me->arch.toc_section].sh_addr + 0x8000;
  217. }
  218. /* Both low and high 16 bits are added as SIGNED additions, so if low
  219. 16 bits has high bit set, high 16 bits must be adjusted. These
  220. macros do that (stolen from binutils). */
  221. #define PPC_LO(v) ((v) & 0xffff)
  222. #define PPC_HI(v) (((v) >> 16) & 0xffff)
  223. #define PPC_HA(v) PPC_HI ((v) + 0x8000)
  224. /* Patch stub to reference function and correct r2 value. */
  225. static inline int create_stub(Elf64_Shdr *sechdrs,
  226. struct ppc64_stub_entry *entry,
  227. struct ppc64_opd_entry *opd,
  228. struct module *me)
  229. {
  230. Elf64_Half *loc1, *loc2;
  231. long reladdr;
  232. *entry = ppc64_stub;
  233. loc1 = (Elf64_Half *)&entry->jump[2];
  234. loc2 = (Elf64_Half *)&entry->jump[6];
  235. /* Stub uses address relative to r2. */
  236. reladdr = (unsigned long)entry - my_r2(sechdrs, me);
  237. if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
  238. printk("%s: Address %p of stub out of range of %p.\n",
  239. me->name, (void *)reladdr, (void *)my_r2);
  240. return 0;
  241. }
  242. DEBUGP("Stub %p get data from reladdr %li\n", entry, reladdr);
  243. *loc1 = PPC_HA(reladdr);
  244. *loc2 = PPC_LO(reladdr);
  245. entry->opd.funcaddr = opd->funcaddr;
  246. entry->opd.r2 = opd->r2;
  247. return 1;
  248. }
  249. /* Create stub to jump to function described in this OPD: we need the
  250. stub to set up the TOC ptr (r2) for the function. */
  251. static unsigned long stub_for_addr(Elf64_Shdr *sechdrs,
  252. unsigned long opdaddr,
  253. struct module *me)
  254. {
  255. struct ppc64_stub_entry *stubs;
  256. struct ppc64_opd_entry *opd = (void *)opdaddr;
  257. unsigned int i, num_stubs;
  258. num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
  259. /* Find this stub, or if that fails, the next avail. entry */
  260. stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
  261. for (i = 0; stubs[i].opd.funcaddr; i++) {
  262. BUG_ON(i >= num_stubs);
  263. if (stubs[i].opd.funcaddr == opd->funcaddr)
  264. return (unsigned long)&stubs[i];
  265. }
  266. if (!create_stub(sechdrs, &stubs[i], opd, me))
  267. return 0;
  268. return (unsigned long)&stubs[i];
  269. }
  270. /* We expect a noop next: if it is, replace it with instruction to
  271. restore r2. */
  272. static int restore_r2(u32 *instruction, struct module *me)
  273. {
  274. if (*instruction != PPC_INST_NOP) {
  275. printk("%s: Expect noop after relocate, got %08x\n",
  276. me->name, *instruction);
  277. return 0;
  278. }
  279. *instruction = 0xe8410028; /* ld r2,40(r1) */
  280. return 1;
  281. }
  282. int apply_relocate_add(Elf64_Shdr *sechdrs,
  283. const char *strtab,
  284. unsigned int symindex,
  285. unsigned int relsec,
  286. struct module *me)
  287. {
  288. unsigned int i;
  289. Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  290. Elf64_Sym *sym;
  291. unsigned long *location;
  292. unsigned long value;
  293. DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
  294. sechdrs[relsec].sh_info);
  295. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  296. /* This is where to make the change */
  297. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  298. + rela[i].r_offset;
  299. /* This is the symbol it is referring to */
  300. sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
  301. + ELF64_R_SYM(rela[i].r_info);
  302. DEBUGP("RELOC at %p: %li-type as %s (%lu) + %li\n",
  303. location, (long)ELF64_R_TYPE(rela[i].r_info),
  304. strtab + sym->st_name, (unsigned long)sym->st_value,
  305. (long)rela[i].r_addend);
  306. /* `Everything is relative'. */
  307. value = sym->st_value + rela[i].r_addend;
  308. switch (ELF64_R_TYPE(rela[i].r_info)) {
  309. case R_PPC64_ADDR32:
  310. /* Simply set it */
  311. *(u32 *)location = value;
  312. break;
  313. case R_PPC64_ADDR64:
  314. /* Simply set it */
  315. *(unsigned long *)location = value;
  316. break;
  317. case R_PPC64_TOC:
  318. *(unsigned long *)location = my_r2(sechdrs, me);
  319. break;
  320. case R_PPC64_TOC16:
  321. /* Subtract TOC pointer */
  322. value -= my_r2(sechdrs, me);
  323. if (value + 0x8000 > 0xffff) {
  324. printk("%s: bad TOC16 relocation (%lu)\n",
  325. me->name, value);
  326. return -ENOEXEC;
  327. }
  328. *((uint16_t *) location)
  329. = (*((uint16_t *) location) & ~0xffff)
  330. | (value & 0xffff);
  331. break;
  332. case R_PPC64_TOC16_DS:
  333. /* Subtract TOC pointer */
  334. value -= my_r2(sechdrs, me);
  335. if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
  336. printk("%s: bad TOC16_DS relocation (%lu)\n",
  337. me->name, value);
  338. return -ENOEXEC;
  339. }
  340. *((uint16_t *) location)
  341. = (*((uint16_t *) location) & ~0xfffc)
  342. | (value & 0xfffc);
  343. break;
  344. case R_PPC_REL24:
  345. /* FIXME: Handle weak symbols here --RR */
  346. if (sym->st_shndx == SHN_UNDEF) {
  347. /* External: go via stub */
  348. value = stub_for_addr(sechdrs, value, me);
  349. if (!value)
  350. return -ENOENT;
  351. if (!restore_r2((u32 *)location + 1, me))
  352. return -ENOEXEC;
  353. }
  354. /* Convert value to relative */
  355. value -= (unsigned long)location;
  356. if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
  357. printk("%s: REL24 %li out of range!\n",
  358. me->name, (long int)value);
  359. return -ENOEXEC;
  360. }
  361. /* Only replace bits 2 through 26 */
  362. *(uint32_t *)location
  363. = (*(uint32_t *)location & ~0x03fffffc)
  364. | (value & 0x03fffffc);
  365. break;
  366. case R_PPC64_REL64:
  367. /* 64 bits relative (used by features fixups) */
  368. *location = value - (unsigned long)location;
  369. break;
  370. default:
  371. printk("%s: Unknown ADD relocation: %lu\n",
  372. me->name,
  373. (unsigned long)ELF64_R_TYPE(rela[i].r_info));
  374. return -ENOEXEC;
  375. }
  376. }
  377. #ifdef CONFIG_DYNAMIC_FTRACE
  378. me->arch.toc = my_r2(sechdrs, me);
  379. me->arch.tramp = stub_for_addr(sechdrs,
  380. (unsigned long)ftrace_caller,
  381. me);
  382. #endif
  383. return 0;
  384. }