1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2020 Collabora Ltd.
5 * Benchmark and test syscall user dispatch
17 #include <sys/sysinfo.h>
18 #include <sys/prctl.h>
19 #include <sys/syscall.h>
21 #ifndef PR_SET_SYSCALL_USER_DISPATCH
22 # define PR_SET_SYSCALL_USER_DISPATCH 59
23 # define PR_SYS_DISPATCH_OFF 0
24 # define PR_SYS_DISPATCH_ON 1
25 # define SYSCALL_DISPATCH_FILTER_ALLOW 0
26 # define SYSCALL_DISPATCH_FILTER_BLOCK 1
30 # define MAGIC_SYSCALL_1 (__NR_syscalls + 1) /* Bad Linux syscall number */
32 # define MAGIC_SYSCALL_1 (0xff00) /* Bad Linux syscall number */
36 * To test returning from a sigsys with selector blocked, the test
37 * requires some per-architecture support (i.e. knowledge about the
38 * signal trampoline address). On i386, we know it is on the vdso, and
39 * a small trampoline is open-coded for x86_64. Other architectures
40 * that have a trampoline in the vdso will support TEST_BLOCKED_RETURN
41 * out of the box, but don't enable them until they support syscall user
44 #if defined(__x86_64__) || defined(__i386__)
45 #define TEST_BLOCKED_RETURN
49 void* (syscall_dispatcher_start)(void);
50 void* (syscall_dispatcher_end)(void);
52 unsigned long syscall_dispatcher_start = 0;
53 unsigned long syscall_dispatcher_end = 0;
56 unsigned long trapped_call_count = 0;
57 unsigned long native_call_count = 0;
60 #define SYSCALL_BLOCK (selector = SYSCALL_DISPATCH_FILTER_BLOCK)
61 #define SYSCALL_UNBLOCK (selector = SYSCALL_DISPATCH_FILTER_ALLOW)
63 #define CALIBRATION_STEP 100000
64 #define CALIBRATE_TO_SECS 5
67 static double one_sysinfo_step(void)
69 struct timespec t1, t2;
73 clock_gettime(CLOCK_MONOTONIC, &t1);
74 for (i = 0; i < CALIBRATION_STEP; i++)
76 clock_gettime(CLOCK_MONOTONIC, &t2);
77 return (t2.tv_sec - t1.tv_sec) + 1.0e-9 * (t2.tv_nsec - t1.tv_nsec);
80 static void calibrate_set(void)
84 printf("Calibrating test set to last ~%d seconds...\n", CALIBRATE_TO_SECS);
87 elapsed += one_sysinfo_step();
88 factor += CALIBRATE_TO_SECS;
91 printf("test iterations = %d\n", CALIBRATION_STEP * factor);
94 static double perf_syscall(void)
99 for (i = 0; i < factor; ++i)
100 partial += one_sysinfo_step()/(CALIBRATION_STEP*factor);
104 static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)
111 /* printf and friends are not signal-safe. */
112 len = snprintf(buf, 1024, "Caught sys_%x\n", info->si_syscall);
115 if (info->si_syscall == MAGIC_SYSCALL_1)
116 trapped_call_count++;
120 #ifdef TEST_BLOCKED_RETURN
125 __asm__ volatile("movq $0xf, %rax");
126 __asm__ volatile("leaveq");
127 __asm__ volatile("add $0x8, %rsp");
128 __asm__ volatile("syscall_dispatcher_start:");
129 __asm__ volatile("syscall");
130 __asm__ volatile("nop"); /* Landing pad within dispatcher area */
131 __asm__ volatile("syscall_dispatcher_end:");
138 struct sigaction act;
143 memset(&act, 0, sizeof(act));
146 act.sa_sigaction = handle_sigsys;
147 act.sa_flags = SA_SIGINFO;
152 time1 = perf_syscall();
153 printf("Avg syscall time %.0lfns.\n", time1 * 1.0e9);
155 ret = sigaction(SIGSYS, &act, NULL);
157 perror("Error sigaction:");
161 fprintf(stderr, "Enabling syscall trapping.\n");
163 if (prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON,
164 syscall_dispatcher_start,
165 (syscall_dispatcher_end - syscall_dispatcher_start + 1),
167 perror("prctl failed\n");
172 syscall(MAGIC_SYSCALL_1);
174 #ifdef TEST_BLOCKED_RETURN
175 if (selector == SYSCALL_DISPATCH_FILTER_ALLOW) {
176 fprintf(stderr, "Failed to return with selector blocked.\n");
183 if (!trapped_call_count) {
184 fprintf(stderr, "syscall trapping does not work.\n");
188 time2 = perf_syscall();
190 if (native_call_count) {
191 perror("syscall trapping intercepted more syscalls than expected\n");
195 printf("trapped_call_count %lu, native_call_count %lu.\n",
196 trapped_call_count, native_call_count);
197 printf("Avg syscall time %.0lfns.\n", time2 * 1.0e9);
198 printf("Interception overhead: %.1lf%% (+%.0lfns).\n",
199 100.0 * (time2 / time1 - 1.0), 1.0e9 * (time2 - time1));