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