tlbmiss.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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/tlbmiss.c
  7. *
  8. * Original code from fault.c
  9. * Copyright (C) 2000, 2001 Paolo Alberelli
  10. *
  11. * Fast PTE->TLB refill path
  12. * Copyright (C) 2003 Richard.Curnow@superh.com
  13. *
  14. * IMPORTANT NOTES :
  15. * The do_fast_page_fault function is called from a context in entry.S where very few registers
  16. * have been saved. In particular, the code in this file must be compiled not to use ANY
  17. * caller-save regiseters that are not part of the restricted save set. Also, it means that
  18. * code in this file must not make calls to functions elsewhere in the kernel, or else the
  19. * excepting context will see corruption in its caller-save registers. Plus, the entry.S save
  20. * area is non-reentrant, so this code has to run with SR.BL==1, i.e. no interrupts taken inside
  21. * it and panic on any exception.
  22. *
  23. */
  24. #include <linux/signal.h>
  25. #include <linux/sched.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/string.h>
  29. #include <linux/types.h>
  30. #include <linux/ptrace.h>
  31. #include <linux/mman.h>
  32. #include <linux/mm.h>
  33. #include <linux/smp.h>
  34. #include <linux/smp_lock.h>
  35. #include <linux/interrupt.h>
  36. #include <asm/system.h>
  37. #include <asm/tlb.h>
  38. #include <asm/io.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/pgalloc.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/registers.h> /* required by inline asm statements */
  43. /* Callable from fault.c, so not static */
  44. inline void __do_tlb_refill(unsigned long address,
  45. unsigned long long is_text_not_data, pte_t *pte)
  46. {
  47. unsigned long long ptel;
  48. unsigned long long pteh=0;
  49. struct tlb_info *tlbp;
  50. unsigned long long next;
  51. /* Get PTEL first */
  52. ptel = pte_val(*pte);
  53. /*
  54. * Set PTEH register
  55. */
  56. pteh = address & MMU_VPN_MASK;
  57. /* Sign extend based on neff. */
  58. #if (NEFF == 32)
  59. /* Faster sign extension */
  60. pteh = (unsigned long long)(signed long long)(signed long)pteh;
  61. #else
  62. /* General case */
  63. pteh = (pteh & NEFF_SIGN) ? (pteh | NEFF_MASK) : pteh;
  64. #endif
  65. /* Set the ASID. */
  66. pteh |= get_asid() << PTEH_ASID_SHIFT;
  67. pteh |= PTEH_VALID;
  68. /* Set PTEL register, set_pte has performed the sign extension */
  69. ptel &= _PAGE_FLAGS_HARDWARE_MASK; /* drop software flags */
  70. tlbp = is_text_not_data ? &(cpu_data->itlb) : &(cpu_data->dtlb);
  71. next = tlbp->next;
  72. __flush_tlb_slot(next);
  73. asm volatile ("putcfg %0,1,%2\n\n\t"
  74. "putcfg %0,0,%1\n"
  75. : : "r" (next), "r" (pteh), "r" (ptel) );
  76. next += TLB_STEP;
  77. if (next > tlbp->last) next = tlbp->first;
  78. tlbp->next = next;
  79. }
  80. static int handle_vmalloc_fault(struct mm_struct *mm, unsigned long protection_flags,
  81. unsigned long long textaccess,
  82. unsigned long address)
  83. {
  84. pgd_t *dir;
  85. pmd_t *pmd;
  86. static pte_t *pte;
  87. pte_t entry;
  88. dir = pgd_offset_k(address);
  89. pmd = pmd_offset(dir, address);
  90. if (pmd_none(*pmd)) {
  91. return 0;
  92. }
  93. if (pmd_bad(*pmd)) {
  94. pmd_clear(pmd);
  95. return 0;
  96. }
  97. pte = pte_offset_kernel(pmd, address);
  98. entry = *pte;
  99. if (pte_none(entry) || !pte_present(entry)) {
  100. return 0;
  101. }
  102. if ((pte_val(entry) & protection_flags) != protection_flags) {
  103. return 0;
  104. }
  105. __do_tlb_refill(address, textaccess, pte);
  106. return 1;
  107. }
  108. static int handle_tlbmiss(struct mm_struct *mm, unsigned long long protection_flags,
  109. unsigned long long textaccess,
  110. unsigned long address)
  111. {
  112. pgd_t *dir;
  113. pmd_t *pmd;
  114. pte_t *pte;
  115. pte_t entry;
  116. /* NB. The PGD currently only contains a single entry - there is no
  117. page table tree stored for the top half of the address space since
  118. virtual pages in that region should never be mapped in user mode.
  119. (In kernel mode, the only things in that region are the 512Mb super
  120. page (locked in), and vmalloc (modules) + I/O device pages (handled
  121. by handle_vmalloc_fault), so no PGD for the upper half is required
  122. by kernel mode either).
  123. See how mm->pgd is allocated and initialised in pgd_alloc to see why
  124. the next test is necessary. - RPC */
  125. if (address >= (unsigned long) TASK_SIZE) {
  126. /* upper half - never has page table entries. */
  127. return 0;
  128. }
  129. dir = pgd_offset(mm, address);
  130. if (pgd_none(*dir)) {
  131. return 0;
  132. }
  133. if (!pgd_present(*dir)) {
  134. return 0;
  135. }
  136. pmd = pmd_offset(dir, address);
  137. if (pmd_none(*pmd)) {
  138. return 0;
  139. }
  140. if (!pmd_present(*pmd)) {
  141. return 0;
  142. }
  143. pte = pte_offset_kernel(pmd, address);
  144. entry = *pte;
  145. if (pte_none(entry)) {
  146. return 0;
  147. }
  148. if (!pte_present(entry)) {
  149. return 0;
  150. }
  151. /* If the page doesn't have sufficient protection bits set to service the
  152. kind of fault being handled, there's not much point doing the TLB refill.
  153. Punt the fault to the general handler. */
  154. if ((pte_val(entry) & protection_flags) != protection_flags) {
  155. return 0;
  156. }
  157. __do_tlb_refill(address, textaccess, pte);
  158. return 1;
  159. }
  160. /* Put all this information into one structure so that everything is just arithmetic
  161. relative to a single base address. This reduces the number of movi/shori pairs needed
  162. just to load addresses of static data. */
  163. struct expevt_lookup {
  164. unsigned short protection_flags[8];
  165. unsigned char is_text_access[8];
  166. unsigned char is_write_access[8];
  167. };
  168. #define PRU (1<<9)
  169. #define PRW (1<<8)
  170. #define PRX (1<<7)
  171. #define PRR (1<<6)
  172. #define DIRTY (_PAGE_DIRTY | _PAGE_ACCESSED)
  173. #define YOUNG (_PAGE_ACCESSED)
  174. /* Sized as 8 rather than 4 to allow checking the PTE's PRU bit against whether
  175. the fault happened in user mode or privileged mode. */
  176. static struct expevt_lookup expevt_lookup_table = {
  177. .protection_flags = {PRX, PRX, 0, 0, PRR, PRR, PRW, PRW},
  178. .is_text_access = {1, 1, 0, 0, 0, 0, 0, 0}
  179. };
  180. /*
  181. This routine handles page faults that can be serviced just by refilling a
  182. TLB entry from an existing page table entry. (This case represents a very
  183. large majority of page faults.) Return 1 if the fault was successfully
  184. handled. Return 0 if the fault could not be handled. (This leads into the
  185. general fault handling in fault.c which deals with mapping file-backed
  186. pages, stack growth, segmentation faults, swapping etc etc)
  187. */
  188. asmlinkage int do_fast_page_fault(unsigned long long ssr_md, unsigned long long expevt,
  189. unsigned long address)
  190. {
  191. struct task_struct *tsk;
  192. struct mm_struct *mm;
  193. unsigned long long textaccess;
  194. unsigned long long protection_flags;
  195. unsigned long long index;
  196. unsigned long long expevt4;
  197. /* The next few lines implement a way of hashing EXPEVT into a small array index
  198. which can be used to lookup parameters specific to the type of TLBMISS being
  199. handled. Note:
  200. ITLBMISS has EXPEVT==0xa40
  201. RTLBMISS has EXPEVT==0x040
  202. WTLBMISS has EXPEVT==0x060
  203. */
  204. expevt4 = (expevt >> 4);
  205. /* TODO : xor ssr_md into this expression too. Then we can check that PRU is set
  206. when it needs to be. */
  207. index = expevt4 ^ (expevt4 >> 5);
  208. index &= 7;
  209. protection_flags = expevt_lookup_table.protection_flags[index];
  210. textaccess = expevt_lookup_table.is_text_access[index];
  211. #ifdef CONFIG_SH64_PROC_TLB
  212. ++calls_to_do_fast_page_fault;
  213. #endif
  214. /* SIM
  215. * Note this is now called with interrupts still disabled
  216. * This is to cope with being called for a missing IO port
  217. * address with interupts disabled. This should be fixed as
  218. * soon as we have a better 'fast path' miss handler.
  219. *
  220. * Plus take care how you try and debug this stuff.
  221. * For example, writing debug data to a port which you
  222. * have just faulted on is not going to work.
  223. */
  224. tsk = current;
  225. mm = tsk->mm;
  226. if ((address >= VMALLOC_START && address < VMALLOC_END) ||
  227. (address >= IOBASE_VADDR && address < IOBASE_END)) {
  228. if (ssr_md) {
  229. /* Process-contexts can never have this address range mapped */
  230. if (handle_vmalloc_fault(mm, protection_flags, textaccess, address)) {
  231. return 1;
  232. }
  233. }
  234. } else if (!in_interrupt() && mm) {
  235. if (handle_tlbmiss(mm, protection_flags, textaccess, address)) {
  236. return 1;
  237. }
  238. }
  239. return 0;
  240. }