Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _ASM_FSGSBASE_H |
Ingo Molnar | ec3a941 | 2018-10-08 10:41:59 +0200 | [diff] [blame] | 3 | #define _ASM_FSGSBASE_H |
Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 4 | |
| 5 | #ifndef __ASSEMBLY__ |
| 6 | |
| 7 | #ifdef CONFIG_X86_64 |
| 8 | |
| 9 | #include <asm/msr-index.h> |
| 10 | |
Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 11 | /* |
Ingo Molnar | ec3a941 | 2018-10-08 10:41:59 +0200 | [diff] [blame] | 12 | * Read/write a task's FSBASE or GSBASE. This returns the value that |
Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 13 | * the FS/GS base would have (if the task were to be resumed). These |
Ingo Molnar | ec3a941 | 2018-10-08 10:41:59 +0200 | [diff] [blame] | 14 | * work on the current task or on a non-running (typically stopped |
| 15 | * ptrace child) task. |
Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 16 | */ |
Ingo Molnar | ec3a941 | 2018-10-08 10:41:59 +0200 | [diff] [blame] | 17 | extern unsigned long x86_fsbase_read_task(struct task_struct *task); |
| 18 | extern unsigned long x86_gsbase_read_task(struct task_struct *task); |
Chang S. Bae | 87ab468 | 2018-11-26 11:55:24 -0800 | [diff] [blame] | 19 | extern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase); |
| 20 | extern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase); |
Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 21 | |
Andi Kleen | b15378c | 2020-05-28 16:13:49 -0400 | [diff] [blame] | 22 | /* Must be protected by X86_FEATURE_FSGSBASE check. */ |
| 23 | |
| 24 | static __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 | |
| 33 | static __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 | |
| 42 | static __always_inline void wrfsbase(unsigned long fsbase) |
| 43 | { |
| 44 | asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory"); |
| 45 | } |
| 46 | |
| 47 | static __always_inline void wrgsbase(unsigned long gsbase) |
| 48 | { |
| 49 | asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory"); |
| 50 | } |
| 51 | |
Chang S. Bae | 58edfd2 | 2020-05-28 16:13:50 -0400 | [diff] [blame] | 52 | #include <asm/cpufeature.h> |
| 53 | |
Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 54 | /* Helper functions for reading/writing FS/GS base */ |
| 55 | |
| 56 | static inline unsigned long x86_fsbase_read_cpu(void) |
| 57 | { |
| 58 | unsigned long fsbase; |
| 59 | |
Chang S. Bae | 58edfd2 | 2020-05-28 16:13:50 -0400 | [diff] [blame] | 60 | if (static_cpu_has(X86_FEATURE_FSGSBASE)) |
| 61 | fsbase = rdfsbase(); |
| 62 | else |
| 63 | rdmsrl(MSR_FS_BASE, fsbase); |
Ingo Molnar | ec3a941 | 2018-10-08 10:41:59 +0200 | [diff] [blame] | 64 | |
Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 65 | return fsbase; |
| 66 | } |
| 67 | |
Chang S. Bae | 87ab468 | 2018-11-26 11:55:24 -0800 | [diff] [blame] | 68 | static inline void x86_fsbase_write_cpu(unsigned long fsbase) |
| 69 | { |
Chang S. Bae | 58edfd2 | 2020-05-28 16:13:50 -0400 | [diff] [blame] | 70 | if (static_cpu_has(X86_FEATURE_FSGSBASE)) |
| 71 | wrfsbase(fsbase); |
| 72 | else |
| 73 | wrmsrl(MSR_FS_BASE, fsbase); |
Chang S. Bae | 87ab468 | 2018-11-26 11:55:24 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Chang S. Bae | 58edfd2 | 2020-05-28 16:13:50 -0400 | [diff] [blame] | 76 | extern unsigned long x86_gsbase_read_cpu_inactive(void); |
| 77 | extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase); |
Andy Lutomirski | 40c4590 | 2020-06-26 10:24:29 -0700 | [diff] [blame^] | 78 | extern unsigned long x86_fsgsbase_read_task(struct task_struct *task, |
| 79 | unsigned short selector); |
Chang S. Bae | b1378a5 | 2018-09-18 16:08:53 -0700 | [diff] [blame] | 80 | |
| 81 | #endif /* CONFIG_X86_64 */ |
| 82 | |
| 83 | #endif /* __ASSEMBLY__ */ |
| 84 | |
| 85 | #endif /* _ASM_FSGSBASE_H */ |