forked from jaskaran1312/dstn-assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlb.c
81 lines (73 loc) · 2.79 KB
/
tlb.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
#include<stdio.h>
#include<stdlib.h>
#include "hardware.h"
#include "tlb.h"
int64_t fetchTLB(struct Hardware *hardware, int64_t pageNumber, int64_t pid) {
printf("Checking in TLB...\n");
for (int i=0;i<32;i++) {
if(hardware->tlb->valid[i] && hardware->tlb->pid[i] == pid && hardware->tlb->pageNumber[i] == pageNumber) {
printf("TLB hit for Process: %ld & Page Number: %ld\n", pid,pageNumber);
updateMatrix(hardware, i); // updating LRU square matrix
return hardware->tlb->frameNumber[i];
}
}
printf("TLB miss for Process: %ld & Page Number: %ld\n", pid,pageNumber);
return -1; // tlb miss
}
void invalidateTLB(struct Hardware *hardware, int64_t pid) {
for(int i=0;i<32;i++) {
if(hardware->tlb->pid[i] == pid)
hardware->tlb->valid[i] = 0;
}
}
void updateTLB(struct Hardware *hardware, int64_t pageNumber, int64_t frameNumber, int64_t pid) {
for(int i=0;i<32;i++) {
if(!hardware->tlb->valid[i]) {
printf("Found an invalid line!\nUpdating TLB...\n");
hardware->tlb->pid[i] = pid;
hardware->tlb->pageNumber[i] = pageNumber;
hardware->tlb->frameNumber[i] = frameNumber;
hardware->tlb->valid[i] = 1;
updateMatrix(hardware, i);
printf("TLB updated\n");
return;
}
}
printf("All the lines are valid in TLB!\nFinding a line to evict...\n");
for(int i=0;i<32;i++) {
int zeroCount=0;
for(int j=0;j<32;j++) {
if(hardware->tlb->lru[i][j] == 0)
zeroCount++;
}
if(zeroCount == 32) {
printf("Found a line to evict, Process: %ld, Page Number: %ld & Frame Number: %ld\n",hardware->tlb->pid[i], hardware->tlb->pageNumber[i], hardware->tlb->frameNumber[i]);
hardware->tlb->pid[i] = pid;
hardware->tlb->pageNumber[i] = pageNumber;
hardware->tlb->frameNumber[i] = frameNumber;
updateMatrix(hardware, i);
printf("TLB updated\n");
return;
}
}
return;
}
void updateMatrix(struct Hardware *hardware, int64_t line) {
for(int j=0;j<32;j++)
hardware->tlb->lru[line][j] = 1;
for(int j=0;j<32;j++)
hardware->tlb->lru[j][line] = 0;
return;
}
void invalidateLine(struct Hardware *hardware, int64_t pageNumber, int64_t pid) {
for(int i=0;i<32;i++) {
if(hardware->tlb->pid[i] == pid && hardware->tlb->pageNumber[i] == pageNumber) {
printf("Found the line in TLB to invalidate!\n");
hardware->tlb->valid[i] = 0;
printf("Invalidated the line with Process: %ld and Page Number: %ld\n", pid, pageNumber);
return;
}
}
printf("Line not found in TLB to invalidate\n");
return;
}