cregit-Linux how code gets into the kernel

Release 4.10 tools/testing/selftests/prctl/disable-tsc-ctxt-sw-stress-test.c

/*
 * Tests for prctl(PR_GET_TSC, ...) / prctl(PR_SET_TSC, ...)
 *
 * Tests if the control register is updated correctly
 * at context switches
 *
 * Warning: this test will cause a very high load for a few seconds
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <inttypes.h>
#include <wait.h>


#include <sys/prctl.h>
#include <linux/prctl.h>

/* Get/set the process' ability to use the timestamp counter instruction */
#ifndef PR_GET_TSC

#define PR_GET_TSC 25

#define PR_SET_TSC 26

# define PR_TSC_ENABLE		1   
/* allow the use of the timestamp counter */

# define PR_TSC_SIGSEGV		2   
/* throw a SIGSEGV instead of reading the TSC */
#endif


static uint64_t rdtsc(void) { uint32_t lo, hi; /* We cannot use "=A", since this would use %rax on x86_64 */ __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi)); return (uint64_t)hi << 32 | lo; }

Contributors

PersonTokensPropCommitsCommitProp
erik bosmanerik bosman2385.19%150.00%
peter foleypeter foley414.81%150.00%
Total27100.00%2100.00%


static void sigsegv_expect(int sig) { /* */ }

Contributors

PersonTokensPropCommitsCommitProp
erik bosmanerik bosman990.00%150.00%
peter foleypeter foley110.00%150.00%
Total10100.00%2100.00%


static void segvtask(void) { if (prctl(PR_SET_TSC, PR_TSC_SIGSEGV) < 0) { perror("prctl"); exit(0); } signal(SIGSEGV, sigsegv_expect); alarm(10); rdtsc(); fprintf(stderr, "FATAL ERROR, rdtsc() succeeded while disabled\n"); exit(0); }

Contributors

PersonTokensPropCommitsCommitProp
erik bosmanerik bosman5798.28%150.00%
peter foleypeter foley11.72%150.00%
Total58100.00%2100.00%


static void sigsegv_fail(int sig) { fprintf(stderr, "FATAL ERROR, rdtsc() failed while enabled\n"); exit(0); }

Contributors

PersonTokensPropCommitsCommitProp
erik bosmanerik bosman2095.24%150.00%
peter foleypeter foley14.76%150.00%
Total21100.00%2100.00%


static void rdtsctask(void) { if (prctl(PR_SET_TSC, PR_TSC_ENABLE) < 0) { perror("prctl"); exit(0); } signal(SIGSEGV, sigsegv_fail); alarm(10); for(;;) rdtsc(); }

Contributors

PersonTokensPropCommitsCommitProp
erik bosmanerik bosman5098.04%150.00%
peter foleypeter foley11.96%150.00%
Total51100.00%2100.00%


int main(void) { int n_tasks = 100, i; fprintf(stderr, "[No further output means we're allright]\n"); for (i=0; i<n_tasks; i++) if (fork() == 0) { if (i & 1) segvtask(); else rdtsctask(); } for (i=0; i<n_tasks; i++) wait(NULL); exit(0); }

Contributors

PersonTokensPropCommitsCommitProp
erik bosmanerik bosman7898.73%150.00%
mahesh khanwalkarmahesh khanwalkar11.27%150.00%
Total79100.00%2100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
erik bosmanerik bosman28696.95%133.33%
peter foleypeter foley82.71%133.33%
mahesh khanwalkarmahesh khanwalkar10.34%133.33%
Total295100.00%3100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.