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
28 # define MAGIC_SYSCALL_1 (__NR_syscalls + 1) /* Bad Linux syscall number */
30 # define MAGIC_SYSCALL_1 (0xff00) /* Bad Linux syscall number */
34 * To test returning from a sigsys with selector blocked, the test
35 * requires some per-architecture support (i.e. knowledge about the
36 * signal trampoline address). On i386, we know it is on the vdso, and
37 * a small trampoline is open-coded for x86_64. Other architectures
38 * that have a trampoline in the vdso will support TEST_BLOCKED_RETURN
39 * out of the box, but don't enable them until they support syscall user
42 #if defined(__x86_64__) || defined(__i386__)
43 #define TEST_BLOCKED_RETURN
47 void* (syscall_dispatcher_start)(void);
48 void* (syscall_dispatcher_end)(void);
50 unsigned long syscall_dispatcher_start = 0;
51 unsigned long syscall_dispatcher_end = 0;
54 unsigned long trapped_call_count = 0;
55 unsigned long native_call_count = 0;
58 #define SYSCALL_BLOCK (selector = PR_SYS_DISPATCH_ON)
59 #define SYSCALL_UNBLOCK (selector = PR_SYS_DISPATCH_OFF)
61 #define CALIBRATION_STEP 100000
62 #define CALIBRATE_TO_SECS 5
65 static double one_sysinfo_step(void)
67 struct timespec t1, t2;
71 clock_gettime(CLOCK_MONOTONIC, &t1);
72 for (i = 0; i < CALIBRATION_STEP; i++)
74 clock_gettime(CLOCK_MONOTONIC, &t2);
75 return (t2.tv_sec - t1.tv_sec) + 1.0e-9 * (t2.tv_nsec - t1.tv_nsec);
78 static void calibrate_set(void)
82 printf("Calibrating test set to last ~%d seconds...\n", CALIBRATE_TO_SECS);
85 elapsed += one_sysinfo_step();
86 factor += CALIBRATE_TO_SECS;
89 printf("test iterations = %d\n", CALIBRATION_STEP * factor);
92 static double perf_syscall(void)
97 for (i = 0; i < factor; ++i)
98 partial += one_sysinfo_step()/(CALIBRATION_STEP*factor);
102 static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)
109 /* printf and friends are not signal-safe. */
110 len = snprintf(buf, 1024, "Caught sys_%x\n", info->si_syscall);
113 if (info->si_syscall == MAGIC_SYSCALL_1)
114 trapped_call_count++;
118 #ifdef TEST_BLOCKED_RETURN
123 __asm__ volatile("movq $0xf, %rax");
124 __asm__ volatile("leaveq");
125 __asm__ volatile("add $0x8, %rsp");
126 __asm__ volatile("syscall_dispatcher_start:");
127 __asm__ volatile("syscall");
128 __asm__ volatile("nop"); /* Landing pad within dispatcher area */
129 __asm__ volatile("syscall_dispatcher_end:");
136 struct sigaction act;
141 memset(&act, 0, sizeof(act));
144 act.sa_sigaction = handle_sigsys;
145 act.sa_flags = SA_SIGINFO;
150 time1 = perf_syscall();
151 printf("Avg syscall time %.0lfns.\n", time1 * 1.0e9);
153 ret = sigaction(SIGSYS, &act, NULL);
155 perror("Error sigaction:");
159 fprintf(stderr, "Enabling syscall trapping.\n");
161 if (prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON,
162 syscall_dispatcher_start,
163 (syscall_dispatcher_end - syscall_dispatcher_start + 1),
165 perror("prctl failed\n");
170 syscall(MAGIC_SYSCALL_1);
172 #ifdef TEST_BLOCKED_RETURN
173 if (selector == PR_SYS_DISPATCH_OFF) {
174 fprintf(stderr, "Failed to return with selector blocked.\n");
181 if (!trapped_call_count) {
182 fprintf(stderr, "syscall trapping does not work.\n");
186 time2 = perf_syscall();
188 if (native_call_count) {
189 perror("syscall trapping intercepted more syscalls than expected\n");
193 printf("trapped_call_count %lu, native_call_count %lu.\n",
194 trapped_call_count, native_call_count);
195 printf("Avg syscall time %.0lfns.\n", time2 * 1.0e9);
196 printf("Interception overhead: %.1lf%% (+%.0lfns).\n",
197 100.0 * (time2 / time1 - 1.0), 1.0e9 * (time2 - time1));