《操作系统》的实验代码。
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.

112 lines
3.5 KiB

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