fsgsbase.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_FSGSBASE_H
  3. #define _ASM_FSGSBASE_H
  4. #ifndef __ASSEMBLY__
  5. #ifdef CONFIG_X86_64
  6. #include <asm/msr-index.h>
  7. /*
  8. * Read/write a task's FSBASE or GSBASE. This returns the value that
  9. * the FS/GS base would have (if the task were to be resumed). These
  10. * work on the current task or on a non-running (typically stopped
  11. * ptrace child) task.
  12. */
  13. extern unsigned long x86_fsbase_read_task(struct task_struct *task);
  14. extern unsigned long x86_gsbase_read_task(struct task_struct *task);
  15. extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
  16. extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
  17. /* Must be protected by X86_FEATURE_FSGSBASE check. */
  18. static __always_inline unsigned long rdfsbase(void)
  19. {
  20. unsigned long fsbase;
  21. asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory");
  22. return fsbase;
  23. }
  24. static __always_inline unsigned long rdgsbase(void)
  25. {
  26. unsigned long gsbase;
  27. asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory");
  28. return gsbase;
  29. }
  30. static __always_inline void wrfsbase(unsigned long fsbase)
  31. {
  32. asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory");
  33. }
  34. static __always_inline void wrgsbase(unsigned long gsbase)
  35. {
  36. asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory");
  37. }
  38. #include <asm/cpufeature.h>
  39. /* Helper functions for reading/writing FS/GS base */
  40. static inline unsigned long x86_fsbase_read_cpu(void)
  41. {
  42. unsigned long fsbase;
  43. if (boot_cpu_has(X86_FEATURE_FSGSBASE))
  44. fsbase = rdfsbase();
  45. else
  46. rdmsrl(MSR_FS_BASE, fsbase);
  47. return fsbase;
  48. }
  49. static inline void x86_fsbase_write_cpu(unsigned long fsbase)
  50. {
  51. if (boot_cpu_has(X86_FEATURE_FSGSBASE))
  52. wrfsbase(fsbase);
  53. else
  54. wrmsrl(MSR_FS_BASE, fsbase);
  55. }
  56. extern unsigned long x86_gsbase_read_cpu_inactive(void);
  57. extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
  58. extern unsigned long x86_fsgsbase_read_task(struct task_struct *task,
  59. unsigned short selector);
  60. #endif /* CONFIG_X86_64 */
  61. #endif /* __ASSEMBLY__ */
  62. #endif /* _ASM_FSGSBASE_H */