-
Notifications
You must be signed in to change notification settings - Fork 8
/
nop_timer.c
147 lines (131 loc) · 5.1 KB
/
nop_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Environmental Characterization & Response
* Copyright (C) 2018 Assured Information Security, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "main.h"
#include "nop_timer.h"
struct task_struct *nop_th;
struct task_struct *instr_th;
unsigned long count;
bool reset_count;
bool print_count;
bool finish;
int nop_f(void *unused) {
allow_signal(SIGKILL);
/* Run NOP thread until signalled to stop by module cleanup */
while ( !kthread_should_stop() ) {
/* Check for thread kill signal */
if ( signal_pending(nop_th) )
break;
/* Check for reset counter signal from INSTR thread */
if ( reset_count == true ) {
count = 0;
reset_count = false;
}
/* Check for print counter signal from INSTR thread */
if ( print_count == true ) {
pr_info("%lu\n", count);
print_count = false;
}
/* Run NOP command and increment counter */
asm volatile ("nop");
count++;
}
do_exit(0);
return 0;
}
int instr_f(void *unused) {
unsigned long flags;
extern uintptr_t *vmem_aligned;
allow_signal(SIGKILL);
/* Run series of instruction timings */
pr_info("Aggregating NOP based timings\n");
NOP_TIMING_LOOP(xor_f(), "XOR", 100)
NOP_TIMING_LOOP(cpuid_f(), "CPUID", 100)
NOP_TIMING_LOOP(rdtsc_f(), "RDTSC", 100)
NOP_TIMING_LOOP(rdtscp_f(), "RDTSCP", 100)
NOP_TIMING_LOOP(invd_f(), "INVD", 100)
NOP_TIMING_LOOP(clts_f(), "CLTS", 100)
NOP_TIMING_LOOP(inb_f(), "INB", 100)
NOP_TIMING_LOOP(outb_f(), "OUTB", 100)
NOP_TIMING_LOOP(invlpg_f(), "INVLPG", 100)
/* NOP_TIMING_LOOP(invpcid_f(), "INVPCID", 100) */
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_from_cr8_f(), "MOV_FROM_CR8", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_to_cr0_f(), "MOV_TO_CR0", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_to_cr3_f(), "MOV_TO_CR3", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_to_cr4_f(), "MOV_TO_CR4", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_to_cr8_f(), "MOV_TO_CR8", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_dr0_f(), "MOV_DR0", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_dr1_f(), "MOV_DR1", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_dr2_f(), "MOV_DR2", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_dr3_f(), "MOV_DR3", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_dr4_f(), "MOV_DR4", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_dr5_f(), "MOV_DR5", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_dr6_f(), "MOV_DR6", 100, flags)
NOP_TIMING_LOOP_SUSPEND_INTERRUPTS(mov_dr7_f(), "MOV_DR7", 100, flags)
NOP_TIMING_LOOP(rdmsr_f(), "RDMSR", 100)
NOP_TIMING_LOOP(rdpmc_f(), "RDPMC", 100)
NOP_TIMING_LOOP_BOOT_FEATURE_CHECK(rdrand_f(), "RDRAND", 100, X86_FEATURE_RDRAND)
NOP_TIMING_LOOP_BOOT_FEATURE_CHECK(rdseed_f(), "RDSEED", 100, X86_FEATURE_RDSEED)
NOP_TIMING_LOOP(wbinvd_f(), "WBINVD", 100)
NOP_TIMING_LOOP(wrmsr_f(), "WRMSR", 100)
NOP_TIMING_LOOP_CHECK_FOR_KMALLOC(movntdqa_f(), "MOVNTDQA", 100)
NOP_TIMING_LOOP_CHECK_FOR_KMALLOC(movntdq_f(), "MOVNTDQ", 100)
finish = true;
do_exit(0);
return 0;
}
int buffer_flush_instr_f(void *unused) {
allow_signal(SIGKILL);
/* Run series of instruction timings */
pr_info("Aggregating buffer flush NOP based timing\n");
NOP_TIMING_LOOP_CHECK_FOR_KMALLOC(clflush_f(), "CLFLUSH", 100)
NOP_TIMING_LOOP(sfence_f(), "SFENCE", 100)
NOP_TIMING_LOOP(mfence_f(), "MFENCE", 100)
finish = true;
do_exit(0);
return 0;
}
int nop_timing_start(bool action) {
count = 0;
reset_count = false;
print_count = false;
finish = false;
/* Create NOP thread and INSTR thread */
nop_th = kthread_create(nop_f, NULL, "nop");
if ( action ) {
instr_th = kthread_create(instr_f, NULL, "instr");
} else {
instr_th = kthread_create(buffer_flush_instr_f, NULL, "instr");
}
if ( nop_th && instr_th ) {
/* Bind threads to separate CPUs */
kthread_bind(nop_th, 0);
kthread_bind(instr_th, 1);
/* Start running threads */
wake_up_process(nop_th);
wake_up_process(instr_th);
}
/* Wait until INSTR thread has finished */
while ( finish == false ) {
ssleep(1);
}
/* Send stop signals to threads if not already finished */
kthread_stop(nop_th);
kthread_stop(instr_th);
return 0;
}