vma.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "mm.h"
  2. #include "slab.h"
  3. #include "internal.h"
  4. /**
  5. * @brief 获取一块新的vma结构体,并将其与指定的mm进行绑定
  6. *
  7. * @param mm 与VMA绑定的内存空间分布结构体
  8. * @return struct vm_area_struct* 新的VMA
  9. */
  10. struct vm_area_struct *vm_area_alloc(struct mm_struct *mm)
  11. {
  12. struct vm_area_struct *vma = (struct vm_area_struct *)kmalloc(sizeof(struct vm_area_struct), 0);
  13. if (vma)
  14. vma_init(vma, mm);
  15. return vma;
  16. }
  17. /**
  18. * @brief 从链表中删除指定的vma结构体
  19. *
  20. * @param vma
  21. */
  22. void vm_area_del(struct vm_area_struct *vma)
  23. {
  24. if (vma->vm_mm == NULL)
  25. return;
  26. __vma_unlink_list(vma->vm_mm, vma);
  27. }
  28. /**
  29. * @brief 释放vma结构体
  30. *
  31. * @param vma 待释放的vma结构体
  32. */
  33. void vm_area_free(struct vm_area_struct *vma)
  34. {
  35. if (vma->vm_prev == NULL && vma->vm_next == NULL) // 如果当前是剩余的最后一个vma
  36. vma->vm_mm->vmas = NULL;
  37. kfree(vma);
  38. }
  39. /**
  40. * @brief 将vma结构体插入mm_struct的链表之中
  41. *
  42. * @param mm 内存空间分布结构体
  43. * @param vma 待插入的VMA结构体
  44. * @param prev 链表的前一个结点
  45. */
  46. void __vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma, struct vm_area_struct *prev)
  47. {
  48. struct vm_area_struct *next = NULL;
  49. vma->vm_prev = prev;
  50. if (prev) // 若指定了前一个结点,则直接连接
  51. {
  52. next = prev->vm_next;
  53. prev->vm_next = vma;
  54. }
  55. else // 否则将vma直接插入到给定的mm的vma链表之中
  56. {
  57. next = mm->vmas;
  58. mm->vmas = vma;
  59. }
  60. vma->vm_next = next;
  61. if (next != NULL)
  62. next->vm_prev = vma;
  63. }
  64. /**
  65. * @brief 将vma给定结构体从vma链表的结点之中删除
  66. *
  67. * @param mm 内存空间分布结构体
  68. * @param vma 待插入的VMA结构体
  69. */
  70. void __vma_unlink_list(struct mm_struct *mm, struct vm_area_struct *vma)
  71. {
  72. struct vm_area_struct *prev, *next;
  73. next = vma->vm_next;
  74. prev = vma->vm_prev;
  75. if (prev)
  76. prev->vm_next = next;
  77. else // 当前vma是链表中的第一个vma
  78. mm->vmas = next;
  79. if (next)
  80. next->vm_prev = prev;
  81. }
  82. /**
  83. * @brief 查找第一个符合“addr < vm_end”条件的vma
  84. *
  85. * @param mm 内存空间分布结构体
  86. * @param addr 虚拟地址
  87. * @return struct vm_area_struct* 符合条件的vma
  88. */
  89. struct vm_area_struct *vma_find(struct mm_struct *mm, uint64_t addr)
  90. {
  91. struct vm_area_struct *vma = mm->vmas;
  92. struct vm_area_struct *result = NULL;
  93. while (vma != NULL)
  94. {
  95. if (vma->vm_end > addr)
  96. {
  97. result = vma;
  98. break;
  99. }
  100. vma = vma->vm_next;
  101. }
  102. return result;
  103. }
  104. /**
  105. * @brief 插入vma
  106. *
  107. * @param mm
  108. * @param vma
  109. * @return int
  110. */
  111. int vma_insert(struct mm_struct *mm, struct vm_area_struct *vma)
  112. {
  113. struct vm_area_struct *prev;
  114. prev = vma_find(mm, vma->vm_start);
  115. if (prev && prev->vm_start == vma->vm_start && prev->vm_end == vma->vm_end)
  116. {
  117. // 已经存在了相同的vma
  118. return -EEXIST;
  119. }
  120. else if (prev && (prev->vm_start == vma->vm_start || prev->vm_end == vma->vm_end)) // 暂时不支持扩展vma
  121. {
  122. kwarn("Not support: expand vma");
  123. return -ENOTSUP;
  124. }
  125. prev = vma_find(mm, vma->vm_end);
  126. if (prev)
  127. prev = prev->vm_prev;
  128. if (prev == NULL) // 要将当前vma插入到链表的尾部
  129. {
  130. struct vm_area_struct * ptr = mm->vmas;
  131. while(ptr)
  132. {
  133. if(ptr->vm_next)
  134. ptr = ptr->vm_next;
  135. else
  136. {
  137. prev = ptr;
  138. break;
  139. }
  140. }
  141. }
  142. __vma_link_list(mm, vma, prev);
  143. return 0;
  144. }