1 /*
2 * Copyright (C) 2012 Regents of the University of California
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14 #ifndef _ASM_RISCV_SWITCH_TO_H
15 #define _ASM_RISCV_SWITCH_TO_H
16
17 #include <asm/processor.h>
18 #include <asm/ptrace.h>
19 #include <asm/csr.h>
20
21 extern void __fstate_save(struct task_struct *save_to);
22 extern void __fstate_restore(struct task_struct *restore_from);
23
__fstate_clean(struct pt_regs * regs)24 static inline void __fstate_clean(struct pt_regs *regs)
25 {
26 regs->sstatus = (regs->sstatus & ~SR_FS) | SR_FS_CLEAN;
27 }
28
fstate_save(struct task_struct * task,struct pt_regs * regs)29 static inline void fstate_save(struct task_struct *task,
30 struct pt_regs *regs)
31 {
32 if ((regs->sstatus & SR_FS) == SR_FS_DIRTY) {
33 __fstate_save(task);
34 __fstate_clean(regs);
35 }
36 }
37
fstate_restore(struct task_struct * task,struct pt_regs * regs)38 static inline void fstate_restore(struct task_struct *task,
39 struct pt_regs *regs)
40 {
41 if ((regs->sstatus & SR_FS) != SR_FS_OFF) {
42 __fstate_restore(task);
43 __fstate_clean(regs);
44 }
45 }
46
__switch_to_aux(struct task_struct * prev,struct task_struct * next)47 static inline void __switch_to_aux(struct task_struct *prev,
48 struct task_struct *next)
49 {
50 struct pt_regs *regs;
51
52 regs = task_pt_regs(prev);
53 if (unlikely(regs->sstatus & SR_SD))
54 fstate_save(prev, regs);
55 fstate_restore(next, task_pt_regs(next));
56 }
57
58 extern struct task_struct *__switch_to(struct task_struct *,
59 struct task_struct *);
60
61 #define switch_to(prev, next, last) \
62 do { \
63 struct task_struct *__prev = (prev); \
64 struct task_struct *__next = (next); \
65 __switch_to_aux(__prev, __next); \
66 ((last) = __switch_to(__prev, __next)); \
67 } while (0)
68
69 #endif /* _ASM_RISCV_SWITCH_TO_H */
70