process.hpp 10 KB

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