2 * syscall_nt.c - checks syscalls with NT set
3 * Copyright (c) 2014-2015 Andrew Lutomirski
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * Some obscure user-space code requires the ability to make system calls
15 * with FLAGS.NT set. Make sure it works.
23 #include <sys/syscall.h>
24 #include <asm/processor-flags.h>
32 static unsigned int nerrs;
34 static unsigned long get_eflags(void)
37 asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
41 static void set_eflags(unsigned long eflags)
43 asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
44 : : "rm" (eflags) : "flags");
47 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
51 memset(&sa, 0, sizeof(sa));
52 sa.sa_sigaction = handler;
53 sa.sa_flags = SA_SIGINFO | flags;
54 sigemptyset(&sa.sa_mask);
55 if (sigaction(sig, &sa, 0))
59 static void sigtrap(int sig, siginfo_t *si, void *ctx_void)
63 static void do_it(unsigned long extraflags)
67 set_eflags(get_eflags() | extraflags);
70 set_eflags(X86_EFLAGS_IF | X86_EFLAGS_FIXED);
71 if ((flags & extraflags) == extraflags) {
72 printf("[OK]\tThe syscall worked and flags are still set\n");
74 printf("[FAIL]\tThe syscall worked but flags were cleared (flags = 0x%lx but expected 0x%lx set)\n",
82 printf("[RUN]\tSet NT and issue a syscall\n");
86 * Now try it again with TF set -- TF forces returns via IRET in all
87 * cases except non-ptregs-using 64-bit full fast path syscalls.
90 sethandler(SIGTRAP, sigtrap, 0);
92 printf("[RUN]\tSet NT|TF and issue a syscall\n");
93 do_it(X86_EFLAGS_NT | X86_EFLAGS_TF);
95 return nerrs == 0 ? 0 : 1;