该函数传递进来的两个参数--
regs包含了各个寄存器的值
error_code是触发异常的错误类型,它的含义如下
- /*
- * Page fault error code bits:
- *
- * bit 0 == 0: no page found 1: protection fault
- * bit 1 == 0: read access 1: write access
- * bit 2 == 0: kernel-mode access 1: user-mode access
- * bit 3 == 1: use of reserved bit detected
- * bit 4 == 1: fault was an instruction fetch
- */
- enum x86_pf_error_code {
- PF_PROT = 1 << 0,
- PF_WRITE = 1 << 1,
- PF_USER = 1 << 2,
- PF_RSVD = 1 << 3,
- PF_INSTR = 1 << 4,
- };
首先要检查该异常的触发地址是不是位于内核地址空间 也就是address>=TASK_SIZE_MAX,一般为3GB。然后要检查触发异常时是否处于内核态,满足这两个条件就尝试通过vmalloc_fault()来解决这个异常。由于使用vmalloc申请内存时,内核只会更新主内核页表,所以当前使用的进程页表就有可能因为未与主内核页表同步导致这次异常的触发,因此该函数试图将address对应的页表项与主内核页表进行同步。
- static noinline int vmalloc_fault(unsigned long address)
- {
- unsigned long pgd_paddr;
- pmd_t *pmd_k;
- pte_t *pte_k;
- /* 确定触发异常的地址是否处于VMALLOC区域*/
- if (!(address >= VMALLOC_START && address < VMALLOC_END))
- return -1;
- /*
- * Synchronize this task's top level page-table
- * with the 'reference' page table.
- *
- * Do _not_ use "current" here. We might be inside
- * an interrupt in the middle of a task switch..
- */
- pgd_paddr = read_cr3();//获取当前的PGD地址
- pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);//将当前使用的页表和内核页表同步
- if (!pmd_k)
- return -1;
- /*到这里已经获取了内核页表对应于address的pmd,并且将该值设置给了当前使用页表的pmd,
- 最后一步就是判断pmd对应的pte项是否存在*/
- pte_k = pte_offset_kernel(pmd_k, address);//获取pmd对应address的pte项
- if (!pte_present(*pte_k))//判断pte项是否存在,不存在则失败
- return -1;
- return 0;
- }
