process.hpp 14 KB

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