12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
290cee759SPaul Burton /*
390cee759SPaul Burton * Copyright (C) 2014 Imagination Technologies
4fb615d61SPaul Burton * Author: Paul Burton <paul.burton@mips.com>
590cee759SPaul Burton */
690cee759SPaul Burton
71a770b85SPaul Burton #include <linux/binfmts.h>
890cee759SPaul Burton #include <linux/elf.h>
91a770b85SPaul Burton #include <linux/export.h>
1090cee759SPaul Burton #include <linux/sched.h>
1190cee759SPaul Burton
121a770b85SPaul Burton #include <asm/cpu-features.h>
132b5e869eSMaciej W. Rozycki #include <asm/cpu-info.h>
14*33f49a68SXi Ruoyao #include <asm/fpu.h>
152b5e869eSMaciej W. Rozycki
16ea6a3737SPaul Burton #ifdef CONFIG_MIPS_FP_SUPPORT
17ea6a3737SPaul Burton
18503943e0SMaciej W. Rozycki /* Whether to accept legacy-NaN and 2008-NaN user binaries. */
19503943e0SMaciej W. Rozycki bool mips_use_nan_legacy;
20503943e0SMaciej W. Rozycki bool mips_use_nan_2008;
21503943e0SMaciej W. Rozycki
2246490b57SMarkos Chandras /* FPU modes */
2390cee759SPaul Burton enum {
2446490b57SMarkos Chandras FP_FRE,
2546490b57SMarkos Chandras FP_FR0,
2646490b57SMarkos Chandras FP_FR1,
2790cee759SPaul Burton };
2890cee759SPaul Burton
2946490b57SMarkos Chandras /**
3046490b57SMarkos Chandras * struct mode_req - ABI FPU mode requirements
3146490b57SMarkos Chandras * @single: The program being loaded needs an FPU but it will only issue
3246490b57SMarkos Chandras * single precision instructions meaning that it can execute in
3346490b57SMarkos Chandras * either FR0 or FR1.
3446490b57SMarkos Chandras * @soft: The soft(-float) requirement means that the program being
3546490b57SMarkos Chandras * loaded needs has no FPU dependency at all (i.e. it has no
3646490b57SMarkos Chandras * FPU instructions).
3746490b57SMarkos Chandras * @fr1: The program being loaded depends on FPU being in FR=1 mode.
3846490b57SMarkos Chandras * @frdefault: The program being loaded depends on the default FPU mode.
3946490b57SMarkos Chandras * That is FR0 for O32 and FR1 for N32/N64.
4046490b57SMarkos Chandras * @fre: The program being loaded depends on FPU with FRE=1. This mode is
4146490b57SMarkos Chandras * a bridge which uses FR=1 whilst still being able to maintain
4246490b57SMarkos Chandras * full compatibility with pre-existing code using the O32 FP32
4346490b57SMarkos Chandras * ABI.
4446490b57SMarkos Chandras *
4546490b57SMarkos Chandras * More information about the FP ABIs can be found here:
4646490b57SMarkos Chandras *
4746490b57SMarkos Chandras * https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking#10.4.1._Basic_mode_set-up
4846490b57SMarkos Chandras *
4946490b57SMarkos Chandras */
5046490b57SMarkos Chandras
5146490b57SMarkos Chandras struct mode_req {
5246490b57SMarkos Chandras bool single;
5346490b57SMarkos Chandras bool soft;
5446490b57SMarkos Chandras bool fr1;
5546490b57SMarkos Chandras bool frdefault;
5646490b57SMarkos Chandras bool fre;
5746490b57SMarkos Chandras };
5846490b57SMarkos Chandras
5946490b57SMarkos Chandras static const struct mode_req fpu_reqs[] = {
6046490b57SMarkos Chandras [MIPS_ABI_FP_ANY] = { true, true, true, true, true },
6146490b57SMarkos Chandras [MIPS_ABI_FP_DOUBLE] = { false, false, false, true, true },
6246490b57SMarkos Chandras [MIPS_ABI_FP_SINGLE] = { true, false, false, false, false },
6346490b57SMarkos Chandras [MIPS_ABI_FP_SOFT] = { false, true, false, false, false },
6446490b57SMarkos Chandras [MIPS_ABI_FP_OLD_64] = { false, false, false, false, false },
6546490b57SMarkos Chandras [MIPS_ABI_FP_XX] = { false, false, true, true, true },
6646490b57SMarkos Chandras [MIPS_ABI_FP_64] = { false, false, true, false, false },
6746490b57SMarkos Chandras [MIPS_ABI_FP_64A] = { false, false, true, false, true }
6846490b57SMarkos Chandras };
6946490b57SMarkos Chandras
7046490b57SMarkos Chandras /*
7146490b57SMarkos Chandras * Mode requirements when .MIPS.abiflags is not present in the ELF.
7246490b57SMarkos Chandras * Not present means that everything is acceptable except FR1.
7346490b57SMarkos Chandras */
7446490b57SMarkos Chandras static struct mode_req none_req = { true, true, false, true, true };
7546490b57SMarkos Chandras
arch_elf_pt_proc(void * _ehdr,void * _phdr,struct file * elf,bool is_interp,struct arch_elf_state * state)7690cee759SPaul Burton int arch_elf_pt_proc(void *_ehdr, void *_phdr, struct file *elf,
7790cee759SPaul Burton bool is_interp, struct arch_elf_state *state)
7890cee759SPaul Burton {
792ed02dd4SMaciej W. Rozycki union {
802ed02dd4SMaciej W. Rozycki struct elf32_hdr e32;
812ed02dd4SMaciej W. Rozycki struct elf64_hdr e64;
822ed02dd4SMaciej W. Rozycki } *ehdr = _ehdr;
8346490b57SMarkos Chandras struct elf32_phdr *phdr32 = _phdr;
8446490b57SMarkos Chandras struct elf64_phdr *phdr64 = _phdr;
8590cee759SPaul Burton struct mips_elf_abiflags_v0 abiflags;
862ed02dd4SMaciej W. Rozycki bool elf32;
872ed02dd4SMaciej W. Rozycki u32 flags;
8890cee759SPaul Burton int ret;
89bdd1d2d3SChristoph Hellwig loff_t pos;
9090cee759SPaul Burton
912ed02dd4SMaciej W. Rozycki elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
922ed02dd4SMaciej W. Rozycki flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
932ed02dd4SMaciej W. Rozycki
944939788eSRalf Baechle /* Let's see if this is an O32 ELF */
952ed02dd4SMaciej W. Rozycki if (elf32) {
962ed02dd4SMaciej W. Rozycki if (flags & EF_MIPS_FP64) {
9746490b57SMarkos Chandras /*
9846490b57SMarkos Chandras * Set MIPS_ABI_FP_OLD_64 for EF_MIPS_FP64. We will override it
9946490b57SMarkos Chandras * later if needed
10046490b57SMarkos Chandras */
10146490b57SMarkos Chandras if (is_interp)
10246490b57SMarkos Chandras state->interp_fp_abi = MIPS_ABI_FP_OLD_64;
10346490b57SMarkos Chandras else
10446490b57SMarkos Chandras state->fp_abi = MIPS_ABI_FP_OLD_64;
10546490b57SMarkos Chandras }
10646490b57SMarkos Chandras if (phdr32->p_type != PT_MIPS_ABIFLAGS)
10790cee759SPaul Burton return 0;
10846490b57SMarkos Chandras
10946490b57SMarkos Chandras if (phdr32->p_filesz < sizeof(abiflags))
11090cee759SPaul Burton return -EINVAL;
111bdd1d2d3SChristoph Hellwig pos = phdr32->p_offset;
11246490b57SMarkos Chandras } else {
11346490b57SMarkos Chandras if (phdr64->p_type != PT_MIPS_ABIFLAGS)
11446490b57SMarkos Chandras return 0;
11546490b57SMarkos Chandras if (phdr64->p_filesz < sizeof(abiflags))
11646490b57SMarkos Chandras return -EINVAL;
117bdd1d2d3SChristoph Hellwig pos = phdr64->p_offset;
11846490b57SMarkos Chandras }
11946490b57SMarkos Chandras
120bdd1d2d3SChristoph Hellwig ret = kernel_read(elf, &abiflags, sizeof(abiflags), &pos);
12190cee759SPaul Burton if (ret < 0)
12290cee759SPaul Burton return ret;
12390cee759SPaul Burton if (ret != sizeof(abiflags))
12490cee759SPaul Burton return -EIO;
12590cee759SPaul Burton
12690cee759SPaul Burton /* Record the required FP ABIs for use by mips_check_elf */
12790cee759SPaul Burton if (is_interp)
12890cee759SPaul Burton state->interp_fp_abi = abiflags.fp_abi;
12990cee759SPaul Burton else
13090cee759SPaul Burton state->fp_abi = abiflags.fp_abi;
13190cee759SPaul Burton
13290cee759SPaul Burton return 0;
13390cee759SPaul Burton }
13490cee759SPaul Burton
arch_check_elf(void * _ehdr,bool has_interpreter,void * _interp_ehdr,struct arch_elf_state * state)135eb4bc076SMaciej W. Rozycki int arch_check_elf(void *_ehdr, bool has_interpreter, void *_interp_ehdr,
13690cee759SPaul Burton struct arch_elf_state *state)
13790cee759SPaul Burton {
1382ed02dd4SMaciej W. Rozycki union {
1392ed02dd4SMaciej W. Rozycki struct elf32_hdr e32;
1402ed02dd4SMaciej W. Rozycki struct elf64_hdr e64;
1412ed02dd4SMaciej W. Rozycki } *ehdr = _ehdr;
1422b5e869eSMaciej W. Rozycki union {
1432b5e869eSMaciej W. Rozycki struct elf32_hdr e32;
1442b5e869eSMaciej W. Rozycki struct elf64_hdr e64;
1452b5e869eSMaciej W. Rozycki } *iehdr = _interp_ehdr;
14646490b57SMarkos Chandras struct mode_req prog_req, interp_req;
14746490b57SMarkos Chandras int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
1482ed02dd4SMaciej W. Rozycki bool elf32;
1492ed02dd4SMaciej W. Rozycki u32 flags;
1502ed02dd4SMaciej W. Rozycki
1512ed02dd4SMaciej W. Rozycki elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
1522ed02dd4SMaciej W. Rozycki flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
15390cee759SPaul Burton
1542b5e869eSMaciej W. Rozycki /*
155503943e0SMaciej W. Rozycki * Determine the NaN personality, reject the binary if not allowed.
156503943e0SMaciej W. Rozycki * Also ensure that any interpreter matches the executable.
1572b5e869eSMaciej W. Rozycki */
1582b5e869eSMaciej W. Rozycki if (flags & EF_MIPS_NAN2008) {
159503943e0SMaciej W. Rozycki if (mips_use_nan_2008)
1602b5e869eSMaciej W. Rozycki state->nan_2008 = 1;
1612b5e869eSMaciej W. Rozycki else
1622b5e869eSMaciej W. Rozycki return -ENOEXEC;
1632b5e869eSMaciej W. Rozycki } else {
164503943e0SMaciej W. Rozycki if (mips_use_nan_legacy)
1652b5e869eSMaciej W. Rozycki state->nan_2008 = 0;
1662b5e869eSMaciej W. Rozycki else
1672b5e869eSMaciej W. Rozycki return -ENOEXEC;
1682b5e869eSMaciej W. Rozycki }
1692b5e869eSMaciej W. Rozycki if (has_interpreter) {
1702b5e869eSMaciej W. Rozycki bool ielf32;
1712b5e869eSMaciej W. Rozycki u32 iflags;
1722b5e869eSMaciej W. Rozycki
1732b5e869eSMaciej W. Rozycki ielf32 = iehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
1742b5e869eSMaciej W. Rozycki iflags = ielf32 ? iehdr->e32.e_flags : iehdr->e64.e_flags;
1752b5e869eSMaciej W. Rozycki
1762b5e869eSMaciej W. Rozycki if ((flags ^ iflags) & EF_MIPS_NAN2008)
1772b5e869eSMaciej W. Rozycki return -ELIBBAD;
1782b5e869eSMaciej W. Rozycki }
1792b5e869eSMaciej W. Rozycki
18097f2645fSMasahiro Yamada if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
18190cee759SPaul Burton return 0;
18290cee759SPaul Burton
183a49dc427SMaciej W. Rozycki fp_abi = state->fp_abi;
18490cee759SPaul Burton
18590cee759SPaul Burton if (has_interpreter) {
186a49dc427SMaciej W. Rozycki interp_fp_abi = state->interp_fp_abi;
18790cee759SPaul Burton
18890cee759SPaul Burton abi0 = min(fp_abi, interp_fp_abi);
18990cee759SPaul Burton abi1 = max(fp_abi, interp_fp_abi);
19090cee759SPaul Burton } else {
19190cee759SPaul Burton abi0 = abi1 = fp_abi;
19290cee759SPaul Burton }
19390cee759SPaul Burton
1942ed02dd4SMaciej W. Rozycki if (elf32 && !(flags & EF_MIPS_ABI2)) {
195620b1550SPaul Burton /* Default to a mode capable of running code expecting FR=0 */
196620b1550SPaul Burton state->overall_fp_mode = cpu_has_mips_r6 ? FP_FRE : FP_FR0;
197620b1550SPaul Burton
198620b1550SPaul Burton /* Allow all ABIs we know about */
199620b1550SPaul Burton max_abi = MIPS_ABI_FP_64A;
2002ed02dd4SMaciej W. Rozycki } else {
2012ed02dd4SMaciej W. Rozycki /* MIPS64 code always uses FR=1, thus the default is easy */
2022ed02dd4SMaciej W. Rozycki state->overall_fp_mode = FP_FR1;
2032ed02dd4SMaciej W. Rozycki
2042ed02dd4SMaciej W. Rozycki /* Disallow access to the various FPXX & FP64 ABIs */
2052ed02dd4SMaciej W. Rozycki max_abi = MIPS_ABI_FP_SOFT;
206620b1550SPaul Burton }
20790cee759SPaul Burton
20846490b57SMarkos Chandras if ((abi0 > max_abi && abi0 != MIPS_ABI_FP_UNKNOWN) ||
20946490b57SMarkos Chandras (abi1 > max_abi && abi1 != MIPS_ABI_FP_UNKNOWN))
21090cee759SPaul Burton return -ELIBBAD;
21190cee759SPaul Burton
21246490b57SMarkos Chandras /* It's time to determine the FPU mode requirements */
21346490b57SMarkos Chandras prog_req = (abi0 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
21446490b57SMarkos Chandras interp_req = (abi1 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
21546490b57SMarkos Chandras
21646490b57SMarkos Chandras /*
21746490b57SMarkos Chandras * Check whether the program's and interp's ABIs have a matching FPU
21846490b57SMarkos Chandras * mode requirement.
21946490b57SMarkos Chandras */
22046490b57SMarkos Chandras prog_req.single = interp_req.single && prog_req.single;
22146490b57SMarkos Chandras prog_req.soft = interp_req.soft && prog_req.soft;
22246490b57SMarkos Chandras prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
22346490b57SMarkos Chandras prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
22446490b57SMarkos Chandras prog_req.fre = interp_req.fre && prog_req.fre;
22546490b57SMarkos Chandras
22646490b57SMarkos Chandras /*
22746490b57SMarkos Chandras * Determine the desired FPU mode
22846490b57SMarkos Chandras *
22946490b57SMarkos Chandras * Decision making:
23046490b57SMarkos Chandras *
23146490b57SMarkos Chandras * - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
23246490b57SMarkos Chandras * means that we have a combination of program and interpreter
23346490b57SMarkos Chandras * that inherently require the hybrid FP mode.
23446490b57SMarkos Chandras * - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
23546490b57SMarkos Chandras * fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
23646490b57SMarkos Chandras * instructions so we don't care about the mode. We will simply use
23746490b57SMarkos Chandras * the one preferred by the hardware. In fpxx case, that ABI can
23846490b57SMarkos Chandras * handle both FR=1 and FR=0, so, again, we simply choose the one
23946490b57SMarkos Chandras * preferred by the hardware. Next, if we only use single-precision
24046490b57SMarkos Chandras * FPU instructions, and the default ABI FPU mode is not good
24146490b57SMarkos Chandras * (ie single + any ABI combination), we set again the FPU mode to the
24246490b57SMarkos Chandras * one is preferred by the hardware. Next, if we know that the code
24346490b57SMarkos Chandras * will only use single-precision instructions, shown by single being
24446490b57SMarkos Chandras * true but frdefault being false, then we again set the FPU mode to
24546490b57SMarkos Chandras * the one that is preferred by the hardware.
24646490b57SMarkos Chandras * - We want FP_FR1 if that's the only matching mode and the default one
24746490b57SMarkos Chandras * is not good.
24846490b57SMarkos Chandras * - Return with -ELIBADD if we can't find a matching FPU mode.
24946490b57SMarkos Chandras */
25046490b57SMarkos Chandras if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
25146490b57SMarkos Chandras state->overall_fp_mode = FP_FRE;
25246490b57SMarkos Chandras else if ((prog_req.fr1 && prog_req.frdefault) ||
25346490b57SMarkos Chandras (prog_req.single && !prog_req.frdefault))
25446490b57SMarkos Chandras /* Make sure 64-bit MIPS III/IV/64R1 will not pick FR1 */
255c46f59e9SJames Cowgill state->overall_fp_mode = ((raw_current_cpu_data.fpu_id & MIPS_FPIR_F64) &&
25646490b57SMarkos Chandras cpu_has_mips_r2_r6) ?
25746490b57SMarkos Chandras FP_FR1 : FP_FR0;
25846490b57SMarkos Chandras else if (prog_req.fr1)
25946490b57SMarkos Chandras state->overall_fp_mode = FP_FR1;
26046490b57SMarkos Chandras else if (!prog_req.fre && !prog_req.frdefault &&
26146490b57SMarkos Chandras !prog_req.fr1 && !prog_req.single && !prog_req.soft)
26290cee759SPaul Burton return -ELIBBAD;
26390cee759SPaul Burton
26490cee759SPaul Burton return 0;
26590cee759SPaul Burton }
26690cee759SPaul Burton
set_thread_fp_mode(int hybrid,int regs32)26746490b57SMarkos Chandras static inline void set_thread_fp_mode(int hybrid, int regs32)
26890cee759SPaul Burton {
26946490b57SMarkos Chandras if (hybrid)
270f4af6fb2SPaul Burton set_thread_flag(TIF_HYBRID_FPREGS);
27146490b57SMarkos Chandras else
27290cee759SPaul Burton clear_thread_flag(TIF_HYBRID_FPREGS);
27346490b57SMarkos Chandras if (regs32)
27490cee759SPaul Burton set_thread_flag(TIF_32BIT_FPREGS);
27590cee759SPaul Burton else
27690cee759SPaul Burton clear_thread_flag(TIF_32BIT_FPREGS);
27746490b57SMarkos Chandras }
27890cee759SPaul Burton
mips_set_personality_fp(struct arch_elf_state * state)27946490b57SMarkos Chandras void mips_set_personality_fp(struct arch_elf_state *state)
28046490b57SMarkos Chandras {
28146490b57SMarkos Chandras /*
28246490b57SMarkos Chandras * This function is only ever called for O32 ELFs so we should
28346490b57SMarkos Chandras * not be worried about N32/N64 binaries.
28446490b57SMarkos Chandras */
28546490b57SMarkos Chandras
28697f2645fSMasahiro Yamada if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
28746490b57SMarkos Chandras return;
28846490b57SMarkos Chandras
28946490b57SMarkos Chandras switch (state->overall_fp_mode) {
29046490b57SMarkos Chandras case FP_FRE:
29146490b57SMarkos Chandras set_thread_fp_mode(1, 0);
29290cee759SPaul Burton break;
29346490b57SMarkos Chandras case FP_FR0:
29446490b57SMarkos Chandras set_thread_fp_mode(0, 1);
29546490b57SMarkos Chandras break;
29646490b57SMarkos Chandras case FP_FR1:
29746490b57SMarkos Chandras set_thread_fp_mode(0, 0);
29846490b57SMarkos Chandras break;
29990cee759SPaul Burton default:
30090cee759SPaul Burton BUG();
30190cee759SPaul Burton }
30290cee759SPaul Burton }
3032b5e869eSMaciej W. Rozycki
3042b5e869eSMaciej W. Rozycki /*
3052b5e869eSMaciej W. Rozycki * Select the IEEE 754 NaN encoding and ABS.fmt/NEG.fmt execution mode
3062b5e869eSMaciej W. Rozycki * in FCSR according to the ELF NaN personality.
3072b5e869eSMaciej W. Rozycki */
mips_set_personality_nan(struct arch_elf_state * state)3082b5e869eSMaciej W. Rozycki void mips_set_personality_nan(struct arch_elf_state *state)
3092b5e869eSMaciej W. Rozycki {
3102b5e869eSMaciej W. Rozycki struct cpuinfo_mips *c = &boot_cpu_data;
3112b5e869eSMaciej W. Rozycki struct task_struct *t = current;
3122b5e869eSMaciej W. Rozycki
313*33f49a68SXi Ruoyao /* Do this early so t->thread.fpu.fcr31 won't be clobbered in case
314*33f49a68SXi Ruoyao * we are preempted before the lose_fpu(0) in start_thread.
315*33f49a68SXi Ruoyao */
316*33f49a68SXi Ruoyao lose_fpu(0);
317*33f49a68SXi Ruoyao
3182b5e869eSMaciej W. Rozycki t->thread.fpu.fcr31 = c->fpu_csr31;
3192b5e869eSMaciej W. Rozycki switch (state->nan_2008) {
3202b5e869eSMaciej W. Rozycki case 0:
3212b5e869eSMaciej W. Rozycki break;
3222b5e869eSMaciej W. Rozycki case 1:
3232b5e869eSMaciej W. Rozycki if (!(c->fpu_msk31 & FPU_CSR_NAN2008))
3242b5e869eSMaciej W. Rozycki t->thread.fpu.fcr31 |= FPU_CSR_NAN2008;
3252b5e869eSMaciej W. Rozycki if (!(c->fpu_msk31 & FPU_CSR_ABS2008))
3262b5e869eSMaciej W. Rozycki t->thread.fpu.fcr31 |= FPU_CSR_ABS2008;
3272b5e869eSMaciej W. Rozycki break;
3282b5e869eSMaciej W. Rozycki default:
3292b5e869eSMaciej W. Rozycki BUG();
3302b5e869eSMaciej W. Rozycki }
3312b5e869eSMaciej W. Rozycki }
3321a770b85SPaul Burton
333ea6a3737SPaul Burton #endif /* CONFIG_MIPS_FP_SUPPORT */
334ea6a3737SPaul Burton
mips_elf_read_implies_exec(void * elf_ex,int exstack)3351a770b85SPaul Burton int mips_elf_read_implies_exec(void *elf_ex, int exstack)
3361a770b85SPaul Burton {
337fbb1d4b3SKees Cook /*
338fbb1d4b3SKees Cook * Set READ_IMPLIES_EXEC only on non-NX systems that
339fbb1d4b3SKees Cook * do not request a specific state via PT_GNU_STACK.
340fbb1d4b3SKees Cook */
341fbb1d4b3SKees Cook return (!cpu_has_rixi && exstack == EXSTACK_DEFAULT);
3421a770b85SPaul Burton }
3431a770b85SPaul Burton EXPORT_SYMBOL(mips_elf_read_implies_exec);
344