module.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Kernel module help for s390.
  4. *
  5. * S390 version
  6. * Copyright IBM Corp. 2002, 2003
  7. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. *
  10. * based on i386 version
  11. * Copyright (C) 2001 Rusty Russell.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/elf.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/fs.h>
  17. #include <linux/string.h>
  18. #include <linux/kernel.h>
  19. #include <linux/moduleloader.h>
  20. #include <linux/bug.h>
  21. #include <asm/alternative.h>
  22. #include <asm/nospec-branch.h>
  23. #include <asm/facility.h>
  24. #if 0
  25. #define DEBUGP printk
  26. #else
  27. #define DEBUGP(fmt , ...)
  28. #endif
  29. #define PLT_ENTRY_SIZE 20
  30. void *module_alloc(unsigned long size)
  31. {
  32. if (PAGE_ALIGN(size) > MODULES_LEN)
  33. return NULL;
  34. return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  35. GFP_KERNEL, PAGE_KERNEL_EXEC,
  36. 0, NUMA_NO_NODE,
  37. __builtin_return_address(0));
  38. }
  39. void module_arch_freeing_init(struct module *mod)
  40. {
  41. if (is_livepatch_module(mod) &&
  42. mod->state == MODULE_STATE_LIVE)
  43. return;
  44. vfree(mod->arch.syminfo);
  45. mod->arch.syminfo = NULL;
  46. }
  47. static void check_rela(Elf_Rela *rela, struct module *me)
  48. {
  49. struct mod_arch_syminfo *info;
  50. info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
  51. switch (ELF_R_TYPE (rela->r_info)) {
  52. case R_390_GOT12: /* 12 bit GOT offset. */
  53. case R_390_GOT16: /* 16 bit GOT offset. */
  54. case R_390_GOT20: /* 20 bit GOT offset. */
  55. case R_390_GOT32: /* 32 bit GOT offset. */
  56. case R_390_GOT64: /* 64 bit GOT offset. */
  57. case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
  58. case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
  59. case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
  60. case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
  61. case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
  62. case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
  63. case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
  64. if (info->got_offset == -1UL) {
  65. info->got_offset = me->arch.got_size;
  66. me->arch.got_size += sizeof(void*);
  67. }
  68. break;
  69. case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
  70. case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
  71. case R_390_PLT32: /* 32 bit PC relative PLT address. */
  72. case R_390_PLT64: /* 64 bit PC relative PLT address. */
  73. case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
  74. case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
  75. case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
  76. if (info->plt_offset == -1UL) {
  77. info->plt_offset = me->arch.plt_size;
  78. me->arch.plt_size += PLT_ENTRY_SIZE;
  79. }
  80. break;
  81. case R_390_COPY:
  82. case R_390_GLOB_DAT:
  83. case R_390_JMP_SLOT:
  84. case R_390_RELATIVE:
  85. /* Only needed if we want to support loading of
  86. modules linked with -shared. */
  87. break;
  88. }
  89. }
  90. /*
  91. * Account for GOT and PLT relocations. We can't add sections for
  92. * got and plt but we can increase the core module size.
  93. */
  94. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  95. char *secstrings, struct module *me)
  96. {
  97. Elf_Shdr *symtab;
  98. Elf_Sym *symbols;
  99. Elf_Rela *rela;
  100. char *strings;
  101. int nrela, i, j;
  102. /* Find symbol table and string table. */
  103. symtab = NULL;
  104. for (i = 0; i < hdr->e_shnum; i++)
  105. switch (sechdrs[i].sh_type) {
  106. case SHT_SYMTAB:
  107. symtab = sechdrs + i;
  108. break;
  109. }
  110. if (!symtab) {
  111. printk(KERN_ERR "module %s: no symbol table\n", me->name);
  112. return -ENOEXEC;
  113. }
  114. /* Allocate one syminfo structure per symbol. */
  115. me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
  116. me->arch.syminfo = vmalloc(array_size(sizeof(struct mod_arch_syminfo),
  117. me->arch.nsyms));
  118. if (!me->arch.syminfo)
  119. return -ENOMEM;
  120. symbols = (void *) hdr + symtab->sh_offset;
  121. strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
  122. for (i = 0; i < me->arch.nsyms; i++) {
  123. if (symbols[i].st_shndx == SHN_UNDEF &&
  124. strcmp(strings + symbols[i].st_name,
  125. "_GLOBAL_OFFSET_TABLE_") == 0)
  126. /* "Define" it as absolute. */
  127. symbols[i].st_shndx = SHN_ABS;
  128. me->arch.syminfo[i].got_offset = -1UL;
  129. me->arch.syminfo[i].plt_offset = -1UL;
  130. me->arch.syminfo[i].got_initialized = 0;
  131. me->arch.syminfo[i].plt_initialized = 0;
  132. }
  133. /* Search for got/plt relocations. */
  134. me->arch.got_size = me->arch.plt_size = 0;
  135. for (i = 0; i < hdr->e_shnum; i++) {
  136. if (sechdrs[i].sh_type != SHT_RELA)
  137. continue;
  138. nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
  139. rela = (void *) hdr + sechdrs[i].sh_offset;
  140. for (j = 0; j < nrela; j++)
  141. check_rela(rela + j, me);
  142. }
  143. /* Increase core size by size of got & plt and set start
  144. offsets for got and plt. */
  145. me->core_layout.size = ALIGN(me->core_layout.size, 4);
  146. me->arch.got_offset = me->core_layout.size;
  147. me->core_layout.size += me->arch.got_size;
  148. me->arch.plt_offset = me->core_layout.size;
  149. if (me->arch.plt_size) {
  150. if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable)
  151. me->arch.plt_size += PLT_ENTRY_SIZE;
  152. me->core_layout.size += me->arch.plt_size;
  153. }
  154. return 0;
  155. }
  156. static int apply_rela_bits(Elf_Addr loc, Elf_Addr val,
  157. int sign, int bits, int shift)
  158. {
  159. unsigned long umax;
  160. long min, max;
  161. if (val & ((1UL << shift) - 1))
  162. return -ENOEXEC;
  163. if (sign) {
  164. val = (Elf_Addr)(((long) val) >> shift);
  165. min = -(1L << (bits - 1));
  166. max = (1L << (bits - 1)) - 1;
  167. if ((long) val < min || (long) val > max)
  168. return -ENOEXEC;
  169. } else {
  170. val >>= shift;
  171. umax = ((1UL << (bits - 1)) << 1) - 1;
  172. if ((unsigned long) val > umax)
  173. return -ENOEXEC;
  174. }
  175. if (bits == 8)
  176. *(unsigned char *) loc = val;
  177. else if (bits == 12)
  178. *(unsigned short *) loc = (val & 0xfff) |
  179. (*(unsigned short *) loc & 0xf000);
  180. else if (bits == 16)
  181. *(unsigned short *) loc = val;
  182. else if (bits == 20)
  183. *(unsigned int *) loc = (val & 0xfff) << 16 |
  184. (val & 0xff000) >> 4 |
  185. (*(unsigned int *) loc & 0xf00000ff);
  186. else if (bits == 32)
  187. *(unsigned int *) loc = val;
  188. else if (bits == 64)
  189. *(unsigned long *) loc = val;
  190. return 0;
  191. }
  192. static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
  193. const char *strtab, struct module *me)
  194. {
  195. struct mod_arch_syminfo *info;
  196. Elf_Addr loc, val;
  197. int r_type, r_sym;
  198. int rc = -ENOEXEC;
  199. /* This is where to make the change */
  200. loc = base + rela->r_offset;
  201. /* This is the symbol it is referring to. Note that all
  202. undefined symbols have been resolved. */
  203. r_sym = ELF_R_SYM(rela->r_info);
  204. r_type = ELF_R_TYPE(rela->r_info);
  205. info = me->arch.syminfo + r_sym;
  206. val = symtab[r_sym].st_value;
  207. switch (r_type) {
  208. case R_390_NONE: /* No relocation. */
  209. rc = 0;
  210. break;
  211. case R_390_8: /* Direct 8 bit. */
  212. case R_390_12: /* Direct 12 bit. */
  213. case R_390_16: /* Direct 16 bit. */
  214. case R_390_20: /* Direct 20 bit. */
  215. case R_390_32: /* Direct 32 bit. */
  216. case R_390_64: /* Direct 64 bit. */
  217. val += rela->r_addend;
  218. if (r_type == R_390_8)
  219. rc = apply_rela_bits(loc, val, 0, 8, 0);
  220. else if (r_type == R_390_12)
  221. rc = apply_rela_bits(loc, val, 0, 12, 0);
  222. else if (r_type == R_390_16)
  223. rc = apply_rela_bits(loc, val, 0, 16, 0);
  224. else if (r_type == R_390_20)
  225. rc = apply_rela_bits(loc, val, 1, 20, 0);
  226. else if (r_type == R_390_32)
  227. rc = apply_rela_bits(loc, val, 0, 32, 0);
  228. else if (r_type == R_390_64)
  229. rc = apply_rela_bits(loc, val, 0, 64, 0);
  230. break;
  231. case R_390_PC16: /* PC relative 16 bit. */
  232. case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
  233. case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
  234. case R_390_PC32: /* PC relative 32 bit. */
  235. case R_390_PC64: /* PC relative 64 bit. */
  236. val += rela->r_addend - loc;
  237. if (r_type == R_390_PC16)
  238. rc = apply_rela_bits(loc, val, 1, 16, 0);
  239. else if (r_type == R_390_PC16DBL)
  240. rc = apply_rela_bits(loc, val, 1, 16, 1);
  241. else if (r_type == R_390_PC32DBL)
  242. rc = apply_rela_bits(loc, val, 1, 32, 1);
  243. else if (r_type == R_390_PC32)
  244. rc = apply_rela_bits(loc, val, 1, 32, 0);
  245. else if (r_type == R_390_PC64)
  246. rc = apply_rela_bits(loc, val, 1, 64, 0);
  247. break;
  248. case R_390_GOT12: /* 12 bit GOT offset. */
  249. case R_390_GOT16: /* 16 bit GOT offset. */
  250. case R_390_GOT20: /* 20 bit GOT offset. */
  251. case R_390_GOT32: /* 32 bit GOT offset. */
  252. case R_390_GOT64: /* 64 bit GOT offset. */
  253. case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
  254. case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
  255. case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
  256. case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
  257. case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
  258. case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
  259. case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
  260. if (info->got_initialized == 0) {
  261. Elf_Addr *gotent;
  262. gotent = me->core_layout.base + me->arch.got_offset +
  263. info->got_offset;
  264. *gotent = val;
  265. info->got_initialized = 1;
  266. }
  267. val = info->got_offset + rela->r_addend;
  268. if (r_type == R_390_GOT12 ||
  269. r_type == R_390_GOTPLT12)
  270. rc = apply_rela_bits(loc, val, 0, 12, 0);
  271. else if (r_type == R_390_GOT16 ||
  272. r_type == R_390_GOTPLT16)
  273. rc = apply_rela_bits(loc, val, 0, 16, 0);
  274. else if (r_type == R_390_GOT20 ||
  275. r_type == R_390_GOTPLT20)
  276. rc = apply_rela_bits(loc, val, 1, 20, 0);
  277. else if (r_type == R_390_GOT32 ||
  278. r_type == R_390_GOTPLT32)
  279. rc = apply_rela_bits(loc, val, 0, 32, 0);
  280. else if (r_type == R_390_GOT64 ||
  281. r_type == R_390_GOTPLT64)
  282. rc = apply_rela_bits(loc, val, 0, 64, 0);
  283. else if (r_type == R_390_GOTENT ||
  284. r_type == R_390_GOTPLTENT) {
  285. val += (Elf_Addr) me->core_layout.base - loc;
  286. rc = apply_rela_bits(loc, val, 1, 32, 1);
  287. }
  288. break;
  289. case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
  290. case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
  291. case R_390_PLT32: /* 32 bit PC relative PLT address. */
  292. case R_390_PLT64: /* 64 bit PC relative PLT address. */
  293. case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
  294. case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
  295. case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
  296. if (info->plt_initialized == 0) {
  297. unsigned int *ip;
  298. ip = me->core_layout.base + me->arch.plt_offset +
  299. info->plt_offset;
  300. ip[0] = 0x0d10e310; /* basr 1,0 */
  301. ip[1] = 0x100a0004; /* lg 1,10(1) */
  302. if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) {
  303. unsigned int *ij;
  304. ij = me->core_layout.base +
  305. me->arch.plt_offset +
  306. me->arch.plt_size - PLT_ENTRY_SIZE;
  307. ip[2] = 0xa7f40000 + /* j __jump_r1 */
  308. (unsigned int)(u16)
  309. (((unsigned long) ij - 8 -
  310. (unsigned long) ip) / 2);
  311. } else {
  312. ip[2] = 0x07f10000; /* br %r1 */
  313. }
  314. ip[3] = (unsigned int) (val >> 32);
  315. ip[4] = (unsigned int) val;
  316. info->plt_initialized = 1;
  317. }
  318. if (r_type == R_390_PLTOFF16 ||
  319. r_type == R_390_PLTOFF32 ||
  320. r_type == R_390_PLTOFF64)
  321. val = me->arch.plt_offset - me->arch.got_offset +
  322. info->plt_offset + rela->r_addend;
  323. else {
  324. if (!((r_type == R_390_PLT16DBL &&
  325. val - loc + 0xffffUL < 0x1ffffeUL) ||
  326. (r_type == R_390_PLT32DBL &&
  327. val - loc + 0xffffffffULL < 0x1fffffffeULL)))
  328. val = (Elf_Addr) me->core_layout.base +
  329. me->arch.plt_offset +
  330. info->plt_offset;
  331. val += rela->r_addend - loc;
  332. }
  333. if (r_type == R_390_PLT16DBL)
  334. rc = apply_rela_bits(loc, val, 1, 16, 1);
  335. else if (r_type == R_390_PLTOFF16)
  336. rc = apply_rela_bits(loc, val, 0, 16, 0);
  337. else if (r_type == R_390_PLT32DBL)
  338. rc = apply_rela_bits(loc, val, 1, 32, 1);
  339. else if (r_type == R_390_PLT32 ||
  340. r_type == R_390_PLTOFF32)
  341. rc = apply_rela_bits(loc, val, 0, 32, 0);
  342. else if (r_type == R_390_PLT64 ||
  343. r_type == R_390_PLTOFF64)
  344. rc = apply_rela_bits(loc, val, 0, 64, 0);
  345. break;
  346. case R_390_GOTOFF16: /* 16 bit offset to GOT. */
  347. case R_390_GOTOFF32: /* 32 bit offset to GOT. */
  348. case R_390_GOTOFF64: /* 64 bit offset to GOT. */
  349. val = val + rela->r_addend -
  350. ((Elf_Addr) me->core_layout.base + me->arch.got_offset);
  351. if (r_type == R_390_GOTOFF16)
  352. rc = apply_rela_bits(loc, val, 0, 16, 0);
  353. else if (r_type == R_390_GOTOFF32)
  354. rc = apply_rela_bits(loc, val, 0, 32, 0);
  355. else if (r_type == R_390_GOTOFF64)
  356. rc = apply_rela_bits(loc, val, 0, 64, 0);
  357. break;
  358. case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
  359. case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
  360. val = (Elf_Addr) me->core_layout.base + me->arch.got_offset +
  361. rela->r_addend - loc;
  362. if (r_type == R_390_GOTPC)
  363. rc = apply_rela_bits(loc, val, 1, 32, 0);
  364. else if (r_type == R_390_GOTPCDBL)
  365. rc = apply_rela_bits(loc, val, 1, 32, 1);
  366. break;
  367. case R_390_COPY:
  368. case R_390_GLOB_DAT: /* Create GOT entry. */
  369. case R_390_JMP_SLOT: /* Create PLT entry. */
  370. case R_390_RELATIVE: /* Adjust by program base. */
  371. /* Only needed if we want to support loading of
  372. modules linked with -shared. */
  373. return -ENOEXEC;
  374. default:
  375. printk(KERN_ERR "module %s: unknown relocation: %u\n",
  376. me->name, r_type);
  377. return -ENOEXEC;
  378. }
  379. if (rc) {
  380. printk(KERN_ERR "module %s: relocation error for symbol %s "
  381. "(r_type %i, value 0x%lx)\n",
  382. me->name, strtab + symtab[r_sym].st_name,
  383. r_type, (unsigned long) val);
  384. return rc;
  385. }
  386. return 0;
  387. }
  388. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  389. unsigned int symindex, unsigned int relsec,
  390. struct module *me)
  391. {
  392. Elf_Addr base;
  393. Elf_Sym *symtab;
  394. Elf_Rela *rela;
  395. unsigned long i, n;
  396. int rc;
  397. DEBUGP("Applying relocate section %u to %u\n",
  398. relsec, sechdrs[relsec].sh_info);
  399. base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
  400. symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
  401. rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
  402. n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
  403. for (i = 0; i < n; i++, rela++) {
  404. rc = apply_rela(rela, base, symtab, strtab, me);
  405. if (rc)
  406. return rc;
  407. }
  408. return 0;
  409. }
  410. int module_finalize(const Elf_Ehdr *hdr,
  411. const Elf_Shdr *sechdrs,
  412. struct module *me)
  413. {
  414. const Elf_Shdr *s;
  415. char *secstrings, *secname;
  416. void *aseg;
  417. if (IS_ENABLED(CONFIG_EXPOLINE) &&
  418. !nospec_disable && me->arch.plt_size) {
  419. unsigned int *ij;
  420. ij = me->core_layout.base + me->arch.plt_offset +
  421. me->arch.plt_size - PLT_ENTRY_SIZE;
  422. if (test_facility(35)) {
  423. ij[0] = 0xc6000000; /* exrl %r0,.+10 */
  424. ij[1] = 0x0005a7f4; /* j . */
  425. ij[2] = 0x000007f1; /* br %r1 */
  426. } else {
  427. ij[0] = 0x44000000 | (unsigned int)
  428. offsetof(struct lowcore, br_r1_trampoline);
  429. ij[1] = 0xa7f40000; /* j . */
  430. }
  431. }
  432. secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  433. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  434. aseg = (void *) s->sh_addr;
  435. secname = secstrings + s->sh_name;
  436. if (!strcmp(".altinstructions", secname))
  437. /* patch .altinstructions */
  438. apply_alternatives(aseg, aseg + s->sh_size);
  439. if (IS_ENABLED(CONFIG_EXPOLINE) &&
  440. (!strncmp(".s390_indirect", secname, 14)))
  441. nospec_revert(aseg, aseg + s->sh_size);
  442. if (IS_ENABLED(CONFIG_EXPOLINE) &&
  443. (!strncmp(".s390_return", secname, 12)))
  444. nospec_revert(aseg, aseg + s->sh_size);
  445. }
  446. jump_label_apply_nops(me);
  447. return 0;
  448. }