process.hpp 11 KB

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