Advertisement
Guest User

brk

a guest
Sep 19th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. /*
  2. ** This is the only file that will be uploaded to the testing server for grading.
  3. ** Please put your implementation of the tasks in here.
  4. ** In order to configure the simulated memory hardware to satisfy the requests
  5. ** use the Kernel::mmu member through its interface.
  6. ** Although you cannot change the definitions in the Kernel class you can declare
  7. ** procedures in this file for any code that you want to write (and if you pass
  8. ** "this" to these procedures then you can access the Kernel members and methods.
  9. */
  10.  
  11. #include "base/system.h"
  12. #include <iostream>
  13. using namespace std;
  14.  
  15. uint32_t heapStart=0x00200000;
  16. uint32_t frameSize=1024*4; //4096
  17.  
  18. //change non-zero values to zero
  19. uint32_t sizeAllocated=0;
  20.  
  21. void Kernel::syscall(int num, uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
  22. {
  23. printf("output: syscall %d arg0=%08x arg1=%08x arg2=%08x arg3=%08x\n", num, arg0, arg1, arg2, arg3);
  24.  
  25. switch(num)
  26. {
  27. case SYS_BRK:
  28. {
  29. //std::cout << "Brk is not implemented" << std::endl;
  30. heapStart= userModeHeap.base;
  31. //uint32_t size=arg0; //arg0 is size
  32. if(arg0>sizeAllocated)
  33. {
  34. //increase
  35. cout<<" - Increase - "<<endl;
  36. for(uint32_t i=heapStart;i<heapStart+arg0;i+=4096)
  37. {
  38. uint32_t loc = (i>>12UL)&0x3ffUL;
  39. uint32_t tableID = i>>22UL;
  40. if(mmu.cr3.tables[tableID]==NULL)
  41. {
  42. cout<<"- Creating table - "<<endl;
  43. mmu.cr3.tables[tableID]= new PageTable;
  44. //std::cin.get();
  45. }
  46. //uint32_t loc= ((heapStart+((i)*frameSize))>>12UL)&0x3ffUL;
  47. mmu.cr3.tables[tableID]->entries[loc].frame = allocateFrame();
  48. mmu.cr3.tables[tableID]->entries[loc].present = true;
  49. }
  50. }
  51. else if(arg0<=sizeAllocated)
  52. {
  53. //decrease
  54. cout<<" - Decrease - "<<endl;
  55. for(uint32_t i=heapStart+sizeAllocated;i>=heapStart+arg0;i-=4096)
  56. {
  57. uint32_t loc = (i>>12UL)&0x3ffUL;
  58. uint32_t tableID = i>>22UL;
  59. freeFrame(mmu.cr3.tables[tableID]->entries[loc].frame);
  60. mmu.cr3.tables[tableID]->entries[loc].frame = 0;
  61. mmu.cr3.tables[tableID]->entries[loc].present = false;
  62. }
  63. }
  64. sizeAllocated = arg0;
  65. }
  66. return;
  67. default:
  68. std::cout << "Unrecognised syscall: " << num << std::endl;
  69. return;
  70. }
  71. }
  72.  
  73.  
  74. void Kernel::pageFaultHandler(uint32_t vaddr)
  75. {
  76. printf("output: Page fault! addr=%08x\n",vaddr);
  77. uint32_t stack =userModeStack.base;
  78.  
  79. cout<<hex<<stack<<dec<<endl;
  80. //syscall(2,0,0,0,0);
  81. //printf("Success! addr=%08x\n",vaddr);
  82. throw std::string("output: Program segv");
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement