tlb.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /* the following functions are similar to those used in the PPC port */
  28. static inline void
  29. alloc_context(struct mm_struct *mm)
  30. {
  31. struct mm_struct *old_mm;
  32. D(printk("tlb: alloc context %d (%p)\n", map_replace_ptr, mm));
  33. /* did we replace an mm ? */
  34. old_mm = page_id_map[map_replace_ptr];
  35. if(old_mm) {
  36. /* throw out any TLB entries belonging to the mm we replace
  37. * in the map
  38. */
  39. flush_tlb_mm(old_mm);
  40. old_mm->context.page_id = NO_CONTEXT;
  41. }
  42. /* insert it into the page_id_map */
  43. mm->context.page_id = map_replace_ptr;
  44. page_id_map[map_replace_ptr] = mm;
  45. map_replace_ptr++;
  46. if(map_replace_ptr == INVALID_PAGEID)
  47. map_replace_ptr = 0; /* wrap around */
  48. }
  49. /*
  50. * if needed, get a new MMU context for the mm. otherwise nothing is done.
  51. */
  52. void
  53. get_mmu_context(struct mm_struct *mm)
  54. {
  55. if(mm->context.page_id == NO_CONTEXT)
  56. alloc_context(mm);
  57. }
  58. /* called by __exit_mm to destroy the used MMU context if any before
  59. * destroying the mm itself. this is only called when the last user of the mm
  60. * drops it.
  61. *
  62. * the only thing we really need to do here is mark the used PID slot
  63. * as empty.
  64. */
  65. void
  66. destroy_context(struct mm_struct *mm)
  67. {
  68. if(mm->context.page_id != NO_CONTEXT) {
  69. D(printk("destroy_context %d (%p)\n", mm->context.page_id, mm));
  70. flush_tlb_mm(mm); /* TODO this might be redundant ? */
  71. page_id_map[mm->context.page_id] = NULL;
  72. }
  73. }
  74. /* called once during VM initialization, from init.c */
  75. void __init
  76. tlb_init(void)
  77. {
  78. int i;
  79. /* clear the page_id map */
  80. for (i = 1; i < sizeof (page_id_map) / sizeof (page_id_map[0]); i++)
  81. page_id_map[i] = NULL;
  82. /* invalidate the entire TLB */
  83. flush_tlb_all();
  84. /* the init_mm has context 0 from the boot */
  85. page_id_map[0] = &init_mm;
  86. }