/
pagetable.c
280 lines (243 loc) · 8.09 KB
/
pagetable.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* ============================================================================================
* File Name : pagetable.c
* Name : Seungkyu Kim
* Created Date : Nov.1.2015
* Modified Date : Nov.5.2015
*
* Copyright 2015 Seungkyu Kim all rights reserved
* ============================================================================================
*/
#include <assert.h>
#include <string.h>
#include "sim.h"
#include "pagetable.h"
// The top-level page table (also known as the 'page directory')
pgdir_entry_t pgdir[PTRS_PER_PGDIR];
// Counters for various events.
// Your code must increment these when the related events occur.
int hit_count = 0;
int miss_count = 0;
int ref_count = 0;
int evict_clean_count = 0;
int evict_dirty_count = 0;
/*
* Allocates a frame to be used for the virtual page represented by p.
* If all frames are in use, calls the replacement algorithm's evict_fcn to
* select a victim frame. Writes victim to swap if needed, and updates
* pagetable entry for victim to indicate that virtual page is no longer in
* (simulated) physical memory.
*
* Counters for evictions should be updated appropriately in this function.
*/
int allocate_frame(pgtbl_entry_t *p) {
int i;
int frame = -1;
for(i = 0; i < memsize; i++) {
if(!coremap[i].in_use) {
frame = i;
break;
}
}
if(frame == -1) { // Didn't find a free page.
// Call replacement algorithm's evict function to select victim
frame = evict_fcn();
// All frames were in use, so victim frame must hold some page
// Write victim page to swap, if needed, and update pagetable
// IMPLEMENTATION NEEDED
// Take pte from coremap with given frame
pgtbl_entry_t *pte = coremap[frame].pte;
// Swap old one with the given frame
if(pte->frame & PG_DIRTY){
pte->swap_off = swap_pageout(frame, pte->swap_off);
pte->frame = pte->frame & ~PG_DIRTY;
pte->frame = pte->frame | PG_ONSWAP;
// Increase count for evict_dirty
evict_dirty_count++;
}else{
// Increase count for evict_clean
evict_clean_count++;
}
// Frame is invalid
pte->frame = pte->frame & ~PG_VALID;
}
// Record information for virtual page that will now be stored in frame
coremap[frame].in_use = 1;
coremap[frame].pte = p;
coremap[frame].length = p->length;
return frame;
}
/*
* Initializes the top-level pagetable.
* This function is called once at the start of the simulation.
* For the simulation, there is a single "process" whose reference trace is
* being simulated, so there is just one top-level page table (page directory).
* To keep things simple, we use a global array of 'page directory entries'.
*
* In a real OS, each process would have its own page directory, which would
* need to be allocated and initialized as part of process creation.
*/
void init_pagetable() {
int i;
// Set all entries in top-level pagetable to 0, which ensures valid
// bits are all 0 initially.
for (i=0; i < PTRS_PER_PGDIR; i++) {
pgdir[i].pde = 0;
}
}
// For simulation, we get second-level pagetables from ordinary memory
pgdir_entry_t init_second_level() {
int i;
pgdir_entry_t new_entry;
pgtbl_entry_t *pgtbl;
// Allocating aligned memory ensures the low bits in the pointer must
// be zero, so we can use them to store our status bits, like PG_VALID
if (posix_memalign((void **)&pgtbl, PAGE_SIZE,
PTRS_PER_PGTBL*sizeof(pgtbl_entry_t)) != 0) {
perror("Failed to allocate aligned memory for page table");
exit(1);
}
// Initialize all entries in second-level pagetable
for (i=0; i < PTRS_PER_PGTBL; i++) {
pgtbl[i].frame = 0; // sets all bits, including valid, to zero
pgtbl[i].swap_off = INVALID_SWAP;
}
// Mark the new page directory entry as valid
new_entry.pde = (uintptr_t)pgtbl | PG_VALID;
return new_entry;
}
/*
* Initializes the content of a (simulated) physical memory frame when it
* is first allocated for some virtual address. Just like in a real OS,
* we fill the frame with zero's to prevent leaking information across
* pages.
*
* In our simulation, we also store the the virtual address itself in the
* page frame to help with error checking.
*
*/
void init_frame(int frame, addr_t vaddr) {
// Calculate pointer to start of frame in (simulated) physical memory
char *mem_ptr = &physmem[frame*SIMPAGESIZE];
// Calculate pointer to location in page where we keep the vaddr
addr_t *vaddr_ptr = (addr_t *)(mem_ptr + sizeof(int));
memset(mem_ptr, 0, SIMPAGESIZE); // zero-fill the frame
*vaddr_ptr = vaddr; // record the vaddr for error checking
return;
}
/*
* Locate the physical frame number for the given vaddr using the page table.
*
* If the entry is invalid and not on swap, then this is the first reference
* to the page and a (simulated) physical frame should be allocated and
* initialized (using init_frame).
*
* If the entry is invalid and on swap, then a (simulated) physical frame
* should be allocated and filled by reading the page data from swap.
*
* Counters for hit, miss and reference events should be incremented in
* this function.
*/
char *find_physpage(addr_t vaddr, char type) {
pgtbl_entry_t *p=NULL; // pointer to the full page table entry for vaddr
unsigned idx = PGDIR_INDEX(vaddr); // get index into page directory
// IMPLEMENTATION NEEDED
// Use top-level page directory to get pointer to 2nd-level page table
if (!(pgdir[idx].pde & PG_VALID)){
pgdir[idx] = init_second_level();
}
// Use vaddr to get index into 2nd-level page table and initialize 'p'
uintptr_t ptr_table = PAGE_MASK & pgdir[idx].pde;
p = (pgtbl_entry_t*)(ptr_table) + PGTBL_INDEX(vaddr);
// Check if p is valid or not, on swap or not, and handle appropriately
// Page is present in the memory
if (p->frame & PG_VALID){
hit_count++;
// Page is not present in the memory
} else {
miss_count++;
ref_count++;
int frame = allocate_frame(p);
// Page on disk.
if (p->frame & PG_ONSWAP){
swap_pagein(frame, p->swap_off);
p->frame = frame << PAGE_SHIFT;
p->frame = p->frame | PG_VALID;
// Page not on disk, first time access the page
} else {
init_frame(frame, vaddr);
p->frame = frame << PAGE_SHIFT;
p->frame = p->frame | PG_DIRTY;
}
}
// Make sure that p is marked valid. Also mark it dirty
// if the access type indicates that the page will be written to
p->frame = p->frame | PG_VALID;
if(type == 'S' || type == 'M'){
p->frame = p->frame | PG_DIRTY;
}
// Call replacement algorithm's ref_fcn for this page
ref_fcn(p);
// p is marked referenced.
p->frame = p->frame | PG_REF;
// Return pointer into (simulated) physical memory at start of frame
return &physmem[(p->frame >> PAGE_SHIFT)*SIMPAGESIZE];
}
void print_pagetbl(pgtbl_entry_t *pgtbl) {
int i;
int first_invalid, last_invalid;
first_invalid = last_invalid = -1;
for (i=0; i < PTRS_PER_PGTBL; i++) {
if (!(pgtbl[i].frame & PG_VALID) &&
!(pgtbl[i].frame & PG_ONSWAP)) {
if (first_invalid == -1) {
first_invalid = i;
}
last_invalid = i;
} else {
if (first_invalid != -1) {
printf("\t[%d] - [%d]: INVALID\n",
first_invalid, last_invalid);
first_invalid = last_invalid = -1;
}
printf("\t[%d]: ",i);
if (pgtbl[i].frame & PG_VALID) {
printf("VALID, ");
if (pgtbl[i].frame & PG_DIRTY) {
printf("DIRTY, ");
}
printf("in frame %d\n",pgtbl[i].frame >> PAGE_SHIFT);
} else {
assert(pgtbl[i].frame & PG_ONSWAP);
printf("ONSWAP, at offset %lu\n",pgtbl[i].swap_off);
}
}
}
if (first_invalid != -1) {
printf("\t[%d] - [%d]: INVALID\n", first_invalid, last_invalid);
first_invalid = last_invalid = -1;
}
}
void print_pagedirectory() {
int i; // index into pgdir
int first_invalid,last_invalid;
first_invalid = last_invalid = -1;
pgtbl_entry_t *pgtbl;
for (i=0; i < PTRS_PER_PGDIR; i++) {
if (!(pgdir[i].pde & PG_VALID)) {
if (first_invalid == -1) {
first_invalid = i;
}
last_invalid = i;
} else {
if (first_invalid != -1) {
printf("[%d]: INVALID\n to\n[%d]: INVALID\n",
first_invalid, last_invalid);
first_invalid = last_invalid = -1;
}
pgtbl = (pgtbl_entry_t *)(pgdir[i].pde & PAGE_MASK);
printf("[%d]: %p\n",i, pgtbl);
print_pagetbl(pgtbl);
}
}
}