elf.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2014 Imagination Technologies
  4. * Author: Paul Burton <[email protected]>
  5. */
  6. #include <linux/binfmts.h>
  7. #include <linux/elf.h>
  8. #include <linux/export.h>
  9. #include <linux/sched.h>
  10. #include <asm/cpu-features.h>
  11. #include <asm/cpu-info.h>
  12. #ifdef CONFIG_MIPS_FP_SUPPORT
  13. /* Whether to accept legacy-NaN and 2008-NaN user binaries. */
  14. bool mips_use_nan_legacy;
  15. bool mips_use_nan_2008;
  16. /* FPU modes */
  17. enum {
  18. FP_FRE,
  19. FP_FR0,
  20. FP_FR1,
  21. };
  22. /**
  23. * struct mode_req - ABI FPU mode requirements
  24. * @single: The program being loaded needs an FPU but it will only issue
  25. * single precision instructions meaning that it can execute in
  26. * either FR0 or FR1.
  27. * @soft: The soft(-float) requirement means that the program being
  28. * loaded needs has no FPU dependency at all (i.e. it has no
  29. * FPU instructions).
  30. * @fr1: The program being loaded depends on FPU being in FR=1 mode.
  31. * @frdefault: The program being loaded depends on the default FPU mode.
  32. * That is FR0 for O32 and FR1 for N32/N64.
  33. * @fre: The program being loaded depends on FPU with FRE=1. This mode is
  34. * a bridge which uses FR=1 whilst still being able to maintain
  35. * full compatibility with pre-existing code using the O32 FP32
  36. * ABI.
  37. *
  38. * More information about the FP ABIs can be found here:
  39. *
  40. * https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking#10.4.1._Basic_mode_set-up
  41. *
  42. */
  43. struct mode_req {
  44. bool single;
  45. bool soft;
  46. bool fr1;
  47. bool frdefault;
  48. bool fre;
  49. };
  50. static const struct mode_req fpu_reqs[] = {
  51. [MIPS_ABI_FP_ANY] = { true, true, true, true, true },
  52. [MIPS_ABI_FP_DOUBLE] = { false, false, false, true, true },
  53. [MIPS_ABI_FP_SINGLE] = { true, false, false, false, false },
  54. [MIPS_ABI_FP_SOFT] = { false, true, false, false, false },
  55. [MIPS_ABI_FP_OLD_64] = { false, false, false, false, false },
  56. [MIPS_ABI_FP_XX] = { false, false, true, true, true },
  57. [MIPS_ABI_FP_64] = { false, false, true, false, false },
  58. [MIPS_ABI_FP_64A] = { false, false, true, false, true }
  59. };
  60. /*
  61. * Mode requirements when .MIPS.abiflags is not present in the ELF.
  62. * Not present means that everything is acceptable except FR1.
  63. */
  64. static struct mode_req none_req = { true, true, false, true, true };
  65. int arch_elf_pt_proc(void *_ehdr, void *_phdr, struct file *elf,
  66. bool is_interp, struct arch_elf_state *state)
  67. {
  68. union {
  69. struct elf32_hdr e32;
  70. struct elf64_hdr e64;
  71. } *ehdr = _ehdr;
  72. struct elf32_phdr *phdr32 = _phdr;
  73. struct elf64_phdr *phdr64 = _phdr;
  74. struct mips_elf_abiflags_v0 abiflags;
  75. bool elf32;
  76. u32 flags;
  77. int ret;
  78. loff_t pos;
  79. elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
  80. flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
  81. /* Let's see if this is an O32 ELF */
  82. if (elf32) {
  83. if (flags & EF_MIPS_FP64) {
  84. /*
  85. * Set MIPS_ABI_FP_OLD_64 for EF_MIPS_FP64. We will override it
  86. * later if needed
  87. */
  88. if (is_interp)
  89. state->interp_fp_abi = MIPS_ABI_FP_OLD_64;
  90. else
  91. state->fp_abi = MIPS_ABI_FP_OLD_64;
  92. }
  93. if (phdr32->p_type != PT_MIPS_ABIFLAGS)
  94. return 0;
  95. if (phdr32->p_filesz < sizeof(abiflags))
  96. return -EINVAL;
  97. pos = phdr32->p_offset;
  98. } else {
  99. if (phdr64->p_type != PT_MIPS_ABIFLAGS)
  100. return 0;
  101. if (phdr64->p_filesz < sizeof(abiflags))
  102. return -EINVAL;
  103. pos = phdr64->p_offset;
  104. }
  105. ret = kernel_read(elf, &abiflags, sizeof(abiflags), &pos);
  106. if (ret < 0)
  107. return ret;
  108. if (ret != sizeof(abiflags))
  109. return -EIO;
  110. /* Record the required FP ABIs for use by mips_check_elf */
  111. if (is_interp)
  112. state->interp_fp_abi = abiflags.fp_abi;
  113. else
  114. state->fp_abi = abiflags.fp_abi;
  115. return 0;
  116. }
  117. int arch_check_elf(void *_ehdr, bool has_interpreter, void *_interp_ehdr,
  118. struct arch_elf_state *state)
  119. {
  120. union {
  121. struct elf32_hdr e32;
  122. struct elf64_hdr e64;
  123. } *ehdr = _ehdr;
  124. union {
  125. struct elf32_hdr e32;
  126. struct elf64_hdr e64;
  127. } *iehdr = _interp_ehdr;
  128. struct mode_req prog_req, interp_req;
  129. int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
  130. bool elf32;
  131. u32 flags;
  132. elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
  133. flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
  134. /*
  135. * Determine the NaN personality, reject the binary if not allowed.
  136. * Also ensure that any interpreter matches the executable.
  137. */
  138. if (flags & EF_MIPS_NAN2008) {
  139. if (mips_use_nan_2008)
  140. state->nan_2008 = 1;
  141. else
  142. return -ENOEXEC;
  143. } else {
  144. if (mips_use_nan_legacy)
  145. state->nan_2008 = 0;
  146. else
  147. return -ENOEXEC;
  148. }
  149. if (has_interpreter) {
  150. bool ielf32;
  151. u32 iflags;
  152. ielf32 = iehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
  153. iflags = ielf32 ? iehdr->e32.e_flags : iehdr->e64.e_flags;
  154. if ((flags ^ iflags) & EF_MIPS_NAN2008)
  155. return -ELIBBAD;
  156. }
  157. if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
  158. return 0;
  159. fp_abi = state->fp_abi;
  160. if (has_interpreter) {
  161. interp_fp_abi = state->interp_fp_abi;
  162. abi0 = min(fp_abi, interp_fp_abi);
  163. abi1 = max(fp_abi, interp_fp_abi);
  164. } else {
  165. abi0 = abi1 = fp_abi;
  166. }
  167. if (elf32 && !(flags & EF_MIPS_ABI2)) {
  168. /* Default to a mode capable of running code expecting FR=0 */
  169. state->overall_fp_mode = cpu_has_mips_r6 ? FP_FRE : FP_FR0;
  170. /* Allow all ABIs we know about */
  171. max_abi = MIPS_ABI_FP_64A;
  172. } else {
  173. /* MIPS64 code always uses FR=1, thus the default is easy */
  174. state->overall_fp_mode = FP_FR1;
  175. /* Disallow access to the various FPXX & FP64 ABIs */
  176. max_abi = MIPS_ABI_FP_SOFT;
  177. }
  178. if ((abi0 > max_abi && abi0 != MIPS_ABI_FP_UNKNOWN) ||
  179. (abi1 > max_abi && abi1 != MIPS_ABI_FP_UNKNOWN))
  180. return -ELIBBAD;
  181. /* It's time to determine the FPU mode requirements */
  182. prog_req = (abi0 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
  183. interp_req = (abi1 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
  184. /*
  185. * Check whether the program's and interp's ABIs have a matching FPU
  186. * mode requirement.
  187. */
  188. prog_req.single = interp_req.single && prog_req.single;
  189. prog_req.soft = interp_req.soft && prog_req.soft;
  190. prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
  191. prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
  192. prog_req.fre = interp_req.fre && prog_req.fre;
  193. /*
  194. * Determine the desired FPU mode
  195. *
  196. * Decision making:
  197. *
  198. * - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
  199. * means that we have a combination of program and interpreter
  200. * that inherently require the hybrid FP mode.
  201. * - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
  202. * fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
  203. * instructions so we don't care about the mode. We will simply use
  204. * the one preferred by the hardware. In fpxx case, that ABI can
  205. * handle both FR=1 and FR=0, so, again, we simply choose the one
  206. * preferred by the hardware. Next, if we only use single-precision
  207. * FPU instructions, and the default ABI FPU mode is not good
  208. * (ie single + any ABI combination), we set again the FPU mode to the
  209. * one is preferred by the hardware. Next, if we know that the code
  210. * will only use single-precision instructions, shown by single being
  211. * true but frdefault being false, then we again set the FPU mode to
  212. * the one that is preferred by the hardware.
  213. * - We want FP_FR1 if that's the only matching mode and the default one
  214. * is not good.
  215. * - Return with -ELIBADD if we can't find a matching FPU mode.
  216. */
  217. if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
  218. state->overall_fp_mode = FP_FRE;
  219. else if ((prog_req.fr1 && prog_req.frdefault) ||
  220. (prog_req.single && !prog_req.frdefault))
  221. /* Make sure 64-bit MIPS III/IV/64R1 will not pick FR1 */
  222. state->overall_fp_mode = ((raw_current_cpu_data.fpu_id & MIPS_FPIR_F64) &&
  223. cpu_has_mips_r2_r6) ?
  224. FP_FR1 : FP_FR0;
  225. else if (prog_req.fr1)
  226. state->overall_fp_mode = FP_FR1;
  227. else if (!prog_req.fre && !prog_req.frdefault &&
  228. !prog_req.fr1 && !prog_req.single && !prog_req.soft)
  229. return -ELIBBAD;
  230. return 0;
  231. }
  232. static inline void set_thread_fp_mode(int hybrid, int regs32)
  233. {
  234. if (hybrid)
  235. set_thread_flag(TIF_HYBRID_FPREGS);
  236. else
  237. clear_thread_flag(TIF_HYBRID_FPREGS);
  238. if (regs32)
  239. set_thread_flag(TIF_32BIT_FPREGS);
  240. else
  241. clear_thread_flag(TIF_32BIT_FPREGS);
  242. }
  243. void mips_set_personality_fp(struct arch_elf_state *state)
  244. {
  245. /*
  246. * This function is only ever called for O32 ELFs so we should
  247. * not be worried about N32/N64 binaries.
  248. */
  249. if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
  250. return;
  251. switch (state->overall_fp_mode) {
  252. case FP_FRE:
  253. set_thread_fp_mode(1, 0);
  254. break;
  255. case FP_FR0:
  256. set_thread_fp_mode(0, 1);
  257. break;
  258. case FP_FR1:
  259. set_thread_fp_mode(0, 0);
  260. break;
  261. default:
  262. BUG();
  263. }
  264. }
  265. /*
  266. * Select the IEEE 754 NaN encoding and ABS.fmt/NEG.fmt execution mode
  267. * in FCSR according to the ELF NaN personality.
  268. */
  269. void mips_set_personality_nan(struct arch_elf_state *state)
  270. {
  271. struct cpuinfo_mips *c = &boot_cpu_data;
  272. struct task_struct *t = current;
  273. t->thread.fpu.fcr31 = c->fpu_csr31;
  274. switch (state->nan_2008) {
  275. case 0:
  276. break;
  277. case 1:
  278. if (!(c->fpu_msk31 & FPU_CSR_NAN2008))
  279. t->thread.fpu.fcr31 |= FPU_CSR_NAN2008;
  280. if (!(c->fpu_msk31 & FPU_CSR_ABS2008))
  281. t->thread.fpu.fcr31 |= FPU_CSR_ABS2008;
  282. break;
  283. default:
  284. BUG();
  285. }
  286. }
  287. #endif /* CONFIG_MIPS_FP_SUPPORT */
  288. int mips_elf_read_implies_exec(void *elf_ex, int exstack)
  289. {
  290. /*
  291. * Set READ_IMPLIES_EXEC only on non-NX systems that
  292. * do not request a specific state via PT_GNU_STACK.
  293. */
  294. return (!cpu_has_rixi && exstack == EXSTACK_DEFAULT);
  295. }
  296. EXPORT_SYMBOL(mips_elf_read_implies_exec);