elf.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (C) 2014 Imagination Technologies
  3. * Author: Paul Burton <paul.burton@mips.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. loff_t pos;
  82. elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
  83. flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
  84. /* Let's see if this is an O32 ELF */
  85. if (elf32) {
  86. if (flags & EF_MIPS_FP64) {
  87. /*
  88. * Set MIPS_ABI_FP_OLD_64 for EF_MIPS_FP64. We will override it
  89. * later if needed
  90. */
  91. if (is_interp)
  92. state->interp_fp_abi = MIPS_ABI_FP_OLD_64;
  93. else
  94. state->fp_abi = MIPS_ABI_FP_OLD_64;
  95. }
  96. if (phdr32->p_type != PT_MIPS_ABIFLAGS)
  97. return 0;
  98. if (phdr32->p_filesz < sizeof(abiflags))
  99. return -EINVAL;
  100. pos = phdr32->p_offset;
  101. } else {
  102. if (phdr64->p_type != PT_MIPS_ABIFLAGS)
  103. return 0;
  104. if (phdr64->p_filesz < sizeof(abiflags))
  105. return -EINVAL;
  106. pos = phdr64->p_offset;
  107. }
  108. ret = kernel_read(elf, &abiflags, sizeof(abiflags), &pos);
  109. if (ret < 0)
  110. return ret;
  111. if (ret != sizeof(abiflags))
  112. return -EIO;
  113. /* Record the required FP ABIs for use by mips_check_elf */
  114. if (is_interp)
  115. state->interp_fp_abi = abiflags.fp_abi;
  116. else
  117. state->fp_abi = abiflags.fp_abi;
  118. return 0;
  119. }
  120. int arch_check_elf(void *_ehdr, bool has_interpreter, void *_interp_ehdr,
  121. struct arch_elf_state *state)
  122. {
  123. union {
  124. struct elf32_hdr e32;
  125. struct elf64_hdr e64;
  126. } *ehdr = _ehdr;
  127. union {
  128. struct elf32_hdr e32;
  129. struct elf64_hdr e64;
  130. } *iehdr = _interp_ehdr;
  131. struct mode_req prog_req, interp_req;
  132. int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
  133. bool elf32;
  134. u32 flags;
  135. elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
  136. flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
  137. /*
  138. * Determine the NaN personality, reject the binary if not allowed.
  139. * Also ensure that any interpreter matches the executable.
  140. */
  141. if (flags & EF_MIPS_NAN2008) {
  142. if (mips_use_nan_2008)
  143. state->nan_2008 = 1;
  144. else
  145. return -ENOEXEC;
  146. } else {
  147. if (mips_use_nan_legacy)
  148. state->nan_2008 = 0;
  149. else
  150. return -ENOEXEC;
  151. }
  152. if (has_interpreter) {
  153. bool ielf32;
  154. u32 iflags;
  155. ielf32 = iehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
  156. iflags = ielf32 ? iehdr->e32.e_flags : iehdr->e64.e_flags;
  157. if ((flags ^ iflags) & EF_MIPS_NAN2008)
  158. return -ELIBBAD;
  159. }
  160. if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
  161. return 0;
  162. fp_abi = state->fp_abi;
  163. if (has_interpreter) {
  164. interp_fp_abi = state->interp_fp_abi;
  165. abi0 = min(fp_abi, interp_fp_abi);
  166. abi1 = max(fp_abi, interp_fp_abi);
  167. } else {
  168. abi0 = abi1 = fp_abi;
  169. }
  170. if (elf32 && !(flags & EF_MIPS_ABI2)) {
  171. /* Default to a mode capable of running code expecting FR=0 */
  172. state->overall_fp_mode = cpu_has_mips_r6 ? FP_FRE : FP_FR0;
  173. /* Allow all ABIs we know about */
  174. max_abi = MIPS_ABI_FP_64A;
  175. } else {
  176. /* MIPS64 code always uses FR=1, thus the default is easy */
  177. state->overall_fp_mode = FP_FR1;
  178. /* Disallow access to the various FPXX & FP64 ABIs */
  179. max_abi = MIPS_ABI_FP_SOFT;
  180. }
  181. if ((abi0 > max_abi && abi0 != MIPS_ABI_FP_UNKNOWN) ||
  182. (abi1 > max_abi && abi1 != MIPS_ABI_FP_UNKNOWN))
  183. return -ELIBBAD;
  184. /* It's time to determine the FPU mode requirements */
  185. prog_req = (abi0 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
  186. interp_req = (abi1 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
  187. /*
  188. * Check whether the program's and interp's ABIs have a matching FPU
  189. * mode requirement.
  190. */
  191. prog_req.single = interp_req.single && prog_req.single;
  192. prog_req.soft = interp_req.soft && prog_req.soft;
  193. prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
  194. prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
  195. prog_req.fre = interp_req.fre && prog_req.fre;
  196. /*
  197. * Determine the desired FPU mode
  198. *
  199. * Decision making:
  200. *
  201. * - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
  202. * means that we have a combination of program and interpreter
  203. * that inherently require the hybrid FP mode.
  204. * - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
  205. * fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
  206. * instructions so we don't care about the mode. We will simply use
  207. * the one preferred by the hardware. In fpxx case, that ABI can
  208. * handle both FR=1 and FR=0, so, again, we simply choose the one
  209. * preferred by the hardware. Next, if we only use single-precision
  210. * FPU instructions, and the default ABI FPU mode is not good
  211. * (ie single + any ABI combination), we set again the FPU mode to the
  212. * one is preferred by the hardware. Next, if we know that the code
  213. * will only use single-precision instructions, shown by single being
  214. * true but frdefault being false, then we again set the FPU mode to
  215. * the one that is preferred by the hardware.
  216. * - We want FP_FR1 if that's the only matching mode and the default one
  217. * is not good.
  218. * - Return with -ELIBADD if we can't find a matching FPU mode.
  219. */
  220. if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
  221. state->overall_fp_mode = FP_FRE;
  222. else if ((prog_req.fr1 && prog_req.frdefault) ||
  223. (prog_req.single && !prog_req.frdefault))
  224. /* Make sure 64-bit MIPS III/IV/64R1 will not pick FR1 */
  225. state->overall_fp_mode = ((raw_current_cpu_data.fpu_id & MIPS_FPIR_F64) &&
  226. cpu_has_mips_r2_r6) ?
  227. FP_FR1 : FP_FR0;
  228. else if (prog_req.fr1)
  229. state->overall_fp_mode = FP_FR1;
  230. else if (!prog_req.fre && !prog_req.frdefault &&
  231. !prog_req.fr1 && !prog_req.single && !prog_req.soft)
  232. return -ELIBBAD;
  233. return 0;
  234. }
  235. static inline void set_thread_fp_mode(int hybrid, int regs32)
  236. {
  237. if (hybrid)
  238. set_thread_flag(TIF_HYBRID_FPREGS);
  239. else
  240. clear_thread_flag(TIF_HYBRID_FPREGS);
  241. if (regs32)
  242. set_thread_flag(TIF_32BIT_FPREGS);
  243. else
  244. clear_thread_flag(TIF_32BIT_FPREGS);
  245. }
  246. void mips_set_personality_fp(struct arch_elf_state *state)
  247. {
  248. /*
  249. * This function is only ever called for O32 ELFs so we should
  250. * not be worried about N32/N64 binaries.
  251. */
  252. if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
  253. return;
  254. switch (state->overall_fp_mode) {
  255. case FP_FRE:
  256. set_thread_fp_mode(1, 0);
  257. break;
  258. case FP_FR0:
  259. set_thread_fp_mode(0, 1);
  260. break;
  261. case FP_FR1:
  262. set_thread_fp_mode(0, 0);
  263. break;
  264. default:
  265. BUG();
  266. }
  267. }
  268. /*
  269. * Select the IEEE 754 NaN encoding and ABS.fmt/NEG.fmt execution mode
  270. * in FCSR according to the ELF NaN personality.
  271. */
  272. void mips_set_personality_nan(struct arch_elf_state *state)
  273. {
  274. struct cpuinfo_mips *c = &boot_cpu_data;
  275. struct task_struct *t = current;
  276. t->thread.fpu.fcr31 = c->fpu_csr31;
  277. switch (state->nan_2008) {
  278. case 0:
  279. break;
  280. case 1:
  281. if (!(c->fpu_msk31 & FPU_CSR_NAN2008))
  282. t->thread.fpu.fcr31 |= FPU_CSR_NAN2008;
  283. if (!(c->fpu_msk31 & FPU_CSR_ABS2008))
  284. t->thread.fpu.fcr31 |= FPU_CSR_ABS2008;
  285. break;
  286. default:
  287. BUG();
  288. }
  289. }
  290. int mips_elf_read_implies_exec(void *elf_ex, int exstack)
  291. {
  292. if (exstack != EXSTACK_DISABLE_X) {
  293. /* The binary doesn't request a non-executable stack */
  294. return 1;
  295. }
  296. if (!cpu_has_rixi) {
  297. /* The CPU doesn't support non-executable memory */
  298. return 1;
  299. }
  300. return 0;
  301. }
  302. EXPORT_SYMBOL(mips_elf_read_implies_exec);