fault_64.c 7.6 KB

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