027-mips_module_reloc.patch 9.1 KB

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