fault-common.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:8k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/arch/arm/mm/fault-common.c
  3.  *
  4.  *  Copyright (C) 1995  Linus Torvalds
  5.  *  Modifications for ARM processor (c) 1995-2001 Russell King
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 2 as
  9.  * published by the Free Software Foundation.
  10.  */
  11. #include <linux/config.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/string.h>
  17. #include <linux/types.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/mman.h>
  20. #include <linux/mm.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/init.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/unaligned.h>
  28. #ifdef CONFIG_CPU_26
  29. #define FAULT_CODE_WRITE 0x02
  30. #define FAULT_CODE_FORCECOW 0x01
  31. #define DO_COW(m) ((m) & (FAULT_CODE_WRITE|FAULT_CODE_FORCECOW))
  32. #define READ_FAULT(m) (!((m) & FAULT_CODE_WRITE))
  33. #else
  34. /*
  35.  * On 32-bit processors, we define "mode" to be zero when reading,
  36.  * non-zero when writing.  This now ties up nicely with the polarity
  37.  * of the 26-bit machines, and also means that we avoid the horrible
  38.  * gcc code for "int val = !other_val;".
  39.  */
  40. #define DO_COW(m) (m)
  41. #define READ_FAULT(m) (!(m))
  42. #endif
  43. NORET_TYPE void die(const char *msg, struct pt_regs *regs, int err) ATTRIB_NORET;
  44. /*
  45.  * This is useful to dump out the page tables associated with
  46.  * 'addr' in mm 'mm'.
  47.  */
  48. void show_pte(struct mm_struct *mm, unsigned long addr)
  49. {
  50. pgd_t *pgd;
  51. if (!mm)
  52. mm = &init_mm;
  53. printk(KERN_ALERT "pgd = %pn", mm->pgd);
  54. pgd = pgd_offset(mm, addr);
  55. printk(KERN_ALERT "*pgd = %08lx", pgd_val(*pgd));
  56. do {
  57. pmd_t *pmd;
  58. pte_t *pte;
  59. if (pgd_none(*pgd))
  60. break;
  61. if (pgd_bad(*pgd)) {
  62. printk("(bad)");
  63. break;
  64. }
  65. pmd = pmd_offset(pgd, addr);
  66. printk(", *pmd = %08lx", pmd_val(*pmd));
  67. if (pmd_none(*pmd))
  68. break;
  69. if (pmd_bad(*pmd)) {
  70. printk("(bad)");
  71. break;
  72. }
  73. pte = pte_offset(pmd, addr);
  74. printk(", *pte = %08lx", pte_val(*pte));
  75. #ifdef CONFIG_CPU_32
  76. printk(", *ppte = %08lx", pte_val(pte[-PTRS_PER_PTE]));
  77. #endif
  78. } while(0);
  79. printk("n");
  80. }
  81. /*
  82.  * Oops.  The kernel tried to access some page that wasn't present.
  83.  */
  84. static void
  85. __do_kernel_fault(struct mm_struct *mm, unsigned long addr, int error_code,
  86.   struct pt_regs *regs)
  87. {
  88. unsigned long fixup;
  89. /*
  90.  * Are we prepared to handle this kernel fault?
  91.  */
  92. if ((fixup = search_exception_table(instruction_pointer(regs))) != 0) {
  93. #ifdef DEBUG
  94. printk(KERN_DEBUG "%s: Exception at [<%lx>] addr=%lx (fixup: %lx)n",
  95. current->comm, regs->ARM_pc, addr, fixup);
  96. #endif
  97. regs->ARM_pc = fixup;
  98. return;
  99. }
  100. /*
  101.  * No handler, we'll have to terminate things with extreme prejudice.
  102.  */
  103. printk(KERN_ALERT
  104. "Unable to handle kernel %s at virtual address %08lxn",
  105. (addr < PAGE_SIZE) ? "NULL pointer dereference" :
  106. "paging request", addr);
  107. show_pte(mm, addr);
  108. die("Oops", regs, error_code);
  109. do_exit(SIGKILL);
  110. }
  111. /*
  112.  * Something tried to access memory that isn't in our memory map..
  113.  * User mode accesses just cause a SIGSEGV
  114.  */
  115. static void
  116. __do_user_fault(struct task_struct *tsk, unsigned long addr, int error_code,
  117. int code, struct pt_regs *regs)
  118. {
  119. struct siginfo si;
  120. #ifdef CONFIG_DEBUG_USER
  121. printk(KERN_DEBUG "%s: unhandled page fault at pc=0x%08lx, "
  122.        "lr=0x%08lx (bad address=0x%08lx, code %d)n",
  123.        tsk->comm, regs->ARM_pc, regs->ARM_lr, addr, error_code);
  124. show_regs(regs);
  125. #endif
  126. tsk->thread.address = addr;
  127. tsk->thread.error_code = error_code;
  128. tsk->thread.trap_no = 14;
  129. si.si_signo = SIGSEGV;
  130. si.si_errno = 0;
  131. si.si_code = code;
  132. si.si_addr = (void *)addr;
  133. force_sig_info(SIGSEGV, &si, tsk);
  134. }
  135. void
  136. do_bad_area(struct task_struct *tsk, struct mm_struct *mm, unsigned long addr,
  137.     int error_code, struct pt_regs *regs)
  138. {
  139. /*
  140.  * If we are in kernel mode at this point, we
  141.  * have no context to handle this fault with.
  142.  */
  143. if (user_mode(regs))
  144. __do_user_fault(tsk, addr, error_code, SEGV_MAPERR, regs);
  145. else
  146. __do_kernel_fault(mm, addr, error_code, regs);
  147. }
  148. static int
  149. __do_page_fault(struct mm_struct *mm, unsigned long addr, int error_code,
  150. struct task_struct *tsk)
  151. {
  152. struct vm_area_struct *vma;
  153. int fault, mask;
  154. vma = find_vma(mm, addr);
  155. fault = -2; /* bad map area */
  156. if (!vma)
  157. goto out;
  158. if (vma->vm_start > addr)
  159. goto check_stack;
  160. /*
  161.  * Ok, we have a good vm_area for this
  162.  * memory access, so we can handle it.
  163.  */
  164. good_area:
  165. if (READ_FAULT(error_code)) /* read? */
  166. mask = VM_READ|VM_EXEC;
  167. else
  168. mask = VM_WRITE;
  169. fault = -1; /* bad access type */
  170. if (!(vma->vm_flags & mask))
  171. goto out;
  172. /*
  173.  * If for any reason at all we couldn't handle
  174.  * the fault, make sure we exit gracefully rather
  175.  * than endlessly redo the fault.
  176.  */
  177. survive:
  178. fault = handle_mm_fault(mm, vma, addr & PAGE_MASK, DO_COW(error_code));
  179. /*
  180.  * Handle the "normal" cases first - successful and sigbus
  181.  */
  182. switch (fault) {
  183. case 2:
  184. tsk->maj_flt++;
  185. return fault;
  186. case 1:
  187. tsk->min_flt++;
  188. case 0:
  189. return fault;
  190. }
  191. fault = -3; /* out of memory */
  192. if (tsk->pid != 1)
  193. goto out;
  194. /*
  195.  * If we are out of memory for pid1,
  196.  * sleep for a while and retry
  197.  */
  198. tsk->policy |= SCHED_YIELD;
  199. schedule();
  200. goto survive;
  201. check_stack:
  202. if (vma->vm_flags & VM_GROWSDOWN && !expand_stack(vma, addr))
  203. goto good_area;
  204. out:
  205. return fault;
  206. }
  207. int do_page_fault(unsigned long addr, int error_code, struct pt_regs *regs)
  208. {
  209. struct task_struct *tsk;
  210. struct mm_struct *mm;
  211. int fault;
  212. tsk = current;
  213. mm  = tsk->mm;
  214. /*
  215.  * If we're in an interrupt or have no user
  216.  * context, we must not take the fault..
  217.  */
  218. if (in_interrupt() || !mm)
  219. goto no_context;
  220. down_read(&mm->mmap_sem);
  221. fault = __do_page_fault(mm, addr, error_code, tsk);
  222. up_read(&mm->mmap_sem);
  223. /*
  224.  * Handle the "normal" case first
  225.  */
  226. if (fault > 0)
  227. return 0;
  228. /*
  229.  * We had some memory, but were unable to
  230.  * successfully fix up this page fault.
  231.  */
  232. if (fault == 0)
  233. goto do_sigbus;
  234. /*
  235.  * If we are in kernel mode at this point, we
  236.  * have no context to handle this fault with.
  237.  */
  238. if (!user_mode(regs))
  239. goto no_context;
  240. if (fault == -3) {
  241. /*
  242.  * We ran out of memory, or some other thing happened to
  243.  * us that made us unable to handle the page fault gracefully.
  244.  */
  245. printk("VM: killing process %sn", tsk->comm);
  246. do_exit(SIGKILL);
  247. } else
  248. __do_user_fault(tsk, addr, error_code, fault == -1 ?
  249. SEGV_ACCERR : SEGV_MAPERR, regs);
  250. return 0;
  251. /*
  252.  * We ran out of memory, or some other thing happened to us that made
  253.  * us unable to handle the page fault gracefully.
  254.  */
  255. do_sigbus:
  256. /*
  257.  * Send a sigbus, regardless of whether we were in kernel
  258.  * or user mode.
  259.  */
  260. tsk->thread.address = addr;
  261. tsk->thread.error_code = error_code;
  262. tsk->thread.trap_no = 14;
  263. force_sig(SIGBUS, tsk);
  264. #ifdef CONFIG_DEBUG_USER
  265. printk(KERN_DEBUG "%s: sigbus at 0x%08lx, pc=0x%08lxn",
  266. current->comm, addr, instruction_pointer(regs));
  267. #endif
  268. /* Kernel mode? Handle exceptions or die */
  269. if (user_mode(regs))
  270. return 0;
  271. no_context:
  272. __do_kernel_fault(mm, addr, error_code, regs);
  273. return 0;
  274. }
  275. /*
  276.  * First Level Translation Fault Handler
  277.  *
  278.  * We enter here because the first level page table doesn't contain
  279.  * a valid entry for the address.
  280.  *
  281.  * If the address is in kernel space (>= TASK_SIZE), then we are
  282.  * probably faulting in the vmalloc() area.
  283.  *
  284.  * If the init_task's first level page tables contains the relevant
  285.  * entry, we copy the it to this task.  If not, we send the process
  286.  * a signal, fixup the exception, or oops the kernel.
  287.  *
  288.  * NOTE! We MUST NOT take any locks for this case. We may be in an
  289.  * interrupt or a critical region, and should only copy the information
  290.  * from the master page table, nothing more.
  291.  */
  292. int do_translation_fault(unsigned long addr, int error_code, struct pt_regs *regs)
  293. {
  294. struct task_struct *tsk;
  295. struct mm_struct *mm;
  296. int offset;
  297. pgd_t *pgd, *pgd_k;
  298. pmd_t *pmd, *pmd_k;
  299. if (addr < TASK_SIZE)
  300. return do_page_fault(addr, error_code, regs);
  301. offset = __pgd_offset(addr);
  302. pgd = cpu_get_pgd() + offset;
  303. pgd_k = init_mm.pgd + offset;
  304. if (pgd_none(*pgd_k))
  305. goto bad_area;
  306. #if 0 /* note that we are two-level */
  307. if (!pgd_present(*pgd))
  308. set_pgd(pgd, *pgd_k);
  309. #endif
  310. pmd_k = pmd_offset(pgd_k, addr);
  311. pmd   = pmd_offset(pgd, addr);
  312. if (pmd_none(*pmd_k))
  313. goto bad_area;
  314. set_pmd(pmd, *pmd_k);
  315. return 0;
  316. bad_area:
  317. tsk = current;
  318. mm  = tsk->active_mm;
  319. do_bad_area(tsk, mm, addr, error_code, regs);
  320. return 0;
  321. }