I'm just asking this question because I'm curious how the Linux kernel works. According to http://i-web.i.u-tokyo.ac.jp/edu/training/ss/lecture/new-documents/Lectures/02-VirtualMemory/VirtualMemory.ppt Windows uses special entries in its page directory and page tables named self-map in order to be able to manipulate page directory/tables content from kernel virtual address space. If anyone is familiar with Linux memory management, please tell me if Linux kernel handle this problem in a similar or different way. Thanks.
1 Answers
Yes, in Linux also page tables are mapped to address space. But paging data structures in some of the architectures may use physical addresses. So it not fixed in Linux. But you can access the table easily.
Here is the kernel code to access the page table
struct mm_struct *mm = current->mm;
pgd = pgd_offset(mm, address);
pmd = pmd_offset(pgd, address);
pte = *pte_offset_map(pmd, address);
To understand more about Linux memory management see this
Cr3 register on IA32 stores the page table base pointer (pgd pointer), which stores physical address. This is true even for Windows (as it is a feature of the x86 processor, not of the OS).
Read this article to understand IA32 paging.
Edit2:
Task struct contains a mm_struct instance related to Memory management of that task (so a process), this mm_struct
has a pgd_t * pgd
. load_cr3 loads a physical address of page directory table in cr3
register but it takes the virtual address of pgt. So mm_struct
contains the virtual address of pgt
.
Since page tables are in kernel space and kernel virtual memory is mapped directly to ram it's just easy macro.
-
Could you please comment just a little bit about this code? What exactly do this functions do? And what are pgd, pmd, pte? Virtual addresses of page directory, page middle directory and page table entry? What is address? Thanks. – tichy Mar 11 '11 at 17:10
-
actually they are structures if you see but that just for abstraction, deep inside they finally get you the virtual results of the "page directory", "page middle directory" and "page table entry" – Zimbabao Mar 11 '11 at 17:22
-
Do the strcutures store physical or virtual addresses of the appropriate tables? – tichy Mar 11 '11 at 22:54
-
They store virtual address of the tables. – Zimbabao Mar 12 '11 at 03:32
-
Just one more question - the macro pgd_offset is defined as #define pgd_offset(mm, address) ((mm)->pgd + pgd_index((address))). Does mm->pgd store the physical or virtual address of page directory? – tichy Mar 13 '11 at 21:32
-
It actually contains physical address on IA32 architecture,I need to check about other architectures. – Zimbabao Mar 14 '11 at 04:15
-
Macro pgd_offset is defined as #define pgd_offset(mm, address) ((mm)->pgd + pgd_index((address))). If mm->pgd is physical address, I guess pgd struct also contain physical, not virtual address. The same with pmd and pte. – tichy Mar 14 '11 at 10:05
-
Yeah. trying to find out if its for all architectures or few .. Couldn't get anything in Intel documentation. – Zimbabao Mar 14 '11 at 10:19
-
OK, so to access the pgd the kernel only needs now to do something like pointer = (void*)__va(pgd) and it has got virtual address of pgd. I'm just wondering if page tables for some process can be placed in high memory and if so, how can the kernel access it. – tichy Mar 14 '11 at 10:43
-
Yes. I have modified answer with few more points. Page tables need to be accessed by kernel, so I think it can't be in high mem, need to check though. – Zimbabao Mar 14 '11 at 11:46
-
I've found that page table entries may be stored in high memory: http://www.kernel.org/doc/gorman/html/understand/understand006.html - chapter "PTEs in High Memory". – tichy Mar 14 '11 at 12:26