switch_to.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifndef _ASM_RISCV_SWITCH_TO_H
  14. #define _ASM_RISCV_SWITCH_TO_H
  15. #include <asm/processor.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/csr.h>
  18. #ifdef CONFIG_FPU
  19. extern void __fstate_save(struct task_struct *save_to);
  20. extern void __fstate_restore(struct task_struct *restore_from);
  21. static inline void __fstate_clean(struct pt_regs *regs)
  22. {
  23. regs->sstatus |= (regs->sstatus & ~(SR_FS)) | SR_FS_CLEAN;
  24. }
  25. static inline void fstate_save(struct task_struct *task,
  26. struct pt_regs *regs)
  27. {
  28. if ((regs->sstatus & SR_FS) == SR_FS_DIRTY) {
  29. __fstate_save(task);
  30. __fstate_clean(regs);
  31. }
  32. }
  33. static inline void fstate_restore(struct task_struct *task,
  34. struct pt_regs *regs)
  35. {
  36. if ((regs->sstatus & SR_FS) != SR_FS_OFF) {
  37. __fstate_restore(task);
  38. __fstate_clean(regs);
  39. }
  40. }
  41. static inline void __switch_to_aux(struct task_struct *prev,
  42. struct task_struct *next)
  43. {
  44. struct pt_regs *regs;
  45. regs = task_pt_regs(prev);
  46. if (unlikely(regs->sstatus & SR_SD))
  47. fstate_save(prev, regs);
  48. fstate_restore(next, task_pt_regs(next));
  49. }
  50. extern bool has_fpu;
  51. #else
  52. #define has_fpu false
  53. #define fstate_save(task, regs) do { } while (0)
  54. #define fstate_restore(task, regs) do { } while (0)
  55. #define __switch_to_aux(__prev, __next) do { } while (0)
  56. #endif
  57. extern struct task_struct *__switch_to(struct task_struct *,
  58. struct task_struct *);
  59. #define switch_to(prev, next, last) \
  60. do { \
  61. struct task_struct *__prev = (prev); \
  62. struct task_struct *__next = (next); \
  63. if (has_fpu) \
  64. __switch_to_aux(__prev, __next); \
  65. ((last) = __switch_to(__prev, __next)); \
  66. } while (0)
  67. #endif /* _ASM_RISCV_SWITCH_TO_H */