blob: 35cff5f2becf6bcbbdc34a890f29d3ba197e100d [file] [log] [blame]
Chang S. Baeb1378a52018-09-18 16:08:53 -07001/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ASM_FSGSBASE_H
Ingo Molnarec3a9412018-10-08 10:41:59 +02003#define _ASM_FSGSBASE_H
Chang S. Baeb1378a52018-09-18 16:08:53 -07004
5#ifndef __ASSEMBLY__
6
7#ifdef CONFIG_X86_64
8
9#include <asm/msr-index.h>
10
Chang S. Baeb1378a52018-09-18 16:08:53 -070011/*
Ingo Molnarec3a9412018-10-08 10:41:59 +020012 * Read/write a task's FSBASE or GSBASE. This returns the value that
Chang S. Baeb1378a52018-09-18 16:08:53 -070013 * the FS/GS base would have (if the task were to be resumed). These
Ingo Molnarec3a9412018-10-08 10:41:59 +020014 * work on the current task or on a non-running (typically stopped
15 * ptrace child) task.
Chang S. Baeb1378a52018-09-18 16:08:53 -070016 */
Ingo Molnarec3a9412018-10-08 10:41:59 +020017extern unsigned long x86_fsbase_read_task(struct task_struct *task);
18extern unsigned long x86_gsbase_read_task(struct task_struct *task);
Chang S. Bae87ab4682018-11-26 11:55:24 -080019extern 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);
Chang S. Baeb1378a52018-09-18 16:08:53 -070021
Andi Kleenb15378c2020-05-28 16:13:49 -040022/* 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
Chang S. Bae58edfd22020-05-28 16:13:50 -040052#include <asm/cpufeature.h>
53
Chang S. Baeb1378a52018-09-18 16:08:53 -070054/* Helper functions for reading/writing FS/GS base */
55
56static inline unsigned long x86_fsbase_read_cpu(void)
57{
58 unsigned long fsbase;
59
Borislav Petkov5f1dd4d2020-08-18 12:28:31 +020060 if (boot_cpu_has(X86_FEATURE_FSGSBASE))
Chang S. Bae58edfd22020-05-28 16:13:50 -040061 fsbase = rdfsbase();
62 else
63 rdmsrl(MSR_FS_BASE, fsbase);
Ingo Molnarec3a9412018-10-08 10:41:59 +020064
Chang S. Baeb1378a52018-09-18 16:08:53 -070065 return fsbase;
66}
67
Chang S. Bae87ab4682018-11-26 11:55:24 -080068static inline void x86_fsbase_write_cpu(unsigned long fsbase)
69{
Borislav Petkov5f1dd4d2020-08-18 12:28:31 +020070 if (boot_cpu_has(X86_FEATURE_FSGSBASE))
Chang S. Bae58edfd22020-05-28 16:13:50 -040071 wrfsbase(fsbase);
72 else
73 wrmsrl(MSR_FS_BASE, fsbase);
Chang S. Bae87ab4682018-11-26 11:55:24 -080074}
75
Chang S. Bae58edfd22020-05-28 16:13:50 -040076extern unsigned long x86_gsbase_read_cpu_inactive(void);
77extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
Andy Lutomirski40c45902020-06-26 10:24:29 -070078extern unsigned long x86_fsgsbase_read_task(struct task_struct *task,
79 unsigned short selector);
Chang S. Baeb1378a52018-09-18 16:08:53 -070080
81#endif /* CONFIG_X86_64 */
82
83#endif /* __ASSEMBLY__ */
84
85#endif /* _ASM_FSGSBASE_H */