305-mips_module_reloc.patch 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. @@ -44,14 +44,219 @@ static struct mips_hi16 *mips_hi16_list;
  28. static LIST_HEAD(dbe_list);
  29. static DEFINE_SPINLOCK(dbe_lock);
  30. -#ifdef MODULE_START
  31. +/*
  32. + * Get the potential max trampolines size required of the init and
  33. + * non-init sections. Only used if we cannot find enough contiguous
  34. + * physically mapped memory to put the module into.
  35. + */
  36. +static unsigned int
  37. +get_plt_size(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
  38. + const char *secstrings, unsigned int symindex, bool is_init)
  39. +{
  40. + unsigned long ret = 0;
  41. + unsigned int i, j;
  42. + Elf_Sym *syms;
  43. +
  44. + /* Everything marked ALLOC (this includes the exported symbols) */
  45. + for (i = 1; i < hdr->e_shnum; ++i) {
  46. + unsigned int info = sechdrs[i].sh_info;
  47. +
  48. + if (sechdrs[i].sh_type != SHT_REL
  49. + && sechdrs[i].sh_type != SHT_RELA)
  50. + continue;
  51. +
  52. + /* Not a valid relocation section? */
  53. + if (info >= hdr->e_shnum)
  54. + continue;
  55. +
  56. + /* Don't bother with non-allocated sections */
  57. + if (!(sechdrs[info].sh_flags & SHF_ALLOC))
  58. + continue;
  59. +
  60. + /* If it's called *.init*, and we're not init, we're
  61. + not interested */
  62. + if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
  63. + != is_init)
  64. + continue;
  65. +
  66. + syms = (Elf_Sym *) sechdrs[symindex].sh_addr;
  67. + if (sechdrs[i].sh_type == SHT_REL) {
  68. + Elf_Mips_Rel *rel = (void *) sechdrs[i].sh_addr;
  69. + unsigned int size = sechdrs[i].sh_size / sizeof(*rel);
  70. +
  71. + for (j = 0; j < size; ++j) {
  72. + Elf_Sym *sym;
  73. +
  74. + if (ELF_MIPS_R_TYPE(rel[j]) != R_MIPS_26)
  75. + continue;
  76. +
  77. + sym = syms + ELF_MIPS_R_SYM(rel[j]);
  78. + if (!is_init && sym->st_shndx != SHN_UNDEF)
  79. + continue;
  80. +
  81. + ret += 4 * sizeof(int);
  82. + }
  83. + } else {
  84. + Elf_Mips_Rela *rela = (void *) sechdrs[i].sh_addr;
  85. + unsigned int size = sechdrs[i].sh_size / sizeof(*rela);
  86. +
  87. + for (j = 0; j < size; ++j) {
  88. + Elf_Sym *sym;
  89. +
  90. + if (ELF_MIPS_R_TYPE(rela[j]) != R_MIPS_26)
  91. + continue;
  92. +
  93. + sym = syms + ELF_MIPS_R_SYM(rela[j]);
  94. + if (!is_init && sym->st_shndx != SHN_UNDEF)
  95. + continue;
  96. +
  97. + ret += 4 * sizeof(int);
  98. + }
  99. + }
  100. + }
  101. +
  102. + return ret;
  103. +}
  104. +
  105. +#ifndef MODULE_START
  106. +static void *alloc_phys(unsigned long size)
  107. +{
  108. + unsigned order;
  109. + struct page *page;
  110. + struct page *p;
  111. +
  112. + size = PAGE_ALIGN(size);
  113. + order = get_order(size);
  114. +
  115. + page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
  116. + __GFP_THISNODE, order);
  117. + if (!page)
  118. + return NULL;
  119. +
  120. + split_page(page, order);
  121. +
  122. + for (p = page + (size >> PAGE_SHIFT); p < page + (1 << order); ++p)
  123. + __free_page(p);
  124. +
  125. + return page_address(page);
  126. +}
  127. +#endif
  128. +
  129. +static void free_phys(void *ptr, unsigned long size)
  130. +{
  131. + struct page *page;
  132. + struct page *end;
  133. +
  134. + page = virt_to_page(ptr);
  135. + end = page + (PAGE_ALIGN(size) >> PAGE_SHIFT);
  136. +
  137. + for (; page < end; ++page)
  138. + __free_page(page);
  139. +}
  140. +
  141. +
  142. void *module_alloc(unsigned long size)
  143. {
  144. +#ifdef MODULE_START
  145. return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
  146. GFP_KERNEL, PAGE_KERNEL, -1,
  147. __builtin_return_address(0));
  148. +#else
  149. + void *ptr;
  150. +
  151. + if (size == 0)
  152. + return NULL;
  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. +
  175. +/* Free memory returned from module_alloc */
  176. +void module_free(struct module *mod, void *module_region)
  177. +{
  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. +
  209. +int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  210. + char *secstrings, struct module *mod)
  211. +{
  212. + unsigned int symindex = 0;
  213. + unsigned int core_size, init_size;
  214. + int i;
  215. +
  216. + for (i = 1; i < hdr->e_shnum; i++)
  217. + if (sechdrs[i].sh_type == SHT_SYMTAB)
  218. + symindex = i;
  219. +
  220. + core_size = get_plt_size(hdr, sechdrs, secstrings, symindex, false);
  221. + init_size = get_plt_size(hdr, sechdrs, secstrings, symindex, true);
  222. +
  223. + mod->arch.phys_plt_offset = 0;
  224. + mod->arch.virt_plt_offset = 0;
  225. + mod->arch.phys_plt_tbl = NULL;
  226. + mod->arch.virt_plt_tbl = NULL;
  227. +
  228. + if ((core_size + init_size) == 0)
  229. + return 0;
  230. +
  231. + mod->arch.phys_plt_tbl = __module_alloc(core_size + init_size, 1);
  232. + if (!mod->arch.phys_plt_tbl)
  233. + return -ENOMEM;
  234. +
  235. + mod->arch.virt_plt_tbl = __module_alloc(core_size + init_size, 0);
  236. + if (!mod->arch.virt_plt_tbl) {
  237. + __module_free(mod->arch.phys_plt_tbl);
  238. + mod->arch.phys_plt_tbl = NULL;
  239. + return -ENOMEM;
  240. + }
  241. +
  242. + return 0;
  243. +}
  244. static int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
  245. {
  246. @@ -72,28 +277,36 @@ static int apply_r_mips_32_rela(struct m
  247. return 0;
  248. }
  249. -static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
  250. +static Elf_Addr add_plt_entry_to(unsigned *plt_offset,
  251. + void *start, Elf_Addr v)
  252. {
  253. - if (v % 4) {
  254. - pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
  255. - me->name);
  256. - return -ENOEXEC;
  257. - }
  258. + unsigned *tramp = start + *plt_offset;
  259. + *plt_offset += 4 * sizeof(int);
  260. - if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  261. - printk(KERN_ERR
  262. - "module %s: relocation overflow\n",
  263. - me->name);
  264. - return -ENOEXEC;
  265. - }
  266. + /* adjust carry for addiu */
  267. + if (v & 0x00008000)
  268. + v += 0x10000;
  269. - *location = (*location & ~0x03ffffff) |
  270. - ((*location + (v >> 2)) & 0x03ffffff);
  271. + tramp[0] = 0x3c190000 | (v >> 16); /* lui t9, hi16 */
  272. + tramp[1] = 0x27390000 | (v & 0xffff); /* addiu t9, t9, lo16 */
  273. + tramp[2] = 0x03200008; /* jr t9 */
  274. + tramp[3] = 0x00000000; /* nop */
  275. - return 0;
  276. + return (Elf_Addr) tramp;
  277. }
  278. -static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
  279. +static Elf_Addr add_plt_entry(struct module *me, void *location, Elf_Addr v)
  280. +{
  281. + if (is_phys_addr(location))
  282. + return add_plt_entry_to(&me->arch.phys_plt_offset,
  283. + me->arch.phys_plt_tbl, v);
  284. + else
  285. + return add_plt_entry_to(&me->arch.virt_plt_offset,
  286. + me->arch.virt_plt_tbl, v);
  287. +
  288. +}
  289. +
  290. +static int set_r_mips_26(struct module *me, u32 *location, u32 ofs, Elf_Addr v)
  291. {
  292. if (v % 4) {
  293. pr_err("module %s: dangerous R_MIPS_26 RELArelocation\n",
  294. @@ -102,17 +315,31 @@ static int apply_r_mips_26_rela(struct m
  295. }
  296. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  297. - printk(KERN_ERR
  298. + v = add_plt_entry(me, location, v + (ofs << 2));
  299. + if (!v) {
  300. + printk(KERN_ERR
  301. "module %s: relocation overflow\n",
  302. me->name);
  303. - return -ENOEXEC;
  304. + return -ENOEXEC;
  305. + }
  306. + ofs = 0;
  307. }
  308. - *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
  309. + *location = (*location & ~0x03ffffff) | ((ofs + (v >> 2)) & 0x03ffffff);
  310. return 0;
  311. }
  312. +static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
  313. +{
  314. + return set_r_mips_26(me, location, *location & 0x03ffffff, v);
  315. +}
  316. +
  317. +static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
  318. +{
  319. + return set_r_mips_26(me, location, 0, v);
  320. +}
  321. +
  322. static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
  323. {
  324. struct mips_hi16 *n;
  325. @@ -380,11 +607,32 @@ int module_finalize(const Elf_Ehdr *hdr,
  326. list_add(&me->arch.dbe_list, &dbe_list);
  327. spin_unlock_irq(&dbe_lock);
  328. }
  329. +
  330. + /* Get rid of the fixup trampoline if we're running the module
  331. + * from physically mapped address space */
  332. + if (me->arch.phys_plt_offset == 0) {
  333. + __module_free(me->arch.phys_plt_tbl);
  334. + me->arch.phys_plt_tbl = NULL;
  335. + }
  336. + if (me->arch.virt_plt_offset == 0) {
  337. + __module_free(me->arch.virt_plt_tbl);
  338. + me->arch.virt_plt_tbl = NULL;
  339. + }
  340. +
  341. return 0;
  342. }
  343. void module_arch_cleanup(struct module *mod)
  344. {
  345. + if (mod->arch.phys_plt_tbl) {
  346. + __module_free(mod->arch.phys_plt_tbl);
  347. + mod->arch.phys_plt_tbl = NULL;
  348. + }
  349. + if (mod->arch.virt_plt_tbl) {
  350. + __module_free(mod->arch.virt_plt_tbl);
  351. + mod->arch.virt_plt_tbl = NULL;
  352. + }
  353. +
  354. spin_lock_irq(&dbe_lock);
  355. list_del(&mod->arch.dbe_list);
  356. spin_unlock_irq(&dbe_lock);