手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux缺页异常处理--内核空间

[日期:2012-08-16] 来源:Linux社区  作者:vanbreaker [字体:大 中 小]

如果do_page_fault()函数执行到了bad_area_nosemaphore(),那么就表明这次异常是由于对非法的地址访问造成的。在内核中产生这样的结果的情况一般有两种:

1.内核通过用户空间传递的系统调用参数,访问了无效的地址

2.内核的程序设计缺陷

第一种情况内核尚且能通过异常修正机制来进行修复,而第二种情况就会导致OOPS错误了,内核将强制用SIGKILL结束当前进程。

内核态的bad_area_nosemaphore()的实际处理函数为bad_area_nosemaphore()-->__bad_area_nosemaphore()-->no_context()

  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.   
  10.     /* Are we prepared to handle this kernel fault? */  
  11.     /*fixup_exception()用于搜索异常表,并试图找到一个对应该异常的例程来进行修正, 
  12.       这个例程在fixup_exception()返回后执行*/  
  13.     if (fixup_exception(regs))  
  14.         return;  
  15.   
  16.     /* 
  17.      * 32-bit: 
  18.      * 
  19.      *   Valid to do another page fault here, because if this fault 
  20.      *   had been triggered by is_prefetch fixup_exception would have 
  21.      *   handled it. 
  22.      * 
  23.      * 64-bit: 
  24.      * 
  25.      *   Hall of shame of CPU/BIOS bugs. 
  26.      */  
  27.     if (is_prefetch(regs, error_code, address))  
  28.         return;  
  29.   
  30.     if (is_errata93(regs, address))  
  31.         return;  
  32.   
  33.     /* 
  34.      * Oops. The kernel tried to access some bad page. We'll have to 
  35.      * terminate things with extreme prejudice: 
  36.      */  
  37.     /* 走到这里就说明异常确实是由于内核的程序设计缺陷导致的了,内核将 
  38.        产生一个oops,下面的工作就是打印CPU寄存器和内核态堆栈的信息到控制台并 
  39.        终结当前的进程*/  
  40.     flags = oops_begin();  
  41.   
  42.     show_fault_oops(regs, error_code, address);  
  43.   
  44.     stackend = end_of_stack(tsk);  
  45.     if (*stackend != STACK_END_MAGIC)  
  46.         printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");  
  47.   
  48.     tsk->thread.cr2      = address;  
  49.     tsk->thread.trap_no  = 14;  
  50.     tsk->thread.error_code   = error_code;  
  51.   
  52.     sig = SIGKILL;  
  53.     if (__die("Oops", regs, error_code))  
  54.         sig = 0;  
  55.   
  56.     /* Executive summary in case the body of the oops scrolled away */  
  57.     printk(KERN_EMERG "CR2: %016lx\n", address);  
  58.   
  59.     oops_end(flags, regs, sig);  
  60. }  
linux
相关资讯       Linux内存管理 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款