hash_low.S 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * ppc64 MMU hashtable management routines
  3. *
  4. * (c) Copyright IBM Corp. 2003
  5. *
  6. * Maintained by: Benjamin Herrenschmidt
  7. * <benh@kernel.crashing.org>
  8. *
  9. * This file is covered by the GNU Public Licence v2 as
  10. * described in the kernel's COPYING file.
  11. */
  12. #include <asm/processor.h>
  13. #include <asm/pgtable.h>
  14. #include <asm/mmu.h>
  15. #include <asm/page.h>
  16. #include <asm/types.h>
  17. #include <asm/ppc_asm.h>
  18. #include <asm/offsets.h>
  19. #include <asm/cputable.h>
  20. .text
  21. /*
  22. * Stackframe:
  23. *
  24. * +-> Back chain (SP + 256)
  25. * | General register save area (SP + 112)
  26. * | Parameter save area (SP + 48)
  27. * | TOC save area (SP + 40)
  28. * | link editor doubleword (SP + 32)
  29. * | compiler doubleword (SP + 24)
  30. * | LR save area (SP + 16)
  31. * | CR save area (SP + 8)
  32. * SP ---> +-- Back chain (SP + 0)
  33. */
  34. #define STACKFRAMESIZE 256
  35. /* Save parameters offsets */
  36. #define STK_PARM(i) (STACKFRAMESIZE + 48 + ((i)-3)*8)
  37. /* Save non-volatile offsets */
  38. #define STK_REG(i) (112 + ((i)-14)*8)
  39. /*
  40. * _hash_page(unsigned long ea, unsigned long access, unsigned long vsid,
  41. * pte_t *ptep, unsigned long trap, int local)
  42. *
  43. * Adds a page to the hash table. This is the non-LPAR version for now
  44. */
  45. _GLOBAL(__hash_page)
  46. mflr r0
  47. std r0,16(r1)
  48. stdu r1,-STACKFRAMESIZE(r1)
  49. /* Save all params that we need after a function call */
  50. std r6,STK_PARM(r6)(r1)
  51. std r8,STK_PARM(r8)(r1)
  52. /* Add _PAGE_PRESENT to access */
  53. ori r4,r4,_PAGE_PRESENT
  54. /* Save non-volatile registers.
  55. * r31 will hold "old PTE"
  56. * r30 is "new PTE"
  57. * r29 is "va"
  58. * r28 is a hash value
  59. * r27 is hashtab mask (maybe dynamic patched instead ?)
  60. */
  61. std r27,STK_REG(r27)(r1)
  62. std r28,STK_REG(r28)(r1)
  63. std r29,STK_REG(r29)(r1)
  64. std r30,STK_REG(r30)(r1)
  65. std r31,STK_REG(r31)(r1)
  66. /* Step 1:
  67. *
  68. * Check permissions, atomically mark the linux PTE busy
  69. * and hashed.
  70. */
  71. 1:
  72. ldarx r31,0,r6
  73. /* Check access rights (access & ~(pte_val(*ptep))) */
  74. andc. r0,r4,r31
  75. bne- htab_wrong_access
  76. /* Check if PTE is busy */
  77. andi. r0,r31,_PAGE_BUSY
  78. /* If so, just bail out and refault if needed. Someone else
  79. * is changing this PTE anyway and might hash it.
  80. */
  81. bne- bail_ok
  82. /* Prepare new PTE value (turn access RW into DIRTY, then
  83. * add BUSY,HASHPTE and ACCESSED)
  84. */
  85. rlwinm r30,r4,32-9+7,31-7,31-7 /* _PAGE_RW -> _PAGE_DIRTY */
  86. or r30,r30,r31
  87. ori r30,r30,_PAGE_BUSY | _PAGE_ACCESSED | _PAGE_HASHPTE
  88. /* Write the linux PTE atomically (setting busy) */
  89. stdcx. r30,0,r6
  90. bne- 1b
  91. isync
  92. /* Step 2:
  93. *
  94. * Insert/Update the HPTE in the hash table. At this point,
  95. * r4 (access) is re-useable, we use it for the new HPTE flags
  96. */
  97. /* Calc va and put it in r29 */
  98. rldicr r29,r5,28,63-28
  99. rldicl r3,r3,0,36
  100. or r29,r3,r29
  101. /* Calculate hash value for primary slot and store it in r28 */
  102. rldicl r5,r5,0,25 /* vsid & 0x0000007fffffffff */
  103. rldicl r0,r3,64-12,48 /* (ea >> 12) & 0xffff */
  104. xor r28,r5,r0
  105. /* Convert linux PTE bits into HW equivalents */
  106. andi. r3,r30,0x1fe /* Get basic set of flags */
  107. xori r3,r3,HW_NO_EXEC /* _PAGE_EXEC -> NOEXEC */
  108. rlwinm r0,r30,32-9+1,30,30 /* _PAGE_RW -> _PAGE_USER (r0) */
  109. rlwinm r4,r30,32-7+1,30,30 /* _PAGE_DIRTY -> _PAGE_USER (r4) */
  110. and r0,r0,r4 /* _PAGE_RW & _PAGE_DIRTY -> r0 bit 30 */
  111. andc r0,r30,r0 /* r0 = pte & ~r0 */
  112. rlwimi r3,r0,32-1,31,31 /* Insert result into PP lsb */
  113. /* We eventually do the icache sync here (maybe inline that
  114. * code rather than call a C function...)
  115. */
  116. BEGIN_FTR_SECTION
  117. BEGIN_FTR_SECTION
  118. mr r4,r30
  119. mr r5,r7
  120. bl .hash_page_do_lazy_icache
  121. END_FTR_SECTION_IFSET(CPU_FTR_NOEXECUTE)
  122. END_FTR_SECTION_IFCLR(CPU_FTR_COHERENT_ICACHE)
  123. /* At this point, r3 contains new PP bits, save them in
  124. * place of "access" in the param area (sic)
  125. */
  126. std r3,STK_PARM(r4)(r1)
  127. /* Get htab_hash_mask */
  128. ld r4,htab_hash_mask@got(2)
  129. ld r27,0(r4) /* htab_hash_mask -> r27 */
  130. /* Check if we may already be in the hashtable, in this case, we
  131. * go to out-of-line code to try to modify the HPTE
  132. */
  133. andi. r0,r31,_PAGE_HASHPTE
  134. bne htab_modify_pte
  135. htab_insert_pte:
  136. /* Clear hpte bits in new pte (we also clear BUSY btw) and
  137. * add _PAGE_HASHPTE
  138. */
  139. lis r0,_PAGE_HPTEFLAGS@h
  140. ori r0,r0,_PAGE_HPTEFLAGS@l
  141. andc r30,r30,r0
  142. ori r30,r30,_PAGE_HASHPTE
  143. /* page number in r5 */
  144. rldicl r5,r31,64-PTE_SHIFT,PTE_SHIFT
  145. /* Calculate primary group hash */
  146. and r0,r28,r27
  147. rldicr r3,r0,3,63-3 /* r0 = (hash & mask) << 3 */
  148. /* Call ppc_md.hpte_insert */
  149. ld r7,STK_PARM(r4)(r1) /* Retreive new pp bits */
  150. mr r4,r29 /* Retreive va */
  151. li r6,0 /* primary slot */
  152. li r8,0 /* not bolted and not large */
  153. li r9,0
  154. _GLOBAL(htab_call_hpte_insert1)
  155. bl . /* Will be patched by htab_finish_init() */
  156. cmpdi 0,r3,0
  157. bge htab_pte_insert_ok /* Insertion successful */
  158. cmpdi 0,r3,-2 /* Critical failure */
  159. beq- htab_pte_insert_failure
  160. /* Now try secondary slot */
  161. /* page number in r5 */
  162. rldicl r5,r31,64-PTE_SHIFT,PTE_SHIFT
  163. /* Calculate secondary group hash */
  164. andc r0,r27,r28
  165. rldicr r3,r0,3,63-3 /* r0 = (~hash & mask) << 3 */
  166. /* Call ppc_md.hpte_insert */
  167. ld r7,STK_PARM(r4)(r1) /* Retreive new pp bits */
  168. mr r4,r29 /* Retreive va */
  169. li r6,1 /* secondary slot */
  170. li r8,0 /* not bolted and not large */
  171. li r9,0
  172. _GLOBAL(htab_call_hpte_insert2)
  173. bl . /* Will be patched by htab_finish_init() */
  174. cmpdi 0,r3,0
  175. bge+ htab_pte_insert_ok /* Insertion successful */
  176. cmpdi 0,r3,-2 /* Critical failure */
  177. beq- htab_pte_insert_failure
  178. /* Both are full, we need to evict something */
  179. mftb r0
  180. /* Pick a random group based on TB */
  181. andi. r0,r0,1
  182. mr r5,r28
  183. bne 2f
  184. not r5,r5
  185. 2: and r0,r5,r27
  186. rldicr r3,r0,3,63-3 /* r0 = (hash & mask) << 3 */
  187. /* Call ppc_md.hpte_remove */
  188. _GLOBAL(htab_call_hpte_remove)
  189. bl . /* Will be patched by htab_finish_init() */
  190. /* Try all again */
  191. b htab_insert_pte
  192. bail_ok:
  193. li r3,0
  194. b bail
  195. htab_pte_insert_ok:
  196. /* Insert slot number & secondary bit in PTE */
  197. rldimi r30,r3,12,63-15
  198. /* Write out the PTE with a normal write
  199. * (maybe add eieio may be good still ?)
  200. */
  201. htab_write_out_pte:
  202. ld r6,STK_PARM(r6)(r1)
  203. std r30,0(r6)
  204. li r3, 0
  205. bail:
  206. ld r27,STK_REG(r27)(r1)
  207. ld r28,STK_REG(r28)(r1)
  208. ld r29,STK_REG(r29)(r1)
  209. ld r30,STK_REG(r30)(r1)
  210. ld r31,STK_REG(r31)(r1)
  211. addi r1,r1,STACKFRAMESIZE
  212. ld r0,16(r1)
  213. mtlr r0
  214. blr
  215. htab_modify_pte:
  216. /* Keep PP bits in r4 and slot idx from the PTE around in r3 */
  217. mr r4,r3
  218. rlwinm r3,r31,32-12,29,31
  219. /* Secondary group ? if yes, get a inverted hash value */
  220. mr r5,r28
  221. andi. r0,r31,_PAGE_SECONDARY
  222. beq 1f
  223. not r5,r5
  224. 1:
  225. /* Calculate proper slot value for ppc_md.hpte_updatepp */
  226. and r0,r5,r27
  227. rldicr r0,r0,3,63-3 /* r0 = (hash & mask) << 3 */
  228. add r3,r0,r3 /* add slot idx */
  229. /* Call ppc_md.hpte_updatepp */
  230. mr r5,r29 /* va */
  231. li r6,0 /* large is 0 */
  232. ld r7,STK_PARM(r8)(r1) /* get "local" param */
  233. _GLOBAL(htab_call_hpte_updatepp)
  234. bl . /* Will be patched by htab_finish_init() */
  235. /* if we failed because typically the HPTE wasn't really here
  236. * we try an insertion.
  237. */
  238. cmpdi 0,r3,-1
  239. beq- htab_insert_pte
  240. /* Clear the BUSY bit and Write out the PTE */
  241. li r0,_PAGE_BUSY
  242. andc r30,r30,r0
  243. b htab_write_out_pte
  244. htab_wrong_access:
  245. /* Bail out clearing reservation */
  246. stdcx. r31,0,r6
  247. li r3,1
  248. b bail
  249. htab_pte_insert_failure:
  250. /* Bail out restoring old PTE */
  251. ld r6,STK_PARM(r6)(r1)
  252. std r31,0(r6)
  253. li r3,-1
  254. b bail