I was always confused when I was doing this. I know that all processes and all kernel threads share the kernel page table, which is the page table pointed to by more than 768 directory items in the global directory of the page, I always thought that when creating a new process, when creating the global directory of the page of the new process, it would be reasonable to copy the basic global directory of the kernel, when I see many articles on the internet saying this, I seem to feel gratified: I am so talented! However, when I read the source code 2.6.17, the dream was broken and the above action was not found in pgd_alloc. The Code is as follows:
Pgd_t * pgd_alloc (struct mm_struct * mm)
{
Int I;
Pgd_t * PGD = quicklist_alloc (0, gfp_kernel, pgd_ctor );
If (ptrs_per_pmd = 1 |! PGD)
Return PGD;
For (I = 0; I unshared_ptrs_per_pgd; ++ I ){
Pmd_t * PMD = pmd_cache_alloc (I );
If (! PMD)
Goto out_oom;
Paravirt_alloc_pd (_ Pa (PMD)> page_shift );
Set_pgd (& PGD [I], _ PGD (1 + _ Pa (PMD )));
}
Return PGD;
Out_oom:
For (I --; I> = 0; I --){
Pgd_t pgdent = PGD [I];
Void * PMD = (void *) _ VA (pgd_val (pgdent)-1 );
Paravirt_release_pd (_ Pa (PMD)> page_shift );
Pmd_cache_free (PMD, I );
}
Quicklist_free (0, pgd_dtor, PGD );
Return NULL;
}
We can see that only page Directory items before 768 are initialized, and there is no trace of the kernel page Directory. What is going on? What is the basis for so many articles on the Internet? Confused, I thought of the version issue, so I checked the 2.4 series kernel source code, which is indeed the case:
172 pgd_t * pgd_alloc (struct mm_struct * mm)
{
Int I;
Pgd_t * PGD = kmem_cache_alloc (pae_pgd_cachu, gfp_kernel );
If (PGD ){
Unsigned long PMD;
For (I = 0; I user_ptrs_per_pgd; I ++ ){
PMD = _ get_free_page (gfp_kernel );
If (! PMD)
Goto out_oom;
Clear_page (PMD );
Set_pgd (PGD + I, _ PGD (1 + _ Pa (PMD )));
}
Memcpy (PGD + user_ptrs_per_pgd,
Swapper_pg_dir + user_ptrs_per_pgd,
(Ptrs_per_pgd-user_ptrs_per_pgd) * sizeof (pgd_t ));
}
Return PGD;
Out_oom:
For (I --; I> = 0; I --)
Free_page (unsigned long) _ VA (pgd_val (PGD [I])-1 ));
Kmem_cache_free (pae_pgd_cachu, PGD );
Return NULL;
}
It seems much longer than the 2.6 kernel. It is reasonable to do so, but is it necessary? Will user processes enter the kernel so frequently to access the kernel? Think about the malloc library function, and then think about the user's Io buffer provided by the Library. If you have not studied the first two, MMAP should always know, why should we use the previous library function, in addition, their library-level implementation is very complicated. They have their own set of strategies. What is the temptation of library designers to pay such a high price? The reason is that in order to make as few system calls as possible to enter the kernel space, we need to know that the purpose of the computer is to serve people, and to serve people is to run user programs, that is, user processes, it is not for people to study the operating system. The operating system kernel only provides services for overall management. Unfortunately, it is shared with user processes.
Processor resources, which requires that it must complete its own tasks in the shortest time, the whole process only needs to distinguish a primary-secondary relationship, the user process is the main, the kernel runs as a supplement, the user does not ask for help, the kernel should not intervene in user transactions. The kernel will come forward only when the user really wants the kernel, so that the kernel transactions will not be dragged to the end, as a result, I thought of page-missing interruptions when I copied the previous issue. Let's see how to deal with page-missing interruptions:
Asmlinkage void do_page_fault (struct pt_regs * regs, unsigned long error_code)
{
......
If (unlikely (address> = task_size )){
If (! (Error_code & 5 ))
Goto vmalloc_fault;
......
Vmalloc_fault:
{
......
Int Index = pgd_index (Address); // obtain the index of the global directory of the page where the page address is missing
Pgd_t * PGD, * pgd_k;
Pmd_t * PMD, * pmd_k;
Pte_t * pte_k;
ASM ("movl % 32a, % 0": "= r" (PGD); // read the location of the global directory of the current process page
PGD = index + (pgd_t *) _ VA (PGD); // you can find the directory items corresponding to the missing page address.
Pgd_k = init_mm.pgd + index; // directory items for the swapper_pgd_dir squadron
If (! Pgd_present (* pgd_k) // If none of the swapper_pgd_dir templates are available, do it after preparation.
Goto no_context; // aftercare
......
PMD = pmd_offset (PGD, address); // The following analysis method is the same as above:
Pmd_k = pmd_offset (pgd_k, address );
If (! Pmd_present (* pmd_k ))
Goto no_context; // aftercare
Set_pmd (PMD, * pmd_k );
Pte_k = pte_offset_kernel (pmd_k, address); // the kernel and the user process share the kernel page table. Therefore, there is no such thing as set_pmd (* pmd_k) below.
If (! Pte_present (* pte_k ))
Goto no_context;
Return; // the MMU element that eventually causes the page missing address is created and the access restarts.
}
}
The above analysis should be very clear. Here I will whisper that the online articles are just the author's understanding. They can only help you understand the problem and solve the problem. You have to come by yourself, for example, to read the kernel, you must read it yourself.