Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
I'm starting to work on a paging system, my first task is to get a physical page allocator ( pages are 4k ) working, i am having trouble just finding a blank page, i wrote this:
thank you, that fixed the page finding loop ( i think, i cant determine that until a can set pages, then test if it wont give me that page ), which is what i'm having more problems, here is code:
Okay well I guess I'll let you see mine, I have always found code better than long explanations sometimes. Of course there is something i should tell you. All of functions use physical addresses and not page numbers.
#define BIT( bit ) ( 1 << bit )
#define SET_BIT( data, bit ) ( data |= BIT( bit ) )
#define RESET_BIT( data, bit ) ( data &= ( ~( BIT( bit ) ) ) )
#define GET_PAGE_NUMBER( address ) ( address >> 12 )
#define GET_PAGE_ADDRESS( page ) ( page << 12 )
// m_memory_bitmap = DWORD *m_memory_bitmap;
DWORD CPhysical_Memory_Manager::Allocate_Free_Page( )
{
DWORD i = 0, j = 0;
// look for a free page
for ( i = ( m_first_free / 32 ); i < ( m_page_count / 32 ); i++ )
{
// are all of those pages used
if ( m_memory_bitmap[i] == 0xFFFFFFFF )
continue;
// find the correct bit
for ( j = 0; j < 32; j++ )
{
// test this page
if ( m_memory_bitmap[i] & BIT( j ) )
{
// this page is allocated
continue;
} // end if
else
{
// this page is now used
SET_BIT( m_memory_bitmap[i], j );
// set the new first free page
m_first_free = ( 32 * i ) + j;
// update the free pages count
m_free_pages--;
// return the page address
return( GET_PAGE_ADDRESS( ( 32 * i ) + j ) );
} // end else
} // end for
} // end for
// no more free pages
// TODO: Add swaping code
// for now return an error
return( 0xFFFFFFFF );
} // end Allocate_Free_Page
void CPhysical_Memory_Manager::Set_Page_Used( DWORD address )
{
DWORD *bitmap = m_memory_bitmap;
// make sure this page is in the bitmap
if ( GET_PAGE_NUMBER( address ) > m_page_count )
return;
// find the offset into the page
DWORD offset = GET_PAGE_NUMBER( address ) / 32;
// find the bit
DWORD bit = GET_PAGE_NUMBER( address ) % 32;
// go to the correct DWORD
bitmap += offset;
// now set the correct bit
SET_BIT( (*bitmap), bit );
// update the free page count
m_free_pages--;
} // end Set_Page_Used
void CPhysical_Memory_Manager::Set_Page_Free( DWORD address )
{
DWORD *bitmap = m_memory_bitmap;
// make sure this page is in the bitmap
if ( GET_PAGE_NUMBER( address ) > m_page_count )
return;
// find the offset into the page
DWORD offset = GET_PAGE_NUMBER( address ) / 32;
// find the bit
DWORD bit = GET_PAGE_NUMBER( address ) % 32;
// go to the correct DWORD
bitmap += offset;
// now set the correct bit
RESET_BIT( (*bitmap), bit );
// update the first free page if neccessary
if ( GET_PAGE_NUMBER( address ) < m_first_free )
m_first_free = GET_PAGE_NUMBER( address );
// update the free page count
m_free_pages++;
} // end Set_Page_Free
it is not commented but i would expect anyone working with this code to be competent enough to understand it
I'm am now in the process of writing code to easily add pages to a processes page directory, so the user can say give me 100k and it will peace together all the fragmented pages, but i haven't the slightest idea where to begin?
DWORD Allocate_Page( DWORD location, DWORD type );
Allocate_Page calls Allocate_Free_Page to get a free physical page and then calls Map_Page to map that page at the virtual location specified.
Map_Page first checks to see if a page table needs to be allocated for that address and if so calls Allocate_Free_Page to get a free page. It then maps the physical address sent to the virtual address.
explanation would help, but i learn best by example, i think i understand the idea ( add the page to the users task's page directory ), but the exact workings I'm still clueless, so any explanation or code would be greatly appreciated
Okay well what do you already know? Do you know how to map pages? Do you have paging enabled now or are you looking for help in that area too?
Here is the code for my Map_Page function. It makes several assumptions, that the page directory can be accessed at 0xFFFFF000, and the page tables can be accessed from 0xFFC00000 to 0xFFFFEFFF.