Skip to content

Commit 2b5e869

Browse files
Maciej W. Rozyckiralfbaechle
authored andcommitted
MIPS: ELF: Interpret the NAN2008 file header flag
Handle the EF_MIPS_NAN2008 ELF file header flag and refuse execution where there is no support in the FPU for the NaN encoding mode requested by a binary invoked. Ensure that the setting of the bit in the binary matches one in any intepreter used. Set the thread's initial FCSR contents according to the value of the EF_MIPS_NAN2008. Set the values of the FCSR ABS2008 and NAN2008 bits both to the same value if possible, to take the approach taken with existing FPU hardware into account. As of now all implementations have both bits hardwired to the same value, that is both are fixed at 0 or both are fixed at 1, even though the architecture allows for implementations where the amount of control implemented with each of these two individual bits is independent of each other. Signed-off-by: Maciej W. Rozycki <macro@imgtec.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Matthew Fortune <Matthew.Fortune@imgtec.com> Cc: linux-mips@linux-mips.org Cc: linux-kernel@vger.kernel.org Patchwork: https://patchwork.linux-mips.org/patch/11479/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent eb4bc07 commit 2b5e869

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎arch/mips/include/asm/elf.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <linux/fs.h>
1313
#include <uapi/linux/elf.h>
1414

15-
#include <asm/cpu-info.h>
1615
#include <asm/current.h>
1716

1817
/* ELF header e_flags defines. */
@@ -44,6 +43,7 @@
4443
#define EF_MIPS_OPTIONS_FIRST 0x00000080
4544
#define EF_MIPS_32BITMODE 0x00000100
4645
#define EF_MIPS_FP64 0x00000200
46+
#define EF_MIPS_NAN2008 0x00000400
4747
#define EF_MIPS_ABI 0x0000f000
4848
#define EF_MIPS_ARCH 0xf0000000
4949

@@ -305,7 +305,7 @@ do { \
305305
\
306306
current->thread.abi = &mips_abi; \
307307
\
308-
current->thread.fpu.fcr31 = boot_cpu_data.fpu_csr31; \
308+
mips_set_personality_nan(state); \
309309
} while (0)
310310

311311
#endif /* CONFIG_32BIT */
@@ -367,7 +367,7 @@ do { \
367367
else \
368368
current->thread.abi = &mips_abi; \
369369
\
370-
current->thread.fpu.fcr31 = boot_cpu_data.fpu_csr31; \
370+
mips_set_personality_nan(state); \
371371
\
372372
p = personality(current->personality); \
373373
if (p != PER_LINUX32 && p != PER_LINUX) \
@@ -432,6 +432,7 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm,
432432
int uses_interp);
433433

434434
struct arch_elf_state {
435+
int nan_2008;
435436
int fp_abi;
436437
int interp_fp_abi;
437438
int overall_fp_mode;
@@ -440,6 +441,7 @@ struct arch_elf_state {
440441
#define MIPS_ABI_FP_UNKNOWN (-1) /* Unknown FP ABI (kernel internal) */
441442

442443
#define INIT_ARCH_ELF_STATE { \
444+
.nan_2008 = -1, \
443445
.fp_abi = MIPS_ABI_FP_UNKNOWN, \
444446
.interp_fp_abi = MIPS_ABI_FP_UNKNOWN, \
445447
.overall_fp_mode = -1, \
@@ -451,6 +453,7 @@ extern int arch_elf_pt_proc(void *ehdr, void *phdr, struct file *elf,
451453
extern int arch_check_elf(void *ehdr, bool has_interpreter, void *interp_ehdr,
452454
struct arch_elf_state *state);
453455

456+
extern void mips_set_personality_nan(struct arch_elf_state *state);
454457
extern void mips_set_personality_fp(struct arch_elf_state *state);
455458

456459
#endif /* _ASM_ELF_H */

‎arch/mips/kernel/elf.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <linux/elf.h>
1212
#include <linux/sched.h>
1313

14+
#include <asm/cpu-info.h>
15+
1416
/* FPU modes */
1517
enum {
1618
FP_FRE,
@@ -135,6 +137,10 @@ int arch_check_elf(void *_ehdr, bool has_interpreter, void *_interp_ehdr,
135137
struct elf32_hdr e32;
136138
struct elf64_hdr e64;
137139
} *ehdr = _ehdr;
140+
union {
141+
struct elf32_hdr e32;
142+
struct elf64_hdr e64;
143+
} *iehdr = _interp_ehdr;
138144
struct mode_req prog_req, interp_req;
139145
int fp_abi, interp_fp_abi, abi0, abi1, max_abi;
140146
bool elf32;
@@ -143,6 +149,32 @@ int arch_check_elf(void *_ehdr, bool has_interpreter, void *_interp_ehdr,
143149
elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
144150
flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
145151

152+
/*
153+
* Determine the NaN personality, reject the binary if no hardware
154+
* support. Also ensure that any interpreter matches the executable.
155+
*/
156+
if (flags & EF_MIPS_NAN2008) {
157+
if (cpu_has_nan_2008)
158+
state->nan_2008 = 1;
159+
else
160+
return -ENOEXEC;
161+
} else {
162+
if (cpu_has_nan_legacy)
163+
state->nan_2008 = 0;
164+
else
165+
return -ENOEXEC;
166+
}
167+
if (has_interpreter) {
168+
bool ielf32;
169+
u32 iflags;
170+
171+
ielf32 = iehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
172+
iflags = ielf32 ? iehdr->e32.e_flags : iehdr->e64.e_flags;
173+
174+
if ((flags ^ iflags) & EF_MIPS_NAN2008)
175+
return -ELIBBAD;
176+
}
177+
146178
if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
147179
return 0;
148180

@@ -266,3 +298,27 @@ void mips_set_personality_fp(struct arch_elf_state *state)
266298
BUG();
267299
}
268300
}
301+
302+
/*
303+
* Select the IEEE 754 NaN encoding and ABS.fmt/NEG.fmt execution mode
304+
* in FCSR according to the ELF NaN personality.
305+
*/
306+
void mips_set_personality_nan(struct arch_elf_state *state)
307+
{
308+
struct cpuinfo_mips *c = &boot_cpu_data;
309+
struct task_struct *t = current;
310+
311+
t->thread.fpu.fcr31 = c->fpu_csr31;
312+
switch (state->nan_2008) {
313+
case 0:
314+
break;
315+
case 1:
316+
if (!(c->fpu_msk31 & FPU_CSR_NAN2008))
317+
t->thread.fpu.fcr31 |= FPU_CSR_NAN2008;
318+
if (!(c->fpu_msk31 & FPU_CSR_ABS2008))
319+
t->thread.fpu.fcr31 |= FPU_CSR_ABS2008;
320+
break;
321+
default:
322+
BUG();
323+
}
324+
}

0 commit comments

Comments
 (0)