fault.c 15 KB

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