WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / x86 / syscall_nt.c
bloba108b80dd082309ed24e0c3fd687c3353210c71b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * syscall_nt.c - checks syscalls with NT set
4 * Copyright (c) 2014-2015 Andrew Lutomirski
6 * Some obscure user-space code requires the ability to make system calls
7 * with FLAGS.NT set. Make sure it works.
8 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include <signal.h>
14 #include <err.h>
15 #include <sys/syscall.h>
17 #include "helpers.h"
19 static unsigned int nerrs;
21 static void sethandler(int sig, void (*handler)(int, siginfo_t *, void *),
22 int flags)
24 struct sigaction sa;
25 memset(&sa, 0, sizeof(sa));
26 sa.sa_sigaction = handler;
27 sa.sa_flags = SA_SIGINFO | flags;
28 sigemptyset(&sa.sa_mask);
29 if (sigaction(sig, &sa, 0))
30 err(1, "sigaction");
33 static void sigtrap(int sig, siginfo_t *si, void *ctx_void)
37 static void do_it(unsigned long extraflags)
39 unsigned long flags;
41 set_eflags(get_eflags() | extraflags);
42 syscall(SYS_getpid);
43 flags = get_eflags();
44 set_eflags(X86_EFLAGS_IF | X86_EFLAGS_FIXED);
45 if ((flags & extraflags) == extraflags) {
46 printf("[OK]\tThe syscall worked and flags are still set\n");
47 } else {
48 printf("[FAIL]\tThe syscall worked but flags were cleared (flags = 0x%lx but expected 0x%lx set)\n",
49 flags, extraflags);
50 nerrs++;
54 int main(void)
56 printf("[RUN]\tSet NT and issue a syscall\n");
57 do_it(X86_EFLAGS_NT);
59 printf("[RUN]\tSet AC and issue a syscall\n");
60 do_it(X86_EFLAGS_AC);
62 printf("[RUN]\tSet NT|AC and issue a syscall\n");
63 do_it(X86_EFLAGS_NT | X86_EFLAGS_AC);
66 * Now try it again with TF set -- TF forces returns via IRET in all
67 * cases except non-ptregs-using 64-bit full fast path syscalls.
70 sethandler(SIGTRAP, sigtrap, 0);
72 printf("[RUN]\tSet TF and issue a syscall\n");
73 do_it(X86_EFLAGS_TF);
75 printf("[RUN]\tSet NT|TF and issue a syscall\n");
76 do_it(X86_EFLAGS_NT | X86_EFLAGS_TF);
78 printf("[RUN]\tSet AC|TF and issue a syscall\n");
79 do_it(X86_EFLAGS_AC | X86_EFLAGS_TF);
81 printf("[RUN]\tSet NT|AC|TF and issue a syscall\n");
82 do_it(X86_EFLAGS_NT | X86_EFLAGS_AC | X86_EFLAGS_TF);
85 * Now try DF. This is evil and it's plausible that we will crash
86 * glibc, but glibc would have to do something rather surprising
87 * for this to happen.
89 printf("[RUN]\tSet DF and issue a syscall\n");
90 do_it(X86_EFLAGS_DF);
92 printf("[RUN]\tSet TF|DF and issue a syscall\n");
93 do_it(X86_EFLAGS_TF | X86_EFLAGS_DF);
95 return nerrs == 0 ? 0 : 1;