Merge tag 'soc-drivers-6.9' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[linux-block.git] / arch / x86 / include / asm / fsgsbase.h
CommitLineData
b1378a56
CB
1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_FSGSBASE_H
ec3a9418 3#define _ASM_FSGSBASE_H
b1378a56
CB
4
5#ifndef __ASSEMBLY__
6
7#ifdef CONFIG_X86_64
8
154fcf3a 9#include <asm/msr.h>
b1378a56 10
b1378a56 11/*
ec3a9418 12 * Read/write a task's FSBASE or GSBASE. This returns the value that
b1378a56 13 * the FS/GS base would have (if the task were to be resumed). These
ec3a9418
IM
14 * work on the current task or on a non-running (typically stopped
15 * ptrace child) task.
b1378a56 16 */
ec3a9418
IM
17extern unsigned long x86_fsbase_read_task(struct task_struct *task);
18extern unsigned long x86_gsbase_read_task(struct task_struct *task);
87ab4689
CB
19extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase);
20extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase);
b1378a56 21
b15378ca
AK
22/* Must be protected by X86_FEATURE_FSGSBASE check. */
23
24static __always_inline unsigned long rdfsbase(void)
25{
26 unsigned long fsbase;
27
28 asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory");
29
30 return fsbase;
31}
32
33static __always_inline unsigned long rdgsbase(void)
34{
35 unsigned long gsbase;
36
37 asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory");
38
39 return gsbase;
40}
41
42static __always_inline void wrfsbase(unsigned long fsbase)
43{
44 asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory");
45}
46
47static __always_inline void wrgsbase(unsigned long gsbase)
48{
49 asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory");
50}
51
58edfd2e
CB
52#include <asm/cpufeature.h>
53
b1378a56
CB
54/* Helper functions for reading/writing FS/GS base */
55
56static inline unsigned long x86_fsbase_read_cpu(void)
57{
58 unsigned long fsbase;
59
5f1dd4dd 60 if (boot_cpu_has(X86_FEATURE_FSGSBASE))
58edfd2e
CB
61 fsbase = rdfsbase();
62 else
63 rdmsrl(MSR_FS_BASE, fsbase);
ec3a9418 64
b1378a56
CB
65 return fsbase;
66}
67
87ab4689
CB
68static inline void x86_fsbase_write_cpu(unsigned long fsbase)
69{
5f1dd4dd 70 if (boot_cpu_has(X86_FEATURE_FSGSBASE))
58edfd2e
CB
71 wrfsbase(fsbase);
72 else
73 wrmsrl(MSR_FS_BASE, fsbase);
87ab4689
CB
74}
75
58edfd2e
CB
76extern unsigned long x86_gsbase_read_cpu_inactive(void);
77extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
40c45904
AL
78extern unsigned long x86_fsgsbase_read_task(struct task_struct *task,
79 unsigned short selector);
b1378a56
CB
80
81#endif /* CONFIG_X86_64 */
82
83#endif /* __ASSEMBLY__ */
84
85#endif /* _ASM_FSGSBASE_H */