2
0

305-mips_module_reloc.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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,222 @@ 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. + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
  127. + __free_page(p);
  128. +
  129. + return page_address(page);
  130. +}
  131. +#endif
  132. +
  133. +static void free_phys(void *ptr, unsigned long size)
  134. +{
  135. + struct page *page;
  136. + struct page *end;
  137. +
  138. + page = virt_to_page(ptr);
  139. + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
  140. +
  141. + for (; page < end; ++page)
  142. + __free_page(page);
  143. +}
  144. +
  145. +
  146. void *module_alloc(unsigned long size)
  147. {
  148. +#ifdef MODULE_START
  149. return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
  150. GFP_KERNEL, PAGE_KERNEL, NUMA_NO_NODE,
  151. __builtin_return_address(0));
  152. +#else
  153. + void *ptr;
  154. +
  155. + if (size == 0)
  156. + return NULL;
  157. +
  158. + ptr = alloc_phys(size);
  159. +
  160. + /* If we failed to allocate physically contiguous memory,
  161. + * fall back to regular vmalloc. The module loader code will
  162. + * create jump tables to handle long jumps */
  163. + if (!ptr)
  164. + return vmalloc(size);
  165. +
  166. + return ptr;
  167. +#endif
  168. }
  169. +
  170. +static inline bool is_phys_addr(void *ptr)
  171. +{
  172. +#ifdef CONFIG_64BIT
  173. + return (KSEGX((unsigned long)ptr) == CKSEG0);
  174. +#else
  175. + return (KSEGX(ptr) == KSEG0);
  176. #endif
  177. +}
  178. +
  179. +/* Free memory returned from module_alloc */
  180. +void module_free(struct module *mod, void *module_region)
  181. +{
  182. + if (is_phys_addr(module_region)) {
  183. + if (mod->module_init == module_region)
  184. + free_phys(module_region, mod->init_size);
  185. + else if (mod->module_core == module_region)
  186. + free_phys(module_region, mod->core_size);
  187. + else
  188. + BUG();
  189. + } else {
  190. + vfree(module_region);
  191. + }
  192. +}
  193. +
  194. +static void *__module_alloc(int size, bool phys)
  195. +{
  196. + void *ptr;
  197. +
  198. + if (phys)
  199. + ptr = kmalloc(size, GFP_KERNEL);
  200. + else
  201. + ptr = vmalloc(size);
  202. + return ptr;
  203. +}
  204. +
  205. +static void __module_free(void *ptr)
  206. +{
  207. + if (is_phys_addr(ptr))
  208. + kfree(ptr);
  209. + else
  210. + vfree(ptr);
  211. +}
  212. +
  213. +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  214. + char *secstrings, struct module *mod)
  215. +{
  216. + unsigned int symindex = 0;
  217. + unsigned int core_size, init_size;
  218. + int i;
  219. +
  220. + mod->arch.phys_plt_offset = 0;
  221. + mod->arch.virt_plt_offset = 0;
  222. + mod->arch.phys_plt_tbl = NULL;
  223. + mod->arch.virt_plt_tbl = NULL;
  224. +
  225. + if (IS_ENABLED(CONFIG_64BIT))
  226. + return 0;
  227. +
  228. + for (i = 1; i < hdr->e_shnum; i++)
  229. + if (sechdrs[i].sh_type == SHT_SYMTAB)
  230. + symindex = i;
  231. +
  232. + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
  233. + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
  234. +
  235. + if ((core_size + init_size) == 0)
  236. + return 0;
  237. +
  238. + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
  239. + if (!mod->arch.phys_plt_tbl)
  240. + return -ENOMEM;
  241. +
  242. + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
  243. + if (!mod->arch.virt_plt_tbl) {
  244. + __module_free(mod->arch.phys_plt_tbl);
  245. + mod->arch.phys_plt_tbl = NULL;
  246. + return -ENOMEM;
  247. + }
  248. +
  249. + return 0;
  250. +}
  251. int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
  252. {
  253. @@ -64,8 +272,39 @@ static int apply_r_mips_32_rel(struct mo
  254. return 0;
  255. }
  256. +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
  257. + void *start, Elf_Addr v)
  258. +{
  259. + unsigned *tramp = start + *plt_offset;
  260. + *plt_offset += 4 * sizeof(int);
  261. +
  262. + /* adjust carry for addiu */
  263. + if (v & 0x00008000)
  264. + v += 0x10000;
  265. +
  266. + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
  267. + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
  268. + tramp[2] = 0x03200008; /* jr t9 */
  269. + tramp[3] = 0x00000000; /* nop */
  270. +
  271. + return (Elf_Addr) tramp;
  272. +}
  273. +
  274. +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
  275. +{
  276. + if (is_phys_addr(location))
  277. + return add_plt_entry_to(&me->arch.phys_plt_offset,
  278. + me->arch.phys_plt_tbl, v);
  279. + else
  280. + return add_plt_entry_to(&me->arch.virt_plt_offset,
  281. + me->arch.virt_plt_tbl, v);
  282. +
  283. +}
  284. +
  285. static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
  286. {
  287. + u32 ofs = *location & 0x03ffffff;
  288. +
  289. if (v % 4) {
  290. pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
  291. me->name);
  292. @@ -73,14 +312,17 @@ static int apply_r_mips_26_rel(struct mo
  293. }
  294. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  295. - printk(KERN_ERR
  296. - "module %s: relocation overflow\n",
  297. - me->name);
  298. - return -ENOEXEC;
  299. + v = add_plt_entry(me, location, v + (ofs << 2));
  300. + if (!v) {
  301. + printk(KERN_ERR
  302. + "module %s: relocation overflow\n", me->name);
  303. + return -ENOEXEC;
  304. + }
  305. + ofs = 0;
  306. }
  307. *location = (*location & ~0x03ffffff) |
  308. - ((*location + (v >> 2)) & 0x03ffffff);
  309. + ((ofs + (v >> 2)) & 0x03ffffff);
  310. return 0;
  311. }
  312. @@ -287,9 +529,36 @@ int module_finalize(const Elf_Ehdr *hdr,
  313. list_add(&me->arch.dbe_list, &dbe_list);
  314. spin_unlock_irq(&dbe_lock);
  315. }
  316. +
  317. + /* Get rid of the fixup trampoline if we're running the module
  318. + * from physically mapped address space */
  319. + if (me->arch.phys_plt_offset == 0) {
  320. + __module_free(me->arch.phys_plt_tbl);
  321. + me->arch.phys_plt_tbl = NULL;
  322. + }
  323. + if (me->arch.virt_plt_offset == 0) {
  324. + __module_free(me->arch.virt_plt_tbl);
  325. + me->arch.virt_plt_tbl = NULL;
  326. + }
  327. +
  328. return 0;
  329. }
  330. +void module_arch_freeing_init(struct module *mod)
  331. +{
  332. + if (mod->state == MODULE_STATE_LIVE)
  333. + return;
  334. +
  335. + if (mod->arch.phys_plt_tbl) {
  336. + __module_free(mod->arch.phys_plt_tbl);
  337. + mod->arch.phys_plt_tbl = NULL;
  338. + }
  339. + if (mod->arch.virt_plt_tbl) {
  340. + __module_free(mod->arch.virt_plt_tbl);
  341. + mod->arch.virt_plt_tbl = NULL;
  342. + }
  343. +}
  344. +
  345. void module_arch_cleanup(struct module *mod)
  346. {
  347. spin_lock_irq(&dbe_lock);