Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _ASM_FSGSBASE_H
0003 #define _ASM_FSGSBASE_H
0004 
0005 #ifndef __ASSEMBLY__
0006 
0007 #ifdef CONFIG_X86_64
0008 
0009 #include <asm/msr-index.h>
0010 
0011 /*
0012  * Read/write a task's FSBASE or GSBASE. This returns the value that
0013  * the FS/GS base would have (if the task were to be resumed). These
0014  * work on the current task or on a non-running (typically stopped
0015  * ptrace child) task.
0016  */
0017 extern unsigned long x86_fsbase_read_task(struct task_struct *task);
0018 extern unsigned long x86_gsbase_read_task(struct task_struct *task);
0019 extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
0020 extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
0021 
0022 /* Must be protected by X86_FEATURE_FSGSBASE check. */
0023 
0024 static __always_inline unsigned long rdfsbase(void)
0025 {
0026     unsigned long fsbase;
0027 
0028     asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory");
0029 
0030     return fsbase;
0031 }
0032 
0033 static __always_inline unsigned long rdgsbase(void)
0034 {
0035     unsigned long gsbase;
0036 
0037     asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory");
0038 
0039     return gsbase;
0040 }
0041 
0042 static __always_inline void wrfsbase(unsigned long fsbase)
0043 {
0044     asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory");
0045 }
0046 
0047 static __always_inline void wrgsbase(unsigned long gsbase)
0048 {
0049     asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory");
0050 }
0051 
0052 #include <asm/cpufeature.h>
0053 
0054 /* Helper functions for reading/writing FS/GS base */
0055 
0056 static inline unsigned long x86_fsbase_read_cpu(void)
0057 {
0058     unsigned long fsbase;
0059 
0060     if (boot_cpu_has(X86_FEATURE_FSGSBASE))
0061         fsbase = rdfsbase();
0062     else
0063         rdmsrl(MSR_FS_BASE, fsbase);
0064 
0065     return fsbase;
0066 }
0067 
0068 static inline void x86_fsbase_write_cpu(unsigned long fsbase)
0069 {
0070     if (boot_cpu_has(X86_FEATURE_FSGSBASE))
0071         wrfsbase(fsbase);
0072     else
0073         wrmsrl(MSR_FS_BASE, fsbase);
0074 }
0075 
0076 extern unsigned long x86_gsbase_read_cpu_inactive(void);
0077 extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
0078 extern unsigned long x86_fsgsbase_read_task(struct task_struct *task,
0079                         unsigned short selector);
0080 
0081 #endif /* CONFIG_X86_64 */
0082 
0083 #endif /* __ASSEMBLY__ */
0084 
0085 #endif /* _ASM_FSGSBASE_H */