elf.c 9.2 KB

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