mm.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #pragma once
  2. #include <set>
  3. #include <vector>
  4. #include <bit>
  5. #include <cstddef>
  6. #include <utility>
  7. #include <kernel/mem.h>
  8. #include <kernel/vfs.hpp>
  9. #include <stdint.h>
  10. #include <types/allocator.hpp>
  11. #include <types/cplusplus.hpp>
  12. #include <types/size.h>
  13. #include <types/status.h>
  14. #include <types/types.h>
  15. #define invalidate_tlb(addr) asm volatile("invlpg (%0)" \
  16. : \
  17. : "r"(addr) \
  18. : "memory")
  19. constexpr size_t THREAD_KERNEL_STACK_SIZE = 8 * PAGE_SIZE;
  20. constexpr uint32_t PAGE_COW = (1 << 0);
  21. constexpr uint32_t PAGE_MMAP = (1 << 1);
  22. #define PAGE_COW PAGE_COW
  23. #define PAGE_MMAP PAGE_MMAP
  24. struct page {
  25. page_t phys_page_id;
  26. size_t* ref_count;
  27. // 0 :11 : pte_index
  28. // 12:31 : pt_page
  29. uint32_t pg_pteidx;
  30. mutable uint32_t attr;
  31. };
  32. // private memory mapping
  33. // changes won't be neither written back to file nor shared between processes
  34. // TODO: shared mapping
  35. // @param len is aligned to 4kb boundary automatically, exceeding part will
  36. // be filled with '0's and not written back to the file
  37. // @param offset MUST be aligned to 4kb
  38. int mmap(
  39. void* hint,
  40. size_t len,
  41. fs::inode* file,
  42. size_t offset,
  43. int write,
  44. int priv);
  45. template <uint32_t base, uint32_t expo>
  46. constexpr uint32_t pow()
  47. {
  48. if constexpr (expo == 0)
  49. return 1;
  50. if constexpr (expo == 1)
  51. return base;
  52. if constexpr (expo % 2 == 0)
  53. return pow<base, expo / 2>() * pow<base, expo / 2>();
  54. else
  55. return pow<base, expo / 2>() * pow<base, expo / 2 + 1>();
  56. }
  57. template <int N>
  58. constexpr uint32_t align_down(uint32_t v)
  59. {
  60. return v & ~(pow<2, N>() - 1);
  61. }
  62. template <int N>
  63. constexpr void* align_down(void* v)
  64. {
  65. return std::bit_cast<void*>(align_down<N>(std::bit_cast<uint32_t>(v)));
  66. }
  67. template <int N>
  68. constexpr uint32_t align_up(uint32_t v)
  69. {
  70. return align_down<N>(v + pow<2, N>() - 1);
  71. }
  72. template <int N>
  73. constexpr void* align_up(void* v)
  74. {
  75. return std::bit_cast<void*>(align_up<N>(std::bit_cast<uint32_t>(v)));
  76. }
  77. constexpr size_t vptrdiff(void* p1, void* p2)
  78. {
  79. auto* _p1 = static_cast<std::byte*>(p1);
  80. auto* _p2 = static_cast<std::byte*>(p2);
  81. return _p1 - _p2;
  82. }
  83. constexpr void* vptradd(void* p, std::size_t off)
  84. {
  85. auto* _p = static_cast<std::byte*>(p);
  86. return _p + off;
  87. }
  88. void dealloc_pd(page_t pd);
  89. // allocate a struct page together with the raw page
  90. page allocate_page(void);
  91. void free_page(page* pg);
  92. // TODO: this is for alloc_kstack()
  93. // CHANGE THIS
  94. page_t __alloc_raw_page(void);
  95. void __free_raw_page(page_t pg);
  96. namespace kernel {
  97. void* pmap(page_t pg, bool cached = true);
  98. void pfree(page_t pg);
  99. class paccess : public types::non_copyable {
  100. private:
  101. page_t m_pg;
  102. void* m_ptr;
  103. public:
  104. paccess(void) = delete;
  105. paccess(paccess&&) = delete;
  106. paccess& operator=(paccess&&) = delete;
  107. inline explicit paccess(page_t pg, bool cached = true)
  108. : m_pg(pg)
  109. {
  110. m_ptr = pmap(pg, cached);
  111. }
  112. constexpr void* ptr(void) const { return m_ptr; }
  113. ~paccess()
  114. {
  115. pfree(m_pg);
  116. }
  117. };
  118. namespace memory {
  119. struct mm {
  120. public:
  121. using pages_vector = std::vector<page, types::memory::ident_allocator<page>>;
  122. public:
  123. void* start {};
  124. struct mm_attr {
  125. uint32_t write : 1;
  126. uint32_t system : 1;
  127. uint32_t mapped : 1;
  128. } attr {};
  129. pages_vector* pgs {};
  130. fs::inode* mapped_file {};
  131. size_t file_offset {};
  132. public:
  133. constexpr void* end() const noexcept
  134. { return vptradd(start, pgs->size() * PAGE_SIZE); }
  135. constexpr bool is_kernel_space() const noexcept
  136. { return attr.system; }
  137. constexpr bool is_avail(void* ostart, void* oend) const noexcept
  138. {
  139. void* m_start = start;
  140. void* m_end = end();
  141. return (ostart >= m_end || oend <= m_start);
  142. }
  143. void append_page(pd_t pd, const page& pg, uint32_t attr, bool priv);
  144. /**
  145. * @brief Splits the memory block at the specified address.
  146. *
  147. * @param addr The address at which the memory block will be split.
  148. * @return The new memory block created after splitting.
  149. */
  150. mm split(void* addr);
  151. constexpr bool operator<(const mm& rhs) const noexcept
  152. { return end() <= rhs.start; }
  153. constexpr bool operator<(void* rhs) const noexcept
  154. { return end() <= rhs; }
  155. friend constexpr bool operator<(void* lhs, const mm& rhs) noexcept
  156. { return lhs < rhs.start; }
  157. };
  158. class mm_list {
  159. private:
  160. struct comparator {
  161. constexpr bool operator()(const mm& lhs, const mm& rhs) const noexcept
  162. { return lhs < rhs; }
  163. constexpr bool operator()(const mm& lhs, void* rhs) const noexcept
  164. { return lhs < rhs; }
  165. constexpr bool operator()(void* lhs, const mm& rhs) const noexcept
  166. { return lhs < rhs; }
  167. };
  168. public:
  169. using list_type = std::set<mm, comparator, types::memory::ident_allocator<mm>>;
  170. using iterator = list_type::iterator;
  171. using const_iterator = list_type::const_iterator;
  172. public:
  173. static inline mm_list* s_kernel_mms;
  174. private:
  175. list_type m_areas;
  176. page_t m_pd;
  177. mm* m_brk {};
  178. public:
  179. // for system initialization only
  180. explicit constexpr mm_list(page_t pd)
  181. : m_pd(pd) { }
  182. // default constructor copies kernel_mms
  183. explicit mm_list();
  184. // copies kernel_mms and mirrors user space
  185. explicit mm_list(const mm_list& other);
  186. constexpr mm_list(mm_list&& v)
  187. : m_areas(std::move(v.m_areas))
  188. , m_pd(std::exchange(v.m_pd, 0)) { }
  189. ~mm_list();
  190. void switch_pd() const;
  191. int register_brk(void* addr);
  192. void* set_brk(void* addr);
  193. void* find_avail(void* hint, size_t len, bool priv) const;
  194. int unmap(void* start, size_t len, bool priv);
  195. constexpr mm& addarea(void* start, bool w, bool system)
  196. {
  197. auto [ iter, inserted ] = m_areas.emplace(mm {
  198. .start = start,
  199. .attr {
  200. .write = w,
  201. .system = system,
  202. .mapped = 0,
  203. },
  204. .pgs = types::memory::kinew<mm::pages_vector>(),
  205. });
  206. assert(inserted);
  207. return *iter;
  208. }
  209. mm& add_empty_area(void* start, std::size_t page_count,
  210. uint32_t page_attr, bool w, bool system);
  211. constexpr void clear_user()
  212. {
  213. for (auto iter = m_areas.begin(); iter != m_areas.end(); ) {
  214. if (iter->is_kernel_space()) {
  215. ++iter;
  216. continue;
  217. }
  218. this->unmap(*iter);
  219. iter = m_areas.erase(iter);
  220. }
  221. m_brk = nullptr;
  222. }
  223. inline void unmap(mm& area)
  224. {
  225. int i = 0;
  226. // TODO:
  227. // if there are more than 4 pages, calling invlpg
  228. // should be faster. otherwise, we use movl cr3
  229. // bool should_invlpg = (area->pgs->size() > 4);
  230. for (auto& pg : *area.pgs) {
  231. kernel::paccess pa(pg.pg_pteidx >> 12);
  232. auto pt = (pt_t)pa.ptr();
  233. assert(pt);
  234. auto* pte = *pt + (pg.pg_pteidx & 0xfff);
  235. pte->v = 0;
  236. free_page(&pg);
  237. invalidate_tlb((uint32_t)area.start + (i++) * PAGE_SIZE);
  238. }
  239. types::memory::kidelete<mm::pages_vector>(area.pgs);
  240. }
  241. constexpr mm* find(void* lp)
  242. {
  243. auto iter = m_areas.find(lp);
  244. if (iter == m_areas.end())
  245. return nullptr;
  246. return &*iter;
  247. }
  248. constexpr const mm* find(void* lp) const
  249. {
  250. auto iter = m_areas.find(lp);
  251. if (iter == m_areas.end())
  252. return nullptr;
  253. return &*iter;
  254. }
  255. constexpr bool is_avail(void* start, size_t len) const noexcept
  256. {
  257. start = align_down<12>(start);
  258. len = vptrdiff(align_up<12>(vptradd(start, len)), start);
  259. for (const auto& area : m_areas) {
  260. if (!area.is_avail(start, vptradd(start, len)))
  261. return false;
  262. }
  263. return true;
  264. }
  265. constexpr bool is_avail(void* addr) const
  266. {
  267. auto iter = m_areas.find(addr);
  268. return iter == m_areas.end();
  269. }
  270. };
  271. } // namespace memory
  272. } // namespace kernel
  273. // global variables
  274. inline page empty_page;
  275. // --------------------------------
  276. // inline constexpr page* lto_page(mm* mm_area, void* l_ptr)
  277. // {
  278. // size_t offset = vptrdiff(l_ptr, mm_area->start);
  279. // return &mm_area->pgs->at(offset / PAGE_SIZE);
  280. // }
  281. // inline constexpr page_t to_page(pptr_t ptr)
  282. // {
  283. // return ptr >> 12;
  284. // }
  285. // inline constexpr size_t to_pdi(page_t pg)
  286. // {
  287. // return pg >> 10;
  288. // }
  289. // inline constexpr size_t to_pti(page_t pg)
  290. // {
  291. // return pg & (1024 - 1);
  292. // }
  293. // inline constexpr pptr_t to_pp(page_t p)
  294. // {
  295. // return p << 12;
  296. // }
  297. constexpr size_t v_to_pdi(void* addr)
  298. {
  299. return std::bit_cast<uint32_t>(addr) >> 22;
  300. }
  301. constexpr size_t v_to_pti(void* addr)
  302. {
  303. return (std::bit_cast<uint32_t>(addr) >> 12) & 0x3ff;
  304. }
  305. // inline constexpr pte_t* to_pte(pt_t pt, page_t pg)
  306. // {
  307. // return *pt + to_pti(pg);
  308. // }
  309. // inline void* to_vp(page_t pg)
  310. // {
  311. // return ptovp(to_pp(pg));
  312. // }
  313. // inline pd_t to_pd(page_t pg)
  314. // {
  315. // return reinterpret_cast<pd_t>(to_vp(pg));
  316. // }
  317. // inline pt_t to_pt(page_t pg)
  318. // {
  319. // return reinterpret_cast<pt_t>(to_vp(pg));
  320. // }
  321. // inline pt_t to_pt(pde_t* pde)
  322. // {
  323. // return to_pt(pde->in.pt_page);
  324. // }
  325. // inline pde_t* to_pde(pd_t pd, void* addr)
  326. // {
  327. // return *pd + lto_pdi((pptr_t)addr);
  328. // }
  329. // inline pte_t* to_pte(pt_t pt, void* addr)
  330. // {
  331. // return *pt + lto_pti((pptr_t)addr);
  332. // }
  333. // inline pte_t* to_pte(pde_t* pde, void* addr)
  334. // {
  335. // return to_pte(to_pt(pde), addr);
  336. // }
  337. // inline pte_t* to_pte(pd_t pd, void* addr)
  338. // {
  339. // return to_pte(to_pde(pd, addr), addr);
  340. // }
  341. // inline pte_t* to_pte(pde_t* pde, page_t pg)
  342. // {
  343. // return to_pte(to_pt(pde), pg);
  344. // }