This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ccpuimpl.cpp
executable file
·436 lines (347 loc) · 11.1 KB
/
ccpuimpl.cpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#ifndef __PROGTEST__
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <pthread.h>
#include "common.h"
using namespace std;
#endif /* __PROGTEST__ */
class CMemMngr {
public:
CMemMngr(uint8_t* mem, uint32_t totalPages);
~CMemMngr();
uint32_t* allocatePage();
void freePage(uint32_t pos);
uint32_t getFreeSpace();
uint32_t* setValue(uint32_t page, uint32_t offset, bool setVal, uint32_t val);
uint8_t processCnt;
pthread_mutex_t processCntLock;
pthread_mutex_t memAcc;
pthread_cond_t cond;
private:
bool isUsed(uint32_t pos);
void markUsed(uint32_t pos);
void markUnused(uint32_t pos);
uint32_t systemDataPos;
uint8_t* memStart;
uint32_t totalPages;
uint32_t freeSpace;
uint32_t memPointer;
};
CMemMngr::CMemMngr(uint8_t* memStart, uint32_t totalPages) : systemDataPos(0), memStart(memStart),
totalPages(totalPages)
{
pthread_mutex_init(&processCntLock, NULL);
pthread_mutex_init(&memAcc, NULL);
pthread_cond_init(&cond, NULL);
processCnt = 0;
// how many pages do we need to store system info?
uint32_t sysPageCnt = (totalPages / CCPU::PAGE_SIZE) + (totalPages % CCPU::PAGE_SIZE != 0);
// set memory to 0 indicating that none of the pages are used
memset(memStart, 0, CCPU::PAGE_SIZE * totalPages);
// mark the first ones as used
for(uint32_t i = 0; i < sysPageCnt; i++) {
markUsed(i);
}
freeSpace = totalPages - sysPageCnt;
memPointer = sysPageCnt;
}
CMemMngr::~CMemMngr()
{
pthread_mutex_destroy(&processCntLock);
pthread_mutex_destroy(&memAcc);
pthread_cond_destroy(&cond);
}
bool CMemMngr::isUsed(uint32_t pos)
{
uint32_t index = (pos) / 8;
uint32_t offset = (pos) % 8;
// we do not need to lock memAccess here because this function is called only from a already locked position
// get the result (first, go to the right uint8_t number and then shift it to the right)
uint8_t res = (memStart[index] >> (7 - offset)) & (uint8_t) 1;
return res;
}
void CMemMngr::markUsed(uint32_t pos)
{
uint32_t index = (pos) / 8;
uint32_t offset = (pos) % 8;
// we do not need to lock memAccess here because this function is called only from a already locked position
memStart[index] |= (1 << (7 - offset));
}
void CMemMngr::markUnused(uint32_t pos)
{
uint32_t index = (pos) / 8;
uint32_t offset = (pos) % 8;
// we do not need to lock memAccess here because this function is called only from a already locked position
memStart[index] &= ~(1 << (7 - offset));
}
uint32_t* CMemMngr::allocatePage()
{
// have we reached the end once already?
bool roundTrip = false;
while(isUsed(memPointer)) {
if(memPointer >= totalPages) {
if(roundTrip) { return NULL; }
roundTrip = true;
memPointer = 0;
continue;
}
++memPointer;
}
markUsed(memPointer);
memset(memStart + CCPU::PAGE_SIZE * memPointer, 0, CCPU::PAGE_SIZE);
--freeSpace;
return &memPointer;
}
void CMemMngr::freePage(uint32_t pos)
{
++freeSpace;
markUnused(pos);
}
uint32_t CMemMngr::getFreeSpace()
{
return freeSpace;
}
uint32_t* CMemMngr::setValue(uint32_t page, uint32_t offset, bool setVal, uint32_t val)
{
uint32_t* ptrLoc = (uint32_t*) ((memStart + CCPU::PAGE_SIZE * page) + (offset * 4));
if(setVal) {
*ptrLoc = val;
}
return ptrLoc;
}
//======================================================================================================================
class CCPUImpl : public CCPU {
public:
CCPUImpl(uint8_t* memStart, uint32_t pageTableRoot, CMemMngr* memMngr);
virtual uint32_t GetMemLimit(void) const;
virtual bool SetMemLimit(uint32_t pages);
virtual bool NewProcess(void* processArg, void (* entryPoint)(CCPU*, void*), bool copyMem);
protected:
uint32_t memoryLimit;
CMemMngr* memMngr;
uint32_t lvl2PageTableRoot;
uint32_t lvl1Offset;
uint32_t lvl2Offset;
/*
if copy-on-write is implemented:
virtual bool pageFaultHandler(uint32_t address, bool write);
*/
};
//======================================================================================================================
void MemMgr(void* mem, uint32_t totalPages, void* processArg, void (* mainProcess)(CCPU*, void*))
{
CMemMngr* memMngr = new CMemMngr((uint8_t*) mem, totalPages);
pthread_mutex_lock(&memMngr->processCntLock);
memMngr->processCnt++;
pthread_mutex_unlock(&memMngr->processCntLock);
CCPUImpl* ccpu = new CCPUImpl((uint8_t*) mem, *(memMngr->allocatePage()) << 12, memMngr);
mainProcess(ccpu, processArg);
pthread_mutex_lock(&memMngr->processCntLock);
while(memMngr->processCnt > 1) {
pthread_cond_wait(&memMngr->cond, &memMngr->processCntLock);
}
pthread_mutex_unlock(&memMngr->processCntLock);
delete memMngr;
delete ccpu;
return;
}
struct processInfo {
void* processArg;
void (* entryPoint)(CCPU*, void*);
bool copyMem;
uint8_t* memStart;
CMemMngr* memMngr;
CCPU* oldCpu;
uint32_t memoryLimit;
bool finished = false;
pthread_cond_t finishedCond;
pthread_mutex_t finishedMutex;
};
void* processWrapper(void* atr)
{
processInfo* info = (processInfo*) atr;
CMemMngr* memMngr = info->memMngr;
void* args = info->processArg;
pthread_mutex_lock(&memMngr->memAcc);
uint32_t pageTableRoot = *(info->memMngr->allocatePage()) << 12;
pthread_mutex_unlock(&memMngr->memAcc);
CCPUImpl* ccpu = new CCPUImpl(info->memStart, pageTableRoot, info->memMngr);
if(info->copyMem) {
ccpu->SetMemLimit(info->memoryLimit);
uint32_t val;
for(uint32_t addr = 0; addr < info->memoryLimit * CCPU::PAGE_SIZE; addr += 32) {
info->oldCpu->ReadInt(addr, val);
ccpu->WriteInt(addr, val);
}
}
pthread_mutex_lock(&info->finishedMutex);
info->finished = true;
pthread_cond_signal(&info->finishedCond);
pthread_mutex_unlock(&info->finishedMutex);
// execute the process
info->entryPoint(ccpu, args);
// reduce the processCnt and send the signal if this is the last process
pthread_mutex_lock(&memMngr->processCntLock);
memMngr->processCnt--;
if(memMngr->processCnt <= 1) {
pthread_cond_signal(&memMngr->cond);
}
pthread_mutex_unlock(&memMngr->processCntLock);
delete ccpu;
return NULL;
}
//======================================================================================================================
CCPUImpl::CCPUImpl(uint8_t* memStart, uint32_t pageTableRoot, CMemMngr* memMngr) : CCPU(memStart, pageTableRoot),
memoryLimit(0), memMngr(memMngr),
lvl1Offset(0),
lvl2Offset(CCPU::PAGE_DIR_ENTRIES)
{ }
uint32_t CCPUImpl::GetMemLimit(void) const
{
return memoryLimit;
}
bool CCPUImpl::SetMemLimit(uint32_t pages)
{
uint32_t* ptrLoc;
pthread_mutex_lock(&memMngr->memAcc);
// are we allocating or freeing space?
if(memoryLimit == pages) {
pthread_mutex_unlock(&memMngr->memAcc);
return true;
} else if(memoryLimit > pages) {
// free
while(memoryLimit != pages) {
if(lvl1Offset == 0) {
// there is nothing more to remove
pthread_mutex_unlock(&memMngr->memAcc);
return true;
}
if(lvl2Offset == 0) {
// free page
if(lvl1Offset != 0) {
memMngr->freePage(lvl2PageTableRoot);
}
// remove one entry from lvl1 pageTableRoot
memMngr->setValue(m_PageTableRoot >> 12, --lvl1Offset, true, 0);
// set lvl2 pageTableRoot for the previous entry
if(lvl1Offset > 0) {
ptrLoc = memMngr->setValue(m_PageTableRoot >> 12, lvl1Offset - 1, false, 0);
lvl2PageTableRoot = (*ptrLoc) >> 12;
} else {
// there is nothing more to remove
pthread_mutex_unlock(&memMngr->memAcc);
return true;
}
lvl2Offset = CCPU::PAGE_DIR_ENTRIES;
}
if(lvl1Offset == 0) {
// there is nothing more to remove
pthread_mutex_unlock(&memMngr->memAcc);
return true;
}
// free a single page
ptrLoc = memMngr->setValue(lvl2PageTableRoot, --lvl2Offset, false, 0);
memMngr->freePage((*ptrLoc) >> 12);
memMngr->setValue(lvl2PageTableRoot, lvl2Offset, true, 0);
memoryLimit--;
}
} else {
if(memMngr->getFreeSpace() < (pages - memoryLimit)) {
pthread_mutex_unlock(&memMngr->memAcc);
return false;
}
// allocate
while(pages != memoryLimit) {
// is the lvl1 pageTableRoot full?
if(lvl1Offset >= CCPU::PAGE_DIR_ENTRIES) {
// this should be improved, but 1048576 pages (512 MiB) for one process is enough for now
pthread_mutex_unlock(&memMngr->memAcc);
return false;
}
// is the lvl2 pageTableRoot full?
if(lvl2Offset == CCPU::PAGE_DIR_ENTRIES) {
// create a new lvl2 pageTableRoot
lvl2PageTableRoot = *(memMngr->allocatePage());
memMngr->setValue(m_PageTableRoot >> 12, lvl1Offset, true, ((lvl2PageTableRoot << 12) | 0x00000FFF));
++lvl1Offset;
lvl2Offset = 0;
}
// create an entry in the lvl2 pageTableRoot
memMngr->setValue(lvl2PageTableRoot, lvl2Offset, true, (*memMngr->allocatePage() << 12) | 0x00000FFF);
++lvl2Offset;
memoryLimit++;
}
}
pthread_mutex_unlock(&memMngr->memAcc);
return true;
}
bool CCPUImpl::NewProcess(void* processArg, void (* entryPoint)(CCPU*, void*), bool copyMem)
{
if(copyMem && (memMngr->getFreeSpace() < memoryLimit)) {
// there is not enough space to copy the memory
return false;
}
pthread_mutex_lock(&memMngr->processCntLock);
if(memMngr->processCnt >= PROCESS_MAX) {
pthread_mutex_unlock(&memMngr->processCntLock);
return false;
}
memMngr->processCnt++;
pthread_mutex_unlock(&memMngr->processCntLock);
processInfo* info = new processInfo;
info->copyMem = copyMem;
info->entryPoint = entryPoint;
info->processArg = processArg;
info->memMngr = memMngr;
info->oldCpu = this;
info->memStart = m_MemStart;
info->memoryLimit = memoryLimit;
int res;
pthread_attr_t attr;
pthread_t thread;
res = pthread_attr_init(&attr);
if(res != 0) {
pthread_mutex_lock(&memMngr->processCntLock);
memMngr->processCnt--;
pthread_mutex_unlock(&memMngr->processCntLock);
delete info;
return false;
}
res = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
if(res != 0) {
pthread_mutex_lock(&memMngr->processCntLock);
memMngr->processCnt--;
pthread_mutex_unlock(&memMngr->processCntLock);
pthread_attr_destroy(&attr);
delete info;
return false;
}
pthread_mutex_init(&info->finishedMutex, NULL);
pthread_cond_init(&info->finishedCond, NULL);
res = pthread_create(&thread, &attr, processWrapper, (void*) info);
if(res != 0) {
pthread_mutex_lock(&memMngr->processCntLock);
memMngr->processCnt--;
pthread_mutex_unlock(&memMngr->processCntLock);
pthread_mutex_destroy(&info->finishedMutex);
pthread_cond_destroy(&info->finishedCond);
pthread_attr_destroy(&attr);
delete info;
return false;
}
pthread_attr_destroy(&attr);
// wait for the other process to finish initializing
// we need to copy all the data before this process dies
pthread_mutex_lock(&info->finishedMutex);
while(!info->finished) {
pthread_cond_wait(&info->finishedCond, &info->finishedMutex);
}
pthread_mutex_unlock(&info->finishedMutex);
// child process is now finished initializing, we can delete the temp structure
pthread_mutex_destroy(&info->finishedMutex);
pthread_cond_destroy(&info->finishedCond);
delete info;
return true;
}