elf.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. static inline unsigned get_fp_abi(int in_abi)
  118. {
  119. /* If the ABI requirement is provided, simply return that */
  120. if (in_abi != MIPS_ABI_FP_UNKNOWN)
  121. return in_abi;
  122. /* Unknown ABI */
  123. return MIPS_ABI_FP_UNKNOWN;
  124. }
  125. int arch_check_elf(void *_ehdr, bool has_interpreter,
  126. struct arch_elf_state *state)
  127. {
  128. struct elf32_hdr *ehdr = _ehdr;
  129. struct mode_req prog_req, interp_req;
  130. int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
  131. if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
  132. return 0;
  133. fp_abi = get_fp_abi(state->fp_abi);
  134. if (has_interpreter) {
  135. interp_fp_abi = get_fp_abi(state->interp_fp_abi);
  136. abi0 = min(fp_abi, interp_fp_abi);
  137. abi1 = max(fp_abi, interp_fp_abi);
  138. } else {
  139. abi0 = abi1 = fp_abi;
  140. }
  141. /* ABI limits. O32 = FP_64A, N32/N64 = FP_SOFT */
  142. max_abi = ((ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
  143. (!(ehdr->e_flags & EF_MIPS_ABI2))) ?
  144. MIPS_ABI_FP_64A : MIPS_ABI_FP_SOFT;
  145. if ((abi0 > max_abi && abi0 != MIPS_ABI_FP_UNKNOWN) ||
  146. (abi1 > max_abi && abi1 != MIPS_ABI_FP_UNKNOWN))
  147. return -ELIBBAD;
  148. /* It's time to determine the FPU mode requirements */
  149. prog_req = (abi0 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
  150. interp_req = (abi1 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
  151. /*
  152. * Check whether the program's and interp's ABIs have a matching FPU
  153. * mode requirement.
  154. */
  155. prog_req.single = interp_req.single && prog_req.single;
  156. prog_req.soft = interp_req.soft && prog_req.soft;
  157. prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
  158. prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
  159. prog_req.fre = interp_req.fre && prog_req.fre;
  160. /*
  161. * Determine the desired FPU mode
  162. *
  163. * Decision making:
  164. *
  165. * - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
  166. * means that we have a combination of program and interpreter
  167. * that inherently require the hybrid FP mode.
  168. * - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
  169. * fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
  170. * instructions so we don't care about the mode. We will simply use
  171. * the one preferred by the hardware. In fpxx case, that ABI can
  172. * handle both FR=1 and FR=0, so, again, we simply choose the one
  173. * preferred by the hardware. Next, if we only use single-precision
  174. * FPU instructions, and the default ABI FPU mode is not good
  175. * (ie single + any ABI combination), we set again the FPU mode to the
  176. * one is preferred by the hardware. Next, if we know that the code
  177. * will only use single-precision instructions, shown by single being
  178. * true but frdefault being false, then we again set the FPU mode to
  179. * the one that is preferred by the hardware.
  180. * - We want FP_FR1 if that's the only matching mode and the default one
  181. * is not good.
  182. * - Return with -ELIBADD if we can't find a matching FPU mode.
  183. */
  184. if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
  185. state->overall_fp_mode = FP_FRE;
  186. else if ((prog_req.fr1 && prog_req.frdefault) ||
  187. (prog_req.single && !prog_req.frdefault))
  188. /* Make sure 64-bit MIPS III/IV/64R1 will not pick FR1 */
  189. state->overall_fp_mode = ((current_cpu_data.fpu_id & MIPS_FPIR_F64) &&
  190. cpu_has_mips_r2_r6) ?
  191. FP_FR1 : FP_FR0;
  192. else if (prog_req.fr1)
  193. state->overall_fp_mode = FP_FR1;
  194. else if (!prog_req.fre && !prog_req.frdefault &&
  195. !prog_req.fr1 && !prog_req.single && !prog_req.soft)
  196. return -ELIBBAD;
  197. return 0;
  198. }
  199. static inline void set_thread_fp_mode(int hybrid, int regs32)
  200. {
  201. if (hybrid)
  202. set_thread_flag(TIF_HYBRID_FPREGS);
  203. else
  204. clear_thread_flag(TIF_HYBRID_FPREGS);
  205. if (regs32)
  206. set_thread_flag(TIF_32BIT_FPREGS);
  207. else
  208. clear_thread_flag(TIF_32BIT_FPREGS);
  209. }
  210. void mips_set_personality_fp(struct arch_elf_state *state)
  211. {
  212. /*
  213. * This function is only ever called for O32 ELFs so we should
  214. * not be worried about N32/N64 binaries.
  215. */
  216. if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
  217. return;
  218. switch (state->overall_fp_mode) {
  219. case FP_FRE:
  220. set_thread_fp_mode(1, 0);
  221. break;
  222. case FP_FR0:
  223. set_thread_fp_mode(0, 1);
  224. break;
  225. case FP_FR1:
  226. set_thread_fp_mode(0, 0);
  227. break;
  228. default:
  229. BUG();
  230. }
  231. }