tlbflush_64.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * arch/sh/mm/tlb-flush_64.c
  3. *
  4. * Copyright (C) 2000, 2001 Paolo Alberelli
  5. * Copyright (C) 2003 Richard Curnow (/proc/tlb, bug fixes)
  6. * Copyright (C) 2003 - 2009 Paul Mundt
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/signal.h>
  13. #include <linux/rwsem.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/types.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/mman.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp.h>
  23. #include <linux/perf_event.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. extern void die(const char *,struct pt_regs *,long);
  32. #define PFLAG(val,flag) (( (val) & (flag) ) ? #flag : "" )
  33. #define PPROT(flag) PFLAG(pgprot_val(prot),flag)
  34. static inline void print_prots(pgprot_t prot)
  35. {
  36. printk("prot is 0x%016llx\n",pgprot_val(prot));
  37. printk("%s %s %s %s %s\n",PPROT(_PAGE_SHARED),PPROT(_PAGE_READ),
  38. PPROT(_PAGE_EXECUTE),PPROT(_PAGE_WRITE),PPROT(_PAGE_USER));
  39. }
  40. static inline void print_vma(struct vm_area_struct *vma)
  41. {
  42. printk("vma start 0x%08lx\n", vma->vm_start);
  43. printk("vma end 0x%08lx\n", vma->vm_end);
  44. print_prots(vma->vm_page_prot);
  45. printk("vm_flags 0x%08lx\n", vma->vm_flags);
  46. }
  47. static inline void print_task(struct task_struct *tsk)
  48. {
  49. printk("Task pid %d\n", task_pid_nr(tsk));
  50. }
  51. static pte_t *lookup_pte(struct mm_struct *mm, unsigned long address)
  52. {
  53. pgd_t *dir;
  54. pud_t *pud;
  55. pmd_t *pmd;
  56. pte_t *pte;
  57. pte_t entry;
  58. dir = pgd_offset(mm, address);
  59. if (pgd_none(*dir))
  60. return NULL;
  61. pud = pud_offset(dir, address);
  62. if (pud_none(*pud))
  63. return NULL;
  64. pmd = pmd_offset(pud, address);
  65. if (pmd_none(*pmd))
  66. return NULL;
  67. pte = pte_offset_kernel(pmd, address);
  68. entry = *pte;
  69. if (pte_none(entry) || !pte_present(entry))
  70. return NULL;
  71. return pte;
  72. }
  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. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long writeaccess,
  79. unsigned long textaccess, unsigned long address)
  80. {
  81. struct task_struct *tsk;
  82. struct mm_struct *mm;
  83. struct vm_area_struct * vma;
  84. const struct exception_table_entry *fixup;
  85. pte_t *pte;
  86. int fault;
  87. /* SIM
  88. * Note this is now called with interrupts still disabled
  89. * This is to cope with being called for a missing IO port
  90. * address with interrupts disabled. This should be fixed as
  91. * soon as we have a better 'fast path' miss handler.
  92. *
  93. * Plus take care how you try and debug this stuff.
  94. * For example, writing debug data to a port which you
  95. * have just faulted on is not going to work.
  96. */
  97. tsk = current;
  98. mm = tsk->mm;
  99. /* Not an IO address, so reenable interrupts */
  100. local_irq_enable();
  101. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
  102. /*
  103. * If we're in an interrupt or have no user
  104. * context, we must not take the fault..
  105. */
  106. if (in_atomic() || !mm)
  107. goto no_context;
  108. /* TLB misses upon some cache flushes get done under cli() */
  109. down_read(&mm->mmap_sem);
  110. vma = find_vma(mm, address);
  111. if (!vma) {
  112. #ifdef DEBUG_FAULT
  113. print_task(tsk);
  114. printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
  115. __func__, __LINE__,
  116. address,regs->pc,textaccess,writeaccess);
  117. show_regs(regs);
  118. #endif
  119. goto bad_area;
  120. }
  121. if (vma->vm_start <= address) {
  122. goto good_area;
  123. }
  124. if (!(vma->vm_flags & VM_GROWSDOWN)) {
  125. #ifdef DEBUG_FAULT
  126. print_task(tsk);
  127. printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
  128. __func__, __LINE__,
  129. address,regs->pc,textaccess,writeaccess);
  130. show_regs(regs);
  131. print_vma(vma);
  132. #endif
  133. goto bad_area;
  134. }
  135. if (expand_stack(vma, address)) {
  136. #ifdef DEBUG_FAULT
  137. print_task(tsk);
  138. printk("%s:%d fault, address is 0x%08x PC %016Lx textaccess %d writeaccess %d\n",
  139. __func__, __LINE__,
  140. address,regs->pc,textaccess,writeaccess);
  141. show_regs(regs);
  142. #endif
  143. goto bad_area;
  144. }
  145. /*
  146. * Ok, we have a good vm_area for this memory access, so
  147. * we can handle it..
  148. */
  149. good_area:
  150. if (textaccess) {
  151. if (!(vma->vm_flags & VM_EXEC))
  152. goto bad_area;
  153. } else {
  154. if (writeaccess) {
  155. if (!(vma->vm_flags & VM_WRITE))
  156. goto bad_area;
  157. } else {
  158. if (!(vma->vm_flags & VM_READ))
  159. goto bad_area;
  160. }
  161. }
  162. /*
  163. * If for any reason at all we couldn't handle the fault,
  164. * make sure we exit gracefully rather than endlessly redo
  165. * the fault.
  166. */
  167. fault = handle_mm_fault(mm, vma, address, writeaccess ? FAULT_FLAG_WRITE : 0);
  168. if (unlikely(fault & VM_FAULT_ERROR)) {
  169. if (fault & VM_FAULT_OOM)
  170. goto out_of_memory;
  171. else if (fault & VM_FAULT_SIGBUS)
  172. goto do_sigbus;
  173. BUG();
  174. }
  175. if (fault & VM_FAULT_MAJOR) {
  176. tsk->maj_flt++;
  177. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
  178. regs, address);
  179. } else {
  180. tsk->min_flt++;
  181. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
  182. regs, address);
  183. }
  184. /* If we get here, the page fault has been handled. Do the TLB refill
  185. now from the newly-setup PTE, to avoid having to fault again right
  186. away on the same instruction. */
  187. pte = lookup_pte (mm, address);
  188. if (!pte) {
  189. /* From empirical evidence, we can get here, due to
  190. !pte_present(pte). (e.g. if a swap-in occurs, and the page
  191. is swapped back out again before the process that wanted it
  192. gets rescheduled?) */
  193. goto no_pte;
  194. }
  195. __do_tlb_refill(address, textaccess, pte);
  196. no_pte:
  197. up_read(&mm->mmap_sem);
  198. return;
  199. /*
  200. * Something tried to access memory that isn't in our memory map..
  201. * Fix it, but check if it's kernel or user first..
  202. */
  203. bad_area:
  204. #ifdef DEBUG_FAULT
  205. printk("fault:bad area\n");
  206. #endif
  207. up_read(&mm->mmap_sem);
  208. if (user_mode(regs)) {
  209. static int count=0;
  210. siginfo_t info;
  211. if (count < 4) {
  212. /* This is really to help debug faults when starting
  213. * usermode, so only need a few */
  214. count++;
  215. printk("user mode bad_area address=%08lx pid=%d (%s) pc=%08lx\n",
  216. address, task_pid_nr(current), current->comm,
  217. (unsigned long) regs->pc);
  218. #if 0
  219. show_regs(regs);
  220. #endif
  221. }
  222. if (is_global_init(tsk)) {
  223. panic("INIT had user mode bad_area\n");
  224. }
  225. tsk->thread.address = address;
  226. tsk->thread.error_code = writeaccess;
  227. info.si_signo = SIGSEGV;
  228. info.si_errno = 0;
  229. info.si_addr = (void *) address;
  230. force_sig_info(SIGSEGV, &info, tsk);
  231. return;
  232. }
  233. no_context:
  234. #ifdef DEBUG_FAULT
  235. printk("fault:No context\n");
  236. #endif
  237. /* Are we prepared to handle this kernel fault? */
  238. fixup = search_exception_tables(regs->pc);
  239. if (fixup) {
  240. regs->pc = fixup->fixup;
  241. return;
  242. }
  243. /*
  244. * Oops. The kernel tried to access some bad page. We'll have to
  245. * terminate things with extreme prejudice.
  246. *
  247. */
  248. if (address < PAGE_SIZE)
  249. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  250. else
  251. printk(KERN_ALERT "Unable to handle kernel paging request");
  252. printk(" at virtual address %08lx\n", address);
  253. printk(KERN_ALERT "pc = %08Lx%08Lx\n", regs->pc >> 32, regs->pc & 0xffffffff);
  254. die("Oops", regs, writeaccess);
  255. do_exit(SIGKILL);
  256. /*
  257. * We ran out of memory, or some other thing happened to us that made
  258. * us unable to handle the page fault gracefully.
  259. */
  260. out_of_memory:
  261. up_read(&mm->mmap_sem);
  262. if (!user_mode(regs))
  263. goto no_context;
  264. pagefault_out_of_memory();
  265. return;
  266. do_sigbus:
  267. printk("fault:Do sigbus\n");
  268. up_read(&mm->mmap_sem);
  269. /*
  270. * Send a sigbus, regardless of whether we were in kernel
  271. * or user mode.
  272. */
  273. tsk->thread.address = address;
  274. tsk->thread.error_code = writeaccess;
  275. tsk->thread.trap_no = 14;
  276. force_sig(SIGBUS, tsk);
  277. /* Kernel mode? Handle exceptions or die */
  278. if (!user_mode(regs))
  279. goto no_context;
  280. }
  281. void local_flush_tlb_one(unsigned long asid, unsigned long page)
  282. {
  283. unsigned long long match, pteh=0, lpage;
  284. unsigned long tlb;
  285. /*
  286. * Sign-extend based on neff.
  287. */
  288. lpage = neff_sign_extend(page);
  289. match = (asid << PTEH_ASID_SHIFT) | PTEH_VALID;
  290. match |= lpage;
  291. for_each_itlb_entry(tlb) {
  292. asm volatile ("getcfg %1, 0, %0"
  293. : "=r" (pteh)
  294. : "r" (tlb) );
  295. if (pteh == match) {
  296. __flush_tlb_slot(tlb);
  297. break;
  298. }
  299. }
  300. for_each_dtlb_entry(tlb) {
  301. asm volatile ("getcfg %1, 0, %0"
  302. : "=r" (pteh)
  303. : "r" (tlb) );
  304. if (pteh == match) {
  305. __flush_tlb_slot(tlb);
  306. break;
  307. }
  308. }
  309. }
  310. void local_flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  311. {
  312. unsigned long flags;
  313. if (vma->vm_mm) {
  314. page &= PAGE_MASK;
  315. local_irq_save(flags);
  316. local_flush_tlb_one(get_asid(), page);
  317. local_irq_restore(flags);
  318. }
  319. }
  320. void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
  321. unsigned long end)
  322. {
  323. unsigned long flags;
  324. unsigned long long match, pteh=0, pteh_epn, pteh_low;
  325. unsigned long tlb;
  326. unsigned int cpu = smp_processor_id();
  327. struct mm_struct *mm;
  328. mm = vma->vm_mm;
  329. if (cpu_context(cpu, mm) == NO_CONTEXT)
  330. return;
  331. local_irq_save(flags);
  332. start &= PAGE_MASK;
  333. end &= PAGE_MASK;
  334. match = (cpu_asid(cpu, mm) << PTEH_ASID_SHIFT) | PTEH_VALID;
  335. /* Flush ITLB */
  336. for_each_itlb_entry(tlb) {
  337. asm volatile ("getcfg %1, 0, %0"
  338. : "=r" (pteh)
  339. : "r" (tlb) );
  340. pteh_epn = pteh & PAGE_MASK;
  341. pteh_low = pteh & ~PAGE_MASK;
  342. if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
  343. __flush_tlb_slot(tlb);
  344. }
  345. /* Flush DTLB */
  346. for_each_dtlb_entry(tlb) {
  347. asm volatile ("getcfg %1, 0, %0"
  348. : "=r" (pteh)
  349. : "r" (tlb) );
  350. pteh_epn = pteh & PAGE_MASK;
  351. pteh_low = pteh & ~PAGE_MASK;
  352. if (pteh_low == match && pteh_epn >= start && pteh_epn <= end)
  353. __flush_tlb_slot(tlb);
  354. }
  355. local_irq_restore(flags);
  356. }
  357. void local_flush_tlb_mm(struct mm_struct *mm)
  358. {
  359. unsigned long flags;
  360. unsigned int cpu = smp_processor_id();
  361. if (cpu_context(cpu, mm) == NO_CONTEXT)
  362. return;
  363. local_irq_save(flags);
  364. cpu_context(cpu, mm) = NO_CONTEXT;
  365. if (mm == current->mm)
  366. activate_context(mm, cpu);
  367. local_irq_restore(flags);
  368. }
  369. void local_flush_tlb_all(void)
  370. {
  371. /* Invalidate all, including shared pages, excluding fixed TLBs */
  372. unsigned long flags, tlb;
  373. local_irq_save(flags);
  374. /* Flush each ITLB entry */
  375. for_each_itlb_entry(tlb)
  376. __flush_tlb_slot(tlb);
  377. /* Flush each DTLB entry */
  378. for_each_dtlb_entry(tlb)
  379. __flush_tlb_slot(tlb);
  380. local_irq_restore(flags);
  381. }
  382. void local_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  383. {
  384. /* FIXME: Optimize this later.. */
  385. flush_tlb_all();
  386. }
  387. void __update_tlb(struct vm_area_struct *vma, unsigned long address, pte_t pte)
  388. {
  389. }