mm.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #pragma once
  2. #include <bit>
  3. #include <cstddef>
  4. #include <utility>
  5. #include <kernel/mem.h>
  6. #include <kernel/vfs.hpp>
  7. #include <stdint.h>
  8. #include <types/allocator.hpp>
  9. #include <types/cplusplus.hpp>
  10. #include <types/list.hpp>
  11. #include <types/size.h>
  12. #include <types/status.h>
  13. #include <types/types.h>
  14. #include <types/vector.hpp>
  15. #define invalidate_tlb(addr) asm("invlpg (%0)" \
  16. : \
  17. : "r"(addr) \
  18. : "memory")
  19. constexpr size_t THREAD_KERNEL_STACK_SIZE = 2 * 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. 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. using page_arr = types::vector<page, types::kernel_ident_allocator>;
  46. // forward declaration
  47. namespace kernel {
  48. class mm_list;
  49. } // namespace kernel
  50. template <uint32_t base, uint32_t expo>
  51. constexpr uint32_t pow()
  52. {
  53. if constexpr (expo == 0)
  54. return 1;
  55. if constexpr (expo == 1)
  56. return base;
  57. if constexpr (expo % 2 == 0)
  58. return pow<base, expo / 2>() * pow<base, expo / 2>();
  59. else
  60. return pow<base, expo / 2>() * pow<base, expo / 2 + 1>();
  61. }
  62. template <int N>
  63. constexpr uint32_t align_down(uint32_t v)
  64. {
  65. return v & ~(pow<2, N>() - 1);
  66. }
  67. template <int N>
  68. constexpr void* align_down(void* v)
  69. {
  70. return std::bit_cast<void*>(align_down<N>(std::bit_cast<uint32_t>(v)));
  71. }
  72. template <int N>
  73. constexpr uint32_t align_up(uint32_t v)
  74. {
  75. return align_down<N>(v + pow<2, N>() - 1);
  76. }
  77. template <int N>
  78. constexpr void* align_up(void* v)
  79. {
  80. return std::bit_cast<void*>(align_up<N>(std::bit_cast<uint32_t>(v)));
  81. }
  82. constexpr size_t vptrdiff(void* p1, void* p2)
  83. {
  84. auto* _p1 = static_cast<std::byte*>(p1);
  85. auto* _p2 = static_cast<std::byte*>(p2);
  86. return _p1 - _p2;
  87. }
  88. constexpr void* vptradd(void* p, std::size_t off)
  89. {
  90. auto* _p = static_cast<std::byte*>(p);
  91. return _p + off;
  92. }
  93. void dealloc_pd(page_t pd);
  94. // allocate a struct page together with the raw page
  95. page allocate_page(void);
  96. void free_page(page* pg);
  97. // TODO: this is for alloc_kstack()
  98. // CHANGE THIS
  99. page_t __alloc_raw_page(void);
  100. void __free_raw_page(page_t pg);
  101. struct mm {
  102. public:
  103. void* start;
  104. union {
  105. uint32_t v;
  106. struct {
  107. uint32_t read : 1;
  108. uint32_t write : 1;
  109. uint32_t system : 1;
  110. } in;
  111. } attr;
  112. kernel::mm_list* owner;
  113. page_arr* pgs = nullptr;
  114. fs::inode* mapped_file = nullptr;
  115. size_t file_offset = 0;
  116. public:
  117. constexpr void* end(void) const
  118. {
  119. return (char*)this->start + this->pgs->size() * PAGE_SIZE;
  120. }
  121. constexpr bool is_kernel_space(void) const
  122. {
  123. return this->start >= std::bit_cast<void*>(0xc0000000);
  124. }
  125. constexpr bool is_avail(void* start, void* end) const
  126. {
  127. void* m_start = this->start;
  128. void* m_end = this->end();
  129. return (start >= m_end || end <= m_start);
  130. }
  131. int append_page(page& pg, uint32_t attr, bool priv);
  132. };
  133. namespace kernel {
  134. void* pmap(page_t pg);
  135. void pfree(page_t pg);
  136. class paccess : public types::non_copyable {
  137. private:
  138. page_t m_pg;
  139. void* m_ptr;
  140. public:
  141. paccess(void) = delete;
  142. paccess(paccess&&) = delete;
  143. paccess& operator=(paccess&&) = delete;
  144. constexpr explicit paccess(page_t pg)
  145. : m_pg(pg)
  146. {
  147. m_ptr = pmap(pg);
  148. }
  149. constexpr void* ptr(void) const
  150. {
  151. return m_ptr;
  152. }
  153. ~paccess()
  154. {
  155. pfree(m_pg);
  156. }
  157. };
  158. class mm_list {
  159. public:
  160. using list_type = ::types::list<mm, types::kernel_ident_allocator>;
  161. using iterator_type = list_type::iterator_type;
  162. using const_iterator_type = list_type::const_iterator_type;
  163. private:
  164. list_type m_areas;
  165. public:
  166. page_t m_pd;
  167. public:
  168. explicit constexpr mm_list(page_t pd)
  169. : m_pd(pd)
  170. {
  171. }
  172. mm_list(const mm_list& v);
  173. constexpr mm_list(mm_list&& v)
  174. : m_areas(std::move(v.m_areas))
  175. , m_pd(v.m_pd)
  176. {
  177. v.m_pd = 0;
  178. for (auto& area : m_areas)
  179. area.owner = this;
  180. }
  181. ~mm_list()
  182. {
  183. if (!m_pd)
  184. return;
  185. this->clear_user();
  186. dealloc_pd(m_pd);
  187. }
  188. constexpr iterator_type begin(void)
  189. {
  190. return m_areas.begin();
  191. }
  192. constexpr iterator_type end(void)
  193. {
  194. return m_areas.end();
  195. }
  196. constexpr const_iterator_type begin(void) const
  197. {
  198. return m_areas.begin();
  199. }
  200. constexpr const_iterator_type end(void) const
  201. {
  202. return m_areas.end();
  203. }
  204. constexpr const_iterator_type cbegin(void) const
  205. {
  206. return m_areas.cbegin();
  207. }
  208. constexpr const_iterator_type cend(void) const
  209. {
  210. return m_areas.cend();
  211. }
  212. constexpr iterator_type addarea(void* start, bool w, bool system)
  213. {
  214. return m_areas.emplace_back(mm {
  215. .start = start,
  216. .attr {
  217. .in {
  218. .read = 1,
  219. .write = w,
  220. .system = system,
  221. },
  222. },
  223. .owner = this,
  224. .pgs = types::_new<types::kernel_ident_allocator, page_arr>(),
  225. });
  226. }
  227. constexpr void clear_user()
  228. {
  229. for (auto iter = this->begin(); iter != this->end();) {
  230. if (iter->is_kernel_space()) {
  231. ++iter;
  232. continue;
  233. }
  234. this->unmap(iter);
  235. iter = m_areas.erase(iter);
  236. }
  237. }
  238. constexpr int mirror_area(mm& src)
  239. {
  240. auto area = this->addarea(
  241. src.start, src.attr.in.write, src.attr.in.system);
  242. if (src.mapped_file) {
  243. area->mapped_file = src.mapped_file;
  244. area->file_offset = src.file_offset;
  245. }
  246. for (auto& pg : *src.pgs) {
  247. if (area->append_page(pg,
  248. PAGE_COW | (pg.attr & PAGE_MMAP),
  249. src.attr.in.system)
  250. != GB_OK) {
  251. return GB_FAILED;
  252. }
  253. }
  254. return GB_OK;
  255. }
  256. inline void unmap(iterator_type area)
  257. {
  258. int i = 0;
  259. // TODO:
  260. // if there are more than 4 pages, calling invlpg
  261. // should be faster. otherwise, we use movl cr3
  262. // bool should_invlpg = (area->pgs->size() > 4);
  263. for (auto& pg : *area->pgs) {
  264. kernel::paccess pa(pg.pg_pteidx >> 12);
  265. auto pt = (pt_t)pa.ptr();
  266. assert(pt);
  267. auto* pte = *pt + (pg.pg_pteidx & 0xfff);
  268. pte->v = 0;
  269. free_page(&pg);
  270. invalidate_tlb((uint32_t)area->start + (i++) * PAGE_SIZE);
  271. }
  272. types::pdelete<types::kernel_ident_allocator>(area->pgs);
  273. area->attr.v = 0;
  274. area->start = 0;
  275. }
  276. constexpr iterator_type find(void* lp)
  277. {
  278. for (auto iter = this->begin(); iter != this->end(); ++iter)
  279. if (lp >= iter->start && lp < iter->end())
  280. return iter;
  281. return this->end();
  282. }
  283. constexpr bool is_avail(void* start, size_t len)
  284. {
  285. start = align_down<12>(start);
  286. len = vptrdiff(align_up<12>(vptradd(start, len)), start);
  287. for (const auto& area : *this) {
  288. if (!area.is_avail(start, (char*)start + len))
  289. return false;
  290. }
  291. return true;
  292. }
  293. };
  294. } // namespace kernel
  295. // global variables
  296. inline kernel::mm_list* kernel_mms;
  297. inline page empty_page;
  298. // --------------------------------
  299. // inline constexpr page* lto_page(mm* mm_area, void* l_ptr)
  300. // {
  301. // size_t offset = vptrdiff(l_ptr, mm_area->start);
  302. // return &mm_area->pgs->at(offset / PAGE_SIZE);
  303. // }
  304. // inline constexpr page_t to_page(pptr_t ptr)
  305. // {
  306. // return ptr >> 12;
  307. // }
  308. // inline constexpr size_t to_pdi(page_t pg)
  309. // {
  310. // return pg >> 10;
  311. // }
  312. // inline constexpr size_t to_pti(page_t pg)
  313. // {
  314. // return pg & (1024 - 1);
  315. // }
  316. // inline constexpr pptr_t to_pp(page_t p)
  317. // {
  318. // return p << 12;
  319. // }
  320. constexpr size_t v_to_pdi(void* addr)
  321. {
  322. return std::bit_cast<uint32_t>(addr) >> 22;
  323. }
  324. constexpr size_t v_to_pti(void* addr)
  325. {
  326. return (std::bit_cast<uint32_t>(addr) >> 12) & 0x3ff;
  327. }
  328. // inline constexpr pte_t* to_pte(pt_t pt, page_t pg)
  329. // {
  330. // return *pt + to_pti(pg);
  331. // }
  332. // inline void* to_vp(page_t pg)
  333. // {
  334. // return ptovp(to_pp(pg));
  335. // }
  336. // inline pd_t to_pd(page_t pg)
  337. // {
  338. // return reinterpret_cast<pd_t>(to_vp(pg));
  339. // }
  340. // inline pt_t to_pt(page_t pg)
  341. // {
  342. // return reinterpret_cast<pt_t>(to_vp(pg));
  343. // }
  344. // inline pt_t to_pt(pde_t* pde)
  345. // {
  346. // return to_pt(pde->in.pt_page);
  347. // }
  348. // inline pde_t* to_pde(pd_t pd, void* addr)
  349. // {
  350. // return *pd + lto_pdi((pptr_t)addr);
  351. // }
  352. // inline pte_t* to_pte(pt_t pt, void* addr)
  353. // {
  354. // return *pt + lto_pti((pptr_t)addr);
  355. // }
  356. // inline pte_t* to_pte(pde_t* pde, void* addr)
  357. // {
  358. // return to_pte(to_pt(pde), addr);
  359. // }
  360. // inline pte_t* to_pte(pd_t pd, void* addr)
  361. // {
  362. // return to_pte(to_pde(pd, addr), addr);
  363. // }
  364. // inline pte_t* to_pte(pde_t* pde, page_t pg)
  365. // {
  366. // return to_pte(to_pt(pde), pg);
  367. // }