mm.hpp 8.9 KB

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