process.hpp 9.7 KB

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