305-mips_module_reloc.patch 8.6 KB

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