fault.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* $Id: fault.c,v 1.14 2004/01/13 05:52:11 kkojima Exp $
  2. *
  3. * linux/arch/sh/mm/fault.c
  4. * Copyright (C) 1999 Niibe Yutaka
  5. * Copyright (C) 2003 Paul Mundt
  6. *
  7. * Based on linux/arch/i386/mm/fault.c:
  8. * Copyright (C) 1995 Linus Torvalds
  9. */
  10. #include <linux/signal.h>
  11. #include <linux/sched.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/string.h>
  15. #include <linux/types.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/mman.h>
  18. #include <linux/mm.h>
  19. #include <linux/smp.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <asm/system.h>
  24. #include <asm/io.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/pgalloc.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/cacheflush.h>
  29. #include <asm/kgdb.h>
  30. extern void die(const char *,struct pt_regs *,long);
  31. /*
  32. * This routine handles page faults. It determines the address,
  33. * and the problem, and then passes it off to one of the appropriate
  34. * routines.
  35. */
  36. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
  37. unsigned long address)
  38. {
  39. struct task_struct *tsk;
  40. struct mm_struct *mm;
  41. struct vm_area_struct * vma;
  42. unsigned long page;
  43. #ifdef CONFIG_SH_KGDB
  44. if (kgdb_nofault && kgdb_bus_err_hook)
  45. kgdb_bus_err_hook();
  46. #endif
  47. tsk = current;
  48. mm = tsk->mm;
  49. /*
  50. * If we're in an interrupt or have no user
  51. * context, we must not take the fault..
  52. */
  53. if (in_atomic() || !mm)
  54. goto no_context;
  55. down_read(&mm->mmap_sem);
  56. vma = find_vma(mm, address);
  57. if (!vma)
  58. goto bad_area;
  59. if (vma->vm_start <= address)
  60. goto good_area;
  61. if (!(vma->vm_flags & VM_GROWSDOWN))
  62. goto bad_area;
  63. if (expand_stack(vma, address))
  64. goto bad_area;
  65. /*
  66. * Ok, we have a good vm_area for this memory access, so
  67. * we can handle it..
  68. */
  69. good_area:
  70. if (writeaccess) {
  71. if (!(vma->vm_flags & VM_WRITE))
  72. goto bad_area;
  73. } else {
  74. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  75. goto bad_area;
  76. }
  77. /*
  78. * If for any reason at all we couldn't handle the fault,
  79. * make sure we exit gracefully rather than endlessly redo
  80. * the fault.
  81. */
  82. survive:
  83. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  84. case VM_FAULT_MINOR:
  85. tsk->min_flt++;
  86. break;
  87. case VM_FAULT_MAJOR:
  88. tsk->maj_flt++;
  89. break;
  90. case VM_FAULT_SIGBUS:
  91. goto do_sigbus;
  92. case VM_FAULT_OOM:
  93. goto out_of_memory;
  94. default:
  95. BUG();
  96. }
  97. up_read(&mm->mmap_sem);
  98. return;
  99. /*
  100. * Something tried to access memory that isn't in our memory map..
  101. * Fix it, but check if it's kernel or user first..
  102. */
  103. bad_area:
  104. up_read(&mm->mmap_sem);
  105. if (user_mode(regs)) {
  106. tsk->thread.address = address;
  107. tsk->thread.error_code = writeaccess;
  108. force_sig(SIGSEGV, tsk);
  109. return;
  110. }
  111. no_context:
  112. /* Are we prepared to handle this kernel fault? */
  113. if (fixup_exception(regs))
  114. return;
  115. /*
  116. * Oops. The kernel tried to access some bad page. We'll have to
  117. * terminate things with extreme prejudice.
  118. *
  119. */
  120. if (address < PAGE_SIZE)
  121. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  122. else
  123. printk(KERN_ALERT "Unable to handle kernel paging request");
  124. printk(" at virtual address %08lx\n", address);
  125. printk(KERN_ALERT "pc = %08lx\n", regs->pc);
  126. asm volatile("mov.l %1, %0"
  127. : "=r" (page)
  128. : "m" (__m(MMU_TTB)));
  129. if (page) {
  130. page = ((unsigned long *) page)[address >> 22];
  131. printk(KERN_ALERT "*pde = %08lx\n", page);
  132. if (page & _PAGE_PRESENT) {
  133. page &= PAGE_MASK;
  134. address &= 0x003ff000;
  135. page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
  136. printk(KERN_ALERT "*pte = %08lx\n", page);
  137. }
  138. }
  139. die("Oops", regs, writeaccess);
  140. do_exit(SIGKILL);
  141. /*
  142. * We ran out of memory, or some other thing happened to us that made
  143. * us unable to handle the page fault gracefully.
  144. */
  145. out_of_memory:
  146. up_read(&mm->mmap_sem);
  147. if (current->pid == 1) {
  148. yield();
  149. down_read(&mm->mmap_sem);
  150. goto survive;
  151. }
  152. printk("VM: killing process %s\n", tsk->comm);
  153. if (user_mode(regs))
  154. do_exit(SIGKILL);
  155. goto no_context;
  156. do_sigbus:
  157. up_read(&mm->mmap_sem);
  158. /*
  159. * Send a sigbus, regardless of whether we were in kernel
  160. * or user mode.
  161. */
  162. tsk->thread.address = address;
  163. tsk->thread.error_code = writeaccess;
  164. tsk->thread.trap_no = 14;
  165. force_sig(SIGBUS, tsk);
  166. /* Kernel mode? Handle exceptions or die */
  167. if (!user_mode(regs))
  168. goto no_context;
  169. }
  170. /*
  171. * Called with interrupt disabled.
  172. */
  173. asmlinkage int __do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
  174. unsigned long address)
  175. {
  176. unsigned long addrmax = P4SEG;
  177. pgd_t *pgd;
  178. pmd_t *pmd;
  179. pte_t *pte;
  180. pte_t entry;
  181. struct mm_struct *mm;
  182. spinlock_t *ptl;
  183. int ret = 1;
  184. #ifdef CONFIG_SH_KGDB
  185. if (kgdb_nofault && kgdb_bus_err_hook)
  186. kgdb_bus_err_hook();
  187. #endif
  188. #ifdef CONFIG_SH_STORE_QUEUES
  189. addrmax = P4SEG_STORE_QUE + 0x04000000;
  190. #endif
  191. if (address >= P3SEG && address < addrmax) {
  192. pgd = pgd_offset_k(address);
  193. mm = NULL;
  194. } else if (address >= TASK_SIZE)
  195. return 1;
  196. else if (!(mm = current->mm))
  197. return 1;
  198. else
  199. pgd = pgd_offset(mm, address);
  200. pmd = pmd_offset(pgd, address);
  201. if (pmd_none_or_clear_bad(pmd))
  202. return 1;
  203. if (mm)
  204. pte = pte_offset_map_lock(mm, pmd, address, &ptl);
  205. else
  206. pte = pte_offset_kernel(pmd, address);
  207. entry = *pte;
  208. if (pte_none(entry) || pte_not_present(entry)
  209. || (writeaccess && !pte_write(entry)))
  210. goto unlock;
  211. if (writeaccess)
  212. entry = pte_mkdirty(entry);
  213. entry = pte_mkyoung(entry);
  214. #ifdef CONFIG_CPU_SH4
  215. /*
  216. * ITLB is not affected by "ldtlb" instruction.
  217. * So, we need to flush the entry by ourselves.
  218. */
  219. {
  220. unsigned long flags;
  221. local_irq_save(flags);
  222. __flush_tlb_page(get_asid(), address&PAGE_MASK);
  223. local_irq_restore(flags);
  224. }
  225. #endif
  226. set_pte(pte, entry);
  227. update_mmu_cache(NULL, address, entry);
  228. ret = 0;
  229. unlock:
  230. if (mm)
  231. pte_unmap_unlock(pte, ptl);
  232. return ret;
  233. }
  234. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  235. {
  236. if (vma->vm_mm && vma->vm_mm->context != NO_CONTEXT) {
  237. unsigned long flags;
  238. unsigned long asid;
  239. unsigned long saved_asid = MMU_NO_ASID;
  240. asid = vma->vm_mm->context & MMU_CONTEXT_ASID_MASK;
  241. page &= PAGE_MASK;
  242. local_irq_save(flags);
  243. if (vma->vm_mm != current->mm) {
  244. saved_asid = get_asid();
  245. set_asid(asid);
  246. }
  247. __flush_tlb_page(asid, page);
  248. if (saved_asid != MMU_NO_ASID)
  249. set_asid(saved_asid);
  250. local_irq_restore(flags);
  251. }
  252. }
  253. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  254. unsigned long end)
  255. {
  256. struct mm_struct *mm = vma->vm_mm;
  257. if (mm->context != NO_CONTEXT) {
  258. unsigned long flags;
  259. int size;
  260. local_irq_save(flags);
  261. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  262. if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
  263. mm->context = NO_CONTEXT;
  264. if (mm == current->mm)
  265. activate_context(mm);
  266. } else {
  267. unsigned long asid = mm->context&MMU_CONTEXT_ASID_MASK;
  268. unsigned long saved_asid = MMU_NO_ASID;
  269. start &= PAGE_MASK;
  270. end += (PAGE_SIZE - 1);
  271. end &= PAGE_MASK;
  272. if (mm != current->mm) {
  273. saved_asid = get_asid();
  274. set_asid(asid);
  275. }
  276. while (start < end) {
  277. __flush_tlb_page(asid, start);
  278. start += PAGE_SIZE;
  279. }
  280. if (saved_asid != MMU_NO_ASID)
  281. set_asid(saved_asid);
  282. }
  283. local_irq_restore(flags);
  284. }
  285. }
  286. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  287. {
  288. unsigned long flags;
  289. int size;
  290. local_irq_save(flags);
  291. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  292. if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
  293. flush_tlb_all();
  294. } else {
  295. unsigned long asid = init_mm.context&MMU_CONTEXT_ASID_MASK;
  296. unsigned long saved_asid = get_asid();
  297. start &= PAGE_MASK;
  298. end += (PAGE_SIZE - 1);
  299. end &= PAGE_MASK;
  300. set_asid(asid);
  301. while (start < end) {
  302. __flush_tlb_page(asid, start);
  303. start += PAGE_SIZE;
  304. }
  305. set_asid(saved_asid);
  306. }
  307. local_irq_restore(flags);
  308. }
  309. void flush_tlb_mm(struct mm_struct *mm)
  310. {
  311. /* Invalidate all TLB of this process. */
  312. /* Instead of invalidating each TLB, we get new MMU context. */
  313. if (mm->context != NO_CONTEXT) {
  314. unsigned long flags;
  315. local_irq_save(flags);
  316. mm->context = NO_CONTEXT;
  317. if (mm == current->mm)
  318. activate_context(mm);
  319. local_irq_restore(flags);
  320. }
  321. }
  322. void flush_tlb_all(void)
  323. {
  324. unsigned long flags, status;
  325. /*
  326. * Flush all the TLB.
  327. *
  328. * Write to the MMU control register's bit:
  329. * TF-bit for SH-3, TI-bit for SH-4.
  330. * It's same position, bit #2.
  331. */
  332. local_irq_save(flags);
  333. status = ctrl_inl(MMUCR);
  334. status |= 0x04;
  335. ctrl_outl(status, MMUCR);
  336. local_irq_restore(flags);
  337. }