stab.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. struct stab_entry {
  22. unsigned long esid_data;
  23. unsigned long vsid_data;
  24. };
  25. #define NR_STAB_CACHE_ENTRIES 8
  26. static DEFINE_PER_CPU(long, stab_cache_ptr);
  27. static DEFINE_PER_CPU(long [NR_STAB_CACHE_ENTRIES], stab_cache);
  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) >= PAGE_OFFSET;
  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. eieio();
  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) != PAGE_OFFSET)
  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. eieio(); /* 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 (is_kernel_addr(ea)) {
  104. vsid = get_kernel_vsid(ea, MMU_SEGSIZE_256M);
  105. } else {
  106. if ((ea >= TASK_SIZE_USER64) || (! mm))
  107. return 1;
  108. vsid = get_vsid(mm->context.id, ea, MMU_SEGSIZE_256M);
  109. }
  110. stab_entry = make_ste(get_paca()->stab_addr, GET_ESID(ea), vsid);
  111. if (!is_kernel_addr(ea)) {
  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;
  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. /*
  143. * We need interrupts hard-disabled here, not just soft-disabled,
  144. * so that a PMU interrupt can't occur, which might try to access
  145. * user memory (to get a stack trace) and possible cause an STAB miss
  146. * which would update the stab_cache/stab_cache_ptr per-cpu variables.
  147. */
  148. hard_irq_disable();
  149. offset = __get_cpu_var(stab_cache_ptr);
  150. if (offset <= NR_STAB_CACHE_ENTRIES) {
  151. int i;
  152. for (i = 0; i < offset; i++) {
  153. ste = stab + __get_cpu_var(stab_cache[i]);
  154. ste->esid_data = 0; /* invalidate entry */
  155. }
  156. } else {
  157. unsigned long entry;
  158. /* Invalidate all entries. */
  159. ste = stab;
  160. /* Never flush the first entry. */
  161. ste += 1;
  162. for (entry = 1;
  163. entry < (HW_PAGE_SIZE / sizeof(struct stab_entry));
  164. entry++, ste++) {
  165. unsigned long ea;
  166. ea = ste->esid_data & ESID_MASK;
  167. if (!is_kernel_addr(ea)) {
  168. ste->esid_data = 0;
  169. }
  170. }
  171. }
  172. asm volatile("sync; slbia; sync":::"memory");
  173. __get_cpu_var(stab_cache_ptr) = 0;
  174. /* Now preload some entries for the new task */
  175. if (test_tsk_thread_flag(tsk, TIF_32BIT))
  176. unmapped_base = TASK_UNMAPPED_BASE_USER32;
  177. else
  178. unmapped_base = TASK_UNMAPPED_BASE_USER64;
  179. __ste_allocate(pc, mm);
  180. if (GET_ESID(pc) == GET_ESID(stack))
  181. return;
  182. __ste_allocate(stack, mm);
  183. if ((GET_ESID(pc) == GET_ESID(unmapped_base))
  184. || (GET_ESID(stack) == GET_ESID(unmapped_base)))
  185. return;
  186. __ste_allocate(unmapped_base, mm);
  187. /* Order update */
  188. asm volatile("sync" : : : "memory");
  189. }
  190. /*
  191. * Allocate segment tables for secondary CPUs. These must all go in
  192. * the first (bolted) segment, so that do_stab_bolted won't get a
  193. * recursive segment miss on the segment table itself.
  194. */
  195. void __init stabs_alloc(void)
  196. {
  197. int cpu;
  198. if (mmu_has_feature(MMU_FTR_SLB))
  199. return;
  200. for_each_possible_cpu(cpu) {
  201. unsigned long newstab;
  202. if (cpu == 0)
  203. continue; /* stab for CPU 0 is statically allocated */
  204. newstab = memblock_alloc_base(HW_PAGE_SIZE, HW_PAGE_SIZE,
  205. 1<<SID_SHIFT);
  206. newstab = (unsigned long)__va(newstab);
  207. memset((void *)newstab, 0, HW_PAGE_SIZE);
  208. paca[cpu].stab_addr = newstab;
  209. paca[cpu].stab_real = __pa(newstab);
  210. printk(KERN_INFO "Segment table for CPU %d at 0x%llx "
  211. "virtual, 0x%llx absolute\n",
  212. cpu, paca[cpu].stab_addr, paca[cpu].stab_real);
  213. }
  214. }
  215. /*
  216. * Build an entry for the base kernel segment and put it into
  217. * the segment table or SLB. All other segment table or SLB
  218. * entries are faulted in.
  219. */
  220. void stab_initialize(unsigned long stab)
  221. {
  222. unsigned long vsid = get_kernel_vsid(PAGE_OFFSET, MMU_SEGSIZE_256M);
  223. unsigned long stabreal;
  224. asm volatile("isync; slbia; isync":::"memory");
  225. make_ste(stab, GET_ESID(PAGE_OFFSET), vsid);
  226. /* Order update */
  227. asm volatile("sync":::"memory");
  228. /* Set ASR */
  229. stabreal = get_paca()->stab_real | 0x1ul;
  230. mtspr(SPRN_ASR, stabreal);
  231. }