-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_manager.c
More file actions
247 lines (209 loc) · 5.8 KB
/
memory_manager.c
File metadata and controls
247 lines (209 loc) · 5.8 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
//memory storing the page tables
unsigned char memory[64];
int freelist[4] = {0,0,0,0};
int writable[4] = {0,0,0,0};
//simulated hardware registers
int registers[4] = {-1, -1, -1, -1};
//struct for each page table entry
struct pte {
uint8_t address:6;
uint8_t present:1; //if it is present/valid
uint8_t write:1; //for write permission
};
int allocate(int processId, int value)
{
//first, check if a page table exists
//registers[processId] == address of the process page table
if(registers[processId] == -1)
{
//page table does not exist, we must create it
for(int i = 0; i < 4; i++)
{
if(freelist[i] == 0)
{
//page is available
registers[processId] = i*16;
freelist[i] = 1;
if(value) //if the page should be writable.
{
writable[i] = 1;
}
break;
//create and allocate a bunch of pte?
}
}
}
int page_table_address = registers[processId];
//cast the array to a struct pte pointer (1 byte in size)
struct pte * page_table_pointer;
page_table_pointer = (struct pte *)®isters;
//increment the pointer to point to the start of our page table
page_table_pointer += page_table_address;
printf("page table address for process %d: %d \n", processId, page_table_address);
//create a page table entry to map between the virtual and physical address
for(int i = 0; i < 4; i++)
{
if(freelist[i] == 0)
{
//page frame is free
freelist[i] == 1;
//create a page table entry to map between the virtual and physical address
//registers[processId] + offset?
//create a page table entry
}
}
//add case for full memory (return 1)
return 0;
}
int getPageTable(int processId)
{
if(registers[processId] == -1)
{
for(int i = 0; i < 4; i++)
{
if(freelist[i] == 0)
{
registers[processId] = i*16;
freelist[i] = 1;
printf("put page table for PID %d into physical frame %d\n", processId, i);
break;
}
}
}
return registers[processId];
}
int getPageNumber(int address)
{
if( address < 16)
{
return 0;
}else if(address > 15 && address < 32)
{
return 1;
}else if(address > 31 && address < 48)
{
return 2;
}else if(address > 47)
{
return 3;
}
return -1;
}
int main(int argc, char * argv[])
{
printf("Initializing... \n");
while(1)
{
printf("Instruction? ");
int pid, virtual_address,value;
char * instruction;
char input[256];
scanf("%s", input);
printf("input: %s \n", input);
char * pidString = strtok(input, ",");
char * endptr;
if(pidString == NULL)
{
printf("Error. Invalid input.\n");
continue;
}
pid = strtol(pidString, &endptr, 10);
if(*endptr != '\0' || pid > 3 || pid < 0)
{
printf("Error. Invalid input.\n");
continue;
}
char * splitWord = 0;
int count = 0;
splitWord = pidString;
while( splitWord != NULL)
{
splitWord = strtok(NULL, ",");
if(splitWord != NULL)
{
if(count == 0)
{
instruction = splitWord;
}else if(count == 1)
{
virtual_address = strtol(splitWord, &endptr, 10);
}else if(count == 2)
{
value = strtol(splitWord, &endptr, 10);
}
count++;
}
}
//printf("PID: %d instruction: %s value: %d \n", pid, instruction, value);
//printf("virtual address: %d \n", virtual_address);
//first 2 bits of virtual address is the VPN
int offset_mask = 15;
int vpn_mask = 48;
int vpn = virtual_address & vpn_mask;
int offset = virtual_address & offset_mask;
//printf("vpn: %d \n", vpn);
//printf("offset: %d \n", offset);
//gets the page table address, if there isn't a page table, it allocates one.
int page_table_address = getPageTable(pid);
if( strcmp(instruction, "allocate") == 0 )
{
//printf("Allocating...\n");
//check freelist, get a page, add a pte for that vpn.
//page_table_address = vpn*16;
//if the page table can be fetched from the pid, why does the VPN even exist?
//there are actually only 4 page table entries, 0,1,2, or 3 (so we add vpn)
int page_table_entry_index = page_table_address + vpn;
//create page table entry struct by casting the pointer
struct pte * pageTableEntry = (struct pte *)memory + page_table_entry_index;
if(pageTableEntry->address != 0)
{
printf("Updating permissions for virtual page %d (frame %d) \n", vpn, getPageNumber(pageTableEntry->address));
pageTableEntry->write = value;
}else{
int address = -1;
for(int i = 0; i < 4; i++)
{
if(freelist[i] == 0)
{
//found an empty page
address = i*16; //base address
freelist[i] = 1;
break;
}
}
int frameNumber = getPageNumber(address);
pageTableEntry->address = address;
pageTableEntry->write = value;
printf("Mapped virtual address %d into physical frame %d\n", virtual_address, frameNumber);
}
//int result = allocate(pid, value);
//where to set the valid bit?
}
if( strcmp(instruction, "load") == 0 )
{
int page_table_entry_index = page_table_address + vpn;
struct pte * pageTableEntry = (struct pte *)memory + page_table_entry_index;
int value_index = pageTableEntry->address + offset;
int read_value = memory[value_index];
printf("The value %d is virtual address %d \n", read_value, virtual_address);
}
if( strcmp(instruction, "store") == 0)
{
int page_table_entry_index = page_table_address + vpn;
struct pte * pageTableEntry = (struct pte *)memory + page_table_entry_index;
if(pageTableEntry->write == 0)
{
printf("Error: writes are not allowed to this page \n");
}else
{
int value_index = pageTableEntry->address + offset;
memory[value_index] = value;
printf("Store value %d at virtual address %d (physical address %d)\n", value, virtual_address, value_index);
}
}
}
}