fsgsbase.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fsgsbase.c, an fsgsbase test
  4. * Copyright (c) 2014-2016 Andy Lutomirski
  5. */
  6. #define _GNU_SOURCE
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <stdbool.h>
  10. #include <string.h>
  11. #include <sys/syscall.h>
  12. #include <unistd.h>
  13. #include <err.h>
  14. #include <sys/user.h>
  15. #include <asm/prctl.h>
  16. #include <sys/prctl.h>
  17. #include <signal.h>
  18. #include <limits.h>
  19. #include <sys/ucontext.h>
  20. #include <sched.h>
  21. #include <linux/futex.h>
  22. #include <pthread.h>
  23. #include <asm/ldt.h>
  24. #include <sys/mman.h>
  25. #include <stddef.h>
  26. #include <sys/ptrace.h>
  27. #include <sys/wait.h>
  28. #include <setjmp.h>
  29. #ifndef __x86_64__
  30. # error This test is 64-bit only
  31. #endif
  32. static volatile sig_atomic_t want_segv;
  33. static volatile unsigned long segv_addr;
  34. static unsigned short *shared_scratch;
  35. static int nerrs;
  36. static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
  37. int flags)
  38. {
  39. struct sigaction sa;
  40. memset(&sa, 0, sizeof(sa));
  41. sa.sa_sigaction = handler;
  42. sa.sa_flags = SA_SIGINFO | flags;
  43. sigemptyset(&sa.sa_mask);
  44. if (sigaction(sig, &sa, 0))
  45. err(1, "sigaction");
  46. }
  47. static void clearhandler(int sig)
  48. {
  49. struct sigaction sa;
  50. memset(&sa, 0, sizeof(sa));
  51. sa.sa_handler = SIG_DFL;
  52. sigemptyset(&sa.sa_mask);
  53. if (sigaction(sig, &sa, 0))
  54. err(1, "sigaction");
  55. }
  56. static void sigsegv(int sig, siginfo_t *si, void *ctx_void)
  57. {
  58. ucontext_t *ctx = (ucontext_t*)ctx_void;
  59. if (!want_segv) {
  60. clearhandler(SIGSEGV);
  61. return; /* Crash cleanly. */
  62. }
  63. want_segv = false;
  64. segv_addr = (unsigned long)si->si_addr;
  65. ctx->uc_mcontext.gregs[REG_RIP] += 4; /* Skip the faulting mov */
  66. }
  67. static jmp_buf jmpbuf;
  68. static void sigill(int sig, siginfo_t *si, void *ctx_void)
  69. {
  70. siglongjmp(jmpbuf, 1);
  71. }
  72. static bool have_fsgsbase;
  73. static inline unsigned long rdgsbase(void)
  74. {
  75. unsigned long gsbase;
  76. asm volatile("rdgsbase %0" : "=r" (gsbase) :: "memory");
  77. return gsbase;
  78. }
  79. static inline unsigned long rdfsbase(void)
  80. {
  81. unsigned long fsbase;
  82. asm volatile("rdfsbase %0" : "=r" (fsbase) :: "memory");
  83. return fsbase;
  84. }
  85. static inline void wrgsbase(unsigned long gsbase)
  86. {
  87. asm volatile("wrgsbase %0" :: "r" (gsbase) : "memory");
  88. }
  89. enum which_base { FS, GS };
  90. static unsigned long read_base(enum which_base which)
  91. {
  92. unsigned long offset;
  93. /*
  94. * Unless we have FSGSBASE, there's no direct way to do this from
  95. * user mode. We can get at it indirectly using signals, though.
  96. */
  97. want_segv = true;
  98. offset = 0;
  99. if (which == FS) {
  100. /* Use a constant-length instruction here. */
  101. asm volatile ("mov %%fs:(%%rcx), %%rax" : : "c" (offset) : "rax");
  102. } else {
  103. asm volatile ("mov %%gs:(%%rcx), %%rax" : : "c" (offset) : "rax");
  104. }
  105. if (!want_segv)
  106. return segv_addr + offset;
  107. /*
  108. * If that didn't segfault, try the other end of the address space.
  109. * Unless we get really unlucky and run into the vsyscall page, this
  110. * is guaranteed to segfault.
  111. */
  112. offset = (ULONG_MAX >> 1) + 1;
  113. if (which == FS) {
  114. asm volatile ("mov %%fs:(%%rcx), %%rax"
  115. : : "c" (offset) : "rax");
  116. } else {
  117. asm volatile ("mov %%gs:(%%rcx), %%rax"
  118. : : "c" (offset) : "rax");
  119. }
  120. if (!want_segv)
  121. return segv_addr + offset;
  122. abort();
  123. }
  124. static void check_gs_value(unsigned long value)
  125. {
  126. unsigned long base;
  127. unsigned short sel;
  128. printf("[RUN]\tARCH_SET_GS to 0x%lx\n", value);
  129. if (syscall(SYS_arch_prctl, ARCH_SET_GS, value) != 0)
  130. err(1, "ARCH_SET_GS");
  131. asm volatile ("mov %%gs, %0" : "=rm" (sel));
  132. base = read_base(GS);
  133. if (base == value) {
  134. printf("[OK]\tGSBASE was set as expected (selector 0x%hx)\n",
  135. sel);
  136. } else {
  137. nerrs++;
  138. printf("[FAIL]\tGSBASE was not as expected: got 0x%lx (selector 0x%hx)\n",
  139. base, sel);
  140. }
  141. if (syscall(SYS_arch_prctl, ARCH_GET_GS, &base) != 0)
  142. err(1, "ARCH_GET_GS");
  143. if (base == value) {
  144. printf("[OK]\tARCH_GET_GS worked as expected (selector 0x%hx)\n",
  145. sel);
  146. } else {
  147. nerrs++;
  148. printf("[FAIL]\tARCH_GET_GS was not as expected: got 0x%lx (selector 0x%hx)\n",
  149. base, sel);
  150. }
  151. }
  152. static void mov_0_gs(unsigned long initial_base, bool schedule)
  153. {
  154. unsigned long base, arch_base;
  155. printf("[RUN]\tARCH_SET_GS to 0x%lx then mov 0 to %%gs%s\n", initial_base, schedule ? " and schedule " : "");
  156. if (syscall(SYS_arch_prctl, ARCH_SET_GS, initial_base) != 0)
  157. err(1, "ARCH_SET_GS");
  158. if (schedule)
  159. usleep(10);
  160. asm volatile ("mov %0, %%gs" : : "rm" (0));
  161. base = read_base(GS);
  162. if (syscall(SYS_arch_prctl, ARCH_GET_GS, &arch_base) != 0)
  163. err(1, "ARCH_GET_GS");
  164. if (base == arch_base) {
  165. printf("[OK]\tGSBASE is 0x%lx\n", base);
  166. } else {
  167. nerrs++;
  168. printf("[FAIL]\tGSBASE changed to 0x%lx but kernel reports 0x%lx\n", base, arch_base);
  169. }
  170. }
  171. static volatile unsigned long remote_base;
  172. static volatile unsigned int ftx;
  173. /*
  174. * ARCH_SET_FS/GS(0) may or may not program a selector of zero. HARD_ZERO
  175. * means to force the selector to zero to improve test coverage.
  176. */
  177. #define HARD_ZERO 0xa1fa5f343cb85fa4
  178. static void do_remote_base()
  179. {
  180. unsigned long to_set = remote_base;
  181. bool hard_zero = false;
  182. if (to_set == HARD_ZERO) {
  183. to_set = 0;
  184. hard_zero = true;
  185. }
  186. if (syscall(SYS_arch_prctl, ARCH_SET_GS, to_set) != 0)
  187. err(1, "ARCH_SET_GS");
  188. if (hard_zero)
  189. asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
  190. unsigned short sel;
  191. asm volatile ("mov %%gs, %0" : "=rm" (sel));
  192. printf("\tother thread: ARCH_SET_GS(0x%lx)%s -- sel is 0x%hx\n",
  193. to_set, hard_zero ? " and clear gs" : "", sel);
  194. }
  195. static __thread int set_thread_area_entry_number = -1;
  196. static unsigned short load_gs(void)
  197. {
  198. /*
  199. * Sets GS != 0 and GSBASE != 0 but arranges for the kernel to think
  200. * that GSBASE == 0 (i.e. thread.gsbase == 0).
  201. */
  202. /* Step 1: tell the kernel that we have GSBASE == 0. */
  203. if (syscall(SYS_arch_prctl, ARCH_SET_GS, 0) != 0)
  204. err(1, "ARCH_SET_GS");
  205. /* Step 2: change GSBASE without telling the kernel. */
  206. struct user_desc desc = {
  207. .entry_number = 0,
  208. .base_addr = 0xBAADF00D,
  209. .limit = 0xfffff,
  210. .seg_32bit = 1,
  211. .contents = 0, /* Data, grow-up */
  212. .read_exec_only = 0,
  213. .limit_in_pages = 1,
  214. .seg_not_present = 0,
  215. .useable = 0
  216. };
  217. if (syscall(SYS_modify_ldt, 1, &desc, sizeof(desc)) == 0) {
  218. printf("\tusing LDT slot 0\n");
  219. asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0x7));
  220. return 0x7;
  221. } else {
  222. /* No modify_ldt for us (configured out, perhaps) */
  223. struct user_desc *low_desc = mmap(
  224. NULL, sizeof(desc),
  225. PROT_READ | PROT_WRITE,
  226. MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
  227. memcpy(low_desc, &desc, sizeof(desc));
  228. low_desc->entry_number = set_thread_area_entry_number;
  229. /* 32-bit set_thread_area */
  230. long ret;
  231. asm volatile ("int $0x80"
  232. : "=a" (ret), "+m" (*low_desc)
  233. : "a" (243), "b" (low_desc)
  234. : "r8", "r9", "r10", "r11");
  235. memcpy(&desc, low_desc, sizeof(desc));
  236. munmap(low_desc, sizeof(desc));
  237. if (ret != 0) {
  238. printf("[NOTE]\tcould not create a segment -- test won't do anything\n");
  239. return 0;
  240. }
  241. printf("\tusing GDT slot %d\n", desc.entry_number);
  242. set_thread_area_entry_number = desc.entry_number;
  243. unsigned short gs = (unsigned short)((desc.entry_number << 3) | 0x3);
  244. asm volatile ("mov %0, %%gs" : : "rm" (gs));
  245. return gs;
  246. }
  247. }
  248. void test_wrbase(unsigned short index, unsigned long base)
  249. {
  250. unsigned short newindex;
  251. unsigned long newbase;
  252. printf("[RUN]\tGS = 0x%hx, GSBASE = 0x%lx\n", index, base);
  253. asm volatile ("mov %0, %%gs" : : "rm" (index));
  254. wrgsbase(base);
  255. remote_base = 0;
  256. ftx = 1;
  257. syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
  258. while (ftx != 0)
  259. syscall(SYS_futex, &ftx, FUTEX_WAIT, 1, NULL, NULL, 0);
  260. asm volatile ("mov %%gs, %0" : "=rm" (newindex));
  261. newbase = rdgsbase();
  262. if (newindex == index && newbase == base) {
  263. printf("[OK]\tIndex and base were preserved\n");
  264. } else {
  265. printf("[FAIL]\tAfter switch, GS = 0x%hx and GSBASE = 0x%lx\n",
  266. newindex, newbase);
  267. nerrs++;
  268. }
  269. }
  270. static void *threadproc(void *ctx)
  271. {
  272. while (1) {
  273. while (ftx == 0)
  274. syscall(SYS_futex, &ftx, FUTEX_WAIT, 0, NULL, NULL, 0);
  275. if (ftx == 3)
  276. return NULL;
  277. if (ftx == 1) {
  278. do_remote_base();
  279. } else if (ftx == 2) {
  280. /*
  281. * On AMD chips, this causes GSBASE != 0, GS == 0, and
  282. * thread.gsbase == 0.
  283. */
  284. load_gs();
  285. asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
  286. } else {
  287. errx(1, "helper thread got bad command");
  288. }
  289. ftx = 0;
  290. syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
  291. }
  292. }
  293. static void set_gs_and_switch_to(unsigned long local,
  294. unsigned short force_sel,
  295. unsigned long remote)
  296. {
  297. unsigned long base;
  298. unsigned short sel_pre_sched, sel_post_sched;
  299. bool hard_zero = false;
  300. if (local == HARD_ZERO) {
  301. hard_zero = true;
  302. local = 0;
  303. }
  304. printf("[RUN]\tARCH_SET_GS(0x%lx)%s, then schedule to 0x%lx\n",
  305. local, hard_zero ? " and clear gs" : "", remote);
  306. if (force_sel)
  307. printf("\tBefore schedule, set selector to 0x%hx\n", force_sel);
  308. if (syscall(SYS_arch_prctl, ARCH_SET_GS, local) != 0)
  309. err(1, "ARCH_SET_GS");
  310. if (hard_zero)
  311. asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
  312. if (read_base(GS) != local) {
  313. nerrs++;
  314. printf("[FAIL]\tGSBASE wasn't set as expected\n");
  315. }
  316. if (force_sel) {
  317. asm volatile ("mov %0, %%gs" : : "rm" (force_sel));
  318. sel_pre_sched = force_sel;
  319. local = read_base(GS);
  320. /*
  321. * Signal delivery is quite likely to change a selector
  322. * of 1, 2, or 3 back to 0 due to IRET being defective.
  323. */
  324. asm volatile ("mov %0, %%gs" : : "rm" (force_sel));
  325. } else {
  326. asm volatile ("mov %%gs, %0" : "=rm" (sel_pre_sched));
  327. }
  328. remote_base = remote;
  329. ftx = 1;
  330. syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
  331. while (ftx != 0)
  332. syscall(SYS_futex, &ftx, FUTEX_WAIT, 1, NULL, NULL, 0);
  333. asm volatile ("mov %%gs, %0" : "=rm" (sel_post_sched));
  334. base = read_base(GS);
  335. if (base == local && sel_pre_sched == sel_post_sched) {
  336. printf("[OK]\tGS/BASE remained 0x%hx/0x%lx\n",
  337. sel_pre_sched, local);
  338. } else if (base == local && sel_pre_sched >= 1 && sel_pre_sched <= 3 &&
  339. sel_post_sched == 0) {
  340. /*
  341. * IRET is misdesigned and will squash selectors 1, 2, or 3
  342. * to zero. Don't fail the test just because this happened.
  343. */
  344. printf("[OK]\tGS/BASE changed from 0x%hx/0x%lx to 0x%hx/0x%lx because IRET is defective\n",
  345. sel_pre_sched, local, sel_post_sched, base);
  346. } else {
  347. nerrs++;
  348. printf("[FAIL]\tGS/BASE changed from 0x%hx/0x%lx to 0x%hx/0x%lx\n",
  349. sel_pre_sched, local, sel_post_sched, base);
  350. }
  351. }
  352. static void test_unexpected_base(void)
  353. {
  354. unsigned long base;
  355. printf("[RUN]\tARCH_SET_GS(0), clear gs, then manipulate GSBASE in a different thread\n");
  356. if (syscall(SYS_arch_prctl, ARCH_SET_GS, 0) != 0)
  357. err(1, "ARCH_SET_GS");
  358. asm volatile ("mov %0, %%gs" : : "rm" ((unsigned short)0));
  359. ftx = 2;
  360. syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
  361. while (ftx != 0)
  362. syscall(SYS_futex, &ftx, FUTEX_WAIT, 1, NULL, NULL, 0);
  363. base = read_base(GS);
  364. if (base == 0) {
  365. printf("[OK]\tGSBASE remained 0\n");
  366. } else {
  367. nerrs++;
  368. printf("[FAIL]\tGSBASE changed to 0x%lx\n", base);
  369. }
  370. }
  371. #define USER_REGS_OFFSET(r) offsetof(struct user_regs_struct, r)
  372. static void test_ptrace_write_gs_read_base(void)
  373. {
  374. int status;
  375. pid_t child = fork();
  376. if (child < 0)
  377. err(1, "fork");
  378. if (child == 0) {
  379. printf("[RUN]\tPTRACE_POKE GS, read GSBASE back\n");
  380. printf("[RUN]\tARCH_SET_GS to 1\n");
  381. if (syscall(SYS_arch_prctl, ARCH_SET_GS, 1) != 0)
  382. err(1, "ARCH_SET_GS");
  383. if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0)
  384. err(1, "PTRACE_TRACEME");
  385. raise(SIGTRAP);
  386. _exit(0);
  387. }
  388. wait(&status);
  389. if (WSTOPSIG(status) == SIGTRAP) {
  390. unsigned long base;
  391. unsigned long gs_offset = USER_REGS_OFFSET(gs);
  392. unsigned long base_offset = USER_REGS_OFFSET(gs_base);
  393. /* Read the initial base. It should be 1. */
  394. base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
  395. if (base == 1) {
  396. printf("[OK]\tGSBASE started at 1\n");
  397. } else {
  398. nerrs++;
  399. printf("[FAIL]\tGSBASE started at 0x%lx\n", base);
  400. }
  401. printf("[RUN]\tSet GS = 0x7, read GSBASE\n");
  402. /* Poke an LDT selector into GS. */
  403. if (ptrace(PTRACE_POKEUSER, child, gs_offset, 0x7) != 0)
  404. err(1, "PTRACE_POKEUSER");
  405. /* And read the base. */
  406. base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
  407. if (base == 0 || base == 1) {
  408. printf("[OK]\tGSBASE reads as 0x%lx with invalid GS\n", base);
  409. } else {
  410. nerrs++;
  411. printf("[FAIL]\tGSBASE=0x%lx (should be 0 or 1)\n", base);
  412. }
  413. }
  414. ptrace(PTRACE_CONT, child, NULL, NULL);
  415. wait(&status);
  416. if (!WIFEXITED(status))
  417. printf("[WARN]\tChild didn't exit cleanly.\n");
  418. }
  419. static void test_ptrace_write_gsbase(void)
  420. {
  421. int status;
  422. pid_t child = fork();
  423. if (child < 0)
  424. err(1, "fork");
  425. if (child == 0) {
  426. printf("[RUN]\tPTRACE_POKE(), write GSBASE from ptracer\n");
  427. *shared_scratch = load_gs();
  428. if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != 0)
  429. err(1, "PTRACE_TRACEME");
  430. raise(SIGTRAP);
  431. _exit(0);
  432. }
  433. wait(&status);
  434. if (WSTOPSIG(status) == SIGTRAP) {
  435. unsigned long gs, base;
  436. unsigned long gs_offset = USER_REGS_OFFSET(gs);
  437. unsigned long base_offset = USER_REGS_OFFSET(gs_base);
  438. gs = ptrace(PTRACE_PEEKUSER, child, gs_offset, NULL);
  439. if (gs != *shared_scratch) {
  440. nerrs++;
  441. printf("[FAIL]\tGS is not prepared with nonzero\n");
  442. goto END;
  443. }
  444. if (ptrace(PTRACE_POKEUSER, child, base_offset, 0xFF) != 0)
  445. err(1, "PTRACE_POKEUSER");
  446. gs = ptrace(PTRACE_PEEKUSER, child, gs_offset, NULL);
  447. base = ptrace(PTRACE_PEEKUSER, child, base_offset, NULL);
  448. /*
  449. * In a non-FSGSBASE system, the nonzero selector will load
  450. * GSBASE (again). But what is tested here is whether the
  451. * selector value is changed or not by the GSBASE write in
  452. * a ptracer.
  453. */
  454. if (gs != *shared_scratch) {
  455. nerrs++;
  456. printf("[FAIL]\tGS changed to %lx\n", gs);
  457. /*
  458. * On older kernels, poking a nonzero value into the
  459. * base would zero the selector. On newer kernels,
  460. * this behavior has changed -- poking the base
  461. * changes only the base and, if FSGSBASE is not
  462. * available, this may have no effect once the tracee
  463. * is resumed.
  464. */
  465. if (gs == 0)
  466. printf("\tNote: this is expected behavior on older kernels.\n");
  467. } else if (have_fsgsbase && (base != 0xFF)) {
  468. nerrs++;
  469. printf("[FAIL]\tGSBASE changed to %lx\n", base);
  470. } else {
  471. printf("[OK]\tGS remained 0x%hx", *shared_scratch);
  472. if (have_fsgsbase)
  473. printf(" and GSBASE changed to 0xFF");
  474. printf("\n");
  475. }
  476. }
  477. END:
  478. ptrace(PTRACE_CONT, child, NULL, NULL);
  479. wait(&status);
  480. if (!WIFEXITED(status))
  481. printf("[WARN]\tChild didn't exit cleanly.\n");
  482. }
  483. int main()
  484. {
  485. pthread_t thread;
  486. shared_scratch = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
  487. MAP_ANONYMOUS | MAP_SHARED, -1, 0);
  488. /* Do these tests before we have an LDT. */
  489. test_ptrace_write_gs_read_base();
  490. /* Probe FSGSBASE */
  491. sethandler(SIGILL, sigill, 0);
  492. if (sigsetjmp(jmpbuf, 1) == 0) {
  493. rdfsbase();
  494. have_fsgsbase = true;
  495. printf("\tFSGSBASE instructions are enabled\n");
  496. } else {
  497. printf("\tFSGSBASE instructions are disabled\n");
  498. }
  499. clearhandler(SIGILL);
  500. sethandler(SIGSEGV, sigsegv, 0);
  501. check_gs_value(0);
  502. check_gs_value(1);
  503. check_gs_value(0x200000000);
  504. check_gs_value(0);
  505. check_gs_value(0x200000000);
  506. check_gs_value(1);
  507. for (int sched = 0; sched < 2; sched++) {
  508. mov_0_gs(0, !!sched);
  509. mov_0_gs(1, !!sched);
  510. mov_0_gs(0x200000000, !!sched);
  511. }
  512. /* Set up for multithreading. */
  513. cpu_set_t cpuset;
  514. CPU_ZERO(&cpuset);
  515. CPU_SET(0, &cpuset);
  516. if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
  517. err(1, "sched_setaffinity to CPU 0"); /* should never fail */
  518. if (pthread_create(&thread, 0, threadproc, 0) != 0)
  519. err(1, "pthread_create");
  520. static unsigned long bases_with_hard_zero[] = {
  521. 0, HARD_ZERO, 1, 0x200000000,
  522. };
  523. for (int local = 0; local < 4; local++) {
  524. for (int remote = 0; remote < 4; remote++) {
  525. for (unsigned short s = 0; s < 5; s++) {
  526. unsigned short sel = s;
  527. if (s == 4)
  528. asm ("mov %%ss, %0" : "=rm" (sel));
  529. set_gs_and_switch_to(
  530. bases_with_hard_zero[local],
  531. sel,
  532. bases_with_hard_zero[remote]);
  533. }
  534. }
  535. }
  536. test_unexpected_base();
  537. if (have_fsgsbase) {
  538. unsigned short ss;
  539. asm volatile ("mov %%ss, %0" : "=rm" (ss));
  540. test_wrbase(0, 0);
  541. test_wrbase(0, 1);
  542. test_wrbase(0, 0x200000000);
  543. test_wrbase(0, 0xffffffffffffffff);
  544. test_wrbase(ss, 0);
  545. test_wrbase(ss, 1);
  546. test_wrbase(ss, 0x200000000);
  547. test_wrbase(ss, 0xffffffffffffffff);
  548. }
  549. ftx = 3; /* Kill the thread. */
  550. syscall(SYS_futex, &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
  551. if (pthread_join(thread, NULL) != 0)
  552. err(1, "pthread_join");
  553. test_ptrace_write_gsbase();
  554. return nerrs == 0 ? 0 : 1;
  555. }