mmu_context.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * File: include/asm-blackfin/mmu_context.h
  3. * Based on:
  4. * Author:
  5. *
  6. * Created:
  7. * Description:
  8. *
  9. * Modified:
  10. * Copyright 2004-2006 Analog Devices Inc.
  11. *
  12. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, see the file COPYING, or write
  26. * to the Free Software Foundation, Inc.,
  27. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #ifndef __BLACKFIN_MMU_CONTEXT_H__
  30. #define __BLACKFIN_MMU_CONTEXT_H__
  31. #include <linux/gfp.h>
  32. #include <linux/sched.h>
  33. #include <asm/setup.h>
  34. #include <asm/page.h>
  35. #include <asm/pgalloc.h>
  36. #include <asm/cplbinit.h>
  37. /* Note: L1 stacks are CPU-private things, so we bluntly disable this
  38. feature in SMP mode, and use the per-CPU scratch SRAM bank only to
  39. store the PDA instead. */
  40. extern void *current_l1_stack_save;
  41. extern int nr_l1stack_tasks;
  42. extern void *l1_stack_base;
  43. extern unsigned long l1_stack_len;
  44. extern int l1sram_free(const void*);
  45. extern void *l1sram_alloc_max(void*);
  46. static inline void free_l1stack(void)
  47. {
  48. nr_l1stack_tasks--;
  49. if (nr_l1stack_tasks == 0)
  50. l1sram_free(l1_stack_base);
  51. }
  52. static inline unsigned long
  53. alloc_l1stack(unsigned long length, unsigned long *stack_base)
  54. {
  55. if (nr_l1stack_tasks == 0) {
  56. l1_stack_base = l1sram_alloc_max(&l1_stack_len);
  57. if (!l1_stack_base)
  58. return 0;
  59. }
  60. if (l1_stack_len < length) {
  61. if (nr_l1stack_tasks == 0)
  62. l1sram_free(l1_stack_base);
  63. return 0;
  64. }
  65. *stack_base = (unsigned long)l1_stack_base;
  66. nr_l1stack_tasks++;
  67. return l1_stack_len;
  68. }
  69. static inline int
  70. activate_l1stack(struct mm_struct *mm, unsigned long sp_base)
  71. {
  72. if (current_l1_stack_save)
  73. memcpy(current_l1_stack_save, l1_stack_base, l1_stack_len);
  74. mm->context.l1_stack_save = current_l1_stack_save = (void*)sp_base;
  75. memcpy(l1_stack_base, current_l1_stack_save, l1_stack_len);
  76. return 1;
  77. }
  78. #define deactivate_mm(tsk,mm) do { } while (0)
  79. #define activate_mm(prev, next) switch_mm(prev, next, NULL)
  80. static inline void switch_mm(struct mm_struct *prev_mm, struct mm_struct *next_mm,
  81. struct task_struct *tsk)
  82. {
  83. #ifdef CONFIG_MPU
  84. unsigned int cpu = smp_processor_id();
  85. #endif
  86. if (prev_mm == next_mm)
  87. return;
  88. #ifdef CONFIG_MPU
  89. if (prev_mm->context.page_rwx_mask == current_rwx_mask[cpu]) {
  90. flush_switched_cplbs(cpu);
  91. set_mask_dcplbs(next_mm->context.page_rwx_mask, cpu);
  92. }
  93. #endif
  94. #ifdef CONFIG_APP_STACK_L1
  95. /* L1 stack switching. */
  96. if (!next_mm->context.l1_stack_save)
  97. return;
  98. if (next_mm->context.l1_stack_save == current_l1_stack_save)
  99. return;
  100. if (current_l1_stack_save) {
  101. memcpy(current_l1_stack_save, l1_stack_base, l1_stack_len);
  102. }
  103. current_l1_stack_save = next_mm->context.l1_stack_save;
  104. memcpy(l1_stack_base, current_l1_stack_save, l1_stack_len);
  105. #endif
  106. }
  107. #ifdef CONFIG_MPU
  108. static inline void protect_page(struct mm_struct *mm, unsigned long addr,
  109. unsigned long flags)
  110. {
  111. unsigned long *mask = mm->context.page_rwx_mask;
  112. unsigned long page = addr >> 12;
  113. unsigned long idx = page >> 5;
  114. unsigned long bit = 1 << (page & 31);
  115. if (flags & VM_MAYREAD)
  116. mask[idx] |= bit;
  117. else
  118. mask[idx] &= ~bit;
  119. mask += page_mask_nelts;
  120. if (flags & VM_MAYWRITE)
  121. mask[idx] |= bit;
  122. else
  123. mask[idx] &= ~bit;
  124. mask += page_mask_nelts;
  125. if (flags & VM_MAYEXEC)
  126. mask[idx] |= bit;
  127. else
  128. mask[idx] &= ~bit;
  129. }
  130. static inline void update_protections(struct mm_struct *mm)
  131. {
  132. unsigned int cpu = smp_processor_id();
  133. if (mm->context.page_rwx_mask == current_rwx_mask[cpu]) {
  134. flush_switched_cplbs(cpu);
  135. set_mask_dcplbs(mm->context.page_rwx_mask, cpu);
  136. }
  137. }
  138. #endif
  139. static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
  140. {
  141. }
  142. /* Called when creating a new context during fork() or execve(). */
  143. static inline int
  144. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  145. {
  146. #ifdef CONFIG_MPU
  147. unsigned long p = __get_free_pages(GFP_KERNEL, page_mask_order);
  148. mm->context.page_rwx_mask = (unsigned long *)p;
  149. memset(mm->context.page_rwx_mask, 0,
  150. page_mask_nelts * 3 * sizeof(long));
  151. #endif
  152. return 0;
  153. }
  154. static inline void destroy_context(struct mm_struct *mm)
  155. {
  156. struct sram_list_struct *tmp;
  157. #ifdef CONFIG_MPU
  158. unsigned int cpu = smp_processor_id();
  159. #endif
  160. #ifdef CONFIG_APP_STACK_L1
  161. if (current_l1_stack_save == mm->context.l1_stack_save)
  162. current_l1_stack_save = 0;
  163. if (mm->context.l1_stack_save)
  164. free_l1stack();
  165. #endif
  166. while ((tmp = mm->context.sram_list)) {
  167. mm->context.sram_list = tmp->next;
  168. sram_free(tmp->addr);
  169. kfree(tmp);
  170. }
  171. #ifdef CONFIG_MPU
  172. if (current_rwx_mask[cpu] == mm->context.page_rwx_mask)
  173. current_rwx_mask[cpu] = NULL;
  174. free_pages((unsigned long)mm->context.page_rwx_mask, page_mask_order);
  175. #endif
  176. }
  177. #endif