fault.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * linux/arch/m32r/mm/fault.c
  3. *
  4. * Copyright (c) 2001, 2002 Hitoshi Yamamoto, and H. Kondo
  5. * Copyright (c) 2004 Naoto Sugai, NIIBE Yutaka
  6. *
  7. * Some code taken from i386 version.
  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/interrupt.h>
  21. #include <linux/init.h>
  22. #include <linux/tty.h>
  23. #include <linux/vt_kern.h> /* For unblank_screen() */
  24. #include <linux/highmem.h>
  25. #include <linux/module.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/m32r.h>
  28. #include <asm/hardirq.h>
  29. #include <asm/mmu_context.h>
  30. #include <asm/tlbflush.h>
  31. extern void die(const char *, struct pt_regs *, long);
  32. #ifndef CONFIG_SMP
  33. asmlinkage unsigned int tlb_entry_i_dat;
  34. asmlinkage unsigned int tlb_entry_d_dat;
  35. #define tlb_entry_i tlb_entry_i_dat
  36. #define tlb_entry_d tlb_entry_d_dat
  37. #else
  38. unsigned int tlb_entry_i_dat[NR_CPUS];
  39. unsigned int tlb_entry_d_dat[NR_CPUS];
  40. #define tlb_entry_i tlb_entry_i_dat[smp_processor_id()]
  41. #define tlb_entry_d tlb_entry_d_dat[smp_processor_id()]
  42. #endif
  43. extern void init_tlb(void);
  44. /*======================================================================*
  45. * do_page_fault()
  46. *======================================================================*
  47. * This routine handles page faults. It determines the address,
  48. * and the problem, and then passes it off to one of the appropriate
  49. * routines.
  50. *
  51. * ARGUMENT:
  52. * regs : M32R SP reg.
  53. * error_code : See below
  54. * address : M32R MMU MDEVA reg. (Operand ACE)
  55. * : M32R BPC reg. (Instruction ACE)
  56. *
  57. * error_code :
  58. * bit 0 == 0 means no page found, 1 means protection fault
  59. * bit 1 == 0 means read, 1 means write
  60. * bit 2 == 0 means kernel, 1 means user-mode
  61. * bit 3 == 0 means data, 1 means instruction
  62. *======================================================================*/
  63. #define ACE_PROTECTION 1
  64. #define ACE_WRITE 2
  65. #define ACE_USERMODE 4
  66. #define ACE_INSTRUCTION 8
  67. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code,
  68. unsigned long address)
  69. {
  70. struct task_struct *tsk;
  71. struct mm_struct *mm;
  72. struct vm_area_struct * vma;
  73. unsigned long page, addr;
  74. unsigned long flags = 0;
  75. int fault;
  76. siginfo_t info;
  77. /*
  78. * If BPSW IE bit enable --> set PSW IE bit
  79. */
  80. if (regs->psw & M32R_PSW_BIE)
  81. local_irq_enable();
  82. tsk = current;
  83. info.si_code = SEGV_MAPERR;
  84. /*
  85. * We fault-in kernel-space virtual memory on-demand. The
  86. * 'reference' page table is init_mm.pgd.
  87. *
  88. * NOTE! We MUST NOT take any locks for this case. We may
  89. * be in an interrupt or a critical region, and should
  90. * only copy the information from the master page table,
  91. * nothing more.
  92. *
  93. * This verifies that the fault happens in kernel space
  94. * (error_code & ACE_USERMODE) == 0, and that the fault was not a
  95. * protection error (error_code & ACE_PROTECTION) == 0.
  96. */
  97. if (address >= TASK_SIZE && !(error_code & ACE_USERMODE))
  98. goto vmalloc_fault;
  99. mm = tsk->mm;
  100. /*
  101. * If we're in an interrupt or have no user context or have pagefaults
  102. * disabled then we must not take the fault.
  103. */
  104. if (faulthandler_disabled() || !mm)
  105. goto bad_area_nosemaphore;
  106. if (error_code & ACE_USERMODE)
  107. flags |= FAULT_FLAG_USER;
  108. /* When running in the kernel we expect faults to occur only to
  109. * addresses in user space. All other faults represent errors in the
  110. * kernel and should generate an OOPS. Unfortunately, in the case of an
  111. * erroneous fault occurring in a code path which already holds mmap_sem
  112. * we will deadlock attempting to validate the fault against the
  113. * address space. Luckily the kernel only validly references user
  114. * space from well defined areas of code, which are listed in the
  115. * exceptions table.
  116. *
  117. * As the vast majority of faults will be valid we will only perform
  118. * the source reference check when there is a possibility of a deadlock.
  119. * Attempt to lock the address space, if we cannot we then validate the
  120. * source. If this is invalid we can skip the address space check,
  121. * thus avoiding the deadlock.
  122. */
  123. if (!down_read_trylock(&mm->mmap_sem)) {
  124. if ((error_code & ACE_USERMODE) == 0 &&
  125. !search_exception_tables(regs->psw))
  126. goto bad_area_nosemaphore;
  127. down_read(&mm->mmap_sem);
  128. }
  129. vma = find_vma(mm, address);
  130. if (!vma)
  131. goto bad_area;
  132. if (vma->vm_start <= address)
  133. goto good_area;
  134. if (!(vma->vm_flags & VM_GROWSDOWN))
  135. goto bad_area;
  136. if (error_code & ACE_USERMODE) {
  137. /*
  138. * accessing the stack below "spu" is always a bug.
  139. * The "+ 4" is there due to the push instruction
  140. * doing pre-decrement on the stack and that
  141. * doesn't show up until later..
  142. */
  143. if (address + 4 < regs->spu)
  144. goto bad_area;
  145. }
  146. if (expand_stack(vma, address))
  147. goto bad_area;
  148. /*
  149. * Ok, we have a good vm_area for this memory access, so
  150. * we can handle it..
  151. */
  152. good_area:
  153. info.si_code = SEGV_ACCERR;
  154. switch (error_code & (ACE_WRITE|ACE_PROTECTION)) {
  155. default: /* 3: write, present */
  156. /* fall through */
  157. case ACE_WRITE: /* write, not present */
  158. if (!(vma->vm_flags & VM_WRITE))
  159. goto bad_area;
  160. flags |= FAULT_FLAG_WRITE;
  161. break;
  162. case ACE_PROTECTION: /* read, present */
  163. case 0: /* read, not present */
  164. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  165. goto bad_area;
  166. }
  167. /*
  168. * For instruction access exception, check if the area is executable
  169. */
  170. if ((error_code & ACE_INSTRUCTION) && !(vma->vm_flags & VM_EXEC))
  171. goto bad_area;
  172. /*
  173. * If for any reason at all we couldn't handle the fault,
  174. * make sure we exit gracefully rather than endlessly redo
  175. * the fault.
  176. */
  177. addr = (address & PAGE_MASK);
  178. set_thread_fault_code(error_code);
  179. fault = handle_mm_fault(vma, addr, flags);
  180. if (unlikely(fault & VM_FAULT_ERROR)) {
  181. if (fault & VM_FAULT_OOM)
  182. goto out_of_memory;
  183. else if (fault & VM_FAULT_SIGSEGV)
  184. goto bad_area;
  185. else if (fault & VM_FAULT_SIGBUS)
  186. goto do_sigbus;
  187. BUG();
  188. }
  189. if (fault & VM_FAULT_MAJOR)
  190. tsk->maj_flt++;
  191. else
  192. tsk->min_flt++;
  193. set_thread_fault_code(0);
  194. up_read(&mm->mmap_sem);
  195. return;
  196. /*
  197. * Something tried to access memory that isn't in our memory map..
  198. * Fix it, but check if it's kernel or user first..
  199. */
  200. bad_area:
  201. up_read(&mm->mmap_sem);
  202. bad_area_nosemaphore:
  203. /* User mode accesses just cause a SIGSEGV */
  204. if (error_code & ACE_USERMODE) {
  205. tsk->thread.address = address;
  206. tsk->thread.error_code = error_code | (address >= TASK_SIZE);
  207. tsk->thread.trap_no = 14;
  208. info.si_signo = SIGSEGV;
  209. info.si_errno = 0;
  210. /* info.si_code has been set above */
  211. info.si_addr = (void __user *)address;
  212. force_sig_info(SIGSEGV, &info, tsk);
  213. return;
  214. }
  215. no_context:
  216. /* Are we prepared to handle this kernel fault? */
  217. if (fixup_exception(regs))
  218. return;
  219. /*
  220. * Oops. The kernel tried to access some bad page. We'll have to
  221. * terminate things with extreme prejudice.
  222. */
  223. bust_spinlocks(1);
  224. if (address < PAGE_SIZE)
  225. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  226. else
  227. printk(KERN_ALERT "Unable to handle kernel paging request");
  228. printk(" at virtual address %08lx\n",address);
  229. printk(KERN_ALERT " printing bpc:\n");
  230. printk("%08lx\n", regs->bpc);
  231. page = *(unsigned long *)MPTB;
  232. page = ((unsigned long *) page)[address >> PGDIR_SHIFT];
  233. printk(KERN_ALERT "*pde = %08lx\n", page);
  234. if (page & _PAGE_PRESENT) {
  235. page &= PAGE_MASK;
  236. address &= 0x003ff000;
  237. page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
  238. printk(KERN_ALERT "*pte = %08lx\n", page);
  239. }
  240. die("Oops", regs, error_code);
  241. bust_spinlocks(0);
  242. do_exit(SIGKILL);
  243. /*
  244. * We ran out of memory, or some other thing happened to us that made
  245. * us unable to handle the page fault gracefully.
  246. */
  247. out_of_memory:
  248. up_read(&mm->mmap_sem);
  249. if (!(error_code & ACE_USERMODE))
  250. goto no_context;
  251. pagefault_out_of_memory();
  252. return;
  253. do_sigbus:
  254. up_read(&mm->mmap_sem);
  255. /* Kernel mode? Handle exception or die */
  256. if (!(error_code & ACE_USERMODE))
  257. goto no_context;
  258. tsk->thread.address = address;
  259. tsk->thread.error_code = error_code;
  260. tsk->thread.trap_no = 14;
  261. info.si_signo = SIGBUS;
  262. info.si_errno = 0;
  263. info.si_code = BUS_ADRERR;
  264. info.si_addr = (void __user *)address;
  265. force_sig_info(SIGBUS, &info, tsk);
  266. return;
  267. vmalloc_fault:
  268. {
  269. /*
  270. * Synchronize this task's top level page-table
  271. * with the 'reference' page table.
  272. *
  273. * Do _not_ use "tsk" here. We might be inside
  274. * an interrupt in the middle of a task switch..
  275. */
  276. int offset = pgd_index(address);
  277. pgd_t *pgd, *pgd_k;
  278. pmd_t *pmd, *pmd_k;
  279. pte_t *pte_k;
  280. pgd = (pgd_t *)*(unsigned long *)MPTB;
  281. pgd = offset + (pgd_t *)pgd;
  282. pgd_k = init_mm.pgd + offset;
  283. if (!pgd_present(*pgd_k))
  284. goto no_context;
  285. /*
  286. * set_pgd(pgd, *pgd_k); here would be useless on PAE
  287. * and redundant with the set_pmd() on non-PAE.
  288. */
  289. pmd = pmd_offset(pgd, address);
  290. pmd_k = pmd_offset(pgd_k, address);
  291. if (!pmd_present(*pmd_k))
  292. goto no_context;
  293. set_pmd(pmd, *pmd_k);
  294. pte_k = pte_offset_kernel(pmd_k, address);
  295. if (!pte_present(*pte_k))
  296. goto no_context;
  297. addr = (address & PAGE_MASK);
  298. set_thread_fault_code(error_code);
  299. update_mmu_cache(NULL, addr, pte_k);
  300. set_thread_fault_code(0);
  301. return;
  302. }
  303. }
  304. /*======================================================================*
  305. * update_mmu_cache()
  306. *======================================================================*/
  307. #define TLB_MASK (NR_TLB_ENTRIES - 1)
  308. #define ITLB_END (unsigned long *)(ITLB_BASE + (NR_TLB_ENTRIES * 8))
  309. #define DTLB_END (unsigned long *)(DTLB_BASE + (NR_TLB_ENTRIES * 8))
  310. void update_mmu_cache(struct vm_area_struct *vma, unsigned long vaddr,
  311. pte_t *ptep)
  312. {
  313. volatile unsigned long *entry1, *entry2;
  314. unsigned long pte_data, flags;
  315. unsigned int *entry_dat;
  316. int inst = get_thread_fault_code() & ACE_INSTRUCTION;
  317. int i;
  318. /* Ptrace may call this routine. */
  319. if (vma && current->active_mm != vma->vm_mm)
  320. return;
  321. local_irq_save(flags);
  322. vaddr = (vaddr & PAGE_MASK) | get_asid();
  323. pte_data = pte_val(*ptep);
  324. #ifdef CONFIG_CHIP_OPSP
  325. entry1 = (unsigned long *)ITLB_BASE;
  326. for (i = 0; i < NR_TLB_ENTRIES; i++) {
  327. if (*entry1++ == vaddr) {
  328. set_tlb_data(entry1, pte_data);
  329. break;
  330. }
  331. entry1++;
  332. }
  333. entry2 = (unsigned long *)DTLB_BASE;
  334. for (i = 0; i < NR_TLB_ENTRIES; i++) {
  335. if (*entry2++ == vaddr) {
  336. set_tlb_data(entry2, pte_data);
  337. break;
  338. }
  339. entry2++;
  340. }
  341. #else
  342. /*
  343. * Update TLB entries
  344. * entry1: ITLB entry address
  345. * entry2: DTLB entry address
  346. */
  347. __asm__ __volatile__ (
  348. "seth %0, #high(%4) \n\t"
  349. "st %2, @(%5, %0) \n\t"
  350. "ldi %1, #1 \n\t"
  351. "st %1, @(%6, %0) \n\t"
  352. "add3 r4, %0, %7 \n\t"
  353. ".fillinsn \n"
  354. "1: \n\t"
  355. "ld %1, @(%6, %0) \n\t"
  356. "bnez %1, 1b \n\t"
  357. "ld %0, @r4+ \n\t"
  358. "ld %1, @r4 \n\t"
  359. "st %3, @+%0 \n\t"
  360. "st %3, @+%1 \n\t"
  361. : "=&r" (entry1), "=&r" (entry2)
  362. : "r" (vaddr), "r" (pte_data), "i" (MMU_REG_BASE),
  363. "i" (MSVA_offset), "i" (MTOP_offset), "i" (MIDXI_offset)
  364. : "r4", "memory"
  365. );
  366. #endif
  367. if ((!inst && entry2 >= DTLB_END) || (inst && entry1 >= ITLB_END))
  368. goto notfound;
  369. found:
  370. local_irq_restore(flags);
  371. return;
  372. /* Valid entry not found */
  373. notfound:
  374. /*
  375. * Update ITLB or DTLB entry
  376. * entry1: TLB entry address
  377. * entry2: TLB base address
  378. */
  379. if (!inst) {
  380. entry2 = (unsigned long *)DTLB_BASE;
  381. entry_dat = &tlb_entry_d;
  382. } else {
  383. entry2 = (unsigned long *)ITLB_BASE;
  384. entry_dat = &tlb_entry_i;
  385. }
  386. entry1 = entry2 + (((*entry_dat - 1) & TLB_MASK) << 1);
  387. for (i = 0 ; i < NR_TLB_ENTRIES ; i++) {
  388. if (!(entry1[1] & 2)) /* Valid bit check */
  389. break;
  390. if (entry1 != entry2)
  391. entry1 -= 2;
  392. else
  393. entry1 += TLB_MASK << 1;
  394. }
  395. if (i >= NR_TLB_ENTRIES) { /* Empty entry not found */
  396. entry1 = entry2 + (*entry_dat << 1);
  397. *entry_dat = (*entry_dat + 1) & TLB_MASK;
  398. }
  399. *entry1++ = vaddr; /* Set TLB tag */
  400. set_tlb_data(entry1, pte_data);
  401. goto found;
  402. }
  403. /*======================================================================*
  404. * flush_tlb_page() : flushes one page
  405. *======================================================================*/
  406. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  407. {
  408. if (vma->vm_mm && mm_context(vma->vm_mm) != NO_CONTEXT) {
  409. unsigned long flags;
  410. local_irq_save(flags);
  411. page &= PAGE_MASK;
  412. page |= (mm_context(vma->vm_mm) & MMU_CONTEXT_ASID_MASK);
  413. __flush_tlb_page(page);
  414. local_irq_restore(flags);
  415. }
  416. }
  417. /*======================================================================*
  418. * flush_tlb_range() : flushes a range of pages
  419. *======================================================================*/
  420. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  421. unsigned long end)
  422. {
  423. struct mm_struct *mm;
  424. mm = vma->vm_mm;
  425. if (mm_context(mm) != NO_CONTEXT) {
  426. unsigned long flags;
  427. int size;
  428. local_irq_save(flags);
  429. size = (end - start + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  430. if (size > (NR_TLB_ENTRIES / 4)) { /* Too many TLB to flush */
  431. mm_context(mm) = NO_CONTEXT;
  432. if (mm == current->mm)
  433. activate_context(mm);
  434. } else {
  435. unsigned long asid;
  436. asid = mm_context(mm) & MMU_CONTEXT_ASID_MASK;
  437. start &= PAGE_MASK;
  438. end += (PAGE_SIZE - 1);
  439. end &= PAGE_MASK;
  440. start |= asid;
  441. end |= asid;
  442. while (start < end) {
  443. __flush_tlb_page(start);
  444. start += PAGE_SIZE;
  445. }
  446. }
  447. local_irq_restore(flags);
  448. }
  449. }
  450. /*======================================================================*
  451. * flush_tlb_mm() : flushes the specified mm context TLB's
  452. *======================================================================*/
  453. void local_flush_tlb_mm(struct mm_struct *mm)
  454. {
  455. /* Invalidate all TLB of this process. */
  456. /* Instead of invalidating each TLB, we get new MMU context. */
  457. if (mm_context(mm) != NO_CONTEXT) {
  458. unsigned long flags;
  459. local_irq_save(flags);
  460. mm_context(mm) = NO_CONTEXT;
  461. if (mm == current->mm)
  462. activate_context(mm);
  463. local_irq_restore(flags);
  464. }
  465. }
  466. /*======================================================================*
  467. * flush_tlb_all() : flushes all processes TLBs
  468. *======================================================================*/
  469. void local_flush_tlb_all(void)
  470. {
  471. unsigned long flags;
  472. local_irq_save(flags);
  473. __flush_tlb_all();
  474. local_irq_restore(flags);
  475. }
  476. /*======================================================================*
  477. * init_mmu()
  478. *======================================================================*/
  479. void __init init_mmu(void)
  480. {
  481. tlb_entry_i = 0;
  482. tlb_entry_d = 0;
  483. mmu_context_cache = MMU_CONTEXT_FIRST_VERSION;
  484. set_asid(mmu_context_cache & MMU_CONTEXT_ASID_MASK);
  485. *(volatile unsigned long *)MPTB = (unsigned long)swapper_pg_dir;
  486. }