Find the answer to your Linux question:
Results 1 to 7 of 7
I have following understanding, please correct it if it is wrong. 1) kernel has direct mapping for first 896 MB of RAM. 2) kernel sets up the page table during ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    55

    kerenel addess space


    I have following understanding, please correct it if it is wrong.
    1) kernel has direct mapping for first 896 MB of RAM.
    2) kernel sets up the page table during the initialization phase in function pagetable_init()
    3) whenever kernel virtual address is given, it always goes through MMU and page tables, always.
    4) but there may be a requirement where kernel needs to get corresponding virtual address from physical address (where physical address are known in advance), that is why direct mapping of first 896 MB is done.
    Direct mapping is also done because, when kernel wants a page, it does not need to waste time in allocating ptes and making an entry.
    please someone elaborate on fourth point, if there is something missing.
    5)If corresponding physical page to kernel virtual address is already allocated to the user process, then kernel virtual address become unusable !!
    6) is there any dynamic mapping done for kernel page tables ? on first 896 MB of RAM ? or they are always setup in the beginning ?

  2. #2
    Just Joined!
    Join Date
    Sep 2008
    Posts
    25
    Well I am not a Linux expert so anybody correct if I am wrong:

    1) kernel has direct mapping for first 896 MB of RAM.
    I think it is true.

    2) kernel sets up the page table during the initialization phase in function pagetable_init()
    I didn't do a deep code search but might be true

    3) whenever kernel virtual address is given, it always goes through MMU and page tables, always.
    Did you think also about TLB.

    4) but there may be a requirement where kernel needs to get corresponding virtual address from physical address (where physical address are known in advance), that is why direct mapping of first 896 MB is done.
    Direct mapping is also done because, when kernel wants a page, it does not need to waste time in allocating ptes and making an entry.
    please someone elaborate on fourth point, if there is something missing.
    I think the reason why Linux does this mapping is that it can't access memory which hasn't been directly mapped into its address space.

    5)If corresponding physical page to kernel virtual address is already allocated to the user process, then kernel virtual address become unusable !!
    I'm not sure whether I understand you well but memory is assigned to process per-pages so once the page belongs to process then kernel will not use it for its purposes probably, but it doesn't not mean that kernel can't access it.

    6) is there any dynamic mapping done for kernel page tables ? on first 896 MB of RAM ? or they are always setup in the beginning ?
    I think dynamic mapping is done for high memory area and the first 896MB are always setup at beginning.

    Regards,
    ndevel.

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    55
    let us discuss point 4
    as yous said "I think the reason why Linux does this mapping is that it can't access memory which hasn't been directly mapped into its address space."

    Linux kernel has capability to setup and allocate pagetable on the fly.
    so it can exactly set up the page table for ZONE_NORMAL as it does for high memory (dynamic memory)
    so kernel can access virtually any part of RAM by dynamic mapping....(for its whole virtual address space)

    I would not agree with the reason you have given why kernel establish one to one mapping in the beginning.

    would you please re-look into fourth point ?

  4. #4
    Just Joined!
    Join Date
    Sep 2008
    Posts
    25
    Linux kernel has capability to setup and allocate pagetable on the fly.
    so it can exactly set up the page table for ZONE_NORMAL as it does for high memory (dynamic memory)
    so kernel can access virtually any part of RAM by dynamic mapping....(for its whole virtual address space)
    ... and as you wrote it takes some time so I think that's the reason to do so once at the beginning.

  5. #5
    Just Joined!
    Join Date
    May 2008
    Posts
    55
    The reason what I gave is my speculation. I am not sure whether there are other reasons .
    if you have other reasons then let me know.

  6. #6
    Just Joined!
    Join Date
    Oct 2008
    Posts
    6
    let us refer to source codes:

    to setup one page table, (which assume that PDE/PTE and PGD tables are setup):

    /*
    * Create a page table and place a pointer to it in a middle page
    * directory entry:
    */
    static pte_t * __init one_page_table_init(pmd_t *pmd)
    {

    Next, this function create the PMD/PTE/pagetable etc from PAGE_OFFSET onwards (0xc000000 - and this is the 1G linear address dedicated to kernel, assuming 3G/1G memory split).

    /*
    * This maps the physical memory to kernel virtual address space, a total
    * of max_low_pfn pages, by creating page tables starting from address
    * PAGE_OFFSET:
    */
    static void __init kernel_physical_mapping_init(pgd_t *pgd_base,
    unsigned long start_pfn,
    unsigned long end_pfn,
    int use_pse)
    {
    int pgd_idx, pmd_idx, pte_ofs;

    reading into this function, u can clearly see that the entire pagetable is setup here.

    kernel_physical_mapping_init() is called by init_memory_mapping() in kernel/setup.c:setup_arch().

    and after kernel_physical_mapping_init()==>load_cr3() is called, which will enable the pagetable mechanism.

    quite a many lines after calling init_memory_mapping(), it will call paging_init() (which then call pagetable_init()). Inside pagetable_init() is just setting up pointers, but the contents of the pagetable has already been setup earlier.

    my answer:

    I have following understanding, please correct it if it is wrong.
    1) kernel has direct mapping for first 896 MB of RAM.

    yes, called identity mapping......but like u said later....MMU always execute...so pagetable always still must be setup.

    2) kernel sets up the page table during the initialization phase in function pagetable_init()

    explained earlier..

    3) whenever kernel virtual address is given, it always goes through MMU and page tables, always.

    yes.

    4) but there may be a requirement where kernel needs to get corresponding virtual address from physical address (where physical address are known in advance), that is why direct mapping of first 896 MB is done.

    and yes, u are right, identity mapping certainly does helped a lot, especially during the early phase of setup, or in arch without MMU. but that is normally only the 1st 1M (ARM), or 8M (x86), correct?

    Direct mapping is also done because, when kernel wants a page, it does not need to waste time in allocating ptes and making an entry.
    please someone elaborate on fourth point, if there is something missing.

    u have a point there, but people are also trying hard to remove it as early as possible:

    LKML: Vivek Goyal: [PATCH 14/20] x86_64: Remove the identity mapping as early as possible

    5)If corresponding physical page to kernel virtual address is already allocated to the user process, then kernel virtual address become unusable !!

    what do u mean? < PAGE_OFFSET is for userspace, and > PAGE_OFFSET is assigned to kernel, how can the two page ever conflict each other?

    6) is there any dynamic mapping done for kernel page tables ? on first 896 MB of RAM ? or they are always setup in the beginning ?

    erh......question is vague. dynamic kernel mapping is implemented via kmap(). is this what u wanted to know? for certain hardware device, that must always use certain fixed address to access them, but the data is located in high memory, we can do a "ioremap" to map the high memory to the low memory, and kmap() involved modifying the pagetable TEMPORARILY, so that read/write goes to where it is needed. and as it is a precious resource, kunmap() must be called ASAP when it it no longer needed, so that another part of the kernel can use that mapping (KM_PTE0).

  7. #7
    Just Joined!
    Join Date
    May 2008
    Posts
    55
    thank you for your reply.
    lets us talk about 5th and 6th point.

    5) what I meant was : kernel has direct mapping to 896 MB of RAM.
    now user does a malloc....kernel gives the page from that 896 MB RAM. (in short ZONE_NORMAL)

    now as page address ; say (ox40000000-physical address) ; is allocated to user space...
    the corresponding directly mapped kernal virtual address
    (0xC0000000 + 0x40000000) bcomes unusuable, in the sense kernel can not issue any virtual address from (0xC0000000 + 0x40000000) to page_size

    6) I have an idea that dynamic mapping is done through kmap.
    as far as I know ioremap is called when we know the physical address of io and which falls into ZONE_NORMAL where we can just map it.
    I do not understand data being in high memory.
    mostly DMA capability IO use ZONE_DMA for buffer.
    the bounce buffer can be in high memory.

    7) I have one further question.
    while writing kernel module if I do get_free_pages, and kernel fails to allocate from ZONE_NORMAL then, is it advisable to get the page from high memory ?
    if yes/no, justify please.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •