18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 28c2ecf20Sopenharmony_ci#ifndef _ASM_FSGSBASE_H 38c2ecf20Sopenharmony_ci#define _ASM_FSGSBASE_H 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_64 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#include <asm/msr-index.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci/* 128c2ecf20Sopenharmony_ci * Read/write a task's FSBASE or GSBASE. This returns the value that 138c2ecf20Sopenharmony_ci * the FS/GS base would have (if the task were to be resumed). These 148c2ecf20Sopenharmony_ci * work on the current task or on a non-running (typically stopped 158c2ecf20Sopenharmony_ci * ptrace child) task. 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_ciextern unsigned long x86_fsbase_read_task(struct task_struct *task); 188c2ecf20Sopenharmony_ciextern unsigned long x86_gsbase_read_task(struct task_struct *task); 198c2ecf20Sopenharmony_ciextern void x86_fsbase_write_task(struct task_struct *task, unsigned long fsbase); 208c2ecf20Sopenharmony_ciextern void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase); 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/* Must be protected by X86_FEATURE_FSGSBASE check. */ 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_cistatic __always_inline unsigned long rdfsbase(void) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci unsigned long fsbase; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory"); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci return fsbase; 318c2ecf20Sopenharmony_ci} 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic __always_inline unsigned long rdgsbase(void) 348c2ecf20Sopenharmony_ci{ 358c2ecf20Sopenharmony_ci unsigned long gsbase; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory"); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci return gsbase; 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic __always_inline void wrfsbase(unsigned long fsbase) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory"); 458c2ecf20Sopenharmony_ci} 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_cistatic __always_inline void wrgsbase(unsigned long gsbase) 488c2ecf20Sopenharmony_ci{ 498c2ecf20Sopenharmony_ci asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory"); 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#include <asm/cpufeature.h> 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci/* Helper functions for reading/writing FS/GS base */ 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic inline unsigned long x86_fsbase_read_cpu(void) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci unsigned long fsbase; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci if (boot_cpu_has(X86_FEATURE_FSGSBASE)) 618c2ecf20Sopenharmony_ci fsbase = rdfsbase(); 628c2ecf20Sopenharmony_ci else 638c2ecf20Sopenharmony_ci rdmsrl(MSR_FS_BASE, fsbase); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci return fsbase; 668c2ecf20Sopenharmony_ci} 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_cistatic inline void x86_fsbase_write_cpu(unsigned long fsbase) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci if (boot_cpu_has(X86_FEATURE_FSGSBASE)) 718c2ecf20Sopenharmony_ci wrfsbase(fsbase); 728c2ecf20Sopenharmony_ci else 738c2ecf20Sopenharmony_ci wrmsrl(MSR_FS_BASE, fsbase); 748c2ecf20Sopenharmony_ci} 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ciextern unsigned long x86_gsbase_read_cpu_inactive(void); 778c2ecf20Sopenharmony_ciextern void x86_gsbase_write_cpu_inactive(unsigned long gsbase); 788c2ecf20Sopenharmony_ciextern unsigned long x86_fsgsbase_read_task(struct task_struct *task, 798c2ecf20Sopenharmony_ci unsigned short selector); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci#endif /* CONFIG_X86_64 */ 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */ 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci#endif /* _ASM_FSGSBASE_H */ 86