歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> Linux內核分析之缺頁中斷

Linux內核分析之缺頁中斷

日期:2017/2/28 16:00:38   编辑:Linux內核

Linux缺頁異常程序必須能夠區分由編程引起的異常以及由引用屬於進程地址空間但還尚未分配物理頁框的頁所引起的異常。在x86-ia32體系上由do_page_fault函數處理,每個版本有所差異,現分析的版本為2.6.32

  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. /* Get the faulting address: */
  27. /*獲取出錯的地址*/
  28. address = read_cr2();
  29. /*
  30. * Detect and handle instructions that would cause a page fault for
  31. * both a tracked kernel page and a userspace page.
  32. */
  33. if (kmemcheck_active(regs))
  34. kmemcheck_hide(regs);
  35. prefetchw(&mm->mmap_sem);
  36. if (unlikely(kmmio_fault(regs, address)))
  37. return;
  38. /*
  39. * We fault-in kernel-space virtual memory on-demand. The
  40. * 'reference' page table is init_mm.pgd.
  41. *
  42. * NOTE! We MUST NOT take any locks for this case. We may
  43. * be in an interrupt or a critical region, and should
  44. * only copy the information from the master page table,
  45. * nothing more.
  46. *
  47. * This verifies that the fault happens in kernel space
  48. * (error_code & 4) == 0, and that the fault was not a
  49. * protection error (error_code & 9) == 0.
  50. */
  51. /*頁訪問出錯地址address在內核空間*/
  52. if (unlikely(fault_in_kernel_space(address))) {
  53. /*檢查標志位確定訪問發生在"內核態"*/
  54. if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
  55. /*如果是內核空間"非連續內存"的訪問,
  56. 則直接拷貝"內核頁表項"到"用戶頁表項"
  57. 如果"內核頁表項"為null,說明內核有BUG,返回-1
  58. 這裡就是把init_mm中addr對應的項拷貝到本進程
  59. 的相應頁表,防止缺頁中斷
  60. */
  61. if (vmalloc_fault(address) >= 0)
  62. return;
  63. /*關於kmemcheck的操作需要設置宏,這個版本
  64. 沒有設置,可以不看;
  65. 檢查不能為vm86模式以及讀寫權限是否正確*/
  66. if (kmemcheck_fault(regs, address, error_code))
  67. return;
  68. }
  69. /* Can handle a stale RO->RW TLB: */
  70. /*內核空間的地址,檢查頁表對應項的寫、執行權限*/
  71. if (spurious_fault(error_code, address))
  72. return;
  73. /* kprobes don't want to hook the spurious faults: */
  74. if (notify_page_fault(regs))
  75. return;
  76. /*
  77. * Don't take the mm semaphore here. If we fixup a prefetch
  78. * fault we could otherwise deadlock:
  79. */
  80. /*如果上面的檢查不能搞定直接進入"非法訪問"處理函數*/
  81. bad_area_nosemaphore(regs, error_code, address);
  82. return;
  83. }
  84. /* kprobes don't want to hook the spurious faults: */
  85. if (unlikely(notify_page_fault(regs)))
  86. return;
  87. /*
  88. * It's safe to allow irq's after cr2 has been saved and the
  89. * vmalloc fault has been handled.
  90. *
  91. * User-mode registers count as a user access even for any
  92. * potential system fault or CPU buglet:
  93. */
  94. if (user_mode_vm(regs)) {
  95. local_irq_enable();
  96. error_code |= PF_USER;
  97. } else {
  98. if (regs->flags & X86_EFLAGS_IF)
  99. local_irq_enable();
  100. }
  101. if (unlikely(error_code & PF_RSVD))/*使用了保留位*/
  102. /*CPU寄存器和內核態堆棧的全部轉儲打印到控制台,
  103. 以及頁表的相關信息,並輸出到一個系統消息緩沖
  104. 區,然後調用函數do_exit()殺死當前進程*/
  105. pgtable_bad(regs, error_code, address);
  106. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  107. /*
  108. * If we're in an interrupt, have no user context or are running
  109. * in an atomic region then we must not take the fault:
  110. */
  111. /*如果運行在中斷環境中,沒有用戶上下文
  112. 或運行在臨界區中*/
  113. if (unlikely(in_atomic() || !mm)) {
  114. bad_area_nosemaphore(regs, error_code, address);
  115. return;
  116. }
  117. /*
  118. * When running in the kernel we expect faults to occur only to
  119. * addresses in user space. All other faults represent errors in
  120. * the kernel and should generate an OOPS. Unfortunately, in the
  121. * case of an erroneous fault occurring in a code path which already
  122. * holds mmap_sem we will deadlock attempting to validate the fault
  123. * against the address space. Luckily the kernel only validly
  124. * references user space from well defined areas of code, which are
  125. * listed in the exceptions table.
  126. *
  127. * As the vast majority of faults will be valid we will only perform
  128. * the source reference check when there is a possibility of a
  129. * deadlock. Attempt to lock the address space, if we cannot we then
  130. * validate the source. If this is invalid we can skip the address
  131. * space check, thus avoiding the deadlock:
  132. */
  133. /*此時可以確定出錯addr在用戶空間*/
  134. if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
  135. /*錯誤發生在"內核態",查看異常表
  136. 如果在內核態引起缺頁,則引起缺頁的
  137. "指令地址"一定在"異常表"中
  138. 如果"異常表"中返回指令地址
  139. ,則說明可能是"請求調頁",也可能是"非法訪問"
  140. 如果"異常表"中無地址,則肯定是內核錯誤
  141. */
  142. if ((error_code & PF_USER) == 0 &&
  143. !search_exception_tables(regs->ip)) {
  144. bad_area_nosemaphore(regs, error_code, address);
  145. return;
  146. }
  147. down_read(&mm->mmap_sem);
  148. } else {
  149. /*
  150. * The above down_read_trylock() might have succeeded in
  151. * which case we'll have missed the might_sleep() from
  152. * down_read():
  153. */
  154. might_sleep();
  155. }
  156. /*尋找address所在的vma*/
  157. vma = find_vma(mm, address);
  158. /*如果address之後無vma,則肯定是非法訪問*/
  159. if (unlikely(!vma)) {
  160. bad_area(regs, error_code, address);
  161. return;
  162. }
  163. /*1 如果vma->start_address<=address,則直接跳到 "合法訪問"階段
  164. 2 如果vma->start_address>address,則也有可能是用戶的"入棧行為"導致缺頁*/
  165. if (likely(vma->vm_start <= address))
  166. goto good_area;
  167. /* "入棧"操作,則該vma的標志為 "向下增長"*/
  168. if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
  169. bad_area(regs, error_code, address);
  170. return;
  171. }
  172. /*確定缺頁發生在"用戶態"*/
  173. if (error_code & PF_USER) {
  174. /*
  175. * Accessing the stack below %sp is always a bug.
  176. * The large cushion allows instructions like enter
  177. * and pusha to work. ("enter $65535, $31" pushes
  178. * 32 pointers and then decrements %sp by 65535.)
  179. */
  180. /*驗證缺頁address和棧頂sp的關系*/
  181. if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
  182. bad_area(regs, error_code, address);
  183. return;
  184. }
  185. }/*擴展棧*/
  186. if (unlikely(expand_stack(vma, address))) {
  187. bad_area(regs, error_code, address);
  188. return;
  189. }
  190. /*
  191. * Ok, we have a good vm_area for this memory access, so
  192. * we can handle it..
  193. */
  194. good_area:
  195. write = error_code & PF_WRITE;
  196. /*再次驗證"權限"*/
  197. if (unlikely(access_error(error_code, write, vma))) {
  198. bad_area_access_error(regs, error_code, address);
  199. return;
  200. }
  201. /*
  202. * If for any reason at all we couldn't handle the fault,
  203. * make sure we exit gracefully rather than endlessly redo
  204. * the fault:
  205. */
  206. /*分配新"頁框"*/
  207. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  208. if (unlikely(fault & VM_FAULT_ERROR)) {
  209. mm_fault_error(regs, error_code, address, fault);
  210. return;
  211. }
  212. if (fault & VM_FAULT_MAJOR) {
  213. tsk->maj_flt++;
  214. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  215. regs, address);
  216. } else {
  217. tsk->min_flt++;
  218. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  219. regs, address);
  220. }
  221. check_v8086_mode(regs, address, tsk);
  222. up_read(&mm->mmap_sem);
  223. }

大致流程中分為:

地址為內核空間:

1,當地址為內核地址空間並且在內核中訪問時,如果是非連續內存地址,將init_mm中對應的項復制到本進程對應的頁表項做修正;

2,地址為內核空間時,檢查頁表的訪問權限;

3,如果1,2沒搞定,跳到非法訪問處理(在後面詳細分析這個);

地址為用戶空間:

4,如果使用了保留位,打印信息,殺死當前進程;

5,如果在中斷上下文中火臨界區中時,直接跳到非法訪問;

6,如果出錯在內核空間中,查看異常表,進行相應的處理;

7,查找地址對應的vma,如果找不到,直接跳到非法訪問處,如果找到正常,跳到good_area;

8,如果vma->start_address>address,可能是棧太小,對齊進行擴展;

9,good_area處,再次檢查權限;

10,權限正確後分配新頁框,頁表等;

Copyright © Linux教程網 All Rights Reserved