[LTP] [PATCH v2] arch_prctl: add ARCH_GET_FS, ARCH_GET_GS tests
dm.chestnykh@gmail.com
dm.chestnykh@gmail.com
Fri Oct 9 17:52:24 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 | 87 +++++++++++++++++++
5 files changed, 99 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..180c64b27
--- /dev/null
+++ b/testcases/kernel/syscalls/arch_prctl/arch_prctl02.c
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Dmitry Chestnykh <dm.chestnykh@gmail.com>
+ * Author: Dmitry Chestnykh <dm.chestnykh@gmail.com>
+ */
+
+/*\
+ * Simple test on arch_prctl to get FS_BASE and GS_BASE
+ */
+
+#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 *out)
+{
+ return tst_syscall(__NR_arch_prctl, code, out);
+}
+
+static bool tag;
+
+static void setup(void)
+{
+ unsigned 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; \
+ asm volatile("rdfsbase %0" : "=r"(__res)); \
+ __res; \
+})
+
+#define READ_GS_BASE() ({ \
+ unsigned long long __res; \
+ asm volatile("rdgsbase %0" : "=r"(__res)); \
+ __res; \
+})
+
+static void run(unsigned int index)
+{
+ (void)index;
+ if (!tag)
+ tst_brk(TCONF, "rdfsbase/rdgsbase are not supported/enabled");
+
+ unsigned long fs = READ_FS_BASE();
+ unsigned long gs = READ_GS_BASE();
+ unsigned long fs_prctl, gs_prctl;
+
+ TST_EXP_PASS_SILENT(arch_prctl_get(ARCH_GET_FS, &fs_prctl));
+ if (TST_PASS)
+ TST_EXP_EQ_LU(fs_prctl, fs);
+ else
+ tst_res(TFAIL, "got wrong fs register value");
+
+ TST_EXP_PASS_SILENT(arch_prctl_get(ARCH_GET_GS, &gs_prctl));
+ if (TST_PASS)
+ TST_EXP_EQ_LU(gs_prctl, gs);
+ else
+ tst_res(TFAIL, "got wrong gs register value");
+}
+
+static struct tst_test test = {
+ .test = run,
+ .setup = setup,
+ .tcnt = 1,
+ .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