elf.c 9.6 KB

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