Skip to content

Instantly share code, notes, and snippets.

@soez
Last active July 5, 2024 02:24
Show Gist options
  • Save soez/c39906c58b020fb31ae047659ccf100e to your computer and use it in GitHub Desktop.
Save soez/c39906c58b020fb31ae047659ccf100e to your computer and use it in GitHub Desktop.
Samsung virt_to_phys - phys_to_virt - virt_to_page - page_to_virt
#define MEMSTART 0x80000000
#define VIRTUAL_KERNEL_START 0xffffffc008000000UL
#define LINEAR_MAP_START 0xffffff8000000000UL
bool is_lm_addr(uint64_t kaddr)
{
return (kaddr & VIRTUAL_KERNEL_START) == 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 virt_to_page(uint64_t kaddr)
{
return vmemmap + ((virt_to_phys(kaddr) >> 12) << 6);
}
uint64_t page_to_virt(uint64_t page)
{
return (((page - vmemmap) >> 6) << 12) + LINEAR_MAP_START - MEMSTART;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment