Giter Site home page Giter Site logo

davidhcefx / translate-virtual-address-to-physical-address-in-linux-kernel Goto Github PK

View Code? Open in Web Editor NEW
13.0 2.0 0.0 16 KB

Translate Virtual Address To Physical Address in Linux Kernel

linux-kernel virtual-address physical-addresses translate pagetable

translate-virtual-address-to-physical-address-in-linux-kernel's Introduction

Translate Virtual Address To Physical Address

The followings are some ways to translate a virtual address to the corresponding physical address, if exist.

Step 1. Walk the page tables to get pte_t *.

// struct mm_struct *mm : The memory descriptor of the current process.
// unsigned long address: The virtual address to be translated.
pgd_t *pgd = pgd_offset(mm, address);
if (!pgd_none(*pgd) && !pgd_bad(*pgd)) {
    pud_t *pud = pud_offset(pgd, address);
    if (!pud_none(*pud) && !pud_bad(*pud)) {
        pmd_t *pmd = pmd_offset(pud, address);
        if (!pmd_none(*pmd) && !pmd_bad(*pmd)) {
            pte_t *pte = pte_offset_map(pmd, address);
            if (!pte_none(*pte)) {
                // now you get a pointer pointing to a pte entry!
            }
            pte_unmap(pte);
        }
    }
}       

Step 2.

  • A) Calculate the bits directly.
// Assume it is on 32-bit machine with 4 KB pages.
unsigned long phys = pte_val(*pte) & 0xfffff000 + address & 0x00000fff;
  • B) With the help of a page descriptor.
struct page *pg = pte_page(*pte);
unsigned long phys = page_to_phys(pg);

Userspace:

cat /proc/<pid>/maps to get virtual address intervals.

xxd /proc/<pid>/pagemap to get virtual - physical address mappings. documention

Others:

  • virt_to_page(unsigned long address) seems to be a faster way to get struct page*, but you have to make sure the return value is valid. (code)

  • virt_to_phys(volatile void *address) is only suitable for addresses directly mapped or mapped by kmalloc. (code)

Reference:

  • Understanding the Linux Kernel 3rd (Daniel P. Bovet, Marco Cesati), Sections 2.5, 8.1, 9.2, etc.
  • page_to_phys from <asm/io.h>
  • How to get the physical address from the logical one in a Linux kernel module? Stack Overflow
  • How to find the physical address of a variable from user-space in Linux? Stack Overflow

translate-virtual-address-to-physical-address-in-linux-kernel's People

Contributors

davidhcefx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.