stab.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * PowerPC64 Segment Translation Support.
  3. *
  4. * Dave Engebretsen and Mike Corrigan {engebret|mikejc}@us.ibm.com
  5. * Copyright (c) 2001 Dave Engebretsen
  6. *
  7. * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/config.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/mmu.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/paca.h>
  19. #include <asm/cputable.h>
  20. struct stab_entry {
  21. unsigned long esid_data;
  22. unsigned long vsid_data;
  23. };
  24. /* Both the segment table and SLB code uses the following cache */
  25. #define NR_STAB_CACHE_ENTRIES 8
  26. DEFINE_PER_CPU(long, stab_cache_ptr);
  27. DEFINE_PER_CPU(long, stab_cache[NR_STAB_CACHE_ENTRIES]);
  28. /*
  29. * Create a segment table entry for the given esid/vsid pair.
  30. */
  31. static int make_ste(unsigned long stab, unsigned long esid, unsigned long vsid)
  32. {
  33. unsigned long esid_data, vsid_data;
  34. unsigned long entry, group, old_esid, castout_entry, i;
  35. unsigned int global_entry;
  36. struct stab_entry *ste, *castout_ste;
  37. unsigned long kernel_segment = (esid << SID_SHIFT) >= KERNELBASE;
  38. vsid_data = vsid << STE_VSID_SHIFT;
  39. esid_data = esid << SID_SHIFT | STE_ESID_KP | STE_ESID_V;
  40. if (! kernel_segment)
  41. esid_data |= STE_ESID_KS;
  42. /* Search the primary group first. */
  43. global_entry = (esid & 0x1f) << 3;
  44. ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
  45. /* Find an empty entry, if one exists. */
  46. for (group = 0; group < 2; group++) {
  47. for (entry = 0; entry < 8; entry++, ste++) {
  48. if (!(ste->esid_data & STE_ESID_V)) {
  49. ste->vsid_data = vsid_data;
  50. asm volatile("eieio":::"memory");
  51. ste->esid_data = esid_data;
  52. return (global_entry | entry);
  53. }
  54. }
  55. /* Now search the secondary group. */
  56. global_entry = ((~esid) & 0x1f) << 3;
  57. ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
  58. }
  59. /*
  60. * Could not find empty entry, pick one with a round robin selection.
  61. * Search all entries in the two groups.
  62. */
  63. castout_entry = get_paca()->stab_rr;
  64. for (i = 0; i < 16; i++) {
  65. if (castout_entry < 8) {
  66. global_entry = (esid & 0x1f) << 3;
  67. ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
  68. castout_ste = ste + castout_entry;
  69. } else {
  70. global_entry = ((~esid) & 0x1f) << 3;
  71. ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
  72. castout_ste = ste + (castout_entry - 8);
  73. }
  74. /* Dont cast out the first kernel segment */
  75. if ((castout_ste->esid_data & ESID_MASK) != KERNELBASE)
  76. break;
  77. castout_entry = (castout_entry + 1) & 0xf;
  78. }
  79. get_paca()->stab_rr = (castout_entry + 1) & 0xf;
  80. /* Modify the old entry to the new value. */
  81. /* Force previous translations to complete. DRENG */
  82. asm volatile("isync" : : : "memory");
  83. old_esid = castout_ste->esid_data >> SID_SHIFT;
  84. castout_ste->esid_data = 0; /* Invalidate old entry */
  85. asm volatile("sync" : : : "memory"); /* Order update */
  86. castout_ste->vsid_data = vsid_data;
  87. asm volatile("eieio" : : : "memory"); /* Order update */
  88. castout_ste->esid_data = esid_data;
  89. asm volatile("slbie %0" : : "r" (old_esid << SID_SHIFT));
  90. /* Ensure completion of slbie */
  91. asm volatile("sync" : : : "memory");
  92. return (global_entry | (castout_entry & 0x7));
  93. }
  94. /*
  95. * Allocate a segment table entry for the given ea and mm
  96. */
  97. static int __ste_allocate(unsigned long ea, struct mm_struct *mm)
  98. {
  99. unsigned long vsid;
  100. unsigned char stab_entry;
  101. unsigned long offset;
  102. /* Kernel or user address? */
  103. if (ea >= KERNELBASE) {
  104. vsid = get_kernel_vsid(ea);
  105. } else {
  106. if ((ea >= TASK_SIZE_USER64) || (! mm))
  107. return 1;
  108. vsid = get_vsid(mm->context.id, ea);
  109. }
  110. stab_entry = make_ste(get_paca()->stab_addr, GET_ESID(ea), vsid);
  111. if (ea < KERNELBASE) {
  112. offset = __get_cpu_var(stab_cache_ptr);
  113. if (offset < NR_STAB_CACHE_ENTRIES)
  114. __get_cpu_var(stab_cache[offset++]) = stab_entry;
  115. else
  116. offset = NR_STAB_CACHE_ENTRIES+1;
  117. __get_cpu_var(stab_cache_ptr) = offset;
  118. /* Order update */
  119. asm volatile("sync":::"memory");
  120. }
  121. return 0;
  122. }
  123. int ste_allocate(unsigned long ea)
  124. {
  125. return __ste_allocate(ea, current->mm);
  126. }
  127. /*
  128. * Do the segment table work for a context switch: flush all user
  129. * entries from the table, then preload some probably useful entries
  130. * for the new task
  131. */
  132. void switch_stab(struct task_struct *tsk, struct mm_struct *mm)
  133. {
  134. struct stab_entry *stab = (struct stab_entry *) get_paca()->stab_addr;
  135. struct stab_entry *ste;
  136. unsigned long offset = __get_cpu_var(stab_cache_ptr);
  137. unsigned long pc = KSTK_EIP(tsk);
  138. unsigned long stack = KSTK_ESP(tsk);
  139. unsigned long unmapped_base;
  140. /* Force previous translations to complete. DRENG */
  141. asm volatile("isync" : : : "memory");
  142. if (offset <= NR_STAB_CACHE_ENTRIES) {
  143. int i;
  144. for (i = 0; i < offset; i++) {
  145. ste = stab + __get_cpu_var(stab_cache[i]);
  146. ste->esid_data = 0; /* invalidate entry */
  147. }
  148. } else {
  149. unsigned long entry;
  150. /* Invalidate all entries. */
  151. ste = stab;
  152. /* Never flush the first entry. */
  153. ste += 1;
  154. for (entry = 1;
  155. entry < (PAGE_SIZE / sizeof(struct stab_entry));
  156. entry++, ste++) {
  157. unsigned long ea;
  158. ea = ste->esid_data & ESID_MASK;
  159. if (ea < KERNELBASE) {
  160. ste->esid_data = 0;
  161. }
  162. }
  163. }
  164. asm volatile("sync; slbia; sync":::"memory");
  165. __get_cpu_var(stab_cache_ptr) = 0;
  166. /* Now preload some entries for the new task */
  167. if (test_tsk_thread_flag(tsk, TIF_32BIT))
  168. unmapped_base = TASK_UNMAPPED_BASE_USER32;
  169. else
  170. unmapped_base = TASK_UNMAPPED_BASE_USER64;
  171. __ste_allocate(pc, mm);
  172. if (GET_ESID(pc) == GET_ESID(stack))
  173. return;
  174. __ste_allocate(stack, mm);
  175. if ((GET_ESID(pc) == GET_ESID(unmapped_base))
  176. || (GET_ESID(stack) == GET_ESID(unmapped_base)))
  177. return;
  178. __ste_allocate(unmapped_base, mm);
  179. /* Order update */
  180. asm volatile("sync" : : : "memory");
  181. }
  182. extern void slb_initialize(void);
  183. /*
  184. * Build an entry for the base kernel segment and put it into
  185. * the segment table or SLB. All other segment table or SLB
  186. * entries are faulted in.
  187. */
  188. void stab_initialize(unsigned long stab)
  189. {
  190. unsigned long vsid = get_kernel_vsid(KERNELBASE);
  191. if (cpu_has_feature(CPU_FTR_SLB)) {
  192. slb_initialize();
  193. } else {
  194. asm volatile("isync; slbia; isync":::"memory");
  195. make_ste(stab, GET_ESID(KERNELBASE), vsid);
  196. /* Order update */
  197. asm volatile("sync":::"memory");
  198. }
  199. }