LCOV - code coverage report
Current view: top level - arch/x86/include/asm - fsgsbase.h (source / functions) Hit Total Coverage
Test: landlock.info Lines: 17 24 70.8 %
Date: 2021-04-22 12:43:58 Functions: 1 2 50.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: GPL-2.0 */
       2             : #ifndef _ASM_FSGSBASE_H
       3             : #define _ASM_FSGSBASE_H
       4             : 
       5             : #ifndef __ASSEMBLY__
       6             : 
       7             : #ifdef CONFIG_X86_64
       8             : 
       9             : #include <asm/msr-index.h>
      10             : 
      11             : /*
      12             :  * Read/write a task's FSBASE or GSBASE. This returns the value that
      13             :  * the FS/GS base would have (if the task were to be resumed). These
      14             :  * work on the current task or on a non-running (typically stopped
      15             :  * ptrace child) task.
      16             :  */
      17             : extern unsigned long x86_fsbase_read_task(struct task_struct *task);
      18             : extern unsigned long x86_gsbase_read_task(struct task_struct *task);
      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);
      21             : 
      22             : /* Must be protected by X86_FEATURE_FSGSBASE check. */
      23             : 
      24       56003 : static __always_inline unsigned long rdfsbase(void)
      25             : {
      26       56003 :         unsigned long fsbase;
      27             : 
      28       56003 :         asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory");
      29             : 
      30       56003 :         return fsbase;
      31             : }
      32             : 
      33       56005 : static __always_inline unsigned long rdgsbase(void)
      34             : {
      35       56005 :         unsigned long gsbase;
      36             : 
      37       56005 :         asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory");
      38             : 
      39       56006 :         return gsbase;
      40             : }
      41             : 
      42       55644 : static __always_inline void wrfsbase(unsigned long fsbase)
      43             : {
      44       53467 :         asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory");
      45        2177 : }
      46             : 
      47       53473 : static __always_inline void wrgsbase(unsigned long gsbase)
      48             : {
      49       53473 :         asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory");
      50             : }
      51             : 
      52             : #include <asm/cpufeature.h>
      53             : 
      54             : /* Helper functions for reading/writing FS/GS base */
      55             : 
      56           0 : static inline unsigned long x86_fsbase_read_cpu(void)
      57             : {
      58           0 :         unsigned long fsbase;
      59             : 
      60           0 :         if (boot_cpu_has(X86_FEATURE_FSGSBASE))
      61           0 :                 fsbase = rdfsbase();
      62             :         else
      63           0 :                 rdmsrl(MSR_FS_BASE, fsbase);
      64             : 
      65           0 :         return fsbase;
      66             : }
      67             : 
      68        2177 : static inline void x86_fsbase_write_cpu(unsigned long fsbase)
      69             : {
      70        2177 :         if (boot_cpu_has(X86_FEATURE_FSGSBASE))
      71        2177 :                 wrfsbase(fsbase);
      72             :         else
      73           0 :                 wrmsrl(MSR_FS_BASE, fsbase);
      74        2177 : }
      75             : 
      76             : extern unsigned long x86_gsbase_read_cpu_inactive(void);
      77             : extern void x86_gsbase_write_cpu_inactive(unsigned long gsbase);
      78             : extern unsigned long x86_fsgsbase_read_task(struct task_struct *task,
      79             :                                             unsigned short selector);
      80             : 
      81             : #endif /* CONFIG_X86_64 */
      82             : 
      83             : #endif /* __ASSEMBLY__ */
      84             : 
      85             : #endif /* _ASM_FSGSBASE_H */

Generated by: LCOV version 1.14