process.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <fcntl.h>
  10. #include <kernel/errno.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. iter->second->close();
  171. _fds.push(fd);
  172. arr.erase(iter);
  173. }
  174. constexpr void onexec()
  175. {
  176. for (auto&& [ fd, fp ] : arr) {
  177. if (fp->flags.close_on_exec)
  178. close(fd);
  179. }
  180. }
  181. constexpr void close_all(void)
  182. {
  183. for (auto&& [ fd, fp ] : arr) {
  184. fp->close();
  185. _fds.push(fd);
  186. }
  187. arr.clear();
  188. }
  189. constexpr ~filearr()
  190. {
  191. close_all();
  192. }
  193. };
  194. class process {
  195. public:
  196. struct wait_obj {
  197. pid_t pid;
  198. int code;
  199. };
  200. public:
  201. kernel::memory::mm_list mms {};
  202. std::set<kernel::tasks::thread> thds;
  203. kernel::cond_var cv_wait;
  204. std::list<wait_obj> waitlist;
  205. process_attr attr {};
  206. filearr files;
  207. types::path pwd;
  208. kernel::signal_list signals;
  209. pid_t pid {};
  210. pid_t ppid {};
  211. pid_t pgid {};
  212. pid_t sid {};
  213. tty* control_tty {};
  214. fs::vfs::dentry* root { fs::fs_root };
  215. std::set<pid_t> children;
  216. public:
  217. process(const process&) = delete;
  218. explicit process(const process& parent, pid_t pid);
  219. // this function is used for system initialization
  220. // DO NOT use this after the system is on
  221. explicit process(pid_t pid, pid_t ppid);
  222. constexpr bool is_system(void) const
  223. { return attr.system; }
  224. constexpr bool is_zombie(void) const
  225. { return attr.zombie; }
  226. };
  227. class proclist final {
  228. public:
  229. using list_type = std::map<pid_t, process>;
  230. using iterator = list_type::iterator;
  231. using const_iterator = list_type::const_iterator;
  232. private:
  233. list_type m_procs;
  234. pid_t m_nextpid = 1;
  235. constexpr pid_t next_pid() { return m_nextpid++; }
  236. public:
  237. process& emplace(pid_t ppid)
  238. {
  239. pid_t pid = next_pid();
  240. auto [ iter, inserted ] = m_procs.try_emplace(pid, pid, ppid);
  241. assert(inserted);
  242. if (try_find(ppid)) {
  243. bool success = false;
  244. std::tie(std::ignore, success) =
  245. find(ppid).children.insert(pid);
  246. assert(success);
  247. }
  248. return iter->second;
  249. }
  250. process& copy_from(process& proc)
  251. {
  252. pid_t pid = next_pid();
  253. auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
  254. assert(inserted);
  255. proc.children.insert(pid);
  256. return iter->second;
  257. }
  258. constexpr void remove(pid_t pid)
  259. {
  260. make_children_orphans(pid);
  261. auto proc_iter = m_procs.find(pid);
  262. auto ppid = proc_iter->second.ppid;
  263. find(ppid).children.erase(pid);
  264. m_procs.erase(proc_iter);
  265. }
  266. constexpr bool try_find(pid_t pid) const
  267. { return m_procs.find(pid); }
  268. // if process doesn't exist, the behavior is undefined
  269. constexpr process& find(pid_t pid)
  270. {
  271. auto iter = m_procs.find(pid);
  272. assert(iter);
  273. return iter->second;
  274. }
  275. constexpr bool has_child(pid_t pid)
  276. {
  277. auto& proc = find(pid);
  278. return !proc.children.empty();
  279. }
  280. constexpr void make_children_orphans(pid_t pid)
  281. {
  282. auto& children = find(pid).children;
  283. auto& init_children = find(1).children;
  284. for (auto item : children) {
  285. init_children.insert(item);
  286. find(item).ppid = 1;
  287. }
  288. children.clear();
  289. }
  290. // the process MUST exist, or the behavior is undefined
  291. void send_signal(pid_t pid, kernel::sig_t signal)
  292. {
  293. auto& proc = this->find(pid);
  294. proc.signals.set(signal);
  295. }
  296. void send_signal_grp(pid_t pgid, kernel::sig_t signal)
  297. {
  298. for (auto& [ pid, proc ] : m_procs) {
  299. if (proc.pgid == pgid)
  300. proc.signals.set(signal);
  301. }
  302. }
  303. void kill(pid_t pid, int exit_code);
  304. };
  305. // TODO: lock and unlock
  306. class readyqueue final {
  307. public:
  308. using thread = kernel::tasks::thread;
  309. using list_type = std::list<thread*>;
  310. private:
  311. list_type m_thds;
  312. private:
  313. readyqueue(const readyqueue&) = delete;
  314. readyqueue(readyqueue&&) = delete;
  315. readyqueue& operator=(const readyqueue&) = delete;
  316. readyqueue& operator=(readyqueue&&) = delete;
  317. ~readyqueue() = delete;
  318. public:
  319. constexpr explicit readyqueue(void) = default;
  320. constexpr void push(thread* thd)
  321. { m_thds.push_back(thd); }
  322. constexpr thread* pop(void)
  323. {
  324. m_thds.remove_if([](thread* item) {
  325. return !item->attr.ready;
  326. });
  327. auto* retval = m_thds.front();
  328. m_thds.pop_front();
  329. return retval;
  330. }
  331. constexpr thread* query(void)
  332. {
  333. auto* thd = this->pop();
  334. this->push(thd);
  335. return thd;
  336. }
  337. constexpr void remove_all(thread* thd)
  338. { m_thds.remove(thd); }
  339. };
  340. void NORETURN init_scheduler(void);
  341. /// @return true if returned normally, false if being interrupted
  342. bool schedule(void);
  343. void NORETURN schedule_noreturn(void);
  344. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  345. {
  346. --*stack;
  347. **stack = val;
  348. return val;
  349. }
  350. void k_new_thread(void (*func)(void*), void* data);
  351. void NORETURN freeze(void);
  352. void NORETURN kill_current(int exit_code);
  353. void check_signal(void);