process.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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/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 { std::exchange(val.esp, nullptr) }
  61. , pkstack { std::exchange(val.pkstack, 0) }
  62. , owner { std::exchange(val.owner, nullptr) }
  63. , attr { std::exchange(val.attr, {}) } { }
  64. inline thread(const thread& val)
  65. : owner { val.owner }
  66. , attr { val.attr }
  67. {
  68. alloc_kstack();
  69. }
  70. inline thread(const thread& thd, process* new_parent)
  71. : thread { thd }
  72. {
  73. owner = new_parent;
  74. }
  75. constexpr ~thread()
  76. {
  77. if (pkstack)
  78. free_kstack(pkstack);
  79. }
  80. };
  81. class thdlist {
  82. public:
  83. using list_type = types::list<thread>;
  84. private:
  85. list_type thds;
  86. public:
  87. constexpr thdlist(const thdlist& obj) = delete;
  88. constexpr thdlist(thdlist&& obj) = delete;
  89. constexpr thdlist& operator=(const thdlist& obj) = delete;
  90. constexpr thdlist& operator=(thdlist&& obj) = delete;
  91. constexpr thdlist(thdlist&& obj, process* new_parent)
  92. : thds { std::move(obj.thds) }
  93. {
  94. for (auto& thd : thds)
  95. thd.owner = new_parent;
  96. }
  97. explicit constexpr thdlist(void) = default;
  98. // implementation is below
  99. constexpr ~thdlist();
  100. template <typename... Args>
  101. constexpr thread& Emplace(Args&&... args)
  102. {
  103. return *thds.emplace_back(std::forward<Args>(args)...);
  104. }
  105. constexpr size_t size(void) const
  106. {
  107. return thds.size();
  108. }
  109. constexpr list_type& underlying_list(void)
  110. {
  111. return thds;
  112. }
  113. };
  114. class process {
  115. public:
  116. class filearr {
  117. public:
  118. using container_type = types::list<fs::file>;
  119. using array_type = types::map<int, container_type::iterator_type>;
  120. private:
  121. inline static container_type* files;
  122. array_type arr;
  123. public:
  124. inline static void init_global_file_container(void)
  125. {
  126. files = new container_type;
  127. }
  128. private:
  129. // iter should not be nullptr
  130. constexpr void _close(container_type::iterator_type iter)
  131. {
  132. if (iter->ref == 1) {
  133. if (iter->type == fs::file::types::pipe) {
  134. assert(iter->flags.read | iter->flags.write);
  135. if (iter->flags.read)
  136. iter->ptr.pp->close_read();
  137. else
  138. iter->ptr.pp->close_write();
  139. if (iter->ptr.pp->is_free())
  140. delete iter->ptr.pp;
  141. }
  142. files->erase(iter);
  143. } else
  144. --iter->ref;
  145. }
  146. constexpr int _next_fd(void) const
  147. {
  148. int fd = 0;
  149. for (auto [ item_fd, iter_file ] : arr) {
  150. if (item_fd == fd)
  151. ++fd;
  152. }
  153. return fd;
  154. }
  155. public:
  156. constexpr filearr(const filearr&) = delete;
  157. constexpr filearr& operator=(const filearr&) = delete;
  158. constexpr filearr& operator=(filearr&&) = delete;
  159. constexpr filearr(void) = default;
  160. constexpr filearr(filearr&& val)
  161. : arr { std::move(val.arr) }
  162. {
  163. }
  164. constexpr int dup(int old_fd)
  165. {
  166. return dup2(old_fd, _next_fd());
  167. }
  168. // TODO: the third parameter should be int flags
  169. // determining whether the fd should be closed
  170. // after exec() (FD_CLOEXEC)
  171. constexpr int dup2(int old_fd, int new_fd)
  172. {
  173. close(new_fd);
  174. auto iter = arr.find(old_fd);
  175. if (!iter)
  176. return -EBADF;
  177. auto [ _, iter_file ] = *iter;
  178. this->arr.insert(std::make_pair(new_fd, iter_file));
  179. ++iter_file->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(std::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->second;
  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(std::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(std::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(std::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->second);
  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. tty* control_tty;
  296. public:
  297. // if waitlist is not empty or mutex in cv_wait
  298. // is locked, its behavior is undefined
  299. constexpr process(process&& val)
  300. : mms(std::move(val.mms))
  301. , thds { std::move(val.thds), this }
  302. , attr { val.attr }
  303. , files(std::move(val.files))
  304. , pwd(std::move(val.pwd))
  305. , pid(val.pid)
  306. , ppid(val.ppid)
  307. , pgid(val.pgid)
  308. , sid(val.sid)
  309. {
  310. if (current_process == &val)
  311. current_process = this;
  312. }
  313. process(const process&);
  314. // this function is used for system initialization
  315. // DO NOT use this after the system is on
  316. explicit process(pid_t ppid);
  317. constexpr bool is_system(void) const
  318. { return attr.system; }
  319. constexpr bool is_zombie(void) const
  320. { return attr.zombie; }
  321. private:
  322. static inline pid_t max_pid;
  323. static inline pid_t alloc_pid(void)
  324. {
  325. return ++max_pid;
  326. }
  327. };
  328. class proclist final {
  329. public:
  330. using list_type = types::map<pid_t, process>;
  331. using child_index_type = types::hash_map<pid_t, types::list<pid_t>>;
  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. public:
  338. template <typename... Args>
  339. iterator_type emplace(Args&&... args)
  340. {
  341. process _proc(std::forward<Args>(args)...);
  342. auto pid = _proc.pid;
  343. auto ppid = _proc.ppid;
  344. auto iter = m_procs.insert(std::make_pair(pid, std::move(_proc)));
  345. auto children = m_child_idx.find(ppid);
  346. if (!children) {
  347. m_child_idx.emplace(ppid, types::list<pid_t> {});
  348. children = m_child_idx.find(ppid);
  349. }
  350. children->second.push_back(pid);
  351. return iter;
  352. }
  353. constexpr void remove(pid_t pid)
  354. {
  355. make_children_orphans(pid);
  356. auto proc_iter = m_procs.find(pid);
  357. auto ppid = proc_iter->second.ppid;
  358. auto& [ idx, parent_children ] = *m_child_idx.find(ppid);
  359. auto i = parent_children.find(pid);
  360. parent_children.erase(i);
  361. m_procs.erase(proc_iter);
  362. }
  363. constexpr bool try_find(pid_t pid) const
  364. {
  365. return !!m_procs.find(pid);
  366. }
  367. // if process doesn't exist, the behavior is undefined
  368. constexpr process& find(pid_t pid)
  369. {
  370. auto iter = m_procs.find(pid);
  371. assert(!!iter);
  372. return iter->second;
  373. }
  374. constexpr bool has_child(pid_t pid)
  375. {
  376. auto children = m_child_idx.find(pid);
  377. return children && !children->second.empty();
  378. }
  379. constexpr void make_children_orphans(pid_t pid)
  380. {
  381. auto children = m_child_idx.find(pid);
  382. auto init_children = m_child_idx.find(1);
  383. if (!children || !init_children)
  384. return;
  385. for (auto item : children->second) {
  386. init_children->second.push_back(item);
  387. find(item).ppid = 1;
  388. }
  389. m_child_idx.remove(children);
  390. }
  391. // the process MUST exist, or the behavior is undefined
  392. void send_signal(pid_t pid, kernel::sig_t signal)
  393. {
  394. auto proc = this->find(pid);
  395. proc.signals.set(signal);
  396. }
  397. void send_signal_grp(pid_t pgid, kernel::sig_t signal)
  398. {
  399. for (auto& [ pid, proc ] : m_procs) {
  400. if (proc.pgid == pgid)
  401. proc.signals.set(signal);
  402. }
  403. }
  404. void kill(pid_t pid, int exit_code);
  405. };
  406. class readyqueue final {
  407. public:
  408. using list_type = types::list<thread*>;
  409. using iterator_type = list_type::iterator_type;
  410. using const_iterator_type = list_type::const_iterator_type;
  411. private:
  412. list_type m_thds;
  413. private:
  414. readyqueue(const readyqueue&) = delete;
  415. readyqueue(readyqueue&&) = delete;
  416. readyqueue& operator=(const readyqueue&) = delete;
  417. readyqueue& operator=(readyqueue&&) = delete;
  418. ~readyqueue() = delete;
  419. public:
  420. constexpr explicit readyqueue(void) = default;
  421. constexpr void push(thread* thd)
  422. {
  423. m_thds.push_back(thd);
  424. }
  425. constexpr thread* pop(void)
  426. {
  427. auto iter = m_thds.begin();
  428. while (!((*iter)->attr.ready))
  429. iter = m_thds.erase(iter);
  430. auto* ptr = *iter;
  431. m_thds.erase(iter);
  432. return ptr;
  433. }
  434. constexpr thread* query(void)
  435. {
  436. auto* thd = this->pop();
  437. this->push(thd);
  438. return thd;
  439. }
  440. constexpr void remove_all(thread* thd)
  441. {
  442. auto iter = m_thds.find(thd);
  443. while (iter != m_thds.end()) {
  444. m_thds.erase(iter);
  445. iter = m_thds.find(thd);
  446. }
  447. }
  448. };
  449. void NORETURN init_scheduler(void);
  450. /// @return true if returned normally, false if being interrupted
  451. bool schedule(void);
  452. void NORETURN schedule_noreturn(void);
  453. constexpr uint32_t push_stack(uint32_t** stack, uint32_t val)
  454. {
  455. --*stack;
  456. **stack = val;
  457. return val;
  458. }
  459. // class thdlist
  460. constexpr thdlist::~thdlist()
  461. {
  462. for (auto iter = thds.begin(); iter != thds.end(); ++iter)
  463. readythds->remove_all(&iter);
  464. }
  465. void k_new_thread(void (*func)(void*), void* data);
  466. void NORETURN freeze(void);
  467. void NORETURN kill_current(int exit_code);
  468. void check_signal(void);