-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsender.cpp
More file actions
226 lines (163 loc) · 4.76 KB
/
Copy pathsender.cpp
File metadata and controls
226 lines (163 loc) · 4.76 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include"util.hpp"
using namespace std;
class ConfigSender
{
public:
bool debug_mode;
uint64_t period; // in microseconds
char* buffer;
list<ADDR_PTR> eviction_list;
void print_eviction_list()
{
cout << "Dumping eviction list.." << endl;
list<ADDR_PTR>::iterator i;
for(i = eviction_list.begin();
i != eviction_list.end();
i++)
{
cout << *i << endl;
}
}
};
/*
Adds all addresses in buffer to the eviction list.
Buffer is the size of the L3 cache.
*/
void build_eviction_list(ConfigSender* configuration)
{
int line_offsets = log2(CACHE_LINESIZE);
int sets = log2(CACHE_L3_SETS);
// Create buffer at least as large as the L3 cache
// TODO what is c?
int buffer_size = CACHE_L3_ASSOC * exp2(line_offsets + sets);
configuration->buffer = (char*) malloc(buffer_size);
for(int i=0; i < CACHE_L3_SETS ; i++)
{
for(int j=0; j < CACHE_L3_ASSOC; j++)
{
int idx = (int)(i * exp2(line_offsets)) + (int)(j * exp2(line_offsets + sets));
ADDR_PTR addr = (ADDR_PTR) &(configuration->buffer[idx]);
// Focus fire on a single cache set. In particular, cache set 0.
if (cache_set_index(addr) == 0x0) {
configuration->eviction_list.push_back(addr);
}
}
}
}
/*
Send a zero bit to the receiver through the covert channel.
To send zero, the cache is NOT flushed.
Stalls for configuration.period microseconds.
*/
void send_zero(ConfigSender* configuration)
{
clock_t start;
start = clock();
while(clock() - start < configuration->period){
// do nothing
}
}
/*
Send a one bit to the receiver through the covert channel.
To send one, the entire LLC cache is flushed.
Flush until configuration.period microseconds has passed.
*/
void send_one(ConfigSender* configuration)
{
clock_t start = clock();
clock_t dt = clock() - start;
while(dt < configuration->period)
{
list<ADDR_PTR>::iterator i;
for(i=configuration->eviction_list.begin();
i != configuration->eviction_list.end();
i++)
{
ADDR_PTR address = (ADDR_PTR) *i;
CLFLUSH(address);
}
dt = clock() - start;
}
}
/*
Parse command line input flags.
-d for debug mode
-p [double] to specify a different period length (in microseconds)
*/
void parse_input_flags(ConfigSender* configuration, int argc, char** argv)
{
int option = 0;
string usage = "Usage: ./sender -d -p 170\n";
string help = "\t-d for debug mode.\n\t-p to specify a period in microseconds. (must match with receiver)\n";
while ((option = getopt(argc, argv, "dp:")) != -1) {
switch (option) {
case 'd':
cout << "DEBUG MODE" << endl;
configuration->debug_mode = true;
break;
case 'p':
configuration->period = atoi(optarg);
cout << "Setting period to " << configuration->period << endl;
break;
case '?':
cout << usage + help << endl;
exit(1);
default:
exit(1);
}
}
}
int main(int argc, char **argv)
{
// setup
clock_t begin, end;
ConfigSender configuration = ConfigSender();
configuration.debug_mode = false;
configuration.period = PERIOD;
parse_input_flags(&configuration, argc, argv);
build_eviction_list(&configuration);
// if(configuration.debug_mode)
// configuration.print_eviction_list();
bool sending = true;
while (sending) {
char text_buf[128];
int i;
printf("Please type a message.\n");
fgets(text_buf, sizeof(text_buf), stdin);
// Turn buffer into binary (8 bits per character)
char* binary_payload = convert_to_binary(text_buf);
if(configuration.debug_mode)
cout << "binary message\n" << binary_payload << endl;
// START THE CLOCK!
begin = clock();
// send an initiate sequence 10011011
send_one(&configuration);
send_zero(&configuration);
send_zero(&configuration);
send_one(&configuration);
send_one(&configuration);
send_zero(&configuration);
send_one(&configuration);
send_one(&configuration);
// send payload
size_t payload_length = strlen(binary_payload);
for(i=0; i<payload_length; i++)
{
if(binary_payload[i] == '1')
{
// flush LLC
send_one(&configuration);
}else{
// do nothing
send_zero(&configuration);
}
}
// STOP THE CLOCK!
end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
double bitrate = (double) strlen(text_buf) / (double) time_spent;
cout << "\nbitrate (B/s): " << bitrate << endl;
}
printf("Sender finished.\n");
return 0;
}