mmu-context.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* mmu-context.c: MMU context allocation and management
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/sched.h>
  12. #include <linux/mm.h>
  13. #include <asm/tlbflush.h>
  14. #define NR_CXN 4096
  15. static unsigned long cxn_bitmap[NR_CXN / (sizeof(unsigned long) * 8)];
  16. static LIST_HEAD(cxn_owners_lru);
  17. static DEFINE_SPINLOCK(cxn_owners_lock);
  18. int __nongpreldata cxn_pinned = -1;
  19. /*****************************************************************************/
  20. /*
  21. * initialise a new context
  22. */
  23. int init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  24. {
  25. memset(&mm->context, 0, sizeof(mm->context));
  26. INIT_LIST_HEAD(&mm->context.id_link);
  27. mm->context.itlb_cached_pge = 0xffffffffUL;
  28. mm->context.dtlb_cached_pge = 0xffffffffUL;
  29. return 0;
  30. } /* end init_new_context() */
  31. /*****************************************************************************/
  32. /*
  33. * make sure a kernel MMU context has a CPU context number
  34. * - call with cxn_owners_lock held
  35. */
  36. static unsigned get_cxn(mm_context_t *ctx)
  37. {
  38. struct list_head *_p;
  39. mm_context_t *p;
  40. unsigned cxn;
  41. if (!list_empty(&ctx->id_link)) {
  42. list_move_tail(&ctx->id_link, &cxn_owners_lru);
  43. }
  44. else {
  45. /* find the first unallocated context number
  46. * - 0 is reserved for the kernel
  47. */
  48. cxn = find_next_zero_bit(cxn_bitmap, NR_CXN, 1);
  49. if (cxn < NR_CXN) {
  50. set_bit(cxn, cxn_bitmap);
  51. }
  52. else {
  53. /* none remaining - need to steal someone else's cxn */
  54. p = NULL;
  55. list_for_each(_p, &cxn_owners_lru) {
  56. p = list_entry(_p, mm_context_t, id_link);
  57. if (!p->id_busy && p->id != cxn_pinned)
  58. break;
  59. }
  60. BUG_ON(_p == &cxn_owners_lru);
  61. cxn = p->id;
  62. p->id = 0;
  63. list_del_init(&p->id_link);
  64. __flush_tlb_mm(cxn);
  65. }
  66. ctx->id = cxn;
  67. list_add_tail(&ctx->id_link, &cxn_owners_lru);
  68. }
  69. return ctx->id;
  70. } /* end get_cxn() */
  71. /*****************************************************************************/
  72. /*
  73. * restore the current TLB miss handler mapped page tables into the MMU context and set up a
  74. * mapping for the page directory
  75. */
  76. void change_mm_context(mm_context_t *old, mm_context_t *ctx, pgd_t *pgd)
  77. {
  78. unsigned long _pgd;
  79. _pgd = virt_to_phys(pgd);
  80. /* save the state of the outgoing MMU context */
  81. old->id_busy = 0;
  82. asm volatile("movsg scr0,%0" : "=r"(old->itlb_cached_pge));
  83. asm volatile("movsg dampr4,%0" : "=r"(old->itlb_ptd_mapping));
  84. asm volatile("movsg scr1,%0" : "=r"(old->dtlb_cached_pge));
  85. asm volatile("movsg dampr5,%0" : "=r"(old->dtlb_ptd_mapping));
  86. /* select an MMU context number */
  87. spin_lock(&cxn_owners_lock);
  88. get_cxn(ctx);
  89. ctx->id_busy = 1;
  90. spin_unlock(&cxn_owners_lock);
  91. asm volatile("movgs %0,cxnr" : : "r"(ctx->id));
  92. /* restore the state of the incoming MMU context */
  93. asm volatile("movgs %0,scr0" : : "r"(ctx->itlb_cached_pge));
  94. asm volatile("movgs %0,dampr4" : : "r"(ctx->itlb_ptd_mapping));
  95. asm volatile("movgs %0,scr1" : : "r"(ctx->dtlb_cached_pge));
  96. asm volatile("movgs %0,dampr5" : : "r"(ctx->dtlb_ptd_mapping));
  97. /* map the PGD into uncached virtual memory */
  98. asm volatile("movgs %0,ttbr" : : "r"(_pgd));
  99. asm volatile("movgs %0,dampr3"
  100. :: "r"(_pgd | xAMPRx_L | xAMPRx_M | xAMPRx_SS_16Kb |
  101. xAMPRx_S | xAMPRx_C | xAMPRx_V));
  102. } /* end change_mm_context() */
  103. /*****************************************************************************/
  104. /*
  105. * finished with an MMU context number
  106. */
  107. void destroy_context(struct mm_struct *mm)
  108. {
  109. mm_context_t *ctx = &mm->context;
  110. spin_lock(&cxn_owners_lock);
  111. if (!list_empty(&ctx->id_link)) {
  112. if (ctx->id == cxn_pinned)
  113. cxn_pinned = -1;
  114. list_del_init(&ctx->id_link);
  115. clear_bit(ctx->id, cxn_bitmap);
  116. __flush_tlb_mm(ctx->id);
  117. ctx->id = 0;
  118. }
  119. spin_unlock(&cxn_owners_lock);
  120. } /* end destroy_context() */
  121. /*****************************************************************************/
  122. /*
  123. * display the MMU context currently a process is currently using
  124. */
  125. #ifdef CONFIG_PROC_FS
  126. char *proc_pid_status_frv_cxnr(struct mm_struct *mm, char *buffer)
  127. {
  128. spin_lock(&cxn_owners_lock);
  129. buffer += sprintf(buffer, "CXNR: %u\n", mm->context.id);
  130. spin_unlock(&cxn_owners_lock);
  131. return buffer;
  132. } /* end proc_pid_status_frv_cxnr() */
  133. #endif
  134. /*****************************************************************************/
  135. /*
  136. * (un)pin a process's mm_struct's MMU context ID
  137. */
  138. int cxn_pin_by_pid(pid_t pid)
  139. {
  140. struct task_struct *tsk;
  141. struct mm_struct *mm = NULL;
  142. int ret;
  143. /* unpin if pid is zero */
  144. if (pid == 0) {
  145. cxn_pinned = -1;
  146. return 0;
  147. }
  148. ret = -ESRCH;
  149. /* get a handle on the mm_struct */
  150. read_lock(&tasklist_lock);
  151. tsk = find_task_by_vpid(pid);
  152. if (tsk) {
  153. ret = -EINVAL;
  154. task_lock(tsk);
  155. if (tsk->mm) {
  156. mm = tsk->mm;
  157. atomic_inc(&mm->mm_users);
  158. ret = 0;
  159. }
  160. task_unlock(tsk);
  161. }
  162. read_unlock(&tasklist_lock);
  163. if (ret < 0)
  164. return ret;
  165. /* make sure it has a CXN and pin it */
  166. spin_lock(&cxn_owners_lock);
  167. cxn_pinned = get_cxn(&mm->context);
  168. spin_unlock(&cxn_owners_lock);
  169. mmput(mm);
  170. return 0;
  171. } /* end cxn_pin_by_pid() */