GSBASE is used to find per-CPU data in the kernel. But when it is unknown,
the per-CPU base can be found from the per_cpu_offset table with a CPU NR.
The CPU NR is extracted from the limit field of the CPUNODE entry in GDT,
or by the RDPID instruction.

Also, add the GAS-compatible RDPID macro.

The new macro will be used on a following patch.

Suggested-by: H. Peter Anvin <h...@zytor.com>
Signed-off-by: Chang S. Bae <chang.seok....@intel.com>
Cc: Andi Kleen <a...@linux.intel.com>
Cc: Andy Lutomirski <l...@kernel.org>
Cc: Dave Hansen <dave.han...@linux.intel.com>
Cc: Thomas Gleixner <t...@linutronix.de>
Cc: Ingo Molnar <mi...@kernel.org>
---
 arch/x86/include/asm/fsgsbase.h | 52 +++++++++++++++++++++++++++++++++
 arch/x86/include/asm/inst.h     | 15 ++++++++++
 2 files changed, 67 insertions(+)

diff --git a/arch/x86/include/asm/fsgsbase.h b/arch/x86/include/asm/fsgsbase.h
index e500d771155f..0c2d7d8a8c01 100644
--- a/arch/x86/include/asm/fsgsbase.h
+++ b/arch/x86/include/asm/fsgsbase.h
@@ -111,6 +111,58 @@ extern void x86_gsbase_write_cpu_inactive(unsigned long 
gsbase);
        MODRM 0xd0 wrgsbase_opd 1
 .endm
 
+#if CONFIG_SMP
+
+/*
+ * Fetch the per-CPU GSBASE value for this processor and put it in @reg.
+ * We normally use %gs for accessing per-CPU data, but we are setting up
+ * %gs here and obviously can not use %gs itself to access per-CPU data.
+ */
+.macro FIND_PERCPU_BASE_RDPID reg:req
+       /*
+        * The CPU/node NR is initialized earlier, directly in cpu_init().
+        * The CPU NR is extracted from it.
+        */
+       RDPID   \reg
+       andq    $VDSO_CPUNODE_MASK, \reg
+
+       /*
+        * The kernel GSBASE value is found from the __per_cpu_offset table
+        * with the CPU NR.
+        */
+       movq    __per_cpu_offset(, \reg, 8), \reg
+.endm
+
+/*
+ * Same as above FIND_PERCPU_BASERDPID, except that CPU/node NR is loaded
+ * from the limit (size) field of a special segment descriptor entry in
+ * GDT.
+ */
+.macro FIND_PERCPU_BASE_SEG_LIMIT reg:req
+       /* Read CPU NR */
+       movq    $__CPUNODE_SEG, \reg
+       lsl     \reg, \reg
+       andq    $VDSO_CPUNODE_MASK, \reg
+
+       movq    __per_cpu_offset(, \reg, 8), \reg
+.endm
+
+.macro FIND_PERCPU_BASE reg:req
+       ALTERNATIVE \
+               "FIND_PERCPU_BASE_SEG_LIMIT \reg", \
+               "FIND_PERCPU_BASE_RDPID \reg", \
+               X86_FEATURE_RDPID
+.endm
+
+#else
+
+.macro FIND_PERCPU_BASE reg:req
+       /* Tracking the base offset value */
+       movq    pcpu_unit_offsets(%rip), \reg
+.endm
+
+#endif /* CONFIG_SMP */
+
 #endif /* CONFIG_X86_64 */
 
 #endif /* __ASSEMBLY__ */
diff --git a/arch/x86/include/asm/inst.h b/arch/x86/include/asm/inst.h
index f5a796da07f8..d063841a17e3 100644
--- a/arch/x86/include/asm/inst.h
+++ b/arch/x86/include/asm/inst.h
@@ -306,6 +306,21 @@
        .endif
        MODRM 0xc0 movq_r64_xmm_opd1 movq_r64_xmm_opd2
        .endm
+
+.macro RDPID opd
+       REG_TYPE rdpid_opd_type \opd
+       .if rdpid_opd_type == REG_TYPE_R64
+       R64_NUM rdpid_opd \opd
+       .else
+       R32_NUM rdpid_opd \opd
+       .endif
+       .byte 0xf3
+       .if rdpid_opd > 7
+       PFX_REX rdpid_opd 0
+       .endif
+       .byte 0x0f, 0xc7
+       MODRM 0xc0 rdpid_opd 0x7
+.endm
 #endif
 
 #endif
-- 
2.19.1

Reply via email to