Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1044986
  • 博文数量: 277
  • 博客积分: 8313
  • 博客等级: 中将
  • 技术积分: 2976
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-22 11:25
文章分类

全部博文(277)

文章存档

2013年(17)

2012年(66)

2011年(104)

2010年(90)

我的朋友

分类: LINUX

2012-06-21 10:02:40

linux缺页异常程序必须能够区分由编程引起的异常以及由引用属于进程地址空间但还尚未分配物理页框的页所引起的异常。在x86-ia32体系上由do_page_fault函数处理,每个版本有所差异,现分析的版本为2.6.32

[cpp] view plaincopyprint?

1.        /* 

2.        regs:该结构包含当异常发生时的微处理器寄存器的值 

3.        3位的error_code,当异常发生时由控制单元压入栈中 

4.        -如果第0位被清0,则异常由访问一个不存在的页所 

5.        引起,否则,则异常由无效的访问权限所引起; 

6.        -如果第1位被清0,表示异常由读访问或者执行访问 

7.        所引起,反之,异常由写访问引起; 

8.        -如果第2位被清0,则异常发生在处理器处于内核态 

9.        时,否则,异常发生在处理器处于用户态时 

10.     -如果3位为1表示检测到使用了保留位。4位为1表示 

11.     1表示缺页异常是在取指令的时候出现的 

12.      */  

13.     dotraplinkage void __kprobes  

14.     do_page_fault(struct pt_regs *regs, unsigned long error_code)  

15.     {  

16.         struct vm_area_struct *vma;  

17.         struct task_struct *tsk;  

18.         unsigned long address;  

19.         struct mm_struct *mm;  

20.         int write;  

21.         int fault;  

22.         /*获取当前cpu正在运行的进程的进程描述符 

23.             然后获取该进程的内存描述符*/  

24.         tsk = current;  

25.         mm = tsk->mm;  

26.       

27.         /* Get the faulting address: */  

28.         /*获取出错的地址*/  

29.         address = read_cr2();  

30.       

31.         /* 

32.          * Detect and handle instructions that would cause a page fault for 

33.          * both a tracked kernel page and a userspace page. 

34.          */  

35.         if (kmemcheck_active(regs))  

36.             kmemcheck_hide(regs);  

37.         prefetchw(&mm->mmap_sem);  

38.       

39.         if (unlikely(kmmio_fault(regs, address)))  

40.             return;  

41.       

42.         /* 

43.          * We fault-in kernel-space virtual memory on-demand. The 

44.          * 'reference' page table is init_mm.pgd. 

45.          * 

46.          * NOTE! We MUST NOT take any locks for this case. We may 

47.          * be in an interrupt or a critical region, and should 

48.          * only copy the information from the master page table, 

49.          * nothing more. 

50.          * 

51.          * This verifies that the fault happens in kernel space 

52.          * (error_code & 4) == 0, and that the fault was not a 

53.          * protection error (error_code & 9) == 0. 

54.          */  

55.          /*页访问出错地址address在内核空间*/  

56.         if (unlikely(fault_in_kernel_space(address))) {  

57.             /*检查标志位确定访问发生在"内核态"*/  

58.             if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {  

59.                 /*如果是内核空间"非连续内存"的访问, 

60.                             则直接拷贝"内核页表项""用户页表项" 

61.                             如果"内核页表项"null,说明内核有BUG,返回-1 

62.                 这里就是把init_mmaddr对应的项拷贝到本进程 

63.                 的相应页表,防止缺页中断 

64.                 */  

65.                 if (vmalloc_fault(address) >= 0)  

66.                     return;  

67.                 /*关于kmemcheck的操作需要设置宏,这个版本 

68.                 没有设置,可以不看; 

69.                 检查不能为vm86模式以及读写权限是否正确*/   

70.                 if (kmemcheck_fault(regs, address, error_code))  

71.                     return;  

72.             }  

73.       

74.             /* Can handle a stale RO->RW TLB: */  

75.             /*内核空间的地址,检查页表对应项的写、执行权限*/  

76.             if (spurious_fault(error_code, address))  

77.                 return;  

78.       

79.             /* kprobes don't want to hook the spurious faults: */  

80.             if (notify_page_fault(regs))  

81.                 return;  

82.             /* 

83.              * Don't take the mm semaphore here. If we fixup a prefetch 

84.              * fault we could otherwise deadlock: 

85.              */  

86.              /*如果上面的检查不能搞定直接进入"非法访问"处理函数*/  

87.                     bad_area_nosemaphore(regs, error_code, address);  

88.       

89.             return;  

90.         }  

91.       

92.         /* kprobes don't want to hook the spurious faults: */  

93.         if (unlikely(notify_page_fault(regs)))  

94.             return;  

95.         /* 

96.          * It's safe to allow irq's after cr2 has been saved and the 

97.          * vmalloc fault has been handled. 

98.          * 

99.          * User-mode registers count as a user access even for any 

100.       * potential system fault or CPU buglet: 

101.       */  

102.      if (user_mode_vm(regs)) {  

103.          local_irq_enable();  

104.          error_code |= PF_USER;  

105.      } else {  

106.          if (regs->flags & X86_EFLAGS_IF)  

107.              local_irq_enable();  

108.      }  

109.    

110.      if (unlikely(error_code & PF_RSVD))/*使用了保留位*/  

111.          /*CPU寄存器和内核态堆栈的全部转储打印到控制台, 

112.          以及页表的相关信息,并输出到一个系统消息缓冲 

113.          区,然后调用函数do_exit()杀死当前进程*/  

114.          pgtable_bad(regs, error_code, address);  

115.    

116.      perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);  

117.    

118.      /* 

119.       * If we're in an interrupt, have no user context or are running 

120.       * in an atomic region then we must not take the fault: 

121.       */  

122.       /*如果运行在中断环境中,没有用户上下文 

123.       或运行在临界区中*/  

124.      if (unlikely(in_atomic() || !mm)) {  

125.          bad_area_nosemaphore(regs, error_code, address);  

126.          return;  

127.      }  

128.    

129.      /* 

130.       * When running in the kernel we expect faults to occur only to 

131.       * addresses in user space.  All other faults represent errors in 

132.       * the kernel and should generate an OOPS.  Unfortunately, in the 

133.       * case of an erroneous fault occurring in a code path which already 

134.       * holds mmap_sem we will deadlock attempting to validate the fault 

135.       * against the address space.  Luckily the kernel only validly 

136.       * references user space from well defined areas of code, which are 

137.       * listed in the exceptions table. 

138.       * 

139.       * As the vast majority of faults will be valid we will only perform 

140.       * the source reference check when there is a possibility of a 

141.       * deadlock. Attempt to lock the address space, if we cannot we then 

142.       * validate the source. If this is invalid we can skip the address 

143.       * space check, thus avoiding the deadlock: 

144.       */  

145.       /*此时可以确定出错addr在用户空间*/  

146.      if (unlikely(!down_read_trylock(&mm->mmap_sem))) {  

147.          /*错误发生在"内核态",查看异常表 

148.                  如果在内核态引起缺页,则引起缺页的 

149.                  "指令地址"一定在"异常表" 

150.                  如果"异常表"中返回指令地址 

151.                  ,则说明可能是"请求调页",也可能是"非法访问" 

152.                  如果"异常表"中无地址,则肯定是内核错误 

153.          */  

154.          if ((error_code & PF_USER) == 0 &&  

155.              !search_exception_tables(regs->ip)) {  

156.              bad_area_nosemaphore(regs, error_code, address);  

157.              return;  

158.          }  

159.          down_read(&mm->mmap_sem);  

160.      } else {  

161.          /* 

162.           * The above down_read_trylock() might have succeeded in 

163.           * which case we'll have missed the might_sleep() from 

164.           * down_read(): 

165.           */  

166.          might_sleep();  

167.      }  

168.      /*寻找address所在的vma*/  

169.      vma = find_vma(mm, address);  

170.      /*如果address之后无vma,则肯定是非法访问*/  

171.      if (unlikely(!vma)) {  

172.          bad_area(regs, error_code, address);  

173.          return;  

174.      }  

175.        

176.      /*1 如果vma->start_address<=address,则直接跳到 "合法访问"阶段 

177.              2 如果vma->start_address>address,则也有可能是用户的"入栈行为"导致缺页*/  

178.      if (likely(vma->vm_start <= address))  

179.          goto good_area;  

180.      /* "入栈"操作,则该vma的标志为 "向下增长"*/  

181.      if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {  

182.          bad_area(regs, error_code, address);  

183.          return;  

184.      }  

185.      /*确定缺页发生在"用户态"*/  

186.      if (error_code & PF_USER) {  

187.          /* 

188.           * Accessing the stack below %sp is always a bug. 

189.           * The large cushion allows instructions like enter 

190.           * and pusha to work. ("enter $65535, $31" pushes 

191.           * 32 pointers and then decrements %sp by 65535.) 

192.           */  

193.           /*验证缺页address和栈顶sp的关系*/  

194.          if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {  

195.              bad_area(regs, error_code, address);  

196.              return;  

197.          }  

198.      }/*扩展栈*/  

199.      if (unlikely(expand_stack(vma, address))) {  

200.          bad_area(regs, error_code, address);  

201.          return;  

202.      }  

203.    

204.      /* 

205.       * Ok, we have a good vm_area for this memory access, so 

206.       * we can handle it.. 

207.       */  

208.  good_area:  

209.      write = error_code & PF_WRITE;  

210.      /*再次验证"权限"*/  

211.      if (unlikely(access_error(error_code, write, vma))) {  

212.          bad_area_access_error(regs, error_code, address);  

213.          return;  

214.      }  

215.    

216.      /* 

217.       * If for any reason at all we couldn't handle the fault, 

218.       * make sure we exit gracefully rather than endlessly redo 

219.       * the fault: 

220.       */  

221.       /*分配新"页框"*/  

222.      fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);  

223.    

224.      if (unlikely(fault & VM_FAULT_ERROR)) {  

225.          mm_fault_error(regs, error_code, address, fault);  

226.          return;  

227.      }  

228.    

229.      if (fault & VM_FAULT_MAJOR) {  

230.          tsk->maj_flt++;  

231.          perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,  

232.                       regs, address);  

233.      } else {  

234.          tsk->min_flt++;  

235.          perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,  

236.                       regs, address);  

237.      }  

238.    

239.      check_v8086_mode(regs, address, tsk);  

240.    

241.      up_read(&mm->mmap_sem);  

242.  }  

大致流程中分为:

地址为内核空间:

1,当地址为内核地址空间并且在内核中访问时,如果是非连续内存地址,将init_mm中对应的项复制到本进程对应的页表项做修正;

2,地址为内核空间时,检查页表的访问权限;

3,如果1,2没搞定,跳到非法访问处理(在后面详细分析这个);

地址为用户空间:

4,如果使用了保留位,打印信息,杀死当前进程;

5,如果在中断上下文中火临界区中时,直接跳到非法访问;

6,如果出错在内核空间中,查看异常表,进行相应的处理;

7,查找地址对应的vma,如果找不到,直接跳到非法访问处,如果找到正常,跳到good_area

8,如果vma->start_address>address,可能是栈太小,对齐进行扩展;

9good_area处,再次检查权限;

10,权限正确后分配新页框,页表等;

 

阅读(1329) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~