-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmu.c
88 lines (75 loc) · 2.98 KB
/
mmu.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
#include "mmu.h"
bool MMU_Init() {
ASSERT_PRINT("Entering:MMU_Create()\n");
ASSERT_PRINT("Exiting:MMU_Create()\n");
return TRUE;
}
void MMU_Close() {
ASSERT_PRINT("Entering:MMU_Close()\n");
MMU_shouldClose = TRUE;
ASSERT_PRINT("Exiting:MMU_Close()\n");
}
char MMU_ReadAddress(MemoryAddress_t address, int bitIndex) {
ASSERT_PRINT("Entering:MMU_ReadAddress(pid:%d,addr:%d)\n", address.processID, address.pageNumber);
MMFI res;
READERSWRITERS_LockDataRead();
IPT_t_p addr = HAT_GetEntry(address);
int hashIndex = HAT_PRIVATE_Hash(address);
bool wasInLoop = FALSE;
while (IPT_FindFrame(hashIndex, address.processID, address.pageNumber, &res) == FALSE) {
ASSERT_PRINT("segmentation fault...\n");
wasInLoop = TRUE;
QueueCommand_t_p comm = malloc(sizeof (QueueCommand_t));
comm->command = PRMSegmentationFault;
comm->params = calloc(2, sizeof (int));
comm->params[0] = address.pageNumber;
comm->params[1] = address.processID;
comm->paramsAmount = 2;
comm->stringParamsAmount = 0;
comm->voidParamsAmount = 0;
QUEUES_WriteToPRM(comm);
}
if (!wasInLoop)
MM_Hit();
Page toReturn = MM_ReadPage(res); //May be an error here, need some mutex to protect agains RACE conditions..
READERSWRITERS_UnlockDataRead();
return toReturn[bitIndex];
ASSERT_PRINT("Exiting:MMU_ReadAddress(pid:%d,addr:%d)\n", address.processID, address.pageNumber);
}
bool MMU_WriteToAddress(MemoryAddress_t address, Page value, int bitsToWrite, int startingFrom) {
ASSERT_PRINT("Entering:MMU_WriteToAddress(pid:%d,addr:%d)\n", address.processID, address.pageNumber);
MMFI res;
READERSWRITERS_LockDataRead();
IPT_t_p addr = HAT_GetEntry(address);
int hashIndex = HAT_PRIVATE_Hash(address);
bool wasInLoop = FALSE;
while (IPT_FindFrame(hashIndex, address.processID, address.pageNumber, &res) == FALSE) {
ASSERT_PRINT("segmentation fault...\n");
wasInLoop = TRUE;
QueueCommand_t_p comm = malloc(sizeof (QueueCommand_t));
comm->command = PRMSegmentationFault;
comm->params = calloc(2, sizeof (int));
comm->params[0] = address.pageNumber;
comm->params[1] = address.processID;
comm->paramsAmount = 2;
comm->stringParamsAmount = 0;
comm->voidParamsAmount = 0;
QUEUES_WriteToPRM(comm);
}
if (!wasInLoop)
MM_Hit();
MM_WritePage(value, res, bitsToWrite, startingFrom, 1);
READERSWRITERS_UnlockDataRead();
return TRUE;
ASSERT_PRINT("Exiting:MMU_WriteToAddress(pid:%d,addr:%d)\n", address.processID, address.pageNumber);
}
void* MMU_Main() {
ASSERT_PRINT("Entering:MMU_Main()\n");
while (!MMU_shouldClose) {
ASSERT_PRINT("MMU trying to read from queue /MMU\n");
// QueueCommand_t_p command = QUEUES_ReadMMU();
// QUEUES_PrintCommand(command);
// free(command);
}
ASSERT_PRINT("Exiting:MMU_Main()\n");
}