OS 제작의 원리 그리고 Codes - Ch 6. Memory Management

Programming/OS Development 2009. 5. 19. 02:29

To calculate the size of physical memory

We could calculate in a simple way to check data. Writting data from the certain address then read it. Comparing that two data. If it's the same, there is memory. If not, there is no memory.
 

 // 실메모리의 크기를 구한다.
int nGetPhysMemSize()
{
 int  nI, nSize;
 UCHAR *pX, byTe;;

 // 실메모리의 크기를 구한다.
    
 // 메모리는 512메가부터 2기가 바이트까지 뒤진다.
 for( nSize = nI = 512; nI < 2048; nI++ )
 {
  pX = (UCHAR*)( nI * (ULONG)0x100000 );  //  nl * 1MB
  

  byTe = pX[0];         // The start address of the memory.
  byTe++;                 // Increasing the value of the start address.
  pX[0] = byTe;        // Writting the value on the start address.
  if( byTe != pX[0] )  // Comparing between the increased data and the value of the start address.
   break;                  // If it's not the same, there is no memory on that address.
  else
   nSize++;              // If it's the same, there is memory. Keep calculating.
        
        pX[0] -= 1;     // Restoring to the previous value
 }

 return( nSize * 0x100000 );  // 바이트 단위로 리턴한다.
}



Physical Memory Management Table

Assigning 1byte to save the status of physical memory. It's like Reference Counter.
When it's zero, it's not used and is available. When it's five, five processors are using it by mapping.

Each page has 1024 entries, which is 4 byte. It means 4 KB allocate to each page.
There are 256 pages In 1 MB Memory. That's why needing 256 byte per 1MB.

To save the counter for 1024MB Memory, 1024 * 256 bytes ( 256 KB ) are required.
Putting P.M.M.T right after kernel image. But, it is no matter where you put P.M.M.T.

Caution ) There is the space for ROM BIOS on 0xF0000. You must not write any data on that.

// allocate one physical page
 DWORD dwAllocPhysPage()
{
 int  nI;
 UCHAR *pTbl;

 pTbl = bell.pPhysRefTbl; // Physical Memory Management Table

 // search after 64K
 for( nI = 16; nI < bell.nPhysRefSize; nI++ )
 {
  // if the address is video memory then next search is started at 1.5M( = 0x180000 ) area
  if( nI == ( (int)0xA0000 / 4096 ) ) // Skipping the space of ROM BIOS.
   nI = ( (int)0x180000 / 4096 );

  if( pTbl[nI] == 0 )
  {
   pTbl[nI]++;  // increase counter
   return( (DWORD)( (DWORD)nI * (DWORD)4096 ) );
  }
 }
 return( 0 ); // allocation failed
}


 int nFreePage( DWORD dwPhysAddr )
{
 DWORD dwI;
 UCHAR *pTbl;

 dwI = (DWORD)( dwPhysAddr / (DWORD)4096 );

 pTbl = bell.pPhysRefTbl;
 if( pTbl[dwI] == 0 )
  return( -1 );   // 할당되지 않은 메모리이다.

 pTbl[dwI]--;  // Decreasing the counter value with 1

 return( 1 );
}


'Programming > OS Development' 카테고리의 다른 글

VGA 관련  (0) 2009.06.19
하드 디스크 드라이버 ( Hard Disk Driver )  (0) 2009.05.29
메모리 주소별 용량.  (0) 2009.05.15
어셈블러에서 16진수 입력  (0) 2009.05.15
naked 함수  (0) 2009.05.11

설정

트랙백

댓글