process.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. #pragma once
  2. #include <kernel/errno.h>
  3. #include <kernel/event/evtqueue.hpp>
  4. #include <kernel/interrupt.h>
  5. #include <kernel/mm.hpp>
  6. #include <kernel/task.h>
  7. #include <kernel/vfs.hpp>
  8. #include <types/allocator.hpp>
  9. #include <types/cplusplus.hpp>
  10. #include <types/hash_map.hpp>
  11. #include <types/list.hpp>
  12. #include <types/map.hpp>
  13. #include <types/pair.hpp>
  14. #include <types/status.h>
  15. #include <types/stdint.h>
  16. #include <types/types.h>
  17. typedef size_t pid_t;
  18. class process;
  19. struct thread;
  20. class proclist;
  21. class readyqueue;
  22. inline process* volatile current_process;
  23. inline thread* volatile current_thread;
  24. inline proclist* procs;
  25. inline readyqueue* readythds;
  26. struct process_attr {
  27. uint16_t system : 1;
  28. uint16_t zombie : 1 = 0;
  29. };
  30. struct thread_attr {
  31. uint32_t system : 1;
  32. uint32_t ready : 1;
  33. uint32_t wait : 1;
  34. };
  35. struct thread {
  36. private:
  37. inline void alloc_kstack(void)
  38. {
  39. // TODO: alloc low mem
  40. kstack = to_pp(alloc_n_raw_pages(2));
  41. kstack += THREAD_KERNEL_STACK_SIZE;
  42. esp = reinterpret_cast<uint32_t*>(kstack);
  43. }
  44. public:
  45. uint32_t* esp;
  46. pptr_t kstack;
  47. process* owner;
  48. thread_attr attr;
  49. explicit inline thread(process* _owner, bool system)
  50. : owner { _owner }
  51. , attr {
  52. .system = system,
  53. .ready = 1,
  54. .wait = 0,
  55. }
  56. {
  57. alloc_kstack();
  58. }
  59. constexpr thread(thread&& val)
  60. : esp { val.esp }
  61. , kstack { val.kstack }
  62. , owner { val.owner }
  63. , attr { val.attr }
  64. {
  65. val.attr = {};
  66. val.esp = 0;
  67. val.kstack = 0;
  68. val.owner = nullptr;
  69. }
  70. inline thread(const thread& val)
  71. : owner { val.owner }
  72. , attr { val.attr }
  73. {
  74. alloc_kstack();
  75. }
  76. inline thread(const thread& thd, process* new_parent)
  77. : thread { thd }
  78. {
  79. owner = new_parent;
  80. }
  81. constexpr ~thread()
  82. {
  83. if (kstack)
  84. free_n_raw_pages(to_page(kstack), 2);
  85. }
  86. };
  87. class thdlist {
  88. public:
  89. using list_type = types::list<thread>;
  90. private:
  91. list_type thds;
  92. public:
  93. constexpr thdlist(const thdlist& obj) = delete;
  94. constexpr thdlist(thdlist&& obj) = delete;
  95. constexpr thdlist& operator=(const thdlist& obj) = delete;
  96. constexpr thdlist& operator=(thdlist&& obj) = delete;
  97. constexpr thdlist(thdlist&& obj, process* new_parent)
  98. : thds { types::move(obj.thds) }
  99. {
  100. for (auto& thd : thds)
  101. thd.owner = new_parent;
  102. }
  103. explicit constexpr thdlist(void)
  104. {
  105. }
  106. // implementation is below
  107. constexpr ~thdlist();
  108. template <typename... Args>
  109. constexpr thread& Emplace(Args&&... args)
  110. {
  111. return *thds.emplace_back(types::forward<Args>(args)...);
  112. }
  113. constexpr size_t size(void) const
  114. {
  115. return thds.size();
  116. }
  117. constexpr list_type& underlying_list(void)
  118. {
  119. return thds;
  120. }
  121. };
  122. class process {
  123. public:
  124. class filearr {
  125. public:
  126. using container_type = types::list<fs::file>;
  127. using array_type = types::hash_map<int, container_type::iterator_type, types::linux_hasher<size_t>>;
  128. private:
  129. inline static container_type* files = nullptr;
  130. array_type arr;
  131. int next_fd = 0;
  132. public:
  133. constexpr filearr(const filearr&) = delete;
  134. constexpr filearr& operator=(const filearr&) = delete;
  135. constexpr filearr& operator=(filearr&&) = delete;
  136. constexpr filearr(filearr&& val)
  137. : arr { types::move(val.arr) }
  138. , next_fd { val.next_fd }
  139. {
  140. val.next_fd = 0;
  141. }
  142. explicit filearr()
  143. {
  144. if (!files)
  145. files = types::pnew<types::kernel_allocator>(files);
  146. }
  147. constexpr void dup(const filearr& orig)
  148. {
  149. if (this->next_fd)
  150. return;
  151. this->next_fd = orig.next_fd;
  152. for (int i = 0; i < this->next_fd; ++i) {
  153. auto iter = orig.arr.find(i);
  154. if (!iter)
  155. continue;
  156. this->arr.emplace(iter->key, iter->value);
  157. ++iter->value->ref;
  158. }
  159. }
  160. constexpr fs::file* operator[](int i) const
  161. {
  162. auto iter = arr.find(i);
  163. if (!iter)
  164. return nullptr;
  165. else
  166. return &iter->value;
  167. }
  168. // TODO: file opening flags (permissions etc.)
  169. int open(const char* filename, uint32_t)
  170. {
  171. auto* dentry = fs::vfs_open(filename);
  172. if (!dentry) {
  173. errno = ENOTFOUND;
  174. return -1;
  175. }
  176. if (!(dentry->ind->flags.in.file || dentry->ind->flags.in.special_node)) {
  177. errno = EISDIR;
  178. return -1;
  179. }
  180. auto iter = files->emplace_back(fs::file {
  181. fs::file::types::regular_file,
  182. { .ind = dentry->ind },
  183. 0,
  184. 1 });
  185. int fd = next_fd++;
  186. arr.emplace(fd, iter);
  187. return fd;
  188. }
  189. // close file descriptor
  190. // where iter is guaranteed not nullptr
  191. constexpr void close(array_type::iterator_type iter)
  192. {
  193. if (iter->value->ref == 1)
  194. files->erase(iter->value);
  195. else
  196. --iter->value->ref;
  197. }
  198. constexpr void close(int fd)
  199. {
  200. auto iter = arr.find(fd);
  201. if (iter)
  202. close(iter);
  203. }
  204. constexpr ~filearr()
  205. {
  206. for (int i = 0; i < next_fd; ++i)
  207. close(i);
  208. }
  209. };
  210. public:
  211. mutable kernel::mm_list mms;
  212. thdlist thds;
  213. kernel::evtqueue wait_lst;
  214. process_attr attr;
  215. pid_t pid;
  216. pid_t ppid;
  217. filearr files;
  218. public:
  219. process(process&& val);
  220. process(const process&);
  221. explicit process(pid_t ppid, bool system = true);
  222. constexpr bool is_system(void) const
  223. {
  224. return attr.system;
  225. }
  226. constexpr bool is_zombie(void) const
  227. {
  228. return attr.zombie;
  229. }
  230. private:
  231. static inline pid_t max_pid;
  232. static inline pid_t alloc_pid(void)
  233. {
  234. return ++max_pid;
  235. }
  236. };
  237. class proclist final {
  238. public:
  239. using list_type = types::map<pid_t, process>;
  240. using child_index_type = types::hash_map<pid_t, types::list<pid_t>, types::linux_hasher<pid_t>>;
  241. using iterator_type = list_type::iterator_type;
  242. using const_iterator_type = list_type::const_iterator_type;
  243. private:
  244. list_type m_procs;
  245. child_index_type m_child_idx;
  246. public:
  247. template <typename... Args>
  248. iterator_type emplace(Args&&... args)
  249. {
  250. process _proc(types::forward<Args>(args)...);
  251. auto pid = _proc.pid;
  252. auto ppid = _proc.ppid;
  253. auto iter = m_procs.insert(types::make_pair(pid, types::move(_proc)));
  254. auto children = m_child_idx.find(ppid);
  255. if (!children) {
  256. m_child_idx.emplace(ppid, types::list<pid_t> {});
  257. children = m_child_idx.find(ppid);
  258. }
  259. children->value.push_back(pid);
  260. return iter;
  261. }
  262. constexpr void remove(pid_t pid)
  263. {
  264. make_children_orphans(pid);
  265. auto proc_iter = m_procs.find(pid);
  266. auto ppid = proc_iter->value.ppid;
  267. auto& parent_children = m_child_idx.find(ppid)->value;
  268. auto i = parent_children.find(pid);
  269. parent_children.erase(i);
  270. m_procs.erase(proc_iter);
  271. }
  272. constexpr process* find(pid_t pid)
  273. {
  274. return &m_procs.find(pid)->value;
  275. }
  276. constexpr bool has_child(pid_t pid)
  277. {
  278. auto children = m_child_idx.find(pid);
  279. return children && !children->value.empty();
  280. }
  281. constexpr void make_children_orphans(pid_t pid)
  282. {
  283. auto children = m_child_idx.find(pid);
  284. if (children) {
  285. auto init_children = m_child_idx.find(1);
  286. for (auto iter = children->value.begin(); iter != children->value.end(); ++iter) {
  287. init_children->value.push_back(*iter);
  288. this->find(*iter)->ppid = 1;
  289. }
  290. m_child_idx.remove(children);
  291. }
  292. }
  293. };
  294. class readyqueue final {
  295. public:
  296. using list_type = types::list<thread*>;
  297. using iterator_type = list_type::iterator_type;
  298. using const_iterator_type = list_type::const_iterator_type;
  299. private:
  300. list_type m_thds;
  301. private:
  302. readyqueue(const readyqueue&) = delete;
  303. readyqueue(readyqueue&&) = delete;
  304. readyqueue& operator=(const readyqueue&) = delete;
  305. readyqueue& operator=(readyqueue&&) = delete;
  306. ~readyqueue() = delete;
  307. public:
  308. constexpr explicit readyqueue(void) = default;
  309. constexpr void push(thread* thd)
  310. {
  311. m_thds.push_back(thd);
  312. }
  313. constexpr thread* pop(void)
  314. {
  315. auto iter = m_thds.begin();
  316. while (!((*iter)->attr.ready))
  317. iter = m_thds.erase(iter);
  318. auto* ptr = *iter;
  319. m_thds.erase(iter);
  320. return ptr;
  321. }
  322. constexpr thread* query(void)
  323. {
  324. auto* thd = this->pop();
  325. this->push(thd);
  326. return thd;
  327. }
  328. constexpr void remove_all(thread* thd)
  329. {
  330. auto iter = m_thds.find(thd);
  331. while (iter != m_thds.end()) {
  332. m_thds.erase(iter);
  333. iter = m_thds.find(thd);
  334. }
  335. }
  336. };
  337. extern "C" void NORETURN init_scheduler();
  338. void schedule(void);
  339. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  340. {
  341. --*stack;
  342. **stack = val;
  343. return val;
  344. }
  345. // class thdlist
  346. constexpr thdlist::~thdlist()
  347. {
  348. for (auto iter = thds.begin(); iter != thds.end(); ++iter)
  349. readythds->remove_all(&iter);
  350. }
  351. void k_new_thread(void (*func)(void*), void* data);