process.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #pragma once
  2. #include "types/map.hpp"
  3. #include "types/pair.hpp"
  4. #include <kernel/event/evtqueue.hpp>
  5. #include <kernel/interrupt.h>
  6. #include <kernel/mm.hpp>
  7. #include <kernel/task.h>
  8. #include <types/cplusplus.hpp>
  9. #include <types/hash_map.hpp>
  10. #include <types/list.hpp>
  11. #include <types/stdint.h>
  12. #include <types/types.h>
  13. typedef size_t pid_t;
  14. class process;
  15. struct thread;
  16. class proclist;
  17. class readyqueue;
  18. inline process* volatile current_process;
  19. inline thread* volatile current_thread;
  20. inline proclist* procs;
  21. inline readyqueue* readythds;
  22. struct process_attr {
  23. uint16_t system : 1;
  24. uint16_t zombie : 1 = 0;
  25. };
  26. struct thread_attr {
  27. uint32_t system : 1;
  28. uint32_t ready : 1;
  29. uint32_t wait : 1;
  30. };
  31. struct thread {
  32. private:
  33. inline void alloc_kstack(void)
  34. {
  35. // TODO: alloc low mem
  36. kstack = to_pp(alloc_n_raw_pages(2));
  37. kstack += THREAD_KERNEL_STACK_SIZE;
  38. esp = reinterpret_cast<uint32_t*>(kstack);
  39. }
  40. public:
  41. uint32_t* esp;
  42. pptr_t kstack;
  43. process* owner;
  44. thread_attr attr;
  45. explicit inline thread(process* _owner, bool system)
  46. : owner { _owner }
  47. , attr {
  48. .system = system,
  49. .ready = 1,
  50. .wait = 0,
  51. }
  52. {
  53. alloc_kstack();
  54. }
  55. constexpr thread(thread&& val)
  56. : esp { val.esp }
  57. , kstack { val.kstack }
  58. , owner { val.owner }
  59. , attr { val.attr }
  60. {
  61. val.attr = {};
  62. val.esp = 0;
  63. val.kstack = 0;
  64. val.owner = nullptr;
  65. }
  66. inline thread(const thread& val)
  67. : owner { val.owner }
  68. , attr { val.attr }
  69. {
  70. alloc_kstack();
  71. }
  72. inline thread(const thread& thd, process* new_parent)
  73. : thread { thd }
  74. {
  75. owner = new_parent;
  76. }
  77. constexpr ~thread()
  78. {
  79. if (kstack)
  80. free_n_raw_pages(to_page(kstack), 2);
  81. }
  82. };
  83. class thdlist {
  84. public:
  85. using list_type = types::list<thread>;
  86. private:
  87. list_type thds;
  88. public:
  89. constexpr thdlist(const thdlist& obj) = delete;
  90. constexpr thdlist(thdlist&& obj) = delete;
  91. constexpr thdlist& operator=(const thdlist& obj) = delete;
  92. constexpr thdlist& operator=(thdlist&& obj) = delete;
  93. constexpr thdlist(thdlist&& obj, process* new_parent)
  94. : thds { types::move(obj.thds) }
  95. {
  96. for (auto& thd : thds)
  97. thd.owner = new_parent;
  98. }
  99. explicit constexpr thdlist(void)
  100. {
  101. }
  102. // implementation is below
  103. constexpr ~thdlist();
  104. template <typename... Args>
  105. constexpr thread& Emplace(Args&&... args)
  106. {
  107. return *thds.emplace_back(types::forward<Args>(args)...);
  108. }
  109. constexpr size_t size(void) const
  110. {
  111. return thds.size();
  112. }
  113. constexpr list_type& underlying_list(void)
  114. {
  115. return thds;
  116. }
  117. };
  118. class process {
  119. public:
  120. mutable kernel::mm_list mms;
  121. thdlist thds;
  122. kernel::evtqueue wait_lst;
  123. process_attr attr;
  124. pid_t pid;
  125. pid_t ppid;
  126. public:
  127. process(process&& val);
  128. process(const process&);
  129. explicit process(pid_t ppid, bool system = true);
  130. constexpr bool is_system(void) const
  131. {
  132. return attr.system;
  133. }
  134. constexpr bool is_zombie(void) const
  135. {
  136. return attr.zombie;
  137. }
  138. private:
  139. static inline pid_t max_pid;
  140. static inline pid_t alloc_pid(void)
  141. {
  142. return ++max_pid;
  143. }
  144. };
  145. class proclist final {
  146. public:
  147. using list_type = types::map<pid_t, process>;
  148. using child_index_type = types::hash_map<pid_t, types::list<pid_t>, types::linux_hasher<pid_t>>;
  149. using iterator_type = list_type::iterator_type;
  150. using const_iterator_type = list_type::const_iterator_type;
  151. private:
  152. list_type m_procs;
  153. child_index_type m_child_idx;
  154. public:
  155. template <typename... Args>
  156. iterator_type emplace(Args&&... args)
  157. {
  158. process _proc(types::forward<Args>(args)...);
  159. auto pid = _proc.pid;
  160. auto ppid = _proc.ppid;
  161. auto iter = m_procs.insert(types::make_pair(pid, types::move(_proc)));
  162. auto children = m_child_idx.find(ppid);
  163. if (!children) {
  164. m_child_idx.emplace(ppid, types::list<pid_t> {});
  165. children = m_child_idx.find(ppid);
  166. }
  167. children->value.push_back(pid);
  168. return iter;
  169. }
  170. constexpr void remove(pid_t pid)
  171. {
  172. make_children_orphans(pid);
  173. auto proc_iter = m_procs.find(pid);
  174. auto ppid = proc_iter->value.ppid;
  175. auto& parent_children = m_child_idx.find(ppid)->value;
  176. auto i = parent_children.find(pid);
  177. parent_children.erase(i);
  178. m_procs.erase(proc_iter);
  179. }
  180. constexpr process* find(pid_t pid)
  181. {
  182. return &m_procs.find(pid)->value;
  183. }
  184. constexpr bool has_child(pid_t pid)
  185. {
  186. auto children = m_child_idx.find(pid);
  187. return children && !children->value.empty();
  188. }
  189. constexpr void make_children_orphans(pid_t pid)
  190. {
  191. auto children = m_child_idx.find(pid);
  192. if (children) {
  193. auto init_children = m_child_idx.find(1);
  194. for (auto iter = children->value.begin(); iter != children->value.end(); ++iter) {
  195. init_children->value.push_back(*iter);
  196. this->find(*iter)->ppid = 1;
  197. }
  198. m_child_idx.remove(children);
  199. }
  200. }
  201. };
  202. class readyqueue final {
  203. public:
  204. using list_type = types::list<thread*>;
  205. using iterator_type = list_type::iterator_type;
  206. using const_iterator_type = list_type::const_iterator_type;
  207. private:
  208. list_type m_thds;
  209. private:
  210. readyqueue(const readyqueue&) = delete;
  211. readyqueue(readyqueue&&) = delete;
  212. readyqueue& operator=(const readyqueue&) = delete;
  213. readyqueue& operator=(readyqueue&&) = delete;
  214. ~readyqueue() = delete;
  215. public:
  216. constexpr explicit readyqueue(void) = default;
  217. constexpr void push(thread* thd)
  218. {
  219. m_thds.push_back(thd);
  220. }
  221. constexpr thread* pop(void)
  222. {
  223. auto iter = m_thds.begin();
  224. while (!((*iter)->attr.ready))
  225. iter = m_thds.erase(iter);
  226. auto* ptr = *iter;
  227. m_thds.erase(iter);
  228. return ptr;
  229. }
  230. constexpr thread* query(void)
  231. {
  232. auto* thd = this->pop();
  233. this->push(thd);
  234. return thd;
  235. }
  236. constexpr void remove_all(thread* thd)
  237. {
  238. auto iter = m_thds.find(thd);
  239. while (iter != m_thds.end()) {
  240. m_thds.erase(iter);
  241. iter = m_thds.find(thd);
  242. }
  243. }
  244. };
  245. extern "C" void NORETURN init_scheduler();
  246. void schedule(void);
  247. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  248. {
  249. --*stack;
  250. **stack = val;
  251. return val;
  252. }
  253. // class thdlist
  254. constexpr thdlist::~thdlist()
  255. {
  256. for (auto iter = thds.begin(); iter != thds.end(); ++iter)
  257. readythds->remove_all(&iter);
  258. }
  259. void k_new_thread(void (*func)(void*), void* data);