Skip to content

Instantly share code, notes, and snippets.

@bb33bb
Forked from soez/address_functions.c
Created August 19, 2024 06:34
Show Gist options
  • Save bb33bb/ab78d91982a404b5fdc7561da49ae01c to your computer and use it in GitHub Desktop.
Save bb33bb/ab78d91982a404b5fdc7561da49ae01c to your computer and use it in GitHub Desktop.
Samsung virt_to_phys - phys_to_virt - virt_to_page - page_to_virt
#define MEMSTART 0x80000000UL
#define VIRTUAL_KERNEL_START 0xffffffc008000000UL
#define LINEAR_MAP_START 0xffffff8000000000UL
bool is_lm_addr(uint64_t kaddr)
{
return (kaddr & (VIRTUAL_KERNEL_START - (0x8 << (6 * 4)))) == LINEAR_MAP_START;
}
uint64_t virt_to_phys(uint64_t kaddr)
{
if (is_lm_addr(kaddr)) {
return kaddr - LINEAR_MAP_START + MEMSTART;
} else {
return kaddr - VIRTUAL_KERNEL_START + MEMSTART;
}
}
uint64_t phys_to_virt(uint64_t paddr, bool is_lm_addr)
{
if (is_lm_addr) {
return paddr + LINEAR_MAP_START - MEMSTART;
} else {
return paddr + VIRTUAL_KERNEL_START - MEMSTART;
}
}
uint64_t vmemmap = 0xffffffff00000000UL;
uint64_t virt_to_page(uint64_t kaddr)
{
return vmemmap + (((virt_to_phys(kaddr) - MEMSTART) >> 12) << 6);
}
uint64_t page_to_virt(uint64_t page, bool is_lm_addr)
{
return phys_to_virt((((page - vmemmap) >> 6) << 12) + MEMSTART, is_lm_addr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment