elf.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. /* FPU modes */
  13. enum {
  14. FP_FRE,
  15. FP_FR0,
  16. FP_FR1,
  17. };
  18. /**
  19. * struct mode_req - ABI FPU mode requirements
  20. * @single: The program being loaded needs an FPU but it will only issue
  21. * single precision instructions meaning that it can execute in
  22. * either FR0 or FR1.
  23. * @soft: The soft(-float) requirement means that the program being
  24. * loaded needs has no FPU dependency at all (i.e. it has no
  25. * FPU instructions).
  26. * @fr1: The program being loaded depends on FPU being in FR=1 mode.
  27. * @frdefault: The program being loaded depends on the default FPU mode.
  28. * That is FR0 for O32 and FR1 for N32/N64.
  29. * @fre: The program being loaded depends on FPU with FRE=1. This mode is
  30. * a bridge which uses FR=1 whilst still being able to maintain
  31. * full compatibility with pre-existing code using the O32 FP32
  32. * ABI.
  33. *
  34. * More information about the FP ABIs can be found here:
  35. *
  36. * https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking#10.4.1._Basic_mode_set-up
  37. *
  38. */
  39. struct mode_req {
  40. bool single;
  41. bool soft;
  42. bool fr1;
  43. bool frdefault;
  44. bool fre;
  45. };
  46. static const struct mode_req fpu_reqs[] = {
  47. [MIPS_ABI_FP_ANY] = { true, true, true, true, true },
  48. [MIPS_ABI_FP_DOUBLE] = { false, false, false, true, true },
  49. [MIPS_ABI_FP_SINGLE] = { true, false, false, false, false },
  50. [MIPS_ABI_FP_SOFT] = { false, true, false, false, false },
  51. [MIPS_ABI_FP_OLD_64] = { false, false, false, false, false },
  52. [MIPS_ABI_FP_XX] = { false, false, true, true, true },
  53. [MIPS_ABI_FP_64] = { false, false, true, false, false },
  54. [MIPS_ABI_FP_64A] = { false, false, true, false, true }
  55. };
  56. /*
  57. * Mode requirements when .MIPS.abiflags is not present in the ELF.
  58. * Not present means that everything is acceptable except FR1.
  59. */
  60. static struct mode_req none_req = { true, true, false, true, true };
  61. int arch_elf_pt_proc(void *_ehdr, void *_phdr, struct file *elf,
  62. bool is_interp, struct arch_elf_state *state)
  63. {
  64. struct elf32_hdr *ehdr32 = _ehdr;
  65. struct elf32_phdr *phdr32 = _phdr;
  66. struct elf64_phdr *phdr64 = _phdr;
  67. struct mips_elf_abiflags_v0 abiflags;
  68. int ret;
  69. /* Lets see if this is an O32 ELF */
  70. if (ehdr32->e_ident[EI_CLASS] == ELFCLASS32) {
  71. /* FR = 1 for N32 */
  72. if (ehdr32->e_flags & EF_MIPS_ABI2)
  73. state->overall_fp_mode = FP_FR1;
  74. else
  75. /* Set a good default FPU mode for O32 */
  76. state->overall_fp_mode = cpu_has_mips_r6 ?
  77. FP_FRE : FP_FR0;
  78. if (ehdr32->e_flags & EF_MIPS_FP64) {
  79. /*
  80. * Set MIPS_ABI_FP_OLD_64 for EF_MIPS_FP64. We will override it
  81. * later if needed
  82. */
  83. if (is_interp)
  84. state->interp_fp_abi = MIPS_ABI_FP_OLD_64;
  85. else
  86. state->fp_abi = MIPS_ABI_FP_OLD_64;
  87. }
  88. if (phdr32->p_type != PT_MIPS_ABIFLAGS)
  89. return 0;
  90. if (phdr32->p_filesz < sizeof(abiflags))
  91. return -EINVAL;
  92. ret = kernel_read(elf, phdr32->p_offset,
  93. (char *)&abiflags,
  94. sizeof(abiflags));
  95. } else {
  96. /* FR=1 is really the only option for 64-bit */
  97. state->overall_fp_mode = FP_FR1;
  98. if (phdr64->p_type != PT_MIPS_ABIFLAGS)
  99. return 0;
  100. if (phdr64->p_filesz < sizeof(abiflags))
  101. return -EINVAL;
  102. ret = kernel_read(elf, phdr64->p_offset,
  103. (char *)&abiflags,
  104. sizeof(abiflags));
  105. }
  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,
  118. struct arch_elf_state *state)
  119. {
  120. struct elf32_hdr *ehdr = _ehdr;
  121. struct mode_req prog_req, interp_req;
  122. int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
  123. if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
  124. return 0;
  125. fp_abi = state->fp_abi;
  126. if (has_interpreter) {
  127. interp_fp_abi = state->interp_fp_abi;
  128. abi0 = min(fp_abi, interp_fp_abi);
  129. abi1 = max(fp_abi, interp_fp_abi);
  130. } else {
  131. abi0 = abi1 = fp_abi;
  132. }
  133. /* ABI limits. O32 = FP_64A, N32/N64 = FP_SOFT */
  134. max_abi = ((ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
  135. (!(ehdr->e_flags & EF_MIPS_ABI2))) ?
  136. MIPS_ABI_FP_64A : MIPS_ABI_FP_SOFT;
  137. if ((abi0 > max_abi && abi0 != MIPS_ABI_FP_UNKNOWN) ||
  138. (abi1 > max_abi && abi1 != MIPS_ABI_FP_UNKNOWN))
  139. return -ELIBBAD;
  140. /* It's time to determine the FPU mode requirements */
  141. prog_req = (abi0 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
  142. interp_req = (abi1 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
  143. /*
  144. * Check whether the program's and interp's ABIs have a matching FPU
  145. * mode requirement.
  146. */
  147. prog_req.single = interp_req.single && prog_req.single;
  148. prog_req.soft = interp_req.soft && prog_req.soft;
  149. prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
  150. prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
  151. prog_req.fre = interp_req.fre && prog_req.fre;
  152. /*
  153. * Determine the desired FPU mode
  154. *
  155. * Decision making:
  156. *
  157. * - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
  158. * means that we have a combination of program and interpreter
  159. * that inherently require the hybrid FP mode.
  160. * - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
  161. * fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
  162. * instructions so we don't care about the mode. We will simply use
  163. * the one preferred by the hardware. In fpxx case, that ABI can
  164. * handle both FR=1 and FR=0, so, again, we simply choose the one
  165. * preferred by the hardware. Next, if we only use single-precision
  166. * FPU instructions, and the default ABI FPU mode is not good
  167. * (ie single + any ABI combination), we set again the FPU mode to the
  168. * one is preferred by the hardware. Next, if we know that the code
  169. * will only use single-precision instructions, shown by single being
  170. * true but frdefault being false, then we again set the FPU mode to
  171. * the one that is preferred by the hardware.
  172. * - We want FP_FR1 if that's the only matching mode and the default one
  173. * is not good.
  174. * - Return with -ELIBADD if we can't find a matching FPU mode.
  175. */
  176. if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
  177. state->overall_fp_mode = FP_FRE;
  178. else if ((prog_req.fr1 && prog_req.frdefault) ||
  179. (prog_req.single && !prog_req.frdefault))
  180. /* Make sure 64-bit MIPS III/IV/64R1 will not pick FR1 */
  181. state->overall_fp_mode = ((current_cpu_data.fpu_id & MIPS_FPIR_F64) &&
  182. cpu_has_mips_r2_r6) ?
  183. FP_FR1 : FP_FR0;
  184. else if (prog_req.fr1)
  185. state->overall_fp_mode = FP_FR1;
  186. else if (!prog_req.fre && !prog_req.frdefault &&
  187. !prog_req.fr1 && !prog_req.single && !prog_req.soft)
  188. return -ELIBBAD;
  189. return 0;
  190. }
  191. static inline void set_thread_fp_mode(int hybrid, int regs32)
  192. {
  193. if (hybrid)
  194. set_thread_flag(TIF_HYBRID_FPREGS);
  195. else
  196. clear_thread_flag(TIF_HYBRID_FPREGS);
  197. if (regs32)
  198. set_thread_flag(TIF_32BIT_FPREGS);
  199. else
  200. clear_thread_flag(TIF_32BIT_FPREGS);
  201. }
  202. void mips_set_personality_fp(struct arch_elf_state *state)
  203. {
  204. /*
  205. * This function is only ever called for O32 ELFs so we should
  206. * not be worried about N32/N64 binaries.
  207. */
  208. if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
  209. return;
  210. switch (state->overall_fp_mode) {
  211. case FP_FRE:
  212. set_thread_fp_mode(1, 0);
  213. break;
  214. case FP_FR0:
  215. set_thread_fp_mode(0, 1);
  216. break;
  217. case FP_FR1:
  218. set_thread_fp_mode(0, 0);
  219. break;
  220. default:
  221. BUG();
  222. }
  223. }