process.hpp 11 KB

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