Few queries - Linux

This is a discussion on Few queries - Linux ; Dear All, I have few queries about user/kernel vitual addresses. Suppose I have got a virtual address, how can i find is this address is user virtual address or kernel virtual address in kernel space. If this is kernel virtual ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: Few queries

  1. Few queries

    Dear All,

    I have few queries about user/kernel vitual addresses.

    Suppose I have got a virtual address, how can i find is this address is
    user virtual address or kernel virtual address in kernel space.

    If this is kernel virtual address, how can i get its physical address.

    __pa() returns the physical address for kernel logical address but not
    for kernel virtual address.

    Please help me.

    Regards,
    Neeraj


  2. Re: Few queries

    kushneeraj@gmail.com wrote:
    > Dear All,
    >
    > I have few queries about user/kernel vitual addresses.
    >
    > Suppose I have got a virtual address, how can i find is this address is
    > user virtual address or kernel virtual address in kernel space.


    On ix86, if the address is above 0xc0000000, it is a kernel virtual
    address, else it is a user virtual address. On x86_64, the boundary is
    ffffffff80000000.
    However, this may change (One could recompile the kernel with a
    different boundary) and it may even be non-existant in certain
    architectures. In general, you could have two identical address spaces
    for user and kernel.
    So each address exists in both spaces, not necessarily valid.

    > If this is kernel virtual address, how can i get its physical address.


    > __pa() returns the physical address for kernel logical address but not
    > for kernel virtual address.


    What's the difference between a logical and a virtual address?

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  3. Re: Few queries

    thx. Josef

    As stated in LDDK 3rd edition kernel logical addresses maps some
    portion of main memory and are often treated as if they were physical
    addresses. Memory returned by kmalloc has a kernel logical address
    &
    Kernel virtual addresses do not necessarily have the linear one to one
    mapping to physical addresses that characterize the logical addresses.
    All logical addresses are kernel virtual addresses but many kernel
    virtual addresses are not logical addresses. Memory allocated by
    vmalloc has virtual address(but no direct physical mapping)

    So i m getting confused ..what exactly these r...
    addresses above 3GB on i386 is kernel virtual address..does this means
    its also kernel logical address?

    then where memory is allocated by vmalloc above 3GB?


    Josef Moellers wrote:
    > kushneeraj@gmail.com wrote:
    > > Dear All,
    > >
    > > I have few queries about user/kernel vitual addresses.
    > >
    > > Suppose I have got a virtual address, how can i find is this address is
    > > user virtual address or kernel virtual address in kernel space.

    >
    > On ix86, if the address is above 0xc0000000, it is a kernel virtual
    > address, else it is a user virtual address. On x86_64, the boundary is
    > ffffffff80000000.
    > However, this may change (One could recompile the kernel with a
    > different boundary) and it may even be non-existant in certain
    > architectures. In general, you could have two identical address spaces
    > for user and kernel.
    > So each address exists in both spaces, not necessarily valid.
    >
    > > If this is kernel virtual address, how can i get its physical address.

    >
    > > __pa() returns the physical address for kernel logical address but not
    > > for kernel virtual address.

    >
    > What's the difference between a logical and a virtual address?
    >
    > --
    > Josef Möllers (Pinguinpfleger bei FSC)
    > If failure had no penalty success would not be a prize
    > -- T. Pratchett



  4. Re: Few queries

    Also how to find physical address of kernel virtual address returned by
    vmalloc()

    kushneeraj@gmail.com wrote:
    > thx. Josef
    >
    > As stated in LDDK 3rd edition kernel logical addresses maps some
    > portion of main memory and are often treated as if they were physical
    > addresses. Memory returned by kmalloc has a kernel logical address
    > &
    > Kernel virtual addresses do not necessarily have the linear one to one
    > mapping to physical addresses that characterize the logical addresses.
    > All logical addresses are kernel virtual addresses but many kernel
    > virtual addresses are not logical addresses. Memory allocated by
    > vmalloc has virtual address(but no direct physical mapping)
    >
    > So i m getting confused ..what exactly these r...
    > addresses above 3GB on i386 is kernel virtual address..does this means
    > its also kernel logical address?
    >
    > then where memory is allocated by vmalloc above 3GB?
    >
    >
    > Josef Moellers wrote:
    > > kushneeraj@gmail.com wrote:
    > > > Dear All,
    > > >
    > > > I have few queries about user/kernel vitual addresses.
    > > >
    > > > Suppose I have got a virtual address, how can i find is this address is
    > > > user virtual address or kernel virtual address in kernel space.

    > >
    > > On ix86, if the address is above 0xc0000000, it is a kernel virtual
    > > address, else it is a user virtual address. On x86_64, the boundary is
    > > ffffffff80000000.
    > > However, this may change (One could recompile the kernel with a
    > > different boundary) and it may even be non-existant in certain
    > > architectures. In general, you could have two identical address spaces
    > > for user and kernel.
    > > So each address exists in both spaces, not necessarily valid.
    > >
    > > > If this is kernel virtual address, how can i get its physical address.

    > >
    > > > __pa() returns the physical address for kernel logical address but not
    > > > for kernel virtual address.

    > >
    > > What's the difference between a logical and a virtual address?
    > >
    > > --
    > > Josef Möllers (Pinguinpfleger bei FSC)
    > > If failure had no penalty success would not be a prize
    > > -- T. Pratchett



  5. Re: Few queries

    kushneeraj@gmail.com wrote:
    > thx. Josef
    >
    > As stated in LDDK 3rd edition kernel logical addresses maps some
    > portion of main memory and are often treated as if they were physical
    > addresses. Memory returned by kmalloc has a kernel logical address
    > &
    > Kernel virtual addresses do not necessarily have the linear one to one
    > mapping to physical addresses that characterize the logical addresses.
    > All logical addresses are kernel virtual addresses but many kernel
    > virtual addresses are not logical addresses. Memory allocated by
    > vmalloc has virtual address(but no direct physical mapping)
    >
    > So i m getting confused ..what exactly these r...
    > addresses above 3GB on i386 is kernel virtual address..does this means
    > its also kernel logical address?


    I still don't see a difference between a "logical" and a "virtual" address.
    Apparently the distinction is that "logical addresses" have a simple,
    fixed mapping to physical addresses (e.g. a constant offset) while
    virtual addresses have an arbitrary mapping
    (http://linux.derkeiler.com/Newsgroup.../msg00245.html)
    IMHO, this is just trying to name things differently.

    There are virtual addresses, i.e. addresses your registers and the code
    reference and there are physical addresses, the mapping being done by
    the MMU. Not all virtual addresses have a physical address and vice
    versa. In the kernel, all "logical" addresses would have a fixed
    physical address assigned which wouldn't ever change and which would be
    the same on the next instantiation (i.e. 0xc1234567 would _always_ be
    mapped to 0x01234567).
    Again, I don't make this distinction.

    > then where memory is allocated by vmalloc above 3GB?


    1. All addresses, user _and_ kernel, are virtual addresses.
    2. In general, the lower 3GB are user addresses, the upper 1GB is kernel
    addresses (this is on i386)
    3. Within the upper 1GB, there is a section which has a fixed 1:1
    mapping from physical to (kernel) virtual address. Although this is a
    simple mapping (just add 0xc0000000), this is nonetheless implemented by
    setting up page directories and page tables.
    4. There is another section which is not mapped initially (i.e. it does
    have a virtual address, but no physical address associated) and which
    can be used by the kernel to temporarily map physical addresses.

    While I don't know any details (what I do know seems to be a tad
    outdated), but my guess is that the kernel code/data/bass is statically
    mapped 1:1, everything else is dynamically mapped.
    Ages ago, systems rarely had more than 1GB of memory, so a static 1:1
    mapping was set up for (almost) all physical memory. Nowadays systems
    are common which have lots more than 1GB, so this will not work any more.
    Your best approach is to start with vmalloc() (in mm/vmalloc.c) and dig
    through the source code.

    As to the use of __pa: This just subtracts 0xc0000000, so it is not
    gerenally available. This is not really an area I'm very familiar with.
    The best I could find is some code in megaraid.c where the page
    descriptor if taken and the offset within the page and that is processed
    further. The general problem lies in several areas:
    1. The virtual address may not be valid
    2. The virtual address may not be mapped
    3. The physical address may not be directly usable (e.g. in PCI).
    So, I guess there is not generic phys_to_virt function.

    Josef

    > Josef Moellers wrote:
    >
    >>kushneeraj@gmail.com wrote:
    >>
    >>>Dear All,
    >>>
    >>>I have few queries about user/kernel vitual addresses.
    >>>
    >>>Suppose I have got a virtual address, how can i find is this address is
    >>>user virtual address or kernel virtual address in kernel space.

    >>
    >>On ix86, if the address is above 0xc0000000, it is a kernel virtual
    >>address, else it is a user virtual address. On x86_64, the boundary is
    >>ffffffff80000000.
    >>However, this may change (One could recompile the kernel with a
    >>different boundary) and it may even be non-existant in certain
    >>architectures. In general, you could have two identical address spaces
    >>for user and kernel.
    >>So each address exists in both spaces, not necessarily valid.
    >>
    >>
    >>>If this is kernel virtual address, how can i get its physical address.

    >>
    >>>__pa() returns the physical address for kernel logical address but not
    >>>for kernel virtual address.

    >>
    >>What's the difference between a logical and a virtual address?



    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  6. Re: Few queries

    Josef Moellers wrote:
    > I still don't see a difference between a "logical" and a "virtual" address.
    > Apparently the distinction is that "logical addresses" have a simple,
    > fixed mapping to physical addresses (e.g. a constant offset) while
    > virtual addresses have an arbitrary mapping
    > (http://linux.derkeiler.com/Newsgroup.../msg00245.html)
    > IMHO, this is just trying to name things differently.


    On some better-designed architectures, there can be sections of memory
    which are not subject to paging, but instead to a simple translation.
    E.g. the kernel segments or "ksegs" on MIPS.

    It would not be quite accurate to call these addresses virtual, since
    they don't go through virtual address translation.

    It's an advantage to have this feature, because accesses to these
    segments do not map through the TLB, which is precious cache space.
    It's stupid to use virtual address translation to implement a simple
    displacement over a large memory range, and the more physical RAM you
    make visible that way, the stupider it gets due to the greater demand
    on the TLB.

    So, ideally, every platform would have a way to do kernel segments, and
    so the MMU hardware would never be used for these logical pages at all.
    Only for highmem pages, and for truly virtual allocations (user space
    mmap, vmalloc).

    So it does make sense to distinguish logical and virtual addresses. On
    platforms like i386 can think of logical addresses as being a kernel
    segment inefficiently emulated using the paging hardware.


  7. Re: Few queries

    can we follow the page table to get the physical address for kernel
    virtual address like this

    unsigned long get_pte(unsigned long va)
    {
    pgd_t *pgd;
    pmd_t *pmd;
    pte_t *ptep, pte;
    unsigned long page_base, pa ;

    pgd = pgd_offset(current->mm, va);
    if(pgd_none(*pgd) || pgd_bad(*pgd))
    goto out;

    pmd = pmd_offset(pgd, va);
    if(pmd_none(*pmd) || pmd_bad(*pmd))
    goto out;

    ptep = pte_offset_map(pmd, va);
    if(!ptep)
    goto out;

    pte = *ptep;
    pte_unmap(ptep);

    if(!pte_present(pte))
    goto out;

    page_base = pte_val(pte);
    pa = (va & ~PAGE_MASK) | (page_base & PAGE_MASK);
    return pa;

    out:
    return -1;
    }

    Kaz Kylheku wrote:
    > Josef Moellers wrote:
    > > I still don't see a difference between a "logical" and a "virtual" address.
    > > Apparently the distinction is that "logical addresses" have a simple,
    > > fixed mapping to physical addresses (e.g. a constant offset) while
    > > virtual addresses have an arbitrary mapping
    > > (http://linux.derkeiler.com/Newsgroup.../msg00245.html)
    > > IMHO, this is just trying to name things differently.

    >
    > On some better-designed architectures, there can be sections of memory
    > which are not subject to paging, but instead to a simple translation.
    > E.g. the kernel segments or "ksegs" on MIPS.
    >
    > It would not be quite accurate to call these addresses virtual, since
    > they don't go through virtual address translation.
    >
    > It's an advantage to have this feature, because accesses to these
    > segments do not map through the TLB, which is precious cache space.
    > It's stupid to use virtual address translation to implement a simple
    > displacement over a large memory range, and the more physical RAM you
    > make visible that way, the stupider it gets due to the greater demand
    > on the TLB.
    >
    > So, ideally, every platform would have a way to do kernel segments, and
    > so the MMU hardware would never be used for these logical pages at all.
    > Only for highmem pages, and for truly virtual allocations (user space
    > mmap, vmalloc).
    >
    > So it does make sense to distinguish logical and virtual addresses. On
    > platforms like i386 can think of logical addresses as being a kernel
    > segment inefficiently emulated using the paging hardware.



  8. Re: Few queries

    It looks reasonalbe, have you verify it with __pa?

    kushneeraj@gmail.com wrote:
    > can we follow the page table to get the physical address for kernel
    > virtual address like this
    >
    > unsigned long get_pte(unsigned long va)
    > {
    > pgd_t *pgd;
    > pmd_t *pmd;
    > pte_t *ptep, pte;
    > unsigned long page_base, pa ;
    >
    > pgd = pgd_offset(current->mm, va);
    > if(pgd_none(*pgd) || pgd_bad(*pgd))
    > goto out;
    >
    > pmd = pmd_offset(pgd, va);
    > if(pmd_none(*pmd) || pmd_bad(*pmd))
    > goto out;
    >
    > ptep = pte_offset_map(pmd, va);
    > if(!ptep)
    > goto out;
    >
    > pte = *ptep;
    > pte_unmap(ptep);
    >
    > if(!pte_present(pte))
    > goto out;
    >
    > page_base = pte_val(pte);
    > pa = (va & ~PAGE_MASK) | (page_base & PAGE_MASK);
    > return pa;
    >
    > out:
    > return -1;
    > }
    >
    > Kaz Kylheku wrote:
    > > Josef Moellers wrote:
    > > > I still don't see a difference between a "logical" and a "virtual" address.
    > > > Apparently the distinction is that "logical addresses" have a simple,
    > > > fixed mapping to physical addresses (e.g. a constant offset) while
    > > > virtual addresses have an arbitrary mapping
    > > > (http://linux.derkeiler.com/Newsgroup.../msg00245.html)
    > > > IMHO, this is just trying to name things differently.

    > >
    > > On some better-designed architectures, there can be sections of memory
    > > which are not subject to paging, but instead to a simple translation.
    > > E.g. the kernel segments or "ksegs" on MIPS.
    > >
    > > It would not be quite accurate to call these addresses virtual, since
    > > they don't go through virtual address translation.
    > >
    > > It's an advantage to have this feature, because accesses to these
    > > segments do not map through the TLB, which is precious cache space.
    > > It's stupid to use virtual address translation to implement a simple
    > > displacement over a large memory range, and the more physical RAM you
    > > make visible that way, the stupider it gets due to the greater demand
    > > on the TLB.
    > >
    > > So, ideally, every platform would have a way to do kernel segments, and
    > > so the MMU hardware would never be used for these logical pages at all.
    > > Only for highmem pages, and for truly virtual allocations (user space
    > > mmap, vmalloc).
    > >
    > > So it does make sense to distinguish logical and virtual addresses. On
    > > platforms like i386 can think of logical addresses as being a kernel
    > > segment inefficiently emulated using the paging hardware.



  9. Re: Few queries

    I tried with kmalloc() but the pmd_bad(*pmd) is returning 1, so I am
    not able to compare with the physical address returned by __pa()

    Neeraj

    comp.os.linux.dev.system wrote:
    > It looks reasonalbe, have you verify it with __pa?
    >
    > kushneeraj@gmail.com wrote:
    > > can we follow the page table to get the physical address for kernel
    > > virtual address like this
    > >
    > > unsigned long get_pte(unsigned long va)
    > > {
    > > pgd_t *pgd;
    > > pmd_t *pmd;
    > > pte_t *ptep, pte;
    > > unsigned long page_base, pa ;
    > >
    > > pgd = pgd_offset(current->mm, va);
    > > if(pgd_none(*pgd) || pgd_bad(*pgd))
    > > goto out;
    > >
    > > pmd = pmd_offset(pgd, va);
    > > if(pmd_none(*pmd) || pmd_bad(*pmd))
    > > goto out;
    > >
    > > ptep = pte_offset_map(pmd, va);
    > > if(!ptep)
    > > goto out;
    > >
    > > pte = *ptep;
    > > pte_unmap(ptep);
    > >
    > > if(!pte_present(pte))
    > > goto out;
    > >
    > > page_base = pte_val(pte);
    > > pa = (va & ~PAGE_MASK) | (page_base & PAGE_MASK);
    > > return pa;
    > >
    > > out:
    > > return -1;
    > > }
    > >
    > > Kaz Kylheku wrote:
    > > > Josef Moellers wrote:
    > > > > I still don't see a difference between a "logical" and a "virtual" address.
    > > > > Apparently the distinction is that "logical addresses" have a simple,
    > > > > fixed mapping to physical addresses (e.g. a constant offset) while
    > > > > virtual addresses have an arbitrary mapping
    > > > > (http://linux.derkeiler.com/Newsgroup.../msg00245.html)
    > > > > IMHO, this is just trying to name things differently.
    > > >
    > > > On some better-designed architectures, there can be sections of memory
    > > > which are not subject to paging, but instead to a simple translation.
    > > > E.g. the kernel segments or "ksegs" on MIPS.
    > > >
    > > > It would not be quite accurate to call these addresses virtual, since
    > > > they don't go through virtual address translation.
    > > >
    > > > It's an advantage to have this feature, because accesses to these
    > > > segments do not map through the TLB, which is precious cache space.
    > > > It's stupid to use virtual address translation to implement a simple
    > > > displacement over a large memory range, and the more physical RAM you
    > > > make visible that way, the stupider it gets due to the greater demand
    > > > on the TLB.
    > > >
    > > > So, ideally, every platform would have a way to do kernel segments, and
    > > > so the MMU hardware would never be used for these logical pages at all.
    > > > Only for highmem pages, and for truly virtual allocations (user space
    > > > mmap, vmalloc).
    > > >
    > > > So it does make sense to distinguish logical and virtual addresses. On
    > > > platforms like i386 can think of logical addresses as being a kernel
    > > > segment inefficiently emulated using the paging hardware.



+ Reply to Thread