fault.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 *dir;
  178. pmd_t *pmd;
  179. pte_t *pte;
  180. pte_t entry;
  181. #ifdef CONFIG_SH_KGDB
  182. if (kgdb_nofault && kgdb_bus_err_hook)
  183. kgdb_bus_err_hook();
  184. #endif
  185. #ifdef CONFIG_SH_STORE_QUEUES
  186. addrmax = P4SEG_STORE_QUE + 0x04000000;
  187. #endif
  188. if (address >= P3SEG && address < addrmax)
  189. dir = pgd_offset_k(address);
  190. else if (address >= TASK_SIZE)
  191. return 1;
  192. else if (!current->mm)
  193. return 1;
  194. else
  195. dir = pgd_offset(current->mm, address);
  196. pmd = pmd_offset(dir, address);
  197. if (pmd_none(*pmd))
  198. return 1;
  199. if (pmd_bad(*pmd)) {
  200. pmd_ERROR(*pmd);
  201. pmd_clear(pmd);
  202. return 1;
  203. }
  204. pte = pte_offset_kernel(pmd, address);
  205. entry = *pte;
  206. if (pte_none(entry) || pte_not_present(entry)
  207. || (writeaccess && !pte_write(entry)))
  208. return 1;
  209. if (writeaccess)
  210. entry = pte_mkdirty(entry);
  211. entry = pte_mkyoung(entry);
  212. #ifdef CONFIG_CPU_SH4
  213. /*
  214. * ITLB is not affected by "ldtlb" instruction.
  215. * So, we need to flush the entry by ourselves.
  216. */
  217. {
  218. unsigned long flags;
  219. local_irq_save(flags);
  220. __flush_tlb_page(get_asid(), address&PAGE_MASK);
  221. local_irq_restore(flags);
  222. }
  223. #endif
  224. set_pte(pte, entry);
  225. update_mmu_cache(NULL, address, entry);
  226. return 0;
  227. }
  228. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  229. {
  230. if (vma->vm_mm && vma->vm_mm->context != NO_CONTEXT) {
  231. unsigned long flags;
  232. unsigned long asid;
  233. unsigned long saved_asid = MMU_NO_ASID;
  234. asid = vma->vm_mm->context & MMU_CONTEXT_ASID_MASK;
  235. page &= PAGE_MASK;
  236. local_irq_save(flags);
  237. if (vma->vm_mm != current->mm) {
  238. saved_asid = get_asid();
  239. set_asid(asid);
  240. }
  241. __flush_tlb_page(asid, page);
  242. if (saved_asid != MMU_NO_ASID)
  243. set_asid(saved_asid);
  244. local_irq_restore(flags);
  245. }
  246. }
  247. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  248. unsigned long end)
  249. {
  250. struct mm_struct *mm = vma->vm_mm;
  251. if (mm->context != NO_CONTEXT) {
  252. unsigned long flags;
  253. int size;
  254. local_irq_save(flags);
  255. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  256. if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
  257. mm->context = NO_CONTEXT;
  258. if (mm == current->mm)
  259. activate_context(mm);
  260. } else {
  261. unsigned long asid = mm->context&MMU_CONTEXT_ASID_MASK;
  262. unsigned long saved_asid = MMU_NO_ASID;
  263. start &= PAGE_MASK;
  264. end += (PAGE_SIZE - 1);
  265. end &= PAGE_MASK;
  266. if (mm != current->mm) {
  267. saved_asid = get_asid();
  268. set_asid(asid);
  269. }
  270. while (start < end) {
  271. __flush_tlb_page(asid, start);
  272. start += PAGE_SIZE;
  273. }
  274. if (saved_asid != MMU_NO_ASID)
  275. set_asid(saved_asid);
  276. }
  277. local_irq_restore(flags);
  278. }
  279. }
  280. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  281. {
  282. unsigned long flags;
  283. int size;
  284. local_irq_save(flags);
  285. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  286. if (size > (MMU_NTLB_ENTRIES/4)) { /* Too many TLB to flush */
  287. flush_tlb_all();
  288. } else {
  289. unsigned long asid = init_mm.context&MMU_CONTEXT_ASID_MASK;
  290. unsigned long saved_asid = get_asid();
  291. start &= PAGE_MASK;
  292. end += (PAGE_SIZE - 1);
  293. end &= PAGE_MASK;
  294. set_asid(asid);
  295. while (start < end) {
  296. __flush_tlb_page(asid, start);
  297. start += PAGE_SIZE;
  298. }
  299. set_asid(saved_asid);
  300. }
  301. local_irq_restore(flags);
  302. }
  303. void flush_tlb_mm(struct mm_struct *mm)
  304. {
  305. /* Invalidate all TLB of this process. */
  306. /* Instead of invalidating each TLB, we get new MMU context. */
  307. if (mm->context != NO_CONTEXT) {
  308. unsigned long flags;
  309. local_irq_save(flags);
  310. mm->context = NO_CONTEXT;
  311. if (mm == current->mm)
  312. activate_context(mm);
  313. local_irq_restore(flags);
  314. }
  315. }
  316. void flush_tlb_all(void)
  317. {
  318. unsigned long flags, status;
  319. /*
  320. * Flush all the TLB.
  321. *
  322. * Write to the MMU control register's bit:
  323. * TF-bit for SH-3, TI-bit for SH-4.
  324. * It's same position, bit #2.
  325. */
  326. local_irq_save(flags);
  327. status = ctrl_inl(MMUCR);
  328. status |= 0x04;
  329. ctrl_outl(status, MMUCR);
  330. local_irq_restore(flags);
  331. }