linux内核分析之缺页中断【转】

转自:http://blog.csdn.net/bullbat/article/details/7108402

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

[cpp] view plain copy

print?

  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,权限正确后分配新页框,页表等;

对于缺页中断的非法访问由函数bad_area执行,该函数的执行情况分为:

1,如果在用户空间访问,直接发送SEGSEGV信号;

2,如果在内核空间访问分为两种情况:

1)地址是一个错误的系统调用参数,修正码(典型是发送SIGSEGV信号);

2)反之,杀死进程并显示内核的OOPS信息;

  1. static void
  2. __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
  3. unsigned long address, int si_code)
  4. {
  5. struct task_struct *tsk = current;
  6. /* User mode accesses just cause a SIGSEGV */
  7. /*如果用户态*/
  8. if (error_code & PF_USER) {
  9. /*
  10. * It‘s possible to have interrupts off here:
  11. */
  12. local_irq_enable();
  13. /*
  14. * Valid to do another page fault here because this one came
  15. * from user space:
  16. */
  17. if (is_prefetch(regs, error_code, address))
  18. return;
  19. if (is_errata100(regs, address))
  20. return;
  21. if (unlikely(show_unhandled_signals))
  22. show_signal_msg(regs, error_code, address, tsk);
  23. /* Kernel addresses are always protection faults: */
  24. tsk->thread.cr2      = address;
  25. tsk->thread.error_code   = error_code | (address >= TASK_SIZE);
  26. tsk->thread.trap_no  = 14;
  27. /*发送SIGSEGV信号*/
  28. force_sig_info_fault(SIGSEGV, si_code, address, tsk);
  29. return;
  30. }
  31. if (is_f00f_bug(regs, address))
  32. return;
  33. /*内核态访问*/
  34. no_context(regs, error_code, address);
  35. }

内核访问时

  1. static noinline void
  2. no_context(struct pt_regs *regs, unsigned long error_code,
  3. unsigned long address)
  4. {
  5. struct task_struct *tsk = current;
  6. unsigned long *stackend;
  7. unsigned long flags;
  8. int sig;
  9. /* Are we prepared to handle this kernel fault? */
  10. /*地址是一个系统调用参数,"修正码",典型情况是发送
  11. SIGSEGV信号*/
  12. if (fixup_exception(regs))
  13. return;
  14. /*
  15. * 32-bit:
  16. *
  17. *   Valid to do another page fault here, because if this fault
  18. *   had been triggered by is_prefetch fixup_exception would have
  19. *   handled it.
  20. *
  21. * 64-bit:
  22. *
  23. *   Hall of shame of CPU/BIOS bugs.
  24. */
  25. if (is_prefetch(regs, error_code, address))
  26. return;
  27. if (is_errata93(regs, address))
  28. return;
  29. /*
  30. * Oops. The kernel tried to access some bad page. We‘ll have to
  31. * terminate things with extreme prejudice:
  32. */
  33. /*下面代码用于oops信息的显示和杀死当前
  34. 进程*/
  35. flags = oops_begin();
  36. show_fault_oops(regs, error_code, address);
  37. stackend = end_of_stack(tsk);
  38. if (*stackend != STACK_END_MAGIC)
  39. printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
  40. tsk->thread.cr2      = address;
  41. tsk->thread.trap_no  = 14;
  42. tsk->thread.error_code   = error_code;
  43. sig = SIGKILL;
  44. if (__die("Oops", regs, error_code))
  45. sig = 0;
  46. /* Executive summary in case the body of the oops scrolled away */
  47. printk(KERN_EMERG "CR2: %016lx\n", address);
  48. oops_end(flags, regs, sig);
  49. }
时间: 2024-08-02 01:02:14

linux内核分析之缺页中断【转】的相关文章

《linux 内核分析》 第4周

王一 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.linux 系统的状态 Linux在x86平台下支持0内核态和3用户态.在内核态32位平台能访问0x00000000以上的空间,而用户态只能访问小于0xc0000000一下的地址空间 (此处的地址空间为逻辑地址).当用户态切换到内核态的时候主要方式为中断. 1.当int128调用时,系统会自动的两个状态下的cs:eip,ss:es

《linux 内核分析》 第二周 实验

王一 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 本次课的核心是通过中断机制完成进程的调度 ,在本次课程中__init my_start_kernel作为入口函数,定义0号进程的tPCB结构体,通过复制来制造其他进程的tPCB数据结构,中断时间函数被 my_timer_handler周期性的调用来修改my_need_sched 的值,而0号进程一直在检测my_need_sched 的

Linux内核分析8

周子轩 原创作品转载请注明出处  <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验目的: 使用gdb跟踪分析一个schedule()函数,理解Linux系统中进程调度的时机. 实验过程: 登陆实验楼虚拟机http://www.shiyanlou.com/courses/195 打开shell终端,执行以下命令: cd LinuxKernel rm -rf menu git clone https://git

《Linux内核分析》课程第七周学习总结

姓名:何伟钦 学号:20135223 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-100002900 学习内容:Linux内核如何装载和启动一个可执行程序 理解编译链接的过程和ELF可执行文件格式: 编程使用exec*库函数加载一个可执行文件,动态链接分为可执行程序装载时动态链接和运行时动态链接,编程练习动态链接库的这两种使用方式: 使用gdb跟踪分析一个execve系统调用内核处

LINUX内核分析第七周学习总结——可执行程序的装载

LINUX内核分析第六周学习总结——进程的描述和进程的创建 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.知识概要 (一)预处理.编译.链接和目标文件的格式 1.可执行程序是怎么得来的 2.目标文件的格式ELF 3.静态链接的ELF可执行文件和进程的地址空间 (二)可执行程序.共享库和动态加载 1.装载可执行程序之前的工作 2.装载时动态链接和运行时动态链接应用举例 (三)

《Linux内核分析》第六周学习小结

进程的描述和进程的创建 一.进程的描述 进程描述符task_struct数据结构: (1)操作系统的三大功能: 进程管理.内存管理.文件系统 (2)进程的作用: 将信号.进程间通信.内存管理和文件系统联系起来 (3)进程控制块PCB——task_struct数据结构 提供了内核需要了解的信息 (4)task_struct结构庞大,有400多行代码.包含了进程状态.内核堆栈等相关信息的定义. (5)Linux的进程和操作系统原理中描述的进程状态有所不同,实际内核中,就绪和运行状态都用TASK_RU

20135201李辰希《Linux内核分析》第六周 进程的描述与创建

李辰希 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.进程的描述 操作系统的三大管理功能: 进程管理(最重要的) 内存管理 文件系统 为了管理进程,内核必须对每个进程进行清晰的描述,进程描述符提供了内核所需了解的进程信息. 进程控制块PCB task_struct: 进程状态 进程打开的文件 进程优先级信息 task_struct总体数据结构的抽象: tty:控制台 fs:文件系统

Linux内核分析——进程的描述和进程的创建

Linux内核分析——进程的描述和进程的创建 20135111李光豫 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.实验内容 阅读理解task_struct数据结构http://codelab.shiyanlou.com/xref/linux-3.18.6/include/linux/sched.h#1235: 分析fork函数对应的内核处理过程sys_clone,理解创建一个新进

《Linux内核分析》第六周学习笔记

<Linux内核分析>第六周学习笔记 进程的描述和创建 郭垚 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 [学习视频时间:1小时 撰写博客时间:2小时] [学习内容:进程创建的过程.使用gdb跟踪分析内核处理函数sys_clone] 一.进程的描述 1.1 进程描述符task_struct数据结构(一) 1. 进程控制块PCB——task_struct 为了管理进程,内核