process.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. };
  46. namespace kernel::tasks {
  47. using tid_t = uint32_t;
  48. struct thread {
  49. private:
  50. void alloc_kstack(void);
  51. void free_kstack(uint32_t p);
  52. public:
  53. uint32_t* esp;
  54. uint32_t pkstack;
  55. pid_t owner;
  56. thread_attr attr;
  57. signal_list signals;
  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, }
  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. void sleep();
  74. void wakeup();
  75. constexpr bool is_ready() const
  76. { return attr.ready; }
  77. void send_signal(kernel::signal_list::signo_type signal);
  78. constexpr thread(thread&& val) = default;
  79. inline ~thread() { free_kstack(pkstack); }
  80. constexpr tid_t tid() const { return pkstack; }
  81. constexpr bool operator==(const thread& rhs) const
  82. { return pkstack == rhs.pkstack; }
  83. constexpr bool operator<(const thread& rhs) const
  84. { return pkstack < rhs.pkstack; }
  85. };
  86. }
  87. class filearr {
  88. private:
  89. // TODO: change this
  90. struct fditem {
  91. int flags;
  92. std::shared_ptr<fs::file> file;
  93. };
  94. std::map<int, fditem> arr;
  95. int min_avail { };
  96. private:
  97. int allocate_fd(int from);
  98. void release_fd(int fd);
  99. inline int next_fd() { return allocate_fd(min_avail); }
  100. public:
  101. constexpr filearr() = default;
  102. constexpr filearr(const filearr& val) = default;
  103. constexpr filearr(filearr&& val) = default;
  104. constexpr filearr& operator=(const filearr&) = delete;
  105. constexpr filearr& operator=(filearr&&) = delete;
  106. // TODO: the third parameter should be int flags
  107. // determining whether the fd should be closed
  108. // after exec() (FD_CLOEXEC)
  109. int dup2(int old_fd, int new_fd);
  110. int dup(int old_fd);
  111. int dupfd(int fd, int minfd, int flags);
  112. int set_flags(int fd, int flags);
  113. int clear_flags(int fd, int flags);
  114. constexpr fs::file* operator[](int i) const
  115. {
  116. auto iter = arr.find(i);
  117. if (!iter)
  118. return nullptr;
  119. return iter->second.file.get();
  120. }
  121. int pipe(int pipefd[2])
  122. {
  123. std::shared_ptr<fs::pipe> ppipe { new fs::pipe };
  124. bool inserted = false;
  125. int fd = next_fd();
  126. std::tie(std::ignore, inserted) = arr.emplace(fd, fditem {
  127. 0, std::shared_ptr<fs::file> {
  128. new fs::fifo_file(nullptr, {
  129. .read = 1,
  130. .write = 0,
  131. }, ppipe),
  132. } } );
  133. assert(inserted);
  134. // TODO: use copy_to_user()
  135. pipefd[0] = fd;
  136. fd = next_fd();
  137. std::tie(std::ignore, inserted) = arr.emplace(fd, fditem {
  138. 0, std::shared_ptr<fs::file> {
  139. new fs::fifo_file(nullptr, {
  140. .read = 0,
  141. .write = 1,
  142. }, ppipe),
  143. } } );
  144. assert(inserted);
  145. // TODO: use copy_to_user()
  146. pipefd[1] = fd;
  147. return 0;
  148. }
  149. int open(const process& current, const types::path& filepath, int flags, mode_t mode);
  150. constexpr void close(int fd)
  151. {
  152. auto iter = arr.find(fd);
  153. if (!iter)
  154. return;
  155. release_fd(fd);
  156. arr.erase(iter);
  157. }
  158. constexpr void onexec()
  159. {
  160. for (auto iter = arr.begin(); iter != arr.end(); ) {
  161. if (!(iter->second.flags & O_CLOEXEC)) {
  162. ++iter;
  163. continue;
  164. }
  165. release_fd(iter->first);
  166. iter = arr.erase(iter);
  167. }
  168. }
  169. constexpr void close_all(void)
  170. {
  171. for (const auto& item : arr)
  172. release_fd(item.first);
  173. arr.clear();
  174. }
  175. constexpr ~filearr()
  176. {
  177. close_all();
  178. }
  179. };
  180. class process {
  181. public:
  182. struct wait_obj {
  183. pid_t pid;
  184. int code;
  185. };
  186. public:
  187. kernel::memory::mm_list mms {};
  188. std::set<kernel::tasks::thread> thds;
  189. kernel::cond_var cv_wait;
  190. std::list<wait_obj> waitlist;
  191. process_attr attr {};
  192. filearr files;
  193. types::path pwd;
  194. mode_t umask { 0022 };
  195. pid_t pid {};
  196. pid_t ppid {};
  197. pid_t pgid {};
  198. pid_t sid {};
  199. tty* control_tty {};
  200. fs::vfs::dentry* root { fs::fs_root };
  201. std::set<pid_t> children;
  202. public:
  203. process(const process&) = delete;
  204. explicit process(const process& parent, pid_t pid);
  205. // this function is used for system initialization
  206. // DO NOT use this after the system is on
  207. explicit process(pid_t pid, pid_t ppid);
  208. constexpr bool is_system(void) const
  209. { return attr.system; }
  210. constexpr bool is_zombie(void) const
  211. { return attr.zombie; }
  212. void send_signal(kernel::signal_list::signo_type signal);
  213. };
  214. class proclist final {
  215. public:
  216. using list_type = std::map<pid_t, process>;
  217. using iterator = list_type::iterator;
  218. using const_iterator = list_type::const_iterator;
  219. private:
  220. list_type m_procs;
  221. pid_t m_nextpid = 1;
  222. constexpr pid_t next_pid() { return m_nextpid++; }
  223. public:
  224. process& emplace(pid_t ppid)
  225. {
  226. pid_t pid = next_pid();
  227. auto [ iter, inserted ] = m_procs.try_emplace(pid, pid, ppid);
  228. assert(inserted);
  229. if (try_find(ppid)) {
  230. bool success = false;
  231. std::tie(std::ignore, success) =
  232. find(ppid).children.insert(pid);
  233. assert(success);
  234. }
  235. return iter->second;
  236. }
  237. process& copy_from(process& proc)
  238. {
  239. pid_t pid = next_pid();
  240. auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
  241. assert(inserted);
  242. proc.children.insert(pid);
  243. return iter->second;
  244. }
  245. constexpr void remove(pid_t pid)
  246. {
  247. make_children_orphans(pid);
  248. auto proc_iter = m_procs.find(pid);
  249. auto ppid = proc_iter->second.ppid;
  250. find(ppid).children.erase(pid);
  251. m_procs.erase(proc_iter);
  252. }
  253. constexpr bool try_find(pid_t pid) const
  254. { return m_procs.find(pid); }
  255. // if process doesn't exist, the behavior is undefined
  256. constexpr process& find(pid_t pid)
  257. {
  258. auto iter = m_procs.find(pid);
  259. assert(iter);
  260. return iter->second;
  261. }
  262. constexpr void make_children_orphans(pid_t pid)
  263. {
  264. auto& children = find(pid).children;
  265. auto& init_children = find(1).children;
  266. for (auto item : children) {
  267. init_children.insert(item);
  268. find(item).ppid = 1;
  269. }
  270. children.clear();
  271. }
  272. // the process MUST exist, or the behavior is undefined
  273. void send_signal(pid_t pid, kernel::signal_list::signo_type signal)
  274. {
  275. auto& proc = find(pid);
  276. proc.send_signal(signal);
  277. }
  278. void send_signal_grp(pid_t pgid, kernel::signal_list::signo_type signal)
  279. {
  280. // TODO: find processes that are in the same session quickly
  281. for (auto& [ pid, proc ] : m_procs) {
  282. if (proc.pgid != pgid)
  283. continue;
  284. proc.send_signal(signal);
  285. }
  286. }
  287. void kill(pid_t pid, int exit_code);
  288. };
  289. // TODO: lock and unlock
  290. class readyqueue final {
  291. public:
  292. using thread = kernel::tasks::thread;
  293. using list_type = std::list<thread*>;
  294. private:
  295. list_type m_thds;
  296. private:
  297. readyqueue(const readyqueue&) = delete;
  298. readyqueue(readyqueue&&) = delete;
  299. readyqueue& operator=(const readyqueue&) = delete;
  300. readyqueue& operator=(readyqueue&&) = delete;
  301. ~readyqueue() = delete;
  302. public:
  303. constexpr explicit readyqueue(void) = default;
  304. constexpr void push(thread* thd)
  305. { m_thds.push_back(thd); }
  306. constexpr thread* pop(void)
  307. {
  308. m_thds.remove_if([](thread* item) {
  309. return !item->is_ready();
  310. });
  311. auto* retval = m_thds.front();
  312. m_thds.pop_front();
  313. return retval;
  314. }
  315. constexpr thread* query(void)
  316. {
  317. auto* thd = this->pop();
  318. this->push(thd);
  319. return thd;
  320. }
  321. constexpr void remove_all(thread* thd)
  322. { m_thds.remove(thd); }
  323. };
  324. void NORETURN init_scheduler(void);
  325. /// @return true if returned normally, false if being interrupted
  326. bool schedule(void);
  327. void NORETURN schedule_noreturn(void);
  328. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  329. {
  330. --*stack;
  331. **stack = val;
  332. return val;
  333. }
  334. void k_new_thread(void (*func)(void*), void* data);
  335. void NORETURN freeze(void);
  336. void NORETURN kill_current(int signo);
  337. void check_signal(void);