Skip to content

Commit 4dc70e1

Browse files
committed
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: Stafford Horne <shorne@gmail.com>
1 parent 1f33446 commit 4dc70e1

File tree

8 files changed

+76
-32
lines changed

8 files changed

+76
-32
lines changed

arch/openrisc/include/asm/fpu.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __ASM_OPENRISC_FPU_H
3+
#define __ASM_OPENRISC_FPU_H
4+
5+
struct task_struct;
6+
7+
#ifdef CONFIG_FPU
8+
static inline void save_fpu(struct task_struct *task)
9+
{
10+
task->thread.fpcsr = mfspr(SPR_FPCSR);
11+
}
12+
13+
static inline void restore_fpu(struct task_struct *task)
14+
{
15+
mtspr(SPR_FPCSR, task->thread.fpcsr);
16+
}
17+
#else
18+
#define save_fpu(tsk) do { } while (0)
19+
#define restore_fpu(tsk) do { } while (0)
20+
#endif
21+
22+
#endif /* __ASM_OPENRISC_FPU_H */

arch/openrisc/include/asm/processor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
struct task_struct;
4545

4646
struct thread_struct {
47+
long fpcsr; /* Floating point control status register. */
4748
};
4849

4950
/*

arch/openrisc/include/asm/ptrace.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct pt_regs {
5959
* -1 for all other exceptions.
6060
*/
6161
long orig_gpr11; /* For restarting system calls */
62-
long fpcsr; /* Floating point control status register. */
62+
long dummy; /* Cheap alignment fix */
6363
long dummy2; /* Cheap alignment fix */
6464
};
6565

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

120119
#endif /* __ASM_OPENRISC_PTRACE_H */

arch/openrisc/kernel/entry.S

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@
106106
l.mtspr r0,r3,SPR_EPCR_BASE ;\
107107
l.lwz r3,PT_SR(r1) ;\
108108
l.mtspr r0,r3,SPR_ESR_BASE ;\
109-
l.lwz r3,PT_FPCSR(r1) ;\
110-
l.mtspr r0,r3,SPR_FPCSR ;\
111109
l.lwz r2,PT_GPR2(r1) ;\
112110
l.lwz r3,PT_GPR3(r1) ;\
113111
l.lwz r4,PT_GPR4(r1) ;\
@@ -177,8 +175,6 @@ handler: ;\
177175
/* r30 already save */ ;\
178176
l.sw PT_GPR31(r1),r31 ;\
179177
TRACE_IRQS_OFF_ENTRY ;\
180-
l.mfspr r30,r0,SPR_FPCSR ;\
181-
l.sw PT_FPCSR(r1),r30 ;\
182178
/* Store -1 in orig_gpr11 for non-syscall exceptions */ ;\
183179
l.addi r30,r0,-1 ;\
184180
l.sw PT_ORIG_GPR11(r1),r30
@@ -219,8 +215,6 @@ handler: ;\
219215
/* Store -1 in orig_gpr11 for non-syscall exceptions */ ;\
220216
l.addi r30,r0,-1 ;\
221217
l.sw PT_ORIG_GPR11(r1),r30 ;\
222-
l.mfspr r30,r0,SPR_FPCSR ;\
223-
l.sw PT_FPCSR(r1),r30 ;\
224218
l.addi r3,r1,0 ;\
225219
/* r4 is exception EA */ ;\
226220
l.addi r5,r0,vector ;\
@@ -852,6 +846,7 @@ _syscall_badsys:
852846

853847
EXCEPTION_ENTRY(_fpe_trap_handler)
854848
CLEAR_LWA_FLAG(r3)
849+
855850
/* r4: EA of fault (set by EXCEPTION_HANDLE) */
856851
l.jal do_fpe_trap
857852
l.addi r3,r1,0 /* pt_regs */
@@ -1100,10 +1095,6 @@ ENTRY(_switch)
11001095
l.sw PT_GPR28(r1),r28
11011096
l.sw PT_GPR30(r1),r30
11021097

1103-
/* Store the old FPU state to new pt_regs */
1104-
l.mfspr r29,r0,SPR_FPCSR
1105-
l.sw PT_FPCSR(r1),r29
1106-
11071098
l.addi r11,r10,0 /* Save old 'current' to 'last' return value*/
11081099

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

1129-
/* Restore the old value of FPCSR */
1130-
l.lwz r29,PT_FPCSR(r1)
1131-
l.mtspr r0,r29,SPR_FPCSR
1132-
11331120
/* ...and restore the registers, except r11 because the return value
11341121
* has already been set above.
11351122
*/

arch/openrisc/kernel/process.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/reboot.h>
3737

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

245246
local_irq_save(flags);
246247

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

262+
restore_fpu(current);
263+
259264
local_irq_restore(flags);
260265

261266
return last;

arch/openrisc/kernel/ptrace.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,23 +98,17 @@ static int fpregs_get(struct task_struct *target,
9898
const struct user_regset *regset,
9999
struct membuf to)
100100
{
101-
const struct pt_regs *regs = task_pt_regs(target);
102-
103-
return membuf_store(&to, regs->fpcsr);
101+
return membuf_store(&to, target->thread.fpcsr);
104102
}
105103

106104
static int fpregs_set(struct task_struct *target,
107105
const struct user_regset *regset,
108106
unsigned int pos, unsigned int count,
109107
const void *kbuf, const void __user *ubuf)
110108
{
111-
struct pt_regs *regs = task_pt_regs(target);
112-
int ret;
113-
114109
/* FPCSR */
115-
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
116-
&regs->fpcsr, 0, 4);
117-
return ret;
110+
return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
111+
&target->thread.fpcsr, 0, 4);
118112
}
119113
#endif
120114

arch/openrisc/kernel/signal.c

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <linux/stddef.h>
2424
#include <linux/resume_user_mode.h>
2525

26+
#include <asm/fpu.h>
2627
#include <asm/processor.h>
2728
#include <asm/syscall.h>
2829
#include <asm/ucontext.h>
@@ -39,6 +40,37 @@ asmlinkage long _sys_rt_sigreturn(struct pt_regs *regs);
3940
asmlinkage int do_work_pending(struct pt_regs *regs, unsigned int thread_flags,
4041
int syscall);
4142

43+
#ifdef CONFIG_FPU
44+
static long restore_fp_state(struct sigcontext __user *sc)
45+
{
46+
long err;
47+
48+
err = __copy_from_user(&current->thread.fpcsr, &sc->fpcsr, sizeof(unsigned long));
49+
if (unlikely(err))
50+
return err;
51+
52+
/* Restore the FPU state */
53+
restore_fpu(current);
54+
55+
return 0;
56+
}
57+
58+
static long save_fp_state(struct sigcontext __user *sc)
59+
{
60+
long err;
61+
62+
/* Sync the user FPU state so we can copy to sigcontext */
63+
save_fpu(current);
64+
65+
err = __copy_to_user(&sc->fpcsr, &current->thread.fpcsr, sizeof(unsigned long));
66+
67+
return err;
68+
}
69+
#else
70+
#define save_fp_state(sc) (0)
71+
#define restore_fp_state(sc) (0)
72+
#endif
73+
4274
static int restore_sigcontext(struct pt_regs *regs,
4375
struct sigcontext __user *sc)
4476
{
@@ -55,7 +87,7 @@ static int restore_sigcontext(struct pt_regs *regs,
5587
err |= __copy_from_user(regs, sc->regs.gpr, 32 * sizeof(unsigned long));
5688
err |= __copy_from_user(&regs->pc, &sc->regs.pc, sizeof(unsigned long));
5789
err |= __copy_from_user(&regs->sr, &sc->regs.sr, sizeof(unsigned long));
58-
err |= __copy_from_user(&regs->fpcsr, &sc->fpcsr, sizeof(unsigned long));
90+
err |= restore_fp_state(sc);
5991

6092
/* make sure the SM-bit is cleared so user-mode cannot fool us */
6193
regs->sr &= ~SPR_SR_SM;
@@ -118,7 +150,7 @@ static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
118150
err |= __copy_to_user(sc->regs.gpr, regs, 32 * sizeof(unsigned long));
119151
err |= __copy_to_user(&sc->regs.pc, &regs->pc, sizeof(unsigned long));
120152
err |= __copy_to_user(&sc->regs.sr, &regs->sr, sizeof(unsigned long));
121-
err |= __copy_to_user(&sc->fpcsr, &regs->fpcsr, sizeof(unsigned long));
153+
err |= save_fp_state(sc);
122154

123155
return err;
124156
}

arch/openrisc/kernel/traps.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/uaccess.h>
3232

3333
#include <asm/bug.h>
34+
#include <asm/fpu.h>
3435
#include <asm/io.h>
3536
#include <asm/processor.h>
3637
#include <asm/unwinder.h>
@@ -84,9 +85,8 @@ void show_registers(struct pt_regs *regs)
8485
in_kernel = 0;
8586

8687
pr_info("CPU #: %d\n"
87-
" PC: %08lx SR: %08lx SP: %08lx FPCSR: %08lx\n",
88-
smp_processor_id(), regs->pc, regs->sr, regs->sp,
89-
regs->fpcsr);
88+
" PC: %08lx SR: %08lx SP: %08lx\n",
89+
smp_processor_id(), regs->pc, regs->sr, regs->sp);
9090
pr_info("GPR00: %08lx GPR01: %08lx GPR02: %08lx GPR03: %08lx\n",
9191
0L, regs->gpr[1], regs->gpr[2], regs->gpr[3]);
9292
pr_info("GPR04: %08lx GPR05: %08lx GPR06: %08lx GPR07: %08lx\n",
@@ -183,7 +183,10 @@ asmlinkage void do_fpe_trap(struct pt_regs *regs, unsigned long address)
183183
if (user_mode(regs)) {
184184
int code = FPE_FLTUNK;
185185
#ifdef CONFIG_FPU
186-
unsigned long fpcsr = regs->fpcsr;
186+
unsigned long fpcsr;
187+
188+
save_fpu(current);
189+
fpcsr = current->thread.fpcsr;
187190

188191
if (fpcsr & SPR_FPCSR_IVF)
189192
code = FPE_FLTINV;
@@ -197,7 +200,8 @@ asmlinkage void do_fpe_trap(struct pt_regs *regs, unsigned long address)
197200
code = FPE_FLTRES;
198201

199202
/* Clear all flags */
200-
regs->fpcsr &= ~SPR_FPCSR_ALLF;
203+
current->thread.fpcsr &= ~SPR_FPCSR_ALLF;
204+
restore_fpu(current);
201205
#endif
202206
force_sig_fault(SIGFPE, code, (void __user *)regs->pc);
203207
} else {

0 commit comments

Comments
 (0)