TODOs - OS assignments

 avatar
user_0531932
c_cpp
a year ago
3.4 kB
3
Indexable
Never
/*pg_getpage - get the page in ram
 *@mm: memory region
 *@pagenum: PGN
 *@framenum: return FPN
 *@caller: caller
 *
 */
int pg_getpage(struct mm_struct *mm, int pgn, int *fpn, struct pcb_t *caller)
{
  uint32_t pte = mm->pgd[pgn];
 
  if (!PAGING_PAGE_PRESENT(pte))
  { /* Page is not online, make it actively living */
    int vicpgn, swpfpn; 
    //int vicfpn;
    //uint32_t vicpte;

      int tgtfpn = PAGING_SWP(pte);//the target frame storing our variable

/* TODO: Play with your paging theory here */
/* Find victim page */
      find_victim_page(caller->mm, &vicpgn);

/* Get free frame in MEMSWP */
      MEMPHY_get_freefp(caller->active_mswp, &swpfpn);

/* Do swap frame from MEMRAM to MEMSWP and vice versa*/
/* Copy victim frame to swap */
      __swap_cp_page(caller->active_mswp, vicpgn, caller->active_mswp, swpfpn);

/* Copy target frame from swap to mem */
      __swap_cp_page(caller->active_mswp, tgtfpn, caller->active_mswp, vicpgn);

/* Update page table */
      pte_t swap_pgd = mm->swp_pgd;

/* Update victim page table entry */
      pte_clear_fpn(&swap_pgd[vicpgn]);
      pte_clear_page_present(&swap_pgd[vicpgn]);
      pte_set_fpn(&swap_pgd[vicpgn], tgtfpn);

/* Update target page table entry */
      pte_clear_swap(&pte);
      pte_set_fpn(&pte, vicpgn);
      pte_set_page_present(&pte);

/* Update page directory for each process */
      caller->mm->pgd[pgn] = pte;
      mm->swp_pgd[vicpgn] = swap_pgd[vicpgn];

/* Update its online status of the target page */
//pte_set_fpn() & mm->pgd[pgn];
//pte_set_fpn(&pte, tgtfpn);

      enlist_pgn_node(&caller->mm->fifo_pgn,pgn);

  }

  *fpn = PAGING_FPN(pte);

  return 0;
}




/* 
 * vmap_page_range - map a range of page at aligned address
 */
int vmap_page_range(struct pcb_t *caller, // process call
                                int addr, // start address which is aligned to pagesz
                               int pgnum, // num of mapping page
           struct framephy_struct *frames,// list of the mapped frames
              struct vm_rg_struct *ret_rg)// return mapped region, the real mapped fp
{                                         // no guarantee all given pages are mapped
  //uint32_t * pte = malloc(sizeof(uint32_t));
  struct framephy_struct *fpit = malloc(sizeof(struct framephy_struct));
  //int  fpn;
  int pgit = 0;
  int pgn = PAGING_PGN(addr);

  ret_rg->rg_end = ret_rg->rg_start = addr; // at least the very first space is usable

  fpit->fp_next = frames;

  /* TODO map range of frame to address space 
   *      [addr to addr + pgnum*PAGING_PAGESZ
   *      in page table caller->mm->pgd[]
   */

  

   /* Tracking for later page replacement activities (if needed)
    * Enqueue new usage page */
   enlist_pgn_node(&caller->mm->fifo_pgn, pgn+pgit);


  return 0;
}




// TODO: implement this function
// Khang
int MEMPHY_dump(struct memphy_struct *mp)
{
   if (mp == NULL || mp->storage == NULL)
   {
      return -1; // invalid argument or empty memory region
   }

   printf("Memory dump:\n");

   int i, j;
   uint32_t *storage = (uint32_t *)mp->storage;
   for (i = 0; i < mp->maxsz; i += 4)
   {
      printf("%08x: ", i);
      for (j = 0; j < 4 && (i + j) < mp->maxsz; j++)
      {
         printf("%08x ", storage[i / 4 + j]);
      }
      printf("\n");
   }

   return 0;
}