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/mem.h>
  15. #include <kernel/user/thread_local.hpp>
  16. #include <kernel/signal.hpp>
  17. #include <kernel/task.h>
  18. #include <kernel/tty.hpp>
  19. #include <kernel/vfs.hpp>
  20. #include <stdint.h>
  21. #include <sys/types.h>
  22. #include <types/allocator.hpp>
  23. #include <types/cplusplus.hpp>
  24. #include <types/hash_map.hpp>
  25. #include <types/path.hpp>
  26. #include <types/status.h>
  27. #include <types/string.hpp>
  28. #include <types/types.h>
  29. class process;
  30. namespace kernel::tasks {
  31. struct thread;
  32. } // namespace kernel::tasks
  33. class proclist;
  34. class readyqueue;
  35. inline process* volatile current_process;
  36. inline kernel::tasks::thread* volatile current_thread;
  37. inline proclist* procs;
  38. inline readyqueue* readythds;
  39. inline tss32_t tss;
  40. struct process_attr {
  41. uint16_t system : 1;
  42. uint16_t zombie : 1 = 0;
  43. };
  44. struct thread_attr {
  45. uint32_t system : 1;
  46. uint32_t ready : 1;
  47. };
  48. namespace kernel::tasks {
  49. using tid_t = uint32_t;
  50. struct thread {
  51. private:
  52. struct kernel_stack {
  53. std::byte* stack_base;
  54. uint32_t* esp;
  55. kernel_stack();
  56. kernel_stack(const kernel_stack& other);
  57. kernel_stack(kernel_stack&& other);
  58. ~kernel_stack();
  59. };
  60. public:
  61. kernel_stack kstack;
  62. pid_t owner;
  63. thread_attr attr;
  64. signal_list signals;
  65. int* __user set_child_tid {};
  66. int* __user clear_child_tid {};
  67. types::string<> name {};
  68. segment_descriptor tls_desc {};
  69. explicit inline thread(types::string<> name, pid_t owner)
  70. : owner { owner }
  71. , attr { .system = 1, .ready = 1, }
  72. , name { name }
  73. {
  74. }
  75. inline thread(const thread& val, pid_t owner)
  76. : owner { owner }, attr { val.attr }, name { val.name }
  77. {
  78. }
  79. int set_thread_area(user::user_desc* ptr);
  80. int load_thread_area() const;
  81. void sleep();
  82. void wakeup();
  83. constexpr bool is_ready() const
  84. { return attr.ready; }
  85. void send_signal(kernel::signal_list::signo_type signal);
  86. thread(thread&& val) = default;
  87. inline tid_t tid() const { return (tid_t)kstack.stack_base; }
  88. inline bool operator==(const thread& rhs) const
  89. { return tid() == rhs.tid(); }
  90. inline bool operator<(const thread& rhs) const
  91. { return tid() < rhs.tid(); }
  92. };
  93. }
  94. class filearr {
  95. private:
  96. // TODO: change this
  97. struct fditem {
  98. int flags;
  99. std::shared_ptr<fs::file> file;
  100. };
  101. std::map<int, fditem> arr;
  102. int min_avail { };
  103. private:
  104. int allocate_fd(int from);
  105. void release_fd(int fd);
  106. inline int next_fd() { return allocate_fd(min_avail); }
  107. public:
  108. constexpr filearr() = default;
  109. constexpr filearr(const filearr& val) = default;
  110. constexpr filearr(filearr&& val) = default;
  111. constexpr filearr& operator=(const filearr&) = delete;
  112. constexpr filearr& operator=(filearr&&) = delete;
  113. // TODO: the third parameter should be int flags
  114. // determining whether the fd should be closed
  115. // after exec() (FD_CLOEXEC)
  116. int dup2(int old_fd, int new_fd);
  117. int dup(int old_fd);
  118. int dupfd(int fd, int minfd, int flags);
  119. int set_flags(int fd, int flags);
  120. int clear_flags(int fd, int flags);
  121. constexpr fs::file* operator[](int i) const
  122. {
  123. auto iter = arr.find(i);
  124. if (!iter)
  125. return nullptr;
  126. return iter->second.file.get();
  127. }
  128. int pipe(int pipefd[2])
  129. {
  130. std::shared_ptr<fs::pipe> ppipe { new fs::pipe };
  131. bool inserted = false;
  132. int fd = next_fd();
  133. std::tie(std::ignore, inserted) = arr.emplace(fd, fditem {
  134. 0, std::shared_ptr<fs::file> {
  135. new fs::fifo_file(nullptr, {
  136. .read = 1,
  137. .write = 0,
  138. .append = 0,
  139. }, ppipe),
  140. } } );
  141. assert(inserted);
  142. // TODO: use copy_to_user()
  143. pipefd[0] = fd;
  144. fd = next_fd();
  145. std::tie(std::ignore, inserted) = arr.emplace(fd, fditem {
  146. 0, std::shared_ptr<fs::file> {
  147. new fs::fifo_file(nullptr, {
  148. .read = 0,
  149. .write = 1,
  150. .append = 0,
  151. }, ppipe),
  152. } } );
  153. assert(inserted);
  154. // TODO: use copy_to_user()
  155. pipefd[1] = fd;
  156. return 0;
  157. }
  158. int open(const process& current, const types::path& filepath, int flags, mode_t mode);
  159. constexpr void close(int fd)
  160. {
  161. auto iter = arr.find(fd);
  162. if (!iter)
  163. return;
  164. release_fd(fd);
  165. arr.erase(iter);
  166. }
  167. constexpr void onexec()
  168. {
  169. for (auto iter = arr.begin(); iter != arr.end(); ) {
  170. if (!(iter->second.flags & FD_CLOEXEC)) {
  171. ++iter;
  172. continue;
  173. }
  174. release_fd(iter->first);
  175. iter = arr.erase(iter);
  176. }
  177. }
  178. constexpr void close_all(void)
  179. {
  180. for (const auto& item : arr)
  181. release_fd(item.first);
  182. arr.clear();
  183. }
  184. constexpr ~filearr()
  185. {
  186. close_all();
  187. }
  188. };
  189. class process {
  190. public:
  191. struct wait_obj {
  192. pid_t pid;
  193. int code;
  194. };
  195. public:
  196. kernel::memory::mm_list mms {};
  197. std::set<kernel::tasks::thread> thds;
  198. kernel::cond_var cv_wait;
  199. std::list<wait_obj> waitlist;
  200. process_attr attr {};
  201. filearr files;
  202. types::path pwd;
  203. mode_t umask { 0022 };
  204. pid_t pid {};
  205. pid_t ppid {};
  206. pid_t pgid {};
  207. pid_t sid {};
  208. tty* control_tty {};
  209. fs::dentry* root { fs::fs_root };
  210. std::set<pid_t> children;
  211. public:
  212. process(const process&) = delete;
  213. explicit process(const process& parent, pid_t pid);
  214. // this function is used for system initialization
  215. // DO NOT use this after the system is on
  216. explicit process(pid_t pid, pid_t ppid);
  217. constexpr bool is_system(void) const
  218. { return attr.system; }
  219. constexpr bool is_zombie(void) const
  220. { return attr.zombie; }
  221. void send_signal(kernel::signal_list::signo_type signal);
  222. };
  223. class proclist final {
  224. private:
  225. std::map<pid_t, process> m_procs;
  226. pid_t m_nextpid = 2;
  227. constexpr pid_t next_pid() { return m_nextpid++; }
  228. process& real_emplace(pid_t pid, pid_t ppid);
  229. public:
  230. proclist();
  231. constexpr process& copy_from(process& proc)
  232. {
  233. pid_t pid = next_pid();
  234. auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
  235. assert(inserted);
  236. proc.children.insert(pid);
  237. return iter->second;
  238. }
  239. constexpr void remove(pid_t pid)
  240. {
  241. make_children_orphans(pid);
  242. auto proc_iter = m_procs.find(pid);
  243. auto ppid = proc_iter->second.ppid;
  244. find(ppid).children.erase(pid);
  245. m_procs.erase(proc_iter);
  246. }
  247. constexpr std::pair<process*, bool> try_find(pid_t pid) const
  248. {
  249. auto iter = m_procs.find(pid);
  250. if (iter)
  251. return { (process*)&iter->second, true };
  252. else
  253. return { nullptr, false };
  254. }
  255. // if process doesn't exist, the behavior is undefined
  256. constexpr process& find(pid_t pid)
  257. {
  258. auto [ ptr, found] = try_find(pid);
  259. assert(found);
  260. return *ptr;
  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);