[LTP] [PATCH] arch_prctl: add ARCH_GET_FS, ARCH_GET_GS tests

dm.chestnykh@gmail.com dm.chestnykh@gmail.com
Thu Oct 8 21:47:25 CEST 2026


From: Dmitry Chestnykh <dm.chestnykh@gmail.com>

Signed-off-by: Dmitry Chestnykh <dm.chestnykh@gmail.com>
---
 .../kernel/syscalls/arch_prctl/.gitignore     |  1 +
 testcases/kernel/syscalls/arch_prctl/Makefile |  2 +
 .../kernel/syscalls/arch_prctl/arch_prctl02.c | 81 +++++++++++++++++++
 3 files changed, 84 insertions(+)
 create mode 100644 testcases/kernel/syscalls/arch_prctl/arch_prctl02.c

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/Makefile b/testcases/kernel/syscalls/arch_prctl/Makefile
index 272949d57..c4413a81f 100644
--- a/testcases/kernel/syscalls/arch_prctl/Makefile
+++ b/testcases/kernel/syscalls/arch_prctl/Makefile
@@ -6,3 +6,5 @@ top_srcdir		?= ../../../..
 include $(top_srcdir)/include/mk/testcases.mk

 include $(top_srcdir)/include/mk/generic_leaf_target.mk
+
+CFLAGS += -mfsgsbase
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..124dd13b9
--- /dev/null
+++ b/testcases/kernel/syscalls/arch_prctl/arch_prctl02.c
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * 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>
+
+#include <sys/auxv.h>
+#ifndef HWCAP2_FSGSBASE
+#if defined(__has_include) && __has_include(<asm/hwcap2.h>)
+#include <asm/hwcap2.h>
+#else
+#warning "Cannot obtain HWCAP2_FSGSBASE from system headers"
+#define HWCAP2_FSGSBASE (1UL << 1)
+#endif /* defined(__has_include) && __has_include(<asm/hwcap2.h>) */
+#endif /* HWCAP2_FSGSBASE */
+
+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 = getauxval(AT_HWCAP2);
+
+	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;
+
+	TEST(arch_prctl_get(ARCH_GET_FS, &fs_prctl));
+	if (TST_RET == 0 && fs_prctl == fs)
+		tst_res(TPASS, "get fs succeed");
+	else
+		tst_res(TFAIL, "got wrong fs register value");
+
+	TEST(arch_prctl_get(ARCH_GET_GS, &gs_prctl));
+	if (TST_RET == 0 && gs_prctl == gs)
+		tst_res(TPASS, "get gs succeed");
+	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}
+};
--
2.56.0



More information about the ltp mailing list