fault.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * arch/sh64/mm/fault.c
  7. *
  8. * Copyright (C) 2000, 2001 Paolo Alberelli
  9. * Copyright (C) 2003 Richard Curnow (/proc/tlb, bug fixes)
  10. * Copyright (C) 2003 Paul Mundt
  11. *
  12. */
  13. #include <linux/signal.h>
  14. #include <linux/rwsem.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/string.h>
  19. #include <linux/types.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/mman.h>
  22. #include <linux/mm.h>
  23. #include <linux/smp.h>
  24. #include <linux/smp_lock.h>
  25. #include <linux/interrupt.h>
  26. #include <asm/system.h>
  27. #include <asm/io.h>
  28. #include <asm/tlb.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgalloc.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/registers.h> /* required by inline asm statements */
  33. #if defined(CONFIG_SH64_PROC_TLB)
  34. #include <linux/init.h>
  35. #include <linux/proc_fs.h>
  36. /* Count numbers of tlb refills in each region */
  37. static unsigned long long calls_to_update_mmu_cache = 0ULL;
  38. static unsigned long long calls_to_flush_tlb_page = 0ULL;
  39. static unsigned long long calls_to_flush_tlb_range = 0ULL;
  40. static unsigned long long calls_to_flush_tlb_mm = 0ULL;
  41. static unsigned long long calls_to_flush_tlb_all = 0ULL;
  42. unsigned long long calls_to_do_slow_page_fault = 0ULL;
  43. unsigned long long calls_to_do_fast_page_fault = 0ULL;
  44. /* Count size of ranges for flush_tlb_range */
  45. static unsigned long long flush_tlb_range_1 = 0ULL;
  46. static unsigned long long flush_tlb_range_2 = 0ULL;
  47. static unsigned long long flush_tlb_range_3_4 = 0ULL;
  48. static unsigned long long flush_tlb_range_5_7 = 0ULL;
  49. static unsigned long long flush_tlb_range_8_11 = 0ULL;
  50. static unsigned long long flush_tlb_range_12_15 = 0ULL;
  51. static unsigned long long flush_tlb_range_16_up = 0ULL;
  52. static unsigned long long page_not_present = 0ULL;
  53. #endif
  54. extern void die(const char *,struct pt_regs *,long);
  55. #define PFLAG(val,flag) (( (val) & (flag) ) ? #flag : "" )
  56. #define PPROT(flag) PFLAG(pgprot_val(prot),flag)
  57. static inline void print_prots(pgprot_t prot)
  58. {
  59. printk("prot is 0x%08lx\n",pgprot_val(prot));
  60. printk("%s %s %s %s %s\n",PPROT(_PAGE_SHARED),PPROT(_PAGE_READ),
  61. PPROT(_PAGE_EXECUTE),PPROT(_PAGE_WRITE),PPROT(_PAGE_USER));
  62. }
  63. static inline void print_vma(struct vm_area_struct *vma)
  64. {
  65. printk("vma start 0x%08lx\n", vma->vm_start);
  66. printk("vma end 0x%08lx\n", vma->vm_end);
  67. print_prots(vma->vm_page_prot);
  68. printk("vm_flags 0x%08lx\n", vma->vm_flags);
  69. }
  70. static inline void print_task(struct task_struct *tsk)
  71. {
  72. printk("Task pid %d\n", tsk->pid);
  73. }
  74. static pte_t *lookup_pte(struct mm_struct *mm, unsigned long address)
  75. {
  76. pgd_t *dir;
  77. pmd_t *pmd;
  78. pte_t *pte;
  79. pte_t entry;
  80. dir = pgd_offset(mm, address);
  81. if (pgd_none(*dir)) {
  82. return NULL;
  83. }
  84. pmd = pmd_offset(dir, address);
  85. if (pmd_none(*pmd)) {
  86. return NULL;
  87. }
  88. pte = pte_offset_kernel(pmd, address);
  89. entry = *pte;
  90. if (pte_none(entry)) {
  91. return NULL;
  92. }
  93. if (!pte_present(entry)) {
  94. return NULL;
  95. }
  96. return pte;
  97. }
  98. /*
  99. * This routine handles page faults. It determines the address,
  100. * and the problem, and then passes it off to one of the appropriate
  101. * routines.
  102. */
  103. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
  104. unsigned long textaccess, unsigned long address)
  105. {
  106. struct task_struct *tsk;
  107. struct mm_struct *mm;
  108. struct vm_area_struct * vma;
  109. const struct exception_table_entry *fixup;
  110. pte_t *pte;
  111. #if defined(CONFIG_SH64_PROC_TLB)
  112. ++calls_to_do_slow_page_fault;
  113. #endif
  114. /* SIM
  115. * Note this is now called with interrupts still disabled
  116. * This is to cope with being called for a missing IO port
  117. * address with interupts disabled. This should be fixed as
  118. * soon as we have a better 'fast path' miss handler.
  119. *
  120. * Plus take care how you try and debug this stuff.
  121. * For example, writing debug data to a port which you
  122. * have just faulted on is not going to work.
  123. */
  124. tsk = current;
  125. mm = tsk->mm;
  126. /* Not an IO address, so reenable interrupts */
  127. local_irq_enable();
  128. /*
  129. * If we're in an interrupt or have no user
  130. * context, we must not take the fault..
  131. */
  132. if (in_interrupt() || !mm)
  133. goto no_context;
  134. /* TLB misses upon some cache flushes get done under cli() */
  135. down_read(&mm->mmap_sem);
  136. vma = find_vma(mm, address);
  137. if (!vma) {
  138. #ifdef DEBUG_FAULT
  139. print_task(tsk);
  140. printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
  141. __FUNCTION__,__LINE__,
  142. address,regs->pc,textaccess,writeaccess);
  143. show_regs(regs);
  144. #endif
  145. goto bad_area;
  146. }
  147. if (vma->vm_start <= address) {
  148. goto good_area;
  149. }
  150. if (!(vma->vm_flags & VM_GROWSDOWN)) {
  151. #ifdef DEBUG_FAULT
  152. print_task(tsk);
  153. printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
  154. __FUNCTION__,__LINE__,
  155. address,regs->pc,textaccess,writeaccess);
  156. show_regs(regs);
  157. print_vma(vma);
  158. #endif
  159. goto bad_area;
  160. }
  161. if (expand_stack(vma, address)) {
  162. #ifdef DEBUG_FAULT
  163. print_task(tsk);
  164. printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
  165. __FUNCTION__,__LINE__,
  166. address,regs->pc,textaccess,writeaccess);
  167. show_regs(regs);
  168. #endif
  169. goto bad_area;
  170. }
  171. /*
  172. * Ok, we have a good vm_area for this memory access, so
  173. * we can handle it..
  174. */
  175. good_area:
  176. if (textaccess) {
  177. if (!(vma->vm_flags & VM_EXEC))
  178. goto bad_area;
  179. } else {
  180. if (writeaccess) {
  181. if (!(vma->vm_flags & VM_WRITE))
  182. goto bad_area;
  183. } else {
  184. if (!(vma->vm_flags & VM_READ))
  185. goto bad_area;
  186. }
  187. }
  188. /*
  189. * If for any reason at all we couldn't handle the fault,
  190. * make sure we exit gracefully rather than endlessly redo
  191. * the fault.
  192. */
  193. survive:
  194. switch (handle_mm_fault(mm, vma, address, writeaccess)) {
  195. case VM_FAULT_MINOR:
  196. tsk->min_flt++;
  197. break;
  198. case VM_FAULT_MAJOR:
  199. tsk->maj_flt++;
  200. break;
  201. case VM_FAULT_SIGBUS:
  202. goto do_sigbus;
  203. default:
  204. goto out_of_memory;
  205. }
  206. /* If we get here, the page fault has been handled. Do the TLB refill
  207. now from the newly-setup PTE, to avoid having to fault again right
  208. away on the same instruction. */
  209. pte = lookup_pte (mm, address);
  210. if (!pte) {
  211. /* From empirical evidence, we can get here, due to
  212. !pte_present(pte). (e.g. if a swap-in occurs, and the page
  213. is swapped back out again before the process that wanted it
  214. gets rescheduled?) */
  215. goto no_pte;
  216. }
  217. __do_tlb_refill(address, textaccess, pte);
  218. no_pte:
  219. up_read(&mm->mmap_sem);
  220. return;
  221. /*
  222. * Something tried to access memory that isn't in our memory map..
  223. * Fix it, but check if it's kernel or user first..
  224. */
  225. bad_area:
  226. #ifdef DEBUG_FAULT
  227. printk("fault:bad area\n");
  228. #endif
  229. up_read(&mm->mmap_sem);
  230. if (user_mode(regs)) {
  231. static int count=0;
  232. siginfo_t info;
  233. if (count < 4) {
  234. /* This is really to help debug faults when starting
  235. * usermode, so only need a few */
  236. count++;
  237. printk("user mode bad_area address=%08lx pid=%d (%s) pc=%08lx\n",
  238. address, current->pid, current->comm,
  239. (unsigned long) regs->pc);
  240. #if 0
  241. show_regs(regs);
  242. #endif
  243. }
  244. if (tsk->pid == 1) {
  245. panic("INIT had user mode bad_area\n");
  246. }
  247. tsk->thread.address = address;
  248. tsk->thread.error_code = writeaccess;
  249. info.si_signo = SIGSEGV;
  250. info.si_errno = 0;
  251. info.si_addr = (void *) address;
  252. force_sig_info(SIGSEGV, &info, tsk);
  253. return;
  254. }
  255. no_context:
  256. #ifdef DEBUG_FAULT
  257. printk("fault:No context\n");
  258. #endif
  259. /* Are we prepared to handle this kernel fault? */
  260. fixup = search_exception_tables(regs->pc);
  261. if (fixup) {
  262. regs->pc = fixup->fixup;
  263. return;
  264. }
  265. /*
  266. * Oops. The kernel tried to access some bad page. We'll have to
  267. * terminate things with extreme prejudice.
  268. *
  269. */
  270. if (address < PAGE_SIZE)
  271. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  272. else
  273. printk(KERN_ALERT "Unable to handle kernel paging request");
  274. printk(" at virtual address %08lx\n", address);
  275. printk(KERN_ALERT "pc = %08Lx%08Lx\n", regs->pc >> 32, regs->pc & 0xffffffff);
  276. die("Oops", regs, writeaccess);
  277. do_exit(SIGKILL);
  278. /*
  279. * We ran out of memory, or some other thing happened to us that made
  280. * us unable to handle the page fault gracefully.
  281. */
  282. out_of_memory:
  283. if (current->pid == 1) {
  284. panic("INIT out of memory\n");
  285. yield();
  286. goto survive;
  287. }
  288. printk("fault:Out of memory\n");
  289. up_read(&mm->mmap_sem);
  290. if (current->pid == 1) {
  291. yield();
  292. down_read(&mm->mmap_sem);
  293. goto survive;
  294. }
  295. printk("VM: killing process %s\n", tsk->comm);
  296. if (user_mode(regs))
  297. do_exit(SIGKILL);
  298. goto no_context;
  299. do_sigbus:
  300. printk("fault:Do sigbus\n");
  301. up_read(&mm->mmap_sem);
  302. /*
  303. * Send a sigbus, regardless of whether we were in kernel
  304. * or user mode.
  305. */
  306. tsk->thread.address = address;
  307. tsk->thread.error_code = writeaccess;
  308. tsk->thread.trap_no = 14;
  309. force_sig(SIGBUS, tsk);
  310. /* Kernel mode? Handle exceptions or die */
  311. if (!user_mode(regs))
  312. goto no_context;
  313. }
  314. void flush_tlb_all(void);
  315. void update_mmu_cache(struct vm_area_struct * vma,
  316. unsigned long address, pte_t pte)
  317. {
  318. #if defined(CONFIG_SH64_PROC_TLB)
  319. ++calls_to_update_mmu_cache;
  320. #endif
  321. /*
  322. * This appears to get called once for every pte entry that gets
  323. * established => I don't think it's efficient to try refilling the
  324. * TLBs with the pages - some may not get accessed even. Also, for
  325. * executable pages, it is impossible to determine reliably here which
  326. * TLB they should be mapped into (or both even).
  327. *
  328. * So, just do nothing here and handle faults on demand. In the
  329. * TLBMISS handling case, the refill is now done anyway after the pte
  330. * has been fixed up, so that deals with most useful cases.
  331. */
  332. }
  333. static void __flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  334. {
  335. unsigned long long match, pteh=0, lpage;
  336. unsigned long tlb;
  337. struct mm_struct *mm;
  338. mm = vma->vm_mm;
  339. if (mm->context == NO_CONTEXT)
  340. return;
  341. /*
  342. * Sign-extend based on neff.
  343. */
  344. lpage = (page & NEFF_SIGN) ? (page | NEFF_MASK) : page;
  345. match = ((mm->context & MMU_CONTEXT_ASID_MASK) << PTEH_ASID_SHIFT) | PTEH_VALID;
  346. match |= lpage;
  347. /* Do ITLB : don't bother for pages in non-exectutable VMAs */
  348. if (vma->vm_flags & VM_EXEC) {
  349. for_each_itlb_entry(tlb) {
  350. asm volatile ("getcfg %1, 0, %0"
  351. : "=r" (pteh)
  352. : "r" (tlb) );
  353. if (pteh == match) {
  354. __flush_tlb_slot(tlb);
  355. break;
  356. }
  357. }
  358. }
  359. /* Do DTLB : any page could potentially be in here. */
  360. for_each_dtlb_entry(tlb) {
  361. asm volatile ("getcfg %1, 0, %0"
  362. : "=r" (pteh)
  363. : "r" (tlb) );
  364. if (pteh == match) {
  365. __flush_tlb_slot(tlb);
  366. break;
  367. }
  368. }
  369. }
  370. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  371. {
  372. unsigned long flags;
  373. #if defined(CONFIG_SH64_PROC_TLB)
  374. ++calls_to_flush_tlb_page;
  375. #endif
  376. if (vma->vm_mm) {
  377. page &= PAGE_MASK;
  378. local_irq_save(flags);
  379. __flush_tlb_page(vma, page);
  380. local_irq_restore(flags);
  381. }
  382. }
  383. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  384. unsigned long end)
  385. {
  386. unsigned long flags;
  387. unsigned long long match, pteh=0, pteh_epn, pteh_low;
  388. unsigned long tlb;
  389. struct mm_struct *mm;
  390. mm = vma->vm_mm;
  391. #if defined(CONFIG_SH64_PROC_TLB)
  392. ++calls_to_flush_tlb_range;
  393. {
  394. unsigned long size = (end - 1) - start;
  395. size >>= 12; /* divide by PAGE_SIZE */
  396. size++; /* end=start+4096 => 1 page */
  397. switch (size) {
  398. case 1 : flush_tlb_range_1++; break;
  399. case 2 : flush_tlb_range_2++; break;
  400. case 3 ... 4 : flush_tlb_range_3_4++; break;
  401. case 5 ... 7 : flush_tlb_range_5_7++; break;
  402. case 8 ... 11 : flush_tlb_range_8_11++; break;
  403. case 12 ... 15 : flush_tlb_range_12_15++; break;
  404. default : flush_tlb_range_16_up++; break;
  405. }
  406. }
  407. #endif
  408. if (mm->context == NO_CONTEXT)
  409. return;
  410. local_irq_save(flags);
  411. start &= PAGE_MASK;
  412. end &= PAGE_MASK;
  413. match = ((mm->context & MMU_CONTEXT_ASID_MASK) << PTEH_ASID_SHIFT) | PTEH_VALID;
  414. /* Flush ITLB */
  415. for_each_itlb_entry(tlb) {
  416. asm volatile ("getcfg %1, 0, %0"
  417. : "=r" (pteh)
  418. : "r" (tlb) );
  419. pteh_epn = pteh & PAGE_MASK;
  420. pteh_low = pteh & ~PAGE_MASK;
  421. if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
  422. __flush_tlb_slot(tlb);
  423. }
  424. /* Flush DTLB */
  425. for_each_dtlb_entry(tlb) {
  426. asm volatile ("getcfg %1, 0, %0"
  427. : "=r" (pteh)
  428. : "r" (tlb) );
  429. pteh_epn = pteh & PAGE_MASK;
  430. pteh_low = pteh & ~PAGE_MASK;
  431. if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
  432. __flush_tlb_slot(tlb);
  433. }
  434. local_irq_restore(flags);
  435. }
  436. void flush_tlb_mm(struct mm_struct *mm)
  437. {
  438. unsigned long flags;
  439. #if defined(CONFIG_SH64_PROC_TLB)
  440. ++calls_to_flush_tlb_mm;
  441. #endif
  442. if (mm->context == NO_CONTEXT)
  443. return;
  444. local_irq_save(flags);
  445. mm->context=NO_CONTEXT;
  446. if(mm==current->mm)
  447. activate_context(mm);
  448. local_irq_restore(flags);
  449. }
  450. void flush_tlb_all(void)
  451. {
  452. /* Invalidate all, including shared pages, excluding fixed TLBs */
  453. unsigned long flags, tlb;
  454. #if defined(CONFIG_SH64_PROC_TLB)
  455. ++calls_to_flush_tlb_all;
  456. #endif
  457. local_irq_save(flags);
  458. /* Flush each ITLB entry */
  459. for_each_itlb_entry(tlb) {
  460. __flush_tlb_slot(tlb);
  461. }
  462. /* Flush each DTLB entry */
  463. for_each_dtlb_entry(tlb) {
  464. __flush_tlb_slot(tlb);
  465. }
  466. local_irq_restore(flags);
  467. }
  468. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  469. {
  470. /* FIXME: Optimize this later.. */
  471. flush_tlb_all();
  472. }
  473. #if defined(CONFIG_SH64_PROC_TLB)
  474. /* Procfs interface to read the performance information */
  475. static int
  476. tlb_proc_info(char *buf, char **start, off_t fpos, int length, int *eof, void *data)
  477. {
  478. int len=0;
  479. len += sprintf(buf+len, "do_fast_page_fault called %12lld times\n", calls_to_do_fast_page_fault);
  480. len += sprintf(buf+len, "do_slow_page_fault called %12lld times\n", calls_to_do_slow_page_fault);
  481. len += sprintf(buf+len, "update_mmu_cache called %12lld times\n", calls_to_update_mmu_cache);
  482. len += sprintf(buf+len, "flush_tlb_page called %12lld times\n", calls_to_flush_tlb_page);
  483. len += sprintf(buf+len, "flush_tlb_range called %12lld times\n", calls_to_flush_tlb_range);
  484. len += sprintf(buf+len, "flush_tlb_mm called %12lld times\n", calls_to_flush_tlb_mm);
  485. len += sprintf(buf+len, "flush_tlb_all called %12lld times\n", calls_to_flush_tlb_all);
  486. len += sprintf(buf+len, "flush_tlb_range_sizes\n"
  487. " 1 : %12lld\n"
  488. " 2 : %12lld\n"
  489. " 3 - 4 : %12lld\n"
  490. " 5 - 7 : %12lld\n"
  491. " 8 - 11 : %12lld\n"
  492. "12 - 15 : %12lld\n"
  493. "16+ : %12lld\n",
  494. flush_tlb_range_1, flush_tlb_range_2, flush_tlb_range_3_4,
  495. flush_tlb_range_5_7, flush_tlb_range_8_11, flush_tlb_range_12_15,
  496. flush_tlb_range_16_up);
  497. len += sprintf(buf+len, "page not present %12lld times\n", page_not_present);
  498. *eof = 1;
  499. return len;
  500. }
  501. static int __init register_proc_tlb(void)
  502. {
  503. create_proc_read_entry("tlb", 0, NULL, tlb_proc_info, NULL);
  504. return 0;
  505. }
  506. __initcall(register_proc_tlb);
  507. #endif