18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * fsgsbase.c, an fsgsbase test
48c2ecf20Sopenharmony_ci * Copyright (c) 2014-2016 Andy Lutomirski
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define _GNU_SOURCE
88c2ecf20Sopenharmony_ci#include <stdio.h>
98c2ecf20Sopenharmony_ci#include <stdlib.h>
108c2ecf20Sopenharmony_ci#include <stdbool.h>
118c2ecf20Sopenharmony_ci#include <string.h>
128c2ecf20Sopenharmony_ci#include <sys/syscall.h>
138c2ecf20Sopenharmony_ci#include <unistd.h>
148c2ecf20Sopenharmony_ci#include <err.h>
158c2ecf20Sopenharmony_ci#include <sys/user.h>
168c2ecf20Sopenharmony_ci#include <asm/prctl.h>
178c2ecf20Sopenharmony_ci#include <sys/prctl.h>
188c2ecf20Sopenharmony_ci#include <signal.h>
198c2ecf20Sopenharmony_ci#include <limits.h>
208c2ecf20Sopenharmony_ci#include <sys/ucontext.h>
218c2ecf20Sopenharmony_ci#include <sched.h>
228c2ecf20Sopenharmony_ci#include <linux/futex.h>
238c2ecf20Sopenharmony_ci#include <pthread.h>
248c2ecf20Sopenharmony_ci#include <asm/ldt.h>
258c2ecf20Sopenharmony_ci#include <sys/mman.h>
268c2ecf20Sopenharmony_ci#include <stddef.h>
278c2ecf20Sopenharmony_ci#include <sys/ptrace.h>
288c2ecf20Sopenharmony_ci#include <sys/wait.h>
298c2ecf20Sopenharmony_ci#include <setjmp.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#ifndef __x86_64__
328c2ecf20Sopenharmony_ci# error This test is 64-bit only
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistatic volatile sig_atomic_t want_segv;
368c2ecf20Sopenharmony_cistatic volatile unsigned long segv_addr;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic unsigned short *shared_scratch;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic int nerrs;
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
438c2ecf20Sopenharmony_ci		       int flags)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct sigaction sa;
468c2ecf20Sopenharmony_ci	memset(&sa, 0, sizeof(sa));
478c2ecf20Sopenharmony_ci	sa.sa_sigaction = handler;
488c2ecf20Sopenharmony_ci	sa.sa_flags = SA_SIGINFO | flags;
498c2ecf20Sopenharmony_ci	sigemptyset(&sa.sa_mask);
508c2ecf20Sopenharmony_ci	if (sigaction(sig, &sa, 0))
518c2ecf20Sopenharmony_ci		err(1, "sigaction");
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic void clearhandler(int sig)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct sigaction sa;
578c2ecf20Sopenharmony_ci	memset(&sa, 0, sizeof(sa));
588c2ecf20Sopenharmony_ci	sa.sa_handler = SIG_DFL;
598c2ecf20Sopenharmony_ci	sigemptyset(&sa.sa_mask);
608c2ecf20Sopenharmony_ci	if (sigaction(sig, &sa, 0))
618c2ecf20Sopenharmony_ci		err(1, "sigaction");
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic void sigsegv(int sig, siginfo_t *si, void *ctx_void)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	ucontext_t *ctx = (ucontext_t*)ctx_void;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (!want_segv) {
698c2ecf20Sopenharmony_ci		clearhandler(SIGSEGV);
708c2ecf20Sopenharmony_ci		return;  /* Crash cleanly. */
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	want_segv = false;
748c2ecf20Sopenharmony_ci	segv_addr = (unsigned long)si->si_addr;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	ctx->uc_mcontext.gregs[REG_RIP] += 4;	/* Skip the faulting mov */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic jmp_buf jmpbuf;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void sigill(int sig, siginfo_t *si, void *ctx_void)
838c2ecf20Sopenharmony_ci{
848c2ecf20Sopenharmony_ci	siglongjmp(jmpbuf, 1);
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic bool have_fsgsbase;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic inline unsigned long rdgsbase(void)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	unsigned long gsbase;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory");
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	return gsbase;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic inline unsigned long rdfsbase(void)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	unsigned long fsbase;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory");
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return fsbase;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic inline void wrgsbase(unsigned long gsbase)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory");
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_cistatic inline void wrfsbase(unsigned long fsbase)
1138c2ecf20Sopenharmony_ci{
1148c2ecf20Sopenharmony_ci	asm volatile("wrfsbase %0" :: "r" (fsbase) : "memory");
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cienum which_base { FS, GS };
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic unsigned long read_base(enum which_base which)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	unsigned long offset;
1228c2ecf20Sopenharmony_ci	/*
1238c2ecf20Sopenharmony_ci	 * Unless we have FSGSBASE, there's no direct way to do this from
1248c2ecf20Sopenharmony_ci	 * user mode.  We can get at it indirectly using signals, though.
1258c2ecf20Sopenharmony_ci	 */
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	want_segv = true;
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	offset = 0;
1308c2ecf20Sopenharmony_ci	if (which == FS) {
1318c2ecf20Sopenharmony_ci		/* Use a constant-length instruction here. */
1328c2ecf20Sopenharmony_ci		asm volatile ("mov %%fs:(%%rcx), %%rax" : : "c" (offset) : "rax");
1338c2ecf20Sopenharmony_ci	} else {
1348c2ecf20Sopenharmony_ci		asm volatile ("mov %%gs:(%%rcx), %%rax" : : "c" (offset) : "rax");
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci	if (!want_segv)
1378c2ecf20Sopenharmony_ci		return segv_addr + offset;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/*
1408c2ecf20Sopenharmony_ci	 * If that didn't segfault, try the other end of the address space.
1418c2ecf20Sopenharmony_ci	 * Unless we get really unlucky and run into the vsyscall page, this
1428c2ecf20Sopenharmony_ci	 * is guaranteed to segfault.
1438c2ecf20Sopenharmony_ci	 */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	offset = (ULONG_MAX >> 1) + 1;
1468c2ecf20Sopenharmony_ci	if (which == FS) {
1478c2ecf20Sopenharmony_ci		asm volatile ("mov %%fs:(%%rcx), %%rax"
1488c2ecf20Sopenharmony_ci			      : : "c" (offset) : "rax");
1498c2ecf20Sopenharmony_ci	} else {
1508c2ecf20Sopenharmony_ci		asm volatile ("mov %%gs:(%%rcx), %%rax"
1518c2ecf20Sopenharmony_ci			      : : "c" (offset) : "rax");
1528c2ecf20Sopenharmony_ci	}
1538c2ecf20Sopenharmony_ci	if (!want_segv)
1548c2ecf20Sopenharmony_ci		return segv_addr + offset;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	abort();
1578c2ecf20Sopenharmony_ci}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic void check_gs_value(unsigned long value)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	unsigned long base;
1628c2ecf20Sopenharmony_ci	unsigned short sel;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	printf("[RUN]\tARCH_SET_GS to 0x%lx\n", value);
1658c2ecf20Sopenharmony_ci	if (syscall(SYS_arch_prctl, ARCH_SET_GS, value) != 0)
1668c2ecf20Sopenharmony_ci		err(1, "ARCH_SET_GS");
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	asm volatile ("mov %%gs, %0" : "=rm" (sel));
1698c2ecf20Sopenharmony_ci	base = read_base(GS);
1708c2ecf20Sopenharmony_ci	if (base == value) {
1718c2ecf20Sopenharmony_ci		printf("[OK]\tGSBASE was set as expected (selector 0x%hx)\n",
1728c2ecf20Sopenharmony_ci		       sel);
1738c2ecf20Sopenharmony_ci	} else {
1748c2ecf20Sopenharmony_ci		nerrs++;
1758c2ecf20Sopenharmony_ci		printf("[FAIL]\tGSBASE was not as expected: got 0x%lx (selector 0x%hx)\n",
1768c2ecf20Sopenharmony_ci		       base, sel);
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if (syscall(SYS_arch_prctl, ARCH_GET_GS, &base) != 0)
1808c2ecf20Sopenharmony_ci		err(1, "ARCH_GET_GS");
1818c2ecf20Sopenharmony_ci	if (base == value) {
1828c2ecf20Sopenharmony_ci		printf("[OK]\tARCH_GET_GS worked as expected (selector 0x%hx)\n",
1838c2ecf20Sopenharmony_ci		       sel);
1848c2ecf20Sopenharmony_ci	} else {
1858c2ecf20Sopenharmony_ci		nerrs++;
1868c2ecf20Sopenharmony_ci		printf("[FAIL]\tARCH_GET_GS was not as expected: got 0x%lx (selector 0x%hx)\n",
1878c2ecf20Sopenharmony_ci		       base, sel);
1888c2ecf20Sopenharmony_ci	}
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic void mov_0_gs(unsigned long initial_base, bool schedule)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	unsigned long base, arch_base;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	printf("[RUN]\tARCH_SET_GS to 0x%lx then mov 0 to %%gs%s\n", initial_base, schedule ? " and schedule " : "");
1968c2ecf20Sopenharmony_ci	if (syscall(SYS_arch_prctl, ARCH_SET_GS, initial_base) != 0)
1978c2ecf20Sopenharmony_ci		err(1, "ARCH_SET_GS");
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	if (schedule)
2008c2ecf20Sopenharmony_ci		usleep(10);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	asm volatile ("mov %0, %%gs" : : "rm" (0));
2038c2ecf20Sopenharmony_ci	base = read_base(GS);
2048c2ecf20Sopenharmony_ci	if (syscall(SYS_arch_prctl, ARCH_GET_GS, &arch_base) != 0)
2058c2ecf20Sopenharmony_ci		err(1, "ARCH_GET_GS");
2068c2ecf20Sopenharmony_ci	if (base == arch_base) {
2078c2ecf20Sopenharmony_ci		printf("[OK]\tGSBASE is 0x%lx\n", base);
2088c2ecf20Sopenharmony_ci	} else {
2098c2ecf20Sopenharmony_ci		nerrs++;
2108c2ecf20Sopenharmony_ci		printf("[FAIL]\tGSBASE changed to 0x%lx but kernel reports 0x%lx\n", base, arch_base);
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic volatile unsigned long remote_base;
2158c2ecf20Sopenharmony_cistatic volatile bool remote_hard_zero;
2168c2ecf20Sopenharmony_cistatic volatile unsigned int ftx;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/*
2198c2ecf20Sopenharmony_ci * ARCH_SET_FS/GS(0) may or may not program a selector of zero.  HARD_ZERO
2208c2ecf20Sopenharmony_ci * means to force the selector to zero to improve test coverage.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_ci#define HARD_ZERO 0xa1fa5f343cb85fa4
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic void do_remote_base()
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	unsigned long to_set = remote_base;
2278c2ecf20Sopenharmony_ci	bool hard_zero = false;
2288c2ecf20Sopenharmony_ci	if (to_set == HARD_ZERO) {
2298c2ecf20Sopenharmony_ci		to_set = 0;
2308c2ecf20Sopenharmony_ci		hard_zero = true;
2318c2ecf20Sopenharmony_ci	}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	if (syscall(SYS_arch_prctl, ARCH_SET_GS, to_set) != 0)
2348c2ecf20Sopenharmony_ci		err(1, "ARCH_SET_GS");
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	if (hard_zero)
2378c2ecf20Sopenharmony_ci		asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	unsigned short sel;
2408c2ecf20Sopenharmony_ci	asm volatile ("mov %%gs, %0" : "=rm" (sel));
2418c2ecf20Sopenharmony_ci	printf("\tother thread: ARCH_SET_GS(0x%lx)%s -- sel is 0x%hx\n",
2428c2ecf20Sopenharmony_ci	       to_set, hard_zero ? " and clear gs" : "", sel);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic __thread int set_thread_area_entry_number = -1;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_cistatic unsigned short load_gs(void)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	/*
2508c2ecf20Sopenharmony_ci	 * Sets GS != 0 and GSBASE != 0 but arranges for the kernel to think
2518c2ecf20Sopenharmony_ci	 * that GSBASE == 0 (i.e. thread.gsbase == 0).
2528c2ecf20Sopenharmony_ci	 */
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/* Step 1: tell the kernel that we have GSBASE == 0. */
2558c2ecf20Sopenharmony_ci	if (syscall(SYS_arch_prctl, ARCH_SET_GS, 0) != 0)
2568c2ecf20Sopenharmony_ci		err(1, "ARCH_SET_GS");
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	/* Step 2: change GSBASE without telling the kernel. */
2598c2ecf20Sopenharmony_ci	struct user_desc desc = {
2608c2ecf20Sopenharmony_ci		.entry_number    = 0,
2618c2ecf20Sopenharmony_ci		.base_addr       = 0xBAADF00D,
2628c2ecf20Sopenharmony_ci		.limit           = 0xfffff,
2638c2ecf20Sopenharmony_ci		.seg_32bit       = 1,
2648c2ecf20Sopenharmony_ci		.contents        = 0, /* Data, grow-up */
2658c2ecf20Sopenharmony_ci		.read_exec_only  = 0,
2668c2ecf20Sopenharmony_ci		.limit_in_pages  = 1,
2678c2ecf20Sopenharmony_ci		.seg_not_present = 0,
2688c2ecf20Sopenharmony_ci		.useable         = 0
2698c2ecf20Sopenharmony_ci	};
2708c2ecf20Sopenharmony_ci	if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) == 0) {
2718c2ecf20Sopenharmony_ci		printf("\tusing LDT slot 0\n");
2728c2ecf20Sopenharmony_ci		asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0x7));
2738c2ecf20Sopenharmony_ci		return 0x7;
2748c2ecf20Sopenharmony_ci	} else {
2758c2ecf20Sopenharmony_ci		/* No modify_ldt for us (configured out, perhaps) */
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci		struct user_desc *low_desc = mmap(
2788c2ecf20Sopenharmony_ci			NULL, sizeof(desc),
2798c2ecf20Sopenharmony_ci			PROT_READ | PROT_WRITE,
2808c2ecf20Sopenharmony_ci			MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
2818c2ecf20Sopenharmony_ci		memcpy(low_desc, &desc, sizeof(desc));
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci		low_desc->entry_number = set_thread_area_entry_number;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci		/* 32-bit set_thread_area */
2868c2ecf20Sopenharmony_ci		long ret;
2878c2ecf20Sopenharmony_ci		asm volatile ("int $0x80"
2888c2ecf20Sopenharmony_ci			      : "=a" (ret), "+m" (*low_desc)
2898c2ecf20Sopenharmony_ci			      : "a" (243), "b" (low_desc)
2908c2ecf20Sopenharmony_ci			      : "r8", "r9", "r10", "r11");
2918c2ecf20Sopenharmony_ci		memcpy(&desc, low_desc, sizeof(desc));
2928c2ecf20Sopenharmony_ci		munmap(low_desc, sizeof(desc));
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci		if (ret != 0) {
2958c2ecf20Sopenharmony_ci			printf("[NOTE]\tcould not create a segment -- test won't do anything\n");
2968c2ecf20Sopenharmony_ci			return 0;
2978c2ecf20Sopenharmony_ci		}
2988c2ecf20Sopenharmony_ci		printf("\tusing GDT slot %d\n", desc.entry_number);
2998c2ecf20Sopenharmony_ci		set_thread_area_entry_number = desc.entry_number;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		unsigned short gs = (unsigned short)((desc.entry_number << 3) | 0x3);
3028c2ecf20Sopenharmony_ci		asm volatile ("mov %0, %%gs" : : "rm" (gs));
3038c2ecf20Sopenharmony_ci		return gs;
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_civoid test_wrbase(unsigned short index, unsigned long base)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	unsigned short newindex;
3108c2ecf20Sopenharmony_ci	unsigned long newbase;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	printf("[RUN]\tGS = 0x%hx, GSBASE = 0x%lx\n", index, base);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	asm volatile ("mov %0, %%gs" : : "rm" (index));
3158c2ecf20Sopenharmony_ci	wrgsbase(base);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	remote_base = 0;
3188c2ecf20Sopenharmony_ci	ftx = 1;
3198c2ecf20Sopenharmony_ci	syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
3208c2ecf20Sopenharmony_ci	while (ftx != 0)
3218c2ecf20Sopenharmony_ci		syscall(SYS_futex, &ftx, FUTEX_WAIT, 1, NULL, NULL, 0);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	asm volatile ("mov %%gs, %0" : "=rm" (newindex));
3248c2ecf20Sopenharmony_ci	newbase = rdgsbase();
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	if (newindex == index && newbase == base) {
3278c2ecf20Sopenharmony_ci		printf("[OK]\tIndex and base were preserved\n");
3288c2ecf20Sopenharmony_ci	} else {
3298c2ecf20Sopenharmony_ci		printf("[FAIL]\tAfter switch, GS = 0x%hx and GSBASE = 0x%lx\n",
3308c2ecf20Sopenharmony_ci		       newindex, newbase);
3318c2ecf20Sopenharmony_ci		nerrs++;
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic void *threadproc(void *ctx)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	while (1) {
3388c2ecf20Sopenharmony_ci		while (ftx == 0)
3398c2ecf20Sopenharmony_ci			syscall(SYS_futex, &ftx, FUTEX_WAIT, 0, NULL, NULL, 0);
3408c2ecf20Sopenharmony_ci		if (ftx == 3)
3418c2ecf20Sopenharmony_ci			return NULL;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci		if (ftx == 1) {
3448c2ecf20Sopenharmony_ci			do_remote_base();
3458c2ecf20Sopenharmony_ci		} else if (ftx == 2) {
3468c2ecf20Sopenharmony_ci			/*
3478c2ecf20Sopenharmony_ci			 * On AMD chips, this causes GSBASE != 0, GS == 0, and
3488c2ecf20Sopenharmony_ci			 * thread.gsbase == 0.
3498c2ecf20Sopenharmony_ci			 */
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci			load_gs();
3528c2ecf20Sopenharmony_ci			asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
3538c2ecf20Sopenharmony_ci		} else {
3548c2ecf20Sopenharmony_ci			errx(1, "helper thread got bad command");
3558c2ecf20Sopenharmony_ci		}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci		ftx = 0;
3588c2ecf20Sopenharmony_ci		syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
3598c2ecf20Sopenharmony_ci	}
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic void set_gs_and_switch_to(unsigned long local,
3638c2ecf20Sopenharmony_ci				 unsigned short force_sel,
3648c2ecf20Sopenharmony_ci				 unsigned long remote)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	unsigned long base;
3678c2ecf20Sopenharmony_ci	unsigned short sel_pre_sched, sel_post_sched;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	bool hard_zero = false;
3708c2ecf20Sopenharmony_ci	if (local == HARD_ZERO) {
3718c2ecf20Sopenharmony_ci		hard_zero = true;
3728c2ecf20Sopenharmony_ci		local = 0;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	printf("[RUN]\tARCH_SET_GS(0x%lx)%s, then schedule to 0x%lx\n",
3768c2ecf20Sopenharmony_ci	       local, hard_zero ? " and clear gs" : "", remote);
3778c2ecf20Sopenharmony_ci	if (force_sel)
3788c2ecf20Sopenharmony_ci		printf("\tBefore schedule, set selector to 0x%hx\n", force_sel);
3798c2ecf20Sopenharmony_ci	if (syscall(SYS_arch_prctl, ARCH_SET_GS, local) != 0)
3808c2ecf20Sopenharmony_ci		err(1, "ARCH_SET_GS");
3818c2ecf20Sopenharmony_ci	if (hard_zero)
3828c2ecf20Sopenharmony_ci		asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	if (read_base(GS) != local) {
3858c2ecf20Sopenharmony_ci		nerrs++;
3868c2ecf20Sopenharmony_ci		printf("[FAIL]\tGSBASE wasn't set as expected\n");
3878c2ecf20Sopenharmony_ci	}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	if (force_sel) {
3908c2ecf20Sopenharmony_ci		asm volatile ("mov %0, %%gs" : : "rm" (force_sel));
3918c2ecf20Sopenharmony_ci		sel_pre_sched = force_sel;
3928c2ecf20Sopenharmony_ci		local = read_base(GS);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci		/*
3958c2ecf20Sopenharmony_ci		 * Signal delivery seems to mess up weird selectors.  Put it
3968c2ecf20Sopenharmony_ci		 * back.
3978c2ecf20Sopenharmony_ci		 */
3988c2ecf20Sopenharmony_ci		asm volatile ("mov %0, %%gs" : : "rm" (force_sel));
3998c2ecf20Sopenharmony_ci	} else {
4008c2ecf20Sopenharmony_ci		asm volatile ("mov %%gs, %0" : "=rm" (sel_pre_sched));
4018c2ecf20Sopenharmony_ci	}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	remote_base = remote;
4048c2ecf20Sopenharmony_ci	ftx = 1;
4058c2ecf20Sopenharmony_ci	syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
4068c2ecf20Sopenharmony_ci	while (ftx != 0)
4078c2ecf20Sopenharmony_ci		syscall(SYS_futex, &ftx, FUTEX_WAIT, 1, NULL, NULL, 0);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	asm volatile ("mov %%gs, %0" : "=rm" (sel_post_sched));
4108c2ecf20Sopenharmony_ci	base = read_base(GS);
4118c2ecf20Sopenharmony_ci	if (base == local && sel_pre_sched == sel_post_sched) {
4128c2ecf20Sopenharmony_ci		printf("[OK]\tGS/BASE remained 0x%hx/0x%lx\n",
4138c2ecf20Sopenharmony_ci		       sel_pre_sched, local);
4148c2ecf20Sopenharmony_ci	} else {
4158c2ecf20Sopenharmony_ci		nerrs++;
4168c2ecf20Sopenharmony_ci		printf("[FAIL]\tGS/BASE changed from 0x%hx/0x%lx to 0x%hx/0x%lx\n",
4178c2ecf20Sopenharmony_ci		       sel_pre_sched, local, sel_post_sched, base);
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci}
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic void test_unexpected_base(void)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	unsigned long base;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	printf("[RUN]\tARCH_SET_GS(0), clear gs, then manipulate GSBASE in a different thread\n");
4268c2ecf20Sopenharmony_ci	if (syscall(SYS_arch_prctl, ARCH_SET_GS, 0) != 0)
4278c2ecf20Sopenharmony_ci		err(1, "ARCH_SET_GS");
4288c2ecf20Sopenharmony_ci	asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	ftx = 2;
4318c2ecf20Sopenharmony_ci	syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
4328c2ecf20Sopenharmony_ci	while (ftx != 0)
4338c2ecf20Sopenharmony_ci		syscall(SYS_futex, &ftx, FUTEX_WAIT, 1, NULL, NULL, 0);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	base = read_base(GS);
4368c2ecf20Sopenharmony_ci	if (base == 0) {
4378c2ecf20Sopenharmony_ci		printf("[OK]\tGSBASE remained 0\n");
4388c2ecf20Sopenharmony_ci	} else {
4398c2ecf20Sopenharmony_ci		nerrs++;
4408c2ecf20Sopenharmony_ci		printf("[FAIL]\tGSBASE changed to 0x%lx\n", base);
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci#define USER_REGS_OFFSET(r) offsetof(struct user_regs_struct, r)
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic void test_ptrace_write_gs_read_base(void)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	int status;
4498c2ecf20Sopenharmony_ci	pid_t child = fork();
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	if (child < 0)
4528c2ecf20Sopenharmony_ci		err(1, "fork");
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	if (child == 0) {
4558c2ecf20Sopenharmony_ci		printf("[RUN]\tPTRACE_POKE GS, read GSBASE back\n");
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci		printf("[RUN]\tARCH_SET_GS to 1\n");
4588c2ecf20Sopenharmony_ci		if (syscall(SYS_arch_prctl, ARCH_SET_GS, 1) != 0)
4598c2ecf20Sopenharmony_ci			err(1, "ARCH_SET_GS");
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci		if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0)
4628c2ecf20Sopenharmony_ci			err(1, "PTRACE_TRACEME");
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci		raise(SIGTRAP);
4658c2ecf20Sopenharmony_ci		_exit(0);
4668c2ecf20Sopenharmony_ci	}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	wait(&status);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_ci	if (WSTOPSIG(status) == SIGTRAP) {
4718c2ecf20Sopenharmony_ci		unsigned long base;
4728c2ecf20Sopenharmony_ci		unsigned long gs_offset = USER_REGS_OFFSET(gs);
4738c2ecf20Sopenharmony_ci		unsigned long base_offset = USER_REGS_OFFSET(gs_base);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci		/* Read the initial base.  It should be 1. */
4768c2ecf20Sopenharmony_ci		base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
4778c2ecf20Sopenharmony_ci		if (base == 1) {
4788c2ecf20Sopenharmony_ci			printf("[OK]\tGSBASE started at 1\n");
4798c2ecf20Sopenharmony_ci		} else {
4808c2ecf20Sopenharmony_ci			nerrs++;
4818c2ecf20Sopenharmony_ci			printf("[FAIL]\tGSBASE started at 0x%lx\n", base);
4828c2ecf20Sopenharmony_ci		}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci		printf("[RUN]\tSet GS = 0x7, read GSBASE\n");
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci		/* Poke an LDT selector into GS. */
4878c2ecf20Sopenharmony_ci		if (ptrace(PTRACE_POKEUSER, child, gs_offset, 0x7) != 0)
4888c2ecf20Sopenharmony_ci			err(1, "PTRACE_POKEUSER");
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci		/* And read the base. */
4918c2ecf20Sopenharmony_ci		base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		if (base == 0 || base == 1) {
4948c2ecf20Sopenharmony_ci			printf("[OK]\tGSBASE reads as 0x%lx with invalid GS\n", base);
4958c2ecf20Sopenharmony_ci		} else {
4968c2ecf20Sopenharmony_ci			nerrs++;
4978c2ecf20Sopenharmony_ci			printf("[FAIL]\tGSBASE=0x%lx (should be 0 or 1)\n", base);
4988c2ecf20Sopenharmony_ci		}
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	ptrace(PTRACE_CONT, child, NULL, NULL);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	wait(&status);
5048c2ecf20Sopenharmony_ci	if (!WIFEXITED(status))
5058c2ecf20Sopenharmony_ci		printf("[WARN]\tChild didn't exit cleanly.\n");
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic void test_ptrace_write_gsbase(void)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	int status;
5118c2ecf20Sopenharmony_ci	pid_t child = fork();
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	if (child < 0)
5148c2ecf20Sopenharmony_ci		err(1, "fork");
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	if (child == 0) {
5178c2ecf20Sopenharmony_ci		printf("[RUN]\tPTRACE_POKE(), write GSBASE from ptracer\n");
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci		*shared_scratch = load_gs();
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci		if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0)
5228c2ecf20Sopenharmony_ci			err(1, "PTRACE_TRACEME");
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci		raise(SIGTRAP);
5258c2ecf20Sopenharmony_ci		_exit(0);
5268c2ecf20Sopenharmony_ci	}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	wait(&status);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (WSTOPSIG(status) == SIGTRAP) {
5318c2ecf20Sopenharmony_ci		unsigned long gs, base;
5328c2ecf20Sopenharmony_ci		unsigned long gs_offset = USER_REGS_OFFSET(gs);
5338c2ecf20Sopenharmony_ci		unsigned long base_offset = USER_REGS_OFFSET(gs_base);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci		gs = ptrace(PTRACE_PEEKUSER, child, gs_offset, NULL);
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		if (gs != *shared_scratch) {
5388c2ecf20Sopenharmony_ci			nerrs++;
5398c2ecf20Sopenharmony_ci			printf("[FAIL]\tGS is not prepared with nonzero\n");
5408c2ecf20Sopenharmony_ci			goto END;
5418c2ecf20Sopenharmony_ci		}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci		if (ptrace(PTRACE_POKEUSER, child, base_offset, 0xFF) != 0)
5448c2ecf20Sopenharmony_ci			err(1, "PTRACE_POKEUSER");
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci		gs = ptrace(PTRACE_PEEKUSER, child, gs_offset, NULL);
5478c2ecf20Sopenharmony_ci		base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci		/*
5508c2ecf20Sopenharmony_ci		 * In a non-FSGSBASE system, the nonzero selector will load
5518c2ecf20Sopenharmony_ci		 * GSBASE (again). But what is tested here is whether the
5528c2ecf20Sopenharmony_ci		 * selector value is changed or not by the GSBASE write in
5538c2ecf20Sopenharmony_ci		 * a ptracer.
5548c2ecf20Sopenharmony_ci		 */
5558c2ecf20Sopenharmony_ci		if (gs != *shared_scratch) {
5568c2ecf20Sopenharmony_ci			nerrs++;
5578c2ecf20Sopenharmony_ci			printf("[FAIL]\tGS changed to %lx\n", gs);
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci			/*
5608c2ecf20Sopenharmony_ci			 * On older kernels, poking a nonzero value into the
5618c2ecf20Sopenharmony_ci			 * base would zero the selector.  On newer kernels,
5628c2ecf20Sopenharmony_ci			 * this behavior has changed -- poking the base
5638c2ecf20Sopenharmony_ci			 * changes only the base and, if FSGSBASE is not
5648c2ecf20Sopenharmony_ci			 * available, this may have no effect once the tracee
5658c2ecf20Sopenharmony_ci			 * is resumed.
5668c2ecf20Sopenharmony_ci			 */
5678c2ecf20Sopenharmony_ci			if (gs == 0)
5688c2ecf20Sopenharmony_ci				printf("\tNote: this is expected behavior on older kernels.\n");
5698c2ecf20Sopenharmony_ci		} else if (have_fsgsbase && (base != 0xFF)) {
5708c2ecf20Sopenharmony_ci			nerrs++;
5718c2ecf20Sopenharmony_ci			printf("[FAIL]\tGSBASE changed to %lx\n", base);
5728c2ecf20Sopenharmony_ci		} else {
5738c2ecf20Sopenharmony_ci			printf("[OK]\tGS remained 0x%hx", *shared_scratch);
5748c2ecf20Sopenharmony_ci			if (have_fsgsbase)
5758c2ecf20Sopenharmony_ci				printf(" and GSBASE changed to 0xFF");
5768c2ecf20Sopenharmony_ci			printf("\n");
5778c2ecf20Sopenharmony_ci		}
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ciEND:
5818c2ecf20Sopenharmony_ci	ptrace(PTRACE_CONT, child, NULL, NULL);
5828c2ecf20Sopenharmony_ci	wait(&status);
5838c2ecf20Sopenharmony_ci	if (!WIFEXITED(status))
5848c2ecf20Sopenharmony_ci		printf("[WARN]\tChild didn't exit cleanly.\n");
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ciint main()
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	pthread_t thread;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	shared_scratch = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
5928c2ecf20Sopenharmony_ci			      MAP_ANONYMOUS | MAP_SHARED, -1, 0);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	/* Do these tests before we have an LDT. */
5958c2ecf20Sopenharmony_ci	test_ptrace_write_gs_read_base();
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	/* Probe FSGSBASE */
5988c2ecf20Sopenharmony_ci	sethandler(SIGILL, sigill, 0);
5998c2ecf20Sopenharmony_ci	if (sigsetjmp(jmpbuf, 1) == 0) {
6008c2ecf20Sopenharmony_ci		rdfsbase();
6018c2ecf20Sopenharmony_ci		have_fsgsbase = true;
6028c2ecf20Sopenharmony_ci		printf("\tFSGSBASE instructions are enabled\n");
6038c2ecf20Sopenharmony_ci	} else {
6048c2ecf20Sopenharmony_ci		printf("\tFSGSBASE instructions are disabled\n");
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci	clearhandler(SIGILL);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	sethandler(SIGSEGV, sigsegv, 0);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	check_gs_value(0);
6118c2ecf20Sopenharmony_ci	check_gs_value(1);
6128c2ecf20Sopenharmony_ci	check_gs_value(0x200000000);
6138c2ecf20Sopenharmony_ci	check_gs_value(0);
6148c2ecf20Sopenharmony_ci	check_gs_value(0x200000000);
6158c2ecf20Sopenharmony_ci	check_gs_value(1);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	for (int sched = 0; sched < 2; sched++) {
6188c2ecf20Sopenharmony_ci		mov_0_gs(0, !!sched);
6198c2ecf20Sopenharmony_ci		mov_0_gs(1, !!sched);
6208c2ecf20Sopenharmony_ci		mov_0_gs(0x200000000, !!sched);
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	/* Set up for multithreading. */
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	cpu_set_t cpuset;
6268c2ecf20Sopenharmony_ci	CPU_ZERO(&cpuset);
6278c2ecf20Sopenharmony_ci	CPU_SET(0, &cpuset);
6288c2ecf20Sopenharmony_ci	if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
6298c2ecf20Sopenharmony_ci		err(1, "sched_setaffinity to CPU 0");	/* should never fail */
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	if (pthread_create(&thread, 0, threadproc, 0) != 0)
6328c2ecf20Sopenharmony_ci		err(1, "pthread_create");
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	static unsigned long bases_with_hard_zero[] = {
6358c2ecf20Sopenharmony_ci		0, HARD_ZERO, 1, 0x200000000,
6368c2ecf20Sopenharmony_ci	};
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	for (int local = 0; local < 4; local++) {
6398c2ecf20Sopenharmony_ci		for (int remote = 0; remote < 4; remote++) {
6408c2ecf20Sopenharmony_ci			for (unsigned short s = 0; s < 5; s++) {
6418c2ecf20Sopenharmony_ci				unsigned short sel = s;
6428c2ecf20Sopenharmony_ci				if (s == 4)
6438c2ecf20Sopenharmony_ci					asm ("mov %%ss, %0" : "=rm" (sel));
6448c2ecf20Sopenharmony_ci				set_gs_and_switch_to(
6458c2ecf20Sopenharmony_ci					bases_with_hard_zero[local],
6468c2ecf20Sopenharmony_ci					sel,
6478c2ecf20Sopenharmony_ci					bases_with_hard_zero[remote]);
6488c2ecf20Sopenharmony_ci			}
6498c2ecf20Sopenharmony_ci		}
6508c2ecf20Sopenharmony_ci	}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	test_unexpected_base();
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	if (have_fsgsbase) {
6558c2ecf20Sopenharmony_ci		unsigned short ss;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci		asm volatile ("mov %%ss, %0" : "=rm" (ss));
6588c2ecf20Sopenharmony_ci
6598c2ecf20Sopenharmony_ci		test_wrbase(0, 0);
6608c2ecf20Sopenharmony_ci		test_wrbase(0, 1);
6618c2ecf20Sopenharmony_ci		test_wrbase(0, 0x200000000);
6628c2ecf20Sopenharmony_ci		test_wrbase(0, 0xffffffffffffffff);
6638c2ecf20Sopenharmony_ci		test_wrbase(ss, 0);
6648c2ecf20Sopenharmony_ci		test_wrbase(ss, 1);
6658c2ecf20Sopenharmony_ci		test_wrbase(ss, 0x200000000);
6668c2ecf20Sopenharmony_ci		test_wrbase(ss, 0xffffffffffffffff);
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	ftx = 3;  /* Kill the thread. */
6708c2ecf20Sopenharmony_ci	syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci	if (pthread_join(thread, NULL) != 0)
6738c2ecf20Sopenharmony_ci		err(1, "pthread_join");
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	test_ptrace_write_gsbase();
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	return nerrs == 0 ? 0 : 1;
6788c2ecf20Sopenharmony_ci}
679