Skip to content

Commit 1975ed4

Browse files
committed
MIPS: Ensure emulated FP sets PF_USED_MATH
Emulated floating point instructions don't ensure that the PF_USED_MATH flag is set for the task. This results in a couple of inconsistencies: - ptrace will return the default initial state of FP registers rather than the values actually stored in struct thread_struct, hiding state that has been updated by emulated floating point instructions. - If a task migrates to a CPU with an FPU after having emulated floating point instructions then its floating point register state will be reset to the default ~0 bit pattern, losing state from the emulated instructions. Fix this by calling init_fp_ctx() from fpu_emulator_cop1Handler() to consistently initialize FP state if it was previously uninitialized, setting the PF_USED_MATH flag in the process. All callers of fpu_emulator_cop1Handler() either call lose_fpu(1) before it in order to save any live FPU registers to struct thread_struct, or in the case of do_cpu() already know that the task does not own an FPU so lose_fpu(1) would be a no-op. Since we know that saving FP context will be unnecessary in the case where FP context was just initialized we move this call into fpu_emulator_cop1Handler() too, providing consistency & avoiding needless duplication. Calls to own_fpu(1) are common after return from fpu_emulator_cop1Handler() too, but this would not be a no-op in the do_cpu() case so these are left as-is. A potential future improvement could be to have fpu_emulator_cop1Handler() restore FPU state automatically only if it saved it, though this may not be optimal if some callers are better off without their current calls to own_fpu(1). One potential example of this could be mipsr2_decoder() which as-is could end up saving & restoring FP context repeatedly & unnecessarily if emulating multiple FP instructions. Signed-off-by: Paul Burton <paul.burton@mips.com> Patchwork: https://patchwork.linux-mips.org/patch/21003/ Cc: linux-mips@linux-mips.org
1 parent cc97ab2 commit 1975ed4

4 files changed

Lines changed: 7 additions & 10 deletions

File tree

‎arch/mips/kernel/mips-r2-to-r6-emul.c‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,9 +1175,6 @@ int mipsr2_decoder(struct pt_regs *regs, u32 inst, unsigned long *fcr31)
11751175
regs->regs[31] = r31;
11761176
regs->cp0_epc = epc;
11771177

1178-
if (!init_fp_ctx(current))
1179-
lose_fpu(1);
1180-
11811178
err = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 0,
11821179
&fault_addr);
11831180

‎arch/mips/kernel/traps.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,6 @@ static int simulate_fp(struct pt_regs *regs, unsigned int opcode,
794794
regs->cp0_epc = old_epc;
795795
regs->regs[31] = old_ra;
796796

797-
/* Save the FP context to struct thread_struct */
798-
lose_fpu(1);
799-
800797
/* Run the emulator */
801798
sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
802799
&fault_addr);
@@ -848,8 +845,6 @@ asmlinkage void do_fpe(struct pt_regs *regs, unsigned long fcr31)
848845
* register operands before invoking the emulator, which seems
849846
* a bit extreme for what should be an infrequent event.
850847
*/
851-
/* Ensure 'resume' not overwrite saved fp context again. */
852-
lose_fpu(1);
853848

854849
/* Run the emulator */
855850
sig = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,

‎arch/mips/kernel/unaligned.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,6 @@ static void emulate_load_store_insn(struct pt_regs *regs,
12201220
die_if_kernel("Unaligned FP access in kernel code", regs);
12211221
BUG_ON(!used_math());
12221222

1223-
lose_fpu(1); /* Save FPU state for the emulator. */
12241223
res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
12251224
&fault_addr);
12261225
own_fpu(1); /* Restore FPU state. */
@@ -1733,7 +1732,6 @@ static void emulate_load_store_microMIPS(struct pt_regs *regs,
17331732
BUG_ON(!used_math());
17341733
BUG_ON(!is_fpu_owner());
17351734

1736-
lose_fpu(1); /* save the FPU state for the emulator */
17371735
res = fpu_emulator_cop1Handler(regs, &current->thread.fpu, 1,
17381736
&fault_addr);
17391737
own_fpu(1); /* restore FPU state */

‎arch/mips/math-emu/cp1emu.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,6 +2831,13 @@ int fpu_emulator_cop1Handler(struct pt_regs *xcp, struct mips_fpu_struct *ctx,
28312831
u16 *instr_ptr;
28322832
int sig = 0;
28332833

2834+
/*
2835+
* Initialize context if it hasn't been used already, otherwise ensure
2836+
* it has been saved to struct thread_struct.
2837+
*/
2838+
if (!init_fp_ctx(current))
2839+
lose_fpu(1);
2840+
28342841
oldepc = xcp->cp0_epc;
28352842
do {
28362843
prevepc = xcp->cp0_epc;

0 commit comments

Comments
 (0)