-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
160 lines (142 loc) · 4.06 KB
/
queue.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
// #include "queue.h"
#include "types.h"
#include "param.h"
#include "mmu.h"
#include "proc.h"
#include "defs.h"
static int queue_findfree(struct clockqueue *queue)
{
int i;
for (i = 0; i < CLOCKSIZE; i++)
{
if (queue->buffer[i].pte == (uint *)NULL &&
queue->buffer[i].va == NULL &&
queue->buffer[i].prev == -1 &&
queue->buffer[i].next == -1)
return i;
}
return -1;
}
static void send_to_end(struct clockqueue *queue)
{
// There aren't any entries in the queue
if (queue->head == -1)
return;
// Only one entry in the queue
if (queue->head == queue->tail)
return;
// Temp variable for current head
int old_head = queue->head;
// Move head up the queue
queue->head = queue->buffer[queue->head].next;
queue->buffer[queue->head].prev = -1;
// Move the previous head to end of queue
queue->buffer[old_head].next = -1;
queue->buffer[queue->tail].next = old_head;
queue->buffer[old_head].prev = queue->tail;
// Update the tail to be the previous head
queue->tail = old_head;
}
static int find_victim(struct clockqueue *queue)
{
int victim = -1;
int curr;
while (victim == -1)
{
curr = queue->head;
pte_t *pte = queue->buffer[curr].pte;
// Haven't found our victim
if ((*pte & PTE_A) != 0)
{
// Flip PTE_A bit to 0
*pte = *pte & ~PTE_A;
send_to_end(queue);
}
// Found our victim
else
{
// Encrypt the victim page
mencrypt(queue->buffer[curr].va, 1);
// Move the head to current head's next
queue->head = queue->buffer[curr].next;
// Set new head's previous
queue->buffer[queue->head].prev = -1;
victim = curr;
}
}
return victim;
}
void queue_init(struct clockqueue *queue)
{
int i;
for (i = 0; i < CLOCKSIZE; i++)
{
queue->buffer[i].pte = (uint *)NULL;
queue->buffer[i].va = NULL;
queue->buffer[i].prev = -1;
queue->buffer[i].next = -1;
}
queue->head = -1;
queue->tail = -1;
}
void queue_append(struct clockqueue *queue, char *va, pte_t *pte)
{
int new_tail = queue_findfree(queue);
// Number of decrypted is CLOCKSIZE, find victim
if (new_tail == -1)
{
new_tail = find_victim(queue);
}
// Add to working set
queue->buffer[new_tail].pte = pte;
queue->buffer[new_tail].va = va;
queue->buffer[new_tail].prev = queue->tail;
queue->buffer[new_tail].next = -1;
// Only update the current tails next if tail isn't -1
if (queue->tail != -1)
queue->buffer[queue->tail].next = new_tail;
// Update tail
queue->tail = new_tail;
// Edge case where we are pushing the first process
if (queue->head == -1)
{
queue->head = queue->tail;
}
}
void queue_remove(struct clockqueue *queue, pte_t *pte)
{
int i;
for (i = 0; i < CLOCKSIZE; i++)
{
if (queue->buffer[i].pte == pte)
{
// Edge case: removing tail
if (queue->tail == i)
{
queue->tail = queue->buffer[i].prev;
}
// Edge case: removing head
if (queue->head == i)
{
queue->head = queue->buffer[i].next;
}
// Update the prev and next "pointers" of the
// neightboring entries if of the proc we are removing
// if those entries exist
if (queue->buffer[i].prev != -1)
{
queue->buffer[queue->buffer[i].prev].next = queue->buffer[i].next;
}
if (queue->buffer[i].next != -1)
{
queue->buffer[queue->buffer[i].next].prev = queue->buffer[i].prev;
}
// "Free" the memory in the queue
queue->buffer[i].pte = (uint *)NULL;
queue->buffer[i].va = NULL;
queue->buffer[i].next = -1;
queue->buffer[i].prev = -1;
break;
}
}
}