페이징 내용을 읽어보고 선형 주소를 물리주소로 변환하는 함수를 만들고 있는데


이게 제대로 동작하는건지 확인이 어려워서요


저자님의 답변을 기다립니다


unsigned long long *GetPhysAddr(void *virtualaddr)

{

unsigned long pml4index         = (unsigned long)virtualaddr      >> 39 & 0x1ff;

unsigned long pdpindex = (unsigned long)virtualaddr >> 30 & 0x1ff;

unsigned long pdindex  = (unsigned long)virtualaddr >> 21 & 0x1ff;

// unsigned long ptindex  = (unsigned long)virtualaddr >> 12 & 0x1ff;

// unsigned long offset    = (unsigned long)virtualaddr & 0xfff;


//3단계까지만 사용 (pml4,pdp,pd)


Printf("pml4 %d pdpindex %d pdindex %d \n", pml4index,pdpindex,pdindex);


PML4TENTRY *pstPML4TEntry = ( PML4TENTRY* ) ReadCr3();

// PDPTENTRY  *pstPDPTEntry = ( PDPTENTRY* ) 0x101000;

// PDENTRY    *pstPDEntry = ( PDENTRY* ) 0x102000;


unsigned long lowAddress = ((pstPML4TEntry[pml4index].dwAttributeAndLowerBaseAddress >> 12));

unsigned long upperAddress = (pstPML4TEntry[pml4index].dwUpperBaseAddressAndEXB & 0xff);


unsigned long long pdp4Address = lowAddress;

pdp4Address = pdp4Address << 12 | upperAddress;

Printf("lowAddress %q upperAddress %q pdpe Address %q base %q\n",lowAddress,upperAddress,pdp4Address,pstPML4TEntry->dwAttributeAndLowerBaseAddress);


PDPTENTRY *pstPDPTEntry = ( PDPTENTRY* ) pdp4Address;


lowAddress = ((pstPDPTEntry[pdpindex].dwAttributeAndLowerBaseAddress >> 12));

upperAddress = (pstPDPTEntry[pdpindex].dwUpperBaseAddressAndEXB & 0xff);

unsigned long long pdAddress = lowAddress;

pdAddress = pdAddress << 12 | upperAddress;


Printf("lowAddress %q upperAddress %q pde Address %q base %q\n",lowAddress,upperAddress,pdAddress,pstPDPTEntry->dwAttributeAndLowerBaseAddress);

PDENTRY    *pstPDEntry = ( PDENTRY* ) pdAddress;


lowAddress = ((pstPDEntry[pdindex].dwAttributeAndLowerBaseAddress >> 12));

upperAddress = (pstPDEntry[pdindex].dwUpperBaseAddressAndEXB & 0xff);

unsigned long long pAddress = lowAddress;

pdAddress = pdAddress << 12 | upperAddress;



Printf("pdentry %q %q\n",virtualaddr,pdAddress);


unsigned long virtualoffset = ((unsigned long )virtualaddr & 0x1FFFFF);


unsigned long long *ptr = (unsigned long long *)pdAddress + virtualoffset;


return ptr;//(unsigned long long *)(&pstPDEntry[pdindex] + ((unsigned long long *)virtualaddr & (unsigned long long *)0x1FFFFF));

}.