Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 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.  
  13. void Kernel::syscall(int num, uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3)
  14. {
  15. uint32_t numFrame, currSize, numDir, lastIndex;
  16. uint32_t PT1, PT2, freeThis;
  17.  
  18.  
  19.  
  20. printf("output: syscall %d arg0=%08x arg1=%08x arg2=%08x arg3=%08x\n", num, arg0, arg1, arg2, arg3);
  21. switch(num) {
  22. case SYS_BRK:
  23.  
  24. currSize = 0;
  25.  
  26. numFrame = arg0/4096;
  27.  
  28. numDir = (numFrame/1024)+1;
  29.  
  30. PT1 = userModeHeap.base>>22;
  31. PT2 = (userModeHeap.base>>12)&0x3ff;
  32.  
  33. for(int k = PT1; k<numDir; k++){
  34. if(this -> mmu.cr3.tables[k] == NULL){
  35. this -> mmu.cr3.tables[k] = new PageTable();
  36. }
  37.  
  38. if(k == PT1){
  39. for (int i = PT2; i < 1024; i++) {
  40. if(this -> mmu.cr3.tables[k]->entries[i].present == false){
  41. this -> mmu.cr3.tables[k]->entries[i].frame = allocateFrame();
  42. this -> mmu.cr3.tables[k]->entries[i].present = true;
  43. }
  44. currSize++;
  45. if(currSize == numFrame){
  46. lastIndex = i;
  47. i=1024;
  48. }
  49. }
  50. }
  51. else{
  52. for (int i = 0; i < 1024; i++) {
  53. if(this -> mmu.cr3.tables[k]->entries[i].present == false){
  54. this -> mmu.cr3.tables[k]->entries[i].frame = allocateFrame();
  55. this -> mmu.cr3.tables[k]->entries[i].present = true;
  56. }
  57. currSize++;
  58. if(currSize == numFrame){
  59. lastIndex = i;
  60. i=1024;
  61. }
  62. }
  63. }
  64. }
  65.  
  66. lastIndex++;
  67.  
  68. for(int k = (numDir-1); k < 1024; k++)
  69. {
  70. if(k == (numDir-1)){
  71. for (int i = lastIndex; i < 1024; i++) {
  72. //printf("%d\n", i);
  73. //printf("%d\n", (numFrame+PT2));
  74. if(this -> mmu.cr3.tables[k] -> entries[i].present == true){
  75. freeThis = this -> mmu.cr3.tables[k]->entries[i].frame;
  76. freeFrame(freeThis);
  77. this -> mmu.cr3.tables[k]->entries[i].frame = 0;
  78. this -> mmu.cr3.tables[k]->entries[i].present = false;
  79. }
  80. }
  81. }
  82. else{
  83. for (int i = 0; i < 1024; i++) {
  84.  
  85. //printf("%d\n", (numFrame+PT2));
  86. if(this -> mmu.cr3.tables[k] != NULL){
  87. if(this -> mmu.cr3.tables[k] -> entries[i].present == true){
  88. freeThis = this -> mmu.cr3.tables[k]->entries[i].frame;
  89. freeFrame(freeThis);
  90. this -> mmu.cr3.tables[k]->entries[i].frame = 0;
  91. this -> mmu.cr3.tables[k]->entries[i].present = false;
  92. }
  93. }
  94. }
  95. }
  96.  
  97. }
  98.  
  99. return;
  100. default:
  101. std::cout << "Unrecognised syscall: " << num << std::endl;
  102. return;
  103. }
  104.  
  105. }
  106.  
  107.  
  108. void Kernel::pageFaultHandler(uint32_t vaddr)
  109. {
  110. printf("output: Page fault! addr=%08x\n",vaddr);
  111. throw std::string("output: Program segv");
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement