2018-09-18 23:08:53 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
#ifndef _ASM_FSGSBASE_H
|
2018-10-08 08:41:59 +00:00
|
|
|
#define _ASM_FSGSBASE_H
|
2018-09-18 23:08:53 +00:00
|
|
|
|
|
|
|
#ifndef __ASSEMBLY__
|
|
|
|
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
|
|
|
|
#include <asm/msr-index.h>
|
|
|
|
|
|
|
|
/*
|
2018-10-08 08:41:59 +00:00
|
|
|
* Read/write a task's FSBASE or GSBASE. This returns the value that
|
2018-09-18 23:08:53 +00:00
|
|
|
* the FS/GS base would have (if the task were to be resumed). These
|
2018-10-08 08:41:59 +00:00
|
|
|
* work on the current task or on a non-running (typically stopped
|
|
|
|
* ptrace child) task.
|
2018-09-18 23:08:53 +00:00
|
|
|
*/
|
2018-10-08 08:41:59 +00:00
|
|
|
extern unsigned long x86_fsbase_read_task(struct task_struct *task);
|
|
|
|
extern unsigned long x86_gsbase_read_task(struct task_struct *task);
|
2018-11-26 19:55:24 +00:00
|
|
|
extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
|
|
|
|
extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
|
2018-09-18 23:08:53 +00:00
|
|
|
|
2020-05-28 20:13:49 +00:00
|
|
|
/* Must be protected by X86_FEATURE_FSGSBASE check. */
|
|
|
|
|
|
|
|
static __always_inline unsigned long rdfsbase(void)
|
|
|
|
{
|
|
|
|
unsigned long fsbase;
|
|
|
|
|
|
|
|
asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory");
|
|
|
|
|
|
|
|
return fsbase;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline unsigned long rdgsbase(void)
|
|
|
|
{
|
|
|
|
unsigned long gsbase;
|
|
|
|
|
|
|
|
asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory");
|
|
|
|
|
|
|
|
return gsbase;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline void wrfsbase(unsigned long fsbase)
|
|
|
|
{
|
|
|
|
asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline void wrgsbase(unsigned long gsbase)
|
|
|
|
{
|
|
|
|
asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory");
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:13:50 +00:00
|
|
|
#include <asm/cpufeature.h>
|
|
|
|
|
2018-09-18 23:08:53 +00:00
|
|
|
/* Helper functions for reading/writing FS/GS base */
|
|
|
|
|
|
|
|
static inline unsigned long x86_fsbase_read_cpu(void)
|
|
|
|
{
|
|
|
|
unsigned long fsbase;
|
|
|
|
|
2020-08-18 10:28:31 +00:00
|
|
|
if (boot_cpu_has(X86_FEATURE_FSGSBASE))
|
2020-05-28 20:13:50 +00:00
|
|
|
fsbase = rdfsbase();
|
|
|
|
else
|
|
|
|
rdmsrl(MSR_FS_BASE, fsbase);
|
2018-10-08 08:41:59 +00:00
|
|
|
|
2018-09-18 23:08:53 +00:00
|
|
|
return fsbase;
|
|
|
|
}
|
|
|
|
|
2018-11-26 19:55:24 +00:00
|
|
|
static inline void x86_fsbase_write_cpu(unsigned long fsbase)
|
|
|
|
{
|
2020-08-18 10:28:31 +00:00
|
|
|
if (boot_cpu_has(X86_FEATURE_FSGSBASE))
|
2020-05-28 20:13:50 +00:00
|
|
|
wrfsbase(fsbase);
|
|
|
|
else
|
|
|
|
wrmsrl(MSR_FS_BASE, fsbase);
|
2018-11-26 19:55:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:13:50 +00:00
|
|
|
extern unsigned long x86_gsbase_read_cpu_inactive(void);
|
|
|
|
extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
|
2020-06-26 17:24:29 +00:00
|
|
|
extern unsigned long x86_fsgsbase_read_task(struct task_struct *task,
|
|
|
|
unsigned short selector);
|
2018-09-18 23:08:53 +00:00
|
|
|
|
|
|
|
#endif /* CONFIG_X86_64 */
|
|
|
|
|
|
|
|
#endif /* __ASSEMBLY__ */
|
|
|
|
|
|
|
|
#endif /* _ASM_FSGSBASE_H */
|