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