fault.c 14 KB

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