0

I'm trying to port liballoc on a small kernel that I'm writing for my thesis. In order to do that, I need a function that scan a range of address to find free (and used) pages. I wrote that function that scan from and address (it should be pagetable aligned) and print if a page is free or is used:

uint32_t check_pages(uint32_t startAddr,uint32_t length){
    pdirectory* dir = vmm_get_directory ();
    pd_entry* pagedir = dir->m_entries;
    int cfreepage = 0;
    int cusedpage = 0;
    uint32_t x = 0, y = 0;
    for(x = startAddr; x < (startAddr+length) ; x+=4096*1024){ // check 1 pagetable at a time
        if(pagedir[x>>22] != 0){   // check if pagetable exist
            ptable* table =(ptable*) pagedir[x>>22]; 
            for(y=x;;y+=4096){ // scan every single pages in the pagetable
                pt_entry* page = (pt_entry*)table->m_entries [ PAGE_TABLE_INDEX (y) ]; 
                if(((uint32_t)(page)>>22) != 0){ // check if a page is present FIXME this might be the problem
                    cusedpage++;
                    kernelPrintf("Found used page number: %d\n",PAGE_TABLE_INDEX (y));
                }
                else{
                    cfreepage++;
                    kernelPrintf("Found free page number: %d\n",PAGE_TABLE_INDEX (y));
                }
                if(PAGE_TABLE_INDEX (y)==1023) break;
            }
        }
        else{ // if a pagetable doesn't exist add 1024 free pages to the counter
            kernelPrintf("found free pagetable! (1024 free pages)\n");
            cfreepage+=1024;

        }
    }
    kernelPrintf("Used Pages Found: %d\n",cusedpage);   
    kernelPrintf("Free Pages Found: %d\n",cfreepage);   
    return 0;
}

This code works, but have one issue: some pages that are used, will result free.. I think that the problem is this if:

if(((uint32_t)(page)>>22) != 0)

There might be a better way to check if a page is used or not.. Thanks for the help

Luca D'Amico
  • 3,192
  • 2
  • 26
  • 38

2 Answers2

0

This maybe not what you want but it might help. I had once a similar task to do (memory allocator for an embedded system), here is what I did:

  1. Define and align the allocable pages
  2. Define elsewhere an array that references all the pages: I update the arry[idx] value when I allocate/release a page and it makes the count easy
n0p
  • 3,399
  • 2
  • 29
  • 50
  • That's so cool, but I'm looking for a way to find if a page is used or not by looking on it "page present" bit. Can you tell me how you checked if a page is present or not ? Many Thanks – Luca D'Amico Feb 07 '14 at 14:41
  • Ok, I lack of knowledge on how liballoc is working, maybe this [link](https://github.com/blanham/liballoc) can help (found [here](http://forum.osdev.org/viewtopic.php?f=1&t=27429)) – n0p Feb 07 '14 at 15:39
  • Well mate I've already checked that link, as I said in the question, I'd like to know if "if(((uint32_t)(page)>>22) != 0)" is a good way to check if a page is marked as present or not – Luca D'Amico Feb 07 '14 at 15:41
0

if (x >> 22) checks if any bit higher than 21th is set. I have no clue why you shift by 22 (looks like an arbitrary number - why the heck do you do it this way?). If you want to check if an entry is present (in a paging structure of any level), check bit 0 of that entry. Note that checking the highest bits would only work if the entry was assigned with high address (wouldn't catch, say, 0x100000).

Also note that if present bit is 0, all the other bytes are ignored, hence the OS can store any values in them, which might also be an information that will come handy one day.

Griwes
  • 8,805
  • 2
  • 43
  • 70
  • Thank you for your reply, I've tried: if(((uint32_t)(page) & 1) != 0) but all pages results free now (I'm 100% sure that those pages are present).. why ? – Luca D'Amico Feb 09 '14 at 18:06
  • I've fixed it using that if statement and modifing the ptable table declaration in that way: ptable* table = (ptable*)(pagedir[x>>22] & 0xFFFFF000); By the way I'm accepting your answer because it is right to check the bit 0 of every entry in order to test if a page is present or not :) – Luca D'Amico Feb 09 '14 at 18:19