3
votes

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

1 Answers

6
votes

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.