elf.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if (ehdr32->e_flags & EF_MIPS_FP64) {
  72. /*
  73. * Set MIPS_ABI_FP_OLD_64 for EF_MIPS_FP64. We will override it
  74. * later if needed
  75. */
  76. if (is_interp)
  77. state->interp_fp_abi = MIPS_ABI_FP_OLD_64;
  78. else
  79. state->fp_abi = MIPS_ABI_FP_OLD_64;
  80. }
  81. if (phdr32->p_type != PT_MIPS_ABIFLAGS)
  82. return 0;
  83. if (phdr32->p_filesz < sizeof(abiflags))
  84. return -EINVAL;
  85. ret = kernel_read(elf, phdr32->p_offset,
  86. (char *)&abiflags,
  87. sizeof(abiflags));
  88. } else {
  89. if (phdr64->p_type != PT_MIPS_ABIFLAGS)
  90. return 0;
  91. if (phdr64->p_filesz < sizeof(abiflags))
  92. return -EINVAL;
  93. ret = kernel_read(elf, phdr64->p_offset,
  94. (char *)&abiflags,
  95. sizeof(abiflags));
  96. }
  97. if (ret < 0)
  98. return ret;
  99. if (ret != sizeof(abiflags))
  100. return -EIO;
  101. /* Record the required FP ABIs for use by mips_check_elf */
  102. if (is_interp)
  103. state->interp_fp_abi = abiflags.fp_abi;
  104. else
  105. state->fp_abi = abiflags.fp_abi;
  106. return 0;
  107. }
  108. int arch_check_elf(void *_ehdr, bool has_interpreter,
  109. struct arch_elf_state *state)
  110. {
  111. struct elf32_hdr *ehdr = _ehdr;
  112. struct mode_req prog_req, interp_req;
  113. int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
  114. bool is_mips64;
  115. if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
  116. return 0;
  117. fp_abi = state->fp_abi;
  118. if (has_interpreter) {
  119. interp_fp_abi = state->interp_fp_abi;
  120. abi0 = min(fp_abi, interp_fp_abi);
  121. abi1 = max(fp_abi, interp_fp_abi);
  122. } else {
  123. abi0 = abi1 = fp_abi;
  124. }
  125. is_mips64 = (ehdr->e_ident[EI_CLASS] == ELFCLASS64) ||
  126. (ehdr->e_flags & EF_MIPS_ABI2);
  127. if (is_mips64) {
  128. /* MIPS64 code always uses FR=1, thus the default is easy */
  129. state->overall_fp_mode = FP_FR1;
  130. /* Disallow access to the various FPXX & FP64 ABIs */
  131. max_abi = MIPS_ABI_FP_SOFT;
  132. } else {
  133. /* Default to a mode capable of running code expecting FR=0 */
  134. state->overall_fp_mode = cpu_has_mips_r6 ? FP_FRE : FP_FR0;
  135. /* Allow all ABIs we know about */
  136. max_abi = MIPS_ABI_FP_64A;
  137. }
  138. if ((abi0 > max_abi && abi0 != MIPS_ABI_FP_UNKNOWN) ||
  139. (abi1 > max_abi && abi1 != MIPS_ABI_FP_UNKNOWN))
  140. return -ELIBBAD;
  141. /* It's time to determine the FPU mode requirements */
  142. prog_req = (abi0 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
  143. interp_req = (abi1 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
  144. /*
  145. * Check whether the program's and interp's ABIs have a matching FPU
  146. * mode requirement.
  147. */
  148. prog_req.single = interp_req.single && prog_req.single;
  149. prog_req.soft = interp_req.soft && prog_req.soft;
  150. prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
  151. prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
  152. prog_req.fre = interp_req.fre && prog_req.fre;
  153. /*
  154. * Determine the desired FPU mode
  155. *
  156. * Decision making:
  157. *
  158. * - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
  159. * means that we have a combination of program and interpreter
  160. * that inherently require the hybrid FP mode.
  161. * - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
  162. * fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
  163. * instructions so we don't care about the mode. We will simply use
  164. * the one preferred by the hardware. In fpxx case, that ABI can
  165. * handle both FR=1 and FR=0, so, again, we simply choose the one
  166. * preferred by the hardware. Next, if we only use single-precision
  167. * FPU instructions, and the default ABI FPU mode is not good
  168. * (ie single + any ABI combination), we set again the FPU mode to the
  169. * one is preferred by the hardware. Next, if we know that the code
  170. * will only use single-precision instructions, shown by single being
  171. * true but frdefault being false, then we again set the FPU mode to
  172. * the one that is preferred by the hardware.
  173. * - We want FP_FR1 if that's the only matching mode and the default one
  174. * is not good.
  175. * - Return with -ELIBADD if we can't find a matching FPU mode.
  176. */
  177. if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
  178. state->overall_fp_mode = FP_FRE;
  179. else if ((prog_req.fr1 && prog_req.frdefault) ||
  180. (prog_req.single && !prog_req.frdefault))
  181. /* Make sure 64-bit MIPS III/IV/64R1 will not pick FR1 */
  182. state->overall_fp_mode = ((current_cpu_data.fpu_id & MIPS_FPIR_F64) &&
  183. cpu_has_mips_r2_r6) ?
  184. FP_FR1 : FP_FR0;
  185. else if (prog_req.fr1)
  186. state->overall_fp_mode = FP_FR1;
  187. else if (!prog_req.fre && !prog_req.frdefault &&
  188. !prog_req.fr1 && !prog_req.single && !prog_req.soft)
  189. return -ELIBBAD;
  190. return 0;
  191. }
  192. static inline void set_thread_fp_mode(int hybrid, int regs32)
  193. {
  194. if (hybrid)
  195. set_thread_flag(TIF_HYBRID_FPREGS);
  196. else
  197. clear_thread_flag(TIF_HYBRID_FPREGS);
  198. if (regs32)
  199. set_thread_flag(TIF_32BIT_FPREGS);
  200. else
  201. clear_thread_flag(TIF_32BIT_FPREGS);
  202. }
  203. void mips_set_personality_fp(struct arch_elf_state *state)
  204. {
  205. /*
  206. * This function is only ever called for O32 ELFs so we should
  207. * not be worried about N32/N64 binaries.
  208. */
  209. if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
  210. return;
  211. switch (state->overall_fp_mode) {
  212. case FP_FRE:
  213. set_thread_fp_mode(1, 0);
  214. break;
  215. case FP_FR0:
  216. set_thread_fp_mode(0, 1);
  217. break;
  218. case FP_FR1:
  219. set_thread_fp_mode(0, 0);
  220. break;
  221. default:
  222. BUG();
  223. }
  224. }