Page 1 of 1

Copy on Write Page Directory

Posted: Thu Jul 06, 2006 4:09 am
by spix
Hi

I'm trying to implement COW so my fork function works correctly. I seem to have the page faulting bit working using Pype's handy hint, but the forked task isn't working.

The parent task keeps on running fine, copying read only pages and stuff, buf the forked task when it returns from the fork syscall just sits there, so I am thinking there is a problem in the forked task's page directory.

This is the function that creates the page directory (and modifys the creator's page directory)

Code: Select all

uint32_t *phys_mem_new_page_dir() {
   uint32_t *new_page_dir;
   uint32_t *old_page_dir;
   uint32_t *old_page_table;
   uint32_t *new_page_table;
   uint32_t tab_ent;
   uint32_t addr;
   uint32_t *fake_dir = (uint32_t *)0x30000000;
   uint32_t *fake_tab = (uint32_t *)0x30001000;
   
  int i, j;

  new_page_dir = (uint32_t *)phys_mem_blk_alloc();
  old_page_dir = (uint32_t *)0xfffff000;
   old_page_table = (uint32_t *)0xffc00000;

   if (new_page_dir == NULL) return NULL;      

   map_page((uint32_t)fake_dir, (uint32_t)new_page_dir, 3);
//   map_page((uint32_t)new_page_dir, (uint32_t)new_page_dir, 3);
  // map the kernel area
  for (i=0;i<256;i++) {
     fake_dir[i] = old_page_dir[i];
//      new_page_dir[i] = old_page_dir[i];
  }
   for (i=256;i<1023;i++) {
      if (old_page_dir[i] & 1) {
         new_page_table = (uint32_t *)phys_mem_blk_alloc();
         if (new_page_table == NULL) return NULL;
            
         map_page((uint32_t)fake_tab, (uint32_t)new_page_table, 7);
         //map_page((uint32_t)new_page_table, (uint32_t)new_page_table, 7);
         fake_dir[i] = (uint32_t)new_page_table & 7;
         //new_page_dir[i] = (uint32_t)new_page_table & 7;
         
         for (j=0;j<1024;j++) {
            addr = i * 4096 * 1024 + j * 4096;
            tab_ent = (addr >> 12);
            if (old_page_table[tab_ent] & 1) {
               // duplicating a page...
               // check if page frame for addr exists 
               if (!(old_page_table[tab_ent] & 2)) {
                  // if so, increase ref count
                  page_frame_inc(current->pid, addr);
               } else {
            // if not, create page frame and set ref_count = 2;
            page_frame_create(current->pid, addr);
                  old_page_table[tab_ent] = (old_page_table[tab_ent] & 0xFFFFF000) | 5;
               }
               fake_tab[j] = (old_page_table[tab_ent] & 0xFFFFF000) | 5;
               //new_page_table[j] = (old_page_table[tab_ent] & 0xFFFFF000) | 5;
               ivld_tlb(addr);
            } else {
               fake_tab[j] = 0 | 2;
            }
         }
      } else {
        fake_dir[i] = 0 | 2;
        //new_page_dir[i] = 0 | 2;
      }
   }

  fake_dir[1023] = (uint32_t)new_page_dir | 3;
  //new_page_dir[1023] = (uint32_t)new_page_dir | 3;
  return new_page_dir;
}
I was thinking I need somewhere to map the new page directories entry, it's just a temporary map so i can adjust the data in the new page table and it gets mapped over each time.

My kernel doesn't allow process switching while running in kernel mode, so this function can not be interrupted (it's in a syscall)

I've been looking at this a few days now, in my tests, I have discovered if i simply duplicate the entire page directory, ie,

Code: Select all

  // map the kernel area
  for (i=0;i<1023;i++) {
     fake_dir[i] = old_page_dir[i];
  }
  fake_dir[1023] = new_page_dir | 3;
the problem goes away.. am I missing something?

any help would be great, thanks

Re:Copy on Write Page Directory

Posted: Thu Jul 06, 2006 9:46 am
by spix
Sorry guys, I feel like a real dork. I've spent days on this.. I found the problem, I had an & instead of an |...

oops.

Re:Copy on Write Page Directory

Posted: Thu Jul 06, 2006 10:00 am
by Solar
Don't worry. We have a saying here in the department - "Every bug is trivial. {pause} Once you've found it."

Good luck with your project!