-
Notifications
You must be signed in to change notification settings - Fork 3
/
pageTable.c
393 lines (288 loc) · 11.8 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Author: Chinmay, Divya
* Date: 21/3/20
*
* Contains the ADT of the Page Table
*/
#include <stdio.h>
#include <stdlib.h>
#include "dataTypes.h"
#include "pageTable.h"
#include "frameTable.h"
#include "segmentTable.h"
#include "pcb.h"
#include "configuration.h"
/*
3 level paging required since linear address size is 32 bits and frame size is 1KB(so 10 bit offset) and
each page table entry need 32 bits
*/
extern PCB pcbArr[30];
extern FILE *outputFile;
/*
Initializes all 3 levels of page table by dynamically allocating memory
for ADTs correxponding to page tables and their frames.
Input: Read/Write permissions of pages pointed to by level 1 page table
Output: Pointer to level 3 page table
*/
pageTable* initPageTable(int readWrite){
pageTable *level3PageTableptr, *level2PageTableptr, *level1PageTableptr;
frameOfPageTable *level3PageTableFramesptr,*level2PageTableFramesptr,*level1PageTableFramesptr;
level1PageTableptr = (pageTable*) calloc(1, sizeof(pageTable));
level2PageTableptr = (pageTable*) calloc(1, sizeof(pageTable));
level3PageTableptr = (pageTable*) calloc(1, sizeof(pageTable));
level3PageTableFramesptr = (frameOfPageTable*) calloc(NUMBER_OF_PAGES_IN_LEVEL_3_PAGE_TABLE, sizeof(frameOfPageTable));
level2PageTableFramesptr = (frameOfPageTable*) calloc(NUMBER_OF_PAGES_IN_LEVEL_2_PAGE_TABLE, sizeof(frameOfPageTable));
level1PageTableFramesptr = (frameOfPageTable*) calloc(NUMBER_OF_PAGES_IN_LEVEL_1_PAGE_TABLE, sizeof(frameOfPageTable));
level3PageTableptr->frames = level3PageTableFramesptr;
level2PageTableptr->frames = level2PageTableFramesptr;
level1PageTableptr->frames = level1PageTableFramesptr;
int i,j;
for(i = 0; i < NUMBER_OF_PAGES_IN_LEVEL_1_PAGE_TABLE; i++){
for(j = 0; j < NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE; j++){
level1PageTableptr->frames[i].entries[j].present = 0;
level1PageTableptr->frames[i].entries[j].modified = 0;
// level1PageTableptr->frames[i].entries[j].readWrite = 0;
if(readWrite == 0){
//Its a read only segment
level1PageTableptr->frames[i].entries[j].readWrite = 0;
}
else{
level1PageTableptr->frames[i].entries[j].readWrite = 1;
}
}
}
for(i = 0; i < NUMBER_OF_PAGES_IN_LEVEL_2_PAGE_TABLE; i++){
for(j = 0; j < NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE; j++){
level2PageTableptr->frames[i].entries[j].present = 0;
level2PageTableptr->frames[i].entries[j].modified = 0;
level2PageTableptr->frames[i].entries[j].cachingDisabled = 1;
level2PageTableptr->frames[i].entries[j].readWrite = 1;
}
}
for(i = 0; i < NUMBER_OF_PAGES_IN_LEVEL_3_PAGE_TABLE; i++){
for(j = 0; j < NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE; j++){
level3PageTableptr->frames[i].entries[j].present = 0;
level3PageTableptr->frames[i].entries[j].modified = 0;
level3PageTableptr->frames[i].entries[j].cachingDisabled = 1;
level3PageTableptr->frames[i].entries[j].readWrite = 1;
}
}
level3PageTableptr->nextLevelPageTablePointer = level2PageTableptr;
level2PageTableptr->nextLevelPageTablePointer = level1PageTableptr;
level1PageTableptr->nextLevelPageTablePointer = NULL;
return level3PageTableptr;
}
/*
This function returns physical frame number for a given linear address.
Also updates LFU count in frame table
Input: pointer to level 3 page table, linear address, whether access is read or write,
double pointer to store pointer to store pointer to page table in which page fault occured,
pointer to store page number which caused page fault,
pointer to store level of page in whcih page fault occured
Output: 0 if search successful,
-1 if page fault,
-2 if invalid linear address
*/
int searchPageTable(pageTable* level3PageTable,pageTable** ptref,unsigned int linearAddr, unsigned int readWrite,
unsigned int* pageFaultPageNumber, unsigned int* level){
// fprintf(outputFile, "Inside searchPageTable\n");
pageTable* level2PageTable = level3PageTable->nextLevelPageTablePointer;
pageTable* level1PageTable = level2PageTable->nextLevelPageTablePointer;
// First 6 bits from left used for indexing level 3
unsigned int level3Index = linearAddr >> 26;
fprintf(outputFile, "PageTable: linear addr:%x\tindex3:%x\n",linearAddr,level3Index);
if (level3Index > 64)
{
fprintf(outputFile, "Page Table: Error: Invalid address\n"); //level 3 page table has only 8 entries
return -2;
}
unsigned int frameNumberOfLevel2PageTable,indexOfLevel2PageTable=level3Index;
if(level3PageTable->frames[0].entries[level3Index].present == 1){
frameNumberOfLevel2PageTable = level3PageTable->frames[0].entries[level3Index].frameNumber;
updateLfuCount(frameNumberOfLevel2PageTable);
}
else{
fprintf(outputFile, "Page Table: Page fault in level 2 page table\n");
*level=2;
*pageFaultPageNumber = level3Index;
*ptref = level3PageTable;
return -1;
}
// We have got frame number of level 2 page table
unsigned int level2Index = (linearAddr >> 18) & 0x000000FF;
fprintf(outputFile, "Page Table: level2index: %d, frameof page table index: %d\n",level2Index,indexOfLevel2PageTable);
unsigned int frameNumberOfLevel1PageTable,indexOfLevel1PageTable=linearAddr>>18;
if(level2PageTable->frames[indexOfLevel2PageTable].entries[level2Index].present == 1){
frameNumberOfLevel1PageTable = level2PageTable->frames[indexOfLevel2PageTable].entries[level2Index].frameNumber;
updateLfuCount(frameNumberOfLevel1PageTable);
}
else{
fprintf(outputFile, "Page Table : Page fault in level 1 page table\n");
*level=1;
*pageFaultPageNumber = linearAddr >> 18;
*ptref = level2PageTable;
return -1;
}
// We have got frame number of level 1 page table
unsigned int level1Index = (linearAddr>>10) & 0x000000FF;
unsigned int frameNumberOfProcess;
if(level1PageTable->frames[indexOfLevel1PageTable].entries[level1Index].present == 1){
//Checking read/write permissions
if(level1PageTable->frames[indexOfLevel1PageTable].entries[level1Index].readWrite == 0 && readWrite == 1){
//Page is read only but we are trying to write
fprintf(outputFile, "Page Table: Write permission denied. The page is a read only page.\n");
}
frameNumberOfProcess = level1PageTable->frames[indexOfLevel1PageTable].entries[level1Index].frameNumber;
updateLfuCount(frameNumberOfProcess);
}
else{
fprintf(outputFile, "Page Table: Page fault in process\n");
*level = 0;
*pageFaultPageNumber = linearAddr >> 10;
*ptref = level1PageTable;
return -1;
}
//Return the frame number. The calling function should concatenate this frame number with offset to get physical address
return frameNumberOfProcess;
}
/*
Sets or resets modified bit of a page table entry
Input: Pointer to page table, index of entry to modify, new value of modified bit
Output: 0 on success
*/
int updatePageTableModifiedBit(pageTable* pageTableptr,unsigned int index, int value){
unsigned int pageNumber = index / NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE;
unsigned int entryNumber = index % NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE;
pageTableptr->frames[pageNumber].entries[entryNumber].modified = value;
fprintf(outputFile, "Page Table: Modified bit of entry# %d in page number %d has been set to %d\n",entryNumber,pageNumber,value);
return 0;
}
/*
Sets or resets present bit of a page table entry
Input: Pointer to page table, index of entry to modify, new value of present bit
Output: 0 on success
*/
int updatePageTablePresentBit(pageTable *pT, unsigned int index, int value){
unsigned int pageNumber = index / NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE;
unsigned int entryNumber = index % NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE;
pT->frames[pageNumber].entries[entryNumber].present = value;
fprintf(outputFile, "Page Table: Present bit of entry# %d in page number %d has been set to %d\n",entryNumber,pageNumber,value);
return 0;
}
/*
Input: Pointer to page table
Output: Pointer to next level page table
*/
pageTable* getPointertoNextLevelPageTable(pageTable* pTPointer){
return pTPointer->nextLevelPageTablePointer;
}
/*
Takes pid, segment number in LDT and level of page table required as input
and returns a pointer to the required page table
Input: pid, segment number, level of page table to return
Output: pointer to page table of specified level
*/
pageTable* getPageTableFromPid(unsigned int pid,unsigned int segNum,unsigned int level){
// Access the PCB of the process and use pointers to get access to req page table
//Get PCB
PCB pcb = pcbArr[pid];
//
pageTable* pT3Pointer = getLevel3PageTablePointer(pcb,segNum);
pageTable* pT2Pointer;
pageTable* pT1Pointer;
if(level == 3){
return pT3Pointer;
}
else if(level == 2){
pT2Pointer = getPointertoNextLevelPageTable(pT3Pointer);
return pT2Pointer;
}
else if(level == 1){
pT2Pointer = getPointertoNextLevelPageTable(pT3Pointer);
pT1Pointer = getPointertoNextLevelPageTable(pT2Pointer);
return pT1Pointer;
}
}
/*
Sets the frameNumber field of the entry in the pageTable pointed
to by pT with specified index to value
Input: pointer to page table, index of entry to modify, frame number assigned to the page
Output: 0 on success
*/
int setFrameNo(pageTable *pT, unsigned int index, int value)
{
unsigned int pageNumber = index / NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE;
unsigned int entryNumber = index % NUMBER_OF_ENTRIES_PER_PAGE_IN_PAGE_TABLE;
pT->frames[pageNumber].entries[entryNumber].frameNumber = value;
return 0;
}
/*
Finds the page number of first code page and first data page of a process
Input: Name of linear address input file, name of segment number input file,
pointer to store page number of first code page,
pointer to store page number of first data page
Output: 0 on success
*/
int findFirst2Pages(char *linearAddrInp,char *segNumInp,int *pageCode,int *pageData)
{
int pageCodefound=0,pageDatafound=0;
FILE *fpLinearAddr = fopen(linearAddrInp,"r");
FILE *fpSegNum = fopen(segNumInp,"r");
if(fpLinearAddr==NULL||fpSegNum==NULL)
return -1;
int addr,segNum;
char readWrite;
while(fscanf(fpLinearAddr,"%x",&addr)!=0 && (pageCodefound==0 || pageDatafound==0))
{
fscanf(fpSegNum,"%x %c",&segNum,&readWrite);
if(segNum==0x8&&pageCodefound==0){
*pageCode = addr>>10;
pageCodefound = 1;
}
else if(segNum==0x0&&pageDatafound==0){
*pageData = addr>>10;
pageDatafound = 1;
}
}
fclose(fpLinearAddr);
fclose(fpSegNum);
return 0;
}
/*
Allocates frames in memory for first code page and first data page of a process
Input: pid, name of linear address input file, name of segment number input file
Output: 0 on success
*/
int prepaging(int pid,char *LinearAddrInputFile,char *SegNumAddrFile)
{
int pageNumCode,pageNumData;
//find first data page and code page
if(findFirst2Pages(LinearAddrInputFile,SegNumAddrFile,&pageNumCode,&pageNumData)==-1)
return -1;
//get page table frame corresponding to the first code and data pages
int pt1NumCode = pageNumCode>>8,
pt2NumCode = pageNumCode>>16,
pt1NumData = pageNumData>>8,
pt2NumData = pageNumData>>16;
int4 segNumCode,segNumData;
segNumCode.value = 8;
segNumData.value = 0;
pageTable *pt3RefCode = searchSegmentTable(pid,segNumCode);
pageTable *pt3RefData = searchSegmentTable(pid,segNumData);
fprintf(outputFile, "Page Table: Prepaging\n");
//Level 2 page table frame for Code
allocateFrame(pid,0x8,pt3RefCode,pt2NumCode,2);
//Level 1 page table frame for Code
pageTable *pt2RefCode = getPointertoNextLevelPageTable(pt3RefCode);
allocateFrame(pid,0x8,pt2RefCode,pt1NumCode,1);
//Level 2 page table frame for Data
allocateFrame(pid,0x0,pt3RefData,pt2NumData,2);
pageTable *pt2RefData = getPointertoNextLevelPageTable(pt3RefData);
//Level 1 page table frame for Data
allocateFrame(pid,0x0,pt2RefData,pt1NumData,1);
//allocate frame for first code page
allocateFrame(pid,0x8,getPointertoNextLevelPageTable(pt2RefCode),pageNumCode ,0);
//allocate frame for first data page
allocateFrame(pid,0x0,getPointertoNextLevelPageTable(pt2RefData),pageNumData ,0);
return 0;
}