Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** This is the only file that will be uploaded to the testing server for grading.
- ** Please put your implementation of the tasks in here.
- ** In order to configure the simulated memory hardware to satisfy the requests
- ** use the Kernel::mmu member through its interface.
- ** Although you cannot change the definitions in the Kernel class you can declare
- ** procedures in this file for any code that you want to write (and if you pass
- ** "this" to these procedures then you can access the Kernel members and methods.
- */
- #include "base/system.h"
- void Kernel::syscall(int num, uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
- {
- uint32_t numFrame, currSize, numDir, lastIndex;
- uint32_t PT1, PT2, freeThis;
- printf("output: syscall %d arg0=%08x arg1=%08x arg2=%08x arg3=%08x\n", num, arg0, arg1, arg2, arg3);
- switch(num) {
- case SYS_BRK:
- currSize = 0;
- numFrame = arg0/4096;
- numDir = (numFrame/1024)+1;
- PT1 = userModeHeap.base>>22;
- PT2 = (userModeHeap.base>>12)&0x3ff;
- for(int k = PT1; k<numDir; k++){
- if(this -> mmu.cr3.tables[k] == NULL){
- this -> mmu.cr3.tables[k] = new PageTable();
- }
- if(k == PT1){
- for (int i = PT2; i < 1024; i++) {
- if(this -> mmu.cr3.tables[k]->entries[i].present == false){
- this -> mmu.cr3.tables[k]->entries[i].frame = allocateFrame();
- this -> mmu.cr3.tables[k]->entries[i].present = true;
- }
- currSize++;
- if(currSize == numFrame){
- lastIndex = i;
- i=1024;
- }
- }
- }
- else{
- for (int i = 0; i < 1024; i++) {
- if(this -> mmu.cr3.tables[k]->entries[i].present == false){
- this -> mmu.cr3.tables[k]->entries[i].frame = allocateFrame();
- this -> mmu.cr3.tables[k]->entries[i].present = true;
- }
- currSize++;
- if(currSize == numFrame){
- lastIndex = i;
- i=1024;
- }
- }
- }
- }
- lastIndex++;
- for(int k = (numDir-1); k < 1024; k++)
- {
- if(k == (numDir-1)){
- for (int i = lastIndex; i < 1024; i++) {
- //printf("%d\n", i);
- //printf("%d\n", (numFrame+PT2));
- if(this -> mmu.cr3.tables[k] -> entries[i].present == true){
- freeThis = this -> mmu.cr3.tables[k]->entries[i].frame;
- freeFrame(freeThis);
- this -> mmu.cr3.tables[k]->entries[i].frame = 0;
- this -> mmu.cr3.tables[k]->entries[i].present = false;
- }
- }
- }
- else{
- for (int i = 0; i < 1024; i++) {
- //printf("%d\n", (numFrame+PT2));
- if(this -> mmu.cr3.tables[k] != NULL){
- if(this -> mmu.cr3.tables[k] -> entries[i].present == true){
- freeThis = this -> mmu.cr3.tables[k]->entries[i].frame;
- freeFrame(freeThis);
- this -> mmu.cr3.tables[k]->entries[i].frame = 0;
- this -> mmu.cr3.tables[k]->entries[i].present = false;
- }
- }
- }
- }
- }
- return;
- default:
- std::cout << "Unrecognised syscall: " << num << std::endl;
- return;
- }
- }
- void Kernel::pageFaultHandler(uint32_t vaddr)
- {
- printf("output: Page fault! addr=%08x\n",vaddr);
- throw std::string("output: Program segv");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement