xref: /linux/tools/testing/selftests/x86/syscall_nt.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * syscall_nt.c - checks syscalls with NT set
3  * Copyright (c) 2014-2015 Andrew Lutomirski
4  *
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.
8  *
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.
13  *
14  * Some obscure user-space code requires the ability to make system calls
15  * with FLAGS.NT set.  Make sure it works.
16  */
17 
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <sys/syscall.h>
21 #include <asm/processor-flags.h>
22 
23 #ifdef __x86_64__
24 # define WIDTH "q"
25 #else
26 # define WIDTH "l"
27 #endif
28 
29 static unsigned long get_eflags(void)
30 {
31 	unsigned long eflags;
32 	asm volatile ("pushf" WIDTH "\n\tpop" WIDTH " %0" : "=rm" (eflags));
33 	return eflags;
34 }
35 
36 static void set_eflags(unsigned long eflags)
37 {
38 	asm volatile ("push" WIDTH " %0\n\tpopf" WIDTH
39 		      : : "rm" (eflags) : "flags");
40 }
41 
42 int main()
43 {
44 	printf("[RUN]\tSet NT and issue a syscall\n");
45 	set_eflags(get_eflags() | X86_EFLAGS_NT);
46 	syscall(SYS_getpid);
47 	if (get_eflags() & X86_EFLAGS_NT) {
48 		printf("[OK]\tThe syscall worked and NT is still set\n");
49 		return 0;
50 	} else {
51 		printf("[FAIL]\tThe syscall worked but NT was cleared\n");
52 		return 1;
53 	}
54 }
55