Commit 4dc70e1a authored by Stafford Horne's avatar Stafford Horne
Browse files

openrisc: Move FPU state out of pt_regs



My original, naive, FPU support patch had the FPCSR register stored
during both the *mode switch* and *context switch*.  This is wasteful.

Also, the original patches did not save the FPU state when handling
signals during the system call fast path.

We fix this by moving the FPCSR state to thread_struct in task_struct.
We also introduce new helper functions save_fpu and restore_fpu which
can be used to sync the FPU with thread_struct.  These functions are now
called when needed:

 - Setting up and restoring sigcontext when handling signals
 - Before and after __switch_to during context switches
 - When handling FPU exceptions
 - When reading and writing FPU register sets

In the future we can further optimize this by doing lazy FPU save and
restore.  For example, FPU sync is not needed when switching to and from
kernel threads (x86 does this).  FPU save and restore does not need to
be done two times if we have both rescheduling and signal work to do.
However, since OpenRISC FPU state is a single register, I leave these
optimizations for future consideration.

Signed-off-by: default avatarStafford Horne <shorne@gmail.com>
parent 1f33446d
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __ASM_OPENRISC_FPU_H
#define __ASM_OPENRISC_FPU_H

struct task_struct;

#ifdef CONFIG_FPU
static inline void save_fpu(struct task_struct *task)
{
	task->thread.fpcsr = mfspr(SPR_FPCSR);
}

static inline void restore_fpu(struct task_struct *task)
{
	mtspr(SPR_FPCSR, task->thread.fpcsr);
}
#else
#define save_fpu(tsk)			do { } while (0)
#define restore_fpu(tsk)		do { } while (0)
#endif

#endif /* __ASM_OPENRISC_FPU_H */
+1 −0
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@
struct task_struct;

struct thread_struct {
	long fpcsr;		/* Floating point control status register. */
};

/*
+1 −2
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ struct pt_regs {
	 * -1 for all other exceptions.
	 */
	long  orig_gpr11;	/* For restarting system calls */
	long fpcsr;		/* Floating point control status register. */
	long dummy;		/* Cheap alignment fix */
	long dummy2;		/* Cheap alignment fix */
};

@@ -115,6 +115,5 @@ static inline long regs_return_value(struct pt_regs *regs)
#define PT_GPR31      124
#define PT_PC	      128
#define PT_ORIG_GPR11 132
#define PT_FPCSR      136

#endif /* __ASM_OPENRISC_PTRACE_H */
+1 −14
Original line number Diff line number Diff line
@@ -106,8 +106,6 @@
	l.mtspr r0,r3,SPR_EPCR_BASE				;\
	l.lwz   r3,PT_SR(r1)					;\
	l.mtspr r0,r3,SPR_ESR_BASE				;\
	l.lwz	r3,PT_FPCSR(r1)					;\
	l.mtspr	r0,r3,SPR_FPCSR					;\
	l.lwz   r2,PT_GPR2(r1)					;\
	l.lwz   r3,PT_GPR3(r1)					;\
	l.lwz   r4,PT_GPR4(r1)					;\
@@ -177,8 +175,6 @@ handler: ;\
	/* r30 already save */					;\
	l.sw    PT_GPR31(r1),r31					;\
	TRACE_IRQS_OFF_ENTRY						;\
	l.mfspr	r30,r0,SPR_FPCSR				;\
	l.sw	PT_FPCSR(r1),r30				;\
	/* Store -1 in orig_gpr11 for non-syscall exceptions */	;\
	l.addi	r30,r0,-1					;\
	l.sw	PT_ORIG_GPR11(r1),r30
@@ -219,8 +215,6 @@ handler: ;\
	/* Store -1 in orig_gpr11 for non-syscall exceptions */	;\
	l.addi	r30,r0,-1					;\
	l.sw	PT_ORIG_GPR11(r1),r30				;\
	l.mfspr	r30,r0,SPR_FPCSR				;\
	l.sw	PT_FPCSR(r1),r30				;\
	l.addi	r3,r1,0						;\
	/* r4 is exception EA */				;\
	l.addi	r5,r0,vector					;\
@@ -852,6 +846,7 @@ _syscall_badsys:

EXCEPTION_ENTRY(_fpe_trap_handler)
	CLEAR_LWA_FLAG(r3)

	/* r4: EA of fault (set by EXCEPTION_HANDLE) */
	l.jal   do_fpe_trap
	 l.addi  r3,r1,0 /* pt_regs */
@@ -1100,10 +1095,6 @@ ENTRY(_switch)
	l.sw    PT_GPR28(r1),r28
	l.sw    PT_GPR30(r1),r30

	/* Store the old FPU state to new pt_regs */
	l.mfspr	r29,r0,SPR_FPCSR
	l.sw	PT_FPCSR(r1),r29

	l.addi	r11,r10,0			/* Save old 'current' to 'last' return value*/

	/* We use thread_info->ksp for storing the address of the above
@@ -1126,10 +1117,6 @@ ENTRY(_switch)
	l.lwz	r29,PT_SP(r1)
	l.sw	TI_KSP(r10),r29

	/* Restore the old value of FPCSR */
	l.lwz	r29,PT_FPCSR(r1)
	l.mtspr	r0,r29,SPR_FPCSR

	/* ...and restore the registers, except r11 because the return value
	 * has already been set above.
	 */
+5 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@
#include <linux/reboot.h>

#include <linux/uaccess.h>
#include <asm/fpu.h>
#include <asm/io.h>
#include <asm/processor.h>
#include <asm/spr_defs.h>
@@ -244,6 +245,8 @@ struct task_struct *__switch_to(struct task_struct *old,

	local_irq_save(flags);

	save_fpu(current);

	/* current_set is an array of saved current pointers
	 * (one for each cpu). we need them at user->kernel transition,
	 * while we save them at kernel->user transition
@@ -256,6 +259,8 @@ struct task_struct *__switch_to(struct task_struct *old,
	current_thread_info_set[smp_processor_id()] = new_ti;
	last = (_switch(old_ti, new_ti))->task;

	restore_fpu(current);

	local_irq_restore(flags);

	return last;
Loading