If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below. ** If you are logged in, most ads will not be displayed. **

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 ?

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.

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.

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.

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.

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()

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:

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).

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.