tlbflush_64.c 11 KB

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