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