《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.3 KiB

10 years ago
  1. #ifndef __KERN_MM_VMM_H__
  2. #define __KERN_MM_VMM_H__
  3. #include <defs.h>
  4. #include <list.h>
  5. #include <memlayout.h>
  6. #include <sync.h>
  7. #include <proc.h>
  8. #include <sem.h>
  9. //pre define
  10. struct mm_struct;
  11. // the virtual continuous memory area(vma)
  12. struct vma_struct {
  13. struct mm_struct *vm_mm; // the set of vma using the same PDT
  14. uintptr_t vm_start; // start addr of vma
  15. uintptr_t vm_end; // end addr of vma
  16. uint32_t vm_flags; // flags of vma
  17. list_entry_t list_link; // linear list link which sorted by start addr of vma
  18. };
  19. #define le2vma(le, member) \
  20. to_struct((le), struct vma_struct, member)
  21. #define VM_READ 0x00000001
  22. #define VM_WRITE 0x00000002
  23. #define VM_EXEC 0x00000004
  24. #define VM_STACK 0x00000008
  25. // the control struct for a set of vma using the same PDT
  26. struct mm_struct {
  27. list_entry_t mmap_list; // linear list link which sorted by start addr of vma
  28. struct vma_struct *mmap_cache; // current accessed vma, used for speed purpose
  29. pde_t *pgdir; // the PDT of these vma
  30. int map_count; // the count of these vma
  31. void *sm_priv; // the private data for swap manager
  32. int mm_count; // the number ofprocess which shared the mm
  33. semaphore_t mm_sem; // mutex for using dup_mmap fun to duplicat the mm
  34. int locked_by; // the lock owner process's pid
  35. };
  36. struct vma_struct *find_vma(struct mm_struct *mm, uintptr_t addr);
  37. struct vma_struct *vma_create(uintptr_t vm_start, uintptr_t vm_end, uint32_t vm_flags);
  38. void insert_vma_struct(struct mm_struct *mm, struct vma_struct *vma);
  39. struct mm_struct *mm_create(void);
  40. void mm_destroy(struct mm_struct *mm);
  41. void vmm_init(void);
  42. int mm_map(struct mm_struct *mm, uintptr_t addr, size_t len, uint32_t vm_flags,
  43. struct vma_struct **vma_store);
  44. int do_pgfault(struct mm_struct *mm, uint32_t error_code, uintptr_t addr);
  45. int mm_unmap(struct mm_struct *mm, uintptr_t addr, size_t len);
  46. int dup_mmap(struct mm_struct *to, struct mm_struct *from);
  47. void exit_mmap(struct mm_struct *mm);
  48. uintptr_t get_unmapped_area(struct mm_struct *mm, size_t len);
  49. int mm_brk(struct mm_struct *mm, uintptr_t addr, size_t len);
  50. extern volatile unsigned int pgfault_num;
  51. extern struct mm_struct *check_mm_struct;
  52. bool user_mem_check(struct mm_struct *mm, uintptr_t start, size_t len, bool write);
  53. bool copy_from_user(struct mm_struct *mm, void *dst, const void *src, size_t len, bool writable);
  54. bool copy_to_user(struct mm_struct *mm, void *dst, const void *src, size_t len);
  55. static inline int
  56. mm_count(struct mm_struct *mm) {
  57. return mm->mm_count;
  58. }
  59. static inline void
  60. set_mm_count(struct mm_struct *mm, int val) {
  61. mm->mm_count = val;
  62. }
  63. static inline int
  64. mm_count_inc(struct mm_struct *mm) {
  65. mm->mm_count += 1;
  66. return mm->mm_count;
  67. }
  68. static inline int
  69. mm_count_dec(struct mm_struct *mm) {
  70. mm->mm_count -= 1;
  71. return mm->mm_count;
  72. }
  73. static inline void
  74. lock_mm(struct mm_struct *mm) {
  75. if (mm != NULL) {
  76. down(&(mm->mm_sem));
  77. if (current != NULL) {
  78. mm->locked_by = current->pid;
  79. }
  80. }
  81. }
  82. static inline void
  83. unlock_mm(struct mm_struct *mm) {
  84. if (mm != NULL) {
  85. up(&(mm->mm_sem));
  86. mm->locked_by = 0;
  87. }
  88. }
  89. #endif /* !__KERN_MM_VMM_H__ */