Skip to content

Commit 503943e

Browse files
Maciej W. Rozyckiralfbaechle
authored andcommitted
MIPS: Add IEEE Std 754 conformance mode selection
Add an `ieee754=' kernel parameter to control IEEE Std 754 conformance mode. Use separate flags copied from the respective CPU feature flags, and adjusted according to the conformance mode selected, to make binaries requesting individual NaN encoding modes accepted or rejected as needed. Update the initial setting for FCSR and, in the full FPU emulation mode, its read-only mask accordingly. Accept the mode selection requested for legacy processors as well. As with the EF_MIPS_NAN2008 ELF file header flag adjust both ABS2008 and NAN2008 bits at the same time, to match the choice made for hardware currently implemented. 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/11481/ Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
1 parent 93adeaf commit 503943e

4 files changed

Lines changed: 142 additions & 14 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,41 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
14361436
In such case C2/C3 won't be used again.
14371437
idle=nomwait: Disable mwait for CPU C-states
14381438

1439+
ieee754= [MIPS] Select IEEE Std 754 conformance mode
1440+
Format: { strict | legacy | 2008 | relaxed }
1441+
Default: strict
1442+
1443+
Choose which programs will be accepted for execution
1444+
based on the IEEE 754 NaN encoding(s) supported by
1445+
the FPU and the NaN encoding requested with the value
1446+
of an ELF file header flag individually set by each
1447+
binary. Hardware implementations are permitted to
1448+
support either or both of the legacy and the 2008 NaN
1449+
encoding mode.
1450+
1451+
Available settings are as follows:
1452+
strict accept binaries that request a NaN encoding
1453+
supported by the FPU
1454+
legacy only accept legacy-NaN binaries, if supported
1455+
by the FPU
1456+
2008 only accept 2008-NaN binaries, if supported
1457+
by the FPU
1458+
relaxed accept any binaries regardless of whether
1459+
supported by the FPU
1460+
1461+
The FPU emulator is always able to support both NaN
1462+
encodings, so if no FPU hardware is present or it has
1463+
been disabled with 'nofpu', then the settings of
1464+
'legacy' and '2008' strap the emulator accordingly,
1465+
'relaxed' straps the emulator for both legacy-NaN and
1466+
2008-NaN, whereas 'strict' enables legacy-NaN only on
1467+
legacy processors and both NaN encodings on MIPS32 or
1468+
MIPS64 CPUs.
1469+
1470+
The setting for ABS.fmt/NEG.fmt instruction execution
1471+
mode generally follows that for the NaN encoding,
1472+
except where unsupported by hardware.
1473+
14391474
ignore_loglevel [KNL]
14401475
Ignore loglevel setting - this will print /all/
14411476
kernel messages to the console. Useful for debugging.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,10 @@ struct arch_elf_state {
447447
.overall_fp_mode = -1, \
448448
}
449449

450+
/* Whether to accept legacy-NaN and 2008-NaN user binaries. */
451+
extern bool mips_use_nan_legacy;
452+
extern bool mips_use_nan_2008;
453+
450454
extern int arch_elf_pt_proc(void *ehdr, void *phdr, struct file *elf,
451455
bool is_interp, struct arch_elf_state *state);
452456

‎arch/mips/kernel/cpu-probe.c‎

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,25 +151,108 @@ static void cpu_set_fpu_2008(struct cpuinfo_mips *c)
151151
}
152152

153153
/*
154-
* Set the IEEE 754 NaN encodings and ABS.fmt/NEG.fmt execution modes
155-
* for the FPU emulator. Clear the flags where required in case called
156-
* from `fpu_disable', to override details obtained from FPU hardware.
154+
* IEEE 754 conformance mode to use. Affects the NaN encoding and the
155+
* ABS.fmt/NEG.fmt execution mode.
156+
*/
157+
static enum { STRICT, LEGACY, STD2008, RELAXED } ieee754 = STRICT;
158+
159+
/*
160+
* Set the IEEE 754 NaN encodings and the ABS.fmt/NEG.fmt execution modes
161+
* to support by the FPU emulator according to the IEEE 754 conformance
162+
* mode selected. Note that "relaxed" straps the emulator so that it
163+
* allows 2008-NaN binaries even for legacy processors.
157164
*/
158165
static void cpu_set_nofpu_2008(struct cpuinfo_mips *c)
159166
{
167+
c->options &= ~(MIPS_CPU_NAN_2008 | MIPS_CPU_NAN_LEGACY);
160168
c->fpu_csr31 &= ~(FPU_CSR_ABS2008 | FPU_CSR_NAN2008);
161-
if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M64R1 |
162-
MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
163-
MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6)) {
164-
c->options |= MIPS_CPU_NAN_2008 | MIPS_CPU_NAN_LEGACY;
165-
c->fpu_msk31 &= ~(FPU_CSR_ABS2008 | FPU_CSR_NAN2008);
166-
} else {
167-
c->options &= ~MIPS_CPU_NAN_2008;
169+
c->fpu_msk31 &= ~(FPU_CSR_ABS2008 | FPU_CSR_NAN2008);
170+
171+
switch (ieee754) {
172+
case STRICT:
173+
if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M64R1 |
174+
MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
175+
MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6)) {
176+
c->options |= MIPS_CPU_NAN_2008 | MIPS_CPU_NAN_LEGACY;
177+
} else {
178+
c->options |= MIPS_CPU_NAN_LEGACY;
179+
c->fpu_msk31 |= FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
180+
}
181+
break;
182+
case LEGACY:
168183
c->options |= MIPS_CPU_NAN_LEGACY;
169184
c->fpu_msk31 |= FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
185+
break;
186+
case STD2008:
187+
c->options |= MIPS_CPU_NAN_2008;
188+
c->fpu_csr31 |= FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
189+
c->fpu_msk31 |= FPU_CSR_ABS2008 | FPU_CSR_NAN2008;
190+
break;
191+
case RELAXED:
192+
c->options |= MIPS_CPU_NAN_2008 | MIPS_CPU_NAN_LEGACY;
193+
break;
194+
}
195+
}
196+
197+
/*
198+
* Override the IEEE 754 NaN encoding and ABS.fmt/NEG.fmt execution mode
199+
* according to the "ieee754=" parameter.
200+
*/
201+
static void cpu_set_nan_2008(struct cpuinfo_mips *c)
202+
{
203+
switch (ieee754) {
204+
case STRICT:
205+
mips_use_nan_legacy = !!cpu_has_nan_legacy;
206+
mips_use_nan_2008 = !!cpu_has_nan_2008;
207+
break;
208+
case LEGACY:
209+
mips_use_nan_legacy = !!cpu_has_nan_legacy;
210+
mips_use_nan_2008 = !cpu_has_nan_legacy;
211+
break;
212+
case STD2008:
213+
mips_use_nan_legacy = !cpu_has_nan_2008;
214+
mips_use_nan_2008 = !!cpu_has_nan_2008;
215+
break;
216+
case RELAXED:
217+
mips_use_nan_legacy = true;
218+
mips_use_nan_2008 = true;
219+
break;
170220
}
171221
}
172222

223+
/*
224+
* IEEE 754 NaN encoding and ABS.fmt/NEG.fmt execution mode override
225+
* settings:
226+
*
227+
* strict: accept binaries that request a NaN encoding supported by the FPU
228+
* legacy: only accept legacy-NaN binaries
229+
* 2008: only accept 2008-NaN binaries
230+
* relaxed: accept any binaries regardless of whether supported by the FPU
231+
*/
232+
static int __init ieee754_setup(char *s)
233+
{
234+
if (!s)
235+
return -1;
236+
else if (!strcmp(s, "strict"))
237+
ieee754 = STRICT;
238+
else if (!strcmp(s, "legacy"))
239+
ieee754 = LEGACY;
240+
else if (!strcmp(s, "2008"))
241+
ieee754 = STD2008;
242+
else if (!strcmp(s, "relaxed"))
243+
ieee754 = RELAXED;
244+
else
245+
return -1;
246+
247+
if (!(boot_cpu_data.options & MIPS_CPU_FPU))
248+
cpu_set_nofpu_2008(&boot_cpu_data);
249+
cpu_set_nan_2008(&boot_cpu_data);
250+
251+
return 0;
252+
}
253+
254+
early_param("ieee754", ieee754_setup);
255+
173256
/*
174257
* Set the FIR feature flags for the FPU emulator.
175258
*/
@@ -212,6 +295,7 @@ static void cpu_set_fpu_opts(struct cpuinfo_mips *c)
212295

213296
cpu_set_fpu_fcsr_mask(c);
214297
cpu_set_fpu_2008(c);
298+
cpu_set_nan_2008(c);
215299
}
216300

217301
/*
@@ -223,6 +307,7 @@ static void cpu_set_nofpu_opts(struct cpuinfo_mips *c)
223307
c->fpu_msk31 = mips_nofpu_msk31;
224308

225309
cpu_set_nofpu_2008(c);
310+
cpu_set_nan_2008(c);
226311
cpu_set_nofpu_id(c);
227312
}
228313

‎arch/mips/kernel/elf.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
#include <asm/cpu-info.h>
1515

16+
/* Whether to accept legacy-NaN and 2008-NaN user binaries. */
17+
bool mips_use_nan_legacy;
18+
bool mips_use_nan_2008;
19+
1620
/* FPU modes */
1721
enum {
1822
FP_FRE,
@@ -150,16 +154,16 @@ int arch_check_elf(void *_ehdr, bool has_interpreter, void *_interp_ehdr,
150154
flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
151155

152156
/*
153-
* Determine the NaN personality, reject the binary if no hardware
154-
* support. Also ensure that any interpreter matches the executable.
157+
* Determine the NaN personality, reject the binary if not allowed.
158+
* Also ensure that any interpreter matches the executable.
155159
*/
156160
if (flags & EF_MIPS_NAN2008) {
157-
if (cpu_has_nan_2008)
161+
if (mips_use_nan_2008)
158162
state->nan_2008 = 1;
159163
else
160164
return -ENOEXEC;
161165
} else {
162-
if (cpu_has_nan_legacy)
166+
if (mips_use_nan_legacy)
163167
state->nan_2008 = 0;
164168
else
165169
return -ENOEXEC;

0 commit comments

Comments
 (0)