Skip to content

Commit 90cee75

Browse files
paulburtonralfbaechle
authored andcommitted
MIPS: ELF: Set FP mode according to .MIPS.abiflags
This patch reads the .MIPS.abiflags section when it is present, and sets the FP mode of the task accordingly. Any loaded ELF files which do not contain a .MIPS.abiflags section will continue to observe the previous behaviour, that is FR=1 if EF_MIPS_FP64 is set else FR=0. Signed-off-by: Paul Burton <paul.burton@imgtec.com> Cc: linux-mips@linux-mips.org Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: linux-fsdevel@vger.kernel.org Cc: linux-kernel@vger.kernel.org Patchwork: https://patchwork.linux-mips.org/patch/7681/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 6cd9622 commit 90cee75

4 files changed

Lines changed: 211 additions & 20 deletions

File tree

‎arch/mips/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ config MIPS
5353
select HAVE_CC_STACKPROTECTOR
5454
select CPU_PM if CPU_IDLE
5555
select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST
56+
select ARCH_BINFMT_ELF_STATE
5657

5758
menu "Machine selection"
5859

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

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef _ASM_ELF_H
99
#define _ASM_ELF_H
1010

11+
#include <linux/fs.h>
12+
#include <uapi/linux/elf.h>
1113

1214
/* ELF header e_flags defines. */
1315
/* MIPS architecture level. */
@@ -287,18 +289,13 @@ extern struct mips_abi mips_abi_n32;
287289

288290
#ifdef CONFIG_32BIT
289291

290-
#define SET_PERSONALITY(ex) \
292+
#define SET_PERSONALITY2(ex, state) \
291293
do { \
292-
if ((ex).e_flags & EF_MIPS_FP64) \
293-
clear_thread_flag(TIF_32BIT_FPREGS); \
294-
else \
295-
set_thread_flag(TIF_32BIT_FPREGS); \
296-
\
297-
clear_thread_flag(TIF_HYBRID_FPREGS); \
298-
\
299294
if (personality(current->personality) != PER_LINUX) \
300295
set_personality(PER_LINUX); \
301296
\
297+
mips_set_personality_fp(state); \
298+
\
302299
current->thread.abi = &mips_abi; \
303300
} while (0)
304301

@@ -318,35 +315,34 @@ do { \
318315
#endif
319316

320317
#ifdef CONFIG_MIPS32_O32
321-
#define __SET_PERSONALITY32_O32(ex) \
318+
#define __SET_PERSONALITY32_O32(ex, state) \
322319
do { \
323320
set_thread_flag(TIF_32BIT_REGS); \
324321
set_thread_flag(TIF_32BIT_ADDR); \
325322
\
326-
if (!((ex).e_flags & EF_MIPS_FP64)) \
327-
set_thread_flag(TIF_32BIT_FPREGS); \
323+
mips_set_personality_fp(state); \
328324
\
329325
current->thread.abi = &mips_abi_32; \
330326
} while (0)
331327
#else
332-
#define __SET_PERSONALITY32_O32(ex) \
328+
#define __SET_PERSONALITY32_O32(ex, state) \
333329
do { } while (0)
334330
#endif
335331

336332
#ifdef CONFIG_MIPS32_COMPAT
337-
#define __SET_PERSONALITY32(ex) \
333+
#define __SET_PERSONALITY32(ex, state) \
338334
do { \
339335
if ((((ex).e_flags & EF_MIPS_ABI2) != 0) && \
340336
((ex).e_flags & EF_MIPS_ABI) == 0) \
341337
__SET_PERSONALITY32_N32(); \
342338
else \
343-
__SET_PERSONALITY32_O32(ex); \
339+
__SET_PERSONALITY32_O32(ex, state); \
344340
} while (0)
345341
#else
346-
#define __SET_PERSONALITY32(ex) do { } while (0)
342+
#define __SET_PERSONALITY32(ex, state) do { } while (0)
347343
#endif
348344

349-
#define SET_PERSONALITY(ex) \
345+
#define SET_PERSONALITY2(ex, state) \
350346
do { \
351347
unsigned int p; \
352348
\
@@ -356,7 +352,7 @@ do { \
356352
clear_thread_flag(TIF_32BIT_ADDR); \
357353
\
358354
if ((ex).e_ident[EI_CLASS] == ELFCLASS32) \
359-
__SET_PERSONALITY32(ex); \
355+
__SET_PERSONALITY32(ex, state); \
360356
else \
361357
current->thread.abi = &mips_abi; \
362358
\
@@ -418,4 +414,24 @@ struct mm_struct;
418414
extern unsigned long arch_randomize_brk(struct mm_struct *mm);
419415
#define arch_randomize_brk arch_randomize_brk
420416

417+
struct arch_elf_state {
418+
int fp_abi;
419+
int interp_fp_abi;
420+
int overall_abi;
421+
};
422+
423+
#define INIT_ARCH_ELF_STATE { \
424+
.fp_abi = -1, \
425+
.interp_fp_abi = -1, \
426+
.overall_abi = -1, \
427+
}
428+
429+
extern int arch_elf_pt_proc(void *ehdr, void *phdr, struct file *elf,
430+
bool is_interp, struct arch_elf_state *state);
431+
432+
extern int arch_check_elf(void *ehdr, bool has_interpreter,
433+
struct arch_elf_state *state);
434+
435+
extern void mips_set_personality_fp(struct arch_elf_state *state);
436+
421437
#endif /* _ASM_ELF_H */

‎arch/mips/kernel/Makefile‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
extra-y := head.o vmlinux.lds
66

7-
obj-y += cpu-probe.o branch.o entry.o genex.o idle.o irq.o process.o \
8-
prom.o ptrace.o reset.o setup.o signal.o syscall.o \
9-
time.o topology.o traps.o unaligned.o watch.o vdso.o
7+
obj-y += cpu-probe.o branch.o elf.o entry.o genex.o idle.o irq.o \
8+
process.o prom.o ptrace.o reset.o setup.o signal.o \
9+
syscall.o time.o topology.o traps.o unaligned.o watch.o \
10+
vdso.o
1011

1112
ifdef CONFIG_FUNCTION_TRACER
1213
CFLAGS_REMOVE_ftrace.o = -pg

‎arch/mips/kernel/elf.c‎

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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+
enum {
15+
FP_ERROR = -1,
16+
FP_DOUBLE_64A = -2,
17+
};
18+
19+
int arch_elf_pt_proc(void *_ehdr, void *_phdr, struct file *elf,
20+
bool is_interp, struct arch_elf_state *state)
21+
{
22+
struct elfhdr *ehdr = _ehdr;
23+
struct elf_phdr *phdr = _phdr;
24+
struct mips_elf_abiflags_v0 abiflags;
25+
int ret;
26+
27+
if (config_enabled(CONFIG_64BIT) &&
28+
(ehdr->e_ident[EI_CLASS] != ELFCLASS32))
29+
return 0;
30+
if (phdr->p_type != PT_MIPS_ABIFLAGS)
31+
return 0;
32+
if (phdr->p_filesz < sizeof(abiflags))
33+
return -EINVAL;
34+
35+
ret = kernel_read(elf, phdr->p_offset, (char *)&abiflags,
36+
sizeof(abiflags));
37+
if (ret < 0)
38+
return ret;
39+
if (ret != sizeof(abiflags))
40+
return -EIO;
41+
42+
/* Record the required FP ABIs for use by mips_check_elf */
43+
if (is_interp)
44+
state->interp_fp_abi = abiflags.fp_abi;
45+
else
46+
state->fp_abi = abiflags.fp_abi;
47+
48+
return 0;
49+
}
50+
51+
static inline unsigned get_fp_abi(struct elfhdr *ehdr, int in_abi)
52+
{
53+
/* If the ABI requirement is provided, simply return that */
54+
if (in_abi != -1)
55+
return in_abi;
56+
57+
/* If the EF_MIPS_FP64 flag was set, return MIPS_ABI_FP_64 */
58+
if (ehdr->e_flags & EF_MIPS_FP64)
59+
return MIPS_ABI_FP_64;
60+
61+
/* Default to MIPS_ABI_FP_DOUBLE */
62+
return MIPS_ABI_FP_DOUBLE;
63+
}
64+
65+
int arch_check_elf(void *_ehdr, bool has_interpreter,
66+
struct arch_elf_state *state)
67+
{
68+
struct elfhdr *ehdr = _ehdr;
69+
unsigned fp_abi, interp_fp_abi, abi0, abi1;
70+
71+
/* Ignore non-O32 binaries */
72+
if (config_enabled(CONFIG_64BIT) &&
73+
(ehdr->e_ident[EI_CLASS] != ELFCLASS32))
74+
return 0;
75+
76+
fp_abi = get_fp_abi(ehdr, state->fp_abi);
77+
78+
if (has_interpreter) {
79+
interp_fp_abi = get_fp_abi(ehdr, state->interp_fp_abi);
80+
81+
abi0 = min(fp_abi, interp_fp_abi);
82+
abi1 = max(fp_abi, interp_fp_abi);
83+
} else {
84+
abi0 = abi1 = fp_abi;
85+
}
86+
87+
state->overall_abi = FP_ERROR;
88+
89+
if (abi0 == abi1) {
90+
state->overall_abi = abi0;
91+
} else if (abi0 == MIPS_ABI_FP_ANY) {
92+
state->overall_abi = abi1;
93+
} else if (abi0 == MIPS_ABI_FP_DOUBLE) {
94+
switch (abi1) {
95+
case MIPS_ABI_FP_XX:
96+
state->overall_abi = MIPS_ABI_FP_DOUBLE;
97+
break;
98+
99+
case MIPS_ABI_FP_64A:
100+
state->overall_abi = FP_DOUBLE_64A;
101+
break;
102+
}
103+
} else if (abi0 == MIPS_ABI_FP_SINGLE ||
104+
abi0 == MIPS_ABI_FP_SOFT) {
105+
/* Cannot link with other ABIs */
106+
} else if (abi0 == MIPS_ABI_FP_OLD_64) {
107+
switch (abi1) {
108+
case MIPS_ABI_FP_XX:
109+
case MIPS_ABI_FP_64:
110+
case MIPS_ABI_FP_64A:
111+
state->overall_abi = MIPS_ABI_FP_64;
112+
break;
113+
}
114+
} else if (abi0 == MIPS_ABI_FP_XX ||
115+
abi0 == MIPS_ABI_FP_64 ||
116+
abi0 == MIPS_ABI_FP_64A) {
117+
state->overall_abi = MIPS_ABI_FP_64;
118+
}
119+
120+
switch (state->overall_abi) {
121+
case MIPS_ABI_FP_64:
122+
case MIPS_ABI_FP_64A:
123+
case FP_DOUBLE_64A:
124+
if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
125+
return -ELIBBAD;
126+
break;
127+
128+
case FP_ERROR:
129+
return -ELIBBAD;
130+
}
131+
132+
return 0;
133+
}
134+
135+
void mips_set_personality_fp(struct arch_elf_state *state)
136+
{
137+
switch (state->overall_abi) {
138+
case MIPS_ABI_FP_DOUBLE:
139+
case MIPS_ABI_FP_SINGLE:
140+
case MIPS_ABI_FP_SOFT:
141+
/* FR=0 */
142+
set_thread_flag(TIF_32BIT_FPREGS);
143+
clear_thread_flag(TIF_HYBRID_FPREGS);
144+
break;
145+
146+
case FP_DOUBLE_64A:
147+
/* FR=1, FRE=1 */
148+
clear_thread_flag(TIF_32BIT_FPREGS);
149+
set_thread_flag(TIF_HYBRID_FPREGS);
150+
break;
151+
152+
case MIPS_ABI_FP_64:
153+
case MIPS_ABI_FP_64A:
154+
/* FR=1, FRE=0 */
155+
clear_thread_flag(TIF_32BIT_FPREGS);
156+
clear_thread_flag(TIF_HYBRID_FPREGS);
157+
break;
158+
159+
case MIPS_ABI_FP_XX:
160+
case MIPS_ABI_FP_ANY:
161+
if (!config_enabled(CONFIG_MIPS_O32_FP64_SUPPORT))
162+
set_thread_flag(TIF_32BIT_FPREGS);
163+
else
164+
clear_thread_flag(TIF_32BIT_FPREGS);
165+
166+
clear_thread_flag(TIF_HYBRID_FPREGS);
167+
break;
168+
169+
default:
170+
case FP_ERROR:
171+
BUG();
172+
}
173+
}

0 commit comments

Comments
 (0)