process.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 <stdint.h>
  9. #include <types/allocator.hpp>
  10. #include <types/cplusplus.hpp>
  11. #include <types/hash_map.hpp>
  12. #include <types/list.hpp>
  13. #include <types/map.hpp>
  14. #include <types/pair.hpp>
  15. #include <types/status.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. // TODO: check whether dentry is a file if O_DIRECTORY is set
  177. // if (!(dentry->ind->flags.in.file || dentry->ind->flags.in.special_node)) {
  178. // errno = EISDIR;
  179. // return -1;
  180. // }
  181. // TODO: unify file, inode, dentry TYPE
  182. fs::file::types type = fs::file::types::regular_file;
  183. if (dentry->ind->flags.in.directory)
  184. type = fs::file::types::directory;
  185. if (dentry->ind->flags.in.special_node)
  186. type = fs::file::types::block_dev;
  187. auto iter = files->emplace_back(fs::file {
  188. type,
  189. dentry->ind,
  190. dentry->parent,
  191. 0,
  192. 1 });
  193. int fd = next_fd++;
  194. arr.emplace(fd, iter);
  195. return fd;
  196. }
  197. // close file descriptor
  198. // where iter is guaranteed not nullptr
  199. constexpr void close(array_type::iterator_type iter)
  200. {
  201. if (iter->value->ref == 1)
  202. files->erase(iter->value);
  203. else
  204. --iter->value->ref;
  205. }
  206. constexpr void close(int fd)
  207. {
  208. auto iter = arr.find(fd);
  209. if (iter)
  210. close(iter);
  211. }
  212. constexpr ~filearr()
  213. {
  214. for (int i = 0; i < next_fd; ++i)
  215. close(i);
  216. }
  217. };
  218. public:
  219. mutable kernel::mm_list mms;
  220. thdlist thds;
  221. kernel::evtqueue wait_lst;
  222. process_attr attr;
  223. pid_t pid;
  224. pid_t ppid;
  225. filearr files;
  226. public:
  227. process(process&& val);
  228. process(const process&);
  229. explicit process(pid_t ppid, bool system = true);
  230. constexpr bool is_system(void) const
  231. {
  232. return attr.system;
  233. }
  234. constexpr bool is_zombie(void) const
  235. {
  236. return attr.zombie;
  237. }
  238. private:
  239. static inline pid_t max_pid;
  240. static inline pid_t alloc_pid(void)
  241. {
  242. return ++max_pid;
  243. }
  244. };
  245. class proclist final {
  246. public:
  247. using list_type = types::map<pid_t, process>;
  248. using child_index_type = types::hash_map<pid_t, types::list<pid_t>, types::linux_hasher<pid_t>>;
  249. using iterator_type = list_type::iterator_type;
  250. using const_iterator_type = list_type::const_iterator_type;
  251. private:
  252. list_type m_procs;
  253. child_index_type m_child_idx;
  254. public:
  255. template <typename... Args>
  256. iterator_type emplace(Args&&... args)
  257. {
  258. process _proc(types::forward<Args>(args)...);
  259. auto pid = _proc.pid;
  260. auto ppid = _proc.ppid;
  261. auto iter = m_procs.insert(types::make_pair(pid, types::move(_proc)));
  262. auto children = m_child_idx.find(ppid);
  263. if (!children) {
  264. m_child_idx.emplace(ppid, types::list<pid_t> {});
  265. children = m_child_idx.find(ppid);
  266. }
  267. children->value.push_back(pid);
  268. return iter;
  269. }
  270. constexpr void remove(pid_t pid)
  271. {
  272. make_children_orphans(pid);
  273. auto proc_iter = m_procs.find(pid);
  274. auto ppid = proc_iter->value.ppid;
  275. auto& parent_children = m_child_idx.find(ppid)->value;
  276. auto i = parent_children.find(pid);
  277. parent_children.erase(i);
  278. m_procs.erase(proc_iter);
  279. }
  280. constexpr process* find(pid_t pid)
  281. {
  282. return &m_procs.find(pid)->value;
  283. }
  284. constexpr bool has_child(pid_t pid)
  285. {
  286. auto children = m_child_idx.find(pid);
  287. return children && !children->value.empty();
  288. }
  289. constexpr void make_children_orphans(pid_t pid)
  290. {
  291. auto children = m_child_idx.find(pid);
  292. if (children) {
  293. auto init_children = m_child_idx.find(1);
  294. for (auto iter = children->value.begin(); iter != children->value.end(); ++iter) {
  295. init_children->value.push_back(*iter);
  296. this->find(*iter)->ppid = 1;
  297. }
  298. m_child_idx.remove(children);
  299. }
  300. }
  301. };
  302. class readyqueue final {
  303. public:
  304. using list_type = types::list<thread*>;
  305. using iterator_type = list_type::iterator_type;
  306. using const_iterator_type = list_type::const_iterator_type;
  307. private:
  308. list_type m_thds;
  309. private:
  310. readyqueue(const readyqueue&) = delete;
  311. readyqueue(readyqueue&&) = delete;
  312. readyqueue& operator=(const readyqueue&) = delete;
  313. readyqueue& operator=(readyqueue&&) = delete;
  314. ~readyqueue() = delete;
  315. public:
  316. constexpr explicit readyqueue(void) = default;
  317. constexpr void push(thread* thd)
  318. {
  319. m_thds.push_back(thd);
  320. }
  321. constexpr thread* pop(void)
  322. {
  323. auto iter = m_thds.begin();
  324. while (!((*iter)->attr.ready))
  325. iter = m_thds.erase(iter);
  326. auto* ptr = *iter;
  327. m_thds.erase(iter);
  328. return ptr;
  329. }
  330. constexpr thread* query(void)
  331. {
  332. auto* thd = this->pop();
  333. this->push(thd);
  334. return thd;
  335. }
  336. constexpr void remove_all(thread* thd)
  337. {
  338. auto iter = m_thds.find(thd);
  339. while (iter != m_thds.end()) {
  340. m_thds.erase(iter);
  341. iter = m_thds.find(thd);
  342. }
  343. }
  344. };
  345. void NORETURN init_scheduler(void);
  346. void schedule(void);
  347. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  348. {
  349. --*stack;
  350. **stack = val;
  351. return val;
  352. }
  353. // class thdlist
  354. constexpr thdlist::~thdlist()
  355. {
  356. for (auto iter = thds.begin(); iter != thds.end(); ++iter)
  357. readythds->remove_all(&iter);
  358. }
  359. void k_new_thread(void (*func)(void*), void* data);