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"
- #include <iostream>
- using namespace std;
- uint32_t heapStart=0x00200000;
- uint32_t frameSize=1024*4; //4096
- //change non-zero values to zero
- uint32_t sizeAllocated=0;
- void Kernel::syscall(int num, uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
- {
- printf("output: syscall %d arg0=%08x arg1=%08x arg2=%08x arg3=%08x\n", num, arg0, arg1, arg2, arg3);
- switch(num)
- {
- case SYS_BRK:
- {
- //std::cout << "Brk is not implemented" << std::endl;
- heapStart= userModeHeap.base;
- //uint32_t size=arg0; //arg0 is size
- if(arg0>sizeAllocated)
- {
- //increase
- cout<<" - Increase - "<<endl;
- for(uint32_t i=heapStart;i<heapStart+arg0;i+=4096)
- {
- uint32_t loc = (i>>12UL)&0x3ffUL;
- uint32_t tableID = i>>22UL;
- if(mmu.cr3.tables[tableID]==NULL)
- {
- cout<<"- Creating table - "<<endl;
- mmu.cr3.tables[tableID]= new PageTable;
- //std::cin.get();
- }
- //uint32_t loc= ((heapStart+((i)*frameSize))>>12UL)&0x3ffUL;
- mmu.cr3.tables[tableID]->entries[loc].frame = allocateFrame();
- mmu.cr3.tables[tableID]->entries[loc].present = true;
- }
- }
- else if(arg0<=sizeAllocated)
- {
- //decrease
- cout<<" - Decrease - "<<endl;
- for(uint32_t i=heapStart+sizeAllocated;i>=heapStart+arg0;i-=4096)
- {
- uint32_t loc = (i>>12UL)&0x3ffUL;
- uint32_t tableID = i>>22UL;
- freeFrame(mmu.cr3.tables[tableID]->entries[loc].frame);
- mmu.cr3.tables[tableID]->entries[loc].frame = 0;
- mmu.cr3.tables[tableID]->entries[loc].present = false;
- }
- }
- sizeAllocated = arg0;
- }
- return;
- default:
- std::cout << "Unrecognised syscall: " << num << std::endl;
- return;
- }
- }
- void Kernel::pageFaultHandler(uint32_t vaddr)
- {
- printf("output: Page fault! addr=%08x\n",vaddr);
- uint32_t stack =userModeStack.base;
- cout<<hex<<stack<<dec<<endl;
- //syscall(2,0,0,0,0);
- //printf("Success! addr=%08x\n",vaddr);
- throw std::string("output: Program segv");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement