Guest User

Untitled

a guest
Jun 21st, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #include "types.h"
  2. #include "PMM.h"
  3.  
  4. HIVSTATUS PmmVaToPhysicalAddress
  5.     (u32 pdeBase,
  6.      u32 virtualAdd,
  7.      u32 *physicalAdd) {
  8.  
  9.          u32 pdeEntry, pteEntry, pageEntry;
  10.  
  11.          pdeEntry = __CR3_READ(pdeBase) + __VA_PDE_OFF(virtualAdd);
  12.          pteEntry = __SET_PDE(pdeEntry) + __VA_PTE_OFF(virtualAdd);
  13.          //pageEntry = __SET_PTE(pteEntry) + __VA_PAGE_OFF(virtualAdd);
  14.          *physicalAdd =  __SET_PTE(pteEntry) + __VA_PAGE_OFF(virtualAdd);
  15.  
  16.           return STATUS_SUCCESS;
  17. }
  18.  
  19. HIVSTATUS PmmMapPhysicalPages
  20.     (u32 physicalAdd,
  21.      u32 size,
  22.      u32 *virtualAdd,
  23.      u32 pdeBase) {
  24.  
  25.          PPTE pte;
  26.          u32 nPages, oldPteBase, oldPdeB;
  27.          u32 *pteBase, *pdeB;
  28.          u32 v_table, v_offset, v_dir;
  29.          u32 index = 1;
  30.  
  31.          //See how many pages I've to locate
  32.          nPages = size/4096;
  33.          if (nPages % 4096 != 0) nPages++;
  34.  
  35.          //Find va of a random PTE, because the stored datas
  36.          //are reloaded and saved for each restore.
  37.          //Then I just assign standard pte flag values;
  38.          //Check if bde base has PS flag to 0 (pte base)
  39.  
  40.          //FIXME: I need a pte for each page!
  41.          *pdeB = __CR3_READ(pdeBase);
  42.          oldPdeB = *pdeB;
  43.          for (*pdeB; *pdeB < MAX_PTE_SIZE; *pdeB++) {
  44.              if (pdeB[7] == 0)     break;
  45.              else                  continue;
  46.          }
  47.          *pteBase = __SET_PDE(*pdeB);
  48.          oldPteBase = *pteBase;
  49.          for (*pteBase; *pteBase < MAX_PTE_SIZE; *pteBase++) {
  50.              if (pteBase[0] == 1)  break;
  51.              else                  continue;
  52.          }
  53.  
  54.          v_table =  *pteBase - oldPteBase;
  55.          v_offset = physicalAdd - __PHY_PAGE_BASE(physicalAdd);
  56.          v_dir = *pdeB - oldPdeB;
  57.          *virtualAdd = V_ALIGN(v_dir, v_table, v_offset);
  58.  
  59.          pte = (PPTE)pteBase;
  60.          pte->Physical  = (u32)__PHY_PAGE_BASE(physicalAdd);
  61.          pte->Present   = 1;
  62.          pte->ReadWrite = 1;
  63.          pte->Accessed  = 0;
  64.          pte->UserSup   = 0;
  65.          pte->WriteTh   = 0;
  66.          pte->CacheDi   = 0;
  67.          pte->Dirty     = 0;
  68.          pte->PAT       = 0;
  69.          pte->Reserved  = 0;
  70.          pte->Global    = 0;
  71.  
  72.          //HOWTO: return logical address
  73.  
  74.          return STATUS_SUCCESS;
  75.  
  76. }
Add Comment
Please, Sign In to add comment