process.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. #pragma once
  2. #include <map>
  3. #include <list>
  4. #include <memory>
  5. #include <queue>
  6. #include <set>
  7. #include <tuple>
  8. #include <utility>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <kernel/event/evtqueue.hpp>
  12. #include <kernel/interrupt.h>
  13. #include <kernel/mm.hpp>
  14. #include <kernel/signal.hpp>
  15. #include <kernel/task.h>
  16. #include <kernel/tty.hpp>
  17. #include <kernel/vfs.hpp>
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <types/allocator.hpp>
  21. #include <types/cplusplus.hpp>
  22. #include <types/hash_map.hpp>
  23. #include <types/path.hpp>
  24. #include <types/status.h>
  25. #include <types/string.hpp>
  26. #include <types/types.h>
  27. class process;
  28. namespace kernel::tasks {
  29. struct thread;
  30. } // namespace kernel::tasks
  31. class proclist;
  32. class readyqueue;
  33. inline process* volatile current_process;
  34. inline kernel::tasks::thread* volatile current_thread;
  35. inline proclist* procs;
  36. inline readyqueue* readythds;
  37. inline tss32_t tss;
  38. struct process_attr {
  39. uint16_t system : 1;
  40. uint16_t zombie : 1 = 0;
  41. };
  42. struct thread_attr {
  43. uint32_t system : 1;
  44. uint32_t ready : 1;
  45. uint32_t wait : 1;
  46. };
  47. namespace kernel::tasks {
  48. using tid_t = uint32_t;
  49. struct thread {
  50. private:
  51. void alloc_kstack(void);
  52. void free_kstack(uint32_t p);
  53. public:
  54. uint32_t* esp;
  55. uint32_t pkstack;
  56. pid_t owner;
  57. thread_attr attr;
  58. int* __user set_child_tid {};
  59. int* __user clear_child_tid {};
  60. types::string<> name {};
  61. explicit inline thread(types::string<> name, pid_t owner)
  62. : owner { owner }
  63. , attr { .system = 1, .ready = 1, .wait = 0, }
  64. , name { name }
  65. {
  66. alloc_kstack();
  67. }
  68. inline thread(const thread& val, pid_t owner)
  69. : owner { owner }, attr { val.attr }, name { val.name }
  70. {
  71. alloc_kstack();
  72. }
  73. constexpr thread(thread&& val) = default;
  74. inline ~thread() { free_kstack(pkstack); }
  75. constexpr tid_t tid() const { return pkstack; }
  76. constexpr bool operator==(const thread& rhs) const
  77. { return pkstack == rhs.pkstack; }
  78. constexpr bool operator<(const thread& rhs) const
  79. { return pkstack < rhs.pkstack; }
  80. };
  81. }
  82. class filearr {
  83. public:
  84. using array_type = std::map<int, std::shared_ptr<fs::file>>;
  85. private:
  86. array_type arr;
  87. std::priority_queue<int, std::vector<int>, std::greater<int>> _fds;
  88. int _greatest_fd;
  89. private:
  90. constexpr int next_fd()
  91. {
  92. if (_fds.empty())
  93. return _greatest_fd++;
  94. int retval = _fds.top();
  95. _fds.pop();
  96. return retval;
  97. }
  98. public:
  99. constexpr filearr(const filearr&) = delete;
  100. constexpr filearr& operator=(const filearr&) = delete;
  101. constexpr filearr& operator=(filearr&&) = delete;
  102. constexpr filearr(void) = default;
  103. constexpr filearr(filearr&& val) = default;
  104. constexpr int dup(int old_fd)
  105. {
  106. return dup2(old_fd, next_fd());
  107. }
  108. // TODO: the third parameter should be int flags
  109. // determining whether the fd should be closed
  110. // after exec() (FD_CLOEXEC)
  111. constexpr int dup2(int old_fd, int new_fd)
  112. {
  113. close(new_fd);
  114. auto iter = arr.find(old_fd);
  115. if (!iter)
  116. return -EBADF;
  117. this->arr.emplace(new_fd, iter->second);
  118. return new_fd;
  119. }
  120. constexpr void dup_all(const filearr& orig)
  121. {
  122. this->_fds = orig._fds;
  123. this->_greatest_fd = orig._greatest_fd;
  124. for (auto [ fd, fp ] : orig.arr)
  125. this->arr.emplace(fd, fp);
  126. }
  127. constexpr fs::file* operator[](int i) const
  128. {
  129. auto iter = arr.find(i);
  130. if (!iter)
  131. return nullptr;
  132. return iter->second.get();
  133. }
  134. int pipe(int pipefd[2])
  135. {
  136. std::shared_ptr<fs::pipe> ppipe { new fs::pipe };
  137. bool inserted = false;
  138. int fd = next_fd();
  139. std::tie(std::ignore, inserted) = arr.emplace(fd,
  140. std::shared_ptr<fs::file> {
  141. new fs::fifo_file(nullptr, {
  142. .read = 1,
  143. .write = 0,
  144. .close_on_exec = 0,
  145. }, ppipe),
  146. });
  147. assert(inserted);
  148. // TODO: use copy_to_user()
  149. pipefd[0] = fd;
  150. fd = next_fd();
  151. std::tie(std::ignore, inserted) = arr.emplace(fd,
  152. std::shared_ptr<fs::file> {
  153. new fs::fifo_file(nullptr, {
  154. .read = 0,
  155. .write = 1,
  156. .close_on_exec = 0,
  157. }, ppipe),
  158. });
  159. assert(inserted);
  160. // TODO: use copy_to_user()
  161. pipefd[1] = fd;
  162. return 0;
  163. }
  164. int open(const process& current, const types::path& filepath, int flags, mode_t mode);
  165. constexpr void close(int fd)
  166. {
  167. auto iter = arr.find(fd);
  168. if (!iter)
  169. return;
  170. _fds.push(fd);
  171. arr.erase(iter);
  172. }
  173. constexpr void onexec()
  174. {
  175. for (auto iter = arr.begin(); iter != arr.end(); ) {
  176. if (!iter->second->flags.close_on_exec) {
  177. ++iter;
  178. continue;
  179. }
  180. _fds.push(iter->first);
  181. iter = arr.erase(iter);
  182. }
  183. }
  184. constexpr void close_all(void)
  185. {
  186. for (const auto& item : arr)
  187. _fds.push(item.first);
  188. arr.clear();
  189. }
  190. constexpr ~filearr()
  191. {
  192. close_all();
  193. }
  194. };
  195. class process {
  196. public:
  197. struct wait_obj {
  198. pid_t pid;
  199. int code;
  200. };
  201. public:
  202. kernel::memory::mm_list mms {};
  203. std::set<kernel::tasks::thread> thds;
  204. kernel::cond_var cv_wait;
  205. std::list<wait_obj> waitlist;
  206. process_attr attr {};
  207. filearr files;
  208. types::path pwd;
  209. kernel::signal_list signals;
  210. mode_t umask { 0022 };
  211. pid_t pid {};
  212. pid_t ppid {};
  213. pid_t pgid {};
  214. pid_t sid {};
  215. tty* control_tty {};
  216. fs::vfs::dentry* root { fs::fs_root };
  217. std::set<pid_t> children;
  218. public:
  219. process(const process&) = delete;
  220. explicit process(const process& parent, pid_t pid);
  221. // this function is used for system initialization
  222. // DO NOT use this after the system is on
  223. explicit process(pid_t pid, pid_t ppid);
  224. constexpr bool is_system(void) const
  225. { return attr.system; }
  226. constexpr bool is_zombie(void) const
  227. { return attr.zombie; }
  228. };
  229. class proclist final {
  230. public:
  231. using list_type = std::map<pid_t, process>;
  232. using iterator = list_type::iterator;
  233. using const_iterator = list_type::const_iterator;
  234. private:
  235. list_type m_procs;
  236. pid_t m_nextpid = 1;
  237. constexpr pid_t next_pid() { return m_nextpid++; }
  238. public:
  239. process& emplace(pid_t ppid)
  240. {
  241. pid_t pid = next_pid();
  242. auto [ iter, inserted ] = m_procs.try_emplace(pid, pid, ppid);
  243. assert(inserted);
  244. if (try_find(ppid)) {
  245. bool success = false;
  246. std::tie(std::ignore, success) =
  247. find(ppid).children.insert(pid);
  248. assert(success);
  249. }
  250. return iter->second;
  251. }
  252. process& copy_from(process& proc)
  253. {
  254. pid_t pid = next_pid();
  255. auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
  256. assert(inserted);
  257. proc.children.insert(pid);
  258. return iter->second;
  259. }
  260. constexpr void remove(pid_t pid)
  261. {
  262. make_children_orphans(pid);
  263. auto proc_iter = m_procs.find(pid);
  264. auto ppid = proc_iter->second.ppid;
  265. find(ppid).children.erase(pid);
  266. m_procs.erase(proc_iter);
  267. }
  268. constexpr bool try_find(pid_t pid) const
  269. { return m_procs.find(pid); }
  270. // if process doesn't exist, the behavior is undefined
  271. constexpr process& find(pid_t pid)
  272. {
  273. auto iter = m_procs.find(pid);
  274. assert(iter);
  275. return iter->second;
  276. }
  277. constexpr bool has_child(pid_t pid)
  278. {
  279. auto& proc = find(pid);
  280. return !proc.children.empty();
  281. }
  282. constexpr void make_children_orphans(pid_t pid)
  283. {
  284. auto& children = find(pid).children;
  285. auto& init_children = find(1).children;
  286. for (auto item : children) {
  287. init_children.insert(item);
  288. find(item).ppid = 1;
  289. }
  290. children.clear();
  291. }
  292. // the process MUST exist, or the behavior is undefined
  293. void send_signal(pid_t pid, kernel::sig_t signal)
  294. {
  295. auto& proc = this->find(pid);
  296. proc.signals.set(signal);
  297. }
  298. void send_signal_grp(pid_t pgid, kernel::sig_t signal)
  299. {
  300. for (auto& [ pid, proc ] : m_procs) {
  301. if (proc.pgid == pgid)
  302. proc.signals.set(signal);
  303. }
  304. }
  305. void kill(pid_t pid, int exit_code);
  306. };
  307. // TODO: lock and unlock
  308. class readyqueue final {
  309. public:
  310. using thread = kernel::tasks::thread;
  311. using list_type = std::list<thread*>;
  312. private:
  313. list_type m_thds;
  314. private:
  315. readyqueue(const readyqueue&) = delete;
  316. readyqueue(readyqueue&&) = delete;
  317. readyqueue& operator=(const readyqueue&) = delete;
  318. readyqueue& operator=(readyqueue&&) = delete;
  319. ~readyqueue() = delete;
  320. public:
  321. constexpr explicit readyqueue(void) = default;
  322. constexpr void push(thread* thd)
  323. { m_thds.push_back(thd); }
  324. constexpr thread* pop(void)
  325. {
  326. m_thds.remove_if([](thread* item) {
  327. return !item->attr.ready;
  328. });
  329. auto* retval = m_thds.front();
  330. m_thds.pop_front();
  331. return retval;
  332. }
  333. constexpr thread* query(void)
  334. {
  335. auto* thd = this->pop();
  336. this->push(thd);
  337. return thd;
  338. }
  339. constexpr void remove_all(thread* thd)
  340. { m_thds.remove(thd); }
  341. };
  342. void NORETURN init_scheduler(void);
  343. /// @return true if returned normally, false if being interrupted
  344. bool schedule(void);
  345. void NORETURN schedule_noreturn(void);
  346. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  347. {
  348. --*stack;
  349. **stack = val;
  350. return val;
  351. }
  352. void k_new_thread(void (*func)(void*), void* data);
  353. void NORETURN freeze(void);
  354. void NORETURN kill_current(int exit_code);
  355. void check_signal(void);