-
Notifications
You must be signed in to change notification settings - Fork 0
/
os.c
267 lines (216 loc) · 5.85 KB
/
os.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//
//#define _GNU_SOURCE
//
//#include <assert.h>
//#include <stdlib.h>
//#include <stdio.h>
//#include <err.h>
//#include <sys/mman.h>
//
//#include "os.h"
//
///* 2^20 pages ought to be enough for anybody */
//#define NPAGES (1024*1024)
//
//static void* pages[NPAGES];
//
//uint64_t alloc_page_frame(void)
//{
// static uint64_t nalloc;
// uint64_t ppn;
// void* va;
//
// if (nalloc == NPAGES)
// errx(1, "out of physical memory");
//
// /* OS memory management isn't really this simple */
// ppn = nalloc;
// nalloc++;
//
// va = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
// if (va == MAP_FAILED)
// err(1, "mmap failed");
//
// pages[ppn] = va;
// return ppn;
//}
//
//void* phys_to_virt(uint64_t phys_addr)
//{
// uint64_t ppn = phys_addr >> 12;
// uint64_t off = phys_addr & 0xfff;
// void* va = NULL;
//
// if (ppn < NPAGES)
// va = pages[ppn] + off;
//
// return va;
//}
//
//int main(int argc, char **argv)
//{
// uint64_t pt = alloc_page_frame();
//
// assert(page_table_query(pt, 0xcafe) == NO_MAPPING);
// page_table_update(pt, 0xcafe, 0xf00d);
// assert(page_table_query(pt, 0xcafe) == 0xf00d);
// page_table_update(pt, 0xcafe, NO_MAPPING);
// assert(page_table_query(pt, 0xcafe) == NO_MAPPING);
//
// return 0;
//}
//
#define _GNU_SOURCE
#define _GNU_SOURCE
#include <assert.h>
#include <time.h>
#include <execinfo.h>
#include <stdlib.h>
#include <stdio.h>
#include <err.h>
#include <sys/mman.h>
#include "os.h"
#include "math.h"
/* 2^20 pages ought to be enough for anybody */
#define NPAGES (1024*1024)
#define VPN_MASK 0x1FFFFFFFFFFF
#define PPN_MASK 0xFFFFFFFFFFFFF
static void* pages[NPAGES];
uint64_t alloc_page_frame(void)
{
static uint64_t nalloc;
uint64_t ppn;
void* va;
if (nalloc == NPAGES)
errx(1, "out of physical memory");
/* OS memory management isn't really this simple */
ppn = nalloc;
nalloc++;
va = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (va == MAP_FAILED)
err(1, "mmap failed");
pages[ppn] = va;
return ppn;
}
void* phys_to_virt(uint64_t phys_addr)
{
uint64_t ppn = phys_addr >> 12;
uint64_t off = phys_addr & 0xfff;
void* va = NULL;
if (ppn < NPAGES)
va = pages[ppn] + off;
return va;
}
// TESTS FUNCTIONS
void assert_equal(uint64_t received, uint64_t expected) {
static int counter = 0;
if (expected != received) {
printf("\n\033[0;31mFailed test!\nExpected \033[0m\033[0;32m%llX\033[0m\033[0;31m and received \033[0m\033[0;33m%llX\033[0m\033[0;31m.\033[0m\n", expected, received);
void* callstack[128];
int i, frames = backtrace(callstack, 128);
char** strs = backtrace_symbols(callstack, frames);
printf("\033[0;36m(Almost readable) stacktrace\n");
for (i = 0; i < frames; ++i) {
printf("%s\n", strs[i]);
}
printf("\033[0m\n");
free(strs);
assert(0);
}
if (counter % 500 == 0)
printf("\033[0;32m.\033[0m");
counter++;
}
uint64_t get_random(uint64_t mask) {
return rand() & mask;
}
int in_array(uint64_t *arr, int size, uint64_t value) {
for (int i = 0; i < size; i++)
if (arr[i] == value)
return 1;
return 0;
}
void get_random_list(uint64_t **out, int size, uint64_t mask) {
*out = calloc(size, sizeof(uint64_t));
uint64_t *arr = *out;
int count = 0;
uint64_t val;
while (count < size) {
val = get_random(mask);
if (!in_array(arr, count, val)) {
arr[count] = val;
count++;
}
}
}
uint64_t get_random_vpn() {
return get_random(VPN_MASK);
}
uint64_t get_random_ppn() {
return get_random(VPN_MASK);
}
void update_random_and_check(uint64_t pt) {
uint64_t vpn = get_random_vpn();
uint64_t ppn = get_random_ppn();
if (rand() % 10 < 3)
ppn = NO_MAPPING;
page_table_update(pt, vpn, ppn);
assert_equal(page_table_query(pt, vpn), ppn);
}
void update_many_with_prefix(uint64_t pt) {
int prefix = (rand() % 45) + 1;
uint64_t mask = pow(2, prefix + 1) - 1;
uint64_t vpn_mask = pow(2, (45 - prefix) + 1) - 1;
int amount = (rand() % 20) + 2;
if (amount > vpn_mask / 2)
amount = vpn_mask / 2;
uint64_t block = get_random(mask) << prefix;
uint64_t* vpn_arr;
uint64_t* ppn_arr = malloc(sizeof(uint64_t) * amount);
get_random_list(&vpn_arr, amount, vpn_mask);
for (int i = 0; i < amount; i++) {
vpn_arr[i] = block + vpn_arr[i];
ppn_arr[i] = get_random_ppn();
page_table_update(pt, vpn_arr[i], ppn_arr[i]);
assert_equal(page_table_query(pt, vpn_arr[i]), ppn_arr[i]);
}
for (int i = 0; i < amount; i++) {
uint64_t value = page_table_query(pt, vpn_arr[i]);
uint64_t expected = ppn_arr[i];
if (value != expected) {
printf("Set values:\n");
for (int j = 0; j < amount; j++)
printf("page_table[%llX] = %llX\n", vpn_arr[j], ppn_arr[j]);
printf("\nFailed on index %d,\ngot value %llX instead of %llX\n", i, value, expected);
assert(0);
}
}
free(vpn_arr);
free(ppn_arr);
}
void perform_random_move(uint64_t pt) {
int option = rand() % 2;
switch (option) {
case 0:
update_random_and_check(pt);
break;
case 1:
update_many_with_prefix(pt);
break;
}
}
int main(int argc, char **argv)
{
srand(time(NULL));
uint64_t pt = alloc_page_frame();
assert_equal(page_table_query(pt, 0xcafe), NO_MAPPING);
page_table_update(pt, 0xcafe, 0xf00d);
assert_equal(page_table_query(pt, 0xcafe), 0xf00d);
page_table_update(pt, 0xcafe, NO_MAPPING);
assert_equal(page_table_query(pt, 0xcafe), NO_MAPPING);
for (int i = 0; i < 10000; i++) {
perform_random_move(pt);
}
printf("\nAll tests passed!\n");
return 0;
}