tlb.c 2.6 KB

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