tlb.c 2.7 KB

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