stab.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/memblock.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. #include <asm/prom.h>
  21. #include <asm/abs_addr.h>
  22. struct stab_entry {
  23. unsigned long esid_data;
  24. unsigned long vsid_data;
  25. };
  26. #define NR_STAB_CACHE_ENTRIES 8
  27. static DEFINE_PER_CPU(long, stab_cache_ptr);
  28. static DEFINE_PER_CPU(long [NR_STAB_CACHE_ENTRIES], stab_cache);
  29. /*
  30. * Create a segment table entry for the given esid/vsid pair.
  31. */
  32. static int make_ste(unsigned long stab, unsigned long esid, unsigned long vsid)
  33. {
  34. unsigned long esid_data, vsid_data;
  35. unsigned long entry, group, old_esid, castout_entry, i;
  36. unsigned int global_entry;
  37. struct stab_entry *ste, *castout_ste;
  38. unsigned long kernel_segment = (esid << SID_SHIFT) >= PAGE_OFFSET;
  39. vsid_data = vsid << STE_VSID_SHIFT;
  40. esid_data = esid << SID_SHIFT | STE_ESID_KP | STE_ESID_V;
  41. if (! kernel_segment)
  42. esid_data |= STE_ESID_KS;
  43. /* Search the primary group first. */
  44. global_entry = (esid & 0x1f) << 3;
  45. ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
  46. /* Find an empty entry, if one exists. */
  47. for (group = 0; group < 2; group++) {
  48. for (entry = 0; entry < 8; entry++, ste++) {
  49. if (!(ste->esid_data & STE_ESID_V)) {
  50. ste->vsid_data = vsid_data;
  51. eieio();
  52. ste->esid_data = esid_data;
  53. return (global_entry | entry);
  54. }
  55. }
  56. /* Now search the secondary group. */
  57. global_entry = ((~esid) & 0x1f) << 3;
  58. ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
  59. }
  60. /*
  61. * Could not find empty entry, pick one with a round robin selection.
  62. * Search all entries in the two groups.
  63. */
  64. castout_entry = get_paca()->stab_rr;
  65. for (i = 0; i < 16; i++) {
  66. if (castout_entry < 8) {
  67. global_entry = (esid & 0x1f) << 3;
  68. ste = (struct stab_entry *)(stab | ((esid & 0x1f) << 7));
  69. castout_ste = ste + castout_entry;
  70. } else {
  71. global_entry = ((~esid) & 0x1f) << 3;
  72. ste = (struct stab_entry *)(stab | (((~esid) & 0x1f) << 7));
  73. castout_ste = ste + (castout_entry - 8);
  74. }
  75. /* Dont cast out the first kernel segment */
  76. if ((castout_ste->esid_data & ESID_MASK) != PAGE_OFFSET)
  77. break;
  78. castout_entry = (castout_entry + 1) & 0xf;
  79. }
  80. get_paca()->stab_rr = (castout_entry + 1) & 0xf;
  81. /* Modify the old entry to the new value. */
  82. /* Force previous translations to complete. DRENG */
  83. asm volatile("isync" : : : "memory");
  84. old_esid = castout_ste->esid_data >> SID_SHIFT;
  85. castout_ste->esid_data = 0; /* Invalidate old entry */
  86. asm volatile("sync" : : : "memory"); /* Order update */
  87. castout_ste->vsid_data = vsid_data;
  88. eieio(); /* Order update */
  89. castout_ste->esid_data = esid_data;
  90. asm volatile("slbie %0" : : "r" (old_esid << SID_SHIFT));
  91. /* Ensure completion of slbie */
  92. asm volatile("sync" : : : "memory");
  93. return (global_entry | (castout_entry & 0x7));
  94. }
  95. /*
  96. * Allocate a segment table entry for the given ea and mm
  97. */
  98. static int __ste_allocate(unsigned long ea, struct mm_struct *mm)
  99. {
  100. unsigned long vsid;
  101. unsigned char stab_entry;
  102. unsigned long offset;
  103. /* Kernel or user address? */
  104. if (is_kernel_addr(ea)) {
  105. vsid = get_kernel_vsid(ea, MMU_SEGSIZE_256M);
  106. } else {
  107. if ((ea >= TASK_SIZE_USER64) || (! mm))
  108. return 1;
  109. vsid = get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M);
  110. }
  111. stab_entry = make_ste(get_paca()->stab_addr, GET_ESID(ea), vsid);
  112. if (!is_kernel_addr(ea)) {
  113. offset = __get_cpu_var(stab_cache_ptr);
  114. if (offset < NR_STAB_CACHE_ENTRIES)
  115. __get_cpu_var(stab_cache[offset++]) = stab_entry;
  116. else
  117. offset = NR_STAB_CACHE_ENTRIES+1;
  118. __get_cpu_var(stab_cache_ptr) = offset;
  119. /* Order update */
  120. asm volatile("sync":::"memory");
  121. }
  122. return 0;
  123. }
  124. int ste_allocate(unsigned long ea)
  125. {
  126. return __ste_allocate(ea, current->mm);
  127. }
  128. /*
  129. * Do the segment table work for a context switch: flush all user
  130. * entries from the table, then preload some probably useful entries
  131. * for the new task
  132. */
  133. void switch_stab(struct task_struct *tsk, struct mm_struct *mm)
  134. {
  135. struct stab_entry *stab = (struct stab_entry *) get_paca()->stab_addr;
  136. struct stab_entry *ste;
  137. unsigned long offset;
  138. unsigned long pc = KSTK_EIP(tsk);
  139. unsigned long stack = KSTK_ESP(tsk);
  140. unsigned long unmapped_base;
  141. /* Force previous translations to complete. DRENG */
  142. asm volatile("isync" : : : "memory");
  143. /*
  144. * We need interrupts hard-disabled here, not just soft-disabled,
  145. * so that a PMU interrupt can't occur, which might try to access
  146. * user memory (to get a stack trace) and possible cause an STAB miss
  147. * which would update the stab_cache/stab_cache_ptr per-cpu variables.
  148. */
  149. hard_irq_disable();
  150. offset = __get_cpu_var(stab_cache_ptr);
  151. if (offset <= NR_STAB_CACHE_ENTRIES) {
  152. int i;
  153. for (i = 0; i < offset; i++) {
  154. ste = stab + __get_cpu_var(stab_cache[i]);
  155. ste->esid_data = 0; /* invalidate entry */
  156. }
  157. } else {
  158. unsigned long entry;
  159. /* Invalidate all entries. */
  160. ste = stab;
  161. /* Never flush the first entry. */
  162. ste += 1;
  163. for (entry = 1;
  164. entry < (HW_PAGE_SIZE / sizeof(struct stab_entry));
  165. entry++, ste++) {
  166. unsigned long ea;
  167. ea = ste->esid_data & ESID_MASK;
  168. if (!is_kernel_addr(ea)) {
  169. ste->esid_data = 0;
  170. }
  171. }
  172. }
  173. asm volatile("sync; slbia; sync":::"memory");
  174. __get_cpu_var(stab_cache_ptr) = 0;
  175. /* Now preload some entries for the new task */
  176. if (test_tsk_thread_flag(tsk, TIF_32BIT))
  177. unmapped_base = TASK_UNMAPPED_BASE_USER32;
  178. else
  179. unmapped_base = TASK_UNMAPPED_BASE_USER64;
  180. __ste_allocate(pc, mm);
  181. if (GET_ESID(pc) == GET_ESID(stack))
  182. return;
  183. __ste_allocate(stack, mm);
  184. if ((GET_ESID(pc) == GET_ESID(unmapped_base))
  185. || (GET_ESID(stack) == GET_ESID(unmapped_base)))
  186. return;
  187. __ste_allocate(unmapped_base, mm);
  188. /* Order update */
  189. asm volatile("sync" : : : "memory");
  190. }
  191. /*
  192. * Allocate segment tables for secondary CPUs. These must all go in
  193. * the first (bolted) segment, so that do_stab_bolted won't get a
  194. * recursive segment miss on the segment table itself.
  195. */
  196. void __init stabs_alloc(void)
  197. {
  198. int cpu;
  199. if (mmu_has_feature(MMU_FTR_SLB))
  200. return;
  201. for_each_possible_cpu(cpu) {
  202. unsigned long newstab;
  203. if (cpu == 0)
  204. continue; /* stab for CPU 0 is statically allocated */
  205. newstab = memblock_alloc_base(HW_PAGE_SIZE, HW_PAGE_SIZE,
  206. 1<<SID_SHIFT);
  207. newstab = (unsigned long)__va(newstab);
  208. memset((void *)newstab, 0, HW_PAGE_SIZE);
  209. paca[cpu].stab_addr = newstab;
  210. paca[cpu].stab_real = virt_to_abs(newstab);
  211. printk(KERN_INFO "Segment table for CPU %d at 0x%llx "
  212. "virtual, 0x%llx absolute\n",
  213. cpu, paca[cpu].stab_addr, paca[cpu].stab_real);
  214. }
  215. }
  216. /*
  217. * Build an entry for the base kernel segment and put it into
  218. * the segment table or SLB. All other segment table or SLB
  219. * entries are faulted in.
  220. */
  221. void stab_initialize(unsigned long stab)
  222. {
  223. unsigned long vsid = get_kernel_vsid(PAGE_OFFSET, MMU_SEGSIZE_256M);
  224. unsigned long stabreal;
  225. asm volatile("isync; slbia; isync":::"memory");
  226. make_ste(stab, GET_ESID(PAGE_OFFSET), vsid);
  227. /* Order update */
  228. asm volatile("sync":::"memory");
  229. /* Set ASR */
  230. stabreal = get_paca()->stab_real | 0x1ul;
  231. mtspr(SPRN_ASR, stabreal);
  232. }