858605639965fcbe79278e6744195be05784766c
[linux-drm-fsl-dcu.git] / arch / mips / kernel / elf.c
1 /*
2  * Copyright (C) 2014 Imagination Technologies
3  * Author: Paul Burton <paul.burton@imgtec.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation;  either version 2 of the  License, or (at your
8  * option) any later version.
9  */
10
11 #include <linux/elf.h>
12 #include <linux/sched.h>
13
14 /* FPU modes */
15 enum {
16         FP_FRE,
17         FP_FR0,
18         FP_FR1,
19 };
20
21 /**
22  * struct mode_req - ABI FPU mode requirements
23  * @single:     The program being loaded needs an FPU but it will only issue
24  *              single precision instructions meaning that it can execute in
25  *              either FR0 or FR1.
26  * @soft:       The soft(-float) requirement means that the program being
27  *              loaded needs has no FPU dependency at all (i.e. it has no
28  *              FPU instructions).
29  * @fr1:        The program being loaded depends on FPU being in FR=1 mode.
30  * @frdefault:  The program being loaded depends on the default FPU mode.
31  *              That is FR0 for O32 and FR1 for N32/N64.
32  * @fre:        The program being loaded depends on FPU with FRE=1. This mode is
33  *              a bridge which uses FR=1 whilst still being able to maintain
34  *              full compatibility with pre-existing code using the O32 FP32
35  *              ABI.
36  *
37  * More information about the FP ABIs can be found here:
38  *
39  * https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking#10.4.1._Basic_mode_set-up
40  *
41  */
42
43 struct mode_req {
44         bool single;
45         bool soft;
46         bool fr1;
47         bool frdefault;
48         bool fre;
49 };
50
51 static const struct mode_req fpu_reqs[] = {
52         [MIPS_ABI_FP_ANY]    = { true,  true,  true,  true,  true  },
53         [MIPS_ABI_FP_DOUBLE] = { false, false, false, true,  true  },
54         [MIPS_ABI_FP_SINGLE] = { true,  false, false, false, false },
55         [MIPS_ABI_FP_SOFT]   = { false, true,  false, false, false },
56         [MIPS_ABI_FP_OLD_64] = { false, false, false, false, false },
57         [MIPS_ABI_FP_XX]     = { false, false, true,  true,  true  },
58         [MIPS_ABI_FP_64]     = { false, false, true,  false, false },
59         [MIPS_ABI_FP_64A]    = { false, false, true,  false, true  }
60 };
61
62 /*
63  * Mode requirements when .MIPS.abiflags is not present in the ELF.
64  * Not present means that everything is acceptable except FR1.
65  */
66 static struct mode_req none_req = { true, true, false, true, true };
67
68 int arch_elf_pt_proc(void *_ehdr, void *_phdr, struct file *elf,
69                      bool is_interp, struct arch_elf_state *state)
70 {
71         union {
72                 struct elf32_hdr e32;
73                 struct elf64_hdr e64;
74         } *ehdr = _ehdr;
75         struct elf32_phdr *phdr32 = _phdr;
76         struct elf64_phdr *phdr64 = _phdr;
77         struct mips_elf_abiflags_v0 abiflags;
78         bool elf32;
79         u32 flags;
80         int ret;
81
82         elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
83         flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
84
85         /* Lets see if this is an O32 ELF */
86         if (elf32) {
87                 if (flags & EF_MIPS_FP64) {
88                         /*
89                          * Set MIPS_ABI_FP_OLD_64 for EF_MIPS_FP64. We will override it
90                          * later if needed
91                          */
92                         if (is_interp)
93                                 state->interp_fp_abi = MIPS_ABI_FP_OLD_64;
94                         else
95                                 state->fp_abi = MIPS_ABI_FP_OLD_64;
96                 }
97                 if (phdr32->p_type != PT_MIPS_ABIFLAGS)
98                         return 0;
99
100                 if (phdr32->p_filesz < sizeof(abiflags))
101                         return -EINVAL;
102
103                 ret = kernel_read(elf, phdr32->p_offset,
104                                   (char *)&abiflags,
105                                   sizeof(abiflags));
106         } else {
107                 if (phdr64->p_type != PT_MIPS_ABIFLAGS)
108                         return 0;
109                 if (phdr64->p_filesz < sizeof(abiflags))
110                         return -EINVAL;
111
112                 ret = kernel_read(elf, phdr64->p_offset,
113                                   (char *)&abiflags,
114                                   sizeof(abiflags));
115         }
116
117         if (ret < 0)
118                 return ret;
119         if (ret != sizeof(abiflags))
120                 return -EIO;
121
122         /* Record the required FP ABIs for use by mips_check_elf */
123         if (is_interp)
124                 state->interp_fp_abi = abiflags.fp_abi;
125         else
126                 state->fp_abi = abiflags.fp_abi;
127
128         return 0;
129 }
130
131 int arch_check_elf(void *_ehdr, bool has_interpreter,
132                    struct arch_elf_state *state)
133 {
134         union {
135                 struct elf32_hdr e32;
136                 struct elf64_hdr e64;
137         } *ehdr = _ehdr;
138         struct mode_req prog_req, interp_req;
139         int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
140         bool elf32;
141         u32 flags;
142
143         elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
144         flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
145
146         if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
147                 return 0;
148
149         fp_abi = state->fp_abi;
150
151         if (has_interpreter) {
152                 interp_fp_abi = state->interp_fp_abi;
153
154                 abi0 = min(fp_abi, interp_fp_abi);
155                 abi1 = max(fp_abi, interp_fp_abi);
156         } else {
157                 abi0 = abi1 = fp_abi;
158         }
159
160         if (elf32 && !(flags & EF_MIPS_ABI2)) {
161                 /* Default to a mode capable of running code expecting FR=0 */
162                 state->overall_fp_mode = cpu_has_mips_r6 ? FP_FRE : FP_FR0;
163
164                 /* Allow all ABIs we know about */
165                 max_abi = MIPS_ABI_FP_64A;
166         } else {
167                 /* MIPS64 code always uses FR=1, thus the default is easy */
168                 state->overall_fp_mode = FP_FR1;
169
170                 /* Disallow access to the various FPXX & FP64 ABIs */
171                 max_abi = MIPS_ABI_FP_SOFT;
172         }
173
174         if ((abi0 > max_abi && abi0 != MIPS_ABI_FP_UNKNOWN) ||
175             (abi1 > max_abi && abi1 != MIPS_ABI_FP_UNKNOWN))
176                 return -ELIBBAD;
177
178         /* It's time to determine the FPU mode requirements */
179         prog_req = (abi0 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi0];
180         interp_req = (abi1 == MIPS_ABI_FP_UNKNOWN) ? none_req : fpu_reqs[abi1];
181
182         /*
183          * Check whether the program's and interp's ABIs have a matching FPU
184          * mode requirement.
185          */
186         prog_req.single = interp_req.single && prog_req.single;
187         prog_req.soft = interp_req.soft && prog_req.soft;
188         prog_req.fr1 = interp_req.fr1 && prog_req.fr1;
189         prog_req.frdefault = interp_req.frdefault && prog_req.frdefault;
190         prog_req.fre = interp_req.fre && prog_req.fre;
191
192         /*
193          * Determine the desired FPU mode
194          *
195          * Decision making:
196          *
197          * - We want FR_FRE if FRE=1 and both FR=1 and FR=0 are false. This
198          *   means that we have a combination of program and interpreter
199          *   that inherently require the hybrid FP mode.
200          * - If FR1 and FRDEFAULT is true, that means we hit the any-abi or
201          *   fpxx case. This is because, in any-ABI (or no-ABI) we have no FPU
202          *   instructions so we don't care about the mode. We will simply use
203          *   the one preferred by the hardware. In fpxx case, that ABI can
204          *   handle both FR=1 and FR=0, so, again, we simply choose the one
205          *   preferred by the hardware. Next, if we only use single-precision
206          *   FPU instructions, and the default ABI FPU mode is not good
207          *   (ie single + any ABI combination), we set again the FPU mode to the
208          *   one is preferred by the hardware. Next, if we know that the code
209          *   will only use single-precision instructions, shown by single being
210          *   true but frdefault being false, then we again set the FPU mode to
211          *   the one that is preferred by the hardware.
212          * - We want FP_FR1 if that's the only matching mode and the default one
213          *   is not good.
214          * - Return with -ELIBADD if we can't find a matching FPU mode.
215          */
216         if (prog_req.fre && !prog_req.frdefault && !prog_req.fr1)
217                 state->overall_fp_mode = FP_FRE;
218         else if ((prog_req.fr1 && prog_req.frdefault) ||
219                  (prog_req.single && !prog_req.frdefault))
220                 /* Make sure 64-bit MIPS III/IV/64R1 will not pick FR1 */
221                 state->overall_fp_mode = ((current_cpu_data.fpu_id & MIPS_FPIR_F64) &&
222                                           cpu_has_mips_r2_r6) ?
223                                           FP_FR1 : FP_FR0;
224         else if (prog_req.fr1)
225                 state->overall_fp_mode = FP_FR1;
226         else  if (!prog_req.fre && !prog_req.frdefault &&
227                   !prog_req.fr1 && !prog_req.single && !prog_req.soft)
228                 return -ELIBBAD;
229
230         return 0;
231 }
232
233 static inline void set_thread_fp_mode(int hybrid, int regs32)
234 {
235         if (hybrid)
236                 set_thread_flag(TIF_HYBRID_FPREGS);
237         else
238                 clear_thread_flag(TIF_HYBRID_FPREGS);
239         if (regs32)
240                 set_thread_flag(TIF_32BIT_FPREGS);
241         else
242                 clear_thread_flag(TIF_32BIT_FPREGS);
243 }
244
245 void mips_set_personality_fp(struct arch_elf_state *state)
246 {
247         /*
248          * This function is only ever called for O32 ELFs so we should
249          * not be worried about N32/N64 binaries.
250          */
251
252         if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
253                 return;
254
255         switch (state->overall_fp_mode) {
256         case FP_FRE:
257                 set_thread_fp_mode(1, 0);
258                 break;
259         case FP_FR0:
260                 set_thread_fp_mode(0, 1);
261                 break;
262         case FP_FR1:
263                 set_thread_fp_mode(0, 0);
264                 break;
265         default:
266                 BUG();
267         }
268 }