如何在linux内核中提供页面错误?

时间:2014-03-10 00:20:37

标签: linux memory-management linux-kernel

我正在开发一个需要在Linux内核中进行大量修改的项目。在其中一个修改中,我必须改变页面错误处理程序的工作方式。我希望拦截来自特定进程的页面错误,并通过从另一台机器复制数据来满足它们。

作为第一步,我想写一些实验代码,可以帮助我理解Linux如何满足页面错误,以及它如何告诉进程现在无法提供页面错误,它需要做一个稍后重试。

所以,我想以一种帮助我理解上述所有内容的方式修改handle_mm_fault。像这样:

int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
                     unsigned long address, unsigned int flags)
{
     /* some code */
     if(current->pid == my_target_pid)
     {
         /*
          1. Chose randomly between 1 and 2 -> rand_num
          2. if rand_num from (1) is 1 then allocate a block of memory, write 'X's to it and then give it to the process and then return.
          3. if rand_num from (1) is 2 then tell process to come back later and then return.
         */
     }
     /* rest of handle_mm_fault for all other process here */
}

2 个答案:

答案 0 :(得分:1)

您可以查看struct vm_operations_struct。其功能成员'故障'用于处理页面故障情况

答案 1 :(得分:0)

您描述的问题听起来像是要求数据中止的页面。 首先,由于来自内核空间或用户空间的无效页面映射,可能会发生数据中止。 handle_mm_fault是修复linux中用户空间页表的子例程。根据我的理解,您的设计必须涵盖以下内容。

  1. 您需要一个设计来跟踪正确的PID。
  2. 你有没有考虑过,你如何决定哪个部分的vma应该依赖于要求? 页?整个过程VMA还是只是一些部分? Linux可以使用其他技术为用户程序创建内存映射,例如mmap。
  3. 为了避免重试,您还是必须修复映射 因为CPU将从中止位置恢复执行。如果你不能 服务器立即从您指定的区域映射,a 应该创建临时映射并稍后将其分页。