forked from google/rowhammer-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rowhammer_test.cc
148 lines (127 loc) · 3.95 KB
/
rowhammer_test.cc
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
// Copyright 2015, Google, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This is required on Mac OS X for getting PRI* macros #defined.
#define __STDC_FORMAT_MACROS
#include <assert.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
const size_t mem_size = 1 << 30;
const int toggles = 540000;
char *g_mem;
char *pick_addr() {
size_t offset = (rand() << 12) % mem_size;
return g_mem + offset;
}
class Timer {
struct timeval start_time_;
public:
Timer() {
// Note that we use gettimeofday() (with microsecond resolution)
// rather than clock_gettime() (with nanosecond resolution) so
// that this works on Mac OS X, because OS X doesn't provide
// clock_gettime() and we don't really need nanosecond resolution.
int rc = gettimeofday(&start_time_, NULL);
assert(rc == 0);
}
double get_diff() {
struct timeval end_time;
int rc = gettimeofday(&end_time, NULL);
assert(rc == 0);
return (end_time.tv_sec - start_time_.tv_sec
+ (double) (end_time.tv_usec - start_time_.tv_usec) / 1e6);
}
void print_iters(uint64_t iterations) {
double total_time = get_diff();
double iter_time = total_time / iterations;
printf(" %.3f nanosec per iteration: %g sec for %" PRId64 " iterations\n",
iter_time * 1e9, total_time, iterations);
}
};
static void toggle(int iterations, int addr_count) {
Timer t;
for (int j = 0; j < iterations; j++) {
uint32_t *addrs[addr_count];
for (int a = 0; a < addr_count; a++)
addrs[a] = (uint32_t *) pick_addr();
uint32_t sum = 0;
for (int i = 0; i < toggles; i++) {
for (int a = 0; a < addr_count; a++)
sum += *addrs[a] + 1;
for (int a = 0; a < addr_count; a++)
asm volatile("clflush (%0)" : : "r" (addrs[a]) : "memory");
}
// Sanity check. We don't expect this to fail, because reading
// these rows refreshes them.
if (sum != 0) {
printf("error: sum=%x\n", sum);
exit(1);
}
}
t.print_iters(iterations * addr_count * toggles);
}
void main_prog() {
g_mem = (char *) mmap(NULL, mem_size, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0);
assert(g_mem != MAP_FAILED);
printf("clear\n");
memset(g_mem, 0xff, mem_size);
Timer t;
int iter = 0;
for (;;) {
printf("Iteration %i (after %.2fs)\n", iter++, t.get_diff());
toggle(10, 8);
Timer check_timer;
printf("check\n");
uint64_t *end = (uint64_t *) (g_mem + mem_size);
uint64_t *ptr;
int errors = 0;
for (ptr = (uint64_t *) g_mem; ptr < end; ptr++) {
uint64_t got = *ptr;
if (got != ~(uint64_t) 0) {
printf("error at %p: got 0x%" PRIx64 "\n", ptr, got);
errors++;
}
}
printf(" (check took %fs)\n", check_timer.get_diff());
if (errors)
exit(1);
}
}
int main() {
// In case we are running as PID 1, we fork() a subprocess to run
// the test in. Otherwise, if process 1 exits or crashes, this will
// cause a kernel panic (which can cause a reboot or just obscure
// log output and prevent console scrollback from working).
int pid = fork();
if (pid == 0) {
main_prog();
_exit(1);
}
int status;
if (waitpid(pid, &status, 0) == pid) {
printf("** exited with status %i (0x%x)\n", status, status);
}
for (;;) {
sleep(999);
}
return 0;
}