process.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. #pragma once
  2. #include <map>
  3. #include <list>
  4. #include <queue>
  5. #include <set>
  6. #include <tuple>
  7. #include <utility>
  8. #include <fcntl.h>
  9. #include <kernel/errno.h>
  10. #include <kernel/event/evtqueue.hpp>
  11. #include <kernel/interrupt.h>
  12. #include <kernel/mm.hpp>
  13. #include <kernel/signal.hpp>
  14. #include <kernel/task.h>
  15. #include <kernel/tty.hpp>
  16. #include <kernel/vfs.hpp>
  17. #include <stdint.h>
  18. #include <sys/types.h>
  19. #include <types/allocator.hpp>
  20. #include <types/cplusplus.hpp>
  21. #include <types/hash_map.hpp>
  22. #include <types/status.h>
  23. #include <types/string.hpp>
  24. #include <types/types.h>
  25. class process;
  26. namespace kernel::tasks {
  27. struct thread;
  28. } // namespace kernel::tasks
  29. class proclist;
  30. class readyqueue;
  31. inline process* volatile current_process;
  32. inline kernel::tasks::thread* volatile current_thread;
  33. inline proclist* procs;
  34. inline readyqueue* readythds;
  35. inline tss32_t tss;
  36. struct process_attr {
  37. uint16_t system : 1;
  38. uint16_t zombie : 1 = 0;
  39. };
  40. struct thread_attr {
  41. uint32_t system : 1;
  42. uint32_t ready : 1;
  43. uint32_t wait : 1;
  44. };
  45. namespace kernel::tasks {
  46. using tid_t = uint32_t;
  47. struct thread {
  48. private:
  49. void alloc_kstack(void);
  50. void free_kstack(uint32_t p);
  51. public:
  52. uint32_t* esp;
  53. uint32_t pkstack;
  54. pid_t owner;
  55. thread_attr attr;
  56. int* __user set_child_tid {};
  57. int* __user clear_child_tid {};
  58. explicit inline thread(pid_t owner)
  59. : owner { owner }
  60. , attr { .system = 1, .ready = 1, .wait = 0, }
  61. {
  62. alloc_kstack();
  63. }
  64. inline thread(const thread& val, pid_t owner)
  65. : owner { owner } , attr { val.attr }
  66. {
  67. alloc_kstack();
  68. }
  69. constexpr thread(thread&& val) = default;
  70. inline ~thread() { free_kstack(pkstack); }
  71. constexpr tid_t tid() const { return pkstack; }
  72. constexpr bool operator==(const thread& rhs) const
  73. { return pkstack == rhs.pkstack; }
  74. constexpr bool operator<(const thread& rhs) const
  75. { return pkstack < rhs.pkstack; }
  76. };
  77. }
  78. class filearr {
  79. public:
  80. using container_type = std::list<fs::file>;
  81. using array_type = std::map<int, container_type::iterator>;
  82. private:
  83. inline static container_type* files;
  84. array_type arr;
  85. std::priority_queue<int, std::vector<int>, std::greater<int>> _fds;
  86. int _greatest_fd;
  87. public:
  88. inline static void init_global_file_container(void)
  89. {
  90. files = new container_type;
  91. }
  92. private:
  93. // iter should not be nullptr
  94. constexpr void _close(container_type::iterator iter)
  95. {
  96. if (iter->ref == 1) {
  97. if (iter->type == fs::file::types::pipe) {
  98. assert(iter->flags.read | iter->flags.write);
  99. if (iter->flags.read)
  100. iter->ptr.pp->close_read();
  101. else
  102. iter->ptr.pp->close_write();
  103. if (iter->ptr.pp->is_free())
  104. delete iter->ptr.pp;
  105. }
  106. files->erase(iter);
  107. } else
  108. --iter->ref;
  109. }
  110. constexpr int next_fd()
  111. {
  112. if (_fds.empty())
  113. return _greatest_fd++;
  114. int retval = _fds.top();
  115. _fds.pop();
  116. return retval;
  117. }
  118. public:
  119. constexpr filearr(const filearr&) = delete;
  120. constexpr filearr& operator=(const filearr&) = delete;
  121. constexpr filearr& operator=(filearr&&) = delete;
  122. constexpr filearr(void) = default;
  123. constexpr filearr(filearr&& val) = default;
  124. constexpr int dup(int old_fd)
  125. {
  126. return dup2(old_fd, next_fd());
  127. }
  128. // TODO: the third parameter should be int flags
  129. // determining whether the fd should be closed
  130. // after exec() (FD_CLOEXEC)
  131. constexpr int dup2(int old_fd, int new_fd)
  132. {
  133. close(new_fd);
  134. auto iter = arr.find(old_fd);
  135. if (!iter)
  136. return -EBADF;
  137. auto [ _, iter_file ] = *iter;
  138. this->arr.emplace(new_fd, iter_file);
  139. ++iter_file->ref;
  140. return new_fd;
  141. }
  142. constexpr void dup_all(const filearr& orig)
  143. {
  144. this->_fds = orig._fds;
  145. this->_greatest_fd = orig._greatest_fd;
  146. for (auto [ fd, iter_file ] : orig.arr) {
  147. this->arr.emplace(fd, iter_file);
  148. ++iter_file->ref;
  149. }
  150. }
  151. constexpr fs::file* operator[](int i) const
  152. {
  153. auto iter = arr.find(i);
  154. if (!iter)
  155. return nullptr;
  156. return &iter->second;
  157. }
  158. int pipe(int pipefd[2])
  159. {
  160. // TODO: set read/write flags
  161. auto* pipe = new fs::pipe;
  162. auto iter = files->emplace(files->cend(), fs::file {
  163. fs::file::types::pipe,
  164. { .pp = pipe },
  165. nullptr,
  166. 0,
  167. 1,
  168. {
  169. .read = 1,
  170. .write = 0,
  171. },
  172. });
  173. bool inserted = false;
  174. int fd = next_fd();
  175. std::tie(std::ignore, inserted) = arr.emplace(fd, iter);
  176. assert(inserted);
  177. // TODO: use copy_to_user()
  178. pipefd[0] = fd;
  179. iter = files->emplace(files->cend(), fs::file {
  180. fs::file::types::pipe,
  181. { .pp = pipe },
  182. nullptr,
  183. 0,
  184. 1,
  185. {
  186. .read = 0,
  187. .write = 1,
  188. },
  189. });
  190. fd = next_fd();
  191. std::tie(std::ignore, inserted) = arr.emplace(fd, iter);
  192. assert(inserted);
  193. // TODO: use copy_to_user()
  194. pipefd[1] = fd;
  195. return 0;
  196. }
  197. int open(const process& current, const char* filename, uint32_t flags);
  198. constexpr void close(int fd)
  199. {
  200. auto iter = arr.find(fd);
  201. if (!iter)
  202. return;
  203. _close(iter->second);
  204. _fds.push(fd);
  205. arr.erase(iter);
  206. }
  207. constexpr void close_all(void)
  208. {
  209. for (auto&& [ fd, file ] : arr) {
  210. _close(file);
  211. _fds.push(fd);
  212. }
  213. arr.clear();
  214. }
  215. constexpr ~filearr()
  216. {
  217. close_all();
  218. }
  219. };
  220. class process {
  221. public:
  222. struct wait_obj {
  223. pid_t pid;
  224. int code;
  225. };
  226. public:
  227. mutable kernel::mm_list mms;
  228. std::set<kernel::tasks::thread> thds;
  229. kernel::cond_var cv_wait;
  230. std::list<wait_obj> waitlist;
  231. process_attr attr {};
  232. filearr files;
  233. types::string<> pwd;
  234. kernel::signal_list signals;
  235. pid_t pid {};
  236. pid_t ppid {};
  237. pid_t pgid {};
  238. pid_t sid {};
  239. tty* control_tty {};
  240. fs::vfs::dentry* root { fs::fs_root };
  241. std::set<pid_t> children;
  242. public:
  243. process(const process&) = delete;
  244. explicit process(const process& parent, pid_t pid);
  245. // this function is used for system initialization
  246. // DO NOT use this after the system is on
  247. explicit process(pid_t pid, pid_t ppid);
  248. constexpr bool is_system(void) const
  249. { return attr.system; }
  250. constexpr bool is_zombie(void) const
  251. { return attr.zombie; }
  252. };
  253. class proclist final {
  254. public:
  255. using list_type = std::map<pid_t, process>;
  256. using iterator = list_type::iterator;
  257. using const_iterator = list_type::const_iterator;
  258. private:
  259. list_type m_procs;
  260. pid_t m_nextpid = 1;
  261. constexpr pid_t next_pid() { return m_nextpid++; }
  262. public:
  263. process& emplace(pid_t ppid)
  264. {
  265. pid_t pid = next_pid();
  266. auto [ iter, inserted ] = m_procs.try_emplace(pid, pid, ppid);
  267. assert(inserted);
  268. if (try_find(ppid)) {
  269. bool success = false;
  270. std::tie(std::ignore, success) =
  271. find(ppid).children.insert(pid);
  272. assert(success);
  273. }
  274. return iter->second;
  275. }
  276. process& copy_from(process& proc)
  277. {
  278. pid_t pid = next_pid();
  279. auto [ iter, inserted ] = m_procs.try_emplace(pid, proc, pid);
  280. assert(inserted);
  281. proc.children.insert(pid);
  282. return iter->second;
  283. }
  284. constexpr void remove(pid_t pid)
  285. {
  286. make_children_orphans(pid);
  287. auto proc_iter = m_procs.find(pid);
  288. auto ppid = proc_iter->second.ppid;
  289. find(ppid).children.erase(pid);
  290. m_procs.erase(proc_iter);
  291. }
  292. constexpr bool try_find(pid_t pid) const
  293. { return m_procs.find(pid); }
  294. // if process doesn't exist, the behavior is undefined
  295. constexpr process& find(pid_t pid)
  296. {
  297. auto iter = m_procs.find(pid);
  298. assert(iter);
  299. return iter->second;
  300. }
  301. constexpr bool has_child(pid_t pid)
  302. {
  303. auto& proc = find(pid);
  304. return !proc.children.empty();
  305. }
  306. constexpr void make_children_orphans(pid_t pid)
  307. {
  308. auto& children = find(pid).children;
  309. auto& init_children = find(1).children;
  310. for (auto item : children) {
  311. init_children.insert(item);
  312. find(item).ppid = 1;
  313. }
  314. children.clear();
  315. }
  316. // the process MUST exist, or the behavior is undefined
  317. void send_signal(pid_t pid, kernel::sig_t signal)
  318. {
  319. auto& proc = this->find(pid);
  320. proc.signals.set(signal);
  321. }
  322. void send_signal_grp(pid_t pgid, kernel::sig_t signal)
  323. {
  324. for (auto& [ pid, proc ] : m_procs) {
  325. if (proc.pgid == pgid)
  326. proc.signals.set(signal);
  327. }
  328. }
  329. void kill(pid_t pid, int exit_code);
  330. };
  331. // TODO: lock and unlock
  332. class readyqueue final {
  333. public:
  334. using thread = kernel::tasks::thread;
  335. using list_type = std::list<thread*>;
  336. private:
  337. list_type m_thds;
  338. private:
  339. readyqueue(const readyqueue&) = delete;
  340. readyqueue(readyqueue&&) = delete;
  341. readyqueue& operator=(const readyqueue&) = delete;
  342. readyqueue& operator=(readyqueue&&) = delete;
  343. ~readyqueue() = delete;
  344. public:
  345. constexpr explicit readyqueue(void) = default;
  346. constexpr void push(thread* thd)
  347. { m_thds.push_back(thd); }
  348. constexpr thread* pop(void)
  349. {
  350. m_thds.remove_if([](thread* item) {
  351. return !item->attr.ready;
  352. });
  353. auto* retval = m_thds.front();
  354. m_thds.pop_front();
  355. return retval;
  356. }
  357. constexpr thread* query(void)
  358. {
  359. auto* thd = this->pop();
  360. this->push(thd);
  361. return thd;
  362. }
  363. constexpr void remove_all(thread* thd)
  364. { m_thds.remove(thd); }
  365. };
  366. void NORETURN init_scheduler(void);
  367. /// @return true if returned normally, false if being interrupted
  368. bool schedule(void);
  369. void NORETURN schedule_noreturn(void);
  370. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  371. {
  372. --*stack;
  373. **stack = val;
  374. return val;
  375. }
  376. void k_new_thread(void (*func)(void*), void* data);
  377. void NORETURN freeze(void);
  378. void NORETURN kill_current(int exit_code);
  379. void check_signal(void);