process.hpp 9.5 KB

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