process.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. signal_list signals;
  59. int* __user set_child_tid {};
  60. int* __user clear_child_tid {};
  61. types::string<> name {};
  62. explicit inline thread(types::string<> name, pid_t owner)
  63. : owner { owner }
  64. , attr { .system = 1, .ready = 1, .wait = 0, }
  65. , name { name }
  66. {
  67. alloc_kstack();
  68. }
  69. inline thread(const thread& val, pid_t owner)
  70. : owner { owner }, attr { val.attr }, name { val.name }
  71. {
  72. alloc_kstack();
  73. }
  74. constexpr thread(thread&& val) = default;
  75. inline ~thread() { free_kstack(pkstack); }
  76. constexpr tid_t tid() const { return pkstack; }
  77. constexpr bool operator==(const thread& rhs) const
  78. { return pkstack == rhs.pkstack; }
  79. constexpr bool operator<(const thread& rhs) const
  80. { return pkstack < rhs.pkstack; }
  81. };
  82. }
  83. class filearr {
  84. public:
  85. using array_type = std::map<int, std::shared_ptr<fs::file>>;
  86. private:
  87. array_type arr;
  88. std::priority_queue<int, std::vector<int>, std::greater<int>> _fds;
  89. int _greatest_fd;
  90. private:
  91. constexpr int next_fd()
  92. {
  93. if (_fds.empty())
  94. return _greatest_fd++;
  95. int retval = _fds.top();
  96. _fds.pop();
  97. return retval;
  98. }
  99. public:
  100. constexpr filearr(const filearr&) = delete;
  101. constexpr filearr& operator=(const filearr&) = delete;
  102. constexpr filearr& operator=(filearr&&) = delete;
  103. constexpr filearr(void) = default;
  104. constexpr filearr(filearr&& val) = default;
  105. constexpr int dup(int old_fd)
  106. {
  107. return dup2(old_fd, next_fd());
  108. }
  109. // TODO: the third parameter should be int flags
  110. // determining whether the fd should be closed
  111. // after exec() (FD_CLOEXEC)
  112. constexpr int dup2(int old_fd, int new_fd)
  113. {
  114. close(new_fd);
  115. auto iter = arr.find(old_fd);
  116. if (!iter)
  117. return -EBADF;
  118. this->arr.emplace(new_fd, iter->second);
  119. return new_fd;
  120. }
  121. constexpr void dup_all(const filearr& orig)
  122. {
  123. this->_fds = orig._fds;
  124. this->_greatest_fd = orig._greatest_fd;
  125. for (auto [ fd, fp ] : orig.arr)
  126. this->arr.emplace(fd, fp);
  127. }
  128. constexpr fs::file* operator[](int i) const
  129. {
  130. auto iter = arr.find(i);
  131. if (!iter)
  132. return nullptr;
  133. return iter->second.get();
  134. }
  135. int pipe(int pipefd[2])
  136. {
  137. std::shared_ptr<fs::pipe> ppipe { new fs::pipe };
  138. bool inserted = false;
  139. int fd = next_fd();
  140. std::tie(std::ignore, inserted) = arr.emplace(fd,
  141. std::shared_ptr<fs::file> {
  142. new fs::fifo_file(nullptr, {
  143. .read = 1,
  144. .write = 0,
  145. .close_on_exec = 0,
  146. }, ppipe),
  147. });
  148. assert(inserted);
  149. // TODO: use copy_to_user()
  150. pipefd[0] = fd;
  151. fd = next_fd();
  152. std::tie(std::ignore, inserted) = arr.emplace(fd,
  153. std::shared_ptr<fs::file> {
  154. new fs::fifo_file(nullptr, {
  155. .read = 0,
  156. .write = 1,
  157. .close_on_exec = 0,
  158. }, ppipe),
  159. });
  160. assert(inserted);
  161. // TODO: use copy_to_user()
  162. pipefd[1] = fd;
  163. return 0;
  164. }
  165. int open(const process& current, const types::path& filepath, int flags, mode_t mode);
  166. constexpr void close(int fd)
  167. {
  168. auto iter = arr.find(fd);
  169. if (!iter)
  170. return;
  171. _fds.push(fd);
  172. arr.erase(iter);
  173. }
  174. constexpr void onexec()
  175. {
  176. for (auto iter = arr.begin(); iter != arr.end(); ) {
  177. if (!iter->second->flags.close_on_exec) {
  178. ++iter;
  179. continue;
  180. }
  181. _fds.push(iter->first);
  182. iter = arr.erase(iter);
  183. }
  184. }
  185. constexpr void close_all(void)
  186. {
  187. for (const auto& item : arr)
  188. _fds.push(item.first);
  189. arr.clear();
  190. }
  191. constexpr ~filearr()
  192. {
  193. close_all();
  194. }
  195. };
  196. class process {
  197. public:
  198. struct wait_obj {
  199. pid_t pid;
  200. int code;
  201. };
  202. public:
  203. kernel::memory::mm_list mms {};
  204. std::set<kernel::tasks::thread> thds;
  205. kernel::cond_var cv_wait;
  206. std::list<wait_obj> waitlist;
  207. process_attr attr {};
  208. filearr files;
  209. types::path pwd;
  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. void send_signal(kernel::signal_list::signo_type signal);
  229. };
  230. class proclist final {
  231. public:
  232. using list_type = std::map<pid_t, process>;
  233. using iterator = list_type::iterator;
  234. using const_iterator = list_type::const_iterator;
  235. private:
  236. list_type m_procs;
  237. pid_t m_nextpid = 1;
  238. constexpr pid_t next_pid() { return m_nextpid++; }
  239. public:
  240. process& emplace(pid_t ppid)
  241. {
  242. pid_t pid = next_pid();
  243. auto [ iter, inserted ] = m_procs.try_emplace(pid, pid, ppid);
  244. assert(inserted);
  245. if (try_find(ppid)) {
  246. bool success = false;
  247. std::tie(std::ignore, success) =
  248. find(ppid).children.insert(pid);
  249. assert(success);
  250. }
  251. return iter->second;
  252. }
  253. process& copy_from(process& proc)
  254. {
  255. pid_t pid = next_pid();
  256. auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
  257. assert(inserted);
  258. proc.children.insert(pid);
  259. return iter->second;
  260. }
  261. constexpr void remove(pid_t pid)
  262. {
  263. make_children_orphans(pid);
  264. auto proc_iter = m_procs.find(pid);
  265. auto ppid = proc_iter->second.ppid;
  266. find(ppid).children.erase(pid);
  267. m_procs.erase(proc_iter);
  268. }
  269. constexpr bool try_find(pid_t pid) const
  270. { return m_procs.find(pid); }
  271. // if process doesn't exist, the behavior is undefined
  272. constexpr process& find(pid_t pid)
  273. {
  274. auto iter = m_procs.find(pid);
  275. assert(iter);
  276. return iter->second;
  277. }
  278. constexpr bool has_child(pid_t pid)
  279. {
  280. auto& proc = find(pid);
  281. return !proc.children.empty();
  282. }
  283. constexpr void make_children_orphans(pid_t pid)
  284. {
  285. auto& children = find(pid).children;
  286. auto& init_children = find(1).children;
  287. for (auto item : children) {
  288. init_children.insert(item);
  289. find(item).ppid = 1;
  290. }
  291. children.clear();
  292. }
  293. // the process MUST exist, or the behavior is undefined
  294. void send_signal(pid_t pid, kernel::signal_list::signo_type signal)
  295. {
  296. auto& proc = find(pid);
  297. proc.send_signal(signal);
  298. }
  299. void send_signal_grp(pid_t pgid, kernel::signal_list::signo_type signal)
  300. {
  301. // TODO: find processes that are in the same session quickly
  302. for (auto& [ pid, proc ] : m_procs) {
  303. if (proc.pgid != pgid)
  304. continue;
  305. proc.send_signal(signal);
  306. }
  307. }
  308. void kill(pid_t pid, int exit_code);
  309. };
  310. // TODO: lock and unlock
  311. class readyqueue final {
  312. public:
  313. using thread = kernel::tasks::thread;
  314. using list_type = std::list<thread*>;
  315. private:
  316. list_type m_thds;
  317. private:
  318. readyqueue(const readyqueue&) = delete;
  319. readyqueue(readyqueue&&) = delete;
  320. readyqueue& operator=(const readyqueue&) = delete;
  321. readyqueue& operator=(readyqueue&&) = delete;
  322. ~readyqueue() = delete;
  323. public:
  324. constexpr explicit readyqueue(void) = default;
  325. constexpr void push(thread* thd)
  326. { m_thds.push_back(thd); }
  327. constexpr thread* pop(void)
  328. {
  329. m_thds.remove_if([](thread* item) {
  330. return !item->attr.ready;
  331. });
  332. auto* retval = m_thds.front();
  333. m_thds.pop_front();
  334. return retval;
  335. }
  336. constexpr thread* query(void)
  337. {
  338. auto* thd = this->pop();
  339. this->push(thd);
  340. return thd;
  341. }
  342. constexpr void remove_all(thread* thd)
  343. { m_thds.remove(thd); }
  344. };
  345. void NORETURN init_scheduler(void);
  346. /// @return true if returned normally, false if being interrupted
  347. bool schedule(void);
  348. void NORETURN schedule_noreturn(void);
  349. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  350. {
  351. --*stack;
  352. **stack = val;
  353. return val;
  354. }
  355. void k_new_thread(void (*func)(void*), void* data);
  356. void NORETURN freeze(void);
  357. void NORETURN kill_current(int signo);
  358. void check_signal(void);