1 // SPDX-License-Identifier: GPL-2.0-or-later 1 2 /* 3 * Copyright (C) 2014 Imagination Technologies 4 * Author: Paul Burton <paul.burton@mips.com> 5 */ 6 7 #include <linux/binfmts.h> 8 #include <linux/elf.h> 9 #include <linux/export.h> 10 #include <linux/sched.h> 11 12 #include <asm/cpu-features.h> 13 #include <asm/cpu-info.h> 14 #include <asm/fpu.h> 15 16 #ifdef CONFIG_MIPS_FP_SUPPORT 17 18 /* Whether to accept legacy-NaN and 2008-NaN u 19 bool mips_use_nan_legacy; 20 bool mips_use_nan_2008; 21 22 /* FPU modes */ 23 enum { 24 FP_FRE, 25 FP_FR0, 26 FP_FR1, 27 }; 28 29 /** 30 * struct mode_req - ABI FPU mode requirements 31 * @single: The program being loaded needs 32 * single precision instructions 33 * either FR0 or FR1. 34 * @soft: The soft(-float) requirement m 35 * loaded needs has no FPU depend 36 * FPU instructions). 37 * @fr1: The program being loaded depen 38 * @frdefault: The program being loaded depen 39 * That is FR0 for O32 and FR1 fo 40 * @fre: The program being loaded depen 41 * a bridge which uses FR=1 whils 42 * full compatibility with pre-ex 43 * ABI. 44 * 45 * More information about the FP ABIs can be f 46 * 47 * https://dmz-portal.mips.com/wiki/MIPS_O32_A 48 * 49 */ 50 51 struct mode_req { 52 bool single; 53 bool soft; 54 bool fr1; 55 bool frdefault; 56 bool fre; 57 }; 58 59 static const struct mode_req fpu_reqs[] = { 60 [MIPS_ABI_FP_ANY] = { true, true, 61 [MIPS_ABI_FP_DOUBLE] = { false, false, 62 [MIPS_ABI_FP_SINGLE] = { true, false, 63 [MIPS_ABI_FP_SOFT] = { false, true, 64 [MIPS_ABI_FP_OLD_64] = { false, false, 65 [MIPS_ABI_FP_XX] = { false, false, 66 [MIPS_ABI_FP_64] = { false, false, 67 [MIPS_ABI_FP_64A] = { false, false, 68 }; 69 70 /* 71 * Mode requirements when .MIPS.abiflags is no 72 * Not present means that everything is accept 73 */ 74 static struct mode_req none_req = { true, true 75 76 int arch_elf_pt_proc(void *_ehdr, void *_phdr, 77 bool is_interp, struct ar 78 { 79 union { 80 struct elf32_hdr e32; 81 struct elf64_hdr e64; 82 } *ehdr = _ehdr; 83 struct elf32_phdr *phdr32 = _phdr; 84 struct elf64_phdr *phdr64 = _phdr; 85 struct mips_elf_abiflags_v0 abiflags; 86 bool elf32; 87 u32 flags; 88 int ret; 89 loff_t pos; 90 91 elf32 = ehdr->e32.e_ident[EI_CLASS] == 92 flags = elf32 ? ehdr->e32.e_flags : eh 93 94 /* Let's see if this is an O32 ELF */ 95 if (elf32) { 96 if (flags & EF_MIPS_FP64) { 97 /* 98 * Set MIPS_ABI_FP_OLD 99 * later if needed 100 */ 101 if (is_interp) 102 state->interp_ 103 else 104 state->fp_abi 105 } 106 if (phdr32->p_type != PT_MIPS_ 107 return 0; 108 109 if (phdr32->p_filesz < sizeof( 110 return -EINVAL; 111 pos = phdr32->p_offset; 112 } else { 113 if (phdr64->p_type != PT_MIPS_ 114 return 0; 115 if (phdr64->p_filesz < sizeof( 116 return -EINVAL; 117 pos = phdr64->p_offset; 118 } 119 120 ret = kernel_read(elf, &abiflags, size 121 if (ret < 0) 122 return ret; 123 if (ret != sizeof(abiflags)) 124 return -EIO; 125 126 /* Record the required FP ABIs for use 127 if (is_interp) 128 state->interp_fp_abi = abiflag 129 else 130 state->fp_abi = abiflags.fp_ab 131 132 return 0; 133 } 134 135 int arch_check_elf(void *_ehdr, bool has_inter 136 struct arch_elf_state *stat 137 { 138 union { 139 struct elf32_hdr e32; 140 struct elf64_hdr e64; 141 } *ehdr = _ehdr; 142 union { 143 struct elf32_hdr e32; 144 struct elf64_hdr e64; 145 } *iehdr = _interp_ehdr; 146 struct mode_req prog_req, interp_req; 147 int fp_abi, interp_fp_abi, abi0, abi1, 148 bool elf32; 149 u32 flags; 150 151 elf32 = ehdr->e32.e_ident[EI_CLASS] == 152 flags = elf32 ? ehdr->e32.e_flags : eh 153 154 /* 155 * Determine the NaN personality, reje 156 * Also ensure that any interpreter ma 157 */ 158 if (flags & EF_MIPS_NAN2008) { 159 if (mips_use_nan_2008) 160 state->nan_2008 = 1; 161 else 162 return -ENOEXEC; 163 } else { 164 if (mips_use_nan_legacy) 165 state->nan_2008 = 0; 166 else 167 return -ENOEXEC; 168 } 169 if (has_interpreter) { 170 bool ielf32; 171 u32 iflags; 172 173 ielf32 = iehdr->e32.e_ident[EI 174 iflags = ielf32 ? iehdr->e32.e 175 176 if ((flags ^ iflags) & EF_MIPS 177 return -ELIBBAD; 178 } 179 180 if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_S 181 return 0; 182 183 fp_abi = state->fp_abi; 184 185 if (has_interpreter) { 186 interp_fp_abi = state->interp_ 187 188 abi0 = min(fp_abi, interp_fp_a 189 abi1 = max(fp_abi, interp_fp_a 190 } else { 191 abi0 = abi1 = fp_abi; 192 } 193 194 if (elf32 && !(flags & EF_MIPS_ABI2)) 195 /* Default to a mode capable o 196 state->overall_fp_mode = cpu_h 197 198 /* Allow all ABIs we know abou 199 max_abi = MIPS_ABI_FP_64A; 200 } else { 201 /* MIPS64 code always uses FR= 202 state->overall_fp_mode = FP_FR 203 204 /* Disallow access to the vari 205 max_abi = MIPS_ABI_FP_SOFT; 206 } 207 208 if ((abi0 > max_abi && abi0 != MIPS_AB 209 (abi1 > max_abi && abi1 != MIPS_AB 210 return -ELIBBAD; 211 212 /* It's time to determine the FPU mode 213 prog_req = (abi0 == MIPS_ABI_FP_UNKNOW 214 interp_req = (abi1 == MIPS_ABI_FP_UNKN 215 216 /* 217 * Check whether the program's and int 218 * mode requirement. 219 */ 220 prog_req.single = interp_req.single && 221 prog_req.soft = interp_req.soft && pro 222 prog_req.fr1 = interp_req.fr1 && prog_ 223 prog_req.frdefault = interp_req.frdefa 224 prog_req.fre = interp_req.fre && prog_ 225 226 /* 227 * Determine the desired FPU mode 228 * 229 * Decision making: 230 * 231 * - We want FR_FRE if FRE=1 and both 232 * means that we have a combination 233 * that inherently require the hybri 234 * - If FR1 and FRDEFAULT is true, tha 235 * fpxx case. This is because, in an 236 * instructions so we don't care abo 237 * the one preferred by the hardware 238 * handle both FR=1 and FR=0, so, ag 239 * preferred by the hardware. Next, 240 * FPU instructions, and the default 241 * (ie single + any ABI combination) 242 * one is preferred by the hardware. 243 * will only use single-precision in 244 * true but frdefault being false, t 245 * the one that is preferred by the 246 * - We want FP_FR1 if that's the only 247 * is not good. 248 * - Return with -ELIBADD if we can't 249 */ 250 if (prog_req.fre && !prog_req.frdefaul 251 state->overall_fp_mode = FP_FR 252 else if ((prog_req.fr1 && prog_req.frd 253 (prog_req.single && !prog_req 254 /* Make sure 64-bit MIPS III/I 255 state->overall_fp_mode = ((raw 256 cpu_ 257 FP_F 258 else if (prog_req.fr1) 259 state->overall_fp_mode = FP_FR 260 else if (!prog_req.fre && !prog_req.f 261 !prog_req.fr1 && !prog_req.s 262 return -ELIBBAD; 263 264 return 0; 265 } 266 267 static inline void set_thread_fp_mode(int hybr 268 { 269 if (hybrid) 270 set_thread_flag(TIF_HYBRID_FPR 271 else 272 clear_thread_flag(TIF_HYBRID_F 273 if (regs32) 274 set_thread_flag(TIF_32BIT_FPRE 275 else 276 clear_thread_flag(TIF_32BIT_FP 277 } 278 279 void mips_set_personality_fp(struct arch_elf_s 280 { 281 /* 282 * This function is only ever called f 283 * not be worried about N32/N64 binari 284 */ 285 286 if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_S 287 return; 288 289 switch (state->overall_fp_mode) { 290 case FP_FRE: 291 set_thread_fp_mode(1, 0); 292 break; 293 case FP_FR0: 294 set_thread_fp_mode(0, 1); 295 break; 296 case FP_FR1: 297 set_thread_fp_mode(0, 0); 298 break; 299 default: 300 BUG(); 301 } 302 } 303 304 /* 305 * Select the IEEE 754 NaN encoding and ABS.fm 306 * in FCSR according to the ELF NaN personalit 307 */ 308 void mips_set_personality_nan(struct arch_elf_ 309 { 310 struct cpuinfo_mips *c = &boot_cpu_dat 311 struct task_struct *t = current; 312 313 /* Do this early so t->thread.fpu.fcr3 314 * we are preempted before the lose_fp 315 */ 316 lose_fpu(0); 317 318 t->thread.fpu.fcr31 = c->fpu_csr31; 319 switch (state->nan_2008) { 320 case 0: 321 if (!(c->fpu_msk31 & FPU_CSR_N 322 t->thread.fpu.fcr31 &= 323 if (!(c->fpu_msk31 & FPU_CSR_A 324 t->thread.fpu.fcr31 &= 325 break; 326 case 1: 327 if (!(c->fpu_msk31 & FPU_CSR_N 328 t->thread.fpu.fcr31 |= 329 if (!(c->fpu_msk31 & FPU_CSR_A 330 t->thread.fpu.fcr31 |= 331 break; 332 default: 333 BUG(); 334 } 335 } 336 337 #endif /* CONFIG_MIPS_FP_SUPPORT */ 338 339 int mips_elf_read_implies_exec(void *elf_ex, i 340 { 341 /* 342 * Set READ_IMPLIES_EXEC only on non-N 343 * do not request a specific state via 344 */ 345 return (!cpu_has_rixi && exstack == EX 346 } 347 EXPORT_SYMBOL(mips_elf_read_implies_exec); 348
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.