process.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. #pragma once
  2. #include <utility>
  3. #include <fcntl.h>
  4. #include <kernel/errno.h>
  5. #include <kernel/event/evtqueue.hpp>
  6. #include <kernel/interrupt.h>
  7. #include <kernel/mm.hpp>
  8. #include <kernel/signal.hpp>
  9. #include <kernel/task.h>
  10. #include <kernel/tty.hpp>
  11. #include <kernel/vfs.hpp>
  12. #include <stdint.h>
  13. #include <sys/types.h>
  14. #include <types/allocator.hpp>
  15. #include <types/cplusplus.hpp>
  16. #include <types/hash_map.hpp>
  17. #include <types/list.hpp>
  18. #include <types/map.hpp>
  19. #include <types/pair.hpp>
  20. #include <types/status.h>
  21. #include <types/string.hpp>
  22. #include <types/types.h>
  23. class process;
  24. struct thread;
  25. class proclist;
  26. class readyqueue;
  27. inline process* volatile current_process;
  28. inline thread* volatile current_thread;
  29. inline proclist* procs;
  30. inline readyqueue* readythds;
  31. inline tss32_t tss;
  32. struct process_attr {
  33. uint16_t system : 1;
  34. uint16_t zombie : 1 = 0;
  35. };
  36. struct thread_attr {
  37. uint32_t system : 1;
  38. uint32_t ready : 1;
  39. uint32_t wait : 1;
  40. };
  41. struct thread {
  42. private:
  43. void alloc_kstack(void);
  44. void free_kstack(uint32_t p);
  45. public:
  46. uint32_t* esp;
  47. uint32_t pkstack;
  48. process* owner;
  49. thread_attr attr;
  50. explicit inline thread(process* _owner, bool system)
  51. : owner { _owner }
  52. , attr {
  53. .system = system,
  54. .ready = 1,
  55. .wait = 0,
  56. }
  57. {
  58. alloc_kstack();
  59. }
  60. constexpr thread(thread&& val)
  61. : esp { std::exchange(val.esp, nullptr) }
  62. , pkstack { std::exchange(val.pkstack, 0) }
  63. , owner { std::exchange(val.owner, nullptr) }
  64. , attr { std::exchange(val.attr, {}) } { }
  65. inline thread(const thread& val)
  66. : owner { val.owner }
  67. , attr { val.attr }
  68. {
  69. alloc_kstack();
  70. }
  71. inline thread(const thread& thd, process* new_parent)
  72. : thread { thd }
  73. {
  74. owner = new_parent;
  75. }
  76. constexpr ~thread()
  77. {
  78. if (pkstack)
  79. free_kstack(pkstack);
  80. }
  81. };
  82. class thdlist {
  83. public:
  84. using list_type = types::list<thread>;
  85. private:
  86. list_type thds;
  87. public:
  88. constexpr thdlist(const thdlist& obj) = delete;
  89. constexpr thdlist(thdlist&& obj) = delete;
  90. constexpr thdlist& operator=(const thdlist& obj) = delete;
  91. constexpr thdlist& operator=(thdlist&& obj) = delete;
  92. constexpr thdlist(thdlist&& obj, process* new_parent)
  93. : thds { std::move(obj.thds) }
  94. {
  95. for (auto& thd : thds)
  96. thd.owner = new_parent;
  97. }
  98. explicit constexpr thdlist(void) = default;
  99. // implementation is below
  100. constexpr ~thdlist();
  101. template <typename... Args>
  102. constexpr thread& Emplace(Args&&... args)
  103. {
  104. return *thds.emplace_back(std::forward<Args>(args)...);
  105. }
  106. constexpr size_t size(void) const
  107. {
  108. return thds.size();
  109. }
  110. constexpr list_type& underlying_list(void)
  111. {
  112. return thds;
  113. }
  114. };
  115. class process {
  116. public:
  117. class filearr {
  118. public:
  119. using container_type = types::list<fs::file>;
  120. using array_type = types::map<int, container_type::iterator_type>;
  121. private:
  122. inline static container_type* files;
  123. array_type arr;
  124. public:
  125. inline static void init_global_file_container(void)
  126. {
  127. files = new container_type;
  128. }
  129. private:
  130. // iter should not be nullptr
  131. constexpr void _close(container_type::iterator_type iter)
  132. {
  133. if (iter->ref == 1) {
  134. if (iter->type == fs::file::types::pipe) {
  135. assert(iter->flags.read | iter->flags.write);
  136. if (iter->flags.read)
  137. iter->ptr.pp->close_read();
  138. else
  139. iter->ptr.pp->close_write();
  140. if (iter->ptr.pp->is_free())
  141. delete iter->ptr.pp;
  142. }
  143. files->erase(iter);
  144. } else
  145. --iter->ref;
  146. }
  147. constexpr int _next_fd(void) const
  148. {
  149. int fd = 0;
  150. for (auto [ item_fd, iter_file ] : arr) {
  151. if (item_fd == fd)
  152. ++fd;
  153. }
  154. return fd;
  155. }
  156. public:
  157. constexpr filearr(const filearr&) = delete;
  158. constexpr filearr& operator=(const filearr&) = delete;
  159. constexpr filearr& operator=(filearr&&) = delete;
  160. constexpr filearr(void) = default;
  161. constexpr filearr(filearr&& val)
  162. : arr { std::move(val.arr) }
  163. {
  164. }
  165. constexpr int dup(int old_fd)
  166. {
  167. return dup2(old_fd, _next_fd());
  168. }
  169. // TODO: the third parameter should be int flags
  170. // determining whether the fd should be closed
  171. // after exec() (FD_CLOEXEC)
  172. constexpr int dup2(int old_fd, int new_fd)
  173. {
  174. close(new_fd);
  175. auto iter = arr.find(old_fd);
  176. if (!iter)
  177. return -EBADF;
  178. this->arr.insert(types::make_pair(new_fd, iter->value));
  179. ++iter->value->ref;
  180. return new_fd;
  181. }
  182. constexpr void dup_all(const filearr& orig)
  183. {
  184. for (auto [ fd, iter_file ] : orig.arr) {
  185. this->arr.insert(types::make_pair(fd, iter_file));
  186. ++iter_file->ref;
  187. }
  188. }
  189. constexpr fs::file* operator[](int i) const
  190. {
  191. auto iter = arr.find(i);
  192. if (!iter)
  193. return nullptr;
  194. return &iter->value;
  195. }
  196. int pipe(int pipefd[2])
  197. {
  198. // TODO: set read/write flags
  199. auto* pipe = new fs::pipe;
  200. auto iter = files->emplace_back(fs::file {
  201. fs::file::types::pipe,
  202. { .pp = pipe },
  203. nullptr,
  204. 0,
  205. 1,
  206. {
  207. .read = 1,
  208. .write = 0,
  209. },
  210. });
  211. int fd = _next_fd();
  212. arr.insert(types::make_pair(fd, iter));
  213. // TODO: use copy_to_user()
  214. pipefd[0] = fd;
  215. iter = files->emplace_back(fs::file {
  216. fs::file::types::pipe,
  217. { .pp = pipe },
  218. nullptr,
  219. 0,
  220. 1,
  221. {
  222. .read = 0,
  223. .write = 1,
  224. },
  225. });
  226. fd = _next_fd();
  227. arr.insert(types::make_pair(fd, iter));
  228. // TODO: use copy_to_user()
  229. pipefd[1] = fd;
  230. return 0;
  231. }
  232. // TODO: file opening permissions check
  233. int open(const char* filename, uint32_t flags)
  234. {
  235. auto* dentry = fs::vfs_open(filename);
  236. if (!dentry) {
  237. errno = ENOTFOUND;
  238. return -1;
  239. }
  240. // check whether dentry is a file if O_DIRECTORY is set
  241. if ((flags & O_DIRECTORY) && !dentry->ind->flags.in.directory) {
  242. errno = ENOTDIR;
  243. return -1;
  244. }
  245. auto iter = files->emplace_back(fs::file {
  246. fs::file::types::ind,
  247. { .ind = dentry->ind },
  248. dentry->parent,
  249. 0,
  250. 1,
  251. {
  252. .read = !!(flags & (O_RDONLY | O_RDWR)),
  253. .write = !!(flags & (O_WRONLY | O_RDWR)),
  254. },
  255. });
  256. int fd = _next_fd();
  257. arr.insert(types::make_pair(fd, iter));
  258. return fd;
  259. }
  260. constexpr void close(int fd)
  261. {
  262. auto iter = arr.find(fd);
  263. if (iter) {
  264. _close(iter->value);
  265. arr.erase(iter);
  266. }
  267. }
  268. constexpr void close_all(void)
  269. {
  270. for (auto&& [ fd, file ] : arr)
  271. close(fd);
  272. }
  273. constexpr ~filearr()
  274. {
  275. close_all();
  276. }
  277. };
  278. struct wait_obj {
  279. pid_t pid;
  280. int code;
  281. };
  282. public:
  283. mutable kernel::mm_list mms;
  284. thdlist thds;
  285. kernel::cond_var cv_wait;
  286. types::list<wait_obj> waitlist;
  287. process_attr attr;
  288. filearr files;
  289. types::string<> pwd;
  290. kernel::signal_list signals;
  291. pid_t pid;
  292. pid_t ppid;
  293. pid_t pgid;
  294. pid_t sid;
  295. public:
  296. // if waitlist is not empty or mutex in cv_wait
  297. // is locked, its behavior is undefined
  298. process(process&& val);
  299. process(const process&);
  300. explicit process(pid_t ppid,
  301. bool system = true,
  302. types::string<>&& path = "/",
  303. kernel::signal_list&& sigs = {});
  304. constexpr bool is_system(void) const
  305. {
  306. return attr.system;
  307. }
  308. constexpr bool is_zombie(void) const
  309. {
  310. return attr.zombie;
  311. }
  312. private:
  313. static inline pid_t max_pid;
  314. static inline pid_t alloc_pid(void)
  315. {
  316. return ++max_pid;
  317. }
  318. };
  319. class proclist final {
  320. public:
  321. using list_type = types::map<pid_t, process>;
  322. using child_index_type = types::hash_map<pid_t, types::list<pid_t>>;
  323. using tty_index_type = types::map<pid_t, tty*>;
  324. using iterator_type = list_type::iterator_type;
  325. using const_iterator_type = list_type::const_iterator_type;
  326. private:
  327. list_type m_procs;
  328. child_index_type m_child_idx;
  329. tty_index_type m_tty_idx;
  330. public:
  331. template <typename... Args>
  332. iterator_type emplace(Args&&... args)
  333. {
  334. process _proc(std::forward<Args>(args)...);
  335. auto pid = _proc.pid;
  336. auto ppid = _proc.ppid;
  337. auto iter = m_procs.insert(types::make_pair(pid, std::move(_proc)));
  338. auto children = m_child_idx.find(ppid);
  339. if (!children) {
  340. m_child_idx.emplace(ppid, types::list<pid_t> {});
  341. children = m_child_idx.find(ppid);
  342. }
  343. children->value.push_back(pid);
  344. return iter;
  345. }
  346. constexpr void set_ctrl_tty(pid_t pid, tty* _tty)
  347. {
  348. auto iter = m_tty_idx.find(pid);
  349. _tty->set_pgrp(pid);
  350. if (iter) {
  351. iter->value = _tty;
  352. } else {
  353. m_tty_idx.insert(types::make_pair(pid, _tty));
  354. }
  355. }
  356. constexpr tty* get_ctrl_tty(pid_t pid)
  357. {
  358. auto iter = m_tty_idx.find(pid);
  359. if (!iter)
  360. return nullptr;
  361. return iter->value;
  362. }
  363. constexpr void remove(pid_t pid)
  364. {
  365. make_children_orphans(pid);
  366. auto proc_iter = m_procs.find(pid);
  367. auto ppid = proc_iter->value.ppid;
  368. auto& parent_children = m_child_idx.find(ppid)->value;
  369. auto i = parent_children.find(pid);
  370. parent_children.erase(i);
  371. m_procs.erase(proc_iter);
  372. }
  373. constexpr process* find(pid_t pid)
  374. {
  375. auto iter = m_procs.find(pid);
  376. // TODO: change this
  377. assert(!!iter);
  378. return &iter->value;
  379. }
  380. constexpr bool has_child(pid_t pid)
  381. {
  382. auto children = m_child_idx.find(pid);
  383. return children && !children->value.empty();
  384. }
  385. constexpr void make_children_orphans(pid_t pid)
  386. {
  387. auto children = m_child_idx.find(pid);
  388. auto init_children = m_child_idx.find(1);
  389. if (!children || !init_children)
  390. return;
  391. for (auto item : children->value) {
  392. init_children->value.push_back(item);
  393. find(item)->ppid = 1;
  394. }
  395. m_child_idx.remove(children);
  396. }
  397. void send_signal(pid_t pid, kernel::sig_t signal)
  398. {
  399. auto iter = this->find(pid);
  400. if (!iter)
  401. return;
  402. iter->signals.set(signal);
  403. }
  404. void send_signal_grp(pid_t pgid, kernel::sig_t signal)
  405. {
  406. for (auto& [ pid, proc ] : m_procs) {
  407. if (proc.pgid == pgid)
  408. proc.signals.set(signal);
  409. }
  410. }
  411. void kill(pid_t pid, int exit_code);
  412. };
  413. class readyqueue final {
  414. public:
  415. using list_type = types::list<thread*>;
  416. using iterator_type = list_type::iterator_type;
  417. using const_iterator_type = list_type::const_iterator_type;
  418. private:
  419. list_type m_thds;
  420. private:
  421. readyqueue(const readyqueue&) = delete;
  422. readyqueue(readyqueue&&) = delete;
  423. readyqueue& operator=(const readyqueue&) = delete;
  424. readyqueue& operator=(readyqueue&&) = delete;
  425. ~readyqueue() = delete;
  426. public:
  427. constexpr explicit readyqueue(void) = default;
  428. constexpr void push(thread* thd)
  429. {
  430. m_thds.push_back(thd);
  431. }
  432. constexpr thread* pop(void)
  433. {
  434. auto iter = m_thds.begin();
  435. while (!((*iter)->attr.ready))
  436. iter = m_thds.erase(iter);
  437. auto* ptr = *iter;
  438. m_thds.erase(iter);
  439. return ptr;
  440. }
  441. constexpr thread* query(void)
  442. {
  443. auto* thd = this->pop();
  444. this->push(thd);
  445. return thd;
  446. }
  447. constexpr void remove_all(thread* thd)
  448. {
  449. auto iter = m_thds.find(thd);
  450. while (iter != m_thds.end()) {
  451. m_thds.erase(iter);
  452. iter = m_thds.find(thd);
  453. }
  454. }
  455. };
  456. void NORETURN init_scheduler(void);
  457. /// @return true if returned normally, false if being interrupted
  458. bool schedule(void);
  459. void NORETURN schedule_noreturn(void);
  460. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  461. {
  462. --*stack;
  463. **stack = val;
  464. return val;
  465. }
  466. // class thdlist
  467. constexpr thdlist::~thdlist()
  468. {
  469. for (auto iter = thds.begin(); iter != thds.end(); ++iter)
  470. readythds->remove_all(&iter);
  471. }
  472. void k_new_thread(void (*func)(void*), void* data);
  473. void NORETURN freeze(void);
  474. void NORETURN kill_current(int exit_code);
  475. void check_signal(void);