-
Notifications
You must be signed in to change notification settings - Fork 1
/
os345mmu.c
344 lines (297 loc) · 10 KB
/
os345mmu.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
// os345mmu.c - LC-3 Memory Management Unit
// **************************************************************************
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ** **
// ** The code given here is the basis for the CS345 projects. **
// ** It comes "as is" and "unwarranted." As such, when you use part **
// ** or all of the code, it becomes "yours" and you are responsible to **
// ** understand any algorithm or method presented. Likewise, any **
// ** errors or problems become your responsibility to fix. **
// ** **
// ** NOTES: **
// ** -Comments beginning with "// ??" may require some implementation. **
// ** -Tab stops are set at every 3 spaces. **
// ** -The function API's in "OS345.h" should not be altered. **
// ** **
// ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER ** DISCLAMER **
// ***********************************************************************
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <assert.h>
#include "os345.h"
#include "os345lc3.h"
// ***********************************************************************
// mmu variables
// LC-3 memory
unsigned short int memory[LC3_MAX_MEMORY];
// statistics
int memAccess; // memory accesses
int memHits; // memory hits
int memPageFaults; // memory faults
int nextPage; // swap page size
int pageReads; // page reads
int pageWrites; // page writes
extern int lastRpte;
extern int lastUpte;
extern int uptOffset;
extern bool thisFrame;
extern int curTask;
extern bool firstTime;
int getFrame(int);
int getAvailableFrame(void);
int getFrame(int notme)
{
int frame;
frame = getAvailableFrame();
if (frame >=0) return frame;
// run clock
int i, j, upta, frameToRemove;
i = lastRpte;
// i = 0x2400;
// j = lastUpte;
while(1){
// for(i=0x2400;i<0x3000;i+=2){
// printf("%x", i);
if(DEFINED(memory[i])){
upta = FRAME(memory[i]);
upta = (upta<<6);
//double confirm
int k;
bool nothingInFrame = 1;
for(k=upta;k<upta+64;k+=2){
if(DEFINED(memory[k])){
nothingInFrame = 0;
break;
}
}
if(nothingInFrame && FRAME(memory[i])!=notme){
frameToRemove = FRAME(memory[i]);
lastRpte=i+2;
memory[i] = CLEAR_DEFINED(memory[i]);
memory[i] = CLEAR_REF(memory[i]);
if(PAGED(memory[i+1]) && DIRTY(memory[i])){
memory[i+1] = accessPage(SWAPPAGE(memory[i+1]), frameToRemove, PAGE_OLD_WRITE);
}else{
memory[i+1] = accessPage(0, frameToRemove, PAGE_NEW_WRITE);
}
memory[i+1] = SET_PAGED(memory[i+1]);
memory[i] = SET_DIRTY(memory[i]);
//possibly clear frame number
return frameToRemove;
// i can swap out this upt
}
// for(j=upta;j<upta+64;j+=2){
while(uptOffset < 64){
j = upta + uptOffset;
if(DEFINED(memory[j])){
if(REFERENCED(memory[j])){
memory[j] = CLEAR_REF(memory[j]);
}else{
//use this data frame
frameToRemove = FRAME(memory[j]);
lastRpte = i;
memory[j] = CLEAR_DEFINED(memory[j]);
memory[j] = CLEAR_REF(memory[j]);
// memory[j] = SWAPPAGE(memory[j]);
if(PAGED(memory[j+1]) && DIRTY(memory[j])){
memory[j+1] = accessPage(SWAPPAGE(memory[j+1]), frameToRemove, PAGE_OLD_WRITE);
}else{
memory[j+1] = accessPage(0, frameToRemove, PAGE_NEW_WRITE);
}
memory[j+1] = SET_PAGED(memory[j+1]);
uptOffset+=2; // may not need this line
//need to loop through rest next time but if nothing dont remove this one
return frameToRemove;
}
}
uptOffset+=2;
}
uptOffset = 0;
// }
}
i+=2;
if(i==0x3000){
i=0x2400;
}
// }
}
// memory[(frameToRemove<<6)]
return frame;
}
// **************************************************************************
// **************************************************************************
// LC3 Memory Management Unit
// Virtual Memory Process
// **************************************************************************
// ___________________________________Frame defined
// / __________________________________Dirty frame
// / / _________________________________Referenced frame
// / / / ________________________________Pinned in memory
// / / / / ___________________________
// / / / / / __________frame # (0-1023) (2^10)
// / / / / / / _________page defined
// / / / / / / / __page # (0-4096) (2^12)
// / / / / / / / /
// / / / / / / / /
// F D R P - - f f|f f f f f f f f|S - - - p p p p|p p p p p p p p
// 1 word / 4 bytes / 32 bits
// normal int is only 2 bytes
// there is 2^16 words max memory, 2^10 frames,, each frame is 64 words = 256 bytes = 128 words
// frame size is 2^6 words = 128 bytes = 64
#define MMU_ENABLE 1
unsigned short int *getMemAdr(int va, int rwFlg)
{
unsigned short int pa;
int rpta, rpte1, rpte2;
int upta, upte1, upte2;
int rptFrame, uptFrame;
rpta = 0x2400 + RPTI(va) + 0x40*curTask; // address of the root page table
rpte1 = memory[rpta]; // get a word
rpte2 = memory[rpta+1]; // get another word
// turn off virtual addressing for system RAM
if (va < 0x3000) return &memory[va];
#if MMU_ENABLE
// printf("using memory Management\n");
if (DEFINED(rpte1))
{
memHits++;
// defined
}
else
{
memPageFaults++;
// fault
rptFrame = getFrame(-1);
rpte1 = SET_DEFINED(rptFrame); // setting the frame bit in the user page table
rpte1 = SET_DIRTY(rpte1);
if (PAGED(rpte2)) // it exists in swap space
{
accessPage(SWAPPAGE(rpte2), rptFrame, PAGE_READ);
}
else
{
memset(&memory[(rptFrame<<6)], 0, 128); //sets 128 bytes to 0 why is it 128? why bitshift by 6?
}
}
memory[rpta] = rpte1 = SET_REF(rpte1); // sets the referenced bit in the root page table
memory[rpta+1] = rpte2;
upta = (FRAME(rpte1)<<6) + UPTI(va);
upte1 = memory[upta];
upte2 = memory[upta+1];
if (DEFINED(upte1))
{
memHits++;
// defined
}
else
{
memPageFaults++;
// fault
uptFrame = getFrame(FRAME(memory[rpta]));
upte1 = SET_DEFINED(uptFrame);
upte1 = SET_DIRTY(upte1);
if (PAGED(upte2))
{
accessPage(SWAPPAGE(upte2), uptFrame, PAGE_READ);
}
else
{
memset(&memory[(uptFrame<<6)], 0, 128); //sets 128 bytes to 0 why is it 128? why bitshift by 6?
}
}
if(rwFlg){
memory[rpta] = SET_DIRTY(memory[rpta]);
}else{
}
memory[upta] = SET_REF(upte1);
memory[upta+1] = upte2;
return &memory[(FRAME(upte1)<<6) + FRAMEOFFSET(va)];
#else
return &memory[va];
#endif
} // end getMemAdr
// **************************************************************************
// **************************************************************************
// set frames available from sf to ef
// flg = 0 -> clear all others
// = 1 -> just add bits
//
void setFrameTableBits(int flg, int sf, int ef)
{ int i, data;
int adr = LC3_FBT-1; // index to frame bit table
int fmask = 0x0001; // bit mask
// 1024 frames in LC-3 memory
for (i=0; i<LC3_FRAMES; i++)
{ if (fmask & 0x0001)
{ fmask = 0x8000;
adr++;
data = (flg)?MEMWORD(adr):0;
}
else fmask = fmask >> 1;
// allocate frame if in range
if ( (i >= sf) && (i < ef)) data = data | fmask;
MEMWORD(adr) = data;
}
return;
} // end setFrameTableBits
// **************************************************************************
// get frame from frame bit table (else return -1)
int getAvailableFrame()
{
int i, data;
int adr = LC3_FBT - 1; // index to frame bit table
int fmask = 0x0001; // bit mask
for (i=0; i<LC3_FRAMES; i++) // look thru all frames
{ if (fmask & 0x0001)
{ fmask = 0x8000; // move to next work
adr++;
data = MEMWORD(adr);
}
else fmask = fmask >> 1; // next frame
// deallocate frame and return frame #
if (data & fmask)
{ MEMWORD(adr) = data & ~fmask;
return i;
}
}
return -1;
} // end getAvailableFrame
// **************************************************************************
// read/write to swap space
int accessPage(int pnum, int frame, int rwnFlg)
{
static unsigned short int swapMemory[LC3_MAX_SWAP_MEMORY];
if ((nextPage >= LC3_MAX_PAGE) || (pnum >= LC3_MAX_PAGE))
{
printf("nextPage %d pageNum %d", nextPage, pnum);
printf("\nVirtual Memory Space Exceeded! (%d)", LC3_MAX_PAGE);
exit(-4);
}
switch(rwnFlg)
{
case PAGE_INIT: // init paging
nextPage = 0;
return 0;
case PAGE_GET_ADR: // return page address
return (int)(&swapMemory[pnum<<6]);
case PAGE_NEW_WRITE: // new write (Drops thru to write old)
pnum = nextPage++;
// printf("nextPage %d pageNum %d", nextPage, pnum);
case PAGE_OLD_WRITE: // write
//printf("\n (%d) Write frame %d (memory[%04x]) to page %d", p.PID, frame, frame<<6, pnum);
memcpy(&swapMemory[pnum<<6], &memory[frame<<6], 1<<7);
pageWrites++;
return pnum;
case PAGE_READ: // read
//printf("\n (%d) Read page %d into frame %d (memory[%04x])", p.PID, pnum, frame, frame<<6);
memcpy(&memory[frame<<6], &swapMemory[pnum<<6], 1<<7);
pageReads++;
return pnum;
case PAGE_FREE: // free page
break;
}
return pnum;
} // end accessPage