fault.c 15 KB

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