You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

124 lines
2.8 KiB

#include "baseMemoryOperation.h"
const int __endian_bit = 1;
#define is_bigendian() ((*(char *) &__endian_bit) == 0)
#ifdef S32_BIT_OPERATIONS
uint32_t getPhysicalAddr(uint32_t virt_addr)
{
#else
uint64_t getPhysicalAddr(uint64_t virt_addr)
{
#endif
uint64_t read_val, file_offset;
FILE * f;
char path_buf[0x100] = {};
char * end;
int i, c, pid, status;
printf("Big endian? %d\n", is_bigendian());
sprintf(path_buf, "/proc/%d/pagemap", getpid());
f = fopen(path_buf, "rb");
if (! f)
{
printf("Error! Cannot open %s\n", path_buf);
return -1;
}
// Shifting by virt-addr-offset number of bytes
// and multiplying by the size of an address (the size of an entry in pagemap file)
file_offset = virt_addr / getpagesize() * PAGEMAP_ENTRY;
printf("Vaddr: 0x%lx, Page_size: %d, Entry_size: %d\n", virt_addr, getpagesize(), PAGEMAP_ENTRY);
printf("Reading %s at 0x%llx\n", path_buf, (unsigned long long) file_offset);
status = fseek(f, file_offset, SEEK_SET);
if (status)
{
perror("Failed to do fseek!");
return 0;
}
errno = 0;
read_val = 0;
unsigned char c_buf[PAGEMAP_ENTRY];
for (i = 0; i < PAGEMAP_ENTRY; i++)
{
c = getc(f);
if (c == EOF)
{
fclose(f);
printf("\nReached end of the file\n");
return 0;
}
if (is_bigendian())
{
c_buf[i] = c;
}
else
{
c_buf[PAGEMAP_ENTRY - i - 1] = c;
}
printf("[%d]0x%x ", i, c);
};
fclose(f);
for (i = 0; i < PAGEMAP_ENTRY; i++)
{
printf("%d ", c_buf[i]);
read_val = (read_val << 8) + c_buf[i];
}
printf("\n");
printf("Result: 0x%llx\n", (unsigned long long) read_val);
if (GET_BIT(read_val, 62))
{
printf("Page swapped\n");
}
if (GET_BIT(read_val, 61))
{
printf("Page File\n");
}
// if(GET_BIT(read_val, 63))
if (GET_BIT(read_val, 63))
{
printf("PFN: 0x%llx\n", (unsigned long long) GET_PFN(read_val));
return GET_PFN(read_val);
}
else
{
printf("Page not present\n");
return 0;
}
return 0;
}
/*
int main(){
unsigned int *Buffer = NULL;
unsigned int PH_Addr = 0;
char mapFile[128];
unsigned int PageAddress = 0;
unsigned int PageOffset = 0;
int RetVal = 0;
mlockall( MCL_CURRENT | MCL_FUTURE );
printf("pid_t: %zu\n", sizeof(pid_t));
printf("uid_t: %zu\n", sizeof(uid_t));
printf("gid_t: %zu\n", sizeof(gid_t));
sprintf( mapFile, "/proc/%d/pagemap", getpid() );
printf("Mapping File: %s\n", mapFile );
Buffer = (unsigned int *) malloc( 192*1024*1024 ); // 192Mb buffer;
if( Buffer!= NULL ){
printf("Buffer Allocated\n");
printf("Buffer Virtual Address: %p \n", Buffer );
PageAddress = ((unsigned int) Buffer ) >> 12;
PageOffset = ((unsigned int) Buffer ) % 0x1000;
printf(" Page: 0x%08x OffSet: 0x%08x\n", PageAddress, PageOffset );
read_pagemap( mapFile, (int) Buffer );
free( Buffer );
printf("Buffer Released\n");
} else {
printf("Cannot Allocate Buffer\n");
};
munlockall();
return 0;
};
*/