tlb.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * linux/arch/cris/mm/tlb.c
  3. *
  4. * Copyright (C) 2000, 2001 Axis Communications AB
  5. *
  6. * Authors: Bjorn Wesen (bjornw@axis.com)
  7. *
  8. */
  9. #include <linux/init.h>
  10. #include <asm/tlb.h>
  11. #define D(x)
  12. /* The TLB can host up to 64 different mm contexts at the same time.
  13. * The running context is R_MMU_CONTEXT, and each TLB entry contains a
  14. * page_id that has to match to give a hit. In page_id_map, we keep track
  15. * of which mm's we have assigned which page_id's, so that we know when
  16. * to invalidate TLB entries.
  17. *
  18. * The last page_id is never running - it is used as an invalid page_id
  19. * so we can make TLB entries that will never match.
  20. *
  21. * Notice that we need to make the flushes atomic, otherwise an interrupt
  22. * handler that uses vmalloced memory might cause a TLB load in the middle
  23. * of a flush causing.
  24. */
  25. struct mm_struct *page_id_map[NUM_PAGEID];
  26. static int map_replace_ptr = 1; /* which page_id_map entry to replace next */
  27. /*
  28. * Initialize the context related info for a new mm_struct
  29. * instance.
  30. */
  31. int
  32. init_new_context(struct task_struct *tsk, struct mm_struct *mm)
  33. {
  34. mm->context = NO_CONTEXT;
  35. return 0;
  36. }
  37. /* the following functions are similar to those used in the PPC port */
  38. static inline void
  39. alloc_context(struct mm_struct *mm)
  40. {
  41. struct mm_struct *old_mm;
  42. D(printk("tlb: alloc context %d (%p)\n", map_replace_ptr, mm));
  43. /* did we replace an mm ? */
  44. old_mm = page_id_map[map_replace_ptr];
  45. if(old_mm) {
  46. /* throw out any TLB entries belonging to the mm we replace
  47. * in the map
  48. */
  49. flush_tlb_mm(old_mm);
  50. old_mm->context = NO_CONTEXT;
  51. }
  52. /* insert it into the page_id_map */
  53. mm->context = map_replace_ptr;
  54. page_id_map[map_replace_ptr] = mm;
  55. map_replace_ptr++;
  56. if(map_replace_ptr == INVALID_PAGEID)
  57. map_replace_ptr = 0; /* wrap around */
  58. }
  59. /*
  60. * if needed, get a new MMU context for the mm. otherwise nothing is done.
  61. */
  62. void
  63. get_mmu_context(struct mm_struct *mm)
  64. {
  65. if(mm->context == NO_CONTEXT)
  66. alloc_context(mm);
  67. }
  68. /* called by __exit_mm to destroy the used MMU context if any before
  69. * destroying the mm itself. this is only called when the last user of the mm
  70. * drops it.
  71. *
  72. * the only thing we really need to do here is mark the used PID slot
  73. * as empty.
  74. */
  75. void
  76. destroy_context(struct mm_struct *mm)
  77. {
  78. if(mm->context != NO_CONTEXT) {
  79. D(printk("destroy_context %d (%p)\n", mm->context, mm));
  80. flush_tlb_mm(mm); /* TODO this might be redundant ? */
  81. page_id_map[mm->context] = NULL;
  82. /* mm->context = NO_CONTEXT; redundant.. mm will be freed */
  83. }
  84. }
  85. /* called once during VM initialization, from init.c */
  86. void __init
  87. tlb_init(void)
  88. {
  89. int i;
  90. /* clear the page_id map */
  91. for (i = 1; i < sizeof (page_id_map) / sizeof (page_id_map[0]); i++)
  92. page_id_map[i] = NULL;
  93. /* invalidate the entire TLB */
  94. flush_tlb_all();
  95. /* the init_mm has context 0 from the boot */
  96. page_id_map[0] = &init_mm;
  97. }