/
page.c
278 lines (218 loc) · 7.74 KB
/
page.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
/*
Include this license and a link to https://github.com/mpgarate/ with all derivations.
The MIT License (MIT)
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/***********************************/
/****** Two-level page table *******/
/** Operating Systems Project #3 ***/
/*** Michael Garate, Dec 9 2013 ****/
/***********************************/
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "mmu.h"
#include "page.h"
#include "cpu.h"
/* Set this to 1 to print out debug statements */
#define DEBUG 0
/* Macro to print verbose statements and flush stdout */
#ifdef DEBUG
#define SAY(fmt) SAY0(fmt)
#define SAY0(fmt) {printf(fmt); fflush(stdout);}
#define SAY1(fmt,parm1) {printf(fmt,parm1); fflush(stdout);}
#define SAY2(fmt,parm1,parm2) {printf(fmt,parm1,parm2); fflush(stdout);}
#endif
/* The following machine parameters are being used:
Number of bits in an address: 32
Page size: 4KB
Page Table Type: 2 level page table
Size of first level page table: 1024 entries
Size of first level Page Table Entry: 32 bits
Size of each 2nd Level Page Table: 1024 entries
Size of 2nd Level Page Table Entry: 32 bits
Bits of address giving index into first level page table: 10
Bits of address giving index into second level page table: 10
Bits of address giving offset into page: 12
*/
// #define ADDRESS_SIZE 32
// #define PAGE_SIZE 1<<12 //4KB
/*************************************/
/******* Machine Param Macros ********/
/*************************************/
#define TABLE_ENTRIES 1024 // same for L1 and L2
#define ENTRY_SIZE 32 // same for L1 and L2
/* Each entry of a 2nd level page table has
the following:
Present/Absent bit: 1 bit
Page Frame: 20 bits
*/
// This is the type definition for the
// an entry in a second level page table
typedef unsigned int PT_ENTRY;
// This is declaration of the variable representing
// the first level page table
PT_ENTRY **first_level_page_table;
// for performing DIV by 1024 to index into the
// first level page table
#define DIV_FIRST_PT_SHIFT 10
// for performing MOD 1024 to index in a
// second level page table
#define MOD_SECOND_PT_MASK 0x3FF
/*************************************/
/******* Entry Accessor Macros *******/
/*************************************/
#define PRESENT_BIT_MASK 0x80000000
#define PF_NUMBER_MASK 0x000FFFFF
#define PRESENT_BIT_SHIFT 31
/* Example: 0x3FF -> 0011 1111 1111 */
#define get_L1_index(vpage) (vpage >> DIV_FIRST_PT_SHIFT)
#define get_L2_index(vpage) (vpage & MOD_SECOND_PT_MASK)
#define get_pf_number(entry) (entry & PF_NUMBER_MASK)
#define get_present_bit(entry) ((entry & PRESENT_BIT_MASK) >> PRESENT_BIT_SHIFT)
/*************************************/
/********** Table Clearing ***********/
/*************************************/
void clear_L1_page_table(){
int i = 0;
for (i=0;i<TABLE_ENTRIES;i++){
first_level_page_table[i] = NULL; //clear entry i
}
}
void clear_L2_page_table(PT_ENTRY* table_L2){
int i = 0;
for (i=0; i<TABLE_ENTRIES;i++){
table_L2[i] = 0; //clear L2 entry i
}
}
/*************************************/
/******** Print for Debugging ********/
/*************************************/
void print_entry(int i, int j, PT_ENTRY* table_L2){
if (get_present_bit(table_L2[j]))
SAY2("[%d]-%x\n",j,table_L2[j]);
}
void print_all_entries(){
int i = 0;
int j = 0;
PT_ENTRY* table_L2;
for (i = 0; i< TABLE_ENTRIES; i++){
table_L2 = first_level_page_table[i];
if (table_L2 != NULL) {
SAY1("Printing L1 %d\n",i);
SAY("-----------------------------------------\n");
for (j = 0; j<TABLE_ENTRIES; j++){
print_entry(i,j,table_L2);
}
SAY("-----------------------------------------\n");
}
}
}
/*************************************/
/******* Primary Functionality *******/
/*************************************/
// This sets up the initial page table. The function
// is called by the MMU.
//
// Initially, all the entries of the first level
// page table should be set to NULL. Later on,
// when a new page is referenced by the CPU, the
// second level page table for storing the entry
// for that new page should be created if it doesn't
// exist already.
void pt_initialize_page_table()
{
first_level_page_table = malloc(TABLE_ENTRIES*ENTRY_SIZE);
clear_L1_page_table();
}
/*
* Given two table indices, determine whther the pageframe
* number is stored and return it. Otherwise return -1.
*/
PAGEFRAME_NUMBER find_pf_number(int L1_index, int L2_index){
PT_ENTRY* table_L2 = first_level_page_table[L1_index];
if(table_L2 == NULL){
return -1;
}
PT_ENTRY entry = table_L2[L2_index];
if (get_present_bit(entry) == 0){
return -1;
}
else{
return get_pf_number(entry);
}
}
BOOL page_fault; //set to true if there is a page fault
//This is called when there is a TLB_miss.
// Using the page table, this looks up the page frame
// corresponding to the specified virtual page.
// If the desired page is not present, the variable page_fault
// should be set to TRUE (otherwise FALSE).
PAGEFRAME_NUMBER pt_get_pageframe(VPAGE_NUMBER vpage)
{
int L1_index = get_L1_index(vpage);
int L2_index = get_L2_index(vpage);
PAGEFRAME_NUMBER pf_number = find_pf_number(L1_index,L2_index);
if (pf_number == -1) { //could not be found
page_fault = TRUE;
}
else{
page_fault = FALSE;
return pf_number;
}
}
/*
* Create a level 2 page table, clear
* it, and set its level 1 table entry.
*/
PT_ENTRY* create_L2_page_table(int L1_index){
PT_ENTRY* table_L2 = malloc(TABLE_ENTRIES*ENTRY_SIZE);
clear_L2_page_table(table_L2);
first_level_page_table[L1_index] = table_L2;
return table_L2;
}
// This inserts into the page table an entry mapping of the
// the specified virtual page to the specified page frame.
// It might require the creation of a second-level page table
// to hold the entry, if it doesn't already exist.
void pt_update_pagetable(VPAGE_NUMBER vpage, PAGEFRAME_NUMBER pframe)
{
int L1_index = get_L1_index(vpage);
int L2_index = get_L2_index(vpage);
PT_ENTRY* table_L2 = first_level_page_table[L1_index];
if(table_L2 == NULL) {
table_L2 = create_L2_page_table(L1_index);
}
unsigned int value = pframe | PRESENT_BIT_MASK;
table_L2[L2_index] = value;
}
// This clears a page table entry by clearing its present bit.
// It is called by the OS (in kernel.c) when a page is evicted
// from a page frame.
void pt_clear_page_table_entry(VPAGE_NUMBER vpage)
{
int L1_index = get_L1_index(vpage);
int L2_index = get_L2_index(vpage);
PT_ENTRY* table_L2 = first_level_page_table[L1_index];
if(table_L2 == NULL) {
/* Control should never reach here */
SAY("Tried to remove a vpage that does not exist!\n");
return;
}
table_L2[L2_index] = 0; //clear the entry
}