mm.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #include <kernel/mem.h>
  3. #include <kernel/vfs.hpp>
  4. #include <types/allocator.hpp>
  5. #include <types/list.hpp>
  6. #include <types/types.h>
  7. #include <types/vector.hpp>
  8. constexpr size_t THREAD_KERNEL_STACK_SIZE = 2 * PAGE_SIZE;
  9. struct page_attr {
  10. uint32_t cow : 1;
  11. };
  12. struct page {
  13. page_t phys_page_id;
  14. page_table_entry* pte;
  15. size_t* ref_count;
  16. struct page_attr attr;
  17. };
  18. using page_arr = types::vector<page, types::kernel_ident_allocator>;
  19. struct mm_attr {
  20. uint32_t read : 1;
  21. uint32_t write : 1;
  22. uint32_t system : 1;
  23. };
  24. class mm {
  25. public:
  26. linr_ptr_t start;
  27. struct mm_attr attr;
  28. page_directory_entry* pd;
  29. page_arr* pgs;
  30. fs::inode* mapped_file;
  31. size_t file_offset;
  32. public:
  33. mm(const mm& val);
  34. mm(linr_ptr_t start, page_directory_entry* pd, bool write, bool system);
  35. };
  36. using mm_list = types::list<mm, types::kernel_ident_allocator>;
  37. // in mem.cpp
  38. extern mm_list* kernel_mms;
  39. extern page empty_page;
  40. // translate physical address to virtual(mapped) address
  41. void* p_ptr_to_v_ptr(phys_ptr_t p_ptr);
  42. // translate linear address to physical address
  43. phys_ptr_t l_ptr_to_p_ptr(const mm_list* mms, linr_ptr_t v_ptr);
  44. // translate virtual(mapped) address to physical address
  45. phys_ptr_t v_ptr_to_p_ptr(void* v_ptr);
  46. // @return the pointer to the mm_area containing l_ptr
  47. // nullptr if not
  48. mm* find_mm_area(mm_list* mms, linr_ptr_t l_ptr);
  49. // find the corresponding page the l_ptr pointing to
  50. // @return the pointer to the struct if found, NULL if not found
  51. struct page* find_page_by_l_ptr(const mm_list* mms, linr_ptr_t l_ptr);
  52. static inline page_t phys_addr_to_page(phys_ptr_t ptr)
  53. {
  54. return ptr >> 12;
  55. }
  56. static inline pd_i_t page_to_pd_i(page_t p)
  57. {
  58. return p >> 10;
  59. }
  60. static inline pt_i_t page_to_pt_i(page_t p)
  61. {
  62. return p & (1024 - 1);
  63. }
  64. static inline phys_ptr_t page_to_phys_addr(page_t p)
  65. {
  66. return p << 12;
  67. }
  68. static inline pd_i_t linr_addr_to_pd_i(linr_ptr_t ptr)
  69. {
  70. return page_to_pd_i(phys_addr_to_page(ptr));
  71. }
  72. static inline pd_i_t linr_addr_to_pt_i(linr_ptr_t ptr)
  73. {
  74. return page_to_pt_i(phys_addr_to_page(ptr));
  75. }
  76. static inline page_directory_entry* mms_get_pd(const mm_list* mms)
  77. {
  78. return mms->begin()->pd;
  79. }
  80. // map the page to the end of the mm_area in pd
  81. int k_map(
  82. mm* mm_area,
  83. page* page,
  84. int read,
  85. int write,
  86. int priv,
  87. int cow);
  88. // @param len is aligned to 4kb boundary automatically, exceeding part will
  89. // be filled with '0's and not written back to the file
  90. int mmap(
  91. void* hint,
  92. size_t len,
  93. fs::inode* file,
  94. size_t offset,
  95. int write,
  96. int priv);
  97. // allocate a raw page
  98. page_t alloc_raw_page(void);
  99. // allocate n raw page(s)
  100. // @return the id of the first page allocated
  101. page_t alloc_n_raw_pages(size_t n);
  102. // allocate a struct page together with the raw page
  103. struct page allocate_page(void);
  104. page_directory_entry* alloc_pd(void);
  105. page_table_entry* alloc_pt(void);