305-mips_module_reloc.patch 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. --- a/arch/mips/Makefile
  2. +++ b/arch/mips/Makefile
  3. @@ -90,8 +90,8 @@ all-$(CONFIG_SYS_SUPPORTS_ZBOOT)+= vmlin
  4. cflags-y += -G 0 -mno-abicalls -fno-pic -pipe
  5. cflags-y += -msoft-float
  6. LDFLAGS_vmlinux += -G 0 -static -n -nostdlib
  7. -KBUILD_AFLAGS_MODULE += -mlong-calls
  8. -KBUILD_CFLAGS_MODULE += -mlong-calls
  9. +KBUILD_AFLAGS_MODULE += -mno-long-calls
  10. +KBUILD_CFLAGS_MODULE += -mno-long-calls
  11. cflags-y += -ffreestanding
  12. --- a/arch/mips/include/asm/module.h
  13. +++ b/arch/mips/include/asm/module.h
  14. @@ -9,6 +9,11 @@ struct mod_arch_specific {
  15. struct list_head dbe_list;
  16. const struct exception_table_entry *dbe_start;
  17. const struct exception_table_entry *dbe_end;
  18. +
  19. + void *phys_plt_tbl;
  20. + void *virt_plt_tbl;
  21. + unsigned int phys_plt_offset;
  22. + unsigned int virt_plt_offset;
  23. };
  24. typedef uint8_t Elf64_Byte; /* Type for a 8-bit quantity. */
  25. --- a/arch/mips/kernel/module.c
  26. +++ b/arch/mips/kernel/module.c
  27. @@ -45,6 +45,117 @@ static struct mips_hi16 *mips_hi16_list;
  28. static LIST_HEAD(dbe_list);
  29. static DEFINE_SPINLOCK(dbe_lock);
  30. +/*
  31. + * Get the potential max trampolines size required of the init and
  32. + * non-init sections. Only used if we cannot find enough contiguous
  33. + * physically mapped memory to put the module into.
  34. + */
  35. +static unsigned int
  36. +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  37. + const char *secstrings, unsigned int symindex, bool is_init)
  38. +{
  39. + unsigned long ret = 0;
  40. + unsigned int i, j;
  41. + Elf_Sym *syms;
  42. +
  43. + /* Everything marked ALLOC (this includes the exported symbols) */
  44. + for (i = 1; i < hdr->e_shnum; ++i) {
  45. + unsigned int info = sechdrs[i].sh_info;
  46. +
  47. + if (sechdrs[i].sh_type != SHT_REL
  48. + && sechdrs[i].sh_type != SHT_RELA)
  49. + continue;
  50. +
  51. + /* Not a valid relocation section? */
  52. + if (info >= hdr->e_shnum)
  53. + continue;
  54. +
  55. + /* Don't bother with non-allocated sections */
  56. + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  57. + continue;
  58. +
  59. + /* If it's called *.init*, and we're not init, we're
  60. + not interested */
  61. + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
  62. + != is_init)
  63. + continue;
  64. +
  65. + syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
  66. + if (sechdrs[i].sh_type == SHT_REL) {
  67. + Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
  68. + unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
  69. +
  70. + for (j = 0; j < size; ++j) {
  71. + Elf_Sym *sym;
  72. +
  73. + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
  74. + continue;
  75. +
  76. + sym = syms + ELF_MIPS_R_SYM(rel[j]);
  77. + if (!is_init && sym->st_shndx != SHN_UNDEF)
  78. + continue;
  79. +
  80. + ret += 4 * sizeof(int);
  81. + }
  82. + } else {
  83. + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
  84. + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
  85. +
  86. + for (j = 0; j < size; ++j) {
  87. + Elf_Sym *sym;
  88. +
  89. + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
  90. + continue;
  91. +
  92. + sym = syms + ELF_MIPS_R_SYM(rela[j]);
  93. + if (!is_init && sym->st_shndx != SHN_UNDEF)
  94. + continue;
  95. +
  96. + ret += 4 * sizeof(int);
  97. + }
  98. + }
  99. + }
  100. +
  101. + return ret;
  102. +}
  103. +
  104. +#ifndef MODULE_START
  105. +static void *alloc_phys(unsigned long size)
  106. +{
  107. + unsigned order;
  108. + struct page *page;
  109. + struct page *p;
  110. +
  111. + size = PAGE_ALIGN(size);
  112. + order = get_order(size);
  113. +
  114. + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
  115. + __GFP_THISNODE, order);
  116. + if (!page)
  117. + return NULL;
  118. +
  119. + split_page(page, order);
  120. +
  121. + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
  122. + __free_page(p);
  123. +
  124. + return page_address(page);
  125. +}
  126. +#endif
  127. +
  128. +static void free_phys(void *ptr, unsigned long size)
  129. +{
  130. + struct page *page;
  131. + struct page *end;
  132. +
  133. + page = virt_to_page(ptr);
  134. + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
  135. +
  136. + for (; page < end; ++page)
  137. + __free_page(page);
  138. +}
  139. +
  140. +
  141. void *module_alloc(unsigned long size)
  142. {
  143. #ifdef MODULE_START
  144. @@ -52,21 +163,99 @@ void *module_alloc(unsigned long size)
  145. GFP_KERNEL, PAGE_KERNEL, -1,
  146. __builtin_return_address(0));
  147. #else
  148. + void *ptr;
  149. +
  150. if (size == 0)
  151. return NULL;
  152. - return vmalloc(size);
  153. +
  154. + ptr = alloc_phys(size);
  155. +
  156. + /* If we failed to allocate physically contiguous memory,
  157. + * fall back to regular vmalloc. The module loader code will
  158. + * create jump tables to handle long jumps */
  159. + if (!ptr)
  160. + return vmalloc(size);
  161. +
  162. + return ptr;
  163. +#endif
  164. +}
  165. +
  166. +static inline bool is_phys_addr(void *ptr)
  167. +{
  168. +#ifdef CONFIG_64BIT
  169. + return (KSEGX((unsigned long)ptr) == CKSEG0);
  170. +#else
  171. + return (KSEGX(ptr) == KSEG0);
  172. #endif
  173. }
  174. /* Free memory returned from module_alloc */
  175. void module_free(struct module *mod, void *module_region)
  176. {
  177. - vfree(module_region);
  178. + if (is_phys_addr(module_region)) {
  179. + if (mod->module_init == module_region)
  180. + free_phys(module_region, mod->init_size);
  181. + else if (mod->module_core == module_region)
  182. + free_phys(module_region, mod->core_size);
  183. + else
  184. + BUG();
  185. + } else {
  186. + vfree(module_region);
  187. + }
  188. +}
  189. +
  190. +static void *__module_alloc(int size, bool phys)
  191. +{
  192. + void *ptr;
  193. +
  194. + if (phys)
  195. + ptr = kmalloc(size, GFP_KERNEL);
  196. + else
  197. + ptr = vmalloc(size);
  198. + return ptr;
  199. +}
  200. +
  201. +static void __module_free(void *ptr)
  202. +{
  203. + if (is_phys_addr(ptr))
  204. + kfree(ptr);
  205. + else
  206. + vfree(ptr);
  207. }
  208. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  209. char *secstrings, struct module *mod)
  210. {
  211. + unsigned int symindex = 0;
  212. + unsigned int core_size, init_size;
  213. + int i;
  214. +
  215. + for (i = 1; i < hdr->e_shnum; i++)
  216. + if (sechdrs[i].sh_type == SHT_SYMTAB)
  217. + symindex = i;
  218. +
  219. + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
  220. + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
  221. +
  222. + mod->arch.phys_plt_offset = 0;
  223. + mod->arch.virt_plt_offset = 0;
  224. + mod->arch.phys_plt_tbl = NULL;
  225. + mod->arch.virt_plt_tbl = NULL;
  226. +
  227. + if ((core_size + init_size) == 0)
  228. + return 0;
  229. +
  230. + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
  231. + if (!mod->arch.phys_plt_tbl)
  232. + return -ENOMEM;
  233. +
  234. + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
  235. + if (!mod->arch.virt_plt_tbl) {
  236. + __module_free(mod->arch.phys_plt_tbl);
  237. + mod->arch.phys_plt_tbl = NULL;
  238. + return -ENOMEM;
  239. + }
  240. +
  241. return 0;
  242. }
  243. @@ -89,28 +278,36 @@ static int apply_r_mips_32_rela(struct m
  244. return 0;
  245. }
  246. -static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
  247. +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
  248. + void *start, Elf_Addr v)
  249. {
  250. - if (v % 4) {
  251. - pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
  252. - me->name);
  253. - return -ENOEXEC;
  254. - }
  255. + unsigned *tramp = start + *plt_offset;
  256. + *plt_offset += 4 * sizeof(int);
  257. - if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  258. - printk(KERN_ERR
  259. - "module %s: relocation overflow\n",
  260. - me->name);
  261. - return -ENOEXEC;
  262. - }
  263. + /* adjust carry for addiu */
  264. + if (v & 0x00008000)
  265. + v += 0x10000;
  266. - *location = (*location & ~0x03ffffff) |
  267. - ((*location + (v >> 2)) & 0x03ffffff);
  268. + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
  269. + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
  270. + tramp[2] = 0x03200008; /* jr t9 */
  271. + tramp[3] = 0x00000000; /* nop */
  272. - return 0;
  273. + return (Elf_Addr) tramp;
  274. }
  275. -static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
  276. +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
  277. +{
  278. + if (is_phys_addr(location))
  279. + return add_plt_entry_to(&me->arch.phys_plt_offset,
  280. + me->arch.phys_plt_tbl, v);
  281. + else
  282. + return add_plt_entry_to(&me->arch.virt_plt_offset,
  283. + me->arch.virt_plt_tbl, v);
  284. +
  285. +}
  286. +
  287. +static int set_r_mips_26(struct module *me, u32 *location, u32 ofs, Elf_Addr v)
  288. {
  289. if (v % 4) {
  290. pr_err("module %s: dangerous R_MIPS_26 RELArelocation\n",
  291. @@ -119,17 +316,31 @@ static int apply_r_mips_26_rela(struct m
  292. }
  293. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  294. - printk(KERN_ERR
  295. + v = add_plt_entry(me, location, v + (ofs << 2));
  296. + if (!v) {
  297. + printk(KERN_ERR
  298. "module %s: relocation overflow\n",
  299. me->name);
  300. - return -ENOEXEC;
  301. + return -ENOEXEC;
  302. + }
  303. + ofs = 0;
  304. }
  305. - *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
  306. + *location = (*location & ~0x03ffffff) | ((ofs + (v >> 2)) & 0x03ffffff);
  307. return 0;
  308. }
  309. +static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
  310. +{
  311. + return set_r_mips_26(me, location, *location & 0x03ffffff, v);
  312. +}
  313. +
  314. +static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
  315. +{
  316. + return set_r_mips_26(me, location, 0, v);
  317. +}
  318. +
  319. static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
  320. {
  321. struct mips_hi16 *n;
  322. @@ -397,11 +608,32 @@ int module_finalize(const Elf_Ehdr *hdr,
  323. list_add(&me->arch.dbe_list, &dbe_list);
  324. spin_unlock_irq(&dbe_lock);
  325. }
  326. +
  327. + /* Get rid of the fixup trampoline if we're running the module
  328. + * from physically mapped address space */
  329. + if (me->arch.phys_plt_offset == 0) {
  330. + __module_free(me->arch.phys_plt_tbl);
  331. + me->arch.phys_plt_tbl = NULL;
  332. + }
  333. + if (me->arch.virt_plt_offset == 0) {
  334. + __module_free(me->arch.virt_plt_tbl);
  335. + me->arch.virt_plt_tbl = NULL;
  336. + }
  337. +
  338. return 0;
  339. }
  340. void module_arch_cleanup(struct module *mod)
  341. {
  342. + if (mod->arch.phys_plt_tbl) {
  343. + __module_free(mod->arch.phys_plt_tbl);
  344. + mod->arch.phys_plt_tbl = NULL;
  345. + }
  346. + if (mod->arch.virt_plt_tbl) {
  347. + __module_free(mod->arch.virt_plt_tbl);
  348. + mod->arch.virt_plt_tbl = NULL;
  349. + }
  350. +
  351. spin_lock_irq(&dbe_lock);
  352. list_del(&mod->arch.dbe_list);
  353. spin_unlock_irq(&dbe_lock);