tlbex.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * TLB Exception Handling for ARC
  4. *
  5. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  6. *
  7. * Vineetg: April 2011 :
  8. * -MMU v1: moved out legacy code into a seperate file
  9. * -MMU v3: PD{0,1} bits layout changed: They don't overlap anymore,
  10. * helps avoid a shift when preparing PD0 from PTE
  11. *
  12. * Vineetg: July 2009
  13. * -For MMU V2, we need not do heuristics at the time of commiting a D-TLB
  14. * entry, so that it doesn't knock out it's I-TLB entry
  15. * -Some more fine tuning:
  16. * bmsk instead of add, asl.cc instead of branch, delay slot utilise etc
  17. *
  18. * Vineetg: July 2009
  19. * -Practically rewrote the I/D TLB Miss handlers
  20. * Now 40 and 135 instructions a peice as compared to 131 and 449 resp.
  21. * Hence Leaner by 1.5 K
  22. * Used Conditional arithmetic to replace excessive branching
  23. * Also used short instructions wherever possible
  24. *
  25. * Vineetg: Aug 13th 2008
  26. * -Passing ECR (Exception Cause REG) to do_page_fault( ) for printing
  27. * more information in case of a Fatality
  28. *
  29. * Vineetg: March 25th Bug #92690
  30. * -Added Debug Code to check if sw-ASID == hw-ASID
  31. * Rahul Trivedi, Amit Bhor: Codito Technologies 2004
  32. */
  33. #include <linux/linkage.h>
  34. #include <linux/pgtable.h>
  35. #include <asm/entry.h>
  36. #include <asm/mmu.h>
  37. #include <asm/arcregs.h>
  38. #include <asm/cache.h>
  39. #include <asm/processor.h>
  40. #ifdef CONFIG_ISA_ARCOMPACT
  41. ;-----------------------------------------------------------------
  42. ; ARC700 Exception Handling doesn't auto-switch stack and it only provides
  43. ; ONE scratch AUX reg "ARC_REG_SCRATCH_DATA0"
  44. ;
  45. ; For Non-SMP, the scratch AUX reg is repurposed to cache task PGD, so a
  46. ; "global" is used to free-up FIRST core reg to be able to code the rest of
  47. ; exception prologue (IRQ auto-disabled on Exceptions, so it's IRQ-safe).
  48. ; Since the Fast Path TLB Miss handler is coded with 4 regs, the remaining 3
  49. ; need to be saved as well by extending the "global" to be 4 words. Hence
  50. ; ".size ex_saved_reg1, 16"
  51. ; [All of this dance is to avoid stack switching for each TLB Miss, since we
  52. ; only need to save only a handful of regs, as opposed to complete reg file]
  53. ;
  54. ; For ARC700 SMP, the "global" obviously can't be used for free up the FIRST
  55. ; core reg as it will not be SMP safe.
  56. ; Thus scratch AUX reg is used (and no longer used to cache task PGD).
  57. ; To save the rest of 3 regs - per cpu, the global is made "per-cpu".
  58. ; Epilogue thus has to locate the "per-cpu" storage for regs.
  59. ; To avoid cache line bouncing the per-cpu global is aligned/sized per
  60. ; L1_CACHE_SHIFT, despite fundamentally needing to be 12 bytes only. Hence
  61. ; ".size ex_saved_reg1, (CONFIG_NR_CPUS << L1_CACHE_SHIFT)"
  62. ; As simple as that....
  63. ;--------------------------------------------------------------------------
  64. ; scratch memory to save [r0-r3] used to code TLB refill Handler
  65. ARCFP_DATA ex_saved_reg1
  66. .align 1 << L1_CACHE_SHIFT
  67. .type ex_saved_reg1, @object
  68. #ifdef CONFIG_SMP
  69. .size ex_saved_reg1, (CONFIG_NR_CPUS << L1_CACHE_SHIFT)
  70. ex_saved_reg1:
  71. .zero (CONFIG_NR_CPUS << L1_CACHE_SHIFT)
  72. #else
  73. .size ex_saved_reg1, 16
  74. ex_saved_reg1:
  75. .zero 16
  76. #endif
  77. .macro TLBMISS_FREEUP_REGS
  78. #ifdef CONFIG_SMP
  79. sr r0, [ARC_REG_SCRATCH_DATA0] ; freeup r0 to code with
  80. GET_CPU_ID r0 ; get to per cpu scratch mem,
  81. asl r0, r0, L1_CACHE_SHIFT ; cache line wide per cpu
  82. add r0, @ex_saved_reg1, r0
  83. #else
  84. st r0, [@ex_saved_reg1]
  85. mov_s r0, @ex_saved_reg1
  86. #endif
  87. st_s r1, [r0, 4]
  88. st_s r2, [r0, 8]
  89. st_s r3, [r0, 12]
  90. .endm
  91. .macro TLBMISS_RESTORE_REGS
  92. #ifdef CONFIG_SMP
  93. GET_CPU_ID r0 ; get to per cpu scratch mem
  94. asl r0, r0, L1_CACHE_SHIFT ; each is cache line wide
  95. add r0, @ex_saved_reg1, r0
  96. ld_s r3, [r0,12]
  97. ld_s r2, [r0, 8]
  98. ld_s r1, [r0, 4]
  99. lr r0, [ARC_REG_SCRATCH_DATA0]
  100. #else
  101. mov_s r0, @ex_saved_reg1
  102. ld_s r3, [r0,12]
  103. ld_s r2, [r0, 8]
  104. ld_s r1, [r0, 4]
  105. ld_s r0, [r0]
  106. #endif
  107. .endm
  108. #else /* ARCv2 */
  109. .macro TLBMISS_FREEUP_REGS
  110. #ifdef CONFIG_ARC_HAS_LL64
  111. std r0, [sp, -16]
  112. std r2, [sp, -8]
  113. #else
  114. PUSH r0
  115. PUSH r1
  116. PUSH r2
  117. PUSH r3
  118. #endif
  119. .endm
  120. .macro TLBMISS_RESTORE_REGS
  121. #ifdef CONFIG_ARC_HAS_LL64
  122. ldd r0, [sp, -16]
  123. ldd r2, [sp, -8]
  124. #else
  125. POP r3
  126. POP r2
  127. POP r1
  128. POP r0
  129. #endif
  130. .endm
  131. #endif
  132. ;============================================================================
  133. ;TLB Miss handling Code
  134. ;============================================================================
  135. #ifndef PMD_SHIFT
  136. #define PMD_SHIFT PUD_SHIFT
  137. #endif
  138. #ifndef PUD_SHIFT
  139. #define PUD_SHIFT PGDIR_SHIFT
  140. #endif
  141. ;-----------------------------------------------------------------------------
  142. ; This macro does the page-table lookup for the faulting address.
  143. ; OUT: r0 = PTE faulted on, r1 = ptr to PTE, r2 = Faulting V-address
  144. .macro LOAD_FAULT_PTE
  145. lr r2, [efa]
  146. #ifdef CONFIG_ISA_ARCV2
  147. lr r1, [ARC_REG_SCRATCH_DATA0] ; current pgd
  148. #else
  149. GET_CURR_TASK_ON_CPU r1
  150. ld r1, [r1, TASK_ACT_MM]
  151. ld r1, [r1, MM_PGD]
  152. #endif
  153. lsr r0, r2, PGDIR_SHIFT ; Bits for indexing into PGD
  154. ld.as r3, [r1, r0] ; PGD entry corresp to faulting addr
  155. tst r3, r3
  156. bz do_slow_path_pf ; if no Page Table, do page fault
  157. #if CONFIG_PGTABLE_LEVELS > 3
  158. lsr r0, r2, PUD_SHIFT ; Bits for indexing into PUD
  159. and r0, r0, (PTRS_PER_PUD - 1)
  160. ld.as r1, [r3, r0] ; PMD entry
  161. tst r1, r1
  162. bz do_slow_path_pf
  163. mov r3, r1
  164. #endif
  165. #if CONFIG_PGTABLE_LEVELS > 2
  166. lsr r0, r2, PMD_SHIFT ; Bits for indexing into PMD
  167. and r0, r0, (PTRS_PER_PMD - 1)
  168. ld.as r1, [r3, r0] ; PMD entry
  169. tst r1, r1
  170. bz do_slow_path_pf
  171. mov r3, r1
  172. #endif
  173. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  174. and.f 0, r3, _PAGE_HW_SZ ; Is this Huge PMD (thp)
  175. add2.nz r1, r1, r0
  176. bnz.d 2f ; YES: PGD == PMD has THP PTE: stop pgd walk
  177. mov.nz r0, r3
  178. #endif
  179. and r1, r3, PAGE_MASK
  180. ; Get the PTE entry: The idea is
  181. ; (1) x = addr >> PAGE_SHIFT -> masks page-off bits from @fault-addr
  182. ; (2) y = x & (PTRS_PER_PTE - 1) -> to get index
  183. ; (3) z = (pgtbl + y * 4)
  184. #ifdef CONFIG_ARC_HAS_PAE40
  185. #define PTE_SIZE_LOG 3 /* 8 == 2 ^ 3 */
  186. #else
  187. #define PTE_SIZE_LOG 2 /* 4 == 2 ^ 2 */
  188. #endif
  189. ; multiply in step (3) above avoided by shifting lesser in step (1)
  190. lsr r0, r2, ( PAGE_SHIFT - PTE_SIZE_LOG )
  191. and r0, r0, ( (PTRS_PER_PTE - 1) << PTE_SIZE_LOG )
  192. ld.aw r0, [r1, r0] ; r0: PTE (lower word only for PAE40)
  193. ; r1: PTE ptr
  194. 2:
  195. .endm
  196. ;-----------------------------------------------------------------
  197. ; Convert Linux PTE entry into TLB entry
  198. ; A one-word PTE entry is programmed as two-word TLB Entry [PD0:PD1] in mmu
  199. ; (for PAE40, two-words PTE, while three-word TLB Entry [PD0:PD1:PD1HI])
  200. ; IN: r0 = PTE, r1 = ptr to PTE
  201. .macro CONV_PTE_TO_TLB
  202. and r3, r0, PTE_BITS_RWX ; r w x
  203. asl r2, r3, 3 ; Kr Kw Kx 0 0 0 (GLOBAL, kernel only)
  204. and.f 0, r0, _PAGE_GLOBAL
  205. or.z r2, r2, r3 ; Kr Kw Kx Ur Uw Ux (!GLOBAL, user page)
  206. and r3, r0, PTE_BITS_NON_RWX_IN_PD1 ; Extract PFN+cache bits from PTE
  207. or r3, r3, r2
  208. sr r3, [ARC_REG_TLBPD1] ; paddr[31..13] | Kr Kw Kx Ur Uw Ux | C
  209. #ifdef CONFIG_ARC_HAS_PAE40
  210. ld r3, [r1, 4] ; paddr[39..32]
  211. sr r3, [ARC_REG_TLBPD1HI]
  212. #endif
  213. and r2, r0, PTE_BITS_IN_PD0 ; Extract other PTE flags: (V)alid, (G)lb
  214. lr r3,[ARC_REG_TLBPD0] ; MMU prepares PD0 with vaddr and asid
  215. or r3, r3, r2 ; S | vaddr | {sasid|asid}
  216. sr r3,[ARC_REG_TLBPD0] ; rewrite PD0
  217. .endm
  218. ;-----------------------------------------------------------------
  219. ; Commit the TLB entry into MMU
  220. .macro COMMIT_ENTRY_TO_MMU
  221. #ifdef CONFIG_ARC_MMU_V3
  222. /* Get free TLB slot: Set = computed from vaddr, way = random */
  223. sr TLBGetIndex, [ARC_REG_TLBCOMMAND]
  224. /* Commit the Write */
  225. sr TLBWriteNI, [ARC_REG_TLBCOMMAND]
  226. #else
  227. sr TLBInsertEntry, [ARC_REG_TLBCOMMAND]
  228. #endif
  229. 88:
  230. .endm
  231. ARCFP_CODE ;Fast Path Code, candidate for ICCM
  232. ;-----------------------------------------------------------------------------
  233. ; I-TLB Miss Exception Handler
  234. ;-----------------------------------------------------------------------------
  235. ENTRY(EV_TLBMissI)
  236. TLBMISS_FREEUP_REGS
  237. ;----------------------------------------------------------------
  238. ; Get the PTE corresponding to V-addr accessed, r2 is setup with EFA
  239. LOAD_FAULT_PTE
  240. ;----------------------------------------------------------------
  241. ; VERIFY_PTE: Check if PTE permissions approp for executing code
  242. cmp_s r2, VMALLOC_START
  243. mov_s r2, (_PAGE_PRESENT | _PAGE_EXECUTE)
  244. or.hs r2, r2, _PAGE_GLOBAL
  245. and r3, r0, r2 ; Mask out NON Flag bits from PTE
  246. xor.f r3, r3, r2 ; check ( ( pte & flags_test ) == flags_test )
  247. bnz do_slow_path_pf
  248. ; Let Linux VM know that the page was accessed
  249. or r0, r0, _PAGE_ACCESSED ; set Accessed Bit
  250. st_s r0, [r1] ; Write back PTE
  251. CONV_PTE_TO_TLB
  252. COMMIT_ENTRY_TO_MMU
  253. TLBMISS_RESTORE_REGS
  254. EV_TLBMissI_fast_ret: ; additional label for VDK OS-kit instrumentation
  255. rtie
  256. END(EV_TLBMissI)
  257. ;-----------------------------------------------------------------------------
  258. ; D-TLB Miss Exception Handler
  259. ;-----------------------------------------------------------------------------
  260. ENTRY(EV_TLBMissD)
  261. TLBMISS_FREEUP_REGS
  262. ;----------------------------------------------------------------
  263. ; Get the PTE corresponding to V-addr accessed
  264. ; If PTE exists, it will setup, r0 = PTE, r1 = Ptr to PTE, r2 = EFA
  265. LOAD_FAULT_PTE
  266. ;----------------------------------------------------------------
  267. ; VERIFY_PTE: Chk if PTE permissions approp for data access (R/W/R+W)
  268. cmp_s r2, VMALLOC_START
  269. mov_s r2, _PAGE_PRESENT ; common bit for K/U PTE
  270. or.hs r2, r2, _PAGE_GLOBAL ; kernel PTE only
  271. ; Linux PTE [RWX] bits are semantically overloaded:
  272. ; -If PAGE_GLOBAL set, they refer to kernel-only flags (vmalloc)
  273. ; -Otherwise they are user-mode permissions, and those are exactly
  274. ; same for kernel mode as well (e.g. copy_(to|from)_user)
  275. lr r3, [ecr]
  276. btst_s r3, ECR_C_BIT_DTLB_LD_MISS ; Read Access
  277. or.nz r2, r2, _PAGE_READ ; chk for Read flag in PTE
  278. btst_s r3, ECR_C_BIT_DTLB_ST_MISS ; Write Access
  279. or.nz r2, r2, _PAGE_WRITE ; chk for Write flag in PTE
  280. ; Above laddering takes care of XCHG access (both R and W)
  281. ; By now, r2 setup with all the Flags we need to check in PTE
  282. and r3, r0, r2 ; Mask out NON Flag bits from PTE
  283. brne.d r3, r2, do_slow_path_pf ; is ((pte & flags_test) == flags_test)
  284. ;----------------------------------------------------------------
  285. ; UPDATE_PTE: Let Linux VM know that page was accessed/dirty
  286. or r0, r0, _PAGE_ACCESSED ; Accessed bit always
  287. or.nz r0, r0, _PAGE_DIRTY ; if Write, set Dirty bit as well
  288. st_s r0, [r1] ; Write back PTE
  289. CONV_PTE_TO_TLB
  290. COMMIT_ENTRY_TO_MMU
  291. TLBMISS_RESTORE_REGS
  292. EV_TLBMissD_fast_ret: ; additional label for VDK OS-kit instrumentation
  293. rtie
  294. ;-------- Common routine to call Linux Page Fault Handler -----------
  295. do_slow_path_pf:
  296. #ifdef CONFIG_ISA_ARCV2
  297. ; Set Z flag if exception in U mode. Hardware micro-ops do this on any
  298. ; taken interrupt/exception, and thus is already the case at the entry
  299. ; above, but ensuing code would have already clobbered.
  300. ; EXCEPTION_PROLOGUE called in slow path, relies on correct Z flag set
  301. lr r2, [erstatus]
  302. and r2, r2, STATUS_U_MASK
  303. bxor.f 0, r2, STATUS_U_BIT
  304. #endif
  305. ; Restore the 4-scratch regs saved by fast path miss handler
  306. TLBMISS_RESTORE_REGS
  307. ; Slow path TLB Miss handled as a regular ARC Exception
  308. ; (stack switching / save the complete reg-file).
  309. b call_do_page_fault
  310. END(EV_TLBMissD)