[LTP] [PATCH v6] arch_prctl: add ARCH_GET_FS, ARCH_GET_GS tests
dm.chestnykh@gmail.com
dm.chestnykh@gmail.com
Sat Oct 10 08:42:40 CEST 2026
From: Dmitry Chestnykh <dm.chestnykh@gmail.com>
Signed-off-by: Dmitry Chestnykh <dm.chestnykh@gmail.com>
---
configure.ac | 1 +
include/lapi/arch_prctl.h | 9 ++
runtest/syscalls | 1 +
.../kernel/syscalls/arch_prctl/.gitignore | 1 +
.../kernel/syscalls/arch_prctl/arch_prctl02.c | 122 ++++++++++++++++++
5 files changed, 134 insertions(+)
create mode 100644 testcases/kernel/syscalls/arch_prctl/arch_prctl02.c
diff --git a/configure.ac b/configure.ac
index e979de0e7..9db3e06ad 100644
--- a/configure.ac
+++ b/configure.ac
@@ -53,6 +53,7 @@ AC_CHECK_HEADERS_ONCE([ \
aio.h \
asm/ldt.h \
asm/prctl.h \
+ asm/hwcap2.h \
cpuid.h \
emmintrin.h \
ifaddrs.h \
diff --git a/include/lapi/arch_prctl.h b/include/lapi/arch_prctl.h
index a4e7f8458..6d8376e58 100644
--- a/include/lapi/arch_prctl.h
+++ b/include/lapi/arch_prctl.h
@@ -19,4 +19,13 @@
# define ARCH_SET_CPUID 0x1012
#endif
+#ifdef __x86_64__
+# ifdef HAVE_ASM_HWCAP2_H
+# include <asm/hwcap2.h>
+# endif /* HAVE_ASM_HWCAP2_H */
+# ifndef HWCAP2_FSGSBASE
+# define HWCAP2_FSGSBASE (1UL << 1)
+# endif /* HWCAP2_FSGSBASE */
+#endif /* __x86_64__ */
+
#endif /* LAPI_ARCH_PRCTL_H__ */
diff --git a/runtest/syscalls b/runtest/syscalls
index accdf2df9..f2f2a0602 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -32,6 +32,7 @@ alarm06 alarm06
alarm07 alarm07
arch_prctl01 arch_prctl01
+arch_prctl02 arch_prctl02
bind01 bind01
bind02 bind02
diff --git a/testcases/kernel/syscalls/arch_prctl/.gitignore b/testcases/kernel/syscalls/arch_prctl/.gitignore
index 24871e249..72187b8b4 100644
--- a/testcases/kernel/syscalls/arch_prctl/.gitignore
+++ b/testcases/kernel/syscalls/arch_prctl/.gitignore
@@ -1 +1,2 @@
/arch_prctl01
+/arch_prctl02
diff --git a/testcases/kernel/syscalls/arch_prctl/arch_prctl02.c b/testcases/kernel/syscalls/arch_prctl/arch_prctl02.c
new file mode 100644
index 000000000..81295234d
--- /dev/null
+++ b/testcases/kernel/syscalls/arch_prctl/arch_prctl02.c
@@ -0,0 +1,122 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Dmitry Chestnykh <dm.chestnykh@gmail.com>
+ * Author: Dmitry Chestnykh <dm.chestnykh@gmail.com>
+ */
+
+/*\
+ * Test that ``ARCH_GET_FS`` and ``ARCH_GET_GS`` return the same bases read independently
+ * by RDFSBASE and RDGSBASE.
+ */
+
+#include "tst_test.h"
+#include "lapi/syscalls.h"
+#include "lapi/arch_prctl.h"
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef __x86_64__
+
+#ifdef HAVE_GETAUXVAL
+# include <sys/auxv.h>
+#endif
+
+static int arch_prctl_get(int code, unsigned long long *out)
+{
+ return tst_syscall(__NR_arch_prctl, code, out);
+}
+
+static int set_gs(unsigned long long val)
+{
+ return tst_syscall(__NR_arch_prctl, ARCH_SET_GS, val);
+}
+
+static bool tag;
+
+static void setup(void)
+{
+ unsigned long long v = 0;
+
+#ifdef AT_HWCAP2
+ v = getauxval(AT_HWCAP2);
+#endif
+ tag = v & HWCAP2_FSGSBASE ? true : false;
+}
+
+#define READ_FS_BASE() ({ \
+ unsigned long long __res_fs; \
+ asm volatile("rdfsbase %0" : "=r"(__res_fs)); \
+ __res_fs; \
+})
+
+#define READ_GS_BASE() ({ \
+ unsigned long long __res_gs; \
+ asm volatile("rdgsbase %0" : "=r"(__res_gs)); \
+ __res_gs; \
+})
+
+static void test_fs(void)
+{
+ unsigned long long fs = READ_FS_BASE();
+ unsigned long long fs_prctl;
+
+ TST_EXP_PASS_SILENT(arch_prctl_get(ARCH_GET_FS, &fs_prctl));
+ if (TST_PASS)
+ TST_EXP_EQ_LU(fs_prctl, fs);
+}
+
+static void test_gs(void)
+{
+ unsigned long long gs, gs_prctl, orig_gs;
+#define TEST_GS_BASE() do { \
+ gs = READ_GS_BASE(); \
+ TST_EXP_PASS_SILENT(arch_prctl_get(ARCH_GET_GS, &gs_prctl)); \
+ if (TST_PASS) { \
+ TST_EXP_EQ_LU(gs_prctl, gs); \
+ } \
+} while (0)
+
+ TEST_GS_BASE();
+ orig_gs = gs;
+
+ if (gs == 0) {
+ unsigned long long test_gs = 0xabababab;
+
+ TST_EXP_PASS_SILENT(set_gs(test_gs));
+ if (TST_PASS)
+ TEST_GS_BASE();
+
+ set_gs(orig_gs);
+ }
+}
+
+static struct tcase {
+ unsigned int idx;
+ void (*test_fn)(void);
+} tcases[] = {
+ {0, test_fs},
+ {1, test_gs}
+};
+
+static void run(unsigned int index)
+{
+ if (!tag)
+ tst_brk(TCONF, "rdfsbase/rdgsbase are not supported/enabled");
+
+ struct tcase *tc = &tcases[index];
+ (*tc->test_fn)();
+}
+
+static struct tst_test test = {
+ .test = run,
+ .setup = setup,
+ .tcnt = ARRAY_SIZE(tcases),
+ .min_kver = "5.9",
+ .supported_archs = (const char *const []){"x86_64", NULL}
+};
+
+#else
+
+TST_TEST_TCONF("Tests needs x86_64 CPU");
+
+#endif /* __x86_64__ */
--
2.56.0
More information about the ltp
mailing list