xref: /linux/arch/mips/kernel/elf.c (revision a49dc4276e07fb94858bcaf46bf99ac3effd026a)
190cee759SPaul Burton /*
290cee759SPaul Burton  * Copyright (C) 2014 Imagination Technologies
390cee759SPaul Burton  * Author: Paul Burton <paul.burton@imgtec.com>
490cee759SPaul Burton  *
590cee759SPaul Burton  * This program is free software; you can redistribute it and/or modify it
690cee759SPaul Burton  * under the terms of the GNU General Public License as published by the
790cee759SPaul Burton  * Free Software Foundation;  either version 2 of the  License, or (at your
890cee759SPaul Burton  * option) any later version.
990cee759SPaul Burton  */
1090cee759SPaul Burton 
1190cee759SPaul Burton #include <linux/elf.h>
1290cee759SPaul Burton #include <linux/sched.h>
1390cee759SPaul Burton 
1446490b57SMarkos Chandras /* FPU modes */
1590cee759SPaul Burton enum {
1646490b57SMarkos Chandras 	FP_FRE,
1746490b57SMarkos Chandras 	FP_FR0,
1846490b57SMarkos Chandras 	FP_FR1,
1990cee759SPaul Burton };
2090cee759SPaul Burton 
2146490b57SMarkos Chandras /**
2246490b57SMarkos Chandras  * struct mode_req - ABI FPU mode requirements
2346490b57SMarkos Chandras  * @single:	The program being loaded needs an FPU but it will only issue
2446490b57SMarkos Chandras  *		single precision instructions meaning that it can execute in
2546490b57SMarkos Chandras  *		either FR0 or FR1.
2646490b57SMarkos Chandras  * @soft:	The soft(-float) requirement means that the program being
2746490b57SMarkos Chandras  *		loaded needs has no FPU dependency at all (i.e. it has no
2846490b57SMarkos Chandras  *		FPU instructions).
2946490b57SMarkos Chandras  * @fr1:	The program being loaded depends on FPU being in FR=1 mode.
3046490b57SMarkos Chandras  * @frdefault:	The program being loaded depends on the default FPU mode.
3146490b57SMarkos Chandras  *		That is FR0 for O32 and FR1 for N32/N64.
3246490b57SMarkos Chandras  * @fre:	The program being loaded depends on FPU with FRE=1. This mode is
3346490b57SMarkos Chandras  *		a bridge which uses FR=1 whilst still being able to maintain
3446490b57SMarkos Chandras  *		full compatibility with pre-existing code using the O32 FP32
3546490b57SMarkos Chandras  *		ABI.
3646490b57SMarkos Chandras  *
3746490b57SMarkos Chandras  * More information about the FP ABIs can be found here:
3846490b57SMarkos Chandras  *
3946490b57SMarkos Chandras  * https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking#10.4.1._Basic_mode_set-up
4046490b57SMarkos Chandras  *
4146490b57SMarkos Chandras  */
4246490b57SMarkos Chandras 
4346490b57SMarkos Chandras struct mode_req {
4446490b57SMarkos Chandras 	bool single;
4546490b57SMarkos Chandras 	bool soft;
4646490b57SMarkos Chandras 	bool fr1;
4746490b57SMarkos Chandras 	bool frdefault;
4846490b57SMarkos Chandras 	bool fre;
4946490b57SMarkos Chandras };
5046490b57SMarkos Chandras 
5146490b57SMarkos Chandras static const struct mode_req fpu_reqs[] = {
5246490b57SMarkos Chandras 	[MIPS_ABI_FP_ANY]    = { true,  true,  true,  true,  true  },
5346490b57SMarkos Chandras 	[MIPS_ABI_FP_DOUBLE] = { false, false, false, true,  true  },
5446490b57SMarkos Chandras 	[MIPS_ABI_FP_SINGLE] = { true,  false, false, false, false },
5546490b57SMarkos Chandras 	[MIPS_ABI_FP_SOFT]   = { false, true,  false, false, false },
5646490b57SMarkos Chandras 	[MIPS_ABI_FP_OLD_64] = { false, false, false, false, false },
5746490b57SMarkos Chandras 	[MIPS_ABI_FP_XX]     = { false, false, true,  true,  true  },
5846490b57SMarkos Chandras 	[MIPS_ABI_FP_64]     = { false, false, true,  false, false },
5946490b57SMarkos Chandras 	[MIPS_ABI_FP_64A]    = { false, false, true,  false, true  }
6046490b57SMarkos Chandras };
6146490b57SMarkos Chandras 
6246490b57SMarkos Chandras /*
6346490b57SMarkos Chandras  * Mode requirements when .MIPS.abiflags is not present in the ELF.
6446490b57SMarkos Chandras  * Not present means that everything is acceptable except FR1.
6546490b57SMarkos Chandras  */
6646490b57SMarkos Chandras static struct mode_req none_req = { true, true, false, true, true };
6746490b57SMarkos Chandras 
6890cee759SPaul Burton int arch_elf_pt_proc(void *_ehdr, void *_phdr, struct file *elf,
6990cee759SPaul Burton 		     bool is_interp, struct arch_elf_state *state)
7090cee759SPaul Burton {
7146490b57SMarkos Chandras 	struct elf32_hdr *ehdr32 = _ehdr;
7246490b57SMarkos Chandras 	struct elf32_phdr *phdr32 = _phdr;
7346490b57SMarkos Chandras 	struct elf64_phdr *phdr64 = _phdr;
7490cee759SPaul Burton 	struct mips_elf_abiflags_v0 abiflags;
7590cee759SPaul Burton 	int ret;
7690cee759SPaul Burton 
7746490b57SMarkos Chandras 	/* Lets see if this is an O32 ELF */
7846490b57SMarkos Chandras 	if (ehdr32->e_ident[EI_CLASS] == ELFCLASS32) {
7946490b57SMarkos Chandras 		/* FR = 1 for N32 */
8046490b57SMarkos Chandras 		if (ehdr32->e_flags & EF_MIPS_ABI2)
8146490b57SMarkos Chandras 			state->overall_fp_mode = FP_FR1;
8246490b57SMarkos Chandras 		else
8346490b57SMarkos Chandras 			/* Set a good default FPU mode for O32 */
8446490b57SMarkos Chandras 			state->overall_fp_mode = cpu_has_mips_r6 ?
8546490b57SMarkos Chandras 				FP_FRE : FP_FR0;
8646490b57SMarkos Chandras 
8746490b57SMarkos Chandras 		if (ehdr32->e_flags & EF_MIPS_FP64) {
8846490b57SMarkos Chandras 			/*
8946490b57SMarkos Chandras 			 * Set MIPS_ABI_FP_OLD_64 for EF_MIPS_FP64. We will override it
9046490b57SMarkos Chandras 			 * later if needed
9146490b57SMarkos Chandras 			 */
9246490b57SMarkos Chandras 			if (is_interp)
9346490b57SMarkos Chandras 				state->interp_fp_abi = MIPS_ABI_FP_OLD_64;
9446490b57SMarkos Chandras 			else
9546490b57SMarkos Chandras 				state->fp_abi = MIPS_ABI_FP_OLD_64;
9646490b57SMarkos Chandras 		}
9746490b57SMarkos Chandras 		if (phdr32->p_type != PT_MIPS_ABIFLAGS)
9890cee759SPaul Burton 			return 0;
9946490b57SMarkos Chandras 
10046490b57SMarkos Chandras 		if (phdr32->p_filesz < sizeof(abiflags))
10190cee759SPaul Burton 			return -EINVAL;
10290cee759SPaul Burton 
10346490b57SMarkos Chandras 		ret = kernel_read(elf, phdr32->p_offset,
10446490b57SMarkos Chandras 				  (char *)&abiflags,
10590cee759SPaul Burton 				  sizeof(abiflags));
10646490b57SMarkos Chandras 	} else {
10746490b57SMarkos Chandras 		/* FR=1 is really the only option for 64-bit */
10846490b57SMarkos Chandras 		state->overall_fp_mode = FP_FR1;
10946490b57SMarkos Chandras 
11046490b57SMarkos Chandras 		if (phdr64->p_type != PT_MIPS_ABIFLAGS)
11146490b57SMarkos Chandras 			return 0;
11246490b57SMarkos Chandras 		if (phdr64->p_filesz < sizeof(abiflags))
11346490b57SMarkos Chandras 			return -EINVAL;
11446490b57SMarkos Chandras 
11546490b57SMarkos Chandras 		ret = kernel_read(elf, phdr64->p_offset,
11646490b57SMarkos Chandras 				  (char *)&abiflags,
11746490b57SMarkos Chandras 				  sizeof(abiflags));
11846490b57SMarkos Chandras 	}
11946490b57SMarkos Chandras 
12090cee759SPaul Burton 	if (ret < 0)
12190cee759SPaul Burton 		return ret;
12290cee759SPaul Burton 	if (ret != sizeof(abiflags))
12390cee759SPaul Burton 		return -EIO;
12490cee759SPaul Burton 
12590cee759SPaul Burton 	/* Record the required FP ABIs for use by mips_check_elf */
12690cee759SPaul Burton 	if (is_interp)
12790cee759SPaul Burton 		state->interp_fp_abi = abiflags.fp_abi;
12890cee759SPaul Burton 	else
12990cee759SPaul Burton 		state->fp_abi = abiflags.fp_abi;
13090cee759SPaul Burton 
13190cee759SPaul Burton 	return 0;
13290cee759SPaul Burton }
13390cee759SPaul Burton 
13490cee759SPaul Burton int arch_check_elf(void *_ehdr, bool has_interpreter,
13590cee759SPaul Burton 		   struct arch_elf_state *state)
13690cee759SPaul Burton {
13746490b57SMarkos Chandras 	struct elf32_hdr *ehdr = _ehdr;
13846490b57SMarkos Chandras 	struct mode_req prog_req, interp_req;
13946490b57SMarkos Chandras 	int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
14090cee759SPaul Burton 
14146490b57SMarkos Chandras 	if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
14290cee759SPaul Burton 		return 0;
14390cee759SPaul Burton 
144*a49dc427SMaciej W. Rozycki 	fp_abi = state->fp_abi;
14590cee759SPaul Burton 
14690cee759SPaul Burton 	if (has_interpreter) {
147*a49dc427SMaciej W. Rozycki 		interp_fp_abi = state->interp_fp_abi;
14890cee759SPaul Burton 
14990cee759SPaul Burton 		abi0 = min(fp_abi, interp_fp_abi);
15090cee759SPaul Burton 		abi1 = max(fp_abi, interp_fp_abi);
15190cee759SPaul Burton 	} else {
15290cee759SPaul Burton 		abi0 = abi1 = fp_abi;
15390cee759SPaul Burton 	}
15490cee759SPaul Burton 
15546490b57SMarkos Chandras 	/* ABI limits. O32 = FP_64A, N32/N64 = FP_SOFT */
15646490b57SMarkos Chandras 	max_abi = ((ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
15746490b57SMarkos Chandras 		   (!(ehdr->e_flags & EF_MIPS_ABI2))) ?
15846490b57SMarkos Chandras 		MIPS_ABI_FP_64A : MIPS_ABI_FP_SOFT;
15990cee759SPaul Burton 
16046490b57SMarkos Chandras 	if ((abi0 > max_abi && abi0 != MIPS_ABI_FP_UNKNOWN) ||
16146490b57SMarkos Chandras 	    (abi1 > max_abi && abi1 != MIPS_ABI_FP_UNKNOWN))
16290cee759SPaul Burton 		return -ELIBBAD;
16390cee759SPaul Burton 
16446490b57SMarkos Chandras 	/* It's time to determine the FPU mode requirements */
16546490b57SMarkos Chandras 	prog_req = (abi0 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
16646490b57SMarkos Chandras 	interp_req = (abi1 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
16746490b57SMarkos Chandras 
16846490b57SMarkos Chandras 	/*
16946490b57SMarkos Chandras 	 * Check whether the program's and interp's ABIs have a matching FPU
17046490b57SMarkos Chandras 	 * mode requirement.
17146490b57SMarkos Chandras 	 */
17246490b57SMarkos Chandras 	prog_req.single = interp_req.single && prog_req.single;
17346490b57SMarkos Chandras 	prog_req.soft = interp_req.soft && prog_req.soft;
17446490b57SMarkos Chandras 	prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
17546490b57SMarkos Chandras 	prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
17646490b57SMarkos Chandras 	prog_req.fre = interp_req.fre && prog_req.fre;
17746490b57SMarkos Chandras 
17846490b57SMarkos Chandras 	/*
17946490b57SMarkos Chandras 	 * Determine the desired FPU mode
18046490b57SMarkos Chandras 	 *
18146490b57SMarkos Chandras 	 * Decision making:
18246490b57SMarkos Chandras 	 *
18346490b57SMarkos Chandras 	 * - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
18446490b57SMarkos Chandras 	 *   means that we have a combination of program and interpreter
18546490b57SMarkos Chandras 	 *   that inherently require the hybrid FP mode.
18646490b57SMarkos Chandras 	 * - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
18746490b57SMarkos Chandras 	 *   fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
18846490b57SMarkos Chandras 	 *   instructions so we don't care about the mode. We will simply use
18946490b57SMarkos Chandras 	 *   the one preferred by the hardware. In fpxx case, that ABI can
19046490b57SMarkos Chandras 	 *   handle both FR=1 and FR=0, so, again, we simply choose the one
19146490b57SMarkos Chandras 	 *   preferred by the hardware. Next, if we only use single-precision
19246490b57SMarkos Chandras 	 *   FPU instructions, and the default ABI FPU mode is not good
19346490b57SMarkos Chandras 	 *   (ie single + any ABI combination), we set again the FPU mode to the
19446490b57SMarkos Chandras 	 *   one is preferred by the hardware. Next, if we know that the code
19546490b57SMarkos Chandras 	 *   will only use single-precision instructions, shown by single being
19646490b57SMarkos Chandras 	 *   true but frdefault being false, then we again set the FPU mode to
19746490b57SMarkos Chandras 	 *   the one that is preferred by the hardware.
19846490b57SMarkos Chandras 	 * - We want FP_FR1 if that's the only matching mode and the default one
19946490b57SMarkos Chandras 	 *   is not good.
20046490b57SMarkos Chandras 	 * - Return with -ELIBADD if we can't find a matching FPU mode.
20146490b57SMarkos Chandras 	 */
20246490b57SMarkos Chandras 	if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
20346490b57SMarkos Chandras 		state->overall_fp_mode = FP_FRE;
20446490b57SMarkos Chandras 	else if ((prog_req.fr1 && prog_req.frdefault) ||
20546490b57SMarkos Chandras 		 (prog_req.single && !prog_req.frdefault))
20646490b57SMarkos Chandras 		/* Make sure 64-bit MIPS III/IV/64R1 will not pick FR1 */
20746490b57SMarkos Chandras 		state->overall_fp_mode = ((current_cpu_data.fpu_id & MIPS_FPIR_F64) &&
20846490b57SMarkos Chandras 					  cpu_has_mips_r2_r6) ?
20946490b57SMarkos Chandras 					  FP_FR1 : FP_FR0;
21046490b57SMarkos Chandras 	else if (prog_req.fr1)
21146490b57SMarkos Chandras 		state->overall_fp_mode = FP_FR1;
21246490b57SMarkos Chandras 	else  if (!prog_req.fre && !prog_req.frdefault &&
21346490b57SMarkos Chandras 		  !prog_req.fr1 && !prog_req.single && !prog_req.soft)
21490cee759SPaul Burton 		return -ELIBBAD;
21590cee759SPaul Burton 
21690cee759SPaul Burton 	return 0;
21790cee759SPaul Burton }
21890cee759SPaul Burton 
21946490b57SMarkos Chandras static inline void set_thread_fp_mode(int hybrid, int regs32)
22090cee759SPaul Burton {
22146490b57SMarkos Chandras 	if (hybrid)
222f4af6fb2SPaul Burton 		set_thread_flag(TIF_HYBRID_FPREGS);
22346490b57SMarkos Chandras 	else
22490cee759SPaul Burton 		clear_thread_flag(TIF_HYBRID_FPREGS);
22546490b57SMarkos Chandras 	if (regs32)
22690cee759SPaul Burton 		set_thread_flag(TIF_32BIT_FPREGS);
22790cee759SPaul Burton 	else
22890cee759SPaul Burton 		clear_thread_flag(TIF_32BIT_FPREGS);
22946490b57SMarkos Chandras }
23090cee759SPaul Burton 
23146490b57SMarkos Chandras void mips_set_personality_fp(struct arch_elf_state *state)
23246490b57SMarkos Chandras {
23346490b57SMarkos Chandras 	/*
23446490b57SMarkos Chandras 	 * This function is only ever called for O32 ELFs so we should
23546490b57SMarkos Chandras 	 * not be worried about N32/N64 binaries.
23646490b57SMarkos Chandras 	 */
23746490b57SMarkos Chandras 
23846490b57SMarkos Chandras 	if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
23946490b57SMarkos Chandras 		return;
24046490b57SMarkos Chandras 
24146490b57SMarkos Chandras 	switch (state->overall_fp_mode) {
24246490b57SMarkos Chandras 	case FP_FRE:
24346490b57SMarkos Chandras 		set_thread_fp_mode(1, 0);
24490cee759SPaul Burton 		break;
24546490b57SMarkos Chandras 	case FP_FR0:
24646490b57SMarkos Chandras 		set_thread_fp_mode(0, 1);
24746490b57SMarkos Chandras 		break;
24846490b57SMarkos Chandras 	case FP_FR1:
24946490b57SMarkos Chandras 		set_thread_fp_mode(0, 0);
25046490b57SMarkos Chandras 		break;
25190cee759SPaul Burton 	default:
25290cee759SPaul Burton 		BUG();
25390cee759SPaul Burton 	}
25490cee759SPaul Burton }
255